call.cpp 11 KB

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