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