file.h 13 KB

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