Commit Graph

1224 Commits

Author SHA1 Message Date
Jordan Rose
3b07d4e102 Make Pattern (more) const-correct.
Sub-patterns are now considered part of the enclosing pattern, so if the
parent pattern pointer is const, the child pointer will be too.

I changed the minimal number of files to make this work, but future code
should use "const Pattern *" when intended, and "Pattern *" only if they
intend to modify the pattern.

Swift SVN r5743
2013-06-21 17:51:39 +00:00
Joe Groff
f072c48e45 Refactor cast representation in AST and SIL, and implement 'is'.
Improve our representations of casts in the AST and SIL so that 'as!' and 'is' (and eventually 'as?') can share almost all of the same type-checking, SILGen, and IRGen code.

In the AST, we now represent 'as!' and 'is' as UnconditionalCheckedCastExpr and IsaExpr, respectively, with the semantic variations of cast (downcast, super-to-archetype, archetype-to-concrete, etc.) discriminated by an enum field. This keeps the user-visible syntactic and type behavior differences of the two forms cleanly separated for AST consumers.

At the SIL level, we transpose the representation so that the different cast semantics get their own instructions and the conditional/unconditional cast behavior is indicated by an enum, making it easy for IRGen to discriminate the different code paths for the different semantics. We also add an 'IsNonnull' instruction to cover the conditional-cast-result-to-boolean conversion common to all the forms of 'is'.

The upshot of all this is that 'x is T' now works for all the new archetype and existential cast forms supported by 'as!'.

Swift SVN r5737
2013-06-21 05:54:03 +00:00
Joe Groff
f2500d79b7 Sema: Allow dynamic casts from generics to concrete types.
Open us 'a as! T' to allow dynamic casts from archetypes to archetypes, archetypes to concrete types, existentials to archetypes, and existentials to concrete types. When the type-checker finds these cases, generate new Unchecked*To*Expr node types for each case.

We don't yet check whether the target type actually makes sense with the constraints of the archetype or existential, nor do we implement the SILGen/IRGen backends for these operations. We also don't extend 'x is T' to query the new operation kinds. There's a better factoring that would allow 'as!' and 'is' to share more code. For now, I want to make sure 'x as! T' continues to work for ObjC APIs when we flip the switch to import protocol types.

Swift SVN r5611
2013-06-16 21:54:13 +00:00
Joe Groff
8512777d31 Parse 'as' and 'is' as sequence exprs.
Treat 'as' and 'is' as fixed-precedence binary operators, like we now do '=' and '? ... :'. However, since 'as' and 'is' max-munch-parse a type name on their RHS, we only parse them at the tail end of a SequenceExpr. This not only makes 'a = b as T' work as expected again, but also makes 'a += b as T' work, fixing <rdar://problem/13772819>.

Swift SVN r5514
2013-06-07 19:21:02 +00:00
Joe Groff
3e44152023 Replace Unsequenced* placeholders with partial AssignExpr/IfExpr nodes.
We can save some source code noise and ASTContext allocation traffic by representing unsequenced assignments and ternaries using AssignExpr/IfExpr with the left and right subnodes nulled out, filling them in during sequence folding.

Swift SVN r5509
2013-06-07 16:49:57 +00:00
Joe Groff
6dcf8ae206 Parse assignments as part of expr-sequence.
Parse '=' as a binary operator with fixed precedence, parsing it into a temporary UnsequencedAssignExpr that gets matched to operands and turned into an AssignExpr during sequence expr folding. This makes '=' behave like library-defined assignment-like binary operators.

This temporarily puts '=' at the wrong precedence relative to 'as' and 'is', until 'as' and 'is' can be integrated into sequence parsing as well.

Swift SVN r5508
2013-06-07 16:15:40 +00:00
Joe Groff
f0f8be9a1a Sema: Typecheck AssignExprs within the constraint system.
Set up constraints for AssignExprs within the constraint system instead of using typeCheckAssignment to set up their own isolated system. Kill the goofy hack not to consider a single-AssignExpr closure to be single-expression-body, because single-assign-expr-body closures now type-check successfully.

typeCheckAssignment still lingers because of a couple uses in typeCheckConstructorBody, but those should be easy to kill off next.

Swift SVN r5504
2013-06-07 05:15:04 +00:00
Joe Groff
cb1f81db84 Make assignment an expression.
Change AssignStmt into AssignExpr; this will make assignment behave more consistently with assignment-like operators, and is a first step toward integrating '=' parsing with SequenceExpr resolution so that '=' can obey precedence rules. This also nicely simplifies the AST representation of c-style ForStmts; the initializer and increment need only be Expr* instead of awkward Expr*/AssignStmt* unions.

