Package ptolemy.data.expr
Class ParseTreeTypeInference
- java.lang.Object
-
- ptolemy.data.expr.AbstractParseTreeVisitor
-
- ptolemy.data.expr.ParseTreeTypeInference
-
- All Implemented Interfaces:
ParseTreeVisitor
- Direct Known Subclasses:
ExpressionConceptFunctionParseTreeTypeInference
,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$
- Author:
- Steve Neuendorffer
- See Also:
ASTPtRootNode
- Pt.AcceptedRating:
- Yellow (neuendor)
- Pt.ProposedRating:
- Green (neuendor)
-
-
Field Summary
Fields Modifier and Type Field Description protected Type
_inferredChildType
protected ParserScope
_scope
-
Constructor Summary
Constructors Constructor Description ParseTreeTypeInference()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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
-
-
-
-
Field Detail
-
_inferredChildType
protected Type _inferredChildType
-
_scope
protected ParserScope _scope
-
-
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 interfaceParseTreeVisitor
- Overrides:
visitArrayConstructNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitBitwiseNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitFunctionApplicationNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitFunctionDefinitionNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitFunctionalIfNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitLeafNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitLogicalNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitMatrixConstructNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitMethodCallNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitPowerNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitProductNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitRecordConstructNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitRelationalNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitShiftNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitSumNode
in classAbstractParseTreeVisitor
- 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 interfaceParseTreeVisitor
- Overrides:
visitUnaryNode
in classAbstractParseTreeVisitor
- 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.- Parameters:
flag
- If false, then throw an InternalErrorExceptionnode
- The nodemessage
- The message included in the exception
-
_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)
-
-