| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
- // Exceptions. See /LICENSE for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- #ifndef CARBON_TOOLCHAIN_SEM_IR_FILE_H_
- #define CARBON_TOOLCHAIN_SEM_IR_FILE_H_
- #include "common/error.h"
- #include "llvm/ADT/SmallVector.h"
- #include "llvm/ADT/iterator_range.h"
- #include "llvm/Support/Allocator.h"
- #include "llvm/Support/FormatVariadic.h"
- #include "toolchain/base/canonical_value_store.h"
- #include "toolchain/base/int.h"
- #include "toolchain/base/relational_value_store.h"
- #include "toolchain/base/shared_value_stores.h"
- #include "toolchain/base/value_store.h"
- #include "toolchain/base/yaml.h"
- #include "toolchain/parse/tree.h"
- #include "toolchain/sem_ir/associated_constant.h"
- #include "toolchain/sem_ir/class.h"
- #include "toolchain/sem_ir/constant.h"
- #include "toolchain/sem_ir/cpp_file.h"
- #include "toolchain/sem_ir/cpp_global_var.h"
- #include "toolchain/sem_ir/cpp_overload_set.h"
- #include "toolchain/sem_ir/entity_name.h"
- #include "toolchain/sem_ir/facet_type_info.h"
- #include "toolchain/sem_ir/function.h"
- #include "toolchain/sem_ir/generic.h"
- #include "toolchain/sem_ir/ids.h"
- #include "toolchain/sem_ir/impl.h"
- #include "toolchain/sem_ir/import_cpp.h"
- #include "toolchain/sem_ir/import_ir.h"
- #include "toolchain/sem_ir/inst.h"
- #include "toolchain/sem_ir/interface.h"
- #include "toolchain/sem_ir/name.h"
- #include "toolchain/sem_ir/name_scope.h"
- #include "toolchain/sem_ir/named_constraint.h"
- #include "toolchain/sem_ir/require_impls.h"
- #include "toolchain/sem_ir/singleton_insts.h"
- #include "toolchain/sem_ir/specific_interface.h"
- #include "toolchain/sem_ir/struct_type_field.h"
- #include "toolchain/sem_ir/type.h"
- #include "toolchain/sem_ir/type_info.h"
- #include "toolchain/sem_ir/vtable.h"
- namespace Carbon::SemIR {
- // An expression that may contain control flow, represented as a
- // single-entry/single-exit region. `block_ids` are the blocks that are part of
- // evaluation of the expression, and `result_id` represents the result of
- // evaluating the expression. `block_ids` consists of all blocks that are
- // dominated by `block_ids.front()` and post-dominated by `block_ids.back()`,
- // and should be in lexical order. `result_id` will be in `block_ids.back()` or
- // some block that dominates it.
- //
- // `block_ids` cannot be empty. If it has a single element, then the region
- // should be used via a `SpliceBlock` inst. Otherwise, the region should be used
- // by branching to the entry block, and the last inst in the exit block will
- // likewise be a branch.
- struct ExprRegion {
- llvm::SmallVector<InstBlockId> block_ids;
- InstId result_id;
- };
- using ExprRegionStore = ValueStore<ExprRegionId, ExprRegion, Tag<CheckIRId>>;
- using CustomLayoutStore =
- BlockValueStore<CustomLayoutId, ObjectSize, Tag<CheckIRId>>;
- // The semantic IR for a single file.
- class File : public Printable<File> {
- public:
- // Starts a new file for Check::CheckParseTree.
- explicit File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
- const std::optional<Parse::Tree::PackagingDecl>& packaging_decl,
- SharedValueStores& value_stores, std::string filename);
- File(const File&) = delete;
- ~File();
- auto operator=(const File&) -> File& = delete;
- // Verifies that invariants of the semantics IR hold.
- auto Verify() const -> ErrorOr<Success>;
- // Prints the full IR. Allow omitting singletons so that changes to the list
- // of singletons won't churn golden test file content.
- auto Print(llvm::raw_ostream& out, bool include_singletons = false) const
- -> void {
- Yaml::Print(out, OutputYaml(include_singletons));
- }
- auto OutputYaml(bool include_singletons) const -> Yaml::OutputMapping;
- // Collects memory usage of members.
- auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
- -> void;
- // Returns the zero-extended integer value of the given instruction if it is a
- // constant integer.
- // TODO: Move this function elsewhere.
- auto GetZExtIntValue(InstId inst_id) const -> std::optional<uint64_t> {
- if (auto val = insts().TryGetAs<IntValue>(
- constant_values().GetConstantInstId(inst_id))) {
- return ints().Get(val->int_id).getZExtValue();
- }
- return std::nullopt;
- }
- // Returns the layout of a pointer.
- // TODO: This should depend on the target machine.
- auto GetPointerLayout() const -> ObjectLayout {
- return {.size = ObjectSize::Bytes(8), .alignment = ObjectSize::Bytes(8)};
- }
- // Gets the pointee type of the given type, which must be a pointer type.
- // TODO: Move this function elsewhere.
- auto GetPointeeType(TypeId pointer_id) const -> TypeId {
- return types().GetTypeIdForTypeInstId(
- types().GetAs<PointerType>(pointer_id).pointee_id);
- }
- // Returns true if this file is an `impl`.
- auto is_impl() -> bool {
- return import_irs().Get(ImportIRId::ApiForImpl).sem_ir != nullptr;
- }
- auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
- auto package_id() const -> PackageNameId { return package_id_; }
- auto library_id() const -> LibraryNameId { return library_id_; }
- // Directly expose SharedValueStores members.
- auto identifiers() -> SharedValueStores::IdentifierStore& {
- return value_stores_->identifiers();
- }
- auto identifiers() const -> const SharedValueStores::IdentifierStore& {
- return value_stores_->identifiers();
- }
- auto ints() -> SharedValueStores::IntStore& { return value_stores_->ints(); }
- auto ints() const -> const SharedValueStores::IntStore& {
- return value_stores_->ints();
- }
- auto reals() -> SharedValueStores::RealStore& {
- return value_stores_->reals();
- }
- auto reals() const -> const SharedValueStores::RealStore& {
- return value_stores_->reals();
- }
- auto floats() -> SharedValueStores::FloatStore& {
- return value_stores_->floats();
- }
- auto floats() const -> const SharedValueStores::FloatStore& {
- return value_stores_->floats();
- }
- auto string_literal_values() -> SharedValueStores::StringLiteralStore& {
- return value_stores_->string_literal_values();
- }
- auto string_literal_values() const
- -> const SharedValueStores::StringLiteralStore& {
- return value_stores_->string_literal_values();
- }
- auto entity_names() -> EntityNameStore& { return entity_names_; }
- auto entity_names() const -> const EntityNameStore& { return entity_names_; }
- auto cpp_global_vars() -> CppGlobalVarStore& { return cpp_global_vars_; }
- auto cpp_global_vars() const -> const CppGlobalVarStore& {
- return cpp_global_vars_;
- }
- auto functions() -> FunctionStore& { return functions_; }
- auto functions() const -> const FunctionStore& { return functions_; }
- auto cpp_overload_sets() -> CppOverloadSetStore& {
- return cpp_overload_sets_;
- }
- auto cpp_overload_sets() const -> const CppOverloadSetStore& {
- return cpp_overload_sets_;
- }
- auto classes() -> ClassStore& { return classes_; }
- auto classes() const -> const ClassStore& { return classes_; }
- auto interfaces() -> InterfaceStore& { return interfaces_; }
- auto interfaces() const -> const InterfaceStore& { return interfaces_; }
- auto named_constraints() -> NamedConstraintStore& {
- return named_constraints_;
- }
- auto named_constraints() const -> const NamedConstraintStore& {
- return named_constraints_;
- }
- auto require_impls() -> RequireImplsStore& { return require_impls_; }
- auto require_impls() const -> const RequireImplsStore& {
- return require_impls_;
- }
- auto require_impls_blocks() -> RequireImplsBlockStore& {
- return require_impls_blocks_;
- }
- auto require_impls_blocks() const -> const RequireImplsBlockStore& {
- return require_impls_blocks_;
- }
- auto associated_constants() -> AssociatedConstantStore& {
- return associated_constants_;
- }
- auto associated_constants() const -> const AssociatedConstantStore& {
- return associated_constants_;
- }
- // TODO: Rename these to `facet_type_infos`.
- auto facet_types() -> FacetTypeInfoStore& { return facet_types_; }
- auto facet_types() const -> const FacetTypeInfoStore& { return facet_types_; }
- auto identified_facet_types() -> IdentifiedFacetTypeStore& {
- return identified_facet_types_;
- }
- auto identified_facet_types() const -> const IdentifiedFacetTypeStore& {
- return identified_facet_types_;
- }
- auto impls() -> ImplStore& { return impls_; }
- auto impls() const -> const ImplStore& { return impls_; }
- auto specific_interfaces() -> SpecificInterfaceStore& {
- return specific_interfaces_;
- }
- auto specific_interfaces() const -> const SpecificInterfaceStore& {
- return specific_interfaces_;
- }
- auto generics() -> GenericStore& { return generics_; }
- auto generics() const -> const GenericStore& { return generics_; }
- auto specifics() -> SpecificStore& { return specifics_; }
- auto specifics() const -> const SpecificStore& { return specifics_; }
- auto import_irs() -> ImportIRStore& { return import_irs_; }
- auto import_irs() const -> const ImportIRStore& { return import_irs_; }
- auto import_ir_insts() -> ImportIRInstStore& { return import_ir_insts_; }
- auto import_ir_insts() const -> const ImportIRInstStore& {
- return import_ir_insts_;
- }
- auto cpp_file() -> SemIR::CppFile* { return cpp_file_.get(); }
- auto cpp_file() const -> const SemIR::CppFile* { return cpp_file_.get(); }
- // TODO: We should be able to create the initial C++ AST before creating the
- // `File` and initialize the pointer in the constructor instead of using a
- // setter.
- auto set_cpp_file(std::unique_ptr<SemIR::CppFile> cpp_file) -> void;
- auto clang_decls() -> ClangDeclStore& { return clang_decls_; }
- auto clang_decls() const -> const ClangDeclStore& { return clang_decls_; }
- auto names() const -> NameStoreWrapper {
- return NameStoreWrapper(&identifiers());
- }
- auto name_scopes() -> NameScopeStore& { return name_scopes_; }
- auto name_scopes() const -> const NameScopeStore& { return name_scopes_; }
- auto struct_type_fields() -> StructTypeFieldsStore& {
- return struct_type_fields_;
- }
- auto struct_type_fields() const -> const StructTypeFieldsStore& {
- return struct_type_fields_;
- }
- auto custom_layouts() -> CustomLayoutStore& { return custom_layouts_; }
- auto custom_layouts() const -> const CustomLayoutStore& {
- return custom_layouts_;
- }
- auto types() -> TypeStore& { return types_; }
- auto types() const -> const TypeStore& { return types_; }
- auto insts() -> InstStore& { return insts_; }
- auto insts() const -> const InstStore& { return insts_; }
- auto vtables() -> VtableStore& { return vtables_; }
- auto vtables() const -> const VtableStore& { return vtables_; }
- auto constant_values() -> ConstantValueStore& { return constant_values_; }
- auto constant_values() const -> const ConstantValueStore& {
- return constant_values_;
- }
- auto inst_blocks() -> InstBlockStore& { return inst_blocks_; }
- auto inst_blocks() const -> const InstBlockStore& { return inst_blocks_; }
- auto constants() -> ConstantStore& { return constants_; }
- auto constants() const -> const ConstantStore& { return constants_; }
- auto expr_regions() -> ExprRegionStore& { return expr_regions_; }
- auto expr_regions() const -> const ExprRegionStore& { return expr_regions_; }
- using ClangSourceLocStore =
- ValueStore<ClangSourceLocId, clang::SourceLocation, Tag<CheckIRId>>;
- auto clang_source_locs() -> ClangSourceLocStore& {
- return clang_source_locs_;
- }
- auto clang_source_locs() const -> const ClangSourceLocStore& {
- return clang_source_locs_;
- }
- auto top_inst_block_id() const -> InstBlockId { return top_inst_block_id_; }
- auto set_top_inst_block_id(InstBlockId block_id) -> void {
- top_inst_block_id_ = block_id;
- }
- auto global_ctor_id() const -> FunctionId { return global_ctor_id_; }
- auto set_global_ctor_id(FunctionId function_id) -> void {
- global_ctor_id_ = function_id;
- }
- // Returns true if there were errors creating the semantics IR.
- auto has_errors() const -> bool { return has_errors_; }
- auto set_has_errors(bool has_errors) -> void { has_errors_ = has_errors; }
- auto filename() const -> llvm::StringRef { return filename_; }
- auto parse_tree() const -> const Parse::Tree& { return *parse_tree_; }
- private:
- const Parse::Tree* parse_tree_;
- // True if parts of the IR may be invalid.
- bool has_errors_ = false;
- // The file's ID.
- CheckIRId check_ir_id_;
- // The file's package.
- PackageNameId package_id_ = PackageNameId::None;
- // The file's library.
- LibraryNameId library_id_ = LibraryNameId::None;
- // Shared, compile-scoped values.
- SharedValueStores* value_stores_;
- // Slab allocator, used to allocate instruction and type blocks.
- llvm::BumpPtrAllocator allocator_;
- // The associated filename.
- // TODO: If SemIR starts linking back to tokens, reuse its filename.
- std::string filename_;
- // Storage for EntityNames.
- EntityNameStore entity_names_;
- // For imported C++ global variables, the Clang decl to use for mangling.
- CppGlobalVarStore cpp_global_vars_;
- // Storage for callable objects.
- FunctionStore functions_;
- // Storage for CppOverloadSet.
- CppOverloadSetStore cpp_overload_sets_;
- // Storage for classes.
- ClassStore classes_;
- // Storage for interfaces.
- InterfaceStore interfaces_;
- // Storage for named constraints.
- NamedConstraintStore named_constraints_;
- // Storage for interface requirements.
- RequireImplsStore require_impls_;
- // Storage for blocks of RequireImpls.
- RequireImplsBlockStore require_impls_blocks_;
- // Storage for associated constants.
- AssociatedConstantStore associated_constants_;
- // Storage for facet types.
- FacetTypeInfoStore facet_types_;
- // Storage for identified facet types.
- IdentifiedFacetTypeStore identified_facet_types_;
- // Storage for impls.
- ImplStore impls_;
- // Storage for specific interfaces, which are an individual unit of impl
- // lookup for a single interface.
- SpecificInterfaceStore specific_interfaces_;
- // Storage for generics.
- GenericStore generics_;
- // Storage for specifics.
- SpecificStore specifics_;
- // Related IRs. There are some fixed entries at the start; see ImportIRId.
- ImportIRStore import_irs_;
- // Related IR instructions. These are created for LocIds for instructions
- // that are import-related.
- ImportIRInstStore import_ir_insts_;
- // The C++ file to use when looking up `Cpp` names. Null if there are no `Cpp`
- // imports.
- std::unique_ptr<SemIR::CppFile> cpp_file_;
- // Clang AST declarations pointing to the AST and their mapped Carbon
- // instructions. When calling `Lookup()`, `inst_id` is ignored. `Add()` will
- // not add multiple entries with the same `decl` and different `inst_id`.
- ClangDeclStore clang_decls_ = ClangDeclStore(check_ir_id());
- // All instructions. The first entries will always be the singleton
- // instructions.
- InstStore insts_;
- VtableStore vtables_;
- // Storage for name scopes.
- NameScopeStore name_scopes_ = NameScopeStore(this);
- // Constant values for instructions.
- ConstantValueStore constant_values_;
- // Instruction blocks within the IR. These reference entries in
- // insts_. Storage for the data is provided by allocator_.
- InstBlockStore inst_blocks_;
- // The top instruction block ID.
- InstBlockId top_inst_block_id_ = InstBlockId::None;
- // The global constructor function id.
- FunctionId global_ctor_id_ = FunctionId::None;
- // Storage for instructions that represent computed global constants, such as
- // types.
- ConstantStore constants_;
- // Storage for StructTypeField lists.
- StructTypeFieldsStore struct_type_fields_;
- // Storage for custom layouts.
- CustomLayoutStore custom_layouts_;
- // Descriptions of types used in this file.
- TypeStore types_ = TypeStore(this);
- // Single-entry/single-exit regions that are referenced as units, e.g. because
- // they represent expressions.
- ExprRegionStore expr_regions_;
- // C++ source locations for C++ interop.
- ClangSourceLocStore clang_source_locs_;
- };
- } // namespace Carbon::SemIR
- #endif // CARBON_TOOLCHAIN_SEM_IR_FILE_H_
|