summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-json-source-map-util.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-04-19 17:06:56 -0400
committerGitHub <noreply@github.com>2023-04-19 17:06:56 -0400
commit588991f6df3d6813378721166a7260990835817e (patch)
treefca8c57fedb1967af20ca2da50bbd7dc8afc6a07 /source/compiler-core/slang-json-source-map-util.cpp
parent520a3c064c42e8cd50ef4fde21539870d5b19cb7 (diff)
Make SourceMap a value type (#2812)
* #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. * Make some types swappable. * BoxValue impl. * Added asBoxValue. * Remove const get funcs. * Fix typo in asBoxValue. * Fix another typo in asBoxValue. * Slightly simplify conversion to blob of SourceMap. * Small fix for asBoxValue
Diffstat (limited to 'source/compiler-core/slang-json-source-map-util.cpp')
-rw-r--r--source/compiler-core/slang-json-source-map-util.cpp67
1 files changed, 32 insertions, 35 deletions
diff --git a/source/compiler-core/slang-json-source-map-util.cpp b/source/compiler-core/slang-json-source-map-util.cpp
index 4ef6ee3d6..193c812b7 100644
--- a/source/compiler-core/slang-json-source-map-util.cpp
+++ b/source/compiler-core/slang-json-source-map-util.cpp
@@ -172,9 +172,9 @@ void _encode(Index v, StringBuilder& out)
out.append(dst, cur);
}
-/* static */SlangResult JSONSourceMapUtil::decode(JSONContainer* container, JSONValue root, DiagnosticSink* sink, RefPtr<SourceMap>& out)
+/* static */SlangResult JSONSourceMapUtil::decode(JSONContainer* container, JSONValue root, DiagnosticSink* sink, SourceMap& outSourceMap)
{
- RefPtr<SourceMap> sourceMap(new SourceMap);
+ outSourceMap.clear();
// Let's try and decode the JSON into native types to make this easier...
RttiTypeFuncsMap typeMap = JSONNativeUtil::getTypeFuncsMap();
@@ -188,23 +188,23 @@ void _encode(Index v, StringBuilder& out)
SLANG_RETURN_ON_FAIL(converter.convert(root, GetRttiInfo<JSONSourceMap>::get(), &native));
}
- sourceMap->m_file = native.file;
- sourceMap->m_sourceRoot = native.sourceRoot;
+ outSourceMap.m_file = native.file;
+ outSourceMap.m_sourceRoot = native.sourceRoot;
const Count sourcesCount = native.sources.getCount();
// These should all be unique, but for simplicity, we build a table
- sourceMap->m_sources.setCount(sourcesCount);
+ outSourceMap.m_sources.setCount(sourcesCount);
for (Index i = 0; i < sourcesCount; ++i)
{
- sourceMap->m_sources[i] = sourceMap->m_slicePool.add(native.sources[i]);
+ outSourceMap.m_sources[i] = outSourceMap.m_slicePool.add(native.sources[i]);
}
Count sourcesContentCount = native.sourcesContent.getCount();
sourcesContentCount = std::min(sourcesContentCount, sourcesCount);
- sourceMap->m_sourcesContent.setCount(sourcesContentCount);
- for (auto& cur : sourceMap->m_sourcesContent)
+ outSourceMap.m_sourcesContent.setCount(sourcesContentCount);
+ for (auto& cur : outSourceMap.m_sourcesContent)
{
cur = StringSlicePool::kNullHandle;
}
@@ -218,7 +218,7 @@ void _encode(Index v, StringBuilder& out)
if (value.getKind() == JSONValue::Kind::String)
{
auto stringValue = container->getString(value);
- sourceMap->m_sourcesContent[i] = sourceMap->m_slicePool.add(stringValue);
+ outSourceMap.m_sourcesContent[i] = outSourceMap.m_slicePool.add(stringValue);
}
}
}
@@ -237,13 +237,13 @@ void _encode(Index v, StringBuilder& out)
const Count linesCount = lines.getCount();
- sourceMap->m_lineStarts.setCount(linesCount + 1);
+ outSourceMap.m_lineStarts.setCount(linesCount + 1);
for (Index generatedLine = 0; generatedLine < linesCount; ++generatedLine)
{
const auto line = lines[generatedLine];
- sourceMap->m_lineStarts[generatedLine] = sourceMap->m_lineEntries.getCount();
+ outSourceMap.m_lineStarts[generatedLine] = outSourceMap.m_lineEntries.getCount();
// If it's empty move to next line
if (line.getLength() == 0)
@@ -312,47 +312,45 @@ void _encode(Index v, StringBuilder& out)
entry.sourceFileIndex = sourceFileIndex;
entry.nameIndex = nameIndex;
- sourceMap->m_lineEntries.add(entry);
+ outSourceMap.m_lineEntries.add(entry);
}
}
// Mark the end
- sourceMap->m_lineStarts[linesCount] = sourceMap->m_lineEntries.getCount();
-
- out.swapWith(sourceMap);
+ outSourceMap.m_lineStarts[linesCount] = outSourceMap.m_lineEntries.getCount();
return SLANG_OK;
}
-SlangResult JSONSourceMapUtil::encode(SourceMap* sourceMap, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
+SlangResult JSONSourceMapUtil::encode(const SourceMap& sourceMap, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
{
// Convert to native
JSONSourceMap native;
- native.file = sourceMap->m_file;
- native.sourceRoot = sourceMap->m_sourceRoot;
+ native.file = sourceMap.m_file;
+ native.sourceRoot = sourceMap.m_sourceRoot;
// Copy over the sources
{
- const auto count = sourceMap->m_sources.getCount();
+ const auto count = sourceMap.m_sources.getCount();
native.sources.setCount(count);
for (Index i = 0; i < count; ++i)
{
- native.sources[i] = sourceMap->m_slicePool.getSlice(sourceMap->m_sources[i]);
+ native.sources[i] = sourceMap.m_slicePool.getSlice(sourceMap.m_sources[i]);
}
}
// Copy out the sourcesContent, care is needed around handling null
{
- const auto count = sourceMap->m_sourcesContent.getCount();
+ const auto count = sourceMap.m_sourcesContent.getCount();
native.sourcesContent.setCount(count);
for (Index i = 0; i < count; ++i)
{
- const auto srcValue = sourceMap->m_sourcesContent[i];
+ const auto srcValue = sourceMap.m_sourcesContent[i];
const JSONValue dstValue = (srcValue == StringSlicePool::kNullHandle) ?
native.sourcesContent[i] = JSONValue::makeNull() :
- container->createString(sourceMap->m_slicePool.getSlice(srcValue));
+ container->createString(sourceMap.m_slicePool.getSlice(srcValue));
native.sourcesContent[i] = dstValue;
}
@@ -360,11 +358,11 @@ SlangResult JSONSourceMapUtil::encode(SourceMap* sourceMap, JSONContainer* conta
// Copy out the names
{
- const auto count = sourceMap->m_names.getCount();
+ const auto count = sourceMap.m_names.getCount();
native.names.setCount(count);
for (Index i = 0; i < count; ++i)
{
- native.names[i] = sourceMap->m_slicePool.getSlice(sourceMap->m_names[i]);
+ native.names[i] = sourceMap.m_slicePool.getSlice(sourceMap.m_names[i]);
}
}
@@ -372,7 +370,7 @@ SlangResult JSONSourceMapUtil::encode(SourceMap* sourceMap, JSONContainer* conta
// Do the encoding!
{
- const Count linesCount = sourceMap->getGeneratedLineCount();
+ const Count linesCount = sourceMap.getGeneratedLineCount();
Index sourceFileIndex = 0;
@@ -388,7 +386,7 @@ SlangResult JSONSourceMapUtil::encode(SourceMap* sourceMap, JSONContainer* conta
mappings.appendChar(';');
}
- const auto entries = sourceMap->getEntriesForLine(i);
+ const auto entries = sourceMap.getEntriesForLine(i);
const auto entriesCount = entries.getCount();
if (entriesCount == 0)
@@ -455,13 +453,15 @@ SlangResult JSONSourceMapUtil::encode(SourceMap* sourceMap, JSONContainer* conta
return SLANG_OK;
}
-/* static */SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, RefPtr<SourceMap>& outSourceMap)
+/* static */SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, SourceMap& outSourceMap)
{
return read(blob, nullptr, outSourceMap);
}
-SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, DiagnosticSink* parentSink, RefPtr<SourceMap>& outSourceMap)
+SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, DiagnosticSink* parentSink, SourceMap& outSourceMap)
{
+ outSourceMap.clear();
+
SourceManager sourceManager;
sourceManager.initialize(nullptr, nullptr);
DiagnosticSink sink(&sourceManager, nullptr);
@@ -487,16 +487,13 @@ SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, DiagnosticSink* parentSink
rootValue = builder.getRootValue();
}
- RefPtr<SourceMap> sourceMap;
-
- SLANG_RETURN_ON_FAIL(decode(container, rootValue, &sink, sourceMap));
+ SLANG_RETURN_ON_FAIL(decode(container, rootValue, &sink, outSourceMap));
- outSourceMap = sourceMap;
return SLANG_OK;
}
-/* static */SlangResult JSONSourceMapUtil::write(SourceMap* sourceMap, ComPtr<ISlangBlob>& outBlob)
+/* static */SlangResult JSONSourceMapUtil::write(const SourceMap& sourceMap, ComPtr<ISlangBlob>& outBlob)
{
SourceManager sourceMapSourceManager;
sourceMapSourceManager.initialize(nullptr, nullptr);
@@ -508,7 +505,7 @@ SlangResult JSONSourceMapUtil::read(ISlangBlob* blob, DiagnosticSink* parentSink
return SLANG_OK;
}
-/* static */ SlangResult JSONSourceMapUtil::write(SourceMap* sourceMap, DiagnosticSink* sink, ComPtr<ISlangBlob>& outBlob)
+/* static */ SlangResult JSONSourceMapUtil::write(const SourceMap& sourceMap, DiagnosticSink* sink, ComPtr<ISlangBlob>& outBlob)
{
auto sourceManager = sink->getSourceManager();