1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_VARIANTVALUE_H |
17 | #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_VARIANTVALUE_H |
18 | |
19 | #include "clang/ASTMatchers/ASTMatchers.h" |
20 | #include "clang/ASTMatchers/ASTMatchersInternal.h" |
21 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
22 | #include "llvm/ADT/Optional.h" |
23 | #include <memory> |
24 | #include <vector> |
25 | |
26 | namespace clang { |
27 | namespace ast_matchers { |
28 | namespace dynamic { |
29 | |
30 | |
31 | |
32 | |
33 | class ArgKind { |
34 | public: |
35 | enum Kind { |
36 | AK_Matcher, |
37 | AK_Boolean, |
38 | AK_Double, |
39 | AK_Unsigned, |
40 | AK_String |
41 | }; |
42 | |
43 | ArgKind(Kind K) : K(K) { assert(K != AK_Matcher); } |
44 | |
45 | |
46 | ArgKind(ast_type_traits::ASTNodeKind MatcherKind) |
47 | : K(AK_Matcher), MatcherKind(MatcherKind) {} |
48 | |
49 | Kind getArgKind() const { return K; } |
50 | ast_type_traits::ASTNodeKind getMatcherKind() const { |
51 | assert(K == AK_Matcher); |
52 | return MatcherKind; |
53 | } |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | bool isConvertibleTo(ArgKind To, unsigned *Specificity) const; |
62 | |
63 | bool operator<(const ArgKind &Other) const { |
64 | if (K == AK_Matcher && Other.K == AK_Matcher) |
65 | return MatcherKind < Other.MatcherKind; |
66 | return K < Other.K; |
67 | } |
68 | |
69 | |
70 | std::string asString() const; |
71 | |
72 | private: |
73 | Kind K; |
74 | ast_type_traits::ASTNodeKind MatcherKind; |
75 | }; |
76 | |
77 | using ast_matchers::internal::DynTypedMatcher; |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | class VariantMatcher { |
93 | |
94 | class MatcherOps { |
95 | public: |
96 | MatcherOps(ast_type_traits::ASTNodeKind NodeKind) : NodeKind(NodeKind) {} |
97 | |
98 | bool canConstructFrom(const DynTypedMatcher &Matcher, |
99 | bool &IsExactMatch) const; |
100 | |
101 | |
102 | |
103 | virtual DynTypedMatcher |
104 | convertMatcher(const DynTypedMatcher &Matcher) const = 0; |
105 | |
106 | |
107 | |
108 | |
109 | llvm::Optional<DynTypedMatcher> |
110 | constructVariadicOperator(DynTypedMatcher::VariadicOperator Op, |
111 | ArrayRef<VariantMatcher> InnerMatchers) const; |
112 | |
113 | protected: |
114 | ~MatcherOps() = default; |
115 | |
116 | private: |
117 | ast_type_traits::ASTNodeKind NodeKind; |
118 | }; |
119 | |
120 | |
121 | |
122 | |
123 | class Payload { |
124 | public: |
125 | virtual ~Payload(); |
126 | virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const = 0; |
127 | virtual std::string getTypeAsString() const = 0; |
128 | virtual llvm::Optional<DynTypedMatcher> |
129 | getTypedMatcher(const MatcherOps &Ops) const = 0; |
130 | virtual bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, |
131 | unsigned *Specificity) const = 0; |
132 | }; |
133 | |
134 | public: |
135 | |
136 | VariantMatcher(); |
137 | |
138 | |
139 | static VariantMatcher SingleMatcher(const DynTypedMatcher &Matcher); |
140 | |
141 | |
142 | |
143 | |
144 | static VariantMatcher |
145 | PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers); |
146 | |
147 | |
148 | |
149 | |
150 | static VariantMatcher |
151 | VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, |
152 | std::vector<VariantMatcher> Args); |
153 | |
154 | |
155 | void reset(); |
156 | |
157 | |
158 | bool isNull() const { return !Value; } |
159 | |
160 | |
161 | |
162 | |
163 | |
164 | |
165 | llvm::Optional<DynTypedMatcher> getSingleMatcher() const; |
166 | |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | template <class T> |
176 | bool hasTypedMatcher() const { |
177 | if (!Value) return false; |
178 | return Value->getTypedMatcher(TypedMatcherOps<T>()).hasValue(); |
179 | } |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, |
188 | unsigned *Specificity) const { |
189 | if (Value) |
190 | return Value->isConvertibleTo(Kind, Specificity); |
191 | return false; |
192 | } |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | template <class T> |
199 | ast_matchers::internal::Matcher<T> getTypedMatcher() const { |
200 | () == false") ? static_cast (0) . __assert_fail ("hasTypedMatcher() && \"hasTypedMatcher() == false\"", "/home/seafit/code_projects/clang_source/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h", 200, __PRETTY_FUNCTION__))" file_link="../../../../../include/assert.h.html#88" macro="true">assert(hasTypedMatcher<T>() && "hasTypedMatcher<T>() == false"); |
201 | return Value->getTypedMatcher(TypedMatcherOps<T>()) |
202 | ->template convertTo<T>(); |
203 | } |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | std::string getTypeAsString() const; |
210 | |
211 | private: |
212 | explicit VariantMatcher(std::shared_ptr<Payload> Value) |
213 | : Value(std::move(Value)) {} |
214 | |
215 | template <typename T> struct TypedMatcherOps; |
216 | |
217 | class SinglePayload; |
218 | class PolymorphicPayload; |
219 | class VariadicOpPayload; |
220 | |
221 | std::shared_ptr<const Payload> Value; |
222 | }; |
223 | |
224 | template <typename T> |
225 | struct VariantMatcher::TypedMatcherOps final : VariantMatcher::MatcherOps { |
226 | TypedMatcherOps() |
227 | : MatcherOps(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()) {} |
228 | typedef ast_matchers::internal::Matcher<T> MatcherT; |
229 | |
230 | DynTypedMatcher |
231 | convertMatcher(const DynTypedMatcher &Matcher) const override { |
232 | return DynTypedMatcher(Matcher.convertTo<T>()); |
233 | } |
234 | }; |
235 | |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | |
250 | class VariantValue { |
251 | public: |
252 | VariantValue() : Type(VT_Nothing) {} |
253 | |
254 | VariantValue(const VariantValue &Other); |
255 | ~VariantValue(); |
256 | VariantValue &operator=(const VariantValue &Other); |
257 | |
258 | |
259 | VariantValue(bool Boolean); |
260 | VariantValue(double Double); |
261 | VariantValue(unsigned Unsigned); |
262 | VariantValue(StringRef String); |
263 | VariantValue(const VariantMatcher &Matchers); |
264 | |
265 | |
266 | VariantValue(int Signed) : VariantValue(static_cast<unsigned>(Signed)) {} |
267 | |
268 | |
269 | explicit operator bool() const { return hasValue(); } |
270 | bool hasValue() const { return Type != VT_Nothing; } |
271 | |
272 | |
273 | bool isBoolean() const; |
274 | bool getBoolean() const; |
275 | void setBoolean(bool Boolean); |
276 | |
277 | |
278 | bool isDouble() const; |
279 | double getDouble() const; |
280 | void setDouble(double Double); |
281 | |
282 | |
283 | bool isUnsigned() const; |
284 | unsigned getUnsigned() const; |
285 | void setUnsigned(unsigned Unsigned); |
286 | |
287 | |
288 | bool isString() const; |
289 | const std::string &getString() const; |
290 | void setString(StringRef String); |
291 | |
292 | |
293 | bool isMatcher() const; |
294 | const VariantMatcher &getMatcher() const; |
295 | void setMatcher(const VariantMatcher &Matcher); |
296 | |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | bool isConvertibleTo(ArgKind Kind, unsigned* Specificity) const; |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | bool isConvertibleTo(ArrayRef<ArgKind> Kinds, unsigned *Specificity) const; |
314 | |
315 | |
316 | std::string getTypeAsString() const; |
317 | |
318 | private: |
319 | void reset(); |
320 | |
321 | |
322 | enum ValueType { |
323 | VT_Nothing, |
324 | VT_Boolean, |
325 | VT_Double, |
326 | VT_Unsigned, |
327 | VT_String, |
328 | VT_Matcher |
329 | }; |
330 | |
331 | |
332 | union AllValues { |
333 | unsigned Unsigned; |
334 | double Double; |
335 | bool Boolean; |
336 | std::string *String; |
337 | VariantMatcher *Matcher; |
338 | }; |
339 | |
340 | ValueType Type; |
341 | AllValues Value; |
342 | }; |
343 | |
344 | } |
345 | } |
346 | } |
347 | |
348 | #endif |
349 | |