/proc/self/cwd/common/function_descriptor.cc
Line | Count | Source |
1 | | // Copyright 2023 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 | | #include "common/function_descriptor.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cstddef> |
19 | | #include <memory> |
20 | | #include <variant> |
21 | | #include <vector> |
22 | | |
23 | | #include "absl/base/macros.h" |
24 | | #include "absl/functional/overload.h" |
25 | | #include "absl/types/span.h" |
26 | | #include "common/ast.h" |
27 | | #include "common/kind.h" |
28 | | |
29 | | namespace cel { |
30 | | |
31 | | bool FunctionDescriptor::ShapeMatches(bool receiver_style, |
32 | 14.4M | absl::Span<const Kind> types) const { |
33 | 14.4M | if (this->receiver_style() != receiver_style) { |
34 | 497k | return false; |
35 | 497k | } |
36 | | |
37 | 13.9M | if (this->types().size() != types.size()) { |
38 | 348k | return false; |
39 | 348k | } |
40 | | |
41 | 14.8M | for (size_t i = 0; i < this->types().size(); i++) { |
42 | 14.8M | Kind this_type = this->types()[i]; |
43 | 14.8M | Kind other_type = types[i]; |
44 | 14.8M | if (this_type != Kind::kAny && other_type != Kind::kAny && |
45 | 14.5M | this_type != other_type) { |
46 | 13.5M | return false; |
47 | 13.5M | } |
48 | 14.8M | } |
49 | 0 | return true; |
50 | 13.5M | } |
51 | | |
52 | 0 | bool FunctionDescriptor::operator==(const FunctionDescriptor& other) const { |
53 | 0 | return name() == other.name() && receiver_style() == other.receiver_style() && |
54 | 0 | types().size() == other.types().size() && |
55 | 0 | std::equal(types().begin(), types().end(), other.types().begin()); |
56 | 0 | } |
57 | | |
58 | 0 | bool FunctionDescriptor::operator<(const FunctionDescriptor& other) const { |
59 | 0 | if (name() < other.name()) { |
60 | 0 | return true; |
61 | 0 | } |
62 | 0 | if (name() != other.name()) { |
63 | 0 | return false; |
64 | 0 | } |
65 | 0 | if (receiver_style() < other.receiver_style()) { |
66 | 0 | return true; |
67 | 0 | } |
68 | 0 | if (receiver_style() != other.receiver_style()) { |
69 | 0 | return false; |
70 | 0 | } |
71 | 0 | auto lhs_begin = types().begin(); |
72 | 0 | auto lhs_end = types().end(); |
73 | 0 | auto rhs_begin = other.types().begin(); |
74 | 0 | auto rhs_end = other.types().end(); |
75 | 0 | while (lhs_begin != lhs_end && rhs_begin != rhs_end) { |
76 | 0 | if (*lhs_begin < *rhs_begin) { |
77 | 0 | return true; |
78 | 0 | } |
79 | 0 | if (!(*lhs_begin == *rhs_begin)) { |
80 | 0 | return false; |
81 | 0 | } |
82 | 0 | lhs_begin++; |
83 | 0 | rhs_begin++; |
84 | 0 | } |
85 | 0 | if (lhs_begin == lhs_end && rhs_begin == rhs_end) { |
86 | | // Neither has any elements left, they are equal. |
87 | 0 | return false; |
88 | 0 | } |
89 | 0 | if (lhs_begin == lhs_end) { |
90 | | // Left has no more elements. Right is greater. |
91 | 0 | return true; |
92 | 0 | } |
93 | | // Right has no more elements. Left is greater. |
94 | 0 | ABSL_ASSERT(rhs_begin == rhs_end); |
95 | 0 | return false; |
96 | 0 | } |
97 | | |
98 | | namespace { |
99 | | |
100 | | [[nodiscard]] |
101 | 0 | Kind TypeSpecToKind(const TypeSpec& spec) { |
102 | 0 | return std::visit( |
103 | 0 | absl::Overload( |
104 | 0 | [](UnsetTypeSpec) -> Kind { return Kind::kError; }, |
105 | 0 | [](const DynTypeSpec& spec) -> Kind { return Kind::kDyn; }, |
106 | 0 | [](const NullTypeSpec& spec) -> Kind { return Kind::kNull; }, |
107 | 0 | [](const PrimitiveType& spec) -> Kind { |
108 | 0 | switch (spec) { |
109 | 0 | case PrimitiveType::kPrimitiveTypeUnspecified: |
110 | 0 | return Kind::kError; |
111 | 0 | case PrimitiveType::kBool: |
112 | 0 | return Kind::kBool; |
113 | 0 | case PrimitiveType::kInt64: |
114 | 0 | return Kind::kInt; |
115 | 0 | case PrimitiveType::kUint64: |
116 | 0 | return Kind::kUint; |
117 | 0 | case PrimitiveType::kDouble: |
118 | 0 | return Kind::kDouble; |
119 | 0 | case PrimitiveType::kString: |
120 | 0 | return Kind::kString; |
121 | 0 | case PrimitiveType::kBytes: |
122 | 0 | return Kind::kBytes; |
123 | 0 | } |
124 | 0 | }, |
125 | 0 | [](const PrimitiveTypeWrapper& spec) -> Kind { |
126 | 0 | switch (spec.type()) { |
127 | 0 | case PrimitiveType::kPrimitiveTypeUnspecified: |
128 | 0 | return Kind::kError; |
129 | 0 | case PrimitiveType::kBool: |
130 | 0 | return Kind::kBoolWrapper; |
131 | 0 | case PrimitiveType::kInt64: |
132 | 0 | return Kind::kIntWrapper; |
133 | 0 | case PrimitiveType::kUint64: |
134 | 0 | return Kind::kUintWrapper; |
135 | 0 | case PrimitiveType::kDouble: |
136 | 0 | return Kind::kDoubleWrapper; |
137 | 0 | case PrimitiveType::kString: |
138 | 0 | return Kind::kStringWrapper; |
139 | 0 | case PrimitiveType::kBytes: |
140 | 0 | return Kind::kBytesWrapper; |
141 | 0 | } |
142 | 0 | }, |
143 | 0 | [](const WellKnownTypeSpec& spec) -> Kind { |
144 | 0 | switch (spec) { |
145 | 0 | case cel::WellKnownTypeSpec::kWellKnownTypeUnspecified: |
146 | 0 | return Kind::kError; |
147 | 0 | case cel::WellKnownTypeSpec::kDuration: |
148 | 0 | return Kind::kError; |
149 | 0 | case cel::WellKnownTypeSpec::kTimestamp: |
150 | 0 | return Kind::kTimestamp; |
151 | 0 | case cel::WellKnownTypeSpec::kAny: |
152 | 0 | return Kind::kAny; |
153 | 0 | } |
154 | 0 | }, |
155 | 0 | [](const ListTypeSpec& spec) -> Kind { return Kind::kList; }, |
156 | 0 | [](const MapTypeSpec& spec) -> Kind { return Kind::kMap; }, |
157 | 0 | [](const FunctionTypeSpec& spec) -> Kind { return Kind::kFunction; }, |
158 | 0 | [](const MessageTypeSpec& spec) -> Kind { return Kind::kStruct; }, |
159 | 0 | [](const ParamTypeSpec& spec) -> Kind { return Kind::kTypeParam; }, |
160 | 0 | [](const std::unique_ptr<TypeSpec>& spec) -> Kind { |
161 | 0 | return Kind::kType; |
162 | 0 | }, |
163 | 0 | [](const ErrorTypeSpec& spec) -> Kind { return Kind::kError; }, |
164 | 0 | [](const AbstractType& spec) -> Kind { return Kind::kOpaque; }), |
165 | 0 | spec.type_kind()); |
166 | 0 | } |
167 | | |
168 | | } // namespace |
169 | | |
170 | | std::vector<Kind> FunctionDescriptor::FunctionTypeSpecToArgumentKinds( |
171 | 0 | const FunctionTypeSpec& spec) { |
172 | 0 | std::vector<Kind> kinds; |
173 | 0 | kinds.reserve(spec.arg_types().size()); |
174 | 0 | for (const auto& arg : spec.arg_types()) { |
175 | 0 | kinds.push_back(TypeSpecToKind(arg)); |
176 | 0 | } |
177 | 0 | return kinds; |
178 | 0 | } |
179 | | |
180 | | } // namespace cel |