xtc.util
Class SymbolTable

java.lang.Object
  extended by xtc.util.SymbolTable

public class SymbolTable
extends java.lang.Object

A symbol table. This class implements a symbol table, which maps symbols represented as strings to values of any type. The mapping is organized into hierarchical scopes, which allows for multiple definitions of the same symbol across different scopes. Additionally, a symbol may have multiple definitions within the same scope: if the corresponding value is a Java collections framework list, it is recognized as a multiply defined symbol. Scopes are named, with names being represented as strings. Both scope names and symbols can be unqualified — that is, they need to be resolved relative to the current scope — or qualified by the qualification character '.' — that is, they are resolved relative to the symbol table's root. Once created, a scope remains in the symbol table and the corresponding AST node should be associated with that scope by setting the corresponding property to the scope's qualified name. Subsequent traversals over that node can then automatically enter and exit that scope. Alternatively, if traversing out of tree order, the current scope can be set explicitly.

To support different name spaces within the same scope, this class can optionally mangle and unmangle unqualified symbols. By convention, a name in any name space besides the default name space is prefixed by the name of the name space and an opening parenthesis '(' and suffixed by a closing parenthesis ')'.

Version:
$Revision: 1.35 $
Author:
Robert Grimm

Nested Class Summary
static class SymbolTable.Scope
          A symbol table scope.
 
Field Summary
protected  SymbolTable.Scope current
          The current scope.
protected  int freshIdCount
          The fresh identifier count.
protected  int freshNameCount
          The fresh name count.
protected  SymbolTable.Scope root
          The root scope.
 
Constructor Summary
SymbolTable()
          Create a new symbol table with the empty string as the root scope's name.
SymbolTable(java.lang.String root)
          Create a new symbol table.
 
Method Summary
 SymbolTable.Scope current()
          Get the current scope.
 void delete(java.lang.String name)
          Delete the scope with the specified unqualified name.
 void enter(Node n)
          Enter the specified node.
 void enter(java.lang.String name)
          Enter the scope with the specified unqualified name.
 void exit()
          Exit the current scope.
 void exit(Node n)
          Exit the specified node.
 java.lang.String freshCId()
          Create a fresh C identifier.
 java.lang.String freshCId(java.lang.String base)
          Create a fresh C identifier incorporating the specified base name.
 java.lang.String freshJavaId()
          Create a fresh Java identifier.
 java.lang.String freshJavaId(java.lang.String base)
          Create a fresh Java identifier incorporating the specified base name.
 java.lang.String freshName()
          Create a fresh name.
 java.lang.String freshName(java.lang.String base)
          Create a fresh name incorporating the specified base name.
static java.lang.String fromNameSpace(java.lang.String symbol)
          Convert the specified unqualified symbol within a name space to a symbol without a name space.
 SymbolTable.Scope getScope(java.lang.String name)
          Get the scope with the specified qualified name.
static boolean hasScope(Node n)
          Determine whether the specified node has an associated scope.
 boolean isDefined(java.lang.String symbol)
          Determine whether the specified symbol is defined.
 boolean isDefinedMultiply(java.lang.String symbol)
          Determine whether the specified symbol is define multiple times.
static boolean isFunctionScopeName(java.lang.String name)
          Determine whether the specified scope name represents a function's scope.
static boolean isInNameSpace(java.lang.String symbol, java.lang.String space)
          Determine whether the specified symbol is in the specified name space.
static boolean isMacroScopeName(java.lang.String name)
          Determine whether the specified scope name represents a macro's scope.
 java.lang.Object lookup(java.lang.String symbol)
          Get the value for the specified symbol.
 SymbolTable.Scope lookupScope(java.lang.String symbol)
          Get the scope for the specified symbol.
 void mark(Node n)
          Mark the specified node.
 void reset()
          Clear this symbol table.
 SymbolTable.Scope root()
          Get the root scope.
 void setScope(SymbolTable.Scope scope)
          Set the current scope to the specified scope.
static java.lang.String toFunctionScopeName(java.lang.String id)
          Convert the specified C function identifier into a symbol table scope name.
