semantics_handle_call_expression.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 {
  7. auto SemanticsHandleCallExpression(SemanticsContext& context,
  8. ParseTree::Node parse_node) -> 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() != SemanticsNodeKind::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, SemanticsNodeId::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. // TODO: Propagate return types from callable.
  37. SemanticsTypeId type_id = callable.return_type_id;
  38. // For functions with an implicit return type, set the return type to empty
  39. // tuple type.
  40. if (type_id == SemanticsTypeId::Invalid) {
  41. type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
  42. }
  43. auto call_node_id = context.AddNode(SemanticsNode::Call::Make(
  44. call_expr_parse_node, type_id, refs_id, function_id));
  45. context.node_stack().Push(parse_node, call_node_id);
  46. return true;
  47. }
  48. auto SemanticsHandleCallExpressionComma(SemanticsContext& context,
  49. ParseTree::Node /*parse_node*/)
  50. -> bool {
  51. context.ParamOrArgComma(/*for_args=*/true);
  52. return true;
  53. }
  54. auto SemanticsHandleCallExpressionStart(SemanticsContext& context,
  55. ParseTree::Node parse_node) -> bool {
  56. auto name_id = context.node_stack().PopExpression();
  57. context.node_stack().Push(parse_node, name_id);
  58. context.ParamOrArgStart();
  59. return true;
  60. }
  61. } // namespace Carbon