polyglot.visit
Class InnerTranslator

java.lang.Object
  extended by polyglot.visit.NodeVisitor
      extended by polyglot.visit.InnerTranslator
All Implemented Interfaces:
java.lang.Cloneable, Copy

public class InnerTranslator
extends NodeVisitor

Author:
xinqi This class translates inner classes to static nested classes with a field referring to the enclosing instance. It will also add "new" methods to the enclosing class corresponding to constructors of inner classes.

Nested Class Summary
protected  class InnerTranslator.ClassInfo
           
protected  class InnerTranslator.CodeInfo
           
 
Field Summary
protected  java.util.Stack classContext
           
protected  java.util.Stack codeContext
           
protected  java.util.HashMap innerClassInfoMap
           
protected  java.util.Stack insideCode
           
protected  NodeFactory nf
           
protected  java.util.Stack staticFieldDecl
           
protected  TypeSystem ts
           
 
Constructor Summary
InnerTranslator(TypeSystem ts, NodeFactory nf)
           
 
Method Summary
protected  ClassDecl addAnonymousConstructor(ClassDecl cd, ParsedClassType ct, InnerTranslator.ClassInfo cinfo, New newExpr)
           
protected  ClassDecl addNewMembers(ClassDecl cd, InnerTranslator.ClassInfo cinfo)
           
 NodeVisitor enter(Node n)
          Begin normal traversal of a subtree rooted at n.
protected  void enterClassDecl(ClassDecl cd)
           
protected  void enterCodeDecl(CodeDecl cd)
           
protected  void enterFieldDecl(FieldDecl fd)
           
protected  void enterLocalDecl(LocalDecl ld)
           
protected  void enterNew(New newExpr)
           
protected  InnerTranslator.ClassInfo findClassInfo(ClassType ct)
          Find ClassInfo for ClassType ct, from innerClassInfoMap.
protected  ParsedClassType findField(java.lang.String name, ClassType current)
          Find the class type inside which a field with specified name is declared.
protected  ParsedClassType findMethod(MethodInstance mi, ClassType current)
          Find the class type inside which a field with specified name is declared.
protected  boolean isSourceType(ClassType ct)
          Check whether ct is a type in source language.
 Node leave(Node old, Node n, NodeVisitor v)
          This method is called after all of the children of n have been visited.
protected  Node leaveCall(Node old, Call c, NodeVisitor v)
           
protected  Node leaveClassDecl(Node old, ClassDecl cd, NodeVisitor v)
           
protected  Node leaveConstructorCall(Node old, ConstructorCall cc, NodeVisitor v)
           
protected  Node leaveField(Node old, Field field, NodeVisitor v)
           
protected  Node leaveLocal(Node old, Local local, NodeVisitor v)
          Translate final local variables that should become field accesses of local/anonymous classes.
protected  Node leaveNew(Node old, New newExpr, NodeVisitor v)
           
protected  Node leaveSpecial(Node old, Special s, NodeVisitor v)
           
protected  java.lang.String namePrefix()
           
protected  java.lang.String newFieldName(java.lang.String name)
           
protected  java.lang.String outerThisName(ClassType ct)
           
protected  ConstructorDecl produceDefaultConstructor(ParsedClassType ct, InnerTranslator.ClassInfo cinfo)
           
protected  ConstructorCall produceDefaultSuperConstructorCall(ClassType ct)
           
protected  java.util.List produceFieldDecls(ClassType ct, InnerTranslator.ClassInfo cinfo)
           
protected  java.util.List produceFieldInits(InnerTranslator.ClassInfo cinfo)
           
protected  Field produceOuterField(ClassType ct, Expr rec)
           
protected  Formal produceOuterFormal(ParsedClassType ct, ParsedClassType oct)
           
protected  Special produceThis(ClassType ct)
          Generate a special node "this" with the correct type.
protected  ClassDecl updateClassDecl(ClassDecl cd, ParsedClassType ct, InnerTranslator.ClassInfo cinfo)
           
protected  ConstructorDecl updateConstructor(ClassDecl cd, ClassType ct, ConstructorDecl cons, InnerTranslator.ClassInfo cinfo)
           
protected  ConstructorCall updateConstructorCall(ConstructorCall cc, InnerTranslator.ClassInfo selfInfo)
           
protected  ConstructorInstance updateConstructorInst(ClassType ct, ConstructorInstance ci, InnerTranslator.ClassInfo cinfo)
           
protected  Expr updateNewExpr(New newExpr)
          Update new expressions to include necessary arguments (for example, enclosing instances) and eliminate qualifers.
 
Methods inherited from class polyglot.visit.NodeVisitor
begin, copy, enter, finish, finish, leave, override, override, toString, visitEdge, visitEdgeNoOverride
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ts

protected TypeSystem ts

nf

protected NodeFactory nf

classContext

protected java.util.Stack classContext

codeContext

protected java.util.Stack codeContext

innerClassInfoMap

protected java.util.HashMap innerClassInfoMap

insideCode

protected java.util.Stack insideCode

staticFieldDecl

protected java.util.Stack staticFieldDecl
Constructor Detail

InnerTranslator

public InnerTranslator(TypeSystem ts,
                       NodeFactory nf)
Method Detail

namePrefix

protected java.lang.String namePrefix()

newFieldName

protected java.lang.String newFieldName(java.lang.String name)

enter

