Commit Graph

385 Commits

Author SHA1 Message Date
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Chris Lattner
8962cd2665 eliminate an unneccesary intermediate std::vector, NFC.
Swift SVN r26144
2015-03-15 03:01:21 +00:00
Chris Lattner
d8cb56bb13 refactor ClauseRow's ctor to not take a CaseLabelItem, reducing impedence
mismatch with the if/let handling stuff.  NFC.


Swift SVN r26143
2015-03-15 02:57:30 +00:00
Chris Lattner
e2fe7704ad Reimplement emission of StmtCondition (e.g. if/let & while/let) to be
in terms of the pattern binding/emission facilities that are currently
used for switches.  They are more general (handling all patterns,
not hacked up just for optionals).

This leads to us producing better code for if/let bindings, because we 
don't alloc_stack a temporary and deal with memory for non-address-only
types (e.g. the common case of an optional pointer).  Instead, the code 
emits a select_enum{_addr} on the value.

While this changes the generated code in the compiler, there is no exposed
behavioral change to the developer.




Swift SVN r26142
2015-03-14 20:31:23 +00:00
Chris Lattner
5745300718 rename PatternMatchStack back to SwitchStack. I was too quick to rename it: it
is only for processing of fallthrough statements, which really do only apply to
switch.


Swift SVN r26127
2015-03-14 03:32:28 +00:00
Chris Lattner
a6d95e4630 Generalize some terminology in SILGenPattern to say "PatternMatch" instead
of "Switch" since this logic will be used by if/let and while/let pattern
matching in the near future.


Swift SVN r26126
2015-03-14 02:57:42 +00:00
Chris Lattner
5549775677 enhance silgen to treat OptionalSomePattern and EnumElementPattern as
"similar", avoiding false positive "not exhaustive" diagnostics on switches
like:

switch ... {
case let x?: break
case .None: break
}

Also, start using x? patterns in the stdlib more (review appreciated!), which 
is what shook this issue out.



Swift SVN r26004
2015-03-12 00:39:43 +00:00
Chris Lattner
9ac6c23c1d rename IsaPattern -> IsPattern to follow source syntax, NFC.
Swift SVN r25951
2015-03-10 20:09:02 +00:00
Chris Lattner
de808d1603 Progress on: <rdar://problem/19382878> Introduce new x? pattern
This introduces a new pattern, spelled "x?" which is sugar for
matching ".Some(x)".  It also changes the parser slightly so that
_ (the discard expr) is parsed as a unary expr instead of as an
expr.  This allows it to have postfix ? after it, which is important
in pattern contexts to support "case _?:".



Swift SVN r25907
2015-03-10 01:00:23 +00:00
Justin Bogner
d44090d29e InstrProf: Optionally generate coverage maps when profiling
This adds the -profile-coverage-mapping option to swift, and teaches
SILGenProfiling to generate mappings from source ranges to counters.

Swift SVN r25266
2015-02-13 08:42:15 +00:00
Doug Gregor
b642c555be Allow one to change the argument labels of curried function parameters.
Curried function parameters (i.e., those past the first written
parameter list) default to having argument labels (which they always
have), but any attempt to change or remove the argument labels would
fail. Use the fact that we keep both the argument labels and the
parameter names in patterns to generalize our handling of argument
labels to address this problem.

The IDE changes are due to some positive fallout from this change: we
were using the body parameters as labels in code completions for
subscript operations, which was annoying and wrong.

Fixes rdar://problem/17237268.

Swift SVN r24525
2015-01-19 22:15:14 +00:00
Chris Lattner
031f05c82c Unthread PatternKind from SILGen and remove it.
Swift SVN r23958
2014-12-16 19:04:00 +00:00
Chris Lattner
d513803d84 Sigificantly replumb how SILGen generates mark_uninitialized instructions, generating
them in a more consistent and principled way.  Two changes here: MUI is generated
when a vardecl is emitted, not as a separate "MarkPatternUninitialized" pass.  Second,
when generating a MUI for self parameters with a temporary alloc_stack (due to the
possibility of superclass remapping of self) emit the MUI on the allocation itself,
not on the incoming argument.  This is a lot more consistent (dissolving a bunch of 
hacks in DI).

