Top Description Inners Fields Constructors Methods
org.python.indexer

public Class Scope

extends Object
Class Inheritance
Imports
org.python.indexer.ast.NName, .NNode, .NUrl, org.python.indexer.types.NType, .NUnionType, .NUnknownType, java.util.ArrayList, .Collection, .Collections, .HashSet, .LinkedHashMap, .List, .Map.Entry, .Map, .Set

Symbol table.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private Set<String>
private boolean
private int
private static Set<Scope>
looked

For preventing circular inheritance from recursing.

private Scope
private String
private Scope.Type
private List<Scope>
private Map<String, NBinding>
table

XXX: This table is incorrectly overloaded to contain both object attributes and lexical-ish scope names, when they are in some cases separate namespaces.

Constructor Summary

AccessConstructor and Description
public
Scope(Scope parent, Scope.Type type)

Method Summary

Modifier and TypeMethod and Description
public void
addGlobalName(String name)

Mark a name as being global (i.e. module scoped) for name-binding and name-lookup operations in this code block and any nested scopes.

public void
public void
clear()

Dismantles all resources allocated by this scope.

public Scope

Returns:

the symbol table for use by the instance.
copy
(Scope.Type tableType)

Create a copy of the symbol table but without the links to parent, supers and children.

private void
public Set<Map.Entry<String, NBinding>>
public String

Returns:

the qname for name. Does not change this scope's path.
extendPath
(String
a name to append to the current qname
name
)

Constructs a qualified name by appending name to this scope's qname.

public String
extendPathForParam(String name)

Generates a qname for a parameter of a function or method.

public Scope
getEnclosingLexicalScope()

Returns the containing lexical scope (which may be this scope) for lexical name lookups.

public Scope
getGlobalTable()

Returns the global scope (i.e. the module scope for the current module).

private NBinding
getModuleBindingIfGlobal(String name)

If name is declared as a global, return the module binding.

public Scope
public String
public Scope
getScopeSymtab()

Find the enclosing scope-defining symbol table.

public Scope.Type
public List<Scope>
public Scope
getSymtabOfType(Scope.Type type)

Find a symbol table of a certain type in the enclosing scopes.

public Map<String, NBinding>
getTable()

Returns an immutable view of the table.

private NBinding
private NBinding
public boolean
public boolean
public boolean
isGlobalName(String name)

Returns true if name appears in a global statement in this scope or any enclosing scope.

public 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.

private boolean
isNewType(NType curType, NType type)

Returns true if the binding is being assigned a new type.

public boolean
isScope()

Returns true if this is a scope in which names may be bound.

public Set<String>
public NBinding
lookup(String name)

Look up a name (String) in the current symbol table.

public NBinding
lookup(NNode n)

Specialized version for the convenience of looking up Names.

public NBinding
lookupAttr(String name, boolean
search only in the supers' scopes, not in local table.
supersOnly
)

Look up an attribute in the type hierarchy.

public NBinding
lookupAttr(String name)

Look up an attribute in the local scope and superclass scopes.

public NBinding

Returns:

