summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-artifact-impl.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2022-08-11 11:55:49 -0400
committerGitHub <noreply@github.com>2022-08-11 11:55:49 -0400
commitb5d84f60d36b81c7e8263048dda031a9be14a106 (patch)
tree8b64722cd66a7928942c84ca8c17a27459df17e4 /source/compiler-core/slang-artifact-impl.cpp
parenta083a37ee58dc48d92cf2b844466a295eb3e643e (diff)
Artifact closer to being able to replace CompileResult (#2354)
* #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. * Split out CastableList and related types, and placed in core. * Small fixes around IArtifact. Improve IArtifact docs. First impl of getChildren for IArtifact. * Documentation improvements for Artifact related types. * Fix typo. * Special case adding a ICastableList to a LazyCastableList. * Small simplification of LazyCastableList, by adding State member. * Removed the ILockFile interface because IFileArtifactRepresentation can be used. * Implement DiagnosticsArtifactRepresentation. * Added PostEmitMetadataArtifactRepresentation * Add searching by predicate. Added handling of accessing Artifact as ISharedLibrary * Fix typo. * Add find to IArtifacgtList. Fix some missing SLANG_NO_THROW. * Small improvements around ArtifactDesc types. * Another small change around ArtifactKind. * Some more shuffling of ArtifactDesc. * Make IArtifact castable Remove IArtifactList Made IArtifactContainer derive from IArtifact Made ModuleLibrary atomic ref counted/given IModuleLibrary interface. * Must call _requireChildren before any children access. * Fix missing SLANG_MCALL on castAs. * Fix missing SLANG_OVERRIDE. * Added IArtifactHandler * Use ICastable for basis of scope/lookup.
Diffstat (limited to 'source/compiler-core/slang-artifact-impl.cpp')
-rw-r--r--source/compiler-core/slang-artifact-impl.cpp305
1 files changed, 199 insertions, 106 deletions
diff --git a/source/compiler-core/slang-artifact-impl.cpp b/source/compiler-core/slang-artifact-impl.cpp
index 73927db96..061aa9ee7 100644
--- a/source/compiler-core/slang-artifact-impl.cpp
+++ b/source/compiler-core/slang-artifact-impl.cpp
@@ -4,90 +4,44 @@
#include "slang-artifact-representation.h"
#include "slang-artifact-util.h"
+#include "slang-artifact-desc-util.h"
#include "../core/slang-castable-list-impl.h"
namespace Slang {
-/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactList !!!!!!!!!!!!!!!!!!!!!!!!!!! */
-void* ArtifactList::getInterface(const Guid& guid)
-{
- if (guid == ISlangUnknown::getTypeGuid() ||
- guid == ICastable::getTypeGuid() ||
- guid == IArtifactList::getTypeGuid())
- {
- return static_cast<IArtifactList*>(this);
- }
- return nullptr;
-}
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Artifact !!!!!!!!!!!!!!!!!!!!!!!!!!! */
-void* ArtifactList::getObject(const Guid& guid)
+IArtifactHandler* Artifact::_getHandler()
{
- // For now we can't cast to an object
- SLANG_UNUSED(guid);
- return nullptr;
+ // TODO(JS): For now we just use the default handler, but in the future this should probably be a member
+ return DefaultArtifactHandler::getSingleton();
}
-void* ArtifactList::castAs(const Guid& guid)
+void* Artifact::castAs(const Guid& guid)
{
- if (auto intf = getInterface(guid))
+ if (auto ptr = getInterface(guid))
{
- return intf;
+ return ptr;
}
return getObject(guid);
}
-void ArtifactList::add(IArtifact* artifact)
-{
- // Must be set
- SLANG_ASSERT(artifact);
- // Can't already be in the list
- SLANG_ASSERT(m_artifacts.indexOf(artifact) < 0);
- // Can't have another owner
- SLANG_ASSERT(artifact->getParent() == nullptr);
-
- // Set the parent
- artifact->setParent(m_parent);
-
- // Add
- m_artifacts.add(ComPtr<IArtifact>(artifact));
-}
-
-void ArtifactList::removeAt(Index index)
-{
- IArtifact* artifact = m_artifacts[index];
- artifact->setParent(nullptr);
- m_artifacts.removeAt(index);
-}
-
-void ArtifactList::clear()
-{
- _setParent(nullptr);
- m_artifacts.clear();
-}
-
-void ArtifactList::_setParent(IArtifact* parent)
+void* Artifact::getInterface(const Guid& uuid)
{
- if (m_parent == parent)
+ if (uuid == ISlangUnknown::getTypeGuid() ||
+ uuid == ICastable::getTypeGuid() ||
+ uuid == IArtifact::getTypeGuid())
{
- return;
- }
-
- for (IArtifact* artifact : m_artifacts)
- {
- artifact->setParent(artifact);
+ return static_cast<IArtifact*>(this);
}
+ return nullptr;
}
-/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Artifact !!!!!!!!!!!!!!!!!!!!!!!!!!! */
-
-void* Artifact::getInterface(const Guid& uuid)
+void* Artifact::getObject(const Guid& uuid)
{
- if (uuid == ISlangUnknown::getTypeGuid() || uuid == IArtifact::getTypeGuid())
- {
- return static_cast<IArtifact*>(this);
- }
+ SLANG_UNUSED(uuid);
return nullptr;
}
@@ -113,50 +67,43 @@ bool Artifact::exists()
return false;
}
-SlangResult Artifact::requireFile(Keep keep, IFileArtifactRepresentation** outFileRep)
+SlangResult Artifact::requireFile(Keep keep, ISlangMutableFileSystem* fileSystem, IFileArtifactRepresentation** outFileRep)
{
- auto util = ArtifactUtilImpl::getSingleton();
- return util->requireFileDefaultImpl(this, keep, outFileRep);
+ auto handler = _getHandler();
+ return handler->getOrCreateFileRepresentation(this, keep, fileSystem, outFileRep);
}
-SlangResult Artifact::loadBlob(Keep keep, ISlangBlob** outBlob)
+SlangResult Artifact::loadSharedLibrary(ArtifactKeep keep, ISlangSharedLibrary** outSharedLibrary)
{
- // If we have a blob just return it
- if (auto blob = (ISlangBlob*)findRepresentation(ISlangBlob::getTypeGuid()))
- {
- blob->addRef();
- *outBlob = blob;
- return SLANG_OK;
- }
+ auto handler = _getHandler();
- ComPtr<ISlangBlob> blob;
+ ComPtr<ICastable> castable;
+ SLANG_RETURN_ON_FAIL(handler->getOrCreateRepresentation(this, ISlangSharedLibrary::getTypeGuid(), keep, castable.writeRef()));
+
+ ISlangSharedLibrary* lib = as<ISlangSharedLibrary>(castable);
+ lib->addRef();
- // Look for a representation that we can serialize into a blob
- for (auto rep : m_representations.getView())
- {
- if (auto artifactRep = as<IArtifactRepresentation>(rep))
- {
- SlangResult res = artifactRep->writeToBlob(blob.writeRef());
- if (SLANG_SUCCEEDED(res) && blob)
- {
- break;
- }
- }
- }
-
- // Wasn't able to construct
- if (!blob)
- {
- return SLANG_E_NOT_FOUND;
- }
+ *outSharedLibrary = lib;
+ return SLANG_OK;
+}
- // Put in cache
- if (canKeep(keep))
- {
- addRepresentationUnknown(blob);
- }
+SlangResult Artifact::getOrCreateRepresentation(const Guid& typeGuid, ArtifactKeep keep, ICastable** outCastable)
+{
+ auto handler = _getHandler();
+ return handler->getOrCreateRepresentation(this, typeGuid, keep, outCastable);
+}
- *outBlob = blob.detach();
+SlangResult Artifact::loadBlob(Keep keep, ISlangBlob** outBlob)
+{
+ auto handler = _getHandler();
+
+ ComPtr<ICastable> castable;
+ SLANG_RETURN_ON_FAIL(handler->getOrCreateRepresentation(this, ISlangBlob::getTypeGuid(), keep, castable.writeRef()));
+
+ ISlangBlob* blob = as<ISlangBlob>(castable);
+ blob->addRef();
+
+ *outBlob = blob;
return SLANG_OK;
}
@@ -171,6 +118,12 @@ void* Artifact::findAssociated(const Guid& guid)
return m_associated.find(guid);
}
+
+ICastable* Artifact::findAssociatedWithPredicate(ICastableList::FindFunc findFunc, void* data)
+{
+ return m_associated.findWithPredicate(findFunc, data);
+}
+
ICastableList* Artifact::getAssociated()
{
return m_associated.requireList();
@@ -218,24 +171,164 @@ void* Artifact::findRepresentation(const Guid& guid)
return m_representations.find(guid);
}
-ICastableList* Artifact::getRepresentations()
+ICastable* Artifact::findRepresentationWithPredicate(ICastableList::FindFunc findFunc, void* data)
+{
+ return m_representations.findWithPredicate(findFunc, data);
+}
+
+Slice<ICastable*> Artifact::getRepresentations()
+{
+ const auto view = m_representations.getView();
+ return Slice<ICastable*>(view.getBuffer(), view.getCount());
+}
+
+ICastableList* Artifact::getRepresentationList()
{
return m_representations.requireList();
}
-IArtifactList* Artifact::getChildren()
+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactContainer !!!!!!!!!!!!!!!!!!!!!!!!!!! */
+
+void* ArtifactContainer::getInterface(const Guid& guid)
+{
+ if (guid == ISlangUnknown::getTypeGuid() ||
+ guid == ICastable::getTypeGuid() ||
+ guid == IArtifact::getTypeGuid() ||
+ guid == IArtifactContainer::getTypeGuid())
+ {
+ return static_cast<IArtifactContainer*>(this);
+ }
+ return nullptr;
+}
+
+void* ArtifactContainer::getObject(const Guid& guid)
+{
+ SLANG_UNUSED(guid);
+ return nullptr;
+}
+
+void* ArtifactContainer::castAs(const Guid& guid)
+{
+ if (auto ptr = getInterface(guid))
+ {
+ return ptr;
+ }
+ return getObject(guid);
+}
+
+void ArtifactContainer::setChildren(IArtifact** children, Count count)
+{
+ m_expandResult = SLANG_OK;
+
+ m_children.clearAndDeallocate();
+ m_children.setCount(count);
+
+ ComPtr<IArtifact>* dst = m_children.getBuffer();
+ for (Index i = 0; i < count; ++i)
+ {
+ dst[i] = children[i];
+ }
+}
+
+SlangResult ArtifactContainer::expandChildren()
+{
+ auto handler = _getHandler();
+ return handler->expandChildren(this);
+}
+
+Slice<IArtifact*> ArtifactContainer::getChildren()
+{
+ _requireChildren();
+
+ return Slice<IArtifact*>((IArtifact**)m_children.getBuffer(), m_children.getCount());
+}
+
+void ArtifactContainer::addChild(IArtifact* artifact)
+{
+ SLANG_ASSERT(artifact);
+ SLANG_ASSERT(m_children.indexOf(artifact) < 0);
+
+ _requireChildren();
+
+ m_children.add(ComPtr<IArtifact>(artifact));
+}
+
+void ArtifactContainer::removeChildAt(Index index)
{
- // If it has already evaluated, return it.
- if (m_children)
+ _requireChildren();
+
+ m_children.removeAt(index);
+}
+
+void ArtifactContainer::clearChildren()
+{
+ _requireChildren();
+
+ m_children.clearAndDeallocate();
+}
+
+IArtifact* ArtifactContainer::findChildByDesc(const ArtifactDesc& desc)
+{
+ _requireChildren();
+
+ for (IArtifact* artifact : m_children)
{
- return m_children;
+ if (artifact->getDesc() == desc)
+ {
+ return artifact;
+ }
}
+ return nullptr;
+}
- auto util = ArtifactUtilImpl::getSingleton();
- util->getChildrenDefaultImpl(this, m_children.writeRef());
+IArtifact* ArtifactContainer::findChildByDerivedDesc(const ArtifactDesc& desc)
+{
+ _requireChildren();
- return m_children;
+ for (IArtifact* artifact : m_children)
+ {
+ const ArtifactDesc artifactDesc = artifact->getDesc();
+ // TODO(JS): Currently this ignores flags in desc. That may or may not be right
+ // long term.
+ if (isDerivedFrom(artifactDesc.kind, desc.kind) &&
+ isDerivedFrom(artifactDesc.payload, desc.payload) &&
+ isDerivedFrom(artifactDesc.style, desc.style))
+ {
+ return artifact;
+ }
+ }
+ return nullptr;
}
+IArtifact* ArtifactContainer::findChildByName(const char* name)
+{
+ _requireChildren();
+
+ for (IArtifact* artifact : m_children)
+ {
+ const char* artifactName = artifact->getName();
+
+ if (artifactName == name ||
+ ::strcmp(artifactName, name) == 0)
+ {
+ return artifact;
+ }
+ }
+ return nullptr;
+}
+
+IArtifact* ArtifactContainer::findChildByPredicate(FindFunc func, void* data)
+{
+ _requireChildren();
+
+ for (IArtifact* artifact : m_children)
+ {
+ if (func(artifact, data))
+ {
+ return artifact;
+ }
+ }
+ return nullptr;
+}
} // namespace Slang