This doesn't actually change any user-visible behavior yet; AssignExpr is still only parsed at statement scope, and typeCheckAssignment is still segregrated from the constraint checker at large. (In particular, a PipeClosureExpr containing a single assign expr in its body still doesn't use the assign expr to resolve its own type.) The parsing issue will be addressed by handling '=' during SequenceExpr resolution. typeCheckAssignment can hopefully be reworked to work within the constraint checker too.

Swift SVN r5500
2013-06-06 22:18:54 +00:00
Joe Groff
2606b7ca57 Simplify handling of ternaries in SequenceExprs.
Instead of trying to parse '?' and ':' as separate placeholder exprs and matching them up during binary expr resolution, it's a bit cleaner to parse the entire '? ... :' middle expr of the ternary into a single placeholder node at parse time. Then binary expr resolution only ever has to consider a single sequence element.

Swift SVN r5499
2013-06-06 22:18:48 +00:00
Doug Gregor
dea2ad1932 Zap OverloadedSubscriptExpr.
Swift SVN r5414
2013-05-30 20:50:22 +00:00
Doug Gregor
e06f54273a Zap ExplicitClosureExpr; it was only used in the old type checker.
Swift SVN r5388
2013-05-29 22:25:14 +00:00
Doug Gregor
c038ab92e6 Remove -no-constraint-checker/-constraint-checker and the corresponding language option.
Swift SVN r5387
2013-05-29 22:16:34 +00:00
Chris Lattner
bd217c9227 extend the general form of parseIdentifier to optionally return a SourceLoc,
and use this throughout the parser.


Swift SVN r5314
2013-05-25 00:07:56 +00:00
Doug Gregor
fc502b61a6 Rename isPostfixExpr -> isExprPostfix and improve Doxygen comment.
Swift SVN r5257
2013-05-22 00:31:12 +00:00
Doug Gregor
c10b1cef65 Allow trailing closures without pipes wherever we don't expect curly braces.
This moves trailing closures from expr-postfix up to the level of
expr, and introduces an intermediate level (expr-basic) for places
that need to parse expressions followed by curly braces, such as
if/while/switch/for. Trailing closures are still restricted to occur
after expr-postfix, although the parser itself parses a slightly more
general and then complains if it got more than an expr-postfix.


Swift SVN r5256
2013-05-21 22:30:25 +00:00
Doug Gregor
4d60bb7173 Implement trailing closure syntax.
Trailing closure syntax allows one to write a closure following any
other postfix expression, which passes the closure to that postfix
expression as an arguments. For example:

        sort(fruits) { |lhs, rhs|
          print("Comparing \(lhs) to \(rhs)\n")
          return lhs > rhs
        }

As a temporary limitation to work around the ambiguity with

  if foo { ... } { ... }

we require trailing closures to have an explicit parameter list, e.g.,

  if foo { || ... } { ... }



Swift SVN r5210
2013-05-17 19:16:18 +00:00
Doug Gregor
59ef7ca5ee Don't use source location information to distinguish single-expression closures.
Because we synthesize AST nodes fairly often, and those synthesized
AST nodes rarely have useful source-location information, we shouldn't
be using the validity of source locations to describe the AST. In the
case of closures, use a bit instead. No functionality change.


Swift SVN r5205
2013-05-17 17:27:34 +00:00
Doug Gregor
fab984aeae Test and improve parser error recovery for closures.
Swift SVN r5203
2013-05-17 16:39:20 +00:00
Doug Gregor
6e64ca66f0 Treat '|' as a delimiter while parsing the signature of a closure.
'|' is part of the character set for operators, but within the
signature of a closure we need to treat the first non-nested '|' as
the closing delimiter for the closure parameter list. For example,

  { |x = 1| 2 + x}

parses with the default value of '1' for x, with the body 2 + x. If
the '|' operator is needed in the default value, it can be wrapped in
parentheses:

  { |x = (1|2)| x }

