Skip to content

Commit 80d3a32

Browse files
committed
Strip ImplementsAttr of its TypeLoc
1 parent fb8fdd9 commit 80d3a32

File tree

7 files changed

+34
-29
lines changed

7 files changed

+34
-29
lines changed

include/swift/AST/Attr.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class LazyConformanceLoader;
5656
class LazyMemberLoader;
5757
class PatternBindingInitializer;
5858
class TrailingWhereClause;
59+
class TypeExpr;
5960

6061
/// TypeAttributes - These are attributes that may be applied to types.
6162
class TypeAttributes {
@@ -1464,25 +1465,26 @@ class SpecializeAttr : public DeclAttribute {
14641465
/// The @_implements attribute, which treats a decl as the implementation for
14651466
/// some named protocol requirement (but otherwise not-visible by that name).
14661467
class ImplementsAttr : public DeclAttribute {
1467-
1468-
TypeLoc ProtocolType;
1468+
TypeExpr *ProtocolType;
14691469
DeclName MemberName;
14701470
DeclNameLoc MemberNameLoc;
14711471

14721472
public:
14731473
ImplementsAttr(SourceLoc atLoc, SourceRange Range,
1474-
TypeLoc ProtocolType,
1474+
TypeExpr *ProtocolType,
14751475
DeclName MemberName,
14761476
DeclNameLoc MemberNameLoc);
14771477

14781478
static ImplementsAttr *create(ASTContext &Ctx, SourceLoc atLoc,
14791479
SourceRange Range,
1480-
TypeLoc ProtocolType,
1480+
TypeExpr *ProtocolType,
14811481
DeclName MemberName,
14821482
DeclNameLoc MemberNameLoc);
14831483

1484-
TypeLoc getProtocolType() const;
1485-
TypeLoc &getProtocolType();
1484+
void setProtocolType(Type ty);
1485+
Type getProtocolType() const;
1486+
TypeRepr *getProtocolTypeRepr() const;
1487+
14861488
DeclName getMemberName() const { return MemberName; }
14871489
DeclNameLoc getMemberNameLoc() const { return MemberNameLoc; }
14881490

lib/AST/Attr.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
975975
Printer.printAttrName("@_implements");
976976
Printer << "(";
977977
auto *attr = cast<ImplementsAttr>(this);
978-
attr->getProtocolType().getType().print(Printer, Options);
978+
attr->getProtocolType().print(Printer, Options);
979979
Printer << ", " << attr->getMemberName() << ")";
980980
break;
981981
}
@@ -1814,7 +1814,7 @@ TransposeAttr *TransposeAttr::create(ASTContext &context, bool implicit,
18141814
}
18151815

18161816
ImplementsAttr::ImplementsAttr(SourceLoc atLoc, SourceRange range,
1817-
TypeLoc ProtocolType,
1817+
TypeExpr *ProtocolType,
18181818
DeclName MemberName,
18191819
DeclNameLoc MemberNameLoc)
18201820
: DeclAttribute(DAK_Implements, atLoc, range, /*Implicit=*/false),
@@ -1826,20 +1826,25 @@ ImplementsAttr::ImplementsAttr(SourceLoc atLoc, SourceRange range,
18261826

18271827
ImplementsAttr *ImplementsAttr::create(ASTContext &Ctx, SourceLoc atLoc,
18281828
SourceRange range,
1829-
TypeLoc ProtocolType,
1829+
TypeExpr *ProtocolType,
18301830
DeclName MemberName,
18311831
DeclNameLoc MemberNameLoc) {
18321832
void *mem = Ctx.Allocate(sizeof(ImplementsAttr), alignof(ImplementsAttr));
18331833
return new (mem) ImplementsAttr(atLoc, range, ProtocolType,
18341834
MemberName, MemberNameLoc);
18351835
}
18361836

1837-
TypeLoc ImplementsAttr::getProtocolType() const {
1838-
return ProtocolType;
1837+
void ImplementsAttr::setProtocolType(Type ty) {
1838+
assert(ty);
1839+
ProtocolType->setType(MetatypeType::get(ty));
18391840
}
18401841

1841-
TypeLoc &ImplementsAttr::getProtocolType() {
1842-
return ProtocolType;
1842+
Type ImplementsAttr::getProtocolType() const {
1843+
return ProtocolType->getInstanceType();
1844+
}
1845+
1846+
TypeRepr *ImplementsAttr::getProtocolTypeRepr() const {
1847+
return ProtocolType->getTypeRepr();
18431848
}
18441849

18451850
CustomAttr::CustomAttr(SourceLoc atLoc, SourceRange range, TypeLoc type,

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,10 @@ Parser::parseImplementsAttribute(SourceLoc AtLoc, SourceLoc Loc) {
811811
}
812812

813813
// FIXME(ModQual): Reject module qualification on MemberName.
814-
814+
auto *TE = new (Context) TypeExpr(ProtocolType.get());
815815
return ParserResult<ImplementsAttr>(
816816
ImplementsAttr::create(Context, AtLoc, SourceRange(Loc, rParenLoc),
817-
ProtocolType.get(), MemberName.getFullName(),
817+
TE, MemberName.getFullName(),
818818
MemberNameLoc));
819819
}
820820

lib/Sema/DerivedConformanceComparable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ deriveComparable_lt(
289289
if (generatedIdentifier != C.Id_LessThanOperator) {
290290
auto comparable = C.getProtocol(KnownProtocolKind::Comparable);
291291
auto comparableType = comparable->getDeclaredType();
292-
auto comparableTypeLoc = TypeLoc::withoutLoc(comparableType);
292+
auto comparableTypeExpr = TypeExpr::createImplicit(comparableType, C);
293293
SmallVector<Identifier, 2> argumentLabels = { Identifier(), Identifier() };
294294
auto comparableDeclName = DeclName(C, DeclBaseName(C.Id_LessThanOperator),
295295
argumentLabels);
296296
comparableDecl->getAttrs().add(new (C) ImplementsAttr(SourceLoc(),
297297
SourceRange(),
298-
comparableTypeLoc,
298+
comparableTypeExpr,
299299
comparableDeclName,
300300
DeclNameLoc()));
301301
}

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,13 @@ deriveEquatable_eq(
506506
if (generatedIdentifier != C.Id_EqualsOperator) {
507507
auto equatableProto = C.getProtocol(KnownProtocolKind::Equatable);
508508
auto equatableTy = equatableProto->getDeclaredType();
509-
auto equatableTypeLoc = TypeLoc::withoutLoc(equatableTy);
509+
auto equatableTyExpr = TypeExpr::createImplicit(equatableTy, C);
510510
SmallVector<Identifier, 2> argumentLabels = { Identifier(), Identifier() };
511511
auto equalsDeclName = DeclName(C, DeclBaseName(C.Id_EqualsOperator),
512512
argumentLabels);
513513
eqDecl->getAttrs().add(new (C) ImplementsAttr(SourceLoc(),
514514
SourceRange(),
515-
equatableTypeLoc,
515+
equatableTyExpr,
516516
equalsDeclName,
517517
DeclNameLoc()));
518518
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,23 +2841,21 @@ void AttributeChecker::visitTypeEraserAttr(TypeEraserAttr *attr) {
28412841
}
28422842

28432843
void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
2844-
TypeLoc &ProtoTypeLoc = attr->getProtocolType();
2845-
28462844
DeclContext *DC = D->getDeclContext();
28472845

2848-
Type T = ProtoTypeLoc.getType();
2849-
if (!T && ProtoTypeLoc.getTypeRepr()) {
2846+
Type T = attr->getProtocolType();
2847+
if (!T && attr->getProtocolTypeRepr()) {
28502848
TypeResolutionOptions options = None;
28512849
options |= TypeResolutionFlags::AllowUnboundGenerics;
28522850

28532851
T = TypeResolution::forContextual(DC, options)
2854-
.resolveType(ProtoTypeLoc.getTypeRepr());
2855-
ProtoTypeLoc.setType(T);
2852+
.resolveType(attr->getProtocolTypeRepr());
28562853
}
28572854

28582855
// Definite error-types were already diagnosed in resolveType.
2859-
if (T->hasError())
2856+
if (!T || T->hasError())
28602857
return;
2858+
attr->setProtocolType(T);
28612859

28622860
// Check that we got a ProtocolType.
28632861
if (auto PT = T->getAs<ProtocolType>()) {
@@ -2882,12 +2880,12 @@ void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
28822880
diagnose(attr->getLocation(),
28832881
diag::implements_attr_protocol_not_conformed_to,
28842882
NTD->getName(), PD->getName())
2885-
.highlight(ProtoTypeLoc.getTypeRepr()->getSourceRange());
2883+
.highlight(attr->getProtocolTypeRepr()->getSourceRange());
28862884
}
28872885

28882886
} else {
28892887
diagnose(attr->getLocation(), diag::implements_attr_non_protocol_type)
2890-
.highlight(ProtoTypeLoc.getTypeRepr()->getSourceRange());
2888+
.highlight(attr->getProtocolTypeRepr()->getSourceRange());
28912889
}
28922890
}
28932891

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ witnessHasImplementsAttrForExactRequirement(ValueDecl *witness,
10391039
assert(requirement->isProtocolRequirement());
10401040
auto *PD = cast<ProtocolDecl>(requirement->getDeclContext());
10411041
if (auto A = witness->getAttrs().getAttribute<ImplementsAttr>()) {
1042-
if (Type T = A->getProtocolType().getType()) {
1042+
if (Type T = A->getProtocolType()) {
10431043
if (auto ProtoTy = T->getAs<ProtocolType>()) {
10441044
if (ProtoTy->getDecl() == PD) {
10451045
return A->getMemberName() == requirement->getName();

0 commit comments

Comments
 (0)