polyglot.visit
Class Translator

java.lang.Object
  extended by polyglot.visit.PrettyPrinter
      extended by polyglot.visit.Translator
All Implemented Interfaces:
java.lang.Cloneable, Copy
Direct Known Subclasses:
HeaderTranslator

public class Translator
extends PrettyPrinter
implements Copy

A Translator generates output code from the processed AST. Output is sent to one or more java file in the directory Options.output_directory. Each SourceFile in the AST is output to exactly one java file. The name of that file is determined as follows:

To use:
     new Translator(job, ts, nf, tf).translate(ast);
 
The ast must be either a SourceFile or a SourceCollection.


Field Summary
protected  Context context
           
protected  Job job
           
protected  NodeFactory nf
           
protected  ClassType outerClass
           
protected  TargetFactory tf
           
protected  TypeSystem ts
           
 
Fields inherited from class polyglot.visit.PrettyPrinter
appendSemicolon, printType
 
Constructor Summary
Translator(Job job, TypeSystem ts, NodeFactory nf, TargetFactory tf)
          Create a Translator.
 
Method Summary
 Context context()
          Get the current context in which we are translating.
 Translator context(Context c)
          Create a new Translator identical to this but with new context c
 java.lang.Object copy()
          Copy the translator.
static java.lang.String cScope(java.lang.String s)
          Turns a package or class name from Java "x.y.z" format into a C-style scope ("x::y::z")
protected  java.util.List exports(SourceFile sfn)
          Get the list of public top-level classes declared in the source file.
static java.util.HashMap getFileNames()
           
 HeaderTranslator headerContext(Context c)
          Create a new Translator identical to this, except: a) wrapped inside a HeaderTranslator object, and b) with a new context c
 Job job()
          Return the job associated with this Translator.
static java.lang.String macroEscape(java.lang.String s)
          "Escapes" an input string "s" so that it can be used as a macro.
 NodeFactory nodeFactory()
          Get the extension's node factory.
 ClassType outerClass()
          Set the outer class context of the translator.
 void print(Node parent, Node child, CodeWriter w)
          Print an ast node using the given code writer.
 void setOuterClass(ClassType ct)
          Destructively set the outer class context of the translator.
 TargetFactory targetFactory()
           
 java.lang.String toString()
           
 boolean translate(Node ast)
          Translate the entire AST.
protected  boolean translateSource(SourceFile sfn)
          Transate a single SourceFile node
 TypeSystem typeSystem()
          Get the extension's type system.
protected  void writeFooter(SourceFile sfn, CodeWriter w)
          C++ files also require a footer terminal '}' because they need to close the namespace they're opening.
protected  void writeHeader(SourceFile sfn, CodeWriter w)
          Write the package and import declarations for a source file.
protected  void writeHFileFooter(SourceFile sfn, CodeWriter w)
          Write the footer of the .h file if we're in C++ mode
protected  void writeHFileHeader(SourceFile sfn, java.lang.String className, CodeWriter w)
          Write the opening lines of the header file for a given class
 
Methods inherited from class polyglot.visit.PrettyPrinter
appendSemicolon, appendSemicolon, printAst, printType, printType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

job

protected Job job

nf

protected NodeFactory nf

tf

protected TargetFactory tf

ts

protected TypeSystem ts

context

protected Context context

outerClass

protected ClassType outerClass
Constructor Detail

Translator

public Translator(Job job,
                  TypeSystem ts,
                  NodeFactory nf,
                  TargetFactory tf)
Create a Translator. The output of the visitor is a collection of files whose names are added to the collection outputFiles.

Method Detail

getFileNames

public static java.util.HashMap getFileNames()

job

public Job job()
Return the job associated with this Translator.


context

public Translator context(Context c)
Create a new Translator identical to this but with new context c


copy

public java.lang.Object copy()
Copy the translator.

Specified by:
copy in interface Copy

headerContext

public HeaderTranslator headerContext(Context c)
Create a new Translator identical to this, except: a) wrapped inside a HeaderTranslator object, and b) with a new context c

Parameters:
c - - the new context to use
Returns:
- a header translator identical to this one, but with new context.

outerClass

public ClassType outerClass()
Set the outer class context of the translator. This class is used when translating "new" expressions for nested classes. For the expression "e.new C()", the name "C" needs to be looked up in the context of the static type of expression "e" (i.e., outerClass), rather than in the current context returned by context().


setOuterClass

public void setOuterClass(ClassType ct)
Destructively set the outer class context of the translator.


typeSystem

public TypeSystem typeSystem()
Get the extension's type system.


context

public Context context()
Get the current context in which we are translating.


nodeFactory

public NodeFactory nodeFactory()
Get the extension's node factory.


targetFactory

public TargetFactory targetFactory()

print

public void print(Node parent,
                  Node child,
                  CodeWriter w)
Print an ast node using the given code writer. This method should not be called directly to translate a source file AST; use translate(Node) instead. This method should only be called by nodes to print their children.

Overrides:
print in class PrettyPrinter

translate

public boolean translate(Node ast)
Translate the entire AST.


translateSource

protected boolean translateSource(SourceFile sfn)
Transate a single SourceFile node


macroEscape

public static java.lang.String macroEscape(java.lang.String s)
"Escapes" an input string "s" so that it can be used as a macro. Removes all '.' and ':' chars and substitutes in '_' instead.

Parameters:
s - - the input string to escape.
Returns:
an escaped string.

cScope

public static java.lang.String cScope(java.lang.String s)
Turns a package or class name from Java "x.y.z" format into a C-style scope ("x::y::z")

Parameters:
s - the input package or class name
Returns:
A c-scoped version of s

writeHFileHeader

protected void writeHFileHeader(SourceFile sfn,
                                java.lang.String className,
                                CodeWriter w)
Write the opening lines of the header file for a given class

Parameters:
sfn - - representation of the source file we're compiling; used for Imports.
className - - The name of the class we're describing
w - - The CodeWriter to write it all out to (the .h file)

writeHFileFooter

protected void writeHFileFooter(SourceFile sfn,
                                CodeWriter w)
Write the footer of the .h file if we're in C++ mode

Parameters:
w -

writeFooter

protected void writeFooter(SourceFile sfn,
                           CodeWriter w)
C++ files also require a footer terminal '}' because they need to close the namespace they're opening.


writeHeader

protected void writeHeader(SourceFile sfn,
                           CodeWriter w)
Write the package and import declarations for a source file.


exports

protected java.util.List exports(SourceFile sfn)
Get the list of public top-level classes declared in the source file.


toString

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