call.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/function.h"
  9. #include "toolchain/check/generic.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/inst.h"
  12. #include "toolchain/sem_ir/typed_insts.h"
  13. namespace Carbon::Check {
  14. // Performs a call where the callee is the name of a generic class, such as
  15. // `Vector(i32)`.
  16. static auto PerformCallToGenericClass(Context& context, Parse::NodeId node_id,
  17. SemIR::InstId callee_id,
  18. SemIR::ClassId class_id,
  19. llvm::ArrayRef<SemIR::InstId> arg_ids)
  20. -> SemIR::InstId {
  21. auto& class_info = context.classes().Get(class_id);
  22. // TODO: Pass in information about the specific in which the generic class
  23. // name was found.
  24. // TODO: Perform argument deduction.
  25. auto specific_id = SemIR::SpecificId::Invalid;
  26. // Convert the arguments to match the parameters.
  27. auto converted_args_id = ConvertCallArgs(
  28. context, node_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids,
  29. /*return_storage_id=*/SemIR::InstId::Invalid, class_info, specific_id);
  30. return context.AddInst<SemIR::Call>(node_id,
  31. {.type_id = SemIR::TypeId::TypeType,
  32. .callee_id = callee_id,
  33. .args_id = converted_args_id});
  34. }
  35. // Performs a call where the callee is the name of a generic interface, such as
  36. // `AddWith(i32)`.
  37. // TODO: Refactor with PerformCallToGenericClass.
  38. static auto PerformCallToGenericInterface(Context& context,
  39. Parse::NodeId node_id,
  40. SemIR::InstId callee_id,
  41. SemIR::InterfaceId interface_id,
  42. llvm::ArrayRef<SemIR::InstId> arg_ids)
  43. -> SemIR::InstId {
  44. auto& interface_info = context.interfaces().Get(interface_id);
  45. // TODO: Pass in information about the specific in which the generic interface
  46. // name was found.
  47. // TODO: Perform argument deduction.
  48. auto specific_id = SemIR::SpecificId::Invalid;
  49. // Convert the arguments to match the parameters.
  50. auto converted_args_id = ConvertCallArgs(
  51. context, node_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids,
  52. /*return_storage_id=*/SemIR::InstId::Invalid, interface_info,
  53. specific_id);
  54. return context.AddInst<SemIR::Call>(node_id,
  55. {.type_id = SemIR::TypeId::TypeType,
  56. .callee_id = callee_id,
  57. .args_id = converted_args_id});
  58. }
  59. auto PerformCall(Context& context, Parse::NodeId node_id,
  60. SemIR::InstId callee_id, llvm::ArrayRef<SemIR::InstId> arg_ids)
  61. -> SemIR::InstId {
  62. // Identify the function we're calling.
  63. auto callee_function = GetCalleeFunction(context.sem_ir(), callee_id);
  64. if (!callee_function.function_id.is_valid()) {
  65. auto type_inst =
  66. context.types().GetAsInst(context.insts().Get(callee_id).type_id());
  67. CARBON_KIND_SWITCH(type_inst) {
  68. case CARBON_KIND(SemIR::GenericClassType generic_class): {
  69. return PerformCallToGenericClass(context, node_id, callee_id,
  70. generic_class.class_id, arg_ids);
  71. }
  72. case CARBON_KIND(SemIR::GenericInterfaceType generic_interface): {
  73. return PerformCallToGenericInterface(context, node_id, callee_id,
  74. generic_interface.interface_id,
  75. arg_ids);
  76. }
  77. default: {
  78. if (!callee_function.is_error) {
  79. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  80. "Value of type `{0}` is not callable.",
  81. SemIR::TypeId);
  82. context.emitter().Emit(node_id, CallToNonCallable,
  83. context.insts().Get(callee_id).type_id());
  84. }
  85. return SemIR::InstId::BuiltinError;
  86. }
  87. }
  88. }
  89. auto& callable = context.functions().Get(callee_function.function_id);
  90. // TODO: Properly determine the generic argument values for the call. For now,
  91. // we do so only if the function introduces no generic parameters beyond those
  92. // of the enclosing context.
  93. auto specific_id = SemIR::SpecificId::Invalid;
  94. if (callee_function.specific_id.is_valid()) {
  95. auto enclosing_args_id =
  96. context.specifics().Get(callee_function.specific_id).args_id;
  97. auto fn_params_id = context.generics().Get(callable.generic_id).bindings_id;
  98. if (context.inst_blocks().Get(fn_params_id).size() ==
  99. context.inst_blocks().Get(enclosing_args_id).size()) {
  100. specific_id =
  101. MakeSpecific(context, callable.generic_id, enclosing_args_id);
  102. }
  103. }
  104. // For functions with an implicit return type, the return type is the empty
  105. // tuple type.
  106. SemIR::TypeId type_id =
  107. callable.GetDeclaredReturnType(context.sem_ir(), specific_id);
  108. if (!type_id.is_valid()) {
  109. type_id = context.GetTupleType({});
  110. }
  111. // If there is a return slot, build storage for the result.
  112. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  113. SemIR::Function::ReturnSlot return_slot;
  114. {
  115. DiagnosticAnnotationScope annotate_diagnostics(
  116. &context.emitter(), [&](auto& builder) {
  117. CARBON_DIAGNOSTIC(IncompleteReturnTypeHere, Note,
  118. "Return type declared here.");
  119. builder.Note(callable.return_storage_id, IncompleteReturnTypeHere);
  120. });
  121. return_slot =
  122. CheckFunctionReturnType(context, callee_id, callable, specific_id);
  123. }
  124. switch (return_slot) {
  125. case SemIR::Function::ReturnSlot::Present:
  126. // Tentatively put storage for a temporary in the function's return slot.
  127. // This will be replaced if necessary when we perform initialization.
  128. return_storage_id = context.AddInst<SemIR::TemporaryStorage>(
  129. node_id, {.type_id = type_id});
  130. break;
  131. case SemIR::Function::ReturnSlot::Absent:
  132. break;
  133. case SemIR::Function::ReturnSlot::Error:
  134. // Don't form an initializing expression with an incomplete type.
  135. type_id = SemIR::TypeId::Error;
  136. break;
  137. case SemIR::Function::ReturnSlot::NotComputed:
  138. CARBON_FATAL() << "Missing return slot category in call to " << callable;
  139. }
  140. // Convert the arguments to match the parameters.
  141. auto converted_args_id =
  142. ConvertCallArgs(context, node_id, callee_function.self_id, arg_ids,
  143. return_storage_id, callable, specific_id);
  144. auto call_inst_id =
  145. context.AddInst<SemIR::Call>(node_id, {.type_id = type_id,
  146. .callee_id = callee_id,
  147. .args_id = converted_args_id});
  148. return call_inst_id;
  149. }
  150. } // namespace Carbon::Check