Commit Graph

188 Commits

Author SHA1 Message Date
John McCall
80b180a9a1 Implement a syntactic peephole to recognize explicit bridging
conversions that reverse an implicit conversion done to align
foreign declarations with their imported types.

For example, consider an Objective-C method that returns an NSString*:
  - (nonnull NSString*) foo;
This will be imported into Swift as a method returning a String:
  func foo() -> String
A call to this method will implicitly convert the result to String
behind the scenes.  If the user then casts the result back to NSString*,
that would normally be compiled as an additional conversion.  The
compiler cannot simply eliminate the conversion because that is not
necessarily semantically equivalent.

This peephole recognizes as-casts that immediately reverse a bridging
conversion as a special case and gives them special power to eliminate
both conversions.  For example, 'foo() as NSString' will simply return
the original return value.  In addition to call results, this also
applies to call arguments, property accesses, and subscript accesses.
2017-07-15 01:13:41 -04:00
John McCall
7f22faf968 Substantially rework how SILGen handles bridging as part of laying the
ground work for the syntactic bridging peephole.

- Pass source and dest formal types to the bridging routines in addition
  to the dest lowered type.  The dest lowered type is still necessary
  in order to handle non-standard abstraction patterns for the dest type.

- Change bridging abstraction patterns to store bridged formal types
  instead of the formal type.

- Improve how SIL type lowering deals with import-as-member patterns.

- Fix some AST bugs where inadequate information was being stored in
  various expressions.

- Introduce the idea of a converting SGFContext and use it to regularize
  the existing id-as-Any conversion peephole.

- Improve various places in SILGen to emit directly into contexts.
2017-07-11 12:45:13 -04:00
John McCall
44fda629aa Teach SILGen to handle more bridging cases and thread SGFContexts
through a few places.

This patch should be NFC for existing patterns, but it's preparing for
using SILGen's built-in bridging capabilities for more things.
2017-06-11 01:39:51 -04:00
Arnold Schwaighofer
23b30aa76b Merge pull request #10077 from aschwaighofer/irgen_delete_unused_value_witnesses
Delete unused existential value witnesses from the old existential implementation
2017-06-02 19:24:59 -07:00
Arnold Schwaighofer
ca63326e1b Delete unused existential value witnesses from the old existential
implementation

And remove the SWIFT_RUNTIME_ENABLE_COW_EXISTENTIALS flag.
2017-06-02 14:34:41 -07:00
Slava Pestov
29cd885996 SILGen: Remove SILGenFunction::ArchetypeOpenings
SILBuilder now tracks data dependencies between instructions
that open existentials and uses of the opened type, so
SILGen's mechanism for this is no longer needed.

In particular, this simplifies ArchetypeCalleeBuilder.
2017-06-02 01:35:11 -07:00
Joe Groff
c8a7a442ae Runtime: Include source location information in log messages about deprecated implicit Objective-C entry points.
Make it easier for migration by pinpointing exactly where to insert @objc to keep the entry points in Swift 4 mode. rdar://problem/32230003
2017-05-16 19:02:47 -07:00
Slava Pestov
b5721e8d8e AST: Remove AnyObject protocol 2017-05-02 19:45:00 -07:00
Michael Gottesman
a89752f77a [gardening] Eliminate unused variable warnings from non-asserts build. 2017-04-24 16:08:06 -07:00
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
Doug Gregor
8584e97408 [AST] Sink ProtocolConformance::getWitness() down into NormalProtocolConformance.
NormalProtocolConformance has the only correct implementation of this
functionality. Instead, providing a safer getWitnessDecl() that
doesn't promise substitutions that are incorrect (and not actually
used by any clients).
2017-04-18 23:35:24 -07:00
Slava Pestov
d58f049608 AST: Introduce ASTContext::getAnyObjectType()
This replaces a number of usages of KnownProtocolKind::AnyObject,
which is soon going away.
2017-04-13 21:17:05 -07:00
Slava Pestov
f5b6f22ed6 Revert "[sil] Add a new API SILFunction::getTypeLowering(SILType)."
The new API is broken. Popping a generic context frees all
dependent type lowerings, so this function returns a pointer
to freed memory.

This reverts commit 24dfae0716.
2017-04-02 19:55:19 -07:00
John McCall
305c94fc5b Pare down the Initialization interface and allow implementations
to insert code before performing an in-place initialization.

Intended to be NFC, but some of the clients needed more rewriting
than others.
2017-03-31 18:55:09 -04:00
Michael Gottesman
79b225ffa8 [silgen] Fix up EnumElementPatternInitialization::emitEnumMatch to use ownership.
This commit does a few things:

