file.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_SEM_IR_FILE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_FILE_H_
  6. #include "clang/Frontend/ASTUnit.h"
  7. #include "common/error.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "llvm/ADT/iterator_range.h"
  10. #include "llvm/Support/Allocator.h"
  11. #include "llvm/Support/FormatVariadic.h"
  12. #include "toolchain/base/int.h"
  13. #include "toolchain/base/shared_value_stores.h"
  14. #include "toolchain/base/value_store.h"
  15. #include "toolchain/base/yaml.h"
  16. #include "toolchain/parse/tree.h"
  17. #include "toolchain/sem_ir/associated_constant.h"
  18. #include "toolchain/sem_ir/class.h"
  19. #include "toolchain/sem_ir/constant.h"
  20. #include "toolchain/sem_ir/entity_name.h"
  21. #include "toolchain/sem_ir/facet_type_info.h"
  22. #include "toolchain/sem_ir/function.h"
  23. #include "toolchain/sem_ir/generic.h"
  24. #include "toolchain/sem_ir/ids.h"
  25. #include "toolchain/sem_ir/impl.h"
  26. #include "toolchain/sem_ir/import_cpp.h"
  27. #include "toolchain/sem_ir/import_ir.h"
  28. #include "toolchain/sem_ir/inst.h"
  29. #include "toolchain/sem_ir/interface.h"
  30. #include "toolchain/sem_ir/name.h"
  31. #include "toolchain/sem_ir/name_scope.h"
  32. #include "toolchain/sem_ir/singleton_insts.h"
  33. #include "toolchain/sem_ir/struct_type_field.h"
  34. #include "toolchain/sem_ir/type.h"
  35. #include "toolchain/sem_ir/type_info.h"
  36. namespace Carbon::SemIR {
  37. // An expression that may contain control flow, represented as a
  38. // single-entry/single-exit region. `block_ids` are the blocks that are part of
  39. // evaluation of the expression, and `result_id` represents the result of
  40. // evaluating the expression. `block_ids` consists of all blocks that are
  41. // dominated by `block_ids.front()` and post-dominated by `block_ids.back()`,
  42. // and should be in lexical order. `result_id` will be in `block_ids.back()` or
  43. // some block that dominates it.
  44. //
  45. // `block_ids` cannot be empty. If it has a single element, then the region
  46. // should be used via a `SpliceBlock` inst. Otherwise, the region should be used
  47. // by branching to the entry block, and the last inst in the exit block will
  48. // likewise be a branch.
  49. struct ExprRegion {
  50. llvm::SmallVector<InstBlockId> block_ids;
  51. InstId result_id;
  52. };
  53. // Provides semantic analysis on a Parse::Tree.
  54. class File : public Printable<File> {
  55. public:
  56. using IdentifiedFacetTypeStore =
  57. RelationalValueStore<FacetTypeId, IdentifiedFacetTypeId>;
  58. // Starts a new file for Check::CheckParseTree.
  59. explicit File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
  60. const std::optional<Parse::Tree::PackagingDecl>& packaging_decl,
  61. SharedValueStores& value_stores, std::string filename);
  62. File(const File&) = delete;
  63. auto operator=(const File&) -> File& = delete;
  64. // Verifies that invariants of the semantics IR hold.
  65. auto Verify() const -> ErrorOr<Success>;
  66. // Prints the full IR. Allow omitting singletons so that changes to the list
  67. // of singletons won't churn golden test file content.
  68. auto Print(llvm::raw_ostream& out, bool include_singletons = false) const
  69. -> void {
  70. Yaml::Print(out, OutputYaml(include_singletons));
  71. }
  72. auto OutputYaml(bool include_singletons) const -> Yaml::OutputMapping;
  73. // Collects memory usage of members.
  74. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  75. -> void;
  76. // Returns array bound value from the bound instruction.
  77. // TODO: Move this function elsewhere.
  78. auto GetArrayBoundValue(InstId bound_id) const -> std::optional<uint64_t> {
  79. if (auto bound = insts().TryGetAs<IntValue>(
  80. constant_values().GetConstantInstId(bound_id))) {
  81. return ints().Get(bound->int_id).getZExtValue();
  82. }
  83. return std::nullopt;
  84. }
  85. // Gets the pointee type of the given type, which must be a pointer type.
  86. // TODO: Move this function elsewhere.
  87. auto GetPointeeType(TypeId pointer_id) const -> TypeId {
  88. return types().GetTypeIdForTypeInstId(
  89. types().GetAs<PointerType>(pointer_id).pointee_id);
  90. }
  91. // Returns true if this file is an `impl`.
  92. auto is_impl() -> bool {
  93. return import_irs().Get(ImportIRId::ApiForImpl).sem_ir != nullptr;
  94. }
  95. auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
  96. auto package_id() const -> PackageNameId { return package_id_; }
  97. auto library_id() const -> LibraryNameId { return library_id_; }
  98. // Directly expose SharedValueStores members.
  99. auto identifiers() -> SharedValueStores::IdentifierStore& {
  100. return value_stores_->identifiers();
  101. }
  102. auto identifiers() const -> const SharedValueStores::IdentifierStore& {
  103. return value_stores_->identifiers();
  104. }
  105. auto ints() -> SharedValueStores::IntStore& { return value_stores_->ints(); }
  106. auto ints() const -> const SharedValueStores::IntStore& {
  107. return value_stores_->ints();
  108. }
  109. auto reals() -> SharedValueStores::RealStore& {
  110. return value_stores_->reals();
  111. }
  112. auto reals() const -> const SharedValueStores::RealStore& {
  113. return value_stores_->reals();
  114. }
  115. auto floats() -> SharedValueStores::FloatStore& {
  116. return value_stores_->floats();
  117. }
  118. auto floats() const -> const SharedValueStores::FloatStore& {
  119. return value_stores_->floats();
  120. }
  121. auto string_literal_values() -> SharedValueStores::StringLiteralStore& {
  122. return value_stores_->string_literal_values();
  123. }
  124. auto string_literal_values() const
  125. -> const SharedValueStores::StringLiteralStore& {
  126. return value_stores_->string_literal_values();
  127. }
  128. auto entity_names() -> EntityNameStore& { return entity_names_; }
  129. auto entity_names() const -> const EntityNameStore& { return entity_names_; }
  130. auto functions() -> ValueStore<FunctionId>& { return functions_; }
  131. auto functions() const -> const ValueStore<FunctionId>& { return functions_; }
  132. auto classes() -> ValueStore<ClassId>& { return classes_; }
  133. auto classes() const -> const ValueStore<ClassId>& { return classes_; }
  134. auto interfaces() -> ValueStore<InterfaceId>& { return interfaces_; }
  135. auto interfaces() const -> const ValueStore<InterfaceId>& {
  136. return interfaces_;
  137. }
  138. auto associated_constants() -> ValueStore<AssociatedConstantId>& {
  139. return associated_constants_;
  140. }
  141. auto associated_constants() const -> const ValueStore<AssociatedConstantId>& {
  142. return associated_constants_;
  143. }
  144. // TODO: Rename these to `facet_type_infos`.
  145. auto facet_types() -> CanonicalValueStore<FacetTypeId>& {
  146. return facet_types_;
  147. }
  148. auto facet_types() const -> const CanonicalValueStore<FacetTypeId>& {
  149. return facet_types_;
  150. }
  151. auto identified_facet_types() -> IdentifiedFacetTypeStore& {
  152. return identified_facet_types_;
  153. }
  154. auto identified_facet_types() const -> const IdentifiedFacetTypeStore& {
  155. return identified_facet_types_;
  156. }
  157. auto impls() -> ImplStore& { return impls_; }
  158. auto impls() const -> const ImplStore& { return impls_; }
  159. auto specific_interfaces() -> CanonicalValueStore<SpecificInterfaceId>& {
  160. return specific_interfaces_;
  161. }
  162. auto specific_interfaces() const
  163. -> const CanonicalValueStore<SpecificInterfaceId>& {
  164. return specific_interfaces_;
  165. }
  166. auto generics() -> GenericStore& { return generics_; }
  167. auto generics() const -> const GenericStore& { return generics_; }
  168. auto specifics() -> SpecificStore& { return specifics_; }
  169. auto specifics() const -> const SpecificStore& { return specifics_; }
  170. auto import_irs() -> ValueStore<ImportIRId>& { return import_irs_; }
  171. auto import_irs() const -> const ValueStore<ImportIRId>& {
  172. return import_irs_;
  173. }
  174. auto import_ir_insts() -> ValueStore<ImportIRInstId>& {
  175. return import_ir_insts_;
  176. }
  177. auto import_ir_insts() const -> const ValueStore<ImportIRInstId>& {
  178. return import_ir_insts_;
  179. }
  180. auto import_cpps() -> ValueStore<ImportCppId>& { return import_cpps_; }
  181. auto import_cpps() const -> const ValueStore<ImportCppId>& {
  182. return import_cpps_;
  183. }
  184. auto cpp_ast() -> clang::ASTUnit* { return cpp_ast_; }
  185. auto cpp_ast() const -> const clang::ASTUnit* { return cpp_ast_; }
  186. // TODO: When the AST can be created before creating `File`, initialize the
  187. // pointer in the constructor and remove this function. This is part of
  188. // https://github.com/carbon-language/carbon-lang/issues/4666
  189. auto set_cpp_ast(clang::ASTUnit* cpp_ast) -> void { cpp_ast_ = cpp_ast; }
  190. auto names() const -> NameStoreWrapper {
  191. return NameStoreWrapper(&identifiers());
  192. }
  193. auto name_scopes() -> NameScopeStore& { return name_scopes_; }
  194. auto name_scopes() const -> const NameScopeStore& { return name_scopes_; }
  195. auto struct_type_fields() -> StructTypeFieldsStore& {
  196. return struct_type_fields_;
  197. }
  198. auto struct_type_fields() const -> const StructTypeFieldsStore& {
  199. return struct_type_fields_;
  200. }
  201. auto types() -> TypeStore& { return types_; }
  202. auto types() const -> const TypeStore& { return types_; }
  203. auto insts() -> InstStore& { return insts_; }
  204. auto insts() const -> const InstStore& { return insts_; }
  205. auto constant_values() -> ConstantValueStore& { return constant_values_; }
  206. auto constant_values() const -> const ConstantValueStore& {
  207. return constant_values_;
  208. }
  209. auto inst_blocks() -> InstBlockStore& { return inst_blocks_; }
  210. auto inst_blocks() const -> const InstBlockStore& { return inst_blocks_; }
  211. auto constants() -> ConstantStore& { return constants_; }
  212. auto constants() const -> const ConstantStore& { return constants_; }
  213. auto expr_regions() -> ValueStore<ExprRegionId>& { return expr_regions_; }
  214. auto expr_regions() const -> const ValueStore<ExprRegionId>& {
  215. return expr_regions_;
  216. }
  217. auto clang_source_locs() -> ValueStore<ClangSourceLocId>& {
  218. return clang_source_locs_;
  219. }
  220. auto clang_source_locs() const -> const ValueStore<ClangSourceLocId>& {
  221. return clang_source_locs_;
  222. }
  223. auto top_inst_block_id() const -> InstBlockId { return top_inst_block_id_; }
  224. auto set_top_inst_block_id(InstBlockId block_id) -> void {
  225. top_inst_block_id_ = block_id;
  226. }
  227. auto global_ctor_id() const -> FunctionId { return global_ctor_id_; }
  228. auto set_global_ctor_id(FunctionId function_id) -> void {
  229. global_ctor_id_ = function_id;
  230. }
  231. // Returns true if there were errors creating the semantics IR.
  232. auto has_errors() const -> bool { return has_errors_; }
  233. auto set_has_errors(bool has_errors) -> void { has_errors_ = has_errors; }
  234. auto filename() const -> llvm::StringRef { return filename_; }
  235. auto parse_tree() const -> const Parse::Tree& { return *parse_tree_; }
  236. private:
  237. const Parse::Tree* parse_tree_;
  238. // True if parts of the IR may be invalid.
  239. bool has_errors_ = false;
  240. // The file's ID.
  241. CheckIRId check_ir_id_;
  242. // The file's package.
  243. PackageNameId package_id_ = PackageNameId::None;
  244. // The file's library.
  245. LibraryNameId library_id_ = LibraryNameId::None;
  246. // Shared, compile-scoped values.
  247. SharedValueStores* value_stores_;
  248. // Slab allocator, used to allocate instruction and type blocks.
  249. llvm::BumpPtrAllocator allocator_;
  250. // The associated filename.
  251. // TODO: If SemIR starts linking back to tokens, reuse its filename.
  252. std::string filename_;
  253. // Storage for EntityNames.
  254. EntityNameStore entity_names_;
  255. // Storage for callable objects.
  256. ValueStore<FunctionId> functions_;
  257. // Storage for classes.
  258. ValueStore<ClassId> classes_;
  259. // Storage for interfaces.
  260. ValueStore<InterfaceId> interfaces_;
  261. // Storage for associated constants.
  262. ValueStore<AssociatedConstantId> associated_constants_;
  263. // Storage for facet types.
  264. CanonicalValueStore<FacetTypeId> facet_types_;
  265. // Storage for identified facet types.
  266. IdentifiedFacetTypeStore identified_facet_types_;
  267. // Storage for impls.
  268. ImplStore impls_;
  269. // Storage for specific interfaces, which are an individual unit of impl
  270. // lookup for a single interface.
  271. CanonicalValueStore<SpecificInterfaceId> specific_interfaces_;
  272. // Storage for generics.
  273. GenericStore generics_;
  274. // Storage for specifics.
  275. SpecificStore specifics_;
  276. // Related IRs. There are some fixed entries at the start; see ImportIRId.
  277. ValueStore<ImportIRId> import_irs_;
  278. // Related IR instructions. These are created for LocIds for instructions
  279. // that are import-related.
  280. ValueStore<ImportIRInstId> import_ir_insts_;
  281. // List of Cpp imports.
  282. ValueStore<ImportCppId> import_cpps_;
  283. // The Clang AST to use when looking up `Cpp` names. Null if there are no
  284. // `Cpp` imports.
  285. clang::ASTUnit* cpp_ast_ = nullptr;
  286. // All instructions. The first entries will always be the singleton
  287. // instructions.
  288. InstStore insts_;
  289. // Storage for name scopes.
  290. NameScopeStore name_scopes_ = NameScopeStore(this);
  291. // Constant values for instructions.
  292. ConstantValueStore constant_values_;
  293. // Instruction blocks within the IR. These reference entries in
  294. // insts_. Storage for the data is provided by allocator_.
  295. InstBlockStore inst_blocks_;
  296. // The top instruction block ID.
  297. InstBlockId top_inst_block_id_ = InstBlockId::None;
  298. // The global constructor function id.
  299. FunctionId global_ctor_id_ = FunctionId::None;
  300. // Storage for instructions that represent computed global constants, such as
  301. // types.
  302. ConstantStore constants_;
  303. // Storage for StructTypeField lists.
  304. StructTypeFieldsStore struct_type_fields_ = StructTypeFieldsStore(allocator_);
  305. // Descriptions of types used in this file.
  306. TypeStore types_ = TypeStore(this);
  307. // Single-entry/single-exit regions that are referenced as units, e.g. because
  308. // they represent expressions.
  309. ValueStore<ExprRegionId> expr_regions_;
  310. // C++ source locations for C++ interop.
  311. ValueStore<ClangSourceLocId> clang_source_locs_;
  312. };
  313. } // namespace Carbon::SemIR
  314. #endif // CARBON_TOOLCHAIN_SEM_IR_FILE_H_