In terms of behavior changes, this only changes the raw sil generated by SILGen and
consumed by DI, so there is no user-visible change.  This simply unblocks future work.



Swift SVN r23823
2014-12-10 00:29:40 +00:00
Michael Gottesman
1afc987739 Refactor the SILArgument API on SILBasicBlock so we can insert bb arguments anywhere in the argument list. Also clean up the API names so that they all match.
Swift SVN r23543
2014-11-22 00:24:40 +00:00
Adrian Prantl
414b163bf0 reflow comment
Swift SVN r22783
2014-10-15 23:05:58 +00:00
Adrian Prantl
4ccf572d4f Reapply r22739 using the new DebugLoc mechanism in SILLocation.
Change the SILLocations for enum element dispatches to make the line table
more consistent. emitEnumElementDispatch may be invoked several times so
it should use the location of the first pattern rather than TheSwitch.

Swift SVN r22782
2014-10-15 23:05:57 +00:00
Mark Lacey
350c41c35d Revert "Change the SILLocations for enum element dispatches to make the line table"
This reverts commit r22739 because it broke an unreachable code test
and is blocking the bots.

The problem can be reproduced with:
  swift -frontend  -disable-objc-attr-requires-foundation-module -emit-sil test/SILPasses/unreachable_code.swift -o /dev/null -verify

Swift SVN r22750
2014-10-15 08:47:40 +00:00
Adrian Prantl
d0a3cd65a5 Change the SILLocations for enum element dispatches to make the line table
more consistent. emitEnumElementDispatch may be invoked several times so
it should use the location of the first pattern rather than TheSwitch.

Swift SVN r22739
2014-10-15 00:26:44 +00:00
Jordan Rose
042569a3be Optional: Replace uses of Nothing with None.
llvm::Optional (like Swift.Optional!) uses None as its placeholder value,
not Nothing.

Swift SVN r22476
2014-10-02 18:51:42 +00:00
Dmitri Hrybenko
d396134832 Portability: use std::make_tuple instead of relying on a libc++
extension (an implicit constructor in std::tuple)

Swift SVN r22339
2014-09-28 18:44:34 +00:00
John McCall
6923101341 Rename "AccessKind" to "AccessSemantics". NFC.
There are a lot of different ways to interpret the
"kind" of an access.  This enum specifically dictates
the semantic rules for an access:  direct-to-storage
and direct-to-accessor accesses may be semantically
different from ordinary accesses, e.g. if there are
observers or overrides.

Swift SVN r22290
2014-09-25 23:12:39 +00:00
Adrian Prantl
5cb356686c Turn the location for the break out of a switch case into a cleanup
location.
<rdar://problem/18401152> stepping out of the body of the case that was taken goes back to the case statement

Swift SVN r22285
2014-09-25 16:29:25 +00:00
Doug Gregor
44c1709505 Add EnumElementDecl::getArgumentInterfaceType().
Use this in calls to TypeBase::getTypeOfMember() that were relying on
archetypes solely because they were using EnumElementDecl::getArgumentType().

Swift SVN r22205
2014-09-23 03:44:00 +00:00
John McCall
8cae5ba1d0 Generalize 'isDirectPropertyAccess' to allow for
direct (i.e. non-polymorphic) access to accessor
functions, and use this in materializeForSet for
computed properties.

Swift SVN r22059
2014-09-18 05:51:32 +00:00
John McCall
76f0a3d821 After emitting consecutive equivalent isa patterns,
restart after the whole group instead of always at
the second.

rdar://17772217

