Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 1 | //===--- DeclGroup.cpp - Classes for representing groups of Decls -*- 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 the DeclGroup, DeclGroupRef, and OwningDeclGroup classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclGroup.h" |
| 15 | #include "clang/AST/Decl.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "llvm/Support/Allocator.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | DeclGroup* DeclGroup::Create(ASTContext& C, unsigned numdecls, Decl** decls) { |
| 22 | unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * numdecls; |
| 23 | unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment; |
Zhongxing Xu | c61255f | 2008-09-27 03:27:29 +0000 | [diff] [blame] | 24 | void* mem = C.getAllocator().Allocate(size, alignment); |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 25 | new (mem) DeclGroup(numdecls, decls); |
| 26 | return static_cast<DeclGroup*>(mem); |
| 27 | } |
| 28 | |
| 29 | DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) { |
| 30 | assert (numdecls > 0); |
| 31 | assert (decls); |
| 32 | memcpy(this+1, decls, numdecls * sizeof(*decls)); |
| 33 | } |
| 34 | |
| 35 | void DeclGroup::Destroy(ASTContext& C) { |
| 36 | Decl** Decls = (Decl**) this + 1; |
| 37 | |
| 38 | for (unsigned i = 0; i < NumDecls; ++i) |
| 39 | Decls[i]->Destroy(C); |
| 40 | |
| 41 | this->~DeclGroup(); |
| 42 | C.getAllocator().Deallocate((void*) this); |
| 43 | } |
| 44 | |
| 45 | DeclGroupOwningRef::~DeclGroupOwningRef() { |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame^] | 46 | assert (D == 0 && "Destroy method not called."); |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void DeclGroupOwningRef::Destroy(ASTContext& C) { |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame^] | 50 | if (!D) |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 51 | return; |
| 52 | |
| 53 | if (getKind() == DeclKind) |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame^] | 54 | D->Destroy(C); |
| 55 | else |
| 56 | reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) & |
| 57 | ~Mask)->Destroy(C); |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame^] | 59 | D = 0; |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 60 | } |