1. It uses SwitchEnumBuilder so we are not re-inventing any wheels.
2. Instead of hacking around not putting in a destroy for .None on the fail
pass, just *do the right thing* and recognize that we have a binary case enum
and in such a case, just emit code for the other case rather than use a default
case (meaning no cleanup on .none).

rdar://31145255
2017-03-29 15:36:41 -07:00
Joe Shajrawi
1c490c1c5c Add support for global opaque values / subscript rvalues under opaque values mode 2017-03-23 16:54:10 -07:00
Joe Shajrawi
cd38903450 Support reabstraction / partial apply of protocols under opaque value mode 2017-03-23 14:53:38 -07:00
Joe Shajrawi
b0cb327c17 Add support for opaque value call result types 2017-03-23 11:47:39 -07:00
Joe Shajrawi
38e20af6dc Support for capture of a (im)mutable opaque value type 2017-03-21 17:18:52 -07:00
Joe Shajrawi
9a87c07bc2 Add support for creating optionals to optionals during translation / reabstraction 2017-03-16 16:59:40 -07:00
Joe Shajrawi
8a7f9ba057 Support Optional Injection of Opaque value types 2017-03-16 11:32:05 -07:00
Slava Pestov
64adf7495e SILGen: Change ArchetypeOpenings map to be keyed by ArchetypeType * and not CanType 2017-03-14 17:46:54 -07:00
Joe Shajrawi
cb197d5774 Add special corner case: support for Error type under opaque value mode 2017-03-10 20:23:40 -08:00
Arnold Schwaighofer
fc8815900a SILGen: Disable moving out of opened existential values in copy-on-write mode 2017-03-09 17:22:34 -08:00
Michael Gottesman
0769239daf [silgen] Fix ownership of several uses of init_existential_ref in SILGenBuilder.
We were just passing along the previously used cleanup, rather than destroying
the old cleanup and propagating a new one along.

rdar://29791263
2017-03-09 14:22:25 -08:00
Slava Pestov
76eb5dd66d SILGen: Clean up ad-hoc SubstitutionList construction when calling intrinsics
Change emitApplyOfLibraryIntrinsic() to take a SubstitutionMap,
and use the correct abstractions to build the map.

This gets rid of the last remaining uses of gatherAllSubstitutions()
in SIL.
2017-03-08 13:54:29 -08:00
Michael Gottesman
1600a446dd [silgen] Change emitOptionalToOptional to use the SwitchEnumBuilder API.
rdar://29791263
2017-03-06 13:58:34 -08:00
Michael Gottesman
6e44e54991 Fix several metatype to objects that didn't use cleanups. 2017-03-01 15:54:19 -08:00
Joe Shajrawi
16b6cb5e1d Support for deinit of opaque existentials: deinit_existential_opaque instruction + SILGen support 2017-02-27 14:46:43 -08:00
practicalswift
6d09574ffb [gardening] Fix likely word processing artefacts. 2017-02-21 14:20:34 +01:00
Joe Shajrawi
147f844ce4 Merge pull request #7654 from shajrawi/init_opaque
Add support for Init Existentials for opaque value types
2017-02-20 19:37:01 -08:00
Joe Shajrawi
1e521c453b Add support for Init Existentials for opaque value types 2017-02-20 16:40:02 -08:00
Michael Gottesman
26a266b15e [silgen] Cleanup emitPreconditionOptionalHasValue and its uses so that they use proper ownership.
The big change here is that this routine will not emit switch_enum as a +1 value
and create a proper owned argument with cleanup.

rdar://29791263
2017-02-19 22:19:47 -08:00
Michael Gottesman
1e17d86836 [silgen] Change one instance of switch_enum{_addr} that were using default for the some case to just use the some case.
This is the first in a series of changes to make SILGen always emit switch_enums
such that payloads are always passed as an arguments to the destination blocks.
This is important since switch_enum today in SILGen is modeled as a consuming
operation. Thus if we follow the model today where we reuse in the destination
blocks the switched upon value, we have created either a use after free or a
double consuming (depending on how the value is used).

There is no reason why we should use a default case here for these optional enums.

rdar://29791263
2017-02-19 17:43:30 -08:00
Michael Gottesman
dd5120e9f0 [silgen] Create a closure API called bufferForExprResult based on getBufferForExpr/manageBufferForExpr but managing the ownership for the user.
There are many cases where getBufferForExpr and manageBufferForExpr occur right
next to each other. The closure based API expresses this pattern in a more
convenient manner where the user can express his/her intent with one action
instead of two. In these cases, the split is messy and not necessary.
2017-02-19 11:15:01 -08:00
Arnold Schwaighofer
ae0b6b1e06 Merge pull request #7186 from aschwaighofer/cow_exist_silgen_support
SIL/SILGen support for copy-on-write existentials
2017-02-15 17:41:10 -08:00
Arnold Schwaighofer
876cea81ae SIL: Add an allowed access kind to the opened value of an open_existential_addr instruction
Once we move to a copy-on-write implementation of existential value buffers we
can no longer consume or destroy values of an opened existential unless the
buffer is uniquely owned.

