file.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 -> uint64_t {
  73. return ints().Get(insts().GetAs<IntValue>(bound_id).int_id).getZExtValue();
  74. }
  75. // Gets the pointee type of the given type, which must be a pointer type.
  76. auto GetPointeeType(TypeId pointer_id) const -> TypeId {
  77. return types().GetAs<PointerType>(pointer_id).pointee_id;
  78. }
  79. auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
  80. auto package_id() const -> IdentifierId { return package_id_; }
  81. auto library_id() const -> SemIR::LibraryNameId { return library_id_; }
  82. // Directly expose SharedValueStores members.
  83. auto identifiers() -> SharedValueStores::IdentifierStore& {
  84. return value_stores_->identifiers();
  85. }
  86. auto identifiers() const -> const SharedValueStores::IdentifierStore& {
  87. return value_stores_->identifiers();
  88. }
  89. auto ints() -> SharedValueStores::IntStore& { return value_stores_->ints(); }
  90. auto ints() const -> const SharedValueStores::IntStore& {
  91. return value_stores_->ints();
  92. }
  93. auto reals() -> SharedValueStores::RealStore& {
  94. return value_stores_->reals();
  95. }
  96. auto reals() const -> const SharedValueStores::RealStore& {
  97. return value_stores_->reals();
  98. }
  99. auto floats() -> SharedValueStores::FloatStore& {
  100. return value_stores_->floats();
  101. }
  102. auto floats() const -> const SharedValueStores::FloatStore& {
  103. return value_stores_->floats();
  104. }
  105. auto string_literal_values() -> SharedValueStores::StringLiteralStore& {
  106. return value_stores_->string_literal_values();
  107. }
  108. auto string_literal_values() const
  109. -> const SharedValueStores::StringLiteralStore& {
  110. return value_stores_->string_literal_values();
  111. }
  112. auto entity_names() -> EntityNameStore& { return entity_names_; }
  113. auto entity_names() const -> const EntityNameStore& { return entity_names_; }
  114. auto functions() -> ValueStore<FunctionId>& { return functions_; }
  115. auto functions() const -> const ValueStore<FunctionId>& { return functions_; }
  116. auto classes() -> ValueStore<ClassId>& { return classes_; }
  117. auto classes() const -> const ValueStore<ClassId>& { return classes_; }
  118. auto interfaces() -> ValueStore<InterfaceId>& { return interfaces_; }
  119. auto interfaces() const -> const ValueStore<InterfaceId>& {
  120. return interfaces_;
  121. }
  122. auto facet_types() -> CanonicalValueStore<FacetTypeId>& {
  123. return facet_types_;
  124. }
  125. auto facet_types() const -> const CanonicalValueStore<FacetTypeId>& {
  126. return facet_types_;
  127. }
  128. auto impls() -> ImplStore& { return impls_; }
  129. auto impls() const -> const ImplStore& { return impls_; }
  130. auto generics() -> GenericStore& { return generics_; }
  131. auto generics() const -> const GenericStore& { return generics_; }
  132. auto specifics() -> SpecificStore& { return specifics_; }
  133. auto specifics() const -> const SpecificStore& { return specifics_; }
  134. auto import_irs() -> ValueStore<ImportIRId>& { return import_irs_; }
  135. auto import_irs() const -> const ValueStore<ImportIRId>& {
  136. return import_irs_;
  137. }
  138. auto import_ir_insts() -> ValueStore<ImportIRInstId>& {
  139. return import_ir_insts_;
  140. }
  141. auto import_ir_insts() const -> const ValueStore<ImportIRInstId>& {
  142. return import_ir_insts_;
  143. }
  144. auto names() const -> NameStoreWrapper {
  145. return NameStoreWrapper(&identifiers());
  146. }
  147. auto name_scopes() -> NameScopeStore& { return name_scopes_; }
  148. auto name_scopes() const -> const NameScopeStore& { return name_scopes_; }
  149. auto struct_type_fields() -> StructTypeFieldsStore& {
  150. return struct_type_fields_;
  151. }
  152. auto struct_type_fields() const -> const StructTypeFieldsStore& {
  153. return struct_type_fields_;
  154. }
  155. auto types() -> TypeStore& { return types_; }
  156. auto types() const -> const TypeStore& { return types_; }
  157. auto type_blocks() -> BlockValueStore<TypeBlockId>& { return type_blocks_; }
  158. auto type_blocks() const -> const BlockValueStore<TypeBlockId>& {
  159. return type_blocks_;
  160. }
  161. auto insts() -> InstStore& { return insts_; }
  162. auto insts() const -> const InstStore& { return insts_; }
  163. auto constant_values() -> ConstantValueStore& { return constant_values_; }
  164. auto constant_values() const -> const ConstantValueStore& {
  165. return constant_values_;
  166. }
  167. auto inst_blocks() -> InstBlockStore& { return inst_blocks_; }
  168. auto inst_blocks() const -> const InstBlockStore& { return inst_blocks_; }
  169. auto constants() -> ConstantStore& { return constants_; }
  170. auto constants() const -> const ConstantStore& { return constants_; }
  171. auto expr_regions() -> ValueStore<ExprRegionId>& { return expr_regions_; }
  172. auto expr_regions() const -> const ValueStore<ExprRegionId>& {
  173. return expr_regions_;
  174. }
  175. auto top_inst_block_id() const -> InstBlockId { return top_inst_block_id_; }
  176. auto set_top_inst_block_id(InstBlockId block_id) -> void {
  177. top_inst_block_id_ = block_id;
  178. }
  179. auto global_ctor_id() const -> FunctionId { return global_ctor_id_; }
  180. auto set_global_ctor_id(FunctionId function_id) -> void {
  181. global_ctor_id_ = function_id;
  182. }
  183. // Returns true if there were errors creating the semantics IR.
  184. auto has_errors() const -> bool { return has_errors_; }
  185. auto set_has_errors(bool has_errors) -> void { has_errors_ = has_errors; }
  186. auto filename() const -> llvm::StringRef { return filename_; }
  187. auto parse_tree() const -> const Parse::Tree& { return *parse_tree_; }
  188. private:
  189. const Parse::Tree* parse_tree_;
  190. // True if parts of the IR may be invalid.
  191. bool has_errors_ = false;
  192. // The file's ID.
  193. CheckIRId check_ir_id_;
  194. // The file's package.
  195. IdentifierId package_id_ = IdentifierId::Invalid;
  196. // The file's library.
  197. LibraryNameId library_id_ = LibraryNameId::Invalid;
  198. // Shared, compile-scoped values.
  199. SharedValueStores* value_stores_;
  200. // Slab allocator, used to allocate instruction and type blocks.
  201. llvm::BumpPtrAllocator allocator_;
  202. // The associated filename.
  203. // TODO: If SemIR starts linking back to tokens, reuse its filename.
  204. std::string filename_;
  205. // Storage for EntityNames.
  206. EntityNameStore entity_names_;
  207. // Storage for callable objects.
  208. ValueStore<FunctionId> functions_;
  209. // Storage for classes.
  210. ValueStore<ClassId> classes_;
  211. // Storage for interfaces.
  212. ValueStore<InterfaceId> interfaces_;
  213. // Storage for facet types.
  214. CanonicalValueStore<FacetTypeId> facet_types_;
  215. // Storage for impls.
  216. ImplStore impls_;
  217. // Storage for generics.
  218. GenericStore generics_;
  219. // Storage for specifics.
  220. SpecificStore specifics_;
  221. // Related IRs. There are some fixed entries at the start; see ImportIRId.
  222. ValueStore<ImportIRId> import_irs_;
  223. // Related IR instructions. These are created for LocIds for instructions
  224. // that are import-related.
  225. ValueStore<ImportIRInstId> import_ir_insts_;
  226. // Type blocks within the IR. These reference entries in types_. Storage for
  227. // the data is provided by allocator_.
  228. BlockValueStore<TypeBlockId> type_blocks_;
  229. // All instructions. The first entries will always be the singleton
  230. // instructions.
  231. InstStore insts_;
  232. // Storage for name scopes.
  233. NameScopeStore name_scopes_ = NameScopeStore(this);
  234. // Constant values for instructions.
  235. ConstantValueStore constant_values_;
  236. // Instruction blocks within the IR. These reference entries in
  237. // insts_. Storage for the data is provided by allocator_.
  238. InstBlockStore inst_blocks_;
  239. // The top instruction block ID.
  240. InstBlockId top_inst_block_id_ = InstBlockId::Invalid;
  241. // The global constructor function id.
  242. FunctionId global_ctor_id_ = FunctionId::Invalid;
  243. // Storage for instructions that represent computed global constants, such as
  244. // types.
  245. ConstantStore constants_;
  246. // Storage for StructTypeField lists.
  247. StructTypeFieldsStore struct_type_fields_ = StructTypeFieldsStore(allocator_);
  248. // Descriptions of types used in this file.
  249. TypeStore types_ = TypeStore(this);
  250. // Single-entry/single-exit regions that are referenced as units, e.g. because
  251. // they represent expressions.
  252. ValueStore<ExprRegionId> expr_regions_;
  253. };
  254. // The expression category of a sem_ir instruction. See /docs/design/values.md
  255. // for details.
  256. enum class ExprCategory : int8_t {
  257. // This instruction does not correspond to an expression, and as such has no
  258. // category.
  259. NotExpr,
  260. // The category of this instruction is not known due to an error.
  261. Error,
  262. // This instruction represents a value expression.
  263. Value,
  264. // This instruction represents a durable reference expression, that denotes an
  265. // object that outlives the current full expression context.
  266. DurableRef,
  267. // This instruction represents an ephemeral reference expression, that denotes
  268. // an
  269. // object that does not outlive the current full expression context.
  270. EphemeralRef,
  271. // This instruction represents an initializing expression, that describes how
  272. // to
  273. // initialize an object.
  274. Initializing,
  275. // This instruction represents a syntactic combination of expressions that are
  276. // permitted to have different expression categories. This is used for tuple
  277. // and struct literals, where the subexpressions for different elements can
  278. // have different categories.
  279. Mixed,
  280. Last = Mixed
  281. };
  282. // Returns the expression category for an instruction.
  283. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory;
  284. } // namespace Carbon::SemIR
  285. #endif // CARBON_TOOLCHAIN_SEM_IR_FILE_H_