Commit Graph

33 Commits

Author SHA1 Message Date
Rintaro Ishizaki
546aeb2336 [Parse] Remove Parser support for C-style for statement. 2017-08-04 23:27:15 +09:00
Alex Hoppen
ebd701c4b7 Represent the name of destructors by a special DeclBaseName
No longer use the known identifier `deinit` for destructors. This allows
classes to have functions called `deinit`.
2017-07-28 19:20:02 +02:00
Slava Pestov
51aebd2127 AST: Fix name lookup from within lazy property initializers
Allow instance properties and methods to be referenced from
within a lazy property initializer, with or without explicit
'self.' qualification.

The old behavior in Swift 3 was an incredible combination
of odd quirks:

- If the lazy property had an explicitly-written type, it was
  possible to reference instance members from the initializer
  expression by explicitly prefixing 'self.'.

- However, if the lazy property type is inferred, it would
  first be type checked in the initializer context, which
  has no 'self' available.

- Unqualified references to instance members did not work
  at all, because name lookup thought the "location" of the
  lookup was outside of the body of the getter.

- Unqualified references to static properties worked, however
  unqualified references to static methods did not, and
  produced a bogus diagnostic, because one part of the name
  lookup code thought that initializers were "instance
  context" and another thought they were "static context".

This patch improves on the old behavior with the following
fixes:

- Give PatternBindingInitializers associated with lazy
  properties an implicit 'self' declaration for use by
  name lookup.

- In order to allow "re-parenting" the initializer after it
  has been type checked into the body of the getter, "steal"
  the initializer's 'self' when buiding the getter.

- Fix up name lookup and make it aware of the implicit
  'self' decl of a PatternBindingInitializer.

This improves upon an earlier fix for this issue by Doug Gregor
which only worked with ASTScope enabled; the new fix is more
general and shares logic between the two name lookup
implementations.

Fixes <rdar://problem/16888679>, <https://bugs.swift.org/browse/SR-48>,
<https://bugs.swift.org/browse/SR-2203>,
<https://bugs.swift.org/browse/SR-4663>, and the countless other
dupes of this issue.
2017-05-24 18:59:48 -07:00
Jordan Rose
845017cc81 Improve how DeclContext chains are printed in AST dumps. (#7786) 2017-02-28 10:37:21 -08:00
Doug Gregor
8850e12790 [Scope map] Temporarily disable correct lookup via 'self' in lazy properties.
This lookup causes recursion that the rest of the name lookup
machinery cannot yet handle, breaking code that is currently
well-formed. Back off of this lookup temporarily.
2016-10-17 20:06:47 -07:00
Doug Gregor
ae9c29036a [Scope map] Local types and functions are visible in the enclosing braces.
As in non-local scopes, local types and functions are generally
visible anywhere within enclosing braces, and it is up to capture
analysis to determine whether these entities attempt to capture an
entity that isn't guaranteed to be available.
2016-09-15 15:55:26 -07:00
Doug Gregor
ac93c52c96 [Scope map] A local property name is in scope within its own accessors.
While the use of a local property from within its own accessors is a
bit dubious, Swift 3 only warned on it, so model the existing lookup
behavior in the scope map.
2016-09-15 09:16:46 -07:00
Doug Gregor
67fb8cc841 [Scope map] Model top-level code as a continuation of the source file.
Once top-level code is introduced into a source file, turn the source
file itself into a continuation, so that subsequent declarations have
proper scope information. This means we get proper scope information
for, e.g., pattern bindings and guard statements in top-level code, so
that a 'guard' at top level is a parent of all subsequent code,
including (e.g.) function declarations that might capture variables
introduced by the guard.

Continuations are now handled by more explicitly passing them down
from parent nodes to the child nodes that can accept them, and the
source file itself is modeled as a continuation as soon as the first
top-level code declaration is encountered.
2016-09-12 11:35:57 -07:00
Doug Gregor
909de5bd16 [Scope map] Type and abstract function declaration scopes can be continuations.
Rather than trying to specifically model local declarations of
functions and types with a "local declaration" scope, which we
introduced when we saw one of these entities with a local DeclContext,
use a more consistency scheme where:

  1) we always describe a type declaration with a TypeDecl scope and an
  (abstract) function declaration with an AbstractFunctionDecl scope,
  and
  2) either of those scopes can steal the continuation from their
  parent;

This modeling is another step toward supporting top-level code.
2016-09-12 11:35:57 -07:00
Doug Gregor
67bf68ae70 [Name lookup] Support lookup of 'self' in lazy property initializers.
Lazy property initializers can refer to 'self' either directly or
implicitly (via references to instance members). Model this in
ASTScope-based unqualified name lookup.

