Skip to content

Make all CF types Equatable and Hashable. #4394

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 1 commit into from
Aug 19, 2016
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
1 change: 1 addition & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ IDENTIFIER(atIndexedSubscript)
IDENTIFIER_(bridgeToObjectiveC)
IDENTIFIER_WITH_NAME(code_, "_code")
IDENTIFIER(CGFloat)
IDENTIFIER(CoreFoundation)
IDENTIFIER(CVarArg)
IDENTIFIER(Darwin)
IDENTIFIER(dealloc)
Expand Down
4 changes: 3 additions & 1 deletion include/swift/AST/KnownProtocols.def
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ PROTOCOL(Comparable)
PROTOCOL(Error)
PROTOCOL_(ErrorCodeProtocol)
PROTOCOL(OptionSet)

PROTOCOL_(BridgedNSError)
PROTOCOL_(BridgedStoredNSError)
PROTOCOL_(CFObject)
PROTOCOL_(SwiftNewtypeWrapper)

PROTOCOL_(ObjectiveCBridgeable)
PROTOCOL_(DestructorSafeContainer)
PROTOCOL_(SwiftNewtypeWrapper)

EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByArrayLiteral)
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByBooleanLiteral)
Expand Down
33 changes: 18 additions & 15 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,23 +927,26 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
// Find all of the declarations with this name in the appropriate module.
SmallVector<ValueDecl *, 1> results;

