1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the C++ related Decl classes.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/DeclCXX.h"
#include "clang/AST/ASTContext.h"
using namespace clang;
//===----------------------------------------------------------------------===//
// Decl Allocation/Deallocation Method Implementations
//===----------------------------------------------------------------------===//
CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
SourceLocation L, IdentifierInfo *Id,
QualType T, Expr *BW) {
void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();
return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);
}
CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
SourceLocation L, IdentifierInfo *Id,
CXXRecordDecl* PrevDecl) {
void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
CXXRecordDecl* R = new (Mem) CXXRecordDecl(TK, DC, L, Id);
C.getTypeDeclType(R, PrevDecl);
return R;
}
CXXRecordDecl::~CXXRecordDecl() {
delete [] Bases;
}
void CXXRecordDecl::Destroy(ASTContext &C) {
for (OverloadedFunctionDecl::function_iterator func
= Constructors.function_begin();
func != Constructors.function_end(); ++func)
(*func)->Destroy(C);
RecordDecl::Destroy(C);
}
void
CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
unsigned NumBases) {
if (this->Bases)
delete [] this->Bases;
this->Bases = new CXXBaseSpecifier[NumBases];
this->NumBases = NumBases;
for (unsigned i = 0; i < NumBases; ++i)
this->Bases[i] = *Bases[i];
}
bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
for (OverloadedFunctionDecl::function_const_iterator Con
= Constructors.function_begin();
Con != Constructors.function_end(); ++Con) {
unsigned TypeQuals;
if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
(TypeQuals & QualType::Const != 0))
return true;
}
return false;
}
void
CXXRecordDecl::addConstructor(ASTContext &Context,
CXXConstructorDecl *ConDecl) {
if (!ConDecl->isImplicitlyDeclared()) {
// Note that we have a user-declared constructor.
UserDeclaredConstructor = true;
// Note when we have a user-declared copy constructor, which will
// suppress the implicit declaration of a copy constructor.
if (ConDecl->isCopyConstructor(Context))
UserDeclaredCopyConstructor = true;
}
Constructors.addOverload(ConDecl);
}
CXXMethodDecl *
CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
SourceLocation L, IdentifierInfo *Id,
QualType T, bool isStatic, bool isInline,
ScopedDecl *PrevDecl) {
void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();
return new (Mem) CXXMethodDecl(RD, L, Id, T, isStatic, isInline, PrevDecl);
}
QualType CXXMethodDecl::getThisType(ASTContext &C) const {
// C++ 9.3.2p1: The type of this in a member function of a class X is X*.
// If the member function is declared const, the type of this is const X*,
// if the member function is declared volatile, the type of this is
// volatile X*, and if the member function is declared const volatile,
// the type of this is const volatile X*.
assert(isInstance() && "No 'this' for static methods!");
QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
return C.getPointerType(ClassTy).withConst();
}
CXXConstructorDecl *
CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
SourceLocation L, IdentifierInfo *Id,
QualType T, bool isExplicit,
bool isInline, bool isImplicitlyDeclared) {
void *Mem = C.getAllocator().Allocate<CXXConstructorDecl>();
return new (Mem) CXXConstructorDecl(RD, L, Id, T, isExplicit, isInline,
isImplicitlyDeclared);
}
bool CXXConstructorDecl::isDefaultConstructor() const {
// C++ [class.ctor]p5:
//
// A default constructor for a class X is a constructor of class
// X that can be called without an argument.
return (getNumParams() == 0) ||
(getNumParams() > 0 & getParamDecl(1)->getDefaultArg() != 0);
}
bool
CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
unsigned &TypeQuals) const {
// C++ [class.copy]p2:
// A non-template constructor for class X is a copy constructor
// if its first parameter is of type X&, const X&, volatile X& or
// const volatile X&, and either there are no other parameters
// or else all other parameters have default arguments (8.3.6).
if ((getNumParams() < 1) ||
(getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
return false;
const ParmVarDecl *Param = getParamDecl(0);
// Do we have a reference type?
const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType();
if (!ParamRefType)
return false;
// Is it a reference to our class type?
QualType PointeeType
= Context.getCanonicalType(ParamRefType->getPointeeType());
QualType ClassTy
= Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
if (PointeeType.getUnqualifiedType() != ClassTy)
return false;
// We have a copy constructor.
TypeQuals = PointeeType.getCVRQualifiers();
return true;
}
bool CXXConstructorDecl::isConvertingConstructor() const {
// C++ [class.conv.ctor]p1:
// A constructor declared without the function-specifier explicit
// that can be called with a single parameter specifies a
// conversion from the type of its first parameter to the type of
// its class. Such a constructor is called a converting
// constructor.
if (isExplicit())
return false;
return (getNumParams() == 0 &&
getType()->getAsFunctionTypeProto()->isVariadic()) ||
(getNumParams() == 1) ||
(getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
}
CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
SourceLocation L, IdentifierInfo *Id,
QualType T, ScopedDecl *PrevDecl) {
void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
}
OverloadedFunctionDecl *
OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
IdentifierInfo *Id) {
void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
return new (Mem) OverloadedFunctionDecl(DC, Id);
}
|