/// Perform a foreign error check by testing whether the error was nil. static void emitErrorIsNonNilErrorCheck(SILGenFunction &gen, SILLocation loc, ManagedValue errorSlot, bool suppressErrorCheck) { // If we're suppressing the check, just don't check. if (suppressErrorCheck) return; SILValue optionalError = gen.B.createLoad(loc, errorSlot.getValue()); OptionalTypeKind optKind; optionalError->getType().getAnyOptionalObjectType(gen.SGM.M, optKind); ASTContext &ctx = gen.getASTContext(); // Switch on the optional error. SILBasicBlock *errorBB = gen.createBasicBlock(FunctionSection::Postmatter); SILBasicBlock *contBB = gen.createBasicBlock(); gen.B.createSwitchEnum(loc, optionalError, /*default*/ nullptr, { { ctx.getOptionalSomeDecl(optKind), errorBB }, { ctx.getOptionalNoneDecl(optKind), contBB } }); // Emit the error block. Just be lazy and reload the error there. gen.emitForeignErrorBlock(loc, errorBB, errorSlot); // Return the result. gen.B.emitBlock(contBB); return; }
/// Perform a foreign error check by testing whether the error was nil. static void emitErrorIsNonNilErrorCheck(SILGenFunction &gen, SILLocation loc, ManagedValue errorSlot, bool suppressErrorCheck) { // If we're suppressing the check, just don't check. if (suppressErrorCheck) return; SILValue optionalError = gen.B.emitLoadValueOperation( loc, errorSlot.forward(gen), LoadOwnershipQualifier::Take); ASTContext &ctx = gen.getASTContext(); // Switch on the optional error. SILBasicBlock *errorBB = gen.createBasicBlock(FunctionSection::Postmatter); errorBB->createArgument(optionalError->getType().unwrapAnyOptionalType()); SILBasicBlock *contBB = gen.createBasicBlock(); gen.B.createSwitchEnum(loc, optionalError, /*default*/ nullptr, { { ctx.getOptionalSomeDecl(), errorBB }, { ctx.getOptionalNoneDecl(), contBB } }); // Emit the error block. Pass in none for the errorSlot since we have passed // in the errorSlot as our BB argument so we can pass ownership correctly. In // emitForeignErrorBlock, we will create the appropriate cleanup for the // argument. gen.emitForeignErrorBlock(loc, errorBB, None); // Return the result. gen.B.emitBlock(contBB); return; }
/// Perform a foreign error check by testing whether the call result is zero. /// The call result is otherwise ignored. static void emitResultIsZeroErrorCheck(SILGenFunction &gen, SILLocation loc, ManagedValue result, ManagedValue errorSlot, bool suppressErrorCheck, bool zeroIsError) { // Just ignore the call result if we're suppressing the error check. if (suppressErrorCheck) { return; } SILValue resultValue = emitUnwrapIntegerResult(gen, loc, result.getUnmanagedValue()); SILValue zero = gen.B.createIntegerLiteral(loc, resultValue->getType(), 0); ASTContext &ctx = gen.getASTContext(); SILValue resultIsError = gen.B.createBuiltinBinaryFunction(loc, zeroIsError ? "cmp_eq" : "cmp_ne", resultValue->getType(), SILType::getBuiltinIntegerType(1, ctx), {resultValue, zero}); SILBasicBlock *errorBB = gen.createBasicBlock(FunctionSection::Postmatter); SILBasicBlock *contBB = gen.createBasicBlock(); gen.B.createCondBranch(loc, resultIsError, errorBB, contBB); gen.emitForeignErrorBlock(loc, errorBB, errorSlot); gen.B.emitBlock(contBB); }
/// Perform a foreign error check by testing whether the call result is zero. /// The call result is otherwise ignored. static void emitResultIsZeroErrorCheck(SILGenFunction &SGF, SILLocation loc, ManagedValue result, ManagedValue errorSlot, bool suppressErrorCheck, bool zeroIsError) { // Just ignore the call result if we're suppressing the error check. if (suppressErrorCheck) { return; } SILValue resultValue = emitUnwrapIntegerResult(SGF, loc, result.getUnmanagedValue()); CanType resultType = resultValue->getType().getSwiftRValueType(); if (!resultType->isBuiltinIntegerType(1)) { SILValue zero = SGF.B.createIntegerLiteral(loc, resultValue->getType(), 0); ASTContext &ctx = SGF.getASTContext(); resultValue = SGF.B.createBuiltinBinaryFunction(loc, "cmp_ne", resultValue->getType(), SILType::getBuiltinIntegerType(1, ctx), {resultValue, zero}); } SILBasicBlock *errorBB = SGF.createBasicBlock(FunctionSection::Postmatter); SILBasicBlock *contBB = SGF.createBasicBlock(); if (zeroIsError) SGF.B.createCondBranch(loc, resultValue, contBB, errorBB); else SGF.B.createCondBranch(loc, resultValue, errorBB, contBB); SGF.emitForeignErrorBlock(loc, errorBB, errorSlot); SGF.B.emitBlock(contBB); }
/// Perform a foreign error check by testing whether the call result is nil. static ManagedValue emitResultIsNilErrorCheck(SILGenFunction &gen, SILLocation loc, ManagedValue origResult, ManagedValue errorSlot, bool suppressErrorCheck) { // Take local ownership of the optional result value. SILValue optionalResult = origResult.forward(gen); OptionalTypeKind optKind; SILType resultObjectType = optionalResult->getType().getAnyOptionalObjectType(gen.SGM.M, optKind); ASTContext &ctx = gen.getASTContext(); // If we're suppressing the check, just do an unchecked take. if (suppressErrorCheck) { SILValue objectResult = gen.B.createUncheckedEnumData(loc, optionalResult, ctx.getOptionalSomeDecl(optKind)); return gen.emitManagedRValueWithCleanup(objectResult); } // Switch on the optional result. SILBasicBlock *errorBB = gen.createBasicBlock(FunctionSection::Postmatter); SILBasicBlock *contBB = gen.createBasicBlock(); gen.B.createSwitchEnum(loc, optionalResult, /*default*/ nullptr, { { ctx.getOptionalSomeDecl(optKind), contBB }, { ctx.getOptionalNoneDecl(optKind), errorBB } }); // Emit the error block. gen.emitForeignErrorBlock(loc, errorBB, errorSlot); // In the continuation block, take ownership of the now non-optional // result value. gen.B.emitBlock(contBB); SILValue objectResult = contBB->createBBArg(resultObjectType); return gen.emitManagedRValueWithCleanup(objectResult); }