-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathast_impl.h
More file actions
106 lines (85 loc) · 3.32 KB
/
Copy pathast_impl.h
File metadata and controls
106 lines (85 loc) · 3.32 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
// Copyright 2022 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_BASE_INTERNAL_AST_IMPL_H_
#define THIRD_PARTY_CEL_CPP_BASE_INTERNAL_AST_IMPL_H_
#include <cstdint>
#include <string>
#include <utility>
#include "absl/container/flat_hash_map.h"
#include "base/ast.h"
#include "base/ast_internal.h"
#include "internal/casts.h"
namespace cel::ast::internal {
// Runtime implementation of a CEL abstract syntax tree.
// CEL users should not use this directly.
// If AST inspection is needed, prefer to use an existing tool or traverse the
// the protobuf representation.
class AstImpl : public Ast {
public:
// Overloads for down casting from the public interface to the internal
// implementation.
static AstImpl& CastFromPublicAst(Ast& ast) {
return cel::internal::down_cast<AstImpl&>(ast);
}
static const AstImpl& CastFromPublicAst(const Ast& ast) {
return cel::internal::down_cast<const AstImpl&>(ast);
}
static AstImpl* CastFromPublicAst(Ast* ast) {
return cel::internal::down_cast<AstImpl*>(ast);
}
static const AstImpl* CastFromPublicAst(const Ast* ast) {
return cel::internal::down_cast<const AstImpl*>(ast);
}
explicit AstImpl(Expr expr, SourceInfo source_info)
: root_expr_(std::move(expr)),
source_info_(std::move(source_info)),
is_checked_(false) {}
explicit AstImpl(ParsedExpr expr)
: root_expr_(std::move(expr.mutable_expr())),
source_info_(std::move(expr.mutable_source_info())),
is_checked_(false) {}
explicit AstImpl(CheckedExpr expr)
: root_expr_(std::move(expr.mutable_expr())),
source_info_(std::move(expr.mutable_source_info())),
reference_map_(std::move(expr.mutable_reference_map())),
type_map_(std::move(expr.mutable_type_map())),
is_checked_(true) {}
// Implement public Ast APIs.
bool IsChecked() const override { return is_checked_; }
// Private functions.
const Expr& root_expr() const { return root_expr_; }
Expr& root_expr() { return root_expr_; }
const SourceInfo& source_info() const { return source_info_; }
SourceInfo& source_info() { return source_info_; }
const Type& GetType(int64_t expr_id) const;
const Type& GetReturnType() const;
const Reference* GetReference(int64_t expr_id) const;
const absl::flat_hash_map<int64_t, Reference>& reference_map() const {
return reference_map_;
}
absl::flat_hash_map<int64_t, Reference>& reference_map() {
return reference_map_;
}
const absl::flat_hash_map<int64_t, Type>& type_map() const {
return type_map_;
}
private:
Expr root_expr_;
SourceInfo source_info_;
absl::flat_hash_map<int64_t, Reference> reference_map_;
absl::flat_hash_map<int64_t, Type> type_map_;
bool is_checked_;
};
} // namespace cel::ast::internal
#endif // THIRD_PARTY_CEL_CPP_BASE_INTERNAL_AST_IMPL_H_