Skip to content

Commit fc33bae

Browse files
authored
Merge pull request #32331 from CodaFi/no-where-clause-for-alarm
[NFC] Make RequirementRepr a Purely Syntactic Object
2 parents 080bea5 + b9427b0 commit fc33bae

14 files changed

+177
-308
lines changed

include/swift/AST/Decl.h

Lines changed: 13 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,12 +1030,12 @@ class RequirementRepr {
10301030
SourceLoc SeparatorLoc;
10311031
RequirementReprKind Kind : 2;
10321032
bool Invalid : 1;
1033-
TypeLoc FirstType;
1033+
TypeRepr *FirstType;
10341034

10351035
/// The second element represents the right-hand side of the constraint.
10361036
/// It can be e.g. a type or a layout constraint.
10371037
union {
1038-
TypeLoc SecondType;
1038+
TypeRepr *SecondType;
10391039
LayoutConstraintLoc SecondLayout;
10401040
};
10411041

@@ -1044,16 +1044,16 @@ class RequirementRepr {
10441044
StringRef AsWrittenString;
10451045

10461046
RequirementRepr(SourceLoc SeparatorLoc, RequirementReprKind Kind,
1047-
TypeLoc FirstType, TypeLoc SecondType)
1047+
TypeRepr *FirstType, TypeRepr *SecondType)
10481048
: SeparatorLoc(SeparatorLoc), Kind(Kind), Invalid(false),
10491049
FirstType(FirstType), SecondType(SecondType) { }
10501050

10511051
RequirementRepr(SourceLoc SeparatorLoc, RequirementReprKind Kind,
1052-
TypeLoc FirstType, LayoutConstraintLoc SecondLayout)
1052+
TypeRepr *FirstType, LayoutConstraintLoc SecondLayout)
10531053
: SeparatorLoc(SeparatorLoc), Kind(Kind), Invalid(false),
10541054
FirstType(FirstType), SecondLayout(SecondLayout) { }
10551055

1056-
void printImpl(ASTPrinter &OS, bool AsWritten) const;
1056+
void printImpl(ASTPrinter &OS) const;
10571057

