Commit Graph

784 Commits

Author SHA1 Message Date
David Farler
39bfc123a3 Refactor: Rename Parser::consumeToken, consumeLoc. Add consumeToken API.
These APIs return SourceLocs, and eventually the Parser should consume
tokens, which now include source trivia such as whitespace and comments,
and package them into a purely syntactic tree.  Just a tiny step. NFC.
2016-11-15 16:11:57 -08:00
David Farler
d6e2b58382 Preserve whitespace and comments during lexing as Trivia
Store leading a trailing "trivia" around a token, such as whitespace,
comments, doc comments, and escaping backticks. These are syntactically
important for preserving formatting when printing ASTs but don't
semantically affect the program.

Tokens take all trailing trivia up to, but not including, the next
newline. This is important to maintain checks that statements without
semicolon separators start on a new line, among other things.

Trivia are now data attached to the ends of tokens, not tokens
themselves.

Create a new Syntax sublibrary for upcoming immutable, persistent,
thread-safe ASTs, which will contain only the syntactic information
about source structure, as well as for generating new source code, and
structural editing. Proactively move swift::Token into there.

Since this patch is getting a bit large, a token fuzzer which checks
for round-trip equivlence with the workflow:

fuzzer => token stream => file1
  => Lexer => token stream => file 2 => diff(file1, file2)

Will arrive in a subsequent commit.

