-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathImportName.h
558 lines (467 loc) · 19.9 KB
/
ImportName.h
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//===--- ImportName.h - Imported Swift names for Clang decls ----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
//
// This file provides class definitions for naming-related concerns in the
// ClangImporter.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IMPORT_NAME_H
#define SWIFT_IMPORT_NAME_H
#include "ImportEnumInfo.h"
#include "SwiftLookupTable.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Basic/Version.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ForeignAsyncConvention.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "clang/Sema/Sema.h"
namespace swift {
namespace importer {
struct PlatformAvailability;
/// The kind of accessor that an entity will be imported as.
enum class ImportedAccessorKind : unsigned {
None = 0,
PropertyGetter,
PropertySetter,
SubscriptGetter,
SubscriptSetter,
DereferenceGetter,
DereferenceSetter,
};
enum { NumImportedAccessorKindBits = 3 };
/// The name version
class ImportNameVersion : public RelationalOperationsBase<ImportNameVersion> {
unsigned rawValue : 31;
unsigned concurrency : 1;
friend llvm::DenseMapInfo<ImportNameVersion>;
enum AsConstExpr_t { AsConstExpr };
constexpr ImportNameVersion() : rawValue(0), concurrency(false) {}
constexpr ImportNameVersion(unsigned version, AsConstExpr_t)
: rawValue(version), concurrency(false) {}
explicit ImportNameVersion(unsigned version, bool concurrency = false)
: rawValue(version), concurrency(concurrency) {
assert(version >= 2 && "only Swift 2 and later are supported");
}
public:
/// Map a language version into an import name version.
static ImportNameVersion fromOptions(const LangOptions &langOpts) {
// We encode the 'rawValue' as just major version numbers with the
// exception of '4.2', which is a special minor version that can impact
// importing of names. We treat that with a rawValue of 5, and treat
// all major values of 5 or higher as being rawValue = majorversion + 1.
const auto &version = langOpts.EffectiveLanguageVersion;
// If the effective version is 4.x, where x >= 2, the import version
// is 4.2.
if (version.size() > 1 && version[0] == 4 && version[1] >= 2) {
return ImportNameVersion::swift4_2();
}
unsigned major = version[0];
return ImportNameVersion(major >= 5 ? major + 1 : major, false);
}
unsigned majorVersionNumber() const {
assert(*this != ImportNameVersion::raw());
if (*this == ImportNameVersion::swift4_2())
return 4;
return rawValue < 5 ? rawValue : rawValue - 1;
}
unsigned minorVersionNumber() const {
assert(*this != ImportNameVersion::raw());
if (*this == ImportNameVersion::swift4_2())
return 2;
return 0;
}
llvm::VersionTuple asClangVersionTuple() const {
assert(*this != ImportNameVersion::raw());
return llvm::VersionTuple(majorVersionNumber(), minorVersionNumber());
}
/// Whether to consider importing functions as 'async'.
bool supportsConcurrency() const { return concurrency; }
ImportNameVersion withConcurrency(bool concurrency) const {
ImportNameVersion result = *this;
result.concurrency = concurrency;
return result;
}
bool operator==(ImportNameVersion other) const {
return rawValue == other.rawValue && concurrency == other.concurrency;
}
bool operator<(ImportNameVersion other) const {
return rawValue < other.rawValue ||
(rawValue == other.rawValue && concurrency < other.concurrency);
}
/// Calls \p action for each name version other than this one, first going
/// backwards until ImportNameVersion::raw(), and then going forwards to
/// ImportNameVersion::maxVersion().
///
/// This is the most useful order for importing compatibility stubs.
void forEachOtherImportNameVersion(
llvm::function_ref<void(ImportNameVersion)> action) const {
assert(*this >= ImportNameVersion::swift2());
ImportNameVersion nameVersion = *this;
assert(!nameVersion.supportsConcurrency());
// Consider concurrency imports.
action(nameVersion.withConcurrency(true));
while (nameVersion > ImportNameVersion::swift2()) {
--nameVersion.rawValue;
action(nameVersion);
}
action(ImportNameVersion::raw());
nameVersion = *this;
while (nameVersion < ImportNameVersion::maxVersion()) {
++nameVersion.rawValue;
action(nameVersion);
}
}
/// Names as they appear in C/ObjC.
static constexpr inline ImportNameVersion raw() {
return ImportNameVersion{};
}
/// Names as they appeared in Swift 2 family.
static constexpr inline ImportNameVersion swift2() {
return ImportNameVersion{2, AsConstExpr};
}
/// Names as they appeared in Swift 4.2 family.
static constexpr inline ImportNameVersion swift4_2() {
return ImportNameVersion{5, AsConstExpr};
}
/// The latest supported version.
///
/// FIXME: All other version information is in Version.h. Can this go there
/// instead?
static constexpr inline ImportNameVersion maxVersion() {
return ImportNameVersion{6, AsConstExpr};
}
/// The version which should be used for importing types, which need to have
/// one canonical definition.
///
/// FIXME: Is this supposed to be the /newest/ version, or a canonical
/// version that lasts forever as part of the ABI?
static constexpr inline ImportNameVersion forTypes() {
return ImportNameVersion::maxVersion();
}
};
/// Describes a name that was imported from Clang.
class ImportedName {
friend class NameImporter;
/// The imported name.
DeclName declName;
/// The context into which this declaration will be imported.
///
/// When the context into which the declaration will be imported
/// matches a Clang declaration context (the common case), the
/// result will be expressed as a declaration context. Otherwise,
/// if the Clang type is not itself a declaration context (for
/// example, a typedef that comes into Swift as a strong type),
/// the type declaration will be provided.
EffectiveClangContext effectiveContext;
struct Info {
/// For names that map Objective-C error handling conventions into
/// throwing Swift methods, describes how the mapping is performed.
ForeignErrorConvention::Info errorInfo;
/// For names that map Objective-C completion handlers into async
/// Swift methods, describes how the mapping is performed.
ForeignAsyncConvention::Info asyncInfo;
/// For a declaration name that makes the declaration into an
/// instance member, the index of the "Self" parameter.
unsigned selfIndex;
/// For an initializer, the kind of initializer to import.
CtorInitializerKind initKind;
/// What kind of accessor this name refers to, if any.
ImportedAccessorKind accessorKind : NumImportedAccessorKindBits;
/// Whether this name was explicitly specified via a Clang
/// swift_name attribute.
unsigned hasCustomName : 1;
/// Whether this was one of a special class of Objective-C
/// initializers for which we drop the variadic argument rather
/// than refuse to import the initializer.
unsigned droppedVariadic : 1;
/// Whether this is a global being imported as a member
unsigned importAsMember : 1;
unsigned hasSelfIndex : 1;
unsigned hasErrorInfo : 1;
unsigned hasAsyncInfo : 1;
unsigned hasAsyncAlternateInfo: 1;
Info()
: errorInfo(), selfIndex(), initKind(CtorInitializerKind::Designated),
accessorKind(ImportedAccessorKind::None), hasCustomName(false),
droppedVariadic(false), importAsMember(false), hasSelfIndex(false),
hasErrorInfo(false), hasAsyncInfo(false),
hasAsyncAlternateInfo(false) {}
} info;
public:
ImportedName() = default;
/// Produce just the imported name, for clients that don't care
/// about the details.
DeclName getDeclName() const { return declName; }
operator DeclName() const { return getDeclName(); }
void setDeclName(DeclName name) { declName = name; }
/// The context into which this declaration will be imported.
EffectiveClangContext getEffectiveContext() const {
return effectiveContext;
}
void setEffectiveContext(EffectiveClangContext ctx) {
effectiveContext = ctx;
}
/// For an initializer, the kind of initializer to import.
CtorInitializerKind getInitKind() const { return info.initKind; }
/// What kind of accessor this name refers to, if any.
ImportedAccessorKind getAccessorKind() const { return info.accessorKind; }
/// For names that map Objective-C error handling conventions into
/// throwing Swift methods, describes how the mapping is performed.
std::optional<ForeignErrorConvention::Info> getErrorInfo() const {
if (info.hasErrorInfo)
return info.errorInfo;
return std::nullopt;
}
/// For names that map Objective-C methods with completion handlers into
/// async Swift methods, describes how the mapping is performed.
std::optional<ForeignAsyncConvention::Info> getAsyncInfo() const {
if (info.hasAsyncInfo) {
assert(!info.hasAsyncAlternateInfo
&& "both regular and alternate async info?");
return info.asyncInfo;
}
return std::nullopt;
}
/// For names with a variant that maps Objective-C methods with completion
/// handlers into async Swift methods, describes how the mapping is performed.
///
/// That is, if the method imports as both an async method and a completion
/// handler method, this value is set on the completion handler method's name
/// and gives you the contents of \c getAsyncInfo() on the async method's
/// name. It is not set on the async method's name, and it is not set if a
/// non-async method doesn't have an async equivalent.
std::optional<ForeignAsyncConvention::Info> getAsyncAlternateInfo() const {
if (info.hasAsyncAlternateInfo) {
assert(!info.hasAsyncInfo && "both regular and alternate async info?");
return info.asyncInfo;
}
return std::nullopt;
}
/// For a declaration name that makes the declaration into an
/// instance member, the index of the "Self" parameter.
std::optional<unsigned> getSelfIndex() const {
if (info.hasSelfIndex)
return info.selfIndex;
return std::nullopt;
}
/// Retrieve the base name as an identifier, including mapping special
/// names like 'init' or 'subscript' to identifiers.
Identifier getBaseIdentifier(ASTContext &ctx) const;
/// Whether this name was explicitly specified via a Clang
/// swift_name attribute.
bool hasCustomName() const { return info.hasCustomName; }
void setHasCustomName() { info.hasCustomName = true; }
/// Whether this was one of a special class of Objective-C
/// initializers for which we drop the variadic argument rather
/// than refuse to import the initializer.
bool droppedVariadic() const { return info.droppedVariadic; }
/// Whether this is a global being imported as a member
bool importAsMember() const { return info.importAsMember; }
/// Whether any name was imported.
explicit operator bool() const { return static_cast<bool>(declName); }
/// Whether this declaration is a property accessor (getter or setter).
bool isPropertyAccessor() const {
switch (getAccessorKind()) {
case ImportedAccessorKind::None:
case ImportedAccessorKind::SubscriptGetter:
case ImportedAccessorKind::SubscriptSetter:
case ImportedAccessorKind::DereferenceGetter:
case ImportedAccessorKind::DereferenceSetter:
return false;
case ImportedAccessorKind::PropertyGetter:
case ImportedAccessorKind::PropertySetter:
return true;
}
}
/// Whether this declaration is a subscript accessor (getter or setter).
bool isSubscriptAccessor() const {
switch (getAccessorKind()) {
case ImportedAccessorKind::None:
case ImportedAccessorKind::PropertyGetter:
case ImportedAccessorKind::PropertySetter:
case ImportedAccessorKind::DereferenceGetter:
case ImportedAccessorKind::DereferenceSetter:
return false;
case ImportedAccessorKind::SubscriptGetter:
case ImportedAccessorKind::SubscriptSetter:
return true;
}
llvm_unreachable("Invalid ImportedAccessorKind.");
}
bool isDereferenceAccessor() const {
switch (getAccessorKind()) {
case ImportedAccessorKind::None:
case ImportedAccessorKind::PropertyGetter:
case ImportedAccessorKind::PropertySetter:
case ImportedAccessorKind::SubscriptGetter:
case ImportedAccessorKind::SubscriptSetter:
return false;
case ImportedAccessorKind::DereferenceGetter:
case ImportedAccessorKind::DereferenceSetter:
return true;
}
llvm_unreachable("Invalid ImportedAccessorKind.");
}
};
/// Strips a trailing "Notification", if present. Returns {} if name doesn't end
/// in "Notification", or it there would be nothing left.
StringRef stripNotification(StringRef name);
/// Describes how a custom name was provided for 'async' import.
enum class CustomAsyncName {
/// No custom name was provided.
None,
/// A custom swift_name (but not swift_async_name) was provided.
SwiftName,
/// A custom swift_async_name was provided, which won't have a completion
/// handler argument label.
SwiftAsyncName,
};
/// Class to determine the Swift name of foreign entities. Currently fairly
/// stateless and borrows from the ClangImporter::Implementation, but in the
/// future will be more self-contained and encapsulated.
class NameImporter {
ASTContext &swiftCtx;
const PlatformAvailability &availability;
clang::Sema &clangSema;
EnumInfoCache enumInfos;
StringScratchSpace scratch;
// TODO: remove when we drop the options (i.e. import all names)
using CacheKeyType =
std::pair<const clang::NamedDecl *, ImportNameVersion>;
/// Cache for repeated calls
llvm::DenseMap<CacheKeyType, ImportedName> importNameCache;
/// The set of property names that show up in the defining module of
/// an Objective-C class.
llvm::DenseMap<std::pair<const clang::ObjCInterfaceDecl *, char>,
std::unique_ptr<InheritedNameSet>> allProperties;
bool importSymbolicCXXDecls;
public:
NameImporter(ASTContext &ctx, const PlatformAvailability &avail,
clang::Sema &cSema)
: swiftCtx(ctx), availability(avail), clangSema(cSema),
enumInfos(clangSema.getPreprocessor()),
importSymbolicCXXDecls(
ctx.LangOpts.hasFeature(Feature::ImportSymbolicCXXDecls)) {}
/// Determine the Swift name for a Clang decl
ImportedName importName(const clang::NamedDecl *decl,
ImportNameVersion version,
clang::DeclarationName preferredName =
clang::DeclarationName());
/// Attempts to import the name of \p decl with each possible
/// ImportNameVersion. \p action will be called with each unique name.
///
/// In this case, "unique" means either the full name is distinct or the
/// effective context is distinct. This method does not attempt to handle
/// "unresolved" contexts in any special way---if one name references a
/// particular Clang declaration and the other has an unresolved context that
/// will eventually reference that declaration, the contexts will still be
/// considered distinct.
///
/// If \p action returns false, the current name will \e not be added to the
/// set of seen names.
///
/// The active name for \p activeVersion is always first, followed by the
/// other names in the order of
/// ImportNameVersion::forEachOtherImportNameVersion.
///
/// Returns \c true if it fails to import name for the active version.
bool forEachDistinctImportName(
const clang::NamedDecl *decl, ImportNameVersion activeVersion,
llvm::function_ref<bool(ImportedName, ImportNameVersion)> action);
/// Imports the name of the given Clang macro into Swift.
Identifier importMacroName(const clang::IdentifierInfo *clangIdentifier,
const clang::MacroInfo *macro);
ASTContext &getContext() { return swiftCtx; }
const LangOptions &getLangOpts() const { return swiftCtx.LangOpts; }
Identifier getIdentifier(StringRef name) {
return swiftCtx.getIdentifier(name);
}
StringScratchSpace &getScratch() { return scratch; }
EnumInfo getEnumInfo(const clang::EnumDecl *decl) {
return enumInfos.getEnumInfo(decl);
}
EnumKind getEnumKind(const clang::EnumDecl *decl) {
return enumInfos.getEnumKind(decl);
}
clang::Sema &getClangSema() { return clangSema; }
clang::ASTContext &getClangContext() {
return getClangSema().getASTContext();
}
clang::Preprocessor &getClangPreprocessor() {
return getClangSema().getPreprocessor();
}
/// Retrieve the inherited name set for the given Objective-C class.
const InheritedNameSet *getAllPropertyNames(
clang::ObjCInterfaceDecl *classDecl,
bool forInstance);
inline void enableSymbolicImportFeature(bool isEnabled) {
importSymbolicCXXDecls = isEnabled;
}
private:
bool enableObjCInterop() const { return swiftCtx.LangOpts.EnableObjCInterop; }
/// Look for a method that will import to have the same name as the
/// given method after importing the Nth parameter as an elided error
/// parameter.
bool hasErrorMethodNameCollision(const clang::ObjCMethodDecl *method,
unsigned paramIndex,
StringRef suffixToStrip);
/// Test to see if there is a value with the same name as 'proposedName' in
/// the same module as the decl
bool hasNamingConflict(const clang::NamedDecl *decl,
const clang::IdentifierInfo *proposedName,
const clang::TypedefNameDecl *cfTypedef);
std::optional<ForeignErrorConvention::Info>
considerErrorImport(const clang::ObjCMethodDecl *clangDecl,
StringRef &baseName,
SmallVectorImpl<StringRef> ¶mNames,
ArrayRef<const clang::ParmVarDecl *> params,
bool isInitializer, bool hasCustomName);
std::optional<ForeignAsyncConvention::Info> considerAsyncImport(
const clang::ObjCMethodDecl *clangDecl, StringRef baseName,
SmallVectorImpl<StringRef> ¶mNames,
ArrayRef<const clang::ParmVarDecl *> params, bool isInitializer,
std::optional<unsigned> explicitCompletionHandlerParamIndex,
CustomAsyncName customName,
std::optional<unsigned> completionHandlerFlagParamIndex,
bool completionHandlerFlagIsZeroOnError,
std::optional<ForeignErrorConvention::Info> errorInfo);
EffectiveClangContext determineEffectiveContext(const clang::NamedDecl *,
const clang::DeclContext *,
ImportNameVersion version);
ImportedName importNameImpl(const clang::NamedDecl *,
ImportNameVersion version,
clang::DeclarationName);
};
}
}
namespace llvm {
// Provide DenseMapInfo for ImportNameVersion.
template <> struct DenseMapInfo<swift::importer::ImportNameVersion> {
using ImportNameVersion = swift::importer::ImportNameVersion;
using DMIU = DenseMapInfo<unsigned>;
static inline ImportNameVersion getEmptyKey() {
return (ImportNameVersion)DMIU::getEmptyKey();
}
static inline ImportNameVersion getTombstoneKey() {
return (ImportNameVersion)DMIU::getTombstoneKey();
}
static unsigned getHashValue(const ImportNameVersion &Val) {
return DMIU::getHashValue(Val.rawValue);
}
static bool isEqual(const ImportNameVersion &LHS,
const ImportNameVersion &RHS) {
return LHS == RHS;
}
};
}
#endif