ptolemy.data.expr
Class ParseTreeTypeInference

java.lang.Object
  extended by ptolemy.data.expr.AbstractParseTreeVisitor
      extended by ptolemy.data.expr.ParseTreeTypeInference
All Implemented Interfaces:
ParseTreeVisitor
Direct Known Subclasses:
GTParameter.TypeInference

public class ParseTreeTypeInference
extends AbstractParseTreeVisitor

This class visits parse trees and infers a type for each node in the parse tree. This type is stored in the parse tree.

Since:
Ptolemy II 2.1
Version:
$Id: ParseTreeTypeInference.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Steve Neuendorffer
See Also:
ASTPtRootNode
Accepted Rating:
Yellow (neuendor)
Proposed Rating:
Green (neuendor)

Field Summary
protected  Type _inferredChildType
           
protected  ParserScope _scope
           
 
Constructor Summary
ParseTreeTypeInference()
           
 
Method Summary
protected  void _assert(boolean flag, ASTPtRootNode node, java.lang.String message)
          Assert that the given boolean value, which describes the given parse tree node is true.
protected  Type _getMethodReturnType(java.lang.Class<?> clazz, java.lang.String methodName, Type[] argTypes)
          Get the return type of a method belonging to the specified class, or the type of a field belonging to it.
protected  Type _getTypeForName(java.lang.String name)
          Return the type of the identifier with the given name.
protected  Type[] _inferAllChildren(ASTPtRootNode node)
          Loop through all of the children of this node, visiting each one of them, which will cause their token value to be determined.
protected  Type _inferChild(ASTPtRootNode node, int i)
          Visit the child with the given index of the given node.
protected  boolean _isValidName(java.lang.String name)
          Test if the given identifier is valid.
protected  Type _methodCall(java.lang.String methodName, Type[] argTypes)
          Infer the type of the specified method.
protected  void _setType(ASTPtRootNode node, Type type)
           
 Type inferTypes(ASTPtRootNode node)
          Infer the type of the parse tree with the specified root node.
 Type inferTypes(ASTPtRootNode node, ParserScope scope)
          Infer the type of the parse tree with the specified root node using the specified scope to resolve the values of variables.
 void visitArrayConstructNode(ASTPtArrayConstructNode node)
          Set the type of the given node to be an ArrayType that is the least upper bound of the types of the node's children.
 void visitBitwiseNode(ASTPtBitwiseNode node)
          Set the type of the given node to be the type that is the least upper bound of the types of the node's children.
 void visitFunctionalIfNode(ASTPtFunctionalIfNode node)
          Set the type of the given node to be the least upper bound of the types of the two branches of the if.
 void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node)
          Set the type of the given node to be the return type of the function determined for the given node.
 void visitFunctionDefinitionNode(ASTPtFunctionDefinitionNode node)
          Set the type of the given node to be a function type whose argument types are determined by the children of the node.
 void visitLeafNode(ASTPtLeafNode node)
          Set the type of the given node to be the type of constant the variable refers to, if the node represents a constant, or the type of the identifier the node refers to in the current scope.
 void visitLogicalNode(ASTPtLogicalNode node)
          Set the type of the given node to be boolean.
 void visitMatrixConstructNode(ASTPtMatrixConstructNode node)
          Set the type of the given node to be an MatrixType based on the least upper bound of the types of the node's children.
 void visitMethodCallNode(ASTPtMethodCallNode node)
          Set the type of the given node to be the return type of the method determined for the given node.
 void visitPowerNode(ASTPtPowerNode node)
          Set the type of the given node to be the type of the first child of the given node.
 void visitProductNode(ASTPtProductNode node)
          Set the type of the given node to be the least upper bound type of the types of the node's children.
 void visitRecordConstructNode(ASTPtRecordConstructNode node)
          Set the type of the given node to be a record token that contains fields for each name in the record construction, where the type of each field in the record is determined by the corresponding type of the child nodes.
 void visitRelationalNode(ASTPtRelationalNode node)
          Set the type of the given node to be boolean.
 void visitShiftNode(ASTPtShiftNode node)
          Set the type of the given node to be the type of the first child of the given node.
 void visitSumNode(ASTPtSumNode node)
          Set the type of the given node to be the least upper bound type of the types of the node's children.
 void visitUnaryNode(ASTPtUnaryNode node)
          Set the type of the given node to be the type of the child of the given node.
 
