semantics_handle_call_expression.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. namespace Carbon {
  6. auto SemanticsHandleCallExpression(SemanticsContext& context,
  7. ParseTree::Node parse_node) -> bool {
  8. auto [ir_id, refs_id] = context.ParamOrArgEnd(
  9. /*for_args=*/true, ParseNodeKind::CallExpressionStart);
  10. // TODO: Convert to call expression.
  11. auto [call_expr_parse_node, name_id] =
  12. context.node_stack().PopForParseNodeAndNodeId(
  13. ParseNodeKind::CallExpressionStart);
  14. auto name_node = context.semantics().GetNode(name_id);
  15. if (name_node.kind() != SemanticsNodeKind::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 [_, callable_id] = name_node.GetAsFunctionDeclaration();
  22. auto callable = context.semantics().GetCallable(callable_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(ir_id, refs_id, name_node.parse_node(),
  27. callable.param_refs_id, &diagnostic)) {
  28. diagnostic.Emit();
  29. context.node_stack().Push(parse_node, SemanticsNodeId::BuiltinInvalidType);
  30. return true;
  31. }
  32. CARBON_CHECK(context.ImplicitAsForArgs(ir_id, refs_id, name_node.parse_node(),
  33. callable.param_refs_id,
  34. /*diagnostic=*/nullptr));
  35. auto call_id = context.semantics().AddCall({ir_id, refs_id});
  36. // TODO: Propagate return types from callable.
  37. auto call_node_id = context.AddNode(SemanticsNode::Call::Make(
  38. call_expr_parse_node, callable.return_type_id, call_id, callable_id));
  39. context.node_stack().Push(parse_node, call_node_id);
  40. return true;
  41. }
  42. auto SemanticsHandleCallExpressionComma(SemanticsContext& context,
  43. ParseTree::Node /*parse_node*/)
  44. -> bool {
  45. context.ParamOrArgComma(/*for_args=*/true);
  46. return true;
  47. }
  48. auto SemanticsHandleCallExpressionStart(SemanticsContext& context,
  49. ParseTree::Node parse_node) -> bool {
  50. auto name_id =
  51. context.node_stack().PopForNodeId(ParseNodeKind::NameReference);
  52. context.node_stack().Push(parse_node, name_id);
  53. context.ParamOrArgStart();
  54. return true;
  55. }
  56. } // namespace Carbon