示例#1
0
/// Given a pointer to a tuple type, compute the addresses of each element and
/// add them to the ElementAddrs vector.
static void
getScalarizedElementAddresses(SILValue Pointer, SILBuilder &B, SILLocation Loc,
                              SmallVectorImpl<SILValue> &ElementAddrs) {
  TupleType *TT = Pointer->getType().castTo<TupleType>();
  for (auto Index : indices(TT->getElements())) {
    ElementAddrs.push_back(B.createTupleElementAddr(Loc, Pointer, Index));
  }
}
示例#2
0
NullablePtr<SILInstruction>
Projection::
createAddrProjection(SILBuilder &B, SILLocation Loc, SILValue Base) const {
    // Grab Base's type.
    SILType BaseTy = Base.getType();

    // If BaseTy is not an address type, bail.
    if (!BaseTy.isAddress())
        return nullptr;

    // If this projection is associated with an object type, convert its type to
    // an address type.
    //
    // *NOTE* We purposely do not handle local storage types here since we want to
    // always fail in such a case. That is handled by checking that Ty is an
    // address.
    SILType Ty = Type.isObject()? Type.getAddressType() : Type;

    if (!Ty.isAddress())
        return nullptr;

    // Ok, we now know that the type of Base and the type represented by the base
    // of this projection match and that this projection can be represented as
    // value. Create the instruction if we can. Otherwise, return nullptr.
    switch (getKind()) {
    case ProjectionKind::Struct:
        return B.createStructElementAddr(Loc, Base, cast<VarDecl>(getDecl()));
    case ProjectionKind::Tuple:
        return B.createTupleElementAddr(Loc, Base, getIndex());
    case ProjectionKind::Index: {
        auto Ty = SILType::getBuiltinIntegerType(32, B.getASTContext());
        auto *IntLiteral = B.createIntegerLiteral(Loc, Ty, getIndex());
        return B.createIndexAddr(Loc, Base, IntLiteral);
    }
    case ProjectionKind::Enum:
        return B.createUncheckedTakeEnumDataAddr(Loc, Base,
                cast<EnumElementDecl>(getDecl()));
    case ProjectionKind::Class:
        return B.createRefElementAddr(Loc, Base, cast<VarDecl>(getDecl()));
    }
}