use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class FieldASTTransformation method visitConstructorCallExpression.
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression cce) {
if (!insideScriptBody || !cce.isUsingAnonymousInnerClass())
return;
ConstructorCallExpression old = currentAIC;
currentAIC = cce;
Expression newArgs = transform(cce.getArguments());
if (cce.getArguments() instanceof TupleExpression && newArgs instanceof TupleExpression) {
List<Expression> argList = ((TupleExpression) cce.getArguments()).getExpressions();
argList.clear();
argList.addAll(((TupleExpression) newArgs).getExpressions());
}
currentAIC = old;
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class BinaryExpressionTransformer method transformBinaryExpression.
public Expression transformBinaryExpression(final BinaryExpression bin) {
Expression leftExpression = bin.getLeftExpression();
Expression rightExpression = bin.getRightExpression();
if (bin instanceof DeclarationExpression && leftExpression instanceof VariableExpression && rightExpression instanceof ConstantExpression && !((ConstantExpression) rightExpression).isNullExpression()) {
ClassNode declarationType = ((VariableExpression) leftExpression).getOriginType();
// for "char x = 'c'" change 'c' from String to char
if (declarationType.equals(ClassHelper.char_TYPE)) {
Character c = tryCharConstant(rightExpression);
if (c != null)
return transformCharacterInitialization(bin, c);
}
// for "int|long|short|byte|char|float|double|BigDecimal|BigInteger x = n" change n's type
if (!declarationType.equals(rightExpression.getType()) && WideningCategories.isDoubleCategory(ClassHelper.getUnwrapper(declarationType)) && ClassHelper.getWrapper(rightExpression.getType()).isDerivedFrom(ClassHelper.Number_TYPE)) {
return transformNumericalInitialization(bin, (Number) ((ConstantExpression) rightExpression).getValue(), declarationType);
}
}
boolean equal = false;
switch(bin.getOperation().getType()) {
case Types.ASSIGN:
// GROOVY-10029
optimizeArrayCollectionAssignment(bin);
Expression expr = transformAssignmentToSetterCall(bin);
if (expr != null)
return expr;
if (leftExpression instanceof TupleExpression && rightExpression instanceof ListExpression) {
return transformMultipleAssignment(bin);
}
break;
case Types.KEYWORD_IN:
// fallthrough
equal = true;
case Types.COMPARE_NOT_IN:
return transformInOperation(bin, equal);
case Types.COMPARE_EQUAL:
case Types.COMPARE_IDENTICAL:
// fallthrough
equal = true;
case Types.COMPARE_NOT_EQUAL:
case Types.COMPARE_NOT_IDENTICAL:
expr = transformEqualityComparison(bin, equal);
if (expr != null)
return expr;
else
break;
case Types.COMPARE_TO:
expr = transformRelationComparison(bin);
if (expr != null)
return expr;
}
Object[] array = bin.getNodeMetaData(StaticCompilationMetadataKeys.BINARY_EXP_TARGET);
if (array != null) {
return transformToTargetMethodCall(bin, (MethodNode) array[0], (String) array[1]);
}
return staticCompilationTransformer.superTransform(bin);
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class MacroGroovyMethods method getClosureArgument.
protected static ClosureExpression getClosureArgument(SourceUnit source, MethodCallExpression call) {
TupleExpression tupleArguments = getMacroArguments(source, call);
int size = tupleArguments == null ? -1 : tupleArguments.getExpressions().size();
if (size < 1) {
source.addError(new SyntaxException("Call arguments should have at least one argument" + '\n', tupleArguments));
return null;
}
Expression result = tupleArguments.getExpression(size - 1);
if (!(result instanceof ClosureExpression)) {
source.addError(new SyntaxException("Last call argument should be a closure" + '\n', result));
return null;
}
return (ClosureExpression) result;
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class AstBuilder method visitPathElement.
@Override
public Expression visitPathElement(final PathElementContext ctx) {
Expression baseExpr = ctx.getNodeMetaData(PATH_EXPRESSION_BASE_EXPR);
Objects.requireNonNull(baseExpr, "baseExpr is required!");
if (asBoolean(ctx.namePart())) {
Expression namePartExpr = this.visitNamePart(ctx.namePart());
GenericsType[] genericsTypes = this.visitNonWildcardTypeArguments(ctx.nonWildcardTypeArguments());
if (asBoolean(ctx.DOT())) {
boolean isSafeChain = this.isTrue(baseExpr, PATH_EXPRESSION_BASE_EXPR_SAFE_CHAIN);
return this.createDotExpression(ctx, baseExpr, namePartExpr, genericsTypes, isSafeChain);
} else if (asBoolean(ctx.SAFE_DOT())) {
return this.createDotExpression(ctx, baseExpr, namePartExpr, genericsTypes, true);
} else if (asBoolean(ctx.SAFE_CHAIN_DOT())) {
// e.g. obj??.a OR obj??.@a
Expression expression = createDotExpression(ctx, baseExpr, namePartExpr, genericsTypes, true);
expression.putNodeMetaData(PATH_EXPRESSION_BASE_EXPR_SAFE_CHAIN, true);
return expression;
} else if (asBoolean(ctx.METHOD_POINTER())) {
// e.g. obj.&m
return configureAST(new MethodPointerExpression(baseExpr, namePartExpr), ctx);
} else if (asBoolean(ctx.METHOD_REFERENCE())) {
// e.g. obj::m
return configureAST(new MethodReferenceExpression(baseExpr, namePartExpr), ctx);
} else if (asBoolean(ctx.SPREAD_DOT())) {
if (asBoolean(ctx.AT())) {
// e.g. obj*.@a
AttributeExpression attributeExpression = new AttributeExpression(baseExpr, namePartExpr, true);
attributeExpression.setSpreadSafe(true);
return configureAST(attributeExpression, ctx);
} else {
// e.g. obj*.p
PropertyExpression propertyExpression = new PropertyExpression(baseExpr, namePartExpr, true);
propertyExpression.putNodeMetaData(PATH_EXPRESSION_BASE_EXPR_GENERICS_TYPES, genericsTypes);
propertyExpression.setSpreadSafe(true);
return configureAST(propertyExpression, ctx);
}
}
} else if (asBoolean(ctx.creator())) {
CreatorContext creatorContext = ctx.creator();
creatorContext.putNodeMetaData(ENCLOSING_INSTANCE_EXPRESSION, baseExpr);
return configureAST(this.visitCreator(creatorContext), ctx);
} else if (asBoolean(ctx.indexPropertyArgs())) {
// e.g. list[1, 3, 5]
Tuple2<Token, Expression> tuple = this.visitIndexPropertyArgs(ctx.indexPropertyArgs());
boolean isSafeChain = this.isTrue(baseExpr, PATH_EXPRESSION_BASE_EXPR_SAFE_CHAIN);
return configureAST(new BinaryExpression(baseExpr, createGroovyToken(tuple.getV1()), tuple.getV2(), isSafeChain || asBoolean(ctx.indexPropertyArgs().SAFE_INDEX())), ctx);
} else if (asBoolean(ctx.namedPropertyArgs())) {
// this is a special way to signify a cast, e.g. Person[name: 'Daniel.Sun', location: 'Shanghai']
List<MapEntryExpression> mapEntryExpressionList = this.visitNamedPropertyArgs(ctx.namedPropertyArgs());
Expression right;
Expression firstKeyExpression;
int mapEntryExpressionListSize = mapEntryExpressionList.size();
if (mapEntryExpressionListSize == 0) {
// expecting list of MapEntryExpressions later so use SpreadMap to smuggle empty MapExpression to later stages
right = configureAST(new SpreadMapExpression(configureAST(new MapExpression(), ctx.namedPropertyArgs())), ctx.namedPropertyArgs());
} else if (mapEntryExpressionListSize == 1 && (firstKeyExpression = mapEntryExpressionList.get(0).getKeyExpression()) instanceof SpreadMapExpression) {
right = firstKeyExpression;
} else {
ListExpression listExpression = configureAST(new ListExpression(mapEntryExpressionList.stream().map(e -> {
if (e.getKeyExpression() instanceof SpreadMapExpression) {
return e.getKeyExpression();
}
return e;
}).collect(Collectors.toList())), ctx.namedPropertyArgs());
listExpression.setWrapped(true);
right = listExpression;
}
NamedPropertyArgsContext namedPropertyArgsContext = ctx.namedPropertyArgs();
Token token = (namedPropertyArgsContext.LBRACK() == null ? namedPropertyArgsContext.SAFE_INDEX() : namedPropertyArgsContext.LBRACK()).getSymbol();
return configureAST(new BinaryExpression(baseExpr, createGroovyToken(token), right), ctx);
} else if (asBoolean(ctx.arguments())) {
Expression argumentsExpr = this.visitArguments(ctx.arguments());
configureAST(argumentsExpr, ctx);
if (isInsideParentheses(baseExpr)) {
// e.g. (obj.x)(), (obj.@x)()
return configureAST(createCallMethodCallExpression(baseExpr, argumentsExpr), ctx);
}
if (baseExpr instanceof AttributeExpression) {
// e.g. obj.@a(1, 2)
AttributeExpression attributeExpression = (AttributeExpression) baseExpr;
// whether attributeExpression is spread safe or not, we must reset it as false
attributeExpression.setSpreadSafe(false);
return configureAST(createCallMethodCallExpression(attributeExpression, argumentsExpr, true), ctx);
}
if (baseExpr instanceof PropertyExpression) {
// e.g. obj.a(1, 2)
MethodCallExpression methodCallExpression = this.createMethodCallExpression((PropertyExpression) baseExpr, argumentsExpr);
return configureAST(methodCallExpression, ctx);
}
if (baseExpr instanceof VariableExpression) {
// void and primitive type AST node must be an instance of VariableExpression
String baseExprText = baseExpr.getText();
if (VOID_STR.equals(baseExprText)) {
// e.g. void()
return configureAST(this.createCallMethodCallExpression(this.createConstantExpression(baseExpr), argumentsExpr), ctx);
} else if (isPrimitiveType(baseExprText)) {
// e.g. int(), long(), float(), etc.
throw this.createParsingFailedException("Primitive type literal: " + baseExprText + " cannot be used as a method name", ctx);
}
}
if (// e.g. m()
baseExpr instanceof VariableExpression || // e.g. "$m"()
baseExpr instanceof GStringExpression || (baseExpr instanceof ConstantExpression && this.isTrue(baseExpr, IS_STRING))) {
// e.g. "m"()
String baseExprText = baseExpr.getText();
if (THIS_STR.equals(baseExprText) || SUPER_STR.equals(baseExprText)) {
// @MapConstructor(pre={ super(args?.first, args?.last); args = args ?: [:] }, post = { first = first?.toUpperCase() })
if (visitingClosureCount > 0) {
return configureAST(new MethodCallExpression(baseExpr, baseExprText, argumentsExpr), ctx);
}
return configureAST(new ConstructorCallExpression(SUPER_STR.equals(baseExprText) ? ClassNode.SUPER : ClassNode.THIS, argumentsExpr), ctx);
}
MethodCallExpression methodCallExpression = this.createMethodCallExpression(baseExpr, argumentsExpr);
return configureAST(methodCallExpression, ctx);
}
// e.g. 1(), 1.1(), ((int) 1 / 2)(1, 2), {a, b -> a + b }(1, 2), m()()
return configureAST(this.createCallMethodCallExpression(baseExpr, argumentsExpr), ctx);
} else if (asBoolean(ctx.closureOrLambdaExpression())) {
ClosureExpression closureExpression = this.visitClosureOrLambdaExpression(ctx.closureOrLambdaExpression());
if (baseExpr instanceof MethodCallExpression) {
MethodCallExpression methodCallExpression = (MethodCallExpression) baseExpr;
Expression argumentsExpression = methodCallExpression.getArguments();
if (argumentsExpression instanceof ArgumentListExpression) {
// normal arguments, e.g. 1, 2
ArgumentListExpression argumentListExpression = (ArgumentListExpression) argumentsExpression;
argumentListExpression.getExpressions().add(closureExpression);
return configureAST(methodCallExpression, ctx);
}
if (argumentsExpression instanceof TupleExpression) {
// named arguments, e.g. x: 1, y: 2
TupleExpression tupleExpression = (TupleExpression) argumentsExpression;
NamedArgumentListExpression namedArgumentListExpression = (NamedArgumentListExpression) tupleExpression.getExpression(0);
if (asBoolean(tupleExpression.getExpressions())) {
methodCallExpression.setArguments(configureAST(new ArgumentListExpression(configureAST(new MapExpression(namedArgumentListExpression.getMapEntryExpressions()), namedArgumentListExpression), closureExpression), tupleExpression));
} else {
// the branch should never reach, because named arguments must not be empty
methodCallExpression.setArguments(configureAST(new ArgumentListExpression(closureExpression), tupleExpression));
}
return configureAST(methodCallExpression, ctx);
}
}
if (baseExpr instanceof PropertyExpression) {
// e.g. obj.m { }
MethodCallExpression methodCallExpression = this.createMethodCallExpression((PropertyExpression) baseExpr, configureAST(new ArgumentListExpression(closureExpression), closureExpression));
return configureAST(methodCallExpression, ctx);
}
if (// e.g. m { }
baseExpr instanceof VariableExpression || // e.g. "$m" { }
baseExpr instanceof GStringExpression || (baseExpr instanceof ConstantExpression && this.isTrue(baseExpr, IS_STRING))) {
// e.g. "m" { }
MethodCallExpression methodCallExpression = this.createMethodCallExpression(baseExpr, configureAST(new ArgumentListExpression(closureExpression), closureExpression));
return configureAST(methodCallExpression, ctx);
}
// e.g. 1 { }, 1.1 { }, (1 / 2) { }, m() { }, { -> ... } { }
MethodCallExpression methodCallExpression = this.createCallMethodCallExpression(baseExpr, configureAST(new ArgumentListExpression(closureExpression), closureExpression));
return configureAST(methodCallExpression, ctx);
}
throw createParsingFailedException("Unsupported path element: " + ctx.getText(), ctx);
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class AstBuilder method createEnumConstantInitExpression.
private Expression createEnumConstantInitExpression(final ArgumentsContext ctx, final InnerClassNode anonymousInnerClassNode) {
if (!asBoolean(ctx) && !asBoolean(anonymousInnerClassNode)) {
return null;
}
TupleExpression argumentListExpression = (TupleExpression) this.visitArguments(ctx);
List<Expression> expressions = argumentListExpression.getExpressions();
if (expressions.size() == 1) {
Expression expression = expressions.get(0);
if (expression instanceof NamedArgumentListExpression) {
// e.g. SOME_ENUM_CONSTANT(a: "1", b: "2")
List<MapEntryExpression> mapEntryExpressionList = ((NamedArgumentListExpression) expression).getMapEntryExpressions();
ListExpression listExpression = new ListExpression(mapEntryExpressionList.stream().map(e -> (Expression) e).collect(Collectors.toList()));
if (asBoolean(anonymousInnerClassNode)) {
listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
}
if (mapEntryExpressionList.size() > 1) {
listExpression.setWrapped(true);
}
return configureAST(listExpression, ctx);
}
if (!asBoolean(anonymousInnerClassNode)) {
if (expression instanceof ListExpression) {
ListExpression listExpression = new ListExpression();
listExpression.addExpression(expression);
return configureAST(listExpression, ctx);
}
return expression;
}
ListExpression listExpression = new ListExpression();
if (expression instanceof ListExpression) {
((ListExpression) expression).getExpressions().forEach(listExpression::addExpression);
} else {
listExpression.addExpression(expression);
}
listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
return configureAST(listExpression, ctx);
}
ListExpression listExpression = new ListExpression(expressions);
if (asBoolean(anonymousInnerClassNode)) {
listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
}
if (asBoolean(ctx)) {
listExpression.setWrapped(true);
}
return asBoolean(ctx) ? configureAST(listExpression, ctx) : configureAST(listExpression, anonymousInnerClassNode);
}
Aggregations