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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
// slang-artifact.cpp
#include "slang-artifact-util.h"
#include "slang-artifact-impl.h"
#include "slang-artifact-representation-impl.h"
#include "slang-artifact-desc-util.h"
#include "../core/slang-io.h"
namespace Slang {
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactUtil !!!!!!!!!!!!!!!!!!!!!!!!!!! */
/* static */ComPtr<IArtifactContainer> ArtifactUtil::createContainer(const ArtifactDesc& desc)
{
const auto containerDesc = ArtifactDesc::make(ArtifactKind::Container, ArtifactPayload::CompileResults, desc.style);
return ArtifactContainer::create(containerDesc);
}
/* static */ComPtr<IArtifactContainer> ArtifactUtil::createResultsContainer()
{
return ArtifactContainer::create(ArtifactDesc::make(ArtifactKind::Container, ArtifactPayload::CompileResults));
}
/* static */ComPtr<IArtifact> ArtifactUtil::createArtifact(const ArtifactDesc& desc, const char* name)
{
auto artifact = createArtifact(desc);
artifact->setName(name);
return artifact;
}
/* static */ComPtr<IArtifact> ArtifactUtil::createArtifact(const ArtifactDesc& desc)
{
if (isDerivedFrom(desc.kind, ArtifactKind::Container))
{
auto container = ArtifactContainer::create(desc);
ComPtr<IArtifact> artifact;
artifact.attach(container.detach());
return artifact;
}
else
{
return Artifact::create(desc);
}
}
/* static */ComPtr<IArtifact> ArtifactUtil::createArtifactForCompileTarget(SlangCompileTarget target)
{
auto desc = ArtifactDescUtil::makeDescForCompileTarget(target);
return createArtifact(desc);
}
/* static */bool ArtifactUtil::isSignificant(IArtifact* artifact, void* data)
{
SLANG_UNUSED(data);
const auto desc = artifact->getDesc();
// Containers are not significant as of themselves, they may contain something tho
if (isDerivedFrom(desc.kind, ArtifactKind::Container))
{
return false;
}
// If it has no payload.. we are done
if (desc.payload == ArtifactPayload::None ||
desc.payload == ArtifactPayload::Invalid)
{
return false;
}
// If it's binary like or assembly/source we it's significant
if (isDerivedFrom(desc.kind, ArtifactKind::CompileBinary) ||
desc.kind == ArtifactKind::Assembly ||
desc.kind == ArtifactKind::Source)
{
return true;
}
/* Hmm, we might want to have a base class for 'signifiant' payloads,
where signifiance here means somewhat approximately 'the meat' of a compilation result,
as contrasted with 'meta data', 'diagnostics etc'*/
if (isDerivedFrom(desc.payload, ArtifactPayload::Metadata))
{
return false;
}
return true;
}
/* static */IArtifact* ArtifactUtil::findSignificant(IArtifact* artifact)
{
return artifact->findArtifactByPredicate(IArtifact::FindStyle::SelfOrChildren, &ArtifactUtil::isSignificant, nullptr);
}
UnownedStringSlice ArtifactUtil::findPath(IArtifact* artifact)
{
// If a name is set we'll just use that
{
const UnownedStringSlice name(artifact->getName());
if (name.getLength())
{
return name;
}
}
IPathArtifactRepresentation* bestRep = nullptr;
// Look for a rep with a path. Prefer IExtFile because a IOSFile might be a temporary file
for (auto rep : artifact->getRepresentations())
{
if (auto pathRep = as<IPathArtifactRepresentation>(rep))
{
if (pathRep->getPathType() == SLANG_PATH_TYPE_FILE &&
(bestRep == nullptr || as<IExtFileArtifactRepresentation>(rep)))
{
bestRep = pathRep;
}
}
}
const UnownedStringSlice name = bestRep ? UnownedStringSlice(bestRep->getPath()) : UnownedStringSlice();
return name.getLength() ? name : UnownedStringSlice();
}
/* static */UnownedStringSlice ArtifactUtil::inferExtension(IArtifact* artifact)
{
const UnownedStringSlice path = findPath(artifact);
if (path.getLength())
{
auto ext = Path::getPathExt(UnownedStringSlice(path));
if (ext.getLength())
{
return ext;
}
}
return UnownedStringSlice();
}
/* static */UnownedStringSlice ArtifactUtil::findName(IArtifact* artifact)
{
const UnownedStringSlice path = findPath(artifact);
const Index pos = Path::findLastSeparatorIndex(path);
return (pos >= 0) ? path.tail(pos + 1) : path;
}
static SlangResult _calcInferred(IArtifact* artifact, const UnownedStringSlice& basePath, StringBuilder& outPath)
{
auto ext = ArtifactUtil::inferExtension(artifact);
// If no extension was determined by inferring, go with unknown
if (ext.begin() == nullptr)
{
ext = toSlice("unknown");
}
outPath.Clear();
outPath.append(basePath);
if (ext.getLength())
{
outPath.appendChar('.');
outPath.append(ext);
}
return SLANG_OK;
}
/* static */SlangResult ArtifactUtil::calcPath(IArtifact* artifact, const UnownedStringSlice& basePath, StringBuilder& outPath)
{
if (ArtifactDescUtil::hasDefinedNameForDesc(artifact->getDesc()))
{
return ArtifactDescUtil::calcPathForDesc(artifact->getDesc(), basePath, outPath);
}
else
{
return _calcInferred(artifact, basePath, outPath);
}
}
/* static */SlangResult ArtifactUtil::calcName(IArtifact* artifact, const UnownedStringSlice& baseName, StringBuilder& outName)
{
if (ArtifactDescUtil::hasDefinedNameForDesc(artifact->getDesc()))
{
return ArtifactDescUtil::calcNameForDesc(artifact->getDesc(), baseName, outName);
}
else
{
return _calcInferred(artifact, baseName, outName);
}
}
} // namespace Slang
|