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 ReturnExpr implements Expr {
expr()22   public abstract Expr expr();
23 
24   @Override
type()25   public TypeNode type() {
26     return TypeNode.VOID;
27   }
28 
29   @Override
accept(AstNodeVisitor visitor)30   public void accept(AstNodeVisitor visitor) {
31     visitor.visit(this);
32   }
33 
withExpr(Expr expr)34   public static ReturnExpr withExpr(Expr expr) {
35     return builder().setExpr(expr).build();
36   }
37 
38   // Private.
builder()39   static Builder builder() {
40     return new AutoValue_ReturnExpr.Builder();
41   }
42 
43   @AutoValue.Builder
44   abstract static class Builder {
setExpr(Expr expr)45     public abstract Builder setExpr(Expr expr);
46 
47     // Private accessors.
expr()48     abstract Expr expr();
49 
autoBuild()50     abstract ReturnExpr autoBuild();
51 
build()52     public ReturnExpr build() {
53       Preconditions.checkState(
54           !(expr() instanceof ReturnExpr), "ReturnExpr can only return non-ReturnExpr expressions");
55       return autoBuild();
56     }
57   }
58 }
59