Swift SVN r21595
2014-08-29 23:24:03 +00:00
John McCall
f40e9ef0ae Rewrite switch dispatch emission.
Specialization now recurses on only that prefix of the
current matrix which shares a specialization form
(essentially, a pattern kind) with the head row.  This is
inferior to the previous algorithm in a number of ways: we
may require more switches to perform a single dispatch, and
we may introduce more redundant variables in the leaves.
However, it also means that we will have fully specialized a
row along exactly one path in the decision tree, which makes
it much easier to work with dispatches that introduce new
cleanups.

This change also changes switch-dispatch to use the new
dynamic-cast instructions.

Incidentally fixes rdar://16401831.

Swift SVN r19336
2014-06-30 11:55:28 +00:00
Joe Groff
6963fa6322 SILGen: Fix a bug in wildcard matching in 'switch'.
When we specialize the decision matrix on a pattern, we have to start with the *first* preceding wildcard, not the last. Oops. While we're here, clean things up a bit by using Optional instead of magic integer values to track the first wildcard row. Fixes <rdar://problem/17272985>.

Swift SVN r19061
2014-06-21 01:08:01 +00:00
Chris Lattner
a9a1e9fc94 silgen support for breaking out of a switch. rdar://16736255
Swift SVN r16903
2014-04-27 01:48:14 +00:00
Joe Groff
55f6b925de SIL: Rename 'take_enum_data_addr' to 'unchecked_take_enum_data_addr'.
In preparation for adding an 'unchecked_enum_data' equivalent for loadable enums.

Swift SVN r16728
2014-04-24 00:40:47 +00:00
Joe Groff
4fdc20f739 Allow '<pattern> as T' checked patterns with subpattern bindings.
Allow a form of 'case is T' that matches the cast result to a subpattern, 'case <pattern> as T'. This exposes an issue in switch destructuring with casting into complex class hierarchies <rdar://problem/16401831> but works for common cases.

Swift SVN r15396
2014-03-24 00:02:44 +00:00
Dmitri Hrybenko
11fea869c1 Change 'switch' not to fall through between empty cases and always require at
least one statement per case

rdar://16301313


Swift SVN r15266
2014-03-20 11:44:59 +00:00
John McCall
446a9bd4a7 Introduce CanType versions of the various "getXOrBoundGenericX"
accessors.

Optimize these accessors by making them check for
BoundGenericXType instead of BoundGenericType and dyn_cast'ing
the Decl.  (The latter used to be necessary before we split
BoundGenericType.)

Swift SVN r15037
2014-03-14 05:59:44 +00:00
Chris Lattner
1344319677 Rename the internal compiler lexicon from val -> let.
Swift SVN r14408
2014-02-26 21:21:18 +00:00
Chris Lattner
28903887e7 Rename the internal compiler lexicon from let -> val.
Swift SVN r13992
2014-02-17 16:48:21 +00:00
Adrian Prantl
85ee56bd7b Debug info: Improve the line table for SwitchStmt variable bindings by
associating the store to it with the first CaseStmt.
The goal is to make the linetable entries as monotone as possible.

Fixes <rdar://problem/15888936> switch line table's first line misplaced.

Swift SVN r13805
2014-02-12 02:24:15 +00:00
Adrian Prantl
ed5b21ce8f Debug info: emit a cleanup location for branches leading out of switch cases.
rdar://problem/15888936.

Swift SVN r13655
2014-02-07 22:19:00 +00:00
Adrian Prantl
bacd2d3c04 Debug info: Fix the locations for branches in case statements.
<rdar://problem/15899195> Step from a case the middle of a switch goes to the beginning of the containing function

Swift SVN r13623
2014-02-07 02:37:36 +00:00
Doug Gregor
f1be1ed572 Implement super mesage sends for @objc property and subscript getters/setters.
Fixes <rdar://problem/15933008>.


Swift SVN r13100
2014-01-29 07:45:53 +00:00
Chris Lattner
8735a76b0b Simplify emitRValueForPropertyLoad and make it work when we can have
rvalues that are both computed and have storage.


