forked from google/starlark-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve: move resolver types to resolver package (google#211)
The job of the resolver is to compute a binding for each identifer, the local and free variables for each function, and the locals and globals for each module. As a space optimization and for convenience, this information is saved in the syntax tree rather than in a side table. We have a choice between declaring these resolver data structures in the syntax package or the resolve package. Putting them in the syntax package, as we did prior to this change, allowed each Identifier, File, DefStmt, and LambdaExpr to depend directly on the resolver types, even though those types didn't really belong there. This change moves these types to the resolver package where they belong. The dependencies on the types are broken by using an interface{} in each case. (The values are are all pointers, so this does not induce new allocation.) Also, this change eliminates syntax.Function, which was a false abstraction of DefStmt and LambdaExpr. The two syntax nodes are now fully concrete; it is in the resolver that their shared concept of Function belongs. This is a breaking API change, but it should affect very few clients and be trivial to fix.
- Loading branch information
Showing
11 changed files
with
147 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2019 The Bazel Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package resolve | ||
|
||
import "go.starlark.net/syntax" | ||
|
||
// This file defines resolver data types saved in the syntax tree. | ||
// We cannot guarantee API stability for these types | ||
// as they are closely tied to the implementation. | ||
|
||
// A Binding contains resolver information about an identifer. | ||
// The resolver populates the Binding field of each syntax.Identifier. | ||
// The Binding ties together all identifiers that denote the same variable. | ||
type Binding struct { | ||
Scope Scope | ||
|
||
// Index records the index into the enclosing | ||
// - {DefStmt,File}.Locals, if Scope==Local | ||
// - DefStmt.FreeVars, if Scope==Free | ||
// - File.Globals, if Scope==Global. | ||
// It is zero if Scope is Predeclared, Universal, or Undefined. | ||
Index int | ||
|
||
First *syntax.Ident // first binding use (iff Scope==Local/Free/Global) | ||
} | ||
|
||
// The Scope of Binding indicates what kind of scope it has. | ||
type Scope uint8 | ||
|
||
const ( | ||
Undefined Scope = iota // name is not defined | ||
Local // name is local to its function or file | ||
Cell // name is function-local but shared with a nested function | ||
Free // name is cell of some enclosing function | ||
Global // name is global to module | ||
Predeclared // name is predeclared for this module (e.g. glob) | ||
Universal // name is universal (e.g. len) | ||
) | ||
|
||
var scopeNames = [...]string{ | ||
Undefined: "undefined", | ||
Local: "local", | ||
Cell: "cell", | ||
Free: "free", | ||
Global: "global", | ||
Predeclared: "predeclared", | ||
Universal: "universal", | ||
} | ||
|
||
func (scope Scope) String() string { return scopeNames[scope] } | ||
|
||
// A Module contains resolver information about a file. | ||
// The resolver populates the Module field of each syntax.File. | ||
type Module struct { | ||
Locals []*Binding // the file's (comprehension-)local variables | ||
Globals []*Binding // the file's global variables | ||
} | ||
|
||
// A Function contains resolver information about a named or anonymous function. | ||
// The resolver populates the Function field of each syntax.DefStmt and syntax.LambdaExpr. | ||
type Function struct { | ||
Pos syntax.Position // of DEF or LAMBDA | ||
Name string // name of def, or "lambda" | ||
Params []syntax.Expr // param = ident | ident=expr | * | *ident | **ident | ||
Body []syntax.Stmt // contains synthetic 'return expr' for lambda | ||
|
||
HasVarargs bool // whether params includes *args (convenience) | ||
HasKwargs bool // whether params includes **kwargs (convenience) | ||
NumKwonlyParams int // number of keyword-only optional parameters | ||
Locals []*Binding // this function's local/cell variables, parameters first | ||
FreeVars []*Binding // enclosing cells to capture in closure | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.