Skip to content

Commit fb8fdd9

Browse files
committed
Replace resolveCustomAttrType with a Request
1 parent 426d930 commit fb8fdd9

23 files changed

+153
-82
lines changed

include/swift/AST/Attr.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ class TypeEraserAttr final : public DeclAttribute {
11251125
public:
11261126
static TypeEraserAttr *create(ASTContext &ctx,
11271127
SourceLoc atLoc, SourceRange range,
1128-
TypeLoc typeEraserLoc);
1128+
TypeRepr *typeEraserRepr);
11291129

11301130
static TypeEraserAttr *create(ASTContext &ctx,
11311131
LazyMemberLoader *Resolver,
@@ -1628,8 +1628,8 @@ class CustomAttr final : public DeclAttribute,
16281628
unsigned getNumArguments() const { return numArgLabels; }
16291629
bool hasArgumentLabelLocs() const { return hasArgLabelLocs; }
16301630

1631-
TypeLoc &getTypeLoc() { return type; }
1632-
const TypeLoc &getTypeLoc() const { return type; }
1631+
TypeRepr *getTypeRepr() const { return type.getTypeRepr(); }
1632+
Type getType() const { return type.getType(); }
16331633

16341634
Expr *getArg() const { return arg; }
16351635
void setArg(Expr *newArg) { arg = newArg; }
@@ -1642,6 +1642,14 @@ class CustomAttr final : public DeclAttribute,
16421642
static bool classof(const DeclAttribute *DA) {
16431643
return DA->getKind() == DAK_Custom;
16441644
}
1645+
1646+
private:
1647+
friend class CustomAttrNominalRequest;
1648+
void resetTypeInformation(TypeRepr *repr) { type = TypeLoc(repr); }
1649+
1650+
private:
1651+
friend class CustomAttrTypeRequest;
1652+
void setType(Type ty) { type = TypeLoc(getTypeRepr(), ty); }
16451653
};
16461654

16471655
/// Relates a property to its projection value property, as described by a property wrapper. For

include/swift/AST/TypeCheckRequests.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,41 @@ class CodeCompletionFileRequest
25132513
bool isCached() const { return true; }
25142514
};
25152515

