summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-artifact-impl.cpp
blob: d57edd2c83c9755dbd0d71dd794369945b185ee6 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// slang-artifact-impl.cpp
#include "slang-artifact-impl.h"

#include "slang-artifact-representation.h"

#include "slang-artifact-util.h"

namespace Slang {

/*
If we use LazyCastableList for Items, it means we'll have to use the
UnknownCastableAdapter for ISharedLibrary, IBlob etc. 

That means when we look for an item in the list, we will always do a query interface on those types, 
although it will always fail (so no atomic ref count). 

That doesn't seem wholey unreasonable. 

Note that we *can* derive from ICastable for *our* implementations of ISlangBlob, ISharedLibrary. So the 
kludge is only needed for types that really do require adaption. For Blob we'll require multiple interface inheritance.
*/

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! LazyCastableList !!!!!!!!!!!!!!!!!!!!!!!!!!! */

void LazyCastableList::removeAt(Index index)
{
    SLANG_ASSERT(index >= 0 && index < getCount());

    if (auto list = as<ICastableList>(m_castable))
    {
        list->removeAt(index);
    }
    else
    {
        SLANG_ASSERT(index == 0);
        m_castable.setNull();
    }
}

void LazyCastableList::clear()
{
    if (m_castable)
    {
        if (auto list = as<ICastableList>(m_castable))
        {
            list->clear();
        }
        else
        {
            m_castable.setNull();
        }
    }
}

void LazyCastableList::clearAndDeallocate()
{
    m_castable.setNull();
}

Count LazyCastableList::getCount() const
{
    if (m_castable)
    {
        if (auto list = as<ICastableList>(m_castable))
        {
            return list->getCount();
        }
        return 1;
    }
    return 0;
}

void LazyCastableList::add(ICastable* castable)
{
    SLANG_ASSERT(castable);
    SLANG_ASSERT(castable != m_castable);

    if (m_castable)
    {
        if (auto list = as<ICastableList>(m_castable))
        {
            // Shouldn't be in the list
            SLANG_ASSERT(list->indexOf(castable) < 0);
            list->add(castable);
        }
        else
        {
            list = new CastableList;
            list->add(m_castable);
            m_castable = list;
            list->add(castable);
        }
    }
    else
    {
        m_castable = castable;
    }
}

ICastableList* LazyCastableList::requireList()
{
    if (m_castable)
    {
        if (auto list = as<ICastableList>(m_castable))
        {
            return list;
        }
        else
        {
            // Promote to a list with the element in it
            list = new CastableList;
            list->add(m_castable);
            m_castable = list;
            return list;
        }
    }
    else
    {
        // Create an empty list
        ICastableList* list = new CastableList;
        m_castable = list;
        return list;
    }
}

ICastableList* LazyCastableList::getList()
{
    return (m_castable == nullptr) ? nullptr : requireList();
}

void* LazyCastableList::find(const Guid& guid)
{
    if (!m_castable)
    {
        return nullptr;
    }
    if (auto list = as<ICastableList>(m_castable))
    {
        return list->find(guid);
    }
    else
    {
        return m_castable->castAs(guid);
    }
}

ConstArrayView<ICastable*> LazyCastableList::getView() const
{
    if (!m_castable)
    {
        // Empty
        return ConstArrayView<ICastable*>();
    }

    if (auto list = as<ICastableList>(m_castable))
    {
        const auto count = list->getCount();
        const auto buffer = list->getBuffer();

        return ConstArrayView<ICastable*>(buffer, count);
    }
    else
    {
        return ConstArrayView<ICastable*>((ICastable*const*)&m_castable, 1);
    }
}

Index LazyCastableList::indexOf(ICastable* castable) const
{
    return getView().indexOf(castable);
}

Index LazyCastableList::indexOfUnknown(ISlangUnknown* unk) const
{
    {
        ComPtr<ICastable> castable;
        if (SLANG_SUCCEEDED(unk->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())) && castable)
        {
            return indexOf(castable);
        }
    }

    // It's not derived from ICastable, so can only be in list via an adapter
    const auto view = getView();

    const Count count = view.getCount();
    for (Index i = 0; i < count; ++i)
    {
        auto adapter = as<IUnknownCastableAdapter>(view[i]);
        if (adapter && adapter->getContained() == unk)
        {
            return i;
        }
    }

