Skip to content

Commit fd4094c

Browse files
committed
[NFC] Introduce TypeRepr::isSimpleUnqualifiedIdentifier to simplify some code
1 parent d6fd424 commit fd4094c

File tree

9 files changed

+58
-62
lines changed

9 files changed

+58
-62
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
195195
/// \c Type::getCanonicalType() or \c Type::getWithoutParens().
196196
TypeRepr *getWithoutParens() const;
197197

198+
/// Whether this is a `SimpleIdentTypeRepr` matching the given identifier.
199+
bool isSimpleUnqualifiedIdentifier(Identifier identifier) const;
200+
201+
/// Whether this is a `SimpleIdentTypeRepr` matching the given string.
202+
bool isSimpleUnqualifiedIdentifier(StringRef str) const;
203+
198204
//*** Allocation Routines ************************************************/
199205

200206
void print(raw_ostream &OS, const PrintOptions &Opts = PrintOptions()) const;

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4990,14 +4990,13 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
49904990
}
49914991
}
49924992

4993-
if (!ResultTyLoc.getTypeRepr())
4994-
ResultTyLoc = TypeLoc::withoutLoc(ResultTy);
49954993
// FIXME: Hacky way to workaround the fact that 'Self' as return
49964994
// TypeRepr is not getting 'typechecked'. See
49974995
// \c resolveTopLevelIdentTypeComponent function in TypeCheckType.cpp.
4998-
if (auto *simId = dyn_cast_or_null<SimpleIdentTypeRepr>(ResultTyLoc.getTypeRepr())) {
4999-
if (simId->getNameRef().isSimpleName(Ctx.Id_Self))
5000-
ResultTyLoc = TypeLoc::withoutLoc(ResultTy);
4996+
if (ResultTyLoc.getTypeRepr() &&
4997+
ResultTyLoc.getTypeRepr()->isSimpleUnqualifiedIdentifier(
4998+
Ctx.Id_Self)) {
4999+
ResultTyLoc = TypeLoc::withoutLoc(ResultTy);
50015000
}
50025001
Printer << " -> ";
50035002

lib/AST/Decl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,9 +2290,8 @@ static bool isDefaultInitializable(const TypeRepr *typeRepr, ASTContext &ctx) {
22902290

22912291
// Also support the desugared 'Optional<T>' spelling.
22922292
if (!ctx.isSwiftVersionAtLeast(5)) {
2293-
if (auto *identRepr = dyn_cast<SimpleIdentTypeRepr>(typeRepr)) {
2294-
if (identRepr->getNameRef().getBaseIdentifier() == ctx.Id_Void)
2295-
return true;
2293+
if (typeRepr->isSimpleUnqualifiedIdentifier(ctx.Id_Void)) {
2294+
return true;
22962295
}
22972296

22982297
if (auto *identRepr = dyn_cast<GenericIdentTypeRepr>(typeRepr)) {

lib/AST/NameLookup.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,9 +1123,7 @@ SelfBounds SelfBoundsFromWhereClauseRequest::evaluate(
11231123
// The left-hand side of the type constraint must be 'Self'.
11241124
bool isSelfLHS = false;
11251125
if (auto typeRepr = req.getSubjectRepr()) {
1126-
if (auto identTypeRepr = dyn_cast<SimpleIdentTypeRepr>(typeRepr))
1127-
isSelfLHS = (identTypeRepr->getNameRef().getBaseIdentifier() ==
1128-
ctx.Id_Self);
1126+
isSelfLHS = typeRepr->isSimpleUnqualifiedIdentifier(ctx.Id_Self);
11291127
}
11301128
if (!isSelfLHS)
11311129
continue;
@@ -2750,13 +2748,11 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
27502748
// TypeRepr version: Builtin.AnyObject
27512749
if (auto typeRepr = typealias->getUnderlyingTypeRepr()) {
27522750
if (auto memberTR = dyn_cast<MemberTypeRepr>(typeRepr)) {
2753-
if (auto identRoot = dyn_cast<IdentTypeRepr>(memberTR->getRoot())) {
2754-
auto memberComps = memberTR->getMemberComponents();
2755-
if (memberComps.size() == 1 &&
2756-
identRoot->getNameRef().isSimpleName("Builtin") &&
2757-
memberComps.front()->getNameRef().isSimpleName("AnyObject")) {
2758-
anyObject = true;
2759-
}
2751+
auto memberComps = memberTR->getMemberComponents();
2752+
if (memberComps.size() == 1 &&
2753+
memberComps.front()->getNameRef().isSimpleName("AnyObject") &&
2754+
memberTR->getRoot()->isSimpleUnqualifiedIdentifier("Builtin")) {
2755+
anyObject = true;
27602756
}
27612757
}
27622758
}

lib/AST/TypeRepr.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ TypeRepr *TypeRepr::getWithoutParens() const {
119119
return repr;
120120
}
121121

122+
bool TypeRepr::isSimpleUnqualifiedIdentifier(Identifier identifier) const {
123+
if (auto *identTR = dyn_cast<SimpleIdentTypeRepr>(this)) {
124+
if (identTR->getNameRef().getBaseIdentifier() == identifier) {
125+
return true;
126+
}
127+
}
128+
129+
return false;
130+
}
131+
132+
bool TypeRepr::isSimpleUnqualifiedIdentifier(StringRef str) const {
133+
if (auto *identTR = dyn_cast<SimpleIdentTypeRepr>(this)) {
134+
if (identTR->getNameRef().getBaseIdentifier().is(str)) {
135+
return true;
136+
}
137+
}
138+
139+
return false;
140+
}
141+
122142
SourceLoc TypeRepr::findAttrLoc(TypeAttrKind kind) const {
123143
auto typeRepr = this;
124144
while (auto attrTypeRepr = dyn_cast<AttributedTypeRepr>(typeRepr)) {

lib/Parse/ParseType.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -470,26 +470,16 @@ ParserResult<TypeRepr> Parser::parseTypeScalar(
470470
TupleTypeRepr *argsTyR = nullptr;
471471
if (auto *TTArgs = dyn_cast<TupleTypeRepr>(tyR)) {
472472
argsTyR = TTArgs;
473-
} else {
474-
bool isVoid = false;
475-
if (const auto Void = dyn_cast<SimpleIdentTypeRepr>(tyR)) {
476-
if (Void->getNameRef().isSimpleName(Context.Id_Void)) {
477-
isVoid = true;
478-
}
479-
}
480-
481-
if (isVoid) {
482-
diagnose(tyR->getStartLoc(), diag::function_type_no_parens)
473+
} else if (tyR->isSimpleUnqualifiedIdentifier(Context.Id_Void)) {
474+
diagnose(tyR->getStartLoc(), diag::function_type_no_parens)
483475
.fixItReplace(tyR->getStartLoc(), "()");
484-
argsTyR = TupleTypeRepr::createEmpty(Context, tyR->getSourceRange());
485-
} else {
486-
diagnose(tyR->getStartLoc(), diag::function_type_no_parens)
476+
argsTyR = TupleTypeRepr::createEmpty(Context, tyR->getSourceRange());
477+
} else {
478+
diagnose(tyR->getStartLoc(), diag::function_type_no_parens)
487479
.highlight(tyR->getSourceRange())
488480
.fixItInsert(tyR->getStartLoc(), "(")
489481
.fixItInsertAfter(tyR->getEndLoc(), ")");
490-
argsTyR = TupleTypeRepr::create(Context, {tyR},
491-
tyR->getSourceRange());
492-
}
482+
argsTyR = TupleTypeRepr::create(Context, {tyR}, tyR->getSourceRange());
493483
}
494484

495485
// Parse substitutions for substituted SIL types.

lib/Sema/PreCheckExpr.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,14 +1997,7 @@ TypeExpr *PreCheckExpression::simplifyTypeExpr(Expr *E) {
19971997
if (!AE->isFolded()) return nullptr;
19981998

19991999
auto diagnoseMissingParens = [](ASTContext &ctx, TypeRepr *tyR) {
2000-
bool isVoid = false;
2001-
if (const auto Void = dyn_cast<SimpleIdentTypeRepr>(tyR)) {
2002-
if (Void->getNameRef().isSimpleName(ctx.Id_Void)) {
2003-
isVoid = true;
2004-
}
2005-
}
2006-
2007-
if (isVoid) {
2000+
if (tyR->isSimpleUnqualifiedIdentifier(ctx.Id_Void)) {
20082001
ctx.Diags.diagnose(tyR->getStartLoc(), diag::function_type_no_parens)
20092002
.fixItReplace(tyR->getStartLoc(), "()");
20102003
} else {

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,11 +1583,7 @@ static bool isModuleQualified(TypeRepr *repr, ModuleDecl *module) {
15831583

15841584
// FIXME(ModQual): This needs to be updated once we have an explicit
15851585
// module qualification syntax.
1586-
IdentTypeRepr *rootIdent = dyn_cast<IdentTypeRepr>(memberTy->getRoot());
1587-
if (!rootIdent) {
1588-
return false;
1589-
}
1590-
return rootIdent->getNameRef().isSimpleName(module->getName());
1586+
return memberTy->getRoot()->isSimpleUnqualifiedIdentifier(module->getName());
15911587
}
15921588

15931589
/// If the provided type is an AttributedTypeRepr, unwraps it and provides both

lib/Sema/TypeCheckType.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,7 @@ static Type diagnoseUnknownType(TypeResolution resolution,
13541354
return ErrorType::get(ctx);
13551355
}
13561356

1357-
if (repr->getNameRef().isSimpleName(ctx.Id_Self) &&
1358-
!isa<GenericIdentTypeRepr>(repr)) {
1357+
if (repr->isSimpleUnqualifiedIdentifier(ctx.Id_Self)) {
13591358
DeclContext *nominalDC = nullptr;
13601359
NominalTypeDecl *nominal = nullptr;
13611360
if ((nominalDC = dc->getInnermostTypeContext()) &&
@@ -3847,6 +3846,7 @@ TypeResolver::resolveOpaqueReturnType(TypeRepr *repr, StringRef mangledName,
38473846
NeverNullType TypeResolver::resolveASTFunctionType(
38483847
FunctionTypeRepr *repr, TypeResolutionOptions parentOptions,
38493848
TypeAttrSet *attrs) {
3849+
auto &ctx = getASTContext();
38503850

38513851
auto isolatedAttr = claim<IsolatedTypeAttr>(attrs);
38523852

@@ -3907,7 +3907,7 @@ NeverNullType TypeResolver::resolveASTFunctionType(
39073907
diagnoseInvalid(
39083908
repr, diffAttr->getAtLoc(),
39093909
diag::differentiable_programming_attr_used_without_required_module,
3910-
diffAttr->getAttrName(), getASTContext().Id_Differentiation);
3910+
diffAttr->getAttrName(), ctx.Id_Differentiation);
39113911
}
39123912
}
39133913

@@ -3921,8 +3921,8 @@ NeverNullType TypeResolver::resolveASTFunctionType(
39213921
diagnose(repr->getLoc(), diag::isolated_parameter_duplicate_type)
39223922
.warnUntilSwiftVersion(6);
39233923

3924-
if (getASTContext().LangOpts.isSwiftVersionAtLeast(6))
3925-
return ErrorType::get(getASTContext());
3924+
if (ctx.LangOpts.isSwiftVersionAtLeast(6))
3925+
return ErrorType::get(ctx);
39263926
else
39273927
repr->setWarned();
39283928
}
@@ -3974,19 +3974,19 @@ NeverNullType TypeResolver::resolveASTFunctionType(
39743974
if (auto patternParams = repr->getPatternGenericParams()) {
39753975
diagnose(patternParams->getLAngleLoc(),
39763976
diag::ast_subst_function_type);
3977-
return ErrorType::get(getASTContext());
3977+
return ErrorType::get(ctx);
39783978
} else if (!repr->getInvocationSubstitutions().empty()) {
39793979
diagnose(repr->getInvocationSubstitutions()[0]->getStartLoc(),
39803980
diag::ast_subst_function_type);
3981-
return ErrorType::get(getASTContext());
3981+
return ErrorType::get(ctx);
39823982
}
39833983

39843984
llvm::Optional<SILInnerGenericContextRAII> innerGenericContext;
39853985
if (auto *genericParams = repr->getGenericParams()) {
39863986
if (!silContext) {
39873987
diagnose(genericParams->getLAngleLoc(), diag::generic_function_type)
39883988
.highlight(genericParams->getSourceRange());
3989-
return ErrorType::get(getASTContext());
3989+
return ErrorType::get(ctx);
39903990
}
39913991
innerGenericContext.emplace(silContext, genericParams);
39923992
}
@@ -4000,7 +4000,7 @@ NeverNullType TypeResolver::resolveASTFunctionType(
40004000
resultOptions.setContext(TypeResolverContext::FunctionResult);
40014001
auto outputTy = resolveType(repr->getResultTypeRepr(), resultOptions);
40024002
if (outputTy->hasError()) {
4003-
return ErrorType::get(getASTContext());
4003+
return ErrorType::get(ctx);
40044004
}
40054005

40064006
// If this is a function type without parens around the parameter list,
@@ -4011,13 +4011,10 @@ NeverNullType TypeResolver::resolveASTFunctionType(
40114011
// asking if they meant () -> ().
40124012
auto args = repr->getArgsTypeRepr();
40134013
if (args->getNumElements() == 1) {
4014-
if (const auto Void =
4015-
dyn_cast<SimpleIdentTypeRepr>(args->getElementType(0))) {
4016-
if (Void->getNameRef().isSimpleName(getASTContext().Id_Void)) {
4017-
diagnose(args->getStartLoc(), diag::paren_void_probably_void)
4014+
if (args->getElementType(0)->isSimpleUnqualifiedIdentifier(ctx.Id_Void)) {
4015+
diagnose(args->getStartLoc(), diag::paren_void_probably_void)
40184016
.fixItReplace(args->getSourceRange(), "()");
4019-
repr->setWarned();
4020-
}
4017+
repr->setWarned();
40214018
}
40224019
}
40234020
}

0 commit comments

Comments
 (0)