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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
//===--- Ownership.h - Parser Ownership Helpers -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains classes for managing ownership of Stmt and Expr nodes.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_PARSE_OWNERSHIP_H
#define LLVM_CLANG_PARSE_OWNERSHIP_H
namespace clang
{
// Basic
class DiagnosticBuilder;
/// ActionBase - A small part split from Action because of the horrible
/// definition order dependencies between Action and the smart pointers.
class ActionBase {
public:
/// Out-of-line virtual destructor to provide home for this class.
virtual ~ActionBase();
// Types - Though these don't actually enforce strong typing, they document
// what types are required to be identical for the actions.
typedef void ExprTy;
typedef void StmtTy;
/// ActionResult - This structure is used while parsing/acting on
/// expressions, stmts, etc. It encapsulates both the object returned by
/// the action, plus a sense of whether or not it is valid.
template<unsigned UID>
struct ActionResult {
void *Val;
bool isInvalid;
ActionResult(bool Invalid = false) : Val(0), isInvalid(Invalid) {}
template<typename ActualExprTy>
ActionResult(ActualExprTy *val) : Val(val), isInvalid(false) {}
ActionResult(const DiagnosticBuilder &) : Val(0), isInvalid(true) {}
const ActionResult &operator=(void *RHS) {
Val = RHS;
isInvalid = false;
return *this;
}
};
/// Deletion callbacks - Since the parser doesn't know the concrete types of
/// the AST nodes being generated, it must do callbacks to delete objects
/// when recovering from errors. These are in ActionBase because the smart
/// pointers need access to them.
virtual void DeleteExpr(ExprTy *E) {}
virtual void DeleteStmt(StmtTy *E) {}
};
/// ASTDestroyer - The type of an AST node destruction function pointer.
typedef void (ActionBase::*ASTDestroyer)(void *);
/// For the transition phase: translate from an ASTDestroyer to its
/// ActionResult UID.
template <ASTDestroyer Destroyer> struct DestroyerToUID;
template <> struct DestroyerToUID<&ActionBase::DeleteExpr> {
static const unsigned UID = 0;
};
template <> struct DestroyerToUID<&ActionBase::DeleteStmt> {
static const unsigned UID = 1;
};
/// ASTOwningResult - A moveable smart pointer for AST nodes that also
/// has an extra flag to indicate an additional success status.
template <ASTDestroyer Destroyer> class ASTOwningResult;
/// ASTOwningPtr - A moveable smart pointer for AST nodes.
template <ASTDestroyer Destroyer> class ASTOwningPtr;
/// ASTMultiPtr - A moveable smart pointer to multiple AST nodes. Only owns
/// the individual pointers, not the array holding them.
template <ASTDestroyer Destroyer> class ASTMultiPtr;
/// Move emulation helper for ASTOwningResult
template <ASTDestroyer Destroyer>
class ASTResultMover
{
ASTOwningResult<Destroyer> &Moved;
public:
ASTResultMover(ASTOwningResult<Destroyer> &moved) : Moved(moved) {}
ASTOwningResult<Destroyer> * operator ->() { return &Moved; }
// For the transition phase.
operator void*();
// For the transition phase.
operator ActionBase::ActionResult<DestroyerToUID<Destroyer>::UID>();
};
/// Move emulation helper for ASTOwningPtr
template <ASTDestroyer Destroyer>
class ASTPtrMover
{
ASTOwningPtr<Destroyer> &Moved;
public:
ASTPtrMover(ASTOwningPtr<Destroyer> &moved) : Moved(moved) {}
ASTOwningPtr<Destroyer> * operator ->() { return &Moved; }
operator void*();
};
/// Move emulation helper for ASTMultiPtr
template <ASTDestroyer Destroyer>
class ASTMultiMover
{
ASTMultiPtr<Destroyer> &Moved;
public:
ASTMultiMover(ASTMultiPtr<Destroyer> &moved) : Moved(moved) {}
/// Reset the moved object's internal structures.
void release();
};
template <ASTDestroyer Destroyer>
class ASTOwningResult
{
ActionBase *Actions;
void *Node;
bool Invalid;
friend class ASTResultMover<Destroyer>;
friend class ASTOwningPtr<Destroyer>;
ASTOwningResult(const ASTOwningResult&); // DO NOT IMPLEMENT
ASTOwningResult& operator =(const ASTOwningResult&); // DO NOT IMPLEMENT
void destroy() {
if (Node) {
assert(Actions && "Owning pointer without Action owns node.");
(Actions->*Destroyer)(Node);
}
}
void * take() {
if (Invalid)
return 0;
return Node;
}
public:
typedef ActionBase::ActionResult<DestroyerToUID<Destroyer>::UID> DumbResult;
// For convenience and compatibility.
ASTOwningResult(bool invalid = false)
: Actions(0), Node(0), Invalid(invalid) {}
// Same
ASTOwningResult(const DiagnosticBuilder &)
: Actions(0), Node(0), Invalid(true) {}
explicit ASTOwningResult(ActionBase &actions, bool invalid = false)
: Actions(&actions), Node(0), Invalid(invalid) {}
ASTOwningResult(ActionBase &actions, void *node)
: Actions(&actions), Node(node), Invalid(false) {}
ASTOwningResult(ActionBase &actions, const DumbResult &res)
: Actions(&actions), Node(res.Val), Invalid(res.isInvalid) {}
/// Move from another owning result
ASTOwningResult(ASTResultMover<Destroyer> mover)
: Actions(mover->Actions), Node(mover->take()), Invalid(mover->Invalid) {}
/// Move from an owning pointer
ASTOwningResult(ASTPtrMover<Destroyer> mover);
/// Move assignment from another owning result
ASTOwningResult & operator =(ASTResultMover<Destroyer> mover) {
Actions = mover->Actions;
Node = mover->take();
Invalid = mover->Invalid;
return *this;
}
/// Move assignment from an owning ptr
ASTOwningResult & operator =(ASTPtrMover<Destroyer> mover);
/// Assignment from a raw pointer. Takes ownership - beware!
ASTOwningResult & operator =(void *raw)
{
assert((!raw || Actions) &&
"Cannot have raw assignment when there's no Action");
Node = raw;
Invalid = false;
return *this;
}
/// Assignment from an ActionResult. Takes ownership - beware!
ASTOwningResult & operator =(const DumbResult &res) {
assert((!res.Val || Actions) &&
"Cannot assign from ActionResult when there's no Action");
Node = res.Val;
Invalid = res.isInvalid;
return *this;
}
/// Access to the raw pointer.
void * get() const { return Node; }
bool isInvalid() const { return Invalid; }
/// Does this point to a usable AST node? To be usable, the node must be
/// valid and non-null.
bool isUsable() const { return !Invalid && Node; }
/// Move hook
ASTResultMover<Destroyer> move() {
return ASTResultMover<Destroyer>(*this);
}
};
template <ASTDestroyer Destroyer>
class ASTOwningPtr
{
ActionBase *Actions;
void *Node;
friend class ASTPtrMover<Destroyer>;
friend class ASTOwningResult<Destroyer>;
ASTOwningPtr(const ASTOwningPtr&); // DO NOT IMPLEMENT
ASTOwningPtr& operator =(const ASTOwningPtr&); // DO NOT IMPLEMENT
void destroy() {
if (Node) {
assert(Actions && "Owning pointer without Action owns node.");
(Actions->*Destroyer)(Node);
}
}
public:
explicit ASTOwningPtr(ActionBase &actions)
: Actions(&actions), Node(0) {}
ASTOwningPtr(ActionBase &actions, void *node)
: Actions(&actions), Node(node) {}
/// Move from another owning pointer
ASTOwningPtr(ASTPtrMover<Destroyer> mover)
: Actions(mover->Actions), Node(mover->take()) {}
/// Move from an owning result
ASTOwningPtr(ASTResultMover<Destroyer> mover);
/// Move assignment from another owning pointer
ASTOwningPtr & operator =(ASTPtrMover<Destroyer> mover) {
Actions = mover->Actions;
Node
|