semantics_handle_call_expression.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/semantics/semantics_context.h"
  5. #include "toolchain/semantics/semantics_node.h"
  6. namespace Carbon::Check {
  7. auto HandleCallExpression(Context& context, ParseTree::Node parse_node)
  8. -> bool {
  9. auto refs_id = context.ParamOrArgEnd(
  10. /*for_args=*/true, ParseNodeKind::CallExpressionStart);
  11. // TODO: Convert to call expression.
  12. auto [call_expr_parse_node, name_id] =
  13. context.node_stack()
  14. .PopWithParseNode<ParseNodeKind::CallExpressionStart>();
  15. auto name_node = context.semantics_ir().GetNode(name_id);
  16. if (name_node.kind() != SemIR::NodeKind::FunctionDeclaration) {
  17. // TODO: Work on error.
  18. context.TODO(parse_node, "Not a callable name");
  19. context.node_stack().Push(parse_node, name_id);
  20. return true;
  21. }
  22. auto function_id = name_node.GetAsFunctionDeclaration();
  23. const auto& callable = context.semantics_ir().GetFunction(function_id);
  24. CARBON_DIAGNOSTIC(NoMatchingCall, Error, "No matching callable was found.");
  25. auto diagnostic =
  26. context.emitter().Build(call_expr_parse_node, NoMatchingCall);
  27. if (!context.ImplicitAsForArgs(refs_id, name_node.parse_node(),
  28. callable.param_refs_id, &diagnostic)) {
  29. diagnostic.Emit();
  30. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  31. return true;
  32. }
  33. CARBON_CHECK(context.ImplicitAsForArgs(refs_id, name_node.parse_node(),
  34. callable.param_refs_id,
  35. /*diagnostic=*/nullptr));
  36. // For functions with an implicit return type, the return type is the empty
  37. // tuple type.
  38. SemIR::TypeId type_id = callable.return_type_id;
  39. if (!type_id.is_valid()) {
  40. type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
  41. }
  42. // If there is a return slot, add a corresponding argument.
  43. if (callable.return_slot_id.is_valid()) {
  44. if (refs_id == SemIR::NodeBlockId::Empty) {
  45. refs_id = context.semantics_ir().AddNodeBlock();
  46. }
  47. // Tentatively put a materialized temporary in the function's return slot.
  48. // This will be replaced if necessary when we perform initialization.
  49. auto return_slot_id =
  50. context.AddNode(SemIR::Node::MaterializeTemporary::Make(
  51. call_expr_parse_node, callable.return_type_id));
  52. context.semantics_ir().GetNodeBlock(refs_id).push_back(return_slot_id);
  53. }
  54. auto call_node_id = context.AddNode(SemIR::Node::Call::Make(
  55. call_expr_parse_node, type_id, refs_id, function_id));
  56. context.node_stack().Push(parse_node, call_node_id);
  57. return true;
  58. }
  59. auto HandleCallExpressionComma(Context& context, ParseTree::Node /*parse_node*/)
  60. -> bool {
  61. context.ParamOrArgComma(/*for_args=*/true);
  62. return true;
  63. }
  64. auto HandleCallExpressionStart(Context& context, ParseTree::Node parse_node)
  65. -> bool {
  66. auto name_id = context.node_stack().PopExpression();
  67. context.node_stack().Push(parse_node, name_id);
  68. context.ParamOrArgStart();
  69. return true;
  70. }
  71. } // namespace Carbon::Check