cetus.hir
Class TranslationUnit

java.lang.Object
  extended by cetus.hir.TranslationUnit
All Implemented Interfaces:
Printable, SymbolTable, Traversable

public final class TranslationUnit
extends java.lang.Object
implements SymbolTable, Traversable

Represents a single source file of the program. A TranslationUnit is a collection of Declarations and DeclarationStatements. The compilation of a Program may involve several TranslationUnits.


Constructor Summary
TranslationUnit(java.lang.String input_filename)
          Create an empty translation unit associated with a file.
 
Method Summary
 void addDeclaration(Declaration decl)
          Add a declaration to the end of the set of declarations and place the declared symbols in the symbol table.
 void addDeclarationAfter(Declaration ref, Declaration decl)
          Add a declaration after the reference declaration.
 void addDeclarationBefore(Declaration ref, Declaration decl)
          Add a declaration before the reference declaration.
 void addDeclarationFirst(Declaration decl)
           
 void addFirstDeclaration(Declaration decl)
           
static void defaultPrint(TranslationUnit tunit, java.io.OutputStream stream)
          Prints a translation unit to a stream without skipping the included headers.
static void defaultPrint2(TranslationUnit tu, java.io.OutputStream stream)
           
 Declaration findSymbol(IDExpression name)
          Retrieves the declaration for a symbol, possibly searching through parent symbol tables.
 java.util.List<Traversable> getChildren()
          Provides access to the children of this object as a list.
 Declaration getFirstDeclaration()
          Returns the first declaration that comes right after header files.
 java.lang.String getInputFilename()
          Returns the original filename for the translation unit.
 java.lang.String getOutputFilename()
          Returns the filename for the modified version of the translation unit.
 Traversable getParent()
          Provides access to the parent of this object.
 java.util.List getParentTables()
          Returns a list of symbol tables that are parents of this table in the distributed symbol table graph.
 java.util.Map<IDExpression,Declaration> getTable()
          Provides direct access to the symbol table; it's generally not a good idea to modify this directly.
 void parse()
          Parse this translation unit.
protected  void parseAntlr()
          Parse the associated input file using the Antlr parser and create IR for this translation unit.
protected  void parseExternal()
          Parse the associated input file using an external parser and create IR for this translation unit.
 void print()
          Prints this translation unit to the output file with which it is associated.
 void print(java.io.OutputStream stream)
          Print the code for the IR represented by the object.
 void removeChild(Traversable child)
          Removes the specified child.
 void setChild(int index, Traversable t)
          Sets the indexth child of this object to t.
static void setClassPrintMethod(java.lang.reflect.Method m)
          Overrides the class print method, so that all subsequently created objects will use the supplied method.
 void setOutputFilename(java.lang.String output_filename)
          Sets the output filename for the translation unit.
 void setParent(Traversable t)
          Sets the parent program for this translation unit.
 void setPrintMethod(java.lang.reflect.Method m)
          Overrides the print method for this object only.
 java.lang.String toControlledString()
          Returns a string representation of this translation unit skipping unnecessary headers.
 java.lang.String toString()
          Returns a string representation of this translation unit without skipping the included headers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TranslationUnit

public TranslationUnit(java.lang.String input_filename)
Create an empty translation unit associated with a file.

Parameters:
input_filename - The file name for this TranslationUnit
Method Detail

addDeclaration

public void addDeclaration(Declaration decl)
Description copied from interface: SymbolTable
Add a declaration to the end of the set of declarations and place the declared symbols in the symbol table.

Specified by:
addDeclaration in interface SymbolTable
Parameters:
decl - The declaration to add.

addDeclarationFirst

public void addDeclarationFirst(Declaration decl)

addFirstDeclaration

public void addFirstDeclaration(Declaration decl)

addDeclarationBefore

public void addDeclarationBefore(Declaration ref,
                                 Declaration decl)
Description copied from interface: SymbolTable
Add a declaration before the reference declaration.

Specified by:
addDeclarationBefore in interface SymbolTable
Parameters:
ref - The reference point.
decl - The declaration to add.

addDeclarationAfter

public void addDeclarationAfter(Declaration ref,
                                Declaration decl)
Description copied from interface: SymbolTable
Add a declaration after the reference declaration.

Specified by:
addDeclarationAfter in interface SymbolTable
Parameters:
ref - The reference point.
decl - The declaration to add.

getFirstDeclaration

public Declaration getFirstDeclaration()
Returns the first declaration that comes right after header files.

Returns:
First declaration that comes after header files.

defaultPrint

public static void defaultPrint(TranslationUnit tunit,
                                java.io.OutputStream stream)
Prints a translation unit to a stream without skipping the included headers.

Parameters:
tunit - The translation unit to print.
stream - The stream on which to print the translation unit.

