call.cpp 9.3 KB

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