10581058
public:
10591059
/// Construct a new type-constraint requirement.
@@ -1064,9 +1064,9 @@ class RequirementRepr {
10641064
/// this requirement was implied.
10651065
/// \param Constraint The protocol or protocol composition to which the
10661066
/// subject must conform, or superclass from which the subject must inherit.
1067-
static RequirementRepr getTypeConstraint(TypeLoc Subject,
1067+
static RequirementRepr getTypeConstraint(TypeRepr *Subject,
10681068
SourceLoc ColonLoc,
1069-
TypeLoc Constraint) {
1069+
TypeRepr *Constraint) {
10701070
return { ColonLoc, RequirementReprKind::TypeConstraint, Subject, Constraint };
10711071
}
10721072

@@ -1076,9 +1076,9 @@ class RequirementRepr {
10761076
/// \param EqualLoc The location of the '==' in the same-type constraint, or
10771077
/// an invalid location if this requirement was implied.
10781078
/// \param SecondType The second type.
1079-
static RequirementRepr getSameType(TypeLoc FirstType,
1079+
static RequirementRepr getSameType(TypeRepr *FirstType,
10801080
SourceLoc EqualLoc,
1081-
TypeLoc SecondType) {
1081+
TypeRepr *SecondType) {
10821082
return { EqualLoc, RequirementReprKind::SameType, FirstType, SecondType };
10831083
}
10841084

@@ -1090,7 +1090,7 @@ class RequirementRepr {
10901090
/// this requirement was implied.
10911091
/// \param Layout The layout requirement to which the
10921092
/// subject must conform.
1093-
static RequirementRepr getLayoutConstraint(TypeLoc Subject,
1093+
static RequirementRepr getLayoutConstraint(TypeRepr *Subject,
10941094
SourceLoc ColonLoc,
10951095
LayoutConstraintLoc Layout) {
10961096
return {ColonLoc, RequirementReprKind::LayoutConstraint, Subject,
@@ -1108,48 +1108,15 @@ class RequirementRepr {
11081108

11091109
/// For a type-bound requirement, return the subject of the
11101110
/// conformance relationship.
1111-
Type getSubject() const {
1112-
assert(getKind() == RequirementReprKind::TypeConstraint ||
1113-
getKind() == RequirementReprKind::LayoutConstraint);
1114-
return FirstType.getType();
1115-
}
1116-
11171111
TypeRepr *getSubjectRepr() const {
1118-
assert(getKind() == RequirementReprKind::TypeConstraint ||
1119-
getKind() == RequirementReprKind::LayoutConstraint);
1120-
return FirstType.getTypeRepr();
1121-
}
1122-
1123-
TypeLoc &getSubjectLoc() {
1124-
assert(getKind() == RequirementReprKind::TypeConstraint ||
1125-
getKind() == RequirementReprKind::LayoutConstraint);
1126-
return FirstType;
1127-
}
1128-
1129-
const TypeLoc &getSubjectLoc() const {
11301112
assert(getKind() == RequirementReprKind::TypeConstraint ||
11311113
getKind() == RequirementReprKind::LayoutConstraint);
11321114
return FirstType;
11331115
}
11341116

11351117
/// For a type-bound requirement, return the protocol or to which
11361118
/// the subject conforms or superclass it inherits.
1137-
Type getConstraint() const {
1138-
assert(getKind() == RequirementReprKind::TypeConstraint);
1139-
return SecondType.getType();
1140-
}
1141-
11421119
TypeRepr *getConstraintRepr() const {
1143-
assert(getKind() == RequirementReprKind::TypeConstraint);
1144-
return SecondType.getTypeRepr();
1145-
}
1146-
1147-
TypeLoc &getConstraintLoc() {
1148-
assert(getKind() == RequirementReprKind::TypeConstraint);
1149-
return SecondType;
1150-
}
1151-
1152-
const TypeLoc &getConstraintLoc() const {
11531120
assert(getKind() == RequirementReprKind::TypeConstraint);
11541121
return SecondType;
11551122
}
@@ -1170,43 +1137,13 @@ class RequirementRepr {
11701137
}
11711138

11721139
/// Retrieve the first type of a same-type requirement.
1173-
Type getFirstType() const {
1174-
assert(getKind() == RequirementReprKind::SameType);
1175-
return FirstType.getType();
1176-
}
1177-
11781140
TypeRepr *getFirstTypeRepr() const {
1179-
assert(getKind() == RequirementReprKind::SameType);
1180-
return FirstType.getTypeRepr();
1181-
}
1182-
1183-
TypeLoc &getFirstTypeLoc() {
1184-
assert(getKind() == RequirementReprKind::SameType);
1185-
return FirstType;
1186-
}
1187-
1188-
const TypeLoc &getFirstTypeLoc() const {
11891141
assert(getKind() == RequirementReprKind::SameType);
11901142
return FirstType;
11911143
}
11921144

11931145
/// Retrieve the second type of a same-type requirement.
1194-
Type getSecondType() const {
1195-
assert(getKind() == RequirementReprKind::SameType);
1196-
return SecondType.getType();
1197-
}
1198-
11991146
TypeRepr *getSecondTypeRepr() const {
1200-
assert(getKind() == RequirementReprKind::SameType);
1201-
return SecondType.getTypeRepr();
1202-
}
1203-
1204-
TypeLoc &getSecondTypeLoc() {
1205-
assert(getKind() == RequirementReprKind::SameType);
1206-
return SecondType;
1207-
}
1208-
1209-
const TypeLoc &getSecondTypeLoc() const {
12101147
assert(getKind() == RequirementReprKind::SameType);
12111148
return SecondType;
12121149
}
@@ -1217,19 +1154,13 @@ class RequirementRepr {
12171154
return SeparatorLoc;
12181155
}
12191156

1220-
SourceRange getSourceRange() const {
1221-
if (getKind() == RequirementReprKind::LayoutConstraint)
1222-
return SourceRange(FirstType.getSourceRange().Start,
1223-
SecondLayout.getSourceRange().End);
1224-
return SourceRange(FirstType.getSourceRange().Start,
1225-
SecondType.getSourceRange().End);
1226-
}
1157+
SourceRange getSourceRange() const;
12271158

12281159
/// Retrieve the first or subject type representation from the \c repr,
12291160
/// or \c nullptr if \c repr is null.
12301161
static TypeRepr *getFirstTypeRepr(const RequirementRepr *repr) {
12311162
if (!repr) return nullptr;
1232-
return repr->FirstType.getTypeRepr();
1163+
return repr->FirstType;
12331164
}
12341165

12351166
/// Retrieve the second or constraint type representation from the \c repr,
@@ -1238,7 +1169,7 @@ class RequirementRepr {
12381169
if (!repr) return nullptr;
12391170
assert(repr->getKind() == RequirementReprKind::TypeConstraint ||
12401171
repr->getKind() == RequirementReprKind::SameType);
1241-
return repr->SecondType.getTypeRepr();
1172+
return repr->SecondType;
12421173
}
12431174

12441175
SWIFT_DEBUG_DUMP;

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class RequirementRequest :
441441
public SimpleRequest<RequirementRequest,
442442
Requirement(WhereClauseOwner, unsigned,
443443
TypeResolutionStage),
444-
RequestFlags::SeparatelyCached> {
444+
RequestFlags::Cached> {
445445
public:
446446
using SimpleRequest::SimpleRequest;
447447

@@ -464,10 +464,8 @@ class RequirementRequest :
464464
// Cycle handling.
465465
void noteCycleStep(DiagnosticEngine &diags) const;
466466

467-
// Separate caching.
467+
// Caching.
468468
bool isCached() const;
469-
Optional<Requirement> getCachedResult() const;
470-
void cacheResult(Requirement value) const;
471469
};
472470

473471
/// Generate the USR for the given declaration.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ SWIFT_REQUEST(TypeChecker, ProtocolRequiresClassRequest, bool(ProtocolDecl *),
176176
SeparatelyCached, NoLocationInfo)
177177
SWIFT_REQUEST(TypeChecker, RequirementRequest,
178178
Requirement(WhereClauseOwner, unsigned, TypeResolutionStage),
179-
SeparatelyCached, HasNearestLocation)
179+
Cached, HasNearestLocation)
180180
SWIFT_REQUEST(TypeChecker, RequirementSignatureRequest,
181181
ArrayRef<Requirement>(ProtocolDecl *), SeparatelyCached,
182182
NoLocationInfo)

lib/AST/ASTDumper.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,47 +121,49 @@ void RequirementRepr::dump() const {
121121
llvm::errs() << "\n";
122122
}
123123

124-
void RequirementRepr::printImpl(ASTPrinter &out, bool AsWritten) const {
125-
auto printTy = [&](const TypeLoc &TyLoc) {
126-
if (AsWritten && TyLoc.getTypeRepr()) {
127-
TyLoc.getTypeRepr()->print(out, PrintOptions());
128-
} else {
129-
TyLoc.getType().print(out, PrintOptions());
130-
}
131-
};
132-
124+
void RequirementRepr::printImpl(ASTPrinter &out) const {
133125
auto printLayoutConstraint =
134126
[&](const LayoutConstraintLoc &LayoutConstraintLoc) {
135127
LayoutConstraintLoc.getLayoutConstraint()->print(out, PrintOptions());
136128
};
137129

138130
switch (getKind()) {
139131
case RequirementReprKind::LayoutConstraint:
140-
printTy(getSubjectLoc());
132+
if (auto *repr = getSubjectRepr()) {
133+
repr->print(out, PrintOptions());
134+
}
141135
out << " : ";
142136
printLayoutConstraint(getLayoutConstraintLoc());
143137
break;
144138

145139
case RequirementReprKind::TypeConstraint:
146-
printTy(getSubjectLoc());
140+
if (auto *repr = getSubjectRepr()) {
141+
repr->print(out, PrintOptions());
142+
}
147143
out << " : ";
148-
printTy(getConstraintLoc());
144+
if (auto *repr = getConstraintRepr()) {
145+
repr->print(out, PrintOptions());
146+
}
149147
break;
150148

151149
case RequirementReprKind::SameType:
152-
printTy(getFirstTypeLoc());
150+
if (auto *repr = getFirstTypeRepr()) {
151+
repr->print(out, PrintOptions());
152+
}
153153
out << " == ";
154-
printTy(getSecondTypeLoc());
154+
if (auto *repr = getSecondTypeRepr()) {
155+
repr->print(out, PrintOptions());
156+
}
155157
break;
156158
}
157159
}
158160

159161
void RequirementRepr::print(raw_ostream &out) const {
160162
StreamPrinter printer(out);
161-
printImpl(printer, /*AsWritten=*/true);
163+
printImpl(printer);
162164
}
163165
void RequirementRepr::print(ASTPrinter &out) const {
164-
printImpl(out, /*AsWritten=*/true);
166+
printImpl(out);
165167
}
166168

167169
static void printTrailingRequirements(ASTPrinter &Printer,

lib/AST/ASTWalker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,15 +1345,15 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
13451345
bool doIt(RequirementRepr &Req) {
13461346
switch (Req.getKind()) {
13471347
case RequirementReprKind::SameType:
1348-
if (doIt(Req.getFirstTypeLoc()) || doIt(Req.getSecondTypeLoc()))
1348+
if (doIt(Req.getFirstTypeRepr()) || doIt(Req.getSecondTypeRepr()))
13491349
return true;
13501350
break;
13511351
case RequirementReprKind::TypeConstraint:
1352-
if (doIt(Req.getSubjectLoc()) || doIt(Req.getConstraintLoc()))
1352+
if (doIt(Req.getSubjectRepr()) || doIt(Req.getConstraintRepr()))
13531353
return true;
13541354
break;
13551355
case RequirementReprKind::LayoutConstraint:
1356-
if (doIt(Req.getFirstTypeLoc()))
1356+
if (doIt(Req.getFirstTypeRepr()))
13571357
return true;
13581358
break;
13591359
}

lib/AST/Builtins.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -431,26 +431,13 @@ createGenericParam(ASTContext &ctx, const char *name, unsigned index) {
431431

432432
/// Create a generic parameter list with multiple generic parameters.
433433
static GenericParamList *getGenericParams(ASTContext &ctx,
434-
unsigned numParameters,
435-
bool isAnyObject) {
434+
unsigned numParameters) {
436435
assert(numParameters <= llvm::array_lengthof(GenericParamNames));
437436

438-
SmallVector<GenericTypeParamDecl*, 2> genericParams;
437+
SmallVector<GenericTypeParamDecl *, 2> genericParams;
439438
for (unsigned i = 0; i != numParameters; ++i)
440439
genericParams.push_back(createGenericParam(ctx, GenericParamNames[i], i));
441440

442-
443-
if (isAnyObject) {
444-
CanType ao = ctx.getAnyObjectType();
445-
SmallVector<RequirementRepr, 1> req;
446-
req.push_back(RequirementRepr::getTypeConstraint(TypeLoc::withoutLoc(genericParams[0]->getInterfaceType()), SourceLoc(),
447-
TypeLoc::withoutLoc(ao)));
448-
449-
auto paramList = GenericParamList::create(ctx, SourceLoc(), genericParams,
450-
SourceLoc(), req, SourceLoc());
451-
return paramList;
452-
}
453-
454441
auto paramList = GenericParamList::create(ctx, SourceLoc(), genericParams,
455442
SourceLoc());
456443
return paramList;
@@ -474,9 +461,15 @@ namespace {
474461

475462
public:
476463
BuiltinFunctionBuilder(ASTContext &ctx, unsigned numGenericParams = 1,
477-
bool isAnyObject = false)
464+
bool wantsAdditionalAnyObjectRequirement = false)
478465
: Context(ctx) {
479-
TheGenericParamList = getGenericParams(ctx, numGenericParams, isAnyObject);
466+
TheGenericParamList = getGenericParams(ctx, numGenericParams);
467+
if (wantsAdditionalAnyObjectRequirement) {
468+
Requirement req(RequirementKind::Conformance,
469+
TheGenericParamList->getParams()[0]->getInterfaceType(),
470+
ctx.getAnyObjectType());
471+
addedRequirements.push_back(req);
472+
}
480473
for (auto gp : TheGenericParamList->getParams()) {
481474
genericParamTypes.push_back(
482475
gp->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,15 @@ bool Decl::isWeakImported(ModuleDecl *fromModule) const {
875875
return !fromContext.isContainedIn(containingContext);
876876
}
877877

878+
879+
SourceRange RequirementRepr::getSourceRange() const {
880+
if (getKind() == RequirementReprKind::LayoutConstraint)
881+
return SourceRange(FirstType->getSourceRange().Start,
882+
SecondLayout.getSourceRange().End);
883+
return SourceRange(FirstType->getSourceRange().Start,
884+
SecondType->getSourceRange().End);
885+
}
886+
878887
GenericParamList::GenericParamList(SourceLoc LAngleLoc,
879888
ArrayRef<GenericTypeParamDecl *> Params,
880889
SourceLoc WhereLoc,

0 commit comments

Comments
 (0)