    return -1;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! UnknownCastableAdapter !!!!!!!!!!!!!!!!!!!!!!!!!!! */

void* UnknownCastableAdapter::castAs(const Guid& guid)
{
    if (auto intf = getInterface(guid))
    {
        return intf;
    }
    if (auto obj = getObject(guid))
    {
        return obj;
    }

    if (m_found && guid == m_foundGuid)
    {
        return m_found;
    }

    ComPtr<ISlangUnknown> cast;
    if (SLANG_SUCCEEDED(m_contained->queryInterface(guid, (void**)cast.writeRef())) && cast)
    {
        // Save the interface in the cache
        m_found = cast;
        m_foundGuid = guid;

        return cast;
    }
    return nullptr;
}

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

void* UnknownCastableAdapter::getObject(const Guid& guid)
{
    SLANG_UNUSED(guid);
    return nullptr;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CastableList !!!!!!!!!!!!!!!!!!!!!!!!!!! */

CastableList::~CastableList()
{
    for (auto castable : m_list)
    {
        castable->release();
    }
}

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

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

void* CastableList::getObject(const Guid& guid)
{
    SLANG_UNUSED(guid);
    return nullptr;
}

void* CastableList::find(const Guid& guid)
{
    for (ICastable* castable : m_list)
    {
        if (auto ptr = castable->castAs(guid))
        {
            return ptr;
        }
    }
    return nullptr;
}

Index CastableList::indexOf(ICastable* castable)
{
    const Count count = m_list.getCount();
    for (Index i = 0; i < count; ++i)
    {
        ICastable* cur = m_list[i];
        if (cur == castable)
        {
            return i;
        }
    }
    return -1;
}

void CastableList::add(ICastable* castable) 
{ 
    SLANG_ASSERT(castable);
    castable->addRef();
    m_list.add(castable);
}

void CastableList::removeAt(Index i) 
{ 
    auto castable = m_list[i];
    m_list.removeAt(i);
    castable->release();
}

void CastableList::clear() 
{ 
    for (auto castable : m_list)
    {
        castable->release();
    }
    m_list.clear(); 
}

void CastableList::addUnknown(ISlangUnknown* unk)
{
    // If it has ICastable interface we can just add as that
    {
        ComPtr<ICastable> castable;
        if (SLANG_SUCCEEDED(unk->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())) && castable)
        {
            return add(castable);
        }
    }

    // Wrap it in an adapter
    IUnknownCastableAdapter* adapter = new UnknownCastableAdapter(unk);
    add(adapter);
}

Index CastableList::indexOfUnknown(ISlangUnknown* unk)
{
    SLANG_ASSERT(unk);
    // If it has a castable interface we can just look for that
    {
        ComPtr<ICastable> castable;
        if (SLANG_SUCCEEDED(unk->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())) && castable)
        {
            return indexOf(castable);
        }
    }

    // It's not derived from ICastable, so can only be in list via an adapter
    const Count count = m_list.getCount();
    for (Index i = 0; i < count; ++i)
    {
        auto adapter = as<IUnknownCastableAdapter>(m_list[i]);
        if (adapter && adapter->getContained() == unk)
        {
            return i;
        }
    }
    return -1;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactList !!!!!!!!!!!!!!!!!!!!!!!!!!! */

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

void* ArtifactList::getObject(const Guid& guid)
{
    // For now we can't cast to an object
    SLANG_UNUSED(guid);
    return nullptr;
}

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

void ArtifactList::add(IArtifact* artifact)
{
    // Must be set
    SLANG_ASSERT(artifact);
    // Can't already be in the list
    SLANG_ASSERT(m_artifacts.indexOf(artifact) < 0);
    // Can't have another owner
    SLANG_ASSERT(artifact->getParent() == nullptr);

    // Set the parent
    artifact->setParent(m_parent);

    // Add
    m_artifacts.add(ComPtr<IArtifact>(artifact));
}

void ArtifactList::removeAt(Index index) 
{
   IArtifact* artifact = m_artifacts[index];
   artifact->setParent(nullptr);
   m_artifacts.removeAt(index); 
}

void ArtifactList::clear()
{
    _setParent(nullptr);
    m_artifacts.clear();
}

void ArtifactList::_setParent(IArtifact* parent)
{
    if (m_parent == parent)
    {
        return;
    }

    for (IArtifact* artifact : m_artifacts)
    {
        artifact->setParent(artifact);
    }
}

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

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

bool Artifact::exists()
{
    for (auto rep : m_representations.getView())
    {
        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, IFileArtifactRepresentation** outFileRep)
{
    auto util = ArtifactUtilImpl::getSingleton();
    return util->requireFileDefaultImpl(this, keep, outFileRep);
}

SlangResult Artifact::loadBlob(Keep keep, ISlangBlob** outBlob)
{
    // If we have a blob just return it
    if (auto blob = (ISlangBlob*)findRepresentation(ISlangBlob::getTypeGuid()))
    {
        blob->addRef();
        *outBlob = blob;
        return SLANG_OK;
    }

    ComPtr<ISlangBlob> blob;

    // Look for a representation that we can serialize into a blob
    for (auto rep : m_representations.getView())
    {
        if (auto artifactRep = as<IArtifactRepresentation>(rep))
        {
            SlangResult res = artifactRep->writeToBlob(blob.writeRef());
            if (SLANG_SUCCEEDED(res) && blob)
            {
                break;
            }
        }
    }
     
    // Wasn't able to construct
    if (!blob)
    {
        return SLANG_E_NOT_FOUND;
    }

    // Put in cache 
    if (canKeep(keep))
    {
        addRepresentationUnknown(blob);
    }

    *outBlob = blob.detach();
    return SLANG_OK;
}

void Artifact::addAssociated(ICastable* castable)
{
    SLANG_ASSERT(castable);
    m_associated.add(castable);
}
 
void* Artifact::findAssociated(const Guid& guid)
{
    return m_associated.find(guid);
}

ICastableList* Artifact::getAssociated()
{
    return m_associated.requireList();
}

void Artifact::addRepresentation(IArtifactRepresentation* rep)
{
    SLANG_ASSERT(rep);
    if (m_representations.indexOf(rep) >= 0)
    {
        SLANG_ASSERT_FAILURE("Already have this representation");
        return;
    }
    m_representations.add(rep);
}

void Artifact::addRepresentationUnknown(ISlangUnknown* unk)
{
    SLANG_ASSERT(unk);
    if (m_representations.indexOfUnknown(unk) >= 0)
    {
        SLANG_ASSERT_FAILURE("Already have this representation");
        return;
    }

    ComPtr<ICastable> castable;
    if (SLANG_SUCCEEDED(unk->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())) && castable)
    {
        if (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(adapter);
    }
}

void* Artifact::findRepresentation(const Guid& guid)
{
    return m_representations.find(guid);
}

ICastableList* Artifact::getRepresentations()
{
    return m_representations.requireList();
}

} // namespace Slang