|
| 1 | +// Copyright 2021 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 | +import com.google.common.collect.ImmutableList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +@AutoValue |
| 27 | +public abstract class LambdaExpr implements Expr { |
| 28 | + @Override |
| 29 | + public TypeNode type() { |
| 30 | + // TODO(v2): Support set of FunctionalInterface parameterized on the args and return type, |
| 31 | + // which would enable assignment to an appropriate variable. |
| 32 | + return TypeNode.VOID; |
| 33 | + } |
| 34 | + |
| 35 | + public abstract ImmutableList<VariableExpr> arguments(); |
| 36 | + |
| 37 | + public abstract ReturnExpr returnExpr(); |
| 38 | + |
| 39 | + public abstract ImmutableList<Statement> body(); |
| 40 | + |
| 41 | + @Override |
| 42 | + public void accept(AstNodeVisitor visitor) { |
| 43 | + visitor.visit(this); |
| 44 | + } |
| 45 | + |
| 46 | + public static Builder builder() { |
| 47 | + return new AutoValue_LambdaExpr.Builder() |
| 48 | + .setArguments(Collections.emptyList()) |
| 49 | + .setBody(Collections.emptyList()); |
| 50 | + } |
| 51 | + |
| 52 | + @AutoValue.Builder |
| 53 | + public abstract static class Builder { |
| 54 | + public Builder setArguments(VariableExpr... arguments) { |
| 55 | + return setArguments(Arrays.asList(arguments)); |
| 56 | + } |
| 57 | + |
| 58 | + public abstract Builder setArguments(List<VariableExpr> arguments); |
| 59 | + |
| 60 | + public abstract Builder setBody(List<Statement> body); |
| 61 | + |
| 62 | + public abstract Builder setReturnExpr(ReturnExpr returnExpr); |
| 63 | + |
| 64 | + public Builder setReturnExpr(Expr expr) { |
| 65 | + return setReturnExpr(ReturnExpr.builder().setExpr(expr).build()); |
| 66 | + } |
| 67 | + |
| 68 | + public abstract LambdaExpr autoBuild(); |
| 69 | + |
| 70 | + public LambdaExpr build() { |
| 71 | + LambdaExpr lambdaExpr = autoBuild(); |
| 72 | + Preconditions.checkState( |
| 73 | + !lambdaExpr.returnExpr().expr().type().equals(TypeNode.VOID), |
| 74 | + "Lambdas cannot return void-typed expressions."); |
| 75 | + // Must be a declaration. |
| 76 | + lambdaExpr.arguments().stream() |
| 77 | + .forEach( |
| 78 | + varExpr -> |
| 79 | + Preconditions.checkState( |
| 80 | + varExpr.isDecl(), |
| 81 | + String.format( |
| 82 | + "Argument %s must be a variable declaration", |
| 83 | + varExpr.variable().identifier()))); |
| 84 | + // No modifiers allowed. |
| 85 | + lambdaExpr.arguments().stream() |
| 86 | + .forEach( |
| 87 | + varExpr -> |
| 88 | + Preconditions.checkState( |
| 89 | + varExpr.scope().equals(ScopeNode.LOCAL) |
| 90 | + && !varExpr.isStatic() |
| 91 | + && !varExpr.isFinal() |
| 92 | + && !varExpr.isVolatile(), |
| 93 | + String.format( |
| 94 | + "Argument %s must have local scope, and cannot have static, final, or" |
| 95 | + + " volatile modifiers", |
| 96 | + varExpr.variable().identifier()))); |
| 97 | + |
| 98 | + // Check that there aren't any arguments with duplicate names. |
| 99 | + List<String> allArgNames = |
| 100 | + lambdaExpr.arguments().stream() |
| 101 | + .map(v -> v.variable().identifier().name()) |
| 102 | + .collect(Collectors.toList()); |
| 103 | + Set<String> duplicateArgNames = |
| 104 | + allArgNames.stream() |
| 105 | + .filter(n -> Collections.frequency(allArgNames, n) > 1) |
| 106 | + .collect(Collectors.toSet()); |
| 107 | + Preconditions.checkState( |
| 108 | + duplicateArgNames.isEmpty(), |
| 109 | + String.format( |
| 110 | + "Lambda arguments cannot have duplicate names: %s", duplicateArgNames.toString())); |
| 111 | + |
| 112 | + return lambdaExpr; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments