call.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/call.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::Check {
  10. auto PerformCall(Context& context, Parse::NodeId node_id,
  11. SemIR::InstId callee_id, llvm::ArrayRef<SemIR::InstId> arg_ids)
  12. -> SemIR::InstId {
  13. auto diagnose_not_callable = [&] {
  14. auto callee_type_id = context.insts().Get(callee_id).type_id();
  15. if (callee_type_id != SemIR::TypeId::Error) {
  16. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  17. "Value of type `{0}` is not callable.", SemIR::TypeId);
  18. context.emitter().Emit(node_id, CallToNonCallable, callee_type_id);
  19. }
  20. return SemIR::InstId::BuiltinError;
  21. };
  22. // For a method call, pick out the `self` value.
  23. auto function_callee_id = callee_id;
  24. SemIR::InstId self_id = SemIR::InstId::Invalid;
  25. if (auto bound_method =
  26. context.insts().Get(callee_id).TryAs<SemIR::BoundMethod>()) {
  27. self_id = bound_method->object_id;
  28. function_callee_id = bound_method->function_id;
  29. }
  30. // Identify the function we're calling.
  31. auto function_decl_id = context.constant_values().Get(function_callee_id);
  32. if (!function_decl_id.is_constant()) {
  33. return diagnose_not_callable();
  34. }
  35. auto function_decl = context.insts()
  36. .Get(function_decl_id.inst_id())
  37. .TryAs<SemIR::FunctionDecl>();
  38. if (!function_decl) {
  39. return diagnose_not_callable();
  40. }
  41. auto function_id = function_decl->function_id;
  42. const auto& callable = context.functions().Get(function_id);
  43. // For functions with an implicit return type, the return type is the empty
  44. // tuple type.
  45. SemIR::TypeId type_id = callable.return_type_id;
  46. if (!type_id.is_valid()) {
  47. type_id = context.GetTupleType({});
  48. }
  49. // If there is a return slot, build storage for the result.
  50. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  51. if (callable.return_slot_id.is_valid()) {
  52. // Tentatively put storage for a temporary in the function's return slot.
  53. // This will be replaced if necessary when we perform initialization.
  54. return_storage_id = context.AddInst(
  55. {node_id, SemIR::TemporaryStorage{callable.return_type_id}});
  56. }
  57. // Convert the arguments to match the parameters.
  58. auto converted_args_id =
  59. ConvertCallArgs(context, node_id, self_id, arg_ids, return_storage_id,
  60. function_decl_id.inst_id(),
  61. callable.implicit_param_refs_id, callable.param_refs_id);
  62. auto call_inst_id = context.AddInst(
  63. {node_id, SemIR::Call{type_id, callee_id, converted_args_id}});
  64. return call_inst_id;
  65. }
  66. } // namespace Carbon::Check