Therefore we need to track the allowed operation on opened values.

Add qualifiers "mutable_access" and "immutable_access" to open_existential_addr
instructions to indicate the allowed access to the opened value.

Once we move to a copy-on-write implementation, an "open_existential_addr
mutable_access" instruction will ensure unique ownership of the value buffer.
2017-02-15 14:23:12 -08:00
Arnold Schwaighofer
ec802fe9cd SILGen: Thread AccessKind to openExistential()
We will eventually create different open_existential_addr instructions depending on the kind of access to the opened value
2017-02-15 14:22:18 -08:00
Slava Pestov
41eba98902 Gardening: Fix some unused variable warnings in no-assert builds 2017-02-15 12:57:35 -08:00
Michael Gottesman
9747214e92 [silgen] Refactor WritebackScope functionality into a new FormalEvaluationScope.
As per John, WritebackScope was always an unfortunate name. Generally these
scopes are meant for formal evaluations of inout parameters. The cases that I am
interested in generalizing them to be used for are borrows of the base of a
class that will then be used as an lvalue.

This also eliminates the out of line vector of lvalue writebacks.

rdar://29791263
2017-02-14 13:15:23 -08:00
Hugh Bellamy
f001b7562b Use relatively new LLVM_FALLLTHROUGH instead of our own SWIFT_FALLTHROUGH 2017-02-12 10:47:03 +07:00
Joe Shajrawi
df5297d877 Merge pull request #7349 from shajrawi/open_existential
Support for Open Existentials that do no take an address
2017-02-09 12:39:16 -08:00
Joe Shajrawi
c478828de7 Support for Open Existentials that do no take an address 2017-02-09 11:25:34 -08:00
Slava Pestov
3519e0cd25 AST: Introduce new SubstitutionList type to replace ArrayRef<Substitution>
SubstitutionList is going to be a more compact representation of
a SubstitutionMap, suitable for inline allocation inside another
object.

For now, it's just a typedef for ArrayRef<Substitution>.
2017-02-06 21:36:33 -08:00
Slava Pestov
c86b5ae427 AST: Header file gardening - include what you use 2017-01-19 20:07:06 -08:00
Michael Gottesman
ea1f804207 [semantic-sil] Eliminate ValueOwnershipKind::Any from SILPHIArguments in Semantic SIL.
Most of this involved sprinkling ValueOwnershipKind::Owned in many places. In
some of these places, I am sure I was too cavalier and I expect some of them to
be trivial. The verifier will help me to track those down.

On the other hand, I do expect there to be some places where we are willing to
accept guaranteed+trivial or owned+trivial. In those cases, I am going to
provide an aggregate ValueOwnershipKind that will then tell SILArgument that it
should disambiguate using the type. This will eliminate the ackwardness from
such code.

I am going to use a verifier to fix such cases.

This commit also begins the serialization of ValueOwnershipKind of arguments,
but does not implement parsing of value ownership kinds. That and undef are the
last places that we still use ValueOwnershipKind::Any.

rdar://29791263
2017-01-10 20:05:23 -08:00
practicalswift
3918d9d251 [gardening] Replace likely word processor artefacts with ASCII equivalents (— → --, … → ...) 2017-01-08 15:23:06 +01:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Hugh Bellamy
95af3aa7c8 Fix errors and warnings building swift/SILGen on Windows using MSVC 2016-12-22 11:57:33 +00:00
Michael Gottesman
4e8ff35df5 [semantic-sil] Add ValueOwnershipKind field to SILPHIArgument and split Argument creation methods into one for SILPHIArgument and another for SILFunctionArgument.
We preserve the current behavior of assuming Any ownership always and use
default arguments to hide this change most of the time. There are asserts now in
the SILBasicBlock::{create,replace,insert}{PHI,Function}Argument to ensure that
the people can only create SILFunctionArguments in entry blocks and
SILPHIArguments in non-entry blocks. This will ensure that the code in tree
maintains the API distinction even if we are not using the full distinction in
between the two.

Once the verifier is finished being upstreamed, I am going to audit the
createPHIArgument cases for the proper ownership. This is b/c I will be able to
use the verifier to properly debug the code. At that point, I will also start
serializing/printing/parsing the ownershipkind of SILPHIArguments, but lets take
things one step at a time and move incrementally.

In the process, I also discovered a CSE bug. I am not sure how it ever worked.
Basically we replace an argument with a new argument type but return the uses of
the old argument to refer to the old argument instead of a new argument.

rdar://29671437
2016-12-18 14:48:35 -08:00