context.h 24 KB

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