Swift SVN r13010
2014-01-27 22:10:34 +00:00
Joe Groff
fc4ecc92c7 SILGen: Don't box 'let' bindings inside 'switch' patterns.
If all of the bindings in a pattern column are 'let' bindings, don't box the binding. If there is any 'var' in the column, conservatively fall back to binding a box. Factor out the logic for producing an initialization for a variable into an new emitInitializationForVarDecl method that SILGenPattern can use. Add a 'copyInto' method to RValue that can bind a copy of an rvalue to an Initialization.

This doesn't use Chris's new +0 ManagedValue optimization yet, so we end up with an extra copy_value when the value is bound that might still be avoidable.

Swift SVN r12903
2014-01-24 05:33:14 +00:00
Chris Lattner
84f9919016 introduce a SGF::emitRValueAsSingleValue helper function to wrap a common
and repetitive pattern.


Swift SVN r12808
2014-01-22 22:51:55 +00:00
Chris Lattner
63784ca41c remove the ManagedValue::Unmanaged marker, lets just use ManagedValue::forUnmanaged()
instead, which is shorter.  It is better to have one way to do things than two.


Swift SVN r12710
2014-01-22 06:26:34 +00:00
Joe Groff
81759cde52 SILGen: Strip non-semantic pattern nodes before dispatching.
Don't crash when sugary pattern nodes like Paren and Var get mixed and we try to cast subpatterns to the expected semantic pattern type.

Swift SVN r12674
2014-01-21 23:56:11 +00:00
Chris Lattner
d4594b77a5 Switch the nominal type pattern matching destructuring logic to
use the new RValue emission infrastructure instead of duplicating
some of it.  This enables the use of computed properties, fixing
<rdar://problem/15859432> SILGen abort when pattern matching on computed property

and eliminates some code that future changes would otherwise have to 
worry about.  

There are other problems with this code (e.g. see rdar://15863069), so I think 
we should disable the feature until it has time to really bake, but this is still
useful progress in the right direction and is a net reduction of code.



Swift SVN r12618
2014-01-20 23:29:42 +00:00
Chris Lattner
f5b85341a1 Expand out the "isComputed" property in AbstractStorageDecl to be an enum
with two kinds, and some more specific predicates that clients can use.

The notion of 'computed or not' isn't specific enough for how properties
are accessed.  We already have problems with ObjC properties that are 
stored but usually accessed through getters and setters, and a bool here
isn't helping matters.

NFC.



Swift SVN r12593
2014-01-20 18:16:30 +00:00
Jordan Rose
11008f0ed1 Split diagnostics out into separate files.
Thanks to the way we've set up our diagnostics engine, there's not actually
a reason for /everything/ to get rebuilt when /one/ diagnostic changes.
I've split them up into five categories for now: Parse, Sema, SIL, IRGen,
and Frontend, plus a set of "Common" diagnostics that are used in multiple
areas of the compiler. We can massage this later.

No functionality change, but should speed up compile times!

Swift SVN r12438
2014-01-17 00:15:12 +00:00
Chris Lattner
1b6c2e321e change a few "createCopyValue"'s to "emitCopyValue" so that trivial types
don't get copyvalues.  This is mostly to make sure that only Typelowering and
silcloner are calling createCopyValue.


Swift SVN r12365
2014-01-16 00:27:32 +00:00
Chris Lattner
261b21a10f various random cleanups to pattern processing stuff, NFC.
Swift SVN r12335
2014-01-15 19:12:45 +00:00
Joe Groff
ee85fb341a SILGen: Strip non-semantic pattern nodes before dispatching.
Fixes a crash when redundant parens wrapped a subpattern.

Swift SVN r12164
2014-01-10 23:15:32 +00:00
Joe Groff
8f6a58b998 SIL: Split address-only enum dispatch from destructive projection.
Split 'destructive_switch_enum_addr' into separate 'switch_enum_addr' and 'take_enum_data_addr' instructions. This should unblock some optimization work we would like to do with enums.

Swift SVN r12015
2014-01-07 22:58:21 +00:00