Commit Graph

354 Commits

Author SHA1 Message Date
Joe Groff
b6823b930b SIL: Change SILType::subst to be SubstitutionMap-based.
This simplifies the SILType substitution APIs and brings them in line with Doug and Slava's refactorings to improve AST-level type substitution. NFC intended.
2016-12-14 14:33:32 -08:00
Michael Gottesman
96837babda Merge pull request #5920 from gottesmm/vacation_gardening
Vacation gardening
2016-11-25 09:17:21 -06:00
Michael Gottesman
0a8c54d04f [gardening] Always create new SILArguments using SILBasicBlock::createArgument instead of inline placement new.
The reasoning here is the same as in e42bf07.
2016-11-25 01:14:45 -06:00
Michael Gottesman
bf6920650c [gardening] Drop BB from all argument related code in SILBasicBlock.
Before this commit all code relating to handling arguments in SILBasicBlock had
somewhere in the name BB. This is redundant given that the class's name is
already SILBasicBlock. This commit drops those names.

Some examples:

getBBArg() => getArgument()
BBArgList => ArgumentList
bbarg_begin() => args_begin()
2016-11-25 01:14:36 -06:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Huon Wilson
5a08879d06 [AST] Remove GenericSignature params from GenericEnvironment methods.
The GenericEnvironment stores a signature internally.
2016-11-11 14:57:04 -08:00
Michael Gottesman
2ccc251888 [semantic-arc] In SILGen always assign a copy_value's argument to its result.
This ensures that ownership is properly propagated forward through the use-def
graph.

This was the work that was stymied by issues relating to SILBuilder performing
local ARC dataflow. I ripped out that local dataflow in 6f4e2ab and added a
cheap ARC guaranteed dataflow pass that performs the same optimization.

Also in the process of doing this work, I found that there were many SILGen
tests that were either pattern matching in the wrong functions or had wrong
CHECK lines (for instance CHECK_NEXT). I fixed all of these issues and also
expanded many of the tests so that they verify ownership. The only work I left
for a future PR is that there are certain places in tests where we are using the
projection from an original value, instead of a copy. I marked those with a
message SEMANTIC ARC TODO so that they are easy to find.

rdar://28685236
2016-11-06 23:17:17 -08:00
Michael Gottesman
e2419b75fd [semantic-arc] Qualify most of the stores in SILGen as store [init].
All of these cases were trivially inits since they involved storing into
a newly created temporary allocation.

rdar://28685236
2016-10-31 15:51:26 -07:00
Doug Gregor
99daad0f30 Rework witness matching for generic requirements.
Reimplement the witness matching logic used for generic requirements
so that it properly models the expectations required of the witness,
then captures the results in the AST. The new approach has a number of
advantages over the existing hacks:

* The constraint solver no longer requires hacks to try to tangle
  together the innermost archetypes from the requirement with the
  outer archetypes of the context of the protocol
  conformance. Instead, we create a synthetic set of archetypes that
  describes the requirement as it should be matched against
  witnesses. This eliminates the infamous 'SelfTypeVar' hack.
* The type checker no longer records substitutions involving a weird
  mix of archetypes from different contexts (see above), so it's
  actually plausible to reason about the substitutions of a witness. A
  new `Witness` class contains the declaration, substitutions, and all
  other information required to interpret the witness.
