blob: e7f84e1f46c8fd1fcba78263e95a087a11104e39 [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) {
Ted Kremenek401adfa2008-10-07 23:06:01 +000024 assert (numdecls > 0);
Ted Kremenekd17062c2008-09-25 17:13:40 +000025 unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * numdecls;
26 unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;
Steve Naroffc0ac4922009-01-27 23:20:32 +000027 void* mem = C.Allocate(size, alignment);
Ted Kremenekd17062c2008-09-25 17:13:40 +000028 new (mem) DeclGroup(numdecls, decls);
29 return static_cast<DeclGroup*>(mem);
30}
31
Ted Kremenekc7b07c12008-10-06 23:49:24 +000032/// Emit - Serialize a DeclGroup to Bitcode.
33void 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.
39DeclGroup* 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;
Steve Naroffc0ac4922009-01-27 23:20:32 +000043 DeclGroup* DG = (DeclGroup*) C.Allocate(size, alignment);
Ted Kremenekc7b07c12008-10-06 23:49:24 +000044 new (DG) DeclGroup();
45 DG->NumDecls = NumDecls;
46 D.BatchReadOwnedPtrs(NumDecls, &(*DG)[0], C);
47 return DG;
48}
49
Ted Kremenek401adfa2008-10-07 23:06:01 +000050DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) {
Ted Kremenekd17062c2008-09-25 17:13:40 +000051 assert (numdecls > 0);
52 assert (decls);
53 memcpy(this+1, decls, numdecls * sizeof(*decls));
54}
55
56void DeclGroup::Destroy(ASTContext& C) {
Ted Kremenek990601b2008-10-18 19:20:54 +000057 Decl** Decls = (Decl**) (this + 1);
Ted Kremenekd17062c2008-09-25 17:13:40 +000058
59 for (unsigned i = 0; i < NumDecls; ++i)
60 Decls[i]->Destroy(C);
61
62 this->~DeclGroup();
Steve Naroff3e970492009-01-27 21:25:57 +000063 C.Deallocate((void*) this);
Ted Kremenekd17062c2008-09-25 17:13:40 +000064}
65
66DeclGroupOwningRef::~DeclGroupOwningRef() {
Ted Kremeneke285a952008-10-06 22:17:16 +000067 assert (D == 0 && "Destroy method not called.");
Ted Kremenekd17062c2008-09-25 17:13:40 +000068}
69
70void DeclGroupOwningRef::Destroy(ASTContext& C) {
Ted Kremeneke285a952008-10-06 22:17:16 +000071 if (!D)
Ted Kremenekd17062c2008-09-25 17:13:40 +000072 return;
73
74 if (getKind() == DeclKind)
Ted Kremeneke285a952008-10-06 22:17:16 +000075 D->Destroy(C);
76 else
77 reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) &
78 ~Mask)->Destroy(C);
Ted Kremenekd17062c2008-09-25 17:13:40 +000079
Ted Kremeneke285a952008-10-06 22:17:16 +000080 D = 0;
Ted Kremenekd17062c2008-09-25 17:13:40 +000081}
Ted Kremenekc7b07c12008-10-06 23:49:24 +000082
83void 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
95DeclGroupRef DeclGroupRef::ReadVal(llvm::Deserializer& D) {
96 if (D.ReadBool())
97 return DeclGroupRef(D.ReadPtr<Decl>());
98
99 return DeclGroupRef(D.ReadPtr<DeclGroup>());
100}
101
102void 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 Kremenek401adfa2008-10-07 23:06:01 +0000114DeclGroupOwningRef& 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 Kremenekc7b07c12008-10-06 23:49:24 +0000122 }
Ted Kremenek401adfa2008-10-07 23:06:01 +0000123
124 return *this;
Ted Kremenekc7b07c12008-10-06 23:49:24 +0000125}