summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-lower-generic-type.cpp
blob: ae085145c386c2c1df19b694ec51f3120de1b89f (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
// slang-ir-lower-generic-type.cpp
#include "slang-ir-lower-generic-type.h"

#include "slang-ir-generics-lowering-context.h"
#include "slang-ir.h"
#include "slang-ir-clone.h"
#include "slang-ir-insts.h"

namespace Slang
{
    // This is a subpass of generics lowering IR transformation.
    // This pass lowers all generic/polymorphic types into IRAnyValueType.
    struct GenericTypeLoweringContext
    {
        SharedGenericsLoweringContext* sharedContext;

        IRInst* processInst(IRInst* inst)
        {
            // Ensure public struct types has RTTI object defined.
            if (as<IRStructType>(inst))
            {
                if (inst->findDecoration<IRPublicDecoration>())
                {
                    sharedContext->maybeEmitRTTIObject(inst);
                }
            }

            // Don't modify type insts themselves.
            if (as<IRType>(inst))
                return inst;

            IRBuilder builderStorage(sharedContext->module);
            auto builder = &builderStorage;
            builder->setInsertBefore(inst);
           
            auto newType = sharedContext->lowerType(builder, inst->getFullType());
            if (newType != inst->getFullType())
                inst = builder->replaceOperand(&inst->typeUse, newType);

            switch (inst->getOp())
            {
            default:
                break;
            case kIROp_StructField:
                {
                    // Translate the struct field type.
                    auto structField = static_cast<IRStructField*>(inst);
                    auto loweredFieldType =
                        sharedContext->lowerType(builder, structField->getFieldType());
                    structField->setOperand(1, loweredFieldType);
                }
                break;
            }
            return inst;
        }

        void processModule()
        {
            sharedContext->addToWorkList(sharedContext->module->getModuleInst());

            while (sharedContext->workList.getCount() != 0)
            {
                IRInst* inst = sharedContext->workList.getLast();

                sharedContext->workList.removeLast();
                sharedContext->workListSet.remove(inst);

                inst = processInst(inst);

                for (auto child = inst->getLastChild(); child; child = child->getPrevInst())
                {
                    sharedContext->addToWorkList(child);
                }
            }
            sharedContext->mapInterfaceRequirementKeyValue.clear();
        }
    };

    void lowerGenericType(SharedGenericsLoweringContext* sharedContext)
    {
        GenericTypeLoweringContext context;
        context.sharedContext = sharedContext;
        context.processModule();
    }
}