call.cpp 9.6 KB

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