context.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/full_pattern_stack.h"
  13. #include "toolchain/check/generic_region_stack.h"
  14. #include "toolchain/check/global_init.h"
  15. #include "toolchain/check/inst_block_stack.h"
  16. #include "toolchain/check/node_stack.h"
  17. #include "toolchain/check/param_and_arg_refs_stack.h"
  18. #include "toolchain/check/region_stack.h"
  19. #include "toolchain/check/scope_index.h"
  20. #include "toolchain/check/scope_stack.h"
  21. #include "toolchain/parse/node_ids.h"
  22. #include "toolchain/parse/tree.h"
  23. #include "toolchain/parse/tree_and_subtrees.h"
  24. #include "toolchain/sem_ir/file.h"
  25. #include "toolchain/sem_ir/ids.h"
  26. #include "toolchain/sem_ir/import_ir.h"
  27. #include "toolchain/sem_ir/inst.h"
  28. #include "toolchain/sem_ir/name_scope.h"
  29. #include "toolchain/sem_ir/typed_insts.h"
  30. namespace Carbon::Check {
  31. // Context and shared functionality for semantics handlers.
  32. class Context {
  33. public:
  34. using DiagnosticEmitter = Carbon::DiagnosticEmitter<SemIRLoc>;
  35. using DiagnosticBuilder = DiagnosticEmitter::DiagnosticBuilder;
  36. // A function that forms a diagnostic for some kind of problem. The
  37. // DiagnosticBuilder is returned rather than emitted so that the caller can
  38. // add contextual notes as appropriate.
  39. using BuildDiagnosticFn = llvm::function_ref<auto()->DiagnosticBuilder>;
  40. // Stores references for work.
  41. explicit Context(DiagnosticEmitter* emitter,
  42. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter,
  43. SemIR::File* sem_ir, int imported_ir_count,
  44. int total_ir_count, llvm::raw_ostream* vlog_stream);
  45. // Marks an implementation TODO. Always returns false.
  46. auto TODO(SemIRLoc loc, std::string label) -> bool;
  47. // Runs verification that the processing cleanly finished.
  48. auto VerifyOnFinish() -> void;
  49. // Adds an instruction to the current block, returning the produced ID.
  50. auto AddInst(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId {
  51. auto inst_id = AddInstInNoBlock(loc_id_and_inst);
  52. inst_block_stack_.AddInstId(inst_id);
  53. return inst_id;
  54. }
  55. // Convenience for AddInst with typed nodes.
  56. template <typename InstT, typename LocT>
  57. auto AddInst(LocT loc, InstT inst)
  58. -> decltype(AddInst(SemIR::LocIdAndInst(loc, inst))) {
  59. return AddInst(SemIR::LocIdAndInst(loc, inst));
  60. }
  61. // Returns a LocIdAndInst for an instruction with an imported location. Checks
  62. // that the imported location is compatible with the kind of instruction being
  63. // created.
  64. template <typename InstT>
  65. requires SemIR::Internal::HasNodeId<InstT>
  66. auto MakeImportedLocAndInst(SemIR::ImportIRInstId imported_loc_id, InstT inst)
  67. -> SemIR::LocIdAndInst {
  68. if constexpr (!SemIR::Internal::HasUntypedNodeId<InstT>) {
  69. CheckCompatibleImportedNodeKind(imported_loc_id, InstT::Kind);
  70. }
  71. return SemIR::LocIdAndInst::UncheckedLoc(imported_loc_id, inst);
  72. }
  73. // Adds an instruction in no block, returning the produced ID. Should be used
  74. // rarely.
  75. auto AddInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId {
  76. auto inst_id = sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  77. CARBON_VLOG("AddInst: {0}\n", loc_id_and_inst.inst);
  78. FinishInst(inst_id, loc_id_and_inst.inst);
  79. return inst_id;
  80. }
  81. // Convenience for AddInstInNoBlock with typed nodes.
  82. template <typename InstT, typename LocT>
  83. auto AddInstInNoBlock(LocT loc, InstT inst)
  84. -> decltype(AddInstInNoBlock(SemIR::LocIdAndInst(loc, inst))) {
  85. return AddInstInNoBlock(SemIR::LocIdAndInst(loc, inst));
  86. }
  87. // If the instruction has an implicit location and a constant value, returns
  88. // the constant value's instruction ID. Otherwise, same as AddInst.
  89. auto GetOrAddInst(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId;
  90. // Convenience for GetOrAddInst with typed nodes.
  91. template <typename InstT, typename LocT>
  92. auto GetOrAddInst(LocT loc, InstT inst)
  93. -> decltype(GetOrAddInst(SemIR::LocIdAndInst(loc, inst))) {
  94. return GetOrAddInst(SemIR::LocIdAndInst(loc, inst));
  95. }
  96. // Adds an instruction to the current block, returning the produced ID. The
  97. // instruction is a placeholder that is expected to be replaced by
  98. // `ReplaceInstBeforeConstantUse`.
  99. auto AddPlaceholderInst(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId;
  100. // Adds an instruction in no block, returning the produced ID. Should be used
  101. // rarely. The instruction is a placeholder that is expected to be replaced by
  102. // `ReplaceInstBeforeConstantUse`.
  103. auto AddPlaceholderInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst)
  104. -> SemIR::InstId;
  105. // Adds an instruction to the current pattern block, returning the produced
  106. // ID.
  107. // TODO: Is it possible to remove this and pattern_block_stack, now that
  108. // we have BeginSubpattern etc. instead?
  109. auto AddPatternInst(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId {
  110. auto inst_id = AddInstInNoBlock(loc_id_and_inst);
  111. pattern_block_stack_.AddInstId(inst_id);
  112. return inst_id;
  113. }
  114. // Convenience for AddPatternInst with typed nodes.
  115. template <typename InstT>
  116. requires(SemIR::Internal::HasNodeId<InstT>)
  117. auto AddPatternInst(decltype(InstT::Kind)::TypedNodeId node_id, InstT inst)
  118. -> SemIR::InstId {
  119. return AddPatternInst(SemIR::LocIdAndInst(node_id, inst));
  120. }
  121. // Pushes a parse tree node onto the stack, storing the SemIR::Inst as the
  122. // result.
  123. template <typename InstT>
  124. requires(SemIR::Internal::HasNodeId<InstT>)
  125. auto AddInstAndPush(decltype(InstT::Kind)::TypedNodeId node_id, InstT inst)
  126. -> void {
  127. node_stack_.Push(node_id, AddInst(node_id, inst));
  128. }
  129. // Replaces the instruction at `inst_id` with `loc_id_and_inst`. The
  130. // instruction is required to not have been used in any constant evaluation,
  131. // either because it's newly created and entirely unused, or because it's only
  132. // used in a position that constant evaluation ignores, such as a return slot.
  133. auto ReplaceLocIdAndInstBeforeConstantUse(SemIR::InstId inst_id,
  134. SemIR::LocIdAndInst loc_id_and_inst)
  135. -> void;
  136. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  137. // The instruction is required to not have been used in any constant
  138. // evaluation, either because it's newly created and entirely unused, or
  139. // because it's only used in a position that constant evaluation ignores, such
  140. // as a return slot.
  141. auto ReplaceInstBeforeConstantUse(SemIR::InstId inst_id, SemIR::Inst inst)
  142. -> void;
  143. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  144. // The instruction is required to not change its constant value.
  145. auto ReplaceInstPreservingConstantValue(SemIR::InstId inst_id,
  146. SemIR::Inst inst) -> void;
  147. // Sets only the parse node of an instruction. This is only used when setting
  148. // the parse node of an imported namespace. Versus
  149. // ReplaceInstBeforeConstantUse, it is safe to use after the namespace is used
  150. // in constant evaluation. It's exposed this way mainly so that `insts()` can
  151. // remain const.
  152. auto SetNamespaceNodeId(SemIR::InstId inst_id, Parse::NodeId node_id)
  153. -> void {
  154. sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
  155. }
  156. // Adds an exported name.
  157. auto AddExport(SemIR::InstId inst_id) -> void { exports_.push_back(inst_id); }
  158. auto Finalize() -> void;
  159. // Prints information for a stack dump.
  160. auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
  161. // Prints the the formatted sem_ir to stderr.
  162. LLVM_DUMP_METHOD auto DumpFormattedFile() const -> void;
  163. // Get the Lex::TokenKind of a node for diagnostics.
  164. auto token_kind(Parse::NodeId node_id) -> Lex::TokenKind {
  165. return tokens().GetKind(parse_tree().node_token(node_id));
  166. }
  167. auto emitter() -> DiagnosticEmitter& { return *emitter_; }
  168. auto parse_tree_and_subtrees() -> const Parse::TreeAndSubtrees& {
  169. return tree_and_subtrees_getter_();
  170. }
  171. auto sem_ir() -> SemIR::File& { return *sem_ir_; }
  172. auto sem_ir() const -> const SemIR::File& { return *sem_ir_; }
  173. auto parse_tree() const -> const Parse::Tree& {
  174. return sem_ir_->parse_tree();
  175. }
  176. auto tokens() const -> const Lex::TokenizedBuffer& {
  177. return parse_tree().tokens();
  178. }
  179. auto vlog_stream() -> llvm::raw_ostream* { return vlog_stream_; }
  180. auto node_stack() -> NodeStack& { return node_stack_; }
  181. auto inst_block_stack() -> InstBlockStack& { return inst_block_stack_; }
  182. auto pattern_block_stack() -> InstBlockStack& { return pattern_block_stack_; }
  183. auto param_and_arg_refs_stack() -> ParamAndArgRefsStack& {
  184. return param_and_arg_refs_stack_;
  185. }
  186. auto args_type_info_stack() -> InstBlockStack& {
  187. return args_type_info_stack_;
  188. }
  189. auto struct_type_fields_stack() -> ArrayStack<SemIR::StructTypeField>& {
  190. return struct_type_fields_stack_;
  191. }
  192. auto field_decls_stack() -> ArrayStack<SemIR::InstId>& {
  193. return field_decls_stack_;
  194. }
  195. auto decl_name_stack() -> DeclNameStack& { return decl_name_stack_; }
  196. auto decl_introducer_state_stack() -> DeclIntroducerStateStack& {
  197. return decl_introducer_state_stack_;
  198. }
  199. auto scope_stack() -> ScopeStack& { return scope_stack_; }
  200. auto return_scope_stack() -> llvm::SmallVector<ScopeStack::ReturnScope>& {
  201. return scope_stack().return_scope_stack();
  202. }
  203. auto break_continue_stack()
  204. -> llvm::SmallVector<ScopeStack::BreakContinueScope>& {
  205. return scope_stack().break_continue_stack();
  206. }
  207. auto generic_region_stack() -> GenericRegionStack& {
  208. return generic_region_stack_;
  209. }
  210. auto vtable_stack() -> InstBlockStack& { return vtable_stack_; }
  211. auto check_ir_map() -> llvm::MutableArrayRef<SemIR::ImportIRId> {
  212. return check_ir_map_;
  213. }
  214. auto import_ir_constant_values()
  215. -> llvm::SmallVector<SemIR::ConstantValueStore, 0>& {
  216. return import_ir_constant_values_;
  217. }
  218. // Directly expose SemIR::File data accessors for brevity in calls.
  219. auto identifiers() -> SharedValueStores::IdentifierStore& {
  220. return sem_ir().identifiers();
  221. }
  222. auto ints() -> SharedValueStores::IntStore& { return sem_ir().ints(); }
  223. auto reals() -> SharedValueStores::RealStore& { return sem_ir().reals(); }
  224. auto floats() -> SharedValueStores::FloatStore& { return sem_ir().floats(); }
  225. auto string_literal_values() -> SharedValueStores::StringLiteralStore& {
  226. return sem_ir().string_literal_values();
  227. }
  228. auto entity_names() -> SemIR::EntityNameStore& {
  229. return sem_ir().entity_names();
  230. }
  231. auto functions() -> ValueStore<SemIR::FunctionId>& {
  232. return sem_ir().functions();
  233. }
  234. auto classes() -> ValueStore<SemIR::ClassId>& { return sem_ir().classes(); }
  235. auto interfaces() -> ValueStore<SemIR::InterfaceId>& {
  236. return sem_ir().interfaces();
  237. }
  238. auto associated_constants() -> ValueStore<SemIR::AssociatedConstantId>& {
  239. return sem_ir().associated_constants();
  240. }
  241. auto facet_types() -> CanonicalValueStore<SemIR::FacetTypeId>& {
  242. return sem_ir().facet_types();
  243. }
  244. auto impls() -> SemIR::ImplStore& { return sem_ir().impls(); }
  245. auto generics() -> SemIR::GenericStore& { return sem_ir().generics(); }
  246. auto specifics() -> SemIR::SpecificStore& { return sem_ir().specifics(); }
  247. auto import_irs() -> ValueStore<SemIR::ImportIRId>& {
  248. return sem_ir().import_irs();
  249. }
  250. auto import_ir_insts() -> ValueStore<SemIR::ImportIRInstId>& {
  251. return sem_ir().import_ir_insts();
  252. }
  253. auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  254. auto name_scopes() -> SemIR::NameScopeStore& {
  255. return sem_ir().name_scopes();
  256. }
  257. auto struct_type_fields() -> SemIR::StructTypeFieldsStore& {
  258. return sem_ir().struct_type_fields();
  259. }
  260. auto types() -> SemIR::TypeStore& { return sem_ir().types(); }
  261. auto type_blocks() -> SemIR::BlockValueStore<SemIR::TypeBlockId>& {
  262. return sem_ir().type_blocks();
  263. }
  264. // Instructions should be added with `AddInst` or `AddInstInNoBlock`. This is
  265. // `const` to prevent accidental misuse.
  266. auto insts() -> const SemIR::InstStore& { return sem_ir().insts(); }
  267. auto constant_values() -> SemIR::ConstantValueStore& {
  268. return sem_ir().constant_values();
  269. }
  270. auto inst_blocks() -> SemIR::InstBlockStore& {
  271. return sem_ir().inst_blocks();
  272. }
  273. auto constants() -> SemIR::ConstantStore& { return sem_ir().constants(); }
  274. auto definitions_required() -> llvm::SmallVector<SemIR::InstId>& {
  275. return definitions_required_;
  276. }
  277. auto global_init() -> GlobalInit& { return global_init_; }
  278. auto import_ref_ids() -> llvm::SmallVector<SemIR::InstId>& {
  279. return import_ref_ids_;
  280. }
  281. // Map from an AnyBindingPattern inst to precomputed parts of the
  282. // pattern-match SemIR for it.
  283. //
  284. // TODO: Consider putting this behind a narrower API to guard against emitting
  285. // multiple times.
  286. struct BindingPatternInfo {
  287. // The corresponding AnyBindName inst.
  288. SemIR::InstId bind_name_id;
  289. // The region of insts that computes the type of the binding.
  290. SemIR::ExprRegionId type_expr_region_id;
  291. };
  292. auto bind_name_map() -> Map<SemIR::InstId, BindingPatternInfo>& {
  293. return bind_name_map_;
  294. }
  295. auto var_storage_map() -> Map<SemIR::InstId, SemIR::InstId>& {
  296. return var_storage_map_;
  297. }
  298. auto region_stack() -> RegionStack& { return region_stack_; }
  299. auto full_pattern_stack() -> FullPatternStack& {
  300. return scope_stack_.full_pattern_stack();
  301. }
  302. private:
  303. // A FoldingSet node for a type.
  304. class TypeNode : public llvm::FastFoldingSetNode {
  305. public:
  306. explicit TypeNode(const llvm::FoldingSetNodeID& node_id,
  307. SemIR::TypeId type_id)
  308. : llvm::FastFoldingSetNode(node_id), type_id_(type_id) {}
  309. auto type_id() -> SemIR::TypeId { return type_id_; }
  310. private:
  311. SemIR::TypeId type_id_;
  312. };
  313. // Checks that the provided imported location has a node kind that is
  314. // compatible with that of the given instruction.
  315. auto CheckCompatibleImportedNodeKind(SemIR::ImportIRInstId imported_loc_id,
  316. SemIR::InstKind kind) -> void;
  317. // Finish producing an instruction. Set its constant value, and register it in
  318. // any applicable instruction lists.
  319. auto FinishInst(SemIR::InstId inst_id, SemIR::Inst inst) -> void;
  320. // Handles diagnostics.
  321. DiagnosticEmitter* emitter_;
  322. // Returns a lazily constructed TreeAndSubtrees.
  323. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter_;
  324. // The SemIR::File being added to.
  325. SemIR::File* sem_ir_;
  326. // Whether to print verbose output.
  327. llvm::raw_ostream* vlog_stream_;
  328. // The stack during Build. Will contain file-level parse nodes on return.
  329. NodeStack node_stack_;
  330. // The stack of instruction blocks being used for general IR generation.
  331. InstBlockStack inst_block_stack_;
  332. // The stack of instruction blocks that contain pattern instructions.
  333. InstBlockStack pattern_block_stack_;
  334. // The stack of instruction blocks being used for param and arg ref blocks.
  335. ParamAndArgRefsStack param_and_arg_refs_stack_;
  336. // The stack of instruction blocks being used for type information while
  337. // processing arguments. This is used in parallel with
  338. // param_and_arg_refs_stack_. It's currently only used for struct literals,
  339. // where we need to track names for a type separate from the literal
  340. // arguments.
  341. InstBlockStack args_type_info_stack_;
  342. // The stack of StructTypeFields for in-progress StructTypeLiterals.
  343. ArrayStack<SemIR::StructTypeField> struct_type_fields_stack_;
  344. // The stack of FieldDecls for in-progress Class definitions.
  345. ArrayStack<SemIR::InstId> field_decls_stack_;
  346. // The stack used for qualified declaration name construction.
  347. DeclNameStack decl_name_stack_;
  348. // The stack of declarations that could have modifiers.
  349. DeclIntroducerStateStack decl_introducer_state_stack_;
  350. // The stack of scopes we are currently within.
  351. ScopeStack scope_stack_;
  352. // The stack of generic regions we are currently within.
  353. GenericRegionStack generic_region_stack_;
  354. // Contains a vtable block for each `class` scope which is currently being
  355. // defined, regardless of whether the class can have virtual functions.
  356. InstBlockStack vtable_stack_;
  357. // Cache of reverse mapping from type constants to types.
  358. //
  359. // TODO: Instead of mapping to a dense `TypeId` space, we could make `TypeId`
  360. // be a thin wrapper around `ConstantId` and only perform the lookup only when
  361. // we want to access the completeness and value representation of a type. It's
  362. // not clear whether that would result in more or fewer lookups.
  363. //
  364. // TODO: Should this be part of the `TypeStore`?
  365. Map<SemIR::ConstantId, SemIR::TypeId> type_ids_for_type_constants_;
  366. // The list which will form NodeBlockId::Exports.
  367. llvm::SmallVector<SemIR::InstId> exports_;
  368. // Maps CheckIRId to ImportIRId.
  369. llvm::SmallVector<SemIR::ImportIRId> check_ir_map_;
  370. // Per-import constant values. These refer to the main IR and mainly serve as
  371. // a lookup table for quick access.
  372. //
  373. // Inline 0 elements because it's expected to require heap allocation.
  374. llvm::SmallVector<SemIR::ConstantValueStore, 0> import_ir_constant_values_;
  375. // Declaration instructions of entities that should have definitions by the
  376. // end of the current source file.
  377. llvm::SmallVector<SemIR::InstId> definitions_required_;
  378. // State for global initialization.
  379. GlobalInit global_init_;
  380. // A list of import refs which can't be inserted into their current context.
  381. // They're typically added during name lookup or import ref resolution, where
  382. // the current block on inst_block_stack_ is unrelated.
  383. //
  384. // These are instead added here because they're referenced by other
  385. // instructions and needs to be visible in textual IR.
  386. // FinalizeImportRefBlock() will produce an inst block for them.
  387. llvm::SmallVector<SemIR::InstId> import_ref_ids_;
  388. Map<SemIR::InstId, BindingPatternInfo> bind_name_map_;
  389. // Map from VarPattern insts to the corresponding VarStorage insts. The
  390. // VarStorage insts are allocated, emitted, and stored in the map after
  391. // processing the enclosing full-pattern.
  392. Map<SemIR::InstId, SemIR::InstId> var_storage_map_;
  393. // Stack of single-entry regions being built.
  394. RegionStack region_stack_;
  395. };
  396. } // namespace Carbon::Check
  397. #endif // CARBON_TOOLCHAIN_CHECK_CONTEXT_H_