Skip to content

Commit c5950f6

Browse files
committed
Merge pull request swiftlang#98 from apple/clang-import-name-refactoring
[Clang importer] Refactor and centralize name mapping
2 parents ae24c99 + 1e43256 commit c5950f6

18 files changed

+2291
-1343
lines changed

include/swift/AST/Identifier.h

-28
Original file line numberDiff line numberDiff line change
@@ -438,34 +438,6 @@ class ObjCSelector {
438438
/// \param scratch Scratch space to use.
439439
StringRef getString(llvm::SmallVectorImpl<char> &scratch) const;
440440

441-
/// Ask whether this selector is a nullary selector (taking no
442-
/// arguments) whose name matches the given piece.
443-
bool isNullarySelector(StringRef piece) const {
444-
if (Storage.isSimpleName()) {
445-
return Storage.getBaseName().str() == piece;
446-
} else {
447-
return false;
448-
}
449-
}
450-
451-
/// Ask whether this selector is a non-nullary selector matching the
452-
/// given literal pieces.
453-
bool isNonNullarySelector(ArrayRef<StringRef> pieces) const {
454-
if (Storage.isSimpleName()) {
455-
return false;
456-
}
457-
458-
ArrayRef<Identifier> args = Storage.getArgumentNames();
459-
if (args.size() != pieces.size())
460-
return false;
461-
462-
for (size_t i = 0, e = args.size(); i != e; ++i) {
463-
if (args[i].str() != pieces[i])
464-
return false;
465-
}
466-
return true;
467-
}
468-
469441
void *getOpaqueValue() const { return Storage.getOpaqueValue(); }
470442
static ObjCSelector getFromOpaqueValue(void *p) {
471443
return ObjCSelector(DeclName::getFromOpaqueValue(p));

include/swift/Basic/StringExtras.h

+18-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ namespace swift {
5151
/// Determine the part of speech for the given word.
5252
PartOfSpeech getPartOfSpeech(StringRef word);
5353

54+
/// Scratch space used for returning a set of StringRefs.
55+
class StringScratchSpace {
56+
llvm::BumpPtrAllocator Allocator;
57+
58+
public:
59+
StringRef copyString(StringRef string);
60+
};
61+
5462
namespace camel_case {
5563
class WordIterator;
5664

@@ -219,6 +227,16 @@ namespace swift {
219227
/// unchanged.
220228
StringRef toLowercaseWord(StringRef string, SmallVectorImpl<char> &scratch);
221229

230+
/// Lowercase the first word within the given camelCase string.
231+
///
232+
/// \param string The string to lowercase.
233+
/// \param scratch Scratch buffer used to form the resulting string.
234+
///
235+
/// \returns the string with the first word lowercased. When the
236+
/// first word is an acronym, the string will be returned
237+
/// unchanged.
238+
StringRef toLowercaseWord(StringRef string, StringScratchSpace &scratch);
239+
222240
/// Sentence-case the given camelCase string by turning the first
223241
/// letter into an uppercase letter.
224242
///
@@ -358,14 +376,6 @@ struct OmissionTypeName {
358376
/// would produce "ByAppendingString".
359377
StringRef matchLeadingTypeName(StringRef name, OmissionTypeName typeName);
360378

361-
/// Scratch space used for returning a set of StringRefs.
362-
class StringScratchSpace {
363-
llvm::BumpPtrAllocator Allocator;
364-
365-
public:
366-
StringRef copyString(StringRef string);
367-
};
368-
369379
/// Describes a set of names with an inheritance relationship.
370380
class InheritedNameSet {
371381
const InheritedNameSet *Parent;

include/swift/ClangImporter/ClangImporter.h

+3
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ class ClangImporter final : public ClangModuleLoader {
259259
// Print statistics from the Clang AST reader.
260260
void printStatistics() const override;
261261

262+
/// Dump Swift lookup tables.
263+
void dumpSwiftLookupTables();
264+
262265
/// Given the path of a Clang module, collect the names of all its submodules
263266
/// and their corresponding visibility. Calling this function does not load the
264267
/// module.

include/swift/ClangImporter/ClangImporterOptions.h

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class ClangImporterOptions {
7373
// If true, infer default arguments for nullable pointers (nil) and
7474
// option sets ([]).
7575
bool InferDefaultArguments = false;
76+
77+
/// If true, we should use the Swift name lookup tables rather than
78+
/// Clang's name lookup facilities.
79+
bool UseSwiftLookupTables = false;
7680
};
7781

7882
} // end namespace swift

lib/Basic/StringExtras.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,18 @@ static bool isKeyword(StringRef identifier) {
309309
static Optional<StringRef> skipTypeSuffix(StringRef typeName) {
310310
if (typeName.empty()) return None;
311311

312+
auto lastWord = camel_case::getLastWord(typeName);
313+
312314
// "Type" suffix.
313-
if (camel_case::getLastWord(typeName) == "Type" &&
314-
typeName.size() > 4) {
315+
if (lastWord == "Type" && typeName.size() > 4) {
315316
return typeName.drop_back(4);
316317
}
317318

319+
// "Ref" suffix.
320+
if (lastWord == "Ref" && typeName.size() > 3) {
321+
return typeName.drop_back(3);
322+
}
323+
318324
// \d+D for dimensionality.
319325
if (typeName.back() == 'D' && typeName.size() > 1) {
320326
unsigned firstDigit = typeName.size() - 1;
@@ -436,9 +442,10 @@ bool InheritedNameSet::contains(StringRef name) const {
436442
}
437443

438444
/// Wrapper for camel_case::toLowercaseWord that uses string scratch space.
439-
static StringRef toLowercaseWord(StringRef string, StringScratchSpace &scratch){
445+
StringRef camel_case::toLowercaseWord(StringRef string,
446+
StringScratchSpace &scratch){
440447
llvm::SmallString<32> scratchStr;
441-
StringRef result = camel_case::toLowercaseWord(string, scratchStr);
448+
StringRef result = toLowercaseWord(string, scratchStr);
442449
if (string == result)
443450
return string;
444451

lib/ClangImporter/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_swift_library(swiftClangImporter
1414
ImportDecl.cpp
1515
ImportMacro.cpp
1616
ImportType.cpp
17+
SwiftLookupTable.cpp
1718
LINK_LIBRARIES
1819
swiftAST
1920
)

0 commit comments

Comments
 (0)