blob: bd79fafc8f338af7799a58009ae75d21abf087ce [file] [log] [blame]
Ted Kremenekd17062c2008-09-25 17:13:40 +00001//===--- 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 Kremenekc7b07c12008-10-06 23:49:24 +000018#include "llvm/Bitcode/Serialize.h"
19#include "llvm/Bitcode/Deserialize.h"
Ted Kremenekd17062c2008-09-25 17:13:40 +000020
21using namespace clang;
22
23DeclGroup* DeclGroup::Create(ASTContext& C, unsigned numdecls, Decl** decls) {
24 unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * numdecls;
25 unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;
Zhongxing Xuc61255f2008-09-27 03:27:29 +000026 void* mem = C.getAllocator().Allocate(size, alignment);
Ted Kremenekd17062c2008-09-25 17:13:40 +000027 new (mem) DeclGroup(numdecls, decls);
28 return static_cast<DeclGroup*>(mem);
29}
30
Ted Kremenekc7b07c12008-10-06 23:49:24 +000031/// Emit - Serialize a DeclGroup to Bitcode.
32void 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.
38DeclGroup* DeclGroup::Create(llvm::Deserializer& D, ASTContext& C) {
39 unsigned NumDecls = (unsigned) D.ReadInt();
40 unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
41 unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;
42 DeclGroup* DG = (DeclGroup*) C.getAllocator().Allocate(size, alignment);
43 new (DG) DeclGroup();
44 DG->NumDecls = NumDecls;
45 D.BatchReadOwnedPtrs(NumDecls, &(*DG)[0], C);
46 return DG;
47}
48
Ted Kremenekd17062c2008-09-25 17:13:40 +000049DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) {
50 assert (numdecls > 0);
51 assert (decls);
52 memcpy(this+1, decls, numdecls * sizeof(*decls));
53}
54
55void DeclGroup::Destroy(ASTContext& C) {
56 Decl** Decls = (Decl**) this + 1;
57
58 for (unsigned i = 0; i < NumDecls; ++i)
59 Decls[i]->Destroy(C);
60
61 this->~DeclGroup();
62 C.getAllocator().Deallocate((void*) this);
63}
64
65DeclGroupOwningRef::~DeclGroupOwningRef() {
Ted Kremeneke285a952008-10-06 22:17:16 +000066 assert (D == 0 && "Destroy method not called.");
Ted Kremenekd17062c2008-09-25 17:13:40 +000067}
68
69void DeclGroupOwningRef::Destroy(ASTContext& C) {
Ted Kremeneke285a952008-10-06 22:17:16 +000070 if (!D)
Ted Kremenekd17062c2008-09-25 17:13:40 +000071 return;
72
73 if (getKind() == DeclKind)
Ted Kremeneke285a952008-10-06 22:17:16 +000074 D->Destroy(C);
75 else
76 reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) &
77 ~Mask)->Destroy(C);
Ted Kremenekd17062c2008-09-25 17:13:40 +000078
Ted Kremeneke285a952008-10-06 22:17:16 +000079 D = 0;
Ted Kremenekd17062c2008-09-25 17:13:40 +000080}
Ted Kremenekc7b07c12008-10-06 23:49:24 +000081
82void DeclGroupRef::Emit(llvm::Serializer& S) const {
83 if (getKind() == DeclKind) {
84 S.EmitBool(false);
85 S.EmitPtr(D);
86 }
87 else {
88 S.EmitBool(true);
89 S.EmitPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D)
90 & ~Mask));
91 }
92}
93
94DeclGroupRef DeclGroupRef::ReadVal(llvm::Deserializer& D) {
95 if (D.ReadBool())
96 return DeclGroupRef(D.ReadPtr<Decl>());
97
98 return DeclGroupRef(D.ReadPtr<DeclGroup>());
99}
100
101void DeclGroupOwningRef::Emit(llvm::Serializer& S) const {
102 if (getKind() == DeclKind) {
103 S.EmitBool(false);
104 S.EmitOwnedPtr(D);
105 }
106 else {
107 S.EmitBool(true);
108 S.EmitOwnedPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D)
109 & ~Mask));
110 }
111}
112
113DeclGroupOwningRef DeclGroupOwningRef::ReadVal(llvm::Deserializer& D,
114 ASTContext& C) {
115 if (D.ReadBool()) {
116 DeclGroupOwningRef DG(D.ReadOwnedPtr<Decl>(C));
117 return DG;
118 }
119
120 DeclGroupOwningRef DG(D.ReadOwnedPtr<DeclGroup>(C));
121 return DG;
122}