Skip to content

[Clang importer] Refactor and centralize name mapping #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Dec 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c2bf16c
Clang importer: start building a Swift name -> Clang declarations table.
DougGregor Nov 19, 2015
a861a73
Clang importer: introduce "importFullName" to centralize name-mapping…
DougGregor Nov 20, 2015
3caf703
Narrow the Clang importer's importDeclName to just importIdentifier. NFC
DougGregor Nov 20, 2015
f798d53
Clang importer: convert more importName callers over to importFullNam…
DougGregor Nov 20, 2015
2dc262f
Clang importer: Don't add duplicate declarations to the Swift lookup …
DougGregor Nov 20, 2015
28dea3b
Clang importer: recursively add members to the Swift lookup tables.
DougGregor Nov 20, 2015
c41535a
Clang importer: start computing the effective lookup context with the…
DougGregor Nov 20, 2015
fa865c7
Clang importer: handle enumerator prefix stripping in importFullName().
DougGregor Nov 20, 2015
2244cb4
Swift lookup tables: start testing and cleaning up handling for Objec…
DougGregor Nov 21, 2015
5776fa5
Clang importer: migrate "Protocol" suffix computation into importFull…
DougGregor Nov 22, 2015
9ee502b
Clang importer: omit non-accessibility property getters/setters from …
DougGregor Nov 22, 2015
eedf0fc
Clang importer: teach importFullName to map init methods to initializ…
DougGregor Nov 22, 2015
ec91244
Clang importer: teach importFullName about factory methods as initial…
DougGregor Nov 30, 2015
fedb6a8
Clang importer: give importFullName a richer result type.
DougGregor Nov 30, 2015
91d1c3b
Clang importer: use importFullName for factory methods as initializers.
DougGregor Nov 30, 2015
13c1805
Clang importer: teach importFullName about dropping certain variadics.
DougGregor Nov 30, 2015
3102e23
Clang importer: switch init method importing over to importFullName.
DougGregor Nov 30, 2015
4c50598
Clang importer: switch method name import to importFullName().
DougGregor Nov 30, 2015
2d7044c
Clang importer: move name translation for throwing methods into impor…
DougGregor Dec 2, 2015
50eea9c
Clang importer: handle omit-needless-words for methods in importFullN…
DougGregor Dec 2, 2015
2ea58f9
Clang importer: handle CF type renaming in importFullName.
DougGregor Dec 2, 2015
d9ce07f
Clang importer: when dropping the variadic parameter, drop the variad…
DougGregor Dec 2, 2015
1e43256
Clang importer: Swift 2 lowercased subsequent argument names coming f…
DougGregor Dec 3, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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