Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 20 | namespace clang |
| 21 | { |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 22 | /// 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 Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 26 | template <ASTDestroyer Destroyer, unsigned N> |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 27 | 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 Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 53 | |
| 54 | Action &getActions() const { return Actions; } |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 55 | }; |
| 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 Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 62 | |
| 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 Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | #endif |