call.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/base/kind_switch.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/function.h"
  9. #include "toolchain/sem_ir/inst.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::Check {
  12. // Performs a call where the callee is the name of a generic class, such as
  13. // `Vector(i32)`.
  14. static auto PerformCallToGenericClass(Context& context, Parse::NodeId node_id,
  15. SemIR::InstId callee_id,
  16. SemIR::ClassId class_id,
  17. llvm::ArrayRef<SemIR::InstId> arg_ids)
  18. -> SemIR::InstId {
  19. auto& class_info = context.classes().Get(class_id);
  20. // Convert the arguments to match the parameters.
  21. auto converted_args_id = ConvertCallArgs(
  22. context, node_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids,
  23. /*return_storage_id=*/SemIR::InstId::Invalid, class_info.decl_id,
  24. class_info.implicit_param_refs_id, class_info.param_refs_id);
  25. return context.AddInst<SemIR::Call>(node_id,
  26. {.type_id = SemIR::TypeId::TypeType,
  27. .callee_id = callee_id,
  28. .args_id = converted_args_id});
  29. }
  30. // Performs a call where the callee is the name of a generic interface, such as
  31. // `AddWith(i32)`.
  32. // TODO: Refactor with PerformCallToGenericClass.
  33. static auto PerformCallToGenericInterface(Context& context,
  34. Parse::NodeId node_id,
  35. SemIR::InstId callee_id,
  36. SemIR::InterfaceId interface_id,
  37. llvm::ArrayRef<SemIR::InstId> arg_ids)
  38. -> SemIR::InstId {
  39. auto& interface_info = context.interfaces().Get(interface_id);
  40. // Convert the arguments to match the parameters.
  41. auto converted_args_id = ConvertCallArgs(
  42. context, node_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids,
  43. /*return_storage_id=*/SemIR::InstId::Invalid, interface_info.decl_id,
  44. interface_info.implicit_param_refs_id, interface_info.param_refs_id);
  45. return context.AddInst<SemIR::Call>(node_id,
  46. {.type_id = SemIR::TypeId::TypeType,
  47. .callee_id = callee_id,
  48. .args_id = converted_args_id});
  49. }
  50. auto PerformCall(Context& context, Parse::NodeId node_id,
  51. SemIR::InstId callee_id, llvm::ArrayRef<SemIR::InstId> arg_ids)
  52. -> SemIR::InstId {
  53. // Identify the function we're calling.
  54. auto callee_function = GetCalleeFunction(context.sem_ir(), callee_id);
  55. if (!callee_function.function_id.is_valid()) {
  56. auto type_inst =
  57. context.types().GetAsInst(context.insts().Get(callee_id).type_id());
  58. CARBON_KIND_SWITCH(type_inst) {
  59. case CARBON_KIND(SemIR::GenericClassType generic_class): {
  60. return PerformCallToGenericClass(context, node_id, callee_id,
  61. generic_class.class_id, arg_ids);
  62. }
  63. case CARBON_KIND(SemIR::GenericInterfaceType generic_interface): {
  64. return PerformCallToGenericInterface(context, node_id, callee_id,
  65. generic_interface.interface_id,
  66. arg_ids);
  67. }
  68. default: {
  69. if (!callee_function.is_error) {
  70. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  71. "Value of type `{0}` is not callable.",
  72. SemIR::TypeId);
  73. context.emitter().Emit(node_id, CallToNonCallable,
  74. context.insts().Get(callee_id).type_id());
  75. }
  76. return SemIR::InstId::BuiltinError;
  77. }
  78. }
  79. }
  80. auto& callable = context.functions().Get(callee_function.function_id);
  81. // For functions with an implicit return type, the return type is the empty
  82. // tuple type.
  83. SemIR::TypeId type_id = callable.declared_return_type(context.sem_ir());
  84. if (!type_id.is_valid()) {
  85. type_id = context.GetTupleType({});
  86. }
  87. // If there is a return slot, build storage for the result.
  88. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  89. {
  90. DiagnosticAnnotationScope annotate_diagnostics(
  91. &context.emitter(), [&](auto& builder) {
  92. CARBON_DIAGNOSTIC(IncompleteReturnTypeHere, Note,
  93. "Return type declared here.");
  94. builder.Note(callable.return_storage_id, IncompleteReturnTypeHere);
  95. });
  96. CheckFunctionReturnType(context, callee_id, callable);
  97. }
  98. switch (callable.return_slot) {
  99. case SemIR::Function::ReturnSlot::Present:
  100. // Tentatively put storage for a temporary in the function's return slot.
  101. // This will be replaced if necessary when we perform initialization.
  102. return_storage_id = context.AddInst<SemIR::TemporaryStorage>(
  103. node_id, {.type_id = type_id});
  104. break;
  105. case SemIR::Function::ReturnSlot::Absent:
  106. break;
  107. case SemIR::Function::ReturnSlot::Error:
  108. // Don't form an initializing expression with an incomplete type.
  109. type_id = SemIR::TypeId::Error;
  110. break;
  111. case SemIR::Function::ReturnSlot::NotComputed:
  112. CARBON_FATAL() << "Missing return slot category in call to " << callable;
  113. }
  114. // Convert the arguments to match the parameters.
  115. auto converted_args_id =
  116. ConvertCallArgs(context, node_id, callee_function.self_id, arg_ids,
  117. return_storage_id, callable.decl_id,
  118. callable.implicit_param_refs_id, callable.param_refs_id);
  119. auto call_inst_id =
  120. context.AddInst<SemIR::Call>(node_id, {.type_id = type_id,
  121. .callee_id = callee_id,
  122. .args_id = converted_args_id});
  123. return call_inst_id;
  124. }
  125. } // namespace Carbon::Check