a binding, or null if not found
lookupBounded
(String
the name to be looked up
name
,
Scope.Type
the type we wish the search to be bounded at
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.

public NBinding

Returns:

the local binding for name, or null.
lookupLocal
(String name)

Look up a name, but only in the current scope.

public NBinding
lookupScope(String name)

Look up a name, but bounded by a scope defining construct.

public NType
lookupType(String name)

Look up the scope chain for a binding named name and if found, return its type.

public NType
lookupType(String name, boolean
true to look only in the current scope; if false, follows the scope chain.
localOnly
)

Look for a binding named name and if found, return its type.

public NType
public void
merge(Scope other)

Merge all records from another symbol table.

public String
public void
put(String id, NBinding b)

Directly assigns a binding to a name in this table.

public NBinding
put(String id, NNode loc, NType type, NBinding.Kind kind)

Adds a definition and/or reference to the table.

public NBinding

Returns:

the new binding, or null if the current scope does not have a properly initialized path.
putAttr
(String id, NNode loc, NType type, NBinding.Kind kind)

Same as put, but adds the name as an attribute of this scope.

private void
public void
private void
retargetReferences(NBinding b, NType curType)

If the current type had a provisional binding, retarget its refs to the new type.

public void
setNameBindingPhase(boolean isBindingPhase)

public void
setParent(Scope parent)

public void
public void
public void
public void
setSupers(List<Scope> supers)

public void
public String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public NBinding
update(String id, NNode loc, NType type, NBinding.Kind kind)

Adds a new binding for id.

public NBinding
update(String id, Def loc, NType type, NBinding.Kind kind)

Adds a new binding for id.

private void
public Collection<NBinding>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

globalNamesback to summary
private Set<String> globalNames
isBindingPhaseback to summary
private boolean isBindingPhase
lambdaCounterback to summary
private int lambdaCounter
lookedback to summary
private static Set<Scope> looked

For preventing circular inheritance from recursing.

parentback to summary
private Scope parent
pathback to summary
private String path
scopeTypeback to summary
private Scope.Type scopeType
supersback to summary
private List<Scope> supers
tableback to summary
private Map<String, NBinding> table

XXX: This table is incorrectly overloaded to contain both object attributes and lexical-ish scope names, when they are in some cases separate namespaces. (In particular, they're effectively the same namespace for module scope and class scope, and they're different for function scope, which uses the func_dict namespace for storing attributes.)

Constructor Detail

Scopeback to summary
public Scope(Scope parent, Scope.Type type)

Method Detail

addGlobalNameback to summary
public void addGlobalName(String name)

Mark a name as being global (i.e. module scoped) for name-binding and name-lookup operations in this code block and any nested scopes.

addSuperback to summary
public void addSuper(Scope sup)
clearback to summary
public void clear()

Dismantles all resources allocated by this scope.

copyback to summary
public Scope copy(Scope.Type tableType)

Create a copy of the symbol table but without the links to parent, supers and children. Useful for creating instances.

Returns:Scope

the symbol table for use by the instance.

ensureTableback to summary
private void ensureTable()
entrySetback to summary
public Set<Map.Entry<String, NBinding>> entrySet()
extendPathback to summary
public String extendPath(String name)

Constructs a qualified name by appending 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.

Parameters
name:String

a name to append to the current qname

Returns:String

the qname for name. Does not change this scope's path.

extendPathForParamback to summary
public String extendPathForParam(String name)

Generates a qname for a parameter of a function or method. There is not enough context for extendPath to differentiate params from locals, so callers must use this method when the name is known to be a parameter name.

getEnclosingLexicalScopeback to summary
public Scope getEnclosingLexicalScope()

Returns the containing lexical scope (which may be this scope) for lexical name lookups. In particular, it skips class scopes.

getGlobalTableback to summary
public Scope getGlobalTable()

Returns the global scope (i.e. the module scope for the current module).

getModuleBindingIfGlobalback to summary
private NBinding getModuleBindingIfGlobal(String name)

If name is declared as a global, return the module binding.

getParentback to summary
public Scope getParent()
getPathback to summary
public String getPath()
getScopeSymtabback to summary
public Scope getScopeSymtab()

Find the enclosing scope-defining symbol table.

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 org.python.indexer.ast.NameBinder to create new name bindings in the appropriate enclosing table with the appropriate binding type.

getScopeTypeback to summary
public Scope.Type getScopeType()
getSupersback to summary
public List<Scope> getSupers()
getSymtabOfTypeback to summary
public Scope getSymtabOfType(Scope.Type type)

Find a symbol table of a certain type in the enclosing scopes.

getTableback to summary
public Map<String, NBinding> getTable()

Returns an immutable view of the table.

insertBindingback to summary
private NBinding insertBinding(NBinding b)
insertOrUpdateback to summary
private NBinding insertOrUpdate(NBinding b, String id, NNode loc, NType t, NBinding.Kind k)
isEmptyback to summary
public boolean isEmpty()
isFunctionScopeback to summary
public boolean isFunctionScope()
isGlobalNameback to summary
public boolean isGlobalName(String name)

Returns true if name appears in a global statement in this scope or any enclosing scope.

isNameBindingPhaseback to summary
public 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. In this pass, the name binding and lookup rules are slightly different. This condition is transient: no scopes will be in the name-binding phase in a completed index (or module).

isNewTypeback to summary
private boolean isNewType(NType curType, NType type)

Returns true if the binding is being assigned a new type.

isScopeback to summary
public boolean isScope()

Returns true if this is a scope in which names may be bound.

keySetback to summary
public Set<String> keySet()
lookupback to summary
public NBinding lookup(String name)

Look up a name (String) in the current symbol table. If not found, recurse on the parent table.

lookupback to summary
public NBinding lookup(NNode n)

Specialized version for the convenience of looking up Names. For all other types return null.

lookupAttrback to summary
public NBinding lookupAttr(String name, boolean supersOnly)

Look up an attribute in the type hierarchy. Don't look at parent link, because the enclosing scope may not be a super class. The search is "depth first, left to right" as in Python's (old) multiple inheritance rule. The new MRO can be implemented, but will probably not introduce much difference.

Parameters
supersOnly:boolean

search only in the supers' scopes, not in local table.

lookupAttrback to summary
public NBinding lookupAttr(String name)

Look up an attribute in the local scope and superclass scopes.

See Also
lookupAttr(String, boolean)
lookupBoundedback to summary
public NBinding lookupBounded(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.

Parameters
name:String

the name to be looked up

typebound:Scope.Type

the type we wish the search to be bounded at

Returns:NBinding

a binding, or null if not found

lookupLocalback to summary
public NBinding lookupLocal(String name)

Look up a name, but only in the current scope.

Returns:NBinding

the local binding for name, or null.

lookupScopeback to summary
public NBinding lookupScope(String name)

Look up a name, but bounded by a scope defining construct. Those scopes are of type module, class, instance or function. This is used in determining the locations of a variable's definition.

lookupTypeback to summary
public NType lookupType(String name)

Look up the scope chain for a binding named name and if found, return its type.

lookupTypeback to summary
public NType lookupType(String name, boolean localOnly)

Look for a binding named name and if found, return its type.

Parameters
localOnly:boolean

true to look only in the current scope; if false, follows the scope chain.

lookupTypeAttrback to summary
public NType lookupTypeAttr(String name)
mergeback to summary
public void merge(Scope other)

Merge all records from another symbol table. Used by import from *.

newLambdaNameback to summary
public String newLambdaName()
putback to summary
public void put(String id, NBinding b)

Directly assigns a binding to a name in this table. Does not add a new definition or reference to the binding. This form of put is often followed by a call to Indexer#putLocation 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 call is omitted.

putback to summary
public NBinding put(String id, NNode loc, NType type, NBinding.Kind kind)

Adds a definition and/or reference to the table. If there is no binding for 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.

putAttrback to summary
public NBinding putAttr(String id, NNode loc, NType type, NBinding.Kind kind)

Same as put, 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.

Returns:NBinding

the new binding, or null if the current scope does not have a properly initialized path.

putBindingback to summary
private void putBinding(String id, NBinding b)
removeback to summary
public void remove(String id)
retargetReferencesback to summary
private void retargetReferences(NBinding b, NType curType)

If the current type had a provisional binding, retarget its refs to the new type. It probably only works one level deep: need dataflow analysis in the general case. However, it does pick up some extra references, so it's reasonable for now.

setNameBindingPhaseback to summary
public void setNameBindingPhase(boolean isBindingPhase)
setParentback to summary
public void setParent(Scope parent)
setPathback to summary
public void setPath(String path)
setPathback to summary
public void setPath(String a, String b)
setScopeTypeback to summary
public void setScopeType(Scope.Type type)
setSupersback to summary
public void setSupers(List<Scope> supers)
setTableback to summary
public void setTable(Map<String, NBinding> table)
toShortStringback to summary
public String toShortString()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override

updateback to summary
public NBinding update(String id, NNode loc, NType type, NBinding.Kind kind)

Adds a new binding for 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).

updateback to summary
public NBinding update(String id, Def loc, NType type, NBinding.Kind kind)

Adds a new binding for 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).

updateTypeback to summary
private void updateType(NBinding b, NNode loc, NType type, NBinding.Kind kind)
valuesback to summary
public Collection<NBinding> values()
org.python.indexer back to summary

public final Enum Scope.Type

extends Enum<Scope.Type>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final Scope.Type
public static final Scope.Type
public static final Scope.Type
public static final Scope.Type
public static final Scope.Type
public static final Scope.Type

Constructor Summary

AccessConstructor and Description
private
Type()

Method Summary

Modifier and TypeMethod and Description
public static Scope.Type
public static Scope.Type[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CLASSback to summary
public static final Scope.Type CLASS
FUNCTIONback to summary
public static final Scope.Type FUNCTION
GLOBALback to summary
public static final Scope.Type GLOBAL
INSTANCEback to summary
public static final Scope.Type INSTANCE
MODULEback to summary
public static final Scope.Type MODULE
SCOPEback to summary
public static final Scope.Type SCOPE

Constructor Detail

Typeback to summary
private Type()

Method Detail

valueOfback to summary
public static Scope.Type valueOf(String name)
valuesback to summary
public static Scope.Type[] values()