context.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 <string>
  7. #include "common/map.h"
  8. #include "common/ostream.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "toolchain/base/canonical_value_store.h"
  11. #include "toolchain/base/value_store.h"
  12. #include "toolchain/check/decl_introducer_state.h"
  13. #include "toolchain/check/decl_name_stack.h"
  14. #include "toolchain/check/deferred_definition_worklist.h"
  15. #include "toolchain/check/diagnostic_helpers.h"
  16. #include "toolchain/check/full_pattern_stack.h"
  17. #include "toolchain/check/generic_region_stack.h"
  18. #include "toolchain/check/global_init.h"
  19. #include "toolchain/check/inst_block_stack.h"
  20. #include "toolchain/check/node_stack.h"
  21. #include "toolchain/check/param_and_arg_refs_stack.h"
  22. #include "toolchain/check/region_stack.h"
  23. #include "toolchain/check/scope_stack.h"
  24. #include "toolchain/diagnostics/diagnostic_emitter.h"
  25. #include "toolchain/parse/node_ids.h"
  26. #include "toolchain/parse/tree.h"
  27. #include "toolchain/parse/tree_and_subtrees.h"
  28. #include "toolchain/sem_ir/facet_type_info.h"
  29. #include "toolchain/sem_ir/file.h"
  30. #include "toolchain/sem_ir/ids.h"
  31. #include "toolchain/sem_ir/import_ir.h"
  32. #include "toolchain/sem_ir/inst.h"
  33. #include "toolchain/sem_ir/name_scope.h"
  34. #include "toolchain/sem_ir/specific_interface.h"
  35. #include "toolchain/sem_ir/typed_insts.h"
  36. namespace Carbon::Check {
  37. // Context stored during check.
  38. //
  39. // This file stores state, and members objects may provide an API. Other files
  40. // may also have helpers that operate on Context. To keep this file manageable,
  41. // please put logic into other files.
  42. //
  43. // For example, consider the API for functions:
  44. // - `context.functions()`: Exposes storage of `SemIR::Function` objects.
  45. // - `toolchain/check/function.h`: Contains helper functions which use
  46. // `Check::Context`.
  47. // - `toolchain/sem_ir/function.h`: Contains helper functions which only need
  48. // `SemIR` objects, for which it's helpful not to depend on `Check::Context`
  49. // (for example, shared with lowering).
  50. class Context {
  51. public:
  52. // Stores references for work.
  53. explicit Context(DiagnosticEmitterBase* emitter,
  54. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter,
  55. SemIR::File* sem_ir, int imported_ir_count,
  56. int total_ir_count, llvm::raw_ostream* vlog_stream);
  57. // Marks an implementation TODO. Always returns false.
  58. auto TODO(SemIR::LocId loc_id, std::string label) -> bool;
  59. auto TODO(SemIR::InstId loc_inst_id, std::string label) -> bool;
  60. // Runs verification that the processing cleanly finished.
  61. auto VerifyOnFinish() const -> void;
  62. // Prints information for a stack dump.
  63. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  64. // Get the Lex::TokenKind of a node for diagnostics.
  65. auto token_kind(Parse::NodeId node_id) -> Lex::TokenKind {
  66. return tokens().GetKind(parse_tree().node_token(node_id));
  67. }
  68. auto emitter() -> DiagnosticEmitterBase& { return *emitter_; }
  69. auto parse_tree_and_subtrees() -> const Parse::TreeAndSubtrees& {
  70. return tree_and_subtrees_getter_();
  71. }
  72. auto sem_ir() -> SemIR::File& { return *sem_ir_; }
  73. auto sem_ir() const -> const SemIR::File& { return *sem_ir_; }
  74. // Convenience functions for major phase data.
  75. auto parse_tree() const -> const Parse::Tree& {
  76. return sem_ir_->parse_tree();
  77. }
  78. auto tokens() const -> const Lex::TokenizedBuffer& {
  79. return parse_tree().tokens();
  80. }
  81. auto vlog_stream() -> llvm::raw_ostream* { return vlog_stream_; }
  82. auto node_stack() -> NodeStack& { return node_stack_; }
  83. auto inst_block_stack() -> InstBlockStack& { return inst_block_stack_; }
  84. auto pattern_block_stack() -> InstBlockStack& { return pattern_block_stack_; }
  85. auto param_and_arg_refs_stack() -> ParamAndArgRefsStack& {
  86. return param_and_arg_refs_stack_;
  87. }
  88. auto args_type_info_stack() -> InstBlockStack& {
  89. return args_type_info_stack_;
  90. }
  91. auto struct_type_fields_stack() -> ArrayStack<SemIR::StructTypeField>& {
  92. return struct_type_fields_stack_;
  93. }
  94. auto field_decls_stack() -> ArrayStack<SemIR::InstId>& {
  95. return field_decls_stack_;
  96. }
  97. auto require_impls_stack() -> ArrayStack<SemIR::RequireImplsId>& {
  98. return require_impls_stack_;
  99. }
  100. auto decl_name_stack() -> DeclNameStack& { return decl_name_stack_; }
  101. auto decl_introducer_state_stack() -> DeclIntroducerStateStack& {
  102. return decl_introducer_state_stack_;
  103. }
  104. auto scope_stack() -> ScopeStack& { return scope_stack_; }
  105. // Convenience functions for frequently-used `scope_stack` members.
  106. auto break_continue_stack()
  107. -> llvm::SmallVector<ScopeStack::BreakContinueScope>& {
  108. return scope_stack().break_continue_stack();
  109. }
  110. auto full_pattern_stack() -> FullPatternStack& {
  111. return scope_stack_.full_pattern_stack();
  112. }
  113. auto deferred_definition_worklist() -> DeferredDefinitionWorklist& {
  114. return deferred_definition_worklist_;
  115. }
  116. auto generic_region_stack() -> GenericRegionStack& {
  117. return generic_region_stack_;
  118. }
  119. auto vtable_stack() -> InstBlockStack& { return vtable_stack_; }
  120. auto exports() -> llvm::SmallVector<SemIR::InstId>& { return exports_; }
  121. auto check_ir_map()
  122. -> FixedSizeValueStore<SemIR::CheckIRId, SemIR::ImportIRId>& {
  123. return check_ir_map_;
  124. }
  125. auto import_ir_constant_values()
  126. -> llvm::SmallVector<SemIR::ConstantValueStore, 0>& {
  127. return import_ir_constant_values_;
  128. }
  129. auto cpp_carbon_file_locations()
  130. -> llvm::SmallVector<clang::SourceLocation>& {
  131. return cpp_carbon_file_locations_;
  132. }
  133. auto definitions_required_by_decl() -> llvm::SmallVector<SemIR::InstId>& {
  134. return definitions_required_by_decl_;
  135. }
  136. auto definitions_required_by_use()
  137. -> llvm::SmallVector<std::pair<SemIR::LocId, SemIR::SpecificId>>& {
  138. return definitions_required_by_use_;
  139. }
  140. auto global_init() -> GlobalInit& { return global_init_; }
  141. auto imports() -> llvm::SmallVector<SemIR::InstId>& { return imports_; }
  142. // Pre-computed parts of a binding pattern.
  143. // TODO: Consider putting this behind a narrower API to guard against emitting
  144. // multiple times.
  145. struct BindingPatternInfo {
  146. // The corresponding AnyBinding inst.
  147. SemIR::InstId bind_name_id;
  148. // The region of insts that computes the type of the binding.
  149. SemIR::ExprRegionId type_expr_region_id;
  150. };
  151. auto bind_name_map() -> Map<SemIR::InstId, BindingPatternInfo>& {
  152. return bind_name_map_;
  153. }
  154. auto var_storage_map() -> Map<SemIR::InstId, SemIR::InstId>& {
  155. return var_storage_map_;
  156. }
  157. // During Choice typechecking, each alternative turns into a name binding on
  158. // the Choice type, but this can't be done until the full Choice type is
  159. // known. This represents each binding to be done at the end of checking the
  160. // Choice type.
  161. struct ChoiceDeferredBinding {
  162. Parse::NodeIdOneOf<Parse::ChoiceAlternativeListCommaId,
  163. Parse::ChoiceDefinitionId>
  164. node_id;
  165. NameComponent name_component;
  166. };
  167. auto choice_deferred_bindings() -> llvm::SmallVector<ChoiceDeferredBinding>& {
  168. return choice_deferred_bindings_;
  169. }
  170. auto region_stack() -> RegionStack& { return region_stack_; }
  171. // An ongoing impl lookup, used to ensure termination.
  172. struct ImplLookupStackEntry {
  173. SemIR::ConstantId query_self_const_id;
  174. SemIR::ConstantId query_facet_type_const_id;
  175. // The location of the impl being looked at for the stack entry.
  176. SemIR::InstId impl_loc = SemIR::InstId::None;
  177. };
  178. auto impl_lookup_stack() -> llvm::SmallVector<ImplLookupStackEntry>& {
  179. return impl_lookup_stack_;
  180. }
  181. // An impl lookup query that resulted in a concrete witness from finding an
  182. // `impl` declaration (not though a facet value), and its result. Used to look
  183. // for conflicting `impl` declarations.
  184. struct PoisonedConcreteImplLookupQuery {
  185. // The location the LookupImplWitness originated from.
  186. SemIR::LocId loc_id;
  187. // The query for a witness of an impl for an interface.
  188. SemIR::LookupImplWitness query;
  189. // The resulting ImplWitness.
  190. SemIR::InstId impl_witness;
  191. };
  192. auto poisoned_concrete_impl_lookup_queries()
  193. -> llvm::SmallVector<PoisonedConcreteImplLookupQuery>& {
  194. return poisoned_concrete_impl_lookup_queries_;
  195. }
  196. // A stack that tracks the rewrite constraints from a `where` expression being
  197. // checked. The back of the stack is the currently checked `where` expression.
  198. auto rewrites_stack()
  199. -> llvm::SmallVector<Map<SemIR::ConstantId, SemIR::InstId>>& {
  200. return rewrites_stack_;
  201. }
  202. // --------------------------------------------------------------------------
  203. // Directly expose SemIR::File data accessors for brevity in calls.
  204. // --------------------------------------------------------------------------
  205. auto identifiers() -> SharedValueStores::IdentifierStore& {
  206. return sem_ir().identifiers();
  207. }
  208. auto ints() -> SharedValueStores::IntStore& { return sem_ir().ints(); }
  209. auto reals() -> SharedValueStores::RealStore& { return sem_ir().reals(); }
  210. auto floats() -> SharedValueStores::FloatStore& { return sem_ir().floats(); }
  211. auto string_literal_values() -> SharedValueStores::StringLiteralStore& {
  212. return sem_ir().string_literal_values();
  213. }
  214. auto entity_names() -> SemIR::EntityNameStore& {
  215. return sem_ir().entity_names();
  216. }
  217. auto cpp_global_names() -> SemIR::CppGlobalVarStore& {
  218. return sem_ir().cpp_global_vars();
  219. }
  220. auto cpp_overload_sets() -> SemIR::CppOverloadSetStore& {
  221. return sem_ir().cpp_overload_sets();
  222. }
  223. auto functions() -> SemIR::FunctionStore& { return sem_ir().functions(); }
  224. auto classes() -> SemIR::ClassStore& { return sem_ir().classes(); }
  225. auto vtables() -> SemIR::VtableStore& { return sem_ir().vtables(); }
  226. auto interfaces() -> SemIR::InterfaceStore& { return sem_ir().interfaces(); }
  227. auto named_constraints() -> SemIR::NamedConstraintStore& {
  228. return sem_ir().named_constraints();
  229. }
  230. auto require_impls() -> SemIR::RequireImplsStore& {
  231. return sem_ir().require_impls();
  232. }
  233. auto require_impls_blocks() -> SemIR::RequireImplsBlockStore& {
  234. return sem_ir().require_impls_blocks();
  235. }
  236. auto associated_constants() -> SemIR::AssociatedConstantStore& {
  237. return sem_ir().associated_constants();
  238. }
  239. auto facet_types() -> SemIR::FacetTypeInfoStore& {
  240. return sem_ir().facet_types();
  241. }
  242. auto identified_facet_types() -> SemIR::File::IdentifiedFacetTypeStore& {
  243. return sem_ir().identified_facet_types();
  244. }
  245. auto impls() -> SemIR::ImplStore& { return sem_ir().impls(); }
  246. auto specific_interfaces() -> SemIR::SpecificInterfaceStore& {
  247. return sem_ir().specific_interfaces();
  248. }
  249. auto generics() -> SemIR::GenericStore& { return sem_ir().generics(); }
  250. auto specifics() -> SemIR::SpecificStore& { return sem_ir().specifics(); }
  251. auto import_irs() -> SemIR::ImportIRStore& { return sem_ir().import_irs(); }
  252. auto import_ir_insts() -> SemIR::ImportIRInstStore& {
  253. return sem_ir().import_ir_insts();
  254. }
  255. auto ast_context() -> clang::ASTContext& {
  256. return sem_ir().clang_ast_unit()->getASTContext();
  257. }
  258. auto clang_sema() -> clang::Sema& {
  259. return sem_ir().clang_ast_unit()->getSema();
  260. }
  261. auto clang_decls() -> SemIR::ClangDeclStore& {
  262. return sem_ir().clang_decls();
  263. }
  264. auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  265. auto name_scopes() -> SemIR::NameScopeStore& {
  266. return sem_ir().name_scopes();
  267. }
  268. auto struct_type_fields() -> SemIR::StructTypeFieldsStore& {
  269. return sem_ir().struct_type_fields();
  270. }
  271. auto custom_layouts() -> SemIR::CustomLayoutStore& {
  272. return sem_ir().custom_layouts();
  273. }
  274. auto types() -> SemIR::TypeStore& { return sem_ir().types(); }
  275. // Instructions should be added with `AddInst` or `AddInstInNoBlock` from
  276. // `inst.h`. This is `const` to prevent accidental misuse.
  277. auto insts() const -> const SemIR::InstStore& { return sem_ir().insts(); }
  278. auto constant_values() -> SemIR::ConstantValueStore& {
  279. return sem_ir().constant_values();
  280. }
  281. auto inst_blocks() -> SemIR::InstBlockStore& {
  282. return sem_ir().inst_blocks();
  283. }
  284. auto constants() -> SemIR::ConstantStore& { return sem_ir().constants(); }
  285. // --------------------------------------------------------------------------
  286. // End of SemIR::File members.
  287. // --------------------------------------------------------------------------
  288. private:
  289. // Handles diagnostics.
  290. DiagnosticEmitterBase* emitter_;
  291. // Returns a lazily constructed TreeAndSubtrees.
  292. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter_;
  293. // The SemIR::File being added to.
  294. SemIR::File* sem_ir_;
  295. // The total number of files.
  296. int total_ir_count_;
  297. // Whether to print verbose output.
  298. llvm::raw_ostream* vlog_stream_;
  299. // The stack during Build. Will contain file-level parse nodes on return.
  300. NodeStack node_stack_;
  301. // The stack of instruction blocks being used for general IR generation.
  302. InstBlockStack inst_block_stack_;
  303. // The stack of instruction blocks that contain pattern instructions.
  304. InstBlockStack pattern_block_stack_;
  305. // The stack of instruction blocks being used for param and arg ref blocks.
  306. ParamAndArgRefsStack param_and_arg_refs_stack_;
  307. // The stack of instruction blocks being used for type information while
  308. // processing arguments. This is used in parallel with
  309. // param_and_arg_refs_stack_. It's used for:
  310. // - Struct literals, where we need to track names for a type separate from
  311. // the literal arguments.
  312. // - The associated entries witness table, while parsing an interface.
  313. InstBlockStack args_type_info_stack_;
  314. // The stack of StructTypeFields for in-progress StructTypeLiterals.
  315. ArrayStack<SemIR::StructTypeField> struct_type_fields_stack_;
  316. // The stack of FieldDecls for in-progress Class definitions.
  317. ArrayStack<SemIR::InstId> field_decls_stack_;
  318. // The stack of RequireImpls for in-progress Interface and Constraint
  319. // definitions.
  320. ArrayStack<SemIR::RequireImplsId> require_impls_stack_;
  321. // The stack used for qualified declaration name construction.
  322. DeclNameStack decl_name_stack_;
  323. // The stack of declarations that could have modifiers.
  324. DeclIntroducerStateStack decl_introducer_state_stack_;
  325. // The stack of scopes we are currently within.
  326. ScopeStack scope_stack_;
  327. // The worklist of deferred definition tasks to perform at the end of the
  328. // enclosing deferred definition scope.
  329. DeferredDefinitionWorklist deferred_definition_worklist_;
  330. // The stack of generic regions we are currently within.
  331. GenericRegionStack generic_region_stack_;
  332. // Contains a vtable block for each `class` scope which is currently being
  333. // defined, regardless of whether the class can have virtual functions.
  334. InstBlockStack vtable_stack_;
  335. // Instructions which are operands to an `export` directive. This becomes
  336. // `InstBlockId::Exports`.
  337. llvm::SmallVector<SemIR::InstId> exports_;
  338. // Maps CheckIRId to ImportIRId.
  339. FixedSizeValueStore<SemIR::CheckIRId, SemIR::ImportIRId> check_ir_map_;
  340. // Per-import constant values. These refer to the main IR and mainly serve as
  341. // a lookup table for quick access.
  342. //
  343. // Inline 0 elements because it's expected to require heap allocation.
  344. llvm::SmallVector<SemIR::ConstantValueStore, 0> import_ir_constant_values_;
  345. // Per-Carbon-file start locations for corresponding Clang source buffers.
  346. // Owned and managed by code in cpp/location.cpp.
  347. llvm::SmallVector<clang::SourceLocation> cpp_carbon_file_locations_;
  348. // Declaration instructions of entities that should have definitions by the
  349. // end of the current source file.
  350. llvm::SmallVector<SemIR::InstId> definitions_required_by_decl_;
  351. // Entities that should have definitions by the end of the current source
  352. // file, because of a generic was used a concrete specific. This is currently
  353. // only tracking specific functions that should have a definition emitted.
  354. llvm::SmallVector<std::pair<SemIR::LocId, SemIR::SpecificId>>
  355. definitions_required_by_use_;
  356. // State for global initialization.
  357. GlobalInit global_init_;
  358. // Instructions which are generated as a result of imports; both `ImportRef`s
  359. // and instructions they generate. For example, when a name reference resolves
  360. // an imported function, the `ImportRefLoaded` results in a `FunctionDecl`,
  361. // and both end up here. The `FunctionDecl` shouldn't use the current block on
  362. // inst_block_stack_ because it's not tied to the referencing scope.
  363. //
  364. // This becomes `InstBlockId::Imports`.
  365. llvm::SmallVector<SemIR::InstId> imports_;
  366. // Map from an AnyBindingPattern inst to precomputed parts of the
  367. // pattern-match SemIR for it.
  368. Map<SemIR::InstId, BindingPatternInfo> bind_name_map_;
  369. // Map from VarPattern insts to the corresponding VarStorage insts. The
  370. // VarStorage insts are allocated, emitted, and stored in the map after
  371. // processing the enclosing full-pattern.
  372. Map<SemIR::InstId, SemIR::InstId> var_storage_map_;
  373. // Each alternative in a Choice gets an entry here, they are stored in
  374. // declaration order. The vector is consumed and emptied at the end of the
  375. // Choice definition.
  376. //
  377. // TODO: This may need to be a stack of vectors if it becomes possible to
  378. // define a Choice type inside an alternative's parameter set.
  379. llvm::SmallVector<ChoiceDeferredBinding> choice_deferred_bindings_;
  380. // Stack of single-entry regions being built.
  381. RegionStack region_stack_;
  382. // Tracks all ongoing impl lookups in order to ensure that lookup terminates
  383. // via the acyclic rule and the termination rule.
  384. llvm::SmallVector<ImplLookupStackEntry> impl_lookup_stack_;
  385. // Tracks impl lookup queries that lead to concrete witness results, along
  386. // with those results. Used to verify that the same queries produce the same
  387. // results at the end of the file. Any difference is diagnosed.
  388. llvm::SmallVector<PoisonedConcreteImplLookupQuery>
  389. poisoned_concrete_impl_lookup_queries_;
  390. // A map from an ImplWitnessAccess on the LHS of a rewrite constraint to its
  391. // value on the RHS. Used during checking of a `where` expression to allow
  392. // constraints to access values from earlier constraints.
  393. llvm::SmallVector<Map<SemIR::ConstantId, SemIR::InstId>> rewrites_stack_;
  394. };
  395. } // namespace Carbon::Check
  396. #endif // CARBON_TOOLCHAIN_CHECK_CONTEXT_H_