call.cpp 9.7 KB

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