public class Scope
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Scope.Type |
Constructor and Description |
---|
Scope(Scope parent,
Scope.Type type) |
Modifier and Type | Method and Description |
---|---|
void |
addGlobalName(java.lang.String name)
Mark a name as being global (i.e.
|
void |
addSuper(Scope sup) |
void |
clear()
Dismantles all resources allocated by this scope.
|
Scope |
copy(Scope.Type tableType)
Create a copy of the symbol table but without the links to parent, supers
and children.
|
java.util.Set<java.util.Map.Entry<java.lang.String,NBinding>> |
entrySet() |
java.lang.String |
extendPath(java.lang.String name)
Constructs a qualified name by appending
name to this scope's qname. |
java.lang.String |
extendPathForParam(java.lang.String name)
Generates a qname for a parameter of a function or method.
|
Scope |
getEnclosingLexicalScope()
Returns the containing lexical scope (which may be this scope)
for lexical name lookups.
|
Scope |
getGlobalTable()
Returns the global scope (i.e.
|
Scope |
getParent() |
java.lang.String |
getPath() |
Scope |
getScopeSymtab()
Find the enclosing scope-defining symbol table.
|
Scope.Type |
getScopeType() |
java.util.List<Scope> |
getSupers() |
Scope |
getSymtabOfType(Scope.Type type)
Find a symbol table of a certain type in the enclosing scopes.
|
java.util.Map<java.lang.String,NBinding> |
getTable()
Returns an immutable view of the table.
|
boolean |
isEmpty() |
boolean |
isFunctionScope() |
boolean |
isGlobalName(java.lang.String name)
Returns
true if name appears in a global
statement in this scope or any enclosing scope. |
boolean |
isNameBindingPhase()
Name binding occurs in a separate pass before the name resolution pass,
building out the scope tree and binding names in the correct scopes.
|
boolean |
isScope()
Returns
true if this is a scope in which names may be bound. |
java.util.Set<java.lang.String> |
keySet() |
NBinding |
lookup(NNode n)
Specialized version for the convenience of looking up
Name s. |
NBinding |
lookup(java.lang.String name)
Look up a name (String) in the current symbol table.
|
NBinding |
lookupAttr(java.lang.String name)
Look up an attribute in the local scope and superclass scopes.
|
NBinding |
lookupAttr(java.lang.String name,
boolean supersOnly)
Look up an attribute in the type hierarchy.
|
NBinding |
lookupBounded(java.lang.String name,
Scope.Type typebound)
Look up a name, but the search is bounded by a type and will not proceed
to an outer scope when reaching a certain type of symbol table.
|
NBinding |
lookupLocal(java.lang.String name)
Look up a name, but only in the current scope.
|
NBinding |
lookupScope(java.lang.String name)
Look up a name, but bounded by a scope defining construct.
|
NType |
lookupType(java.lang.String name)
Look up the scope chain for a binding named
name
and if found, return its type. |
NType |
lookupType(java.lang.String name,
boolean localOnly)
Look for a binding named
name and if found, return its type. |
NType |
lookupTypeAttr(java.lang.String name) |
void |
merge(Scope other)
Merge all records from another symbol table.
|
java.lang.String |
newLambdaName() |
void |
put(java.lang.String id,
NBinding b)
Directly assigns a binding to a name in this table.
|
NBinding |
put(java.lang.String id,
NNode loc,
NType type,
NBinding.Kind kind)
Adds a definition and/or reference to the table.
|
NBinding |
putAttr(java.lang.String id,
NNode loc,
NType type,
NBinding.Kind kind)
Same as
put(java.lang.String, org.python.indexer.NBinding) , but adds the name as an attribute of this scope. |
void |
remove(java.lang.String id) |
void |
setNameBindingPhase(boolean isBindingPhase) |
void |
setParent(Scope parent) |
void |
setPath(java.lang.String path) |
void |
setPath(java.lang.String a,
java.lang.String b) |
void |
setScopeType(Scope.Type type) |
void |
setSupers(java.util.List<Scope> supers) |
void |
setTable(java.util.Map<java.lang.String,NBinding> table) |
java.lang.String |
toShortString() |
java.lang.String |
toString() |
NBinding |
update(java.lang.String id,
Def loc,
NType type,
NBinding.Kind kind)
Adds a new binding for
id . |
NBinding |
update(java.lang.String id,
NNode loc,
NType type,
NBinding.Kind kind)
Adds a new binding for
id . |
java.util.Collection<NBinding> |
values() |
public Scope(Scope parent, Scope.Type type)
public void setTable(java.util.Map<java.lang.String,NBinding> table)
public java.util.Map<java.lang.String,NBinding> getTable()
public void setParent(Scope parent)
public Scope getParent()
public void addSuper(Scope sup)
public void setSupers(java.util.List<Scope> supers)
public java.util.List<Scope> getSupers()
public void setScopeType(Scope.Type type)
public Scope.Type getScopeType()
public boolean isFunctionScope()
public void addGlobalName(java.lang.String name)
public boolean isGlobalName(java.lang.String name)
true
if name
appears in a global
statement in this scope or any enclosing scope.public void put(java.lang.String id, NBinding b)
put
is
often followed by a call to Indexer.putLocation(org.python.indexer.ast.NNode, org.python.indexer.NBinding)
to create a reference to
the binding. When there is no code location associated with id
,
or it is otherwise undesirable to create a reference, the
Indexer.putLocation(org.python.indexer.ast.NNode, org.python.indexer.NBinding)
call is omitted.public NBinding put(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)
id
, creates one and gives it
type
and kind
.
If a binding already exists, then add either a definition or a reference
at loc
to the binding. By convention we consider it a definition
if the type changes. If the passed type is different from the binding's
current type, set the binding's type to the union of the old and new
types, and add a definition. If the new type is the same, just add a
reference.
If the binding already exists, kind
is only updated if a
definition was added and the binding's type was previously the
unknown type.
public NBinding putAttr(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)
put(java.lang.String, org.python.indexer.NBinding)
, but adds the name as an attribute of this scope.
Looks up the superclass chain to see if the attribute exists, rather
than looking in the lexical scope chain.null
if the current scope does
not have a properly initialized path.public NBinding update(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)
id
. If a binding already existed,
replaces its previous definitions, if any, with loc
. Sets the
binding's type to type
(not a union with the previous type).public NBinding update(java.lang.String id, Def loc, NType type, NBinding.Kind kind)
id
. If a binding already existed,
replaces its previous definitions, if any, with loc
. Sets the
binding's type to type
(not a union with the previous type).public void remove(java.lang.String id)
public Scope copy(Scope.Type tableType)
public void setPath(java.lang.String path)
public java.lang.String getPath()
public void setPath(java.lang.String a, java.lang.String b)
public NBinding lookup(java.lang.String name)
public NBinding lookup(NNode n)
Name
s.
For all other types return null
.public NBinding lookupLocal(java.lang.String name)
name
, or null
.public NBinding lookupAttr(java.lang.String name, boolean supersOnly)
supersOnly
- search only in the supers' scopes, not in local table.public NBinding lookupAttr(java.lang.String name)
lookupAttr(String,boolean)
public NType lookupType(java.lang.String name)
name
and if found, return its type.public NType lookupType(java.lang.String name, boolean localOnly)
name
and if found, return its type.localOnly
- true
to look only in the current scope;
if false
, follows the scope chain.public NType lookupTypeAttr(java.lang.String name)
public NBinding lookupBounded(java.lang.String name, Scope.Type typebound)
name
- the name to be looked uptypebound
- the type we wish the search to be bounded atnull
if not foundpublic boolean isScope()
true
if this is a scope in which names may be bound.public Scope getScopeSymtab()
More precisely, if a form introduces a new name in the "current scope",
resolving the form needs to search up the symbol-table chain until it
finds the table representing the scope to which the name should be added.
Used by NameBinder
to create new name
bindings in the appropriate enclosing table with the appropriate binding
type.
public NBinding lookupScope(java.lang.String name)
public Scope getSymtabOfType(Scope.Type type)
public Scope getGlobalTable()
public Scope getEnclosingLexicalScope()
public boolean isNameBindingPhase()
public void setNameBindingPhase(boolean isBindingPhase)
public void merge(Scope other)
import from *
.public java.util.Set<java.lang.String> keySet()
public java.util.Collection<NBinding> values()
public java.util.Set<java.util.Map.Entry<java.lang.String,NBinding>> entrySet()
public boolean isEmpty()
public void clear()
public java.lang.String newLambdaName()
public java.lang.String extendPathForParam(java.lang.String name)
extendPath(java.lang.String)
to differentiate
params from locals, so callers must use this method when the name is
known to be a parameter name.public java.lang.String extendPath(java.lang.String name)
name
to this scope's qname. The indexer uses globally unique fully qualified names to address identifier definition sites. Many Python identifiers are already globally addressable using dot-separated package, class and attribute names.
Function variables and parameters are not globally addressable in the language, so the indexer uses a special path syntax for creating globally unique qualified names for them. By convention the syntax is "@" for parameters and "&" for local variables.
name
- a name to append to the current qnamename
. Does not change this scope's path.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toShortString()