File: | include/llvm/ADT/IntrusiveRefCntPtr.h |
Warning: | line 157, column 38 Potential memory leak |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===---- QueryParser.cpp - clang-query command parser --------------------===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | ||||
10 | #include "QueryParser.h" | |||
11 | #include "Query.h" | |||
12 | #include "QuerySession.h" | |||
13 | #include "clang/ASTMatchers/Dynamic/Parser.h" | |||
14 | #include "clang/Basic/CharInfo.h" | |||
15 | #include "llvm/ADT/StringRef.h" | |||
16 | #include "llvm/ADT/StringSwitch.h" | |||
17 | #include <set> | |||
18 | ||||
19 | using namespace llvm; | |||
20 | using namespace clang::ast_matchers::dynamic; | |||
21 | ||||
22 | namespace clang { | |||
23 | namespace query { | |||
24 | ||||
25 | // Lex any amount of whitespace followed by a "word" (any sequence of | |||
26 | // non-whitespace characters) from the start of region [Begin,End). If no word | |||
27 | // is found before End, return StringRef(). Begin is adjusted to exclude the | |||
28 | // lexed region. | |||
29 | StringRef QueryParser::lexWord() { | |||
30 | while (true) { | |||
31 | if (Begin == End) | |||
32 | return StringRef(Begin, 0); | |||
33 | ||||
34 | if (!isWhitespace(*Begin)) | |||
35 | break; | |||
36 | ||||
37 | ++Begin; | |||
38 | } | |||
39 | ||||
40 | if (*Begin == '#') { | |||
41 | End = Begin; | |||
42 | return StringRef(); | |||
43 | } | |||
44 | ||||
45 | const char *WordBegin = Begin; | |||
46 | ||||
47 | while (true) { | |||
48 | ++Begin; | |||
49 | ||||
50 | if (Begin == End || isWhitespace(*Begin)) | |||
51 | return StringRef(WordBegin, Begin - WordBegin); | |||
52 | } | |||
53 | } | |||
54 | ||||
55 | // This is the StringSwitch-alike used by lexOrCompleteWord below. See that | |||
56 | // function for details. | |||
57 | template <typename T> struct QueryParser::LexOrCompleteWord { | |||
58 | StringRef Word; | |||
59 | StringSwitch<T> Switch; | |||
60 | ||||
61 | QueryParser *P; | |||
62 | // Set to the completion point offset in Word, or StringRef::npos if | |||
63 | // completion point not in Word. | |||
64 | size_t WordCompletionPos; | |||
65 | ||||
66 | // Lexes a word and stores it in Word. Returns a LexOrCompleteWord<T> object | |||
67 | // that can be used like a llvm::StringSwitch<T>, but adds cases as possible | |||
68 | // completions if the lexed word contains the completion point. | |||
69 | LexOrCompleteWord(QueryParser *P, StringRef &OutWord) | |||
70 | : Word(P->lexWord()), Switch(Word), P(P), | |||
71 | WordCompletionPos(StringRef::npos) { | |||
72 | OutWord = Word; | |||
73 | if (P->CompletionPos && P->CompletionPos <= Word.data() + Word.size()) { | |||
74 | if (P->CompletionPos < Word.data()) | |||
75 | WordCompletionPos = 0; | |||
76 | else | |||
77 | WordCompletionPos = P->CompletionPos - Word.data(); | |||
78 | } | |||
79 | } | |||
80 | ||||
81 | LexOrCompleteWord &Case(llvm::StringLiteral CaseStr, const T &Value, | |||
82 | bool IsCompletion = true) { | |||
83 | ||||
84 | if (WordCompletionPos == StringRef::npos) | |||
85 | Switch.Case(CaseStr, Value); | |||
86 | else if (CaseStr.size() != 0 && IsCompletion && WordCompletionPos <= CaseStr.size() && | |||
87 | CaseStr.substr(0, WordCompletionPos) == | |||
88 | Word.substr(0, WordCompletionPos)) | |||
89 | P->Completions.push_back(LineEditor::Completion( | |||
90 | (CaseStr.substr(WordCompletionPos) + " ").str(), CaseStr)); | |||
91 | return *this; | |||
92 | } | |||
93 | ||||
94 | T Default(T Value) { return Switch.Default(Value); } | |||
95 | }; | |||
96 | ||||
97 | QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) { | |||
98 | StringRef ValStr; | |||
99 | unsigned Value = LexOrCompleteWord<unsigned>(this, ValStr) | |||
100 | .Case("false", 0) | |||
101 | .Case("true", 1) | |||
102 | .Default(~0u); | |||
103 | if (Value == ~0u) { | |||
104 | return new InvalidQuery("expected 'true' or 'false', got '" + ValStr + "'"); | |||
105 | } | |||
106 | return new SetQuery<bool>(Var, Value); | |||
107 | } | |||
108 | ||||
109 | QueryRef QueryParser::parseSetOutputKind() { | |||
110 | StringRef ValStr; | |||
111 | unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr) | |||
112 | .Case("diag", OK_Diag) | |||
113 | .Case("print", OK_Print) | |||
114 | .Case("detailed-ast", OK_DetailedAST) | |||
115 | .Case("dump", OK_DetailedAST) | |||
116 | .Default(~0u); | |||
117 | if (OutKind == ~0u) { | |||
118 | return new InvalidQuery( | |||
119 | "expected 'diag', 'print', 'detailed-ast' or 'dump', got '" + ValStr + | |||
120 | "'"); | |||
121 | } | |||
122 | ||||
123 | switch (OutKind) { | |||
124 | case OK_DetailedAST: | |||
125 | return new SetExclusiveOutputQuery(&QuerySession::DetailedASTOutput); | |||
126 | case OK_Diag: | |||
127 | return new SetExclusiveOutputQuery(&QuerySession::DiagOutput); | |||
128 | case OK_Print: | |||
129 | return new SetExclusiveOutputQuery(&QuerySession::PrintOutput); | |||
130 | } | |||
131 | ||||
132 | llvm_unreachable("Invalid output kind")::llvm::llvm_unreachable_internal("Invalid output kind", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/tools/extra/clang-query/QueryParser.cpp" , 132); | |||
133 | } | |||
134 | ||||
135 | QueryRef QueryParser::endQuery(QueryRef Q) { | |||
136 | const char *Extra = Begin; | |||
137 | if (!lexWord().empty()) | |||
138 | return new InvalidQuery("unexpected extra input: '" + | |||
139 | StringRef(Extra, End - Extra) + "'"); | |||
140 | return Q; | |||
141 | } | |||
142 | ||||
143 | namespace { | |||
144 | ||||
145 | enum ParsedQueryKind { | |||
146 | PQK_Invalid, | |||
147 | PQK_Comment, | |||
148 | PQK_NoOp, | |||
149 | PQK_Help, | |||
150 | PQK_Let, | |||
151 | PQK_Match, | |||
152 | PQK_Set, | |||
153 | PQK_Unlet, | |||
154 | PQK_Quit | |||
155 | }; | |||
156 | ||||
157 | enum ParsedQueryVariable { | |||
158 | PQV_Invalid, | |||
159 | PQV_Output, | |||
160 | PQV_BindRoot, | |||
161 | PQV_PrintMatcher | |||
162 | }; | |||
163 | ||||
164 | QueryRef makeInvalidQueryFromDiagnostics(const Diagnostics &Diag) { | |||
165 | std::string ErrStr; | |||
166 | llvm::raw_string_ostream OS(ErrStr); | |||
167 | Diag.printToStreamFull(OS); | |||
168 | return new InvalidQuery(OS.str()); | |||
169 | } | |||
170 | ||||
171 | } // namespace | |||
172 | ||||
173 | QueryRef QueryParser::completeMatcherExpression() { | |||
174 | std::vector<MatcherCompletion> Comps = Parser::completeExpression( | |||
175 | StringRef(Begin, End - Begin), CompletionPos - Begin, nullptr, | |||
176 | &QS.NamedValues); | |||
177 | for (auto I = Comps.begin(), E = Comps.end(); I != E; ++I) { | |||
178 | Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl)); | |||
179 | } | |||
180 | return QueryRef(); | |||
181 | } | |||
182 | ||||
183 | QueryRef QueryParser::doParse() { | |||
184 | StringRef CommandStr; | |||
185 | ParsedQueryKind QKind = LexOrCompleteWord<ParsedQueryKind>(this, CommandStr) | |||
186 | .Case("", PQK_NoOp) | |||
187 | .Case("#", PQK_Comment, /*IsCompletion=*/false) | |||
188 | .Case("help", PQK_Help) | |||
189 | .Case("l", PQK_Let, /*IsCompletion=*/false) | |||
190 | .Case("let", PQK_Let) | |||
191 | .Case("m", PQK_Match, /*IsCompletion=*/false) | |||
192 | .Case("match", PQK_Match) | |||
193 | .Case("q", PQK_Quit, /*IsCompletion=*/false) | |||
194 | .Case("quit", PQK_Quit) | |||
195 | .Case("set", PQK_Set) | |||
196 | .Case("unlet", PQK_Unlet) | |||
197 | .Default(PQK_Invalid); | |||
198 | ||||
199 | switch (QKind) { | |||
200 | case PQK_Comment: | |||
201 | case PQK_NoOp: | |||
202 | return new NoOpQuery; | |||
203 | ||||
204 | case PQK_Help: | |||
205 | return endQuery(new HelpQuery); | |||
206 | ||||
207 | case PQK_Quit: | |||
208 | return endQuery(new QuitQuery); | |||
209 | ||||
210 | case PQK_Let: { | |||
211 | StringRef Name = lexWord(); | |||
212 | ||||
213 | if (Name.empty()) | |||
214 | return new InvalidQuery("expected variable name"); | |||
215 | ||||
216 | if (CompletionPos) | |||
217 | return completeMatcherExpression(); | |||
218 | ||||
219 | Diagnostics Diag; | |||
220 | ast_matchers::dynamic::VariantValue Value; | |||
221 | if (!Parser::parseExpression(StringRef(Begin, End - Begin), nullptr, | |||
222 | &QS.NamedValues, &Value, &Diag)) { | |||
223 | return makeInvalidQueryFromDiagnostics(Diag); | |||
224 | } | |||
225 | ||||
226 | return new LetQuery(Name, Value); | |||
227 | } | |||
228 | ||||
229 | case PQK_Match: { | |||
230 | if (CompletionPos) | |||
231 | return completeMatcherExpression(); | |||
232 | ||||
233 | Diagnostics Diag; | |||
234 | auto MatcherSource = StringRef(Begin, End - Begin).trim(); | |||
235 | Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression( | |||
236 | MatcherSource, nullptr, &QS.NamedValues, &Diag); | |||
237 | if (!Matcher) { | |||
238 | return makeInvalidQueryFromDiagnostics(Diag); | |||
239 | } | |||
240 | return new MatchQuery(MatcherSource, *Matcher); | |||
241 | } | |||
242 | ||||
243 | case PQK_Set: { | |||
244 | StringRef VarStr; | |||
245 | ParsedQueryVariable Var = | |||
246 | LexOrCompleteWord<ParsedQueryVariable>(this, VarStr) | |||
247 | .Case("output", PQV_Output) | |||
248 | .Case("bind-root", PQV_BindRoot) | |||
249 | .Case("print-matcher", PQV_PrintMatcher) | |||
250 | .Default(PQV_Invalid); | |||
251 | if (VarStr.empty()) | |||
252 | return new InvalidQuery("expected variable name"); | |||
253 | if (Var == PQV_Invalid) | |||
254 | return new InvalidQuery("unknown variable: '" + VarStr + "'"); | |||
255 | ||||
256 | QueryRef Q; | |||
257 | switch (Var) { | |||
258 | case PQV_Output: | |||
259 | Q = parseSetOutputKind(); | |||
260 | break; | |||
261 | case PQV_BindRoot: | |||
262 | Q = parseSetBool(&QuerySession::BindRoot); | |||
263 | break; | |||
264 | case PQV_PrintMatcher: | |||
265 | Q = parseSetBool(&QuerySession::PrintMatcher); | |||
266 | break; | |||
267 | case PQV_Invalid: | |||
268 | llvm_unreachable("Invalid query kind")::llvm::llvm_unreachable_internal("Invalid query kind", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/tools/extra/clang-query/QueryParser.cpp" , 268); | |||
269 | } | |||
270 | ||||
271 | return endQuery(Q); | |||
272 | } | |||
273 | ||||
274 | case PQK_Unlet: { | |||
275 | StringRef Name = lexWord(); | |||
276 | ||||
277 | if (Name.empty()) | |||
278 | return new InvalidQuery("expected variable name"); | |||
279 | ||||
280 | return endQuery(new LetQuery(Name, VariantValue())); | |||
281 | } | |||
282 | ||||
283 | case PQK_Invalid: | |||
284 | return new InvalidQuery("unknown command: " + CommandStr); | |||
285 | } | |||
286 | ||||
287 | llvm_unreachable("Invalid query kind")::llvm::llvm_unreachable_internal("Invalid query kind", "/build/llvm-toolchain-snapshot-8~svn345461/tools/clang/tools/extra/clang-query/QueryParser.cpp" , 287); | |||
288 | } | |||
289 | ||||
290 | QueryRef QueryParser::parse(StringRef Line, const QuerySession &QS) { | |||
291 | return QueryParser(Line, QS).doParse(); | |||
292 | } | |||
293 | ||||
294 | std::vector<LineEditor::Completion> | |||
295 | QueryParser::complete(StringRef Line, size_t Pos, const QuerySession &QS) { | |||
296 | QueryParser P(Line, QS); | |||
297 | P.CompletionPos = Line.data() + Pos; | |||
298 | ||||
299 | P.doParse(); | |||
| ||||
300 | return P.Completions; | |||
301 | } | |||
302 | ||||
303 | } // namespace query | |||
304 | } // namespace clang |
1 | //==- llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer --*- C++ -*-==// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | // | |||
10 | // This file defines the RefCountedBase, ThreadSafeRefCountedBase, and | |||
11 | // IntrusiveRefCntPtr classes. | |||
12 | // | |||
13 | // IntrusiveRefCntPtr is a smart pointer to an object which maintains a | |||
14 | // reference count. (ThreadSafe)RefCountedBase is a mixin class that adds a | |||
15 | // refcount member variable and methods for updating the refcount. An object | |||
16 | // that inherits from (ThreadSafe)RefCountedBase deletes itself when its | |||
17 | // refcount hits zero. | |||
18 | // | |||
19 | // For example: | |||
20 | // | |||
21 | // class MyClass : public RefCountedBase<MyClass> {}; | |||
22 | // | |||
23 | // void foo() { | |||
24 | // // Constructing an IntrusiveRefCntPtr increases the pointee's refcount by | |||
25 | // // 1 (from 0 in this case). | |||
26 | // IntrusiveRefCntPtr<MyClass> Ptr1(new MyClass()); | |||
27 | // | |||
28 | // // Copying an IntrusiveRefCntPtr increases the pointee's refcount by 1. | |||
29 | // IntrusiveRefCntPtr<MyClass> Ptr2(Ptr1); | |||
30 | // | |||
31 | // // Constructing an IntrusiveRefCntPtr has no effect on the object's | |||
32 | // // refcount. After a move, the moved-from pointer is null. | |||
33 | // IntrusiveRefCntPtr<MyClass> Ptr3(std::move(Ptr1)); | |||
34 | // assert(Ptr1 == nullptr); | |||
35 | // | |||
36 | // // Clearing an IntrusiveRefCntPtr decreases the pointee's refcount by 1. | |||
37 | // Ptr2.reset(); | |||
38 | // | |||
39 | // // The object deletes itself when we return from the function, because | |||
40 | // // Ptr3's destructor decrements its refcount to 0. | |||
41 | // } | |||
42 | // | |||
43 | // You can use IntrusiveRefCntPtr with isa<T>(), dyn_cast<T>(), etc.: | |||
44 | // | |||
45 | // IntrusiveRefCntPtr<MyClass> Ptr(new MyClass()); | |||
46 | // OtherClass *Other = dyn_cast<OtherClass>(Ptr); // Ptr.get() not required | |||
47 | // | |||
48 | // IntrusiveRefCntPtr works with any class that | |||
49 | // | |||
50 | // - inherits from (ThreadSafe)RefCountedBase, | |||
51 | // - has Retain() and Release() methods, or | |||
52 | // - specializes IntrusiveRefCntPtrInfo. | |||
53 | // | |||
54 | //===----------------------------------------------------------------------===// | |||
55 | ||||
56 | #ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H | |||
57 | #define LLVM_ADT_INTRUSIVEREFCNTPTR_H | |||
58 | ||||
59 | #include <atomic> | |||
60 | #include <cassert> | |||
61 | #include <cstddef> | |||
62 | ||||
63 | namespace llvm { | |||
64 | ||||
65 | /// A CRTP mixin class that adds reference counting to a type. | |||
66 | /// | |||
67 | /// The lifetime of an object which inherits from RefCountedBase is managed by | |||
68 | /// calls to Release() and Retain(), which increment and decrement the object's | |||
69 | /// refcount, respectively. When a Release() call decrements the refcount to 0, | |||
70 | /// the object deletes itself. | |||
71 | template <class Derived> class RefCountedBase { | |||
72 | mutable unsigned RefCount = 0; | |||
73 | ||||
74 | public: | |||
75 | RefCountedBase() = default; | |||
76 | RefCountedBase(const RefCountedBase &) {} | |||
77 | ||||
78 | void Retain() const { ++RefCount; } | |||
79 | ||||
80 | void Release() const { | |||
81 | assert(RefCount > 0 && "Reference count is already zero.")((RefCount > 0 && "Reference count is already zero." ) ? static_cast<void> (0) : __assert_fail ("RefCount > 0 && \"Reference count is already zero.\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/IntrusiveRefCntPtr.h" , 81, __PRETTY_FUNCTION__)); | |||
82 | if (--RefCount == 0) | |||
83 | delete static_cast<const Derived *>(this); | |||
84 | } | |||
85 | }; | |||
86 | ||||
87 | /// A thread-safe version of \c RefCountedBase. | |||
88 | template <class Derived> class ThreadSafeRefCountedBase { | |||
89 | mutable std::atomic<int> RefCount; | |||
90 | ||||
91 | protected: | |||
92 | ThreadSafeRefCountedBase() : RefCount(0) {} | |||
93 | ||||
94 | public: | |||
95 | void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); } | |||
96 | ||||
97 | void Release() const { | |||
98 | int NewRefCount = RefCount.fetch_sub(1, std::memory_order_acq_rel) - 1; | |||
99 | assert(NewRefCount >= 0 && "Reference count was already zero.")((NewRefCount >= 0 && "Reference count was already zero." ) ? static_cast<void> (0) : __assert_fail ("NewRefCount >= 0 && \"Reference count was already zero.\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/IntrusiveRefCntPtr.h" , 99, __PRETTY_FUNCTION__)); | |||
100 | if (NewRefCount == 0) | |||
101 | delete static_cast<const Derived *>(this); | |||
102 | } | |||
103 | }; | |||
104 | ||||
105 | /// Class you can specialize to provide custom retain/release functionality for | |||
106 | /// a type. | |||
107 | /// | |||
108 | /// Usually specializing this class is not necessary, as IntrusiveRefCntPtr | |||
109 | /// works with any type which defines Retain() and Release() functions -- you | |||
110 | /// can define those functions yourself if RefCountedBase doesn't work for you. | |||
111 | /// | |||
112 | /// One case when you might want to specialize this type is if you have | |||
113 | /// - Foo.h defines type Foo and includes Bar.h, and | |||
114 | /// - Bar.h uses IntrusiveRefCntPtr<Foo> in inline functions. | |||
115 | /// | |||
116 | /// Because Foo.h includes Bar.h, Bar.h can't include Foo.h in order to pull in | |||
117 | /// the declaration of Foo. Without the declaration of Foo, normally Bar.h | |||
118 | /// wouldn't be able to use IntrusiveRefCntPtr<Foo>, which wants to call | |||
119 | /// T::Retain and T::Release. | |||
120 | /// | |||
121 | /// To resolve this, Bar.h could include a third header, FooFwd.h, which | |||
122 | /// forward-declares Foo and specializes IntrusiveRefCntPtrInfo<Foo>. Then | |||
123 | /// Bar.h could use IntrusiveRefCntPtr<Foo>, although it still couldn't call any | |||
124 | /// functions on Foo itself, because Foo would be an incomplete type. | |||
125 | template <typename T> struct IntrusiveRefCntPtrInfo { | |||
126 | static void retain(T *obj) { obj->Retain(); } | |||
127 | static void release(T *obj) { obj->Release(); } | |||
128 | }; | |||
129 | ||||
130 | /// A smart pointer to a reference-counted object that inherits from | |||
131 | /// RefCountedBase or ThreadSafeRefCountedBase. | |||
132 | /// | |||
133 | /// This class increments its pointee's reference count when it is created, and | |||
134 | /// decrements its refcount when it's destroyed (or is changed to point to a | |||
135 | /// different object). | |||
136 | template <typename T> class IntrusiveRefCntPtr { | |||
137 | T *Obj = nullptr; | |||
138 | ||||
139 | public: | |||
140 | using element_type = T; | |||
141 | ||||
142 | explicit IntrusiveRefCntPtr() = default; | |||
143 | IntrusiveRefCntPtr(T *obj) : Obj(obj) { retain(); } | |||
144 | IntrusiveRefCntPtr(const IntrusiveRefCntPtr &S) : Obj(S.Obj) { retain(); } | |||
145 | IntrusiveRefCntPtr(IntrusiveRefCntPtr &&S) : Obj(S.Obj) { S.Obj = nullptr; } | |||
146 | ||||
147 | template <class X> | |||
148 | IntrusiveRefCntPtr(IntrusiveRefCntPtr<X> &&S) : Obj(S.get()) { | |||
149 | S.Obj = nullptr; | |||
150 | } | |||
151 | ||||
152 | template <class X> | |||
153 | IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X> &S) : Obj(S.get()) { | |||
154 | retain(); | |||
155 | } | |||
156 | ||||
157 | ~IntrusiveRefCntPtr() { release(); } | |||
| ||||
158 | ||||
159 | IntrusiveRefCntPtr &operator=(IntrusiveRefCntPtr S) { | |||
160 | swap(S); | |||
161 | return *this; | |||
162 | } | |||
163 | ||||
164 | T &operator*() const { return *Obj; } | |||
165 | T *operator->() const { return Obj; } | |||
166 | T *get() const { return Obj; } | |||
167 | explicit operator bool() const { return Obj; } | |||
168 | ||||
169 | void swap(IntrusiveRefCntPtr &other) { | |||
170 | T *tmp = other.Obj; | |||
171 | other.Obj = Obj; | |||
172 | Obj = tmp; | |||
173 | } | |||
174 | ||||
175 | void reset() { | |||
176 | release(); | |||
177 | Obj = nullptr; | |||
178 | } | |||
179 | ||||
180 | void resetWithoutRelease() { Obj = nullptr; } | |||
181 | ||||
182 | private: | |||
183 | void retain() { | |||
184 | if (Obj) | |||
185 | IntrusiveRefCntPtrInfo<T>::retain(Obj); | |||
186 | } | |||
187 | ||||
188 | void release() { | |||
189 | if (Obj) | |||
190 | IntrusiveRefCntPtrInfo<T>::release(Obj); | |||
191 | } | |||
192 | ||||
193 | template <typename X> friend class IntrusiveRefCntPtr; | |||
194 | }; | |||
195 | ||||
196 | template <class T, class U> | |||
197 | inline bool operator==(const IntrusiveRefCntPtr<T> &A, | |||
198 | const IntrusiveRefCntPtr<U> &B) { | |||
199 | return A.get() == B.get(); | |||
200 | } | |||
201 | ||||
202 | template <class T, class U> | |||
203 | inline bool operator!=(const IntrusiveRefCntPtr<T> &A, | |||
204 | const IntrusiveRefCntPtr<U> &B) { | |||
205 | return A.get() != B.get(); | |||
206 | } | |||
207 | ||||
208 | template <class T, class U> | |||
209 | inline bool operator==(const IntrusiveRefCntPtr<T> &A, U *B) { | |||
210 | return A.get() == B; | |||
211 | } | |||
212 | ||||
213 | template <class T, class U> | |||
214 | inline bool operator!=(const IntrusiveRefCntPtr<T> &A, U *B) { | |||
215 | return A.get() != B; | |||
216 | } | |||
217 | ||||
218 | template <class T, class U> | |||
219 | inline bool operator==(T *A, const IntrusiveRefCntPtr<U> &B) { | |||
220 | return A == B.get(); | |||
221 | } | |||
222 | ||||
223 | template <class T, class U> | |||
224 | inline bool operator!=(T *A, const IntrusiveRefCntPtr<U> &B) { | |||
225 | return A != B.get(); | |||
226 | } | |||
227 | ||||
228 | template <class T> | |||
229 | bool operator==(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) { | |||
230 | return !B; | |||
231 | } | |||
232 | ||||
233 | template <class T> | |||
234 | bool operator==(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) { | |||
235 | return B == A; | |||
236 | } | |||
237 | ||||
238 | template <class T> | |||
239 | bool operator!=(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) { | |||
240 | return !(A == B); | |||
241 | } | |||
242 | ||||
243 | template <class T> | |||
244 | bool operator!=(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) { | |||
245 | return !(A == B); | |||
246 | } | |||
247 | ||||
248 | // Make IntrusiveRefCntPtr work with dyn_cast, isa, and the other idioms from | |||
249 | // Casting.h. | |||
250 | template <typename From> struct simplify_type; | |||
251 | ||||
252 | template <class T> struct simplify_type<IntrusiveRefCntPtr<T>> { | |||
253 | using SimpleType = T *; | |||
254 | ||||
255 | static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T> &Val) { | |||
256 | return Val.get(); | |||
257 | } | |||
258 | }; | |||
259 | ||||
260 | template <class T> struct simplify_type<const IntrusiveRefCntPtr<T>> { | |||
261 | using SimpleType = /*const*/ T *; | |||
262 | ||||
263 | static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T> &Val) { | |||
264 | return Val.get(); | |||
265 | } | |||
266 | }; | |||
267 | ||||
268 | } // end namespace llvm | |||
269 | ||||
270 | #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H |