Skip to content

Commit cda4e44

Browse files
jnthntatumcopybara-github
authored andcommitted
Move Expr to/from proto conversion to common/ast. Similar visibility rules to previous location (all the CEL library components may refer to it, but not CEL users).
Follow up will move the AST conversion code out of extensions. PiperOrigin-RevId: 736184373
1 parent 1e2eaa5 commit cda4e44

File tree

17 files changed

+340
-434
lines changed

17 files changed

+340
-434
lines changed

common/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ cc_test(
133133
":ast_visitor",
134134
":expr",
135135
"//base/ast_internal:ast_impl",
136+
"//base/ast_internal:expr",
137+
"//common/ast:expr_proto",
136138
"//extensions/protobuf:ast_converters",
137139
"//internal:testing",
138140
"//parser",
141+
"@com_google_absl//absl/status:status_matchers",
139142
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
140143
"@com_google_protobuf//:protobuf",
141144
],

common/ast.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class AstImpl;
3030
// manage lifecycle.
3131
//
3232
// Implementations are intentionally opaque to prevent dependencies on the
33-
// details of the runtime representation. To create an new instance, see the
34-
// factories in the extensions package (e.g.
35-
// extensions/protobuf/ast_converters.h).
33+
// details of the runtime representation. To create a new instance, from a
34+
// protobuf representation, use the conversion utilities in
35+
// `extensions/protobuf/ast_converters.h`.
3636
class Ast {
3737
public:
3838
virtual ~Ast() = default;

common/ast/BUILD

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2025 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+
# Internal AST implementation and utilities
16+
# These are needed by various parts of the CEL-C++ library, but are not intended for public use at
17+
# this time.
18+
package(default_visibility = ["//visibility:public"])
19+
20+
cc_library(
21+
name = "constant_proto",
22+
srcs = ["constant_proto.cc"],
23+
hdrs = ["constant_proto.h"],
24+
deps = [
25+
"//common:constant",
26+
"//internal:proto_time_encoding",
27+
"@com_google_absl//absl/base:nullability",
28+
"@com_google_absl//absl/functional:overload",
29+
"@com_google_absl//absl/status",
30+
"@com_google_absl//absl/strings",
31+
"@com_google_absl//absl/time",
32+
"@com_google_absl//absl/types:variant",
33+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
34+
"@com_google_protobuf//:protobuf",
35+
],
36+
)
37+
38+
cc_library(
39+
name = "expr_proto",
40+
srcs = ["expr_proto.cc"],
41+
hdrs = ["expr_proto.h"],
42+
deps = [
43+
":constant_proto",
44+
"//common:constant",
45+
"//common:expr",
46+
"//internal:status_macros",
47+
"@com_google_absl//absl/base:core_headers",
48+
"@com_google_absl//absl/base:nullability",
49+
"@com_google_absl//absl/functional:overload",
50+
"@com_google_absl//absl/status",
51+
"@com_google_absl//absl/strings",
52+
"@com_google_absl//absl/types:variant",
53+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
54+
"@com_google_protobuf//:protobuf",
55+
],
56+
)
57+
58+
cc_test(
59+
name = "expr_proto_test",
60+
srcs = ["expr_proto_test.cc"],
61+
deps = [
62+
":expr_proto",
63+
"//common:expr",
64+
"//internal:proto_matchers",
65+
"//internal:testing",
66+
"@com_google_absl//absl/status",
67+
"@com_google_absl//absl/status:status_matchers",
68+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
69+
"@com_google_protobuf//:protobuf",
70+
],
71+
)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "extensions/protobuf/internal/constant.h"
15+
#include "common/ast/constant_proto.h"
1616

1717
#include <cstddef>
1818
#include <cstdint>
@@ -28,7 +28,7 @@
2828
#include "common/constant.h"
2929
#include "internal/proto_time_encoding.h"
3030

31-
namespace cel::extensions::protobuf_internal {
31+
namespace cel::ast_internal {
3232

3333
using ConstantProto = cel::expr::Constant;
3434

@@ -120,4 +120,4 @@ absl::Status ConstantFromProto(const ConstantProto& proto, Constant& constant) {
120120
return absl::OkStatus();
121121
}
122122

123-
} // namespace cel::extensions::protobuf_internal
123+
} // namespace cel::ast_internal
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_CONSTANT_H_
16-
#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_CONSTANT_H_
15+
#ifndef THIRD_PARTY_CEL_CPP_COMMON_AST_CONSTANT_PROTO_H_
16+
#define THIRD_PARTY_CEL_CPP_COMMON_AST_CONSTANT_PROTO_H_
1717

1818
#include "cel/expr/syntax.pb.h"
1919
#include "absl/base/nullability.h"
2020
#include "absl/status/status.h"
2121
#include "common/constant.h"
2222

23-
namespace cel::extensions::protobuf_internal {
23+
namespace cel::ast_internal {
2424

2525
// `ConstantToProto` converts from native `Constant` to its protocol buffer
2626
// message equivalent.
@@ -32,6 +32,6 @@ absl::Status ConstantToProto(const Constant& constant,
3232
absl::Status ConstantFromProto(const cel::expr::Constant& proto,
3333
Constant& constant);
3434

35-
} // namespace cel::extensions::protobuf_internal
35+
} // namespace cel::ast_internal
3636

37-
#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_CONSTANT_H_
37+
#endif // THIRD_PARTY_CEL_CPP_COMMON_AST_CONSTANT_PROTO_H_
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "extensions/protobuf/internal/ast.h"
15+
#include "common/ast/expr_proto.h"
1616

1717
#include <algorithm>
1818
#include <cstddef>
@@ -28,12 +28,12 @@
2828
#include "absl/status/status.h"
2929
#include "absl/strings/str_cat.h"
3030
#include "absl/types/variant.h"
31-
#include "common/ast.h"
31+
#include "common/ast/constant_proto.h"
3232
#include "common/constant.h"
33-
#include "extensions/protobuf/internal/constant.h"
33+
#include "common/expr.h"
3434
#include "internal/status_macros.h"
3535

36-
namespace cel::extensions::protobuf_internal {
36+
namespace cel::ast_internal {
3737

3838
namespace {
3939

@@ -511,4 +511,4 @@ absl::Status ExprFromProto(const cel::expr::Expr& proto, Expr& expr) {
511511
return state.ExprFromProto(proto, expr);
512512
}
513513

514-
} // namespace cel::extensions::protobuf_internal
514+
} // namespace cel::ast_internal
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_AST_H_
16-
#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_AST_H_
15+
#ifndef THIRD_PARTY_CEL_CPP_COMMON_AST_EXPR_PROTO_H_
16+
#define THIRD_PARTY_CEL_CPP_COMMON_AST_EXPR_PROTO_H_
1717

1818
#include "cel/expr/syntax.pb.h"
1919
#include "absl/base/nullability.h"
2020
#include "absl/status/status.h"
2121
#include "common/expr.h"
2222

23-
namespace cel::extensions::protobuf_internal {
23+
namespace cel::ast_internal {
2424

2525
absl::Status ExprToProto(const Expr& expr,
2626
absl::Nonnull<cel::expr::Expr*> proto);
2727

2828
absl::Status ExprFromProto(const cel::expr::Expr& proto, Expr& expr);
2929

30-
} // namespace cel::extensions::protobuf_internal
30+
} // namespace cel::ast_internal
3131

32-
#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_AST_H_
32+
#endif // THIRD_PARTY_CEL_CPP_COMMON_AST_EXPR_PROTO_H_
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "extensions/protobuf/internal/ast.h"
15+
#include "common/ast/expr_proto.h"
1616

1717
#include <string>
1818

1919
#include "cel/expr/syntax.pb.h"
2020
#include "absl/status/status.h"
21-
#include "common/ast.h"
21+
#include "absl/status/status_matchers.h"
22+
#include "common/expr.h"
2223
#include "internal/proto_matchers.h"
2324
#include "internal/testing.h"
2425
#include "google/protobuf/text_format.h"
2526

26-
namespace cel::extensions::protobuf_internal {
27+
namespace cel::ast_internal {
2728
namespace {
2829

2930
using ::absl_testing::IsOk;
@@ -299,4 +300,4 @@ TEST(ExprFromProto, MapEntryInStruct) {
299300
}
300301

301302
} // namespace
302-
} // namespace cel::extensions::protobuf_internal
303+
} // namespace cel::ast_internal

common/ast_rewrite_test.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
#include <vector>
2020

2121
#include "cel/expr/syntax.pb.h"
22+
#include "absl/status/status_matchers.h"
2223
#include "base/ast_internal/ast_impl.h"
24+
#include "base/ast_internal/expr.h"
2325
#include "common/ast.h"
26+
#include "common/ast/expr_proto.h"
2427
#include "common/ast_visitor.h"
2528
#include "common/expr.h"
2629
#include "extensions/protobuf/ast_converters.h"
@@ -32,9 +35,10 @@ namespace cel {
3235

3336
namespace {
3437

38+
using ::absl_testing::IsOk;
3539
using ::cel::ast_internal::AstImpl;
40+
using ::cel::ast_internal::ExprFromProto;
3641
using ::cel::extensions::CreateAstFromParsedExpr;
37-
using ::cel::extensions::internal::ConvertProtoExprToNative;
3842
using ::testing::_;
3943
using ::testing::ElementsAre;
4044
using ::testing::InSequence;
@@ -543,8 +547,11 @@ TEST(AstRewrite, SelectRewriteExample) {
543547
ident_expr { name: "com.google.Identifier" }
544548
)pb",
545549
&expected_expr);
546-
EXPECT_EQ(ast_impl.root_expr(),
547-
ConvertProtoExprToNative(expected_expr).value());
550+
551+
cel::Expr expected_native;
552+
ASSERT_THAT(ExprFromProto(expected_expr, expected_native), IsOk());
553+
554+
EXPECT_EQ(ast_impl.root_expr(), expected_native);
548555
}
549556

550557
// Rewrites x -> y -> z to demonstrate traversal when a node is rewritten on
@@ -595,8 +602,10 @@ TEST(AstRewrite, PreAndPostVisitExpample) {
595602
ident_expr { name: "z" }
596603
)pb",
597604
&expected_expr);
598-
EXPECT_EQ(ast_impl.root_expr(),
599-
ConvertProtoExprToNative(expected_expr).value());
605+
cel::Expr expected_native;
606+
ASSERT_THAT(ExprFromProto(expected_expr, expected_native), IsOk());
607+
608+
EXPECT_EQ(ast_impl.root_expr(), expected_native);
600609
EXPECT_THAT(visitor.visited_idents(), ElementsAre("y"));
601610
}
602611

eval/compiler/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,19 @@ cc_test(
434434
"//base:builtins",
435435
"//base/ast_internal:ast_impl",
436436
"//base/ast_internal:expr",
437+
"//common/ast:expr_proto",
437438
"//eval/public:builtin_func_registrar",
438439
"//eval/public:cel_function",
439440
"//eval/public:cel_function_registry",
440441
"//extensions/protobuf:ast_converters",
441442
"//internal:casts",
443+
"//internal:proto_matchers",
442444
"//internal:testing",
443445
"//runtime:runtime_issue",
444446
"//runtime:type_registry",
445447
"//runtime/internal:issue_collector",
446448
"@com_google_absl//absl/container:flat_hash_map",
449+
"@com_google_absl//absl/log:absl_check",
447450
"@com_google_absl//absl/memory",
448451
"@com_google_absl//absl/status",
449452
"@com_google_absl//absl/strings",

0 commit comments

Comments
 (0)