2516+
/// Kinds of types for CustomAttr.
2517+
enum class CustomAttrTypeKind {
2518+
/// The type is required to not be expressed in terms of
2519+
/// any contextual type parameters.
2520+
NonGeneric,
2521+
2522+
/// Property delegates have some funky rules, like allowing
2523+
/// unbound generic types.
2524+
PropertyDelegate,
2525+
};
2526+
2527+
void simple_display(llvm::raw_ostream &out, CustomAttrTypeKind value);
2528+
2529+
class CustomAttrTypeRequest
2530+
: public SimpleRequest<CustomAttrTypeRequest,
2531+
Type(CustomAttr *, DeclContext *,
2532+
CustomAttrTypeKind),
2533+
RequestFlags::SeparatelyCached> {
2534+
public:
2535+
using SimpleRequest::SimpleRequest;
2536+
2537+
private:
2538+
friend SimpleRequest;
2539+
2540+
// Evaluation.
2541+
Type evaluate(Evaluator &evaluator, CustomAttr *, DeclContext *,
2542+
CustomAttrTypeKind) const;
2543+
2544+
public:
2545+
// Separate caching.
2546+
bool isCached() const { return true; }
2547+
Optional<Type> getCachedResult() const;
2548+
void cacheResult(Type value) const;
2549+
};
2550+
25162551
// Allow AnyValue to compare two Type values, even though Type doesn't
25172552
// support ==.
25182553
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ SWIFT_REQUEST(TypeChecker, CodeCompletionFileRequest,
4141
SWIFT_REQUEST(TypeChecker, CompareDeclSpecializationRequest,
4242
bool (DeclContext *, ValueDecl *, ValueDecl *, bool), Cached,
4343
NoLocationInfo)
44+
SWIFT_REQUEST(TypeChecker, CustomAttrTypeRequest,
45+
Type(CustomAttr *, DeclContext *, CustomAttrTypeKind),
46+
SeparatelyCached, NoLocationInfo)
4447
SWIFT_REQUEST(TypeChecker, DefaultArgumentExprRequest,
4548
Expr *(ParamDecl *), SeparatelyCached, NoLocationInfo)
4649
SWIFT_REQUEST(TypeChecker, DefaultArgumentInitContextRequest,

lib/AST/ASTScope.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ AttachedPropertyWrapperScope::getSourceRangeOfVarDecl(const VarDecl *const vd) {
9999
SourceRange sr;
100100
for (auto *attr : vd->getAttrs().getAttributes<CustomAttr>()) {
101101
if (sr.isInvalid())
102-
sr = attr->getTypeLoc().getSourceRange();
102+
sr = attr->getTypeRepr()->getSourceRange();
103103
else
104-
sr.widen(attr->getTypeLoc().getSourceRange());
104+
sr.widen(attr->getTypeRepr()->getSourceRange());
105105
}
106106
return sr;
107107
}

lib/AST/Attr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,11 +1022,11 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
10221022
case DAK_Custom: {
10231023
Printer.callPrintNamePre(PrintNameContext::Attribute);
10241024
Printer << "@";
1025-
const TypeLoc &typeLoc = cast<CustomAttr>(this)->getTypeLoc();
1026-
if (auto type = typeLoc.getType())
1027-
type->print(Printer, Options);
1025+
auto *attr = cast<CustomAttr>(this);
1026+
if (auto type = attr->getType())
1027+
type.print(Printer, Options);
10281028
else
1029-
typeLoc.getTypeRepr()->print(Printer, Options);
1029+
attr->getTypeRepr()->print(Printer, Options);
10301030
Printer.printNamePost(PrintNameContext::Attribute);
10311031
break;
10321032
}
@@ -1386,8 +1386,8 @@ SourceLoc DynamicReplacementAttr::getRParenLoc() const {
13861386

13871387
TypeEraserAttr *TypeEraserAttr::create(ASTContext &ctx,
13881388
SourceLoc atLoc, SourceRange range,
1389-
TypeLoc typeEraserLoc) {
1390-
return new (ctx) TypeEraserAttr(atLoc, range, typeEraserLoc, nullptr, 0);
1389+
TypeRepr *typeEraserRepr) {
1390+
return new (ctx) TypeEraserAttr(atLoc, range, typeEraserRepr, nullptr, 0);
13911391
}
13921392

13931393
TypeEraserAttr *TypeEraserAttr::create(ASTContext &ctx,

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6515,7 +6515,7 @@ ParamDecl::getDefaultValueStringRepresentation(
65156515
if (wrapperAttrs.size() > 0) {
65166516
auto attr = wrapperAttrs.front();
65176517
if (auto arg = attr->getArg()) {
6518-
SourceRange fullRange(attr->getTypeLoc().getSourceRange().Start,
6518+
SourceRange fullRange(attr->getTypeRepr()->getSourceRange().Start,
65196519
arg->getEndLoc());
65206520
auto charRange = Lexer::getCharSourceRangeFromSourceRange(
65216521
getASTContext().SourceMgr, fullRange);

lib/AST/NameLookup.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,12 +2296,11 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
22962296
CustomAttr *attr, DeclContext *dc) const {
22972297
// Find the types referenced by the custom attribute.
22982298
auto &ctx = dc->getASTContext();
2299-
TypeLoc &typeLoc = attr->getTypeLoc();
23002299
DirectlyReferencedTypeDecls decls;
2301-
if (auto typeRepr = typeLoc.getTypeRepr()) {
2300+
if (auto *typeRepr = attr->getTypeRepr()) {
23022301
decls = directReferencesForTypeRepr(
23032302
evaluator, ctx, typeRepr, dc);
2304-
} else if (Type type = typeLoc.getType()) {
2303+
} else if (Type type = attr->getType()) {
23052304
decls = directReferencesForType(type);
23062305
}
23072306

@@ -2316,7 +2315,7 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
23162315
// If we found declarations that are associated types, look outside of
23172316
// the current context to see if we can recover.
23182317
if (declsAreAssociatedTypes(decls)) {
2319-
if (auto typeRepr = typeLoc.getTypeRepr()) {
2318+
if (auto typeRepr = attr->getTypeRepr()) {
23202319
if (auto identTypeRepr = dyn_cast<SimpleIdentTypeRepr>(typeRepr)) {
23212320
auto assocType = cast<AssociatedTypeDecl>(decls.front());
23222321

@@ -2348,7 +2347,7 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
23482347
identTypeRepr
23492348
};
23502349

2351-
typeLoc = TypeLoc(IdentTypeRepr::create(ctx, components));
2350+
attr->resetTypeInformation(IdentTypeRepr::create(ctx, components));
23522351
return nominal;
23532352
}
23542353
}

lib/AST/TypeCheckRequests.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,3 +1498,33 @@ SourceLoc swift::extractNearestSourceLoc(const TypeRepr *repr) {
14981498
return SourceLoc();
14991499
return repr->getLoc();
15001500
}
1501+
1502+
//----------------------------------------------------------------------------//
1503+
// CustomAttrTypeRequest computation.
1504+
//----------------------------------------------------------------------------//
1505+
1506+
void swift::simple_display(llvm::raw_ostream &out, CustomAttrTypeKind value) {
1507+
switch (value) {
1508+
case CustomAttrTypeKind::NonGeneric:
1509+
out << "non-generic";
1510+
return;
1511+
1512+
case CustomAttrTypeKind::PropertyDelegate:
1513+
out << "property-delegate";
1514+
return;
1515+
}
1516+
llvm_unreachable("bad kind");
1517+
}
1518+
1519+
Optional<Type> CustomAttrTypeRequest::getCachedResult() const {
1520+
auto *attr = std::get<0>(getStorage());
1521+
if (auto ty = attr->getType()) {
1522+
return ty;
1523+
}
1524+
return None;
1525+
}
1526+
1527+
void CustomAttrTypeRequest::cacheResult(Type value) const {
1528+
auto *attr = std::get<0>(getStorage());
1529+
attr->setType(value);
1530+
}

lib/IDE/Formatting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class RangeWalker: protected ASTWalker {
446446
}
447447
}
448448
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
449-
if (auto *Repr = customAttr->getTypeLoc().getTypeRepr()) {
449+
if (auto *Repr = customAttr->getTypeRepr()) {
450450
if (!Repr->walk(*this))
451451
return false;
452452
}
@@ -1260,7 +1260,7 @@ class FormatWalker : public ASTWalker {
12601260
}
12611261
}
12621262
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
1263-
if (auto *Repr = customAttr->getTypeLoc().getTypeRepr()) {
1263+
if (auto *Repr = customAttr->getTypeRepr()) {
12641264
if (!Repr->walk(*this))
12651265
return false;
12661266
}

lib/IDE/SourceEntityWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
598598
}
599599
}
600600
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
601-
if (auto *Repr = customAttr->getTypeLoc().getTypeRepr()) {
601+
if (auto *Repr = customAttr->getTypeRepr()) {
602602
if (!Repr->walk(*this))
603603
return false;
604604
}

0 commit comments

Comments
 (0)