file.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. // Produces a string version of a type.
  63. auto StringifyType(TypeId type_id) const -> std::string;
  64. // Same, but with a constant ID rather than a type ID.
  65. auto StringifyType(ConstantId type_const_id) const -> std::string;
  66. // Same as `StringifyType`, but starting with an instruction representing a
  67. // type expression rather than a canonical type.
  68. auto StringifyTypeExpr(InstId outer_inst_id) const -> std::string;
  69. auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
  70. auto package_id() const -> IdentifierId { return package_id_; }
  71. auto library_id() const -> SemIR::LibraryNameId { return library_id_; }
  72. // Directly expose SharedValueStores members.
  73. auto identifiers() -> CanonicalValueStore<IdentifierId>& {
  74. return value_stores_->identifiers();
  75. }
  76. auto identifiers() const -> const CanonicalValueStore<IdentifierId>& {
  77. return value_stores_->identifiers();
  78. }
  79. auto ints() -> CanonicalValueStore<IntId>& { return value_stores_->ints(); }
  80. auto ints() const -> const CanonicalValueStore<IntId>& {
  81. return value_stores_->ints();
  82. }
  83. auto reals() -> ValueStore<RealId>& { return value_stores_->reals(); }
  84. auto reals() const -> const ValueStore<RealId>& {
  85. return value_stores_->reals();
  86. }
  87. auto floats() -> FloatValueStore& { return value_stores_->floats(); }
  88. auto floats() const -> const FloatValueStore& {
  89. return value_stores_->floats();
  90. }
  91. auto string_literal_values() -> CanonicalValueStore<StringLiteralValueId>& {
  92. return value_stores_->string_literal_values();
  93. }
  94. auto string_literal_values() const
  95. -> const CanonicalValueStore<StringLiteralValueId>& {
  96. return value_stores_->string_literal_values();
  97. }
  98. auto entity_names() -> EntityNameStore& { return entity_names_; }
  99. auto entity_names() const -> const EntityNameStore& { return entity_names_; }
  100. auto functions() -> ValueStore<FunctionId>& { return functions_; }
  101. auto functions() const -> const ValueStore<FunctionId>& { return functions_; }
  102. auto classes() -> ValueStore<ClassId>& { return classes_; }
  103. auto classes() const -> const ValueStore<ClassId>& { return classes_; }
  104. auto interfaces() -> ValueStore<InterfaceId>& { return interfaces_; }
  105. auto interfaces() const -> const ValueStore<InterfaceId>& {
  106. return interfaces_;
  107. }
  108. auto impls() -> ImplStore& { return impls_; }
  109. auto impls() const -> const ImplStore& { return impls_; }
  110. auto generics() -> GenericStore& { return generics_; }
  111. auto generics() const -> const GenericStore& { return generics_; }
  112. auto specifics() -> SpecificStore& { return specifics_; }
  113. auto specifics() const -> const SpecificStore& { return specifics_; }
  114. auto import_irs() -> ValueStore<ImportIRId>& { return import_irs_; }
  115. auto import_irs() const -> const ValueStore<ImportIRId>& {
  116. return import_irs_;
  117. }
  118. auto import_ir_insts() -> ValueStore<ImportIRInstId>& {
  119. return import_ir_insts_;
  120. }
  121. auto import_ir_insts() const -> const ValueStore<ImportIRInstId>& {
  122. return import_ir_insts_;
  123. }
  124. auto names() const -> NameStoreWrapper {
  125. return NameStoreWrapper(&identifiers());
  126. }
  127. auto name_scopes() -> NameScopeStore& { return name_scopes_; }
  128. auto name_scopes() const -> const NameScopeStore& { return name_scopes_; }
  129. auto types() -> TypeStore& { return types_; }
  130. auto types() const -> const TypeStore& { return types_; }
  131. auto type_blocks() -> BlockValueStore<TypeBlockId>& { return type_blocks_; }
  132. auto type_blocks() const -> const BlockValueStore<TypeBlockId>& {
  133. return type_blocks_;
  134. }
  135. auto insts() -> InstStore& { return insts_; }
  136. auto insts() const -> const InstStore& { return insts_; }
  137. auto constant_values() -> ConstantValueStore& { return constant_values_; }
  138. auto constant_values() const -> const ConstantValueStore& {
  139. return constant_values_;
  140. }
  141. auto inst_blocks() -> InstBlockStore& { return inst_blocks_; }
  142. auto inst_blocks() const -> const InstBlockStore& { return inst_blocks_; }
  143. auto constants() -> ConstantStore& { return constants_; }
  144. auto constants() const -> const ConstantStore& { return constants_; }
  145. auto top_inst_block_id() const -> InstBlockId { return top_inst_block_id_; }
  146. auto set_top_inst_block_id(InstBlockId block_id) -> void {
  147. top_inst_block_id_ = block_id;
  148. }
  149. auto global_ctor_id() const -> FunctionId { return global_ctor_id_; }
  150. auto set_global_ctor_id(FunctionId function_id) -> void {
  151. global_ctor_id_ = function_id;
  152. }
  153. // Returns true if there were errors creating the semantics IR.
  154. auto has_errors() const -> bool { return has_errors_; }
  155. auto set_has_errors(bool has_errors) -> void { has_errors_ = has_errors; }
  156. auto filename() const -> llvm::StringRef { return filename_; }
  157. private:
  158. // True if parts of the IR may be invalid.
  159. bool has_errors_ = false;
  160. // The file's ID.
  161. CheckIRId check_ir_id_;
  162. // The file's package.
  163. IdentifierId package_id_ = IdentifierId::Invalid;
  164. // The file's library.
  165. LibraryNameId library_id_ = LibraryNameId::Invalid;
  166. // Shared, compile-scoped values.
  167. SharedValueStores* value_stores_;
  168. // Slab allocator, used to allocate instruction and type blocks.
  169. llvm::BumpPtrAllocator allocator_;
  170. // The associated filename.
  171. // TODO: If SemIR starts linking back to tokens, reuse its filename.
  172. std::string filename_;
  173. // Storage for EntityNames.
  174. EntityNameStore entity_names_;
  175. // Storage for callable objects.
  176. ValueStore<FunctionId> functions_;
  177. // Storage for classes.
  178. ValueStore<ClassId> classes_;
  179. // Storage for interfaces.
  180. ValueStore<InterfaceId> interfaces_;
  181. // Storage for impls.
  182. ImplStore impls_;
  183. // Storage for generics.
  184. GenericStore generics_;
  185. // Storage for specifics.
  186. SpecificStore specifics_;
  187. // Related IRs. There are some fixed entries at the start; see ImportIRId.
  188. ValueStore<ImportIRId> import_irs_;
  189. // Related IR instructions. These are created for LocIds for instructions
  190. // that are import-related.
  191. ValueStore<ImportIRInstId> import_ir_insts_;
  192. // Type blocks within the IR. These reference entries in types_. Storage for
  193. // the data is provided by allocator_.
  194. BlockValueStore<TypeBlockId> type_blocks_;
  195. // All instructions. The first entries will always be BuiltinInsts, at
  196. // indices matching BuiltinInstKind ordering.
  197. InstStore insts_;
  198. // Storage for name scopes.
  199. NameScopeStore name_scopes_;
  200. // Constant values for instructions.
  201. ConstantValueStore constant_values_;
  202. // Instruction blocks within the IR. These reference entries in
  203. // insts_. Storage for the data is provided by allocator_.
  204. InstBlockStore inst_blocks_;
  205. // The top instruction block ID.
  206. InstBlockId top_inst_block_id_ = InstBlockId::Invalid;
  207. // The global constructor function id.
  208. FunctionId global_ctor_id_ = FunctionId::Invalid;
  209. // Storage for instructions that represent computed global constants, such as
  210. // types.
  211. ConstantStore constants_;
  212. // Descriptions of types used in this file.
  213. TypeStore types_ = TypeStore(&insts_, &constant_values_);
  214. };
  215. // The expression category of a sem_ir instruction. See /docs/design/values.md
  216. // for details.
  217. enum class ExprCategory : int8_t {
  218. // This instruction does not correspond to an expression, and as such has no
  219. // category.
  220. NotExpr,
  221. // The category of this instruction is not known due to an error.
  222. Error,
  223. // This instruction represents a value expression.
  224. Value,
  225. // This instruction represents a durable reference expression, that denotes an
  226. // object that outlives the current full expression context.
  227. DurableRef,
  228. // This instruction represents an ephemeral reference expression, that denotes
  229. // an
  230. // object that does not outlive the current full expression context.
  231. EphemeralRef,
  232. // This instruction represents an initializing expression, that describes how
  233. // to
  234. // initialize an object.
  235. Initializing,
  236. // This instruction represents a syntactic combination of expressions that are
  237. // permitted to have different expression categories. This is used for tuple
  238. // and struct literals, where the subexpressions for different elements can
  239. // have different categories.
  240. Mixed,
  241. Last = Mixed
  242. };
  243. // Returns the expression category for an instruction.
  244. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory;
  245. } // namespace Carbon::SemIR
  246. #endif // CARBON_TOOLCHAIN_SEM_IR_FILE_H_