call.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. IntAsSelect, IntAsSelect, IntAsSelect);
  54. CARBON_DIAGNOSTIC(
  55. InCallToEntity, Note,
  56. "calling {0:=0:function|=1:generic class|=2:generic interface}"
  57. " declared here",
  58. IntAsSelect);
  59. context.emitter()
  60. .Build(loc_id, CallArgCountMismatch, arg_ids.size(),
  61. static_cast<int>(entity_kind_for_diagnostic), params.size())
  62. .Note(entity.latest_decl_id(), InCallToEntity,
  63. static_cast<int>(entity_kind_for_diagnostic))
  64. .Emit();
  65. return std::nullopt;
  66. }
  67. // Perform argument deduction.
  68. auto specific_id = SemIR::SpecificId::None;
  69. if (entity.generic_id.has_value()) {
  70. specific_id = DeduceGenericCallArguments(
  71. context, loc_id, entity.generic_id, enclosing_specific_id, self_type_id,
  72. entity.implicit_param_patterns_id, entity.param_patterns_id, self_id,
  73. arg_ids);
  74. if (!specific_id.has_value()) {
  75. return std::nullopt;
  76. }
  77. }
  78. return specific_id;
  79. }
  80. // Performs a call where the callee is the name of a generic class, such as
  81. // `Vector(i32)`.
  82. static auto PerformCallToGenericClass(Context& context, SemIR::LocId loc_id,
  83. SemIR::ClassId class_id,
  84. SemIR::SpecificId enclosing_specific_id,
  85. llvm::ArrayRef<SemIR::InstId> arg_ids)
  86. -> SemIR::InstId {
  87. const auto& generic_class = context.classes().Get(class_id);
  88. auto callee_specific_id =
  89. ResolveCalleeInCall(context, loc_id, generic_class,
  90. EntityKind::GenericClass, enclosing_specific_id,
  91. /*self_type_id=*/SemIR::InstId::None,
  92. /*self_id=*/SemIR::InstId::None, arg_ids);
  93. if (!callee_specific_id) {
  94. return SemIR::ErrorInst::SingletonInstId;
  95. }
  96. return GetOrAddInst<SemIR::ClassType>(
  97. context, loc_id,
  98. {.type_id = SemIR::TypeType::SingletonTypeId,
  99. .class_id = class_id,
  100. .specific_id = *callee_specific_id});
  101. }
  102. // Performs a call where the callee is the name of a generic interface, such as
  103. // `AddWith(i32)`.
  104. static auto PerformCallToGenericInterface(
  105. Context& context, SemIR::LocId loc_id, SemIR::InterfaceId interface_id,
  106. SemIR::SpecificId enclosing_specific_id,
  107. llvm::ArrayRef<SemIR::InstId> arg_ids) -> SemIR::InstId {
  108. const auto& interface = context.interfaces().Get(interface_id);
  109. auto callee_specific_id =
  110. ResolveCalleeInCall(context, loc_id, interface,
  111. EntityKind::GenericInterface, enclosing_specific_id,
  112. /*self_type_id=*/SemIR::InstId::None,
  113. /*self_id=*/SemIR::InstId::None, arg_ids);
  114. if (!callee_specific_id) {
  115. return SemIR::ErrorInst::SingletonInstId;
  116. }
  117. return GetOrAddInst(
  118. context, loc_id,
  119. FacetTypeFromInterface(context, interface_id, *callee_specific_id));
  120. }
  121. auto PerformCall(Context& context, SemIR::LocId loc_id, SemIR::InstId callee_id,
  122. llvm::ArrayRef<SemIR::InstId> arg_ids) -> SemIR::InstId {
  123. // Identify the function we're calling.
  124. auto callee_function = GetCalleeFunction(context.sem_ir(), callee_id);
  125. if (!callee_function.function_id.has_value()) {
  126. auto type_inst =
  127. context.types().GetAsInst(context.insts().Get(callee_id).type_id());
  128. CARBON_KIND_SWITCH(type_inst) {
  129. case CARBON_KIND(SemIR::GenericClassType generic_class): {
  130. return PerformCallToGenericClass(
  131. context, loc_id, generic_class.class_id,
  132. generic_class.enclosing_specific_id, arg_ids);
  133. }
  134. case CARBON_KIND(SemIR::GenericInterfaceType generic_interface): {
  135. return PerformCallToGenericInterface(
  136. context, loc_id, generic_interface.interface_id,
  137. generic_interface.enclosing_specific_id, arg_ids);
  138. }
  139. default: {
  140. if (!callee_function.is_error) {
  141. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  142. "value of type {0} is not callable", TypeOfInstId);
  143. context.emitter().Emit(loc_id, CallToNonCallable, callee_id);
  144. }
  145. return SemIR::ErrorInst::SingletonInstId;
  146. }
  147. }
  148. }
  149. // If the callee is a generic function, determine the generic argument values
  150. // for the call.
  151. auto callee_specific_id = ResolveCalleeInCall(
  152. context, loc_id, context.functions().Get(callee_function.function_id),
  153. EntityKind::Function, callee_function.enclosing_specific_id,
  154. callee_function.self_type_id, callee_function.self_id, arg_ids);
  155. if (!callee_specific_id) {
  156. return SemIR::ErrorInst::SingletonInstId;
  157. }
  158. if (callee_specific_id->has_value()) {
  159. auto inner_callee_id = callee_id;
  160. if (auto bound_method =
  161. context.insts().TryGetAs<SemIR::BoundMethod>(callee_id)) {
  162. inner_callee_id = GetOrAddInst(
  163. context, context.insts().GetLocId(bound_method->function_decl_id),
  164. SemIR::SpecificFunction{
  165. .type_id = GetSingletonType(
  166. context, SemIR::SpecificFunctionType::SingletonInstId),
  167. .callee_id = bound_method->function_decl_id,
  168. .specific_id = *callee_specific_id});
  169. callee_id = GetOrAddInst<SemIR::BoundMethod>(
  170. context, loc_id,
  171. {.type_id = bound_method->type_id,
  172. .object_id = bound_method->object_id,
  173. .function_decl_id = inner_callee_id});
  174. } else {
  175. callee_id = GetOrAddInst(
  176. context, context.insts().GetLocId(callee_id),
  177. SemIR::SpecificFunction{
  178. .type_id = GetSingletonType(
  179. context, SemIR::SpecificFunctionType::SingletonInstId),
  180. .callee_id = callee_id,
  181. .specific_id = *callee_specific_id});
  182. inner_callee_id = callee_id;
  183. }
  184. if (callee_function.self_type_id.has_value()) {
  185. // This is an associated function, and will be required to be defined as
  186. // part of checking that the impl is complete.
  187. } else {
  188. context.definitions_required().push_back(inner_callee_id);
  189. }
  190. }
  191. // If there is a return slot, build storage for the result.
  192. SemIR::InstId return_slot_arg_id = SemIR::InstId::None;
  193. SemIR::ReturnTypeInfo return_info = [&] {
  194. auto& function = context.functions().Get(callee_function.function_id);
  195. DiagnosticAnnotationScope annotate_diagnostics(
  196. &context.emitter(), [&](auto& builder) {
  197. CARBON_DIAGNOSTIC(IncompleteReturnTypeHere, Note,
  198. "return type declared here");
  199. builder.Note(function.return_slot_pattern_id,
  200. IncompleteReturnTypeHere);
  201. });
  202. return CheckFunctionReturnType(context, loc_id, function,
  203. *callee_specific_id);
  204. }();
  205. switch (return_info.init_repr.kind) {
  206. case SemIR::InitRepr::InPlace:
  207. // Tentatively put storage for a temporary in the function's return slot.
  208. // This will be replaced if necessary when we perform initialization.
  209. return_slot_arg_id = AddInst<SemIR::TemporaryStorage>(
  210. context, loc_id, {.type_id = return_info.type_id});
  211. break;
  212. case SemIR::InitRepr::None:
  213. // For functions with an implicit return type, the return type is the
  214. // empty tuple type.
  215. if (!return_info.type_id.has_value()) {
  216. return_info.type_id = GetTupleType(context, {});
  217. }
  218. break;
  219. case SemIR::InitRepr::ByCopy:
  220. break;
  221. case SemIR::InitRepr::Incomplete:
  222. // Don't form an initializing expression with an incomplete type.
  223. // CheckFunctionReturnType will have diagnosed this for us if needed.
  224. return_info.type_id = SemIR::ErrorInst::SingletonTypeId;
  225. break;
  226. }
  227. // Convert the arguments to match the parameters.
  228. auto converted_args_id = ConvertCallArgs(
  229. context, loc_id, callee_function.self_id, arg_ids, return_slot_arg_id,
  230. context.functions().Get(callee_function.function_id),
  231. *callee_specific_id);
  232. auto call_inst_id = GetOrAddInst<SemIR::Call>(context, loc_id,
  233. {.type_id = return_info.type_id,
  234. .callee_id = callee_id,
  235. .args_id = converted_args_id});
  236. return call_inst_id;
  237. }
  238. } // namespace Carbon::Check