Commit Graph

119 Commits

Author SHA1 Message Date
Joe Groff
b0e3c0be59 Merge pull request #4865 from jckarter/nsvalue-bridging
SE-0139: NSValue bridging
2016-09-22 09:13:47 -07:00
Joe Groff
86fbeee285 SE-0139: Bridge Cocoa framework structs to NSValue.
For every struct type for which the frameworks provides an NSValue category for boxing and unboxing values of that type, provide an _ObjectiveCBridgeable conformance in the Swift overlay that bridges that struct to NSValue, allowing the structs to be used naturally with id-as-Any APIs and Cocoa container classes. This is mostly a matter of gyb-ing out boilerplate using `NSValue.init(bytes:objCType:)` to construct the instance, `NSValue.objCType` to check its type when casting, and `NSValue.getValue(_:)` to extract the unboxed value, though there are a number of special snowflake cases that need special accommodation:

- To maintain proper layering, CoreGraphics structs need to be bridged in the Foundation overlay.
- AVFoundation provides the NSValue boxing categories for structs owned by CoreMedia, but it does so using its own internal subclasses of NSValue, and these subclasses do not interop properly with the standard `NSValue` subclasses instantiated by Foundation. To do the right thing, we therefore have to let AVFoundation provide the bridging implementation for the CoreMedia types, and we have to use its category methods to do so.
- SceneKit provides NSValue categories to box and unbox SCNVector3, SCNVector4, and SCNMatrix4; however, the methods it provides do so in an unusual way. SCNVector3 and SCNVector4 are packaged into `CGRect`s and then the CGRect is boxed using `valueWithCGRect:`. SCNMatrix4 is copied into a CATransform3D, which is then boxed using `valueWithCATransform3D:` from CoreAnimation. To be consistent with what SceneKit does, use its category methods for these types as well, and when casting, check the type against the type encoding SceneKit uses rather than the type encoding of the expected type.
2016-09-21 19:26:10 -07:00
Doug Gregor
8da7d7c156 [SIL Optimizer] Determine feasibility of dynamic casts between tuple types.
The SIL optimizer logic that determined feasability of dynamic casts
completely ignored tuple types, therefore assuming that they would
always fail. Check for structural identity, ignoring adding/removing
labels. Fixes rdar://problem/28121915.
2016-09-20 16:16:33 -07:00
John McCall
34fb15e375 Abstract the object type of an optional type according to the
abstraction pattern of the type rather than always using the
most-general pattern, and erase ImplicitlyUnwrappedOptional from
the SIL type system.
2016-09-08 23:26:19 -07:00
John McCall
219d093ef4 Fix the cast optimizer to handle CF/NS bridging correctly,
and fix the dynamic cast runtime to handle class + enum casts
to AnyHashable correctly.
2016-08-23 07:22:28 -07:00
Doug Gregor
51529ae888 Eliminate the -enable-id-as-any flag; it's always on now anyway.
Simplify e.g., ASTContext::getBridgedToObjC(), which no longer needs
the optional return.

Eliminate the now-unused constraint kind for checking bridging to
Objective-C.
2016-08-19 21:17:09 -07:00
John McCall
a6e1e87585 Add implicit conversions and casts from T:Hashable <-> AnyHashable.
rdar://27615802
2016-08-04 23:13:27 -07:00
John McCall
afdda3d107 Implement SE-0117.
One minor revision: this lifts the proposed restriction against
overriding a non-open method with an open one.  On reflection,
that was inconsistent with the existing rule permitting non-public
methods to be overridden with public ones.  The restriction on
subclassing a non-open class with an open class remains, and is
in fact consistent with the existing access rule.
2016-08-02 07:46:38 -07:00
Arnold Schwaighofer
f6e3f67293 Fix ASTContext::getBridgedToObjC to not return None under id-as-any
This would lead us to conclude that a check-cast-branch fails when it does not.

rdar://27536049
2016-07-26 14:19:03 -07:00
Jordan Rose
508e825ff2 Split 'fileprivate' and 'private', but give them the same behavior.
'fileprivate' is considered a broader level of access than 'private',
but for now both of them are still available to the entire file. This
is intended as a migration aid.

One interesting fallout of the "access scope" model described in
758cf64 is that something declared 'private' at file scope is actually
treated as 'fileprivate' for diagnostic purposes. This is something
we can fix later, once the full model is in place. (It's not really
/wrong/ in that they have identical behavior, but diagnostics still
shouldn't refer to a type explicitly declared 'private' as
'fileprivate'.)

As a note, ValueDecl::getEffectiveAccess will always return 'FilePrivate'
rather than 'Private'; for purposes of optimization and code generation,
we should never try to distinguish these two cases.

This should have essentially no effect on code that's /not/ using
'fileprivate' other than altered diagnostics.

