summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-artifact-util.cpp
blob: 703f8fddb27ad841c87f58eb92762309994739e6 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// slang-artifact-util.cpp
#include "slang-artifact-util.h"

#include "../core/slang-castable.h"
#include "../core/slang-io.h"
#include "slang-artifact-desc-util.h"
#include "slang-artifact-impl.h"
#include "slang-artifact-representation-impl.h"

namespace Slang
{

static bool _checkSelf(ArtifactUtil::FindStyle findStyle)
{
    return Index(findStyle) <= Index(ArtifactUtil::FindStyle::SelfOrChildren);
}

static bool _checkChildren(ArtifactUtil::FindStyle findStyle)
{
    return Index(findStyle) >= Index(ArtifactUtil::FindStyle::SelfOrChildren);
}

static bool _checkRecursive(ArtifactUtil::FindStyle findStyle)
{
    return findStyle == ArtifactUtil::FindStyle::Recursive ||
           findStyle == ArtifactUtil::FindStyle::ChildrenRecursive;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactUtil !!!!!!!!!!!!!!!!!!!!!!!!!!! */

/* 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)
{
    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)
{
    return isSignificant(artifact->getDesc());
}

/* static */ bool ArtifactUtil::isSignificant(const ArtifactDesc& desc)
{
    // 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 'significant' 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;
    }

    /* We currently can't write out diagnostics, so for now we'll say they are insignificant */
    if (isDerivedFrom(desc.payload, ArtifactPayload::Diagnostics))
    {
        return false;
    }

    return true;
}


/* static */ bool ArtifactUtil::isSignificant(IArtifact* artifact, void* data)
{
    SLANG_UNUSED(data);
    return isSignificant(artifact->getDesc());
}

/* static */ IArtifact* ArtifactUtil::findSignificant(IArtifact* artifact)
{
    return findArtifactByPredicate(
        artifact,
        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);
    }
}

static bool _isByDerivedDesc(IArtifact* artifact, void* data)
{
    const ArtifactDesc& desc = *(const ArtifactDesc*)data;
    return ArtifactDescUtil::isDescDerivedFrom(artifact->getDesc(), desc);
}

static bool _isDesc(IArtifact* artifact, void* data)
{
    const ArtifactDesc& desc = *(const ArtifactDesc*)data;
    return artifact->getDesc() == desc;
}

static bool _isName(IArtifact* artifact, void* data)
{
    const char* name = (const char*)data;
    const auto artifactName = artifact->getName();

    if (name == nullptr || artifactName == nullptr)
    {
        return name == artifactName;
    }
    return ::strcmp(name, artifactName) == 0;
}

/* static */ IArtifact* ArtifactUtil::findArtifactByDerivedDesc(
    IArtifact* artifact,
    FindStyle findStyle,
    const ArtifactDesc& desc)
{
    return findArtifactByPredicate(
        artifact,
        findStyle,
        _isByDerivedDesc,
        &const_cast<ArtifactDesc&>(desc));
}

/* static */ IArtifact* ArtifactUtil::findArtifactByName(
    IArtifact* artifact,
    FindStyle findStyle,
    const char* name)
{
    return findArtifactByPredicate(artifact, findStyle, _isName, const_cast<char*>(name));
}

/* static */ IArtifact* ArtifactUtil::findArtifactByDesc(
    IArtifact* artifact,
    FindStyle findStyle,
    const ArtifactDesc& desc)
{
    return findArtifactByPredicate(artifact, findStyle, _isDesc, &const_cast<ArtifactDesc&>(desc));
}

/* static */ IArtifact* ArtifactUtil::findArtifactByPredicate(
    IArtifact* artifact,
    FindStyle findStyle,
    FindFunc func,
    void* data)
{
    if (_checkSelf(findStyle) && func(artifact, data))
    {
        return artifact;
    }

    if (!_checkChildren(findStyle))
    {
        return nullptr;
    }

    // Expand the children so we can search them
    artifact->expandChildren();

    auto children = artifact->getChildren();
    if (children.count == 0)
    {
        return nullptr;
    }

    // Check the children
    for (auto child : children)
    {
        if (func(child, data))
        {
            return child;
        }
    }

    // If it's recursive, we check all the children of children
    if (_checkRecursive(findStyle))
    {
        for (auto child : children)
        {
            if (auto found =
                    findArtifactByPredicate(child, FindStyle::ChildrenRecursive, func, data))
            {
                return found;
            }
        }
    }

    return nullptr;
}

/* static */ void ArtifactUtil::addAssociated(
    IArtifact* artifact,
    IArtifactPostEmitMetadata* metadata)
{
    if (metadata)
    {
        auto metadataArtifact = ArtifactUtil::createArtifact(
            ArtifactDesc::make(ArtifactKind::Instance, ArtifactPayload::PostEmitMetadata));
        metadataArtifact->addRepresentation(metadata);
        artifact->addAssociated(metadataArtifact);
    }
}

/* static */ void ArtifactUtil::addAssociated(
    IArtifact* artifact,
    IArtifactDiagnostics* diagnostics)
{
    if (diagnostics)
    {
        auto diagnosticsArtifact = ArtifactUtil::createArtifact(
            ArtifactDesc::make(ArtifactKind::Instance, ArtifactPayload::Diagnostics));
        diagnosticsArtifact->addRepresentation(diagnostics);
        artifact->addAssociated(diagnosticsArtifact);
    }
}

} // namespace Slang