semantics_handle_call_expression.cpp 3.1 KB

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