Search in sources :

Example 6 with DynamicVariable

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;
        }
    };
}
Also used : Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer)

Example 7 with DynamicVariable

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);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression)

Example 8 with DynamicVariable

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;
}
Also used : Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 9 with DynamicVariable

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);
    }
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 10 with DynamicVariable

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);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression)

Aggregations

DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)16 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)12 Expression (org.codehaus.groovy.ast.expr.Expression)10 ClassNode (org.codehaus.groovy.ast.ClassNode)9 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)9 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)9 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)8 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)8 Variable (org.codehaus.groovy.ast.Variable)7 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)7 FieldNode (org.codehaus.groovy.ast.FieldNode)5 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)5 PropertyNode (org.codehaus.groovy.ast.PropertyNode)4 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)4 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)4 ClassCodeExpressionTransformer (org.codehaus.groovy.ast.ClassCodeExpressionTransformer)3 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)3 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)3 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2