toString

public java.lang.String toString()
Returns a string representation of this translation unit without skipping the included headers.

Overrides:
toString in class java.lang.Object

toControlledString

public java.lang.String toControlledString()
Returns a string representation of this translation unit skipping unnecessary headers.


findSymbol

public Declaration findSymbol(IDExpression name)
Description copied from interface: SymbolTable
Retrieves the declaration for a symbol, possibly searching through parent symbol tables.

Specified by:
findSymbol in interface SymbolTable
Parameters:
name - The name of the symbol.

getChildren

public java.util.List<Traversable> getChildren()
Description copied from interface: Traversable
Provides access to the children of this object as a list. This object is free to internally implement its list as a LinkedList or an LinkedList. It is generally not good practice to call this method yourself; the ordering of children is not guaranteed so instead you should use the methods of the particular class whose children you wish to access.

Specified by:
getChildren in interface Traversable
Returns:
the children as a list.

getInputFilename

public java.lang.String getInputFilename()
Returns the original filename for the translation unit.

Returns:
the original filename for the translation unit.

getOutputFilename

public java.lang.String getOutputFilename()
Returns the filename for the modified version of the translation unit.

Returns:
the filename for the modified version of the translation unit.

getParent

public Traversable getParent()
Description copied from interface: Traversable
Provides access to the parent of this object. Every IR object has at most one parent. (The parent relationship is the same as the parent relationship in the parse tree, and is not to be confused with the base class of a derived class declaration.)

Specified by:
getParent in interface Traversable
Returns:
the parent of this object.

getParentTables

public java.util.List getParentTables()
Description copied from interface: SymbolTable
Returns a list of symbol tables that are parents of this table in the distributed symbol table graph. A list is necessary because representing multiple inheritence in C++ requires it. Note that the parent tables are not necessarily tables of the immediate parent. The list is all symbol tables enclosing this object which appear at the same level such that no other symbol tables appear between this object and those tables.

Specified by:
getParentTables in interface SymbolTable
Returns:
a List of SymbolTables.

getTable

public java.util.Map<IDExpression,Declaration> getTable()
Description copied from interface: SymbolTable
Provides direct access to the symbol table; it's generally not a good idea to modify this directly.

Specified by:
getTable in interface SymbolTable
Returns:
a map of IDExpressions to Declarations.

parse

public void parse()
           throws java.io.IOException
Parse this translation unit.

Throws:
java.io.IOException - if there is a problem accessing any file.

parseAntlr

protected void parseAntlr()
Parse the associated input file using the Antlr parser and create IR for this translation unit.

Throws:
java.io.IOException - if there is any problem accessing the file.

parseExternal

protected void parseExternal()
                      throws java.io.IOException
Parse the associated input file using an external parser and create IR for this translation unit.

Throws:
java.io.IOException - if there is any problem accessing the file.

defaultPrint2

public static void defaultPrint2(TranslationUnit tu,
                                 java.io.OutputStream stream)

print

public void print()
           throws java.io.FileNotFoundException
Prints this translation unit to the output file with which it is associated.

Throws:
java.io.FileNotFoundException - if an output file could not be opened.

print

public void print(java.io.OutputStream stream)
Description copied from interface: Printable
Print the code for the IR represented by the object. Always calls object_print_method(this, stream). If the object's print method is null, nothing is printed; this provides an easy mechanism to temporarily hide something.

Specified by:
print in interface Printable
Parameters:
stream - The stream on which to print the data.

removeChild

public void removeChild(Traversable child)
Description copied from interface: Traversable
Removes the specified child.

Specified by:
removeChild in interface Traversable
Parameters:
child - a reference to a child object that must match with ==.

setChild

public void setChild(int index,
                     Traversable t)
Description copied from interface: Traversable
Sets the indexth child of this object to t. It is generally not good practice to call this method yourself; the ordering of children is not guaranteed so instead you should use the methods of the particular class whose children you wish to access. There are checks to prevent such actions as making a statement a child of an expression, but you still may be able to do some damage with this method if you are not careful.

Specified by:
setChild in interface Traversable

setClassPrintMethod

public static void setClassPrintMethod(java.lang.reflect.Method m)
Overrides the class print method, so that all subsequently created objects will use the supplied method.

Parameters:
m - The new print method.

setOutputFilename

public void setOutputFilename(java.lang.String output_filename)
Sets the output filename for the translation unit.

Parameters:
output_filename - The output filename for the translation unit.

setParent

public void setParent(Traversable t)
Sets the parent program for this translation unit.

Specified by:
setParent in interface Traversable
Parameters:
t - The parent program.

setPrintMethod

public void setPrintMethod(java.lang.reflect.Method m)
Overrides the print method for this object only.

Parameters:
m - The new print method.