Skip to content

Commit 1958d7a

Browse files
authored
[AST] Add an AccessScope::dump helper (swiftlang#18756)
And factor an existing getAccessLevelString out into a generally-available swift::getAccessLevelSpelling.
1 parent 3a6b1e9 commit 1958d7a

File tree

5 files changed

+58
-25
lines changed

5 files changed

+58
-25
lines changed

include/swift/AST/AccessScope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class AccessScope {
8686
return None;
8787
}
8888

89+
void dump() const;
8990
};
9091

9192
} // end namespace swift

include/swift/AST/AttrKind.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_ATTRKIND_H
1919

2020
#include "swift/Basic/InlineBitfield.h"
21+
#include "swift/Basic/LLVM.h"
2122
#include "swift/Config.h"
2223
#include "llvm/Support/DataTypes.h"
2324

@@ -63,6 +64,9 @@ enum class AccessLevel : uint8_t {
6364
Open,
6465
};
6566

67+
/// Returns the in-source spelling of the given access level.
68+
StringRef getAccessLevelSpelling(AccessLevel value);
69+
6670
enum class InlineKind : uint8_t {
6771
Never = 0,
6872
Always = 1,

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,7 @@ static StringRef getImportKindString(ImportKind value) {
282282

283283
llvm_unreachable("Unhandled ImportKind in switch.");
284284
}
285-
static StringRef getAccessLevelString(AccessLevel value) {
286-
switch (value) {
287-
case AccessLevel::Private: return "private";
288-
case AccessLevel::FilePrivate: return "fileprivate";
289-
case AccessLevel::Internal: return "internal";
290-
case AccessLevel::Public: return "public";
291-
case AccessLevel::Open: return "open";
292-
}
293285

294-
llvm_unreachable("Unhandled AccessLevel in switch.");
295-
}
296286
static StringRef
297287
getForeignErrorConventionKindString(ForeignErrorConvention::Kind value) {
298288
switch (value) {
@@ -763,7 +753,7 @@ namespace {
763753

764754
if (VD->hasAccess()) {
765755
PrintWithColorRAII(OS, AccessLevelColor) << " access="
766-
<< getAccessLevelString(VD->getFormalAccess());
756+
<< getAccessLevelSpelling(VD->getFormalAccess());
767757
}
768758

769759
if (auto Overridden = VD->getOverriddenDecl()) {

lib/AST/Attr.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ void *AttributeBase::operator new(size_t Bytes, ASTContext &C,
3535
return C.Allocate(Bytes, Alignment);
3636
}
3737

38+
StringRef swift::getAccessLevelSpelling(AccessLevel value) {
39+
switch (value) {
40+
case AccessLevel::Private: return "private";
41+
case AccessLevel::FilePrivate: return "fileprivate";
42+
case AccessLevel::Internal: return "internal";
43+
case AccessLevel::Public: return "public";
44+
case AccessLevel::Open: return "open";
45+
}
46+
47+
llvm_unreachable("Unhandled AccessLevel in switch.");
48+
}
49+
3850
/// Given a name like "autoclosure", return the type attribute ID that
3951
/// corresponds to it. This returns TAK_Count on failure.
4052
///
@@ -609,20 +621,10 @@ StringRef DeclAttribute::getAttrName() const {
609621
return "effects(unspecified)";
610622
}
611623
case DAK_AccessControl:
612-
case DAK_SetterAccess:
613-
switch (cast<AbstractAccessControlAttr>(this)->getAccess()) {
614-
case AccessLevel::Private:
615-
return "private";
616-
case AccessLevel::FilePrivate:
617-
return "fileprivate";
618-
case AccessLevel::Internal:
619-
return "internal";
620-
case AccessLevel::Public:
621-
return "public";
622-
case AccessLevel::Open:
623-
return "open";
624-
}
625-
llvm_unreachable("bad access level");
624+
case DAK_SetterAccess: {
625+
AccessLevel access = cast<AbstractAccessControlAttr>(this)->getAccess();
626+
return getAccessLevelSpelling(access);
627+
}
626628

627629
case DAK_ReferenceOwnership:
628630
return keywordOf(cast<ReferenceOwnershipAttr>(this)->get());

lib/AST/DeclContext.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,42 @@ void DeclContext::dumpContext() const {
492492
printContext(llvm::errs());
493493
}
494494

495+
void AccessScope::dump() const {
496+
llvm::errs() << getAccessLevelSpelling(accessLevelForDiagnostics()) << ": ";
497+
498+
if (isPublic()) {
499+
llvm::errs() << "(null)\n";
500+
return;
501+
}
502+
503+
if (auto *file = dyn_cast<SourceFile>(getDeclContext())) {
504+
llvm::errs() << "file '" << file->getFilename() << "'\n";
505+
return;
506+
}
507+
508+
if (auto *decl = getDeclContext()->getAsDeclOrDeclExtensionContext()) {
509+
llvm::errs() << Decl::getKindName(decl->getKind()) << " ";
510+
if (auto *ext = dyn_cast<ExtensionDecl>(decl))
511+
llvm::errs() << ext->getExtendedNominal()->getName();
512+
else if (auto *named = dyn_cast<ValueDecl>(decl))
513+
llvm::errs() << named->getFullName();
514+
else
515+
llvm::errs() << (const void *)decl;
516+
517+
SourceLoc loc = decl->getLoc();
518+
if (loc.isValid()) {
519+
llvm::errs() << " at ";
520+
loc.print(llvm::errs(), decl->getASTContext().SourceMgr);
521+
}
522+
llvm::errs() << "\n";
523+
524+
return;
525+
}
526+
527+
// If all else fails, dump the DeclContext tree.
528+
getDeclContext()->printContext(llvm::errs());
529+
}
530+
495531
template <typename DCType>
496532
static unsigned getLineNumber(DCType *DC) {
497533
SourceLoc loc = DC->getLoc();

0 commit comments

Comments
 (0)