call.cpp 13 KB

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