call.cpp 9.4 KB

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