use of org.codehaus.groovy.ast.DynamicVariable in project groovy by apache.
the class MapConstructorASTTransformation method makeMapTypedArgsTransformer.
private static ClassCodeExpressionTransformer makeMapTypedArgsTransformer() {
return new ClassCodeExpressionTransformer() {
@Override
public Expression transform(Expression exp) {
if (exp instanceof ClosureExpression) {
ClosureExpression ce = (ClosureExpression) exp;
ce.getCode().visit(this);
} else if (exp instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) exp;
if (ve.getName().equals("args") && ve.getAccessedVariable() instanceof DynamicVariable) {
VariableExpression newVe = new VariableExpression(new Parameter(MAP_TYPE, "args"));
newVe.setSourcePosition(ve);
return newVe;
}
}
return exp.transformExpression(this);
}
@Override
protected SourceUnit getSourceUnit() {
return null;
}
};
}
use of org.codehaus.groovy.ast.DynamicVariable in project groovy by apache.
the class MarkupBuilderCodeTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression bin) {
Expression left = bin.getLeftExpression();
Expression right = bin.getRightExpression();
boolean assignment = bin.getOperation().getType() == Types.ASSIGN;
if (assignment && left instanceof VariableExpression) {
VariableExpression var = (VariableExpression) left;
if (var.getAccessedVariable() instanceof DynamicVariable) {
String varName = var.getName();
if (!"modelTypes".equals(varName)) {
MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
callGetModel.setImplicitThis(true);
callGetModel.setSourcePosition(left);
MethodCallExpression mce = new MethodCallExpression(callGetModel, "put", new ArgumentListExpression(new ConstantExpression(varName), right));
mce.setSourcePosition(left);
mce.setImplicitThis(false);
return transform(mce);
}
}
}
if (assignment && left instanceof VariableExpression && right instanceof ClosureExpression) {
VariableExpression var = (VariableExpression) left;
if ("modelTypes".equals(var.getName())) {
// template declaring its expected types from model directly
// modelTypes = {
// List<String> items
// ...
// }
Map<String, ClassNode> modelTypes = extractModelTypesFromClosureExpression((ClosureExpression) right);
Expression result = new EmptyExpression();
result.setSourcePosition(bin);
classNode.putNodeMetaData(MarkupTemplateEngine.MODELTYPES_ASTKEY, modelTypes);
return result;
}
}
return super.transform(bin);
}
use of org.codehaus.groovy.ast.DynamicVariable in project groovy-core by groovy.
the class BinaryExpressionMultiTypeDispatcher method doAssignmentToLocalVariable.
private boolean doAssignmentToLocalVariable(String method, BinaryExpression binExp) {
Expression left = binExp.getLeftExpression();
if (left instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) left;
Variable v = ve.getAccessedVariable();
if (v instanceof DynamicVariable)
return false;
if (v instanceof PropertyExpression)
return false;
/* field and declaration we don't return false */
} else {
return false;
}
evaluateBinaryExpression(method, binExp);
getController().getOperandStack().dup();
getController().getCompileStack().pushLHS(true);
binExp.getLeftExpression().visit(getController().getAcg());
getController().getCompileStack().popLHS();
return true;
}
use of org.codehaus.groovy.ast.DynamicVariable in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method visitVariableExpression.
@Override
public void visitVariableExpression(VariableExpression vexp) {
super.visitVariableExpression(vexp);
if (storeTypeForThis(vexp))
return;
if (storeTypeForSuper(vexp))
return;
if (vexp.getAccessedVariable() instanceof PropertyNode) {
// overloaded setters, the type of the property node is arbitrary!
if (tryVariableExpressionAsProperty(vexp, vexp.getName())) {
BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
if (enclosingBinaryExpression != null) {
Expression leftExpression = enclosingBinaryExpression.getLeftExpression();
Expression rightExpression = enclosingBinaryExpression.getRightExpression();
SetterInfo setterInfo = removeSetterInfo(leftExpression);
if (setterInfo != null) {
if (!ensureValidSetter(vexp, leftExpression, rightExpression, setterInfo)) {
return;
}
}
}
}
}
TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
if (enclosingClosure != null) {
String name = vexp.getName();
if (name.equals("owner") || name.equals("thisObject")) {
storeType(vexp, typeCheckingContext.getEnclosingClassNode());
return;
} else if ("delegate".equals(name)) {
DelegationMetadata md = getDelegationMetadata(enclosingClosure.getClosureExpression());
ClassNode type = typeCheckingContext.getEnclosingClassNode();
if (md != null)
type = md.getType();
storeType(vexp, type);
return;
}
}
if (!(vexp.getAccessedVariable() instanceof DynamicVariable))
return;
// a dynamic variable is either an undeclared variable
// or a member of a class used in a 'with'
DynamicVariable dyn = (DynamicVariable) vexp.getAccessedVariable();
// first, we must check the 'with' context
String dynName = dyn.getName();
if (tryVariableExpressionAsProperty(vexp, dynName))
return;
if (!extension.handleUnresolvedVariableExpression(vexp)) {
addStaticTypeError("The variable [" + vexp.getName() + "] is undeclared.", vexp);
}
}
use of org.codehaus.groovy.ast.DynamicVariable in project groovy-core by groovy.
the class MarkupBuilderCodeTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression bin) {
Expression left = bin.getLeftExpression();
Expression right = bin.getRightExpression();
boolean assignment = bin.getOperation().getType() == Types.ASSIGN;
if (assignment && left instanceof VariableExpression) {
VariableExpression var = (VariableExpression) left;
if (var.getAccessedVariable() instanceof DynamicVariable) {
String varName = var.getName();
if (!"modelTypes".equals(varName)) {
MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
callGetModel.setImplicitThis(true);
callGetModel.setSourcePosition(left);
MethodCallExpression mce = new MethodCallExpression(callGetModel, "put", new ArgumentListExpression(new ConstantExpression(varName), right));
mce.setSourcePosition(left);
mce.setImplicitThis(false);
return transform(mce);
}
}
}
if (assignment && left instanceof VariableExpression && right instanceof ClosureExpression) {
VariableExpression var = (VariableExpression) left;
if ("modelTypes".equals(var.getName())) {
// template declaring its expected types from model directly
// modelTypes = {
// List<String> items
// ...
// }
Map<String, ClassNode> modelTypes = extractModelTypesFromClosureExpression((ClosureExpression) right);
Expression result = new EmptyExpression();
result.setSourcePosition(bin);
classNode.putNodeMetaData(MarkupTemplateEngine.MODELTYPES_ASTKEY, modelTypes);
return result;
}
}
return super.transform(bin);
}
Aggregations