semantics_parse_tree_handler.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const ParseTree& parse_tree,
  16. SemanticsIR& semantics,
  17. llvm::raw_ostream* vlog_stream)
  18. : tokens_(&tokens),
  19. parse_tree_(&parse_tree),
  20. semantics_(&semantics),
  21. vlog_stream_(vlog_stream) {}
  22. // Outputs the ParseTree information into SemanticsIR.
  23. auto Build() -> void;
  24. private:
  25. // Prints the node_stack_ on stack dumps.
  26. class PrettyStackTraceNodeStack;
  27. // Prints the node_block_stack_ on stack dumps.
  28. class PrettyStackTraceNodeBlockStack;
  29. struct TraversalStackEntry {
  30. ParseTree::Node parse_node;
  31. llvm::Optional<SemanticsNodeId> result_id;
  32. };
  33. // Adds an identifier for a DeclaredName node, returning its reference.
  34. auto AddIdentifier(ParseTree::Node decl_node) -> SemanticsIdentifierId;
  35. // Adds a node to the current block, returning the produced ID.
  36. auto AddNode(SemanticsNode node) -> SemanticsNodeId;
  37. // Pushes a parse tree onto the stack. Used when there is no IR generated by
  38. // the node.
  39. auto Push(ParseTree::Node parse_node) -> void;
  40. // Pushes a parse tree onto the stack, storing the SemanticsNode as the
  41. // result.
  42. auto Push(ParseTree::Node parse_node, SemanticsNode node) -> void;
  43. // Pops the top of the stack, verifying that it's the expected kind.
  44. auto Pop(ParseNodeKind pop_parse_kind) -> void;
  45. // Pops the top of the stack, returning the result_id. Must only be called for
  46. // nodes that have results.
  47. auto PopWithResult() -> SemanticsNodeId;
  48. // Pops the top of the stack, verifying that it's the expected kind and
  49. // returning the result_id. Must only be called for nodes that have results.
  50. auto PopWithResult(ParseNodeKind pop_parse_kind) -> SemanticsNodeId;
  51. // Parse node handlers.
  52. auto HandleDeclaredName(ParseTree::Node parse_node) -> void;
  53. auto HandleFunctionDefinition(ParseTree::Node parse_node) -> void;
  54. auto HandleFunctionDefinitionStart(ParseTree::Node parse_node) -> void;
  55. auto HandleInfixOperator(ParseTree::Node parse_node) -> void;
  56. auto HandleLiteral(ParseTree::Node parse_node) -> void;
  57. auto HandleParameterList(ParseTree::Node parse_node) -> void;
  58. auto HandleReturnStatement(ParseTree::Node parse_node) -> void;
  59. // Tokens for getting data on literals.
  60. const TokenizedBuffer* tokens_;
  61. // The file's parse tree.
  62. const ParseTree* parse_tree_;
  63. // The SemanticsIR being added to.
  64. SemanticsIR* semantics_;
  65. // Whether to print verbose output.
  66. llvm::raw_ostream* vlog_stream_;
  67. // The stack during Build. Will contain file-level parse nodes on return.
  68. llvm::SmallVector<TraversalStackEntry> node_stack_;
  69. // The stack of node blocks during build. Only updated on ParseTree nodes that
  70. // affect the stack.
  71. llvm::SmallVector<SemanticsNodeBlockId> node_block_stack_;
  72. };
  73. } // namespace Carbon
  74. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_PARSE_TREE_HANDLER_H_