Commit Graph

1233 Commits

Author SHA1 Message Date
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
Dave Zarzycki
fa0b0467e5 Finish generalizing operators in TupleExprs
Before this change, an operator in a tuple forced the entire tuple to
contain only operators.

Swift SVN r4280
2013-03-04 18:56:07 +00:00
Dave Zarzycki
84e757b391 Allow operators in TupleExprs, not just ParenExprs
Swift SVN r4275
2013-03-04 05:58:34 +00:00
Joe Groff
6588b83a53 Parser: Parse '(<op>)' as a DeclRef of an operator
Enable operator functions to be referred to by name as, e.g., (+), (+=), etc. Unary/binary ambiguities are resolved via type context. Prefix/postfix unary ambiguities aren't resolved. References to assignment operators produce plain [byref] functions.

Swift SVN r4274
2013-03-03 19:31:00 +00:00
Dave Zarzycki
68c6d4c6e3 13324820 Change tuple accessor syntax to 'tuple.0'
Swift SVN r4265
2013-03-02 17:51:03 +00:00
Doug Gregor
315451eb45 Implement parsing, AST, semantic analysis, and IRgen for dictionary literals.
Swift SVN r4193
2013-02-25 07:27:15 +00:00
Doug Gregor
b61ea41e3f Refactor array-literal parsing to make way for dictionary literals.
Swift SVN r4192
2013-02-25 05:58:59 +00:00
Joe Groff
a8b5a2ec26 AST: Add UnresolvedSpecializeExpr.
This node represents a type parameter list application in an unresolved expr context. The type checker will use these to explicitly bind type variables of generic types.

Swift SVN r4046
2013-02-14 19:41:23 +00:00
Joe Groff
dc016b8d7e Parse: Fake parsing type parameters in expressions
When parsing an expression, if we see the production [[identifier '<']], use the following heuristic to choose whether to parse it as a type list or as an operator expression:
- Speculatively parse the subsequent production as a type parameter list.
- If the parse succeeds, examine the token after the closing '>'. If it is one of the following:
  l_paren_following
  l_square_following
  r_paren
  r_square
  l_brace
  r_brace
  comma
  semicolon
  period
then accept the parse as a type list.
- If the parse fails, or if the type list is not followed by one of those tokens, reject the type list and parse as an operator expression.

This only implements the parsing rule. The type parameters are just dropped on the floor--the AST representation and Sema changes are forthcoming. Encouragingly, no test or library code appears to be broken by this rule.

Swift SVN r4044
2013-02-14 00:34:55 +00:00
Dave Zarzycki
177e243ad5 Tokens should not be named after their use
This fixes an old TODO item based on feedback from Chris and John.

Swift SVN r4038
2013-02-13 21:40:43 +00:00
Joe Groff
750374f61a Sema: Typecheck super.method expressions.
Add support to the constraint checker for typechecking UnresolvedSuperMemberRef expressions and constructing SuperMemberRef or SuperCall expressions as appropriate. We’ll also need a GenericSuperMemberRefExpr to refer to properties of generic supertypes, but in the interests of demo expedience I’m leaving that case partially-implemented for now.

Swift SVN r4020
2013-02-12 19:23:34 +00:00
Dave Zarzycki
d2ba334949 Simplify parseMatchingToken()
Swift SVN r3966
2013-02-06 06:19:25 +00:00
Dave Zarzycki
8a56780ab8 Subscription should implicitly tuple the key/index
This now works:

struct no_index {
  subscript () -> Int { return 42 }
  func test() -> Int { return this[] }
}

struct tuple_index {
  subscript (x : Int, y : Int) -> (Int, Int) { return (x, y) }
  func test()  -> (Int, Int) { return this[123, 456] }
  func test2() -> (Int, Int) { return this[y = 123, x = 456] }
}

Swift SVN r3965
2013-02-06 05:47:33 +00:00
Joe Groff
b5bc022686 Incomplete support for array literal expressions.
Analyze an expression of the form [<tuple contents>] into a call to T.convertFromArrayLiteral(<tuple contents>) for some T conforming to an ArrayLiteralConvertible protocol. Because of some limitations in the constraint checker and protocol conformance checkers, it currently does an ad-hoc conformance check using member constraints. It also currently fails to typecheck for generic container types, and does not provide a default fallback to 'T[]' if unable to deduce a better type from context.

