blob: 602fea51dc1039ef8eca33e0379fc3956f4cd318 [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:
Sebastian Redl07c487e2009-01-22 17:31:11 +000029#if !defined(DISABLE_SMART_POINTERS)
Sebastian Redla55e52c2008-11-25 22:21:31 +000030 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 Redl07c487e2009-01-22 17:31:11 +000044#endif
Sebastian Redla55e52c2008-11-25 22:21:31 +000045
46 public:
Sebastian Redl07c487e2009-01-22 17:31:11 +000047#if !defined(DISABLE_SMART_POINTERS)
Sebastian Redla55e52c2008-11-25 22:21:31 +000048 ASTVector(Action &actions) : Actions(actions), Owns(true) {}
49
50 ~ASTVector() { destroy(); }
Sebastian Redl07c487e2009-01-22 17:31:11 +000051#else
52 ASTVector(Action &) {}
53#endif
Sebastian Redla55e52c2008-11-25 22:21:31 +000054
55 void **take() {
Sebastian Redl07c487e2009-01-22 17:31:11 +000056#if !defined(DISABLE_SMART_POINTERS)
Sebastian Redla55e52c2008-11-25 22:21:31 +000057 Owns = false;
Sebastian Redl07c487e2009-01-22 17:31:11 +000058#endif
Sebastian Redla55e52c2008-11-25 22:21:31 +000059 return &(*this)[0];
60 }
Sebastian Redla60528c2008-12-21 12:04:03 +000061
Sebastian Redl07c487e2009-01-22 17:31:11 +000062#if !defined(DISABLE_SMART_POINTERS)
Sebastian Redla60528c2008-12-21 12:04:03 +000063 Action &getActions() const { return Actions; }
Sebastian Redl07c487e2009-01-22 17:31:11 +000064#endif
Sebastian Redla55e52c2008-11-25 22:21:31 +000065 };
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 Redla60528c2008-12-21 12:04:03 +000072
73 template <ASTDestroyer Destroyer, unsigned N> inline
Sebastian Redlf512e822009-01-18 18:03:53 +000074 ASTMultiPtr<Destroyer> move_arg(ASTVector<Destroyer, N> &vec) {
Sebastian Redl07c487e2009-01-22 17:31:11 +000075#if !defined(DISABLE_SMART_POINTERS)
Sebastian Redla60528c2008-12-21 12:04:03 +000076 return ASTMultiPtr<Destroyer>(vec.getActions(), vec.take(), vec.size());
Sebastian Redl07c487e2009-01-22 17:31:11 +000077#else
78 return ASTMultiPtr<Destroyer>(vec.take(), vec.size());
79#endif
Sebastian Redla60528c2008-12-21 12:04:03 +000080 }
Sebastian Redla55e52c2008-11-25 22:21:31 +000081}
82
83#endif