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: |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 29 | #if !defined(DISABLE_SMART_POINTERS) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 30 | Action &Actions; |
| 31 | bool Owns; |
| 32 | |
| 33 | void destroy() { |
| 34 | if (Owns) { |
| 35 | while (!this->empty()) { |
| 36 | (Actions.*Destroyer)(this->back()); |
| 37 | this->pop_back(); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | ASTVector(const ASTVector&); // DO NOT IMPLEMENT |
| 43 | // Reference member prevents copy assignment. |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 44 | #endif |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 45 | |
| 46 | public: |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 47 | #if !defined(DISABLE_SMART_POINTERS) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 48 | ASTVector(Action &actions) : Actions(actions), Owns(true) {} |
| 49 | |
| 50 | ~ASTVector() { destroy(); } |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 51 | #else |
| 52 | ASTVector(Action &) {} |
| 53 | #endif |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 54 | |
| 55 | void **take() { |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 56 | #if !defined(DISABLE_SMART_POINTERS) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 57 | Owns = false; |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 58 | #endif |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 59 | return &(*this)[0]; |
| 60 | } |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 61 | |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 62 | #if !defined(DISABLE_SMART_POINTERS) |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 63 | Action &getActions() const { return Actions; } |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 64 | #endif |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /// A SmallVector of statements, with stack size 32 (as that is the only one |
| 68 | /// used.) |
| 69 | typedef ASTVector<&Action::DeleteStmt, 32> StmtVector; |
| 70 | /// A SmallVector of expressions, with stack size 12 (the maximum used.) |
| 71 | typedef ASTVector<&Action::DeleteExpr, 12> ExprVector; |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 72 | |
| 73 | template <ASTDestroyer Destroyer, unsigned N> inline |
Sebastian Redl | f512e82 | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 74 | ASTMultiPtr<Destroyer> move_arg(ASTVector<Destroyer, N> &vec) { |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 75 | #if !defined(DISABLE_SMART_POINTERS) |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 76 | return ASTMultiPtr<Destroyer>(vec.getActions(), vec.take(), vec.size()); |
Sebastian Redl | 07c487e | 2009-01-22 17:31:11 +0000 | [diff] [blame] | 77 | #else |
| 78 | return ASTMultiPtr<Destroyer>(vec.take(), vec.size()); |
| 79 | #endif |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 80 | } |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #endif |