diff options
| author | Yong He <yonghe@outlook.com> | 2022-05-10 19:42:48 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-10 19:42:48 -0700 |
| commit | 765061a77bcf4fe6300721263cc9e0f25595488d (patch) | |
| tree | 5fe52269a46d8db85a869784f8637022f154c20f /source/slang/slang-ir-com-interface.cpp | |
| parent | dc541cc10783de541d8341b5fccee4c6019b5c6e (diff) | |
Initial support for COM interface in host code. (#2230)
Co-authored-by: Yong He <yhe@nvidia.com>
Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com>
Diffstat (limited to 'source/slang/slang-ir-com-interface.cpp')
| -rw-r--r-- | source/slang/slang-ir-com-interface.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/source/slang/slang-ir-com-interface.cpp b/source/slang/slang-ir-com-interface.cpp new file mode 100644 index 000000000..1bcf3d2b6 --- /dev/null +++ b/source/slang/slang-ir-com-interface.cpp @@ -0,0 +1,98 @@ +// slang-ir-com-interface.cpp +#include "slang-ir-com-interface.h" + +#include "slang-ir.h" +#include "slang-ir-insts.h" + +namespace Slang +{ + +struct ComInterfaceLoweringContext +{ + IRModule* module; + DiagnosticSink* diagnosticSink; + + SharedIRBuilder sharedBuilder; + + Dictionary<IRInterfaceType*, IRComPtrType*> comPtrTypes; + + void replaceTypeUses(IRInst* inst, IRInst* newValue) + { + List<IRUse*> uses; + for (auto use = inst->firstUse; use; use = use->nextUse) + { + uses.add(use); + } + for (auto use : uses) + { + switch (use->getUser()->getOp()) + { + case kIROp_WitnessTableIDType: + case kIROp_WitnessTableType: + case kIROp_ThisType: + case kIROp_RTTIPointerType: + case kIROp_RTTIHandleType: + case kIROp_ComPtrType: + continue; + default: + break; + } + use->set(newValue); + } + } + + IRComPtrType* processInterfaceType(IRInterfaceType* type) + { + if (!type->findDecoration<IRComInterfaceDecoration>()) + return nullptr; + + IRComPtrType* result = nullptr; + + if (comPtrTypes.TryGetValue(type, result)) + return result; + + IRBuilder builder(sharedBuilder); + builder.setInsertInto(module->getModuleInst()); + result = builder.getComPtrType(type); + + replaceTypeUses(type, result); + return result; + } + + void processThisType(IRThisType* type) + { + auto comPtrType = processInterfaceType(as<IRInterfaceType>(type->getConstraintType())); + if (!comPtrType) + return; + replaceTypeUses(type, comPtrType); + } + + void processModule() + { + for (auto child : module->getGlobalInsts()) + { + switch (child->getOp()) + { + case kIROp_InterfaceType: + processInterfaceType(as<IRInterfaceType>(child)); + break; + case kIROp_ThisType: + processThisType(as<IRThisType>(child)); + break; + default: + break; + } + } + } +}; + +void lowerComInterfaces(IRModule* module, DiagnosticSink* sink) +{ + ComInterfaceLoweringContext context; + context.module = module; + context.diagnosticSink = sink; + context.sharedBuilder.init(module); + return context.processModule(); +} + +} |
