From 9566e8af25f87ad034a984db9d847942e454a180 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 25 Jul 2022 10:08:28 -0700 Subject: Allow `class` to implement COM interface, [DLLExport] (#2338) * Allow `class` to implement COM interface, [DLLExport] * Fix [COM] usage in tests and examples with UUIDs. Co-authored-by: Yong He --- source/slang/slang-ir-dll-export.cpp | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 source/slang/slang-ir-dll-export.cpp (limited to 'source/slang/slang-ir-dll-export.cpp') diff --git a/source/slang/slang-ir-dll-export.cpp b/source/slang/slang-ir-dll-export.cpp new file mode 100644 index 000000000..a8b464b43 --- /dev/null +++ b/source/slang/slang-ir-dll-export.cpp @@ -0,0 +1,72 @@ +// slang-ir-dll-export.cpp +#include "slang-ir-dll-export.h" + +#include "slang-ir.h" +#include "slang-ir-insts.h" +#include "slang-ir-marshal-native-call.h" + +namespace Slang +{ + +struct DllExportContext +{ + IRModule* module; + DiagnosticSink* diagnosticSink; + + SharedIRBuilder sharedBuilder; + + void processFunc(IRFunc* func, IRDllExportDecoration* dllExportDecoration) + { + NativeCallMarshallingContext marshalContext; + marshalContext.diagnosticSink = diagnosticSink; + IRBuilder builder(sharedBuilder); + auto wrapper = marshalContext.generateDLLExportWrapperFunc(builder, func); + dllExportDecoration->removeFromParent(); + dllExportDecoration->insertAtStart(wrapper); + builder.addNameHintDecoration(wrapper, dllExportDecoration->getFunctionName()); + builder.addExternCppDecoration(wrapper, dllExportDecoration->getFunctionName()); + builder.addPublicDecoration(wrapper); + builder.addKeepAliveDecoration(wrapper); + builder.addHLSLExportDecoration(wrapper); + if (auto oldPublicDecoration = func->findDecoration()) + { + oldPublicDecoration->removeFromParent(); + } + } + + void processModule() + { + struct Candidate { IRFunc* func; IRDllExportDecoration* exportDecoration; }; + List candidates; + for (auto childFunc : module->getGlobalInsts()) + { + switch(childFunc->getOp()) + { + case kIROp_Func: + if (auto dllExportDecoration = childFunc->findDecoration()) + { + candidates.add(Candidate{ as(childFunc), dllExportDecoration }); + } + break; + default: + break; + } + } + + for (auto candidate : candidates) + { + processFunc(candidate.func, candidate.exportDecoration); + } + } +}; + +void generateDllExportFuncs(IRModule* module, DiagnosticSink* sink) +{ + DllExportContext context; + context.module = module; + context.diagnosticSink = sink; + context.sharedBuilder.init(module); + return context.processModule(); +} + +} -- cgit v1.2.3