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