context.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 "common/map.h"
  7. #include "llvm/ADT/FoldingSet.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "toolchain/check/decl_introducer_state.h"
  10. #include "toolchain/check/decl_name_stack.h"
  11. #include "toolchain/check/diagnostic_helpers.h"
  12. #include "toolchain/check/generic_region_stack.h"
  13. #include "toolchain/check/global_init.h"
  14. #include "toolchain/check/inst_block_stack.h"
  15. #include "toolchain/check/node_stack.h"
  16. #include "toolchain/check/param_and_arg_refs_stack.h"
  17. #include "toolchain/check/scope_stack.h"
  18. #include "toolchain/parse/node_ids.h"
  19. #include "toolchain/parse/tree.h"
  20. #include "toolchain/parse/tree_and_subtrees.h"
  21. #include "toolchain/sem_ir/file.h"
  22. #include "toolchain/sem_ir/ids.h"
  23. #include "toolchain/sem_ir/import_ir.h"
  24. #include "toolchain/sem_ir/inst.h"
  25. namespace Carbon::Check {
  26. // Information about a scope in which we can perform name lookup.
  27. struct LookupScope {
  28. // The name scope in which names are searched.
  29. SemIR::NameScopeId name_scope_id;
  30. // The specific for the name scope, or `Invalid` if the name scope is not
  31. // defined by a generic or we should perform lookup into the generic itself.
  32. SemIR::SpecificId specific_id;
  33. };
  34. // A result produced by name lookup.
  35. struct LookupResult {
  36. // The specific in which the lookup result was found. `Invalid` if the result
  37. // was not found in a specific.
  38. SemIR::SpecificId specific_id;
  39. // The declaration that was found by name lookup.
  40. SemIR::InstId inst_id;
  41. };
  42. // Context and shared functionality for semantics handlers.
  43. class Context {
  44. public:
  45. using DiagnosticEmitter = Carbon::DiagnosticEmitter<SemIRLoc>;
  46. using DiagnosticBuilder = DiagnosticEmitter::DiagnosticBuilder;
  47. // Stores references for work.
  48. explicit Context(const Lex::TokenizedBuffer& tokens,
  49. DiagnosticEmitter& emitter, const Parse::Tree& parse_tree,
  50. llvm::function_ref<const Parse::TreeAndSubtrees&()>
  51. get_parse_tree_and_subtrees,
  52. SemIR::File& sem_ir, llvm::raw_ostream* vlog_stream);
  53. // Marks an implementation TODO. Always returns false.
  54. auto TODO(SemIRLoc loc, std::string label) -> bool;
  55. // Runs verification that the processing cleanly finished.
  56. auto VerifyOnFinish() -> void;
  57. // Adds an instruction to the current block, returning the produced ID.
  58. auto AddInst(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId;
  59. // Convenience for AddInst on specific instruction types.
  60. template <typename InstT, typename LocT>
  61. auto AddInst(LocT loc_id, InstT inst) -> SemIR::InstId {
  62. return AddInst(SemIR::LocIdAndInst(loc_id, inst));
  63. }
  64. // Adds an instruction in no block, returning the produced ID. Should be used
  65. // rarely.
  66. auto AddInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId;
  67. // Convenience for AddInstInNoBlock on specific instruction types.
  68. template <typename InstT, typename LocT>
  69. auto AddInstInNoBlock(LocT loc_id, InstT inst) -> SemIR::InstId {
  70. return AddInstInNoBlock(SemIR::LocIdAndInst(loc_id, inst));
  71. }
  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::LocIdAndInst loc_id_and_inst) -> SemIR::InstId;
  76. // Adds an instruction in no block, returning the produced ID. Should be used
  77. // rarely. The instruction is a placeholder that is expected to be replaced by
  78. // `ReplaceInstBeforeConstantUse`.
  79. auto AddPlaceholderInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst)
  80. -> SemIR::InstId;
  81. // Adds an instruction to the constants block, returning the produced ID.
  82. auto AddConstant(SemIR::Inst inst, bool is_symbolic) -> SemIR::ConstantId;
  83. // Pushes a parse tree node onto the stack, storing the SemIR::Inst as the
  84. // result. Only valid if the LocId is for a NodeId.
  85. template <typename InstT, typename LocT>
  86. auto AddInstAndPush(LocT loc_id, InstT inst) -> void {
  87. SemIR::LocIdAndInst arg(loc_id, inst);
  88. auto inst_id = AddInst(arg);
  89. node_stack_.Push(arg.loc_id.node_id(), inst_id);
  90. }
  91. // Replaces the instruction `inst_id` with `loc_id_and_inst`. The instruction
  92. // is required to not have been used in any constant evaluation, either
  93. // because it's newly created and entirely unused, or because it's only used
  94. // in a position that constant evaluation ignores, such as a return slot.
  95. auto ReplaceLocIdAndInstBeforeConstantUse(SemIR::InstId inst_id,
  96. SemIR::LocIdAndInst loc_id_and_inst)
  97. -> void;
  98. // Replaces the instruction `inst_id` with `inst`, not affecting location.
  99. // The instruction is required to not have been used in any constant
  100. // evaluation, either because it's newly created and entirely unused, or
  101. // because it's only used in a position that constant evaluation ignores, such
  102. // as a return slot.
  103. auto ReplaceInstBeforeConstantUse(SemIR::InstId inst_id, SemIR::Inst inst)
  104. -> void;
  105. // Sets only the parse node of an instruction. This is only used when setting
  106. // the parse node of an imported namespace. Versus
  107. // ReplaceInstBeforeConstantUse, it is safe to use after the namespace is used
  108. // in constant evaluation. It's exposed this way mainly so that `insts()` can
  109. // remain const.
  110. auto SetNamespaceNodeId(SemIR::InstId inst_id, Parse::NodeId node_id)
  111. -> void {
  112. sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
  113. }
  114. // Adds a name to name lookup. Prints a diagnostic for name conflicts.
  115. auto AddNameToLookup(SemIR::NameId name_id, SemIR::InstId target_id) -> void;
  116. // Performs name lookup in a specified scope for a name appearing in a
  117. // declaration, returning the referenced instruction. If scope_id is invalid,
  118. // uses the current contextual scope.
  119. auto LookupNameInDecl(SemIR::LocId loc_id, SemIR::NameId name_id,
  120. SemIR::NameScopeId scope_id) -> SemIR::InstId;
  121. // Performs an unqualified name lookup, returning the referenced instruction.
  122. auto LookupUnqualifiedName(Parse::NodeId node_id, SemIR::NameId name_id)
  123. -> LookupResult;
  124. // Performs a name lookup in a specified scope, returning the referenced
  125. // instruction. Does not look into extended scopes. Returns an invalid
  126. // instruction if the name is not found.
  127. auto LookupNameInExactScope(SemIRLoc loc, SemIR::NameId name_id,
  128. SemIR::NameScopeId scope_id,
  129. const SemIR::NameScope& scope) -> SemIR::InstId;
  130. // Performs a qualified name lookup in a specified scope and in scopes that
  131. // it extends, returning the referenced instruction.
  132. auto LookupQualifiedName(Parse::NodeId node_id, SemIR::NameId name_id,
  133. LookupScope scope, bool required = true)
  134. -> LookupResult;
  135. // Returns the instruction corresponding to a name in the core package, or
  136. // BuiltinError if not found.
  137. auto LookupNameInCore(SemIRLoc loc, llvm::StringRef name) -> SemIR::InstId;
  138. // Prints a diagnostic for a duplicate name.
  139. auto DiagnoseDuplicateName(SemIRLoc dup_def, SemIRLoc prev_def) -> void;
  140. // Prints a diagnostic for a missing name.
  141. auto DiagnoseNameNotFound(SemIRLoc loc, SemIR::NameId name_id) -> void;
  142. // Adds a note to a diagnostic explaining that a class is incomplete.
  143. auto NoteIncompleteClass(SemIR::ClassId class_id, DiagnosticBuilder& builder)
  144. -> void;
  145. // Adds a note to a diagnostic explaining that an interface is not defined.
  146. auto NoteUndefinedInterface(SemIR::InterfaceId interface_id,
  147. DiagnosticBuilder& builder) -> void;
  148. // Returns the current scope, if it is of the specified kind. Otherwise,
  149. // returns nullopt.
  150. template <typename InstT>
  151. auto GetCurrentScopeAs() -> std::optional<InstT> {
  152. return scope_stack().GetCurrentScopeAs<InstT>(sem_ir());
  153. }
  154. // Adds a `Branch` instruction branching to a new instruction block, and
  155. // returns the ID of the new block. All paths to the branch target must go
  156. // through the current block, though not necessarily through this branch.
  157. auto AddDominatedBlockAndBranch(Parse::NodeId node_id) -> SemIR::InstBlockId;
  158. // Adds a `Branch` instruction branching to a new instruction block with a
  159. // value, and returns the ID of the new block. All paths to the branch target
  160. // must go through the current block.
  161. auto AddDominatedBlockAndBranchWithArg(Parse::NodeId node_id,
  162. SemIR::InstId arg_id)
  163. -> SemIR::InstBlockId;
  164. // Adds a `BranchIf` instruction branching to a new instruction block, and
  165. // returns the ID of the new block. All paths to the branch target must go
  166. // through the current block.
  167. auto AddDominatedBlockAndBranchIf(Parse::NodeId node_id,
  168. SemIR::InstId cond_id)
  169. -> SemIR::InstBlockId;
  170. // Handles recovergence of control flow. Adds branches from the top
  171. // `num_blocks` on the instruction block stack to a new block, pops the
  172. // existing blocks, and pushes the new block onto the instruction block stack.
  173. auto AddConvergenceBlockAndPush(Parse::NodeId node_id, int num_blocks)
  174. -> void;
  175. // Handles recovergence of control flow with a result value. Adds branches
  176. // from the top few blocks on the instruction block stack to a new block, pops
  177. // the existing blocks, and pushes the new block onto the instruction block
  178. // stack. The number of blocks popped is the size of `block_args`, and the
  179. // corresponding result values are the elements of `block_args`. Returns an
  180. // instruction referring to the result value.
  181. auto AddConvergenceBlockWithArgAndPush(
  182. Parse::NodeId node_id, std::initializer_list<SemIR::InstId> block_args)
  183. -> SemIR::InstId;
  184. // Sets the constant value of a block argument created as the result of a
  185. // branch. `select_id` should be a `BlockArg` that selects between two
  186. // values. `cond_id` is the condition, `if_false` is the value to use if the
  187. // condition is false, and `if_true` is the value to use if the condition is
  188. // true. We don't track enough information in the `BlockArg` inst for
  189. // `TryEvalInst` to do this itself.
  190. auto SetBlockArgResultBeforeConstantUse(SemIR::InstId select_id,
  191. SemIR::InstId cond_id,
  192. SemIR::InstId if_true,
  193. SemIR::InstId if_false) -> void;
  194. // Add the current code block to the enclosing function.
  195. // TODO: The node_id is taken for expressions, which can occur in
  196. // non-function contexts. This should be refactored to support non-function
  197. // contexts, and node_id removed.
  198. auto AddCurrentCodeBlockToFunction(
  199. Parse::NodeId node_id = Parse::NodeId::Invalid) -> void;
  200. // Returns whether the current position in the current block is reachable.
  201. auto is_current_position_reachable() -> bool;
  202. // Returns the type ID for a constant of type `type`.
  203. auto GetTypeIdForTypeConstant(SemIR::ConstantId constant_id) -> SemIR::TypeId;
  204. // Returns the type ID for an instruction whose constant value is of type
  205. // `type`.
  206. auto GetTypeIdForTypeInst(SemIR::InstId inst_id) -> SemIR::TypeId {
  207. return GetTypeIdForTypeConstant(constant_values().Get(inst_id));
  208. }
  209. // Attempts to complete the type `type_id`. Returns `true` if the type is
  210. // complete, or `false` if it could not be completed. A complete type has
  211. // known object and value representations.
  212. //
  213. // If the type is not complete, `diagnoser` is invoked to diagnose the issue,
  214. // if a `diagnoser` is provided. The builder it returns will be annotated to
  215. // describe the reason why the type is not complete.
  216. auto TryToCompleteType(
  217. SemIR::TypeId type_id,
  218. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser =
  219. std::nullopt) -> bool;
  220. // Attempts to complete and define the type `type_id`. Returns `true` if the
  221. // type is defined, or `false` if no definition is available. A defined type
  222. // has known members.
  223. //
  224. // This is the same as `TryToCompleteType` except for interfaces, which are
  225. // complete before they are fully defined.
  226. auto TryToDefineType(
  227. SemIR::TypeId type_id,
  228. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser =
  229. std::nullopt) -> bool;
  230. // Returns the type `type_id` as a complete type, or produces an incomplete
  231. // type error and returns an error type. This is a convenience wrapper around
  232. // TryToCompleteType.
  233. auto AsCompleteType(SemIR::TypeId type_id,
  234. llvm::function_ref<auto()->DiagnosticBuilder> diagnoser)
  235. -> SemIR::TypeId {
  236. return TryToCompleteType(type_id, diagnoser) ? type_id
  237. : SemIR::TypeId::Error;
  238. }
  239. // TODO: Consider moving these `Get*Type` functions to a separate class.
  240. // Gets the type for the name of an associated entity.
  241. auto GetAssociatedEntityType(SemIR::InterfaceId interface_id,
  242. SemIR::TypeId entity_type_id) -> SemIR::TypeId;
  243. // Gets a builtin type. The returned type will be complete.
  244. auto GetBuiltinType(SemIR::BuiltinInstKind kind) -> SemIR::TypeId;
  245. // Gets a function type. The returned type will be complete.
  246. auto GetFunctionType(SemIR::FunctionId fn_id, SemIR::SpecificId specific_id)
  247. -> SemIR::TypeId;
  248. // Gets a generic class type, which is the type of a name of a generic class,
  249. // such as the type of `Vector` given `class Vector(T:! type)`. The returned
  250. // type will be complete.
  251. auto GetGenericClassType(SemIR::ClassId class_id) -> SemIR::TypeId;
  252. // Gets a generic interface type, which is the type of a name of a generic
  253. // interface, such as the type of `AddWith` given
  254. // `interface AddWith(T:! type)`. The returned type will be complete.
  255. auto GetGenericInterfaceType(SemIR::InterfaceId interface_id)
  256. -> SemIR::TypeId;
  257. // Returns a pointer type whose pointee type is `pointee_type_id`.
  258. auto GetPointerType(SemIR::TypeId pointee_type_id) -> SemIR::TypeId;
  259. // Returns a struct type with the given fields, which should be a block of
  260. // `StructTypeField`s.
  261. auto GetStructType(SemIR::InstBlockId refs_id) -> SemIR::TypeId;
  262. // Returns a tuple type with the given element types.
  263. auto GetTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids) -> SemIR::TypeId;
  264. // Returns an unbound element type.
  265. auto GetUnboundElementType(SemIR::TypeId class_type_id,
  266. SemIR::TypeId element_type_id) -> SemIR::TypeId;
  267. // Removes any top-level `const` qualifiers from a type.
  268. auto GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId;
  269. // Adds an exported name.
  270. auto AddExport(SemIR::InstId inst_id) -> void { exports_.push_back(inst_id); }
  271. auto Finalize() -> void;
  272. // Sets the total number of IRs which exist. This is used to prepare a map
  273. // from IR to imported IR.
  274. auto SetTotalIRCount(int num_irs) -> void {
  275. CARBON_CHECK(check_ir_map_.empty())
  276. << "SetTotalIRCount is only called once";
  277. check_ir_map_.resize(num_irs, SemIR::ImportIRId::Invalid);
  278. }
  279. // Returns the imported IR ID for an IR, or invalid if not imported.
  280. auto GetImportIRId(const SemIR::File& sem_ir) -> SemIR::ImportIRId& {
  281. return check_ir_map_[sem_ir.check_ir_id().index];
  282. }
  283. // True if the current file is an impl file.
  284. auto IsImplFile() -> bool {
  285. return sem_ir_->import_irs().Get(SemIR::ImportIRId::ApiForImpl).sem_ir !=
  286. nullptr;
  287. }
  288. // Prints information for a stack dump.
  289. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  290. // Prints the the formatted sem_ir to stderr.
  291. LLVM_DUMP_METHOD auto DumpFormattedFile() const -> void;
  292. // Get the Lex::TokenKind of a node for diagnostics.
  293. auto token_kind(Parse::NodeId node_id) -> Lex::TokenKind {
  294. return tokens().GetKind(parse_tree().node_token(node_id));
  295. }
  296. auto tokens() -> const Lex::TokenizedBuffer& { return *tokens_; }
  297. auto emitter() -> DiagnosticEmitter& { return *emitter_; }
  298. auto parse_tree() -> const Parse::Tree& { return *parse_tree_; }
  299. auto parse_tree_and_subtrees() -> const Parse::TreeAndSubtrees& {
  300. return get_parse_tree_and_subtrees_();
  301. }
  302. auto sem_ir() -> SemIR::File& { return *sem_ir_; }
  303. auto node_stack() -> NodeStack& { return node_stack_; }
  304. auto inst_block_stack() -> InstBlockStack& { return inst_block_stack_; }
  305. auto param_and_arg_refs_stack() -> ParamAndArgRefsStack& {
  306. return param_and_arg_refs_stack_;
  307. }
  308. auto args_type_info_stack() -> InstBlockStack& {
  309. return args_type_info_stack_;
  310. }
  311. auto decl_name_stack() -> DeclNameStack& { return decl_name_stack_; }
  312. auto decl_introducer_state_stack() -> DeclIntroducerStateStack& {
  313. return decl_introducer_state_stack_;
  314. }
  315. auto scope_stack() -> ScopeStack& { return scope_stack_; }
  316. auto return_scope_stack() -> llvm::SmallVector<ScopeStack::ReturnScope>& {
  317. return scope_stack().return_scope_stack();
  318. }
  319. auto break_continue_stack()
  320. -> llvm::SmallVector<ScopeStack::BreakContinueScope>& {
  321. return scope_stack().break_continue_stack();
  322. }
  323. auto generic_region_stack() -> GenericRegionStack& {
  324. return generic_region_stack_;
  325. }
  326. auto import_ir_constant_values()
  327. -> llvm::SmallVector<SemIR::ConstantValueStore, 0>& {
  328. return import_ir_constant_values_;
  329. }
  330. // Directly expose SemIR::File data accessors for brevity in calls.
  331. auto identifiers() -> CanonicalValueStore<IdentifierId>& {
  332. return sem_ir().identifiers();
  333. }
  334. auto ints() -> CanonicalValueStore<IntId>& { return sem_ir().ints(); }
  335. auto reals() -> ValueStore<RealId>& { return sem_ir().reals(); }
  336. auto floats() -> FloatValueStore& { return sem_ir().floats(); }
  337. auto string_literal_values() -> CanonicalValueStore<StringLiteralValueId>& {
  338. return sem_ir().string_literal_values();
  339. }
  340. auto entity_names() -> SemIR::EntityNameStore& {
  341. return sem_ir().entity_names();
  342. }
  343. auto functions() -> ValueStore<SemIR::FunctionId>& {
  344. return sem_ir().functions();
  345. }
  346. auto classes() -> ValueStore<SemIR::ClassId>& { return sem_ir().classes(); }
  347. auto interfaces() -> ValueStore<SemIR::InterfaceId>& {
  348. return sem_ir().interfaces();
  349. }
  350. auto impls() -> SemIR::ImplStore& { return sem_ir().impls(); }
  351. auto generics() -> SemIR::GenericStore& { return sem_ir().generics(); }
  352. auto specifics() -> SemIR::SpecificStore& { return sem_ir().specifics(); }
  353. auto import_irs() -> ValueStore<SemIR::ImportIRId>& {
  354. return sem_ir().import_irs();
  355. }
  356. auto import_ir_insts() -> ValueStore<SemIR::ImportIRInstId>& {
  357. return sem_ir().import_ir_insts();
  358. }
  359. auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  360. auto name_scopes() -> SemIR::NameScopeStore& {
  361. return sem_ir().name_scopes();
  362. }
  363. auto types() -> SemIR::TypeStore& { return sem_ir().types(); }
  364. auto type_blocks() -> SemIR::BlockValueStore<SemIR::TypeBlockId>& {
  365. return sem_ir().type_blocks();
  366. }
  367. // Instructions should be added with `AddInst` or `AddInstInNoBlock`. This is
  368. // `const` to prevent accidental misuse.
  369. auto insts() -> const SemIR::InstStore& { return sem_ir().insts(); }
  370. auto constant_values() -> SemIR::ConstantValueStore& {
  371. return sem_ir().constant_values();
  372. }
  373. auto inst_blocks() -> SemIR::InstBlockStore& {
  374. return sem_ir().inst_blocks();
  375. }
  376. auto constants() -> SemIR::ConstantStore& { return sem_ir().constants(); }
  377. auto definitions_required() -> llvm::SmallVector<SemIR::InstId>& {
  378. return definitions_required_;
  379. }
  380. auto global_init() -> GlobalInit& { return global_init_; }
  381. auto import_ref_ids() -> llvm::SmallVector<SemIR::InstId>& {
  382. return import_ref_ids_;
  383. }
  384. private:
  385. // A FoldingSet node for a type.
  386. class TypeNode : public llvm::FastFoldingSetNode {
  387. public:
  388. explicit TypeNode(const llvm::FoldingSetNodeID& node_id,
  389. SemIR::TypeId type_id)
  390. : llvm::FastFoldingSetNode(node_id), type_id_(type_id) {}
  391. auto type_id() -> SemIR::TypeId { return type_id_; }
  392. private:
  393. SemIR::TypeId type_id_;
  394. };
  395. // Finish producing an instruction. Set its constant value, and register it in
  396. // any applicable instruction lists.
  397. auto FinishInst(SemIR::InstId inst_id, SemIR::Inst inst) -> void;
  398. // Tokens for getting data on literals.
  399. const Lex::TokenizedBuffer* tokens_;
  400. // Handles diagnostics.
  401. DiagnosticEmitter* emitter_;
  402. // The file's parse tree.
  403. const Parse::Tree* parse_tree_;
  404. // Returns a lazily constructed TreeAndSubtrees.
  405. llvm::function_ref<const Parse::TreeAndSubtrees&()>
  406. get_parse_tree_and_subtrees_;
  407. // The SemIR::File being added to.
  408. SemIR::File* sem_ir_;
  409. // Whether to print verbose output.
  410. llvm::raw_ostream* vlog_stream_;
  411. // The stack during Build. Will contain file-level parse nodes on return.
  412. NodeStack node_stack_;
  413. // The stack of instruction blocks being used for general IR generation.
  414. InstBlockStack inst_block_stack_;
  415. // The stack of instruction blocks being used for param and arg ref blocks.
  416. ParamAndArgRefsStack param_and_arg_refs_stack_;
  417. // The stack of instruction blocks being used for type information while
  418. // processing arguments. This is used in parallel with
  419. // param_and_arg_refs_stack_. It's currently only used for struct literals,
  420. // where we need to track names for a type separate from the literal
  421. // arguments.
  422. InstBlockStack args_type_info_stack_;
  423. // The stack used for qualified declaration name construction.
  424. DeclNameStack decl_name_stack_;
  425. // The stack of declarations that could have modifiers.
  426. DeclIntroducerStateStack decl_introducer_state_stack_;
  427. // The stack of scopes we are currently within.
  428. ScopeStack scope_stack_;
  429. // The stack of generic regions we are currently within.
  430. GenericRegionStack generic_region_stack_;
  431. // Cache of reverse mapping from type constants to types.
  432. //
  433. // TODO: Instead of mapping to a dense `TypeId` space, we could make `TypeId`
  434. // be a thin wrapper around `ConstantId` and only perform the lookup only when
  435. // we want to access the completeness and value representation of a type. It's
  436. // not clear whether that would result in more or fewer lookups.
  437. //
  438. // TODO: Should this be part of the `TypeStore`?
  439. Map<SemIR::ConstantId, SemIR::TypeId> type_ids_for_type_constants_;
  440. // The list which will form NodeBlockId::Exports.
  441. llvm::SmallVector<SemIR::InstId> exports_;
  442. // Maps CheckIRId to ImportIRId.
  443. llvm::SmallVector<SemIR::ImportIRId> check_ir_map_;
  444. // Per-import constant values. These refer to the main IR and mainly serve as
  445. // a lookup table for quick access.
  446. //
  447. // Inline 0 elements because it's expected to require heap allocation.
  448. llvm::SmallVector<SemIR::ConstantValueStore, 0> import_ir_constant_values_;
  449. // Declaration instructions of entities that should have definitions by the
  450. // end of the current source file.
  451. llvm::SmallVector<SemIR::InstId> definitions_required_;
  452. // State for global initialization.
  453. GlobalInit global_init_;
  454. // A list of import refs which can't be inserted into their current context.
  455. // They're typically added during name lookup or import ref resolution, where
  456. // the current block on inst_block_stack_ is unrelated.
  457. //
  458. // These are instead added here because they're referenced by other
  459. // instructions and needs to be visible in textual IR.
  460. // FinalizeImportRefBlock() will produce an inst block for them.
  461. llvm::SmallVector<SemIR::InstId> import_ref_ids_;
  462. };
  463. } // namespace Carbon::Check
  464. #endif // CARBON_TOOLCHAIN_CHECK_CONTEXT_H_