public class SymbolTable
extends java.lang.Object
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
')'.| Modifier and Type | Class and Description |
|---|---|
static class |
SymbolTable.Scope
A symbol table scope.
|
| Modifier and Type | Field and Description |
|---|---|
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 and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
protected SymbolTable.Scope root
protected SymbolTable.Scope current
protected int freshNameCount
protected int freshIdCount
public SymbolTable()
public SymbolTable(java.lang.String root)
root - The name of the root scope.public void reset()
public SymbolTable.Scope root()
public SymbolTable.Scope current()
public SymbolTable.Scope getScope(java.lang.String name)
name - The qualified name.null if no such
scope exits.public void setScope(SymbolTable.Scope scope)
scope - The new current scope.java.lang.IllegalArgumentException - Signals that this symbol table's
root is not the specified scope's root.public boolean isDefined(java.lang.String symbol)
symbol - The symbol.true if the specified symbol is defined.public boolean isDefinedMultiply(java.lang.String symbol)
symbol - The symbol.true if the specified symbol is multiply
defined.public SymbolTable.Scope lookupScope(java.lang.String symbol)
symbol - The symbol.null if no such
scope exits.public java.lang.Object lookup(java.lang.String symbol)
symbol - The symbol.null if no such
definition exists.public void enter(java.lang.String name)
name - The unqualified name.public void exit()
java.lang.IllegalStateException - Signals that the current scope is the root scope.public void delete(java.lang.String name)
name - The unqualified name.public static boolean hasScope(Node n)
scope.n - The node.true if the node has an associated scope.public void mark(Node n)
scope, this method set the property with
the current scope's qualified name.n - The node.public void enter(Node n)
scope, this method tries to enter the scope.
Otherwise, it does not change the scope.n - The node.java.lang.IllegalStateException - Signals that the node's scope is
invalid or not nested within the current scope.public void exit(Node n)
scope, the current scope is exited.n - The node.public java.lang.String freshName()
anonymous" as it base name.freshName(String)public java.lang.String freshName(java.lang.String base)
name(count).base - The base name.public java.lang.String freshCId()
tmp" as its base name.freshCId(String)public java.lang.String freshCId(java.lang.String base)
__name_count.base - The base name.public java.lang.String freshJavaId()
tmp" as its base name.freshJavaId(String)public java.lang.String freshJavaId(java.lang.String base)
name$count.base - The base name.public static java.lang.String toNameSpace(java.lang.String symbol,
java.lang.String space)
symbol - The symbolspace - The name space.public static boolean isInNameSpace(java.lang.String symbol,
java.lang.String space)
symbol - The symbol.space - The name space.true if the symbol is mangled symbol in the
name space.public static java.lang.String fromNameSpace(java.lang.String symbol)
symbol - The mangled symbol within a name space.public static java.lang.String toMacroScopeName(java.lang.String id)
id - The macro identifier.public static boolean isMacroScopeName(java.lang.String name)
name - The name.true if the name denotes a macro scope.public static java.lang.String toFunctionScopeName(java.lang.String id)
id - The function identifier.public static boolean isFunctionScopeName(java.lang.String name)
name - The name.true if the name denotes a function scope.public static java.lang.String toTagName(java.lang.String tag)
tag - The tag.public static java.lang.String toLabelName(java.lang.String id)
id - The identifier.public static java.lang.String toMethodName(java.lang.String id)
id - The method identifier.Copyright © 2014. All Rights Reserved.