Commit Graph

2017 Commits

Author SHA1 Message Date
Doug Gregor
b5a848e8b2 <rdar://problem/11926374> Only introduce the implicit, memberwise struct constructor when there is no similar constructor within the struct declaration.
For the implicit memberwise struct constructor to be suppressed, one
has to write a constructor with the same parameters (names, types, and
order) as the instance variables of the struct.


Swift SVN r4819
2013-04-19 00:10:46 +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
Joe Groff
6ed98ac0e5 Parse [objc_block] attribute on function types.
So we can use it in tests.

Swift SVN r4722
2013-04-13 17:05:35 +00:00
Chris Lattner
af3b55c1f0 Further reinforce TopLevelCodeDecl as the container for top level code.
This nests top level PatternBindingDecls (in "main modules") under TopLevelCodeDecls,
instead of having them live in a translation unit.  They contain code that is executed,
so they should be in a TLCD.


Swift SVN r4668
2013-04-10 23:13:22 +00:00
Chris Lattner
1ee0bed38f Clean up Parser::parseBraceItemList even more:
Extend the existing "isTerminatorForBraceItemListKind" logic to
handle the special case for top level code, instead of having
weird logic dumped in the middle of parseBraceItemList with no comments.

This logic is still seriously dubious, but at least it is out of the way
instead of dump into already really complex logic.


Swift SVN r4665
2013-04-10 22:24:08 +00:00
Chris Lattner
a00464dde1 refactor the logic that determines whether newly parsed top-level decls need to
be immediately run by the REPL to live in one simple place, out of the braceitem
parsing loop.


Swift SVN r4663
2013-04-10 22:06:07 +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
ac23437886 Replace infix attributes with operator decl lookup
During name binding, associate func decls with operator decls. When parsing SequenceExprs, look up operator decls to determine associativity and precedence of infix operators. Remove the infix_left and infix_left attributes, and make the infix attribute a simple declared attribute [infix] with no precedence.

Operator decls are resolved as follows:

- If an operator is declared in the same module as the use, resolve to the declaration in the current module.
- Otherwise, import operator declarations from all imported modules. If more than one declaration is imported for the operator and they conflict, raise an ambiguity error. If they are equivalent, pick one arbitrarily.

This allows operator declarations within the current module to override imported declarations if desired or to disambiguate conflicting operator declarations.

I've updated the standard library and the tests. stdlib2 and some of the examples still need to be updated.

Swift SVN r4629
2013-04-07 02:43:03 +00:00
Chris Lattner
28774bf9fc Come full circle on TopLevelCodeDecl, making top level stmts and exprs each get their
own TLCD.  This is important to preserve the ordering of stmt and expr w.r.t. 
PatternBindingDecls that initialize the decls.

We keep the BraceStmt wrapping it to make it more similar to other decls
though.


