semantics_parse_tree_handler.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include "toolchain/semantics/semantics_node_stack.h"
  13. namespace Carbon {
  14. // Handles processing of a ParseTree for semantics.
  15. class SemanticsParseTreeHandler {
  16. public:
  17. // Stores references for work.
  18. explicit SemanticsParseTreeHandler(
  19. const TokenizedBuffer& tokens,
  20. DiagnosticEmitter<ParseTree::Node>& emitter, const ParseTree& parse_tree,
  21. SemanticsIR& semantics, 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. node_stack_(parse_tree, vlog_stream) {}
  28. // Outputs the ParseTree information into SemanticsIR.
  29. auto Build() -> void;
  30. private:
  31. // Prints the node_stack_ on stack dumps.
  32. class PrettyStackTraceNodeStack;
  33. // Prints the node_block_stack_ on stack dumps.
  34. class PrettyStackTraceNodeBlockStack;
  35. // Provides DenseMapInfo for SemanticsStringId.
  36. struct SemanticsStringIdMapInfo {
  37. static inline auto getEmptyKey() -> SemanticsStringId {
  38. return SemanticsStringId(llvm::DenseMapInfo<int32_t>::getEmptyKey());
  39. }
  40. static inline auto getTombstoneKey() -> SemanticsStringId {
  41. return SemanticsStringId(llvm::DenseMapInfo<int32_t>::getTombstoneKey());
  42. }
  43. static auto getHashValue(const SemanticsStringId& val) -> unsigned {
  44. return llvm::DenseMapInfo<int32_t>::getHashValue(val.index);
  45. }
  46. static auto isEqual(const SemanticsStringId& lhs,
  47. const SemanticsStringId& rhs) -> bool {
  48. return lhs == rhs;
  49. }
  50. };
  51. // An entry in scope_stack_.
  52. struct ScopeStackEntry {
  53. // Names which are registered with name_lookup_, and will need to be
  54. // deregistered when the scope ends.
  55. llvm::DenseSet<SemanticsStringId, SemanticsStringIdMapInfo> names;
  56. // TODO: This likely needs to track things which need to be destructed.
  57. };
  58. // Adds a node to the current block, returning the produced ID.
  59. auto AddNode(SemanticsNode node) -> SemanticsNodeId;
  60. // Pushes a parse tree node onto the stack, storing the SemanticsNode as the
  61. // result.
  62. auto AddNodeAndPush(ParseTree::Node parse_node, SemanticsNode node) -> void;
  63. // Adds a name to name lookup. This is typically done through BindName, but
  64. // can also be used to restore removed names.
  65. auto AddNameToLookup(SemanticsStringId name_id, SemanticsNodeId storage_id)
  66. -> void {
  67. name_lookup_[name_id].push_back(storage_id);
  68. }
  69. // Binds a DeclaredName to a target node with the given type.
  70. auto BindName(ParseTree::Node name_node, SemanticsNodeId type_id,
  71. SemanticsNodeId target_id) -> SemanticsStringId;
  72. // Pushes a new scope onto scope_stack_.
  73. auto PushScope() -> void;
  74. // Pops the top scope from scope_stack_, cleaning up names from name_lookup_.
  75. auto PopScope() -> void;
  76. // Attempts a type conversion between arguments of the two arguments with
  77. // provided types, returning the result type. The result type will be invalid
  78. // for errors; this handles printing diagnostics.
  79. auto TryTypeConversion(ParseTree::Node parse_node, SemanticsNodeId lhs_id,
  80. SemanticsNodeId rhs_id, bool can_convert_lhs)
  81. -> SemanticsNodeId;
  82. // Parse node handlers.
  83. #define CARBON_PARSE_NODE_KIND(Name) \
  84. auto Handle##Name(ParseTree::Node parse_node)->void;
  85. #include "toolchain/parser/parse_node_kind.def"
  86. auto current_block_id() -> SemanticsNodeBlockId {
  87. return node_block_stack_.back();
  88. }
  89. auto current_scope() -> ScopeStackEntry& { return scope_stack_.back(); }
  90. // Tokens for getting data on literals.
  91. const TokenizedBuffer* tokens_;
  92. // Handles diagnostics.
  93. DiagnosticEmitter<ParseTree::Node>* emitter_;
  94. // The file's parse tree.
  95. const ParseTree* parse_tree_;
  96. // The SemanticsIR being added to.
  97. SemanticsIR* semantics_;
  98. // Whether to print verbose output.
  99. llvm::raw_ostream* vlog_stream_;
  100. // The stack during Build. Will contain file-level parse nodes on return.
  101. SemanticsNodeStack node_stack_;
  102. // The stack of node blocks during build. Only updated on ParseTree nodes that
  103. // affect the stack.
  104. llvm::SmallVector<SemanticsNodeBlockId> node_block_stack_;
  105. // A stack for scope context.
  106. llvm::SmallVector<ScopeStackEntry> scope_stack_;
  107. // Maps identifiers to name lookup results. Values are a stack of name lookup
  108. // results in the ancestor scopes. This offers constant-time lookup of names,
  109. // regardless of how many scopes exist between the name declaration and
  110. // reference.
  111. //
  112. // Names which no longer have lookup results are erased.
  113. llvm::DenseMap<SemanticsStringId, llvm::SmallVector<SemanticsNodeId>,
  114. SemanticsStringIdMapInfo>
  115. name_lookup_;
  116. };
  117. } // namespace Carbon
  118. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_PARSE_TREE_HANDLER_H_