summaryrefslogtreecommitdiffstats
path: root/source/compiler-core/slang-artifact-impl.cpp
blob: 31c01f1deef32b82f0ecfb2a48fce32ee3e1fa9d (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
// slang-artifact-impl.cpp
#include "slang-artifact-impl.h"

#include "../core/slang-castable.h"
#include "slang-artifact-desc-util.h"
#include "slang-artifact-handler-impl.h"
#include "slang-artifact-representation.h"
#include "slang-artifact-util.h"
#include "slang-slice-allocator.h"

namespace Slang
{

namespace
{ // anonymous

/* Get a view as a slice of *raw* pointers */
template<typename T>
SLANG_FORCE_INLINE ConstArrayView<T*> _getRawView(const List<ComPtr<T>>& in)
{
    return makeConstArrayView((T* const*)in.getBuffer(), in.getCount());
}

} // namespace

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Artifact !!!!!!!!!!!!!!!!!!!!!!!!!!! */

void* Artifact::castAs(const Guid& guid)
{
    if (auto ptr = getInterface(guid))
    {
        return ptr;
    }
    return getObject(guid);
}

void* Artifact::getInterface(const Guid& uuid)
{
    if (uuid == ISlangUnknown::getTypeGuid() || uuid == ICastable::getTypeGuid() ||
        uuid == IArtifact::getTypeGuid())
    {
        return static_cast<IArtifact*>(this);
    }
    return nullptr;
}

void* Artifact::getObject(const Guid& uuid)
{
    SLANG_UNUSED(uuid);
    return nullptr;
}

IArtifactHandler* Artifact::_getHandler()
{
    return m_handler ? m_handler : DefaultArtifactHandler::getSingleton();
}

void Artifact::_requireChildren()
{
    if (m_expandResult == SLANG_E_UNINITIALIZED)
    {
        const auto res = expandChildren();
        SLANG_UNUSED(res);

        SLANG_ASSERT(SLANG_SUCCEEDED(res) || res == SLANG_E_NOT_IMPLEMENTED);
    }
}

bool Artifact::exists()
{
    for (ICastable* rep : m_representations.getArrayView())
    {
        if (auto artifactRep = as<IArtifactRepresentation>(rep))
        {
            // It is an artifact rep and it exists, we are done
            if (artifactRep->exists())
            {
                return true;
            }
        }
        else
        {
            // If it's *not* IArtifactRepresentation derived, it's existance *is* a representation
            return true;
        }
    }

    return false;
}

SlangResult Artifact::requireFile(Keep keep, IOSFileArtifactRepresentation** outFileRep)
{
    auto handler = _getHandler();

    ComPtr<ICastable> castable;
    SLANG_RETURN_ON_FAIL(handler->getOrCreateRepresentation(
        this,
        IOSFileArtifactRepresentation::getTypeGuid(),
        keep,
        castable.writeRef()));

    auto fileRep = as<IOSFileArtifactRepresentation>(castable);
    fileRep->addRef();

    *outFileRep = fileRep;
    return SLANG_OK;
}

SlangResult Artifact::loadSharedLibrary(ArtifactKeep keep, ISlangSharedLibrary** outSharedLibrary)
{
    auto handler = _getHandler();

    ComPtr<ICastable> castable;
    SLANG_RETURN_ON_FAIL(handler->getOrCreateRepresentation(
        this,
        ISlangSharedLibrary::getTypeGuid(),
        keep,
        castable.writeRef()));

    auto lib = as<ISlangSharedLibrary>(castable);
    lib->addRef();

    *outSharedLibrary = lib;
    return SLANG_OK;
}

IArtifactHandler* Artifact::getHandler()
{
    return m_handler;
}

void Artifact::setHandler(IArtifactHandler* handler)
{
    m_handler = handler;
}

void Artifact::clear(IArtifact::ContainedKind kind)
{
    switch (kind)
    {
    case ContainedKind::Associated:
        m_associated.clear();
        break;
    case ContainedKind::Representation:
        m_representations.clear();
        break;
    case ContainedKind::Children:
        m_children.clear();
        break;
    default:
        break;
    }
}

void Artifact::removeAt(ContainedKind kind, Index i)
{
    switch (kind)
    {
    case ContainedKind::Associated:
        m_associated.removeAt(i);
        break;
    case ContainedKind::Representation:
        m_representations.removeAt(i);
        break;
    case ContainedKind::Children:
        m_children.removeAt(i);
        break;
    default:
        break;
    }
}

SlangResult Artifact::getOrCreateRepresentation(
    const Guid& typeGuid,
    ArtifactKeep keep,
    ICastable** outCastable)
{
    auto handler = _getHandler();
    return handler->getOrCreateRepresentation(this, typeGuid, keep, outCastable);
}

SlangResult Artifact::loadBlob(Keep keep, ISlangBlob** outBlob)
{
    auto handler = _getHandler();

    ComPtr<ICastable> castable;
    SLANG_RETURN_ON_FAIL(handler->getOrCreateRepresentation(
        this,
        ISlangBlob::getTypeGuid(),
        keep,
        castable.writeRef()));

    ISlangBlob* blob = as<ISlangBlob>(castable);
    blob->addRef();

    *outBlob = blob;
    return SLANG_OK;
}

void Artifact::addAssociated(IArtifact* artifact)
{
    SLANG_ASSERT(artifact);
    m_associated.add(ComPtr<IArtifact>(artifact));
}

static void* _findRepresentation(const ConstArrayView<ICastable*>& castables, const Guid& guid)
{
    for (const auto& cur : castables)
    {
        if (auto ptr = cur->castAs(guid))
        {
            return ptr;
        }
    }
    return nullptr;
}

static void* _findRepresentation(const ConstArrayView<IArtifact*>& artifacts, const Guid& guid)
{
    for (auto child : artifacts)
    {
        if (auto rep = child->findRepresentation(Artifact::ContainedKind::Representation, guid))
        {
            return rep;
        }
    }
    return nullptr;
}

void* Artifact::findRepresentation(ContainedKind kind, const Guid& guid)
{
    switch (kind)
    {
    case ContainedKind::Associated:
        return _findRepresentation(_getRawView(m_associated), guid);
    case ContainedKind::Representation:
        return _findRepresentation(_getRawView(m_representations), guid);
    case ContainedKind::Children:
        {
            _requireChildren();
            return _findRepresentation(_getRawView(m_children), guid);
        }
    }
    return nullptr;
}

Slice<IArtifact*> Artifact::getAssociated()
{
    return SliceUtil::asSlice(m_associated);
}

void Artifact::addRepresentation(ICastable* castable)
{
    SLANG_ASSERT(castable);

    auto view = _getRawView(m_representations);
    if (view.indexOf(castable) >= 0)
    {
        SLANG_ASSERT_FAILURE("Already have this representation");
        return;
    }

    m_representations.add(ComPtr<ICastable>(castable));
}

void Artifact::addRepresentationUnknown(ISlangUnknown* unk)
{
    SLANG_ASSERT(unk);

    {
        const auto view = makeConstArrayView(
            (ISlangUnknown* const*)m_representations.getBuffer(),
            m_representations.getCount());
        if (view.indexOf(unk) >= 0)
        {
            SLANG_ASSERT_FAILURE("Already have this representation");
            return;
        }
    }

    ComPtr<ICastable> castable;
    if (SLANG_SUCCEEDED(unk->queryInterface(SLANG_IID_PPV_ARGS(castable.writeRef()))) && castable)
    {
        if (_getRawView(m_representations).indexOf(castable) >= 0)
        {
            SLANG_ASSERT_FAILURE("Already have this representation");
            return;
        }
        m_representations.add(castable);
    }
    else
    {
        UnknownCastableAdapter* adapter = new UnknownCastableAdapter(unk);
        m_representations.add(ComPtr<ICastable>(adapter));
    }
}

Slice<ICastable*> Artifact::getRepresentations()
{
    return SliceUtil::asSlice(m_representations);
}

void Artifact::setChildren(IArtifact* const* children, Count count)
{
    m_expandResult = SLANG_OK;

    m_children.clearAndDeallocate();
    m_children.setCount(count);

    ComPtr<IArtifact>* dst = m_children.getBuffer();
    for (Index i = 0; i < count; ++i)
    {
        dst[i] = children[i];
    }
}

SlangResult Artifact::expandChildren()
{
    auto handler = _getHandler();
    return handler->expandChildren(this);
}

Slice<IArtifact*> Artifact::getChildren()
{
    _requireChildren();
    return SliceUtil::asSlice(m_children);
}

void Artifact::addChild(IArtifact* artifact)
{
    SLANG_ASSERT(artifact);
    SLANG_ASSERT(_getRawView(m_children).indexOf(artifact) < 0);

    _requireChildren();

    m_children.add(ComPtr<IArtifact>(artifact));
}

} // namespace Slang