static java.lang.String toLabelName(java.lang.String id)
          Convert the specified label identifier into a symbol table name.
static java.lang.String toMacroScopeName(java.lang.String id)
          Conver the specified C macro identifier into a symbol table scope name.
static java.lang.String toMethodName(java.lang.String id)
          Convert the specified method identifier into a symbol table name.
static java.lang.String toNameSpace(java.lang.String symbol, java.lang.String space)
          Convert the specified unqualified symbol to a symbol in the specified name space.
static java.lang.String toTagName(java.lang.String tag)
          Convert the specified C struct, union, or enum tag into a symbol table name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

protected SymbolTable.Scope root
The root scope.


current

protected SymbolTable.Scope current
The current scope.


freshNameCount

protected int freshNameCount
The fresh name count.


freshIdCount

protected int freshIdCount
The fresh identifier count.

Constructor Detail

SymbolTable

public SymbolTable()
Create a new symbol table with the empty string as the root scope's name.


SymbolTable

public SymbolTable(java.lang.String root)
Create a new symbol table.

Parameters:
root - The name of the root scope.
Method Detail

reset

public void reset()
Clear this symbol table. This method deletes all scopes and their definitions from this symbol table.


root

public SymbolTable.Scope root()
Get the root scope.

Returns:
The root scope.

current

public SymbolTable.Scope current()
Get the current scope.

Returns:
The current scope.

getScope

public SymbolTable.Scope getScope(java.lang.String name)
Get the scope with the specified qualified name.

Parameters:
name - The qualified name.
Returns:
The corresponding scope or null if no such scope exits.

setScope

public void setScope(SymbolTable.Scope scope)
Set the current scope to the specified scope.

Parameters:
scope - The new current scope.
Throws:
java.lang.IllegalArgumentException - Signals that this symbol table's root is not the specified scope's root.

isDefined

public boolean isDefined(java.lang.String symbol)
Determine whether the specified symbol is defined. If the symbol is qualified, this method checks whether the symbol is defined in the named scope. Otherwise, it checks whether the symbol is defined in the current scope or one of its ancestors.

Parameters:
symbol - The symbol.
Returns:
true if the specified symbol is defined.

isDefinedMultiply

public boolean isDefinedMultiply(java.lang.String symbol)
Determine whether the specified symbol is define multiple times. If the symbol is qualified, this method checks whether the symbol has multiple definitions in the named scope. Otherwise, it checks whether the symbol has multiple definitions in the current scope or one of its ancestors.

Parameters:
symbol - The symbol.
Returns:
true if the specified symbol is multiply defined.

lookupScope

public SymbolTable.Scope lookupScope(java.lang.String symbol)
Get the scope for the specified symbol. If the symbol is qualified, this method returns the named scope (without checking whether the symbol is defined in that scope). Otherwise, it searches the current scope and all its ancestors, returning the first defining scope.

Parameters:
symbol - The symbol.
Returns:
The corresponding scope or null if no such scope exits.

lookup

public java.lang.Object lookup(java.lang.String symbol)
Get the value for the specified symbol. If the symbol is qualified, this method returns the definition within the named scope. Otherwise, it searches the current scope and all its ancestors, returning the value of the first definition.

Parameters:
symbol - The symbol.
Returns:
The corresponding value or null if no such definition exists.

enter

public void enter(java.lang.String name)
Enter the scope with the specified unqualified name. If the current scope does not have a scope with the specified name, a new scope with the specified name is created. In either case, the scope with that name becomes the current scope.

Parameters:
name - The unqualified name.

exit

public void exit()
Exit the current scope.

Throws:
java.lang.IllegalStateException - Signals that the current scope is the root scope.

delete

public void delete(java.lang.String name)
Delete the scope with the specified unqualified name. If the current scope contains a nested scope with the specified name, this method deletes that scope and all its contents, including nested scopes.

Parameters:
name - The unqualified name.

hasScope

public static boolean hasScope(Node n)
Determine whether the specified node has an associated scope.

Parameters:
n - The node.
Returns:
true if the node has an associated scope.

mark

public void mark(Node n)
Mark the specified node. If the node does not have an associated scope, this method set the property with the current scope's qualified name.

