Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[javanaut][operationExpr][1/2]feat: Add Assignment operation expr with multiply and assignment operator #308

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ JAVA_SRCS = [
TEST_SRCS = [
"//src/test/java/com/google/api/generator/engine:engine_files",
"//src/test/java/com/google/api/generator/gapic:gapic_files",
"//src/test/java/com/google/api/generator/test/framework:framework_files",
]

# ============= Proto wrappers =================
Expand Down
1 change: 1 addition & 0 deletions dependencies.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ maven.com_google_code_findbugs_jsr305=com.google.code.findbugs:jsr305:3.0.0
maven.com_google_auto_value_auto_value=com.google.auto.value:auto-value:1.7.2
maven.com_google_auto_value_auto_value_annotations=com.google.auto.value:auto-value-annotations:1.7.2
maven.com_google_protobuf_protobuf_java=com.google.protobuf:protobuf-java:3.12.2
maven.io_github_java_diff_utils=io.github.java-diff-utils:java-diff-utils:4.0
maven.javax_annotation_api=javax.annotation:javax.annotation-api:1.3.2
maven.javax_validation_javax_validation_api=javax.validation:validation-api:2.0.1.Final

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// 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() instanceof VariableExpr
? ((VariableExpr) assignmentOperationExpr.valueExpr()).variable().type()
: assignmentOperationExpr.valueExpr().type();
OperatorKind operator = assignmentOperationExpr.operatorKind();

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

// TYPE_CHECK_ERROR_MSG is type checking error message for operators.
final String TYPE_CHECK_ERROR_MSG =
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), TYPE_CHECK_ERROR_MSG);
}

// 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), TYPE_CHECK_ERROR_MSG);
}
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 @@ -55,8 +55,12 @@ public interface AstNodeVisitor {

public void visit(UnaryOperationExpr unaryOperationExpr);

public void visit(RelationalOperationExpr relationalOperationExpr);

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,
UNARY_LOGICAL_NOT,
Expand Down
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 RelationalOperationExpr implements OperationExpr {
public abstract Expr lhsExpr();

public abstract Expr rhsExpr();

public abstract OperatorKind operatorKind();

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

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

// Convenience wrapper.
public static RelationalOperationExpr equalToWithExprs(Expr lhsExpr, Expr rhsExpr) {
return builder()
.setLhsExpr(lhsExpr)
.setRhsExpr(rhsExpr)
.setOperatorKind(OperatorKind.RELATIONAL_EQUAL_TO)
.build();
}

// Convenience wrapper.
public static RelationalOperationExpr notEqualToWithExprs(Expr lhsExpr, Expr rhsExpr) {
return builder()
.setLhsExpr(lhsExpr)
.setRhsExpr(rhsExpr)
.setOperatorKind(OperatorKind.RELATIONAL_NOT_EQUAL_TO)
.build();
}

// TODO(summerji): Add convenience wrapper lessThanWithExprs
// public static RelationalOperationExpr lessThanWithExprs(Expr lhsExpr, Expr rhsExpr) {
// return builder()
// .setLhsExpr(lhsExpr)
// .setRhsExpr(rhsExpr)
// .setOperatorKind(OperatorKind.RELATIONAL_LESS_THAN)
// .build();
// }

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

@AutoValue.Builder
abstract static class Builder {

// Private setter.
abstract Builder setLhsExpr(Expr expr);

// Private setter.
abstract Builder setRhsExpr(Expr expr);

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

abstract RelationalOperationExpr autoBuild();

private RelationalOperationExpr build() {
RelationalOperationExpr relationalOperationExpr = autoBuild();
TypeNode lhsExprType = relationalOperationExpr.lhsExpr().type();
TypeNode rhsExprType = relationalOperationExpr.rhsExpr().type();
OperatorKind operator = relationalOperationExpr.operatorKind();
final String errorMsg =
String.format(
"Relational operator %s can not be applied to %s, %s.",
operator, lhsExprType.toString(), rhsExprType.toString());

if (operator.equals(OperatorKind.RELATIONAL_EQUAL_TO)
|| operator.equals(OperatorKind.RELATIONAL_NOT_EQUAL_TO)) {
Preconditions.checkState(isValidEqualityType(lhsExprType, rhsExprType), errorMsg);
}

return relationalOperationExpr;
}

// isValidEqualityType checks expressions' type for equality operator (==) and non-equality
// operator (!=).
private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) {
// If the expression's types are matched, return true
if (lhsType.equals(rhsType)) {
return true;
}

// If the expressions' type are array, the types should be array and matched, or either is
// null type;
if (lhsType.isArray() || rhsType.isArray()) {
return lhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.NULL);
}

// If lhs expression type is numeric type (char, byte, short, int, long, double), the rhs
// expression type should be any numeric type or any numeric boxed type
if (TypeNode.isNumericType(lhsType) && TypeNode.isNumericType(rhsType)) {
return true;
}

// If lhs expression type is new Object or null, the rhs type should be a reference type or
// null or boxed type;
if (lhsType.equals(TypeNode.OBJECT) || lhsType.equals(TypeNode.NULL)) {
return TypeNode.isReferenceType(rhsType)
|| rhsType.equals(TypeNode.OBJECT)
|| rhsType.equals(TypeNode.NULL);
}

// If lhs expression type is Boxed type or a referenced type, rhs should be null or object,
// other cases have been covered in previous conditions.
if (TypeNode.isBoxedType(lhsType) || TypeNode.isReferenceType(lhsType)) {
return rhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.OBJECT);
}

return false;
}
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/google/api/generator/engine/ast/TypeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,16 @@ public static boolean isNumericType(TypeNode type) {
|| type.equals(TypeNode.DOUBLE)
|| type.equals(TypeNode.SHORT)
|| type.equals(TypeNode.FLOAT)
|| type.equals(TypeNode.CHAR);
|| type.equals(TypeNode.CHAR)
|| 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);
}

public boolean isPrimitiveType() {
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 All @@ -40,6 +41,7 @@
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.ReferenceConstructorExpr;
import com.google.api.generator.engine.ast.RelationalOperationExpr;
import com.google.api.generator.engine.ast.ReturnExpr;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
Expand Down Expand Up @@ -227,12 +229,24 @@ public void visit(UnaryOperationExpr unaryOperationExpr) {
unaryOperationExpr.expr().accept(this);
}

@Override
public void visit(RelationalOperationExpr relationalOperationExpr) {
relationalOperationExpr.lhsExpr().accept(this);
relationalOperationExpr.rhsExpr().accept(this);
}

@Override
public void visit(LogicalOperationExpr logicalOperationExpr) {
logicalOperationExpr.lhsExpr().accept(this);
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
Loading