From 1378fffd9da094beb41b2db89b96f556c23ab6cb Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 10 Aug 2022 10:04:06 -0400 Subject: Artifact and ICastable (#2351) * #include an absolute path didn't work - because paths were taken to always be relative. * WIP with hierarchical enums. * Some small fixes and improvements around artifact desc related types. * Improvements around hierarchical enum. * Fixes to get Artifact types refactor to be able to execute tests. * Attempt to better categorize PTX. * Work around for potentially unused function warning. * Typo fix. * Simplify Artifact header. * Small improvements around Artifact kind/payload/style. * Added IDestroyable/ICastable * Add IArtifactList. * First impl of IArtifactUtil. * Use the ICastable interface for IArtifactRepresentation. * Added IArtifactRepresentation & IArtifactAssociated. * Add SLANG_OVERRIDE to avoid gcc/clang warning. * Fix calling convention issue on win32. * Fix missing SLANG_OVERRIDE. * First attempt at file abstraction around Artifact. * Added creation of lock file. * Move functionality for determining file paths to the IArtifactUtil. Add casting to ICastable. * Added some casting/finding mechanisms. * Simplify IArtifact interface, and use Items for file reps. * Fix problem with libraries on DXIL. * Split out ArtifactRepresentation. * Move ArtifactDesc functionality to ArtifactDescUtil. ArtifactInfoUtil becomes ArtifactDescUtil. * Split implementations from the interfaces for Artifact. * Use TypeTextUtil for target name outputting. * Add artifact impls. * Add ICastableList * Added UnknownCastableAdapter * Make ISlangSharedLibrary derive from ICastable, and remain backwards compatible with slang-llvm. * Refactor Representation on Artifact. * Make our ISlangBlobs also derive from ICastable. Make ISlangBlob atomic ref counted. * Fix typo. --- source/compiler-core/slang-artifact.h | 100 +++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 45 deletions(-) (limited to 'source/compiler-core/slang-artifact.h') diff --git a/source/compiler-core/slang-artifact.h b/source/compiler-core/slang-artifact.h index 86b08dbb9..3c991b7b6 100644 --- a/source/compiler-core/slang-artifact.h +++ b/source/compiler-core/slang-artifact.h @@ -201,6 +201,7 @@ inline /* static */ArtifactDesc ArtifactDesc::make(Packed inPacked) // Forward declare class IFileArtifactRepresentation; +class IArtifactRepresentation; // Controls what items can be kept. enum class ArtifactKeep @@ -217,6 +218,40 @@ SLANG_INLINE bool canKeep(ArtifactKeep keep) { return Index(keep) >= Index(Artif /// Returns the keep type for an intermediate SLANG_INLINE ArtifactKeep getIntermediateKeep(ArtifactKeep keep) { return (keep == ArtifactKeep::All) ? ArtifactKeep::All : ArtifactKeep::No; } +/* A useful interface for handling lists of castable interfaces. Cannot hold nullptr */ +class ICastableList : public ICastable +{ + SLANG_COM_INTERFACE(0x335f3d40, 0x934c, 0x40dc, { 0xb5, 0xe1, 0xf7, 0x6e, 0x40, 0x3, 0x62, 0x5 }) + + /// Get the count of all interfaces held in the list + virtual Count SLANG_MCALL getCount() = 0; + /// Get the interface at the specified index + virtual ICastable* SLANG_MCALL getAt(Index i) = 0; + /// Add an item to the list + virtual void SLANG_MCALL add(ICastable* unk) = 0; + /// Add IUnknown, will cast to ICastable and if that's not possible will wrap + virtual void SLANG_MCALL addUnknown(ISlangUnknown* unk) = 0; + /// Remove item at index, remaining items stay in the same order + virtual void SLANG_MCALL removeAt(Index i) = 0; + /// Clear the list + virtual void SLANG_MCALL clear() = 0; + /// Find the first index of castable, or -1 if not found + virtual Index SLANG_MCALL indexOf(ICastable* castable) = 0; + /// Find the index interface (handling wrapping if necessary) + virtual Index SLANG_MCALL indexOfUnknown(ISlangUnknown* unk) = 0; + /// Find the first item that casts to non null + virtual void* SLANG_MCALL find(const Guid& guid) = 0; + /// Access the internal buffer (any mutation can invalidate this value) + virtual ICastable*const* SLANG_MCALL getBuffer() = 0; +}; + +// Simply finding things in a ICastableList +template +SLANG_FORCE_INLINE T* find(ICastableList* list) +{ + return reinterpret_cast(list->find(T::getTypeGuid())); +} + /* The IArtifact interface is designed to represent some Artifact of compilation. It could be input to or output from a compilation. An abstraction is desirable here, because depending on the compiler the artifact/s could be @@ -286,19 +321,22 @@ public: /// Get the name of the artifact. This can be empty. virtual SLANG_NO_THROW const char* SLANG_MCALL getName() = 0; - /// Find an item by casting it's interface - virtual SLANG_NO_THROW void* SLANG_MCALL findItemInterface(const Guid& uuid) = 0; - /// Only works on ICastable derived items. Can find interfaces or objects. - virtual SLANG_NO_THROW void* SLANG_MCALL findItemObject(const Guid& classGuid) = 0; - - /// Add a representation - virtual SLANG_NO_THROW void SLANG_MCALL addItem(ISlangUnknown* item) = 0; - /// Get the item at the index - virtual SLANG_NO_THROW ISlangUnknown* SLANG_MCALL getItemAt(Index i) = 0; - /// Remove the element at the specified index. - virtual SLANG_NO_THROW void SLANG_MCALL removeItemAt(Index i) = 0; - /// Get the amount of elements - virtual SLANG_NO_THROW Index SLANG_MCALL getItemCount() = 0; + /// Add data associated with this artifact + virtual SLANG_NO_THROW void SLANG_MCALL addAssociated(ICastable* castable) = 0; + /// Find an associated item + virtual void* SLANG_MCALL SLANG_MCALL findAssociated(const Guid& unk) = 0; + /// TODO(JS): We may want this to return nullptr if it's empty. + /// Get the list of associated items + virtual ICastableList* SLANG_MCALL getAssociated() = 0; + + /// Add a representation that derives from IArtifactRepresentation + virtual SLANG_NO_THROW void SLANG_MCALL addRepresentation(IArtifactRepresentation* rep) = 0; + /// Add a representation that doesn't derive from IArtifactRepresentation + virtual SLANG_NO_THROW void SLANG_MCALL addRepresentationUnknown(ISlangUnknown* rep) = 0; + /// Find representation + virtual void* SLANG_MCALL SLANG_MCALL findRepresentation(const Guid& guid) = 0; + /// Get the list of all representations + virtual ICastableList* SLANG_MCALL getRepresentations() = 0; }; /* A list of artifacts. */ @@ -336,44 +374,16 @@ class IArtifactRepresentation : public ICastable /// Convert the instance into a serializable blob. /// Returns SLANG_E_NOT_IMPLEMENTED if an implementation doesn't implement - virtual SLANG_NO_THROW SlangResult SLANG_MCALL writeToBlob(ISlangBlob** blob) = 0; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL writeToBlob(ISlangBlob** blob) = 0; - /// Returns true if this representation exists and is available for use. + /// Returns true if this representation exists and is available for use. virtual SLANG_NO_THROW bool SLANG_MCALL exists() = 0; }; -/* Interface for types that are associated with an artifact, but aren't a representation, or are -only part of a representation. */ -class IArtifactAssociated : public ICastable -{ - SLANG_COM_INTERFACE(0xafc0e4db, 0x16d4, 0x4d7a, { 0x93, 0x5f, 0x3e, 0x47, 0x7a, 0x23, 0x2a, 0x7f }) -}; - - -// Helper template to make finding an item more simple -// There isn't a problem if we only have a forward declaration, because in that case T::getTypeGuid can't work. -SLANG_FORCE_INLINE void* _findItemImpl(IArtifact* artifact, const Guid& guid, const ISlangUnknown* intf) -{ - SLANG_UNUSED(intf); - return artifact->findItemInterface(guid); -} - -SLANG_FORCE_INLINE void* _findItemImpl(IArtifact* artifact, const Guid& guid, const ICastable* castable) -{ - SLANG_UNUSED(castable); - return artifact->findItemObject(guid); -} - -SLANG_FORCE_INLINE void* _findItemImpl(IArtifact* artifact, const Guid& guid, const void* other) -{ - SLANG_UNUSED(other); - return artifact->findItemObject(guid); -} - template -SLANG_FORCE_INLINE T* findItem(IArtifact* artifact) +SLANG_FORCE_INLINE T* findRepresentation(IArtifact* artifact) { - return (T*)_findItemImpl(artifact, T::getTypeGuid(), (T*)nullptr); + return reinterpret_cast(artifact->findRepresentation(T::getTypeGuid())); } } // namespace Slang -- cgit v1.2.3