Commit Graph

212 Commits

Author SHA1 Message Date
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
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
Doug Gregor
c079874625 Implement parsing, AST, type canonicalization, and type validation for
protocol conformance types, e.g., 'protocol<P, Q>'. A few things
people *might* want to scream about, or at least scrutinize:

  - The parsing of the '<' and '>' is odd, because '<' and '>' aren't
    tokens, but are part of the operator grammar. Neither are '>>',
    '>>>', '<>', etc., which also come up and need to be parsed
    here. Rather than turning anything starting with '<' or '>' into a
    different kind of token, I instead parse the initial '<' or '>'
    from an operator token and leave the rest of the token as the
    remaining operator.
  - The canonical form of a protocol-composition type is minimized by
    removing any protocols in the list that were inherited by other
    protocols in the list, then sorting it. If a singleton list is
    left, then the canonical type is simply that protocol type.
  - It's a little unfortunate that we now have two existential types
    in the system (ProtocolType and ProtocolCompositionType), because
    many places will have to check both. Once ProtocolCompositionTypes
    are working, we should consider whether it makes sense to remove
    ProtocolType.

Still to come: name lookup, coercions.



Swift SVN r2066
2012-05-30 00:39:08 +00:00
Doug Gregor
9096497f0f Rename "dependent type" to "unresolved type" universally. We've been
using the term "unresolved" in expressions for a while, and it fits
for types better than "dependent type."

The term "dependent type" will likely come back at some point to mean
"involves an archetype".



Swift SVN r1962
2012-05-23 19:03:14 +00:00
Doug Gregor
8e1cd3990b Implement substitution of types, which substitutes concrete types for
archetypes. Use this substitution when checking the
variable/function/subscript witnesses during protocol conformance.

This allows us to check the conforms-to relationship for the Range
protocol as we want to express it.


Swift SVN r1945
2012-05-23 00:39:06 +00:00
Doug Gregor
d37602629e Implement parsing, AST, and conformance checking for associated types
in protocols, e.g.,

  protocol Range {
    typealias Element
    func getAndAdvance() -> Element
  }



Swift SVN r1941
2012-05-22 21:45:58 +00:00
John McCall
38bac7c706 Add Builtin.ObjCPointer with accompanying IR-gen support and
wrap it in an 'id' type in the standard library.

Also fix a bug noticed by inspection where initWithTake for
function types wasn't entering a cleanup for the taken value.
This probably doesn't matter for existing possibilities, but
it's potentially important under exceptions.

Swift SVN r1902
2012-05-18 23:40:17 +00:00
Eli Friedman
bb0a98d487 Finish off varargs.
Swift SVN r1892
2012-05-18 00:04:07 +00:00
Eli Friedman
79c3276b07 AST representation for ClassDecls.
Swift SVN r1858
2012-05-15 21:53:38 +00:00
Eli Friedman
d98c55f665 Followup to r1819: there was an issue with that commit I hadn't spotted before related to mismatched types. This commit solves it for FuncDecls, but not SubscriptDecls (see the FIXME).
Swift SVN r1822
2012-05-12 02:52:14 +00:00
Eli Friedman
df9156589b Ban default values in patterns in semantic analysis. Null out default values from patterns for function signatures, since default values are really part of the type, not the pattern, in that case. Fixes <rdar://problem/11406484>.
Swift SVN r1821
2012-05-12 01:53:37 +00:00
Eli Friedman
77fa49ec2b Introduce StructDecl and StructType; use them for structs instead of OneOfDecl/OneOfType. To keep this patch small, I'm leaving in LookThroughOneOf etc. for the moment, but that's next on the agenda.
Swift SVN r1780
2012-05-09 00:27:44 +00:00
Chris Lattner
54c7c6ea74 Add some helper functions to help treat "spaced" lparen and lsquares uniformly.
Swift SVN r1708
2012-05-02 00:30:45 +00:00
Eli Friedman
d5e7784010 Get rid of isModuleScope bit, since we don't like scattering bits across the AST; as a replacement, introduce TopLevelCodeDecl, which provides a DeclContext for all expressions and statements at the top level. <rdar://problem/11259941>.
Swift SVN r1503
2012-04-19 21:22:12 +00:00
Doug Gregor
9ba80f7716 Properly pass the arguments specified for subscripting access down to
the functions that we generate for the getter/setter, so that the
index parameters are in scope. 


Swift SVN r1479
2012-04-18 23:38:10 +00:00
John McCall
6133630544 When type-checking a slice type, "desugar" it to a type that is
generic over the type of the base.  By "generic" I mean that it's
looked up by prepending the word "Slice" to the name of the base
type.

Swift SVN r1464
2012-04-18 08:09:10 +00:00
Doug Gregor
6ce55acfc4 Parse getters and setters for variables.
Swift SVN r1408
2012-04-12 23:59:53 +00:00
Eli Friedman
361481835c Introduce a notion of module scope declarations; use it where appropriate instead of checking for a local declcontext.
Swift SVN r1380
2012-04-11 02:52:40 +00:00
Eli Friedman
065a74d237 Make sure we always explicitly set the DeclContext for a VarDecl built from a pattern.
Swift SVN r1379
2012-04-11 02:24:36 +00:00
Chris Lattner
65b400e30d introduce a new "Builtin.RawPointer" type, which corresponds to LLVM's "i8*" type,
and is just an unmanaged pointer.  Also, introduce a basic swift.string type.

This is progress towards rdar://10923403 and strings.  Review welcome.



Swift SVN r1349
2012-04-10 00:52:52 +00:00