Commit Graph

784 Commits

Author SHA1 Message Date
Chris Lattner
8521916b86 change PatternBindingDecl to be created with a static "create" method instead
of using its ctor directly.  NFC, this is in prep for other changes coming.


Swift SVN r26174
2015-03-16 00:44:52 +00:00
Chris Lattner
a0d02cdcdd Several minor changes:
- Strength reduce isAtStartOfBindingName() to just check for 
   identifier or _ and inline into its two callers.
 - Rename Token::isIdentifierOrNone to isIdentifierOrUnderscore.
 - Teach InVarOrLetPattern about matching patterns, so that the
   parser knows when it is parsing an expression as a matching
   pattern but is not yet inside a let/var pattern.
 - Use newfound knowledge of matching patterns to refine handling
   of unexpected let/var when parsing an expression, but not in a
   pattern context, slightly improving QoI in invalid cases.



Swift SVN r26172
2015-03-15 23:36:05 +00:00
Chris Lattner
c050e810c7 Fix: <rdar://problem/17462274> Is increment in for loop optional?
a trivial oversight in parenthesized c-style-for-loop parsing.



Swift SVN r26169
2015-03-15 23:03:35 +00:00
Chris Lattner
d7c26d8758 Speculatively land the grammar change for <rdar://problem/20167393> revise typed-pattern grammar to make refutable patterns a superset of irrefutable ones
This is still a subject of discussion on swift-dev, but it seems like clearly the right
way to go to me.  If it turns out that this isn't a good direction, I'll revert this and 
subsequent patches built on top of it.



Swift SVN r26168
2015-03-15 22:54:45 +00:00
Chris Lattner
bf73cc23f1 fix <rdar://problem/20167543> "for var x = ..." not parsed as a foreach loop
This was because the ambiguity between c-style and foreach loops wasn't being
properly handled.  Use the canParsePattern() logic to handle this in full 
generality.

Since that logic was unused, dust it off and clean it up a bit.  Similarly,
remove some old vestigates of default argument parsing in tuples and 
old-syntax array handling.



Swift SVN r26164
2015-03-15 21:43:04 +00:00
Chris Lattner
27ef444db5 Remove the "isLet" parameter from the pattern parsing logic. It was (almost)
duplicated by the InVarOrLetPattern state in the Parser object.  Beef 
InVarOrLetPattern up so that we can remove it.

NFC except that we now reject pointless let patterns in foreach loops,
similar to how we reject var patterns inside of let patterns.



Swift SVN r26163
2015-03-15 21:06:37 +00:00
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Chris Lattner
3e3f568179 By far, the most common use of Lexer::getLocForEndOfToken is in
conjunction with .fixItInsert().  As such, introduce a helper named
.fixItInsertAfter() that does what we all want.  Adopt this in various
places around the compiler.  NFC.



Swift SVN r26147
2015-03-15 05:30:04 +00:00
John McCall
0802e85975 Implement the naked 'do' statement.
For now, we assume that 'while' after the braces starts
a do/while rather than being an independent statement.
We should disambiguate this, or better, remove do/while.

Tests later.

Swift SVN r26079
2015-03-13 01:58:42 +00:00
Denis Vnukov
9e76e2ba50 Minor (swift migrator related fixes):
Corrected several places where compiler generated AST nodes were not properly 
marked as implicit.

For interpolated strings also fixed string segment locations and made sure 
the first and last segments are preserved in AST even if they are empty.



Swift SVN r25983
2015-03-11 18:08:36 +00:00
Xi Ge
c0bf1f54ff [CodeCompletion] Further support the context-sensitivity of
auto-completing @attributes. By delaying the handling of code completion token after the entire decl being parsed, we know
what are the targets of the attribute to finishe, thus, only suggesting those applicable attributes.

