semantics_parse_tree_handler.h 3.2 KB

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