Parameters:
n - The node.

enter

public void enter(Node n)
Enter the specified node. If the node has an associated scope, this method tries to enter the scope. Otherwise, it does not change the scope.

Parameters:
n - The node.
Throws:
java.lang.IllegalStateException - Signals that the node's scope is invalid or not nested within the current scope.

exit

public void exit(Node n)
Exit the specified node. If the node has an associated scope, the current scope is exited.

Parameters:
n - The node.

freshName

public java.lang.String freshName()
Create a fresh name. The returned name has "anonymous" as it base name.

Returns:
A fresh name.
See Also:
freshName(String)

freshName

public java.lang.String freshName(java.lang.String base)
Create a fresh name incorporating the specified base name. The returned name is of the form name(count).

Parameters:
base - The base name.
Returns:
The corresponding fresh name.

freshCId

public java.lang.String freshCId()
Create a fresh C identifier. The returned identifier has "tmp" as its base name.

Returns:
A fresh C identifier.
See Also:
freshCId(String)

freshCId

public java.lang.String freshCId(java.lang.String base)
Create a fresh C identifier incorporating the specified base name. The returned name is of the form __name_count.

Parameters:
base - The base name.
Returns:
The corresponding fresh C identifier.

freshJavaId

public java.lang.String freshJavaId()
Create a fresh Java identifier. The returned identifier has "tmp" as its base name.

Returns:
A fresh Java identifier.
See Also:
freshJavaId(String)

freshJavaId

public java.lang.String freshJavaId(java.lang.String base)
Create a fresh Java identifier incorporating the specified base name. The returned name is of the form name$count.

Parameters:
base - The base name.
Returns:
The corresponding fresh Java identifier.

toNameSpace

public static java.lang.String toNameSpace(java.lang.String symbol,
                                           java.lang.String space)
Convert the specified unqualified symbol to a symbol in the specified name space.

Parameters:
symbol - The symbol
space - The name space.
Returns:
The mangled symbol.

isInNameSpace

public static boolean isInNameSpace(java.lang.String symbol,
                                    java.lang.String space)
Determine whether the specified symbol is in the specified name space.

Parameters:
symbol - The symbol.
space - The name space.
Returns:
true if the symbol is mangled symbol in the name space.

fromNameSpace

public static java.lang.String fromNameSpace(java.lang.String symbol)
Convert the specified unqualified symbol within a name space to a symbol without a name space.

Parameters:
symbol - The mangled symbol within a name space.
Returns:
The corresponding symbol without a name space.

toMacroScopeName

public static java.lang.String toMacroScopeName(java.lang.String id)
Conver the specified C macro identifier into a symbol table scope name.

Parameters:
id - The macro identifier.
Returns:
The corresponding symbol table scope name.

isMacroScopeName

public static boolean isMacroScopeName(java.lang.String name)
Determine whether the specified scope name represents a macro's scope.

Parameters:
name - The name.
Returns:
true if the name denotes a macro scope.

toFunctionScopeName

public static java.lang.String toFunctionScopeName(java.lang.String id)
Convert the specified C function identifier into a symbol table scope name.

Parameters:
id - The function identifier.
Returns:
The corresponding symbol table scope name.

isFunctionScopeName

public static boolean isFunctionScopeName(java.lang.String name)
Determine whether the specified scope name represents a function's scope.

Parameters:
name - The name.
Returns:
true if the name denotes a function scope.

toTagName

public static java.lang.String toTagName(java.lang.String tag)
Convert the specified C struct, union, or enum tag into a symbol table name.

Parameters:
tag - The tag.
Returns:
The corresponding symbol table name.

toLabelName

public static java.lang.String toLabelName(java.lang.String id)
Convert the specified label identifier into a symbol table name.

Parameters:
id - The identifier.
Returns:
The corresponding symbol table name.

toMethodName

public static java.lang.String toMethodName(java.lang.String id)
Convert the specified method identifier into a symbol table name.

Parameters:
id - The method identifier.
Returns:
The corresponding symbol table name.


Copyright © 2012. All Rights Reserved.