use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.
the class CastExpressionOptimizer method transformCastExpression.
public Expression transformCastExpression(final CastExpression cast) {
if (cast.isCoerce()) {
Expression expression = cast.getExpression();
ClassNode exprInferredType = transformer.getTypeChooser().resolveType(expression, transformer.getClassNode());
ClassNode castType = cast.getType();
if (castType.isArray() && expression instanceof ListExpression) {
ArrayExpression arrayExpression = new ArrayExpression(castType.getComponentType(), ((ListExpression) expression).getExpressions());
arrayExpression.setSourcePosition(cast);
return transformer.transform(arrayExpression);
}
if (isOptimizable(exprInferredType, castType)) {
// coerce is not needed
CastExpression trn = new CastExpression(castType, transformer.transform(expression));
trn.setSourcePosition(cast);
trn.copyNodeMetaData(cast);
return trn;
}
} else if (ClassHelper.char_TYPE.equals(cast.getType())) {
Expression expression = cast.getExpression();
if (expression instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) expression;
if (ClassHelper.STRING_TYPE.equals(ce.getType())) {
String val = (String) ce.getValue();
if (val != null && val.length() == 1) {
ConstantExpression result = new ConstantExpression(val.charAt(0), true);
result.setSourcePosition(cast);
return result;
}
}
}
}
return transformer.superTransform(cast);
}
use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.
the class AnnotationConstantsVisitor method transformConstantExpression.
private static Expression transformConstantExpression(Expression val, ClassNode returnType) {
ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
if (val instanceof ConstantExpression) {
Expression result = revertType(val, returnWrapperType);
if (result != null) {
return result;
}
return val;
}
if (val instanceof CastExpression) {
CastExpression castExp = (CastExpression) val;
Expression castee = castExp.getExpression();
if (castee instanceof ConstantExpression) {
if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
return castee;
}
Expression result = revertType(castee, returnWrapperType);
if (result != null) {
return result;
}
return castee;
}
}
return val;
}
use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.
the class OperandStack method pushDynamicName.
public void pushDynamicName(Expression name) {
if (name instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) name;
Object value = ce.getValue();
if (value instanceof String) {
pushConstant(ce);
return;
}
}
new CastExpression(ClassHelper.STRING_TYPE, name).visit(controller.getAcg());
}
use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.
the class InvokeDynamicWriter method makeIndyCall.
private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean implicitThis, boolean safe, String methodName, Expression arguments) {
OperandStack operandStack = controller.getOperandStack();
String sig = prepareIndyCall(receiver, implicitThis);
// load arguments
int numberOfArguments = 1;
ArgumentListExpression ae = makeArgumentList(arguments);
boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
if (containsSpreadExpression) {
controller.getAcg().despreadList(ae.getExpressions(), true);
sig += getTypeDescription(Object[].class);
} else {
for (Expression arg : ae.getExpressions()) {
arg.visit(controller.getAcg());
if (arg instanceof CastExpression) {
operandStack.box();
controller.getAcg().loadWrapper(arg);
sig += getTypeDescription(Wrapper.class);
} else {
sig += getTypeDescription(operandStack.getTopOperand());
}
numberOfArguments++;
}
}
sig += ")Ljava/lang/Object;";
String callSiteName = METHOD.getCallSiteName();
if (adapter == null)
callSiteName = INIT.getCallSiteName();
int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
}
use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.
the class Verifier method addDefaultParameters.
protected void addDefaultParameters(DefaultArgsAction action, MethodNode method) {
Parameter[] parameters = method.getParameters();
int counter = 0;
List paramValues = new ArrayList();
int size = parameters.length;
for (int i = size - 1; i >= 0; i--) {
Parameter parameter = parameters[i];
if (parameter != null && parameter.hasInitialExpression()) {
paramValues.add(i);
paramValues.add(new CastExpression(parameter.getType(), parameter.getInitialExpression()));
counter++;
}
}
for (int j = 1; j <= counter; j++) {
Parameter[] newParams = new Parameter[parameters.length - j];
ArgumentListExpression arguments = new ArgumentListExpression();
int index = 0;
int k = 1;
for (Parameter parameter : parameters) {
if (parameter == null) {
throw new GroovyBugError("Parameter should not be null for method " + methodNode.getName());
} else {
if (k > counter - j && parameter.hasInitialExpression()) {
arguments.addExpression(new CastExpression(parameter.getType(), parameter.getInitialExpression()));
k++;
} else if (parameter.hasInitialExpression()) {
newParams[index++] = parameter;
arguments.addExpression(new CastExpression(parameter.getType(), new VariableExpression(parameter.getName())));
k++;
} else {
newParams[index++] = parameter;
arguments.addExpression(new CastExpression(parameter.getType(), new VariableExpression(parameter.getName())));
}
}
}
action.call(arguments, newParams, method);
}
for (Parameter parameter : parameters) {
// remove default expression and store it as node metadata
parameter.putNodeMetaData(Verifier.INITIAL_EXPRESSION, parameter.getInitialExpression());
parameter.setInitialExpression(null);
}
}
Aggregations