blob: a65caedeb348f797157dcd77b80ec0ca2c3c4af2 [file] [log] [blame]
Sebastian Redla55e52c2008-11-25 22:21:31 +00001//===--- AstGuard.h - Parser Ownership Tracking Utilities -------*- 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 RAII objects for managing ExprTy* and StmtTy*.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_PARSE_ASTGUARD_H
15#define LLVM_CLANG_PARSE_ASTGUARD_H
16
17#include "clang/Parse/Action.h"
18#include "llvm/ADT/SmallVector.h"
19
20namespace clang
21{
Sebastian Redla55e52c2008-11-25 22:21:31 +000022 /// RAII SmallVector wrapper that holds Action::ExprTy* and similar,
23 /// automatically freeing them on destruction unless it's been disowned.
24 /// Instantiated for statements and expressions (Action::DeleteStmt and
25 /// Action::DeleteExpr).
Sebastian Redla60528c2008-12-21 12:04:03 +000026 template <ASTDestroyer Destroyer, unsigned N>
Sebastian Redla55e52c2008-11-25 22:21:31 +000027 class ASTVector : public llvm::SmallVector<void*, N> {
28 private:
29 Action &Actions;
30 bool Owns;
31
32 void destroy() {
33 if (Owns) {
34 while (!this->empty()) {
35 (Actions.*Destroyer)(this->back());
36 this->pop_back();
37 }
38 }
39 }
40
41 ASTVector(const ASTVector&); // DO NOT IMPLEMENT
42 // Reference member prevents copy assignment.
43
44 public:
45 ASTVector(Action &actions) : Actions(actions), Owns(true) {}
46
47 ~ASTVector() { destroy(); }
48
49 void **take() {
50 Owns = false;
51 return &(*this)[0];
52 }
Sebastian Redla60528c2008-12-21 12:04:03 +000053
54 Action &getActions() const { return Actions; }
Sebastian Redla55e52c2008-11-25 22:21:31 +000055 };
56
57 /// A SmallVector of statements, with stack size 32 (as that is the only one
58 /// used.)
59 typedef ASTVector<&Action::DeleteStmt, 32> StmtVector;
60 /// A SmallVector of expressions, with stack size 12 (the maximum used.)
61 typedef ASTVector<&Action::DeleteExpr, 12> ExprVector;
Sebastian Redla60528c2008-12-21 12:04:03 +000062
63 template <ASTDestroyer Destroyer, unsigned N> inline
64 ASTMultiPtr<Destroyer> move_convert(ASTVector<Destroyer, N> &vec) {
65 return ASTMultiPtr<Destroyer>(vec.getActions(), vec.take(), vec.size());
66 }
Sebastian Redla55e52c2008-11-25 22:21:31 +000067}
68
69#endif