ManagedValue SILGenBuilder::createStructExtract(SILLocation loc, ManagedValue base, VarDecl *decl) { ManagedValue borrowedBase = base.borrow(SGF, loc); SILValue extract = createStructExtract(loc, borrowedBase.getValue(), decl); return ManagedValue::forUnmanaged(extract); }
ManagedValue SILGenBuilder::createOpenExistentialBoxValue(SILLocation loc, ManagedValue original, SILType type) { ManagedValue borrowedExistential = original.borrow(SGF, loc); SILValue openedExistential = createOpenExistentialBoxValue(loc, borrowedExistential.getValue(), type); return ManagedValue::forUnmanaged(openedExistential); }
ManagedValue SILGenBuilder::createRefElementAddr(SILLocation loc, ManagedValue operand, VarDecl *field, SILType resultTy) { operand = operand.borrow(SGF, loc); SILValue result = createRefElementAddr(loc, operand.getValue(), field); return ManagedValue::forUnmanaged(result); }
ManagedValue SILGenBuilder::createUncheckedEnumData(SILLocation loc, ManagedValue operand, EnumElementDecl *element) { if (operand.hasCleanup()) { SILValue newValue = SILBuilder::createUncheckedEnumData(loc, operand.forward(gen), element); return gen.emitManagedRValueWithCleanup(newValue); } ManagedValue borrowedBase = operand.borrow(gen, loc); SILValue newValue = SILBuilder::createUncheckedEnumData( loc, borrowedBase.getValue(), element); return ManagedValue::forUnmanaged(newValue); }
void SILGenFunction::emitClassMemberDestruction(ManagedValue selfValue, ClassDecl *cd, CleanupLocation cleanupLoc) { selfValue = selfValue.borrow(*this, cleanupLoc); for (VarDecl *vd : cd->getStoredProperties()) { const TypeLowering &ti = getTypeLowering(vd->getType()); if (!ti.isTrivial()) { SILValue addr = B.createRefElementAddr(cleanupLoc, selfValue.getValue(), vd, ti.getLoweredType().getAddressType()); B.createDestroyAddr(cleanupLoc, addr); } } }
/// Bridge the given Swift value to its corresponding Objective-C /// object, using the appropriate witness for the /// _ObjectiveCBridgeable._bridgeToObjectiveC requirement. static Optional<ManagedValue> emitBridgeNativeToObjectiveC(SILGenFunction &gen, SILLocation loc, ManagedValue swiftValue, ProtocolConformance *conformance) { // Dig out the nominal type we're bridging from. Type swiftValueType = swiftValue.getSwiftType()->getRValueType(); // Find the _bridgeToObjectiveC requirement. auto requirement = gen.SGM.getBridgeToObjectiveCRequirement(loc); if (!requirement) return None; // Retrieve the _bridgeToObjectiveC witness. auto witness = conformance->getWitness(requirement, nullptr); assert(witness); // Determine the type we're bridging to. auto objcTypeReq = gen.SGM.getBridgedObjectiveCTypeRequirement(loc); if (!objcTypeReq) return None; Type objcType = conformance->getTypeWitness(objcTypeReq, nullptr).getReplacement(); assert(objcType); // Create a reference to the witness. SILDeclRef witnessConstant(witness.getDecl()); auto witnessRef = gen.emitGlobalFunctionRef(loc, witnessConstant); // Determine the substitutions. ArrayRef<Substitution> substitutions; auto witnessFnTy = witnessRef->getType(); if (auto valueTypeBGT = swiftValueType->getAs<BoundGenericType>()) { // Compute the substitutions. substitutions = valueTypeBGT->getSubstitutions(gen.SGM.SwiftModule, nullptr); // Substitute into the witness function tye. witnessFnTy = witnessFnTy.substGenericArgs(gen.SGM.M, substitutions); } // Call the witness. SILType resultTy = gen.getLoweredType(objcType); SILValue bridgedValue = gen.B.createApply(loc, witnessRef, witnessFnTy, resultTy, substitutions, swiftValue.borrow() .getUnmanagedValue()); return gen.emitManagedRValueWithCleanup(bridgedValue); }