Methods inherited from class ptolemy.data.expr.AbstractParseTreeVisitor
_unsupportedVisitException, _visitAllChildren, _visitChild, visitAssignmentNode, visitUnionConstructNode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_inferredChildType

protected Type _inferredChildType

_scope

protected ParserScope _scope
Constructor Detail

ParseTreeTypeInference

public ParseTreeTypeInference()
Method Detail

inferTypes

public Type inferTypes(ASTPtRootNode node)
                throws IllegalActionException
Infer the type of the parse tree with the specified root node.

Parameters:
node - The root of the parse tree.
Returns:
The result of evaluation.
Throws:
IllegalActionException - If an evaluation error occurs.

inferTypes

public Type inferTypes(ASTPtRootNode node,
                       ParserScope scope)
                throws IllegalActionException
Infer the type of the parse tree with the specified root node using the specified scope to resolve the values of variables.

Parameters:
node - The root of the parse tree.
scope - The scope for evaluation.
Returns:
The result of evaluation.
Throws:
IllegalActionException - If an error occurs during evaluation.

visitArrayConstructNode

public void visitArrayConstructNode(ASTPtArrayConstructNode node)
                             throws IllegalActionException
Set the type of the given node to be an ArrayType that is the least upper bound of the types of the node's children.

Specified by:
visitArrayConstructNode in interface ParseTreeVisitor
Overrides:
visitArrayConstructNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitBitwiseNode

public void visitBitwiseNode(ASTPtBitwiseNode node)
                      throws IllegalActionException
Set the type of the given node to be the type that is the least upper bound of the types of the node's children.

Specified by:
visitBitwiseNode in interface ParseTreeVisitor
Overrides:
visitBitwiseNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitFunctionApplicationNode

public void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node)
                                  throws IllegalActionException
Set the type of the given node to be the return type of the function determined for the given node.

Specified by:
visitFunctionApplicationNode in interface ParseTreeVisitor
Overrides:
visitFunctionApplicationNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitFunctionDefinitionNode

public void visitFunctionDefinitionNode(ASTPtFunctionDefinitionNode node)
                                 throws IllegalActionException
Set the type of the given node to be a function type whose argument types are determined by the children of the node. The return type of the function type is determined by inferring the type of function's expression in a scope that adds identifiers for each argument to the current scope.

Specified by:
visitFunctionDefinitionNode in interface ParseTreeVisitor
Overrides:
visitFunctionDefinitionNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitFunctionalIfNode

public void visitFunctionalIfNode(ASTPtFunctionalIfNode node)
                           throws IllegalActionException
Set the type of the given node to be the least upper bound of the types of the two branches of the if.

Specified by:
visitFunctionalIfNode in interface ParseTreeVisitor
Overrides:
visitFunctionalIfNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitLeafNode

public void visitLeafNode(ASTPtLeafNode node)
                   throws IllegalActionException
Set the type of the given node to be the type of constant the variable refers to, if the node represents a constant, or the type of the identifier the node refers to in the current scope.

Specified by:
visitLeafNode in interface ParseTreeVisitor
Overrides:
visitLeafNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs, or an identifier is not bound in the current scope.

visitLogicalNode

public void visitLogicalNode(ASTPtLogicalNode node)
                      throws IllegalActionException
Set the type of the given node to be boolean.

Specified by:
visitLogicalNode in interface ParseTreeVisitor
Overrides:
visitLogicalNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitMatrixConstructNode

public void visitMatrixConstructNode(ASTPtMatrixConstructNode node)
                              throws IllegalActionException
Set the type of the given node to be an MatrixType based on the least upper bound of the types of the node's children.

Specified by:
visitMatrixConstructNode in interface ParseTreeVisitor
Overrides:
visitMatrixConstructNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitMethodCallNode

