mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Refactor cast representation in AST and SIL, and implement 'is'.
Improve our representations of casts in the AST and SIL so that 'as!' and 'is' (and eventually 'as?') can share almost all of the same type-checking, SILGen, and IRGen code. In the AST, we now represent 'as!' and 'is' as UnconditionalCheckedCastExpr and IsaExpr, respectively, with the semantic variations of cast (downcast, super-to-archetype, archetype-to-concrete, etc.) discriminated by an enum field. This keeps the user-visible syntactic and type behavior differences of the two forms cleanly separated for AST consumers. At the SIL level, we transpose the representation so that the different cast semantics get their own instructions and the conditional/unconditional cast behavior is indicated by an enum, making it easy for IRGen to discriminate the different code paths for the different semantics. We also add an 'IsNonnull' instruction to cover the conditional-cast-result-to-boolean conversion common to all the forms of 'is'. The upshot of all this is that 'x is T' now works for all the new archetype and existential cast forms supported by 'as!'. Swift SVN r5737
This commit is contained in:
@@ -241,11 +241,6 @@ public:
|
||||
UpcastInst(Loc, Op, Ty));
|
||||
}
|
||||
|
||||
DowncastInst *createDowncast(SILLocation Loc, SILValue Op, SILType Ty) {
|
||||
return insert(new (F.getModule())
|
||||
DowncastInst(Loc, Op, Ty));
|
||||
}
|
||||
|
||||
AddressToPointerInst *createAddressToPointer(SILLocation Loc, SILValue Op,
|
||||
SILType Ty) {
|
||||
return insert(new (F.getModule())
|
||||
@@ -306,44 +301,55 @@ public:
|
||||
ArchetypeRefToSuperInst(Loc, Archetype, BaseTy));
|
||||
}
|
||||
|
||||
DowncastInst *createDowncast(SILLocation Loc, SILValue Op, SILType Ty,
|
||||
CheckedCastMode Mode) {
|
||||
return insert(new (F.getModule())
|
||||
DowncastInst(Loc, Op, Ty, Mode));
|
||||
}
|
||||
|
||||
SuperToArchetypeRefInst *createSuperToArchetypeRef(SILLocation Loc,
|
||||
SILValue Archetype,
|
||||
SILType BaseTy) {
|
||||
SILType BaseTy,
|
||||
CheckedCastMode Mode) {
|
||||
return insert(new (F.getModule())
|
||||
SuperToArchetypeRefInst(Loc, Archetype, BaseTy));
|
||||
SuperToArchetypeRefInst(Loc, Archetype, BaseTy, Mode));
|
||||
}
|
||||
|
||||
DowncastArchetypeAddrInst *createDowncastArchetypeAddr(SILLocation Loc,
|
||||
SILValue Archetype,
|
||||
SILType Ty) {
|
||||
SILType Ty,
|
||||
CheckedCastMode Mode) {
|
||||
return insert(new (F.getModule())
|
||||
DowncastArchetypeAddrInst(Loc, Archetype, Ty));
|
||||
DowncastArchetypeAddrInst(Loc, Archetype, Ty, Mode));
|
||||
}
|
||||
DowncastArchetypeRefInst *createDowncastArchetypeRef(SILLocation Loc,
|
||||
SILValue Archetype,
|
||||
SILType Ty) {
|
||||
SILType Ty,
|
||||
CheckedCastMode Mode) {
|
||||
return insert(new (F.getModule())
|
||||
DowncastArchetypeRefInst(Loc, Archetype, Ty));
|
||||
DowncastArchetypeRefInst(Loc, Archetype, Ty, Mode));
|
||||
}
|
||||
ProjectDowncastExistentialAddrInst *createProjectDowncastExistentialAddr(SILLocation Loc,
|
||||
ProjectDowncastExistentialAddrInst *createProjectDowncastExistentialAddr(
|
||||
SILLocation Loc,
|
||||
SILValue Existential,
|
||||
SILType Ty) {
|
||||
SILType Ty,
|
||||
CheckedCastMode Mode) {
|
||||
return insert(new (F.getModule())
|
||||
ProjectDowncastExistentialAddrInst(Loc, Existential, Ty));
|
||||
ProjectDowncastExistentialAddrInst(Loc, Existential, Ty, Mode));
|
||||
}
|
||||
DowncastExistentialRefInst *createDowncastExistentialRef(SILLocation Loc,
|
||||
SILValue Existential,
|
||||
SILType Ty) {
|
||||
SILType Ty,
|
||||
CheckedCastMode Mode) {
|
||||
return insert(new (F.getModule())
|
||||
DowncastExistentialRefInst(Loc, Existential, Ty));
|
||||
DowncastExistentialRefInst(Loc, Existential, Ty, Mode));
|
||||
}
|
||||
|
||||
IsaInst *createIsa(SILLocation Loc,
|
||||
SILValue Operand,
|
||||
SILType TestType,
|
||||
SILType ResultType) {
|
||||
IsNonnullInst *createIsNonnull(SILLocation Loc,
|
||||
SILValue Operand,
|
||||
SILType ResultType) {
|
||||
return insert(new (F.getModule())
|
||||
IsaInst(Loc, Operand, TestType, ResultType));
|
||||
IsNonnullInst(Loc, Operand, ResultType));
|
||||
}
|
||||
|
||||
StructInst *createStruct(SILLocation Loc, SILType Ty,
|
||||
|
||||
Reference in New Issue
Block a user