file.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. << ", index: " << bind_index << "}";
  30. }
  31. // The name.
  32. NameId name_id;
  33. // The enclosing scope.
  34. NameScopeId enclosing_scope_id;
  35. // The index for a compile-time binding. Invalid for a runtime binding.
  36. CompileTimeBindIndex bind_index;
  37. };
  38. class File;
  39. // Provides semantic analysis on a Parse::Tree.
  40. class File : public Printable<File> {
  41. public:
  42. // Starts a new file for Check::CheckParseTree.
  43. explicit File(CheckIRId check_ir_id, SharedValueStores& value_stores,
  44. std::string filename);
  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. auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
  87. // Directly expose SharedValueStores members.
  88. auto identifiers() -> StringStoreWrapper<IdentifierId>& {
  89. return value_stores_->identifiers();
  90. }
  91. auto identifiers() const -> const StringStoreWrapper<IdentifierId>& {
  92. return value_stores_->identifiers();
  93. }
  94. auto ints() -> ValueStore<IntId>& { return value_stores_->ints(); }
  95. auto ints() const -> const ValueStore<IntId>& {
  96. return value_stores_->ints();
  97. }
  98. auto reals() -> ValueStore<RealId>& { return value_stores_->reals(); }
  99. auto reals() const -> const ValueStore<RealId>& {
  100. return value_stores_->reals();
  101. }
  102. auto floats() -> ValueStore<FloatId>& { return value_stores_->floats(); }
  103. auto floats() const -> const ValueStore<FloatId>& {
  104. return value_stores_->floats();
  105. }
  106. auto string_literal_values() -> StringStoreWrapper<StringLiteralValueId>& {
  107. return value_stores_->string_literal_values();
  108. }
  109. auto string_literal_values() const
  110. -> const StringStoreWrapper<StringLiteralValueId>& {
  111. return value_stores_->string_literal_values();
  112. }
  113. auto bind_names() -> ValueStore<BindNameId>& { return bind_names_; }
  114. auto bind_names() const -> const ValueStore<BindNameId>& {
  115. return bind_names_;
  116. }
  117. auto functions() -> ValueStore<FunctionId>& { return functions_; }
  118. auto functions() const -> const ValueStore<FunctionId>& { return functions_; }
  119. auto classes() -> ValueStore<ClassId>& { return classes_; }
  120. auto classes() const -> const ValueStore<ClassId>& { return classes_; }
  121. auto interfaces() -> ValueStore<InterfaceId>& { return interfaces_; }
  122. auto interfaces() const -> const ValueStore<InterfaceId>& {
  123. return interfaces_;
  124. }
  125. auto impls() -> ImplStore& { return impls_; }
  126. auto impls() const -> const ImplStore& { return impls_; }
  127. auto import_irs() -> ValueStore<ImportIRId>& { return import_irs_; }
  128. auto import_irs() const -> const ValueStore<ImportIRId>& {
  129. return import_irs_;
  130. }
  131. auto import_ir_insts() -> ValueStore<ImportIRInstId>& {
  132. return import_ir_insts_;
  133. }
  134. auto import_ir_insts() const -> const ValueStore<ImportIRInstId>& {
  135. return import_ir_insts_;
  136. }
  137. auto names() const -> NameStoreWrapper {
  138. return NameStoreWrapper(&identifiers());
  139. }
  140. auto name_scopes() -> NameScopeStore& { return name_scopes_; }
  141. auto name_scopes() const -> const NameScopeStore& { return name_scopes_; }
  142. auto types() -> TypeStore& { return types_; }
  143. auto types() const -> const TypeStore& { return types_; }
  144. auto type_blocks() -> BlockValueStore<TypeBlockId>& { return type_blocks_; }
  145. auto type_blocks() const -> const BlockValueStore<TypeBlockId>& {
  146. return type_blocks_;
  147. }
  148. auto insts() -> InstStore& { return insts_; }
  149. auto insts() const -> const InstStore& { return insts_; }
  150. auto constant_values() -> ConstantValueStore& { return constant_values_; }
  151. auto constant_values() const -> const ConstantValueStore& {
  152. return constant_values_;
  153. }
  154. auto inst_blocks() -> InstBlockStore& { return inst_blocks_; }
  155. auto inst_blocks() const -> const InstBlockStore& { return inst_blocks_; }
  156. auto constants() -> ConstantStore& { return constants_; }
  157. auto constants() const -> const ConstantStore& { return constants_; }
  158. // A list of types that were completed in this file, in the order in which
  159. // they were completed. Earlier types in this list cannot contain instances of
  160. // later types.
  161. auto complete_types() const -> llvm::ArrayRef<TypeId> {
  162. return complete_types_;
  163. }
  164. auto top_inst_block_id() const -> InstBlockId { return top_inst_block_id_; }
  165. auto set_top_inst_block_id(InstBlockId block_id) -> void {
  166. top_inst_block_id_ = block_id;
  167. }
  168. // Returns true if there were errors creating the semantics IR.
  169. auto has_errors() const -> bool { return has_errors_; }
  170. auto set_has_errors(bool has_errors) -> void { has_errors_ = has_errors; }
  171. auto filename() const -> llvm::StringRef { return filename_; }
  172. private:
  173. bool has_errors_ = false;
  174. CheckIRId check_ir_id_;
  175. // Shared, compile-scoped values.
  176. SharedValueStores* value_stores_;
  177. // Slab allocator, used to allocate instruction and type blocks.
  178. llvm::BumpPtrAllocator allocator_;
  179. // The associated filename.
  180. // TODO: If SemIR starts linking back to tokens, reuse its filename.
  181. std::string filename_;
  182. // Storage for bind names.
  183. ValueStore<BindNameId> bind_names_;
  184. // Storage for callable objects.
  185. ValueStore<FunctionId> functions_;
  186. // Storage for classes.
  187. ValueStore<ClassId> classes_;
  188. // Storage for interfaces.
  189. ValueStore<InterfaceId> interfaces_;
  190. // Storage for impls.
  191. ImplStore impls_;
  192. // Related IRs. There are some fixed entries at the start; see ImportIRId.
  193. ValueStore<ImportIRId> import_irs_;
  194. // Related IR instructions. These are created for LocIds for instructions
  195. // that are import-related.
  196. ValueStore<ImportIRInstId> import_ir_insts_;
  197. // Storage for name scopes.
  198. NameScopeStore name_scopes_;
  199. // Type blocks within the IR. These reference entries in types_. Storage for
  200. // the data is provided by allocator_.
  201. BlockValueStore<TypeBlockId> type_blocks_;
  202. // All instructions. The first entries will always be Builtin insts, at
  203. // indices matching BuiltinKind ordering.
  204. InstStore insts_;
  205. // Constant values for instructions.
  206. ConstantValueStore constant_values_;
  207. // Instruction blocks within the IR. These reference entries in
  208. // insts_. Storage for the data is provided by allocator_.
  209. InstBlockStore inst_blocks_;
  210. // The top instruction block ID.
  211. InstBlockId top_inst_block_id_ = InstBlockId::Invalid;
  212. // Storage for instructions that represent computed global constants, such as
  213. // types.
  214. ConstantStore constants_;
  215. // Descriptions of types used in this file.
  216. TypeStore types_ = TypeStore(&insts_);
  217. // Types that were completed in this file.
  218. llvm::SmallVector<TypeId> complete_types_;
  219. };
  220. // The expression category of a sem_ir instruction. See /docs/design/values.md
  221. // for details.
  222. enum class ExprCategory : int8_t {
  223. // This instruction does not correspond to an expression, and as such has no
  224. // category.
  225. NotExpr,
  226. // The category of this instruction is not known due to an error.
  227. Error,
  228. // This instruction represents a value expression.
  229. Value,
  230. // This instruction represents a durable reference expression, that denotes an
  231. // object that outlives the current full expression context.
  232. DurableRef,
  233. // This instruction represents an ephemeral reference expression, that denotes
  234. // an
  235. // object that does not outlive the current full expression context.
  236. EphemeralRef,
  237. // This instruction represents an initializing expression, that describes how
  238. // to
  239. // initialize an object.
  240. Initializing,
  241. // This instruction represents a syntactic combination of expressions that are
  242. // permitted to have different expression categories. This is used for tuple
  243. // and struct literals, where the subexpressions for different elements can
  244. // have different categories.
  245. Mixed,
  246. Last = Mixed
  247. };
  248. // Returns the expression category for an instruction.
  249. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory;
  250. // Returns information about the value representation to use for a type.
  251. inline auto GetValueRepr(const File& file, TypeId type_id) -> ValueRepr {
  252. return file.types().GetValueRepr(type_id);
  253. }
  254. // The initializing representation to use when returning by value.
  255. struct InitRepr {
  256. enum Kind : int8_t {
  257. // The type has no initializing representation. This is used for empty
  258. // types, where no initialization is necessary.
  259. None,
  260. // An initializing expression produces an object representation by value,
  261. // which is copied into the initialized object.
  262. ByCopy,
  263. // An initializing expression takes a location as input, which is
  264. // initialized as a side effect of evaluating the expression.
  265. InPlace,
  266. // TODO: Consider adding a kind where the expression takes an advisory
  267. // location and returns a value plus an indicator of whether the location
  268. // was actually initialized.
  269. };
  270. // The kind of initializing representation used by this type.
  271. Kind kind;
  272. // Returns whether a return slot is used when returning this type.
  273. auto has_return_slot() const -> bool { return kind == InPlace; }
  274. };
  275. // Returns information about the initializing representation to use for a type.
  276. auto GetInitRepr(const File& file, TypeId type_id) -> InitRepr;
  277. } // namespace Carbon::SemIR
  278. #endif // CARBON_TOOLCHAIN_SEM_IR_FILE_H_