call.cpp 6.8 KB

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