blob: 34b37dada5c71e12088f58bd053800faa50d1cc8 [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;
Zhongxing Xuc61255f2008-09-27 03:27:29 +000024 void* mem = C.getAllocator().Allocate(size, alignment);
Ted Kremenekd17062c2008-09-25 17:13:40 +000025 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() {
Ted Kremeneke285a952008-10-06 22:17:16 +000046 assert (D == 0 && "Destroy method not called.");
Ted Kremenekd17062c2008-09-25 17:13:40 +000047}
48
49void DeclGroupOwningRef::Destroy(ASTContext& C) {
Ted Kremeneke285a952008-10-06 22:17:16 +000050 if (!D)
Ted Kremenekd17062c2008-09-25 17:13:40 +000051 return;
52
53 if (getKind() == DeclKind)
Ted Kremeneke285a952008-10-06 22:17:16 +000054 D->Destroy(C);
55 else
56 reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) &
57 ~Mask)->Destroy(C);
Ted Kremenekd17062c2008-09-25 17:13:40 +000058
Ted Kremeneke285a952008-10-06 22:17:16 +000059 D = 0;
Ted Kremenekd17062c2008-09-25 17:13:40 +000060}