file.h 12 KB

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