Search in sources :

Example 61 with Parameter

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

the class AstBuilder method visitStandardLambdaParameters.

@Override
public Parameter[] visitStandardLambdaParameters(final StandardLambdaParametersContext ctx) {
    if (asBoolean(ctx.variableDeclaratorId())) {
        VariableExpression variable = this.visitVariableDeclaratorId(ctx.variableDeclaratorId());
        Parameter parameter = new Parameter(ClassHelper.dynamicType(), variable.getName());
        configureAST(parameter, variable);
        return new Parameter[] { parameter };
    }
    Parameter[] parameters = this.visitFormalParameters(ctx.formalParameters());
    return (parameters.length > 0 ? parameters : null);
}
Also used : Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 62 with Parameter

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

the class AstBuilder method visitForStmtAlt.

@Override
public ForStatement visitForStmtAlt(final ForStmtAltContext ctx) {
    Tuple2<Parameter, Expression> controlTuple = this.visitForControl(ctx.forControl());
    Statement loopBlock = this.unpackStatement((Statement) this.visit(ctx.statement()));
    return configureAST(new ForStatement(controlTuple.getV1(), controlTuple.getV2(), asBoolean(loopBlock) ? loopBlock : EmptyStatement.INSTANCE), ctx);
}
Also used : UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodReferenceExpression(org.codehaus.groovy.ast.expr.MethodReferenceExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) Parameter(org.codehaus.groovy.ast.Parameter) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

Example 63 with Parameter

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

the class AstBuilder method visitMethodDeclaration.

@Override
public MethodNode visitMethodDeclaration(final MethodDeclarationContext ctx) {
    ModifierManager modifierManager = createModifierManager(ctx);
    if (modifierManager.containsAny(VAR)) {
        throw createParsingFailedException("var cannot be used for method declarations", ctx);
    }
    String methodName = this.visitMethodName(ctx.methodName());
    ClassNode returnType = this.visitReturnType(ctx.returnType());
    Parameter[] parameters = this.visitFormalParameters(ctx.formalParameters());
    ClassNode[] exceptions = this.visitQualifiedClassNameList(ctx.qualifiedClassNameList());
    anonymousInnerClassesDefinedInMethodStack.push(new LinkedList<>());
    Statement code = this.visitMethodBody(ctx.methodBody());
    List<InnerClassNode> anonymousInnerClassList = anonymousInnerClassesDefinedInMethodStack.pop();
    MethodNode methodNode;
    // if classNode is not null, the method declaration is for class declaration
    ClassNode classNode = ctx.getNodeMetaData(CLASS_DECLARATION_CLASS_NODE);
    if (asBoolean(classNode)) {
        validateParametersOfMethodDeclaration(parameters, classNode);
        methodNode = createConstructorOrMethodNodeForClass(ctx, modifierManager, methodName, returnType, parameters, exceptions, code, classNode);
    } else {
        // script method declaration
        methodNode = createScriptMethodNode(modifierManager, methodName, returnType, parameters, exceptions, code);
    }
    anonymousInnerClassList.forEach(e -> e.setEnclosingMethod(methodNode));
    methodNode.setGenericsTypes(this.visitTypeParameters(ctx.typeParameters()));
    methodNode.setSyntheticPublic(this.isSyntheticPublic(this.isAnnotationDeclaration(classNode), classNode instanceof EnumConstantClassNode, asBoolean(ctx.returnType()), modifierManager));
    if (modifierManager.containsAny(STATIC)) {
        for (Parameter parameter : methodNode.getParameters()) {
            parameter.setInStaticContext(true);
        }
        methodNode.getVariableScope().setInStaticContext(true);
    }
    configureAST(methodNode, ctx);
    validateMethodDeclaration(ctx, methodNode, modifierManager, classNode);
    groovydocManager.handle(methodNode, ctx);
    return methodNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) Parameter(org.codehaus.groovy.ast.Parameter) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 64 with Parameter

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

the class AstBuilder method transformRecordHeaderToProperties.

private void transformRecordHeaderToProperties(ClassDeclarationContext ctx, ClassNode classNode) {
    Parameter[] parameters = this.visitFormalParameters(ctx.formalParameters());
    classNode.putNodeMetaData(RECORD_HEADER, parameters);
    final int n = parameters.length;
    for (int i = 0; i < n; i += 1) {
        Parameter parameter = parameters[i];
        FormalParameterContext parameterCtx = parameter.getNodeMetaData(PARAMETER_CONTEXT);
        ModifierManager parameterModifierManager = parameter.getNodeMetaData(PARAMETER_MODIFIER_MANAGER);
        ClassNode originType = parameter.getOriginType();
        PropertyNode propertyNode = declareProperty(parameterCtx, parameterModifierManager, originType, classNode, i, parameter, parameter.getName(), parameter.getModifiers(), parameter.getInitialExpression());
        propertyNode.getField().putNodeMetaData(IS_RECORD_GENERATED, Boolean.TRUE);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) FormalParameterContext(org.apache.groovy.parser.antlr4.GroovyParser.FormalParameterContext) ThisFormalParameterContext(org.apache.groovy.parser.antlr4.GroovyParser.ThisFormalParameterContext) Parameter(org.codehaus.groovy.ast.Parameter)

Example 65 with Parameter

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

the class TryWithResourcesASTTransformation method createCatchBlockForOuterNewTryCatchStatement.

/*
     *   catch (Throwable #t) {
     *       #primaryExc = #t;
     *       throw #t;
     *   }
     *
     */
private CatchStatement createCatchBlockForOuterNewTryCatchStatement(String primaryExcName) {
    // { ... }
    BlockStatement blockStatement = new BlockStatement();
    String tExcName = this.genTExcName();
    // #primaryExc = #t;
    ExpressionStatement primaryExcAssignStatement = new ExpressionStatement(new BinaryExpression(new VariableExpression(primaryExcName), newSymbol(Types.ASSIGN, -1, -1), new VariableExpression(tExcName)));
    astBuilder.appendStatementsToBlockStatement(blockStatement, primaryExcAssignStatement);
    // throw #t;
    ThrowStatement throwTExcStatement = new ThrowStatement(new VariableExpression(tExcName));
    astBuilder.appendStatementsToBlockStatement(blockStatement, throwTExcStatement);
    // Throwable #t
    Parameter tExcParameter = new Parameter(ClassHelper.THROWABLE_TYPE.getPlainNodeReference(), tExcName);
    return new CatchStatement(tExcParameter, blockStatement);
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Aggregations

Parameter (org.codehaus.groovy.ast.Parameter)342 ClassNode (org.codehaus.groovy.ast.ClassNode)202 MethodNode (org.codehaus.groovy.ast.MethodNode)135 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)117 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)111 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)102 Expression (org.codehaus.groovy.ast.expr.Expression)98 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)90 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)75 FieldNode (org.codehaus.groovy.ast.FieldNode)71 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)71 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)69 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)68 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)66 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)66 Statement (org.codehaus.groovy.ast.stmt.Statement)66 ArrayList (java.util.ArrayList)62 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)56 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)52 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)51