forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.h
More file actions
157 lines (135 loc) · 5.86 KB
/
Copy pathast.h
File metadata and controls
157 lines (135 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_CEL_CPP_COMMON_AST_H_
#define THIRD_PARTY_CEL_CPP_COMMON_AST_H_
#include <cstdint>
#include <string>
#include <utility>
#include "absl/base/nullability.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "common/ast/metadata.h" // IWYU pragma: export
#include "common/expr.h"
#include "common/source.h"
namespace cel {
// In memory representation of a CEL abstract syntax tree.
//
// If AST inspection or manipulation is needed, prefer to use an existing tool
// or traverse the protobuf representation rather than directly manipulating
// through this class. See `cel::NavigableAst` and `cel::AstTraverse`.
//
// Type and reference maps are only populated if the AST is checked. Any changes
// to the AST are not automatically reflected in the type or reference maps.
//
// To create a new instance from a protobuf representation, use the conversion
// utilities in `common/ast_proto.h`.
class Ast final {
public:
using ReferenceMap = absl::flat_hash_map<int64_t, Reference>;
using TypeMap = absl::flat_hash_map<int64_t, TypeSpec>;
Ast() : is_checked_(false) {}
Ast(Expr expr, SourceInfo source_info)
: root_expr_(std::move(expr)),
source_info_(std::move(source_info)),
is_checked_(false) {}
Ast(Expr expr, SourceInfo source_info, ReferenceMap reference_map,
TypeMap type_map, std::string expr_version)
: root_expr_(std::move(expr)),
source_info_(std::move(source_info)),
reference_map_(std::move(reference_map)),
type_map_(std::move(type_map)),
expr_version_(std::move(expr_version)),
is_checked_(true) {}
Ast(const Ast& other) = default;
Ast& operator=(const Ast& other) = default;
Ast(Ast&& other) = default;
Ast& operator=(Ast&& other) = default;
// Deprecated. Use `is_checked()` instead.
bool IsChecked() const { return is_checked_; }
bool is_checked() const { return is_checked_; }
void set_is_checked(bool is_checked) { is_checked_ = is_checked; }
// The root expression of the AST.
//
// This is the entry point for evaluation and determines the overall result
// of the expression given a context.
const Expr& root_expr() const { return root_expr_; }
Expr& mutable_root_expr() { return root_expr_; }
// Metadata about the source expression.
const SourceInfo& source_info() const { return source_info_; }
SourceInfo& mutable_source_info() { return source_info_; }
// Returns the type of the expression with the given `expr_id`.
//
// Returns `nullptr` if the expression node is not found or has dynamic type.
const TypeSpec* absl_nullable GetType(int64_t expr_id) const;
const TypeSpec& GetTypeOrDyn(int64_t expr_id) const;
const TypeSpec& GetReturnType() const;
// Returns the resolved reference for the expression with the given `expr_id`.
//
// Returns `nullptr` if the expression node is not found or no reference was
// resolved.
const Reference* absl_nullable GetReference(int64_t expr_id) const;
// A map from expression ids to resolved references.
//
// The following entries are in this table:
//
// - An Ident or Select expression is represented here if it resolves to a
// declaration. For instance, if `a.b.c` is represented by
// `select(select(id(a), b), c)`, and `a.b` resolves to a declaration,
// while `c` is a field selection, then the reference is attached to the
// nested select expression (but not to the id or or the outer select).
// In turn, if `a` resolves to a declaration and `b.c` are field selections,
// the reference is attached to the ident expression.
// - Every Call expression has an entry here, identifying the function being
// called.
// - Every CreateStruct expression for a message has an entry, identifying
// the message.
//
// Unpopulated if the AST is not checked.
const ReferenceMap& reference_map() const { return reference_map_; }
ReferenceMap& mutable_reference_map() { return reference_map_; }
// A map from expression ids to types.
//
// Every expression node which has a type different than DYN has a mapping
// here. If an expression has type DYN, it is omitted from this map to save
// space.
//
// Unpopulated if the AST is not checked.
const TypeMap& type_map() const { return type_map_; }
TypeMap& mutable_type_map() { return type_map_; }
// The expr version indicates the major / minor version number of the `expr`
// representation.
//
// The most common reason for a version change will be to indicate to the CEL
// runtimes that transformations have been performed on the expr during static
// analysis.
absl::string_view expr_version() const { return expr_version_; }
void set_expr_version(absl::string_view expr_version) {
expr_version_ = expr_version;
}
// Computes the source location (line and column) for the given expression ID
// from the source info (which stores absolute positions).
//
// Returns a default (empty) source location if the expression ID is not found
// or the source info is not populated correctly.
SourceLocation ComputeSourceLocation(int64_t expr_id) const;
private:
Expr root_expr_;
SourceInfo source_info_;
ReferenceMap reference_map_;
TypeMap type_map_;
std::string expr_version_;
bool is_checked_;
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_AST_H_