summaryrefslogtreecommitdiff
path: root/source/compiler-core
diff options
context:
space:
mode:
Diffstat (limited to 'source/compiler-core')
-rw-r--r--source/compiler-core/slang-artifact-handler-impl.cpp35
-rw-r--r--source/compiler-core/slang-json-source-map-util.cpp67
-rw-r--r--source/compiler-core/slang-json-source-map-util.h12
-rw-r--r--source/compiler-core/slang-source-loc.cpp8
-rw-r--r--source/compiler-core/slang-source-loc.h7
-rw-r--r--source/compiler-core/slang-source-map.cpp14
-rw-r--r--source/compiler-core/slang-source-map.h7
7 files changed, 80 insertions, 70 deletions
diff --git a/source/compiler-core/slang-artifact-handler-impl.cpp b/source/compiler-core/slang-artifact-handler-impl.cpp
index 1a8e8374d..5d2a74036 100644
--- a/source/compiler-core/slang-artifact-handler-impl.cpp
+++ b/source/compiler-core/slang-artifact-handler-impl.cpp
@@ -130,7 +130,7 @@ SlangResult DefaultArtifactHandler::expandChildren(IArtifact* container)
return SLANG_OK;
}
-static SlangResult _loadSourceMap(IArtifact* artifact, ArtifactKeep intermediateKeep, RefPtr<SourceMap>& outSourceMap)
+static SlangResult _loadSourceMap(IArtifact* artifact, ArtifactKeep intermediateKeep, SourceMap& outSourceMap)
{
const auto desc = artifact->getDesc();
if (isDerivedFrom(desc.kind, ArtifactKind::Json) &&
@@ -139,10 +139,7 @@ static SlangResult _loadSourceMap(IArtifact* artifact, ArtifactKeep intermediate
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;
+ SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::read(blob, outSourceMap));
return SLANG_OK;
}
@@ -183,10 +180,9 @@ SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifac
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);
+ ComPtr<IBoxValue<SourceMap>> sourceMap(new BoxValue<SourceMap>);
+ SLANG_RETURN_ON_FAIL(_loadSourceMap(artifact, getIntermediateKeep(keep), sourceMap->get()));
+ return _addRepresentation(artifact, keep, sourceMap, outCastable);
}
else if (guid == ISlangSharedLibrary::getTypeGuid())
{
@@ -200,18 +196,17 @@ 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())
+
+ // Handle known conversion to blobs
+ 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);
- }
+ if (auto sourceMap = findRepresentation<SourceMap>(artifact))
+ {
+ // SourceMap -> Blob
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::write(*sourceMap, blob));
+ // Add the rep
+ return _addRepresentation(artifact, keep, blob, outCastable);
}
}
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();
diff --git a/source/compiler-core/slang-json-source-map-util.h b/source/compiler-core/slang-json-source-map-util.h
index 8d5071d4e..d61936ce4 100644
--- a/source/compiler-core/slang-json-source-map-util.h
+++ b/source/compiler-core/slang-json-source-map-util.h
@@ -10,20 +10,20 @@ namespace Slang {
struct JSONSourceMapUtil
{
/// Decode from root into the source map
- static SlangResult decode(JSONContainer* container, JSONValue root, DiagnosticSink* sink, RefPtr<SourceMap>& out);
+ static SlangResult decode(JSONContainer* container, JSONValue root, DiagnosticSink* sink, SourceMap& out);
/// Converts the source map contents into JSON
- static SlangResult encode(SourceMap* sourceMap, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue);
+ static SlangResult encode(const SourceMap& sourceMap, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue);
/// 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);
+ static SlangResult read(ISlangBlob* blob, DiagnosticSink* sink, SourceMap& outSourceMap);
+ static SlangResult read(ISlangBlob* blob, SourceMap& outSourceMap);
/// Write source map to outBlob JSON
- static SlangResult write(SourceMap* sourceMap, ComPtr<ISlangBlob>& outBlob);
+ static SlangResult write(const SourceMap& sourceMap, ComPtr<ISlangBlob>& outBlob);
/// Write out the source map into a blob
- static SlangResult write(SourceMap* sourceMap, DiagnosticSink* sink, ComPtr<ISlangBlob>& outBlob);
+ static SlangResult write(const SourceMap& sourceMap, DiagnosticSink* sink, ComPtr<ISlangBlob>& outBlob);
};
} // namespace Slang
diff --git a/source/compiler-core/slang-source-loc.cpp b/source/compiler-core/slang-source-loc.cpp
index 23cbdf839..3d992ee8d 100644
--- a/source/compiler-core/slang-source-loc.cpp
+++ b/source/compiler-core/slang-source-loc.cpp
@@ -211,7 +211,7 @@ SlangResult _findLocWithSourceMap(SourceManager* lookupSourceManager, SourceView
auto sourceMap = sourceFile->getSourceMap();
SLANG_ASSERT(sourceMap);
- entryIndex = sourceMap->findEntry(lineIndex, colIndex);
+ entryIndex = sourceMap->get().findEntry(lineIndex, colIndex);
}
if (entryIndex < 0)
@@ -222,7 +222,7 @@ SlangResult _findLocWithSourceMap(SourceManager* lookupSourceManager, SourceView
// Keep searching through source maps
do
{
- auto sourceMap = sourceFile->getSourceMap();
+ auto sourceMap = sourceFile->getSourceMap()->getPtr();
// Find the entry
const auto& entry = sourceMap->getEntryByIndex(entryIndex);
@@ -242,7 +242,7 @@ SlangResult _findLocWithSourceMap(SourceManager* lookupSourceManager, SourceView
// If it has a source map, we try and look up the current location in it's source map
if (auto foundSourceMap = foundSourceFile->getSourceMap())
{
- const auto foundEntryIndex = foundSourceMap->findEntry(entry.sourceLine, entry.sourceColumn);
+ const auto foundEntryIndex = foundSourceMap->get().findEntry(entry.sourceLine, entry.sourceColumn);
// If we found the entry repeat the lookup
if (foundEntryIndex >= 0)
@@ -258,7 +258,7 @@ SlangResult _findLocWithSourceMap(SourceManager* lookupSourceManager, SourceView
} while (false);
// Generate the HandleSourceLoc
- auto sourceMap = sourceFile->getSourceMap();
+ auto sourceMap = sourceFile->getSourceMap()->getPtr();
const auto& entry = sourceMap->getEntryByIndex(entryIndex);
// We need to add the pool of the originating source view/file
diff --git a/source/compiler-core/slang-source-loc.h b/source/compiler-core/slang-source-loc.h
index 98b1bdbbd..fafff7de6 100644
--- a/source/compiler-core/slang-source-loc.h
+++ b/source/compiler-core/slang-source-loc.h
@@ -5,6 +5,7 @@
#include "../core/slang-basic.h"
#include "../core/slang-memory-arena.h"
#include "../core/slang-string-slice-pool.h"
+#include "../core/slang-castable.h"
#include "slang-source-map.h"
@@ -254,9 +255,9 @@ public:
/// Get the source map associated with this file. If it's set when doing
/// lookup for source locations, the source map will be used
- SourceMap* getSourceMap() const { return m_sourceMap; }
+ IBoxValue<SourceMap>* getSourceMap() const { return m_sourceMap; }
/// Set a source map
- void setSourceMap(SourceMap* sourceMap) { m_sourceMap = sourceMap; }
+ void setSourceMap(IBoxValue<SourceMap>* sourceMap) { m_sourceMap = sourceMap; }
/// Ctor
SourceFile(SourceManager* sourceManager, const PathInfo& pathInfo, size_t contentSize);
@@ -279,7 +280,7 @@ public:
// If set then the locations in this file are really from locations from elsewhere,
// where the SourceMap specifies that mapping
- RefPtr<SourceMap> m_sourceMap;
+ ComPtr<IBoxValue<SourceMap>> m_sourceMap;
};
enum class SourceLocType
diff --git a/source/compiler-core/slang-source-map.cpp b/source/compiler-core/slang-source-map.cpp
index 4ea50eb3f..7a6e368a4 100644
--- a/source/compiler-core/slang-source-map.cpp
+++ b/source/compiler-core/slang-source-map.cpp
@@ -4,7 +4,7 @@ namespace Slang {
void SourceMap::clear()
{
- String empty;
+ const String empty;
m_file = empty;
m_sourceRoot = empty;
@@ -23,6 +23,18 @@ void SourceMap::clear()
m_slicePool.clear();
}
+void SourceMap::swapWith(ThisType& rhs)
+{
+ m_file.swapWith(rhs.m_file);
+ m_sourceRoot.swapWith(rhs.m_sourceRoot);
+ m_sources.swapWith(rhs.m_sources);
+ m_names.swapWith(rhs.m_names);
+ m_sourcesContent.swapWith(rhs.m_sourcesContent);
+ m_lineStarts.swapWith(rhs.m_lineStarts);
+ m_lineEntries.swapWith(rhs.m_lineEntries);
+ m_slicePool.swapWith(rhs.m_slicePool);
+}
+
void SourceMap::advanceToLine(Index nextLineIndex)
{
const Count currentLineIndex = getGeneratedLineCount() - 1;
diff --git a/source/compiler-core/slang-source-map.h b/source/compiler-core/slang-source-map.h
index f1338508f..e0c8d065a 100644
--- a/source/compiler-core/slang-source-map.h
+++ b/source/compiler-core/slang-source-map.h
@@ -10,9 +10,11 @@
namespace Slang {
-class SourceMap : public RefObject
+class SourceMap
{
public:
+ typedef SourceMap ThisType;
+
SLANG_CLASS_GUID(0x731383ea, 0xe516, 0x4cc3, { 0xa6, 0xcf, 0x37, 0xd2, 0x8c, 0x24, 0x5c, 0x5e });
struct Entry
@@ -66,6 +68,9 @@ public:
/// Clear the contents of the source map
void clear();
+ /// Swap this with rhs
+ void swapWith(ThisType& rhs);
+
/// Ctor
SourceMap():
m_slicePool(StringSlicePool::Style::Default)