handle_call_expr.cpp 4.0 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 "llvm/ADT/ScopeExit.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleCallExprStart(Context& context, Parse::CallExprStartId parse_node)
  10. -> bool {
  11. auto name_id = context.node_stack().PopExpr();
  12. context.node_stack().Push(parse_node, name_id);
  13. context.ParamOrArgStart();
  14. return true;
  15. }
  16. auto HandleCallExprComma(Context& context,
  17. Parse::CallExprCommaId /*parse_node*/) -> bool {
  18. context.ParamOrArgComma();
  19. return true;
  20. }
  21. auto HandleCallExpr(Context& context, Parse::CallExprId parse_node) -> bool {
  22. // Process the final explicit call argument now, but leave the arguments
  23. // block on the stack until the end of this function.
  24. context.ParamOrArgEndNoPop(Parse::NodeKind::CallExprStart);
  25. auto discard_args_block = llvm::make_scope_exit(
  26. [&] { context.params_or_args_stack().PopAndDiscard(); });
  27. auto [call_expr_parse_node, callee_id] =
  28. context.node_stack().PopWithParseNode<Parse::NodeKind::CallExprStart>();
  29. auto diagnose_not_callable = [&, call_expr_parse_node = call_expr_parse_node,
  30. callee_id = callee_id] {
  31. auto callee_type_id = context.insts().Get(callee_id).type_id();
  32. if (callee_type_id != SemIR::TypeId::Error) {
  33. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  34. "Value of type `{0}` is not callable.", std::string);
  35. context.emitter().Emit(call_expr_parse_node, CallToNonCallable,
  36. context.sem_ir().StringifyType(callee_type_id));
  37. }
  38. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  39. return true;
  40. };
  41. // For a method call, pick out the `self` value.
  42. auto function_callee_id = callee_id;
  43. SemIR::InstId self_id = SemIR::InstId::Invalid;
  44. if (auto bound_method =
  45. context.insts().Get(callee_id).TryAs<SemIR::BoundMethod>()) {
  46. self_id = bound_method->object_id;
  47. function_callee_id = bound_method->function_id;
  48. }
  49. // Identify the function we're calling.
  50. auto function_decl_id = context.constant_values().Get(function_callee_id);
  51. if (!function_decl_id.is_constant()) {
  52. return diagnose_not_callable();
  53. }
  54. auto function_decl = context.insts()
  55. .Get(function_decl_id.inst_id())
  56. .TryAs<SemIR::FunctionDecl>();
  57. if (!function_decl) {
  58. return diagnose_not_callable();
  59. }
  60. auto function_id = function_decl->function_id;
  61. const auto& callable = context.functions().Get(function_id);
  62. // For functions with an implicit return type, the return type is the empty
  63. // tuple type.
  64. SemIR::TypeId type_id = callable.return_type_id;
  65. if (!type_id.is_valid()) {
  66. type_id = context.GetTupleType({});
  67. }
  68. // If there is a return slot, build storage for the result.
  69. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  70. if (callable.return_slot_id.is_valid()) {
  71. // Tentatively put storage for a temporary in the function's return slot.
  72. // This will be replaced if necessary when we perform initialization.
  73. return_storage_id =
  74. context.AddInst({call_expr_parse_node,
  75. SemIR::TemporaryStorage{callable.return_type_id}});
  76. }
  77. // Convert the arguments to match the parameters.
  78. auto converted_args_id =
  79. ConvertCallArgs(context, call_expr_parse_node, self_id,
  80. context.params_or_args_stack().PeekCurrentBlockContents(),
  81. return_storage_id, function_decl_id.inst_id(),
  82. callable.implicit_param_refs_id, callable.param_refs_id);
  83. auto call_inst_id =
  84. context.AddInst({call_expr_parse_node,
  85. SemIR::Call{type_id, callee_id, converted_args_id}});
  86. context.node_stack().Push(parse_node, call_inst_id);
  87. return true;
  88. }
  89. } // namespace Carbon::Check