call.cpp 6.8 KB

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