Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.engine.ast;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;

@AutoValue
public abstract class AssignmentOperationExpr implements OperationExpr {
public abstract VariableExpr variableExpr();

public abstract Expr valueExpr();

public abstract OperatorKind operatorKind();

@Override
public TypeNode type() {
return variableExpr().type();
}

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
}

public static AssignmentOperationExpr xorAssignmentWithExprs(
VariableExpr variableExpr, Expr valueExpr) {
return builder()
.setVariableExpr(variableExpr)
.setValueExpr(valueExpr)
.setOperatorKind(OperatorKind.ASSIGNMENT_XOR)
.build();
}

public static AssignmentOperationExpr multiplyAssignmentWithExprs(
VariableExpr variableExpr, Expr valueExpr) {
return builder()
.setVariableExpr(variableExpr)
.setValueExpr(valueExpr)
.setOperatorKind(OperatorKind.ASSIGNMENT_MULTIPLY)
.build();
}

private static Builder builder() {
return new AutoValue_AssignmentOperationExpr.Builder();
}

@AutoValue.Builder
abstract static class Builder {
// Private setter.
abstract Builder setVariableExpr(VariableExpr variableExpr);

// Private setter.
abstract Builder setValueExpr(Expr valueExpr);

// Private setter.
abstract Builder setOperatorKind(OperatorKind operator);

abstract AssignmentOperationExpr autoBuild();

private AssignmentOperationExpr build() {
AssignmentOperationExpr assignmentOperationExpr = autoBuild();
TypeNode lhsType = assignmentOperationExpr.variableExpr().variable().type();
TypeNode rhsType = assignmentOperationExpr.valueExpr().type();
OperatorKind operator = assignmentOperationExpr.operatorKind();

// Check if the variable exprs have been declared, if yes, throw error.
Preconditions.checkState(
!assignmentOperationExpr.variableExpr().isDecl(),
String.format(
"Variable `%s` should not be declaration in the variable expression.",
assignmentOperationExpr.variableExpr().variable().name()));

// errorMsg is type checking error message for operators.
final String errorMsg =
String.format(
"Assignment operator %s can not be applied to %s, %s.",
operator, lhsType.toString(), rhsType.toString());

// Check type for multiply and assignment operator (*=).
if (operator.equals(OperatorKind.ASSIGNMENT_MULTIPLY)) {
Preconditions.checkState(isValidMultiplyAssignmentType(lhsType, rhsType), errorMsg);
}

// Check type for XOR and assignment operator (^=).
// TODO(summerji): Complete the type-checking for ^= and unit test.
if (operator.equals(OperatorKind.ASSIGNMENT_XOR)) {
Preconditions.checkState(isValidXORAssignmentType(lhsType, rhsType), errorMsg);
}
return assignmentOperationExpr;
}

// isValidMultiplyAssignmentType validates the types for LHS variable expr and RHS expr.
// *= can be only applied on Primitive numeric type.
private boolean isValidMultiplyAssignmentType(TypeNode variableType, TypeNode valueType) {
// LHS is numeric type, RHS should be any numeric type or any numeric boxed type.
if (TypeNode.isNumericType(variableType) && !TypeNode.isBoxedType(variableType)) {
return TypeNode.isNumericType(valueType);
}
// LHS is integer boxed type, RHS should be any numeric type except long, float, double.
if (variableType.equals(TypeNode.INT_OBJECT)) {
return TypeNode.isNumericType(valueType)
&& !(valueType.equals(TypeNode.LONG) || TypeNode.isFloatingPointType(valueType));
}
// LHS is long boxed type, RHS should be any numeric type except float, double.
if (variableType.equals(TypeNode.LONG_OBJECT)) {
return TypeNode.isNumericType(valueType) && !TypeNode.isFloatingPointType(valueType);
}
// LHS is integer boxed type, RHS should be any numeric type except double.
if (variableType.equals(TypeNode.FLOAT_OBJECT)) {
return TypeNode.isNumericType(valueType) && !valueType.equals(TypeNode.DOUBLE);
}
// LHS is integer boxed type, RHS should be any numeric type or any numeric boxed type.
if (variableType.equals(TypeNode.DOUBLE_OBJECT)) {
return TypeNode.isNumericType(valueType);
}
// *= operator does not support boxed Short, Character, Byte, null, reference, void type.
return false;
}

// TODO(summerji): Complete the type-checking for ^= and unit test.
private boolean isValidXORAssignmentType(TypeNode variableType, TypeNode valueType) {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public interface AstNodeVisitor {

public void visit(LogicalOperationExpr logicalOperationExpr);

public void visit(AssignmentOperationExpr assignmentOperationExpr);

/** =============================== COMMENT =============================== */
public void visit(LineComment lineComment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public enum OperatorKind {
ARITHMETIC_ADDITION,
LOGICAL_AND,
LOGICAL_OR,
ASSIGNMENT_XOR,
ASSIGNMENT_MULTIPLY,
RELATIONAL_EQUAL_TO,
RELATIONAL_NOT_EQUAL_TO,
RELATIONAL_LESS_THAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public static boolean isNumericType(TypeNode type) {
|| type.equals(TypeNode.BYTE);
}

public static boolean isFloatingPointType(TypeNode type) {
return type.equals(TypeNode.DOUBLE) || type.equals(TypeNode.FLOAT);
}

public static boolean isBoxedType(TypeNode type) {
return isReferenceType(type) && BOXED_TYPE_MAP.containsValue(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.AssignmentOperationExpr;
import com.google.api.generator.engine.ast.AstNodeVisitor;
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.BlockStatement;
Expand Down Expand Up @@ -240,6 +241,12 @@ public void visit(LogicalOperationExpr logicalOperationExpr) {
logicalOperationExpr.rhsExpr().accept(this);
}

@Override
public void visit(AssignmentOperationExpr assignmentOperationExpr) {
assignmentOperationExpr.variableExpr().accept(this);
assignmentOperationExpr.valueExpr().accept(this);
}

/** =============================== STATEMENTS =============================== */
@Override
public void visit(ExprStatement exprStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.AssignmentOperationExpr;
import com.google.api.generator.engine.ast.AstNodeVisitor;
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.BlockStatement;
Expand Down Expand Up @@ -115,6 +116,8 @@ public class JavaWriterVisitor implements AstNodeVisitor {
private static final String OPERATOR_LOGICAL_NOT = "!";
private static final String OPERATOR_LOGICAL_AND = "&&";
private static final String OPERATOR_LOGICAL_OR = "||";
private static final String OPERATOR_XOR = "^=";
private static final String OPERATOR_MULTIPLE_AND_ASSIGNMENT = "*=";

private final StringBuffer buffer = new StringBuffer();
private final ImportWriterVisitor importWriterVisitor = new ImportWriterVisitor();
Expand Down Expand Up @@ -430,6 +433,15 @@ public void visit(LogicalOperationExpr logicalOperationExpr) {
logicalOperationExpr.rhsExpr().accept(this);
}

@Override
public void visit(AssignmentOperationExpr assignmentOperationExpr) {
assignmentOperationExpr.variableExpr().accept(this);
space();
operator(assignmentOperationExpr.operatorKind());
space();
assignmentOperationExpr.valueExpr().accept(this);
}

/** =============================== STATEMENTS =============================== */
@Override
public void visit(ExprStatement exprStatement) {
Expand Down Expand Up @@ -913,6 +925,15 @@ private void semicolon() {

private void operator(OperatorKind kind) {
switch (kind) {
case ARITHMETIC_ADDITION:
buffer.append(OPERATOR_ADDITION);
break;
case ASSIGNMENT_XOR:
buffer.append(OPERATOR_XOR);
break;
case ASSIGNMENT_MULTIPLY:
buffer.append(OPERATOR_MULTIPLE_AND_ASSIGNMENT);
break;
case RELATIONAL_EQUAL_TO:
buffer.append(OPERATOR_EQUAL_TO);
break;
Expand All @@ -928,9 +949,6 @@ private void operator(OperatorKind kind) {
case UNARY_LOGICAL_NOT:
buffer.append(OPERATOR_LOGICAL_NOT);
break;
case ARITHMETIC_ADDITION:
buffer.append(OPERATOR_ADDITION);
break;
case LOGICAL_AND:
buffer.append(OPERATOR_LOGICAL_AND);
break;
Expand Down
Loading