@@ -1025,6 +1025,76 @@ class alignas(1 << TypeAlignInBits) TypeBase {
1025
1025
void *operator new (size_t Bytes, void *Mem) throw () { return Mem; }
1026
1026
};
1027
1027
1028
+ // / AnyGenericType - This abstract class helps types ensure that fields
1029
+ // / exist at the same offset in memory to improve code generation of the
1030
+ // / compiler itself.
1031
+ class AnyGenericType : public TypeBase {
1032
+ friend class NominalOrBoundGenericNominalType ;
1033
+
1034
+ // / TheDecl - This is the TypeDecl which declares the given type. It
1035
+ // / specifies the name and other useful information about this type.
1036
+ union {
1037
+ GenericTypeDecl *GenDecl;
1038
+ NominalTypeDecl *NomDecl;
1039
+ };
1040
+
1041
+ // / \brief The type of the parent, in which this type is nested.
1042
+ Type Parent;
1043
+
1044
+ template <typename ... Args>
1045
+ AnyGenericType (NominalTypeDecl *TheDecl, Type Parent, Args &&...args)
1046
+ : TypeBase(std::forward<Args>(args)...), NomDecl(TheDecl), Parent(Parent) {}
1047
+
1048
+ protected:
1049
+ template <typename ... Args>
1050
+ AnyGenericType (GenericTypeDecl *TheDecl, Type Parent, Args &&...args)
1051
+ : TypeBase(std::forward<Args>(args)...), GenDecl(TheDecl), Parent(Parent) {}
1052
+
1053
+ public:
1054
+
1055
+ // / \brief Returns the declaration that declares this type.
1056
+ GenericTypeDecl *getDecl () const { return GenDecl; }
1057
+
1058
+ // / \brief Returns the type of the parent of this type. This will
1059
+ // / be null for top-level types or local types, and for non-generic types
1060
+ // / will simply be the same as the declared type of the declaration context
1061
+ // / of TheDecl. For types nested within generic types, however, this will
1062
+ // / involve \c BoundGenericType nodes that provide context for the nested
1063
+ // / type, e.g., the type Dictionary<String, Int>.ItemRange would be
1064
+ // / represented as a NominalType with Dictionary<String, Int> as its parent
1065
+ // / type.
1066
+ Type getParent () const { return Parent; }
1067
+
1068
+ // Implement isa/cast/dyncast/etc.
1069
+ static bool classof (const TypeBase *T) {
1070
+ return T->getKind () >= TypeKind::First_AnyGenericType &&
1071
+ T->getKind () <= TypeKind::Last_AnyGenericType;
1072
+ }
1073
+ };
1074
+ BEGIN_CAN_TYPE_WRAPPER (AnyGenericType, Type)
1075
+ PROXY_CAN_TYPE_SIMPLE_GETTER (getParent)
1076
+ END_CAN_TYPE_WRAPPER (AnyGenericType, Type)
1077
+
1078
+ // / NominalOrBoundGenericNominal - This abstract class helps types ensure that
1079
+ // / fields exist at the same offset in memory to improve code generation of the
1080
+ // / compiler itself.
1081
+ class NominalOrBoundGenericNominalType : public AnyGenericType {
1082
+ public:
1083
+ template <typename ... Args>
1084
+ NominalOrBoundGenericNominalType (Args &&...args)
1085
+ : AnyGenericType(std::forward<Args>(args)...) {}
1086
+
1087
+ // / \brief Returns the declaration that declares this type.
1088
+ NominalTypeDecl *getDecl () const { return NomDecl; }
1089
+
1090
+ // Implement isa/cast/dyncast/etc.
1091
+ static bool classof (const TypeBase *T) {
1092
+ return T->getKind () >= TypeKind::First_NominalOrBoundGenericNominalType &&
1093
+ T->getKind () <= TypeKind::Last_NominalOrBoundGenericNominalType;
1094
+ }
1095
+ };
1096
+ DEFINE_EMPTY_CAN_TYPE_WRAPPER (NominalOrBoundGenericNominalType, AnyGenericType)
1097
+
1028
1098
// / ErrorType - This represents a type that was erroneously constructed. This
1029
1099
// / is produced when parsing types and when name binding type aliases, and is
1030
1100
// / installed in declaration that use these erroneous types. All uses of a
@@ -1679,37 +1749,19 @@ END_CAN_TYPE_WRAPPER(TupleType, Type)
1679
1749
1680
1750
// / UnboundGenericType - Represents a generic type where the type arguments have
1681
1751
// / not yet been resolved.
1682
- class UnboundGenericType : public TypeBase, public llvm::FoldingSetNode {
1683
- GenericTypeDecl *TheDecl;
1684
-
1685
- // / \brief The type of the parent, in which this type is nested.
1686
- Type Parent;
1687
-
1752
+ class UnboundGenericType : public AnyGenericType,
1753
+ public llvm::FoldingSetNode {
1688
1754
private:
1689
1755
UnboundGenericType (GenericTypeDecl *TheDecl, Type Parent, const ASTContext &C,
1690
1756
RecursiveTypeProperties properties)
1691
- : TypeBase (TypeKind::UnboundGeneric,
1692
- (!Parent || Parent->isCanonical ())? &C : nullptr ,
1693
- properties | RecursiveTypeProperties::HasUnboundGeneric),
1694
- TheDecl (TheDecl), Parent (Parent) { }
1757
+ : AnyGenericType (TheDecl, Parent, TypeKind::UnboundGeneric,
1758
+ (!Parent || Parent->isCanonical ()) ? &C : nullptr ,
1759
+ properties | RecursiveTypeProperties::HasUnboundGeneric) {}
1695
1760
1696
1761
public:
1697
1762
static UnboundGenericType* get (GenericTypeDecl *TheDecl, Type Parent,
1698
1763
const ASTContext &C);
1699
1764
1700
- // / \brief Returns the declaration that declares this type.
1701
- GenericTypeDecl *getDecl () const { return TheDecl; }
1702
-
1703
- // / \brief Returns the type of the parent of this type. This will
1704
- // / be null for top-level types or local types, and for non-generic types
1705
- // / will simply be the same as the declared type of the declaration context
1706
- // / of TheDecl. For types nested within generic types, however, this will
1707
- // / involve \c BoundGenericType nodes that provide context for the nested
1708
- // / type, e.g., the bound type Dictionary<String, Int>.Inner would be
1709
- // / represented as an UnboundGenericType with Dictionary<String, Int> as its
1710
- // / parent type.
1711
- Type getParent () const { return Parent; }
1712
-
1713
1765
void Profile (llvm::FoldingSetNodeID &ID) {
1714
1766
Profile (ID, getDecl (), getParent ());
1715
1767
}
@@ -1721,20 +1773,15 @@ class UnboundGenericType : public TypeBase, public llvm::FoldingSetNode {
1721
1773
return T->getKind () == TypeKind::UnboundGeneric;
1722
1774
}
1723
1775
};
1724
- BEGIN_CAN_TYPE_WRAPPER (UnboundGenericType, Type)
1725
- PROXY_CAN_TYPE_SIMPLE_GETTER(getParent)
1726
- END_CAN_TYPE_WRAPPER(UnboundGenericType, Type)
1776
+ DEFINE_EMPTY_CAN_TYPE_WRAPPER (UnboundGenericType, AnyGenericType)
1727
1777
1728
1778
inline CanType getAsCanType(const Type &type) { return CanType (type); }
1729
1779
typedef ArrayRefView<Type,CanType,getAsCanType> CanTypeArrayRef;
1730
1780
1731
1781
// / BoundGenericType - An abstract class for applying a generic type to the
1732
1782
// / given type arguments.
1733
- class BoundGenericType : public TypeBase , public llvm ::FoldingSetNode {
1734
- NominalTypeDecl *TheDecl;
1735
-
1736
- // / \brief The type of the parent, in which this type is nested.
1737
- Type Parent;
1783
+ class BoundGenericType : public NominalOrBoundGenericNominalType ,
1784
+ public llvm::FoldingSetNode {
1738
1785
1739
1786
// / Retrieve the intrusive pointer storage from the subtype
1740
1787
const Type *getTrailingObjectsPointer () const ;
@@ -1752,26 +1799,13 @@ class BoundGenericType : public TypeBase, public llvm::FoldingSetNode {
1752
1799
static BoundGenericType* get (NominalTypeDecl *TheDecl, Type Parent,
1753
1800
ArrayRef<Type> GenericArgs);
1754
1801
1755
- // / \brief Returns the declaration that declares this type.
1756
- NominalTypeDecl *getDecl () const { return TheDecl; }
1757
-
1758
- // / \brief Returns the type of the parent of this type. This will
1759
- // / be null for top-level types or local types, and for non-generic types
1760
- // / will simply be the same as the declared type of the declaration context
1761
- // / of TheDecl. For types nested within generic types, however, this will
1762
- // / involve \c BoundGenericType nodes that provide context for the nested
1763
- // / type, e.g., the bound type Dictionary<String, Int>.Inner<Int> would be
1764
- // / represented as a BoundGenericType with Dictionary<String, Int> as its
1765
- // / parent type.
1766
- Type getParent () const { return Parent; }
1767
-
1768
1802
// / Retrieve the set of generic arguments provided at this level.
1769
1803
ArrayRef<Type> getGenericArgs () const {
1770
1804
return {getTrailingObjectsPointer (), Bits.BoundGenericType .GenericArgCount };
1771
1805
}
1772
1806
1773
1807
void Profile (llvm::FoldingSetNodeID &ID) {
1774
- Profile (ID, TheDecl, Parent , getGenericArgs ());
1808
+ Profile (ID, getDecl (), getParent () , getGenericArgs ());
1775
1809
}
1776
1810
static void Profile (llvm::FoldingSetNodeID &ID, NominalTypeDecl *TheDecl,
1777
1811
Type Parent, ArrayRef<Type> GenericArgs);
@@ -1782,12 +1816,11 @@ class BoundGenericType : public TypeBase, public llvm::FoldingSetNode {
1782
1816
T->getKind () <= TypeKind::Last_BoundGenericType;
1783
1817
}
1784
1818
};
1785
- BEGIN_CAN_TYPE_WRAPPER (BoundGenericType, Type)
1786
- PROXY_CAN_TYPE_SIMPLE_GETTER(getParent)
1819
+ BEGIN_CAN_TYPE_WRAPPER (BoundGenericType, NominalOrBoundGenericNominalType)
1787
1820
CanTypeArrayRef getGenericArgs() const {
1788
1821
return CanTypeArrayRef (getPointer ()->getGenericArgs ());
1789
1822
}
1790
- END_CAN_TYPE_WRAPPER (BoundGenericType, Type )
1823
+ END_CAN_TYPE_WRAPPER (BoundGenericType, NominalOrBoundGenericNominalType )
1791
1824
1792
1825
1793
1826
// / BoundGenericClassType - A subclass of BoundGenericType for the case
@@ -1895,46 +1928,24 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(BoundGenericStructType, BoundGenericType)
1895
1928
// / NominalType - Represents a type with a name that is significant, such that
1896
1929
// / the name distinguishes it from other structurally-similar types that have
1897
1930
// / different names. Nominal types are always canonical.
1898
- class NominalType : public TypeBase {
1899
- // / TheDecl - This is the TypeDecl which declares the given type. It
1900
- // / specifies the name and other useful information about this type.
1901
- NominalTypeDecl * const TheDecl;
1902
-
1903
- // / \brief The type of the parent, in which this type is nested.
1904
- Type Parent;
1931
+ class NominalType : public NominalOrBoundGenericNominalType {
1905
1932
1906
1933
protected:
1907
1934
NominalType (TypeKind K, const ASTContext *C, NominalTypeDecl *TheDecl,
1908
1935
Type Parent, RecursiveTypeProperties properties)
1909
- : TypeBase (K, (!Parent || Parent->isCanonical ())? C : nullptr ,
1910
- properties),
1911
- TheDecl (TheDecl), Parent (Parent) { }
1936
+ : NominalOrBoundGenericNominalType (TheDecl, Parent, K,
1937
+ (!Parent || Parent->isCanonical ())? C : nullptr , properties) {}
1912
1938
1913
1939
public:
1914
1940
static NominalType *get (NominalTypeDecl *D, Type Parent, const ASTContext &C);
1915
1941
1916
- // / \brief Returns the declaration that declares this type.
1917
- NominalTypeDecl *getDecl () const { return TheDecl; }
1918
-
1919
- // / \brief Returns the type of the parent of this type. This will
1920
- // / be null for top-level types or local types, and for non-generic types
1921
- // / will simply be the same as the declared type of the declaration context
1922
- // / of TheDecl. For types nested within generic types, however, this will
1923
- // / involve \c BoundGenericType nodes that provide context for the nested
1924
- // / type, e.g., the type Dictionary<String, Int>.ItemRange would be
1925
- // / represented as a NominalType with Dictionary<String, Int> as its parent
1926
- // / type.
1927
- Type getParent () const { return Parent; }
1928
-
1929
1942
// Implement isa/cast/dyncast/etc.
1930
1943
static bool classof (const TypeBase *T) {
1931
1944
return T->getKind () >= TypeKind::First_NominalType &&
1932
1945
T->getKind () <= TypeKind::Last_NominalType;
1933
1946
}
1934
1947
};
1935
- BEGIN_CAN_TYPE_WRAPPER (NominalType, Type)
1936
- PROXY_CAN_TYPE_SIMPLE_GETTER(getParent)
1937
- END_CAN_TYPE_WRAPPER(NominalType, Type)
1948
+ DEFINE_EMPTY_CAN_TYPE_WRAPPER (NominalType, NominalOrBoundGenericNominalType)
1938
1949
1939
1950
// / EnumType - This represents the type declared by an EnumDecl.
1940
1951
class EnumType : public NominalType, public llvm::FoldingSetNode {
@@ -4964,12 +4975,8 @@ inline NominalTypeDecl *TypeBase::getNominalOrBoundGenericNominal() {
4964
4975
}
4965
4976
4966
4977
inline NominalTypeDecl *CanType::getNominalOrBoundGenericNominal () const {
4967
- if (auto nomTy = dyn_cast<NominalType>(*this ))
4968
- return nomTy->getDecl ();
4969
-
4970
- if (auto boundTy = dyn_cast<BoundGenericType>(*this ))
4971
- return boundTy->getDecl ();
4972
-
4978
+ if (auto Ty = dyn_cast<NominalOrBoundGenericNominalType>(*this ))
4979
+ return Ty->getDecl ();
4973
4980
return nullptr ;
4974
4981
}
4975
4982
@@ -4978,13 +4985,7 @@ inline NominalTypeDecl *TypeBase::getAnyNominal() {
4978
4985
}
4979
4986
4980
4987
inline Type TypeBase::getNominalParent () {
4981
- if (auto classType = getAs<NominalType>()) {
4982
- return classType->getParent ();
4983
- } else if (auto unboundType = getAs<UnboundGenericType>()) {
4984
- return unboundType->getParent ();
4985
- } else {
4986
- return castTo<BoundGenericType>()->getParent ();
4987
- }
4988
+ return castTo<AnyGenericType>()->getParent ();
4988
4989
}
4989
4990
4990
4991
inline GenericTypeDecl *TypeBase::getAnyGeneric () {
@@ -5053,11 +5054,7 @@ inline CanType CanType::getWithoutSpecifierTypeImpl(CanType type) {
5053
5054
}
5054
5055
5055
5056
inline CanType CanType::getNominalParent () const {
5056
- if (auto classType = dyn_cast<NominalType>(*this )) {
5057
- return classType.getParent ();
5058
- } else {
5059
- return cast<BoundGenericType>(*this ).getParent ();
5060
- }
5057
+ return cast<NominalOrBoundGenericNominalType>(*this ).getParent ();
5061
5058
}
5062
5059
5063
5060
inline Type TupleTypeElt::getVarargBaseTy () const {
0 commit comments