handle_call_expr.cpp 3.9 KB

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