call.cpp 8.9 KB

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