context.h 23 KB

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