file.h 10 KB

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