file.h 11 KB

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