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 AssignmentExpr implements Expr {
variableExpr()22   public abstract VariableExpr variableExpr();
23 
valueExpr()24   public abstract Expr valueExpr();
25 
26   @Override
type()27   public TypeNode type() {
28     return TypeNode.VOID;
29   }
30 
31   @Override
accept(AstNodeVisitor visitor)32   public void accept(AstNodeVisitor visitor) {
33     visitor.visit(this);
34   }
35 
builder()36   public static Builder builder() {
37     return new AutoValue_AssignmentExpr.Builder();
38   }
39 
40   @AutoValue.Builder
41   public abstract static class Builder {
setVariableExpr(VariableExpr variableExpr)42     public abstract Builder setVariableExpr(VariableExpr variableExpr);
43 
setValueExpr(Expr valueExpr)44     public abstract Builder setValueExpr(Expr valueExpr);
45 
autoBuild()46     abstract AssignmentExpr autoBuild();
47 
build()48     public AssignmentExpr build() {
49       AssignmentExpr assignmentExpr = autoBuild();
50       TypeNode lhsType = assignmentExpr.variableExpr().variable().type();
51       TypeNode rhsType = assignmentExpr.valueExpr().type();
52       if (lhsType.isPrimitiveType() || rhsType.isPrimitiveType()) {
53         if (rhsType == TypeNode.NULL || lhsType == TypeNode.NULL) {
54           throw new TypeMismatchException(
55               String.format(
56                   "Null cannot be assigned to the primitive type %s", lhsType.toString()));
57         }
58         if (!lhsType.equals(rhsType)) {
59           throw new TypeMismatchException(
60               String.format(
61                   "LHS type %s must match RHS type %s", lhsType.toString(), rhsType.toString()));
62         }
63       } else {
64         if (rhsType != TypeNode.NULL && !lhsType.isSupertypeOrEquals(rhsType)) {
65           throw new TypeMismatchException(
66               String.format(
67                   "LHS type %s of variable %s must be a supertype of the RHS type %s",
68                   lhsType.reference().name(),
69                   assignmentExpr.variableExpr().variable().identifier(),
70                   rhsType.reference().name()));
71         }
72       }
73 
74       if (!assignmentExpr.variableExpr().isDecl()) {
75         Preconditions.checkState(
76             !assignmentExpr.variableExpr().isFinal(),
77             String.format(
78                 "Cannot assign a value to final variable '%s'.",
79                 assignmentExpr.variableExpr().variable().name()));
80       }
81 
82       return assignmentExpr;
83     }
84   }
85 }
86