blob: e878a3df280d65e2d0b37ac0ef472af32619a355 [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) {
291 // If visible conversion list is already evaluated, return it.
292 if (RD == this &&
293 VisibleConversions.function_begin() != VisibleConversions.function_end())
294 return &VisibleConversions;
295
296 QualType ClassType = Context.getTypeDeclType(this);
297 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
298 OverloadedFunctionDecl *Conversions
299 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
300 for (OverloadedFunctionDecl::function_iterator
301 Func = Conversions->function_begin(),
302 FuncEnd = Conversions->function_end();
303 Func != FuncEnd; ++Func) {
304 if (FunctionTemplateDecl *ConversionTemplate =
305 dyn_cast<FunctionTemplateDecl>(*Func)) {
306 RD->addVisibleConversionFunction(Context, ConversionTemplate);
307 continue;
308 }
309 CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func);
310 bool Candidate = true;
311 // Only those conversions not exact match of conversions in current
312 // class are candidateconversion routines.
313 if (RD != this) {
314 OverloadedFunctionDecl *TopConversions = RD->getConversionFunctions();
315 QualType ConvType = Context.getCanonicalType(Conv->getType());
316 for (OverloadedFunctionDecl::function_iterator
317 TFunc = TopConversions->function_begin(),
318 TFuncEnd = TopConversions->function_end();
319 TFunc != TFuncEnd; ++TFunc) {
320 CXXConversionDecl *TopConv = cast<CXXConversionDecl>(*TFunc);
321 QualType TConvType = Context.getCanonicalType(TopConv->getType());
322 if (ConvType == TConvType) {
323 Candidate = false;
324 break;
325 }
326 }
327 }
328 if (Candidate) {
329 if (FunctionTemplateDecl *ConversionTemplate
330 = Conv->getDescribedFunctionTemplate())
331 RD->addVisibleConversionFunction(Context, ConversionTemplate);
332 else if (!Conv->getPrimaryTemplate()) // ignore specializations
333 RD->addVisibleConversionFunction(Context, Conv);
334 }
335 }
336 }
337
338 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
339 E = vbases_end(); VBase != E; ++VBase) {
340 CXXRecordDecl *VBaseClassDecl
341 = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
342 VBaseClassDecl->getVisibleConversionFunctions(Context, RD);
343 }
344 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
345 E = bases_end(); Base != E; ++Base) {
346 if (Base->isVirtual())
347 continue;
348 CXXRecordDecl *BaseClassDecl
349 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
350 BaseClassDecl->getVisibleConversionFunctions(Context, RD);
351 }
352 return &VisibleConversions;
353}
354
355void CXXRecordDecl::addVisibleConversionFunction(ASTContext &Context,
356 CXXConversionDecl *ConvDecl) {
357 assert(!ConvDecl->getDescribedFunctionTemplate() &&
358 "Conversion function templates should cast to FunctionTemplateDecl.");
359 VisibleConversions.addOverload(ConvDecl);
360}
361
362void CXXRecordDecl::addVisibleConversionFunction(ASTContext &Context,
363 FunctionTemplateDecl *ConvDecl) {
364 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
365 "Function template is not a conversion function template");
366 VisibleConversions.addOverload(ConvDecl);
367}
368
Mike Stump1eb44332009-09-09 15:08:12 +0000369void CXXRecordDecl::addConversionFunction(ASTContext &Context,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000370 CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000371 assert(!ConvDecl->getDescribedFunctionTemplate() &&
372 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000373 Conversions.addOverload(ConvDecl);
374}
375
Mike Stump1eb44332009-09-09 15:08:12 +0000376void CXXRecordDecl::addConversionFunction(ASTContext &Context,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000377 FunctionTemplateDecl *ConvDecl) {
378 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
379 "Function template is not a conversion function template");
380 Conversions.addOverload(ConvDecl);
381}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000382
383CXXConstructorDecl *
384CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
385 QualType ClassType = Context.getTypeDeclType(this);
386 DeclarationName ConstructorName
387 = Context.DeclarationNames.getCXXConstructorName(
388 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000390 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000391 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000392 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000393 // FIXME: In C++0x, a constructor template can be a default constructor.
394 if (isa<FunctionTemplateDecl>(*Con))
395 continue;
396
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000397 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
398 if (Constructor->isDefaultConstructor())
399 return Constructor;
400 }
401 return 0;
402}
403
Anders Carlsson7267c162009-05-29 21:03:38 +0000404const CXXDestructorDecl *
405CXXRecordDecl::getDestructor(ASTContext &Context) {
406 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000407
408 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000409 = Context.DeclarationNames.getCXXDestructorName(
410 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000411
412 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000413 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000414 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlsson7267c162009-05-29 21:03:38 +0000416 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
417 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Anders Carlsson7267c162009-05-29 21:03:38 +0000419 return Dtor;
420}
421
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000422CXXMethodDecl *
423CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000424 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000425 QualType T, DeclaratorInfo *DInfo,
426 bool isStatic, bool isInline) {
427 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
428 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000429}
430
Mike Stump1eb44332009-09-09 15:08:12 +0000431typedef llvm::DenseMap<const CXXMethodDecl*,
432 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000433 OverriddenMethodsMapTy;
434
Mike Stumpb9871a22009-08-21 01:45:00 +0000435// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
436// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000437static OverriddenMethodsMapTy *OverriddenMethods = 0;
438
439void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
440 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson05eb2442009-05-16 23:58:37 +0000442 if (!OverriddenMethods)
443 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Anders Carlsson05eb2442009-05-16 23:58:37 +0000445 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
446 if (!Methods)
447 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Anders Carlsson05eb2442009-05-16 23:58:37 +0000449 Methods->push_back(MD);
450}
451
452CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
453 if (!OverriddenMethods)
454 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Anders Carlsson05eb2442009-05-16 23:58:37 +0000456 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000457 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000458 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000459
Anders Carlsson05eb2442009-05-16 23:58:37 +0000460 return &(*it->second)[0];
461}
462
463CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
464 if (!OverriddenMethods)
465 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Anders Carlsson05eb2442009-05-16 23:58:37 +0000467 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000468 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000469 return 0;
470
Daniel Dunbar637ec322009-08-02 01:48:29 +0000471 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000472}
473
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000474QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000475 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
476 // If the member function is declared const, the type of this is const X*,
477 // if the member function is declared volatile, the type of this is
478 // volatile X*, and if the member function is declared const volatile,
479 // the type of this is const volatile X*.
480
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000481 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000482
483 QualType ClassTy;
484 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
485 ClassTy = TD->getInjectedClassNameType(C);
486 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000487 ClassTy = C.getTagDeclType(getParent());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000488 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000489 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000490}
491
Douglas Gregor7ad83902008-11-05 04:29:56 +0000492CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000493CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000494 CXXConstructorDecl *C,
Mike Stump1eb44332009-09-09 15:08:12 +0000495 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000496 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000497 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
498 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
499 BaseOrMember |= 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregor7ad83902008-11-05 04:29:56 +0000501 if (NumArgs > 0) {
502 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000503 // FIXME. Allocation via Context
504 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000505 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
506 this->Args[Idx] = Args[Idx];
507 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000508 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000509}
510
511CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000512CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000513 CXXConstructorDecl *C,
Anders Carlsson8c57a662009-08-29 01:31:33 +0000514 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000515 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000516 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
Mike Stump1eb44332009-09-09 15:08:12 +0000517 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
Douglas Gregor7ad83902008-11-05 04:29:56 +0000518
519 if (NumArgs > 0) {
520 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000521 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000522 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
523 this->Args[Idx] = Args[Idx];
524 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000525 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000526}
527
528CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
529 delete [] Args;
530}
531
Douglas Gregorb48fe382008-10-31 09:07:45 +0000532CXXConstructorDecl *
533CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000534 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000535 QualType T, DeclaratorInfo *DInfo,
536 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000537 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000538 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
539 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000540 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000541 isImplicitlyDeclared);
542}
543
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000544bool CXXConstructorDecl::isDefaultConstructor() const {
545 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000546 // A default constructor for a class X is a constructor of class
547 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000548 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000549 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000550}
551
Mike Stump1eb44332009-09-09 15:08:12 +0000552bool
553CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000554 unsigned &TypeQuals) const {
555 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000556 // A non-template constructor for class X is a copy constructor
557 // if its first parameter is of type X&, const X&, volatile X& or
558 // const volatile X&, and either there are no other parameters
559 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000560 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000561 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000562 return false;
563
564 const ParmVarDecl *Param = getParamDecl(0);
565
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000566 // Do we have a reference type? Rvalue references don't count.
567 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000568 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000569 if (!ParamRefType)
570 return false;
571
572 // Is it a reference to our class type?
Mike Stumpe607ed02009-08-07 18:05:12 +0000573 QualType PointeeType
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000574 = Context.getCanonicalType(ParamRefType->getPointeeType());
Mike Stumpe607ed02009-08-07 18:05:12 +0000575 QualType ClassTy = Context.getTagDeclType(getParent());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000576 if (PointeeType.getUnqualifiedType() != ClassTy)
577 return false;
578
579 // We have a copy constructor.
580 TypeQuals = PointeeType.getCVRQualifiers();
581 return true;
582}
583
Anders Carlssonfaccd722009-08-28 16:57:08 +0000584bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000585 // C++ [class.conv.ctor]p1:
586 // A constructor declared without the function-specifier explicit
587 // that can be called with a single parameter specifies a
588 // conversion from the type of its first parameter to the type of
589 // its class. Such a constructor is called a converting
590 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000591 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000592 return false;
593
Mike Stump1eb44332009-09-09 15:08:12 +0000594 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000595 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000596 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000597 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000598}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000599
Douglas Gregor42a552f2008-11-05 20:51:48 +0000600CXXDestructorDecl *
601CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000602 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000603 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000604 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000605 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
606 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000607 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000608 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000609}
610
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000611void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000612CXXDestructorDecl::Destroy(ASTContext& C) {
613 C.Deallocate(BaseOrMemberDestructions);
614 CXXMethodDecl::Destroy(C);
615}
616
617void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000618CXXConstructorDecl::Destroy(ASTContext& C) {
619 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000620 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000621}
622
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000623CXXConversionDecl *
624CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000625 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000626 QualType T, DeclaratorInfo *DInfo,
627 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000628 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
629 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000630 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000631}
632
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000633OverloadedFunctionDecl *
634OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000635 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000636 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000637}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000638
Douglas Gregordec06662009-08-21 18:42:58 +0000639OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
640 if (!ND)
641 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Douglas Gregordec06662009-08-21 18:42:58 +0000643 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
644 D = ND;
645 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000646 if (Ovl->size() != 0) {
647 D = ND;
648 Iter = Ovl->function_begin();
649 }
Douglas Gregordec06662009-08-21 18:42:58 +0000650 }
651}
652
Douglas Gregor364e0212009-06-27 21:05:07 +0000653void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
654 Functions.push_back(F);
655 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000656}
657
Douglas Gregordaa439a2009-07-08 10:57:20 +0000658OverloadIterator::reference OverloadIterator::operator*() const {
659 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
660 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Douglas Gregordaa439a2009-07-08 10:57:20 +0000662 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
663 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregordaa439a2009-07-08 10:57:20 +0000665 assert(isa<OverloadedFunctionDecl>(D));
666 return *Iter;
667}
668
669OverloadIterator &OverloadIterator::operator++() {
670 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
671 D = 0;
672 return *this;
673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregordaa439a2009-07-08 10:57:20 +0000675 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
676 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Douglas Gregordaa439a2009-07-08 10:57:20 +0000678 return *this;
679}
680
681bool OverloadIterator::Equals(const OverloadIterator &Other) const {
682 if (!D || !Other.D)
683 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Douglas Gregordaa439a2009-07-08 10:57:20 +0000685 if (D != Other.D)
686 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Douglas Gregordaa439a2009-07-08 10:57:20 +0000688 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
689}
690
John McCall02cace72009-08-28 07:59:38 +0000691FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
692 SourceLocation L,
693 FriendUnion Friend,
694 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000695#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000696 if (Friend.is<NamedDecl*>()) {
697 NamedDecl *D = Friend.get<NamedDecl*>();
698 assert(isa<FunctionDecl>(D) ||
699 isa<CXXRecordDecl>(D) ||
700 isa<FunctionTemplateDecl>(D) ||
701 isa<ClassTemplateDecl>(D));
702 assert(D->getFriendObjectKind());
703 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000704#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000705
John McCall02cace72009-08-28 07:59:38 +0000706 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000707}
John McCall3f9a8a62009-08-11 06:59:38 +0000708
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000709LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000710 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000711 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000712 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000713 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000714}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000715
716UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
717 SourceLocation L,
718 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000719 SourceRange QualifierRange,
720 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000721 SourceLocation IdentLoc,
722 NamespaceDecl *Used,
723 DeclContext *CommonAncestor) {
Mike Stump1eb44332009-09-09 15:08:12 +0000724 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000725 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000726}
727
Mike Stump1eb44332009-09-09 15:08:12 +0000728NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
729 SourceLocation L,
730 SourceLocation AliasLoc,
731 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000732 SourceRange QualifierRange,
733 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000734 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000735 NamedDecl *Namespace) {
Mike Stump1eb44332009-09-09 15:08:12 +0000736 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000737 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000738}
739
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000740UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
741 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
742 SourceLocation UL, NamedDecl* Target,
743 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
744 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
745 TargetNNS, IsTypeNameArg);
746}
747
Anders Carlsson665b49c2009-08-28 05:30:28 +0000748UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
749 SourceLocation UsingLoc,
750 SourceRange TargetNNR,
751 NestedNameSpecifier *TargetNNS,
752 SourceLocation TargetNameLoc,
753 DeclarationName TargetName,
754 bool IsTypeNameArg) {
755 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
756 TargetNameLoc, TargetName, IsTypeNameArg);
757}
758
Anders Carlssonfb311762009-03-14 00:25:26 +0000759StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
760 SourceLocation L, Expr *AssertExpr,
761 StringLiteral *Message) {
762 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
763}
764
765void StaticAssertDecl::Destroy(ASTContext& C) {
766 AssertExpr->Destroy(C);
767 Message->Destroy(C);
768 this->~StaticAssertDecl();
769 C.Deallocate((void *)this);
770}
771
772StaticAssertDecl::~StaticAssertDecl() {
773}
774
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000775static const char *getAccessName(AccessSpecifier AS) {
776 switch (AS) {
777 default:
778 case AS_none:
779 assert("Invalid access specifier!");
780 return 0;
781 case AS_public:
782 return "public";
783 case AS_private:
784 return "private";
785 case AS_protected:
786 return "protected";
787 }
788}
789
790const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
791 AccessSpecifier AS) {
792 return DB << getAccessName(AS);
793}
794
795