Skip to content

Strip Attributes of their TypeLocs #32310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "swift/AST/PlatformKind.h"
#include "swift/AST/Requirement.h"
#include "swift/AST/TrailingCallArguments.h"
#include "swift/AST/TypeLoc.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorHandling.h"
Expand All @@ -56,6 +55,7 @@ class LazyConformanceLoader;
class LazyMemberLoader;
class PatternBindingInitializer;
class TrailingWhereClause;
class TypeExpr;

/// TypeAttributes - These are attributes that may be applied to types.
class TypeAttributes {
Expand Down Expand Up @@ -1110,47 +1110,41 @@ class DynamicReplacementAttr final

/// The \c @_typeEraser(TypeEraserType) attribute.
class TypeEraserAttr final : public DeclAttribute {
TypeLoc TypeEraserLoc;
TypeExpr *TypeEraserExpr;
LazyMemberLoader *Resolver;
uint64_t ResolverContextData;

friend class ResolveTypeEraserTypeRequest;

TypeEraserAttr(SourceLoc atLoc, SourceRange range, TypeLoc typeEraserLoc,
TypeEraserAttr(SourceLoc atLoc, SourceRange range, TypeExpr *typeEraserExpr,
LazyMemberLoader *Resolver, uint64_t Data)
: DeclAttribute(DAK_TypeEraser, atLoc, range, /*Implicit=*/false),
TypeEraserLoc(typeEraserLoc),
TypeEraserExpr(typeEraserExpr),
Resolver(Resolver), ResolverContextData(Data) {}

public:
static TypeEraserAttr *create(ASTContext &ctx,
SourceLoc atLoc, SourceRange range,
TypeLoc typeEraserLoc);
TypeExpr *typeEraserRepr);

static TypeEraserAttr *create(ASTContext &ctx,
LazyMemberLoader *Resolver,
uint64_t Data);

/// Retrieve the parsed type repr for this attribute, if it
/// was parsed. Else returns \c nullptr.
TypeRepr *getParsedTypeEraserTypeRepr() const {
return TypeEraserLoc.getTypeRepr();
}
TypeRepr *getParsedTypeEraserTypeRepr() const;

/// Retrieve the parsed location for this attribute, if it was parsed.
SourceLoc getLoc() const {
return TypeEraserLoc.getLoc();
}
SourceLoc getLoc() const;

/// Retrieve the resolved type of this attribute if it has been resolved by a
/// successful call to \c getResolvedType(). Otherwise,
/// returns \c Type()
///
/// This entrypoint is only suitable for syntactic clients like the
/// AST printer. Semantic clients should use \c getResolvedType() instead.
Type getTypeWithoutResolving() const {
return TypeEraserLoc.getType();
}
Type getTypeWithoutResolving() const;

/// Returns \c true if the type eraser type has a valid implementation of the
/// erasing initializer for the given protocol.
Expand Down Expand Up @@ -1464,25 +1458,26 @@ class SpecializeAttr : public DeclAttribute {
/// The @_implements attribute, which treats a decl as the implementation for
/// some named protocol requirement (but otherwise not-visible by that name).
class ImplementsAttr : public DeclAttribute {

TypeLoc ProtocolType;
TypeExpr *ProtocolType;
DeclName MemberName;
DeclNameLoc MemberNameLoc;

public:
ImplementsAttr(SourceLoc atLoc, SourceRange Range,
TypeLoc ProtocolType,
TypeExpr *ProtocolType,
DeclName MemberName,
DeclNameLoc MemberNameLoc);

static ImplementsAttr *create(ASTContext &Ctx, SourceLoc atLoc,
SourceRange Range,
TypeLoc ProtocolType,
TypeExpr *ProtocolType,
DeclName MemberName,
DeclNameLoc MemberNameLoc);

TypeLoc getProtocolType() const;
TypeLoc &getProtocolType();
void setProtocolType(Type ty);
Type getProtocolType() const;
TypeRepr *getProtocolTypeRepr() const;

DeclName getMemberName() const { return MemberName; }
DeclNameLoc getMemberNameLoc() const { return MemberNameLoc; }

Expand Down Expand Up @@ -1595,27 +1590,27 @@ class ClangImporterSynthesizedTypeAttr : public DeclAttribute {
/// Defines a custom attribute.
class CustomAttr final : public DeclAttribute,
public TrailingCallArguments<CustomAttr> {
TypeLoc type;
TypeExpr *typeExpr;
Expr *arg;
PatternBindingInitializer *initContext;
Expr *semanticInit = nullptr;

unsigned hasArgLabelLocs : 1;
unsigned numArgLabels : 16;

CustomAttr(SourceLoc atLoc, SourceRange range, TypeLoc type,
CustomAttr(SourceLoc atLoc, SourceRange range, TypeExpr *type,
PatternBindingInitializer *initContext, Expr *arg,
ArrayRef<Identifier> argLabels, ArrayRef<SourceLoc> argLabelLocs,
bool implicit);

public:
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
bool implicit = false) {
return create(ctx, atLoc, type, false, nullptr, SourceLoc(), { }, { }, { },
SourceLoc(), implicit);
}

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

TypeLoc &getTypeLoc() { return type; }
const TypeLoc &getTypeLoc() const { return type; }
TypeRepr *getTypeRepr() const;
Type getType() const;

Expr *getArg() const { return arg; }
void setArg(Expr *newArg) { arg = newArg; }
Expand All @@ -1642,6 +1637,14 @@ class CustomAttr final : public DeclAttribute,
static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DAK_Custom;
}

private:
friend class CustomAttrNominalRequest;
void resetTypeInformation(TypeExpr *repr);

private:
friend class CustomAttrTypeRequest;
void setType(Type ty);
};

/// Relates a property to its projection value property, as described by a property wrapper. For
Expand Down
1 change: 0 additions & 1 deletion include/swift/AST/LazyResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#define SWIFT_AST_LAZYRESOLVER_H

#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/AST/TypeLoc.h"
#include "llvm/ADT/PointerEmbeddedInt.h"

namespace swift {
Expand Down
35 changes: 35 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,41 @@ class CodeCompletionFileRequest
bool isCached() const { return true; }
};

/// Kinds of types for CustomAttr.
enum class CustomAttrTypeKind {
/// The type is required to not be expressed in terms of
/// any contextual type parameters.
NonGeneric,

/// Property delegates have some funky rules, like allowing
/// unbound generic types.
PropertyDelegate,
};

void simple_display(llvm::raw_ostream &out, CustomAttrTypeKind value);

class CustomAttrTypeRequest
: public SimpleRequest<CustomAttrTypeRequest,
Type(CustomAttr *, DeclContext *,
CustomAttrTypeKind),
RequestFlags::SeparatelyCached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
Type evaluate(Evaluator &evaluator, CustomAttr *, DeclContext *,
CustomAttrTypeKind) const;

public:
// Separate caching.
bool isCached() const { return true; }
Optional<Type> getCachedResult() const;
void cacheResult(Type value) const;
};

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ SWIFT_REQUEST(TypeChecker, CodeCompletionFileRequest,
SWIFT_REQUEST(TypeChecker, CompareDeclSpecializationRequest,
bool (DeclContext *, ValueDecl *, ValueDecl *, bool), Cached,
NoLocationInfo)
SWIFT_REQUEST(TypeChecker, CustomAttrTypeRequest,
Type(CustomAttr *, DeclContext *, CustomAttrTypeKind),
SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, DefaultArgumentExprRequest,
Expr *(ParamDecl *), SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, DefaultArgumentInitContextRequest,
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Basic/Statistics.def
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,6 @@ FRONTEND_STATISTIC(Sema, NamedLazyMemberLoadSuccessCount)
/// Number of types deserialized.
FRONTEND_STATISTIC(Sema, NumTypesDeserialized)

/// Number of types validated.
FRONTEND_STATISTIC(Sema, NumTypesValidated)

/// Number of lazy iterable declaration contexts left unloaded.
FRONTEND_STATISTIC(Sema, NumUnloadedLazyIterableDeclContexts)

Expand Down
4 changes: 2 additions & 2 deletions lib/AST/ASTScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ AttachedPropertyWrapperScope::getSourceRangeOfVarDecl(const VarDecl *const vd) {
SourceRange sr;
for (auto *attr : vd->getAttrs().getAttributes<CustomAttr>()) {
if (sr.isInvalid())
sr = attr->getTypeLoc().getSourceRange();
sr = attr->getTypeRepr()->getSourceRange();
else
sr.widen(attr->getTypeLoc().getSourceRange());
sr.widen(attr->getTypeRepr()->getSourceRange());
}
return sr;
}
Expand Down
Loading