polyglot.visit
Class AscriptionVisitor
java.lang.Object
polyglot.visit.NodeVisitor
polyglot.visit.HaltingVisitor
polyglot.visit.ErrorHandlingVisitor
polyglot.visit.ContextVisitor
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)
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
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)
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 visitedtoType
- 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)