Swift SVN r3953
2013-02-05 21:23:42 +00:00
Joe Groff
7c3c5700ec Parse: Update inaccurate comment
Bit of debris from implementing super.constructor. 'expr-super' isn't an expr-unary.

Swift SVN r3896
2013-01-29 22:38:24 +00:00
Dave Zarzycki
b36678214a Rename l_(paren|square)_(call|subscript)
Thanks Chris and John for the feedback.

Swift SVN r3893
2013-01-29 21:13:39 +00:00
Dave Zarzycki
5fe85d7020 Formalize unary prefix '&' to mean "make ref"
This makes reserved operator parsing more robust and easier to understand.

Swift SVN r3884
2013-01-27 21:20:06 +00:00
Dave Zarzycki
93a81f1cd8 Fix unresolved member versus field access ambiguity
'.' is an operator, therefore unary prefix '.' can mean something
different than binary '.' (field access).

Swift SVN r3883
2013-01-27 00:30:40 +00:00
Dave Zarzycki
1dcfff8497 'this' and 'This' are keywords
Swift SVN r3858
2013-01-24 21:54:43 +00:00
Dave Zarzycki
eb5bb20bf9 Remove unnecessary use of isAnyLParen()
Swift SVN r3857
2013-01-24 17:36:36 +00:00
Joe Groff
057f1b6468 Sema: Type check non-overloaded super.constructor.
Implement base class constructor lookup to resolve the function reference for SuperConstructorRefCallExprs.

Swift SVN r3855
2013-01-24 02:29:41 +00:00
Joe Groff
14c68e95d0 Lexer: Special-case 'super' and 'constructor'.
Dave noted that he's trying to scrub the parser codebase of wishy-washy 'isAnyLParen' and 'isAnyLBrace' calls by consistently lexing opening bracket tokens correctly to begin with. Since currently only 'super' and 'constructor' need to be lexed like identifiers for expression syntax (and, in the future, 'this' and 'This' when those become keywords), mark them as a special kind of 'identifier keyword' in Tokens.def and roll back some of the changes I made to make parsing other decls support either token.

Swift SVN r3848
2013-01-23 22:23:37 +00:00
Joe Groff
e6d3c3e00c Parser: Parse 'super' expressions.
Set up AST nodes for 'super.<identifier>', 'super.constructor', and 'super[<expr>]' expressions, and implement parsing for them without any sema or backend support.

Swift SVN r3847
2013-01-23 21:24:28 +00:00
Joe Groff
8af835edcc Lexer: '[' and '(' after a keyword is non-literal.
Opening brackets after a keyword have to lex as l_paren_call or l_square_subscript in order for expressions like 'super.constructor()' or 'super[i]' to parse. While we're here, let's move the keyword and punctuator list to a metaprogrammable Tokens.def header too. Update decl and stmt parsers to use 'isAnyLParen' so that, e.g., 'constructor(' and 'constructor (' both work as before.

Swift SVN r3846
2013-01-23 21:24:26 +00:00
Dave Zarzycki
735294a5c9 Make the lexing of '(', '[', and '.' consistent
The lexer now models tuples, patterns, subscripting, function calls, and
field access robustly. The output tokens are now better named as well:
l_paren and l_paren_call, and l_square and l_square_subscript. It
should be much more clear now which one to use. Also, the use of
l_paren or l_square will not arbitrarily flip flop if the token before
it is a keyword or if the token before it was the trailing ']' of an
attribute list. Similarly, tuples will always cause the lexer to produce
l_paren, regardless if the user typed '((x,y))' or '( (x,y))'.

When we someday add array literals, the right token is now naturally
falling out of the lexer.

Swift SVN r3840
2013-01-23 03:23:17 +00:00
Dave Zarzycki
2d51225e7d Misc cleanup
Swift SVN r3833
2013-01-22 21:08:47 +00:00
Joe Groff
6449655e21 Implement selector-style function definition syntax.
rdar://12315571
Allow a function to be defined with this syntax:

  func doThing(a:Thing) withItem(b:Item) -> Result { ... }

