本文整理汇总了C++中SILGenFunction::emitManagedRValueWithCleanup方法的典型用法代码示例。如果您正苦于以下问题:C++ SILGenFunction::emitManagedRValueWithCleanup方法的具体用法?C++ SILGenFunction::emitManagedRValueWithCleanup怎么用?C++ SILGenFunction::emitManagedRValueWithCleanup使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SILGenFunction
的用法示例。
在下文中一共展示了SILGenFunction::emitManagedRValueWithCleanup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: buildBlockToFuncThunkBody
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,
AbstractionPattern(param.getType()),
param.getType(),
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->hasIndirectResult()
&& "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);
// Return the result at +1.
auto &resultTL = gen.getTypeLowering(funcTy->getSILResult());
auto convention = funcTy->getResult().getConvention();
assert((resultTL.isTrivial()
? convention == ResultConvention::Unowned
: convention == ResultConvention::Owned)
&& "nonstandard conventions for return not implemented");
(void)convention;
(void)resultTL;
auto r = result.forward(gen);
scope.pop();
gen.B.createReturn(loc, r);
}
示例2: emitImplicitValueConstructorArg
static RValue emitImplicitValueConstructorArg(SILGenFunction &SGF,
SILLocation loc,
CanType interfaceType,
DeclContext *DC) {
auto type = DC->mapTypeIntoContext(interfaceType)->getCanonicalType();
// Restructure tuple arguments.
if (auto tupleTy = dyn_cast<TupleType>(interfaceType)) {
RValue tuple(type);
for (auto fieldType : tupleTy.getElementTypes())
tuple.addElement(emitImplicitValueConstructorArg(SGF, loc, fieldType, DC));
return tuple;
}
auto &AC = SGF.getASTContext();
auto VD = new (AC) ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
AC.getIdentifier("$implicit_value"),
SourceLoc(),
AC.getIdentifier("$implicit_value"), Type(),
DC);
VD->setInterfaceType(interfaceType);
SILFunctionArgument *arg =
SGF.F.begin()->createFunctionArgument(SGF.getLoweredType(type), VD);
ManagedValue mvArg;
if (arg->getArgumentConvention().isOwnedConvention()) {
mvArg = SGF.emitManagedRValueWithCleanup(arg);
} else {
mvArg = ManagedValue::forUnmanaged(arg);
}
return RValue(SGF, loc, type, mvArg);
}
示例3: emitImplicitValueConstructorArg
static RValue emitImplicitValueConstructorArg(SILGenFunction &gen,
SILLocation loc,
CanType interfaceType,
DeclContext *DC) {
auto type = DC->mapTypeIntoContext(interfaceType)->getCanonicalType();
// Restructure tuple arguments.
if (CanTupleType tupleTy = dyn_cast<TupleType>(interfaceType)) {
RValue tuple(type);
for (auto fieldType : tupleTy.getElementTypes())
tuple.addElement(emitImplicitValueConstructorArg(gen, loc, fieldType, DC));
return tuple;
} else {
auto &AC = gen.getASTContext();
auto VD = new (AC) ParamDecl(/*IsLet*/ true, SourceLoc(), SourceLoc(),
AC.getIdentifier("$implicit_value"),
SourceLoc(),
AC.getIdentifier("$implicit_value"), Type(),
DC);
VD->setInterfaceType(interfaceType);
SILValue arg =
gen.F.begin()->createFunctionArgument(gen.getLoweredType(type), VD);
return RValue(gen, loc, type, gen.emitManagedRValueWithCleanup(arg));
}
}
示例4: emitBridgeCollectionFromNative
static ManagedValue emitBridgeCollectionFromNative(SILGenFunction &gen,
SILLocation loc,
SILDeclRef bridgeFnRef,
ManagedValue collection,
SILType bridgedTy) {
SILValue bridgeFn = gen.emitGlobalFunctionRef(loc, bridgeFnRef);
// If the expected return is optional, we'll need to wrap it.
OptionalTypeKind OTK = OTK_None;
SILType origBridgedTy = bridgedTy;
if (auto bridgedObjTy = bridgedTy.getAnyOptionalObjectType(gen.SGM.M, OTK)) {
bridgedTy = bridgedObjTy;
}
// Figure out the type parameters.
auto inputTy
= collection.getType().getSwiftRValueType()->castTo<BoundGenericType>();
auto subs = inputTy->getSubstitutions(gen.SGM.M.getSwiftModule(), nullptr);
auto substFnType = bridgeFn.getType().substGenericArgs(gen.SGM.M, subs);
SILValue bridged = gen.B.createApply(loc, bridgeFn,
substFnType,
bridgedTy,
subs,
{ collection.forward(gen) });
// Wrap the result if necessary.
if (OTK != OTK_None) {
bridged = gen.B.createEnum(loc, bridged,
gen.getASTContext().getOptionalSomeDecl(OTK),
origBridgedTy);
}
return gen.emitManagedRValueWithCleanup(bridged);
}
示例5: emitBridgeCollectionToNative
static ManagedValue emitBridgeCollectionToNative(SILGenFunction &gen,
SILLocation loc,
SILDeclRef bridgeFnRef,
ManagedValue collection,
SILType nativeTy) {
SILValue bridgeFn = gen.emitGlobalFunctionRef(loc, bridgeFnRef);
auto collectionTy = nativeTy.getSwiftRValueType()->castTo<BoundGenericType>();
auto subs = collectionTy->getSubstitutions(gen.SGM.M.getSwiftModule(),
nullptr);
auto substFnType = bridgeFn.getType().substGenericArgs(gen.SGM.M, subs);
Type inputType = collection.getType().getSwiftRValueType();
if (!inputType->getOptionalObjectType()) {
SILType loweredOptTy = gen.SGM.getLoweredType(OptionalType::get(inputType));
auto *someDecl = gen.getASTContext().getOptionalSomeDecl();
auto *enumInst = gen.B.createEnum(loc, collection.getValue(), someDecl,
loweredOptTy);
collection = ManagedValue(enumInst, collection.getCleanup());
}
SILValue result = gen.B.createApply(loc, bridgeFn,
substFnType,
nativeTy,
subs,
{ collection.forward(gen) });
return gen.emitManagedRValueWithCleanup(result);
}
示例6: emitBuiltinCastReferenceFromBridgeObject
/// Specialized emitter for Builtin.castReferenceFromBridgeObject.
static ManagedValue emitBuiltinCastReferenceFromBridgeObject(
SILGenFunction &gen,
SILLocation loc,
SubstitutionList subs,
ArrayRef<ManagedValue> args,
CanFunctionType formalApplyType,
SGFContext C) {
assert(args.size() == 1 && "cast should have one argument");
assert(subs.size() == 1 && "cast should have a type substitution");
// The substitution determines the destination type.
SILType destType = gen.getLoweredType(subs[0].getReplacement());
// Bail if the source type is not a class reference of some kind.
if (!subs[0].getReplacement()->isBridgeableObjectType()
|| !destType.isObject()) {
gen.SGM.diagnose(loc, diag::invalid_sil_builtin,
"castReferenceFromBridgeObject dest must be an object type");
// Recover by propagating an undef result.
SILValue result = SILUndef::get(destType, gen.SGM.M);
return ManagedValue::forUnmanaged(result);
}
SILValue result = gen.B.createBridgeObjectToRef(loc, args[0].forward(gen),
destType);
return gen.emitManagedRValueWithCleanup(result);
}
示例7: ManagedValue
/// Specialized emitter for Builtin.castReference.
static ManagedValue
emitBuiltinCastReference(SILGenFunction &gen,
SILLocation loc,
ArrayRef<Substitution> substitutions,
ArrayRef<ManagedValue> args,
CanFunctionType formalApplyType,
SGFContext C) {
assert(args.size() == 1 && "castReference should be given one argument");
assert(substitutions.size() == 2 && "castReference should have two subs");
auto fromTy = substitutions[0].getReplacement();
auto toTy = substitutions[1].getReplacement();
auto &fromTL = gen.getTypeLowering(fromTy);
auto &toTL = gen.getTypeLowering(toTy);
assert(!fromTL.isTrivial() && !toTL.isTrivial() && "expected ref type");
if (fromTL.isLoadable() || toTL.isLoadable()) {
if (auto refCast = gen.B.tryCreateUncheckedRefCast(loc, args[0].getValue(),
toTL.getLoweredType())) {
// Create a reference cast, forwarding the cleanup.
// The cast takes the source reference.
return ManagedValue(refCast, args[0].getCleanup());
}
}
// We are either casting between address-only types, or cannot promote to a
// cast of reference values.
//
// If the from/to types are invalid, then use a cast that will fail at
// runtime. We cannot catch these errors with SIL verification because they
// may legitimately occur during code specialization on dynamically
// unreachable paths.
//
// TODO: For now, we leave invalid casts in address form so that the runtime
// will trap. We could emit a noreturn call here instead which would provide
// more information to the optimizer.
SILValue srcVal = args[0].forward(gen);
SILValue fromAddr;
if (fromTL.isLoadable()) {
// Move the loadable value into a "source temp". Since the source and
// dest are RC identical, store the reference into the source temp without
// a retain. The cast will load the reference from the source temp and
// store it into a dest temp effectively forwarding the cleanup.
fromAddr = gen.emitTemporaryAllocation(loc, srcVal->getType());
gen.B.createStore(loc, srcVal, fromAddr);
} else {
// The cast loads directly from the source address.
fromAddr = srcVal;
}
// Create a "dest temp" to hold the reference after casting it.
SILValue toAddr = gen.emitTemporaryAllocation(loc, toTL.getLoweredType());
gen.B.createUncheckedRefCastAddr(loc, fromAddr, fromTy->getCanonicalType(),
toAddr, toTy->getCanonicalType());
// Forward it along and register a cleanup.
if (toTL.isAddressOnly())
return gen.emitManagedBufferWithCleanup(toAddr);
// Load the destination value.
auto result = gen.B.createLoad(loc, toAddr);
return gen.emitManagedRValueWithCleanup(result);
}
示例8: copy
/// Emit a copy of this value with independent ownership.
ManagedValue ManagedValue::copy(SILGenFunction &gen, SILLocation l) {
if (!cleanup.isValid()) {
assert(gen.getTypeLowering(getType()).isTrivial());
return *this;
}
auto &lowering = gen.getTypeLowering(getType());
assert(!lowering.isTrivial() && "trivial value has cleanup?");
if (!lowering.isAddressOnly()) {
lowering.emitRetainValue(gen.B, l, getValue());
return gen.emitManagedRValueWithCleanup(getValue(), lowering);
}
SILValue buf = gen.emitTemporaryAllocation(l, getType());
gen.B.createCopyAddr(l, getValue(), buf, IsNotTake, IsInitialization);
return gen.emitManagedRValueWithCleanup(buf, lowering);
}
示例9: copyUnmanaged
/// This is the same operation as 'copy', but works on +0 values that don't
/// have cleanups. It returns a +1 value with one.
ManagedValue ManagedValue::copyUnmanaged(SILGenFunction &SGF, SILLocation loc) {
if (getType().isObject()) {
return SGF.B.createCopyValue(loc, *this);
}
SILValue result = SGF.emitTemporaryAllocation(loc, getType());
SGF.B.createCopyAddr(loc, getValue(), result, IsNotTake, IsInitialization);
return SGF.emitManagedRValueWithCleanup(result);
}
示例10: boxIndirectEnumPayload
static void boxIndirectEnumPayload(SILGenFunction &gen,
ManagedValue &payload,
SILLocation loc,
EnumElementDecl *element) {
// If the payload is indirect, we'll need to box it.
if (payload && (element->isIndirect() ||
element->getParentEnum()->isIndirect())) {
auto box = gen.B.createAllocBox(loc, payload.getType());
payload.forwardInto(gen, loc, box->getAddressResult());
payload = gen.emitManagedRValueWithCleanup(box);
}
}
示例11: emitBridgeReturnValue
static SILValue emitBridgeReturnValue(SILGenFunction &gen,
SILLocation loc,
SILValue result,
SILFunctionTypeRepresentation fnTypeRepr,
CanType bridgedTy) {
Scope scope(gen.Cleanups, CleanupLocation::get(loc));
ManagedValue native = gen.emitManagedRValueWithCleanup(result);
ManagedValue bridged = gen.emitNativeToBridgedValue(loc, native, fnTypeRepr,
bridgedTy);
return bridged.forward(gen);
}
示例12: copy
/// Emit a copy of this value with independent ownership.
ManagedValue ManagedValue::copy(SILGenFunction &SGF, SILLocation loc) {
auto &lowering = SGF.getTypeLowering(getType());
if (lowering.isTrivial())
return *this;
if (getType().isObject()) {
return SGF.B.createCopyValue(loc, *this, lowering);
}
SILValue buf = SGF.emitTemporaryAllocation(loc, getType());
SGF.B.createCopyAddr(loc, getValue(), buf, IsNotTake, IsInitialization);
return SGF.emitManagedRValueWithCleanup(buf, lowering);
}
示例13: emitBridgeStringToNSString
static ManagedValue emitBridgeStringToNSString(SILGenFunction &gen,
SILLocation loc,
ManagedValue str) {
// func _convertStringToNSString(String) -> NSString
SILValue stringToNSStringFn
= gen.emitGlobalFunctionRef(loc, gen.SGM.getStringToNSStringFn());
SILValue nsstr = gen.B.createApply(loc, stringToNSStringFn,
stringToNSStringFn.getType(),
gen.getLoweredType(gen.SGM.Types.getNSStringType()),
{}, str.forward(gen));
return gen.emitManagedRValueWithCleanup(nsstr);
}
示例14: emitBridgeForeignBoolToBool
static ManagedValue emitBridgeForeignBoolToBool(SILGenFunction &gen,
SILLocation loc,
ManagedValue foreignBool,
SILDeclRef bridgingFnRef) {
// func _convertObjCBoolToBool(ObjCBool) -> Bool
SILValue bridgingFn = gen.emitGlobalFunctionRef(loc, bridgingFnRef);
SILType resultTy = gen.getLoweredLoadableType(gen.SGM.Types.getBoolType());
SILValue result = gen.B.createApply(loc, bridgingFn, bridgingFn->getType(),
resultTy, {}, foreignBool.forward(gen));
return gen.emitManagedRValueWithCleanup(result);
}
示例15: emitBridgeBoolToObjCBool
static ManagedValue emitBridgeBoolToObjCBool(SILGenFunction &gen,
SILLocation loc,
ManagedValue swiftBool) {
// func _convertBoolToObjCBool(Bool) -> ObjCBool
SILValue boolToObjCBoolFn
= gen.emitGlobalFunctionRef(loc, gen.SGM.getBoolToObjCBoolFn());
SILType resultTy =gen.getLoweredLoadableType(gen.SGM.Types.getObjCBoolType());
SILValue result = gen.B.createApply(loc, boolToObjCBoolFn,
boolToObjCBoolFn->getType(),
resultTy, {}, swiftBool.forward(gen));
return gen.emitManagedRValueWithCleanup(result);
}