context.h 20 KB

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