From 765061a77bcf4fe6300721263cc9e0f25595488d Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 10 May 2022 19:42:48 -0700 Subject: Initial support for COM interface in host code. (#2230) Co-authored-by: Yong He Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> --- source/slang/slang-ir-com-interface.cpp | 98 +++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 source/slang/slang-ir-com-interface.cpp (limited to 'source/slang/slang-ir-com-interface.cpp') 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 comPtrTypes; + + void replaceTypeUses(IRInst* inst, IRInst* newValue) + { + List 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()) + 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(type->getConstraintType())); + if (!comPtrType) + return; + replaceTypeUses(type, comPtrType); + } + + void processModule() + { + for (auto child : module->getGlobalInsts()) + { + switch (child->getOp()) + { + case kIROp_InterfaceType: + processInterfaceType(as(child)); + break; + case kIROp_ThisType: + processThisType(as(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(); +} + +} -- cgit v1.2.3