581 Commits

Author SHA1 Message Date
Dmitri Hrybenko
4a3c36f87c Selector-style function parsing: try harder to recover
Fixes two code completion tests.


Swift SVN r7445
2013-08-22 01:08:29 +00:00
Dmitri Hrybenko
ec7b2eb3db Parser: use ParserResult in the interface of parseExpr()
But the implementation of expression parsing still does not propagate the code
completion bits because it uses NullablePtr for results.


Swift SVN r7425
2013-08-21 21:10:09 +00:00
Dmitri Hrybenko
c79bb3204c parsePattern(): try harder to recover. Fixes a code completion test case
Swift SVN r7397
2013-08-21 01:04:11 +00:00
Dmitri Hrybenko
aefbf9db99 Parser: fix diagnostic regression from r7387
Swift SVN r7396
2013-08-21 00:38:11 +00:00
Dmitri Hrybenko
354c5f4a18 Parser: replace diagnose(Tok.getLoc(), ...) -> diagnose(Tok, ...)
We already had the Parser::diagnose(Token, ...) overload, but it was not used
in all these cases.


Swift SVN r7395
2013-08-21 00:26:30 +00:00
Dmitri Hrybenko
dc43b10a40 Fix indentation
Swift SVN r7393
2013-08-21 00:13:17 +00:00
Dmitri Hrybenko
581db767bf Use ParserResult and ParserStatus in tuple and function argument parsing
This finally fixes a few code completion test cases.

This also regresses the error message in one parsing testcase because
previously we would just happily skip those tokens, but now error handling path
is a little bit different and these tokens hit different error handling code.
This can be fixed by parsing a tuple for the selector argument and complaining
if the tuple had more than one element.


Swift SVN r7389
2013-08-20 23:55:00 +00:00
Dmitri Hrybenko
b0dd877454 Use ParserResult in type parsing
Swift SVN r7353
2013-08-20 01:19:31 +00:00
Dmitri Hrybenko
c098cd1476 Parser: convert some pattern parsing functions to ParserResult
Swift SVN r7336
2013-08-19 22:38:34 +00:00
Dmitri Hrybenko
19a90b69f7 Parser: add ParserResult<T> -- a wrapper around the AST node plus extra bits
This allows the parser to recover, create an AST node, return it to the caller
*and* signal the caller that there was an error to trigger recovery in the
caller.  Until now the error was signalled with a nullptr result (any non-null
result was considered a success and no recovery was done in that case).

This also allows us to signal the caller if there was a code completion token
inside the production we tried to parse to trigger delayed parsing in the
caller while doing recovery in the callee.  Until now we could not do recovery
in the callee so that the caller could find the code completion token.

Right now we don't take any advantage of these features.  This commit just
replaces some uses of NullablePtr with ParserResult.


Swift SVN r7332
2013-08-19 22:11:23 +00:00
Dmitri Hrybenko
4ed9594310 Parser / code completion: don't stop on a parse error when parsing function
parameters.  If we see an arrow, try parsing the result type.


Swift SVN r7315
2013-08-17 03:04:23 +00:00
Argyrios Kyrtzidis
015c1a892b Refactor Parser methods to return TypeReprs directly, instead of modifying TypeLocs.
Swift SVN r6328
2013-07-17 14:57:40 +00:00
Doug Gregor
423abc5038 Codify the default argument hack for __FILE__/__LINE__/__COLUMN__.
Teach TuplePatternElt to keep track of the kind of the default
argument: none, normal (provided by calling into the appropriate
callee generator), __FILE__, __LINE__, or __COLUMN__. For the latter
three cases, the type checker forms the appropriate argument as part
of the call. 

The actual default argument expression will only be held in the tuple
pattern element when we've parsed it; it won't be serialized or
deserialized, because only the defining module cares. This is a step
toward eliminate the initialization expression from tuple types.

