call.cpp 9.2 KB

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