Skip to content

Commit 6caae0c

Browse files
l46kokcopybara-github
authored andcommitted
Change AstMutator to augment macro source using mutable expr
PiperOrigin-RevId: 624336738
1 parent 91b20d5 commit 6caae0c

24 files changed

Lines changed: 255 additions & 119 deletions

common/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ java_library(
3939
exports = ["//common/src/main/java/dev/cel/common:error_codes"],
4040
)
4141

42+
java_library(
43+
name = "mutable_ast",
44+
exports = ["//common/src/main/java/dev/cel/common:mutable_ast"],
45+
)
46+
47+
java_library(
48+
name = "mutable_source",
49+
exports = ["//common/src/main/java/dev/cel/common:mutable_source"],
50+
)
51+
4252
java_library(
4353
name = "runtime_exception",
4454
visibility = ["//visibility:public"],

common/ast/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ java_library(
3434
)
3535

3636
java_library(
37-
name = "mutable_ast",
38-
exports = ["//common/src/main/java/dev/cel/common/ast:mutable_ast"],
37+
name = "mutable_expr",
38+
exports = ["//common/src/main/java/dev/cel/common/ast:mutable_expr"],
3939
)

common/src/main/java/dev/cel/common/BUILD.bazel

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,31 @@ java_library(
134134
"//common/annotations",
135135
],
136136
)
137+
138+
java_library(
139+
name = "mutable_ast",
140+
srcs = ["CelMutableAst.java"],
141+
tags = [
142+
],
143+
deps = [
144+
":mutable_source",
145+
"//common",
146+
"//common/ast",
147+
"//common/ast:mutable_expr",
148+
"//common/types:type_providers",
149+
],
150+
)
151+
152+
java_library(
153+
name = "mutable_source",
154+
srcs = ["CelMutableSource.java"],
155+
tags = [
156+
],
157+
deps = [
158+
":common",
159+
"//:auto_value",
160+
"//common/ast:mutable_expr",
161+
"@maven//:com_google_errorprone_error_prone_annotations",
162+
"@maven//:com_google_guava_guava",
163+
],
164+
)

common/src/main/java/dev/cel/common/ast/CelMutableAst.java renamed to common/src/main/java/dev/cel/common/CelMutableAst.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package dev.cel.common.ast;
15+
package dev.cel.common;
1616

