blob: 35cb6d3bc614a24456947b423a9b7f3484f3fb9a [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//
Douglas Gregor9653db72009-02-13 19:06:18 +000010// This file defines the DeclGroup and DeclGroupRef classes.
Ted Kremenekd17062c2008-09-25 17:13:40 +000011//
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.
Douglas Gregor9653db72009-02-13 19:06:18 +000039DeclGroup* DeclGroup::Read(llvm::Deserializer& D, ASTContext& C) {
Ted Kremenekc7b07c12008-10-06 23:49:24 +000040 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 Kremenekd17062c2008-09-25 17:13:40 +000057 this->~DeclGroup();
Steve Naroff3e970492009-01-27 21:25:57 +000058 C.Deallocate((void*) this);
Ted Kremenekd17062c2008-09-25 17:13:40 +000059}
60
Ted Kremenekc7b07c12008-10-06 23:49:24 +000061void DeclGroupRef::Emit(llvm::Serializer& S) const {
62 if (getKind() == DeclKind) {
63 S.EmitBool(false);
64 S.EmitPtr(D);
65 }
66 else {
67 S.EmitBool(true);
68 S.EmitPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D)
69 & ~Mask));
70 }
71}
72
73DeclGroupRef DeclGroupRef::ReadVal(llvm::Deserializer& D) {
74 if (D.ReadBool())
75 return DeclGroupRef(D.ReadPtr<Decl>());
76
77 return DeclGroupRef(D.ReadPtr<DeclGroup>());
78}