private Expression parseCount(Expression expr) { BinaryExpression binExpr = ObjectUtil.asInstance(expr, BinaryExpression.class); if (binExpr == null || binExpr.getOperation().getType() != Types.MULTIPLY) return expr; count = binExpr.getLeftExpression(); return binExpr.getRightExpression(); }
private BinaryExpression createVariableNotNullExpression(VariableExpression var) { return new BinaryExpression( new VariableExpression(var), Token.newSymbol(Types.COMPARE_NOT_EQUAL, -1, -1), new ConstantExpression(null)); }
private Token getOpWithoutEqual(Expression exp) { if (!(exp instanceof BinaryExpression)) return null; // should never happen Token op = ((BinaryExpression) exp).getOperation(); int typeWithoutEqual = TokenUtil.removeAssignment(op.getType()); return new Token(typeWithoutEqual, op.getText() /* will do */, op.getStartLine(), op.getStartColumn()); }
/** * Returns a copy of this Token. */ public Token dup() { Token token = new Token( this.type, this.text, this.startLine, this.startColumn ); token.setMeaning( this.meaning ); return token; }
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);
Object[] list = bin.getNodeMetaData(BINARY_EXP_TARGET); Token operation = bin.getOperation(); int operationType = operation.getType(); Expression rightExpression = bin.getRightExpression(); Expression leftExpression = bin.getLeftExpression(); if (bin instanceof DeclarationExpression && leftExpression instanceof VariableExpression) { ClassNode declarationType = ((VariableExpression) leftExpression).getOriginType(); if (rightExpression instanceof ConstantExpression) { ClassNode unwrapper = ClassHelper.getUnwrapper(declarationType); && WideningCategories.isDoubleCategory(unwrapper)) { ConstantExpression constant = (ConstantExpression) rightExpression; if (constant.getValue()!=null) { return optimizeConstantInitialization(bin, operation, constant, leftExpression, declarationType); Expression left = staticCompilationTransformer.transform(leftExpression); Expression right = staticCompilationTransformer.transform(rightExpression); MethodCallExpression call = new MethodCallExpression(left, "compareTo", new ArgumentListExpression(right)); call.setImplicitThis(false); call.setMethodTarget(COMPARE_TO_METHOD); call.setSourcePosition(bin); return new BinaryExpression(left, Token.newSymbol("=", operation.getStartLine(), operation.getStartColumn()), call); if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof TupleExpression && rightExpression instanceof ListExpression) {
private void transformSpreadOnLHS(BinaryExpression origin) { PropertyExpression spreadExpression = (PropertyExpression) origin.getLeftExpression(); Expression value = origin.getRightExpression(); WriterController controller = getController(); MethodVisitor mv = controller.getMethodVisitor(); VariableExpression result = new VariableExpression( this.getClass().getSimpleName()+"$spreadresult" + counter, ARRAYLIST_CLASSNODE DeclarationExpression declr = new DeclarationExpression( result, Token.newSymbol("=", spreadExpression.getLineNumber(), spreadExpression.getColumnNumber()), cce ); pexp.setImplicitThis(spreadExpression.isImplicitThis()); pexp.setSourcePosition(spreadExpression); BinaryExpression assignment = new BinaryExpression( new ExpressionStatement(add) ); stmt.visit(controller.getAcg()); result.visit(controller.getAcg());
private void rewriteDerivedParameterization(BinaryExpression parameterization, Statement enclosingStat) throws InvalidSpecCompileException { VariableExpression dataVar = createDataProcessorVariable(parameterization.getLeftExpression(), enclosingStat); ExpressionStatement exprStat = new ExpressionStatement( new DeclarationExpression( dataVar, Token.newSymbol(Types.ASSIGN, -1, -1), parameterization.getRightExpression())); exprStat.setSourcePosition(enclosingStat); dataProcessorStats.add(exprStat); }
if (trn instanceof BinaryExpression) { BinaryExpression bin = (BinaryExpression) trn; Expression leftExpression = bin.getLeftExpression(); if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof PropertyExpression) { ClassNode traitReceiver = null; PropertyExpression leftPropertyExpression = (PropertyExpression) leftExpression; if (parameters.length==2 && parameters[0].getType().equals(traitReceiver)) { ArgumentListExpression args = new ArgumentListExpression( new VariableExpression("this"), transform(exp.getRightExpression()) ); MethodCallExpression setterCall = new MethodCallExpression( new ClassExpression(helper), setterName, args ); setterCall.setMethodTarget(method); setterCall.setImplicitThis(false); return setterCall;
private Expression transformBinaryExpression(final BinaryExpression exp, final ClassNode weavedType) { Expression leftExpression = exp.getLeftExpression(); Expression rightExpression = exp.getRightExpression(); Token operation = exp.getOperation(); if (operation.getText().equals("=")) { String leftFieldName = null; if (leftExpression instanceof VariableExpression && ((VariableExpression) leftExpression).getAccessedVariable() instanceof FieldNode) { leftFieldName = ((VariableExpression) leftExpression).getAccessedVariable().getName(); } else if (leftExpression instanceof FieldExpression) { leftFieldName = ((FieldExpression) leftExpression).getFieldName(); MethodCallExpression mce = new MethodCallExpression( receiver, method, new ArgumentListExpression(super.transform(rightExpression)) ); mce.setSourcePosition(exp); mce.setImplicitThis(false); markDynamicCall(mce, staticField, isStatic); return mce;
@Override public void visitBinaryExpression(BinaryExpression expr) { // order of convert calls is important or indexes and thus recorded values get confused Expression convertedLeftExpression = Types.ofType(expr.getOperation().getType(), Types.ASSIGNMENT_OPERATOR) ? // prevent lvalue from getting turned into record(lvalue), which can no longer be assigned to convertAndRecordNa(expr.getLeftExpression()) : convert(expr.getLeftExpression()); Expression convertedRightExpression = convert(expr.getRightExpression()); Expression conversion = Types.ofType(expr.getOperation().getType(), Types.KEYWORD_INSTANCEOF) ? // morph instanceof expression to isInstance method call to be able to record rvalue new MethodCallExpression(convertedRightExpression, "isInstance", convertedLeftExpression): new BinaryExpression(convertedLeftExpression, expr.getOperation(), convertedRightExpression); conversion.setSourcePosition(expr); result = record(conversion); }
private Expression convertInOperatorToTernary(final BinaryExpression bin, final Expression rightExpression, final Expression leftExpression) { MethodCallExpression call = new MethodCallExpression( rightExpression, "isCase", leftExpression ); call.setMethodTarget((MethodNode) bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET)); call.setSourcePosition(bin); call.copyNodeMetaData(bin); TernaryExpression tExp = new TernaryExpression( new BooleanExpression( new BinaryExpression(rightExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null)) ), new BinaryExpression(leftExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null)), call ); return staticCompilationTransformer.transform(tExp); }
/** * Creates variable initialization expression in which the specified expression * is written into the specified variable name. */ public static BinaryExpression newInitializationExpression(String variable, ClassNode type, Expression rhs) { VariableExpression lhs = new VariableExpression(variable); if (type != null) { lhs.setType(type); } Token operator = Token.newPlaceholder(Types.ASSIGN); return new BinaryExpression(lhs, operator, rhs); }
/** * Creates an assignment expression in which the specified expression * is written into the specified variable name. */ public static BinaryExpression newAssignmentExpression(Variable variable, Expression rhs) { VariableExpression lhs = new VariableExpression(variable); Token operator = Token.newPlaceholder(Types.ASSIGN); return new BinaryExpression(lhs, operator, rhs); }
private static BinaryExpression tryOptimizeCharComparison(final Expression left, final Expression right, final BinaryExpression bin) { int op = bin.getOperation().getType(); if (isCompareToBoolean(op) || op == COMPARE_EQUAL || op == COMPARE_NOT_EQUAL) { Character cLeft = tryCharConstant(left); Character cRight = tryCharConstant(right); if (cLeft != null || cRight != null) { Expression oLeft = cLeft == null ? left : new ConstantExpression(cLeft, true); oLeft.setSourcePosition(left); Expression oRight = cRight == null ? right : new ConstantExpression(cRight, true); oRight.setSourcePosition(right); bin.setLeftExpression(oLeft); bin.setRightExpression(oRight); return bin; } } return null; }
throw new ASTRuntimeException(node, "\n[" + ((ConstantExpression) leftExpression).getValue() + "] is a constant expression, but it should be a variable expression"); } else if (leftExpression instanceof BinaryExpression) { int lefttype = ((BinaryExpression) leftExpression).getOperation().getType(); if (!Types.ofType(lefttype, Types.ASSIGNMENT_OPERATOR) && lefttype != Types.LEFT_SQUARE_BRACKET) { throw new ASTRuntimeException(node, "\n" + ((BinaryExpression) leftExpression).getText() + " is a binary expression, but it should be a variable expression"); throw new ASTRuntimeException(node, "\n\"" + ((MethodCallExpression) leftExpression).getText() + "\" is a method call expression, but it should be a variable expression"); } else if (leftExpression instanceof MapExpression) { throw new ASTRuntimeException(node, "\n'" + ((MapExpression) leftExpression).getText() + "' is a map expression, but it should be a variable expression"); BinaryExpression binaryExpression = new BinaryExpression(leftExpression, token, rightExpression); configureAST(binaryExpression, node); return binaryExpression;
public static SpecialMethodCall parse(MethodCallExpression methodCallExpr, @Nullable BinaryExpression binaryExpr) { boolean builtInCall = checkIsBuiltInMethodCall(methodCallExpr); boolean conditionBlock = checkIsConditionBlock(methodCallExpr); if (!(builtInCall || conditionBlock)) return null; String methodName = methodCallExpr.getMethodAsString(); Expression inferredName; Expression inferredType; if (binaryExpr != null && binaryExpr.getOperation().getType() == Types.ASSIGN && binaryExpr.getRightExpression() == methodCallExpr) { inferredName = AstUtil.getVariableName(binaryExpr); inferredType = AstUtil.getVariableType(binaryExpr); } else { binaryExpr = null; // not part of this special method call inferredName = ConstantExpression.NULL; inferredType = ConstantExpression.NULL; } ClosureExpression closureExpr = null; List<Expression> arguments = AstUtil.getArgumentList(methodCallExpr); if (!arguments.isEmpty()) { Expression lastArg = CollectionUtil.getLastElement(arguments); if (lastArg instanceof ClosureExpression) { closureExpr = (ClosureExpression) lastArg; } } return new SpecialMethodCall(methodName, inferredName, inferredType, methodCallExpr, binaryExpr, closureExpr, conditionBlock); }
private void moveVariableDeclarations(List<Statement> from, List<Statement> to) { for (Statement stat : from) { DeclarationExpression declExpr = AstUtil.getExpression(stat, DeclarationExpression.class); if (declExpr == null) continue; ((ExpressionStatement) stat).setExpression( new BinaryExpression( copyLhsVariableExpressions(declExpr), Token.newSymbol(Types.ASSIGN, -1, -1), declExpr.getRightExpression())); declExpr.setRightExpression(createDefaultValueInitializer(declExpr)); to.add(new ExpressionStatement(declExpr)); } }
private ClassNode checkForTargetType(final Expression expr, final ClassNode type) { BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression(); if (enclosingBinaryExpression instanceof DeclarationExpression && isEmptyCollection(expr) && isAssignment(enclosingBinaryExpression.getOperation().getType())) { VariableExpression target = (VariableExpression) enclosingBinaryExpression.getLeftExpression(); return adjustForTargetType(target.getType(), type); } if (currentField != null) { return adjustForTargetType(currentField.getType(), type); } if (currentProperty != null) { return adjustForTargetType(currentProperty.getType(), type); } MethodNode enclosingMethod = typeCheckingContext.getEnclosingMethod(); if (enclosingMethod != null) { return adjustForTargetType(enclosingMethod.getReturnType(), type); } return type; }