Swift SVN r4626
2013-04-06 21:58:26 +00:00
Chris Lattner
b4fd6dd04a Change TopLevelCodeDecl to allow it to hold a sequence of different exprs and statements in one unit, wrapping them into a BraceStmt. This makes it more similar to other decls (e.g. funcdecl, ctor decls, etc) and will be useful for future sil work.
Unfortunately, this regresses the repl when expressions like (1,2) are entered. This is because the repl is violating some invariants (forming dags out of ASTs, making ASDAG's which upset the type checker).  I'm going to fix this next, but can't bring myself to do it in the same commit.



Swift SVN r4617
2013-04-05 22:33:14 +00:00
Joe Groff
aeeda4ee12 Parser: Parse operator decls.
At the top level, if 'operator' is followed by 'infix', 'prefix', or 'postfix', consider it a contextual keyword, and parse an operator decl following it that looks like:

  operator {infix|postfix|prefix} <+> {
    attributes…
  }

Prefix and postfix operator decls currently admit no attributes. Infix operators have 'associativity {left|right|none}' and 'precedence <int>' attributes.

This patch implements parsing for operator declarations but does not yet attach the declared attributes to func decls for the operators.

Swift SVN r4596
2013-04-03 23:30:50 +00:00
Joe Groff
ef6d33039e Mark fallthroughs with [[clang::fallthrough]].
Use [[clang::fallthrough]] instead of informal /*fallthrough*/ comments.

Swift SVN r4574
2013-04-02 20:51:38 +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
Doug Gregor
bd61ca5927 Diagnose multiple statements/declarations on the same line that are not separated by a semicolon.
Swift SVN r4364
2013-03-13 01:32:30 +00:00
Joe Groff
062ad267c4 Value-only switch statements.
Implement switch statements with simple value comparison to get the drudge work of parsing and generating switches in place. Cases are checked using a '=~' operator to compare the subject of the switch to the value in the case. Unlike a C switch, cases each have their own scope and don't fall through. 'break' and 'continue' apply to an outer loop rather to the switch itself. Multiple case values can be specified in a comma-separated list, as in 'case 1, 2, 3, 4:'. Currently no effort is made to check for duplicate cases or to rank cases by match strength; cases are just checked in source order, and the first one wins (aside from 'default', which is branched to if all cases fail).

Swift SVN r4359
2013-03-12 04:43:01 +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
Joe Groff
687c61ad1b REPL: Bind top-level expressions to result vars.
If a REPL input parses to an expression, bind it to the next available variable 'r<n>', and print the result as if it were a name binding. Don't bind a variable if the expression consists of a lone DeclRef, and don't print the binding if it has void type.

Swift SVN r4201
2013-02-25 23:21:34 +00:00
Dave Zarzycki
d2ba334949 Simplify parseMatchingToken()
Swift SVN r3966
2013-02-06 06:19:25 +00:00
Dave Zarzycki
3a91cfd7ca attributes should work with behave consistently with comma separated var decls
Swift SVN r3947
2013-02-05 04:43:36 +00:00
Doug Gregor
03b1689b60 Import C enumerations into usable struct types in Swift.
Import C enumeration types as either structs wrapping the underlying
integral type (when the C enumeration type has a name) or as the
underlying integral type (when the C enumeration type has no
name). The structs have a constructor from the underlying integral
type, so one can write, e.g., NSStringCompareOptions(0) to get a
zero-valued enumeration.

Enumerators are imported as a global read-only properties.

Once oneofs start to work, we'll have a way to map some enumeration
types to oneofs, either via a Clang attribute or by sniffing out
NS_ENUM (most likely both).

Once we have static data members of structs working, we'll replace the
global constants with prefix-stripped static variables within the
struct, so we can use ".foo" notation with them.

Once we have constant declarations, we'll map to those instead of
properties.

We can add |, &, and ~ operations are part of
<rdar://problem/13028799> and have not yet been implemented.

Fixes <rdar://problem/13028891>.


Swift SVN r3945
2013-02-05 00:01:27 +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
d7cc4b4a91 Reclaim "in" as an identifier
In Swift the "in" keyword is really a form of punctuation, and highly
context specific punctuation at that. It never begins a statement, nor
does the grammar require it be statement keyword. The grammar also
doesn't use it outside of for-each loops, and its use within a for-each
loop is highly unambiguous.

Thanks to Chris for the performance related feedback. This improves the
performance of getter/setter parsing as well.

Swift SVN r3880
2013-01-26 01:49:18 +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
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
5be0d3d680 Add missing prefix attribute
Swift SVN r3843
2013-01-23 21:13:09 +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
Dave Zarzycki
523c4bc789 Disable operators in nonglobal scopes other than protocols
Operators in non-global scopes other than protocols don't work. Disable
them for now. We can always revisit this later.

This "fixes": 13002566 Operators not found in structs

Swift SVN r3828
2013-01-22 04:45:39 +00:00
Dave Zarzycki
2f31759280 Remove SemiStmt class
We have no intention of ever supporting actual semicolon statements
(separators, statements no), nor do we ever want to because that would
mean the behavior of the program would potentially change if semicolons
were naively removed.

This patch tracks the trailing semicolon now in the decl/expr/stmt,
which will enable someone to write a good "swift indent" tool in the
future.

Swift SVN r3824
2013-01-22 00:25:26 +00:00
Dave Zarzycki
9eb53f37f5 12641063 Fix the double-indent problem with properties
Swift SVN r3822
2013-01-21 22:43:19 +00:00
Dave Zarzycki
c80639270a Function declarations must have bodies
Back when we started the project, we didn't have modules, a standard
library, or even the "asmname" attribute. Not requring function bodies
provided a quick way to prototype the language. We're long past that day
now.

If you still want or need a pure declaration, the [asmname="..."]
attribute still does not require a function body.

This fixes:
13036833 Syntax ambiguity between selector function decl syntax and top-level code

Swift SVN r3821
2013-01-21 22:42:17 +00:00
Dave Zarzycki
572f7686e9 Minor attribute parsing cleanup
Swift SVN r3819
2013-01-21 02:14:23 +00:00
Dave Zarzycki
86fd48456d Clean up var decl parsing
This works now instead of giving a trail of parse errors:
  var x : Int, y : Int { get { return 42 } }

This is now an explicit error rather than a trail of parse errors:
  var x, y : Int { get { return 42 } }

Swift SVN r3811
2013-01-19 19:37:50 +00:00
Dave Zarzycki
cd5eac535a Adopt a consistent comma parsing style
Swift SVN r3810
2013-01-19 19:37:47 +00:00
Dave Zarzycki
b6ba3992fc Mark invalid decls as erroneous and improve error location reporting
This removes a big/gross validation loop at decl parse time that
attempted to catch errors that should have been caught earlier.

Swift SVN r3775
2013-01-16 09:07:39 +00:00
Dave Zarzycki
d0a0d1bce9 Remove fixed FIXME
If I scrolled down more before the last commit, I would have see this.

Swift SVN r3774
2013-01-16 05:36:30 +00:00
Dave Zarzycki
6504d354ae Catch more invalid ctor/dtor/subscript decls during parsing
Found after "subscript(idx : Int) -> Int {}" was accepted at global scope.

Swift SVN r3773
2013-01-16 04:58:52 +00:00
Dave Zarzycki
d22dcc968b Fix copy-and-paste extension parsing error message
Swift SVN r3751
2013-01-11 19:21:00 +00:00
Doug Gregor
2b2b2cfc31 Replace the constructor 'alllocates_this' attribute with an 'allocate-this' expression.
By splitting out the expression used to allocate 'this' (which exists
in the AST but cannot be written in the Swift language proper), we
make it possible to emit non-allocating constructors for imported
Objective-C classes, which are the only classes that have an
allocate-this expression.


Swift SVN r3558
2012-12-20 15:28:37 +00:00
Jordan Rose
5ea8de9094 Match {}, (), and [] during parse recovery (skipUntil*).
<rdar://problem/12456304>

Swift SVN r3517
2012-12-16 22:53:13 +00:00
Jordan Rose
427be94945 Add the [iboutlet] and [ibaction] attributes.
Currently only used for parsing. The immediate intent of these attributes is
to have them behave like [objc] for the purpose of emitting method
implementations; however, they are semantically distinct and should only be
used to expose outlets and actions to Interface Builder.

Swift SVN r3416
2012-12-08 00:16:03 +00:00
Doug Gregor
d3f6890299 Promote the clonePattern() static to Pattern::clone(), since this is common.
Swift SVN r3381
2012-12-06 18:14:53 +00:00
Doug Gregor
a221a6986b Rename the "allocating" attribute to "allocates_this".
Swift SVN r3353
2012-12-04 20:20:58 +00:00
Doug Gregor
08c9b5c7b2 Allocating constructors are expected to allocate and assign 'this' on their own.
This implementation is very lame, because we don't currently have a
way to detect (in Sema or SIL) where 'this' gets uniquely assigned,
and turn that assignment into initialization.

Also, I'm starting to hate the name 'allocating' constructor, because
it's the opposite of the Itanium C++'s notion of the allocating
constructor. Will think up a better name.




Swift SVN r3347
2012-12-04 01:06:30 +00:00
Joe Groff
759f18a329 Consume trailing semicolons at top level.
Instead of writing in an awkward special case for SemiStmt in ParseStmt, apply the existing semicolon-eating syntax in ParseDecl for types to the toplevel. Suggested by Jordan re: r3336.

Swift SVN r3342
2012-12-04 00:21:50 +00:00
Joe Groff
f2437785be Update some bogus-looking comment text in ParseDecl.cpp
Swift SVN r3101
2012-11-02 22:54:39 +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