Note that the modeling of 'self' with the current name lookup
mechanism is broken, so when ASTScope-based unqualified name lookup is
enabled, it fixes SR-2203, rdar://problem/16954496, and the many dupes
of the latter.
2016-09-08 11:24:03 -07:00
Doug Gregor
abf9bfe9d5 [Scope map] Provide scopes for the generic parameters of protocols and extensions. 2016-09-07 16:44:36 -07:00
Doug Gregor
ead78e046f [Scope map] Explicitly model the scope of a function body.
For methods/initializers/deinitializers, modeling the scope of a
function body allows us to correctly introduce instance member lookup
relative to 'self'. Do so so that we can resolve unqualified name
lookup within methods based on the implicit 'self'.
2016-09-06 22:58:18 -07:00
Doug Gregor
311fc55bdb [Scope map] Query the declarations introduced by a given AST scope.
Introduce an operation that produces the set of local declarations
that are newly introduced by a given AST scope. This is a building
block of unqualified name lookup, which walks upward in the tree
(e.g., from children to parents) looking for declarations that have
been made visible at each step.
2016-09-06 09:05:10 -07:00
Doug Gregor
69adfacb71 [Scope map] Deal with some previously-broken ASTs 2016-09-02 17:13:05 -07:00
Doug Gregor
c612f4feda [Scope map] Add support for querying the immediate and nearest enclosing DeclContexts of a scope. 2016-09-02 17:13:05 -07:00
Doug Gregor
8e39f3e0cd [Scope map] Model the scopes of default arguments. 2016-09-02 17:13:05 -07:00
Doug Gregor
ed173d2fbc [Scope map] Explicitly model pattern bindings and their initializers.
Consistently model all pattern bindings in the scope map, as well as
having specific nodes for their initializers. This provides us with
more consistency (the declarations are represented) as well as giving
us a scope we can use to extract the DeclContext for a non-local
initializer.
2016-09-02 17:13:05 -07:00
Doug Gregor
84b043365c [Scope map] Add a scope node for top-level code declarations. 2016-09-02 17:13:05 -07:00
Doug Gregor
35888ee0c2 [Scope map] Always introduce a node for AbstractFunctionDecls.
Part of making the scope map subsume the DeclContext change, so we can
identify the nearest enclosing DeclContext.
2016-09-02 17:13:05 -07:00
Doug Gregor
412d1d5e1e [Scope map] Introduce an operation to find the innermost scope based on a given source location.
Given a source location, we can find the innermost enclosing scope
that describes that source location. Introduce this operation into the
scope map, then add a testing mode where we probe the scope map at
specifi locations to see what we find. Test for:

1) Finding the right innermost enclosing scope, and
2) That we're only expanding the part of the scope map that is needed
to identify that scope.
2016-09-02 17:13:05 -07:00
Doug Gregor
b50ba75458 Tighten up out-of-order checking, fixing up some issues with computed properties in local scopes. 2016-09-02 17:13:05 -07:00
Doug Gregor
4d9a92d898 Split guard statement into three non-overlapping children:
* A child to evaluate the conditions, which covers expressions in the conditions
  * A child for the 'else' body, which does not have access to the names in the conditions
  * A child for the continuation, which re-introduces the names in the conditions for the rest of the body

Now we can assert that the only out-of-order case is for accessors.
2016-09-02 17:13:05 -07:00
Doug Gregor
d1945e7f53 [Scope map] Add top-level code declaration support. 2016-09-02 17:13:05 -07:00
Doug Gregor
9a21677663 Add support for closures 2016-09-02 17:13:05 -07:00
Doug Gregor
9ea2fab6b9 Add support for getters/setters. 2016-09-02 17:13:05 -07:00
Doug Gregor
d1ce3b82ef Add support for the C-style 'for' statement. 2016-09-02 17:13:05 -07:00
Doug Gregor
b3855c9057 Add switch/case support 2016-09-02 17:13:05 -07:00
Doug Gregor
1e52d53ae2 Add support for do-catch 2016-09-02 17:13:05 -07:00
Doug Gregor
8c31960900 Add support for for-each statements 2016-09-02 17:13:05 -07:00
Doug Gregor
ebad77e1af Add support for repeat-while. 2016-09-02 17:13:05 -07:00
Doug Gregor
c775d94478 [Scope map] Add support for conditional statements. 2016-09-02 17:13:05 -07:00
Doug Gregor
b6a93246ac [Scope map] Use continuations for local declarations that introduce scopes. 2016-09-02 17:13:05 -07:00
Doug Gregor
4e214687ca [WIP Name binding] Introduce a basic 'scope map' to model scopes in the AST.
The scope map models all of the name lookup scopes within a source
file. It can be queried by source location to find the innermost scope
that contains that source location. Then, one can follow the parent
pointers in the scope to enumerate the enclosing scopes.

The scope map itself is lazily constructed, only creating scope map
nodes when required implicitly (e.g, when searching for a particular
innermost scope) or forced for debugging purposes.

using a lazily-constructed tree that can be searched by source
location. A search within a particular source location will
2016-09-02 17:13:05 -07:00