-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathast_internal.cc
More file actions
178 lines (151 loc) · 4.32 KB
/
Copy pathast_internal.cc
File metadata and controls
178 lines (151 loc) · 4.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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.
#include "base/ast_internal.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace cel::ast::internal {
namespace {
const Expr& default_expr() {
static Expr* expr = new Expr();
return *expr;
}
} // namespace
const Expr& Select::operand() const {
if (operand_ != nullptr) {
return *operand_;
}
return default_expr();
}
bool Select::operator==(const Select& other) const {
return operand() == other.operand() && field_ == other.field_ &&
test_only_ == other.test_only_;
}
const Expr& Call::target() const {
if (target_ != nullptr) {
return *target_;
}
return default_expr();
}
bool Call::operator==(const Call& other) const {
return target() == other.target() && function_ == other.function_ &&
args_ == other.args_;
}
const Expr& CreateStruct::Entry::map_key() const {
auto* value = absl::get_if<std::unique_ptr<Expr>>(&key_kind_);
if (value != nullptr) {
if (*value != nullptr) return **value;
}
return default_expr();
}
const Expr& CreateStruct::Entry::value() const {
if (value_ != nullptr) {
return *value_;
}
return default_expr();
}
bool CreateStruct::Entry::operator==(const Entry& other) const {
bool has_same_key = false;
if (has_field_key() && other.has_field_key()) {
has_same_key = field_key() == other.field_key();
} else if (has_map_key() && other.has_map_key()) {
has_same_key = map_key() == other.map_key();
}
return id_ == other.id_ && has_same_key && value() == other.value();
}
const Expr& Comprehension::iter_range() const {
if (iter_range_ != nullptr) {
return *iter_range_;
}
return default_expr();
}
const Expr& Comprehension::accu_init() const {
if (accu_init_ != nullptr) {
return *accu_init_;
}
return default_expr();
}
const Expr& Comprehension::loop_condition() const {
if (loop_condition_ != nullptr) {
return *loop_condition_;
}
return default_expr();
}
const Expr& Comprehension::loop_step() const {
if (loop_step_ != nullptr) {
return *loop_step_;
}
return default_expr();
}
const Expr& Comprehension::result() const {
if (result_ != nullptr) {
return *result_;
}
return default_expr();
}
bool Comprehension::operator==(const Comprehension& other) const {
return iter_var_ == other.iter_var_ && iter_range() == other.iter_range() &&
accu_var_ == other.accu_var_ && accu_init() == other.accu_init() &&
loop_condition() == other.loop_condition() &&
loop_step() == other.loop_step() && result() == other.result();
}
namespace {
const Type& default_type() {
static Type* type = new Type();
return *type;
}
} // namespace
const Type& ListType::elem_type() const {
if (elem_type_ != nullptr) {
return *elem_type_;
}
return default_type();
}
bool ListType::operator==(const ListType& other) const {
return elem_type() == other.elem_type();
}
const Type& MapType::key_type() const {
if (key_type_ != nullptr) {
return *key_type_;
}
return default_type();
}
const Type& MapType::value_type() const {
if (value_type_ != nullptr) {
return *value_type_;
}
return default_type();
}
bool MapType::operator==(const MapType& other) const {
return key_type() == other.key_type() && value_type() == other.value_type();
}
const Type& FunctionType::result_type() const {
if (result_type_ != nullptr) {
return *result_type_;
}
return default_type();
}
bool FunctionType::operator==(const FunctionType& other) const {
return result_type() == other.result_type() && arg_types_ == other.arg_types_;
}
const Type& Type::type() const {
auto* value = absl::get_if<std::unique_ptr<Type>>(&type_kind_);
if (value != nullptr) {
if (*value != nullptr) return **value;
}
return default_type();
}
} // namespace cel::ast::internal