semantics_parse_tree_handler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "llvm/ADT/DenseMap.h"
  7. #include "llvm/ADT/DenseSet.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "toolchain/parser/parse_tree.h"
  10. #include "toolchain/semantics/semantics_ir.h"
  11. #include "toolchain/semantics/semantics_node.h"
  12. namespace Carbon {
  13. // Handles processing of a ParseTree for semantics.
  14. class SemanticsParseTreeHandler {
  15. public:
  16. // Stores references for work.
  17. explicit SemanticsParseTreeHandler(const TokenizedBuffer& tokens,
  18. TokenDiagnosticEmitter& emitter,
  19. const ParseTree& parse_tree,
  20. SemanticsIR& semantics,
  21. llvm::raw_ostream* vlog_stream)
  22. : tokens_(&tokens),
  23. emitter_(&emitter),
  24. parse_tree_(&parse_tree),
  25. semantics_(&semantics),
  26. vlog_stream_(vlog_stream) {}
  27. // Outputs the ParseTree information into SemanticsIR.
  28. auto Build() -> void;
  29. private:
  30. // Prints the node_stack_ on stack dumps.
  31. class PrettyStackTraceNodeStack;
  32. // Prints the node_block_stack_ on stack dumps.
  33. class PrettyStackTraceNodeBlockStack;
  34. // Provides DenseMapInfo for SemanticsStringId.
  35. struct SemanticsStringIdMapInfo {
  36. static inline auto getEmptyKey() -> SemanticsStringId {
  37. return SemanticsStringId(llvm::DenseMapInfo<int32_t>::getEmptyKey());
  38. }
  39. static inline auto getTombstoneKey() -> SemanticsStringId {
  40. return SemanticsStringId(llvm::DenseMapInfo<int32_t>::getTombstoneKey());
  41. }
  42. static auto getHashValue(const SemanticsStringId& val) -> unsigned {
  43. return llvm::DenseMapInfo<int32_t>::getHashValue(val.index);
  44. }
  45. static auto isEqual(const SemanticsStringId& lhs,
  46. const SemanticsStringId& rhs) -> bool {
  47. return lhs == rhs;
  48. }
  49. };
  50. // An entry in node_stack_.
  51. struct NodeStackEntry {
  52. ParseTree::Node parse_node;
  53. // The result_id may be invalid if there's no result.
  54. SemanticsNodeId result_id;
  55. };
  56. static_assert(sizeof(NodeStackEntry) == 8, "Unexpected NodeStackEntry size");
  57. // An entry in scope_stack_.
  58. struct ScopeStackEntry {
  59. // Names which are registered with name_lookup_, and will need to be
  60. // deregistered when the scope ends.
  61. llvm::DenseSet<SemanticsStringId, SemanticsStringIdMapInfo> names;
  62. // TODO: This likely needs to track things which need to be destructed.
  63. };
  64. // Adds a node to the current block, returning the produced ID.
  65. auto AddNode(SemanticsNode node) -> SemanticsNodeId;
  66. // Binds a DeclaredName to a target node with the given type.
  67. auto BindName(ParseTree::Node name_node, SemanticsNodeId type_id,
  68. SemanticsNodeId target_id) -> void;
  69. // Pushes a parse tree node onto the stack. Used when there is no IR generated
  70. // by the node.
  71. auto Push(ParseTree::Node parse_node) -> void;
  72. // Pushes a parse tree node onto the stack, storing the SemanticsNode as the
  73. // result.
  74. auto Push(ParseTree::Node parse_node, SemanticsNode node) -> void;
  75. // Pushes a parse tree node onto the stack with an already-built node ID.
  76. auto Push(ParseTree::Node parse_node, SemanticsNodeId node_id) -> void;
  77. // Pops the top of the stack, verifying that it's the expected kind.
  78. auto Pop(ParseNodeKind pop_parse_kind) -> void;
  79. // Pops the top of the stack, returning the result_id. Must only be called for
  80. // nodes that have results.
  81. auto PopWithResult() -> SemanticsNodeId;
  82. // Pops the top of the stack, verifying that it's the expected kind and
  83. // returning the result_id. Must only be called for nodes that have results.
  84. auto PopWithResult(ParseNodeKind pop_parse_kind) -> SemanticsNodeId;
  85. // Pops the top of the stack, verifying that it's the expected kind and
  86. // returning the result_id. Must only be called for nodes that have results.
  87. auto PopWithResultIf(ParseNodeKind pop_parse_kind)
  88. -> std::optional<SemanticsNodeId>;
  89. // Pushes a new scope onto scope_stack_.
  90. auto PushScope() -> void;
  91. // Pops the top scope from scope_stack_, cleaning up names from name_lookup_.
  92. auto PopScope() -> void;
  93. // Attempts a type conversion between arguments of the two arguments with
  94. // provided types, returning the result type. The result type will be invalid
  95. // for errors; this handles printing diagnostics.
  96. auto TryTypeConversion(ParseTree::Node parse_node, SemanticsNodeId lhs_id,
  97. SemanticsNodeId rhs_id, bool can_convert_lhs)
  98. -> SemanticsNodeId;
  99. // Parse node handlers.
  100. #define CARBON_PARSE_NODE_KIND(Name) \
  101. auto Handle##Name(ParseTree::Node parse_node)->void;
  102. #include "toolchain/parser/parse_node_kind.def"
  103. auto current_block_id() -> SemanticsNodeBlockId {
  104. return node_block_stack_.back();
  105. }
  106. auto current_scope() -> ScopeStackEntry& { return scope_stack_.back(); }
  107. // Tokens for getting data on literals.
  108. const TokenizedBuffer* tokens_;
  109. // Handles diagnostics.
  110. TokenDiagnosticEmitter* emitter_;
  111. // The file's parse tree.
  112. const ParseTree* parse_tree_;
  113. // The SemanticsIR being added to.
  114. SemanticsIR* semantics_;
  115. // Whether to print verbose output.
  116. llvm::raw_ostream* vlog_stream_;
  117. // The stack during Build. Will contain file-level parse nodes on return.
  118. llvm::SmallVector<NodeStackEntry> node_stack_;
  119. // The stack of node blocks during build. Only updated on ParseTree nodes that
  120. // affect the stack.
  121. llvm::SmallVector<SemanticsNodeBlockId> node_block_stack_;
  122. // A stack for scope context.
  123. llvm::SmallVector<ScopeStackEntry> scope_stack_;
  124. // Maps identifiers to name lookup results. Values are a stack of name lookup
  125. // results in the ancestor scopes. This offers constant-time lookup of names,
  126. // regardless of how many scopes exist between the name declaration and
  127. // reference.
  128. //
  129. // Names which no longer have lookup results are erased.
  130. llvm::DenseMap<SemanticsStringId, llvm::SmallVector<SemanticsNodeId>,
  131. SemanticsStringIdMapInfo>
  132. name_lookup_;
  133. };
  134. } // namespace Carbon
  135. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_PARSE_TREE_HANDLER_H_