17-
import dev.cel.common.CelAbstractSyntaxTree;
18-
import dev.cel.common.CelSource;
17+
import dev.cel.common.ast.CelMutableExpr;
18+
import dev.cel.common.ast.CelMutableExprConverter;
19+
import dev.cel.common.ast.CelReference;
1920
import dev.cel.common.types.CelType;
2021
import java.util.HashMap;
2122
import java.util.Map;
@@ -30,7 +31,7 @@
3031
*/
3132
public final class CelMutableAst {
3233
private final CelMutableExpr mutatedExpr;
33-
private final CelSource.Builder source;
34+
private final CelMutableSource source;
3435
private final Map<Long, CelReference> references;
3536
private final Map<Long, CelType> types;
3637

@@ -40,9 +41,10 @@ public CelMutableExpr expr() {
4041
}
4142

4243
/**
43-
* Returns the {@link CelSource} that was used during construction of the abstract syntax tree.
44+
* Returns the {@link CelMutableSource} that was used during construction of the abstract syntax
45+
* tree.
4446
*/
45-
public CelSource.Builder source() {
47+
public CelMutableSource source() {
4648
return source;
4749
}
4850

@@ -69,7 +71,7 @@ public Optional<CelType> getType(long exprId) {
6971
/** Converts this mutable AST into a parsed {@link CelAbstractSyntaxTree}. */
7072
public CelAbstractSyntaxTree toParsedAst() {
7173
return CelAbstractSyntaxTree.newParsedAst(
72-
CelMutableExprConverter.fromMutableExpr(mutatedExpr), source.build());
74+
CelMutableExprConverter.fromMutableExpr(mutatedExpr), source.toCelSource());
7375
}
7476

7577
/**
@@ -79,7 +81,7 @@ public CelAbstractSyntaxTree toParsedAst() {
7981
public static CelMutableAst fromCelAst(CelAbstractSyntaxTree ast) {
8082
return new CelMutableAst(
8183
CelMutableExprConverter.fromCelExpr(ast.getExpr()),
82-
ast.getSource().toBuilder(),
84+
CelMutableSource.fromCelSource(ast.getSource()),
8385
ast.getReferenceMap(),
8486
ast.getTypeMap());
8587
}
@@ -88,21 +90,21 @@ public static CelMutableAst fromCelAst(CelAbstractSyntaxTree ast) {
8890
* Constructs an instance of {@link CelMutableAst} with the mutable expression and its source
8991
* builder.
9092
*/
91-
public static CelMutableAst of(CelMutableExpr mutableExpr, CelSource.Builder sourceBuilder) {
92-
return new CelMutableAst(mutableExpr, sourceBuilder);
93+
public static CelMutableAst of(CelMutableExpr mutableExpr, CelMutableSource mutableSource) {
94+
return new CelMutableAst(mutableExpr, mutableSource);
9395
}
9496

95-
private CelMutableAst(CelMutableExpr mutatedExpr, CelSource.Builder source) {
96-
this(mutatedExpr, source, new HashMap<>(), new HashMap<>());
97+
private CelMutableAst(CelMutableExpr mutatedExpr, CelMutableSource mutableSource) {
98+
this(mutatedExpr, mutableSource, new HashMap<>(), new HashMap<>());
9799
}
98100

99101
private CelMutableAst(
100102
CelMutableExpr mutatedExpr,
101-
CelSource.Builder source,
103+
CelMutableSource mutableSource,
102104
Map<Long, CelReference> references,
103105
Map<Long, CelType> types) {
104106
this.mutatedExpr = mutatedExpr;
105-
this.source = source;
107+
this.source = mutableSource;
106108
this.references = new HashMap<>(references);
107109
this.types = new HashMap<>(types);
108110
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2024 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+
// https://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 dev.cel.common;
16+
17+
import static com.google.common.base.Preconditions.checkNotNull;
18+
import static com.google.common.collect.ImmutableMap.toImmutableMap;
19+
20+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
21+
import dev.cel.common.CelSource.Extension;
22+
import dev.cel.common.ast.CelMutableExpr;
23+
import dev.cel.common.ast.CelMutableExprConverter;
24+
import java.util.Collection;
25+
import java.util.HashMap;
26+
import java.util.HashSet;
27+
import java.util.Map;
28+
import java.util.Map.Entry;
29+
import java.util.Set;
30+
import java.util.stream.Collectors;
31+
32+
/**
33+
* Represents the mutable portion of the {@link CelSource}. This is intended for the purposes of
34+
* augmenting an AST through CEL optimizers.
35+
*/
36+
public final class CelMutableSource {
37+
38+
final Map<Long, CelMutableExpr> macroCalls;
39+
final Set<Extension> extensions;
40+
41+
@CanIgnoreReturnValue
42+
public CelMutableSource addMacroCalls(long exprId, CelMutableExpr expr) {
43+
this.macroCalls.put(exprId, checkNotNull(CelMutableExpr.newInstance(expr)));
44+
return this;
45+
}
46+
47+
@CanIgnoreReturnValue
48+
public CelMutableSource addAllMacroCalls(Map<Long, CelMutableExpr> macroCalls) {
49+
this.macroCalls.putAll(macroCalls);
50+
return this;
51+
}
52+
53+
@CanIgnoreReturnValue
54+
public CelMutableSource addAllExtensions(Collection<? extends Extension> extensions) {
55+
checkNotNull(extensions);
56+
this.extensions.addAll(extensions);
57+
return this;
58+
}
59+
60+
@CanIgnoreReturnValue
61+
public CelMutableSource clearMacroCall(long exprId) {
62+
this.macroCalls.remove(exprId);
63+
return this;
64+
}
65+
66+
@CanIgnoreReturnValue
67+
public CelMutableSource clearMacroCalls() {
68+
this.macroCalls.clear();
69+
return this;
70+
}
71+
72+
public Map<Long, CelMutableExpr> getMacroCalls() {
73+
return macroCalls;
74+
}
75+
76+
public Set<Extension> getExtensions() {
77+
return extensions;
78+
}
79+
80+
public CelSource toCelSource() {
81+
return CelSource.newBuilder()
82+
.addAllExtensions(extensions)
83+
.addAllMacroCalls(
84+
macroCalls.entrySet().stream()
85+
.collect(
86+
toImmutableMap(
87+
Entry::getKey, v -> CelMutableExprConverter.fromMutableExpr(v.getValue()))))
88+
.build();
89+
}
90+
91+
public static CelMutableSource newInstance() {
92+
return new CelMutableSource(new HashMap<>(), new HashSet<>());
93+
}
94+
95+
public static CelMutableSource fromCelSource(CelSource source) {
96+
return new CelMutableSource(
97+
source.getMacroCalls().entrySet().stream()
98+
.collect(
99+
Collectors.toMap(
100+
Entry::getKey,
101+
v -> CelMutableExprConverter.fromCelExpr(v.getValue()),
102+
(prev, next) -> {
103+
throw new IllegalStateException(
104+
"Unexpected source collision at ID: " + prev.id());
105+
},
106+
HashMap::new)),
107+
source.getExtensions());
108+
}
109+
110+
CelMutableSource(Map<Long, CelMutableExpr> macroCalls, Set<Extension> extensions) {
111+
this.macroCalls = checkNotNull(macroCalls);
112+
this.extensions = checkNotNull(extensions);
113+
}
114+
}

common/src/main/java/dev/cel/common/CelSource.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,6 @@ public Builder clearMacroCall(long exprId) {
279279
return this;
280280
}
281281

282-
@CanIgnoreReturnValue
283-
public Builder clearMacroCalls() {
284-
this.macroCalls.clear();
285-
return this;
286-
}
287-
288282
public ImmutableSet<Extension> getExtensions() {
289283
return extensions.build();
290284
}

common/src/main/java/dev/cel/common/ast/BUILD.bazel

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ EXPR_FACTORY_SOURCES = [
3333
]
3434

3535
# keep sorted
36-
MUTABLE_AST_SOURCES = [
37-
"CelMutableAst.java",
36+
MUTABLE_EXPR_SOURCES = [
3837
"CelMutableExpr.java",
3938
"CelMutableExprConverter.java",
4039
]
@@ -119,14 +118,12 @@ java_library(
119118
)
120119

121120
java_library(
122-
name = "mutable_ast",
123-
srcs = MUTABLE_AST_SOURCES,
121+
name = "mutable_expr",
122+
srcs = MUTABLE_EXPR_SOURCES,
124123
tags = [
125124
],
126125
deps = [
127126
":ast",
128-
"//common",
129-
"//common/types:type_providers",
130127
"@maven//:com_google_guava_guava",
131128
],
132129
)

common/src/main/java/dev/cel/common/ast/CelMutableExpr.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,9 @@ private CelMutableExpr(CelMutableExpr other) {
10641064
this.id = other.id;
10651065
this.exprKind = other.exprKind;
10661066
switch (other.getKind()) {
1067+
case NOT_SET:
1068+
this.exprValue = CelExpr.newBuilder().build().exprKind().notSet();
1069+
break;
10671070
case CONSTANT:
10681071
this.exprValue = other.exprValue; // Constant is immutable.
10691072
break;

common/src/main/java/dev/cel/common/ast/CelMutableExprConverter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ private static CelMutableCreateMap fromCelMapToMutableMap(CelCreateMap celCreate
134134
return CelMutableCreateMap.create(entries);
135135
}
136136

137-
///////////////////////
138-
139137
public static CelExpr fromMutableExpr(CelMutableExpr mutableExpr) {
140138
long id = mutableExpr.id();
141139
switch (mutableExpr.getKind()) {

common/src/main/java/dev/cel/common/navigation/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ java_library(
5656
deps = [
5757
":common",
5858
"//:auto_value",
59-
"//common/ast:mutable_ast",
59+
"//common:mutable_ast",
60+
"//common/ast:mutable_expr",
6061
"//common/types:type_providers",
6162
"@maven//:com_google_errorprone_error_prone_annotations",
6263
"@maven//:com_google_guava_guava",

0 commit comments

Comments
 (0)