scale.clef.symtab
Class Symtab

java.lang.Object
  extended by scale.clef.symtab.Symtab

public final class Symtab
extends java.lang.Object

A class to represent a program's symbol table.

$Id: Symtab.java,v 1.48 2007-08-30 01:48:30 burrill Exp $

Copyright 2008 by the Scale Compiler Group,
Department of Computer Science
University of Massachusetts,
Amherst MA. 01003, USA
All Rights Reserved.

A symbol table comprises many scopes arranged in a tree. Each scope has a unique identification (the scope number) and a local symbol table. A local symbol table is a dictionary which pairs identifiers with entries. A single "entries" is a list of entry nodes, where each node represents a single declared entity. We maintain a list of entry nodes because an identifier could conceivably be used to name multiple entities even within a single scope. Though not currently used, we do assume that with a scope, each use of a single identifier names a different kind of entity. The other classes are:

SymtabScope
Represents a single scope.
SymtabEntry
Represents a single symbol table entry.


Constructor Summary
Symtab()
          Create a symbol table.
 
Method Summary
 SymtabEntry addRootSymbol(Declaration decl)
          Add a symbol to the current scope.
 SymtabEntry addSymbol(Declaration decl)
          Add a symbol to the current scope.
 SymtabScope beginScope()
          Open a new scope as a child of the current socpe.
 SymtabScope endScope()
          Ends the current scope, and the current scope's outer scope becomes the current scope.
 RoutineDecl findRoutine(java.lang.String rname)
          Return the RoutineDecl specified.
 SymtabScope getCurrentScope()
          Return the current scope.
 SymtabScope getRootScope()
          Return the root scope.
 boolean isAtRoot()
          Return true if the current scope is the root scope.
 SymtabScope lookupScope(Declaration decl)
          Return the scope associated with the Declaration.
 SymtabEntry lookupSymbol(Declaration d)
          Return the symbol table entry for a specific declaration.
 Vector<SymtabEntry> lookupSymbol(java.lang.String id)
          Look in the current scope for a symbol table entry matching a specific name.
 SymtabEntry replaceSymbol(Declaration oldDecl, Declaration newDecl)
          Replace an existing symbol in the current scope.
 void setCurrentScope(SymtabScope scope)
          Set the current scope.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Symtab

public Symtab()
Create a symbol table.

Method Detail

toString

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

beginScope

public final SymtabScope beginScope()
Open a new scope as a child of the current socpe. The new scope becomes the current scope.

Returns:
a pointer to the new current scope.
Throws:
InvalidTableError - if we have incorrectly created a new scope

endScope

public final SymtabScope endScope()
Ends the current scope, and the current scope's outer scope becomes the current scope.

Returns:
a pointer to the new current scope.

getCurrentScope

public final SymtabScope getCurrentScope()
Return the current scope.


getRootScope

public final SymtabScope getRootScope()
Return the root scope.


isAtRoot

public boolean isAtRoot()
Return true if the current scope is the root scope.


setCurrentScope

public final void setCurrentScope(SymtabScope scope)
Set the current scope. Use this routine when walking through a clef tree to set the scope. Then, new symbols and scopes may be added to the symbol table at the correct place.

Parameters:
scope - the scope

addSymbol

public final SymtabEntry addSymbol(Declaration decl)
Add a symbol to the current scope.

Parameters:
decl - the declaration for the symbol
Returns:
a symbol table entry for the symbol

replaceSymbol

public final SymtabEntry replaceSymbol(Declaration oldDecl,
                                       Declaration newDecl)
Replace an existing symbol in the current scope.

Parameters:
oldDecl - the old declaration for the symbol
newDecl - the new declaration for the symbol
Returns:
a SymtabEntry for the new declaration or null if not found

addRootSymbol

public final SymtabEntry addRootSymbol(Declaration decl)
Add a symbol to the current scope.

Parameters:
decl - the declaration for the symbol
Returns:
a symbol table entry for the symbol

lookupScope

public SymtabScope lookupScope(Declaration decl)
Return the scope associated with the Declaration.


lookupSymbol

public final Vector<SymtabEntry> lookupSymbol(java.lang.String id)
Look in the current scope for a symbol table entry matching a specific name. The current scope includes enclosing scopes as well. This routine returns a list of symbol table entries since the name may be defined more than once. It is the responsibility of the user to search through the list for the appropriate entry. The current scope's entries are first in the vector.

Parameters:
id - the symbol's name
Returns:
a vector of SymtabEntry instances in order of scope

lookupSymbol

public final SymtabEntry lookupSymbol(Declaration d)
Return the symbol table entry for a specific declaration. If the declaration doesn't appear in the current scope, then return null. The current scope includes the enclosing scopes as well. We return the symbol table entry that is closest to the scope that we're currently in.

Parameters:
d - the declaration
Returns:
the symbol table entry for the declaration (or null if not found).

findRoutine

public RoutineDecl findRoutine(java.lang.String rname)
Return the RoutineDecl specified.