16
16
17
17
#ifndef SWIFT_ATTR_H
18
18
#define SWIFT_ATTR_H
19
-
20
19
#include " swift/AST/ASTAllocated.h"
21
20
#include " swift/AST/AttrKind.h"
22
21
#include " swift/AST/AutoDiff.h"
@@ -203,7 +202,7 @@ class DeclAttribute : public AttributeBase {
203
202
ownership : NumReferenceOwnershipBits
204
203
);
205
204
206
- SWIFT_INLINE_BITFIELD (SpecializeAttr , DeclAttribute, 1 +1 ,
205
+ SWIFT_INLINE_BITFIELD (AbstractSpecializeAttr , DeclAttribute, 1 +1 ,
207
206
exported : 1 ,
208
207
kind : 1
209
208
);
@@ -1730,15 +1729,17 @@ class SynthesizedProtocolAttr : public DeclAttribute {
1730
1729
}
1731
1730
};
1732
1731
1733
- // / The @_specialize attribute, which forces specialization on the specified
1734
- // / type list.
1735
- class SpecializeAttr final
1732
+ // / The @_specialize/@specialize attribute, which forces specialization on the
1733
+ // / specified type list.
1734
+ template <typename Base, typename ...AdditionalTrailingObjects>
1735
+ using SpecializeAttrTrailingObjects = llvm::TrailingObjects<Base,
1736
+ Identifier, AvailableAttr *, Type , AdditionalTrailingObjects...>;
1737
+
1738
+ class AbstractSpecializeAttr
1736
1739
: public DeclAttribute,
1737
- private llvm::TrailingObjects<SpecializeAttr, Identifier,
1738
- AvailableAttr *, Type> {
1740
+ private llvm::trailing_objects_internal::TrailingObjectsBase {
1739
1741
friend class SpecializeAttrTargetDeclRequest ;
1740
1742
friend class SerializeAttrGenericSignatureRequest ;
1741
- friend TrailingObjects;
1742
1743
1743
1744
public:
1744
1745
// NOTE: When adding new kinds, you must update the inline bitfield macro.
@@ -1759,82 +1760,81 @@ class SpecializeAttr final
1759
1760
size_t numTypeErasedParams;
1760
1761
bool typeErasedParamsInitialized;
1761
1762
1762
- SpecializeAttr (SourceLoc atLoc, SourceRange Range,
1763
- TrailingWhereClause *clause, bool exported,
1763
+ protected:
1764
+ AbstractSpecializeAttr (DeclAttrKind DK, SourceLoc atLoc, SourceRange Range,
1765
+ TrailingWhereClause *clause,
1766
+ bool exported,
1764
1767
SpecializationKind kind, GenericSignature specializedSignature,
1765
1768
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1766
1769
ArrayRef<AvailableAttr *> availabilityAttrs,
1767
1770
size_t typeErasedParamsCount);
1768
1771
1769
1772
public:
1770
- static SpecializeAttr *
1771
- create (ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
1772
- TrailingWhereClause *clause, bool exported, SpecializationKind kind,
1773
- DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1774
- ArrayRef<AvailableAttr *> availabilityAttrs,
1775
- GenericSignature specializedSignature = nullptr );
1776
-
1777
- static SpecializeAttr *create (ASTContext &ctx, bool exported,
1778
- SpecializationKind kind,
1779
- ArrayRef<Identifier> spiGroups,
1780
- ArrayRef<AvailableAttr *> availabilityAttrs,
1781
- GenericSignature specializedSignature,
1782
- DeclNameRef replacedFunction);
1783
-
1784
- static SpecializeAttr *create (ASTContext &ctx, bool exported,
1785
- SpecializationKind kind,
1786
- ArrayRef<Identifier> spiGroups,
1787
- ArrayRef<AvailableAttr *> availabilityAttrs,
1788
- ArrayRef<Type> typeErasedParams,
1789
- GenericSignature specializedSignature,
1790
- DeclNameRef replacedFunction,
1791
- LazyMemberLoader *resolver, uint64_t data);
1792
-
1793
1773
size_t numTrailingObjects (OverloadToken<Identifier>) const {
1794
1774
return numSPIGroups;
1795
1775
}
1796
1776
1797
1777
size_t numTrailingObjects (OverloadToken<AvailableAttr *>) const {
1798
1778
return numAvailableAttrs;
1799
1779
}
1780
+ // Helper to get the trailing objects of one of the subclasses.
1781
+ template <typename Type>
1782
+ const Type *getSubclassTrailingObjects () const ;
1783
+
1784
+ template <typename Type>
1785
+ Type *getSubclassTrailingObjects () {
1786
+ const auto *constThis = this ;
1787
+ return const_cast <Type*>(constThis->getSubclassTrailingObjects <Type>());
1788
+ }
1789
+
1800
1790
// / Name of SPIs declared by the attribute.
1801
1791
// /
1802
1792
// / Note: A single SPI name per attribute is currently supported but this
1803
1793
// / may change with the syntax change.
1804
1794
ArrayRef<Identifier> getSPIGroups () const {
1805
- return { this -> template getTrailingObjects <Identifier>(),
1795
+ return { getSubclassTrailingObjects <Identifier>(),
1806
1796
numSPIGroups };
1807
1797
}
1808
1798
1809
1799
ArrayRef<AvailableAttr *> getAvailableAttrs () const {
1810
- return {this -> template getTrailingObjects <AvailableAttr *>(),
1800
+ return {getSubclassTrailingObjects <AvailableAttr *>(),
1811
1801
numAvailableAttrs};
1812
1802
}
1813
1803
1814
1804
ArrayRef<Type> getTypeErasedParams () const {
1815
1805
if (!typeErasedParamsInitialized)
1816
1806
return {};
1817
1807
1818
- return {this -> template getTrailingObjects <Type>(),
1808
+ return {getSubclassTrailingObjects <Type>(),
1819
1809
numTypeErasedParams};
1820
1810
}
1821
1811
1822
1812
void setTypeErasedParams (const ArrayRef<Type> typeErasedParams) {
1823
1813
assert (typeErasedParams.size () == numTypeErasedParams);
1824
1814
if (!typeErasedParamsInitialized) {
1825
- std::uninitialized_copy (typeErasedParams.begin (), typeErasedParams.end (), getTrailingObjects<Type>());
1815
+ std::uninitialized_copy (typeErasedParams.begin (), typeErasedParams.end (),
1816
+ getSubclassTrailingObjects<Type>());
1826
1817
typeErasedParamsInitialized = true ;
1827
1818
}
1828
1819
}
1829
1820
1821
+ void setResolver (LazyMemberLoader *resolver, uint64_t resolverContextData) {
1822
+ this ->resolver = resolver;
1823
+ this ->resolverContextData = resolverContextData;
1824
+ }
1825
+
1830
1826
TrailingWhereClause *getTrailingWhereClause () const ;
1831
1827
1828
+ bool isPublic () const {
1829
+ return getKind () == DeclAttrKind::Specialized;
1830
+ }
1831
+
1832
1832
bool isExported () const {
1833
- return Bits.SpecializeAttr .exported ;
1833
+ return Bits.AbstractSpecializeAttr .exported ;
1834
1834
}
1835
1835
1836
1836
SpecializationKind getSpecializationKind () const {
1837
- return SpecializationKind (Bits.SpecializeAttr .kind );
1837
+ return SpecializationKind (Bits.AbstractSpecializeAttr .kind );
1838
1838
}
1839
1839
1840
1840
bool isFullSpecialization () const {
@@ -1856,15 +1856,140 @@ class SpecializeAttr final
1856
1856
GenericSignature
1857
1857
getSpecializedSignature (const AbstractFunctionDecl *forDecl) const ;
1858
1858
1859
+ static bool classof (const DeclAttribute *DA) {
1860
+ return DA->getKind () == DeclAttrKind::Specialize ||
1861
+ DA->getKind () == DeclAttrKind::Specialized;
1862
+ }
1863
+
1864
+ UNIMPLEMENTED_CLONE (AbstractSpecializeAttr)
1865
+
1866
+ bool isEquivalent (const AbstractSpecializeAttr *other, Decl *attachedTo) const ;
1867
+ };
1868
+
1869
+ // / The @_specialize attribute.
1870
+ class SpecializeAttr final : public AbstractSpecializeAttr,
1871
+ private SpecializeAttrTrailingObjects<SpecializeAttr> {
1872
+ friend TrailingObjects;
1873
+ friend AbstractSpecializeAttr;
1874
+
1875
+ // WARNING: Do not add storage here. The base class uses TrailingObjects.
1876
+ private:
1877
+ SpecializeAttr (SourceLoc atLoc, SourceRange Range,
1878
+ TrailingWhereClause *clause,
1879
+ bool exported,
1880
+ SpecializationKind kind, GenericSignature specializedSignature,
1881
+ DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1882
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1883
+ size_t typeErasedParamsCount) :
1884
+ AbstractSpecializeAttr (DeclAttrKind::Specialize, atLoc, Range, clause,
1885
+ exported, kind, specializedSignature, targetFunctionName,
1886
+ spiGroups, availabilityAttrs, typeErasedParamsCount) {}
1887
+
1888
+ public:
1889
+ static SpecializeAttr *
1890
+ create (ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
1891
+ TrailingWhereClause *clause, bool exported,
1892
+ SpecializationKind kind,
1893
+ DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1894
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1895
+ GenericSignature specializedSignature = nullptr );
1896
+
1897
+ static SpecializeAttr *create (ASTContext &ctx, bool exported,
1898
+ SpecializationKind kind,
1899
+ ArrayRef<Identifier> spiGroups,
1900
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1901
+ GenericSignature specializedSignature,
1902
+ DeclNameRef replacedFunction);
1903
+
1904
+ static SpecializeAttr *create (ASTContext &ctx, bool exported,
1905
+ SpecializationKind kind,
1906
+ ArrayRef<Identifier> spiGroups,
1907
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1908
+ ArrayRef<Type> typeErasedParams,
1909
+ GenericSignature specializedSignature,
1910
+ DeclNameRef replacedFunction,
1911
+ LazyMemberLoader *resolver, uint64_t data);
1912
+
1859
1913
static bool classof (const DeclAttribute *DA) {
1860
1914
return DA->getKind () == DeclAttrKind::Specialize;
1861
1915
}
1862
1916
1863
1917
UNIMPLEMENTED_CLONE (SpecializeAttr)
1864
1918
1865
- bool isEquivalent (const SpecializeAttr *other, Decl *attachedTo) const ;
1919
+ bool isEquivalent (const SpecializeAttr *other, Decl *attachedTo) const {
1920
+ return AbstractSpecializeAttr::isEquivalent (other, attachedTo);
1921
+ }
1866
1922
};
1867
1923
1924
+ // / The @specialized attribute.
1925
+ class SpecializedAttr final : public AbstractSpecializeAttr ,
1926
+ private SpecializeAttrTrailingObjects<SpecializeAttr> {
1927
+ friend TrailingObjects;
1928
+ friend AbstractSpecializeAttr;
1929
+
1930
+ // WARNING: Do not add storage here. The base class uses TrailingObjects.
1931
+ private:
1932
+
1933
+ SpecializedAttr (SourceLoc atLoc, SourceRange Range,
1934
+ TrailingWhereClause *clause,
1935
+ bool exported,
1936
+ SpecializationKind kind, GenericSignature specializedSignature,
1937
+ DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1938
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1939
+ size_t typeErasedParamsCount) :
1940
+ AbstractSpecializeAttr (DeclAttrKind::Specialized, atLoc, Range, clause,
1941
+ exported, kind, specializedSignature, targetFunctionName,
1942
+ spiGroups, availabilityAttrs, typeErasedParamsCount) {}
1943
+
1944
+ public:
1945
+ static SpecializedAttr *
1946
+ create (ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
1947
+ TrailingWhereClause *clause, bool exported,
1948
+ SpecializationKind kind,
1949
+ DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
1950
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1951
+ GenericSignature specializedSignature = nullptr );
1952
+
1953
+ static SpecializedAttr *create (ASTContext &ctx, bool exported,
1954
+ SpecializationKind kind,
1955
+ ArrayRef<Identifier> spiGroups,
1956
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1957
+ GenericSignature specializedSignature,
1958
+ DeclNameRef replacedFunction);
1959
+
1960
+ static SpecializedAttr *create (ASTContext &ctx, bool exported,
1961
+ SpecializationKind kind,
1962
+ ArrayRef<Identifier> spiGroups,
1963
+ ArrayRef<AvailableAttr *> availabilityAttrs,
1964
+ ArrayRef<Type> typeErasedParams,
1965
+ GenericSignature specializedSignature,
1966
+ DeclNameRef replacedFunction,
1967
+ LazyMemberLoader *resolver, uint64_t data);
1968
+
1969
+ static bool classof (const DeclAttribute *DA) {
1970
+ return DA->getKind () == DeclAttrKind::Specialized;
1971
+ }
1972
+
1973
+ UNIMPLEMENTED_CLONE (SpecializedAttr)
1974
+
1975
+ bool isEquivalent (const SpecializedAttr *other, Decl *attachedTo) const {
1976
+ return AbstractSpecializeAttr::isEquivalent (other, attachedTo);
1977
+ }
1978
+ };
1979
+
1980
+ template <typename Type>
1981
+ const Type *AbstractSpecializeAttr::getSubclassTrailingObjects () const {
1982
+ if (auto attr = dyn_cast<SpecializedAttr>(this )) {
1983
+ return attr->getTrailingObjects <Type>();
1984
+ }
1985
+ if (auto attr = dyn_cast<SpecializeAttr>(this )) {
1986
+ return attr->getTrailingObjects <Type>();
1987
+ }
1988
+ llvm_unreachable (" unhandled AbstractSpecializeAttr subclass?" );
1989
+ }
1990
+
1991
+
1992
+
1868
1993
class StorageRestrictionsAttr final
1869
1994
: public DeclAttribute,
1870
1995
private llvm::TrailingObjects<StorageRestrictionsAttr, Identifier> {
0 commit comments