semantics_parse_tree_handler.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. std::optional<SemanticsNodeId> result_id;
  34. };
  35. // Adds an identifier for a DeclaredName node, returning its reference.
  36. auto AddIdentifier(ParseTree::Node decl_node) -> SemanticsIdentifierId;
  37. // Adds a node to the current block, returning the produced ID.
  38. auto AddNode(SemanticsNode node) -> SemanticsNodeId;
  39. // Pushes a parse tree onto the stack. Used when there is no IR generated by
  40. // the node.
  41. auto Push(ParseTree::Node parse_node) -> void;
  42. // Pushes a parse tree onto the stack, storing the SemanticsNode as the
  43. // result.
  44. auto Push(ParseTree::Node parse_node, SemanticsNode node) -> void;
  45. // Pops the top of the stack, verifying that it's the expected kind.
  46. auto Pop(ParseNodeKind pop_parse_kind) -> void;
  47. // Pops the top of the stack, returning the result_id. Must only be called for
  48. // nodes that have results.
  49. auto PopWithResult() -> SemanticsNodeId;
  50. // Pops the top of the stack, verifying that it's the expected kind and
  51. // returning the result_id. Must only be called for nodes that have results.
  52. auto PopWithResult(ParseNodeKind pop_parse_kind) -> SemanticsNodeId;
  53. // Parse node handlers.
  54. auto HandleDeclaredName(ParseTree::Node parse_node) -> void;
  55. auto HandleFunctionDefinition(ParseTree::Node parse_node) -> void;
  56. auto HandleFunctionDefinitionStart(ParseTree::Node parse_node) -> void;
  57. auto HandleInfixOperator(ParseTree::Node parse_node) -> void;
  58. auto HandleLiteral(ParseTree::Node parse_node) -> void;
  59. auto HandleParameterList(ParseTree::Node parse_node) -> void;
  60. auto HandleReturnStatement(ParseTree::Node parse_node) -> void;
  61. // Tokens for getting data on literals.
  62. const TokenizedBuffer* tokens_;
  63. // Handles diagnostics.
  64. TokenDiagnosticEmitter* emitter_;
  65. // The file's parse tree.
  66. const ParseTree* parse_tree_;
  67. // The SemanticsIR being added to.
  68. SemanticsIR* semantics_;
  69. // Whether to print verbose output.
  70. llvm::raw_ostream* vlog_stream_;
  71. // The stack during Build. Will contain file-level parse nodes on return.
  72. llvm::SmallVector<TraversalStackEntry> node_stack_;
  73. // The stack of node blocks during build. Only updated on ParseTree nodes that
  74. // affect the stack.
  75. llvm::SmallVector<SemanticsNodeBlockId> node_block_stack_;
  76. };
  77. } // namespace Carbon
  78. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_PARSE_TREE_HANDLER_H_