Skip to content

Commit ca26d57

Browse files
authored
Merge pull request swiftlang#32310 from CodaFi/relocation-allowance
Strip Attributes of their TypeLocs
2 parents 122deaa + 8f065e7 commit ca26d57

34 files changed

+258
-168
lines changed

include/swift/AST/Attr.h

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "swift/AST/PlatformKind.h"
3737
#include "swift/AST/Requirement.h"
3838
#include "swift/AST/TrailingCallArguments.h"
39-
#include "swift/AST/TypeLoc.h"
4039
#include "llvm/ADT/SmallVector.h"
4140
#include "llvm/ADT/StringRef.h"
4241
#include "llvm/Support/ErrorHandling.h"
@@ -56,6 +55,7 @@ class LazyConformanceLoader;
5655
class LazyMemberLoader;
5756
class PatternBindingInitializer;
5857
class TrailingWhereClause;
58+
class TypeExpr;
5959

6060
/// TypeAttributes - These are attributes that may be applied to types.
6161
class TypeAttributes {
@@ -1110,47 +1110,41 @@ class DynamicReplacementAttr final
11101110

11111111
/// The \c @_typeEraser(TypeEraserType) attribute.
11121112
class TypeEraserAttr final : public DeclAttribute {
1113-
TypeLoc TypeEraserLoc;
1113+
TypeExpr *TypeEraserExpr;
11141114
LazyMemberLoader *Resolver;
11151115
uint64_t ResolverContextData;
11161116

11171117
friend class ResolveTypeEraserTypeRequest;
11181118

1119-
TypeEraserAttr(SourceLoc atLoc, SourceRange range, TypeLoc typeEraserLoc,
1119+
TypeEraserAttr(SourceLoc atLoc, SourceRange range, TypeExpr *typeEraserExpr,
11201120
LazyMemberLoader *Resolver, uint64_t Data)
11211121
: DeclAttribute(DAK_TypeEraser, atLoc, range, /*Implicit=*/false),
1122-
TypeEraserLoc(typeEraserLoc),
1122+
TypeEraserExpr(typeEraserExpr),
11231123
Resolver(Resolver), ResolverContextData(Data) {}
11241124

11251125
public:
11261126
static TypeEraserAttr *create(ASTContext &ctx,
11271127
SourceLoc atLoc, SourceRange range,
1128-
TypeLoc typeEraserLoc);
1128+
TypeExpr *typeEraserRepr);
11291129

11301130
static TypeEraserAttr *create(ASTContext &ctx,
11311131
LazyMemberLoader *Resolver,
11321132
uint64_t Data);
11331133

11341134
/// Retrieve the parsed type repr for this attribute, if it
11351135
/// was parsed. Else returns \c nullptr.
1136-
TypeRepr *getParsedTypeEraserTypeRepr() const {
1137-
return TypeEraserLoc.getTypeRepr();
1138-
}
1136+
TypeRepr *getParsedTypeEraserTypeRepr() const;
11391137

11401138
/// Retrieve the parsed location for this attribute, if it was parsed.
1141-
SourceLoc getLoc() const {
1142-
return TypeEraserLoc.getLoc();
1143-
}
1139+
SourceLoc getLoc() const;
11441140

11451141
/// Retrieve the resolved type of this attribute if it has been resolved by a
11461142
/// successful call to \c getResolvedType(). Otherwise,
11471143
/// returns \c Type()
11481144
///
11491145
/// This entrypoint is only suitable for syntactic clients like the
11501146
/// AST printer. Semantic clients should use \c getResolvedType() instead.
1151-
Type getTypeWithoutResolving() const {
1152-
return TypeEraserLoc.getType();
1153-
}
1147+
Type getTypeWithoutResolving() const;
11541148

11551149
/// Returns \c true if the type eraser type has a valid implementation of the
11561150
/// erasing initializer for the given protocol.
@@ -1464,25 +1458,26 @@ class SpecializeAttr : public DeclAttribute {
14641458
/// The @_implements attribute, which treats a decl as the implementation for
14651459
/// some named protocol requirement (but otherwise not-visible by that name).
14661460
class ImplementsAttr : public DeclAttribute {
1467-
1468-
TypeLoc ProtocolType;
1461+
TypeExpr *ProtocolType;
14691462
DeclName MemberName;
14701463
DeclNameLoc MemberNameLoc;
14711464

14721465
public:
14731466
ImplementsAttr(SourceLoc atLoc, SourceRange Range,
1474-
TypeLoc ProtocolType,
1467+
TypeExpr *ProtocolType,
14751468
DeclName MemberName,
14761469
DeclNameLoc MemberNameLoc);
14771470

14781471
static ImplementsAttr *create(ASTContext &Ctx, SourceLoc atLoc,
14791472
SourceRange Range,
1480-
TypeLoc ProtocolType,
1473+
TypeExpr *ProtocolType,
14811474
DeclName MemberName,
14821475
DeclNameLoc MemberNameLoc);
14831476

1484-
TypeLoc getProtocolType() const;
1485-
TypeLoc &getProtocolType();
1477+
void setProtocolType(Type ty);
1478+
Type getProtocolType() const;
1479+
TypeRepr *getProtocolTypeRepr() const;
1480+
14861481
DeclName getMemberName() const { return MemberName; }
14871482
DeclNameLoc getMemberNameLoc() const { return MemberNameLoc; }
14881483

@@ -1595,27 +1590,27 @@ class ClangImporterSynthesizedTypeAttr : public DeclAttribute {
15951590
/// Defines a custom attribute.
15961591
class CustomAttr final : public DeclAttribute,
15971592
public TrailingCallArguments<CustomAttr> {
1598-
TypeLoc type;
1593+
TypeExpr *typeExpr;
15991594
Expr *arg;
16001595
PatternBindingInitializer *initContext;
16011596
Expr *semanticInit = nullptr;
16021597

16031598
unsigned hasArgLabelLocs : 1;
16041599
unsigned numArgLabels : 16;
16051600

1606-
CustomAttr(SourceLoc atLoc, SourceRange range, TypeLoc type,
1601+
CustomAttr(SourceLoc atLoc, SourceRange range, TypeExpr *type,
16071602
PatternBindingInitializer *initContext, Expr *arg,
16081603
ArrayRef<Identifier> argLabels, ArrayRef<SourceLoc> argLabelLocs,
16091604
bool implicit);
16101605

16111606
public:
1612-
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
1607+
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
16131608
bool implicit = false) {
16141609
return create(ctx, atLoc, type, false, nullptr, SourceLoc(), { }, { }, { },
16151610
SourceLoc(), implicit);
16161611
}
16171612

1618-
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
1613+
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
16191614
bool hasInitializer,
16201615
PatternBindingInitializer *initContext,
16211616
SourceLoc lParenLoc,
@@ -1628,8 +1623,8 @@ class CustomAttr final : public DeclAttribute,
16281623
unsigned getNumArguments() const { return numArgLabels; }
16291624
bool hasArgumentLabelLocs() const { return hasArgLabelLocs; }
16301625

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

16341629
Expr *getArg() const { return arg; }
16351630
void setArg(Expr *newArg) { arg = newArg; }
@@ -1642,6 +1637,14 @@ class CustomAttr final : public DeclAttribute,
16421637
static bool classof(const DeclAttribute *DA) {
16431638
return DA->getKind() == DAK_Custom;
16441639
}
1640+
1641+
private:
1642+
friend class CustomAttrNominalRequest;
1643+
void resetTypeInformation(TypeExpr *repr);
1644+
1645+
private:
1646+
friend class CustomAttrTypeRequest;
1647+
void setType(Type ty);
16451648
};
16461649

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

include/swift/AST/LazyResolver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#define SWIFT_AST_LAZYRESOLVER_H
1919

2020
#include "swift/AST/ProtocolConformanceRef.h"
21-
#include "swift/AST/TypeLoc.h"
2221
#include "llvm/ADT/PointerEmbeddedInt.h"
2322

2423
namespace swift {

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,

include/swift/Basic/Statistics.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,6 @@ FRONTEND_STATISTIC(Sema, NamedLazyMemberLoadSuccessCount)
247247
/// Number of types deserialized.
248248
FRONTEND_STATISTIC(Sema, NumTypesDeserialized)
249249

250-
/// Number of types validated.
251-
FRONTEND_STATISTIC(Sema, NumTypesValidated)
252-
253250
/// Number of lazy iterable declaration contexts left unloaded.
254251
FRONTEND_STATISTIC(Sema, NumUnloadedLazyIterableDeclContexts)
255252

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
}

0 commit comments

Comments
 (0)