Skip to content

Revert some unnecessary changes from https://github.com/apple/swift/pull/26493/ #26640

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 2 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 19 additions & 7 deletions include/swift/AST/ClangModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#define SWIFT_AST_CLANG_MODULE_LOADER_H

#include "swift/AST/ModuleLoader.h"
#include "swift/Demangling/Demangle.h"

namespace clang {
class ASTContext;
Expand All @@ -28,11 +27,24 @@ namespace swift {
class DeclContext;
class VisibleDeclConsumer;

/// Represents the different namespaces for types in C.
///
/// A simplified version of clang::Sema::LookupKind.
enum class ClangTypeKind {
Typedef,
ObjCClass = Typedef,
/// Structs, enums, and unions.
Tag,
ObjCProtocol,
};

class ClangModuleLoader : public ModuleLoader {
private:
virtual void anchor();

protected:
using ModuleLoader::ModuleLoader;

public:
virtual clang::ASTContext &getClangASTContext() const = 0;
virtual clang::Preprocessor &getClangPreprocessor() const = 0;
Expand All @@ -56,9 +68,9 @@ class ClangModuleLoader : public ModuleLoader {
///
/// This routine is used for various hacks that are only permitted within
/// overlays of imported modules, e.g., Objective-C bridging conformances.
virtual bool isInOverlayModuleForImportedModule(
const DeclContext *overlayDC,
const DeclContext *importedDC) = 0;
virtual bool
isInOverlayModuleForImportedModule(const DeclContext *overlayDC,
const DeclContext *importedDC) = 0;

/// Look for declarations associated with the given name.
///
Expand All @@ -70,7 +82,7 @@ class ClangModuleLoader : public ModuleLoader {
/// Note that this method does no filtering. If it finds the type in a loaded
/// module, it returns it. This is intended for use in reflection / debugging
/// contexts where access is not a problem.
virtual void lookupTypeDecl(StringRef clangName, Demangle::Node::Kind kind,
virtual void lookupTypeDecl(StringRef clangName, ClangTypeKind kind,
llvm::function_ref<void(TypeDecl *)> receiver) {}

/// Look up type a declaration synthesized by the Clang importer itself, using
Expand All @@ -82,11 +94,11 @@ class ClangModuleLoader : public ModuleLoader {
/// module, it returns it. This is intended for use in reflection / debugging
/// contexts where access is not a problem.
virtual void
lookupRelatedEntity(StringRef clangName, StringRef relatedEntityKind,
lookupRelatedEntity(StringRef clangName, ClangTypeKind kind,
StringRef relatedEntityKind,
llvm::function_ref<void(TypeDecl *)> receiver) {}
};

} // namespace swift

#endif // LLVM_SWIFT_AST_CLANG_MODULE_LOADER_H

18 changes: 4 additions & 14 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ class TypeDecl;
class VisibleDeclConsumer;
enum class SelectorSplitKind;

/// Represents the different namespaces for types in C.
///
/// A simplified version of clang::Sema::LookupKind.
enum class ClangTypeKind {
Typedef,
ObjCClass = Typedef,
/// Structs, enums, and unions.
Tag,
ObjCProtocol,
};

/// Class that imports Clang modules into Swift, mapping directly
/// from Clang ASTs over to Swift ASTs.
class ClangImporter final : public ClangModuleLoader {
Expand Down Expand Up @@ -168,8 +157,8 @@ class ClangImporter final : public ClangModuleLoader {
/// Note that this method does no filtering. If it finds the type in a loaded
/// module, it returns it. This is intended for use in reflection / debugging
/// contexts where access is not a problem.
void lookupTypeDecl(StringRef clangName, Demangle::Node::Kind kind,
llvm::function_ref<void(TypeDecl*)> receiver) override;
void lookupTypeDecl(StringRef clangName, ClangTypeKind kind,
llvm::function_ref<void(TypeDecl *)> receiver) override;

/// Look up type a declaration synthesized by the Clang importer itself, using
/// a "related entity kind" to determine which type it should be. For example,
Expand All @@ -180,7 +169,8 @@ class ClangImporter final : public ClangModuleLoader {
/// module, it returns it. This is intended for use in reflection / debugging
/// contexts where access is not a problem.
void
lookupRelatedEntity(StringRef clangName, StringRef relatedEntityKind,
lookupRelatedEntity(StringRef clangName, ClangTypeKind kind,
StringRef relatedEntityKind,
llvm::function_ref<void(TypeDecl *)> receiver) override;

/// Look for textually included declarations from the bridging header.
Expand Down
6 changes: 2 additions & 4 deletions include/swift/DWARFImporter/DWARFImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/Module.h"
#include "swift/Demangling/Demangle.h"

namespace llvm {
}
Expand All @@ -43,8 +42,7 @@ class DWARFImporterDelegate {
virtual ~DWARFImporterDelegate() {}
/// Perform a qualified lookup of a Clang type with this name.
/// \param kind Only return results with this type kind.
virtual void lookupValue(StringRef name,
llvm::Optional<Demangle::Node::Kind> kind,
virtual void lookupValue(StringRef name, llvm::Optional<ClangTypeKind> kind,
SmallVectorImpl<clang::Decl *> &results) {}
};

Expand Down Expand Up @@ -111,7 +109,7 @@ class DWARFImporter final : public ClangModuleLoader {
NLKind lookupKind, SmallVectorImpl<ValueDecl *> &results);
/// Perform a qualified lookup of a Clang type with this name and only return
/// results with the specified type kind.
void lookupTypeDecl(StringRef rawName, Demangle::Node::Kind kind,
void lookupTypeDecl(StringRef rawName, ClangTypeKind kind,
llvm::function_ref<void(TypeDecl *)> receiver) override;
bool
isInOverlayModuleForImportedModule(const DeclContext *overlayDC,
Expand Down
28 changes: 25 additions & 3 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,23 @@ ASTBuilder::findTypeDecl(DeclContext *dc,
return result;
}

static Optional<ClangTypeKind>
getClangTypeKindForNodeKind(Demangle::Node::Kind kind) {
switch (kind) {
case Demangle::Node::Kind::Protocol:
return ClangTypeKind::ObjCProtocol;
case Demangle::Node::Kind::Class:
return ClangTypeKind::ObjCClass;
case Demangle::Node::Kind::TypeAlias:
return ClangTypeKind::Typedef;
case Demangle::Node::Kind::Structure:
case Demangle::Node::Kind::Enum:
return ClangTypeKind::Tag;
default:
return None;
}
}

GenericTypeDecl *ASTBuilder::findForeignTypeDecl(StringRef name,
StringRef relatedEntityKind,
ForeignModuleKind foreignKind,
Expand Down Expand Up @@ -1060,23 +1077,28 @@ GenericTypeDecl *ASTBuilder::findForeignTypeDecl(StringRef name,
consumer.foundDecl(found, DeclVisibilityKind::VisibleAtTopLevel);
};

Optional<ClangTypeKind> lookupKind = getClangTypeKindForNodeKind(kind);
if (!lookupKind)
return nullptr;

switch (foreignKind) {
case ForeignModuleKind::SynthesizedByImporter:
if (!relatedEntityKind.empty()) {
importer->lookupRelatedEntity(name, relatedEntityKind, found);
importer->lookupRelatedEntity(name, *lookupKind, relatedEntityKind,
found);
break;
}
importer->lookupValue(Ctx.getIdentifier(name), consumer);
if (consumer.Result)
consumer.Result = getAcceptableTypeDeclCandidate(consumer.Result, kind);
break;
case ForeignModuleKind::Imported:
importer->lookupTypeDecl(name, kind, found);
importer->lookupTypeDecl(name, *lookupKind, found);

// Try the DWARFImporter if it exists.
if (!consumer.Result)
if (auto *dwarf_importer = Ctx.getDWARFModuleLoader())
dwarf_importer->lookupTypeDecl(name, kind, found);
dwarf_importer->lookupTypeDecl(name, *lookupKind, found);
}

return consumer.Result;
Expand Down
30 changes: 5 additions & 25 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2495,34 +2495,14 @@ void ClangImporter::lookupValue(DeclName name, VisibleDeclConsumer &consumer){
});
}

static Optional<ClangTypeKind>
getClangTypeKindForNodeKind(Demangle::Node::Kind kind) {
switch (kind) {
case Demangle::Node::Kind::Protocol:
return ClangTypeKind::ObjCProtocol;
case Demangle::Node::Kind::Class:
return ClangTypeKind::ObjCClass;
case Demangle::Node::Kind::TypeAlias:
return ClangTypeKind::Typedef;
case Demangle::Node::Kind::Structure:
case Demangle::Node::Kind::Enum:
return ClangTypeKind::Tag;
default:
return None;
}
}

void ClangImporter::lookupTypeDecl(
StringRef rawName, Demangle::Node::Kind kind,
StringRef rawName, ClangTypeKind kind,
llvm::function_ref<void(TypeDecl *)> receiver) {
clang::DeclarationName clangName(
&Impl.Instance->getASTContext().Idents.get(rawName));

clang::Sema::LookupNameKind lookupKind;
auto clang_kind = getClangTypeKindForNodeKind(kind);
if (!clang_kind)
return;
switch (*clang_kind) {
switch (kind) {
case ClangTypeKind::Typedef:
lookupKind = clang::Sema::LookupOrdinaryName;
break;
Expand Down Expand Up @@ -2553,17 +2533,17 @@ void ClangImporter::lookupTypeDecl(
}

void ClangImporter::lookupRelatedEntity(
StringRef rawName, StringRef relatedEntityKind,
StringRef rawName, ClangTypeKind kind, StringRef relatedEntityKind,
llvm::function_ref<void(TypeDecl *)> receiver) {
using CISTAttr = ClangImporterSynthesizedTypeAttr;
if (relatedEntityKind ==
CISTAttr::manglingNameForKind(CISTAttr::Kind::NSErrorWrapper) ||
relatedEntityKind ==
CISTAttr::manglingNameForKind(CISTAttr::Kind::NSErrorWrapperAnon)) {
auto underlyingKind = Demangle::Node::Kind::Structure;
auto underlyingKind = ClangTypeKind::Tag;
if (relatedEntityKind ==
CISTAttr::manglingNameForKind(CISTAttr::Kind::NSErrorWrapperAnon)) {
underlyingKind = Demangle::Node::Kind::TypeAlias;
underlyingKind = ClangTypeKind::Typedef;
}
lookupTypeDecl(rawName, underlyingKind,
[this, receiver] (const TypeDecl *foundType) {
Expand Down
4 changes: 2 additions & 2 deletions lib/DWARFImporter/DWARFImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class DWARFImporter::Implementation {
}
}

void lookupTypeDecl(StringRef rawName, Demangle::Node::Kind kind,
void lookupTypeDecl(StringRef rawName, ClangTypeKind kind,
llvm::function_ref<void(TypeDecl *)> receiver) {
SmallVector<clang::Decl *, 1> decls;
delegate->lookupValue(rawName, kind, decls);
Expand Down Expand Up @@ -250,7 +250,7 @@ void DWARFImporter::lookupValue(ModuleDecl::AccessPathTy accessPath,
}

void DWARFImporter::lookupTypeDecl(
StringRef rawName, Demangle::Node::Kind kind,
StringRef rawName, ClangTypeKind kind,
llvm::function_ref<void(TypeDecl *)> receiver) {
Impl.lookupTypeDecl(rawName, kind, receiver);
}
Expand Down