semantics_parse_tree_handler.h 6.7 KB

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