|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.farrago.ojrex.FarragoOJRexCastImplementor.CastHelper
private class FarragoOJRexCastImplementor.CastHelper
Helps to implement the CAST operator for a specific cast node.
Field Summary | |
---|---|
private OJClass |
lhsClass
|
private Expression |
lhsExp
|
private RelDataType |
lhsType
|
private Expression |
rhsExp
|
private RelDataType |
rhsType
|
private StatementList |
stmtList
|
private String |
targetName
|
private FarragoRexToOJTranslator |
translator
|
Constructor Summary | |
---|---|
FarragoOJRexCastImplementor.CastHelper(FarragoRexToOJTranslator translator,
StatementList stmtList,
String targetName,
RelDataType lhsType,
RelDataType rhsType,
Expression lhsExp,
Expression rhsExp)
Constructs a new CastHelper |
Method Summary | |
---|---|
private void |
addStatement(Statement stmt)
Adds the statement to the statement list if it is not null. |
private void |
addStatementList(StatementList list)
Adds a list of statements according to addStatement(Statement) |
private ExpressionStatement |
assign(Expression a,
Expression b)
Creates a simple assignment statement as in a = b. |
private StatementList |
borrowStmtList(StatementList newList)
Borrows the active statement list, by temporarily setting it to a new statement list. |
private Expression |
castFromNull()
Implements a cast from NULL. |
private Expression |
castPrimitiveToNullablePrimitive()
Implements a cast from any Java primitive to a nullable Java primitive as a simple assignment. |
private Expression |
castToAssignableValue()
Casts the rhs to an AssignableValue using that interface's
standard assignment method. |
private Expression |
castToAssignableValueImpl()
|
private Expression |
castToNotNullPrimitive()
Casts the rhs to a non nullable primitive value. |
private void |
checkNotNull()
Generates code to throw an exception when a NULL value is casted to a NOT NULL type |
private void |
checkOverflow()
Checks for overflow when assigning one primitive type to another. |
private VariableDeclaration |
declareStackVar(OJClass clazz,
Variable var,
Expression init)
Makes a statement to declare a stack variable |
private void |
ensureLhs()
Creates a left hand side variable if one was not provided. |
private OJClass |
getClass(RelDataType type)
Gets the OJ class for a RelDataType |
private Expression |
getCurrentDate()
Gets an expression for the current date |
private Expression |
getDirectAssignment()
Directly assigns the right hand side to to an lhs variable and returns the lhs variable. |
private OJClass |
getLhsClass()
Gets the OJ class for the left hand side (target) type |
private FieldAccess |
getNullIndicator(Expression expr)
Creates a field access, as in expr.[nullIndicator] |
private Expression |
getValue(RelDataType type,
Expression expr)
Creates a field access, as in expr.[value] |
Expression |
implement()
Implement the cast expression. |
private Expression |
minus(OJClass clazz,
Expression expr)
Creates a unary minus, as in -expr. |
private Expression |
not(Expression a)
Creates a not expression as in !a. |
private boolean |
requiresSpecializedCast()
|
private void |
returnStmtList(StatementList oldList)
Restores the active statement list. |
private Expression |
rhsAsJava()
Gets the right hand expression as a simple Java value. |
private Expression |
rhsAsValue()
Gets the right hand expression as a valid value to be assigned to the left hand side. |
private Expression |
round(OJClass clazz,
Expression expr,
boolean isLong)
Generates code to round an expression up and cast it. |
private void |
roundAsNeeded()
Rounds right hand side, if required. |
private Expression |
roundAway()
Generates code to round an expression according to the Farrago convention. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private FarragoRexToOJTranslator translator
private StatementList stmtList
private String targetName
private RelDataType lhsType
private RelDataType rhsType
private Expression lhsExp
private Expression rhsExp
private OJClass lhsClass
Constructor Detail |
---|
public FarragoOJRexCastImplementor.CastHelper(FarragoRexToOJTranslator translator, StatementList stmtList, String targetName, RelDataType lhsType, RelDataType rhsType, Expression lhsExp, Expression rhsExp)
translator
- translator for implementing Rex as Java codestmtList
- statement list in which to insert statementstargetName
- the name of the target column or expressionlhsType
- the left hand side typerhsType
- the right hand side typelhsExp
- the left hand side expressionrhsExp
- the right hand side expressionMethod Detail |
---|
public Expression implement()
TODO: check for overflow
private void checkNotNull()
private Expression rhsAsJava()
private Expression rhsAsValue()
private Expression castFromNull()
private Expression castPrimitiveToNullablePrimitive()
[NullablePrimitiveType] lhs; lhs.[nullIndicator] = ...; if (! lhs.[nullIndicator]) { // check overflow ... // round ... lhs.[value] = ...; }
private Expression castToAssignableValue()
AssignableValue
using that interface's
standard assignment method. i.e.
[AssignableValueType] lhs; lhs.[assignMethod](rhs);or perhaps a type-specific cast:
[AssignableValueType] lhs; lhs.[castMethod](rhs, lhs.getPrecision());
Code is also generated to pad and truncate values which need special handling, such as date and time types. Plus good old null handling.
private boolean requiresSpecializedCast()
private Expression castToAssignableValueImpl()
private Expression castToNotNullPrimitive()
private Expression getDirectAssignment()
private void checkOverflow()
private void ensureLhs()
private OJClass getLhsClass()
private OJClass getClass(RelDataType type)
private void addStatement(Statement stmt)
stmt
- the statement to be addedprivate void addStatementList(StatementList list)
addStatement(Statement)
list
- list of statements to be addedprivate StatementList borrowStmtList(StatementList newList)
Example:
StatementList block = new StatementList(); StatementList oldList = borrowStmtList(block); try { // add statements to block } finally { returnStmtList(oldList); }
newList
- the new statement list
returnStmtList(StatementList)
private void returnStmtList(StatementList oldList)
oldList
- the previously active statement list.borrowStmtList(StatementList)
private ExpressionStatement assign(Expression a, Expression b)
private Expression not(Expression a)
private FieldAccess getNullIndicator(Expression expr)
private Expression getValue(RelDataType type, Expression expr)
private void roundAsNeeded()
private Expression roundAway()
in = rhs; if (value < 0) { in = -in; out = Math.round(in); out = -out; } else { out = Math.round(in); }
PRECONDITION: rhsExp must be an unwrapped (not null) Java primitive
TODO: account for overflow in both unary minus and round.
private Expression minus(OJClass clazz, Expression expr)
byte b; b = -b; // cannot cast int to byteThe unary minus operator returns an integer value. Note that unary minus potentially causes an overflow, because in most cases, |MIN_VALUE| > |MAX_VALUE| (ex. |-128| > |127| for byte)
private Expression round(OJClass clazz, Expression expr, boolean isLong)
clazz
- type to cast rounded expression asexpr
- expression to be roundedisLong
- whether the result should have long precisionprivate VariableDeclaration declareStackVar(OJClass clazz, Variable var, Expression init)
clazz
- OJ class of the variable to declarevar
- the variable to be declaredinit
- initial value for the declaration.private Expression getCurrentDate()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |