Skip to content

Commit 01fa745

Browse files
committed
[NFC] Clean up access note code
• Rename properties for greater consistency with the YAML file • Rename AccessNotes -> AccessNotesFile • Add doc comments • Miscellaneous comment/style improvements.
1 parent bf13139 commit 01fa745

File tree

5 files changed

+83
-49
lines changed

5 files changed

+83
-49
lines changed

include/swift/AST/AccessNotes.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,79 @@ namespace swift {
3232
class ASTContext;
3333
class ValueDecl;
3434

35+
/// The name of the declaration an access note should be applied to.
36+
///
37+
/// Grammatically, this is equivalent to a \c ParsedDeclName, but supports a
38+
/// subset of that type's features.
3539
class AccessNoteDeclName {
3640
public:
41+
/// The names of the parent/contextual declarations containing the declaration
42+
/// the access note should apply to.
3743
std::vector<Identifier> parentNames;
44+
45+
/// The name of the declaration the access note should be applied to. (For
46+
/// accessors, this is actually the name of the storage it's attached to.)
3847
DeclName name;
48+
49+
/// For accessors, the kind of accessor; for non-accessors, \c None.
3950
Optional<AccessorKind> accessorKind;
4051

4152
AccessNoteDeclName(ASTContext &ctx, StringRef str);
4253
AccessNoteDeclName();
4354

55+
/// If true, an access note with this name should apply to \p VD.
4456
bool matches(ValueDecl *VD) const;
57+
58+
/// If true, the name is empty and invalid.
4559
bool empty() const;
4660

4761
void print(llvm::raw_ostream &os) const;
4862
SWIFT_DEBUG_DUMP;
4963
};
5064

65+
/// An individual access note specifying modifications to a declaration.
5166
class AccessNote {
5267
public:
53-
AccessNoteDeclName name;
68+
/// The name of the declaration to modify.
69+
AccessNoteDeclName Name;
5470

71+
/// If \c true, add an @objc attribute; if \c false, delete an @objc
72+
/// attribute; if \c None, do nothing.
5573
Optional<bool> ObjC;
74+
75+
/// If \c true, add a dynamic modifier; if \c false, delete a dynamic
76+
/// modifier; if \c None, do nothing.
5677
Optional<bool> Dynamic;
78+
79+
/// If set, modify an @objc attribute to give it the specified \c ObjCName.
80+
/// If \c ObjC would otherwise be \c None, it will be set to \c true.
5781
Optional<ObjCSelector> ObjCName;
5882

5983
void dump(llvm::raw_ostream &os, int indent = 0) const;
6084
SWIFT_DEBUG_DUMP;
6185
};
6286

63-
class AccessNotes {
87+
/// A set of access notes with module-wide metadata about them.
88+
class AccessNotesFile {
6489
public:
65-
std::string reason;
66-
std::vector<AccessNote> notes;
90+
/// A human-readable string describing why the access notes are being applied.
91+
/// Inserted into diagnostics about access notes in the file.
92+
std::string Reason;
93+
94+
/// Access notes to apply to the module.
95+
std::vector<AccessNote> Notes;
6796

6897
/// Contains keys which were present in the JSON file but were not recognized
6998
/// by the compiler. Has no functional effect, but can be used for error
7099
/// handling.
71100
std::set<std::string> unknownKeys;
72101

73-
static llvm::Expected<AccessNotes> load(ASTContext &ctx,
74-
llvm::MemoryBuffer *buffer);
102+
/// Load the access notes from \p buffer, or return an error if they cannot
103+
/// be loaded.
104+
static llvm::Expected<AccessNotesFile> load(ASTContext &ctx,
105+
llvm::MemoryBuffer *buffer);
75106

107+
/// Look up the access note in this file, if any, which applies to \p VD.
76108
NullablePtr<const AccessNote> lookup(ValueDecl *VD) const;
77109

78110
void dump(llvm::raw_ostream &os) const;

include/swift/AST/Module.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
250250
/// \see EntryPointInfoTy
251251
EntryPointInfoTy EntryPointInfo;
252252

253-
AccessNotes accessNotes;
253+
AccessNotesFile accessNotes;
254254

255255
ModuleDecl(Identifier name, ASTContext &ctx, ImplicitImportInfo importInfo);
256256

@@ -282,8 +282,8 @@ class ModuleDecl : public DeclContext, public TypeDecl {
282282
/// imports.
283283
ImplicitImportList getImplicitImports() const;
284284

285-
AccessNotes &getAccessNotes() { return accessNotes; }
286-
const AccessNotes &getAccessNotes() const { return accessNotes; }
285+
AccessNotesFile &getAccessNotes() { return accessNotes; }
286+
const AccessNotesFile &getAccessNotes() const { return accessNotes; }
287287

288288
ArrayRef<FileUnit *> getFiles() {
289289
assert(!Files.empty() || failedToLoad());

lib/AST/AccessNotes.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ AccessNoteDeclName::AccessNoteDeclName(ASTContext &ctx, StringRef str) {
5656
}
5757

5858
bool AccessNoteDeclName::matches(ValueDecl *VD) const {
59-
// These are normally just `VD` and `name`, but not for accessors.
59+
// The declaration whose name and context we ought to match. Usually `VD`, but
60+
// when `VD` is an accessor, it becomes the accessor's storage instead.
6061
auto lookupVD = VD;
6162

6263
// First, we check if the accessor-ness of `VD` matches the accessor-ness of
@@ -120,17 +121,17 @@ void AccessNoteDeclName::dump() const {
120121
llvm::errs() << '\n';
121122
}
122123

123-
NullablePtr<const AccessNote> AccessNotes::lookup(ValueDecl *VD) const {
124+
NullablePtr<const AccessNote> AccessNotesFile::lookup(ValueDecl *VD) const {
124125
assert(VD != nullptr);
125126

126-
auto iter = llvm::find_if(notes, [&](const AccessNote &note) -> bool {
127-
return note.name.matches(VD);
127+
auto iter = llvm::find_if(Notes, [&](const AccessNote &note) -> bool {
128+
return note.Name.matches(VD);
128129
});
129130

130-
return NullablePtr<const AccessNote>(iter == notes.end() ? nullptr : &*iter);
131+
return NullablePtr<const AccessNote>(iter == Notes.end() ? nullptr : &*iter);
131132
}
132133

133-
void AccessNotes::dump() const {
134+
void AccessNotesFile::dump() const {
134135
dump(llvm::dbgs());
135136
llvm::dbgs() << "\n";
136137
}
@@ -139,20 +140,21 @@ void AccessNote::dump() const {
139140
llvm::dbgs() << "\n";
140141
}
141142

142-
void AccessNotes::dump(llvm::raw_ostream &os) const {
143-
os << "(access_notes reason='" << reason << "'";
144-
for (const auto &note : notes) {
143+
void AccessNotesFile::dump(llvm::raw_ostream &os) const {
144+
os << "(access_notes reason='" << Reason << "'";
145+
for (const auto &note : Notes) {
145146
os << "\n";
146147
note.dump(os, /*indent=*/2);
147148
}
149+
os << ")";
148150
}
149151

150152
void AccessNote::dump(llvm::raw_ostream &os, int indent) const {
151153
os.indent(indent) << "(note name='";
152-
name.print(os);
154+
Name.print(os);
153155
os << "'";
154156

155-
if (name.name.getBaseName().isSpecial())
157+
if (Name.name.getBaseName().isSpecial())
156158
os << " is_special_name";
157159

158160
if (ObjC)
@@ -170,7 +172,7 @@ void AccessNote::dump(llvm::raw_ostream &os, int indent) const {
170172
LLVM_YAML_DECLARE_SCALAR_TRAITS(swift::AccessNoteDeclName, QuotingType::Single);
171173
LLVM_YAML_DECLARE_SCALAR_TRAITS(swift::ObjCSelector, QuotingType::Single);
172174
LLVM_YAML_IS_SEQUENCE_VECTOR(swift::AccessNote)
173-
LLVM_YAML_DECLARE_MAPPING_TRAITS(swift::AccessNotes)
175+
LLVM_YAML_DECLARE_MAPPING_TRAITS(swift::AccessNotesFile)
174176

175177
// Not using macro to avoid validation issues.
176178
template <> struct llvm::yaml::MappingTraits<swift::AccessNote> {
@@ -196,19 +198,19 @@ struct AccessNoteYAMLContext {
196198
std::set<std::string> unknownKeys;
197199
};
198200

199-
llvm::Expected<AccessNotes>
200-
AccessNotes::load(ASTContext &ctx, llvm::MemoryBuffer *buffer) {
201+
llvm::Expected<AccessNotesFile>
202+
AccessNotesFile::load(ASTContext &ctx, llvm::MemoryBuffer *buffer) {
201203
llvm::Error errors = llvm::Error::success();
202204
AccessNoteYAMLContext yamlCtx = { ctx, {} };
203205

204206
llvm::yaml::Input yamlIn(buffer->getBuffer(), (void *)&yamlCtx,
205207
convertToErrorAndJoin, &errors);
206208

207-
AccessNotes notes;
209+
AccessNotesFile notes;
208210
yamlIn >> notes;
209211

210212
if (errors)
211-
return llvm::Expected<AccessNotes>(std::move(errors));
213+
return llvm::Expected<AccessNotesFile>(std::move(errors));
212214

213215
notes.unknownKeys = std::move(yamlCtx.unknownKeys);
214216

@@ -221,7 +223,7 @@ namespace llvm {
221223
namespace yaml {
222224

223225
using AccessNote = swift::AccessNote;
224-
using AccessNotes = swift::AccessNotes;
226+
using AccessNotesFile = swift::AccessNotesFile;
225227
using AccessNoteYAMLContext = swift::AccessNoteYAMLContext;
226228
using AccessNoteDeclName = swift::AccessNoteDeclName;
227229
using ObjCSelector = swift::ObjCSelector;
@@ -286,7 +288,7 @@ static void diagnoseUnknownKeys(IO &io, ArrayRef<StringRef> expectedKeys) {
286288
void MappingTraits<AccessNote>::mapping(IO &io, AccessNote &note) {
287289
diagnoseUnknownKeys(io, { "Name", "ObjC", "Dynamic", "ObjCName" });
288290

289-
io.mapRequired("Name", note.name);
291+
io.mapRequired("Name", note.Name);
290292
io.mapOptional("ObjC", note.ObjC);
291293
io.mapOptional("Dynamic", note.Dynamic);
292294
io.mapOptional("ObjCName", note.ObjCName);
@@ -303,11 +305,11 @@ StringRef MappingTraits<AccessNote>::validate(IO &io, AccessNote &note) {
303305
return "";
304306
}
305307

306-
void MappingTraits<AccessNotes>::mapping(IO &io, AccessNotes &notes) {
308+
void MappingTraits<AccessNotesFile>::mapping(IO &io, AccessNotesFile &notes) {
307309
diagnoseUnknownKeys(io, { "Reason", "Notes" });
308310

309-
io.mapRequired("Reason", notes.reason);
310-
io.mapRequired("Notes", notes.notes);
311+
io.mapRequired("Reason", notes.Reason);
312+
io.mapRequired("Notes", notes.Notes);
311313
}
312314

313315
}

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,10 +951,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
951951
swift::vfs::getFileOrSTDIN(getFileSystem(), accessNotesPath));
952952

953953
auto expectedAccessNotes =
954-
flatMap<AccessNotes, std::unique_ptr<llvm::MemoryBuffer>>(
954+
flatMap<AccessNotesFile, std::unique_ptr<llvm::MemoryBuffer>>(
955955
std::move(expectedBuffer),
956956
[&](auto &&buffer) -> auto {
957-
return AccessNotes::load(*Context, buffer.get());
957+
return AccessNotesFile::load(*Context, buffer.get());
958958
});
959959

960960
if (expectedAccessNotes) {

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,8 @@ void TypeChecker::diagnoseDuplicateCaptureVars(CaptureListExpr *expr) {
14471447
}
14481448

14491449
template<typename ...DiagIDAndArgs> InFlightDiagnostic
1450-
diagnoseForAccessNote(DeclAttribute *attr, ValueDecl *VD,
1451-
DiagIDAndArgs... idAndArgs) {
1450+
diagnoseAtAttrOrDecl(DeclAttribute *attr, ValueDecl *VD,
1451+
DiagIDAndArgs... idAndArgs) {
14521452
ASTContext &ctx = VD->getASTContext();
14531453
if (attr->getLocation().isValid())
14541454
return ctx.Diags.diagnose(attr->getLocation(), idAndArgs...);
@@ -1457,7 +1457,7 @@ diagnoseForAccessNote(DeclAttribute *attr, ValueDecl *VD,
14571457
}
14581458

14591459
template <typename Attr>
1460-
static void addOrRemoveAttr(ValueDecl *VD, const AccessNotes &notes,
1460+
static void addOrRemoveAttr(ValueDecl *VD, const AccessNotesFile &notes,
14611461
Optional<bool> expected,
14621462
llvm::function_ref<Attr*()> willCreate) {
14631463
if (!expected) return;
@@ -1470,9 +1470,9 @@ static void addOrRemoveAttr(ValueDecl *VD, const AccessNotes &notes,
14701470
Diag<bool> fixitID) -> InFlightDiagnostic {
14711471
bool isModifier = attr->isDeclModifier();
14721472

1473-
diagnoseForAccessNote(attr, VD, diagID, notes.reason, isModifier,
1473+
diagnoseAtAttrOrDecl(attr, VD, diagID, notes.Reason, isModifier,
14741474
attr->getAttrName(), VD->getDescriptiveKind());
1475-
return diagnoseForAccessNote(attr, VD, fixitID, isModifier);
1475+
return diagnoseAtAttrOrDecl(attr, VD, fixitID, isModifier);
14761476
};
14771477

14781478
if (*expected) {
@@ -1496,10 +1496,10 @@ static void addOrRemoveAttr(ValueDecl *VD, const AccessNotes &notes,
14961496
}
14971497

14981498
static void applyAccessNote(ValueDecl *VD, const AccessNote &note,
1499-
const AccessNotes &notes) {
1499+
const AccessNotesFile &notes) {
15001500
ASTContext &ctx = VD->getASTContext();
15011501

1502-
addOrRemoveAttr<ObjCAttr>(VD, notes, note.ObjC, [&]() {
1502+
addOrRemoveAttr<ObjCAttr>(VD, notes, note.ObjC, [&]{
15031503
return ObjCAttr::create(ctx, note.ObjCName, false);
15041504
});
15051505

@@ -1513,18 +1513,18 @@ static void applyAccessNote(ValueDecl *VD, const AccessNote &note,
15131513

15141514
if (!attr->hasName()) {
15151515
attr->setName(*note.ObjCName, true);
1516-
diagnoseForAccessNote(attr, VD,
1517-
diag::attr_objc_name_changed_by_access_note,
1518-
notes.reason, VD->getDescriptiveKind(),
1519-
*note.ObjCName);
1516+
diagnoseAtAttrOrDecl(attr, VD,
1517+
diag::attr_objc_name_changed_by_access_note,
1518+
notes.Reason, VD->getDescriptiveKind(),
1519+
*note.ObjCName);
15201520

15211521
SmallString<64> newNameString;
15221522
llvm::raw_svector_ostream os(newNameString);
15231523
os << "(";
15241524
os << *note.ObjCName;
15251525
os << ")";
15261526

1527-
auto note = diagnoseForAccessNote(attr, VD,
1527+
auto note = diagnoseAtAttrOrDecl(attr, VD,
15281528
diag::fixit_attr_objc_name_changed_by_access_note);
15291529

15301530
if (attr->getLParenLoc().isValid())
@@ -1534,17 +1534,17 @@ static void applyAccessNote(ValueDecl *VD, const AccessNote &note,
15341534
note.fixItInsertAfter(attr->getLocation(), newNameString);
15351535
}
15361536
else if (attr->getName() != *note.ObjCName) {
1537-
diagnoseForAccessNote(attr, VD,
1538-
diag::attr_objc_name_conflicts_with_access_note,
1539-
notes.reason, VD->getDescriptiveKind(),
1540-
*note.ObjCName, *attr->getName());
1537+
diagnoseAtAttrOrDecl(attr, VD,
1538+
diag::attr_objc_name_conflicts_with_access_note,
1539+
notes.Reason, VD->getDescriptiveKind(),
1540+
*note.ObjCName, *attr->getName());
15411541
}
15421542
}
15431543
}
15441544

15451545
evaluator::SideEffect
15461546
ApplyAccessNoteRequest::evaluate(Evaluator &evaluator, ValueDecl *VD) const {
1547-
AccessNotes &notes = VD->getModuleContext()->getAccessNotes();
1547+
AccessNotesFile &notes = VD->getModuleContext()->getAccessNotes();
15481548
if (auto note = notes.lookup(VD))
15491549
applyAccessNote(VD, *note.get(), notes);
15501550
return {};

0 commit comments

Comments
 (0)