context.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_CHECK_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CONTEXT_H_
  6. #include "llvm/ADT/DenseMap.h"
  7. #include "llvm/ADT/DenseSet.h"
  8. #include "llvm/ADT/FoldingSet.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "toolchain/check/decl_name_stack.h"
  11. #include "toolchain/check/decl_state.h"
  12. #include "toolchain/check/inst_block_stack.h"
  13. #include "toolchain/check/node_stack.h"
  14. #include "toolchain/parse/tree.h"
  15. #include "toolchain/parse/tree_node_location_translator.h"
  16. #include "toolchain/sem_ir/file.h"
  17. #include "toolchain/sem_ir/ids.h"
  18. #include "toolchain/sem_ir/inst.h"
  19. namespace Carbon::Check {
  20. // Context and shared functionality for semantics handlers.
  21. class Context {
  22. public:
  23. using DiagnosticEmitter = Carbon::DiagnosticEmitter<Parse::NodeLocation>;
  24. using DiagnosticBuilder = DiagnosticEmitter::DiagnosticBuilder;
  25. // A scope in which `break` and `continue` can be used.
  26. struct BreakContinueScope {
  27. SemIR::InstBlockId break_target;
  28. SemIR::InstBlockId continue_target;
  29. };
  30. // A scope in which `return` can be used.
  31. struct ReturnScope {
  32. // The declaration from which we can return. Inside a function, this will
  33. // be a `FunctionDecl`.
  34. SemIR::InstId decl_id;
  35. // The value corresponding to the current `returned var`, if any. Will be
  36. // set and unset as `returned var`s are declared and go out of scope.
  37. SemIR::InstId returned_var = SemIR::InstId::Invalid;
  38. };
  39. // Stores references for work.
  40. explicit Context(const Lex::TokenizedBuffer& tokens,
  41. DiagnosticEmitter& emitter, const Parse::Tree& parse_tree,
  42. SemIR::File& sem_ir, llvm::raw_ostream* vlog_stream);
  43. // Marks an implementation TODO. Always returns false.
  44. auto TODO(Parse::NodeId parse_node, std::string label) -> bool;
  45. // Runs verification that the processing cleanly finished.
  46. auto VerifyOnFinish() -> void;
  47. // Adds an instruction to the current block, returning the produced ID.
  48. auto AddInst(SemIR::Inst inst) -> SemIR::InstId;
  49. // Adds an instruction to the constants block, returning the produced ID.
  50. auto AddConstantInst(SemIR::Inst inst) -> SemIR::InstId;
  51. // Pushes a parse tree node onto the stack, storing the SemIR::Inst as the
  52. // result.
  53. auto AddInstAndPush(Parse::NodeId parse_node, SemIR::Inst inst) -> void;
  54. // Adds a package's imports to name lookup, with all libraries together.
  55. // sem_irs will all be non-null; has_load_error must be used for any errors.
  56. auto AddPackageImports(Parse::NodeId import_node, IdentifierId package_id,
  57. llvm::ArrayRef<const SemIR::File*> sem_irs,
  58. bool has_load_error) -> void;
  59. // Adds a name to name lookup. Prints a diagnostic for name conflicts.
  60. auto AddNameToLookup(Parse::NodeId name_node, SemIR::NameId name_id,
  61. SemIR::InstId target_id) -> void;
  62. // Performs name lookup in a specified scope for a name appearing in a
  63. // declaration, returning the referenced instruction. If scope_id is invalid,
  64. // uses the current contextual scope.
  65. auto LookupNameInDecl(Parse::NodeId parse_node, SemIR::NameId name_id,
  66. SemIR::NameScopeId scope_id) -> SemIR::InstId;
  67. // Performs an unqualified name lookup, returning the referenced instruction.
  68. auto LookupUnqualifiedName(Parse::NodeId parse_node, SemIR::NameId name_id)
  69. -> SemIR::InstId;
  70. // Performs a name lookup in a specified scope, returning the referenced
  71. // instruction. Does not look into extended scopes. Returns an invalid
  72. // instruction if the name is not found.
  73. auto LookupNameInExactScope(SemIR::NameId name_id,
  74. const SemIR::NameScope& scope) -> SemIR::InstId;
  75. // Performs a qualified name lookup in a specified scope and in scopes that
  76. // it extends, returning the referenced instruction.
  77. auto LookupQualifiedName(Parse::NodeId parse_node, SemIR::NameId name_id,
  78. SemIR::NameScopeId scope_id, bool required = true)
  79. -> SemIR::InstId;
  80. // Prints a diagnostic for a duplicate name.
  81. auto DiagnoseDuplicateName(Parse::NodeId parse_node,
  82. SemIR::InstId prev_def_id) -> void;
  83. // Prints a diagnostic for a missing name.
  84. auto DiagnoseNameNotFound(Parse::NodeId parse_node, SemIR::NameId name_id)
  85. -> void;
  86. // Adds a note to a diagnostic explaining that a class is incomplete.
  87. auto NoteIncompleteClass(SemIR::ClassId class_id, DiagnosticBuilder& builder)
  88. -> void;
  89. // Pushes a scope onto scope_stack_. NameScopeId::Invalid is used for new
  90. // scopes. name_lookup_has_load_error is used to limit diagnostics when a
  91. // given namespace may contain a mix of both successful and failed name
  92. // imports.
  93. auto PushScope(SemIR::InstId scope_inst_id = SemIR::InstId::Invalid,
  94. SemIR::NameScopeId scope_id = SemIR::NameScopeId::Invalid,
  95. bool name_lookup_has_load_error = false) -> void;
  96. // Pops the top scope from scope_stack_, cleaning up names from name_lookup_.
  97. auto PopScope() -> void;
  98. // Pops scopes until we return to the specified scope index.
  99. auto PopToScope(ScopeIndex index) -> void;
  100. // Returns the scope index associated with the current scope.
  101. auto current_scope_index() const -> ScopeIndex {
  102. return current_scope().index;
  103. }
  104. // Returns the name scope associated with the current lexical scope, if any.
  105. auto current_scope_id() const -> SemIR::NameScopeId {
  106. return current_scope().scope_id;
  107. }
  108. // Returns true if currently at file scope.
  109. auto at_file_scope() const -> bool { return scope_stack_.size() == 1; }
  110. // Returns true if the current scope is of the specified kind.
  111. template <typename InstT>
  112. auto CurrentScopeIs() -> bool {
  113. auto current_scope_inst_id = current_scope().scope_inst_id;
  114. if (!current_scope_inst_id.is_valid()) {
  115. return false;
  116. }
  117. return sem_ir_->insts().Get(current_scope_inst_id).kind() == InstT::Kind;
  118. }
  119. // Returns the current scope, if it is of the specified kind. Otherwise,
  120. // returns nullopt.
  121. template <typename InstT>
  122. auto GetCurrentScopeAs() -> std::optional<InstT> {
  123. auto current_scope_inst_id = current_scope().scope_inst_id;
  124. if (!current_scope_inst_id.is_valid()) {
  125. return std::nullopt;
  126. }
  127. return insts().Get(current_scope_inst_id).TryAs<InstT>();
  128. }
  129. // If there is no `returned var` in scope, sets the given instruction to be
  130. // the current `returned var` and returns an invalid instruction ID. If there
  131. // is already a `returned var`, returns it instead.
  132. auto SetReturnedVarOrGetExisting(SemIR::InstId inst_id) -> SemIR::InstId;
  133. // Follows NameRef instructions to find the value named by a given
  134. // instruction.
  135. auto FollowNameRefs(SemIR::InstId inst_id) -> SemIR::InstId;
  136. // Gets the constant value of the given instruction, if it has one.
  137. auto GetConstantValue(SemIR::InstId inst_id) -> SemIR::InstId;
  138. // Adds a `Branch` instruction branching to a new instruction block, and
  139. // returns the ID of the new block. All paths to the branch target must go
  140. // through the current block, though not necessarily through this branch.
  141. auto AddDominatedBlockAndBranch(Parse::NodeId parse_node)
  142. -> SemIR::InstBlockId;
  143. // Adds a `Branch` instruction branching to a new instruction block with a
  144. // value, and returns the ID of the new block. All paths to the branch target
  145. // must go through the current block.
  146. auto AddDominatedBlockAndBranchWithArg(Parse::NodeId parse_node,
  147. SemIR::InstId arg_id)
  148. -> SemIR::InstBlockId;
  149. // Adds a `BranchIf` instruction branching to a new instruction block, and
  150. // returns the ID of the new block. All paths to the branch target must go
  151. // through the current block.
  152. auto AddDominatedBlockAndBranchIf(Parse::NodeId parse_node,
  153. SemIR::InstId cond_id)
  154. -> SemIR::InstBlockId;
  155. // Handles recovergence of control flow. Adds branches from the top
  156. // `num_blocks` on the instruction block stack to a new block, pops the
  157. // existing blocks, and pushes the new block onto the instruction block stack.
  158. auto AddConvergenceBlockAndPush(Parse::NodeId parse_node, int num_blocks)
  159. -> void;
  160. // Handles recovergence of control flow with a result value. Adds branches
  161. // from the top few blocks on the instruction block stack to a new block, pops
  162. // the existing blocks, and pushes the new block onto the instruction block
  163. // stack. The number of blocks popped is the size of `block_args`, and the
  164. // corresponding result values are the elements of `block_args`. Returns an
  165. // instruction referring to the result value.
  166. auto AddConvergenceBlockWithArgAndPush(
  167. Parse::NodeId parse_node, std::initializer_list<SemIR::InstId> block_args)
  168. -> SemIR::InstId;
  169. // Add the current code block to the enclosing function.
  170. // TODO: The parse_node is taken for expressions, which can occur in
  171. // non-function contexts. This should be refactored to support non-function
  172. // contexts, and parse_node removed.
  173. auto AddCurrentCodeBlockToFunction(
  174. Parse::NodeId parse_node = Parse::NodeId::Invalid) -> void;
  175. // Returns whether the current position in the current block is reachable.
  176. auto is_current_position_reachable() -> bool;
  177. // Canonicalizes a type which is tracked as a single instruction.
  178. auto CanonicalizeType(SemIR::InstId inst_id) -> SemIR::TypeId;
  179. // Handles canonicalization of struct types. This may create a new struct type
  180. // when it has a new structure, or reference an existing struct type when it
  181. // duplicates a prior type.
  182. //
  183. // Individual struct type fields aren't canonicalized because they may have
  184. // name conflicts or other diagnostics during creation, which can use the
  185. // parse node.
  186. auto CanonicalizeStructType(Parse::NodeId parse_node,
  187. SemIR::InstBlockId refs_id) -> SemIR::TypeId;
  188. // Handles canonicalization of tuple types. This may create a new tuple type
  189. // if the `type_ids` doesn't match an existing tuple type.
  190. auto CanonicalizeTupleType(Parse::NodeId parse_node,
  191. llvm::ArrayRef<SemIR::TypeId> type_ids)
  192. -> SemIR::TypeId;
  193. // Attempts to complete the type `type_id`. Returns `true` if the type is
  194. // complete, or `false` if it could not be completed. A complete type has
  195. // known object and value representations.
  196. //
  197. // If the type is not complete, `diagnoser` is invoked to diagnose the issue,
  198. // if a `diagnoser` is provided. The builder it returns will be annotated to
  199. // describe the reason why the type is not complete.
  200. auto TryToCompleteType(
  201. SemIR::TypeId type_id,
  202. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser =
  203. std::nullopt) -> bool;
  204. // Returns the type `type_id` as a complete type, or produces an incomplete
  205. // type error and returns an error type. This is a convenience wrapper around
  206. // TryToCompleteType.
  207. auto AsCompleteType(SemIR::TypeId type_id,
  208. llvm::function_ref<auto()->DiagnosticBuilder> diagnoser)
  209. -> SemIR::TypeId {
  210. return TryToCompleteType(type_id, diagnoser) ? type_id
  211. : SemIR::TypeId::Error;
  212. }
  213. // Gets a builtin type. The returned type will be complete.
  214. auto GetBuiltinType(SemIR::BuiltinKind kind) -> SemIR::TypeId;
  215. // Returns a pointer type whose pointee type is `pointee_type_id`.
  216. auto GetPointerType(Parse::NodeId parse_node, SemIR::TypeId pointee_type_id)
  217. -> SemIR::TypeId;
  218. // Removes any top-level `const` qualifiers from a type.
  219. auto GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId;
  220. // Starts handling parameters or arguments.
  221. auto ParamOrArgStart() -> void;
  222. // On a comma, pushes the entry. On return, the top of node_stack_ will be
  223. // start_kind.
  224. auto ParamOrArgComma() -> void;
  225. // Detects whether there's an entry to push from the end of a parameter or
  226. // argument list, and if so, moves it to the current parameter or argument
  227. // list. Does not pop the list. `start_kind` is the node kind at the start
  228. // of the parameter or argument list, and will be at the top of the parse node
  229. // stack when this function returns.
  230. auto ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void;
  231. // Pops the current parameter or argument list. Should only be called after
  232. // `ParamOrArgEndNoPop`.
  233. auto ParamOrArgPop() -> SemIR::InstBlockId;
  234. // Detects whether there's an entry to push. Pops and returns the argument
  235. // list. This is the same as `ParamOrArgEndNoPop` followed by `ParamOrArgPop`.
  236. auto ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::InstBlockId;
  237. // Saves a parameter from the top block in node_stack_ to the top block in
  238. // params_or_args_stack_.
  239. auto ParamOrArgSave(SemIR::InstId inst_id) -> void {
  240. params_or_args_stack_.AddInstId(inst_id);
  241. }
  242. // Prints information for a stack dump.
  243. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  244. // Get the Lex::TokenKind of a node for diagnostics.
  245. auto token_kind(Parse::NodeId parse_node) -> Lex::TokenKind {
  246. return tokens().GetKind(parse_tree().node_token(parse_node));
  247. }
  248. auto tokens() -> const Lex::TokenizedBuffer& { return *tokens_; }
  249. auto emitter() -> DiagnosticEmitter& { return *emitter_; }
  250. auto parse_tree() -> const Parse::Tree& { return *parse_tree_; }
  251. auto sem_ir() -> SemIR::File& { return *sem_ir_; }
  252. auto node_stack() -> NodeStack& { return node_stack_; }
  253. auto inst_block_stack() -> InstBlockStack& { return inst_block_stack_; }
  254. auto params_or_args_stack() -> InstBlockStack& {
  255. return params_or_args_stack_;
  256. }
  257. auto args_type_info_stack() -> InstBlockStack& {
  258. return args_type_info_stack_;
  259. }
  260. auto return_scope_stack() -> llvm::SmallVector<ReturnScope>& {
  261. return return_scope_stack_;
  262. }
  263. auto break_continue_stack() -> llvm::SmallVector<BreakContinueScope>& {
  264. return break_continue_stack_;
  265. }
  266. auto decl_name_stack() -> DeclNameStack& { return decl_name_stack_; }
  267. auto decl_state_stack() -> DeclStateStack& { return decl_state_stack_; }
  268. // Directly expose SemIR::File data accessors for brevity in calls.
  269. auto identifiers() -> StringStoreWrapper<IdentifierId>& {
  270. return sem_ir().identifiers();
  271. }
  272. auto ints() -> ValueStore<IntId>& { return sem_ir().ints(); }
  273. auto reals() -> ValueStore<RealId>& { return sem_ir().reals(); }
  274. auto string_literals() -> StringStoreWrapper<StringLiteralId>& {
  275. return sem_ir().string_literals();
  276. }
  277. auto functions() -> ValueStore<SemIR::FunctionId>& {
  278. return sem_ir().functions();
  279. }
  280. auto classes() -> ValueStore<SemIR::ClassId>& { return sem_ir().classes(); }
  281. auto interfaces() -> ValueStore<SemIR::InterfaceId>& {
  282. return sem_ir().interfaces();
  283. }
  284. auto cross_ref_irs() -> ValueStore<SemIR::CrossRefIRId>& {
  285. return sem_ir().cross_ref_irs();
  286. }
  287. auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  288. auto name_scopes() -> SemIR::NameScopeStore& {
  289. return sem_ir().name_scopes();
  290. }
  291. auto types() -> SemIR::TypeStore& { return sem_ir().types(); }
  292. auto type_blocks() -> SemIR::BlockValueStore<SemIR::TypeBlockId>& {
  293. return sem_ir().type_blocks();
  294. }
  295. auto insts() -> SemIR::InstStore& { return sem_ir().insts(); }
  296. auto inst_blocks() -> SemIR::InstBlockStore& {
  297. return sem_ir().inst_blocks();
  298. }
  299. auto constants() -> SemIR::ConstantStore& { return sem_ir().constants(); }
  300. private:
  301. // A FoldingSet node for a type.
  302. class TypeNode : public llvm::FastFoldingSetNode {
  303. public:
  304. explicit TypeNode(const llvm::FoldingSetNodeID& node_id,
  305. SemIR::TypeId type_id)
  306. : llvm::FastFoldingSetNode(node_id), type_id_(type_id) {}
  307. auto type_id() -> SemIR::TypeId { return type_id_; }
  308. private:
  309. SemIR::TypeId type_id_;
  310. };
  311. // An entry in scope_stack_.
  312. struct ScopeStackEntry {
  313. // The sequential index of this scope entry within the file.
  314. ScopeIndex index;
  315. // The instruction associated with this entry, if any. This can be one of:
  316. //
  317. // - A `ClassDecl`, for a class definition scope.
  318. // - A `FunctionDecl`, for the outermost scope in a function
  319. // definition.
  320. // - Invalid, for any other scope.
  321. SemIR::InstId scope_inst_id;
  322. // The name scope associated with this entry, if any.
  323. SemIR::NameScopeId scope_id;
  324. // The previous state of name_lookup_has_load_error_, restored on pop.
  325. bool prev_name_lookup_has_load_error;
  326. // Names which are registered with name_lookup_, and will need to be
  327. // unregistered when the scope ends.
  328. llvm::DenseSet<SemIR::NameId> names;
  329. // Whether a `returned var` was introduced in this scope, and needs to be
  330. // unregistered when the scope ends.
  331. bool has_returned_var = false;
  332. // TODO: This likely needs to track things which need to be destructed.
  333. };
  334. // A lookup result in the lexical lookup table `name_lookup_`.
  335. struct LexicalLookupResult {
  336. // The instruction that was added to lookup.
  337. SemIR::InstId inst_id;
  338. // The scope in which the instruction was added.
  339. ScopeIndex scope_index;
  340. };
  341. // Forms a canonical type ID for a type. This function is given two
  342. // callbacks:
  343. //
  344. // `profile_type(canonical_id)` is called to build a fingerprint for this
  345. // type. The ID should be distinct for all distinct type values with the same
  346. // `kind`.
  347. //
  348. // `make_inst()` is called to obtain a `SemIR::InstId` that describes the
  349. // type. It is only called if the type does not already exist, so can be used
  350. // to lazily build the `SemIR::Inst`. `make_inst()` is not permitted to
  351. // directly or indirectly canonicalize any types.
  352. auto CanonicalizeTypeImpl(
  353. SemIR::InstKind kind,
  354. llvm::function_ref<bool(llvm::FoldingSetNodeID& canonical_id)>
  355. profile_type,
  356. llvm::function_ref<SemIR::InstId()> make_inst) -> SemIR::TypeId;
  357. // Forms a canonical type ID for a type. If the type is new, adds the
  358. // instruction to the current block.
  359. auto CanonicalizeTypeAndAddInstIfNew(SemIR::Inst inst) -> SemIR::TypeId;
  360. // If the passed in instruction ID is a LazyImportRef, resolves it for use.
  361. // Called when name lookup intends to return an inst_id.
  362. auto ResolveIfLazyImportRef(SemIR::InstId inst_id) -> void;
  363. auto current_scope() -> ScopeStackEntry& { return scope_stack_.back(); }
  364. auto current_scope() const -> const ScopeStackEntry& {
  365. return scope_stack_.back();
  366. }
  367. // Tokens for getting data on literals.
  368. const Lex::TokenizedBuffer* tokens_;
  369. // Handles diagnostics.
  370. DiagnosticEmitter* emitter_;
  371. // The file's parse tree.
  372. const Parse::Tree* parse_tree_;
  373. // The SemIR::File being added to.
  374. SemIR::File* sem_ir_;
  375. // Whether to print verbose output.
  376. llvm::raw_ostream* vlog_stream_;
  377. // The stack during Build. Will contain file-level parse nodes on return.
  378. NodeStack node_stack_;
  379. // The stack of instruction blocks being used for general IR generation.
  380. InstBlockStack inst_block_stack_;
  381. // The stack of instruction blocks being used for per-element tracking of
  382. // instructions in parameter and argument instruction blocks. Versus
  383. // inst_block_stack_, an element will have 1 or more instructions in blocks in
  384. // inst_block_stack_, but only ever 1 instruction in blocks here.
  385. InstBlockStack params_or_args_stack_;
  386. // The stack of instruction blocks being used for type information while
  387. // processing arguments. This is used in parallel with params_or_args_stack_.
  388. // It's currently only used for struct literals, where we need to track names
  389. // for a type separate from the literal arguments.
  390. InstBlockStack args_type_info_stack_;
  391. // A stack of scopes from which we can `return`.
  392. llvm::SmallVector<ReturnScope> return_scope_stack_;
  393. // A stack of `break` and `continue` targets.
  394. llvm::SmallVector<BreakContinueScope> break_continue_stack_;
  395. // A stack for scope context.
  396. llvm::SmallVector<ScopeStackEntry> scope_stack_;
  397. // Information about non-lexical scopes. This is a subset of the entries and
  398. // the information in scope_stack_.
  399. llvm::SmallVector<std::pair<ScopeIndex, SemIR::NameScopeId>>
  400. non_lexical_scope_stack_;
  401. // The index of the next scope that will be pushed onto scope_stack_.
  402. ScopeIndex next_scope_index_ = ScopeIndex(0);
  403. // The stack used for qualified declaration name construction.
  404. DeclNameStack decl_name_stack_;
  405. // The stack of declarations that could have modifiers.
  406. DeclStateStack decl_state_stack_;
  407. // Maps identifiers to name lookup results. Values are a stack of name lookup
  408. // results in the ancestor scopes. This offers constant-time lookup of names,
  409. // regardless of how many scopes exist between the name declaration and
  410. // reference. The corresponding scope for each lookup result is tracked, so
  411. // that lexical lookup results can be interleaved with lookup results from
  412. // non-lexical scopes such as classes.
  413. //
  414. // Names which no longer have lookup results are erased.
  415. llvm::DenseMap<SemIR::NameId, llvm::SmallVector<LexicalLookupResult>>
  416. name_lookup_;
  417. // Whether name_lookup_ has load errors, updated whenever scope_stack_ is
  418. // pushed or popped.
  419. bool name_lookup_has_load_error_ = false;
  420. // Cache of the mapping from instructions to types, to avoid recomputing the
  421. // folding set ID.
  422. llvm::DenseMap<SemIR::InstId, SemIR::TypeId> canonical_types_;
  423. // Tracks the canonical representation of types that have been defined.
  424. llvm::FoldingSet<TypeNode> canonical_type_nodes_;
  425. // Storage for the nodes in canonical_type_nodes_. This stores in pointers so
  426. // that FoldingSet can have stable pointers.
  427. llvm::SmallVector<std::unique_ptr<TypeNode>> type_node_storage_;
  428. };
  429. // Parse node handlers. Returns false for unrecoverable errors.
  430. #define CARBON_PARSE_NODE_KIND(Name) \
  431. auto Handle##Name(Context& context, Parse::NodeId parse_node) -> bool;
  432. #include "toolchain/parse/node_kind.def"
  433. } // namespace Carbon::Check
  434. #endif // CARBON_TOOLCHAIN_CHECK_CONTEXT_H_