diff options
| author | Julius Ikkala <julius.ikkala@gmail.com> | 2025-05-23 22:27:37 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-23 12:27:37 -0700 |
| commit | 57c3f938221c427b78da7087f8a832ba4a271a7c (patch) | |
| tree | e9a6d26278dc1ad75b222ac4fc9b7a1d8449e576 /source/slang/slang-ir-marshal-native-call.cpp | |
| parent | d108bfa677c70808b32bd77e93637ed34c19c75d (diff) | |
Implement throw & catch statements (#6916)
* Implement throw statement
It already existed in the IR, so only parsing, checking and lowering was
missing.
* Initial catch implementation
Likely very broken.
* Error out when catch() isn't last in scope
* Prevent accessing variables from scope preceding catch
As those may actually not be available at that point.
* Add IError and use it in Result type lowering
* Add diagnostic tests
* Allow caught throws in non-throw functions
* Fix catch propagating between functions & SPIR-V merge issue
* Add test for non-trivial error types
* Fix MSVC build
* Fix invalid value type from Result lowering
* Also lower error handling in templates
* Lower result types only after specialization
* Attempt to disambiguate error enums by witness table
* Revert matching by witness, types should be distinct too
* Don't assert valueField when getting Result's error value
It may not exist if the function returns void, but getting the error
value is still legitimate.
* Update tests for new error numbers & get rid of expected.txt
* Change catch lowering to resemble breaking a loop
... To make SPIR-V happy.
* Fix dead catch blocks and invalid cached dominator tree
* More SPIR-V adjustment
* Lower catch as two nested loops
* Add defer interaction test and revert broken defer changes
* Fix enum type when throwing literals
* Cleanup and bikeshedding
* Document error handling mechanism
* Fix table of contents
* Use boolean tag in Result<T, E>
* Use anyValue storage for Result<T,E>
* Remove IError
* Fix formatting
* Eradicate success values from docs and tests
* Use parseModernParamDecl for catch parameter
* Implement do-catch syntax
* Implement catch-all
* Fix formatting
* Fix marshalling native calls that throw
---------
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source/slang/slang-ir-marshal-native-call.cpp')
| -rw-r--r-- | source/slang/slang-ir-marshal-native-call.cpp | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/source/slang/slang-ir-marshal-native-call.cpp b/source/slang/slang-ir-marshal-native-call.cpp index 2257ef098..1d3f04318 100644 --- a/source/slang/slang-ir-marshal-native-call.cpp +++ b/source/slang/slang-ir-marshal-native-call.cpp @@ -42,8 +42,10 @@ IRFuncType* NativeCallMarshallingContext::getNativeFuncType( if (auto resultType = as<IRResultType>(declaredFuncType->getResultType())) { auto nativeResultType = getNativeType(builder, resultType->getValueType()); + auto nativeErrorType = getNativeType(builder, resultType->getErrorType()); nativeParamTypes.add(builder.getPtrType(nativeResultType)); - returnType = resultType->getErrorType(); + nativeParamTypes.add(builder.getPtrType(nativeErrorType)); + returnType = builder.getIntType(); } else { @@ -243,10 +245,8 @@ IRFunc* NativeCallMarshallingContext::generateDLLExportWrapperFunc( IRBlock* trueBlock = nullptr; IRBlock* falseBlock = nullptr; IRBlock* afterBlock = nullptr; - builder.emitIfElseWithBlocks(isResultError, trueBlock, falseBlock, afterBlock); - builder.setInsertInto(trueBlock); - builder.emitReturn(builder.emitGetResultError(callInst)); + builder.emitIfElseWithBlocks(isResultError, trueBlock, falseBlock, afterBlock); builder.setInsertInto(falseBlock); auto resultVal = builder.emitGetResultValue(callInst); @@ -258,8 +258,22 @@ IRFunc* NativeCallMarshallingContext::generateDLLExportWrapperFunc( builder.emitStore(params[nativeParamConsumeIndex], nativeVals[i]); nativeParamConsumeIndex++; } + // S_OK builder.emitReturn(builder.getIntValue(builder.getIntType(), 0)); + builder.setInsertInto(trueBlock); + nativeVals.clear(); + auto errorVal = builder.emitGetResultError(callInst); + marshalManagedValueToNativeResultValue(builder, errorVal, nativeVals); + for (Index i = 0; i < nativeVals.getCount(); i++) + { + SLANG_RELEASE_ASSERT(nativeParamConsumeIndex < params.getCount()); + builder.emitStore(params[nativeParamConsumeIndex], nativeVals[i]); + nativeParamConsumeIndex++; + } + // E_FAIL + builder.emitReturn(builder.getIntValue(builder.getIntType(), 0x80004005)); + builder.setInsertInto(afterBlock); builder.emitUnreachable(); } @@ -296,10 +310,13 @@ IRInst* NativeCallMarshallingContext::marshalNativeCall( IRType* originalReturnType = originalFuncType->getResultType(); IRVar* resultVar = nullptr; + IRVar* errorVar = nullptr; if (auto resultType = as<IRResultType>(originalReturnType)) { // Declare a local variable to receive result. resultVar = builder.emitVar(getNativeType(builder, resultType->getValueType())); + errorVar = builder.emitVar(getNativeType(builder, resultType->getErrorType())); + args.add(resultVar); args.add(resultVar); } @@ -314,16 +331,18 @@ IRInst* NativeCallMarshallingContext::marshalNativeCall( if (auto resultType = as<IRResultType>(originalReturnType)) { auto val = builder.emitLoad(resultVar); - auto err = call; + auto err = builder.emitLoad(errorVar); + auto tag = call; val = marshalNativeValueToManagedValue(builder, val); - auto intErr = err; - if (err->getDataType()->getOp() != kIROp_IntType) + err = marshalNativeValueToManagedValue(builder, err); + auto intTag = tag; + if (tag->getDataType()->getOp() != kIROp_IntType) { - intErr = builder.emitCast(builder.getIntType(), err); + intTag = builder.emitCast(builder.getIntType(), tag); } - auto errIsError = builder.emitLess(intErr, builder.getIntValue(builder.getIntType(), 0)); + auto tagIsError = builder.emitLess(intTag, builder.getIntValue(builder.getIntType(), 0)); IRBlock *trueBlock, *falseBlock, *afterBlock; - builder.emitIfElseWithBlocks(errIsError, trueBlock, falseBlock, afterBlock); + builder.emitIfElseWithBlocks(tagIsError, trueBlock, falseBlock, afterBlock); builder.setInsertInto(trueBlock); returnValue = builder.emitMakeResultError(resultType, err); builder.emitBranch(afterBlock, 1, &returnValue); |
