file.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/bind_name.h"
  14. #include "toolchain/sem_ir/class.h"
  15. #include "toolchain/sem_ir/constant.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, SharedValueStores& value_stores,
  33. std::string filename);
  34. File(const File&) = delete;
  35. auto operator=(const File&) -> File& = delete;
  36. // Verifies that invariants of the semantics IR hold.
  37. auto Verify() const -> ErrorOr<Success>;
  38. // Prints the full IR. Allow omitting builtins so that unrelated changes are
  39. // less likely to alter test golden files.
  40. // TODO: In the future, the things to print may change, for example by adding
  41. // preludes. We may then want the ability to omit other things similar to
  42. // builtins.
  43. auto Print(llvm::raw_ostream& out, bool include_builtins = false) const
  44. -> void {
  45. Yaml::Print(out, OutputYaml(include_builtins));
  46. }
  47. auto OutputYaml(bool include_builtins) const -> Yaml::OutputMapping;
  48. // Returns array bound value from the bound instruction.
  49. auto GetArrayBoundValue(InstId bound_id) const -> uint64_t {
  50. return ints()
  51. .Get(insts().GetAs<IntLiteral>(bound_id).int_id)
  52. .getZExtValue();
  53. }
  54. // Marks a type as complete, and sets its value representation.
  55. auto CompleteType(TypeId object_type_id, ValueRepr value_repr) -> void {
  56. if (object_type_id.index < 0) {
  57. // We already know our builtin types are complete.
  58. return;
  59. }
  60. CARBON_CHECK(types().Get(object_type_id).value_repr.kind ==
  61. ValueRepr::Unknown)
  62. << "Type " << object_type_id << " completed more than once";
  63. types().Get(object_type_id).value_repr = value_repr;
  64. complete_types_.push_back(object_type_id);
  65. }
  66. // Gets the pointee type of the given type, which must be a pointer type.
  67. auto GetPointeeType(TypeId pointer_id) const -> TypeId {
  68. return types().GetAs<PointerType>(pointer_id).pointee_id;
  69. }
  70. // Produces a string version of a type.
  71. auto StringifyType(TypeId type_id) const -> std::string;
  72. // Same, but with a constant ID rather than a type ID.
  73. auto StringifyType(ConstantId type_const_id) const -> std::string;
  74. // Same as `StringifyType`, but starting with an instruction representing a
  75. // type expression rather than a canonical type.
  76. auto StringifyTypeExpr(InstId outer_inst_id) const -> std::string;
  77. auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
  78. // Directly expose SharedValueStores members.
  79. auto identifiers() -> StringStoreWrapper<IdentifierId>& {
  80. return value_stores_->identifiers();
  81. }
  82. auto identifiers() const -> const StringStoreWrapper<IdentifierId>& {
  83. return value_stores_->identifiers();
  84. }
  85. auto ints() -> CanonicalValueStore<IntId>& { return value_stores_->ints(); }
  86. auto ints() const -> const CanonicalValueStore<IntId>& {
  87. return value_stores_->ints();
  88. }
  89. auto reals() -> ValueStore<RealId>& { return value_stores_->reals(); }
  90. auto reals() const -> const ValueStore<RealId>& {
  91. return value_stores_->reals();
  92. }
  93. auto floats() -> FloatValueStore& { return value_stores_->floats(); }
  94. auto floats() const -> const FloatValueStore& {
  95. return value_stores_->floats();
  96. }
  97. auto string_literal_values() -> StringStoreWrapper<StringLiteralValueId>& {
  98. return value_stores_->string_literal_values();
  99. }
  100. auto string_literal_values() const
  101. -> const StringStoreWrapper<StringLiteralValueId>& {
  102. return value_stores_->string_literal_values();
  103. }
  104. auto bind_names() -> BindNameStore& { return bind_names_; }
  105. auto bind_names() const -> const BindNameStore& { return bind_names_; }
  106. auto functions() -> ValueStore<FunctionId>& { return functions_; }
  107. auto functions() const -> const ValueStore<FunctionId>& { return functions_; }
  108. auto classes() -> ValueStore<ClassId>& { return classes_; }
  109. auto classes() const -> const ValueStore<ClassId>& { return classes_; }
  110. auto interfaces() -> ValueStore<InterfaceId>& { return interfaces_; }
  111. auto interfaces() const -> const ValueStore<InterfaceId>& {
  112. return interfaces_;
  113. }
  114. auto impls() -> ImplStore& { return impls_; }
  115. auto impls() const -> const ImplStore& { return impls_; }
  116. auto generics() -> ValueStore<GenericId>& { return generics_; }
  117. auto generics() const -> const ValueStore<GenericId>& { return generics_; }
  118. auto generic_instances() -> GenericInstanceStore& {
  119. return generic_instances_;
  120. }
  121. auto generic_instances() const -> const GenericInstanceStore& {
  122. return generic_instances_;
  123. }
  124. auto import_irs() -> ValueStore<ImportIRId>& { return import_irs_; }
  125. auto import_irs() const -> const ValueStore<ImportIRId>& {
  126. return import_irs_;
  127. }
  128. auto import_ir_insts() -> ValueStore<ImportIRInstId>& {
  129. return import_ir_insts_;
  130. }
  131. auto import_ir_insts() const -> const ValueStore<ImportIRInstId>& {
  132. return import_ir_insts_;
  133. }
  134. auto names() const -> NameStoreWrapper {
  135. return NameStoreWrapper(&identifiers());
  136. }
  137. auto name_scopes() -> NameScopeStore& { return name_scopes_; }
  138. auto name_scopes() const -> const NameScopeStore& { return name_scopes_; }
  139. auto types() -> TypeStore& { return types_; }
  140. auto types() const -> const TypeStore& { return types_; }
  141. auto type_blocks() -> BlockValueStore<TypeBlockId>& { return type_blocks_; }
  142. auto type_blocks() const -> const BlockValueStore<TypeBlockId>& {
  143. return type_blocks_;
  144. }
  145. auto insts() -> InstStore& { return insts_; }
  146. auto insts() const -> const InstStore& { return insts_; }
  147. auto constant_values() -> ConstantValueStore& { return constant_values_; }
  148. auto constant_values() const -> const ConstantValueStore& {
  149. return constant_values_;
  150. }
  151. auto inst_blocks() -> InstBlockStore& { return inst_blocks_; }
  152. auto inst_blocks() const -> const InstBlockStore& { return inst_blocks_; }
  153. auto constants() -> ConstantStore& { return constants_; }
  154. auto constants() const -> const ConstantStore& { return constants_; }
  155. // A list of types that were completed in this file, in the order in which
  156. // they were completed. Earlier types in this list cannot contain instances of
  157. // later types.
  158. auto complete_types() const -> llvm::ArrayRef<TypeId> {
  159. return complete_types_;
  160. }
  161. auto top_inst_block_id() const -> InstBlockId { return top_inst_block_id_; }
  162. auto set_top_inst_block_id(InstBlockId block_id) -> void {
  163. top_inst_block_id_ = block_id;
  164. }
  165. // Returns true if there were errors creating the semantics IR.
  166. auto has_errors() const -> bool { return has_errors_; }
  167. auto set_has_errors(bool has_errors) -> void { has_errors_ = has_errors; }
  168. auto filename() const -> llvm::StringRef { return filename_; }
  169. private:
  170. bool has_errors_ = false;
  171. CheckIRId check_ir_id_;
  172. // Shared, compile-scoped values.
  173. SharedValueStores* value_stores_;
  174. // Slab allocator, used to allocate instruction and type blocks.
  175. llvm::BumpPtrAllocator allocator_;
  176. // The associated filename.
  177. // TODO: If SemIR starts linking back to tokens, reuse its filename.
  178. std::string filename_;
  179. // Storage for bind names.
  180. BindNameStore bind_names_;
  181. // Storage for callable objects.
  182. ValueStore<FunctionId> functions_;
  183. // Storage for classes.
  184. ValueStore<ClassId> classes_;
  185. // Storage for interfaces.
  186. ValueStore<InterfaceId> interfaces_;
  187. // Storage for impls.
  188. ImplStore impls_;
  189. // Storage for generics.
  190. ValueStore<GenericId> generics_;
  191. // Storage for instances of generics.
  192. GenericInstanceStore generic_instances_;
  193. // Related IRs. There are some fixed entries at the start; see ImportIRId.
  194. ValueStore<ImportIRId> import_irs_;
  195. // Related IR instructions. These are created for LocIds for instructions
  196. // that are import-related.
  197. ValueStore<ImportIRInstId> import_ir_insts_;
  198. // Type blocks within the IR. These reference entries in types_. Storage for
  199. // the data is provided by allocator_.
  200. BlockValueStore<TypeBlockId> type_blocks_;
  201. // All instructions. The first entries will always be Builtin insts, at
  202. // indices matching BuiltinKind ordering.
  203. InstStore insts_;
  204. // Storage for name scopes.
  205. NameScopeStore name_scopes_;
  206. // Constant values for instructions.
  207. ConstantValueStore constant_values_;
  208. // Instruction blocks within the IR. These reference entries in
  209. // insts_. Storage for the data is provided by allocator_.
  210. InstBlockStore inst_blocks_;
  211. // The top instruction block ID.
  212. InstBlockId top_inst_block_id_ = InstBlockId::Invalid;
  213. // Storage for instructions that represent computed global constants, such as
  214. // types.
  215. ConstantStore constants_;
  216. // Descriptions of types used in this file.
  217. TypeStore types_ = TypeStore(&insts_, &constant_values_);
  218. // Types that were completed in this file.
  219. llvm::SmallVector<TypeId> complete_types_;
  220. };
  221. // The expression category of a sem_ir instruction. See /docs/design/values.md
  222. // for details.
  223. enum class ExprCategory : int8_t {
  224. // This instruction does not correspond to an expression, and as such has no
  225. // category.
  226. NotExpr,
  227. // The category of this instruction is not known due to an error.
  228. Error,
  229. // This instruction represents a value expression.
  230. Value,
  231. // This instruction represents a durable reference expression, that denotes an
  232. // object that outlives the current full expression context.
  233. DurableRef,
  234. // This instruction represents an ephemeral reference expression, that denotes
  235. // an
  236. // object that does not outlive the current full expression context.
  237. EphemeralRef,
  238. // This instruction represents an initializing expression, that describes how
  239. // to
  240. // initialize an object.
  241. Initializing,
  242. // This instruction represents a syntactic combination of expressions that are
  243. // permitted to have different expression categories. This is used for tuple
  244. // and struct literals, where the subexpressions for different elements can
  245. // have different categories.
  246. Mixed,
  247. Last = Mixed
  248. };
  249. // Returns the expression category for an instruction.
  250. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory;
  251. // Returns information about the value representation to use for a type.
  252. inline auto GetValueRepr(const File& file, TypeId type_id) -> ValueRepr {
  253. return file.types().GetValueRepr(type_id);
  254. }
  255. // The initializing representation to use when returning by value.
  256. struct InitRepr {
  257. enum Kind : int8_t {
  258. // The type has no initializing representation. This is used for empty
  259. // types, where no initialization is necessary.
  260. None,
  261. // An initializing expression produces an object representation by value,
  262. // which is copied into the initialized object.
  263. ByCopy,
  264. // An initializing expression takes a location as input, which is
  265. // initialized as a side effect of evaluating the expression.
  266. InPlace,
  267. // TODO: Consider adding a kind where the expression takes an advisory
  268. // location and returns a value plus an indicator of whether the location
  269. // was actually initialized.
  270. };
  271. // The kind of initializing representation used by this type.
  272. Kind kind;
  273. // Returns whether a return slot is used when returning this type.
  274. auto has_return_slot() const -> bool { return kind == InPlace; }
  275. };
  276. // Returns information about the initializing representation to use for a type.
  277. auto GetInitRepr(const File& file, TypeId type_id) -> InitRepr;
  278. } // namespace Carbon::SemIR
  279. #endif // CARBON_TOOLCHAIN_SEM_IR_FILE_H_