file.h 13 KB

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