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 ExprStatement implements Statement { expression()22 public abstract Expr expression(); 23 24 @Override accept(AstNodeVisitor visitor)25 public void accept(AstNodeVisitor visitor) { 26 visitor.visit(this); 27 } 28 withExpr(Expr expr)29 public static ExprStatement withExpr(Expr expr) { 30 return builder().setExpression(expr).build(); 31 } 32 builder()33 static Builder builder() { 34 return new AutoValue_ExprStatement.Builder(); 35 } 36 37 @AutoValue.Builder 38 public abstract static class Builder { setExpression(Expr expr)39 public abstract Builder setExpression(Expr expr); 40 autoBuild()41 abstract ExprStatement autoBuild(); 42 build()43 public ExprStatement build() { 44 ExprStatement exprStatement = autoBuild(); 45 Expr expr = exprStatement.expression(); 46 if (expr instanceof VariableExpr) { 47 VariableExpr variableExpr = (VariableExpr) expr; 48 Preconditions.checkState( 49 variableExpr.isDecl(), "Expression variable statements must be declarations"); 50 } else { 51 Preconditions.checkState( 52 (expr instanceof MethodInvocationExpr) 53 || (expr instanceof ReferenceConstructorExpr) 54 || (expr instanceof AssignmentExpr) 55 || (expr instanceof AssignmentOperationExpr) 56 || (expr instanceof ThrowExpr) 57 || (expr instanceof ReturnExpr) 58 || (expr instanceof UnaryOperationExpr 59 && ((UnaryOperationExpr) expr).isPostfixIncrement()), 60 "Expression statements must be either a method invocation, assignment, throw, " 61 + "this/super constructor, return, or unary post-fix operation expression"); 62 } 63 return exprStatement; 64 } 65 } 66 } 67