summaryrefslogtreecommitdiff
path: root/source/slang/slang-emit-c-like.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/slang/slang-emit-c-like.cpp')
-rw-r--r--source/slang/slang-emit-c-like.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp
index a74459954..d26893987 100644
--- a/source/slang/slang-emit-c-like.cpp
+++ b/source/slang/slang-emit-c-like.cpp
@@ -3321,6 +3321,20 @@ bool CLikeSourceEmitter::isTargetIntrinsic(IRInst* inst)
return findTargetIntrinsicDefinition(inst, intrinsicDef);
}
+bool shouldWrappInExternCBlock(IRFunc* func)
+{
+ for (auto decor : func->getDecorations())
+ {
+ switch (decor->getOp())
+ {
+ case kIROp_ExternCDecoration:
+ case kIROp_CudaKernelDecoration:
+ return true;
+ }
+ }
+ return false;
+}
+
void CLikeSourceEmitter::emitFunc(IRFunc* func)
{
// Target-intrinsic functions should never be emitted
@@ -3329,6 +3343,15 @@ void CLikeSourceEmitter::emitFunc(IRFunc* func)
if (isTargetIntrinsic(func))
return;
+ bool shouldCloseExternCBlock = shouldWrappInExternCBlock(func);
+ if (shouldCloseExternCBlock)
+ {
+ // If this is a C++ `extern "C"` function, then we need to emit
+ // it as a C function, since that is what the C++ compiler will
+ // expect.
+ //
+ m_writer->emit("extern \"C\" {\n");
+ }
if(!isDefinition(func))
{
@@ -3345,6 +3368,10 @@ void CLikeSourceEmitter::emitFunc(IRFunc* func)
//
emitSimpleFunc(func);
}
+ if (shouldCloseExternCBlock)
+ {
+ m_writer->emit("}\n");
+ }
}
void CLikeSourceEmitter::emitFuncDecorationsImpl(IRFunc* func)