-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathAccessNotes.cpp
281 lines (224 loc) · 8.09 KB
/
AccessNotes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//===--- AccessNotes.cpp - Access Notes -------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implements access notes, which allow certain modifiers or attributes to be
// added to the declarations in a module.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/AccessNotes.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Attr.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/Module.h" // DeclContext::isModuleScopeContext()
#include "swift/Basic/Assertions.h"
#include "swift/Parse/ParseDeclName.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/YAMLTraits.h"
namespace swift {
AccessNoteDeclName::AccessNoteDeclName()
: parentNames(), name(), accessorKind(std::nullopt) {}
AccessNoteDeclName::AccessNoteDeclName(ASTContext &ctx, StringRef str) {
auto parsedName = parseDeclName(str);
StringRef first, rest = parsedName.ContextName;
while (!rest.empty()) {
std::tie(first, rest) = rest.split('.');
parentNames.push_back(ctx.getIdentifier(first));
}
if (parsedName.IsGetter)
accessorKind = AccessorKind::Get;
else if (parsedName.IsSetter)
accessorKind = AccessorKind::Set;
else
accessorKind = std::nullopt;
name = parsedName.formDeclName(ctx, /*isSubscript=*/true);
}
bool AccessNoteDeclName::matches(ValueDecl *VD) const {
// The declaration whose name and context we ought to match. Usually `VD`, but
// when `VD` is an accessor, it becomes the accessor's storage instead.
auto lookupVD = VD;
// First, we check if the accessor-ness of `VD` matches the accessor-ness of
// the name, and update `lookupVD` if necessary.
if (auto accessor = dyn_cast<AccessorDecl>(VD)) {
if (!accessorKind || *accessorKind != accessor->getAccessorKind())
return false;
lookupVD = accessor->getStorage();
}
else if (accessorKind.has_value())
return false;
// Check that `name` matches `lookupVD`.
if (!lookupVD->getName().matchesRef(name))
return false;
// The rest of this checks `parentNames` against the parents of `lookupVD`.
ArrayRef<Identifier> remainingContextNames = parentNames;
DeclContext *nextContext = lookupVD->getDeclContext();
while (!nextContext->isModuleScopeContext()) {
// If we've run out of names without reaching module scope, we've failed.
if (remainingContextNames.empty())
return false;
Identifier contextName = remainingContextNames.back();
// If the context is not a type (or extension), we can't name VD in an
// access note and the match fails; if the name doesn't match, the match
// fails too.
auto contextType = nextContext->getSelfNominalTypeDecl();
if (!contextType || contextType->getName() != contextName)
return false;
// Still checking. Move to the parent.
remainingContextNames = remainingContextNames.drop_back();
nextContext = contextType->getParent();
}
// If the context is module-scoped, we've succeeded if we're out of names, or
// failed if we still have some names to go.
return remainingContextNames.empty();
}
bool AccessNoteDeclName::empty() const {
return !name;
}
void AccessNoteDeclName::print(llvm::raw_ostream &os) const {
if (accessorKind)
os << getAccessorLabel(*accessorKind) << "ter:";
for (auto parentName : parentNames)
os << parentName << '.';
name.print(os, /*skipEmptyArgumentNames=*/false);
}
void AccessNoteDeclName::dump() const {
print(llvm::errs());
llvm::errs() << '\n';
}
NullablePtr<const AccessNote> AccessNotesFile::lookup(ValueDecl *VD) const {
assert(VD != nullptr);
auto iter = llvm::find_if(Notes, [&](const AccessNote ¬e) -> bool {
return note.Name.matches(VD);
});
return NullablePtr<const AccessNote>(iter == Notes.end() ? nullptr : &*iter);
}
void AccessNotesFile::dump() const {
dump(llvm::dbgs());
llvm::dbgs() << "\n";
}
void AccessNote::dump() const {
dump(llvm::dbgs());
llvm::dbgs() << "\n";
}
void AccessNotesFile::dump(llvm::raw_ostream &os) const {
os << "(access_notes reason='" << Reason << "'";
for (const auto ¬e : Notes) {
os << "\n";
note.dump(os, /*indent=*/2);
}
os << ")";
}
void AccessNote::dump(llvm::raw_ostream &os, int indent) const {
os.indent(indent) << "(note name='";
Name.print(os);
os << "'";
if (Name.name.getBaseName().isSpecial())
os << " is_special_name";
if (ObjC)
os << " objc=" << *ObjC;
if (ObjCName)
os << " objc_name='" << *ObjCName << "'";
if (Dynamic)
os << " dynamic=" << *Dynamic;
os << ")";
}
}
LLVM_YAML_DECLARE_SCALAR_TRAITS(swift::AccessNoteDeclName, QuotingType::Single)
LLVM_YAML_DECLARE_SCALAR_TRAITS(swift::ObjCSelector, QuotingType::Single)
LLVM_YAML_IS_SEQUENCE_VECTOR(swift::AccessNote)
LLVM_YAML_DECLARE_MAPPING_TRAITS(swift::AccessNotesFile)
// Not using macro to avoid validation issues.
template <> struct llvm::yaml::MappingTraits<swift::AccessNote> {
static void mapping(IO &IO, swift::AccessNote &Obj);
static std::string validate(IO &IO, swift::AccessNote &Obj);
};
namespace swift {
static void
convertToErrorAndJoin(const llvm::SMDiagnostic &diag, void *ctxPtr) {
ASTContext &ctx = *(ASTContext*)ctxPtr;
SourceLoc loc{diag.getLoc()};
assert(ctx.SourceMgr.isOwning(loc));
switch (diag.getKind()) {
case llvm::SourceMgr::DK_Error:
ctx.Diags.diagnose(loc, diag::error_in_access_notes_file,
diag.getMessage());
break;
default:
ctx.Diags.diagnose(loc, diag::warning_in_access_notes_file,
diag.getMessage());
break;
}
}
std::optional<AccessNotesFile>
AccessNotesFile::load(ASTContext &ctx, const llvm::MemoryBuffer *buffer) {
llvm::yaml::Input yamlIn(llvm::MemoryBufferRef(*buffer), (void *)&ctx,
convertToErrorAndJoin, &ctx);
yamlIn.setAllowUnknownKeys(true);
AccessNotesFile notes;
yamlIn >> notes;
if (yamlIn.error())
return std::nullopt;
return notes;
}
}
namespace llvm {
namespace yaml {
using AccessNote = swift::AccessNote;
using AccessNotesFile = swift::AccessNotesFile;
using ASTContext = swift::ASTContext;
using AccessNoteDeclName = swift::AccessNoteDeclName;
using ObjCSelector = swift::ObjCSelector;
void ScalarTraits<AccessNoteDeclName>::
output(const AccessNoteDeclName &name, void *ctxPtr, raw_ostream &os) {
name.print(os);
}
StringRef ScalarTraits<AccessNoteDeclName>::
input(StringRef str, void *ctxPtr, AccessNoteDeclName &name) {
auto &ctx = *static_cast<swift::ASTContext *>(ctxPtr);
name = AccessNoteDeclName(ctx, str);
return name.empty() ? "invalid declaration name" : "";
}
void ScalarTraits<ObjCSelector>::output(const ObjCSelector &selector,
void *ctxPtr, raw_ostream &os) {
os << selector;
}
StringRef ScalarTraits<ObjCSelector>::input(StringRef str, void *ctxPtr,
ObjCSelector &selector) {
auto &ctx = *static_cast<swift::ASTContext *>(ctxPtr);
if (auto sel = ObjCSelector::parse(ctx, str)) {
selector = *sel;
return "";
}
return "invalid selector";
}
void MappingTraits<AccessNote>::mapping(IO &io, AccessNote ¬e) {
io.mapRequired("Name", note.Name);
io.mapOptional("ObjC", note.ObjC);
io.mapOptional("Dynamic", note.Dynamic);
io.mapOptional("ObjCName", note.ObjCName);
}
std::string MappingTraits<AccessNote>::validate(IO &io, AccessNote ¬e) {
if (note.ObjCName.has_value()) {
if (!note.ObjC)
note.ObjC = true;
else if (!*note.ObjC)
return "cannot have an 'ObjCName' if 'ObjC' is false";
}
return "";
}
void MappingTraits<AccessNotesFile>::mapping(IO &io, AccessNotesFile ¬es) {
io.mapRequired("Reason", notes.Reason);
io.mapRequired("Notes", notes.Notes);
}
}
}