blob: 5987d5a7012ae473dbe28ba0f1ada149c0dce839 [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"
18
19using namespace clang;
20
21DeclGroup* DeclGroup::Create(ASTContext& C, unsigned numdecls, Decl** decls) {
22 unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * numdecls;
23 unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;
24 void* mem = C.getAllocator().Allocate(size, alignment);
25 new (mem) DeclGroup(numdecls, decls);
26 return static_cast<DeclGroup*>(mem);
27}
28
29DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) {
30 assert (numdecls > 0);
31 assert (decls);
32 memcpy(this+1, decls, numdecls * sizeof(*decls));
33}
34
35void DeclGroup::Destroy(ASTContext& C) {
36 Decl** Decls = (Decl**) this + 1;
37
38 for (unsigned i = 0; i < NumDecls; ++i)
39 Decls[i]->Destroy(C);
40
41 this->~DeclGroup();
42 C.getAllocator().Deallocate((void*) this);
43}
44
45DeclGroupOwningRef::~DeclGroupOwningRef() {
46 assert (ThePtr == 0 && "Destroy method not called.");
47}
48
49void DeclGroupOwningRef::Destroy(ASTContext& C) {
50 if (!ThePtr)
51 return;
52
53 if (getKind() == DeclKind)
54 reinterpret_cast<Decl*>(ThePtr)->Destroy(C);
55 else
56 reinterpret_cast<DeclGroup*>(ThePtr & ~Mask)->Destroy(C);
57
58 ThePtr = 0;
59}