Progress on SE-0025 ('fileprivate' and 'private')
2016-07-25 13:13:35 -07:00
Slava Pestov
57c58176bc AST: Remove noreturn bit from function types 2016-07-24 00:15:34 -07:00
John McCall
232a314a9f Teach the dynamic-cast machinery how to cast collection element types. 2016-07-23 10:30:10 -07:00
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
Joe Groff
396e4844ac SIL: Handle dynamic casts between ObjC generic classes.
The type parameters disappear at runtime, so consider this when classifying dynamic casts.
2016-05-23 10:23:25 -07:00
John McCall
44f71b1f11 Handle all the cases of scalar dynamic casts allowed by SIL in IRGen.
In particular, reliably look through a single level of optional
types and handle metatype-to-class casts.

Fixes rdar://24924966.
2016-04-06 19:10:02 -07:00
Doug Gregor
a31edf53d0 Simplify the interface to Module::lookupConformance.
Rather than returning a weird PointerIntPair, return an
Optional<ProtocolConformanceRef>. NFC
2016-03-15 22:08:24 -07:00
Max Moiseev
02006f20bc Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-09 16:05:03 -08:00
Joe Groff
77dd9b2992 Split exact-subclass and bindable-to-subclass queries.
In many places, we're interested in whether a type with archetypes *might be* a superclass of another type with the right bindings, particularly in the optimizer. Provide a separate Type::isBindableToSuperclassOf method that performs this check. Use it in the devirtualizer to fix rdar://problem/24993618. Using it might unblock other places where the optimizer is conservative, but we can fix those separately.
2016-03-09 11:14:45 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
Erik Eckstein
506ab9809f SIL: remove getTyp() from SILValue 2016-01-25 15:00:49 -08:00
Max Moiseev
08e1e4a043 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-11 16:51:11 -08:00
Erik Eckstein
6ff2f09796 [SIL] Let alloc_stack return a single value.
Having a separate address and container value returned from alloc_stack is not really needed in SIL.
Even if they differ we have both addresses available during IRGen, because a dealloc_stack is always dominated by the corresponding alloc_stack in the same function.

Although this commit quite large, most changes are trivial. The largest non-trivial change is in IRGenSIL.