* SILGen now uses the substitution information for witnesses when
  building witness thunks, rather than computing all of it from
  scratch. ``substSelfTypeIntoProtocolRequirementType()` is now gone
  (absorbed into the type checker, and improved from there), and the
  witness-thunk emission code is simpler. A few other bits of SILGen
  got simpler because the substitutions can now be trusted.
* Witness matching and thunk generation involving generic requirements
  and nested generics now works, based on some work @slavapestov was
  already doing in this area.
* The AST verifier can now verify the archetypes that occur in witness substitutions.
* Although it's not in this commit, the `Witness` structure is
  suitable for complete (de-)serialization, unlike the weird mix of
  archetypes previously present.

Fixes rdar://problem/24079818 and cleans up an area that's been messy
and poorly understood for a very, very long time.
2016-10-30 23:15:43 -07:00
Michael Gottesman
bffa7addaf [semantic-arc] Eliminate default {Load,Store}OwnershipQualification argument to SILBuilder::create{Load,Store}(...)
Today, loads and stores are treated as having @unowned(unsafe) ownership
semantics. This leaves the user to specify ownership changes on the loaded or
stored value independently of the load/store by inserting ARC operations. With
the change to Semantic SIL, this will no longer be true. Instead loads, stores
have ownership semantics that one must reason about such as copy, take, and
trivial.

This change moves us closer to that world by eliminating the default
OwnershipQualification argument from create{Load,Store}. This means that the
compiler developer cannot ignore reasoning about the ownership semantics of the
memory operation that they are creating.

Operationally, this is a NFC change since I have just gone through the compiler
and updated all places where we create loads, stores to pass in the former
default argument ({Load,Store}OwnershipQualifier::Unqualified), to
SILBuilder::create{Load,Store}(...). For now, one can just do that in situations
where one needs to create loads/stores, but over time, I am going to tighten the
semantics up via the verifier.

rdar://28685236
2016-10-30 13:07:06 -07:00
Michael Gottesman
8b41745b37 [semantic-arc] Change TypeLowering APIs to use the words {Copy,Destroy}Value instead of {Retain,Release}Value. NFC.
This is an attempt to separate functional from superficial changes in the
semantic arc changes.

rdar://28851920
2016-10-24 23:49:11 -07:00
practicalswift
2ba2710cc7 [gardening] Fix recently introduced typo 2016-09-27 10:01:06 +02:00
Joe Groff
c698da6393 SILGen: Properly calculate substitutions to invoke _bridgeToObjectiveC.
The existing code assumed that the _bridgeToObjectiveC witness would never come from a protocol extension. Generating the correct set of substitutions is a bit harder than it ought to be, due to inconsistencies in how Sema represents generic witnesses. Fixes rdar://problem/28279269.
2016-09-24 08:43:18 -07:00
Erik Eckstein
c65c1b5dda Re-apply "SILGen: Only give bridging and re-abstraction thunks a generic signature if necessary"
The commit was reverted because of a regression in the
Prototypes/CollectionTransformers test. I believe the root
cause was an escape analysis bug, which is fixed in my
previous commit.
2016-09-21 23:42:02 -07:00
Joe Groff
1a52e3f2c2 SR-0140: Bridge Optionals to nonnull ObjC objects by bridging their payload, or using a sentinel.
id-as-Any lets you pass Optional to an ObjC API that takes `nonnull id`, and also lets you bridge containers of `Optional` to `NSArray` etc. When this occurs, we can unwrap the value and bridge it so that inhabited optionals still pass into ObjC in the expected way, but we need something to represent `none` other than the `nil` pointer. Cocoa provides `NSNull` as the canonical "null for containers" object, which is the least bad of many possible answers. If we happen to have the rare nested optional `T??`, there is no precedented analog for these in Cocoa, so just generate a unique sentinel object to preserve the `nil`-ness depth so we at least don't lose information round-tripping across the ObjC-Swift bridge.

Making Optional conform to _ObjectiveCBridgeable is more or less enough to make this all work, though there are a few additional edge case things that need to be fixed up. We don't want to accept `AnyObject??` as an @objc-compatible type, so special-case Optional in `getForeignRepresentable`.

Implements SR-0140 (rdar://problem/27905315).
2016-09-20 13:04:09 -07:00
Erik Eckstein
637fc63e09 Revert "SILGen: Only give bridging and re-abstraction thunks a generic signature if necessary"
This reverts commit 8c3d93503f.

It broke the Prototypes/CollectionTransformers.swift test
2016-09-17 16:35:02 -07:00
Slava Pestov
8c3d93503f SILGen: Only give bridging and re-abstraction thunks a generic signature if necessary
If the thunk's type otherwise did not involve type parameters, we
would still pass around the generic parameters from the caller's
context, which is wasteful.
2016-09-15 21:47:58 -07:00
Slava Pestov
b5decb6be4 SILGen: Fix some issues with @pseudogeneric thunks
This would manifest as crashes in IRGen when bridging a block
taking another block in generic context.

- When emitting a re-abstraction thunk, make it pseudogeneric if
  its parent function is pseudogeneric. This ensures we don't
  try to get runtime type metadata when it doesn't exist.

- Mangle pseudogeneric-ness of a reabstraction thunk correctly.
  Otherwise we could emit thunks with different signatures under
  the same mangling.

- Only set the pseudogeneric attribute if the thunk has a generic
  signature, since otherwise the mangling is no longer unique.

Fixes <rdar://problem/27718566>.
2016-09-15 21:45:52 -07:00
Greg Parker
cd9a8afd5d Revert "SR-0140: Bridge Optionals to nonnull ObjC objects by bridging their payload, or using a sentinel." (#4820) 2016-09-15 18:18:07 -07:00
Joe Groff
3441fe8ca4 Merge pull request #4782 from jckarter/bridging-enhancements
SR-0140: Bridge Optionals to nonnull ObjC objects by bridging their payload, or using a sentinel.
2016-09-15 13:06:01 -07:00
Joe Groff
cfa9cd9a08 SR-0140: Bridge Optionals to nonnull ObjC objects by bridging their payload, or using a sentinel.
id-as-Any lets you pass Optional to an ObjC API that takes `nonnull id`, and also lets you bridge containers of `Optional` to `NSArray` etc. When this occurs, we can unwrap the value and bridge it so that inhabited optionals still pass into ObjC in the expected way, but we need something to represent `none` other than the `nil` pointer. Cocoa provides `NSNull` as the canonical "null for containers" object, which is the least bad of many possible answers. If we happen to have the rare nested optional `T??`, there is no precedented analog for these in Cocoa, so just generate a unique sentinel object to preserve the `nil`-ness depth so we at least don't lose information round-tripping across the ObjC-Swift bridge.

Making Optional conform to _ObjectiveCBridgeable is more or less enough to make this all work, though there are a few additional edge case things that need to be fixed up. We don't want to accept `AnyObject??` as an @objc-compatible type, so special-case Optional in `getForeignRepresentable`.

Implements SR-0140 (rdar://problem/27905315).
2016-09-14 15:50:13 -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
Slava Pestov
ca0b548584 SIL: Replace SILFunction::ContextGenericParams with a GenericEnvironment
This patch is rather large, since it was hard to make this change
incrementally, but most of the changes are mechanical.

Now that we have a lighter-weight data structure in the AST for mapping
interface types to archetypes and vice versa, use that in SIL instead of
a GenericParamList.

This means that when serializing a SILFunction body, we no longer need to
serialize references to archetypes from other modules.

Several methods used for forming substitutions can now be moved from
GenericParamList to GenericEnvironment.

Also, GenericParamList::cloneWithOuterParameters() and
GenericParamList::getEmpty() can now go away, since they were only used
when SILGen-ing witness thunks.

Finally, when printing generic parameters with identical names, the
SIL printer used to number them from highest depth to lowest, by
walking generic parameter lists starting with the innermost one.
Now, ambiguous generic parameters are numbered from lowest depth
to highest, by walking the generic signature, which means test
output in one of the SILGen tests has changed.
2016-08-28 13:51:37 -07:00
Slava Pestov
b9b296b32a AST: Use GenericEnvironment in ArchetypeBuilder::mapType{Into,OutOf}Context()
When we call the DeclContext variants of these, use the
DeclContext's GenericEnvironment instead of GenericParamList.

Also, these functions would take a resolver argument, but we
always passed in nullptr, so just remove it now.

The old GenericParamList-based versions are still there
since they're called directly from SIL. They will go away
once SILFunction::ContextGenericParams is replaced with
a GenericEnvironment.
2016-08-28 13:51:37 -07:00
Slava Pestov
c6de555ef8 SILGen: Fix typo in comment, NFC 2016-08-22 10:45:48 -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
Joe Groff
171a2df979 Merge pull request #4378 from jckarter/nil-id-as-any
Handle inaccurate ObjC nullability in `id`-as-`Any` bridging.
2016-08-18 19:17:36 -07:00
Joe Groff
484429daa0 SILGen: Bridge id to Any using a stdlib helper.
Bitcast the AnyObject result to AnyObject?, then call our new helper function, so that we can handle nils without choking. Fixes rdar://problem/27874026.
2016-08-18 15:09:55 -07:00
Doug Gregor
f8e439c142 [SILGen] Correctly manage boxed opened existentials when bridging to Any.
Fixes SR-2380 / rdar://problem/27911827.
2016-08-18 13:45:14 -07:00
Joe Groff
9857824fe7 SILGen: Set context generic params for bridging thunks.
So that we do the right thing with thunks for generic ObjC methods. Partially fixes rdar://problem/27869089.
2016-08-16 11:40:39 -07:00
John McCall
b974fbeba4 Don't double-emit the source expression when erasing an archetype to Error.
This is still a pretty bad code-generation pattern, but the layering
stuff makes it challenging to do the right thing.

Also, bridge non-optional NSErrors to Error using init_existential_ref
instead of going through the runtime function.

rdar://27810321
2016-08-12 17:20:46 -07:00
Joe Groff
ee9675dc33 SILGen: Handle indirect result correctly in throwing foreign-to-native thunks. 2016-07-25 06:40:34 -07:00
Joe Groff
0ed7780fca SILGen: Remove paranoid assertions about non-Any indirect bridged types.
The code works for concrete address-only types too.
2016-07-25 06:01:21 -07:00
Joe Groff
34dd2b8ff8 SILGen: Handle indirect args in foreign-to-native thunks.
These come up with `dynamic` methods and initializers.
2016-07-25 06:01:21 -07:00
Joe Groff
e7f3e25733 SILGen: Relax assertions to admit Any? in bridging positions. 2016-07-19 18:08:14 -07:00
Joe Groff
2841b10d04 SILGen: Peephole Any bridging through optionals.
If a `T?` is converted to `Any?` to pass to a `_Nullable id` interface in ObjC, avoid the intermediate conversion and bridge straight to AnyObject? too.
2016-07-19 18:07:28 -07:00
Slava Pestov
16f60d8064 SILGen: Add bridging support for id-as-Any.
- Previously we didn't know how to bridge address-only types.  Add some
  plumbing for this. Not fully general yet, but with a bit more work we
  could allow resilient value types to adopt _ObjectiveCBridgable, too.
  For now, this is just intended to support the id-as-Any work.

- Specifically when going from a type without any other known bridging
  strategy, emit a call to the new `_bridgeAnythingToObjectiveC` entry
  point from the previous commit.
2016-07-12 14:34:25 -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
Doug Gregor
ca4a32b5a9 [SE-0112] Bridge NSError to ErrorProtocol.
Introduce bridging of NSError to ErrorProtocol, so an Objective-C API
expressed via an "NSError *" will be imported using ErrorProtocol in
the Swift. For example, the Objective-C method:

  - (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted;

will now be imported as:

  func handleError(_ error: ErrorProtocol, userInteractionPermitted: Bool)

This is bullet (3) under the proposed solution of SE-0112. Note that
we made one semantic change here: instead of removing the conformance
of NSError to ErrorProtocol, which caused numerous problems both
theoretical and actual because the model expects that an NSError
conforms to ErrorProtocol without requiring wrapping, we instead limit
the ErrorProtocol -> NSError conversion that would be implied by
bridging. This is defensible in the short term because it also
eliminates the implicit conversion, and aligns with SE-0072, which
eliminates implicit bridging conversions altogether.
2016-07-12 10:53:52 -07:00
Slava Pestov
c044889f32 SILGen: Remove redundant mapType{Into,OutOf}Context() calls 2016-07-02 05:39:23 -07:00
Slava Pestov
98a0e73b02 AST: Merge BoundGenericType::getSubstitutions() with TypeBase::gatherAllSubstitutions(), NFC 2016-06-13 00:57:09 -07:00
Joe Groff
9398ce9413 SILGen: Propagate generic types through func-to-block invoke functions.
Now that ObjC types can be generic, we need to satisfy the type system by plumbing pseudogeneric parameters through func-to-block invocation thunks. Fixes rdar://problem/26524763.
2016-06-01 19:37:43 -07:00
Joe Groff
f6723c325a AST/SILGen: Consider bridgeable value types to be bridgeable when bound to generic parameters.
We were failing to bridge Array<T> parameters in the signatures of Objective-C generics when their NSArray<T> * type in ObjC depended on generic parameters. This partially fixes rdar://problem/26371959, though IRGen still needs a fix to get us all the way through (which @rjmccall is working on).

(@DougGregor did all the work here, I'm just testing and committing.)
2016-05-25 14:46:08 -07:00
Joe Groff
bad77ab0bd SILGen: Contextualize parameter types in foreign-to-native thunks.
Now that foreign entry points may be imported as generic, we need to forward generic parameters when we thunk them. Fixes SR-1392.
2016-05-04 13:18:47 -07:00
Slava Pestov
2b6da60728 SILGen: Add asserts that bridged types are loadable
Bridging thunks don't yet support bridging address-only types, but
ideally they should, so that we can bridge Objective-C types to
resilient value types.

This came up while I was adding @_fixed_layout declarations in
the standard library. To make this easier to figure out in the
future, add the asserts to the bridging logic for now.

Also, to avoid hitting the asserts when we emit a reference to a
C function with an incompatible type, don't emit the foreign
function at all until we determine that the ABI conversion is safe.
2016-04-04 22:34:50 -07:00
Joe Groff
5161974f18 merge fix 2016-03-28 09:50:30 -07:00
Chris Willmore
3bcb169f0b Import lightweight Objective-C generics as Swift generic type
parameters.
2016-03-28 09:50:30 -07:00
Michael Ilseman
7867533f2c Merge commit 'b894798e461077b30a3aa4afe89639f312f44858' into import-as-member 2016-03-23 15:40:04 -07:00
Michael Ilseman
d7581d1b36 Merge commit 'a31edf53d0580efe47f4e9ef89dccc4429c056e8' into import-as-member 2016-03-23 13:05:57 -07:00
Joe Groff
5fe65fb0a5 SILGen: Handle C-functions-as-members in bridging thunks.
This lets C functions imported as members be partially applied and used as Swift function values, like Swift methods are.
2016-03-18 15:57:29 -07:00