blob: 2149005cde5abbc0688fb708c7f72f9409dc8d88 [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"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000018#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000019#include "llvm/ADT/STLExtras.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000025
Douglas Gregor3e00bad2009-02-17 01:05:43 +000026CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000027 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000028 CXXRecordDecl *PrevDecl,
Mike Stump1eb44332009-09-09 15:08:12 +000029 SourceLocation TKL)
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000030 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000031 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000032 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000033 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
34 Abstract(false), HasTrivialConstructor(true),
35 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
36 HasTrivialDestructor(true), Bases(0), NumBases(0), VBases(0), NumVBases(0),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000037 Conversions(DC, DeclarationName()),
Fariborz Jahanian53462782009-09-11 21:44:33 +000038 VisibleConversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000039 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000040
Ted Kremenek4b7c9832008-09-05 17:16:31 +000041CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
42 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000043 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000044 CXXRecordDecl* PrevDecl,
45 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000046 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000047 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000048
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000049 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000050 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000051 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000052 return R;
53}
54
Douglas Gregorf8268ae2008-10-22 17:49:05 +000055CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000056}
57
58void CXXRecordDecl::Destroy(ASTContext &C) {
59 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000060 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000061 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000062}
63
Mike Stump1eb44332009-09-09 15:08:12 +000064void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000065CXXRecordDecl::setBases(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +000066 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000067 unsigned NumBases) {
Mike Stump1eb44332009-09-09 15:08:12 +000068 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000069 // An aggregate is an array or a class (clause 9) with [...]
70 // no base classes [...].
71 Aggregate = false;
72
Douglas Gregor57c856b2008-10-23 18:13:27 +000073 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000074 C.Deallocate(this->Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000076 int vbaseCount = 0;
77 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
78 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000080 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000081 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000082 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000083 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000084 // Keep track of inherited vbases for this base class.
85 const CXXBaseSpecifier *Base = Bases[i];
86 QualType BaseType = Base->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000087 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000088 // FIXME. This means that this list must be rebuilt during template
89 // instantiation.
90 if (BaseType->isDependentType())
91 continue;
92 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000093 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 if (Base->isVirtual())
95 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +000096 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000097 BaseClassDecl->vbases_begin(),
98 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +000099 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000100 // not already in the list.
101 // FIXME. Note that we do a linear search as number of such classes are
102 // very few.
103 int i;
104 for (i = 0; i < vbaseCount; ++i)
105 if (UniqueVbases[i]->getType() == VBase->getType())
106 break;
107 if (i == vbaseCount) {
108 UniqueVbases.push_back(VBase);
109 ++vbaseCount;
110 }
111 }
112 }
113 if (hasDirectVirtualBase) {
114 // Iterate one more time through the direct bases and add the virtual
115 // base to the list of vritual bases for current class.
116 for (unsigned i = 0; i < NumBases; ++i) {
117 const CXXBaseSpecifier *VBase = Bases[i];
118 if (!VBase->isVirtual())
119 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000120 int j;
121 for (j = 0; j < vbaseCount; ++j)
122 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000123 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000124 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000125 UniqueVbases.push_back(VBase);
126 ++vbaseCount;
127 }
128 }
129 }
130 if (vbaseCount > 0) {
131 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000132 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000133 this->NumVBases = vbaseCount;
134 for (int i = 0; i < vbaseCount; i++) {
135 QualType QT = UniqueVbases[i]->getType();
136 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000137 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000138 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000139 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
140 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
141 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000142 }
143 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000144}
145
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000146bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000147 return getCopyConstructor(Context, QualType::Const) != 0;
148}
149
Mike Stump1eb44332009-09-09 15:08:12 +0000150CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000151 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000152 QualType ClassType
153 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000154 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000155 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000156 Context.getCanonicalType(ClassType));
157 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000158 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000159 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000160 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000161 // C++ [class.copy]p2:
162 // A non-template constructor for class X is a copy constructor if [...]
163 if (isa<FunctionTemplateDecl>(*Con))
164 continue;
165
Mike Stump1eb44332009-09-09 15:08:12 +0000166 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000167 FoundTQs)) {
168 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
169 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
170 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000172 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000173 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000174 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000175}
176
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000177bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
178 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000179 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
180 const_cast<CXXRecordDecl*>(this)));
181 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
182
183 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000184 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000185 Op != OpEnd; ++Op) {
186 // C++ [class.copy]p9:
187 // A user-declared copy assignment operator is a non-static non-template
188 // member function of class X with exactly one parameter of type X, X&,
189 // const X&, volatile X& or const volatile X&.
190 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
191 if (Method->isStatic())
192 continue;
193 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000194 const FunctionProtoType *FnType =
195 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000196 assert(FnType && "Overloaded operator has no prototype.");
197 // Don't assert on this; an invalid decl might have been left in the AST.
198 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
199 continue;
200 bool AcceptsConst = true;
201 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000202 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000203 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000204 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000205 if (!ArgType.isConstQualified())
206 AcceptsConst = false;
207 }
208 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
209 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000210 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000211 // We have a single argument of type cv X or cv X&, i.e. we've found the
212 // copy assignment operator. Return whether it accepts const arguments.
213 return AcceptsConst;
214 }
215 assert(isInvalidDecl() &&
216 "No copy assignment operator declared in valid code.");
217 return false;
218}
219
220void
Mike Stump1eb44332009-09-09 15:08:12 +0000221CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000222 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000223 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
224 // Note that we have a user-declared constructor.
225 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000226
Mike Stump1eb44332009-09-09 15:08:12 +0000227 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000228 // An aggregate is an array or a class (clause 9) with no
229 // user-declared constructors (12.1) [...].
230 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000231
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000232 // C++ [class]p4:
233 // A POD-struct is an aggregate class [...]
234 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000235
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000236 // C++ [class.ctor]p5:
237 // A constructor is trivial if it is an implicitly-declared default
238 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000239 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000240 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000242 // Note when we have a user-declared copy constructor, which will
243 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000244 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000245 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000246
247 // C++ [class.copy]p6:
248 // A copy constructor is trivial if it is implicitly declared.
249 // FIXME: C++0x: don't do this for "= default" copy constructors.
250 HasTrivialCopyConstructor = false;
251 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000252}
253
Sebastian Redl64b45f72009-01-05 20:52:13 +0000254void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
255 CXXMethodDecl *OpDecl) {
256 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000257 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000258 assert(FnType && "Overloaded operator has no proto function type.");
259 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
260 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000261 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000262 ArgType = Ref->getPointeeType();
263
264 ArgType = ArgType.getUnqualifiedType();
265 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
266 const_cast<CXXRecordDecl*>(this)));
267
268 if (ClassType != Context.getCanonicalType(ArgType))
269 return;
270
271 // This is a copy assignment operator.
272 // Suppress the implicit declaration of a copy constructor.
273 UserDeclaredCopyAssignment = true;
274
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000275 // C++ [class.copy]p11:
276 // A copy assignment operator is trivial if it is implicitly declared.
277 // FIXME: C++0x: don't do this for "= default" copy operators.
278 HasTrivialCopyAssignment = false;
279
Sebastian Redl64b45f72009-01-05 20:52:13 +0000280 // C++ [class]p4:
281 // A POD-struct is an aggregate class that [...] has no user-defined copy
282 // assignment operator [...].
283 PlainOldData = false;
284}
285
Fariborz Jahanian53462782009-09-11 21:44:33 +0000286/// getVisibleConversionFunctions - get all conversion functions visible
287/// in current class; including conversion function templates.
288OverloadedFunctionDecl *
289CXXRecordDecl::getVisibleConversionFunctions(ASTContext &Context,
290 CXXRecordDecl *RD) {
Fariborz Jahanian4f213d32009-09-11 22:27:50 +0000291 if (RD == this) {
292 // If root class, all conversions are visible.
293 if (RD->bases_begin() == RD->bases_end())
294 return &Conversions;
295 // If visible conversion list is already evaluated, return it.
296 if (VisibleConversions.function_begin()
297 != VisibleConversions.function_end())
298 return &VisibleConversions;
299 }
300
Fariborz Jahanian53462782009-09-11 21:44:33 +0000301 QualType ClassType = Context.getTypeDeclType(this);
302 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
303 OverloadedFunctionDecl *Conversions
304 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
305 for (OverloadedFunctionDecl::function_iterator
306 Func = Conversions->function_begin(),
307 FuncEnd = Conversions->function_end();
308 Func != FuncEnd; ++Func) {
309 if (FunctionTemplateDecl *ConversionTemplate =
310 dyn_cast<FunctionTemplateDecl>(*Func)) {
311 RD->addVisibleConversionFunction(Context, ConversionTemplate);
312 continue;
313 }
314 CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func);
315 bool Candidate = true;
316 // Only those conversions not exact match of conversions in current
317 // class are candidateconversion routines.
318 if (RD != this) {
319 OverloadedFunctionDecl *TopConversions = RD->getConversionFunctions();
320 QualType ConvType = Context.getCanonicalType(Conv->getType());
321 for (OverloadedFunctionDecl::function_iterator
322 TFunc = TopConversions->function_begin(),
323 TFuncEnd = TopConversions->function_end();
324 TFunc != TFuncEnd; ++TFunc) {
325 CXXConversionDecl *TopConv = cast<CXXConversionDecl>(*TFunc);
326 QualType TConvType = Context.getCanonicalType(TopConv->getType());
327 if (ConvType == TConvType) {
328 Candidate = false;
329 break;
330 }
331 }
332 }
333 if (Candidate) {
334 if (FunctionTemplateDecl *ConversionTemplate
335 = Conv->getDescribedFunctionTemplate())
336 RD->addVisibleConversionFunction(Context, ConversionTemplate);
337 else if (!Conv->getPrimaryTemplate()) // ignore specializations
338 RD->addVisibleConversionFunction(Context, Conv);
339 }
340 }
341 }
342
343 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
344 E = vbases_end(); VBase != E; ++VBase) {
345 CXXRecordDecl *VBaseClassDecl
346 = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
347 VBaseClassDecl->getVisibleConversionFunctions(Context, RD);
348 }
349 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
350 E = bases_end(); Base != E; ++Base) {
351 if (Base->isVirtual())
352 continue;
353 CXXRecordDecl *BaseClassDecl
354 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
355 BaseClassDecl->getVisibleConversionFunctions(Context, RD);
356 }
357 return &VisibleConversions;
358}
359
360void CXXRecordDecl::addVisibleConversionFunction(ASTContext &Context,
361 CXXConversionDecl *ConvDecl) {
362 assert(!ConvDecl->getDescribedFunctionTemplate() &&
363 "Conversion function templates should cast to FunctionTemplateDecl.");
364 VisibleConversions.addOverload(ConvDecl);
365}
366
367void CXXRecordDecl::addVisibleConversionFunction(ASTContext &Context,
368 FunctionTemplateDecl *ConvDecl) {
369 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
370 "Function template is not a conversion function template");
371 VisibleConversions.addOverload(ConvDecl);
372}
373
Mike Stump1eb44332009-09-09 15:08:12 +0000374void CXXRecordDecl::addConversionFunction(ASTContext &Context,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000375 CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000376 assert(!ConvDecl->getDescribedFunctionTemplate() &&
377 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000378 Conversions.addOverload(ConvDecl);
379}
380
Mike Stump1eb44332009-09-09 15:08:12 +0000381void CXXRecordDecl::addConversionFunction(ASTContext &Context,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000382 FunctionTemplateDecl *ConvDecl) {
383 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
384 "Function template is not a conversion function template");
385 Conversions.addOverload(ConvDecl);
386}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000387
388CXXConstructorDecl *
389CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
390 QualType ClassType = Context.getTypeDeclType(this);
391 DeclarationName ConstructorName
392 = Context.DeclarationNames.getCXXConstructorName(
393 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000395 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000396 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000397 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000398 // FIXME: In C++0x, a constructor template can be a default constructor.
399 if (isa<FunctionTemplateDecl>(*Con))
400 continue;
401
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000402 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
403 if (Constructor->isDefaultConstructor())
404 return Constructor;
405 }
406 return 0;
407}
408
Anders Carlsson7267c162009-05-29 21:03:38 +0000409const CXXDestructorDecl *
410CXXRecordDecl::getDestructor(ASTContext &Context) {
411 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
413 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000414 = Context.DeclarationNames.getCXXDestructorName(
415 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000416
417 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000418 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000419 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Anders Carlsson7267c162009-05-29 21:03:38 +0000421 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
422 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Anders Carlsson7267c162009-05-29 21:03:38 +0000424 return Dtor;
425}
426
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000427CXXMethodDecl *
428CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000429 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000430 QualType T, DeclaratorInfo *DInfo,
431 bool isStatic, bool isInline) {
432 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
433 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000434}
435
Mike Stump1eb44332009-09-09 15:08:12 +0000436typedef llvm::DenseMap<const CXXMethodDecl*,
437 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000438 OverriddenMethodsMapTy;
439
Mike Stumpb9871a22009-08-21 01:45:00 +0000440// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
441// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000442static OverriddenMethodsMapTy *OverriddenMethods = 0;
443
444void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
445 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Anders Carlsson05eb2442009-05-16 23:58:37 +0000447 if (!OverriddenMethods)
448 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Anders Carlsson05eb2442009-05-16 23:58:37 +0000450 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
451 if (!Methods)
452 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Anders Carlsson05eb2442009-05-16 23:58:37 +0000454 Methods->push_back(MD);
455}
456
457CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
458 if (!OverriddenMethods)
459 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Anders Carlsson05eb2442009-05-16 23:58:37 +0000461 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000462 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000463 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000464
Anders Carlsson05eb2442009-05-16 23:58:37 +0000465 return &(*it->second)[0];
466}
467
468CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
469 if (!OverriddenMethods)
470 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Anders Carlsson05eb2442009-05-16 23:58:37 +0000472 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000473 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000474 return 0;
475
Daniel Dunbar637ec322009-08-02 01:48:29 +0000476 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000477}
478
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000479QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000480 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
481 // If the member function is declared const, the type of this is const X*,
482 // if the member function is declared volatile, the type of this is
483 // volatile X*, and if the member function is declared const volatile,
484 // the type of this is const volatile X*.
485
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000486 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000487
488 QualType ClassTy;
489 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
490 ClassTy = TD->getInjectedClassNameType(C);
491 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000492 ClassTy = C.getTagDeclType(getParent());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000493 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000494 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000495}
496
Douglas Gregor7ad83902008-11-05 04:29:56 +0000497CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000498CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000499 CXXConstructorDecl *C,
Mike Stump1eb44332009-09-09 15:08:12 +0000500 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000501 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000502 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
503 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
504 BaseOrMember |= 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregor7ad83902008-11-05 04:29:56 +0000506 if (NumArgs > 0) {
507 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000508 // FIXME. Allocation via Context
509 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000510 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
511 this->Args[Idx] = Args[Idx];
512 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000513 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000514}
515
516CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000517CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000518 CXXConstructorDecl *C,
Anders Carlsson8c57a662009-08-29 01:31:33 +0000519 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000520 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000521 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
Mike Stump1eb44332009-09-09 15:08:12 +0000522 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
Douglas Gregor7ad83902008-11-05 04:29:56 +0000523
524 if (NumArgs > 0) {
525 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000526 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000527 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
528 this->Args[Idx] = Args[Idx];
529 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000530 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000531}
532
533CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
534 delete [] Args;
535}
536
Douglas Gregorb48fe382008-10-31 09:07:45 +0000537CXXConstructorDecl *
538CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000539 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000540 QualType T, DeclaratorInfo *DInfo,
541 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000542 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000543 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
544 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000545 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000546 isImplicitlyDeclared);
547}
548
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000549bool CXXConstructorDecl::isDefaultConstructor() const {
550 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000551 // A default constructor for a class X is a constructor of class
552 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000553 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000554 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000555}
556
Mike Stump1eb44332009-09-09 15:08:12 +0000557bool
558CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000559 unsigned &TypeQuals) const {
560 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000561 // A non-template constructor for class X is a copy constructor
562 // if its first parameter is of type X&, const X&, volatile X& or
563 // const volatile X&, and either there are no other parameters
564 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000565 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000566 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000567 return false;
568
569 const ParmVarDecl *Param = getParamDecl(0);
570
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000571 // Do we have a reference type? Rvalue references don't count.
572 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000573 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000574 if (!ParamRefType)
575 return false;
576
577 // Is it a reference to our class type?
Mike Stumpe607ed02009-08-07 18:05:12 +0000578 QualType PointeeType
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000579 = Context.getCanonicalType(ParamRefType->getPointeeType());
Mike Stumpe607ed02009-08-07 18:05:12 +0000580 QualType ClassTy = Context.getTagDeclType(getParent());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000581 if (PointeeType.getUnqualifiedType() != ClassTy)
582 return false;
583
584 // We have a copy constructor.
585 TypeQuals = PointeeType.getCVRQualifiers();
586 return true;
587}
588
Anders Carlssonfaccd722009-08-28 16:57:08 +0000589bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000590 // C++ [class.conv.ctor]p1:
591 // A constructor declared without the function-specifier explicit
592 // that can be called with a single parameter specifies a
593 // conversion from the type of its first parameter to the type of
594 // its class. Such a constructor is called a converting
595 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000596 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000597 return false;
598
Mike Stump1eb44332009-09-09 15:08:12 +0000599 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000600 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000601 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000602 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000603}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000604
Douglas Gregor42a552f2008-11-05 20:51:48 +0000605CXXDestructorDecl *
606CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000607 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000608 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000609 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000610 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
611 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000612 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000613 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000614}
615
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000616void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000617CXXDestructorDecl::Destroy(ASTContext& C) {
618 C.Deallocate(BaseOrMemberDestructions);
619 CXXMethodDecl::Destroy(C);
620}
621
622void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000623CXXConstructorDecl::Destroy(ASTContext& C) {
624 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000625 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000626}
627
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000628CXXConversionDecl *
629CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000630 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000631 QualType T, DeclaratorInfo *DInfo,
632 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000633 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
634 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000635 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000636}
637
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000638OverloadedFunctionDecl *
639OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000640 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000641 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000642}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000643
Douglas Gregordec06662009-08-21 18:42:58 +0000644OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
645 if (!ND)
646 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Douglas Gregordec06662009-08-21 18:42:58 +0000648 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
649 D = ND;
650 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000651 if (Ovl->size() != 0) {
652 D = ND;
653 Iter = Ovl->function_begin();
654 }
Douglas Gregordec06662009-08-21 18:42:58 +0000655 }
656}
657
Douglas Gregor364e0212009-06-27 21:05:07 +0000658void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
659 Functions.push_back(F);
660 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000661}
662
Douglas Gregordaa439a2009-07-08 10:57:20 +0000663OverloadIterator::reference OverloadIterator::operator*() const {
664 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
665 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Douglas Gregordaa439a2009-07-08 10:57:20 +0000667 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
668 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Douglas Gregordaa439a2009-07-08 10:57:20 +0000670 assert(isa<OverloadedFunctionDecl>(D));
671 return *Iter;
672}
673
674OverloadIterator &OverloadIterator::operator++() {
675 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
676 D = 0;
677 return *this;
678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Douglas Gregordaa439a2009-07-08 10:57:20 +0000680 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
681 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Douglas Gregordaa439a2009-07-08 10:57:20 +0000683 return *this;
684}
685
686bool OverloadIterator::Equals(const OverloadIterator &Other) const {
687 if (!D || !Other.D)
688 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Douglas Gregordaa439a2009-07-08 10:57:20 +0000690 if (D != Other.D)
691 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregordaa439a2009-07-08 10:57:20 +0000693 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
694}
695
John McCall02cace72009-08-28 07:59:38 +0000696FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
697 SourceLocation L,
698 FriendUnion Friend,
699 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000700#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000701 if (Friend.is<NamedDecl*>()) {
702 NamedDecl *D = Friend.get<NamedDecl*>();
703 assert(isa<FunctionDecl>(D) ||
704 isa<CXXRecordDecl>(D) ||
705 isa<FunctionTemplateDecl>(D) ||
706 isa<ClassTemplateDecl>(D));
707 assert(D->getFriendObjectKind());
708 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000709#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000710
John McCall02cace72009-08-28 07:59:38 +0000711 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000712}
John McCall3f9a8a62009-08-11 06:59:38 +0000713
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000714LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000715 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000716 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000717 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000718 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000719}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000720
721UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
722 SourceLocation L,
723 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000724 SourceRange QualifierRange,
725 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000726 SourceLocation IdentLoc,
727 NamespaceDecl *Used,
728 DeclContext *CommonAncestor) {
Mike Stump1eb44332009-09-09 15:08:12 +0000729 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000730 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000731}
732
Mike Stump1eb44332009-09-09 15:08:12 +0000733NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
734 SourceLocation L,
735 SourceLocation AliasLoc,
736 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000737 SourceRange QualifierRange,
738 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000739 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000740 NamedDecl *Namespace) {
Mike Stump1eb44332009-09-09 15:08:12 +0000741 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000742 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000743}
744
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000745UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
746 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
747 SourceLocation UL, NamedDecl* Target,
748 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
749 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
750 TargetNNS, IsTypeNameArg);
751}
752
Anders Carlsson665b49c2009-08-28 05:30:28 +0000753UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
754 SourceLocation UsingLoc,
755 SourceRange TargetNNR,
756 NestedNameSpecifier *TargetNNS,
757 SourceLocation TargetNameLoc,
758 DeclarationName TargetName,
759 bool IsTypeNameArg) {
760 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
761 TargetNameLoc, TargetName, IsTypeNameArg);
762}
763
Anders Carlssonfb311762009-03-14 00:25:26 +0000764StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
765 SourceLocation L, Expr *AssertExpr,
766 StringLiteral *Message) {
767 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
768}
769
770void StaticAssertDecl::Destroy(ASTContext& C) {
771 AssertExpr->Destroy(C);
772 Message->Destroy(C);
773 this->~StaticAssertDecl();
774 C.Deallocate((void *)this);
775}
776
777StaticAssertDecl::~StaticAssertDecl() {
778}
779
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000780static const char *getAccessName(AccessSpecifier AS) {
781 switch (AS) {
782 default:
783 case AS_none:
784 assert("Invalid access specifier!");
785 return 0;
786 case AS_public:
787 return "public";
788 case AS_private:
789 return "private";
790 case AS_protected:
791 return "protected";
792 }
793}
794
795const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
796 AccessSpecifier AS) {
797 return DB << getAccessName(AS);
798}
799
800