static void buildBlockToFuncThunkBody(SILGenFunction &gen, SILLocation loc, CanSILFunctionType blockTy, CanSILFunctionType funcTy) { // Collect the native arguments, which should all be +1. Scope scope(gen.Cleanups, CleanupLocation::get(loc)); assert(blockTy->getParameters().size() == funcTy->getParameters().size() && "block and function types don't match"); SmallVector<ManagedValue, 4> args; SILBasicBlock *entry = &*gen.F.begin(); for (unsigned i : indices(funcTy->getParameters())) { auto ¶m = funcTy->getParameters()[i]; auto &blockParam = blockTy->getParameters()[i]; auto &tl = gen.getTypeLowering(param.getSILType()); assert((tl.isTrivial() ? param.getConvention() == ParameterConvention::Direct_Unowned : param.getConvention() == ParameterConvention::Direct_Owned) && "nonstandard conventions for native functions not implemented"); SILValue v = new (gen.SGM.M) SILArgument(entry, param.getSILType()); auto mv = gen.emitManagedRValueWithCleanup(v, tl); args.push_back(gen.emitNativeToBridgedValue(loc, mv, SILFunctionTypeRepresentation::Block, blockParam.getType())); } // Add the block argument. SILValue blockV = new (gen.SGM.M) SILArgument(entry, SILType::getPrimitiveObjectType(blockTy)); ManagedValue block = gen.emitManagedRValueWithCleanup(blockV); // Call the block. assert(!funcTy->hasIndirectResults() && "block thunking func with indirect result not supported"); ManagedValue result = gen.emitMonomorphicApply(loc, block, args, funcTy->getSILResult().getSwiftRValueType(), ApplyOptions::None, /*override CC*/ SILFunctionTypeRepresentation::Block, /*foreign error*/ None) .getAsSingleValue(gen, loc); // Return the result at +1. #ifndef NDEBUG for (auto result : funcTy->getDirectResults()) { assert((gen.getTypeLowering(result.getSILType()).isTrivial() ? result.getConvention() == ResultConvention::Unowned : result.getConvention() == ResultConvention::Owned) && "nonstandard conventions for return not implemented"); } #endif auto r = result.forward(gen); scope.pop(); gen.B.createReturn(loc, r); }