summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-emit-base.cpp
blob: 0fe7b811c99824218eb6795893d2fcea3a12aaf1 (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
#include "slang-emit-base.h"

#include "slang-ir-util.h"

namespace Slang
{

IRInst* SourceEmitterBase::getSpecializedValue(IRSpecialize* specInst)
{
    auto base = specInst->getBase();

    // It is possible to have a `specialize(...)` where the first
    // operand is also a `specialize(...)`, so that we need to
    // look at what declaration is being specialized at the inner
    // step to find the one being specialized at the outer step.
    //
    while (auto baseSpecialize = as<IRSpecialize>(base))
    {
        base = getSpecializedValue(baseSpecialize);
    }

    auto baseGeneric = as<IRGeneric>(base);
    if (!baseGeneric)
        return base;

    auto lastBlock = baseGeneric->getLastBlock();
    if (!lastBlock)
        return base;

    auto returnInst = as<IRReturn>(lastBlock->getTerminator());
    if (!returnInst)
        return base;

    return returnInst->getVal();
}

void SourceEmitterBase::handleRequiredCapabilities(IRInst* inst)
{
    auto decoratedValue = inst;
    while (auto specInst = as<IRSpecialize>(decoratedValue))
    {
        decoratedValue = getSpecializedValue(specInst);
    }

    handleRequiredCapabilitiesImpl(decoratedValue);
}

IRVarLayout* SourceEmitterBase::getVarLayout(IRInst* var)
{
    return findVarLayout(var);
}

BaseType SourceEmitterBase::extractBaseType(IRType* inType)
{
    auto type = inType;
    for (;;)
    {
        if (auto irBaseType = as<IRBasicType>(type))
        {
            return irBaseType->getBaseType();
        }
        else if (auto vecType = as<IRVectorType>(type))
        {
            type = vecType->getElementType();
            continue;
        }
        else
        {
            return BaseType::Void;
        }
    }
}

} // namespace Slang