file.h 12 KB

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