|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.api.generator.engine.ast; |
| 16 | + |
| 17 | +import com.google.auto.value.AutoValue; |
| 18 | +import com.google.common.base.Preconditions; |
| 19 | + |
| 20 | +@AutoValue |
| 21 | +public abstract class AssignmentOperationExpr implements OperationExpr { |
| 22 | + public abstract VariableExpr variableExpr(); |
| 23 | + |
| 24 | + public abstract Expr valueExpr(); |
| 25 | + |
| 26 | + public abstract OperatorKind operatorKind(); |
| 27 | + |
| 28 | + @Override |
| 29 | + public TypeNode type() { |
| 30 | + return variableExpr().type(); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void accept(AstNodeVisitor visitor) { |
| 35 | + visitor.visit(this); |
| 36 | + } |
| 37 | + |
| 38 | + public static AssignmentOperationExpr xorAssignmentWithExprs( |
| 39 | + VariableExpr variableExpr, Expr valueExpr) { |
| 40 | + return builder() |
| 41 | + .setVariableExpr(variableExpr) |
| 42 | + .setValueExpr(valueExpr) |
| 43 | + .setOperatorKind(OperatorKind.ASSIGNMENT_XOR) |
| 44 | + .build(); |
| 45 | + } |
| 46 | + |
| 47 | + public static AssignmentOperationExpr multiplyAssignmentWithExprs( |
| 48 | + VariableExpr variableExpr, Expr valueExpr) { |
| 49 | + return builder() |
| 50 | + .setVariableExpr(variableExpr) |
| 51 | + .setValueExpr(valueExpr) |
| 52 | + .setOperatorKind(OperatorKind.ASSIGNMENT_MULTIPLY) |
| 53 | + .build(); |
| 54 | + } |
| 55 | + |
| 56 | + private static Builder builder() { |
| 57 | + return new AutoValue_AssignmentOperationExpr.Builder(); |
| 58 | + } |
| 59 | + |
| 60 | + @AutoValue.Builder |
| 61 | + abstract static class Builder { |
| 62 | + // Private setter. |
| 63 | + abstract Builder setVariableExpr(VariableExpr variableExpr); |
| 64 | + |
| 65 | + // Private setter. |
| 66 | + abstract Builder setValueExpr(Expr valueExpr); |
| 67 | + |
| 68 | + // Private setter. |
| 69 | + abstract Builder setOperatorKind(OperatorKind operator); |
| 70 | + |
| 71 | + abstract AssignmentOperationExpr autoBuild(); |
| 72 | + |
| 73 | + private AssignmentOperationExpr build() { |
| 74 | + AssignmentOperationExpr assignmentOperationExpr = autoBuild(); |
| 75 | + TypeNode lhsType = assignmentOperationExpr.variableExpr().variable().type(); |
| 76 | + TypeNode rhsType = assignmentOperationExpr.valueExpr().type(); |
| 77 | + OperatorKind operator = assignmentOperationExpr.operatorKind(); |
| 78 | + |
| 79 | + // Check if the variable exprs have been declared, if yes, throw error. |
| 80 | + Preconditions.checkState( |
| 81 | + !assignmentOperationExpr.variableExpr().isDecl(), |
| 82 | + String.format( |
| 83 | + "Variable `%s` should not be declaration in the variable expression.", |
| 84 | + assignmentOperationExpr.variableExpr().variable().name())); |
| 85 | + |
| 86 | + // errorMsg is type checking error message for operators. |
| 87 | + final String errorMsg = |
| 88 | + String.format( |
| 89 | + "Assignment operator %s can not be applied to %s, %s.", |
| 90 | + operator, lhsType.toString(), rhsType.toString()); |
| 91 | + |
| 92 | + // Check type for multiply and assignment operator (*=). |
| 93 | + if (operator.equals(OperatorKind.ASSIGNMENT_MULTIPLY)) { |
| 94 | + Preconditions.checkState(isValidMultiplyAssignmentType(lhsType, rhsType), errorMsg); |
| 95 | + } |
| 96 | + |
| 97 | + // Check type for XOR and assignment operator (^=). |
| 98 | + // TODO(summerji): Complete the type-checking for ^= and unit test. |
| 99 | + if (operator.equals(OperatorKind.ASSIGNMENT_XOR)) { |
| 100 | + Preconditions.checkState(isValidXORAssignmentType(lhsType, rhsType), errorMsg); |
| 101 | + } |
| 102 | + return assignmentOperationExpr; |
| 103 | + } |
| 104 | + |
| 105 | + // isValidMultiplyAssignmentType validates the types for LHS variable expr and RHS expr. |
| 106 | + // *= can be only applied on Primitive numeric type. |
| 107 | + private boolean isValidMultiplyAssignmentType(TypeNode variableType, TypeNode valueType) { |
| 108 | + // LHS is numeric type, RHS should be any numeric type or any numeric boxed type. |
| 109 | + if (TypeNode.isNumericType(variableType) && !TypeNode.isBoxedType(variableType)) { |
| 110 | + return TypeNode.isNumericType(valueType); |
| 111 | + } |
| 112 | + // LHS is integer boxed type, RHS should be any numeric type except long, float, double. |
| 113 | + if (variableType.equals(TypeNode.INT)) { |
| 114 | + return TypeNode.isNumericType(valueType) |
| 115 | + && !(valueType.equals(TypeNode.LONG) || TypeNode.isFloatingPointType(valueType)); |
| 116 | + } |
| 117 | + // LHS is long boxed type, RHS should be any numeric type except float, double. |
| 118 | + if (variableType.equals(TypeNode.LONG)) { |
| 119 | + return TypeNode.isNumericType(valueType) && !TypeNode.isFloatingPointType(valueType); |
| 120 | + } |
| 121 | + // LHS is integer boxed type, RHS should be any numeric type except double. |
| 122 | + if (variableType.equals(TypeNode.FLOAT)) { |
| 123 | + return TypeNode.isNumericType(valueType) && !valueType.equals(TypeNode.DOUBLE); |
| 124 | + } |
| 125 | + // LHS is integer boxed type, RHS should be any numeric type or any numeric boxed type. |
| 126 | + if (variableType.equals(TypeNode.DOUBLE)) { |
| 127 | + return TypeNode.isNumericType(valueType); |
| 128 | + } |
| 129 | + // *= operator does not support boxed Short, Character, Byte, null, reference, void type. |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + // TODO(summerji): Complete the type-checking for ^= and unit test. |
| 134 | + private boolean isValidXORAssignmentType(TypeNode variableType, TypeNode valueType) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments