Skip to content

Commit aeed242

Browse files
authored
Merge pull request #27398 from nkcsgexi/get-loc-refactor
AST: refactor getLoc() of decls to have a single entry point for all decl kinds, NFC
2 parents a6dd630 + 4069172 commit aeed242

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

include/swift/AST/Decl.h

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ class alignas(1 << DeclAlignInBits) Decl {
676676

677677
Decl(const Decl&) = delete;
678678
void operator=(const Decl&) = delete;
679+
SourceLoc getLocFromSource() const;
679680

680681
protected:
681682

@@ -1582,7 +1583,7 @@ enum class ImportKind : uint8_t {
15821583
class ImportDecl final : public Decl,
15831584
private llvm::TrailingObjects<ImportDecl, std::pair<Identifier,SourceLoc>> {
15841585
friend TrailingObjects;
1585-
1586+
friend class Decl;
15861587
public:
15871588
typedef std::pair<Identifier, SourceLoc> AccessPathElement;
15881589

@@ -1654,7 +1655,7 @@ class ImportDecl final : public Decl,
16541655
}
16551656

16561657
SourceLoc getStartLoc() const { return ImportLoc; }
1657-
SourceLoc getLoc() const { return getFullAccessPath().front().second; }
1658+
SourceLoc getLocFromSource() const { return getFullAccessPath().front().second; }
16581659
SourceRange getSourceRange() const {
16591660
return SourceRange(ImportLoc, getFullAccessPath().back().second);
16601661
}
@@ -1716,6 +1717,7 @@ class ExtensionDecl final : public GenericContext, public Decl,
17161717
std::pair<LazyMemberLoader *, uint64_t> takeConformanceLoaderSlow();
17171718

17181719
friend class ExtendedNominalRequest;
1720+
friend class Decl;
17191721
public:
17201722
using Decl::getASTContext;
17211723

@@ -1728,7 +1730,7 @@ class ExtensionDecl final : public GenericContext, public Decl,
17281730
ClangNode clangNode = ClangNode());
17291731

17301732
SourceLoc getStartLoc() const { return ExtensionLoc; }
1731-
SourceLoc getLoc() const { return ExtensionLoc; }
1733+
SourceLoc getLocFromSource() const { return ExtensionLoc; }
17321734
SourceRange getSourceRange() const {
17331735
return { ExtensionLoc, Braces.End };
17341736
}
@@ -2041,7 +2043,7 @@ class PatternBindingEntry {
20412043
class PatternBindingDecl final : public Decl,
20422044
private llvm::TrailingObjects<PatternBindingDecl, PatternBindingEntry> {
20432045
friend TrailingObjects;
2044-
2046+
friend class Decl;
20452047
SourceLoc StaticLoc; ///< Location of the 'static/class' keyword, if present.
20462048
SourceLoc VarLoc; ///< Location of the 'var' keyword.
20472049

@@ -2050,7 +2052,7 @@ class PatternBindingDecl final : public Decl,
20502052
PatternBindingDecl(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
20512053
SourceLoc VarLoc, unsigned NumPatternEntries,
20522054
DeclContext *Parent);
2053-
2055+
SourceLoc getLocFromSource() const { return VarLoc; }
20542056
public:
20552057
static PatternBindingDecl *create(ASTContext &Ctx, SourceLoc StaticLoc,
20562058
StaticSpellingKind StaticSpelling,
@@ -2080,7 +2082,6 @@ class PatternBindingDecl final : public Decl,
20802082
SourceLoc getStartLoc() const {
20812083
return StaticLoc.isValid() ? StaticLoc : VarLoc;
20822084
}
2083-
SourceLoc getLoc() const { return VarLoc; }
20842085
SourceRange getSourceRange() const;
20852086

20862087
unsigned getNumPatternEntries() const {
@@ -2216,7 +2217,8 @@ class PatternBindingDecl final : public Decl,
22162217
/// global variables.
22172218
class TopLevelCodeDecl : public DeclContext, public Decl {
22182219
BraceStmt *Body;
2219-
2220+
SourceLoc getLocFromSource() const { return getStartLoc(); }
2221+
friend class Decl;
22202222
public:
22212223
TopLevelCodeDecl(DeclContext *Parent, BraceStmt *Body = nullptr)
22222224
: DeclContext(DeclContextKind::TopLevelCodeDecl, Parent),
@@ -2227,7 +2229,6 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
22272229
void setBody(BraceStmt *b) { Body = b; }
22282230

22292231
SourceLoc getStartLoc() const;
2230-
SourceLoc getLoc() const { return getStartLoc(); }
22312232
SourceRange getSourceRange() const;
22322233

22332234
static bool classof(const Decl *D) {
@@ -2266,6 +2267,8 @@ class IfConfigDecl : public Decl {
22662267
/// The array is ASTContext allocated.
22672268
ArrayRef<IfConfigClause> Clauses;
22682269
SourceLoc EndLoc;
2270+
SourceLoc getLocFromSource() const { return Clauses[0].Loc; }
2271+
friend class Decl;
22692272
public:
22702273

22712274
IfConfigDecl(DeclContext *Parent, ArrayRef<IfConfigClause> Clauses,
@@ -2291,7 +2294,6 @@ class IfConfigDecl : public Decl {
22912294
}
22922295

22932296
SourceLoc getEndLoc() const { return EndLoc; }
2294-
SourceLoc getLoc() const { return Clauses[0].Loc; }
22952297

22962298
bool hadMissingEnd() const { return Bits.IfConfigDecl.HadMissingEnd; }
22972299

@@ -2308,7 +2310,8 @@ class PoundDiagnosticDecl : public Decl {
23082310
SourceLoc StartLoc;
23092311
SourceLoc EndLoc;
23102312
StringLiteralExpr *Message;
2311-
2313+
SourceLoc getLocFromSource() const { return StartLoc; }
2314+
friend class Decl;
23122315
public:
23132316
PoundDiagnosticDecl(DeclContext *Parent, bool IsError, SourceLoc StartLoc,
23142317
SourceLoc EndLoc, StringLiteralExpr *Message)
@@ -2337,7 +2340,6 @@ class PoundDiagnosticDecl : public Decl {
23372340
}
23382341

23392342
SourceLoc getEndLoc() const { return EndLoc; };
2340-
SourceLoc getLoc() const { return StartLoc; }
23412343

23422344
SourceRange getSourceRange() const {
23432345
return SourceRange(StartLoc, EndLoc);
@@ -2400,7 +2402,8 @@ class ValueDecl : public Decl {
24002402
friend class IsFinalRequest;
24012403
friend class IsDynamicRequest;
24022404
friend class IsImplicitlyUnwrappedOptionalRequest;
2403-
2405+
friend class Decl;
2406+
SourceLoc getLocFromSource() const { return NameLoc; }
24042407
protected:
24052408
ValueDecl(DeclKind K,
24062409
llvm::PointerUnion<DeclContext *, ASTContext *> context,
@@ -2473,7 +2476,6 @@ class ValueDecl : public Decl {
24732476
bool canInferObjCFromRequirement(ValueDecl *requirement);
24742477

24752478
SourceLoc getNameLoc() const { return NameLoc; }
2476-
SourceLoc getLoc() const { return NameLoc; }
24772479

24782480
bool isUsableFromInline() const;
24792481

@@ -6281,6 +6283,7 @@ AbstractStorageDecl::AccessorRecord::getAccessor(AccessorKind kind) const {
62816283
class EnumCaseDecl final : public Decl,
62826284
private llvm::TrailingObjects<EnumCaseDecl, EnumElementDecl *> {
62836285
friend TrailingObjects;
6286+
friend class Decl;
62846287
SourceLoc CaseLoc;
62856288

62866289
EnumCaseDecl(SourceLoc CaseLoc,
@@ -6293,6 +6296,7 @@ class EnumCaseDecl final : public Decl,
62936296
std::uninitialized_copy(Elements.begin(), Elements.end(),
62946297
getTrailingObjects<EnumElementDecl *>());
62956298
}
6299+
SourceLoc getLocFromSource() const { return CaseLoc; }
62966300

62976301
public:
62986302
static EnumCaseDecl *create(SourceLoc CaseLoc,
@@ -6304,11 +6308,6 @@ class EnumCaseDecl final : public Decl,
63046308
return {getTrailingObjects<EnumElementDecl *>(),
63056309
Bits.EnumCaseDecl.NumElements};
63066310
}
6307-
6308-
SourceLoc getLoc() const {
6309-
return CaseLoc;
6310-
}
6311-
63126311
SourceRange getSourceRange() const;
63136312

63146313
static bool classof(const Decl *D) {
@@ -6721,7 +6720,8 @@ class PrecedenceGroupDecl : public Decl {
67216720
SourceLoc higherThanLoc, ArrayRef<Relation> higherThan,
67226721
SourceLoc lowerThanLoc, ArrayRef<Relation> lowerThan,
67236722
SourceLoc rbraceLoc);
6724-
6723+
friend class Decl;
6724+
SourceLoc getLocFromSource() const { return NameLoc; }
67256725
public:
67266726
static PrecedenceGroupDecl *create(DeclContext *dc,
67276727
SourceLoc precedenceGroupLoc,
@@ -6741,7 +6741,6 @@ class PrecedenceGroupDecl : public Decl {
67416741
SourceLoc rbraceLoc);
67426742

67436743

6744-
SourceLoc getLoc() const { return NameLoc; }
67456744
SourceRange getSourceRange() const {
67466745
return { PrecedenceGroupLoc, RBraceLoc };
67476746
}
@@ -6860,7 +6859,8 @@ class OperatorDecl : public Decl {
68606859
ArrayRef<Identifier> Identifiers;
68616860
ArrayRef<SourceLoc> IdentifierLocs;
68626861
ArrayRef<NominalTypeDecl *> DesignatedNominalTypes;
6863-
6862+
SourceLoc getLocFromSource() const { return NameLoc; }
6863+
friend class Decl;
68646864
public:
68656865
OperatorDecl(DeclKind kind, DeclContext *DC, SourceLoc OperatorLoc,
68666866
Identifier Name, SourceLoc NameLoc,
@@ -6875,7 +6875,6 @@ class OperatorDecl : public Decl {
68756875
: Decl(kind, DC), OperatorLoc(OperatorLoc), NameLoc(NameLoc), name(Name),
68766876
DesignatedNominalTypes(DesignatedNominalTypes) {}
68776877

6878-
SourceLoc getLoc() const { return NameLoc; }
68796878

68806879
SourceLoc getOperatorLoc() const { return OperatorLoc; }
68816880
SourceLoc getNameLoc() const { return NameLoc; }
@@ -7038,6 +7037,10 @@ class MissingMemberDecl : public Decl {
70387037
&& "not enough bits");
70397038
setImplicit();
70407039
}
7040+
friend class Decl;
7041+
SourceLoc getLocFromSource() const {
7042+
return SourceLoc();
7043+
}
70417044
public:
70427045
static MissingMemberDecl *
70437046
create(ASTContext &ctx, DeclContext *DC, DeclName name,
@@ -7062,10 +7065,6 @@ class MissingMemberDecl : public Decl {
70627065
return Bits.MissingMemberDecl.NumberOfFieldOffsetVectorEntries;
70637066
}
70647067

7065-
SourceLoc getLoc() const {
7066-
return SourceLoc();
7067-
}
7068-
70697068
SourceRange getSourceRange() const {
70707069
return SourceRange();
70717070
}

lib/AST/Decl.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,18 +470,26 @@ SourceRange Decl::getSourceRangeIncludingAttrs() const {
470470
return Range;
471471
}
472472

473-
SourceLoc Decl::getLoc() const {
473+
SourceLoc Decl::getLocFromSource() const {
474474
switch (getKind()) {
475475
#define DECL(ID, X) \
476-
static_assert(sizeof(checkSourceLocType(&ID##Decl::getLoc)) == 1, \
477-
#ID "Decl is missing getLoc()"); \
478-
case DeclKind::ID: return cast<ID##Decl>(this)->getLoc();
476+
static_assert(sizeof(checkSourceLocType(&ID##Decl::getLocFromSource)) == 1, \
477+
#ID "Decl is missing getLocFromSource()"); \
478+
case DeclKind::ID: return cast<ID##Decl>(this)->getLocFromSource();
479479
#include "swift/AST/DeclNodes.def"
480480
}
481481

482482
llvm_unreachable("Unknown decl kind");
483483
}
484484

485+
SourceLoc Decl::getLoc() const {
486+
#define DECL(ID, X) \
487+
static_assert(sizeof(checkSourceLocType(&ID##Decl::getLoc)) == 2, \
488+
#ID "Decl is re-defining getLoc()");
489+
#include "swift/AST/DeclNodes.def"
490+
return getLocFromSource();
491+
}
492+
485493
Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
486494
assert(hasSingleExpressionBody() && "Not a single-expression body");
487495
auto braceStmt = getBody();

0 commit comments

Comments
 (0)