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
|
//===--- AstGuard.h - Parser Ownership Tracking Utilities -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines RAII objects for managing ExprTy* and StmtTy*.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_PARSE_ASTGUARD_H
#define LLVM_CLANG_PARSE_ASTGUARD_H
#include "clang/Parse/Action.h"
#include "llvm/ADT/SmallVector.h"
namespace clang
{
/// RAII guard for freeing StmtTys and ExprTys on early exit in the parser.
/// Instantiated for statements and expressions (Action::DeleteStmt and
/// Action::DeleteExpr).
template <void (Action::*Destroyer)(void*)>
class ASTGuard {
Action &Actions;
void *Node;
void destroy() {
if (Node)
(Actions.*Destroyer)(Node);
}
ASTGuard(const ASTGuard&); // DO NOT IMPLEMENT
// Reference member prevents copy assignment.
public:
explicit ASTGuard(Action &actions) : Actions(actions), Node(0) {}
ASTGuard(Action &actions, void *node)
: Actions(actions), Node(node) {}
template <unsigned N>
ASTGuard(Action &actions, const Action::ActionResult<N> &res)
: Actions(actions), Node(res.Val) {}
~ASTGuard() { destroy(); }
void reset(void *element) {
destroy();
Node = element;
}
template <unsigned N>
void reset(const Action::ActionResult<N> &res) {
reset(res.Val);
}
void *take() {
void *Temp = Node;
Node = 0;
return Temp;
}
void *get() const { return Node; }
};
typedef ASTGuard<&Action::DeleteStmt> StmtGuard;
typedef ASTGuard<&Action::DeleteExpr> ExprGuard;
/// RAII SmallVector wrapper that holds Action::ExprTy* and similar,
/// automatically freeing them on destruction unless it's been disowned.
/// Instantiated for statements and expressions (Action::DeleteStmt and
/// Action::DeleteExpr).
template <void (Action::*Destroyer)(void*), unsigned N>
class ASTVector : public llvm::SmallVector<void*, N> {
private:
Action &Actions;
bool Owns;
void destroy() {
if (Owns) {
while (!this->empty()) {
(Actions.*Destroyer)(this->back());
this->pop_back();
}
}
}
ASTVector(const ASTVector&); // DO NOT IMPLEMENT
// Reference member prevents copy assignment.
public:
ASTVector(Action &actions) : Actions(actions), Owns(true) {}
~ASTVector() { destroy(); }
void **take() {
Owns = false;
return &(*this)[0];
}
};
/// A SmallVector of statements, with stack size 32 (as that is the only one
/// used.)
typedef ASTVector<&Action::DeleteStmt, 32> StmtVector;
/// A SmallVector of expressions, with stack size 12 (the maximum used.)
typedef ASTVector<&Action::DeleteExpr, 12> ExprVector;
}
#endif
|