blob: e695310c3734ae025ab147c3ba8024ae5018d863 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000026#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000027#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattnerd3b90652008-03-15 05:43:15 +000031//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000032// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000033//===----------------------------------------------------------------------===//
34
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000035/// \brief Get the most restrictive linkage for the types in the given
36/// template parameter list.
37static Linkage
38getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
39 Linkage L = ExternalLinkage;
40 for (TemplateParameterList::const_iterator P = Params->begin(),
41 PEnd = Params->end();
42 P != PEnd; ++P) {
43 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
44 if (!NTTP->getType()->isDependentType()) {
45 L = minLinkage(L, NTTP->getType()->getLinkage());
46 continue;
47 }
48
49 if (TemplateTemplateParmDecl *TTP
50 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
51 L = minLinkage(L,
52 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
53 }
54 }
55
56 return L;
57}
58
59/// \brief Get the most restrictive linkage for the types and
60/// declarations in the given template argument list.
61static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
62 unsigned NumArgs) {
63 Linkage L = ExternalLinkage;
64
65 for (unsigned I = 0; I != NumArgs; ++I) {
66 switch (Args[I].getKind()) {
67 case TemplateArgument::Null:
68 case TemplateArgument::Integral:
69 case TemplateArgument::Expression:
70 break;
71
72 case TemplateArgument::Type:
73 L = minLinkage(L, Args[I].getAsType()->getLinkage());
74 break;
75
76 case TemplateArgument::Declaration:
77 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
78 L = minLinkage(L, ND->getLinkage());
79 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
80 L = minLinkage(L, VD->getType()->getLinkage());
81 break;
82
83 case TemplateArgument::Template:
84 if (TemplateDecl *Template
85 = Args[I].getAsTemplate().getAsTemplateDecl())
86 L = minLinkage(L, Template->getLinkage());
87 break;
88
89 case TemplateArgument::Pack:
90 L = minLinkage(L,
91 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
92 Args[I].pack_size()));
93 break;
94 }
95 }
96
97 return L;
98}
99
100static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000101 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
102 "Not a name having namespace scope");
103 ASTContext &Context = D->getASTContext();
104
105 // C++ [basic.link]p3:
106 // A name having namespace scope (3.3.6) has internal linkage if it
107 // is the name of
108 // - an object, reference, function or function template that is
109 // explicitly declared static; or,
110 // (This bullet corresponds to C99 6.2.2p3.)
111 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
112 // Explicitly declared static.
113 if (Var->getStorageClass() == VarDecl::Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000114 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000115
116 // - an object or reference that is explicitly declared const
117 // and neither explicitly declared extern nor previously
118 // declared to have external linkage; or
119 // (there is no equivalent in C99)
120 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000121 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000122 Var->getStorageClass() != VarDecl::Extern &&
123 Var->getStorageClass() != VarDecl::PrivateExtern) {
124 bool FoundExtern = false;
125 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
126 PrevVar && !FoundExtern;
127 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000128 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000129 FoundExtern = true;
130
131 if (!FoundExtern)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000132 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000133 }
134 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000135 // C++ [temp]p4:
136 // A non-member function template can have internal linkage; any
137 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000138 const FunctionDecl *Function = 0;
139 if (const FunctionTemplateDecl *FunTmpl
140 = dyn_cast<FunctionTemplateDecl>(D))
141 Function = FunTmpl->getTemplatedDecl();
142 else
143 Function = cast<FunctionDecl>(D);
144
145 // Explicitly declared static.
146 if (Function->getStorageClass() == FunctionDecl::Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000147 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000148 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
149 // - a data member of an anonymous union.
150 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000151 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000152 }
153
154 // C++ [basic.link]p4:
155
156 // A name having namespace scope has external linkage if it is the
157 // name of
158 //
159 // - an object or reference, unless it has internal linkage; or
160 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
161 if (!Context.getLangOptions().CPlusPlus &&
162 (Var->getStorageClass() == VarDecl::Extern ||
163 Var->getStorageClass() == VarDecl::PrivateExtern)) {
164 // C99 6.2.2p4:
165 // For an identifier declared with the storage-class specifier
166 // extern in a scope in which a prior declaration of that
167 // identifier is visible, if the prior declaration specifies
168 // internal or external linkage, the linkage of the identifier
169 // at the later declaration is the same as the linkage
170 // specified at the prior declaration. If no prior declaration
171 // is visible, or if the prior declaration specifies no
172 // linkage, then the identifier has external linkage.
173 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000174 if (Linkage L = PrevVar->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000175 return L;
176 }
177 }
178
179 // C99 6.2.2p5:
180 // If the declaration of an identifier for an object has file
181 // scope and no storage-class specifier, its linkage is
182 // external.
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000183 if (Var->isInAnonymousNamespace())
184 return UniqueExternalLinkage;
185
186 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000187 }
188
189 // - a function, unless it has internal linkage; or
190 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
191 // C99 6.2.2p5:
192 // If the declaration of an identifier for a function has no
193 // storage-class specifier, its linkage is determined exactly
194 // as if it were declared with the storage-class specifier
195 // extern.
196 if (!Context.getLangOptions().CPlusPlus &&
197 (Function->getStorageClass() == FunctionDecl::Extern ||
198 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
199 Function->getStorageClass() == FunctionDecl::None)) {
200 // C99 6.2.2p4:
201 // For an identifier declared with the storage-class specifier
202 // extern in a scope in which a prior declaration of that
203 // identifier is visible, if the prior declaration specifies
204 // internal or external linkage, the linkage of the identifier
205 // at the later declaration is the same as the linkage
206 // specified at the prior declaration. If no prior declaration
207 // is visible, or if the prior declaration specifies no
208 // linkage, then the identifier has external linkage.
209 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000210 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000211 return L;
212 }
213 }
214
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000215 if (Function->isInAnonymousNamespace())
216 return UniqueExternalLinkage;
217
218 if (FunctionTemplateSpecializationInfo *SpecInfo
219 = Function->getTemplateSpecializationInfo()) {
220 Linkage L = SpecInfo->getTemplate()->getLinkage();
221 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
222 L = minLinkage(L,
223 getLinkageForTemplateArgumentList(
224 TemplateArgs.getFlatArgumentList(),
225 TemplateArgs.flat_size()));
226 return L;
227 }
228
229 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000230 }
231
232 // - a named class (Clause 9), or an unnamed class defined in a
233 // typedef declaration in which the class has the typedef name
234 // for linkage purposes (7.1.3); or
235 // - a named enumeration (7.2), or an unnamed enumeration
236 // defined in a typedef declaration in which the enumeration
237 // has the typedef name for linkage purposes (7.1.3); or
238 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000239 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
240 if (Tag->isInAnonymousNamespace())
241 return UniqueExternalLinkage;
242
243 // If this is a class template specialization, consider the
244 // linkage of the template and template arguments.
245 if (const ClassTemplateSpecializationDecl *Spec
246 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
247 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
248 Linkage L = getLinkageForTemplateArgumentList(
249 TemplateArgs.getFlatArgumentList(),
250 TemplateArgs.flat_size());
251 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
252 }
253
254 return ExternalLinkage;
255 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000256
257 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000258 if (isa<EnumConstantDecl>(D)) {
259 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
260 if (isExternalLinkage(L))
261 return L;
262 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000263
264 // - a template, unless it is a function template that has
265 // internal linkage (Clause 14);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000266 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
267 if (D->isInAnonymousNamespace())
268 return UniqueExternalLinkage;
269
270 return getLinkageForTemplateParameterList(
271 Template->getTemplateParameters());
272 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000273
274 // - a namespace (7.3), unless it is declared within an unnamed
275 // namespace.
276 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000277 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000278
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000279 return NoLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000280}
281
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000282Linkage NamedDecl::getLinkage() const {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000283
284 // Objective-C: treat all Objective-C declarations as having external
285 // linkage.
286 switch (getKind()) {
287 default:
288 break;
289 case Decl::ObjCAtDefsField:
290 case Decl::ObjCCategory:
291 case Decl::ObjCCategoryImpl:
292 case Decl::ObjCClass:
293 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000294 case Decl::ObjCForwardProtocol:
295 case Decl::ObjCImplementation:
296 case Decl::ObjCInterface:
297 case Decl::ObjCIvar:
298 case Decl::ObjCMethod:
299 case Decl::ObjCProperty:
300 case Decl::ObjCPropertyImpl:
301 case Decl::ObjCProtocol:
302 return ExternalLinkage;
303 }
304
Douglas Gregord85b5b92009-11-25 22:24:25 +0000305 // Handle linkage for namespace-scope names.
306 if (getDeclContext()->getLookupContext()->isFileContext())
307 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
308 return L;
309
310 // C++ [basic.link]p5:
311 // In addition, a member function, static data member, a named
312 // class or enumeration of class scope, or an unnamed class or
313 // enumeration defined in a class-scope typedef declaration such
314 // that the class or enumeration has the typedef name for linkage
315 // purposes (7.1.3), has external linkage if the name of the class
316 // has external linkage.
317 if (getDeclContext()->isRecord() &&
318 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
319 (isa<TagDecl>(this) &&
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000320 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
321 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
322 if (isExternalLinkage(L))
323 return L;
324 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000325
326 // C++ [basic.link]p6:
327 // The name of a function declared in block scope and the name of
328 // an object declared by a block scope extern declaration have
329 // linkage. If there is a visible declaration of an entity with
330 // linkage having the same name and type, ignoring entities
331 // declared outside the innermost enclosing namespace scope, the
332 // block scope declaration declares that same entity and receives
333 // the linkage of the previous declaration. If there is more than
334 // one such matching entity, the program is ill-formed. Otherwise,
335 // if no matching entity is found, the block scope entity receives
336 // external linkage.
337 if (getLexicalDeclContext()->isFunctionOrMethod()) {
338 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
339 if (Function->getPreviousDeclaration())
340 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
341 return L;
342
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000343 if (Function->isInAnonymousNamespace())
344 return UniqueExternalLinkage;
345
Douglas Gregord85b5b92009-11-25 22:24:25 +0000346 return ExternalLinkage;
347 }
348
349 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
350 if (Var->getStorageClass() == VarDecl::Extern ||
351 Var->getStorageClass() == VarDecl::PrivateExtern) {
352 if (Var->getPreviousDeclaration())
353 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
354 return L;
355
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000356 if (Var->isInAnonymousNamespace())
357 return UniqueExternalLinkage;
358
Douglas Gregord85b5b92009-11-25 22:24:25 +0000359 return ExternalLinkage;
360 }
361 }
362
363 // C++ [basic.link]p6:
364 // Names not covered by these rules have no linkage.
365 return NoLinkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000366 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000367
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000368std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000369 return getQualifiedNameAsString(getASTContext().getLangOptions());
370}
371
372std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000373 const DeclContext *Ctx = getDeclContext();
374
375 if (Ctx->isFunctionOrMethod())
376 return getNameAsString();
377
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000378 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
379 ContextsTy Contexts;
380
381 // Collect contexts.
382 while (Ctx && isa<NamedDecl>(Ctx)) {
383 Contexts.push_back(Ctx);
384 Ctx = Ctx->getParent();
385 };
386
387 std::string QualName;
388 llvm::raw_string_ostream OS(QualName);
389
390 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
391 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000392 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000393 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000394 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
395 std::string TemplateArgsStr
396 = TemplateSpecializationType::PrintTemplateArgumentList(
397 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000398 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000399 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000400 OS << Spec->getName() << TemplateArgsStr;
401 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000402 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000403 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000404 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000405 OS << ND;
406 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
407 if (!RD->getIdentifier())
408 OS << "<anonymous " << RD->getKindName() << '>';
409 else
410 OS << RD;
411 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000412 const FunctionProtoType *FT = 0;
413 if (FD->hasWrittenPrototype())
414 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
415
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000416 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000417 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000418 unsigned NumParams = FD->getNumParams();
419 for (unsigned i = 0; i < NumParams; ++i) {
420 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000421 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000422 std::string Param;
423 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000424 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000425 }
426
427 if (FT->isVariadic()) {
428 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000429 OS << ", ";
430 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000431 }
432 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000433 OS << ')';
434 } else {
435 OS << cast<NamedDecl>(*I);
436 }
437 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000438 }
439
John McCall8472af42010-03-16 21:48:18 +0000440 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000441 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000442 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000443 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000444
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000445 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000446}
447
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000448bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000449 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
450
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000451 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
452 // We want to keep it, unless it nominates same namespace.
453 if (getKind() == Decl::UsingDirective) {
454 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
455 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
459 // For function declarations, we keep track of redeclarations.
460 return FD->getPreviousDeclaration() == OldD;
461
Douglas Gregore53060f2009-06-25 22:08:12 +0000462 // For function templates, the underlying function declarations are linked.
463 if (const FunctionTemplateDecl *FunctionTemplate
464 = dyn_cast<FunctionTemplateDecl>(this))
465 if (const FunctionTemplateDecl *OldFunctionTemplate
466 = dyn_cast<FunctionTemplateDecl>(OldD))
467 return FunctionTemplate->getTemplatedDecl()
468 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Steve Naroff0de21fd2009-02-22 19:35:57 +0000470 // For method declarations, we keep track of redeclarations.
471 if (isa<ObjCMethodDecl>(this))
472 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
John McCallf36e02d2009-10-09 21:13:30 +0000474 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
475 return true;
476
John McCall9488ea12009-11-17 05:59:44 +0000477 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
478 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
479 cast<UsingShadowDecl>(OldD)->getTargetDecl();
480
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000481 // For non-function declarations, if the declarations are of the
482 // same kind then this must be a redeclaration, or semantic analysis
483 // would not have given us the new declaration.
484 return this->getKind() == OldD->getKind();
485}
486
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000487bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000488 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000489}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000490
Anders Carlssone136e0e2009-06-26 06:29:23 +0000491NamedDecl *NamedDecl::getUnderlyingDecl() {
492 NamedDecl *ND = this;
493 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000494 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000495 ND = UD->getTargetDecl();
496 else if (ObjCCompatibleAliasDecl *AD
497 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
498 return AD->getClassInterface();
499 else
500 return ND;
501 }
502}
503
John McCall161755a2010-04-06 21:38:20 +0000504bool NamedDecl::isCXXInstanceMember() const {
505 assert(isCXXClassMember() &&
506 "checking whether non-member is instance member");
507
508 const NamedDecl *D = this;
509 if (isa<UsingShadowDecl>(D))
510 D = cast<UsingShadowDecl>(D)->getTargetDecl();
511
512 if (isa<FieldDecl>(D))
513 return true;
514 if (isa<CXXMethodDecl>(D))
515 return cast<CXXMethodDecl>(D)->isInstance();
516 if (isa<FunctionTemplateDecl>(D))
517 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
518 ->getTemplatedDecl())->isInstance();
519 return false;
520}
521
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000522//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000523// DeclaratorDecl Implementation
524//===----------------------------------------------------------------------===//
525
Douglas Gregor1693e152010-07-06 18:42:40 +0000526template <typename DeclT>
527static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
528 if (decl->getNumTemplateParameterLists() > 0)
529 return decl->getTemplateParameterList(0)->getTemplateLoc();
530 else
531 return decl->getInnerLocStart();
532}
533
John McCallb6217662010-03-15 10:12:16 +0000534DeclaratorDecl::~DeclaratorDecl() {}
535void DeclaratorDecl::Destroy(ASTContext &C) {
536 if (hasExtInfo())
537 C.Deallocate(getExtInfo());
538 ValueDecl::Destroy(C);
539}
540
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000541SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000542 TypeSourceInfo *TSI = getTypeSourceInfo();
543 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000544 return SourceLocation();
545}
546
John McCallb6217662010-03-15 10:12:16 +0000547void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
548 SourceRange QualifierRange) {
549 if (Qualifier) {
550 // Make sure the extended decl info is allocated.
551 if (!hasExtInfo()) {
552 // Save (non-extended) type source info pointer.
553 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
554 // Allocate external info struct.
555 DeclInfo = new (getASTContext()) ExtInfo;
556 // Restore savedTInfo into (extended) decl info.
557 getExtInfo()->TInfo = savedTInfo;
558 }
559 // Set qualifier info.
560 getExtInfo()->NNS = Qualifier;
561 getExtInfo()->NNSRange = QualifierRange;
562 }
563 else {
564 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
565 assert(QualifierRange.isInvalid());
566 if (hasExtInfo()) {
567 // Save type source info pointer.
568 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
569 // Deallocate the extended decl info.
570 getASTContext().Deallocate(getExtInfo());
571 // Restore savedTInfo into (non-extended) decl info.
572 DeclInfo = savedTInfo;
573 }
574 }
575}
576
Douglas Gregor1693e152010-07-06 18:42:40 +0000577SourceLocation DeclaratorDecl::getOuterLocStart() const {
578 return getTemplateOrInnerLocStart(this);
579}
580
Abramo Bagnara9b934882010-06-12 08:15:14 +0000581void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000582QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
583 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000584 TemplateParameterList **TPLists) {
585 assert((NumTPLists == 0 || TPLists != 0) &&
586 "Empty array of template parameters with positive size!");
587 assert((NumTPLists == 0 || NNS) &&
588 "Nonempty array of template parameters with no qualifier!");
589
590 // Free previous template parameters (if any).
591 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000592 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000593 TemplParamLists = 0;
594 NumTemplParamLists = 0;
595 }
596 // Set info on matched template parameter lists (if any).
597 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000598 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000599 NumTemplParamLists = NumTPLists;
600 for (unsigned i = NumTPLists; i-- > 0; )
601 TemplParamLists[i] = TPLists[i];
602 }
603}
604
Douglas Gregorc722ea42010-06-15 17:44:38 +0000605void QualifierInfo::Destroy(ASTContext &Context) {
606 // FIXME: Deallocate template parameter lists themselves!
607 if (TemplParamLists)
608 Context.Deallocate(TemplParamLists);
609}
610
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000611//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000612// VarDecl Implementation
613//===----------------------------------------------------------------------===//
614
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000615const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
616 switch (SC) {
617 case VarDecl::None: break;
618 case VarDecl::Auto: return "auto"; break;
619 case VarDecl::Extern: return "extern"; break;
620 case VarDecl::PrivateExtern: return "__private_extern__"; break;
621 case VarDecl::Register: return "register"; break;
622 case VarDecl::Static: return "static"; break;
623 }
624
625 assert(0 && "Invalid storage class");
626 return 0;
627}
628
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000629VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000630 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000631 StorageClass S, StorageClass SCAsWritten) {
632 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000633}
634
635void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000636 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000637 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000638 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000639 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
640 Eval->~EvaluatedStmt();
641 C.Deallocate(Eval);
642 }
643 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000644 this->~VarDecl();
John McCallb6217662010-03-15 10:12:16 +0000645 DeclaratorDecl::Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000646}
647
648VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000649}
650
Douglas Gregor1693e152010-07-06 18:42:40 +0000651SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000652 SourceLocation Start = getTypeSpecStartLoc();
653 if (Start.isInvalid())
654 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000655 return Start;
656}
657
658SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000659 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000660 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
661 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000662}
663
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000664bool VarDecl::isExternC() const {
665 ASTContext &Context = getASTContext();
666 if (!Context.getLangOptions().CPlusPlus)
667 return (getDeclContext()->isTranslationUnit() &&
668 getStorageClass() != Static) ||
669 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
670
671 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
672 DC = DC->getParent()) {
673 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
674 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
675 return getStorageClass() != Static;
676
677 break;
678 }
679
680 if (DC->isFunctionOrMethod())
681 return false;
682 }
683
684 return false;
685}
686
687VarDecl *VarDecl::getCanonicalDecl() {
688 return getFirstDeclaration();
689}
690
Sebastian Redle9d12b62010-01-31 22:27:38 +0000691VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
692 // C++ [basic.def]p2:
693 // A declaration is a definition unless [...] it contains the 'extern'
694 // specifier or a linkage-specification and neither an initializer [...],
695 // it declares a static data member in a class declaration [...].
696 // C++ [temp.expl.spec]p15:
697 // An explicit specialization of a static data member of a template is a
698 // definition if the declaration includes an initializer; otherwise, it is
699 // a declaration.
700 if (isStaticDataMember()) {
701 if (isOutOfLine() && (hasInit() ||
702 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
703 return Definition;
704 else
705 return DeclarationOnly;
706 }
707 // C99 6.7p5:
708 // A definition of an identifier is a declaration for that identifier that
709 // [...] causes storage to be reserved for that object.
710 // Note: that applies for all non-file-scope objects.
711 // C99 6.9.2p1:
712 // If the declaration of an identifier for an object has file scope and an
713 // initializer, the declaration is an external definition for the identifier
714 if (hasInit())
715 return Definition;
716 // AST for 'extern "C" int foo;' is annotated with 'extern'.
717 if (hasExternalStorage())
718 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000719
720 if (getStorageClassAsWritten() == Extern ||
721 getStorageClassAsWritten() == PrivateExtern) {
722 for (const VarDecl *PrevVar = getPreviousDeclaration();
723 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
724 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
725 return DeclarationOnly;
726 }
727 }
Sebastian Redle9d12b62010-01-31 22:27:38 +0000728 // C99 6.9.2p2:
729 // A declaration of an object that has file scope without an initializer,
730 // and without a storage class specifier or the scs 'static', constitutes
731 // a tentative definition.
732 // No such thing in C++.
733 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
734 return TentativeDefinition;
735
736 // What's left is (in C, block-scope) declarations without initializers or
737 // external storage. These are definitions.
738 return Definition;
739}
740
Sebastian Redle9d12b62010-01-31 22:27:38 +0000741VarDecl *VarDecl::getActingDefinition() {
742 DefinitionKind Kind = isThisDeclarationADefinition();
743 if (Kind != TentativeDefinition)
744 return 0;
745
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000746 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000747 VarDecl *First = getFirstDeclaration();
748 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
749 I != E; ++I) {
750 Kind = (*I)->isThisDeclarationADefinition();
751 if (Kind == Definition)
752 return 0;
753 else if (Kind == TentativeDefinition)
754 LastTentative = *I;
755 }
756 return LastTentative;
757}
758
759bool VarDecl::isTentativeDefinitionNow() const {
760 DefinitionKind Kind = isThisDeclarationADefinition();
761 if (Kind != TentativeDefinition)
762 return false;
763
764 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
765 if ((*I)->isThisDeclarationADefinition() == Definition)
766 return false;
767 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000768 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000769}
770
Sebastian Redl31310a22010-02-01 20:16:42 +0000771VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000772 VarDecl *First = getFirstDeclaration();
773 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
774 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000775 if ((*I)->isThisDeclarationADefinition() == Definition)
776 return *I;
777 }
778 return 0;
779}
780
781const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000782 redecl_iterator I = redecls_begin(), E = redecls_end();
783 while (I != E && !I->getInit())
784 ++I;
785
786 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000787 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000788 return I->getInit();
789 }
790 return 0;
791}
792
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000793bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000794 if (Decl::isOutOfLine())
795 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000796
797 if (!isStaticDataMember())
798 return false;
799
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000800 // If this static data member was instantiated from a static data member of
801 // a class template, check whether that static data member was defined
802 // out-of-line.
803 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
804 return VD->isOutOfLine();
805
806 return false;
807}
808
Douglas Gregor0d035142009-10-27 18:42:08 +0000809VarDecl *VarDecl::getOutOfLineDefinition() {
810 if (!isStaticDataMember())
811 return 0;
812
813 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
814 RD != RDEnd; ++RD) {
815 if (RD->getLexicalDeclContext()->isFileContext())
816 return *RD;
817 }
818
819 return 0;
820}
821
Douglas Gregor838db382010-02-11 01:19:42 +0000822void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000823 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
824 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000825 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000826 }
827
828 Init = I;
829}
830
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000831VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000832 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000833 return cast<VarDecl>(MSI->getInstantiatedFrom());
834
835 return 0;
836}
837
Douglas Gregor663b5a02009-10-14 20:14:33 +0000838TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +0000839 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000840 return MSI->getTemplateSpecializationKind();
841
842 return TSK_Undeclared;
843}
844
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000845MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000846 return getASTContext().getInstantiatedFromStaticDataMember(this);
847}
848
Douglas Gregor0a897e32009-10-15 17:21:20 +0000849void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
850 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000851 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000852 assert(MSI && "Not an instantiated static data member?");
853 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000854 if (TSK != TSK_ExplicitSpecialization &&
855 PointOfInstantiation.isValid() &&
856 MSI->getPointOfInstantiation().isInvalid())
857 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000858}
859
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000860//===----------------------------------------------------------------------===//
861// ParmVarDecl Implementation
862//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +0000863
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000864ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
865 SourceLocation L, IdentifierInfo *Id,
866 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000867 StorageClass S, StorageClass SCAsWritten,
868 Expr *DefArg) {
869 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
870 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +0000871}
872
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000873Expr *ParmVarDecl::getDefaultArg() {
874 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
875 assert(!hasUninstantiatedDefaultArg() &&
876 "Default argument is not yet instantiated!");
877
878 Expr *Arg = getInit();
879 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
880 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +0000881
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000882 return Arg;
883}
884
885unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
886 if (const CXXExprWithTemporaries *E =
887 dyn_cast<CXXExprWithTemporaries>(getInit()))
888 return E->getNumTemporaries();
889
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000890 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000891}
892
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000893CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
894 assert(getNumDefaultArgTemporaries() &&
895 "Default arguments does not have any temporaries!");
896
897 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
898 return E->getTemporary(i);
899}
900
901SourceRange ParmVarDecl::getDefaultArgRange() const {
902 if (const Expr *E = getInit())
903 return E->getSourceRange();
904
905 if (hasUninstantiatedDefaultArg())
906 return getUninstantiatedDefaultArg()->getSourceRange();
907
908 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000909}
910
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000911//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000912// FunctionDecl Implementation
913//===----------------------------------------------------------------------===//
914
Ted Kremenek27f8a282008-05-20 00:43:19 +0000915void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000916 if (Body && Body.isOffset())
917 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000918
919 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
920 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000921
Douglas Gregor2db32322009-10-07 23:56:10 +0000922 FunctionTemplateSpecializationInfo *FTSInfo
923 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
924 if (FTSInfo)
925 C.Deallocate(FTSInfo);
926
927 MemberSpecializationInfo *MSInfo
928 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
929 if (MSInfo)
930 C.Deallocate(MSInfo);
931
Steve Naroff3e970492009-01-27 21:25:57 +0000932 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000933
John McCallb6217662010-03-15 10:12:16 +0000934 DeclaratorDecl::Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +0000935}
936
John McCall136a6982009-09-11 06:45:03 +0000937void FunctionDecl::getNameForDiagnostic(std::string &S,
938 const PrintingPolicy &Policy,
939 bool Qualified) const {
940 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
941 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
942 if (TemplateArgs)
943 S += TemplateSpecializationType::PrintTemplateArgumentList(
944 TemplateArgs->getFlatArgumentList(),
945 TemplateArgs->flat_size(),
946 Policy);
947
948}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000949
Ted Kremenek9498d382010-04-29 16:49:01 +0000950bool FunctionDecl::isVariadic() const {
951 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
952 return FT->isVariadic();
953 return false;
954}
955
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000956bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
957 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
958 if (I->Body) {
959 Definition = *I;
960 return true;
961 }
962 }
963
964 return false;
965}
966
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000967Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000968 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
969 if (I->Body) {
970 Definition = *I;
971 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000972 }
973 }
974
975 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000976}
977
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000978void FunctionDecl::setBody(Stmt *B) {
979 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000980 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000981 EndRangeLoc = B->getLocEnd();
982}
983
Douglas Gregor48a83b52009-09-12 00:17:51 +0000984bool FunctionDecl::isMain() const {
985 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000986 return !Context.getLangOptions().Freestanding &&
987 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000988 getIdentifier() && getIdentifier()->isStr("main");
989}
990
Douglas Gregor48a83b52009-09-12 00:17:51 +0000991bool FunctionDecl::isExternC() const {
992 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000993 // In C, any non-static, non-overloadable function has external
994 // linkage.
995 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000996 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000997
Mike Stump1eb44332009-09-09 15:08:12 +0000998 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000999 DC = DC->getParent()) {
1000 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1001 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +00001002 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001003 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001004
1005 break;
1006 }
1007 }
1008
1009 return false;
1010}
1011
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001012bool FunctionDecl::isGlobal() const {
1013 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1014 return Method->isStatic();
1015
1016 if (getStorageClass() == Static)
1017 return false;
1018
Mike Stump1eb44332009-09-09 15:08:12 +00001019 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001020 DC->isNamespace();
1021 DC = DC->getParent()) {
1022 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1023 if (!Namespace->getDeclName())
1024 return false;
1025 break;
1026 }
1027 }
1028
1029 return true;
1030}
1031
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001032void
1033FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1034 redeclarable_base::setPreviousDeclaration(PrevDecl);
1035
1036 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1037 FunctionTemplateDecl *PrevFunTmpl
1038 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1039 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1040 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1041 }
1042}
1043
1044const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1045 return getFirstDeclaration();
1046}
1047
1048FunctionDecl *FunctionDecl::getCanonicalDecl() {
1049 return getFirstDeclaration();
1050}
1051
Douglas Gregor3e41d602009-02-13 23:20:09 +00001052/// \brief Returns a value indicating whether this function
1053/// corresponds to a builtin function.
1054///
1055/// The function corresponds to a built-in function if it is
1056/// declared at translation scope or within an extern "C" block and
1057/// its name matches with the name of a builtin. The returned value
1058/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001059/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001060/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001061unsigned FunctionDecl::getBuiltinID() const {
1062 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001063 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1064 return 0;
1065
1066 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1067 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1068 return BuiltinID;
1069
1070 // This function has the name of a known C library
1071 // function. Determine whether it actually refers to the C library
1072 // function or whether it just has the same name.
1073
Douglas Gregor9add3172009-02-17 03:23:10 +00001074 // If this is a static function, it's not a builtin.
1075 if (getStorageClass() == Static)
1076 return 0;
1077
Douglas Gregor3c385e52009-02-14 18:57:46 +00001078 // If this function is at translation-unit scope and we're not in
1079 // C++, it refers to the C library function.
1080 if (!Context.getLangOptions().CPlusPlus &&
1081 getDeclContext()->isTranslationUnit())
1082 return BuiltinID;
1083
1084 // If the function is in an extern "C" linkage specification and is
1085 // not marked "overloadable", it's the real function.
1086 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001087 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001088 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001089 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001090 return BuiltinID;
1091
1092 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001093 return 0;
1094}
1095
1096
Chris Lattner1ad9b282009-04-25 06:03:53 +00001097/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001098/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001099/// after it has been created.
1100unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001101 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001102 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001103 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001104 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Reid Spencer5f016e22007-07-11 17:01:13 +00001106}
1107
Douglas Gregor838db382010-02-11 01:19:42 +00001108void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001110 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 // Zero params -> null pointer.
1113 if (NumParams) {
Douglas Gregor838db382010-02-11 01:19:42 +00001114 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001115 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001117
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001118 // Update source range. The check below allows us to set EndRangeLoc before
1119 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001120 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001121 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 }
1123}
1124
Chris Lattner8123a952008-04-10 02:22:51 +00001125/// getMinRequiredArguments - Returns the minimum number of arguments
1126/// needed to call this function. This may be fewer than the number of
1127/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001128/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001129unsigned FunctionDecl::getMinRequiredArguments() const {
1130 unsigned NumRequiredArgs = getNumParams();
1131 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001132 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001133 --NumRequiredArgs;
1134
1135 return NumRequiredArgs;
1136}
1137
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001138bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001139 // FIXME: This is not enough. Consider:
1140 //
1141 // inline void f();
1142 // void f() { }
1143 //
1144 // f is inlined, but does not have inline specified.
1145 // To fix this we should add an 'inline' flag to FunctionDecl.
1146 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001147 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001148
1149 if (isa<CXXMethodDecl>(this)) {
1150 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1151 return true;
1152 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001153
1154 switch (getTemplateSpecializationKind()) {
1155 case TSK_Undeclared:
1156 case TSK_ExplicitSpecialization:
1157 return false;
1158
1159 case TSK_ImplicitInstantiation:
1160 case TSK_ExplicitInstantiationDeclaration:
1161 case TSK_ExplicitInstantiationDefinition:
1162 // Handle below.
1163 break;
1164 }
1165
1166 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001167 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001168 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001169 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001170
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001171 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001172 return PatternDecl->isInlined();
1173
1174 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001175}
1176
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001177/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001178/// definition will be externally visible.
1179///
1180/// Inline function definitions are always available for inlining optimizations.
1181/// However, depending on the language dialect, declaration specifiers, and
1182/// attributes, the definition of an inline function may or may not be
1183/// "externally" visible to other translation units in the program.
1184///
1185/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001186/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001187/// inline definition becomes externally visible (C99 6.7.4p6).
1188///
1189/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1190/// definition, we use the GNU semantics for inline, which are nearly the
1191/// opposite of C99 semantics. In particular, "inline" by itself will create
1192/// an externally visible symbol, but "extern inline" will not create an
1193/// externally visible symbol.
1194bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1195 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001196 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001197 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001198
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001199 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001200 // GNU inline semantics. Based on a number of examples, we came up with the
1201 // following heuristic: if the "inline" keyword is present on a
1202 // declaration of the function but "extern" is not present on that
1203 // declaration, then the symbol is externally visible. Otherwise, the GNU
1204 // "extern inline" semantics applies and the symbol is not externally
1205 // visible.
1206 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1207 Redecl != RedeclEnd;
1208 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001209 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001210 return true;
1211 }
1212
1213 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001214 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001215 }
1216
1217 // C99 6.7.4p6:
1218 // [...] If all of the file scope declarations for a function in a
1219 // translation unit include the inline function specifier without extern,
1220 // then the definition in that translation unit is an inline definition.
1221 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1222 Redecl != RedeclEnd;
1223 ++Redecl) {
1224 // Only consider file-scope declarations in this test.
1225 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1226 continue;
1227
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001228 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001229 return true; // Not an inline definition
1230 }
1231
1232 // C99 6.7.4p6:
1233 // An inline definition does not provide an external definition for the
1234 // function, and does not forbid an external definition in another
1235 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001236 return false;
1237}
1238
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001239/// getOverloadedOperator - Which C++ overloaded operator this
1240/// function represents, if any.
1241OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001242 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1243 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001244 else
1245 return OO_None;
1246}
1247
Sean Hunta6c058d2010-01-13 09:01:02 +00001248/// getLiteralIdentifier - The literal suffix identifier this function
1249/// represents, if any.
1250const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1251 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1252 return getDeclName().getCXXLiteralIdentifier();
1253 else
1254 return 0;
1255}
1256
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001257FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1258 if (TemplateOrSpecialization.isNull())
1259 return TK_NonTemplate;
1260 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1261 return TK_FunctionTemplate;
1262 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1263 return TK_MemberSpecialization;
1264 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1265 return TK_FunctionTemplateSpecialization;
1266 if (TemplateOrSpecialization.is
1267 <DependentFunctionTemplateSpecializationInfo*>())
1268 return TK_DependentFunctionTemplateSpecialization;
1269
1270 assert(false && "Did we miss a TemplateOrSpecialization type?");
1271 return TK_NonTemplate;
1272}
1273
Douglas Gregor2db32322009-10-07 23:56:10 +00001274FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001275 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001276 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1277
1278 return 0;
1279}
1280
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001281MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1282 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1283}
1284
Douglas Gregor2db32322009-10-07 23:56:10 +00001285void
1286FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1287 TemplateSpecializationKind TSK) {
1288 assert(TemplateOrSpecialization.isNull() &&
1289 "Member function is already a specialization");
1290 MemberSpecializationInfo *Info
1291 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1292 TemplateOrSpecialization = Info;
1293}
1294
Douglas Gregor3b846b62009-10-27 20:53:28 +00001295bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001296 // If the function is invalid, it can't be implicitly instantiated.
1297 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001298 return false;
1299
1300 switch (getTemplateSpecializationKind()) {
1301 case TSK_Undeclared:
1302 case TSK_ExplicitSpecialization:
1303 case TSK_ExplicitInstantiationDefinition:
1304 return false;
1305
1306 case TSK_ImplicitInstantiation:
1307 return true;
1308
1309 case TSK_ExplicitInstantiationDeclaration:
1310 // Handled below.
1311 break;
1312 }
1313
1314 // Find the actual template from which we will instantiate.
1315 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001316 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001317 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001318 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001319
1320 // C++0x [temp.explicit]p9:
1321 // Except for inline functions, other explicit instantiation declarations
1322 // have the effect of suppressing the implicit instantiation of the entity
1323 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001324 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001325 return true;
1326
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001327 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001328}
1329
1330FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1331 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1332 while (Primary->getInstantiatedFromMemberTemplate()) {
1333 // If we have hit a point where the user provided a specialization of
1334 // this template, we're done looking.
1335 if (Primary->isMemberSpecialization())
1336 break;
1337
1338 Primary = Primary->getInstantiatedFromMemberTemplate();
1339 }
1340
1341 return Primary->getTemplatedDecl();
1342 }
1343
1344 return getInstantiatedFromMemberFunction();
1345}
1346
Douglas Gregor16e8be22009-06-29 17:30:29 +00001347FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001348 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001349 = TemplateOrSpecialization
1350 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001351 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001352 }
1353 return 0;
1354}
1355
1356const TemplateArgumentList *
1357FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001358 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001359 = TemplateOrSpecialization
1360 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001361 return Info->TemplateArguments;
1362 }
1363 return 0;
1364}
1365
Abramo Bagnarae03db982010-05-20 15:32:11 +00001366const TemplateArgumentListInfo *
1367FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1368 if (FunctionTemplateSpecializationInfo *Info
1369 = TemplateOrSpecialization
1370 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1371 return Info->TemplateArgumentsAsWritten;
1372 }
1373 return 0;
1374}
1375
Mike Stump1eb44332009-09-09 15:08:12 +00001376void
Douglas Gregor838db382010-02-11 01:19:42 +00001377FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001378 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001379 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001380 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001381 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1382 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001383 assert(TSK != TSK_Undeclared &&
1384 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001385 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001386 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001387 if (!Info)
Douglas Gregor838db382010-02-11 01:19:42 +00001388 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Douglas Gregor127102b2009-06-29 20:59:39 +00001390 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001391 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001392 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001393 Info->TemplateArguments = TemplateArgs;
Abramo Bagnarae03db982010-05-20 15:32:11 +00001394 Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001395 Info->PointOfInstantiation = PointOfInstantiation;
Douglas Gregor1637be72009-06-26 00:10:03 +00001396 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregor127102b2009-06-29 20:59:39 +00001398 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001399 // function template specializations.
1400 if (InsertPos)
1401 Template->getSpecializations().InsertNode(Info, InsertPos);
1402 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001403 // Try to insert the new node. If there is an existing node, leave it, the
1404 // set will contain the canonical decls while
1405 // FunctionTemplateDecl::findSpecialization will return
1406 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001407 FunctionTemplateSpecializationInfo *Existing
1408 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001409 (void)Existing;
1410 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1411 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001412 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001413}
1414
John McCallaf2094e2010-04-08 09:05:18 +00001415void
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001416FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1417 unsigned NumTemplateArgs,
1418 const TemplateArgument *TemplateArgs,
1419 TemplateSpecializationKind TSK,
1420 unsigned NumTemplateArgsAsWritten,
1421 TemplateArgumentLoc *TemplateArgsAsWritten,
1422 SourceLocation LAngleLoc,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001423 SourceLocation RAngleLoc,
1424 SourceLocation PointOfInstantiation) {
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001425 ASTContext &Ctx = getASTContext();
1426 TemplateArgumentList *TemplArgs
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +00001427 = new (Ctx) TemplateArgumentList(Ctx, TemplateArgs, NumTemplateArgs);
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001428 TemplateArgumentListInfo *TemplArgsInfo
1429 = new (Ctx) TemplateArgumentListInfo(LAngleLoc, RAngleLoc);
1430 for (unsigned i=0; i != NumTemplateArgsAsWritten; ++i)
1431 TemplArgsInfo->addArgument(TemplateArgsAsWritten[i]);
1432
1433 setFunctionTemplateSpecialization(Template, TemplArgs, /*InsertPos=*/0, TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001434 TemplArgsInfo, PointOfInstantiation);
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001435}
1436
1437void
John McCallaf2094e2010-04-08 09:05:18 +00001438FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1439 const UnresolvedSetImpl &Templates,
1440 const TemplateArgumentListInfo &TemplateArgs) {
1441 assert(TemplateOrSpecialization.isNull());
1442 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1443 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001444 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001445 void *Buffer = Context.Allocate(Size);
1446 DependentFunctionTemplateSpecializationInfo *Info =
1447 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1448 TemplateArgs);
1449 TemplateOrSpecialization = Info;
1450}
1451
1452DependentFunctionTemplateSpecializationInfo::
1453DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1454 const TemplateArgumentListInfo &TArgs)
1455 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1456
1457 d.NumTemplates = Ts.size();
1458 d.NumArgs = TArgs.size();
1459
1460 FunctionTemplateDecl **TsArray =
1461 const_cast<FunctionTemplateDecl**>(getTemplates());
1462 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1463 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1464
1465 TemplateArgumentLoc *ArgsArray =
1466 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1467 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1468 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1469}
1470
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001471TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001472 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001473 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001474 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001475 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001476 if (FTSInfo)
1477 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Douglas Gregor2db32322009-10-07 23:56:10 +00001479 MemberSpecializationInfo *MSInfo
1480 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1481 if (MSInfo)
1482 return MSInfo->getTemplateSpecializationKind();
1483
1484 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001485}
1486
Mike Stump1eb44332009-09-09 15:08:12 +00001487void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001488FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1489 SourceLocation PointOfInstantiation) {
1490 if (FunctionTemplateSpecializationInfo *FTSInfo
1491 = TemplateOrSpecialization.dyn_cast<
1492 FunctionTemplateSpecializationInfo*>()) {
1493 FTSInfo->setTemplateSpecializationKind(TSK);
1494 if (TSK != TSK_ExplicitSpecialization &&
1495 PointOfInstantiation.isValid() &&
1496 FTSInfo->getPointOfInstantiation().isInvalid())
1497 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1498 } else if (MemberSpecializationInfo *MSInfo
1499 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1500 MSInfo->setTemplateSpecializationKind(TSK);
1501 if (TSK != TSK_ExplicitSpecialization &&
1502 PointOfInstantiation.isValid() &&
1503 MSInfo->getPointOfInstantiation().isInvalid())
1504 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1505 } else
1506 assert(false && "Function cannot have a template specialization kind");
1507}
1508
1509SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001510 if (FunctionTemplateSpecializationInfo *FTSInfo
1511 = TemplateOrSpecialization.dyn_cast<
1512 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001513 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001514 else if (MemberSpecializationInfo *MSInfo
1515 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001516 return MSInfo->getPointOfInstantiation();
1517
1518 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001519}
1520
Douglas Gregor9f185072009-09-11 20:15:17 +00001521bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001522 if (Decl::isOutOfLine())
1523 return true;
1524
1525 // If this function was instantiated from a member function of a
1526 // class template, check whether that member function was defined out-of-line.
1527 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1528 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001529 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001530 return Definition->isOutOfLine();
1531 }
1532
1533 // If this function was instantiated from a function template,
1534 // check whether that function template was defined out-of-line.
1535 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1536 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001537 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001538 return Definition->isOutOfLine();
1539 }
1540
1541 return false;
1542}
1543
Chris Lattner8a934232008-03-31 00:36:02 +00001544//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001545// FieldDecl Implementation
1546//===----------------------------------------------------------------------===//
1547
1548FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1549 IdentifierInfo *Id, QualType T,
1550 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1551 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1552}
1553
1554bool FieldDecl::isAnonymousStructOrUnion() const {
1555 if (!isImplicit() || getDeclName())
1556 return false;
1557
1558 if (const RecordType *Record = getType()->getAs<RecordType>())
1559 return Record->getDecl()->isAnonymousStructOrUnion();
1560
1561 return false;
1562}
1563
1564//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001565// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001566//===----------------------------------------------------------------------===//
1567
John McCallb6217662010-03-15 10:12:16 +00001568void TagDecl::Destroy(ASTContext &C) {
1569 if (hasExtInfo())
1570 C.Deallocate(getExtInfo());
1571 TypeDecl::Destroy(C);
1572}
1573
Douglas Gregor1693e152010-07-06 18:42:40 +00001574SourceLocation TagDecl::getOuterLocStart() const {
1575 return getTemplateOrInnerLocStart(this);
1576}
1577
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001578SourceRange TagDecl::getSourceRange() const {
1579 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001580 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001581}
1582
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001583TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001584 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001585}
1586
Douglas Gregor60e70642010-05-19 18:39:18 +00001587void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1588 TypedefDeclOrQualifier = TDD;
1589 if (TypeForDecl)
1590 TypeForDecl->ClearLinkageCache();
1591}
1592
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001593void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001594 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1595 TagT->decl.setPointer(this);
1596 TagT->decl.setInt(1);
Douglas Gregor9ffce212010-04-30 04:39:27 +00001597 } else if (InjectedClassNameType *Injected
1598 = const_cast<InjectedClassNameType *>(
1599 TypeForDecl->getAs<InjectedClassNameType>())) {
1600 Injected->Decl = cast<CXXRecordDecl>(this);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001601 }
John McCall86ff3082010-02-04 22:26:26 +00001602
1603 if (isa<CXXRecordDecl>(this)) {
1604 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1605 struct CXXRecordDecl::DefinitionData *Data =
1606 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001607 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1608 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001609 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001610}
1611
1612void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001613 assert((!isa<CXXRecordDecl>(this) ||
1614 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1615 "definition completed but not started");
1616
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001617 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001618 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1619 assert(TagT->decl.getPointer() == this &&
1620 "Attempt to redefine a tag definition?");
1621 TagT->decl.setInt(0);
Douglas Gregor9ffce212010-04-30 04:39:27 +00001622 } else if (InjectedClassNameType *Injected
1623 = const_cast<InjectedClassNameType *>(
1624 TypeForDecl->getAs<InjectedClassNameType>())) {
1625 assert(Injected->Decl == this &&
1626 "Attempt to redefine a class template definition?");
Chandler Carrutha8426972010-05-06 05:28:42 +00001627 (void)Injected;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001628 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001629}
1630
Douglas Gregor952b0172010-02-11 01:04:33 +00001631TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001632 if (isDefinition())
1633 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001634
1635 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001636 R != REnd; ++R)
1637 if (R->isDefinition())
1638 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001640 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001641}
1642
John McCallb6217662010-03-15 10:12:16 +00001643void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1644 SourceRange QualifierRange) {
1645 if (Qualifier) {
1646 // Make sure the extended qualifier info is allocated.
1647 if (!hasExtInfo())
1648 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1649 // Set qualifier info.
1650 getExtInfo()->NNS = Qualifier;
1651 getExtInfo()->NNSRange = QualifierRange;
1652 }
1653 else {
1654 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1655 assert(QualifierRange.isInvalid());
1656 if (hasExtInfo()) {
1657 getASTContext().Deallocate(getExtInfo());
1658 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1659 }
1660 }
1661}
1662
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001663//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001664// EnumDecl Implementation
1665//===----------------------------------------------------------------------===//
1666
1667EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1668 IdentifierInfo *Id, SourceLocation TKL,
1669 EnumDecl *PrevDecl) {
1670 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1671 C.getTypeDeclType(Enum, PrevDecl);
1672 return Enum;
1673}
1674
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001675EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
1676 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation());
1677}
1678
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001679void EnumDecl::Destroy(ASTContext& C) {
John McCallb6217662010-03-15 10:12:16 +00001680 TagDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001681}
1682
Douglas Gregor838db382010-02-11 01:19:42 +00001683void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001684 QualType NewPromotionType,
1685 unsigned NumPositiveBits,
1686 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001687 assert(!isDefinition() && "Cannot redefine enums!");
1688 IntegerType = NewType;
1689 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001690 setNumPositiveBits(NumPositiveBits);
1691 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001692 TagDecl::completeDefinition();
1693}
1694
1695//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001696// RecordDecl Implementation
1697//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001698
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001699RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001700 IdentifierInfo *Id, RecordDecl *PrevDecl,
1701 SourceLocation TKL)
1702 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001703 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001704 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001705 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001706 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001707}
1708
1709RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001710 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001711 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001713 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001714 C.getTypeDeclType(R, PrevDecl);
1715 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001716}
1717
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001718RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1719 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1720 SourceLocation());
1721}
1722
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001723RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001724}
1725
1726void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001727 TagDecl::Destroy(C);
1728}
1729
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001730bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001731 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001732 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1733}
1734
Douglas Gregor44b43212008-12-11 16:49:14 +00001735/// completeDefinition - Notes that the definition of this type is now
1736/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001737void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001739 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001740}
1741
John McCallbc365c52010-05-21 01:17:40 +00001742ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1743 // Force the decl chain to come into existence properly.
1744 if (!getNextDeclInContext()) getParent()->decls_begin();
1745
1746 assert(isAnonymousStructOrUnion());
1747 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1748 assert(D->getType()->isRecordType());
1749 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1750 return D;
1751}
1752
Steve Naroff56ee6892008-10-08 17:01:13 +00001753//===----------------------------------------------------------------------===//
1754// BlockDecl Implementation
1755//===----------------------------------------------------------------------===//
1756
1757BlockDecl::~BlockDecl() {
1758}
1759
1760void BlockDecl::Destroy(ASTContext& C) {
1761 if (Body)
1762 Body->Destroy(C);
1763
1764 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1765 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001766
1767 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001768 Decl::Destroy(C);
1769}
Steve Naroffe78b8092009-03-13 16:56:44 +00001770
Douglas Gregor838db382010-02-11 01:19:42 +00001771void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001772 unsigned NParms) {
1773 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Steve Naroffe78b8092009-03-13 16:56:44 +00001775 // Zero params -> null pointer.
1776 if (NParms) {
1777 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001778 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001779 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1780 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1781 }
1782}
1783
1784unsigned BlockDecl::getNumParams() const {
1785 return NumParams;
1786}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001787
1788
1789//===----------------------------------------------------------------------===//
1790// Other Decl Allocation/Deallocation Method Implementations
1791//===----------------------------------------------------------------------===//
1792
1793TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1794 return new (C) TranslationUnitDecl(C);
1795}
1796
1797NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1798 SourceLocation L, IdentifierInfo *Id) {
1799 return new (C) NamespaceDecl(DC, L, Id);
1800}
1801
1802void NamespaceDecl::Destroy(ASTContext& C) {
1803 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1804 // together. They are all top-level Decls.
1805
1806 this->~NamespaceDecl();
John McCallb6217662010-03-15 10:12:16 +00001807 Decl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001808}
1809
1810
1811ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1812 SourceLocation L, IdentifierInfo *Id, QualType T) {
1813 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1814}
1815
1816FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1817 SourceLocation L,
1818 DeclarationName N, QualType T,
1819 TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001820 StorageClass S, StorageClass SCAsWritten,
1821 bool isInline, bool hasWrittenPrototype) {
1822 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1823 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001824 New->HasWrittenPrototype = hasWrittenPrototype;
1825 return New;
1826}
1827
1828BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1829 return new (C) BlockDecl(DC, L);
1830}
1831
1832EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1833 SourceLocation L,
1834 IdentifierInfo *Id, QualType T,
1835 Expr *E, const llvm::APSInt &V) {
1836 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1837}
1838
1839void EnumConstantDecl::Destroy(ASTContext& C) {
1840 if (Init) Init->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001841 ValueDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001842}
1843
1844TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1845 SourceLocation L, IdentifierInfo *Id,
1846 TypeSourceInfo *TInfo) {
1847 return new (C) TypedefDecl(DC, L, Id, TInfo);
1848}
1849
1850// Anchor TypedefDecl's vtable here.
1851TypedefDecl::~TypedefDecl() {}
1852
1853FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1854 SourceLocation L,
1855 StringLiteral *Str) {
1856 return new (C) FileScopeAsmDecl(DC, L, Str);
1857}