This patch does not change the grammar.
2016-11-15 16:11:57 -08:00
Graydon Hoare
1af5c856fa Support #if swift(subminor-version), rdar://problem/28786959 / SR-2908. 2016-10-24 18:12:45 -07:00
Doug Gregor
861d5d33d6 [Parser] Minor location tweak to aid the scope map. 2016-10-17 22:13:52 -07:00
practicalswift
566bfc0d56 [gardening] Fix typos. 2016-10-13 22:19:08 +02:00
Graydon Hoare
77cad91716 Parse and print @available(swift N) / @available(swift, ...) 2016-10-12 11:20:43 -07:00
Graydon Hoare
66f2027f62 s/Version/PlatformVersion/ to availability specs, add LanguageVersion. 2016-10-12 11:20:42 -07:00
Rintaro Ishizaki
07e66a4506 [Parse] Dead code elimination (#5041) 2016-09-29 11:01:28 +09:00
Jordan Rose
1cffaed907 [Parse] 'where' to ',' fix-it should remove a preceding space. (#4944)
rdar://problem/27709302
2016-09-23 10:54:17 -07:00
Graydon Hoare
8970d44675 Add "-swift-version <n>" that sets LangOpts.EffectiveLanguageVersion.
This flag switches the "effective language version" of the compiler,
at least to any version supported (as of this change: "3" or "3.0").

At the moment nothing uses it except the language version build
configuration statements (#if swift(...)) and various other places
that report, encode, or otherwise check version numbers.

In the future, it's intended as scaffolding for backwards compatibility.

Fixes SR-2582
2016-09-20 15:11:37 -07:00
Doug Gregor
7cb130254d [Scope map/parser/AST] Miscellaneous cleanups to avoid producing invalid source ranges.
The scope map relies fairly deeply on having reasonable source ranges
for AST nodes. Fix the construction and query of source ranges in a
few places throughout the parser and AST to provide stronger
invariants.
2016-09-08 14:27:02 -07:00
Doug Gregor
250e6edf7e Merge pull request #4628 from jtbandes/diag-operator
[QoI] improve diagnostics for operator declarations; unify parsing code
2016-09-06 08:38:58 -07:00
Jacob Bandes-Storch
d6590cd781 [QoI] improve diagnostics for operator declarations; unify parsing code 2016-09-04 22:17:29 -07:00
Doug Gregor
d1ce3b82ef Add support for the C-style 'for' statement. 2016-09-02 17:13:05 -07:00
Michael Ilseman
abea7199a7 [SE-0099 Restructuring Condition Clauses] Flip the switch (#3441)
Now that some time has passed, switch the warnings to errors,
completing SE-0099.
2016-07-27 15:00:10 -07:00
Doug Gregor
0b04c0c0a4 Merge pull request #3741 from rintaro/sr-2080-fixit-initializer-delegation
[SR-2080][Parse] Improve fix-it for initializer delegation
2016-07-26 08:22:30 -07:00
Doug Gregor
fdcf45b497 [AST] Introduce factory methods to create CallExprs.
Introduce several new factory methods to create CallExprs, and hide
the constructor. The primary reason for this refactor is to start
moving clients over to the factory method that takes the call
arguments separately from the argument labels. Internally, it
repackages those arguments into a TupleExpr or ParenExpr (as
appropriate) so the result ASTs are the same. However, this will make
it easier for us to tease out the arguments themselves in the
implementation of SE-0111.
2016-07-25 13:27:35 -07:00
Rintaro Ishizaki
e6b38e32e8 [Parse][SR-2080] Improve fix-it for initializer delegation
Suggest inserting `super.` only if it's in designated *class* initializer,
otherwise `self.`.
2016-07-25 21:09:03 +09:00
Jordan Rose
d6a726e6fe SE-0106: Add "macOS" as an alias of "OSX" for #if.
...by canonicalizing it to the known platform name. This isn't a
wonderful answer, but it preserves the invariant that a platform
condition has at most one value.

A later commit will switch which one is the default.
2016-07-07 14:57:57 -07:00
Ben Langmuir
75619c02c4 [CodeCompletion] Improve StmtCondition recovery for code-completion
In the condition expressions of if, while and guard statements we were
throwing away the AST if there was a parse error in the condition, or
the brace statement was missing.  This led to poor code-completion for
unresolved members (enums, options sets) because we couldn't find the
parent expression to type-check.

There are a few minor diagnostic changes because we now do more
type-checking of these conditions, particularly if they end up
containing an unused closure.

SR-2001
2016-07-06 14:08:53 -07:00
Chris Lattner
1b2da8c084 Remove the #setline directive, which was formerly known as #line and is now
known as #sourceLocation.  #setline was an intermediate but never endorsed state.

Upgrade the migration diagnostics for SE-0066 and SE-0049 to be errors instead of warnings.
2016-07-02 16:34:19 -07:00
Chris Lattner
45f2cfaaa0 Implement SE-0099, but where the migration diagnostics are left as warnings
for now.  I'll upgrade them to errors in a week or two to give downstream
projects a chance to update.
2016-07-02 15:44:57 -07:00
Bryan Chan
bb3486df93 Add new _endian() platform condition check; valid values are "little" and "big". 2016-05-24 19:39:15 -04:00
Slava Pestov
170992c39f AST: Add Throws flag and ThrowsLoc to AbstractFunctionDecl
The verifier now asserts that Throws, ThrowsLoc and isBodyThrowing()
match up.

Also, add /*Label=*/ comments where necessary to make the long argument
lists easier to read, and cleaned up some inconsistent naming conventions.

I caught a case where ClangImporter where we were passing in a loc as
StaticLoc instead of FuncLoc, but probably this didn't affect anything.
2016-05-21 12:51:50 -07:00
Ben Langmuir
95cc4525d4 [CodeCompletion] Fix completion in repeat statements
Improve the error recovery so that we get back either a RepeatWhileStmt,
or at least a BraceStmt, which code-completion can use to dig out the
local variables.

rdar://problem/25912182
2016-04-25 14:41:28 -07:00
Xi Ge
152eeaa1fe [Sema] Add a fixit for diagnosis "variable used within its own initial value".
This fixit checks if a decl with the identical name can be found in the parent type
context; if can, we add "self." to try to resolve the issue. rdar://25389852
2016-04-14 15:55:01 -07:00
practicalswift
abfecfde17 [gardening] if ([space]…[space]) → if (…), for(…) → for (…), while(…) → while (…), [[space]x, y[space]] → [x, y] 2016-04-04 16:22:11 +02:00
Ben Langmuir
ff895d2f5e [CodeCompletion] Fix completion in string literal interpolation at top-level
Mostly this was just returning the ParserStatus bits that we got from
parseExprList from parseExprStringLiteral. The rest was just cleaning up
places that didn't handle EOF very well, which is important here because
the code completion token is buried in the string literal, so the
primary lexer will walk past it.

rdar://problem/17101944
2016-03-14 23:13:15 -07:00
Chris Lattner
4992474168 Add support for #sourceLocation in its ratified forms. Switch gyb to produce
the new form.  This keeps accepting #setline for now, but we should rip it out
at some point.
2016-03-11 22:21:42 -08:00
Ben Langmuir
19d13c3aee Reapply "[CodeCompletion] Don't show the loop index before it is visible"
With the tests updated to account for not having the correct behaviour
for brace-stmt items from after the code-completion point.  That part
turns out to be harder to fix.

This reverts commit a5325e6281.
2016-03-09 14:02:51 -08:00
Daniel Duan
9b00e35290 [Parser] refactor nested 'init' diagnostics. NFC
Add better comment. Replace a dyn_cast with cast per Jordan's observation.
2016-03-09 10:38:50 -08:00
Ben Langmuir
a5325e6281 Revert "[CodeCompletion] Don't show the loop index before it is visible"
This reverts commit ce77c868d1.
2016-03-07 22:29:00 -08:00
Ben Langmuir
ce77c868d1 [CodeCompletion] Don't show the loop index before it is visible
for i in <here>            // should *not* show 'i'
for i in ...  where <here> // should show 'i'
for i in ... { <here>      // should show 'i'

Part of rdar://problem/24873625
2016-03-07 22:04:49 -08:00
Daniel Duan
ea0a2d3b8d [Parser][Qol] improve diagnostic with missing "self." in init
This improvement was reported in
[SR-852](https://bugs.swift.org/browse/SR-852).

before:

```
$ cat t.swift
class A {
    init(x: Int) {}

    convenience init() { init(x: 1) }
}

$ swiftc -c t.swift
t.swift:6:13: error: initializers may only be declared within a type
    init(x: 1)
        ^
t.swift:6:17: error: expected parameter type following ':'
    init(x: 1)
            ^
t.swift:6:17: error: expected ',' separator
    init(x: 1)
            ^
           ,
t.swift:6:17: error: expected parameter type following ':'
    init(x: 1)
            ^
t.swift:6:17: error: expected ',' separator
    init(x: 1)
            ^
                                                                                                                                                       ,
```

after:

```
t.swift:6:9: error: missing 'self.' at initializer invocation
    init(x: 1)
    ^
    self.
```
2016-03-03 00:11:21 -08:00
Daniel Duan
ba9809c390 [Parser][SE-0034] deprecate line directive in favor of #setline 2016-03-01 16:33:19 -08:00
Adrian Prantl
310b0433a9 Reapply "Serialize debug scope and location info in the SIL assembler language.""
This ireapplies commit 255c52de9f.

Original commit message:

Serialize debug scope and location info in the SIL assembler language.
At the moment it is only possible to test the effects that SIL
optimization passes have on debug information by observing the
effects of a full .swift -> LLVM IR compilation. This change enable us
to write targeted testcases for single SIL optimization passes.

The new syntax is as follows:

 sil-scope-ref ::= 'scope' [0-9]+
 sil-scope ::= 'sil_scope' [0-9]+ '{'
                 sil-loc
                 'parent' scope-parent
                 ('inlined_at' sil-scope-ref )?
               '}'
 scope-parent ::= sil-function-name ':' sil-type
 scope-parent ::= sil-scope-ref
 sil-loc ::= 'loc' string-literal ':' [0-9]+ ':' [0-9]+

Each instruction may have a debug location and a SIL scope reference
at the end.  Debug locations consist of a filename, a line number, and
a column number.  If the debug location is omitted, it defaults to the
location in the SIL source file.  SIL scopes describe the position
inside the lexical scope structure that the Swift expression a SIL
instruction was generated from had originally. SIL scopes also hold
inlining information.

<rdar://problem/22706994>
2016-02-26 13:28:57 -08:00
Adrian Prantl
255c52de9f Revert "Serialize debug scope and location info in the SIL assembler language."
Temporarily reverting while updating the validation test suite.

This reverts commit c9927f66f0.
2016-02-26 11:51:57 -08:00
Adrian Prantl
c9927f66f0 Serialize debug scope and location info in the SIL assembler language.
At the moment it is only possible to test the effects that SIL
optimization passes have on debug information by observing the
effects of a full .swift -> LLVM IR compilation. This change enable us
to write targeted testcases for single SIL optimization passes.

The new syntax is as follows:

 sil-scope-ref ::= 'scope' [0-9]+
 sil-scope ::= 'sil_scope' [0-9]+ '{'
                 sil-loc
                 'parent' scope-parent
                 ('inlined_at' sil-scope-ref )?
               '}'
 scope-parent ::= sil-function-name ':' sil-type
 scope-parent ::= sil-scope-ref
 sil-loc ::= 'loc' string-literal ':' [0-9]+ ':' [0-9]+

Each instruction may have a debug location and a SIL scope reference
at the end.  Debug locations consist of a filename, a line number, and
a column number.  If the debug location is omitted, it defaults to the
location in the SIL source file.  SIL scopes describe the position
inside the lexical scope structure that the Swift expression a SIL
instruction was generated from had originally. SIL scopes also hold
inlining information.

<rdar://problem/22706994>
2016-02-26 10:46:29 -08:00
gregomni
63f810d2fd Variables in 'case' labels with multiple patterns.
Parser now accepts multiple patterns in switch cases that contain variables.
Every pattern must contain the same variable names, but can be in arbitrary
positions. New error for variable that doesn't exist in all patterns.

Sema now checks cases with multiple patterns that each occurence of a variable
name is bound to the same type. New error for unexpected types.

SILGen now shares basic blocks for switch cases that contain multiple
patterns. That BB takes incoming arguments from each applicable pattern match
emission with the specific var decls for the pattern that matched.

Added tests for all three of these, and some simple IDE completion
sanity tests.
2016-02-24 15:46:36 -08:00
Jordan Rose
6272941c5c Rename "build configurations" to "conditional compilation blocks".
...because "build configuration" is already the name of an Xcode feature.

- '#if' et al are "conditional compilation directives".
- The condition is a "conditional compilation expression", or just
  "condition" if it's obvious.
- The predicates are "platform conditions" (including 'swift(>=...)')
- The options set with -D are "custom conditional compilation flags".
  (Thanks, Kevin!)

I left "IfConfigDecl" as is, as well as SourceKit's various "BuildConfig"
settings because some of them are part of the SourceKit request format.
We can change these in follow-up commits, or not.

rdar://problem/19812930
2016-02-12 11:09:26 -08:00
Slava Pestov
bbbe307980 SIL: Introduce SILDefaultWitnessTable and start plumbing
This will be used to help IRGen record protocol requirements
with resilient default implementations in protocol metadata.

To enable testing before all the Sema support is in place, this
patch adds SIL parser, printer and verifier support for default
witness tables.

For now, SILGen emits empty default witness tables for protocol
declarations in resilient modules, and IRGen ignores them when
emitting protocol metadata.
2016-02-05 20:57:11 -08:00
Chris Lattner
8dedfb31e3 Add support for #file/#line, etc according to SE-0028. __FILE__ and friends
are still accepted without deprecation warning as of this patch.
2016-02-04 14:22:22 -08:00
Ben Langmuir
7eaed61b6f [CodeCompletion] Fix completion after unspaced binary operator
We were miscalculating 'isRightBound' when the RHS was a code-completion
token leading to missing completions in unspaced binary expressions
  1+<here>
  1...<here>

rdar://problem/24278699
2016-02-03 18:46:15 -08:00
Chris Lattner
f6bb09aefb Fix comment typo, NFC. 2016-02-02 22:27:11 -08:00
Chris Lattner
0d57fe5b34 Fix <rdar://problem/24467411> QoI: Using "&& #available" should fixit to comma
This tweet: https://twitter.com/radexp/status/694561060230184960 pointed out
the sad truth that most people don't know that stmt-condition can contain
(including a fixit) when they try to use && instead of commas between clauses.

Before:

t.swift:4:16: error: #available may only be used as condition of an 'if', 'guard' or 'while' statement
  if x == y && #available(iOS 9, *) { }
               ^
t.swift:5:27: error: expected '{' after 'if' condition
  if #available(iOS 9, *) && x == y {}
                          ^
t.swift:5:37: error: braced block of statements is an unused closure
  if #available(iOS 9, *) && x == y {}
                                    ^
t.swift:5:37: error: expression resolves to an unused function
  if #available(iOS 9, *) && x == y {}
                                    ^~

After:

t.swift:4:13: error: expected ',' joining parts of a multi-clause condition
  if x == y && #available(iOS 9, *) { }
            ^~
            ,
t.swift:5:27: error: expected ',' joining parts of a multi-clause condition
  if #available(iOS 9, *) && x == y {}
                          ^~
                          ,
2016-02-02 22:20:20 -08:00
David Farler
3f635d04c7 Reinstante var bindings in refutable patterns, except function parameters.
This reverts commits: b96e06da44,
                      8f2fbdc93a,
                      93b6962478,
                      64024118f4,
                      a759ca9141,
                      3434f9642b,
                      9f33429891,
                      47c043e8a6.

This commit leaves 'var' on function parameters as a warning to be
merged into Swift 2.2. For Swift 3, this will be an error, to be
converted in a follow-up.
2016-01-29 15:27:08 -08:00
Doug Gregor
8336419844 Include completion source location information compound DeclNames.
When one spells a compound declaration name in the source (e.g.,
insertSubview(_:aboveSubview:), keep track of the locations of the
base name, parentheses, and argument labels.
2016-01-25 14:13:13 -08:00
David Farler
c32fb8e7b9 SE-0020: Swift Language Version Build Configuration
Introduce a new "swift" build configuration that guards declarations
and statements with a language version - if the current language version
of the compiler is at least that version, the block will parse as normal.
For inactive blocks, the code will not be parsed an no diagnostics will
be emitted there.

Example:

    #if swift(>=2.2)
      print("Active")
    #else
      this code will not parse or emit diagnostics
    #endif

https://github.com/apple/swift-evolution/blob/master/proposals/0020-if-swift-version.md
rdar://problem/19823607
2016-01-21 16:31:19 -08:00
Doug Gregor
ecfde0e71c Start parsing names with argument labels.
Basic implementatation of SE-0021, naming functions with argument
labels. Handle parsing of compound function names in various
unqualified-identifier productions, updating the AST representation of
various expressions from Identifiers to DeclNames. The result doesn't
capture all of the source locations we want; more on that later.

As part of this, remove the parsing code for the "selector-style"
method names, since we now have a replacement. The feature was never
publicized and doesn't make sense in Swift, so zap it outright.
2016-01-20 17:09:01 -08:00
Chris Lattner
d69a1a00d7 Fix <rdar://problem/21080671> QoI: try/catch (instead of do/catch) creates silly diagnostics
People will keep typing try/catch either due to muscle memory from other languages or
when they are first learning swift.  We now produce a nice error message + fixit of:

t.swift:14:3: error: the 'do' keyword is used to specify a 'catch' region
  try {
  ^~~
  do

instead of spewing out:

t.swift:15:4: error: consecutive statements on a line must be separated by ';'
  } catch { }
   ^
   ;
t.swift:15:5: error: expected expression
  } catch { }
    ^
t.swift:15:11: error: braced block of statements is an unused closure
  } catch { }
          ^
t.swift:14:7: error: expression resolves to an unused function
  try {
  ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.swift:15:11: error: expression resolves to an unused function
  } catch { }
          ^~~
t.swift:14:3: warning: no calls to throwing functions occur within 'try' expression
  try {
  ^
2016-01-17 15:14:35 -08:00