Search in sources :

Example 41 with Variable

use of org.codehaus.groovy.ast.Variable in project groovy by apache.

the class StaticTypeCheckingVisitor method isSecondPassNeededForControlStructure.

protected boolean isSecondPassNeededForControlStructure(final Map<VariableExpression, ClassNode> varOrigType, final Map<VariableExpression, List<ClassNode>> oldTracker) {
    Map<VariableExpression, ClassNode> assignedVars = popAssignmentTracking(oldTracker);
    for (Map.Entry<VariableExpression, ClassNode> entry : assignedVars.entrySet()) {
        Variable key = findTargetVariable(entry.getKey());
        if (key instanceof VariableExpression) {
            ClassNode origType = varOrigType.get(key);
            ClassNode newType = entry.getValue();
            if (varOrigType.containsKey(key) && (origType == null || !newType.equals(origType))) {
                return true;
            }
        }
    }
    return false;
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ListHashMap(org.codehaus.groovy.util.ListHashMap) HashMap(java.util.HashMap)

Example 42 with Variable

use of org.codehaus.groovy.ast.Variable in project groovy by apache.

the class StaticTypeCheckingVisitor method saveVariableExpressionMetadata.

protected void saveVariableExpressionMetadata(final Set<VariableExpression> closureSharedExpressions, final Map<VariableExpression, ListHashMap> typesBeforeVisit) {
    for (VariableExpression ve : closureSharedExpressions) {
        // GROOVY-6921: We must force a call to getType in order to update closure shared variable which types are
        // inferred thanks to closure parameter type inference
        getType(ve);
        ListHashMap<StaticTypesMarker, Object> metadata = new ListHashMap<StaticTypesMarker, Object>();
        for (StaticTypesMarker marker : StaticTypesMarker.values()) {
            Object value = ve.getNodeMetaData(marker);
            if (value != null) {
                metadata.put(marker, value);
            }
        }
        typesBeforeVisit.put(ve, metadata);
        Variable accessedVariable = ve.getAccessedVariable();
        if (accessedVariable != ve && accessedVariable instanceof VariableExpression) {
            saveVariableExpressionMetadata(Collections.singleton((VariableExpression) accessedVariable), typesBeforeVisit);
        }
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ListHashMap(org.codehaus.groovy.util.ListHashMap)

Example 43 with Variable

use of org.codehaus.groovy.ast.Variable in project gradle by gradle.

the class GradleResolveVisitor method transformVariableExpression.

protected Expression transformVariableExpression(VariableExpression ve) {
    visitAnnotations(ve);
    Variable v = ve.getAccessedVariable();
    if (!(v instanceof DynamicVariable) && !checkingVariableTypeInDeclaration) {
        /*
         *  GROOVY-4009: when a normal variable is simply being used, there is no need to try to
         *  resolve its type. Variable type resolve should proceed only if the variable is being declared.
         */
        return ve;
    }
    if (v instanceof DynamicVariable) {
        String name = ve.getName();
        ClassNode t = ClassHelper.make(name);
        // asking isResolved here allows to check if a primitive
        // type name like "int" was used to make t. In such a case
        // we have nothing left to do.
        boolean isClass = t.isResolved();
        if (!isClass) {
            // compiler skip the resolving at several places in this class.
            if (Character.isLowerCase(name.charAt(0))) {
                t = new LowerCaseClass(name);
            }
            isClass = resolve(t);
            if (!isClass) {
                isClass = resolveToNestedOfCurrent(t);
            }
        }
        if (isClass) {
            // for each parentscope too
            for (VariableScope scope = currentScope; scope != null && !scope.isRoot(); scope = scope.getParent()) {
                if (scope.isRoot()) {
                    break;
                }
                if (scope.removeReferencedClassVariable(ve.getName()) == null) {
                    break;
                }
            }
            ClassExpression ce = new ClassExpression(t);
            ce.setSourcePosition(ve);
            return ce;
        }
    }
    resolveOrFail(ve.getType(), ve);
    ClassNode origin = ve.getOriginType();
    if (origin != ve.getType()) {
        resolveOrFail(origin, ve);
    }
    return ve;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableScope(org.codehaus.groovy.ast.VariableScope)

Aggregations

Variable (org.codehaus.groovy.ast.Variable)43 DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)23 ClassNode (org.codehaus.groovy.ast.ClassNode)21 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)16 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)15 FieldNode (org.codehaus.groovy.ast.FieldNode)14 MethodNode (org.codehaus.groovy.ast.MethodNode)13 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)13 Parameter (org.codehaus.groovy.ast.Parameter)12 Expression (org.codehaus.groovy.ast.expr.Expression)9 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)7 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)7 LinkedList (java.util.LinkedList)6 ASTNode (org.codehaus.groovy.ast.ASTNode)6 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)6 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)6 VariableScope (org.codehaus.groovy.ast.VariableScope)5 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)5 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)5 ClosureSignatureHint (groovy.transform.stc.ClosureSignatureHint)4