semantics_parse_tree_handler.h 5.7 KB

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