file.h 11 KB

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