Note that we have problems with both name binding and type checking
for default values in closures (<rdar://problem/13372694>), so they
aren't actually enabled. However, this allows us to parse them and
recover better in their presence.



Swift SVN r5202
2013-05-17 16:02:44 +00:00
Doug Gregor
ce3fe3ae92 Implement Ruby-inspired closure syntax.
This commit implements closure syntax that places the (optional)
parameter list in pipes within the curly braces of a closure. This
syntax "slides" well from very simple closures with anonymous
arguments, e.g.,

  sort(array, {$1 > $0})

to naming the arguments

  sort(array, {|x, y| x > y})

to adding a return type and/or parameter types

  sort(array, {|x : String, y : String| -> Bool x > y})

and with multiple statements in the body:

  sort(array, {|x, y|
    print("Comparing \(x) and \(y)\n")
    return x > y
  })

When the body contains only a single expression, that expression
participates in type inference with its enclosing expression, which
allows one to type-check, e.g.,

  map(strings, {|x| x.toUpper()})

without context. If one has multiple statements, however, one will
need to provide additional type information either with context

  strings = map(strings, {
    return $0.toUpper()
  })

or via annotations

  map(strings, {|x| -> String 
    return x.toUpper()
  }

because we don't perform inter-statement type inference.

The new closure expressions are only available with the new type
checker, where they completely displace the existing { $0 + $1 }
anonymous closures. 'func' expressions remain unchanged.

The tiny test changes (in SIL output and the constraint-checker test)
are due to the PipeClosureExpr AST storing anonymous closure arguments
($0, $1, etc.) within a pattern in the AST. It's far cleaner to
implement this way.

The testing here is still fairly light. In particular, we need better
testing of parser recovery, name lookup for closures with local types,
more deduction scenarios, and multi-statement closures (which don't
get exercised beyond the unit tests).



Swift SVN r5169
2013-05-14 05:17:10 +00:00
Chris Lattner
15f40068d4 remove stmt-brace. If you need something like it for scoping purposes, you can always use "if true {}".
Swift SVN r5034
2013-05-03 05:59:27 +00:00
Joe Groff
3725b4f734 Remove 'x.metatype' expression syntax.
Replace the few uses of it with calls to typeof. We need to keep MetatypeExpr around in the AST because it's consed up by the type checker to represent implicit metatypes that arise in type-checked expressions.

Swift SVN r5017
2013-05-01 21:58:49 +00:00
Doug Gregor
ebecd598d6 Fix weird spacing. No functionality change
Swift SVN r5010
2013-05-01 05:15:45 +00:00
Doug Gregor
2f3e948ad1 Remove the ".(foo:1 bar:2)" message-send syntax.
Swift SVN r5009
2013-05-01 04:33:59 +00:00
Joe Groff
02d6a04b34 Remove 'new' syntax for class constructors.
Fixes <rdar://problem/13723781>.

T(x) still has some lingering conversion behavior, so there's a type-checking ambiguity in classes that are constructible from super- or subclasses, like stdlib's File is from VFSObject. I cheesed around this for now by using keywords in the constructor forms that have ambiguities. This issue should go away when we finish making T(x) mean only construction.

Swift SVN r5002
2013-04-30 23:10:53 +00:00
Doug Gregor
e27deb1494 Remove the least liked of the message-send syntaxes, e.g.,
foo.bar(1) wibble(true)

because it is ridiculously ambiguous.


Swift SVN r4996
2013-04-30 17:05:33 +00:00
Chris Lattner
5b4c31dc94 reland r4968, with a bugfix to avoid breaking the lexer measuring token lengths.
Original message:
SIL Parsing: add plumbing to know when we're parsing a .sil file
Enhance the lexer to lex "sil" as a keyword in sil mode.


Swift SVN r4988
2013-04-30 00:28:37 +00:00
Joe Groff
f026e44c18 Integrate ternary parsing with precedence parsing.
Give the ternary a fixed precedence, parse '?' and ':' into SequenceExprs, and fold them into IfExprs as part of sequence folding. This allows assignment operators like '+=' to have precedence below the ternary as in C. Fixes <rdar://problem/13756211>.

Swift SVN r4983
2013-04-29 22:15:52 +00:00
Chris Lattner
aafe3bdbdc revert r4968, it apparently breaks the world. I'll recommit it when I have time to investigate.
Swift SVN r4971
2013-04-29 16:58:43 +00:00
Chris Lattner
b503206bec SIL Parsing: add plumbing to know when we're parsing a .sil file
Enhance the lexer to lex "sil" as a keyword in sil mode.


Swift SVN r4970
2013-04-29 05:42:59 +00:00
Joe Groff
0f0148355f Parse: Stop parsing ':' as line continuation char.
It's an abomination. IIRC this variant of selector syntax is on the chopping block.

Swift SVN r4956
2013-04-27 14:34:36 +00:00
Joe Groff
69131c2ce3 Parse: Fix parsing of nested ternaries.
Just because the type-checker can't handle them in a reasonable amount of time doesn't mean we should just throw away the AST nodes.

Swift SVN r4955
2013-04-27 14:27:58 +00:00
Jordan Rose
790248d8b4 Diagnostics: use builder pattern instead of streaming for ranges/fix-its.
Per Chris's feedback and suggestions on the verbose fix-it API, convert
diagnostics over to using the builder pattern instead of Clang's streaming
pattern (<<) for fix-its and ranges. Ranges are included because
otherwise it's syntactically difficult to add a fix-it after a range.

New syntax:

  diagnose(Loc, diag::warn_problem)
    .highlight(E->getRange())
    .fixItRemove(E->getLHS()->getRange())
    .fixItInsert(E->getRHS()->getLoc(), "&")
    .fixItReplace(E->getOp()->getRange(), "++");

These builder functions only exist on InFlightDiagnostic; while you can
still modify a plain Diagnostic, you have to do it with plain accessors
and a raw DiagnosticInfo::FixIt.

Swift SVN r4894
2013-04-24 23:15:53 +00:00
Jordan Rose
b6c1ff4483 All the rest of the easy fix-its.
Also, turn a diagnostic into an assertion (string_interpolation_extra),

Swift SVN r4816
2013-04-18 23:34:27 +00:00
Jordan Rose
f7cd3a6ec6 Add fix-its for missing and spurious separators.
Swift SVN r4789
2013-04-18 00:42:59 +00:00
Jordan Rose
49c43d84e6 Allow diagnostics to take either a char range or a token range (SourceRange).
This will be necessary for things like typo-correction, but currently
serves no purpose because all of our diagnostics are token-based. It's
going to be the base range type for Swift fix-its, though, so I thought I'd
get it in place now.

Swift SVN r4750
2013-04-16 01:46:31 +00:00
Dave Zarzycki
54f8cdeb32 Consolidate list parsing and error recovery boilerplate
Fix array/dictionary literal parsing robustness by consolidating and improving
the parsing of lists in general (tuples/array/dictionary literals, attribute
lists, statement lists, declaration lists, etc).

Missing commas in tuple/array/dictionary literals or declaration attributes
are now detected, reported, and recovered from.

Premature ellipsis in tuples are now detected, reported, and recovered from.

Swift SVN r4631
2013-04-08 08:29:51 +00:00
Dave Zarzycki
9f7fa8a6d7 Keyword args now require ':'
NOTE: A nice side effect of this change is that f(x:&y) and g(x:<) parse
correctly regardless of whitespace, unlike f(x=&y) and g(x=<).

Swift SVN r4579
2013-04-02 23:34:30 +00:00
Joe Groff
88d4284178 Parser: Relax whitespace rules for ( and [.
Now that we enforce semicolon or newline separation between statements, we can relax the whitespace requirements on '(' and '[' tokens. A "following" token is now just a token that isn't at the start of a line, and any token can be a "starting" token. This allows for:

  a(b)
  a (b)
  a[b]
  a [b]

to parse as applications and subscripts, and:

  a
  (b)
  a
  [b]

to parse as an expr followed by a tuple or an expr followed by a container literal.

Swift SVN r4573
2013-04-02 16:43:20 +00:00
Joe Groff
9667bda089 Implement 'as' syntax for coercions and casts.
Provide distinct syntax 'a as T' for coercions and 'a as! T' for unchecked downcasts, and add type-checker logic specialized to coercions and downcasts for these expressions. Change the AST representation of ExplicitCastExpr to keep the destination type as a TypeLoc rather than a subexpression, and change the names of the nodes to UncheckedDowncast and UncheckedSuperToArchetype to make their unchecked-ness explicit and disambiguate them from future checked casts.

In order to keep the changes staged, this doesn't yet affect the T(x) constructor syntax, which will for the time being still perform any construction, coercion, or cast.

Swift SVN r4498
2013-03-27 22:27:11 +00:00
Joe Groff
89e1b7e773 Parser: Give IfExpr traditional ternary syntax.
Swift SVN r4489
2013-03-26 01:17:34 +00:00
Joe Groff
4c09ef61e3 Add conditional expressions.
Implement the syntax 'if x then y else z', which evaluates to 'y' if 'x' is true or 'z' if 'x' is false. 'x' must be a valid logic value, and 'y' and 'z' must be implicitly convertible to a common type.

Swift SVN r4407
2013-03-16 20:28:58 +00:00
Doug Gregor
b11b655fcc Introduce another calling syntax for selectors.
This new syntax aims to be closer to the declaration syntax. For
example, to call this method:

  func performSelector(_ : SEL) withObject(obj1 : id)  { }

one would use

  target.performSelector("doThis:") withObject(object)

The additional selector pieces (e.g., withObject(object)) occur on the
same line; otherwise, they are taken as a separate statement. However,
one can use ':' as a continuation character at the beginning of the
next line to continue the message send, e.g.,

  target.performSelector("doThis:")
        :withObject(object)

For the 3-argument version, one could use, e.g.,

  target.performSelector("doThis:") withObject(object1) withObject(object2)

or

  target.performSelector("doThis:")
        :withObject(object1) withObject(object2)

or

  target.performSelector("doThis:")
        :withObject(object1) 
        :withObject(object2)

depending on the width of your screen.

Note that I've tweaked the parsing of case statements slightly to
accommodate this change, by requiring that the ':' that follows a case
statement not start a new line. Thus,

  case foo:

is okay, but

  case foo
    :

is not. This is mostly paranoia, so that

  case target.performSelector("sel"):

is "obviously" a simple method invocation in the case, while

  case target.performSelector("sel")
             :withObject(object):

is "obviously" a two-argument method invocation in the case.

This syntax has some positives, such as similarity with the function
declaration syntax and being a fairly clean extension of the "normal"
Swift method call syntax. It also has some negatives: we have our
first continuation character (':'), the syntax for constructors is
(again) a bit unfortunate

  new NSURL(initWithString="http://www.apple.com")

and it's not clear how to invoke a variadic method with this syntax
without, say, burying the additional arguments in the last argument
(which is currently not permitted), e.g.,

  NSString.alloc().initWithFormat("blah") locale(locale, arg1, arg2)





Swift SVN r4366
2013-03-13 04:19:00 +00:00
Dave Zarzycki
7db15ab4c3 Use backtrackToToken() to remove the CForLoopHack
...and also use the API to forgive tok::period_prefix if it is provably
part of a builder API design pattern.

Swift SVN r4355
2013-03-11 23:30:17 +00:00
Dave Zarzycki
556de46855 Ensure that selector parsing keeps going after errors
"a.(foo:0xQWERTY bar:0xZXCVBNM)" should report two errors, not one.

Swift SVN r4340
2013-03-08 17:16:29 +00:00
Joe Groff
8fb9a46f95 AST: Wrap super.ctor calls in a RebindThis node.
'super.constructor' shouldn't be referenceable without being called, and 'super.constructor(...)' shouldn't return a value. Require super.constructor expressions to be called at parse time, and wrap the call expression in a new RebindThisInConstructorExpr that represents consuming the delegated-to constructor by using it to reassign 'this'. This should theoretically allow super.constructor to an ill-behaved self-modifying ObjC class to work. It's also necessary to support delegating constructors of value types.

Swift SVN r4326
2013-03-08 00:09:39 +00:00
Doug Gregor
4aa6abd9c8 Extend message-send syntax to new expressions and super.constructor expressions.
For example, this allows:
var window = new NSWindow.(initWithContentRect:NSRect(100, 100, 800, 630)
                                     styleMask:Int(NSTitledWindowMask|NSClosableWindowMask|NSResizableWindowMask)
                                       backing:NSBackingStoreType(NSBackingStoreBuffered)
                                         defer:false)


Swift SVN r4315
2013-03-07 04:43:22 +00:00
Doug Gregor
adcd9d91b9 Implement selector argument syntax for method calls.
Introduce a second syntax for method calls that better describes
messages with multiple selector pieces, e.g.,

    undoManager.(registerUndoWithTarget:this
                               selector:"setItemName:"
                                 object:nameSetter)

Note that we're also allowing ':' as a separator for normal keyword
arguments. The intent is to drop the use of '=' for normal keyword
arguments, but that will be a separate change affecting a number of
test cases.



Swift SVN r4313
2013-03-07 02:01:20 +00:00
Joe Groff
f489f2a6fd Clean up AST representation of 'super'.
Replace the more specific Super*RefExpr nodes with a single SuperRefExpr that resolves members of 'this' relative to its superclass. Add an OtherConstructorDeclRefExpr for referring to a constructor as called from another constructor, and use it to represent resolved 'super.constructor' expressions. (It should also be able to represent delegating constructors for free, if we decide we want to add syntax for that.)

Swift SVN r4286
2013-03-05 02:13:49 +00:00
Dave Zarzycki
0b06ffa84d Keyword args should support operators
Swift SVN r4282
2013-03-04 20:44:06 +00:00