call.cpp 11 KB

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