Commit Graph

2357 Commits

Author SHA1 Message Date
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
cdb1df51a3 Code completion: completion of type-ident in type contexts (like Foo.#^A^#)
This introduces the required code completion callbacks which pass partially
parsed TypeReprs to code completion.  These types can refer to generic function
parameters.  Because we need to typecheck these types, we need to typecheck
generic parameters first.  Because exposing fine-grained typechecker interface
just for code completion is bad, we create a function declaration based on the
limited information we have (i.e., just the function name and generic
parameters) and pass that to the typechecker.  This approach (in theory) should
work uniformly for function decls and nominal type decls, but the nominal type
decl case is not tested yet.  Eventually we will also want to use a similar
approach for normal parser recovery as well.


Swift SVN r7313
2013-08-17 02:35:32 +00:00
Dmitri Hrybenko
5b952b080a Rename OOD and OOED variables that used to stand for OneOfDecl and
OneOfElementDecl


Swift SVN r7283
2013-08-16 18:11:28 +00:00
Dmitri Hrybenko
4dbac4ee42 Parser: improve recovery and diagnostics for "extension <type> {}" where <type>
is not a type-identifier

Just parse type-simple instead and let the type checker diagnose the invalid
cases.  The only exception is when a tuple becomes a ParenType: it is
canonically equivalent to the wrapped type, and thus the parser uses the
TypeRepr to diagnose that case.


Swift SVN r7275
2013-08-16 01:52:59 +00:00
Dmitri Hrybenko
23d48b6b3b Use NullablePtr in more decl parser functions
Swift SVN r7274
2013-08-16 00:46:26 +00:00
Dmitri Hrybenko
f49852e875 Parser: don't drop the extension from the AST if any declarations inside had
a parse error


Swift SVN r7273
2013-08-16 00:16:53 +00:00
Dmitri Hrybenko
0fdb8acf09 Parser: don't drop the whole nominal decl from the AST if any member had a
syntax error

Will be tested by code completion.


Swift SVN r7272
2013-08-15 22:33:23 +00:00
Dmitri Hrybenko
2786c30dd8 Code completion: add a parser hook to inform code completion about generic
parameters while parsing the function signature.  Generic parameters are not
accessible at that time through the AST node, because the FuncDecl AST node was
not constructed yet.


Swift SVN r7222
2013-08-14 02:16:55 +00:00
Dmitri Hrybenko
536723eca4 Code completion: implement delayed parsing of all (?) declarations
This allows us to show generic parameters in:
struct S<T> {
  func f(a: #^A^#
}

And show the type Z in:
struct S {
  func f(a: #^A^#
  typealias Z = Int
}


Swift SVN r7216
2013-08-13 23:53:39 +00:00
Dmitri Hrybenko
70f2b64ad9 Add CharSourceRange -- a half-open character range, which will be used in IDE
integration

Motivation: libIDE clients should be simple, and they should not have to
translate token-based SourceRanges to character locations.

This also allows us to remove the dependency of DiagnosticConsumer on the
Lexer.  Now the DiagnosticEngine translates the diagnostics to CharSourceRanges
and passes character-based ranges to the DiagnosticConsumer.


Swift SVN r7173
2013-08-12 20:15:51 +00:00
Dmitri Hrybenko
ca5351e4b6 Fix bad recovery in Parser::parseList() and Parser::parseDeclUnionElement()
Now parseList() always sets RBLoc, which fixes the crash.
parseDeclUnionElement() now skips to the next decl, instead of ":", which may
not appear in the file at all.


Swift SVN r7117
2013-08-10 00:44:09 +00:00
Dmitri Hrybenko
dc655eb1cb Remove more abuse of SourceLoc::Value::getPointer()
Swift SVN r7109
2013-08-09 22:35:33 +00:00
Chris Lattner
03c9ade9b4 Fix rdar://14690497, diagnosing precedence declarations that are
outside the allowed range.




Swift SVN r7106
2013-08-09 21:58:43 +00:00
Dmitri Hrybenko
48faba29f3 Simplify parser constructors
Constructor delegation in parser was useless, because the code was split
between the constructors arbitrarily.

There was no need to pass down IsMainModule because the parser could figure
that out on its own.  Also rename it to allowTopLevelCode() to better describe
what it actually affects.


Swift SVN r7098
2013-08-09 20:43:21 +00:00
Dmitri Hrybenko
de59d8dcd4 Remove unneeded llvm:: qualifier for llvm::StringRef and llvm::SmallVector
Swift SVN r7089
2013-08-09 18:41:46 +00:00
Dmitri Hrybenko
49e15b33e7 Remove the public Lexer::getBufferEnd() function which breaks encapsulation
of the source buffer


Swift SVN r7084
2013-08-09 18:10:29 +00:00
Dmitri Hrybenko
02cf73dc30 Lexer: remove redundant parameters from the sublexer constructor
Swift SVN r7073
2013-08-09 00:15:01 +00:00
Dmitri Hrybenko
41723aeb30 Code completion: correctly delay parsing of the function body that did not have
a closing brace.

Fixes two bugs:
* delayed parsing was not correctly skipping over the function body because it
  stopped at the 'var' decl;
* parser was not creating a BraceStmt for the function body if it could not
  find the closing brace.


Swift SVN r7062
2013-08-08 22:57:25 +00:00
Jordan Rose
093a428ca9 Add parsing support for the [exported] attribute.
As discussed, this is an interim syntax for re-exports:
  import [exported] Foundation
In the long run, we're probably going to use the same syntax as access
control for this, but that hasn't been designed yet.

Swift SVN r7050
2013-08-08 19:09:10 +00:00
Jordan Rose
35f84dfbee Remove "import foo, bar" syntax.
We never really discussed this and it doesn't really buy us much. If we
want to have a compact way to import many things, it may not even end
up looking like this.

Swift SVN r7015
2013-08-07 22:56:58 +00:00
Argyrios Kyrtzidis
d6b048dfe0 [Lexer] Refactor lexing of interpolated strings.
Decouple splitting an interpolated string to segments, from encoding the string segments.
This allows us to tokenize or re-lex a string literal without having to allocate memory for
encoding the string segments when we don't need them encoded.

Swift SVN r6940
2013-08-06 14:59:01 +00:00
John McCall
36aa6c2645 alloc_stack needs to return two values like alloc_box.
The current implementation of dealloc_stack in IR-gen is a
no-op, but that's very much wrong for types with non-trivial
local allocation requirements, e.g. archetypes.  So we need
to be able to do non-trivial code here.  However, that means
modeling both the buffer pointer and the allocated address
in SIL.

To make this more type-safe, introduce a SIL-specific
'[local_storage] T' type that represents the required
allocation for locally storing a T.  alloc_stack now returns
one of those in additon to a *T, and dealloc_stack expects
the former.

IR-gen still implements dealloc_stack as a no-op, but
that's now easy to fix.

Swift SVN r6937
2013-08-06 07:31:41 +00:00
Jordan Rose
1d54332bc5 Validate import kind, e.g. "import var swift.max" now errors, with fix-it.
Again, the import kind rules are:
 - 'import KIND' can import any decl whose introducer is KIND.
 - 'import typealias' can also import a struct, class, or union.
 - Conversely, 'import KIND' can import a typealias for a decl whose
   introducer is KIND.
 - Only functions can be overloaded; anything else counts as an ambiguous
   import and is an error.
 - If an import statement only imports a single decl, but the user got the
   kind wrong, we can issue a fix-it for the kind.

We don't have source locations or synthetic source for declarations yet,
so there are no notes about what's /causing/ the ambiguities. Tracked by
<rdar://problem/14650883>

Swift SVN r6917
2013-08-05 21:03:01 +00:00
Jordan Rose
911c46e607 Error on "import var x" and similar (decl import with no module name).
Swift SVN r6848
2013-08-02 21:00:09 +00:00
Joe Groff
ba774364f1 SIL: Have SILModules track their SILStage.
Modules can be in either 'Raw' or 'Canonical' form, with different invariants on each. We don't actually distinguish those invariants yet, but this patch adds the field to SILModule and adds a "sil_stage" declaration to SIL printer/parser syntax.

Swift SVN r6793
2013-08-01 00:58:31 +00:00
Jordan Rose
757cf9826f Add Parse and AST support for the new import syntax.
Also, update LangRef.

Note that an explicit "import module" has been left out for now, since
it's not strictly necessary and "module" isn't a keyword yet.

Swift SVN r6786
2013-07-31 23:23:26 +00:00
Jordan Rose
674a03b085 Replace "oneof" with "union"...everywhere.
We haven't fully updated references to union cases, and enums still are not
their own thing yet, but "oneof" is gone. Long live "union"!

Swift SVN r6783
2013-07-31 21:33:33 +00:00
Jordan Rose
3e7eef56e7 Kill [stdlib] attribute.
Now that we have true serialized modules, the standard library can import
the Builtin module without any special direction (beyond -parse-stdlib),
and anyone can include those modules without special direction.

Swift SVN r6752
2013-07-30 21:27:42 +00:00
Jordan Rose
736c6d2db6 Don't crash on "var x = { x() }".
We should probably accept this, or at least some variation of it, but
erroring out is still a strict improvement over crashing. However, this
does cause a regression for some properly typed recursive closures, like
'fib' in expressions.swift.

<rdar://problem/14583952> tracks the correct solution.

Swift SVN r6730
2013-07-30 00:17:35 +00:00
Jordan Rose
3ca6768be3 Using a variable within its own initializer is an error.
Currently, this includes cases where a variable of the same name is
available in an outer scope. We can change this later if desired.

<rdar://problem/14566648>

Swift SVN r6729
2013-07-30 00:17:28 +00:00
Dmitri Hrybenko
887997451e Code completion: don't crash while code completing (actually, while delaying
parsing of) a function that does not have a right brace before EOF.


Swift SVN r6625
2013-07-26 00:00:26 +00:00
Dmitri Hrybenko
56998205d6 Delayed function body parsing: replace two error handling code paths with asserts
This should just never happen.


Swift SVN r6607
2013-07-25 20:03:55 +00:00
Argyrios Kyrtzidis
1e597f5d8a [Parser] When delay-parsing a function body, make sure the closing brace is included.
Swift SVN r6599
2013-07-25 16:20:13 +00:00
Argyrios Kyrtzidis
51990109e5 If the parser needs priming, let the parser handle it.
Swift SVN r6597
2013-07-25 14:42:10 +00:00
Argyrios Kyrtzidis
9d157e7dca [Parser] Record the end location of a function body when we skip it.
Swift SVN r6594
2013-07-25 14:42:08 +00:00
Argyrios Kyrtzidis
803ba0e2be [Parser] Introduce PersistentParserState::ParserPos to encapsulate a Parser/Lexer independent info for restoring parsing from a certain token location.
Swift SVN r6593
2013-07-25 14:42:07 +00:00
Argyrios Kyrtzidis
66d1e516c8 Refactor how multiple parsing passes and delayed parsing works.
-Introduce PersistentParserState to represent state persistent among multiple parsing passes.
  The advantage is that PersistentParserState is independent of a particular Parser or Lexer object.
-Use PersistentParserState to keep information about delayed function body parsing and eliminate parser-specific
  state from the AST (ParserTokenRange).
-Introduce DelayedParsingCallbacks to abstract out of the parser the logic about which functions should be delayed
  or skipped.

Many thanks to Dmitri for his valuable feedback!

Swift SVN r6580
2013-07-25 01:40:16 +00:00
Argyrios Kyrtzidis
694ab2cacf Remove 'Flags' from FunctionBodyParserState, it's not getting used.
Swift SVN r6579
2013-07-25 01:40:14 +00:00
Argyrios Kyrtzidis
de4a2dbd8b [Parser] Abstract a bit access to Parser's ScopeInfo.
This will be more useful later on.

Swift SVN r6578
2013-07-25 01:40:13 +00:00
Doug Gregor
26e6833b21 Make the generic parameters of structs and oneofs available within their inheritance clauses.
Even though generic parameters can't be meaningfully used, it's
"obviously" the right thing that they be visible.


Swift SVN r6476
2013-07-22 21:39:07 +00:00
Doug Gregor
109cb0db74 Parse 'static' more permissively, but complain if it isn't used.
Fixes <rdar://problem/14446888>.


Swift SVN r6453
2013-07-22 16:35:57 +00:00
Chris Lattner
500d90995c Change the sentinel used by the parser to represent unprimed state to be something the lexer can't produce.
Swift SVN r6447
2013-07-22 14:48:44 +00:00
Doug Gregor
deca9a7896 Parse class inheritance clause within the context of the class itself.
This allows one to refer to the class's generic parameters within its
inheritance clause, so that one can, e.g., inherit from Foo<T>. Fixes
<rdar://problem/13562287>.


Swift SVN r6439
2013-07-22 05:49:06 +00:00
Dmitri Hrybenko
d52e6ed8bc Update a comment in Parser::parseDeclFunc(): static functions have an implicit
'this' parameter.


Swift SVN r6387
2013-07-19 21:12:59 +00:00
Anna Zaks
74bc6f05b2 Add "noreturn" attribute : stage 1
- Add the attribute to AnyFunctionType::ExtInfo.
- Propagate the attributes from DeclAttributes to AnyFunctionType for
  FuncDecls in TypeCheckDecl.cpp.
- Make sure the new attribute is serialized.

The main missing pieces are checking the applicability of the type attributes
on the FuncDecl and teaching typechecker about conversions on types with
noreturn.

Swift SVN r6359
2013-07-18 22:57:22 +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
Argyrios Kyrtzidis
f616eeee8b Utilize TypeReprs for type checking.
-Refactor Parser to stop creating types
-Refactor TypeChecker to create types by resolving TypeReprs.
-Remove "validation" bit from the type system.
  We don't need to "validate" every type that gets created but there's still a validation bit in TypeLoc,
  necessary because of generic substitutions.

Swift SVN r6326
2013-07-17 14:57:35 +00:00
Doug Gregor
66d3027847 Stop building UnstructuredUnresolvedTypes.
Instead, actually fill in errors types when type checking fails.


Swift SVN r6240
2013-07-13 04:17:32 +00:00
Joe Groff
d956fdbd9e Update 'oneof' syntax.
Give oneof bodies syntax consistent with other NominalTypes. Give oneof elements first-class declaration syntax using the 'case' introducer, as suggested by Jordan. Oneofs can contain 'case' decls, functions, properties, and constructors, but not physical ivars. Non-oneof scopes cannot contain 'case' decls. Add some QoI to the oneof 'case' parser to also parse and complain about things that resemble switch 'case' labels inside decl contexts.

Swift SVN r6211
2013-07-12 20:42:19 +00:00
Jordan Rose
0afaa86bcc [serialization] Add support for subscript decls.
For consistency purposes, VarDecls in the subscript pattern now have the
enclosing nominal as their decl context, rather than no decl context at all.

Swift SVN r6177
2013-07-11 21:22:08 +00:00