| 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 | // | 
| Douglas Gregor | 9653db7 | 2009-02-13 19:06:18 +0000 | [diff] [blame] | 10 | //  This file defines the DeclGroup and DeclGroupRef classes. | 
| Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 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" | 
| Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/Serialize.h" | 
 | 19 | #include "llvm/Bitcode/Deserialize.h" | 
| Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 20 |  | 
 | 21 | using namespace clang; | 
 | 22 |  | 
 | 23 | DeclGroup* DeclGroup::Create(ASTContext& C, unsigned numdecls, Decl** decls) { | 
| Ted Kremenek | 401adfa | 2008-10-07 23:06:01 +0000 | [diff] [blame] | 24 |   assert (numdecls > 0); | 
| Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 25 |   unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * numdecls; | 
 | 26 |   unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;   | 
| Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 27 |   void* mem = C.Allocate(size, alignment); | 
| Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 28 |   new (mem) DeclGroup(numdecls, decls); | 
 | 29 |   return static_cast<DeclGroup*>(mem); | 
 | 30 | } | 
 | 31 |  | 
| Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 32 | /// Emit - Serialize a DeclGroup to Bitcode. | 
 | 33 | void DeclGroup::Emit(llvm::Serializer& S) const { | 
 | 34 |   S.EmitInt(NumDecls); | 
 | 35 |   S.BatchEmitOwnedPtrs(NumDecls, &(*this)[0]); | 
 | 36 | } | 
 | 37 |  | 
 | 38 | /// Read - Deserialize a DeclGroup from Bitcode. | 
| Douglas Gregor | 9653db7 | 2009-02-13 19:06:18 +0000 | [diff] [blame] | 39 | DeclGroup* DeclGroup::Read(llvm::Deserializer& D, ASTContext& C) { | 
| Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 40 |   unsigned NumDecls = (unsigned) D.ReadInt(); | 
 | 41 |   unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls; | 
 | 42 |   unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;   | 
| Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 43 |   DeclGroup* DG = (DeclGroup*) C.Allocate(size, alignment); | 
| Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 44 |   new (DG) DeclGroup(); | 
 | 45 |   DG->NumDecls = NumDecls; | 
 | 46 |   D.BatchReadOwnedPtrs(NumDecls, &(*DG)[0], C); | 
 | 47 |   return DG; | 
 | 48 | } | 
 | 49 |    | 
| Ted Kremenek | 401adfa | 2008-10-07 23:06:01 +0000 | [diff] [blame] | 50 | DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) { | 
| Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 51 |   assert (numdecls > 0); | 
 | 52 |   assert (decls); | 
 | 53 |   memcpy(this+1, decls, numdecls * sizeof(*decls)); | 
 | 54 | } | 
 | 55 |  | 
 | 56 | void DeclGroup::Destroy(ASTContext& C) { | 
| Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 57 |   this->~DeclGroup(); | 
| Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 58 |   C.Deallocate((void*) this); | 
| Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 59 | } | 
 | 60 |  | 
| Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 61 | void DeclGroupRef::Emit(llvm::Serializer& S) const { | 
 | 62 |   if (getKind() == DeclKind) { | 
 | 63 |     S.EmitBool(false); | 
 | 64 |     S.EmitPtr(D); | 
 | 65 |   } | 
 | 66 |   else { | 
 | 67 |     S.EmitBool(true); | 
 | 68 |     S.EmitPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D)  | 
 | 69 |                                            & ~Mask));         | 
 | 70 |   } | 
 | 71 | } | 
 | 72 |  | 
 | 73 | DeclGroupRef DeclGroupRef::ReadVal(llvm::Deserializer& D) { | 
 | 74 |   if (D.ReadBool()) | 
 | 75 |     return DeclGroupRef(D.ReadPtr<Decl>()); | 
 | 76 |    | 
 | 77 |   return DeclGroupRef(D.ReadPtr<DeclGroup>()); | 
 | 78 | } |