com.google.dart.compiler.backend.js.ast
Class JsScope

java.lang.Object
  extended by com.google.dart.compiler.backend.js.ast.JsScope
Direct Known Subclasses:
JsCatchScope, JsRootScope

public abstract class JsScope
extends java.lang.Object

A scope is a factory for creating and allocating JsNames. A JavaScript AST is built in terms of abstract name objects without worrying about obfuscation, keyword/identifier blacklisting, and so on.

Scopes are associated with JsFunctions, but the two are not equivalent. Functions have scopes, but a scope does not necessarily have an associated Function. Examples of this include the JsRootScope and synthetic scopes that might be created by a client.

Scopes can have parents to provide constraints when allocating actual identifiers for names. Specifically, names in child scopes are chosen such that they do not conflict with names in their parent scopes. The ultimate parent is usually the global scope (see JsProgram.getRootScope()), but parentless scopes are useful for managing names that are always accessed with a qualifier and could therefore never be confused with the global scope hierarchy.


Field Summary
protected  int tempIndex
           
 
Constructor Summary
  JsScope(JsScope parent, java.lang.String description, java.lang.String scopeId)
           
protected JsScope(java.lang.String description)
           
 
Method Summary
 void copyOwnNames(JsScope other)
           
 JsName declareFreshName(java.lang.String suggestedName)
          Creates a new variable with an unique ident in this scope.
 JsName declareName(java.lang.String identifier)
          Gets a name object associated with the specified identifier in this scope, creating it if necessary.
If the JsName does not exist yet, a new JsName is created.
 JsName declareTemporary()
          Creates a temporary variable with an unique name in this scope.
protected  JsName doCreateName(java.lang.String ident)
           
 JsName findName(java.lang.String ident)
          Attempts to find the name object for the specified ident, searching in this scope, and if not found, in the parent scopes.
protected  JsName findOwnName(java.lang.String ident)
          Attempts to find the name object for the specified ident, searching in this scope only.
 java.lang.String getDescription()
           
 JsScope getParent()
          Returns the parent scope of this scope, or null if this is the root scope.
 JsProgram getProgram()
           
 boolean hasOwnName(java.lang.String name)
           
 JsScope innerObjectScope(java.lang.String scopeName)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

tempIndex

protected int tempIndex
Constructor Detail

JsScope

public JsScope(JsScope parent,
               @NotNull
               java.lang.String description,
               @Nullable
               java.lang.String scopeId)

JsScope

protected JsScope(@NotNull
                  java.lang.String description)
Method Detail

innerObjectScope

@NotNull
public JsScope innerObjectScope(@NotNull
                                        java.lang.String scopeName)

declareName

@NotNull
public JsName declareName(@NotNull
                                  java.lang.String identifier)
Gets a name object associated with the specified identifier in this scope, creating it if necessary.
If the JsName does not exist yet, a new JsName is created. The identifier, short name, and original name of the newly created JsName are equal to the given identifier.

Parameters:
identifier - An identifier that is unique within this scope.

declareFreshName

@NotNull
public JsName declareFreshName(@NotNull
                                       java.lang.String suggestedName)
Creates a new variable with an unique ident in this scope. The generated JsName is guaranteed to have an identifier that does not clash with any existing variables in the scope. Future declarations of variables might however clash with the temporary (unless they use this function).


declareTemporary

@NotNull
public JsName declareTemporary()
Creates a temporary variable with an unique name in this scope. The generated temporary is guaranteed to have an identifier (but not short name) that does not clash with any existing variables in the scope. Future declarations of variables might however clash with the temporary.


findName

@Nullable
public final JsName findName(java.lang.String ident)
Attempts to find the name object for the specified ident, searching in this scope, and if not found, in the parent scopes.

Returns:
null if the identifier has no associated name

hasOwnName

public boolean hasOwnName(@NotNull
                          java.lang.String name)

getParent

public final JsScope getParent()
Returns the parent scope of this scope, or null if this is the root scope.


getProgram

public JsProgram getProgram()

toString

public final java.lang.String toString()
Overrides:
toString in class java.lang.Object

copyOwnNames

public void copyOwnNames(JsScope other)

getDescription

@NotNull
public java.lang.String getDescription()

doCreateName

@NotNull
protected JsName doCreateName(@NotNull
                                      java.lang.String ident)

findOwnName

protected JsName findOwnName(@NotNull
                             java.lang.String ident)
Attempts to find the name object for the specified ident, searching in this scope only.

Returns:
null if the identifier has no associated name