public void visitMethodCallNode(ASTPtMethodCallNode node)
                         throws IllegalActionException
Set the type of the given node to be the return type of the method determined for the given node.

Specified by:
visitMethodCallNode in interface ParseTreeVisitor
Overrides:
visitMethodCallNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitPowerNode

public void visitPowerNode(ASTPtPowerNode node)
                    throws IllegalActionException
Set the type of the given node to be the type of the first child of the given node.

Specified by:
visitPowerNode in interface ParseTreeVisitor
Overrides:
visitPowerNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitProductNode

public void visitProductNode(ASTPtProductNode node)
                      throws IllegalActionException
Set the type of the given node to be the least upper bound type of the types of the node's children.

Specified by:
visitProductNode in interface ParseTreeVisitor
Overrides:
visitProductNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitRecordConstructNode

public void visitRecordConstructNode(ASTPtRecordConstructNode node)
                              throws IllegalActionException
Set the type of the given node to be a record token that contains fields for each name in the record construction, where the type of each field in the record is determined by the corresponding type of the child nodes.

Specified by:
visitRecordConstructNode in interface ParseTreeVisitor
Overrides:
visitRecordConstructNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitRelationalNode

public void visitRelationalNode(ASTPtRelationalNode node)
                         throws IllegalActionException
Set the type of the given node to be boolean.

Specified by:
visitRelationalNode in interface ParseTreeVisitor
Overrides:
visitRelationalNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitShiftNode

public void visitShiftNode(ASTPtShiftNode node)
                    throws IllegalActionException
Set the type of the given node to be the type of the first child of the given node.

Specified by:
visitShiftNode in interface ParseTreeVisitor
Overrides:
visitShiftNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitSumNode

public void visitSumNode(ASTPtSumNode node)
                  throws IllegalActionException
Set the type of the given node to be the least upper bound type of the types of the node's children.

Specified by:
visitSumNode in interface ParseTreeVisitor
Overrides:
visitSumNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

visitUnaryNode

public void visitUnaryNode(ASTPtUnaryNode node)
                    throws IllegalActionException
Set the type of the given node to be the type of the child of the given node.

Specified by:
visitUnaryNode in interface ParseTreeVisitor
Overrides:
visitUnaryNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an inference error occurs.

_assert

protected void _assert(boolean flag,
                       ASTPtRootNode node,
                       java.lang.String message)
Assert that the given boolean value, which describes the given parse tree node is true. If it is false, then throw a new InternalErrorException that describes the node that includes the given message.


_getMethodReturnType

protected Type _getMethodReturnType(java.lang.Class<?> clazz,
                                    java.lang.String methodName,
                                    Type[] argTypes)
                             throws IllegalActionException
Get the return type of a method belonging to the specified class, or the type of a field belonging to it.

Throws:
IllegalActionException

_getTypeForName

protected Type _getTypeForName(java.lang.String name)
                        throws IllegalActionException
Return the type of the identifier with the given name.

Throws:
IllegalActionException - If the identifier is undefined.

_inferAllChildren

protected Type[] _inferAllChildren(ASTPtRootNode node)
                            throws IllegalActionException
Loop through all of the children of this node, visiting each one of them, which will cause their token value to be determined.

Throws:
IllegalActionException

_inferChild

protected Type _inferChild(ASTPtRootNode node,
                           int i)
                    throws IllegalActionException
Visit the child with the given index of the given node. This is usually called while visiting the given node.

Throws:
IllegalActionException

_isValidName

protected boolean _isValidName(java.lang.String name)
                        throws IllegalActionException
Test if the given identifier is valid.

Throws:
IllegalActionException

_methodCall

protected Type _methodCall(java.lang.String methodName,
                           Type[] argTypes)
                    throws IllegalActionException
Infer the type of the specified method. The type of the object on which the method is evaluated should be the first argument.

Parameters:
methodName - The method name.
argTypes - An array of argument types.
Throws:
IllegalActionException - If an evaluation error occurs.
See Also:
ParseTreeEvaluator._methodCall(String, Type[], Object[])

_setType

protected void _setType(ASTPtRootNode node,
                        Type type)