Skip to content

Commit b3cabb0

Browse files
committed
Use llvm casts in various places instead of looking at {Expr,Decl,TypeBase}::getKind()
Also add some FIXMEs for some code in debug info emission that looks incorrect.
1 parent dca292c commit b3cabb0

File tree

10 files changed

+48
-42
lines changed

10 files changed

+48
-42
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ struct SynthesizedExtensionAnalyzer::Implementation {
325325

326326
std::unique_ptr<ExtensionInfoMap>
327327
collectSynthesizedExtensionInfo(MergeGroupVector &AllGroups) {
328-
if (Target->getKind() == DeclKind::Protocol) {
328+
if (isa<ProtocolDecl>(Target)) {
329329
return collectSynthesizedExtensionInfoForProtocol(AllGroups);
330330
}
331331
std::unique_ptr<ExtensionInfoMap> InfoMap(new ExtensionInfoMap());
@@ -553,7 +553,7 @@ void ASTPrinter::callPrintDeclPre(const Decl *D,
553553
Optional<BracketOptions> Bracket) {
554554
forceNewlines();
555555

556-
if (SynthesizeTarget && D->getKind() == DeclKind::Extension)
556+
if (SynthesizeTarget && isa<ExtensionDecl>(D))
557557
printSynthesizedExtensionPre(cast<ExtensionDecl>(D), SynthesizeTarget, Bracket);
558558
else
559559
printDeclPre(D, Bracket);
@@ -1006,7 +1006,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
10061006
bool Synthesize =
10071007
Options.TransformContext &&
10081008
Options.TransformContext->isPrintingSynthesizedExtension() &&
1009-
D->getKind() == DeclKind::Extension;
1009+
isa<ExtensionDecl>(D);
10101010
if (Synthesize)
10111011
Printer.setSynthesizedTarget(Options.TransformContext->getNominal());
10121012

@@ -2016,7 +2016,7 @@ static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer,
20162016
}
20172017

20182018
// Respect alias type.
2019-
if (ExtendedType->getKind() == TypeKind::NameAlias) {
2019+
if (isa<NameAliasType>(ExtendedType.getPointer())) {
20202020
ExtendedType.print(Printer, Options);
20212021
return;
20222022
}

lib/IDE/CodeCompletion.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ static CodeCompletionResult::ExpectedTypeRelation calculateTypeRelation(
913913
Type ExpectedTy,
914914
DeclContext *DC) {
915915
if (Ty.isNull() || ExpectedTy.isNull() ||
916-
Ty->getKind() == TypeKind::Error ||
917-
ExpectedTy->getKind() == TypeKind::Error)
916+
Ty->is<ErrorType>() ||
917+
ExpectedTy->is<ErrorType>())
918918
return CodeCompletionResult::ExpectedTypeRelation::Unrelated;
919919
if (Ty->isEqual(ExpectedTy))
920920
return CodeCompletionResult::ExpectedTypeRelation::Identical;
@@ -2833,7 +2833,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
28332833
Optional<Type> Result = None;
28342834
if (auto AT = MT->getInstanceType()) {
28352835
if (!CD->getInterfaceType()->is<ErrorType>() &&
2836-
AT->getKind() == TypeKind::NameAlias &&
2836+
isa<NameAliasType>(AT.getPointer()) &&
28372837
AT->getDesugaredType() ==
28382838
CD->getResultInterfaceType().getPointer())
28392839
Result = AT;
@@ -3658,9 +3658,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36583658
}
36593659

36603660
void unboxType(Type T) {
3661-
if (T->getKind() == TypeKind::Paren) {
3661+
if (isa<ParenType>(T.getPointer())) {
36623662
unboxType(T->getDesugaredType());
3663-
} else if (T->getKind() == TypeKind::Tuple) {
3663+
} else if (T->is<TupleType>()) {
36643664
for (auto Ele : T->getAs<TupleType>()->getElements()) {
36653665
unboxType(Ele.getType());
36663666
}
@@ -3838,7 +3838,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
38383838
DeclContext *DC) {
38393839
for (auto It = PossibleArgTypes.begin(); It != PossibleArgTypes.end(); ) {
38403840
llvm::SmallVector<Type, 3> ExpectedTypes;
3841-
if ((*It)->getKind() == TypeKind::Tuple) {
3841+
if (isa<TupleType>((*It).getPointer())) {
38423842
auto Elements = (*It)->getAs<TupleType>()->getElements();
38433843
for (auto Ele : Elements)
38443844
ExpectedTypes.push_back(Ele.getType());
@@ -3874,7 +3874,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
38743874
removeUnlikelyOverloads(PossibleTypes, TupleEleTypesBeforeTarget, &DC);
38753875
return !PossibleTypes.empty();
38763876
}
3877-
} else if (CallE->getArg()->getKind() == ExprKind::Paren) {
3877+
} else if (isa<ParenExpr>(CallE->getArg())) {
38783878
Position = 0;
38793879
HasName = false;
38803880
if (PossibleTypes.empty() &&
@@ -4230,7 +4230,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
42304230
// Implement swift::VisibleDeclConsumer.
42314231
void foundDecl(ValueDecl *D, DeclVisibilityKind Reason) override {
42324232
if (Reason == DeclVisibilityKind::MemberOfCurrentNominal) {
4233-
if (D->getKind() == DeclKind::TypeAlias) {
4233+
if (isa<TypeAliasDecl>(D)) {
42344234
ValueDecl *VD = dyn_cast<ValueDecl>(D);
42354235
SatisfiedAssociatedTypes.insert(VD->getName().str());
42364236
}

lib/IDE/Formatting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class FormatContext {
420420
} else if (auto *Seq = dyn_cast_or_null<SequenceExpr>(Cursor->getAsExpr())) {
421421
ArrayRef<Expr*> Elements = Seq->getElements();
422422
if (Elements.size() == 3 &&
423-
Elements[1]->getKind() == ExprKind::Assign &&
423+
isa<AssignExpr>(Elements[1]) &&
424424
SM.getLineAndColumn(Elements[2]->getEndLoc()).first == Line) {
425425
return false;
426426
}

lib/IDE/TypeReconstruction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ static bool FindFirstNamedDeclWithKind(
514514
// If we didn't find any exact matches, accept any type aliases
515515
if (check_type_aliases) {
516516
for (auto decl : decls) {
517-
if (decl->getKind() == DeclKind::TypeAlias) {
517+
if (isa<TypeAliasDecl>(decl)) {
518518
result._decls.assign(1, decl);
519519
if (decl->hasInterfaceType()) {
520520
result._types.assign(1, decl->getInterfaceType());

lib/IRGen/DebugTypeInfo.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,29 @@ class DebugTypeInfo {
7878

7979
void unwrapLValueOrInOutType() {
8080
Type = Type->getLValueOrInOutObjectType().getPointer();
81-
}
81+
}
8282

83-
// Determine whether this type is an Archetype itself.
84-
bool isArchetype() const {
85-
return Type->getLValueOrInOutObjectType()->is<ArchetypeType>();
86-
}
83+
// Determine whether this type is an Archetype itself.
84+
bool isArchetype() const {
85+
return Type->getLValueOrInOutObjectType()->is<ArchetypeType>();
86+
}
8787

88-
/// LValues, inout args, and Archetypes are implicitly indirect by
89-
/// virtue of their DWARF type.
90-
bool isImplicitlyIndirect() const {
91-
return Type->isLValueType() || isArchetype() ||
92-
(Type->getKind() == TypeKind::InOut);
93-
}
88+
/// LValues, inout args, and Archetypes are implicitly indirect by
89+
/// virtue of their DWARF type.
90+
//
91+
// FIXME: Should this check if the lowered SILType is address only
92+
// instead? Otherwise optionals of archetypes etc will still have
93+
// 'isImplicitlyIndirect()' return false.
94+
bool isImplicitlyIndirect() const {
95+
return Type->isLValueType() || isArchetype() ||
96+
Type->is<InOutType>();
97+
}
9498

95-
bool isNull() const { return Type == nullptr; }
96-
bool operator==(DebugTypeInfo T) const;
97-
bool operator!=(DebugTypeInfo T) const;
99+
bool isNull() const { return Type == nullptr; }
100+
bool operator==(DebugTypeInfo T) const;
101+
bool operator!=(DebugTypeInfo T) const;
98102

99-
void dump() const;
103+
void dump() const;
100104
};
101105
}
102106
}

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3278,9 +3278,13 @@ void IRGenSILFunction::visitDebugValueAddrInst(DebugValueAddrInst *i) {
32783278
auto RealType = SILTy.getSwiftType();
32793279
// Unwrap implicitly indirect types and types that are passed by
32803280
// reference only at the SIL level and below.
3281+
//
3282+
// FIXME: Should this check if the lowered SILType is address only
3283+
// instead? Otherwise optionals of archetypes etc will still have
3284+
// 'Unwrap' set to false.
32813285
bool Unwrap =
32823286
i->getVarInfo().Constant ||
3283-
RealType->getLValueOrInOutObjectType()->getKind() == TypeKind::Archetype;
3287+
RealType->getLValueOrInOutObjectType()->is<ArchetypeType>();
32843288
auto DbgTy = DebugTypeInfo::getLocalVariable(
32853289
CurSILFn->getDeclContext(), Decl, RealType,
32863290
getTypeInfo(SILVal->getType()), Unwrap);

lib/SILOptimizer/Analysis/DestructorAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ bool DestructorAnalysis::isSafeType(CanType Ty) {
5454
cacheResult(Ty, true);
5555

5656
// Trivial value types.
57-
if (Ty->getKind() == TypeKind::BuiltinInteger)
57+
if (Ty->is<BuiltinIntegerType>())
5858
return cacheResult(Ty, true);
59-
if (Ty->getKind() == TypeKind::BuiltinFloat)
59+
if (Ty->is<BuiltinFloatType>())
6060
return cacheResult(Ty, true);
6161

6262
// A struct is safe if

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
16381638
}
16391639

16401640
// Check for CF <-> ObjectiveC bridging.
1641-
if (desugar1->getKind() == TypeKind::Class &&
1641+
if (isa<ClassType>(desugar1) &&
16421642
kind >= ConstraintKind::Subtype) {
16431643
auto class1 = cast<ClassDecl>(nominal1->getDecl());
16441644
auto class2 = cast<ClassDecl>(nominal2->getDecl());
@@ -3662,9 +3662,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
36623662
retry:
36633663
// For a function, bind the output and convert the argument to the input.
36643664
auto func1 = type1->castTo<FunctionType>();
3665-
if (desugar2->getKind() == TypeKind::Function) {
3666-
auto func2 = cast<FunctionType>(desugar2);
3667-
3665+
if (auto func2 = dyn_cast<FunctionType>(desugar2)) {
36683666
// If this application is part of an operator, then we allow an implicit
36693667
// lvalue to be compatible with inout arguments. This is used by
36703668
// assignment operators.

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class AnnotatingPrinter : public StreamPrinter {
124124
}
125125

126126
void deinitDefaultMapToUse(const Decl*D) {
127-
if (D->getKind() == DeclKind::Extension) {
127+
if (isa<ExtensionDecl>(D)) {
128128
DefaultMapToUse = nullptr;
129129
}
130130
}
@@ -154,7 +154,7 @@ class AnnotatingPrinter : public StreamPrinter {
154154
bool shouldContinuePre(const Decl *D, Optional<BracketOptions> Bracket) {
155155
assert(Bracket.hasValue());
156156
if (!Bracket.getValue().shouldOpenExtension(D) &&
157-
D->getKind() == DeclKind::Extension)
157+
isa<ExtensionDecl>(D))
158158
return false;
159159
return true;
160160
}
@@ -164,7 +164,7 @@ class AnnotatingPrinter : public StreamPrinter {
164164
if (!Bracket.getValue().shouldCloseNominal(D) && dyn_cast<NominalTypeDecl>(D))
165165
return false;
166166
if (!Bracket.getValue().shouldCloseExtension(D) &&
167-
D->getKind() == DeclKind::Extension)
167+
isa<ExtensionDecl>(D))
168168
return false;
169169
return true;
170170
}

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ class AnnotatingPrinter : public StreamPrinter {
16181618

16191619
void printDeclPre(const Decl *D, Optional<BracketOptions> Bracket) override {
16201620
StringRef HasDefault = "";
1621-
if (D->getKind() == DeclKind::Protocol) {
1621+
if (isa<ProtocolDecl>(D)) {
16221622
InProtocol = true;
16231623
DefaultImplementationMap.clear();
16241624
ProtocolDecl *PD = const_cast<ProtocolDecl*>(dyn_cast<ProtocolDecl>(D));
@@ -1642,7 +1642,7 @@ class AnnotatingPrinter : public StreamPrinter {
16421642
OS << "</loc>";
16431643
}
16441644
void printDeclPost(const Decl *D, Optional<BracketOptions> Bracket) override {
1645-
if (D->getKind() == DeclKind::Protocol) {
1645+
if (isa<ProtocolDecl>(D)) {
16461646
InProtocol = false;
16471647
}
16481648
OS << "</decl>";
@@ -2235,7 +2235,7 @@ class ASTCommentPrinter : public ASTWalker {
22352235
OS << " ";
22362236
printDocComment(D);
22372237
OS << "\n";
2238-
} else if (D->getKind() == DeclKind::Extension) {
2238+
} else if (isa<ExtensionDecl>(D)) {
22392239
SourceLoc Loc = D->getLoc();
22402240
if (Loc.isValid()) {
22412241
auto LineAndColumn = SM.getLineAndColumn(Loc);

0 commit comments

Comments
 (0)