semantics_handle_call_expression.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // TODO: Propagate return types from callable.
  37. SemIR::TypeId 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 == SemIR::TypeId::Invalid) {
  41. type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
  42. }
  43. auto call_node_id = context.AddNode(SemIR::Node::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 HandleCallExpressionComma(Context& context, ParseTree::Node /*parse_node*/)
  49. -> bool {
  50. context.ParamOrArgComma(/*for_args=*/true);
  51. return true;
  52. }
  53. auto HandleCallExpressionStart(Context& context, ParseTree::Node parse_node)
  54. -> bool {
  55. auto name_id = context.node_stack().PopExpression();
  56. context.node_stack().Push(parse_node, name_id);
  57. context.ParamOrArgStart();
  58. return true;
  59. }
  60. } // namespace Carbon::Check