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" |
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; |
Zhongxing Xu | c61255f | 2008-09-27 03:27:29 +0000 | [diff] [blame] | 27 | void* mem = C.getAllocator().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. |
| 39 | DeclGroup* DeclGroup::Create(llvm::Deserializer& D, ASTContext& C) { |
| 40 | unsigned NumDecls = (unsigned) D.ReadInt(); |
| 41 | unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls; |
| 42 | unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment; |
| 43 | DeclGroup* DG = (DeclGroup*) C.getAllocator().Allocate(size, alignment); |
| 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) { |
| 57 | Decl** Decls = (Decl**) this + 1; |
| 58 | |
| 59 | for (unsigned i = 0; i < NumDecls; ++i) |
| 60 | Decls[i]->Destroy(C); |
| 61 | |
| 62 | this->~DeclGroup(); |
| 63 | C.getAllocator().Deallocate((void*) this); |
| 64 | } |
| 65 | |
| 66 | DeclGroupOwningRef::~DeclGroupOwningRef() { |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame] | 67 | assert (D == 0 && "Destroy method not called."); |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void DeclGroupOwningRef::Destroy(ASTContext& C) { |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame] | 71 | if (!D) |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 72 | return; |
| 73 | |
| 74 | if (getKind() == DeclKind) |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame] | 75 | D->Destroy(C); |
| 76 | else |
| 77 | reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) & |
| 78 | ~Mask)->Destroy(C); |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | e285a95 | 2008-10-06 22:17:16 +0000 | [diff] [blame] | 80 | D = 0; |
Ted Kremenek | d17062c | 2008-09-25 17:13:40 +0000 | [diff] [blame] | 81 | } |
Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 82 | |
| 83 | void DeclGroupRef::Emit(llvm::Serializer& S) const { |
| 84 | if (getKind() == DeclKind) { |
| 85 | S.EmitBool(false); |
| 86 | S.EmitPtr(D); |
| 87 | } |
| 88 | else { |
| 89 | S.EmitBool(true); |
| 90 | S.EmitPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) |
| 91 | & ~Mask)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | DeclGroupRef DeclGroupRef::ReadVal(llvm::Deserializer& D) { |
| 96 | if (D.ReadBool()) |
| 97 | return DeclGroupRef(D.ReadPtr<Decl>()); |
| 98 | |
| 99 | return DeclGroupRef(D.ReadPtr<DeclGroup>()); |
| 100 | } |
| 101 | |
| 102 | void DeclGroupOwningRef::Emit(llvm::Serializer& S) const { |
| 103 | if (getKind() == DeclKind) { |
| 104 | S.EmitBool(false); |
| 105 | S.EmitOwnedPtr(D); |
| 106 | } |
| 107 | else { |
| 108 | S.EmitBool(true); |
| 109 | S.EmitOwnedPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) |
| 110 | & ~Mask)); |
| 111 | } |
| 112 | } |
| 113 | |
Ted Kremenek | 401adfa | 2008-10-07 23:06:01 +0000 | [diff] [blame^] | 114 | DeclGroupOwningRef& DeclGroupOwningRef::Read(llvm::Deserializer& Dezr, |
| 115 | ASTContext& C) { |
| 116 | |
| 117 | if (!Dezr.ReadBool()) |
| 118 | D = Dezr.ReadOwnedPtr<Decl>(C); |
| 119 | else { |
| 120 | uintptr_t x = reinterpret_cast<uintptr_t>(Dezr.ReadOwnedPtr<DeclGroup>(C)); |
| 121 | D = reinterpret_cast<Decl*>(x | DeclGroupKind); |
Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 122 | } |
Ted Kremenek | 401adfa | 2008-10-07 23:06:01 +0000 | [diff] [blame^] | 123 | |
| 124 | return *this; |
Ted Kremenek | c7b07c1 | 2008-10-06 23:49:24 +0000 | [diff] [blame] | 125 | } |