semantics_parse_tree_handler.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_PARSE_TREE_HANDLER_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_PARSE_TREE_HANDLER_H_
  6. #include "toolchain/parser/parse_tree.h"
  7. #include "toolchain/semantics/semantics_ir.h"
  8. #include "toolchain/semantics/semantics_node.h"
  9. namespace Carbon {
  10. // Handles processing of a ParseTree for semantics.
  11. class SemanticsParseTreeHandler {
  12. public:
  13. // Stores references for work.
  14. explicit SemanticsParseTreeHandler(const TokenizedBuffer& tokens,
  15. TokenDiagnosticEmitter& emitter,
  16. const ParseTree& parse_tree,
  17. SemanticsIR& semantics,
  18. llvm::raw_ostream* vlog_stream)
  19. : tokens_(&tokens),
  20. emitter_(&emitter),
  21. parse_tree_(&parse_tree),
  22. semantics_(&semantics),
  23. vlog_stream_(vlog_stream) {}
  24. // Outputs the ParseTree information into SemanticsIR.
  25. auto Build() -> void;
  26. private:
  27. // Prints the node_stack_ on stack dumps.
  28. class PrettyStackTraceNodeStack;
  29. // Prints the node_block_stack_ on stack dumps.
  30. class PrettyStackTraceNodeBlockStack;
  31. struct TraversalStackEntry {
  32. ParseTree::Node parse_node;
  33. // The result_id may be invalid if there's no result.
  34. SemanticsNodeId result_id;
  35. };
  36. static_assert(sizeof(TraversalStackEntry) == 8,
  37. "Unexpected TraversalStackEntry size");
  38. // Adds an identifier for a DeclaredName node, returning its reference.
  39. auto AddIdentifier(ParseTree::Node decl_node) -> SemanticsIdentifierId;
  40. // Adds a node to the current block, returning the produced ID.
  41. auto AddNode(SemanticsNode node) -> SemanticsNodeId;
  42. // Pushes a parse tree node onto the stack. Used when there is no IR generated
  43. // by the node.
  44. auto Push(ParseTree::Node parse_node) -> void;
  45. // Pushes a parse tree node onto the stack, storing the SemanticsNode as the
  46. // result.
  47. auto Push(ParseTree::Node parse_node, SemanticsNode node) -> void;
  48. // Pushes a parse tree node onto the stack with an already-built node ID.
  49. auto Push(ParseTree::Node parse_node, SemanticsNodeId node_id) -> void;
  50. // Pops the top of the stack, verifying that it's the expected kind.
  51. auto Pop(ParseNodeKind pop_parse_kind) -> void;
  52. // Pops the top of the stack, returning the result_id. Must only be called for
  53. // nodes that have results.
  54. auto PopWithResult() -> SemanticsNodeId;
  55. // Pops the top of the stack, verifying that it's the expected kind and
  56. // returning the result_id. Must only be called for nodes that have results.
  57. auto PopWithResult(ParseNodeKind pop_parse_kind) -> SemanticsNodeId;
  58. // Parse node handlers.
  59. auto HandleDeclaredName(ParseTree::Node parse_node) -> void;
  60. auto HandleFunctionDefinition(ParseTree::Node parse_node) -> void;
  61. auto HandleFunctionDefinitionStart(ParseTree::Node parse_node) -> void;
  62. auto HandleInfixOperator(ParseTree::Node parse_node) -> void;
  63. auto HandleLiteral(ParseTree::Node parse_node) -> void;
  64. auto HandleParameterList(ParseTree::Node parse_node) -> void;
  65. auto HandlePatternBinding(ParseTree::Node parse_node) -> void;
  66. auto HandleReturnStatement(ParseTree::Node parse_node) -> void;
  67. auto HandleVariableDeclaration(ParseTree::Node parse_node) -> void;
  68. // Tokens for getting data on literals.
  69. const TokenizedBuffer* tokens_;
  70. // Handles diagnostics.
  71. TokenDiagnosticEmitter* emitter_;
  72. // The file's parse tree.
  73. const ParseTree* parse_tree_;
  74. // The SemanticsIR being added to.
  75. SemanticsIR* semantics_;
  76. // Whether to print verbose output.
  77. llvm::raw_ostream* vlog_stream_;
  78. // The stack during Build. Will contain file-level parse nodes on return.
  79. llvm::SmallVector<TraversalStackEntry> node_stack_;
  80. // The stack of node blocks during build. Only updated on ParseTree nodes that
  81. // affect the stack.
  82. llvm::SmallVector<SemanticsNodeBlockId> node_block_stack_;
  83. };
  84. } // namespace Carbon
  85. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_PARSE_TREE_HANDLER_H_