call.cpp 4.1 KB

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