This allows the keyword names in the function type (in this case
`(_:Thing, withItem:Item) -> Result`) to differ from the names bound in the
function body (in this case `(a:Thing, b:Item) -> Result`, which allows
for Cocoa-style `verbingNoun` keyword idioms to be used without requiring
those keywords to also be used as awkward variable names. In addition
to modifying the parser, this patch extends the FuncExpr type by replacing
the former `getParamPatterns` accessor with separate `getArgParamPatterns`
and `getBodyParamPatterns`, which retrieve the argument name patterns and
body parameter binding patterns respectively.



Swift SVN r3098
2012-11-01 21:53:15 +00:00
Eli Friedman
b62d47a2e5 Rewrite the way we build capture list so we don't miss capturing "this". <rdar://problem/12583581>.
Swift SVN r3093
2012-10-30 22:33:18 +00:00
John McCall
2f8f05615e Rename TypeOfExpr / TypeOfInst to MetatypeExpr / MetatypeInst.
Introduce a '.metatype' form in the syntax and do some basic
type-checking that I probably haven't done right.  Change
IR-generation for that and GetMetatypeExpr to use code that
actually honors the dynamic type of an expression.

Swift SVN r3053
2012-10-24 07:54:23 +00:00
Eli Friedman
9e06964c8a Make the TypeLoc constructor which takes just a type private, and expose a
static method to call it, to make it more explicit what is happening.  Avoid
using TypeLoc::withoutLoc for function definitions; instead, just use an empty
TypeLoc.



Swift SVN r2606
2012-08-10 02:09:00 +00:00
Chris Lattner
5184be484f Finish off rdar://12017658 - func() expression should support type inference of argument types.
This implements inference of return types, per the discussion today.



Swift SVN r2572
2012-08-07 05:12:32 +00:00
Chris Lattner
b6f6eec106 implement rdar://11935352 - accepting closures with no body expression (the closure
just returns "()").


Swift SVN r2503
2012-08-02 20:08:51 +00:00
Eli Friedman
e6917fbc51 Add support for "new" with classes with non-default constructors. <rdar://problem/11917082>.
Swift SVN r2384
2012-07-20 21:37:08 +00:00
Eli Friedman
d6a4ba90dd Move TypeLocs to a design where a TypeLoc is a struct containing a type plus
location info for that type.  Propagate TypeLocs a bit more through the AST.



Swift SVN r2383
2012-07-20 21:00:30 +00:00
Eli Friedman
361f931a16 Start propgating TypeLocs from the parser into the AST.
Swift SVN r2372
2012-07-19 02:54:28 +00:00
Eli Friedman
f1f67db652 Big cleanup for how we handle computing types for functions and semantic
analysis for patterns.

Major changes:
1. We no longer try to compute the types of functions in the parser.
2. The type of a function always matches the type of the argument patterns.
3. Every FuncDecl now has a corresponding FuncExpr; that FuncExpr might not
   have a body, though.
4. We now use a new class "ExprHandle" so that both a pattern and a type
   can hold a reference to the same expression.

Hopefully this will be a more reasonable foundation for further changes to
how we compute the types of FuncDecls in generics and for the implementation
of type location information.



Swift SVN r2370
2012-07-19 02:09:04 +00:00
Eli Friedman
2eb6261838 Don't try to capture TypeDecls in local functions.
Swift SVN r2180
2012-06-08 23:51:02 +00:00
John McCall
30f5e36dbc Semantic analysis and AST support for postfix operators.
Also prevent us from including unary operators in the
lookup results for operators written binary-style and
vice-versa.

Swift SVN r2162
2012-06-07 01:00:08 +00:00
John McCall
8c46c69efa Lexically distinguish prefix, postfix, and binary operators
and use this information as cues in the language.  Right now,
we do not accept things like "-- *i" because the prefix
operator is not correctly right-bound;  instead you have to
write "--(*i)".  I'm okay with that;  I did add a specialized
diagnostic recognizing operator-binary in a place where we're
expecting a potential operator-prefix.

Swift SVN r2161
2012-06-07 01:00:06 +00:00
Eli Friedman
ecc56538b3 Add a basic TypeLoc; start threading it through the parser.
Doug, please double-check that this is going in the right direction...



Swift SVN r2146
2012-06-05 00:11:59 +00:00
Eli Friedman
27f8a5ab62 Teach the parser's handling of scopes to handle local types correctly.
Swift SVN r2138
2012-06-04 19:14:58 +00:00
Eli Friedman
33f20a14e0 Add NewReferenceExpr, for allocating class objects.
Swift SVN r1867
2012-05-16 01:36:03 +00:00