The extension to TupleShuffleExpr is a hack, which will also be
replicated in ScalarToTupleExpr, until we finally rework the
representation of TupleShuffleExpr (<rdar://problem/12340004>).


Swift SVN r6299
2013-07-16 22:52:38 +00:00
Argyrios Kyrtzidis
37dc84e13c Remove VarargBaseType from TuplePatternElt and introduce a bit in TuplePattern to indicate if there is a vararg.
The semantics of varargs (only for the last element) make it more appropriate as a property of the TuplePattern.
Also free the Parser from needing to construct synthetic types (ArraySlice for type of vararg element) to
accommodate the TypeChecker and move the logic to the TypeChecker. This will be more beneficial when the parser stops
creating types in general.

Swift SVN r6271
2013-07-15 20:21:30 +00:00
Argyrios Kyrtzidis
e67b4e6db0 [Parser] Use TuplePattern::createSimple instead of duplicating it in the parser.
Swift SVN r6233
2013-07-13 00:31:34 +00:00
Doug Gregor
50d6fd0455 Lock down on the use of default values in patterns and types.
Per previous discussions, we only want to allow default values for
uncurried 'func' and 'constructor' parameters, and not for return
types or arbitrary tuple types. Introduce this restriction, fixing
part of <rdar://problem/13372694>. 



Swift SVN r6156
2013-07-11 17:53:05 +00:00
Argyrios Kyrtzidis
a70eff6609 Introduce TypeRepr and related subclasses, that is a representation of a type as written in source.
This the first part for improving source location fidelity for types,
changes to follow:

-The Parser will not create any types, it will just create TypeReprs.
-The type checker will create the types by going through TypeReprs.
-IdentifierType will be removed.

Swift SVN r6112
2013-07-10 14:58:52 +00:00
Dmitri Hrybenko
1c0233efb1 Move lib/Parse/{Parser.h, Scope.h} -> include/swift/Parse/
Swift SVN r6062
2013-07-08 20:36:40 +00:00
Joe Groff
e0afea91b8 Sema: Build a full ApplyExpr for ExprPattern ~= operators.
Sema knows better how to call getLogicValue to get an i1 from a conditional than SILGen does. Fake up a placeholder variable we can slot into a 'expr ~= var' expression, and have the type-checker run on the entire apply expr to generate getLogicValue() conversions on the applied result.

Swift SVN r5995
2013-07-04 19:42:56 +00:00
Joe Groff
dff8813642 Parse bare identifiers in 'var' patterns as new variables.
If we're under a 'var' pattern, reinterpret bare identifier exprs early on as variable bindings so we will be able to add them to the case scope.

Swift SVN r5898
2013-06-29 04:50:16 +00:00
Dmitri Hrybenko
e10ca4cdb6 Verify SourceRanges on Patterns and fix bugs uncovered by this
Swift SVN r5848
2013-06-27 21:54:53 +00:00
Joe Groff
675de63208 Parse matching pattern specific productions at top level.
Parse 'var', '_', and 'is' pattern forms at the top level of a matching pattern context. Keep track of VarPatternDepth in the parser state and raise an error if 'var' appears in a 'var'.

Swift SVN r5839
2013-06-27 18:13:01 +00:00
Joe Groff
2cfd14b4c0 Parse: Stub out parsing of matching patterns using expr parser.
Add a parseMatchingPattern method that just calls down to the expr parser and shoves the expr in an ExprPattern. We'll augment the expr parser with matching pattern productions soon.

Swift SVN r5833
2013-06-27 05:13:37 +00:00
Joe Groff
c293dca016 Parse: Make some code more LLVM-ish.
Continue the LLVM tradition of passing 'this' to module-local static functions as an inscrutable single-capital-letter reference argument instead of a more descriptively named pointer argument.

Swift SVN r5821
2013-06-26 21:33:24 +00:00
Joe Groff
a7db082ee9 Parse: Re-allow '_' in foreach statement patterns.
Missed a spot. Add a test for 'for _ in seq'.

Swift SVN r5814
2013-06-26 17:20:06 +00:00
Joe Groff
ba5d752cbb Parse: Reserve '_' as a keyword.
This makes parsing patterns a bit more straightforward and prevents us from inadvertently accepting '_' as an identifier in a place we shouldn't.

Swift SVN r5806
2013-06-26 00:45:13 +00:00
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
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
Doug Gregor
8372f46fb4 Factor parsing of pattern-tuple-element into its own routine.
This simple refactor makes pattern-tuple-element available for re-use
in closure expressions. As a drive-by, diagnose non-final ellipses via
diagnose() rather than via assert(), the latter being considered
rather unfriendly. Also, take pains to restore AST invariants after
such an error.


Swift SVN r5163
2013-05-13 21:32:33 +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
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
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
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
1dcfff8497 'this' and 'This' are keywords
Swift SVN r3858
2013-01-24 21:54:43 +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
ac4592fdca Part 2 of 2: make "for (;;)" work
This requires a gross but simple contract between pattern parsing and C
for loop parsing where pattern parsing will gracefully back out if and
only if we have a potential C for loop pattern AND assignment is
detected in the pattern (which isn't otherwise allowed outside of the
context of func decls).

If we ever want "for (((;;)))" to work, then this we'll need to
implement the fully general arbitrary token lookahead. But for now, the
common C style "just works".

Swift SVN r3831
2013-01-22 08:59:45 +00:00
Dave Zarzycki
0ea22a81e2 Make pattern parsing match LangRef
Only functions support default values in their patterns. To quote: "Within a
function signature, patterns may also be given a default-value expression."
In other words, only functions are allowed default values.

This fixes: 13057022 "var (x : Int = 123, y : Int = 456)" doesn't init x and y

Swift SVN r3829
2013-01-22 07:46:25 +00:00
Joe Groff
79c7e825f7 Don't <>-include llvm headers.
In response to Chris' feedback on r3322.

Swift SVN r3329
2012-12-03 17:16:29 +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
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
Eli Friedman
0f4d49385c Remove SourceLoc from ArraySliceType.
Swift SVN r2425
2012-07-24 06:29:36 +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
a54c5493da Per discussion with Doug, add UnboundGenericType and BoundGenericType to represent generic types.
Swift SVN r2304
2012-07-04 01:17:16 +00:00
Eli Friedman
2268f7155d Introduce UnresolvedNominalType to represent a generic type without an argument list, and add an argument list to NominalType to represent a generic type including an argument list.
Swift SVN r2299
2012-07-03 22:15:02 +00:00
Doug Gregor
b07b05937a Rework our approach to deduction of generic parameters during overload
resolution. When we see a polymorphic function type, we substitute
"deducible generic parameter" types for each of the generic
parameters. Coercion then deduces those deducible generic parameter
types. This approach eliminates the confusion between the types used
in the definition (which must not be coerced) and the types used when
the generic function is referenced (which need to be coerced).

Note that there are still some terrible inefficiencies in our handling
of these types.



Swift SVN r2297
2012-07-03 21:30:49 +00:00
Doug Gregor
04ffc8c432 Introduce a new abstract type, SubstitutableType, to cover types that
can be substituted, and do some simply renaming to distance ourselves
from archetypes in these code paths.


Swift SVN r2295
2012-07-03 17:37:27 +00:00
John McCall
668f674bfa Split off a PolymorphicFunctionType from FunctionType. I am
*positive* that the behavior here is blatantly wrong in a lot
of places, but I'm going to leave it to Doug to clean up after me. :)

Swift SVN r2255
2012-06-27 00:22:15 +00:00