@@ -70,6 +70,7 @@ namespace swift {
7070 class BraceStmt ;
7171 class DeclAttributes ;
7272 class GenericContext ;
73+ class GenericParamList ;
7374 class GenericSignature ;
7475 class GenericTypeParamDecl ;
7576 class GenericTypeParamType ;
@@ -88,6 +89,7 @@ namespace swift {
8889 class ProtocolType ;
8990 struct RawComment ;
9091 enum class ResilienceExpansion : unsigned ;
92+ class TrailingWhereClause ;
9193 class TypeAliasDecl ;
9294 class Stmt ;
9395 class SubscriptDecl ;
@@ -1011,364 +1013,6 @@ void *allocateMemoryForDecl(AllocatorTy &allocator, size_t baseSize,
10111013 return mem;
10121014}
10131015
1014- enum class RequirementReprKind : unsigned {
1015- // / A type bound T : P, where T is a type that depends on a generic
1016- // / parameter and P is some type that should bound T, either as a concrete
1017- // / supertype or a protocol to which T must conform.
1018- TypeConstraint,
1019-
1020- // / A same-type requirement T == U, where T and U are types that shall be
1021- // / equivalent.
1022- SameType,
1023-
1024- // / A layout bound T : L, where T is a type that depends on a generic
1025- // / parameter and L is some layout specification that should bound T.
1026- LayoutConstraint,
1027-
1028- // Note: there is code that packs this enum in a 2-bit bitfield. Audit users
1029- // when adding enumerators.
1030- };
1031-
1032- // / A single requirement in a 'where' clause, which places additional
1033- // / restrictions on the generic parameters or associated types of a generic
1034- // / function, type, or protocol.
1035- // /
1036- // / This always represents a requirement spelled in the source code. It is
1037- // / never generated implicitly.
1038- // /
1039- // / \c GenericParamList assumes these are POD-like.
1040- class RequirementRepr {
1041- SourceLoc SeparatorLoc;
1042- RequirementReprKind Kind : 2 ;
1043- bool Invalid : 1 ;
1044- TypeRepr *FirstType;
1045-
1046- // / The second element represents the right-hand side of the constraint.
1047- // / It can be e.g. a type or a layout constraint.
1048- union {
1049- TypeRepr *SecondType;
1050- LayoutConstraintLoc SecondLayout;
1051- };
1052-
1053- // / Set during deserialization; used to print out the requirements accurately
1054- // / for the generated interface.
1055- StringRef AsWrittenString;
1056-
1057- RequirementRepr (SourceLoc SeparatorLoc, RequirementReprKind Kind,
1058- TypeRepr *FirstType, TypeRepr *SecondType)
1059- : SeparatorLoc(SeparatorLoc), Kind(Kind), Invalid(false ),
1060- FirstType (FirstType), SecondType(SecondType) { }
1061-
1062- RequirementRepr (SourceLoc SeparatorLoc, RequirementReprKind Kind,
1063- TypeRepr *FirstType, LayoutConstraintLoc SecondLayout)
1064- : SeparatorLoc(SeparatorLoc), Kind(Kind), Invalid(false ),
1065- FirstType(FirstType), SecondLayout(SecondLayout) { }
1066-
1067- void printImpl (ASTPrinter &OS) const ;
1068-
1069- public:
1070- // / Construct a new type-constraint requirement.
1071- // /
1072- // / \param Subject The type that must conform to the given protocol or
1073- // / composition, or be a subclass of the given class type.
1074- // / \param ColonLoc The location of the ':', or an invalid location if
1075- // / this requirement was implied.
1076- // / \param Constraint The protocol or protocol composition to which the
1077- // / subject must conform, or superclass from which the subject must inherit.
1078- static RequirementRepr getTypeConstraint (TypeRepr *Subject,
1079- SourceLoc ColonLoc,
1080- TypeRepr *Constraint) {
1081- return { ColonLoc, RequirementReprKind::TypeConstraint, Subject, Constraint };
1082- }
1083-
1084- // / Construct a new same-type requirement.
1085- // /
1086- // / \param FirstType The first type.
1087- // / \param EqualLoc The location of the '==' in the same-type constraint, or
1088- // / an invalid location if this requirement was implied.
1089- // / \param SecondType The second type.
1090- static RequirementRepr getSameType (TypeRepr *FirstType,
1091- SourceLoc EqualLoc,
1092- TypeRepr *SecondType) {
1093- return { EqualLoc, RequirementReprKind::SameType, FirstType, SecondType };
1094- }
1095-
1096- // / Construct a new layout-constraint requirement.
1097- // /
1098- // / \param Subject The type that must conform to the given layout
1099- // / requirement.
1100- // / \param ColonLoc The location of the ':', or an invalid location if
1101- // / this requirement was implied.
1102- // / \param Layout The layout requirement to which the
1103- // / subject must conform.
1104- static RequirementRepr getLayoutConstraint (TypeRepr *Subject,
1105- SourceLoc ColonLoc,
1106- LayoutConstraintLoc Layout) {
1107- return {ColonLoc, RequirementReprKind::LayoutConstraint, Subject,
1108- Layout};
1109- }
1110-
1111- // / Determine the kind of requirement
1112- RequirementReprKind getKind () const { return Kind; }
1113-
1114- // / Determine whether this requirement is invalid.
1115- bool isInvalid () const { return Invalid; }
1116-
1117- // / Mark this requirement invalid.
1118- void setInvalid () { Invalid = true ; }
1119-
1120- // / For a type-bound requirement, return the subject of the
1121- // / conformance relationship.
1122- TypeRepr *getSubjectRepr () const {
1123- assert (getKind () == RequirementReprKind::TypeConstraint ||
1124- getKind () == RequirementReprKind::LayoutConstraint);
1125- return FirstType;
1126- }
1127-
1128- // / For a type-bound requirement, return the protocol or to which
1129- // / the subject conforms or superclass it inherits.
1130- TypeRepr *getConstraintRepr () const {
1131- assert (getKind () == RequirementReprKind::TypeConstraint);
1132- return SecondType;
1133- }
1134-
1135- LayoutConstraint getLayoutConstraint () const {
1136- assert (getKind () == RequirementReprKind::LayoutConstraint);
1137- return SecondLayout.getLayoutConstraint ();
1138- }
1139-
1140- LayoutConstraintLoc &getLayoutConstraintLoc () {
1141- assert (getKind () == RequirementReprKind::LayoutConstraint);
1142- return SecondLayout;
1143- }
1144-
1145- const LayoutConstraintLoc &getLayoutConstraintLoc () const {
1146- assert (getKind () == RequirementReprKind::LayoutConstraint);
1147- return SecondLayout;
1148- }
1149-
1150- // / Retrieve the first type of a same-type requirement.
1151- TypeRepr *getFirstTypeRepr () const {
1152- assert (getKind () == RequirementReprKind::SameType);
1153- return FirstType;
1154- }
1155-
1156- // / Retrieve the second type of a same-type requirement.
1157- TypeRepr *getSecondTypeRepr () const {
1158- assert (getKind () == RequirementReprKind::SameType);
1159- return SecondType;
1160- }
1161-
1162- // / Retrieve the location of the ':' or '==' in an explicitly-written
1163- // / conformance or same-type requirement respectively.
1164- SourceLoc getSeparatorLoc () const {
1165- return SeparatorLoc;
1166- }
1167-
1168- SourceRange getSourceRange () const ;
1169-
1170- // / Retrieve the first or subject type representation from the \c repr,
1171- // / or \c nullptr if \c repr is null.
1172- static TypeRepr *getFirstTypeRepr (const RequirementRepr *repr) {
1173- if (!repr) return nullptr ;
1174- return repr->FirstType ;
1175- }
1176-
1177- // / Retrieve the second or constraint type representation from the \c repr,
1178- // / or \c nullptr if \c repr is null.
1179- static TypeRepr *getSecondTypeRepr (const RequirementRepr *repr) {
1180- if (!repr) return nullptr ;
1181- assert (repr->getKind () == RequirementReprKind::TypeConstraint ||
1182- repr->getKind () == RequirementReprKind::SameType);
1183- return repr->SecondType ;
1184- }
1185-
1186- SWIFT_DEBUG_DUMP;
1187- void print (raw_ostream &OS) const ;
1188- void print (ASTPrinter &Printer) const ;
1189- };
1190-
1191- using GenericParamSource = PointerUnion<GenericContext *, GenericParamList *>;
1192-
1193- // / GenericParamList - A list of generic parameters that is part of a generic
1194- // / function or type, along with extra requirements placed on those generic
1195- // / parameters and types derived from them.
1196- class GenericParamList final :
1197- private llvm::TrailingObjects<GenericParamList, GenericTypeParamDecl *> {
1198- friend TrailingObjects;
1199-
1200- SourceRange Brackets;
1201- unsigned NumParams;
1202- SourceLoc WhereLoc;
1203- MutableArrayRef<RequirementRepr> Requirements;
1204-
1205- GenericParamList *OuterParameters;
1206-
1207- GenericParamList (SourceLoc LAngleLoc,
1208- ArrayRef<GenericTypeParamDecl *> Params,
1209- SourceLoc WhereLoc,
1210- MutableArrayRef<RequirementRepr> Requirements,
1211- SourceLoc RAngleLoc);
1212-
1213- // Don't copy.
1214- GenericParamList (const GenericParamList &) = delete ;
1215- GenericParamList &operator =(const GenericParamList &) = delete ;
1216-
1217- public:
1218- // / create - Create a new generic parameter list within the given AST context.
1219- // /
1220- // / \param Context The ASTContext in which the generic parameter list will
1221- // / be allocated.
1222- // / \param LAngleLoc The location of the opening angle bracket ('<')
1223- // / \param Params The list of generic parameters, which will be copied into
1224- // / ASTContext-allocated memory.
1225- // / \param RAngleLoc The location of the closing angle bracket ('>')
1226- static GenericParamList *create (ASTContext &Context,
1227- SourceLoc LAngleLoc,
1228- ArrayRef<GenericTypeParamDecl *> Params,
1229- SourceLoc RAngleLoc);
1230-
1231- // / create - Create a new generic parameter list and "where" clause within
1232- // / the given AST context.
1233- // /
1234- // / \param Context The ASTContext in which the generic parameter list will
1235- // / be allocated.
1236- // / \param LAngleLoc The location of the opening angle bracket ('<')
1237- // / \param Params The list of generic parameters, which will be copied into
1238- // / ASTContext-allocated memory.
1239- // / \param WhereLoc The location of the 'where' keyword, if any.
1240- // / \param Requirements The list of requirements, which will be copied into
1241- // / ASTContext-allocated memory.
1242- // / \param RAngleLoc The location of the closing angle bracket ('>')
1243- static GenericParamList *create (const ASTContext &Context,
1244- SourceLoc LAngleLoc,
1245- ArrayRef<GenericTypeParamDecl *> Params,
1246- SourceLoc WhereLoc,
1247- ArrayRef<RequirementRepr> Requirements,
1248- SourceLoc RAngleLoc);
1249-
1250- MutableArrayRef<GenericTypeParamDecl *> getParams () {
1251- return {getTrailingObjects<GenericTypeParamDecl *>(), NumParams};
1252- }
1253-
1254- ArrayRef<GenericTypeParamDecl *> getParams () const {
1255- return {getTrailingObjects<GenericTypeParamDecl *>(), NumParams};
1256- }
1257-
1258- using iterator = GenericTypeParamDecl **;
1259- using const_iterator = const GenericTypeParamDecl * const *;
1260-
1261- unsigned size () const { return NumParams; }
1262- iterator begin () { return getParams ().begin (); }
1263- iterator end () { return getParams ().end (); }
1264- const_iterator begin () const { return getParams ().begin (); }
1265- const_iterator end () const { return getParams ().end (); }
1266-
1267- // / Retrieve the location of the 'where' keyword, or an invalid
1268- // / location if 'where' was not present.
1269- SourceLoc getWhereLoc () const { return WhereLoc; }
1270-
1271- // / Retrieve the set of additional requirements placed on these
1272- // / generic parameters and types derived from them.
1273- // /
1274- // / This list may contain both explicitly-written requirements as well as
1275- // / implicitly-generated requirements, and may be non-empty even if no
1276- // / 'where' keyword is present.
1277- MutableArrayRef<RequirementRepr> getRequirements () { return Requirements; }
1278-
1279- // / Retrieve the set of additional requirements placed on these
1280- // / generic parameters and types derived from them.
1281- // /
1282- // / This list may contain both explicitly-written requirements as well as
1283- // / implicitly-generated requirements, and may be non-empty even if no
1284- // / 'where' keyword is present.
1285- ArrayRef<RequirementRepr> getRequirements () const { return Requirements; }
1286-
1287- // / Retrieve the outer generic parameter list.
1288- // /
1289- // / This is used for extensions of nested types, and in SIL mode, where a
1290- // / single lexical context can have multiple logical generic parameter
1291- // / lists.
1292- GenericParamList *getOuterParameters () const { return OuterParameters; }
1293-
1294- // / Set the outer generic parameter list. See \c getOuterParameters
1295- // / for more information.
1296- void setOuterParameters (GenericParamList *Outer) { OuterParameters = Outer; }
1297-
1298- void setDeclContext (DeclContext *dc);
1299-
1300- SourceLoc getLAngleLoc () const { return Brackets.Start ; }
1301- SourceLoc getRAngleLoc () const { return Brackets.End ; }
1302-
1303- SourceRange getSourceRange () const { return Brackets; }
1304-
1305- // / Retrieve the source range covering the where clause.
1306- SourceRange getWhereClauseSourceRange () const {
1307- if (WhereLoc.isInvalid ())
1308- return SourceRange ();
1309-
1310- auto endLoc = Requirements.back ().getSourceRange ().End ;
1311- return SourceRange (WhereLoc, endLoc);
1312- }
1313-
1314- // / Configure the depth of the generic parameters in this list.
1315- void setDepth (unsigned depth);
1316-
1317- // / Create a copy of the generic parameter list and all of its generic
1318- // / parameter declarations. The copied generic parameters are re-parented
1319- // / to the given DeclContext.
1320- GenericParamList *clone (DeclContext *dc) const ;
1321-
1322- void print (raw_ostream &OS) const ;
1323- SWIFT_DEBUG_DUMP;
1324-
1325- bool walk (ASTWalker &walker);
1326-
1327- // / Finds a generic parameter declaration by name. This should only
1328- // / be used from the SIL parser.
1329- GenericTypeParamDecl *lookUpGenericParam (Identifier name) const ;
1330- };
1331-
1332- // / A trailing where clause.
1333- class alignas (RequirementRepr) TrailingWhereClause final :
1334- private llvm::TrailingObjects<TrailingWhereClause, RequirementRepr> {
1335- friend TrailingObjects;
1336-
1337- SourceLoc WhereLoc;
1338-
1339- // / The number of requirements. The actual requirements are tail-allocated.
1340- unsigned NumRequirements;
1341-
1342- TrailingWhereClause (SourceLoc whereLoc,
1343- ArrayRef<RequirementRepr> requirements);
1344-
1345- public:
1346- // / Create a new trailing where clause with the given set of requirements.
1347- static TrailingWhereClause *create (ASTContext &ctx, SourceLoc whereLoc,
1348- ArrayRef<RequirementRepr> requirements);
1349-
1350- // / Retrieve the location of the 'where' keyword.
1351- SourceLoc getWhereLoc () const { return WhereLoc; }
1352-
1353- // / Retrieve the set of requirements.
1354- MutableArrayRef<RequirementRepr> getRequirements () {
1355- return {getTrailingObjects<RequirementRepr>(), NumRequirements};
1356- }
1357-
1358- // / Retrieve the set of requirements.
1359- ArrayRef<RequirementRepr> getRequirements () const {
1360- return {getTrailingObjects<RequirementRepr>(), NumRequirements};
1361- }
1362-
1363- // / Compute the source range containing this trailing where clause.
1364- SourceRange getSourceRange () const {
1365- return SourceRange (WhereLoc,
1366- getRequirements ().back ().getSourceRange ().End );
1367- }
1368-
1369- void print (llvm::raw_ostream &OS, bool printWhereKeyword) const ;
1370- };
1371-
13721016// A private class for forcing exact field layout.
13731017class alignas (8 ) _GenericContext {
13741018// Not really public. See GenericContext.
0 commit comments