This commit is a NFC regarding the generated code. Even the generated SIL is the same (except removed #0, #1 and @local_storage).
2016-01-06 17:35:27 -08:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
practicalswift
22e10737e2 Fix typos 2015-12-26 01:19:40 +01:00
Doug Gregor
a97ab6dd14 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2015-12-18 10:15:47 -08:00
practicalswift
8ab8847684 Fix typos. 2015-12-16 22:09:32 +01:00
Dmitri Gribenko
feacbc4433 Rename ErrorType to ErrorProtocol 2015-12-09 17:12:19 -08:00
Ben Langmuir
4082355244 Split KnownProtocolKind enum case from protocol name
This avoids us using reserved identifiers as the enum case names of all
our underscored protocols like _ObjectiveCBridgeable. I used the
convention PROTOCOL_WITH_NAME to mirror how the known identifiers work.

Swift SVN r32924
2015-10-27 23:10:36 +00:00
Roman Levenstein
05a51b752e [cast-optimizer] Small bug-fix to avoid a compiler crash.
It fixes a compiler crash which occurs on the following code if Foundation is not imported:
class C {}
let x: Int = C() as! Int

The crash happened because Int was considered ObjC-bridgeable, but its ObjC counterpart was not known as Foundation was not imported.

Swift SVN r32594
2015-10-10 05:06:06 +00:00
Slava Pestov
fd141bead9 Sema: Remove unused inExpression parameter from ASTContext::getBridgedToObjC(), NFC
Progress on <rdar://problem/21215099>.

Swift SVN r29968
2015-07-08 06:49:27 +00:00
Joe Groff
8b0e39d776 SIL: Casts from class types to potentially-class metatypes may succeed.
Another step toward fixing rdar://problem/16238475, though Sema-level warnings still need to be suppressed, and container bridging still needs to be exercised.

Swift SVN r29962
2015-07-08 05:03:28 +00:00
Joe Groff
e6a535a0ef The dice has been casted^H^H
Swift SVN r29960
2015-07-08 05:03:19 +00:00
Mark Lacey
d45acfb536 Fix typo in comment.
Swift SVN r29759
2015-06-27 04:42:24 +00:00
Joe Groff
6e9ffc72dd SIL: Dynamic casts from non-throws to throws may succeed.
Swift SVN r29361
2015-06-10 20:27:10 +00:00
Joe Groff
9c0695875e SIL: Casts that may be to NSError must use indirect cast instructions.
checked_cast_br promises to maintain RC identity, but a cast from an ErrorType-conforming class to NSError may change the RC identity by bridging. Make sure that potential class-to-NSError casts go through the indirect cast entry points for now. The runtime implementation still needs to be fixed to handle the class-to-NSError case, but this is part of rdar://problem/21116814.

Swift SVN r29089
2015-05-27 22:50:37 +00:00
Slava Pestov
9e5898e694 SIL: Don't crash when casting something known to not be a metatype to a metatype
eg,

class C {}
func bad(c: C) -> Bool { return c is C.Type }

Swift SVN r28731
2015-05-18 23:59:45 +00:00
Slava Pestov
d368700a6e SILGen: Object to metatype casts have to take the slow path
It looks like we don't know how to open-code the "is-a metatype" check
for scalar casts, so just tighten up canUseScalarCheckedCastInstructions()
so that if the target type is a metatype, the source also has to be a
metatype.

This fixes a regression from "SIL: Use scalar casts more".

Fixes <rdar://problem/21003923>.

Swift SVN r28729
2015-05-18 23:59:43 +00:00
Roman Levenstein
fa5109aaa1 [casts-optimizations] Properly handle casts to protocol compositions.
This fixes wrong results produced at run-time by interpreter/protocol_lookup.swift when it is compiled with optimizations.

Swift SVN r28721
2015-05-18 21:10:40 +00:00
Slava Pestov
085d1fee03 SIL: Use scalar casts more
Now that scalar casts work better, we can make the
canUseScalarCheckedCastInstructions() predicate a bit more
permissive.

This patch can be reverted if it is found that emitting scalar
casts in these additional cases introduces a regression.

Swift SVN r28713
2015-05-18 18:25:32 +00:00
Roman Levenstein
8dac375b4a Loosen the assertion a bit to make the test suite pass with -O.
Running the test quite with check-swift-optimize revealed that metatypes were not covered by the assert, even though it may occur in the real code.

Swift SVN r28297
2015-05-07 22:29:18 +00:00
Ted Kremenek
9f9bb725cf Rename '_ErrorType' to 'ErrorType'.
Swift SVN r28293
2015-05-07 21:59:29 +00:00
Doug Gregor
b8995b0aa3 Transform the Module class into ModuleDecl.
Modules occupy a weird space in the AST now: they can be treated like
types (Swift.Int), which is captured by ModuleType. They can be
treated like values for disambiguation (Swift.print), which is
captured by ModuleExpr. And we jump through hoops in various places to
store "either a module or a decl".

Start cleaning this up by transforming Module into ModuleDecl, a
TypeDecl that's implicitly created to describe a module. Subsequent
changes will start folding away the special cases (ModuleExpr ->
DeclRefExpr, name lookup results stop having a separate Module case,
etc.).

Note that the Module -> ModuleDecl typedef is there to limit the
changes needed. Much of this patch is actually dealing with the fact
that Module used to have Ctx and Name public members that now need to
be accessed via getASTContext() and getName(), respectively.

Swift SVN r28284
2015-05-07 21:10:50 +00:00
Doug Gregor
c180bfc423 Remove the SIL dynamic-cast checking's dependency on getProtocols(). NFC
Swift SVN r27977
2015-04-30 16:13:44 +00:00
Chris Lattner
7185252a2b remove dead argument to canUseScalarCheckedCastInstructions.
Swift SVN r27455
2015-04-18 22:12:50 +00:00
Roman Levenstein
1a58b365b7 Fix a bug in the casts optimizations.
DynamicCasts was not considering the fact that subclasses of a given class may implement a protocol, even if the class does not implement it.

Swift SVN r27265
2015-04-14 00:17:23 +00:00
Chris Willmore
0c52a76918 Revert "Fix two bugs related to the casts optimizations."
This reverts commit r27260, which broke the following tests:
* SILPasses/cast_folding.swift
* SILPasses/sil_combine_enum_addr.sil

Swift SVN r27261
2015-04-13 22:43:38 +00:00
Roman Levenstein
194aed3df2 Fix two bugs related to the casts optimizations.
The protocol_lookup the testcase from Interpreter test-cases exposed two bugs, once I tried to compiler with with -O:
 - SILCloner was generating an open_existential_ref from an open_existential_metatype instruction during cloning even if the existential in question was not a class existential.
 - DynamicCasts was not considering the fact that subclasses of a given class may implement a protocol, even if the class does not implement it.

Swift SVN r27260
2015-04-13 22:12:54 +00:00
Roman Levenstein
2bdba63740 Minor clean-ups and improvements as a preparation for the bridging casts optimizations.
Swift SVN r27099
2015-04-07 22:53:53 +00:00
Roman Levenstein
f63cae6f13 Add initial support into DynamicCasts for handling bridging between error types.
Teach it that casts between error types do not always fail and may succeed, in general.

Swift SVN r27098
2015-04-07 22:53:52 +00:00