1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// slang-module-library.cpp
#include "slang-module-library.h"
#include <assert.h>
#include "../core/slang-blob.h"
#include "../core/slang-riff.h"
#include "../core/slang-type-text-util.h"
// Serialization
#include "slang-serialize-ir.h"
#include "slang-serialize-container.h"
namespace Slang {
void* ModuleLibrary::getInterface(const Guid& uuid)
{
if (uuid == ISlangUnknown::getTypeGuid() ||
uuid == ICastable::getTypeGuid() ||
uuid == IArtifactRepresentation::getTypeGuid() ||
uuid == IModuleLibrary::getTypeGuid())
{
return static_cast<IModuleLibrary*>(this);
}
return nullptr;
}
void* ModuleLibrary::getObject(const Guid& uuid)
{
return uuid == getTypeGuid() ? this : nullptr;
}
void* ModuleLibrary::castAs(const Guid& guid)
{
if (auto intf = getInterface(guid))
{
return intf;
}
return getObject(guid);
}
SlangResult loadModuleLibrary(const Byte* inBytes, size_t bytesCount, String path, EndToEndCompileRequest* req, ComPtr<IModuleLibrary>& outLibrary)
{
auto library = new ModuleLibrary;
ComPtr<IModuleLibrary> scopeLibrary(library);
// Load up the module
MemoryStreamBase memoryStream(FileAccess::Read, inBytes, bytesCount);
RiffContainer riffContainer;
SLANG_RETURN_ON_FAIL(RiffUtil::read(&memoryStream, riffContainer));
auto linkage = req->getLinkage();
{
SerialContainerData containerData;
SerialContainerUtil::ReadOptions options;
options.namePool = req->getNamePool();
options.session = req->getSession();
options.sharedASTBuilder = linkage->getASTBuilder()->getSharedASTBuilder();
options.sourceManager = linkage->getSourceManager();
options.linkage = req->getLinkage();
options.sink = req->getSink();
options.astBuilder = linkage->getASTBuilder();
options.modulePath = path;
SLANG_RETURN_ON_FAIL(SerialContainerUtil::read(&riffContainer, options, nullptr, containerData));
DiagnosticSink sink;
// Modules in the container should be serialized in its depedency order,
// so that we always load the dependencies before the consuming module.
for (auto& module : containerData.modules)
{
// If the irModule is set, add it
if (module.irModule)
{
if (module.dependentFiles.getCount() == 0)
return SLANG_FAIL;
if (!module.astRootNode)
return SLANG_FAIL;
auto loadedModule = linkage->loadDeserializedModule(
as<ModuleDecl>(module.astRootNode)->getName(),
PathInfo::makePath(module.dependentFiles.getFirst()),
module, &sink);
if (!loadedModule)
return SLANG_FAIL;
library->m_modules.add(loadedModule);
}
}
for (const auto& entryPoint : containerData.entryPoints)
{
FrontEndCompileRequest::ExtraEntryPointInfo dst;
dst.mangledName = entryPoint.mangledName;
dst.name = entryPoint.name;
dst.profile = entryPoint.profile;
// Add entry point
library->m_entryPoints.add(dst);
}
}
outLibrary.swap(scopeLibrary);
return SLANG_OK;
}
SlangResult loadModuleLibrary(ArtifactKeep keep, IArtifact* artifact, String path, EndToEndCompileRequest* req, ComPtr<IModuleLibrary>& outLibrary)
{
if (auto foundLibrary = findRepresentation<IModuleLibrary>(artifact))
{
outLibrary = foundLibrary;
return SLANG_OK;
}
// Load the blob
ComPtr<ISlangBlob> blob;
SLANG_RETURN_ON_FAIL(artifact->loadBlob(getIntermediateKeep(keep), blob.writeRef()));
// Load the module
ComPtr<IModuleLibrary> library;
SLANG_RETURN_ON_FAIL(loadModuleLibrary((const Byte*)blob->getBufferPointer(), blob->getBufferSize(), path, req, library));
if (canKeep(keep))
{
artifact->addRepresentation(library);
}
outLibrary.swap(library);
return SLANG_OK;
}
} // namespace Slang
|