summaryrefslogtreecommitdiffstats
path: root/source/compiler-core
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-04-18 12:36:06 -0400
committerGitHub <noreply@github.com>2023-04-18 12:36:06 -0400
commit181fd1f3c9c4b047c1947096e7b3f8e5bc2314c3 (patch)
tree308cbadae2f68d43ca20f92755b08db08e8b2283 /source/compiler-core
parent90a9f43573ec0777c2ae4fa20c8fdc51a4ae7b3a (diff)
On demand SourceMap JSON serialization (#2811)
* #include an absolute path didn't work - because paths were taken to always be relative. * Moved JSON source map writing logic to JSONSourceMapUtil. * Use ArtifactHandler to read/write SourceMaps. Use ObjectCastableAdapter to hold SourceMap Only serialize SourceMap <-> JSON on demand.
Diffstat (limited to 'source/compiler-core')
-rw-r--r--source/compiler-core/slang-artifact-handler-impl.cpp49
-rw-r--r--source/compiler-core/slang-json-source-map-util.cpp43
-rw-r--r--source/compiler-core/slang-json-source-map-util.h6
-rw-r--r--source/compiler-core/slang-source-map.h2
4 files changed, 98 insertions, 2 deletions
diff --git a/source/compiler-core/slang-artifact-handler-impl.cpp b/source/compiler-core/slang-artifact-handler-impl.cpp
index 985aa2337..1a8e8374d 100644
--- a/source/compiler-core/slang-artifact-handler-impl.cpp
+++ b/source/compiler-core/slang-artifact-handler-impl.cpp
@@ -17,6 +17,10 @@
#include "../core/slang-io.h"
#include "../core/slang-shared-library.h"
+#include "slang-source-map.h"
+
+#include "slang-json-source-map-util.h"
+
namespace Slang {
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! DefaultArtifactHandler !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
@@ -126,6 +130,25 @@ SlangResult DefaultArtifactHandler::expandChildren(IArtifact* container)
return SLANG_OK;
}
+static SlangResult _loadSourceMap(IArtifact* artifact, ArtifactKeep intermediateKeep, RefPtr<SourceMap>& outSourceMap)
+{
+ const auto desc = artifact->getDesc();
+ if (isDerivedFrom(desc.kind, ArtifactKind::Json) &&
+ isDerivedFrom(desc.payload, ArtifactPayload::SourceMap))
+ {
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(artifact->loadBlob(intermediateKeep, blob.writeRef()));
+
+ RefPtr<SourceMap> sourceMap;
+ SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::read(blob, sourceMap));
+
+ outSourceMap = sourceMap;
+ return SLANG_OK;
+ }
+
+ return SLANG_FAIL;
+}
+
SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifact, const Guid& guid, ArtifactKeep keep, ICastable** outCastable)
{
const auto reps = artifact->getRepresentations();
@@ -156,8 +179,16 @@ SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifac
}
}
- // Special case shared library
- if (guid == ISlangSharedLibrary::getTypeGuid())
+ // Special cases
+ if (guid == SourceMap::getTypeGuid())
+ {
+ // Blob -> SourceMap
+ RefPtr<SourceMap> sourceMap;
+ SLANG_RETURN_ON_FAIL(_loadSourceMap(artifact, getIntermediateKeep(keep), sourceMap));
+ ComPtr<IObjectCastableAdapter> castable(new ObjectCastableAdapter(sourceMap));
+ return _addRepresentation(artifact, keep, castable, outCastable);
+ }
+ else if (guid == ISlangSharedLibrary::getTypeGuid())
{
ComPtr<ISlangSharedLibrary> sharedLib;
SLANG_RETURN_ON_FAIL(_loadSharedLibrary(artifact, sharedLib.writeRef()));
@@ -169,6 +200,20 @@ SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifac
SLANG_RETURN_ON_FAIL(_createOSFile(artifact, getIntermediateKeep(keep), fileRep.writeRef()));
return _addRepresentation(artifact, keep, fileRep, outCastable);
}
+ else if (guid == ISlangBlob::getTypeGuid())
+ {
+ for (ICastable* rep : reps)
+ {
+ if (SourceMap* sourceMap = as<SourceMap>(rep))
+ {
+ // SourceMap -> Blob
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::write(sourceMap, blob));
+ // Add the rep
+ return _addRepresentation(artifact, keep, blob, outCastable);
+ }
+ }
+ }
return SLANG_E_NOT_AVAILABLE;
}
diff --git a/source/compiler-core/slang-json-source-map-util.cpp b/source/compiler-core/slang-json-source-map-util.cpp
index 3929a3387..4ef6ee3d6 100644
--- a/source/compiler-core/slang-json-source-map-util.cpp
+++ b/source/compiler-core/slang-json-source-map-util.cpp
@@ -3,6 +3,7 @@
#include "../../slang-com-helper.h"
#include "../core/slang-string-util.h"
+#include "../core/slang-blob.h"
#include "slang-json-native.h"
@@ -454,6 +455,11 @@ SlangResult JSONSourceMapUtil::encode(SourceMap* sourceMap, JSONContainer* conta
return SLANG_OK;
}
+/* static */SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, RefPtr<SourceMap>& outSourceMap)
+{
+ return read(blob, nullptr, outSourceMap);
+}
+
SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, DiagnosticSink* parentSink, RefPtr<SourceMap>& outSourceMap)
{
SourceManager sourceManager;
@@ -489,4 +495,41 @@ SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, DiagnosticSink* parentSink
return SLANG_OK;
}
+
+/* static */SlangResult JSONSourceMapUtil::write(SourceMap* sourceMap, ComPtr<ISlangBlob>& outBlob)
+{
+ SourceManager sourceMapSourceManager;
+ sourceMapSourceManager.initialize(nullptr, nullptr);
+
+ // Create a sink
+ DiagnosticSink sourceMapSink(&sourceMapSourceManager, nullptr);
+
+ SLANG_RETURN_ON_FAIL(write(sourceMap, &sourceMapSink, outBlob));
+ return SLANG_OK;
+}
+
+/* static */ SlangResult JSONSourceMapUtil::write(SourceMap* sourceMap, DiagnosticSink* sink, ComPtr<ISlangBlob>& outBlob)
+{
+ auto sourceManager = sink->getSourceManager();
+
+ // Write it out
+ String json;
+ {
+ RefPtr<JSONContainer> jsonContainer(new JSONContainer(sourceManager));
+
+ JSONValue jsonValue;
+
+ SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::encode(sourceMap, jsonContainer, sink, jsonValue));
+
+ // Convert into a string
+ JSONWriter writer(JSONWriter::IndentationStyle::Allman);
+ jsonContainer->traverseRecursively(jsonValue, &writer);
+
+ json = writer.getBuilder();
+ }
+
+ outBlob = StringBlob::moveCreate(json);
+ return SLANG_OK;
+}
+
} // namespace Slang
diff --git a/source/compiler-core/slang-json-source-map-util.h b/source/compiler-core/slang-json-source-map-util.h
index ba417dd7c..8d5071d4e 100644
--- a/source/compiler-core/slang-json-source-map-util.h
+++ b/source/compiler-core/slang-json-source-map-util.h
@@ -18,6 +18,12 @@ struct JSONSourceMapUtil
/// Read the blob (encoded as JSON) as a source map.
/// Sink is optional, and can be passed as nullptr
static SlangResult read(ISlangBlob* blob, DiagnosticSink* sink, RefPtr<SourceMap>& outSourceMap);
+ static SlangResult read(ISlangBlob* blob, RefPtr<SourceMap>& outSourceMap);
+
+ /// Write source map to outBlob JSON
+ static SlangResult write(SourceMap* sourceMap, ComPtr<ISlangBlob>& outBlob);
+ /// Write out the source map into a blob
+ static SlangResult write(SourceMap* sourceMap, DiagnosticSink* sink, ComPtr<ISlangBlob>& outBlob);
};
} // namespace Slang
diff --git a/source/compiler-core/slang-source-map.h b/source/compiler-core/slang-source-map.h
index 1ccc87e90..f1338508f 100644
--- a/source/compiler-core/slang-source-map.h
+++ b/source/compiler-core/slang-source-map.h
@@ -13,6 +13,8 @@ namespace Slang {
class SourceMap : public RefObject
{
public:
+ SLANG_CLASS_GUID(0x731383ea, 0xe516, 0x4cc3, { 0xa6, 0xcf, 0x37, 0xd2, 0x8c, 0x24, 0x5c, 0x5e });
+
struct Entry
{
void init()