Skip to content

Commit

Permalink
Merge pull request swiftlang#98 from apple/clang-import-name-refactoring
Browse files Browse the repository at this point in the history
[Clang importer] Refactor and centralize name mapping
  • Loading branch information
DougGregor committed Dec 3, 2015
2 parents ae24c99 + 1e43256 commit c5950f6
Show file tree
Hide file tree
Showing 18 changed files with 2,291 additions and 1,343 deletions.
28 changes: 0 additions & 28 deletions include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,34 +438,6 @@ class ObjCSelector {
/// \param scratch Scratch space to use.
StringRef getString(llvm::SmallVectorImpl<char> &scratch) const;

/// Ask whether this selector is a nullary selector (taking no
/// arguments) whose name matches the given piece.
bool isNullarySelector(StringRef piece) const {
if (Storage.isSimpleName()) {
return Storage.getBaseName().str() == piece;
} else {
return false;
}
}

/// Ask whether this selector is a non-nullary selector matching the
/// given literal pieces.
bool isNonNullarySelector(ArrayRef<StringRef> pieces) const {
if (Storage.isSimpleName()) {
return false;
}

ArrayRef<Identifier> args = Storage.getArgumentNames();
if (args.size() != pieces.size())
return false;

for (size_t i = 0, e = args.size(); i != e; ++i) {
if (args[i].str() != pieces[i])
return false;
}
return true;
}

void *getOpaqueValue() const { return Storage.getOpaqueValue(); }
static ObjCSelector getFromOpaqueValue(void *p) {
return ObjCSelector(DeclName::getFromOpaqueValue(p));
Expand Down
26 changes: 18 additions & 8 deletions include/swift/Basic/StringExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ namespace swift {
/// Determine the part of speech for the given word.
PartOfSpeech getPartOfSpeech(StringRef word);

/// Scratch space used for returning a set of StringRefs.
class StringScratchSpace {
llvm::BumpPtrAllocator Allocator;

public:
StringRef copyString(StringRef string);
};

namespace camel_case {
class WordIterator;

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

/// Lowercase the first word within the given camelCase string.
///
/// \param string The string to lowercase.
/// \param scratch Scratch buffer used to form the resulting string.
///
/// \returns the string with the first word lowercased. When the
/// first word is an acronym, the string will be returned
/// unchanged.
StringRef toLowercaseWord(StringRef string, StringScratchSpace &scratch);

/// Sentence-case the given camelCase string by turning the first
/// letter into an uppercase letter.
///
Expand Down Expand Up @@ -358,14 +376,6 @@ struct OmissionTypeName {
/// would produce "ByAppendingString".
StringRef matchLeadingTypeName(StringRef name, OmissionTypeName typeName);

/// Scratch space used for returning a set of StringRefs.
class StringScratchSpace {
llvm::BumpPtrAllocator Allocator;

public:
StringRef copyString(StringRef string);
};

/// Describes a set of names with an inheritance relationship.
class InheritedNameSet {
const InheritedNameSet *Parent;
Expand Down
3 changes: 3 additions & 0 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ class ClangImporter final : public ClangModuleLoader {
// Print statistics from the Clang AST reader.
void printStatistics() const override;

/// Dump Swift lookup tables.
void dumpSwiftLookupTables();

/// Given the path of a Clang module, collect the names of all its submodules
/// and their corresponding visibility. Calling this function does not load the
/// module.
Expand Down
4 changes: 4 additions & 0 deletions include/swift/ClangImporter/ClangImporterOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class ClangImporterOptions {
// If true, infer default arguments for nullable pointers (nil) and
// option sets ([]).
bool InferDefaultArguments = false;

/// If true, we should use the Swift name lookup tables rather than
/// Clang's name lookup facilities.
bool UseSwiftLookupTables = false;
};

} // end namespace swift
Expand Down
15 changes: 11 additions & 4 deletions lib/Basic/StringExtras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,18 @@ static bool isKeyword(StringRef identifier) {
static Optional<StringRef> skipTypeSuffix(StringRef typeName) {
if (typeName.empty()) return None;

auto lastWord = camel_case::getLastWord(typeName);

// "Type" suffix.
if (camel_case::getLastWord(typeName) == "Type" &&
typeName.size() > 4) {
if (lastWord == "Type" && typeName.size() > 4) {
return typeName.drop_back(4);
}

// "Ref" suffix.
if (lastWord == "Ref" && typeName.size() > 3) {
return typeName.drop_back(3);
}

// \d+D for dimensionality.
if (typeName.back() == 'D' && typeName.size() > 1) {
unsigned firstDigit = typeName.size() - 1;
Expand Down Expand Up @@ -436,9 +442,10 @@ bool InheritedNameSet::contains(StringRef name) const {
}

/// Wrapper for camel_case::toLowercaseWord that uses string scratch space.
static StringRef toLowercaseWord(StringRef string, StringScratchSpace &scratch){
StringRef camel_case::toLowercaseWord(StringRef string,
StringScratchSpace &scratch){
llvm::SmallString<32> scratchStr;
StringRef result = camel_case::toLowercaseWord(string, scratchStr);
StringRef result = toLowercaseWord(string, scratchStr);
if (string == result)
return string;

Expand Down
1 change: 1 addition & 0 deletions lib/ClangImporter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_swift_library(swiftClangImporter
ImportDecl.cpp
ImportMacro.cpp
ImportType.cpp
SwiftLookupTable.cpp
LINK_LIBRARIES
swiftAST
)
Expand Down
Loading

0 comments on commit c5950f6

Please sign in to comment.