polyglot.visit
Class AscriptionVisitor

java.lang.Object
  extended by polyglot.visit.NodeVisitor
      extended by polyglot.visit.HaltingVisitor
          extended by polyglot.visit.ErrorHandlingVisitor
              extended by polyglot.visit.ContextVisitor
                  extended by polyglot.visit.AscriptionVisitor
All Implemented Interfaces:
java.lang.Cloneable, Copy
Direct Known Subclasses:
PaoBoxer

public class AscriptionVisitor
extends ContextVisitor

Visitor which allows type information to be utilized to perform AST modifications. The major advantage of this visitor is the new ascribe() method, which allows AST translations based on the expression and also the type that is expected. For the base translation (standard Java), the type of the expression and the type that is expceted are the same. Language extensions however may not have this property, and can take advantage of the ascribe method to transform the AST into a different form that will pass Java type-checking.

See Also:
ascribe(polyglot.ast.Expr, polyglot.types.Type)

Field Summary
 
Fields inherited from class polyglot.visit.ContextVisitor
context
 
Fields inherited from class polyglot.visit.ErrorHandlingVisitor
error, job, nf, ts
 
Constructor Summary
AscriptionVisitor(Job job, TypeSystem ts, NodeFactory nf)
          Default constructor.
 
Method Summary
 Expr ascribe(Expr e, Type toType)
          The ascribe() method is called for each expression and is passed the type the expression is used at rather than the type the type checker assigns to it.
 NodeVisitor enterCall(Node parent, Node n)
          Sets up the expected type information for later calls to ascribe().
 Node leaveCall(Node old, Node n, NodeVisitor v)
          Calls ascribe() with the expected type and expression as appropriate.
 AscriptionVisitor pop()
           
 Type toType()
          Returns the type that is expected of the expression that is being visited.
 
Methods inherited from class polyglot.visit.ContextVisitor
addDecls, begin, context, context, enter, enterScope, leave, superEnter
 
Methods inherited from class polyglot.visit.ErrorHandlingVisitor
catchErrors, enterCall, enterError, errorQueue, job, leaveCall, nodeFactory, typeSystem
 
Methods inherited from class polyglot.visit.HaltingVisitor
bypass, bypass, bypassChildren, copy, override, visitChildren
 
Methods inherited from class polyglot.visit.NodeVisitor
enter, finish, finish, leave, override, toString, visitEdge
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AscriptionVisitor

public AscriptionVisitor(Job job,
                         TypeSystem ts,
                         NodeFactory nf)
Default constructor. See the constructor in ErrorHandingVisitor for more details.

See Also:
ErrorHandlingVisitor.ErrorHandlingVisitor(polyglot.frontend.Job, polyglot.types.TypeSystem, polyglot.ast.NodeFactory)
Method Detail

pop

public AscriptionVisitor pop()

toType

public Type toType()
Returns the type that is expected of the expression that is being visited.


enterCall

public NodeVisitor enterCall(Node parent,
                             Node n)
                      throws SemanticException
Sets up the expected type information for later calls to ascribe(). Other than that, plays the same role as the enterCall method in ErrorHandlingVisitor.

Overrides:
enterCall in class ErrorHandlingVisitor
n - The root of the subtree to be traversed.
Returns:
The ErrorHandlingVisitor which should be used to visit the children of n.
Throws:
SemanticException
See Also:
NodeVisitor.enter(Node, Node)

ascribe

public Expr ascribe(Expr e,
                    Type toType)
             throws SemanticException
The ascribe() method is called for each expression and is passed the type the expression is used at rather than the type the type checker assigns to it. For instance, with the following code: Object o = new Integer(3); ascribe() will be called with expression new Integer(3) and type Object.

Parameters:
e - The expression that is being visited
toType - The type that the parent node is expecting.
Returns:
The new translated Expr node, or if nothing has changed, just e.
Throws:
SemanticException

leaveCall

public Node leaveCall(Node old,
                      Node n,
                      NodeVisitor v)
               throws SemanticException
Calls ascribe() with the expected type and expression as appropriate. Otherwise functionally the same as the leaveCall method in ErrorHandlingVisitor.

Overrides:
leaveCall in class ErrorHandlingVisitor
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.
Throws:
SemanticException
See Also:
NodeVisitor.leave(Node, Node, NodeVisitor)