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