blob: 3ec6824566819d622980cdc7238bef5571b5fc7c [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
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 implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ASTContext.h"
16using namespace clang;
17
18//===----------------------------------------------------------------------===//
19// Decl Allocation/Deallocation Method Implementations
20//===----------------------------------------------------------------------===//
21
22CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
23 SourceLocation L, IdentifierInfo *Id,
24 QualType T, Expr *BW) {
25 void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();
26 return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);
27}
28
29CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
30 SourceLocation L, IdentifierInfo *Id,
31 CXXRecordDecl* PrevDecl) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +000032 void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000033 CXXRecordDecl* R = new (Mem) CXXRecordDecl(TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000034 C.getTypeDeclType(R, PrevDecl);
35 return R;
36}
37
Douglas Gregorf8268ae2008-10-22 17:49:05 +000038CXXRecordDecl::~CXXRecordDecl() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000039 delete [] Bases;
40}
41
Douglas Gregorb48fe382008-10-31 09:07:45 +000042void CXXRecordDecl::Destroy(ASTContext &C) {
43 for (OverloadedFunctionDecl::function_iterator func
44 = Constructors.function_begin();
45 func != Constructors.function_end(); ++func)
46 (*func)->Destroy(C);
47 RecordDecl::Destroy(C);
48}
49
Douglas Gregor57c856b2008-10-23 18:13:27 +000050void
51CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
52 unsigned NumBases) {
53 if (this->Bases)
54 delete [] this->Bases;
55
56 this->Bases = new CXXBaseSpecifier[NumBases];
57 this->NumBases = NumBases;
58 for (unsigned i = 0; i < NumBases; ++i)
59 this->Bases[i] = *Bases[i];
60}
61
Ted Kremenek4b7c9832008-09-05 17:16:31 +000062CXXMethodDecl *
63CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
64 SourceLocation L, IdentifierInfo *Id,
65 QualType T, bool isStatic, bool isInline,
66 ScopedDecl *PrevDecl) {
67 void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();
68 return new (Mem) CXXMethodDecl(RD, L, Id, T, isStatic, isInline, PrevDecl);
69}
70
71QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +000072 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
73 // If the member function is declared const, the type of this is const X*,
74 // if the member function is declared volatile, the type of this is
75 // volatile X*, and if the member function is declared const volatile,
76 // the type of this is const volatile X*.
77
Ted Kremenek4b7c9832008-09-05 17:16:31 +000078 assert(isInstance() && "No 'this' for static methods!");
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +000079 QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(
80 cast<CXXRecordDecl>(getParent())));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +000081 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
82 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +000083}
84
Douglas Gregorb48fe382008-10-31 09:07:45 +000085CXXConstructorDecl *
86CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
87 SourceLocation L, IdentifierInfo *Id,
88 QualType T, bool isExplicit,
89 bool isInline, bool isImplicitlyDeclared) {
90 void *Mem = C.getAllocator().Allocate<CXXConstructorDecl>();
91 return new (Mem) CXXConstructorDecl(RD, L, Id, T, isExplicit, isInline,
92 isImplicitlyDeclared);
93}
94
95
Ted Kremenek4b7c9832008-09-05 17:16:31 +000096CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
97 SourceLocation L, IdentifierInfo *Id,
98 QualType T, ScopedDecl *PrevDecl) {
99 void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
100 return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
101}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000102
103OverloadedFunctionDecl *
104OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
105 IdentifierInfo *Id) {
106 void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
107 return new (Mem) OverloadedFunctionDecl(DC, Id);
108}