// _BridgedNSError, _BridgedStoredNSError, and _ErrorCodeProtocol
// are in the Foundation module.
if (kind == KnownProtocolKind::BridgedNSError ||
kind == KnownProtocolKind::BridgedStoredNSError ||
kind == KnownProtocolKind::ErrorCodeProtocol) {
Module *foundation =
const_cast<ASTContext *>(this)->getLoadedModule(Id_Foundation);
if (!foundation)
return nullptr;

auto identifier = getIdentifier(getProtocolName(kind));
foundation->lookupValue({ }, identifier, NLKind::UnqualifiedLookup,
results);
} else {
lookupInSwiftModule(getProtocolName(kind), results);
const Module *M;
switch (kind) {
case KnownProtocolKind::BridgedNSError:
case KnownProtocolKind::BridgedStoredNSError:
case KnownProtocolKind::ErrorCodeProtocol:
M = getLoadedModule(Id_Foundation);
break;
case KnownProtocolKind::CFObject:
M = getLoadedModule(Id_CoreFoundation);
break;
default:
M = getStdlibModule();
break;
}

if (!M)
return nullptr;
M->lookupValue({ }, getIdentifier(getProtocolName(kind)),
NLKind::UnqualifiedLookup, results);

for (auto result : results) {
if (auto protocol = dyn_cast<ProtocolDecl>(result)) {
Impl.KnownProtocols[index] = protocol;
Expand Down
37 changes: 25 additions & 12 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,9 +1279,14 @@ namespace {
addObjCAttribute(theClass, None);
Impl.registerExternalDecl(theClass);

SmallVector<ProtocolDecl *, 4> protocols;
theClass->getImplicitProtocols(protocols);
addObjCProtocolConformances(theClass, protocols);
auto *cfObjectProto =
Impl.SwiftContext.getProtocol(KnownProtocolKind::CFObject);
if (cfObjectProto) {
populateInheritedTypes(theClass, cfObjectProto, superclass);
auto *attr = new (Impl.SwiftContext) SynthesizedProtocolAttr(
KnownProtocolKind::CFObject);
theClass->getAttrs().add(attr);
}

// Look for bridging attributes on the clang record. We can
// just check the most recent redeclaration, which will inherit
Expand Down Expand Up @@ -2209,11 +2214,14 @@ namespace {
}

void populateInheritedTypes(NominalTypeDecl *nominal,
ArrayRef<ProtocolDecl *> protocols) {
ArrayRef<ProtocolDecl *> protocols,
Type superclass = Type()) {
SmallVector<TypeLoc, 4> inheritedTypes;
inheritedTypes.resize(protocols.size());
for_each(MutableArrayRef<TypeLoc>(inheritedTypes),
ArrayRef<ProtocolDecl *>(protocols),
inheritedTypes.resize(protocols.size() + (superclass ? 1 : 0));
if (superclass)
inheritedTypes[0] = TypeLoc::withoutLoc(superclass);
for_each(MutableArrayRef<TypeLoc>(inheritedTypes).slice(superclass?1:0),
protocols,
[](TypeLoc &tl, ProtocolDecl *proto) {
tl = TypeLoc::withoutLoc(proto->getDeclaredType());
});
Expand Down Expand Up @@ -4995,7 +5003,7 @@ namespace {
/// given vector, guarded by the known set of protocols.
void addProtocols(ProtocolDecl *protocol,
SmallVectorImpl<ProtocolDecl *> &protocols,
llvm::SmallPtrSet<ProtocolDecl *, 4> &known) {
llvm::SmallPtrSetImpl<ProtocolDecl *> &known) {
if (!known.insert(protocol).second)
return;

Expand All @@ -5005,6 +5013,13 @@ namespace {
addProtocols(inherited, protocols, known);
}

void addProtocols(ProtocolDecl *protocol,
SmallVectorImpl<ProtocolDecl *> &protocols) {
llvm::SmallPtrSet<ProtocolDecl *, 4> known(protocols.begin(),
protocols.end());
addProtocols(protocol, protocols, known);
}

// Import the given Objective-C protocol list, along with any
// implicitly-provided protocols, and attach them to the given
// declaration.
Expand All @@ -5013,16 +5028,14 @@ namespace {
SmallVectorImpl<TypeLoc> &inheritedTypes) {
SmallVector<ProtocolDecl *, 4> protocols;
llvm::SmallPtrSet<ProtocolDecl *, 4> knownProtocols;
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
if (auto nominal = dyn_cast<NominalTypeDecl>(decl))
nominal->getImplicitProtocols(protocols);
knownProtocols.insert(protocols.begin(), protocols.end());
}

for (auto cp = clangProtocols.begin(), cpEnd = clangProtocols.end();
cp != cpEnd; ++cp) {
if (auto proto = cast_or_null<ProtocolDecl>(Impl.importDecl(*cp,
false))) {
addProtocols(proto, protocols, knownProtocols);
addProtocols(proto, protocols);
inheritedTypes.push_back(
TypeLoc::withoutLoc(proto->getDeclaredType()));
}
Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,7 @@ SpecialProtocol irgen::getSpecialProtocolID(ProtocolDecl *P) {
case KnownProtocolKind::OptionSet:
case KnownProtocolKind::BridgedNSError:
case KnownProtocolKind::BridgedStoredNSError:
case KnownProtocolKind::CFObject:
case KnownProtocolKind::ErrorCodeProtocol:
return SpecialProtocol::None;
}
Expand Down
105 changes: 77 additions & 28 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/AST/TypeMatcher.h"
#include "swift/AST/TypeWalker.h"
#include "swift/Basic/Defer.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Sema/IDETypeChecking.h"
#include "llvm/ADT/ScopedHashTable.h"
#include "llvm/ADT/SmallString.h"
Expand Down Expand Up @@ -1139,39 +1140,87 @@ bool WitnessChecker::findBestWitness(ValueDecl *requirement,
unsigned &numViable,
unsigned &bestIdx,
bool &doNotDiagnoseMatches) {
auto witnesses = lookupValueWitnesses(requirement, ignoringNames);

// Match each of the witnesses to the requirement.
bool anyFromUnconstrainedExtension = false;
numViable = 0;
bestIdx = 0;

for (auto witness : witnesses) {
// Don't match anything in a protocol.
// FIXME: When default implementations come along, we can try to match
// these when they're default implementations coming from another
// (unrelated) protocol.
if (isa<ProtocolDecl>(witness->getDeclContext())) {
continue;
}
enum Attempt {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like too much of a special case -- can we generalize this somehow? Why are operators special? I see the operator in question is inside a type anyway, why isn't it found with the usual mechanisms?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operator lookup is always global right now. We can take it back out if/when that's no longer true or when it becomes a fallback path.

I want this to be narrow because there shouldn't be many cases where it happens; we can broaden it later if we need to.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll stop complaining. Let's tack this onto @DougGregor's todo list for his upcoming name lookup cleanup. :)

Regular,
OperatorsFromOverlay,
Done
};

if (!witness->hasType())
TC.validateDecl(witness, true);
SmallVector<ValueDecl *, 4> witnesses;
bool anyFromUnconstrainedExtension;

auto match = matchWitness(TC, Proto, conformance, DC,
requirement, witness);
if (match.isViable()) {
++numViable;
bestIdx = matches.size();
} else if (match.Kind == MatchKind::WitnessInvalid) {
doNotDiagnoseMatches = true;
for (Attempt attempt = Regular; attempt != Done;
attempt = static_cast<Attempt>(attempt + 1)) {
switch (attempt) {
case Regular:
witnesses = lookupValueWitnesses(requirement, ignoringNames);
break;
case OperatorsFromOverlay: {
// If we have a Clang declaration, the matching operator might be in the
// overlay for that module.
if (!requirement->isOperator())
continue;

auto *clangModule =
dyn_cast<ClangModuleUnit>(DC->getModuleScopeContext());
if (!clangModule)
continue;

DeclContext *overlay = clangModule->getAdapterModule();
if (!overlay)
continue;

auto lookupOptions = defaultUnqualifiedLookupOptions;
lookupOptions |= NameLookupFlags::KnownPrivate;
auto lookup = TC.lookupUnqualified(overlay, requirement->getName(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does qualified lookup not find operators inside types for some reason?

SourceLoc(), lookupOptions);
witnesses.clear();
for (auto candidate : lookup)
witnesses.push_back(candidate.Decl);
break;
}
case Done:
llvm_unreachable("should have exited loop");
}

// Match each of the witnesses to the requirement.
anyFromUnconstrainedExtension = false;
numViable = 0;
bestIdx = 0;

for (auto witness : witnesses) {
// Don't match anything in a protocol.
// FIXME: When default implementations come along, we can try to match
// these when they're default implementations coming from another
// (unrelated) protocol.
if (isa<ProtocolDecl>(witness->getDeclContext())) {
continue;
}

if (!witness->hasType())
TC.validateDecl(witness, true);

if (auto *ext = dyn_cast<ExtensionDecl>(match.Witness->getDeclContext()))
if (!ext->isConstrainedExtension() && ext->getAsProtocolExtensionContext())
anyFromUnconstrainedExtension = true;
auto match = matchWitness(TC, Proto, conformance, DC,
requirement, witness);
if (match.isViable()) {
++numViable;
bestIdx = matches.size();
} else if (match.Kind == MatchKind::WitnessInvalid) {
doNotDiagnoseMatches = true;
}

if (auto *ext = dyn_cast<ExtensionDecl>(match.Witness->getDeclContext())){
if (!ext->isConstrainedExtension() &&
ext->getAsProtocolExtensionContext()) {
anyFromUnconstrainedExtension = true;
}
}

matches.push_back(std::move(match));
matches.push_back(std::move(match));
}

if (numViable > 0)
break;
}

if (numViable == 0) {
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/SDK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_subdirectory(CloudKit)
add_subdirectory(Contacts)
add_subdirectory(CoreAudio)
add_subdirectory(CoreData)
add_subdirectory(CoreFoundation)
add_subdirectory(CoreGraphics)
add_subdirectory(CoreImage)
add_subdirectory(CoreLocation)
Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/SDK/CoreAudio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ add_swift_library(swiftCoreAudio ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR WATCHOS WATCHOS_SIMULATOR
SWIFT_MODULE_DEPENDS Dispatch
SWIFT_MODULE_DEPENDS Dispatch CoreFoundation
SWIFT_MODULE_DEPENDS_OSX IOKit
# Also depends on: CoreFoundation
FRAMEWORK_DEPENDS CoreAudio)

7 changes: 7 additions & 0 deletions stdlib/public/SDK/CoreFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_swift_library(swiftCoreFoundation ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
CoreFoundation.swift

SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
SWIFT_MODULE_DEPENDS ObjectiveC Dispatch os
FRAMEWORK_DEPENDS CoreFoundation)
23 changes: 23 additions & 0 deletions stdlib/public/SDK/CoreFoundation/CoreFoundation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@_exported import CoreFoundation

public protocol _CFObject: class, Hashable {}
extension _CFObject {
public var hashValue: Int {
return Int(bitPattern: CFHash(self))
}
public static func ==(left: Self, right: Self) -> Bool {
return CFEqual(left, right)
}
}
2 changes: 1 addition & 1 deletion stdlib/public/SDK/CoreGraphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_swift_library(swiftCoreGraphics ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_

SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
SWIFT_MODULE_DEPENDS ObjectiveC Dispatch Darwin
SWIFT_MODULE_DEPENDS ObjectiveC Dispatch Darwin CoreFoundation
SWIFT_MODULE_DEPENDS_OSX IOKit XPC
FRAMEWORK_DEPENDS CoreGraphics)

10 changes: 0 additions & 10 deletions stdlib/public/SDK/CoreGraphics/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ extension CGColor {
#endif
}

extension CGColor: Equatable {}
public func ==(lhs: CGColor, rhs: CGColor) -> Bool {
return lhs.__equalTo(rhs)
}


//===----------------------------------------------------------------------===//
// CGColorSpace
Expand Down Expand Up @@ -471,11 +466,6 @@ extension CGPath {
}
}

extension CGPath: Equatable {}
public func ==(lhs: CGPath, rhs: CGPath) -> Bool {
return lhs.__equalTo(rhs)
}

extension CGMutablePath {

public func addRoundedRect(in rect: CGRect, cornerWidth: CGFloat,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ add_swift_library(swiftFoundation ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SD

SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
SWIFT_MODULE_DEPENDS ObjectiveC CoreGraphics Dispatch os
SWIFT_MODULE_DEPENDS ObjectiveC CoreFoundation CoreGraphics Dispatch os
SWIFT_MODULE_DEPENDS_OSX XPC
FRAMEWORK_DEPENDS Foundation)

2 changes: 1 addition & 1 deletion stdlib/public/SDK/IOKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ add_swift_library(swiftIOKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVE
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS OSX
SWIFT_MODULE_DEPENDS ObjectiveC Dispatch
SWIFT_MODULE_DEPENDS ObjectiveC Dispatch CoreFoundation
FRAMEWORK_DEPENDS IOKit)
2 changes: 1 addition & 1 deletion test/ClangModules/Inputs/SwiftPrivateAttr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct NSOptions : OptionSet {
static var __privA: NSOptions { get }
static var B: NSOptions { get }
}
class __PrivCFType {
class __PrivCFType : _CFObject {
}
typealias __PrivCFSub = __PrivCFType
typealias __PrivInt = Int32
Loading