Skip to content

Commit 1ff1b94

Browse files
slavapestovAzoy
authored andcommitted
AST: Pick off some usages of GenericTypeParamType::getDecl()
1 parent a434126 commit 1ff1b94

File tree

13 files changed

+47
-50
lines changed

13 files changed

+47
-50
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7009,6 +7009,10 @@ class GenericTypeParamType : public SubstitutableType,
70097009
/// Note: This should only be called by the GenericTypeParamDecl constructor.
70107010
static GenericTypeParamType *get(GenericTypeParamDecl *param);
70117011

7012+
/// If this is an opaque parameter, return the declaration of the
7013+
/// parameter, otherwise null.
7014+
GenericTypeParamDecl *getOpaqueDecl() const;
7015+
70127016
/// Retrieve the declaration of the generic type parameter, or null if
70137017
/// there is no such declaration.
70147018
GenericTypeParamDecl *getDecl() const {

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4280,8 +4280,8 @@ namespace {
42804280
printCommon("generic_type_param_type", label);
42814281
printField(T->getDepth(), "depth");
42824282
printField(T->getIndex(), "index");
4283-
if (auto decl = T->getDecl())
4284-
printFieldQuoted(decl->printRef(), "decl");
4283+
if (!T->isCanonical())
4284+
printFieldQuoted(T->getName(), "name");
42854285
printFlag(T->isParameterPack(), "pack");
42864286

42874287
if (T->isValue())

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
15901590
auto paramTy = cast<GenericTypeParamType>(tybase);
15911591
// If this assertion fires, it probably means the type being mangled here
15921592
// didn't go through getTypeForDWARFMangling().
1593-
assert(paramTy->getDecl() == nullptr &&
1593+
assert(paramTy->isCanonical() &&
15941594
"cannot mangle non-canonical generic parameter");
15951595
// A special mangling for the very first generic parameter. This shows up
15961596
// frequently because it corresponds to 'Self' in protocol requirement

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,17 +1808,15 @@ void PrintAST::printSingleDepthOfGenericSignature(
18081808
/// generic parameters. We only print the former.
18091809
ArrayRef<GenericTypeParamType *> opaqueGenericParams;
18101810
for (unsigned index : indices(genericParams)) {
1811-
auto gpDecl = genericParams[index]->getDecl();
1811+
auto gpDecl = genericParams[index]->getOpaqueDecl();
18121812
if (!gpDecl)
18131813
continue;
18141814

1815-
if (gpDecl->isOpaqueType() && gpDecl->isImplicit()) {
1816-
// We found the first implicit opaque type parameter. Split the
1817-
// generic parameters array at this position.
1818-
opaqueGenericParams = genericParams.slice(index);
1819-
genericParams = genericParams.slice(0, index);
1820-
break;
1821-
}
1815+
// We found the first implicit opaque type parameter. Split the
1816+
// generic parameters array at this position.
1817+
opaqueGenericParams = genericParams.slice(index);
1818+
genericParams = genericParams.slice(0, index);
1819+
break;
18221820
}
18231821

18241822
// Determines whether a given type is based on one of the opaque generic
@@ -7178,8 +7176,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
71787176
if (T->isValue()) printLet();
71797177
};
71807178

7181-
auto decl = T->getDecl();
7182-
if (!decl) {
7179+
if (T->isCanonical()) {
71837180
// If we have an alternate name for this type, use it.
71847181
if (Options.AlternativeTypeNames) {
71857182
auto found = Options.AlternativeTypeNames->find(T->getCanonicalType());
@@ -7194,12 +7191,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
71947191
// canonical types to sugared types.
71957192
if (Options.GenericSig)
71967193
T = Options.GenericSig->getSugaredType(T);
7197-
7198-
decl = T->getDecl();
71997194
}
72007195

72017196
// Print opaque types as "some ..."
7202-
if (decl && decl->isOpaqueType()) {
7197+
if (auto *decl =T->getOpaqueDecl()) {
72037198
// For SIL, we print opaque parameter types as canonical types, and parse
72047199
// them that way too (because they're printed in this way in the SIL
72057200
// generic parameter list).

lib/AST/Type.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,13 @@ unsigned GenericTypeParamType::getIndex() const {
20172017
return fixedNum & 0xFFFF;
20182018
}
20192019

2020+
GenericTypeParamDecl *GenericTypeParamType::getOpaqueDecl() const {
2021+
auto *decl = getDecl();
2022+
if (decl && decl->isOpaqueType())
2023+
return decl;
2024+
return nullptr;
2025+
}
2026+
20202027
Identifier GenericTypeParamType::getName() const {
20212028
// Use the declaration name if we still have that sugar.
20222029
if (auto decl = getDecl())
@@ -4492,10 +4499,8 @@ bool TypeBase::hasSimpleTypeRepr() const {
44924499
}
44934500

44944501
case TypeKind::GenericTypeParam: {
4495-
if (auto *decl = cast<const GenericTypeParamType>(this)->getDecl()) {
4496-
return !decl->isOpaqueType();
4497-
}
4498-
4502+
if (cast<const GenericTypeParamType>(this)->getOpaqueDecl())
4503+
return false;
44994504
return true;
45004505
}
45014506

lib/SIL/IR/SILPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ static void printSILFunctionNameAndType(
531531

532532
// Opaque parameter types are printed as their canonical types and not
533533
// the unparseable "<anonymous>".
534-
if (sugaredTy->getDecl() && sugaredTy->getDecl()->isOpaqueType())
534+
if (sugaredTy->getOpaqueDecl())
535535
continue;
536536

537537
Identifier name = sugaredTy->getName();

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,8 @@ void RequirementFailure::maybeEmitRequirementNote(const Decl *anchor, Type lhs,
550550

551551
// Handle 'some X' where the '& InvertibleProtocol' is implicit
552552
if (auto substTy = req.getFirstType()->getAs<GenericTypeParamType>())
553-
if (auto gtpd = substTy->getDecl())
554-
if (gtpd->isOpaqueType())
555-
diag = diag::noncopyable_generics_implicit_composition;
553+
if (substTy->getOpaqueDecl() != nullptr)
554+
diag = diag::noncopyable_generics_implicit_composition;
556555

557556
emitDiagnosticAt(anchor, diag, req.getFirstType(), req.getSecondType());
558557
return;
@@ -6668,7 +6667,7 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote(
66686667
: nullptr;
66696668
};
66706669

6671-
llvm::SmallDenseMap<GenericTypeParamDecl *, Type> params;
6670+
llvm::SmallDenseMap<GenericTypeParamType *, Type> params;
66726671
for (auto &entry : solution.typeBindings) {
66736672
auto *typeVar = entry.first;
66746673

@@ -6687,11 +6686,11 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote(
66876686
}))
66886687
continue;
66896688

6690-
params[GP->getDecl()] = type;
6689+
params[cast<GenericTypeParamType>(GP->getCanonicalType())] = type;
66916690
}
66926691

6693-
auto getPreferredType = [&](const GenericTypeParamDecl *GP) -> Type {
6694-
auto type = params.find(GP);
6692+
auto getPreferredType = [&](const GenericTypeParamType *GP) -> Type {
6693+
auto type = params.find(cast<GenericTypeParamType>(GP->getCanonicalType()));
66956694
return (type == params.end()) ? Type() : type->second;
66966695
};
66976696

lib/Sema/MiscDiagnostics.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,14 +2578,13 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
25782578
bool TypeChecker::getDefaultGenericArgumentsString(
25792579
SmallVectorImpl<char> &buf,
25802580
const swift::GenericTypeDecl *typeDecl,
2581-
llvm::function_ref<Type(const GenericTypeParamDecl *)> getPreferredType) {
2581+
llvm::function_ref<Type(const GenericTypeParamType *)> getPreferredType) {
25822582
llvm::raw_svector_ostream genericParamText{buf};
25832583
genericParamText << "<";
25842584

25852585
auto printGenericParamSummary =
25862586
[&](GenericTypeParamType *genericParamTy) {
2587-
const GenericTypeParamDecl *genericParam = genericParamTy->getDecl();
2588-
if (Type result = getPreferredType(genericParam)) {
2587+
if (Type result = getPreferredType(genericParamTy)) {
25892588
result.print(genericParamText);
25902589
return;
25912590
}
@@ -2608,7 +2607,7 @@ bool TypeChecker::getDefaultGenericArgumentsString(
26082607
return;
26092608
}
26102609

2611-
genericParamText << "<#" << genericParam->getName() << ": ";
2610+
genericParamText << "<#" << genericParamTy->getName() << ": ";
26122611
genericParamText << upperBound << "#>";
26132612
};
26142613

@@ -6426,8 +6425,8 @@ static OmissionTypeName getTypeNameForOmission(Type type) {
64266425

64276426
// Generic type parameters.
64286427
if (auto genericParamTy = type->getAs<GenericTypeParamType>()) {
6429-
if (auto genericParam = genericParamTy->getDecl())
6430-
return genericParam->getName().str();
6428+
if (!genericParamTy->isCanonical())
6429+
return genericParamTy->getName().str();
64316430

64326431
return "";
64336432
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,8 @@ bool TypeVariableType::Implementation::isOpaqueType() const {
202202
if (!GP)
203203
return false;
204204

205-
if (auto *GPT = GP->getType()->getAs<GenericTypeParamType>()) {
206-
auto *decl = GPT->getDecl();
207-
return decl && decl->isOpaqueType();
208-
}
205+
if (auto *GPT = GP->getType()->getAs<GenericTypeParamType>())
206+
return (GPT->getOpaqueDecl() != nullptr);
209207

210208
return false;
211209
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4292,13 +4292,10 @@ void TypeChecker::checkParameterList(ParameterList *params,
42924292
if (!genericParam)
42934293
return false;
42944294

4295-
auto genericParamDecl = genericParam->getDecl();
4295+
auto genericParamDecl = genericParam->getOpaqueDecl();
42964296
if (!genericParamDecl)
42974297
return false;
42984298

4299-
if (!genericParamDecl->isOpaqueType())
4300-
return false;
4301-
43024299
param->diagnose(
43034300
diag::opaque_type_in_parameter, true, interfaceType);
43044301
return true;

0 commit comments

Comments
 (0)