call.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/check/function.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::Check {
  11. // Performs a call where the callee is the name of a generic class, such as
  12. // `Vector(i32)`.
  13. static auto PerformCallToGenericClass(Context& context, Parse::NodeId node_id,
  14. SemIR::ClassId class_id,
  15. llvm::ArrayRef<SemIR::InstId> arg_ids)
  16. -> SemIR::InstId {
  17. auto& class_info = context.classes().Get(class_id);
  18. // Convert the arguments to match the parameters.
  19. auto converted_args_id = ConvertCallArgs(
  20. context, node_id, /*self_id=*/SemIR::InstId::Invalid, arg_ids,
  21. /*return_storage_id=*/SemIR::InstId::Invalid, class_info.decl_id,
  22. class_info.implicit_param_refs_id, class_info.param_refs_id);
  23. return context.AddInst<SemIR::ClassType>(node_id,
  24. {.type_id = SemIR::TypeId::TypeType,
  25. .class_id = class_id,
  26. .args_id = converted_args_id});
  27. }
  28. auto PerformCall(Context& context, Parse::NodeId node_id,
  29. SemIR::InstId callee_id, llvm::ArrayRef<SemIR::InstId> arg_ids)
  30. -> SemIR::InstId {
  31. // Identify the function we're calling.
  32. auto callee_function = GetCalleeFunction(context.sem_ir(), callee_id);
  33. if (!callee_function.function_id.is_valid()) {
  34. if (auto generic_class = context.types().TryGetAs<SemIR::GenericClassType>(
  35. context.insts().Get(callee_id).type_id())) {
  36. return PerformCallToGenericClass(context, node_id,
  37. generic_class->class_id, arg_ids);
  38. }
  39. if (!callee_function.is_error) {
  40. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  41. "Value of type `{0}` is not callable.", SemIR::TypeId);
  42. context.emitter().Emit(node_id, CallToNonCallable,
  43. context.insts().Get(callee_id).type_id());
  44. }
  45. return SemIR::InstId::BuiltinError;
  46. }
  47. auto& callable = context.functions().Get(callee_function.function_id);
  48. // For functions with an implicit return type, the return type is the empty
  49. // tuple type.
  50. SemIR::TypeId type_id = callable.return_type_id;
  51. if (!type_id.is_valid()) {
  52. type_id = context.GetTupleType({});
  53. }
  54. // If there is a return slot, build storage for the result.
  55. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  56. {
  57. DiagnosticAnnotationScope annotate_diagnostics(
  58. &context.emitter(), [&](auto& builder) {
  59. CARBON_DIAGNOSTIC(IncompleteReturnTypeHere, Note,
  60. "Return type declared here.");
  61. builder.Note(callable.return_storage_id, IncompleteReturnTypeHere);
  62. });
  63. CheckFunctionReturnType(context, callee_id, callable);
  64. }
  65. switch (callable.return_slot) {
  66. case SemIR::Function::ReturnSlot::Present:
  67. // Tentatively put storage for a temporary in the function's return slot.
  68. // This will be replaced if necessary when we perform initialization.
  69. return_storage_id = context.AddInst<SemIR::TemporaryStorage>(
  70. node_id, {.type_id = callable.return_type_id});
  71. break;
  72. case SemIR::Function::ReturnSlot::Absent:
  73. break;
  74. case SemIR::Function::ReturnSlot::Error:
  75. // Don't form an initializing expression with an incomplete type.
  76. type_id = SemIR::TypeId::Error;
  77. break;
  78. case SemIR::Function::ReturnSlot::NotComputed:
  79. CARBON_FATAL() << "Missing return slot category in call to " << callable;
  80. }
  81. // Convert the arguments to match the parameters.
  82. auto converted_args_id =
  83. ConvertCallArgs(context, node_id, callee_function.self_id, arg_ids,
  84. return_storage_id, callable.decl_id,
  85. callable.implicit_param_refs_id, callable.param_refs_id);
  86. auto call_inst_id =
  87. context.AddInst<SemIR::Call>(node_id, {.type_id = type_id,
  88. .callee_id = callee_id,
  89. .args_id = converted_args_id});
  90. return call_inst_id;
  91. }
  92. } // namespace Carbon::Check