semantics_handle_paren.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <utility>
  5. #include "toolchain/semantics/semantics_context.h"
  6. namespace Carbon {
  7. auto SemanticsHandleParenExpression(SemanticsContext& context,
  8. ParseTree::Node parse_node) -> bool {
  9. auto value_id = context.node_stack().PopExpression();
  10. // ParamOrArgStart was called for tuple handling; clean up the ParamOrArg
  11. // support for non-tuple cases.
  12. context.ParamOrArgEnd(
  13. /*for_args=*/true, ParseNodeKind::ParenExpressionOrTupleLiteralStart);
  14. context.node_stack()
  15. .PopAndDiscardSoloParseNode<
  16. ParseNodeKind::ParenExpressionOrTupleLiteralStart>();
  17. context.node_stack().Push(parse_node, value_id);
  18. return true;
  19. }
  20. auto SemanticsHandleParenExpressionOrTupleLiteralStart(
  21. SemanticsContext& context, ParseTree::Node parse_node) -> bool {
  22. context.node_stack().Push(parse_node);
  23. context.ParamOrArgStart();
  24. return true;
  25. }
  26. auto SemanticsHandleTupleLiteralComma(SemanticsContext& context,
  27. ParseTree::Node /*parse_node*/) -> bool {
  28. context.ParamOrArgComma(/*for_args=*/true);
  29. return true;
  30. }
  31. auto SemanticsHandleTupleLiteral(SemanticsContext& context,
  32. ParseTree::Node parse_node) -> bool {
  33. auto refs_id = context.ParamOrArgEnd(
  34. /*for_args=*/true, ParseNodeKind::ParenExpressionOrTupleLiteralStart);
  35. context.node_stack()
  36. .PopAndDiscardSoloParseNode<
  37. ParseNodeKind::ParenExpressionOrTupleLiteralStart>();
  38. const auto& node_block = context.semantics_ir().GetNodeBlock(refs_id);
  39. llvm::SmallVector<SemanticsTypeId> type_ids;
  40. type_ids.reserve(node_block.size());
  41. for (auto node : node_block) {
  42. type_ids.push_back(context.semantics_ir().GetNode(node).type_id());
  43. }
  44. auto type_id = context.CanonicalizeTupleType(parse_node, std::move(type_ids));
  45. auto value_id = context.AddNode(
  46. SemanticsNode::TupleValue::Make(parse_node, type_id, refs_id));
  47. context.node_stack().Push(parse_node, value_id);
  48. return true;
  49. }
  50. } // namespace Carbon