public NodeVisitor enter(Node n)
Description copied from class: NodeVisitor
Begin normal traversal of a subtree rooted at n. This gives the visitor the option of changing internal state or returning a new visitor which will be used to visit the children of n.

This method is typically called by the method enter(parent, n). If a subclass overrides the method enter(parent, n) then this method may not be called.

Overrides:
enter in class NodeVisitor
Parameters:
n - The root of the subtree to be traversed.
Returns:
The NodeVisitor which should be used to visit the children of n.

enterFieldDecl

protected void enterFieldDecl(FieldDecl fd)

enterClassDecl

protected void enterClassDecl(ClassDecl cd)

enterNew

protected void enterNew(New newExpr)

enterCodeDecl

protected void enterCodeDecl(CodeDecl cd)

enterLocalDecl

protected void enterLocalDecl(LocalDecl ld)

leave

public Node leave(Node old,
                  Node n,
                  NodeVisitor v)
Description copied from class: NodeVisitor
This method is called after all of the children of n have been visited. In this case, these children were visited by the visitor v. This is the last chance for the visitor to modify the tree rooted at n. This method will be called exactly the same number of times as entry is called. That is, for each node that is not overriden, enter and leave are each called exactly once.

Note that if old == n then the vistior should make a copy of n before modifying it. It should then return the modified copy.

This method is typically called by the method leave(parent, old, n v). If a subclass overrides the method leave(parent, old, n v) then this method may not be called.

Overrides:
leave in class NodeVisitor
Parameters:
old - The original state of root of the current subtree.
n - The current state of the root of the current subtree.
v - The NodeVisitor object used to visit the children.
Returns:
The final result of the traversal of the tree rooted at n.

leaveClassDecl

protected Node leaveClassDecl(Node old,
                              ClassDecl cd,
                              NodeVisitor v)

leaveNew

protected Node leaveNew(Node old,
                        New newExpr,
                        NodeVisitor v)

leaveConstructorCall

protected Node leaveConstructorCall(Node old,
                                    ConstructorCall cc,
                                    NodeVisitor v)

leaveSpecial

protected Node leaveSpecial(Node old,
                            Special s,
                            NodeVisitor v)

leaveField

protected Node leaveField(Node old,
                          Field field,
                          NodeVisitor v)

leaveCall

protected Node leaveCall(Node old,
                         Call c,
                         NodeVisitor v)

leaveLocal

protected Node leaveLocal(Node old,
                          Local local,
                          NodeVisitor v)
Translate final local variables that should become field accesses of local/anonymous classes.

Parameters:
old -
local -
v -

produceThis

protected Special produceThis(ClassType ct)
Generate a special node "this" with the correct type.

Parameters:
ct -

addAnonymousConstructor

protected ClassDecl addAnonymousConstructor(ClassDecl cd,
                                            ParsedClassType ct,
                                            InnerTranslator.ClassInfo cinfo,
                                            New newExpr)

findField

protected ParsedClassType findField(java.lang.String name,
                                    ClassType current)
Find the class type inside which a field with specified name is declared.


findMethod

protected ParsedClassType findMethod(MethodInstance mi,
                                     ClassType current)
Find the class type inside which a field with specified name is declared.


updateConstructorCall

protected ConstructorCall updateConstructorCall(ConstructorCall cc,
                                                InnerTranslator.ClassInfo selfInfo)

addNewMembers

protected ClassDecl addNewMembers(ClassDecl cd,
                                  InnerTranslator.ClassInfo cinfo)

findClassInfo

protected InnerTranslator.ClassInfo findClassInfo(ClassType ct)
Find ClassInfo for ClassType ct, from innerClassInfoMap.

Parameters:
ct -

isSourceType

protected boolean isSourceType(ClassType ct)
Check whether ct is a type in source language.

Parameters:
ct -

updateNewExpr

protected Expr updateNewExpr(New newExpr)
Update new expressions to include necessary arguments (for example, enclosing instances) and eliminate qualifers.

Parameters:
newExpr -

produceDefaultConstructor

protected ConstructorDecl produceDefaultConstructor(ParsedClassType ct,
                                                    InnerTranslator.ClassInfo cinfo)

updateClassDecl

protected ClassDecl updateClassDecl(ClassDecl cd,
                                    ParsedClassType ct,
                                    InnerTranslator.ClassInfo cinfo)

produceFieldDecls

protected java.util.List produceFieldDecls(ClassType ct,
                                           InnerTranslator.ClassInfo cinfo)

outerThisName

protected java.lang.String outerThisName(ClassType ct)

produceOuterFormal

protected Formal produceOuterFormal(ParsedClassType ct,
                                    ParsedClassType oct)

produceOuterField

protected Field produceOuterField(ClassType ct,
                                  Expr rec)

updateConstructorInst

protected ConstructorInstance updateConstructorInst(ClassType ct,
                                                    ConstructorInstance ci,
                                                    InnerTranslator.ClassInfo cinfo)

produceDefaultSuperConstructorCall

protected ConstructorCall produceDefaultSuperConstructorCall(ClassType ct)

updateConstructor

protected ConstructorDecl updateConstructor(ClassDecl cd,
                                            ClassType ct,
                                            ConstructorDecl cons,
                                            InnerTranslator.ClassInfo cinfo)

produceFieldInits

protected java.util.List produceFieldInits(InnerTranslator.ClassInfo cinfo)