context.h 23 KB

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