Skip to content

Introduce a new data structure to explicitly model scopes in the AST #4609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Sep 6, 2016

Conversation

DougGregor
Copy link
Member

This PR introduces a new data structure that explicitly models scopes in the AST allowing one to query the innermost scope based on a source location and then walk the enclosing scopes. The data structure itself operates on parsed ASTs (which may also be type-checked; it doesn't matter) and is lazily constructed as needed.

At the moment, there are no clients for this data structure. However, it is intended to be used for unqualified name lookup and should also be able to subsume the type refinement context.

…he 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
  * 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.
…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.
Source ranges are non-trivial to compute, small to store, and used often. Cache 'em.
Part of making the scope map subsume the DeclContext change, so we can
identify the nearest enclosing DeclContext.
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.
@DougGregor
Copy link
Member Author

@swift-ci please test and merge

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor
Copy link
Member Author

(the runtime weak reference race tests failure was not related to this change, but I'll smoke-test again regardless)

@DougGregor DougGregor merged commit 54031f1 into swiftlang:master Sep 6, 2016
@DougGregor DougGregor deleted the scope-map branch September 6, 2016 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant