call.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/deduce.h"
  9. #include "toolchain/check/function.h"
  10. #include "toolchain/sem_ir/entity_with_params_base.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/inst.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Check {
  15. // Resolves the callee expression in a call to a specific callee, or diagnoses
  16. // if no specific callee can be identified. This verifies the arity of the
  17. // callee and determines any compile-time arguments, but doesn't check that the
  18. // runtime arguments are convertible to the parameter types.
  19. //
  20. // `self_id` and `arg_ids` are the self argument and explicit arguments in the
  21. // call.
  22. //
  23. // Returns a SpecificId for the specific callee, or `nullopt` if an error has
  24. // been diagnosed.
  25. static auto ResolveCalleeInCall(Context& context, SemIR::LocId loc_id,
  26. const SemIR::EntityWithParamsBase& entity,
  27. llvm::StringLiteral entity_kind_for_diagnostic,
  28. SemIR::GenericId entity_generic_id,
  29. SemIR::SpecificId enclosing_specific_id,
  30. SemIR::InstId self_id,
  31. llvm::ArrayRef<SemIR::InstId> arg_ids)
  32. -> std::optional<SemIR::SpecificId> {
  33. CalleeParamsInfo callee_info(entity);
  34. // Check that the arity matches.
  35. auto params = context.inst_blocks().GetOrEmpty(callee_info.param_refs_id);
  36. if (arg_ids.size() != params.size()) {
  37. CARBON_DIAGNOSTIC(CallArgCountMismatch, Error,
  38. "{0} argument(s) passed to {1} expecting "
  39. "{2} argument(s).",
  40. int, llvm::StringLiteral, int);
  41. CARBON_DIAGNOSTIC(InCallToEntity, Note, "calling {0} declared here",
  42. llvm::StringLiteral);
  43. context.emitter()
  44. .Build(loc_id, CallArgCountMismatch, arg_ids.size(),
  45. entity_kind_for_diagnostic, params.size())
  46. .Note(callee_info.callee_loc, InCallToEntity,
  47. entity_kind_for_diagnostic)
  48. .Emit();
  49. return std::nullopt;
  50. }
  51. // Perform argument deduction.
  52. auto specific_id = SemIR::SpecificId::Invalid;
  53. if (entity_generic_id.is_valid()) {
  54. specific_id = DeduceGenericCallArguments(
  55. context, loc_id, entity_generic_id, enclosing_specific_id,
  56. callee_info.implicit_param_refs_id, callee_info.param_refs_id, self_id,
  57. arg_ids);
  58. if (!specific_id.is_valid()) {
  59. return std::nullopt;
  60. }
  61. }
  62. return specific_id;
  63. }
  64. // Performs a call where the callee is the name of a generic class, such as
  65. // `Vector(i32)`.
  66. static auto PerformCallToGenericClass(Context& context, SemIR::LocId loc_id,
  67. SemIR::ClassId class_id,
  68. SemIR::SpecificId enclosing_specific_id,
  69. llvm::ArrayRef<SemIR::InstId> arg_ids)
  70. -> SemIR::InstId {
  71. const auto& generic_class = context.classes().Get(class_id);
  72. auto callee_specific_id = ResolveCalleeInCall(
  73. context, loc_id, generic_class, "generic class", generic_class.generic_id,
  74. enclosing_specific_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids);
  75. if (!callee_specific_id) {
  76. return SemIR::InstId::BuiltinError;
  77. }
  78. return context.AddInst<SemIR::ClassType>(
  79. loc_id, {.type_id = SemIR::TypeId::TypeType,
  80. .class_id = class_id,
  81. .specific_id = *callee_specific_id});
  82. }
  83. // Performs a call where the callee is the name of a generic interface, such as
  84. // `AddWith(i32)`.
  85. static auto PerformCallToGenericInterface(
  86. Context& context, SemIR::LocId loc_id, SemIR::InterfaceId interface_id,
  87. SemIR::SpecificId enclosing_specific_id,
  88. llvm::ArrayRef<SemIR::InstId> arg_ids) -> SemIR::InstId {
  89. const auto& interface = context.interfaces().Get(interface_id);
  90. auto callee_specific_id = ResolveCalleeInCall(
  91. context, loc_id, interface, "generic interface", interface.generic_id,
  92. enclosing_specific_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids);
  93. if (!callee_specific_id) {
  94. return SemIR::InstId::BuiltinError;
  95. }
  96. return context.AddInst<SemIR::InterfaceType>(
  97. loc_id, {.type_id = SemIR::TypeId::TypeType,
  98. .interface_id = interface_id,
  99. .specific_id = *callee_specific_id});
  100. }
  101. auto PerformCall(Context& context, SemIR::LocId loc_id, SemIR::InstId callee_id,
  102. llvm::ArrayRef<SemIR::InstId> arg_ids) -> SemIR::InstId {
  103. // Identify the function we're calling.
  104. auto callee_function = GetCalleeFunction(context.sem_ir(), callee_id);
  105. if (!callee_function.function_id.is_valid()) {
  106. auto type_inst =
  107. context.types().GetAsInst(context.insts().Get(callee_id).type_id());
  108. CARBON_KIND_SWITCH(type_inst) {
  109. case CARBON_KIND(SemIR::GenericClassType generic_class): {
  110. return PerformCallToGenericClass(
  111. context, loc_id, generic_class.class_id,
  112. generic_class.enclosing_specific_id, arg_ids);
  113. }
  114. case CARBON_KIND(SemIR::GenericInterfaceType generic_interface): {
  115. return PerformCallToGenericInterface(
  116. context, loc_id, generic_interface.interface_id,
  117. generic_interface.enclosing_specific_id, arg_ids);
  118. }
  119. default: {
  120. if (!callee_function.is_error) {
  121. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  122. "value of type `{0}` is not callable",
  123. SemIR::TypeId);
  124. context.emitter().Emit(loc_id, CallToNonCallable,
  125. context.insts().Get(callee_id).type_id());
  126. }
  127. return SemIR::InstId::BuiltinError;
  128. }
  129. }
  130. }
  131. auto& callable = context.functions().Get(callee_function.function_id);
  132. // If the callee is a generic function, determine the generic argument values
  133. // for the call.
  134. auto callee_specific_id = ResolveCalleeInCall(
  135. context, loc_id, callable, "function", callable.generic_id,
  136. callee_function.specific_id, callee_function.self_id, arg_ids);
  137. if (!callee_specific_id) {
  138. return SemIR::InstId::BuiltinError;
  139. }
  140. // If there is a return slot, build storage for the result.
  141. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  142. SemIR::ReturnTypeInfo return_info = [&] {
  143. DiagnosticAnnotationScope annotate_diagnostics(
  144. &context.emitter(), [&](auto& builder) {
  145. CARBON_DIAGNOSTIC(IncompleteReturnTypeHere, Note,
  146. "return type declared here");
  147. builder.Note(callable.return_storage_id, IncompleteReturnTypeHere);
  148. });
  149. return CheckFunctionReturnType(context, callee_id, callable,
  150. *callee_specific_id);
  151. }();
  152. switch (return_info.init_repr.kind) {
  153. case SemIR::InitRepr::InPlace:
  154. // Tentatively put storage for a temporary in the function's return slot.
  155. // This will be replaced if necessary when we perform initialization.
  156. return_storage_id = context.AddInst<SemIR::TemporaryStorage>(
  157. loc_id, {.type_id = return_info.type_id});
  158. break;
  159. case SemIR::InitRepr::None:
  160. // For functions with an implicit return type, the return type is the
  161. // empty tuple type.
  162. if (!return_info.type_id.is_valid()) {
  163. return_info.type_id = context.GetTupleType({});
  164. }
  165. break;
  166. case SemIR::InitRepr::ByCopy:
  167. break;
  168. case SemIR::InitRepr::Incomplete:
  169. // Don't form an initializing expression with an incomplete type.
  170. // CheckFunctionReturnType will have diagnosed this for us if needed.
  171. return_info.type_id = SemIR::TypeId::Error;
  172. break;
  173. }
  174. // Convert the arguments to match the parameters.
  175. auto converted_args_id = ConvertCallArgs(
  176. context, loc_id, callee_function.self_id, arg_ids, return_storage_id,
  177. CalleeParamsInfo(callable), *callee_specific_id);
  178. auto call_inst_id =
  179. context.AddInst<SemIR::Call>(loc_id, {.type_id = return_info.type_id,
  180. .callee_id = callee_id,
  181. .args_id = converted_args_id});
  182. return call_inst_id;
  183. }
  184. } // namespace Carbon::Check