Search in sources :

Example 41 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.

the class ImmutableASTTransformation method createConstructorStatementMapSpecial.

private Statement createConstructorStatementMapSpecial(FieldNode fNode) {
    final Expression fieldExpr = varX(fNode);
    final ClassNode fieldType = fieldExpr.getType();
    final Expression initExpr = fNode.getInitialValueExpression();
    final Statement assignInit;
    if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
        assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
    } else {
        assignInit = assignS(fieldExpr, cloneCollectionExpr(initExpr, fieldType));
    }
    Expression namedArgs = findArg(fNode.getName());
    Expression baseArgs = varX("args");
    return ifElseS(equalsNullX(baseArgs), assignInit, ifElseS(equalsNullX(namedArgs), ifElseS(isTrueX(callX(baseArgs, "containsKey", constX(fNode.getName()))), assignS(fieldExpr, namedArgs), assignS(fieldExpr, cloneCollectionExpr(baseArgs, fieldType))), ifElseS(isOneX(callX(baseArgs, "size")), assignS(fieldExpr, cloneCollectionExpr(namedArgs, fieldType)), assignS(fieldExpr, cloneCollectionExpr(baseArgs, fieldType)))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 42 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.

the class JavaAwareResolveVisitor method visitConstructor.

@Override
public void visitConstructor(ConstructorNode node) {
    super.visitConstructor(node);
    Statement code = node.getCode();
    Expression cce = getConstructorCall(code);
    if (cce == null)
        return;
    cce.visit(this);
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 43 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy-core by groovy.

the class LineCheckVisitor method visitConstructorOrMethod.

protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
    visitAnnotations(node);
    analyseMethodHead(node);
    Statement code = node.getCode();
    visitClassCodeContainer(code);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement)

Example 44 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class StaticTypeCheckingVisitor method visitClosureExpression.

@Override
public void visitClosureExpression(final ClosureExpression expression) {
    boolean oldStaticContext = typeCheckingContext.isInStaticContext;
    typeCheckingContext.isInStaticContext = false;
    // collect every variable expression used in the loop body
    final Map<VariableExpression, ClassNode> varOrigType = new HashMap<VariableExpression, ClassNode>();
    Statement code = expression.getCode();
    code.visit(new VariableExpressionTypeMemoizer(varOrigType));
    Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
    // first, collect closure shared variables and reinitialize types
    SharedVariableCollector collector = new SharedVariableCollector(getSourceUnit());
    collector.visitClosureExpression(expression);
    Set<VariableExpression> closureSharedExpressions = collector.getClosureSharedExpressions();
    Map<VariableExpression, ListHashMap> typesBeforeVisit = null;
    if (!closureSharedExpressions.isEmpty()) {
        typesBeforeVisit = new HashMap<VariableExpression, ListHashMap>();
        saveVariableExpressionMetadata(closureSharedExpressions, typesBeforeVisit);
    }
    // perform visit
    typeCheckingContext.pushEnclosingClosureExpression(expression);
    DelegationMetadata dmd = getDelegationMetadata(expression);
    if (dmd == null) {
        typeCheckingContext.delegationMetadata = new DelegationMetadata(typeCheckingContext.getEnclosingClassNode(), Closure.OWNER_FIRST, typeCheckingContext.delegationMetadata);
    } else {
        typeCheckingContext.delegationMetadata = new DelegationMetadata(dmd.getType(), dmd.getStrategy(), typeCheckingContext.delegationMetadata);
    }
    super.visitClosureExpression(expression);
    typeCheckingContext.delegationMetadata = typeCheckingContext.delegationMetadata.getParent();
    MethodNode node = new MethodNode("dummy", 0, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code);
    returnAdder.visitMethod(node);
    TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
    if (!enclosingClosure.getReturnTypes().isEmpty()) {
        ClassNode returnType = lowestUpperBound(enclosingClosure.getReturnTypes());
        storeInferredReturnType(expression, returnType);
        ClassNode inferredType = wrapClosureType(returnType);
        storeType(enclosingClosure.getClosureExpression(), inferredType);
    }
    typeCheckingContext.popEnclosingClosure();
    boolean typeChanged = isSecondPassNeededForControlStructure(varOrigType, oldTracker);
    if (typeChanged)
        visitClosureExpression(expression);
    // restore original metadata
    restoreVariableExpressionMetadata(typesBeforeVisit);
    typeCheckingContext.isInStaticContext = oldStaticContext;
    Parameter[] parameters = expression.getParameters();
    if (parameters != null) {
        for (Parameter parameter : parameters) {
            typeCheckingContext.controlStructureVariables.remove(parameter);
        }
    }
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) LinkedHashMap(java.util.LinkedHashMap) ListHashMap(org.codehaus.groovy.util.ListHashMap) HashMap(java.util.HashMap) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ListHashMap(org.codehaus.groovy.util.ListHashMap) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 45 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class StaticTypeCheckingVisitor method visitIfElse.

@Override
public void visitIfElse(final IfStatement ifElse) {
    Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
    try {
        // create a new temporary element in the if-then-else type info
        typeCheckingContext.pushTemporaryTypeInfo();
        visitStatement(ifElse);
        ifElse.getBooleanExpression().visit(this);
        ifElse.getIfBlock().visit(this);
        // pop if-then-else temporary type info
        typeCheckingContext.popTemporaryTypeInfo();
        // GROOVY-6099: restore assignment info as before the if branch
        restoreTypeBeforeConditional();
        Statement elseBlock = ifElse.getElseBlock();
        if (elseBlock instanceof EmptyStatement) {
            // dispatching to EmptyStatement will not call back visitor,
            // must call our visitEmptyStatement explicitly
            visitEmptyStatement((EmptyStatement) elseBlock);
        } else {
            elseBlock.visit(this);
        }
    } finally {
        popAssignmentTracking(oldTracker);
    }
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

Statement (org.codehaus.groovy.ast.stmt.Statement)205 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)167 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)121 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)90 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)79 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)71 Expression (org.codehaus.groovy.ast.expr.Expression)63 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)57 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)55 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)54 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)53 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)53 ClassNode (org.codehaus.groovy.ast.ClassNode)50 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)50 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)40 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)39 WhileStatement (org.codehaus.groovy.ast.stmt.WhileStatement)39 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)38 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)38 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)38