Swift SVN r25938
2015-03-10 18:40:39 +00:00
Chris Willmore
42dba24464 Don't allow '{' at start of if-condition
If '{' is encountered immediately after 'if', assume that the condition
is missing. Apply the same treatment to while, do-while, for-in, and
switch. This way we're not trying to re-parse, backtrack, repurpose
misparsed closure bodies, etc. in those cases. Users who want to write a
condition that starts with '{' can wrap it in parens.

Addressing feedback re <rdar://problem/18940198>.

Swift SVN r25747
2015-03-04 05:39:49 +00:00
Jordan Rose
c6bb14f71f [Parse] Use a clearer condition for inactive #if blocks.
Fix-up to r25567 suggested by Denis. No functionality change.

Swift SVN r25601
2015-02-27 16:51:01 +00:00
Jordan Rose
9b5134d10a [Parse] Don't add local types from inactive #if blocks to the master list.
Otherwise we'll get all the way to IRGen and then try to emit them. And try
to reference them in serialization.

rdar://problem/19935034

Swift SVN r25567
2015-02-26 23:37:20 +00:00
Chris Lattner
da1dfcd55c implement <rdar://problem/19150249> Allow labeled "break" from an "if" statement
This doesn't allow 'continue' out of an if statement for the same reason we don't
allow it on switch: we'd prefer people to write loops more explicitly.



Swift SVN r25565
2015-02-26 22:32:30 +00:00
Chris Lattner
58aadafe82 follow up to improve r25511.
Swift SVN r25525
2015-02-25 01:27:40 +00:00
Chris Lattner
02f5c325d4 fix <rdar://problem/19939746> Improve error recovery for malformed if statements
Swift SVN r25511
2015-02-24 20:39:06 +00:00
Chris Lattner
3357ee21f3 Revert r25417, r25408, and r25370. This adds back back the warning for
un-type-annotated AnyObject binding in "if let", and allows general 
patterns in if-let.

This also reverts some unrelated QoI improvements that went in with those
patches, but I'll add those back next.


Swift SVN r25507
2015-02-24 18:55:54 +00:00
Chris Willmore
e8c8cfaa3c When recovering from missing switch statement subject, discard
diagnostics that arose from attempting to parse the switch statement
body as an expression.

Swift SVN r25448
2015-02-21 00:23:10 +00:00
Chris Willmore
776c6a1bd2 Use RAII with DiagnosticTransaction instead of manually opening and
committing transactions.

Swift SVN r25440
2015-02-20 22:51:20 +00:00
Chris Willmore
9134d7032d When parsing if/while statements, if the condition turns out to be a
closure, reparse it as a brace statement instead of attempting to
repurpose the closure body. Suppress diagnostics from the initial
condition parse until we're committed to that parse.

<rdar://problem/18940198> Fuzzing Swift: performTypeChecking(...) crashes in ContextualizeClosures::walkToExprPre(...): Assertion failed: "Incorrect parent decl context for closure"

Swift SVN r25438
2015-02-20 22:06:35 +00:00
Chris Lattner
dc46dd1a5a improve diagnostic and add fixit for if/let pattern error, as recommended by Dmitri.
Swift SVN r25417
2015-02-20 04:49:31 +00:00
Chris Lattner
fee103c42c Reapply two tweaks to if/let handling:
two logically independent but related patches conflated together: 
 - Improve error recovery for malformed if/let conditions, particularly 
   when the user uses "," instead of &&. Add testcases for error recovery 
   requested by Jordan.

- Add a syntactic requirement that the pattern of an if/let condition be 
  a simple identifier or _. We allow slightly broader patterns here now, 
  but they will change in the future when refutable patterns are allowed. 
  It is best to be narrow and then open it up later so we can do a great
  job of QoI then.

This includes the changes to the stdlib directory that I forgot to commit
with the patch the previous time.



Swift SVN r25408
2015-02-20 02:15:25 +00:00
Erik Eckstein
a44814b776 Revert 25371: "[25371] two logically independent but related patches conflated together:"
It broke the stdlib build.



Swift SVN r25372
2015-02-18 17:30:31 +00:00
Chris Lattner
6ad3a975db two logically independent but related patches conflated together:
- Improve error recovery for malformed if/let conditions, particularly
  when the user uses "," instead of &&.  Add testcases for error recovery
  requested by Jordan.

- Add a syntactic requirement that the pattern of an if/let condition be
  a simple identifier or _.  We allow slightly broader patterns here now,
  but they will change in the future when refutable patterns are allowed.
  It is best to be narrow and then open it up later so we can do a great 
  job of QoI then.



Swift SVN r25371
2015-02-18 06:16:06 +00:00
Chris Lattner
3e723974cc Implement support for a leading boolean condition in an if-let condition,
resolving <rdar://problem/19797158> Swift 1.2's "if" has 2 behaviours. They could be unified.



Swift SVN r25369
2015-02-18 04:54:27 +00:00
Denis Vnukov
6776cb2e95 Fix for rdar://problem/19582877, Fuzzing Swift: Parser::parseTopLevel() crashes in swift::verify(...)
Brace statement created for wrapping IfConfig inside TopLevelCodeDecl does not have 
closing brace, so we should use the previous token’s location as right brace location.



Swift SVN r24797
2015-01-28 17:48:36 +00:00
Denis Vnukov
6d232296d4 Fix for rdar://problem/19540536, Fuzzing Swift: assertion in parser: Assertion failed: (Tok.getKind() == tok::kw_let || Tok.getKind() == tok::kw_var)
Added an error in c-style for statement parsing for cases when we don’t find ‘var’ or ‘let’ after we parsed declaration attributes.



Swift SVN r24761
2015-01-27 22:20:00 +00:00
Jordan Rose
36aab716da [Parse] Disallow computed accessors in C-style for-loop variables.
This improves recovery for "for var i {}".

Swift SVN r24662
2015-01-23 00:32:45 +00:00
Chris Lattner
7faecf021e minor cleanups along the way, NFC.
Swift SVN r24347
2015-01-10 01:58:25 +00:00
Chris Lattner
763bb6386f Implement parser, AST representation, type checker, etc support for generalized
if-let statements (also while and var, of course) that include multiple bindings
and where clauses.

SILGen support still remains, it currently just asserts on the new constructs.



Swift SVN r24239
2015-01-07 07:03:02 +00:00
Chris Lattner
54a49cbd85 rearrange code to put simple case + early exit first, NFC.
Swift SVN r24231
2015-01-06 23:44:37 +00:00
Chris Lattner
9414378210 change maintenance of "ParentPattern" in VarDecl to be implicitly handled by PatternBindingDecl itself, instead of having all clients do it.
Swift SVN r23918
2014-12-13 07:35:34 +00:00
Chris Lattner
274310b888 Fix a few VarDecls that are initialized with a PatternBindingDecl but which aren't
getting their ParentPattern set up properly.


Swift SVN r23915
2014-12-13 07:09:30 +00:00
Chris Lattner
11a84793ec Add a new bit to VarDecl to track cases where a vardecl gets an
initializer but has no "parent" PatternBindingDecl or Pattern (i.e.
paramdecls).  This is currently set on decls in the pattern of 
foreach loops and case patterns, but I'll add it to other places I
find as well.

NFC since this bit is only set and not read, just more yak shaving.


Swift SVN r23910
2014-12-13 06:25:14 +00:00
Manman Ren
16cc4dfa65 Revert r23713
Swift SVN r23739
2014-12-05 18:41:27 +00:00
Manman Ren
d0068877f5 [PGO] Add SILMetadata for branch weights.
SILMetadata is the base class with a single enum member (MDKind).
SILBranchNode is the derived class with additional members:
  unsigned NumOperands
  an array of uint32_t

A static member function SILBranchNode::get is implemented to get or create
SILBranchNode. All SILMetadata created are uniqued and saved in SILModule's
member variable:
  llvm::FoldingSet<SILMetadata> Metadatas

Usage of SILMetadta by SILInstruction is captured in SILModule's member variable:
  llvm::DenseMap<const SILInstruction *, SILMetadata *> MetadataStore
This is similar to LLVM's Metadata. Another option is to add a SILMetadata* to
SILInstruction. The disadvantage is the waste of space when we don't have PGO on.

This commit also enables parsing and printing of SILMetadata.

We add keyword sil_metadata to define SILMetadata:
  sil_metadata !0 = {"branch_weights", 3, 5}

For parsing, we add a map in SILModule
  llvm::DenseMap<unsigned, SILMetadata *> NumberedMetadata
that maps from ID to SILMetadata* to help matching usage of "!id" in SILFunction
with definition of "!id" in sil_metadata section.

For printing, we assign IDs to SILMetadata at SILModule scope, we then pass in
an optional argument of
  llvm::DenseMap<const SILMetadata *, unsigned> *MetadataMap
to SILFunction::print in order to get the ID of SILMetadata used in
SILInstruction.

Post-commit review will be appreciated.

rdar://18269754


Swift SVN r23713
2014-12-05 01:47:11 +00:00
Denis Vnukov
7b3daab9b3 Fix for rdar://problem/19016271, Fuzzing swift: Swift parser/validator crash.
When BraceStmt is generated to represent missing {...} block it should be marked as 
implicit and be associated with the owning statement.




Swift SVN r23531
2014-11-21 20:35:44 +00:00
Denis Vnukov
448822b1c4 Fixed an issue in IfConfigStmt parsing leading to source ranges verification assertions (rdar://problem/18251200).
The change also includes replacing BraceStmt* reference in IfConfigStmtClause structure with a simple list of clause elements.




Swift SVN r22868
2014-10-21 22:48:20 +00:00
Graham Batty
7676c8240d Add compile time config flag for detecting objc-interop.
Swift SVN r22605
2014-10-08 17:09:34 +00:00
Joe Groff
ad78204c50 Parser: Explicitly ban closures at the beginning of statements.
This already can't happen in most circumstances because of trailing closures, but we didn't explicitly disallow it at the beginning of a BraceStmt or following a statement production. Fixes the parser part of rdar://problem/17850752 (though there's a type checker bug there too).

Swift SVN r21663
2014-09-03 00:37:55 +00:00
Dmitri Hrybenko
caabe782e4 Parser: when recovering from an incomplete 'for' statement, don't set the
source location of synthesized AST nodes to point to EOF.  This causes the
source range to span more tokens than the parent node does.

rdar://17630645


Swift SVN r21282
2014-08-19 15:56:52 +00:00
Dmitri Hrybenko
8234c70f88 Code completion: fix delayed parsing of closures
The delayed parsing was in place, but the expressions were being thrown away by
"recovery" in the parser.

rdar://16274593


Swift SVN r20151
2014-07-18 11:43:38 +00:00
Chris Lattner
fe30b66a93 move "override" onto the new fangle decl modifier code, instead of being a virtual
attribute.  As part of this, introduce a new "NotSerialized" flag in Attr.def.
This eliminates a bunch of special case code in the parser and elsewhere for handling
this modifier.



Swift SVN r19997
2014-07-16 01:23:58 +00:00
Doug Gregor
2f3f6acf21 Make "true" and "false" Boolean literal constants for the BooleanLiteralConvertible protocol.
Introduce the new BooleanLiteralConvertible protocol for Boolean
literals. Take "true" and "false" as real keywords (which is most of the
reason for the testsuite churn). Make Bool BooleanLiteralConvertible
and the default Boolean literal type, and ObjCBool
BooleanLiteralConvertible. Fixes <rdar://problem/17405310> and the
recent regression that made ObjCBool not work with true/false.


Swift SVN r19728
2014-07-09 16:57:35 +00:00
Adrian Prantl
400f1774e9 Add support for a #line directive.
This patch extends the syntax with a new #line directive that is inspired
by the homonymous CPP directive. It can be specified in all locations a #if
is legal (Stmt, Decl).

Semantics
---------

#line 42 "file.swift"
This makes diagnostics and debug information behave as if the subsequent
lines came from file.swift+42.

#line // without arguments
This switches back to the main source file and the switches back to the
normal line numbering. Any previous #line directives will result in gaps
in the main file.

Rationale
---------

LLDB and the REPL need this for making expressions that are entered into
the expression evaluator or REPL debugable. For more info see
<rdar://problem/17441710> Need #line directive or something similar so we can enhance the debugging of expressions and REPL

Also, I believe the stdlib would benefit from this and it would allow us
to get rid of the line-directive wrapper script.

Swift SVN r19384
2014-06-30 23:50:11 +00:00
Doug Gregor
461cde049a Teach parseMatchingToken() to set a matched location even when it wasn't matched.
We were working around this in several different places, which was
error-prone (see <rdar://problem/17479771>). This way, we always have
usable left/right delimiter locations.

Swift SVN r19292
2014-06-27 15:17:05 +00:00
Dmitri Hrybenko
71427b241a Parser: don't refer to 'default:' as 'case' in diagnostics
rdar://16846052

Swift SVN r19090
2014-06-23 13:48:14 +00:00
Argyrios Kyrtzidis
24bbc2d0a5 [Parser] Fix infinite loop when there is a syntax error inside an uncovered statement in a switch.
rdar://17303380

Swift SVN r19031
2014-06-20 04:37:15 +00:00
Joe Pamer
a17cec6cb2 Some test and diagnostic cleanup:
- Mine conjunction constraints for constraint failure data. (rdar://problem/16833763)
- Rather than crash, add a diagnostic to signify a missing user constraint. (rdar://problem/16747055) I don't have a deterministic repro for this to include as a test, but users hit it from time to time, I'd like to address this issue holistically, and we're hoping that the new diagnostic will help us collect isolated repros.
- As promised, remove the temporary "compiler_submit_version" build configuration predicate in time for WWDC. (rdar://problem/16380797)

Swift SVN r17705
2014-05-08 18:46:08 +00:00