blob: 1d94c20ef80c933174cc6703a726ecfff1fd0599 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000026#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2ada0482009-02-04 17:27:36 +000028#include <vector>
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000032/// \brief Return the TypeLoc wrapper for the type source info.
John McCallbcd03502009-12-07 02:54:59 +000033TypeLoc TypeSourceInfo::getTypeLoc() const {
Argyrios Kyrtzidis1b7c4ca2009-09-29 19:40:20 +000034 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000035}
Chris Lattner9631e182009-03-04 06:34:08 +000036
Chris Lattner88f70d62008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
Douglas Gregor7dc5c172010-02-03 09:33:45 +000041/// \brief Get the most restrictive linkage for the types in the given
42/// template parameter list.
43static Linkage
44getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
45 Linkage L = ExternalLinkage;
46 for (TemplateParameterList::const_iterator P = Params->begin(),
47 PEnd = Params->end();
48 P != PEnd; ++P) {
49 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
50 if (!NTTP->getType()->isDependentType()) {
51 L = minLinkage(L, NTTP->getType()->getLinkage());
52 continue;
53 }
54
55 if (TemplateTemplateParmDecl *TTP
56 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
57 L = minLinkage(L,
58 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
59 }
60 }
61
62 return L;
63}
64
65/// \brief Get the most restrictive linkage for the types and
66/// declarations in the given template argument list.
67static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
68 unsigned NumArgs) {
69 Linkage L = ExternalLinkage;
70
71 for (unsigned I = 0; I != NumArgs; ++I) {
72 switch (Args[I].getKind()) {
73 case TemplateArgument::Null:
74 case TemplateArgument::Integral:
75 case TemplateArgument::Expression:
76 break;
77
78 case TemplateArgument::Type:
79 L = minLinkage(L, Args[I].getAsType()->getLinkage());
80 break;
81
82 case TemplateArgument::Declaration:
83 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
84 L = minLinkage(L, ND->getLinkage());
85 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
86 L = minLinkage(L, VD->getType()->getLinkage());
87 break;
88
89 case TemplateArgument::Template:
90 if (TemplateDecl *Template
91 = Args[I].getAsTemplate().getAsTemplateDecl())
92 L = minLinkage(L, Template->getLinkage());
93 break;
94
95 case TemplateArgument::Pack:
96 L = minLinkage(L,
97 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
98 Args[I].pack_size()));
99 break;
100 }
101 }
102
103 return L;
104}
105
106static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000107 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
108 "Not a name having namespace scope");
109 ASTContext &Context = D->getASTContext();
110
111 // C++ [basic.link]p3:
112 // A name having namespace scope (3.3.6) has internal linkage if it
113 // is the name of
114 // - an object, reference, function or function template that is
115 // explicitly declared static; or,
116 // (This bullet corresponds to C99 6.2.2p3.)
117 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
118 // Explicitly declared static.
119 if (Var->getStorageClass() == VarDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000120 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000121
122 // - an object or reference that is explicitly declared const
123 // and neither explicitly declared extern nor previously
124 // declared to have external linkage; or
125 // (there is no equivalent in C99)
126 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000127 Var->getType().isConstant(Context) &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000128 Var->getStorageClass() != VarDecl::Extern &&
129 Var->getStorageClass() != VarDecl::PrivateExtern) {
130 bool FoundExtern = false;
131 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
132 PrevVar && !FoundExtern;
133 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000134 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000135 FoundExtern = true;
136
137 if (!FoundExtern)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000138 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000139 }
140 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000141 // C++ [temp]p4:
142 // A non-member function template can have internal linkage; any
143 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000144 const FunctionDecl *Function = 0;
145 if (const FunctionTemplateDecl *FunTmpl
146 = dyn_cast<FunctionTemplateDecl>(D))
147 Function = FunTmpl->getTemplatedDecl();
148 else
149 Function = cast<FunctionDecl>(D);
150
151 // Explicitly declared static.
152 if (Function->getStorageClass() == FunctionDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000153 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000154 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
155 // - a data member of an anonymous union.
156 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000157 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000158 }
159
160 // C++ [basic.link]p4:
161
162 // A name having namespace scope has external linkage if it is the
163 // name of
164 //
165 // - an object or reference, unless it has internal linkage; or
166 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
167 if (!Context.getLangOptions().CPlusPlus &&
168 (Var->getStorageClass() == VarDecl::Extern ||
169 Var->getStorageClass() == VarDecl::PrivateExtern)) {
170 // C99 6.2.2p4:
171 // For an identifier declared with the storage-class specifier
172 // extern in a scope in which a prior declaration of that
173 // identifier is visible, if the prior declaration specifies
174 // internal or external linkage, the linkage of the identifier
175 // at the later declaration is the same as the linkage
176 // specified at the prior declaration. If no prior declaration
177 // is visible, or if the prior declaration specifies no
178 // linkage, then the identifier has external linkage.
179 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000180 if (Linkage L = PrevVar->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000181 return L;
182 }
183 }
184
185 // C99 6.2.2p5:
186 // If the declaration of an identifier for an object has file
187 // scope and no storage-class specifier, its linkage is
188 // external.
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000189 if (Var->isInAnonymousNamespace())
190 return UniqueExternalLinkage;
191
192 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000193 }
194
195 // - a function, unless it has internal linkage; or
196 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
197 // C99 6.2.2p5:
198 // If the declaration of an identifier for a function has no
199 // storage-class specifier, its linkage is determined exactly
200 // as if it were declared with the storage-class specifier
201 // extern.
202 if (!Context.getLangOptions().CPlusPlus &&
203 (Function->getStorageClass() == FunctionDecl::Extern ||
204 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
205 Function->getStorageClass() == FunctionDecl::None)) {
206 // C99 6.2.2p4:
207 // For an identifier declared with the storage-class specifier
208 // extern in a scope in which a prior declaration of that
209 // identifier is visible, if the prior declaration specifies
210 // internal or external linkage, the linkage of the identifier
211 // at the later declaration is the same as the linkage
212 // specified at the prior declaration. If no prior declaration
213 // is visible, or if the prior declaration specifies no
214 // linkage, then the identifier has external linkage.
215 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000216 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000217 return L;
218 }
219 }
220
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000221 if (Function->isInAnonymousNamespace())
222 return UniqueExternalLinkage;
223
224 if (FunctionTemplateSpecializationInfo *SpecInfo
225 = Function->getTemplateSpecializationInfo()) {
226 Linkage L = SpecInfo->getTemplate()->getLinkage();
227 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
228 L = minLinkage(L,
229 getLinkageForTemplateArgumentList(
230 TemplateArgs.getFlatArgumentList(),
231 TemplateArgs.flat_size()));
232 return L;
233 }
234
235 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000236 }
237
238 // - a named class (Clause 9), or an unnamed class defined in a
239 // typedef declaration in which the class has the typedef name
240 // for linkage purposes (7.1.3); or
241 // - a named enumeration (7.2), or an unnamed enumeration
242 // defined in a typedef declaration in which the enumeration
243 // has the typedef name for linkage purposes (7.1.3); or
244 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000245 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
246 if (Tag->isInAnonymousNamespace())
247 return UniqueExternalLinkage;
248
249 // If this is a class template specialization, consider the
250 // linkage of the template and template arguments.
251 if (const ClassTemplateSpecializationDecl *Spec
252 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
253 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
254 Linkage L = getLinkageForTemplateArgumentList(
255 TemplateArgs.getFlatArgumentList(),
256 TemplateArgs.flat_size());
257 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
258 }
259
260 return ExternalLinkage;
261 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000262
263 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000264 if (isa<EnumConstantDecl>(D)) {
265 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
266 if (isExternalLinkage(L))
267 return L;
268 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000269
270 // - a template, unless it is a function template that has
271 // internal linkage (Clause 14);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000272 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
273 if (D->isInAnonymousNamespace())
274 return UniqueExternalLinkage;
275
276 return getLinkageForTemplateParameterList(
277 Template->getTemplateParameters());
278 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000279
280 // - a namespace (7.3), unless it is declared within an unnamed
281 // namespace.
282 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000283 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000284
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000285 return NoLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000286}
287
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000288Linkage NamedDecl::getLinkage() const {
Ted Kremenek926d8602010-04-20 23:15:35 +0000289
290 // Objective-C: treat all Objective-C declarations as having external
291 // linkage.
292 switch (getKind()) {
293 default:
294 break;
295 case Decl::ObjCAtDefsField:
296 case Decl::ObjCCategory:
297 case Decl::ObjCCategoryImpl:
298 case Decl::ObjCClass:
299 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000300 case Decl::ObjCForwardProtocol:
301 case Decl::ObjCImplementation:
302 case Decl::ObjCInterface:
303 case Decl::ObjCIvar:
304 case Decl::ObjCMethod:
305 case Decl::ObjCProperty:
306 case Decl::ObjCPropertyImpl:
307 case Decl::ObjCProtocol:
308 return ExternalLinkage;
309 }
310
Douglas Gregorf73b2822009-11-25 22:24:25 +0000311 // Handle linkage for namespace-scope names.
312 if (getDeclContext()->getLookupContext()->isFileContext())
313 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
314 return L;
315
316 // C++ [basic.link]p5:
317 // In addition, a member function, static data member, a named
318 // class or enumeration of class scope, or an unnamed class or
319 // enumeration defined in a class-scope typedef declaration such
320 // that the class or enumeration has the typedef name for linkage
321 // purposes (7.1.3), has external linkage if the name of the class
322 // has external linkage.
323 if (getDeclContext()->isRecord() &&
324 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
325 (isa<TagDecl>(this) &&
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000326 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
327 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
328 if (isExternalLinkage(L))
329 return L;
330 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000331
332 // C++ [basic.link]p6:
333 // The name of a function declared in block scope and the name of
334 // an object declared by a block scope extern declaration have
335 // linkage. If there is a visible declaration of an entity with
336 // linkage having the same name and type, ignoring entities
337 // declared outside the innermost enclosing namespace scope, the
338 // block scope declaration declares that same entity and receives
339 // the linkage of the previous declaration. If there is more than
340 // one such matching entity, the program is ill-formed. Otherwise,
341 // if no matching entity is found, the block scope entity receives
342 // external linkage.
343 if (getLexicalDeclContext()->isFunctionOrMethod()) {
344 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
345 if (Function->getPreviousDeclaration())
346 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
347 return L;
348
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000349 if (Function->isInAnonymousNamespace())
350 return UniqueExternalLinkage;
351
Douglas Gregorf73b2822009-11-25 22:24:25 +0000352 return ExternalLinkage;
353 }
354
355 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
356 if (Var->getStorageClass() == VarDecl::Extern ||
357 Var->getStorageClass() == VarDecl::PrivateExtern) {
358 if (Var->getPreviousDeclaration())
359 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
360 return L;
361
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000362 if (Var->isInAnonymousNamespace())
363 return UniqueExternalLinkage;
364
Douglas Gregorf73b2822009-11-25 22:24:25 +0000365 return ExternalLinkage;
366 }
367 }
368
369 // C++ [basic.link]p6:
370 // Names not covered by these rules have no linkage.
371 return NoLinkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000372 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000373
Douglas Gregor2ada0482009-02-04 17:27:36 +0000374std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000375 return getQualifiedNameAsString(getASTContext().getLangOptions());
376}
377
378std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000379 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
380 // std::string thrashing.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000381 std::vector<std::string> Names;
382 std::string QualName;
383 const DeclContext *Ctx = getDeclContext();
384
385 if (Ctx->isFunctionOrMethod())
386 return getNameAsString();
387
388 while (Ctx) {
Mike Stump11289f42009-09-09 15:08:12 +0000389 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000390 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
391 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
392 std::string TemplateArgsStr
393 = TemplateSpecializationType::PrintTemplateArgumentList(
394 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000395 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000396 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000397 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig07d211e2009-12-24 23:15:03 +0000398 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
399 if (ND->isAnonymousNamespace())
400 Names.push_back("<anonymous namespace>");
401 else
402 Names.push_back(ND->getNameAsString());
403 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
404 if (!RD->getIdentifier()) {
405 std::string RecordString = "<anonymous ";
406 RecordString += RD->getKindName();
407 RecordString += ">";
408 Names.push_back(RecordString);
409 } else {
410 Names.push_back(RD->getNameAsString());
411 }
Sam Weinigb999f682009-12-28 03:19:38 +0000412 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
413 std::string Proto = FD->getNameAsString();
414
415 const FunctionProtoType *FT = 0;
416 if (FD->hasWrittenPrototype())
417 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
418
419 Proto += "(";
420 if (FT) {
421 llvm::raw_string_ostream POut(Proto);
422 unsigned NumParams = FD->getNumParams();
423 for (unsigned i = 0; i < NumParams; ++i) {
424 if (i)
425 POut << ", ";
426 std::string Param;
427 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
428 POut << Param;
429 }
430
431 if (FT->isVariadic()) {
432 if (NumParams > 0)
433 POut << ", ";
434 POut << "...";
435 }
436 }
437 Proto += ")";
438
439 Names.push_back(Proto);
Douglas Gregor85673582009-05-18 17:01:57 +0000440 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000441 Names.push_back(ND->getNameAsString());
442 else
443 break;
444
445 Ctx = Ctx->getParent();
446 }
447
448 std::vector<std::string>::reverse_iterator
449 I = Names.rbegin(),
450 End = Names.rend();
451
452 for (; I!=End; ++I)
453 QualName += *I + "::";
454
John McCalla2a3f7d2010-03-16 21:48:18 +0000455 if (getDeclName())
456 QualName += getNameAsString();
457 else
458 QualName += "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000459
460 return QualName;
461}
462
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000463bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000464 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
465
Douglas Gregor889ceb72009-02-03 19:21:40 +0000466 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
467 // We want to keep it, unless it nominates same namespace.
468 if (getKind() == Decl::UsingDirective) {
469 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
470 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000473 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
474 // For function declarations, we keep track of redeclarations.
475 return FD->getPreviousDeclaration() == OldD;
476
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000477 // For function templates, the underlying function declarations are linked.
478 if (const FunctionTemplateDecl *FunctionTemplate
479 = dyn_cast<FunctionTemplateDecl>(this))
480 if (const FunctionTemplateDecl *OldFunctionTemplate
481 = dyn_cast<FunctionTemplateDecl>(OldD))
482 return FunctionTemplate->getTemplatedDecl()
483 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000484
Steve Naroffc4173fa2009-02-22 19:35:57 +0000485 // For method declarations, we keep track of redeclarations.
486 if (isa<ObjCMethodDecl>(this))
487 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000488
John McCall9f3059a2009-10-09 21:13:30 +0000489 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
490 return true;
491
John McCall3f746822009-11-17 05:59:44 +0000492 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
493 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
494 cast<UsingShadowDecl>(OldD)->getTargetDecl();
495
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000496 // For non-function declarations, if the declarations are of the
497 // same kind then this must be a redeclaration, or semantic analysis
498 // would not have given us the new declaration.
499 return this->getKind() == OldD->getKind();
500}
501
Douglas Gregoreddf4332009-02-24 20:03:32 +0000502bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000503 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000504}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000505
Anders Carlsson6915bf62009-06-26 06:29:23 +0000506NamedDecl *NamedDecl::getUnderlyingDecl() {
507 NamedDecl *ND = this;
508 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000509 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000510 ND = UD->getTargetDecl();
511 else if (ObjCCompatibleAliasDecl *AD
512 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
513 return AD->getClassInterface();
514 else
515 return ND;
516 }
517}
518
John McCalla8ae2222010-04-06 21:38:20 +0000519bool NamedDecl::isCXXInstanceMember() const {
520 assert(isCXXClassMember() &&
521 "checking whether non-member is instance member");
522
523 const NamedDecl *D = this;
524 if (isa<UsingShadowDecl>(D))
525 D = cast<UsingShadowDecl>(D)->getTargetDecl();
526
527 if (isa<FieldDecl>(D))
528 return true;
529 if (isa<CXXMethodDecl>(D))
530 return cast<CXXMethodDecl>(D)->isInstance();
531 if (isa<FunctionTemplateDecl>(D))
532 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
533 ->getTemplatedDecl())->isInstance();
534 return false;
535}
536
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000537//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000538// DeclaratorDecl Implementation
539//===----------------------------------------------------------------------===//
540
John McCall3e11ebe2010-03-15 10:12:16 +0000541DeclaratorDecl::~DeclaratorDecl() {}
542void DeclaratorDecl::Destroy(ASTContext &C) {
543 if (hasExtInfo())
544 C.Deallocate(getExtInfo());
545 ValueDecl::Destroy(C);
546}
547
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000548SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000549 if (DeclInfo) {
John McCall3e11ebe2010-03-15 10:12:16 +0000550 TypeLoc TL = getTypeSourceInfo()->getTypeLoc();
John McCall17001972009-10-18 01:05:36 +0000551 while (true) {
552 TypeLoc NextTL = TL.getNextTypeLoc();
553 if (!NextTL)
554 return TL.getSourceRange().getBegin();
555 TL = NextTL;
556 }
557 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000558 return SourceLocation();
559}
560
John McCall3e11ebe2010-03-15 10:12:16 +0000561void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
562 SourceRange QualifierRange) {
563 if (Qualifier) {
564 // Make sure the extended decl info is allocated.
565 if (!hasExtInfo()) {
566 // Save (non-extended) type source info pointer.
567 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
568 // Allocate external info struct.
569 DeclInfo = new (getASTContext()) ExtInfo;
570 // Restore savedTInfo into (extended) decl info.
571 getExtInfo()->TInfo = savedTInfo;
572 }
573 // Set qualifier info.
574 getExtInfo()->NNS = Qualifier;
575 getExtInfo()->NNSRange = QualifierRange;
576 }
577 else {
578 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
579 assert(QualifierRange.isInvalid());
580 if (hasExtInfo()) {
581 // Save type source info pointer.
582 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
583 // Deallocate the extended decl info.
584 getASTContext().Deallocate(getExtInfo());
585 // Restore savedTInfo into (non-extended) decl info.
586 DeclInfo = savedTInfo;
587 }
588 }
589}
590
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000591//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000592// VarDecl Implementation
593//===----------------------------------------------------------------------===//
594
Sebastian Redl833ef452010-01-26 22:01:41 +0000595const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
596 switch (SC) {
597 case VarDecl::None: break;
598 case VarDecl::Auto: return "auto"; break;
599 case VarDecl::Extern: return "extern"; break;
600 case VarDecl::PrivateExtern: return "__private_extern__"; break;
601 case VarDecl::Register: return "register"; break;
602 case VarDecl::Static: return "static"; break;
603 }
604
605 assert(0 && "Invalid storage class");
606 return 0;
607}
608
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000609VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000610 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000611 StorageClass S, StorageClass SCAsWritten) {
612 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000613}
614
615void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000616 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000617 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000618 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000619 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
620 Eval->~EvaluatedStmt();
621 C.Deallocate(Eval);
622 }
623 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000624 this->~VarDecl();
John McCall3e11ebe2010-03-15 10:12:16 +0000625 DeclaratorDecl::Destroy(C);
Nuno Lopes394ec982008-12-17 23:39:55 +0000626}
627
628VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000629}
630
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000631SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000632 SourceLocation Start = getTypeSpecStartLoc();
633 if (Start.isInvalid())
634 Start = getLocation();
635
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000636 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000637 return SourceRange(Start, getInit()->getLocEnd());
638 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000639}
640
Sebastian Redl833ef452010-01-26 22:01:41 +0000641bool VarDecl::isExternC() const {
642 ASTContext &Context = getASTContext();
643 if (!Context.getLangOptions().CPlusPlus)
644 return (getDeclContext()->isTranslationUnit() &&
645 getStorageClass() != Static) ||
646 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
647
648 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
649 DC = DC->getParent()) {
650 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
651 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
652 return getStorageClass() != Static;
653
654 break;
655 }
656
657 if (DC->isFunctionOrMethod())
658 return false;
659 }
660
661 return false;
662}
663
664VarDecl *VarDecl::getCanonicalDecl() {
665 return getFirstDeclaration();
666}
667
Sebastian Redl35351a92010-01-31 22:27:38 +0000668VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
669 // C++ [basic.def]p2:
670 // A declaration is a definition unless [...] it contains the 'extern'
671 // specifier or a linkage-specification and neither an initializer [...],
672 // it declares a static data member in a class declaration [...].
673 // C++ [temp.expl.spec]p15:
674 // An explicit specialization of a static data member of a template is a
675 // definition if the declaration includes an initializer; otherwise, it is
676 // a declaration.
677 if (isStaticDataMember()) {
678 if (isOutOfLine() && (hasInit() ||
679 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
680 return Definition;
681 else
682 return DeclarationOnly;
683 }
684 // C99 6.7p5:
685 // A definition of an identifier is a declaration for that identifier that
686 // [...] causes storage to be reserved for that object.
687 // Note: that applies for all non-file-scope objects.
688 // C99 6.9.2p1:
689 // If the declaration of an identifier for an object has file scope and an
690 // initializer, the declaration is an external definition for the identifier
691 if (hasInit())
692 return Definition;
693 // AST for 'extern "C" int foo;' is annotated with 'extern'.
694 if (hasExternalStorage())
695 return DeclarationOnly;
696
697 // C99 6.9.2p2:
698 // A declaration of an object that has file scope without an initializer,
699 // and without a storage class specifier or the scs 'static', constitutes
700 // a tentative definition.
701 // No such thing in C++.
702 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
703 return TentativeDefinition;
704
705 // What's left is (in C, block-scope) declarations without initializers or
706 // external storage. These are definitions.
707 return Definition;
708}
709
Sebastian Redl35351a92010-01-31 22:27:38 +0000710VarDecl *VarDecl::getActingDefinition() {
711 DefinitionKind Kind = isThisDeclarationADefinition();
712 if (Kind != TentativeDefinition)
713 return 0;
714
715 VarDecl *LastTentative = false;
716 VarDecl *First = getFirstDeclaration();
717 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
718 I != E; ++I) {
719 Kind = (*I)->isThisDeclarationADefinition();
720 if (Kind == Definition)
721 return 0;
722 else if (Kind == TentativeDefinition)
723 LastTentative = *I;
724 }
725 return LastTentative;
726}
727
728bool VarDecl::isTentativeDefinitionNow() const {
729 DefinitionKind Kind = isThisDeclarationADefinition();
730 if (Kind != TentativeDefinition)
731 return false;
732
733 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
734 if ((*I)->isThisDeclarationADefinition() == Definition)
735 return false;
736 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000737 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000738}
739
Sebastian Redl5ca79842010-02-01 20:16:42 +0000740VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000741 VarDecl *First = getFirstDeclaration();
742 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
743 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000744 if ((*I)->isThisDeclarationADefinition() == Definition)
745 return *I;
746 }
747 return 0;
748}
749
750const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000751 redecl_iterator I = redecls_begin(), E = redecls_end();
752 while (I != E && !I->getInit())
753 ++I;
754
755 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000756 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000757 return I->getInit();
758 }
759 return 0;
760}
761
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000762bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000763 if (Decl::isOutOfLine())
764 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000765
766 if (!isStaticDataMember())
767 return false;
768
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000769 // If this static data member was instantiated from a static data member of
770 // a class template, check whether that static data member was defined
771 // out-of-line.
772 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
773 return VD->isOutOfLine();
774
775 return false;
776}
777
Douglas Gregor1d957a32009-10-27 18:42:08 +0000778VarDecl *VarDecl::getOutOfLineDefinition() {
779 if (!isStaticDataMember())
780 return 0;
781
782 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
783 RD != RDEnd; ++RD) {
784 if (RD->getLexicalDeclContext()->isFileContext())
785 return *RD;
786 }
787
788 return 0;
789}
790
Douglas Gregord5058122010-02-11 01:19:42 +0000791void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000792 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
793 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000794 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000795 }
796
797 Init = I;
798}
799
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000800VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000801 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000802 return cast<VarDecl>(MSI->getInstantiatedFrom());
803
804 return 0;
805}
806
Douglas Gregor3c74d412009-10-14 20:14:33 +0000807TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000808 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000809 return MSI->getTemplateSpecializationKind();
810
811 return TSK_Undeclared;
812}
813
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000814MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000815 return getASTContext().getInstantiatedFromStaticDataMember(this);
816}
817
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000818void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
819 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000820 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000821 assert(MSI && "Not an instantiated static data member?");
822 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000823 if (TSK != TSK_ExplicitSpecialization &&
824 PointOfInstantiation.isValid() &&
825 MSI->getPointOfInstantiation().isInvalid())
826 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000827}
828
Sebastian Redl833ef452010-01-26 22:01:41 +0000829//===----------------------------------------------------------------------===//
830// ParmVarDecl Implementation
831//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000832
Sebastian Redl833ef452010-01-26 22:01:41 +0000833ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
834 SourceLocation L, IdentifierInfo *Id,
835 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000836 StorageClass S, StorageClass SCAsWritten,
837 Expr *DefArg) {
838 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
839 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000840}
841
Sebastian Redl833ef452010-01-26 22:01:41 +0000842Expr *ParmVarDecl::getDefaultArg() {
843 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
844 assert(!hasUninstantiatedDefaultArg() &&
845 "Default argument is not yet instantiated!");
846
847 Expr *Arg = getInit();
848 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
849 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000850
Sebastian Redl833ef452010-01-26 22:01:41 +0000851 return Arg;
852}
853
854unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
855 if (const CXXExprWithTemporaries *E =
856 dyn_cast<CXXExprWithTemporaries>(getInit()))
857 return E->getNumTemporaries();
858
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000859 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000860}
861
Sebastian Redl833ef452010-01-26 22:01:41 +0000862CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
863 assert(getNumDefaultArgTemporaries() &&
864 "Default arguments does not have any temporaries!");
865
866 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
867 return E->getTemporary(i);
868}
869
870SourceRange ParmVarDecl::getDefaultArgRange() const {
871 if (const Expr *E = getInit())
872 return E->getSourceRange();
873
874 if (hasUninstantiatedDefaultArg())
875 return getUninstantiatedDefaultArg()->getSourceRange();
876
877 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000878}
879
Nuno Lopes394ec982008-12-17 23:39:55 +0000880//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000881// FunctionDecl Implementation
882//===----------------------------------------------------------------------===//
883
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000884void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000885 if (Body && Body.isOffset())
886 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000887
888 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
889 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000890
Douglas Gregord801b062009-10-07 23:56:10 +0000891 FunctionTemplateSpecializationInfo *FTSInfo
892 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
893 if (FTSInfo)
894 C.Deallocate(FTSInfo);
895
896 MemberSpecializationInfo *MSInfo
897 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
898 if (MSInfo)
899 C.Deallocate(MSInfo);
900
Steve Naroff13ae6f42009-01-27 21:25:57 +0000901 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000902
John McCall3e11ebe2010-03-15 10:12:16 +0000903 DeclaratorDecl::Destroy(C);
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000904}
905
John McCalle1f2ec22009-09-11 06:45:03 +0000906void FunctionDecl::getNameForDiagnostic(std::string &S,
907 const PrintingPolicy &Policy,
908 bool Qualified) const {
909 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
910 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
911 if (TemplateArgs)
912 S += TemplateSpecializationType::PrintTemplateArgumentList(
913 TemplateArgs->getFlatArgumentList(),
914 TemplateArgs->flat_size(),
915 Policy);
916
917}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000918
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000919Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000920 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
921 if (I->Body) {
922 Definition = *I;
923 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000924 }
925 }
926
927 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000928}
929
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000930void FunctionDecl::setBody(Stmt *B) {
931 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000932 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000933 EndRangeLoc = B->getLocEnd();
934}
935
Douglas Gregor16618f22009-09-12 00:17:51 +0000936bool FunctionDecl::isMain() const {
937 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000938 return !Context.getLangOptions().Freestanding &&
939 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000940 getIdentifier() && getIdentifier()->isStr("main");
941}
942
Douglas Gregor16618f22009-09-12 00:17:51 +0000943bool FunctionDecl::isExternC() const {
944 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000945 // In C, any non-static, non-overloadable function has external
946 // linkage.
947 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000948 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000949
Mike Stump11289f42009-09-09 15:08:12 +0000950 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000951 DC = DC->getParent()) {
952 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
953 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000954 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000955 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000956
957 break;
958 }
959 }
960
961 return false;
962}
963
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000964bool FunctionDecl::isGlobal() const {
965 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
966 return Method->isStatic();
967
968 if (getStorageClass() == Static)
969 return false;
970
Mike Stump11289f42009-09-09 15:08:12 +0000971 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000972 DC->isNamespace();
973 DC = DC->getParent()) {
974 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
975 if (!Namespace->getDeclName())
976 return false;
977 break;
978 }
979 }
980
981 return true;
982}
983
Sebastian Redl833ef452010-01-26 22:01:41 +0000984void
985FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
986 redeclarable_base::setPreviousDeclaration(PrevDecl);
987
988 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
989 FunctionTemplateDecl *PrevFunTmpl
990 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
991 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
992 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
993 }
994}
995
996const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
997 return getFirstDeclaration();
998}
999
1000FunctionDecl *FunctionDecl::getCanonicalDecl() {
1001 return getFirstDeclaration();
1002}
1003
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001004/// \brief Returns a value indicating whether this function
1005/// corresponds to a builtin function.
1006///
1007/// The function corresponds to a built-in function if it is
1008/// declared at translation scope or within an extern "C" block and
1009/// its name matches with the name of a builtin. The returned value
1010/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001011/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001012/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001013unsigned FunctionDecl::getBuiltinID() const {
1014 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001015 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1016 return 0;
1017
1018 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1019 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1020 return BuiltinID;
1021
1022 // This function has the name of a known C library
1023 // function. Determine whether it actually refers to the C library
1024 // function or whether it just has the same name.
1025
Douglas Gregora908e7f2009-02-17 03:23:10 +00001026 // If this is a static function, it's not a builtin.
1027 if (getStorageClass() == Static)
1028 return 0;
1029
Douglas Gregore711f702009-02-14 18:57:46 +00001030 // If this function is at translation-unit scope and we're not in
1031 // C++, it refers to the C library function.
1032 if (!Context.getLangOptions().CPlusPlus &&
1033 getDeclContext()->isTranslationUnit())
1034 return BuiltinID;
1035
1036 // If the function is in an extern "C" linkage specification and is
1037 // not marked "overloadable", it's the real function.
1038 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001039 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001040 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001041 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001042 return BuiltinID;
1043
1044 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001045 return 0;
1046}
1047
1048
Chris Lattner47c0d002009-04-25 06:03:53 +00001049/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001050/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001051/// after it has been created.
1052unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001053 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001054 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001055 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001056 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001057
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001058}
1059
Douglas Gregord5058122010-02-11 01:19:42 +00001060void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001061 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001062 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001063
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001064 // Zero params -> null pointer.
1065 if (NumParams) {
Douglas Gregord5058122010-02-11 01:19:42 +00001066 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001067 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001068 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001069
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001070 // Update source range. The check below allows us to set EndRangeLoc before
1071 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001072 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001073 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001074 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001075}
Chris Lattner41943152007-01-25 04:52:46 +00001076
Chris Lattner58258242008-04-10 02:22:51 +00001077/// getMinRequiredArguments - Returns the minimum number of arguments
1078/// needed to call this function. This may be fewer than the number of
1079/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001080/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001081unsigned FunctionDecl::getMinRequiredArguments() const {
1082 unsigned NumRequiredArgs = getNumParams();
1083 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001084 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001085 --NumRequiredArgs;
1086
1087 return NumRequiredArgs;
1088}
1089
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001090bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001091 // FIXME: This is not enough. Consider:
1092 //
1093 // inline void f();
1094 // void f() { }
1095 //
1096 // f is inlined, but does not have inline specified.
1097 // To fix this we should add an 'inline' flag to FunctionDecl.
1098 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001099 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001100
1101 if (isa<CXXMethodDecl>(this)) {
1102 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1103 return true;
1104 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001105
1106 switch (getTemplateSpecializationKind()) {
1107 case TSK_Undeclared:
1108 case TSK_ExplicitSpecialization:
1109 return false;
1110
1111 case TSK_ImplicitInstantiation:
1112 case TSK_ExplicitInstantiationDeclaration:
1113 case TSK_ExplicitInstantiationDefinition:
1114 // Handle below.
1115 break;
1116 }
1117
1118 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1119 Stmt *Pattern = 0;
1120 if (PatternDecl)
1121 Pattern = PatternDecl->getBody(PatternDecl);
1122
1123 if (Pattern && PatternDecl)
1124 return PatternDecl->isInlined();
1125
1126 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001127}
1128
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001129/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001130/// definition will be externally visible.
1131///
1132/// Inline function definitions are always available for inlining optimizations.
1133/// However, depending on the language dialect, declaration specifiers, and
1134/// attributes, the definition of an inline function may or may not be
1135/// "externally" visible to other translation units in the program.
1136///
1137/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001138/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001139/// inline definition becomes externally visible (C99 6.7.4p6).
1140///
1141/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1142/// definition, we use the GNU semantics for inline, which are nearly the
1143/// opposite of C99 semantics. In particular, "inline" by itself will create
1144/// an externally visible symbol, but "extern inline" will not create an
1145/// externally visible symbol.
1146bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1147 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001148 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001149 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001150
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001151 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001152 // GNU inline semantics. Based on a number of examples, we came up with the
1153 // following heuristic: if the "inline" keyword is present on a
1154 // declaration of the function but "extern" is not present on that
1155 // declaration, then the symbol is externally visible. Otherwise, the GNU
1156 // "extern inline" semantics applies and the symbol is not externally
1157 // visible.
1158 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1159 Redecl != RedeclEnd;
1160 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001161 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001162 return true;
1163 }
1164
1165 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001166 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001167 }
1168
1169 // C99 6.7.4p6:
1170 // [...] If all of the file scope declarations for a function in a
1171 // translation unit include the inline function specifier without extern,
1172 // then the definition in that translation unit is an inline definition.
1173 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1174 Redecl != RedeclEnd;
1175 ++Redecl) {
1176 // Only consider file-scope declarations in this test.
1177 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1178 continue;
1179
Douglas Gregor35b57532009-10-27 21:01:01 +00001180 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001181 return true; // Not an inline definition
1182 }
1183
1184 // C99 6.7.4p6:
1185 // An inline definition does not provide an external definition for the
1186 // function, and does not forbid an external definition in another
1187 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001188 return false;
1189}
1190
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001191/// getOverloadedOperator - Which C++ overloaded operator this
1192/// function represents, if any.
1193OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001194 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1195 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001196 else
1197 return OO_None;
1198}
1199
Alexis Huntc88db062010-01-13 09:01:02 +00001200/// getLiteralIdentifier - The literal suffix identifier this function
1201/// represents, if any.
1202const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1203 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1204 return getDeclName().getCXXLiteralIdentifier();
1205 else
1206 return 0;
1207}
1208
Douglas Gregord801b062009-10-07 23:56:10 +00001209FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001210 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001211 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1212
1213 return 0;
1214}
1215
Douglas Gregor06db9f52009-10-12 20:18:28 +00001216MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1217 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1218}
1219
Douglas Gregord801b062009-10-07 23:56:10 +00001220void
1221FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1222 TemplateSpecializationKind TSK) {
1223 assert(TemplateOrSpecialization.isNull() &&
1224 "Member function is already a specialization");
1225 MemberSpecializationInfo *Info
1226 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1227 TemplateOrSpecialization = Info;
1228}
1229
Douglas Gregorafca3b42009-10-27 20:53:28 +00001230bool FunctionDecl::isImplicitlyInstantiable() const {
1231 // If this function already has a definition or is invalid, it can't be
1232 // implicitly instantiated.
1233 if (isInvalidDecl() || getBody())
1234 return false;
1235
1236 switch (getTemplateSpecializationKind()) {
1237 case TSK_Undeclared:
1238 case TSK_ExplicitSpecialization:
1239 case TSK_ExplicitInstantiationDefinition:
1240 return false;
1241
1242 case TSK_ImplicitInstantiation:
1243 return true;
1244
1245 case TSK_ExplicitInstantiationDeclaration:
1246 // Handled below.
1247 break;
1248 }
1249
1250 // Find the actual template from which we will instantiate.
1251 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1252 Stmt *Pattern = 0;
1253 if (PatternDecl)
1254 Pattern = PatternDecl->getBody(PatternDecl);
1255
1256 // C++0x [temp.explicit]p9:
1257 // Except for inline functions, other explicit instantiation declarations
1258 // have the effect of suppressing the implicit instantiation of the entity
1259 // to which they refer.
1260 if (!Pattern || !PatternDecl)
1261 return true;
1262
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001263 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001264}
1265
1266FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1267 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1268 while (Primary->getInstantiatedFromMemberTemplate()) {
1269 // If we have hit a point where the user provided a specialization of
1270 // this template, we're done looking.
1271 if (Primary->isMemberSpecialization())
1272 break;
1273
1274 Primary = Primary->getInstantiatedFromMemberTemplate();
1275 }
1276
1277 return Primary->getTemplatedDecl();
1278 }
1279
1280 return getInstantiatedFromMemberFunction();
1281}
1282
Douglas Gregor70d83e22009-06-29 17:30:29 +00001283FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001284 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001285 = TemplateOrSpecialization
1286 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001287 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001288 }
1289 return 0;
1290}
1291
1292const TemplateArgumentList *
1293FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001294 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001295 = TemplateOrSpecialization
1296 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001297 return Info->TemplateArguments;
1298 }
1299 return 0;
1300}
1301
Mike Stump11289f42009-09-09 15:08:12 +00001302void
Douglas Gregord5058122010-02-11 01:19:42 +00001303FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001304 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001305 void *InsertPos,
1306 TemplateSpecializationKind TSK) {
1307 assert(TSK != TSK_Undeclared &&
1308 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001309 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001310 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001311 if (!Info)
Douglas Gregord5058122010-02-11 01:19:42 +00001312 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001313
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001314 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001315 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001316 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001317 Info->TemplateArguments = TemplateArgs;
1318 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001319
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001320 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001321 // function template specializations.
1322 if (InsertPos)
1323 Template->getSpecializations().InsertNode(Info, InsertPos);
1324 else {
1325 // Try to insert the new node. If there is an existing node, remove it
1326 // first.
1327 FunctionTemplateSpecializationInfo *Existing
1328 = Template->getSpecializations().GetOrInsertNode(Info);
1329 if (Existing) {
1330 Template->getSpecializations().RemoveNode(Existing);
1331 Template->getSpecializations().GetOrInsertNode(Info);
1332 }
1333 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001334}
1335
John McCallb9c78482010-04-08 09:05:18 +00001336void
1337FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1338 const UnresolvedSetImpl &Templates,
1339 const TemplateArgumentListInfo &TemplateArgs) {
1340 assert(TemplateOrSpecialization.isNull());
1341 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1342 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001343 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001344 void *Buffer = Context.Allocate(Size);
1345 DependentFunctionTemplateSpecializationInfo *Info =
1346 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1347 TemplateArgs);
1348 TemplateOrSpecialization = Info;
1349}
1350
1351DependentFunctionTemplateSpecializationInfo::
1352DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1353 const TemplateArgumentListInfo &TArgs)
1354 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1355
1356 d.NumTemplates = Ts.size();
1357 d.NumArgs = TArgs.size();
1358
1359 FunctionTemplateDecl **TsArray =
1360 const_cast<FunctionTemplateDecl**>(getTemplates());
1361 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1362 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1363
1364 TemplateArgumentLoc *ArgsArray =
1365 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1366 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1367 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1368}
1369
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001370TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001371 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001372 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001373 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001374 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001375 if (FTSInfo)
1376 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001377
Douglas Gregord801b062009-10-07 23:56:10 +00001378 MemberSpecializationInfo *MSInfo
1379 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1380 if (MSInfo)
1381 return MSInfo->getTemplateSpecializationKind();
1382
1383 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001384}
1385
Mike Stump11289f42009-09-09 15:08:12 +00001386void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001387FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1388 SourceLocation PointOfInstantiation) {
1389 if (FunctionTemplateSpecializationInfo *FTSInfo
1390 = TemplateOrSpecialization.dyn_cast<
1391 FunctionTemplateSpecializationInfo*>()) {
1392 FTSInfo->setTemplateSpecializationKind(TSK);
1393 if (TSK != TSK_ExplicitSpecialization &&
1394 PointOfInstantiation.isValid() &&
1395 FTSInfo->getPointOfInstantiation().isInvalid())
1396 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1397 } else if (MemberSpecializationInfo *MSInfo
1398 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1399 MSInfo->setTemplateSpecializationKind(TSK);
1400 if (TSK != TSK_ExplicitSpecialization &&
1401 PointOfInstantiation.isValid() &&
1402 MSInfo->getPointOfInstantiation().isInvalid())
1403 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1404 } else
1405 assert(false && "Function cannot have a template specialization kind");
1406}
1407
1408SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001409 if (FunctionTemplateSpecializationInfo *FTSInfo
1410 = TemplateOrSpecialization.dyn_cast<
1411 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001412 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001413 else if (MemberSpecializationInfo *MSInfo
1414 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001415 return MSInfo->getPointOfInstantiation();
1416
1417 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001418}
1419
Douglas Gregor6411b922009-09-11 20:15:17 +00001420bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001421 if (Decl::isOutOfLine())
1422 return true;
1423
1424 // If this function was instantiated from a member function of a
1425 // class template, check whether that member function was defined out-of-line.
1426 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1427 const FunctionDecl *Definition;
1428 if (FD->getBody(Definition))
1429 return Definition->isOutOfLine();
1430 }
1431
1432 // If this function was instantiated from a function template,
1433 // check whether that function template was defined out-of-line.
1434 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1435 const FunctionDecl *Definition;
1436 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1437 return Definition->isOutOfLine();
1438 }
1439
1440 return false;
1441}
1442
Chris Lattner59a25942008-03-31 00:36:02 +00001443//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001444// FieldDecl Implementation
1445//===----------------------------------------------------------------------===//
1446
1447FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1448 IdentifierInfo *Id, QualType T,
1449 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1450 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1451}
1452
1453bool FieldDecl::isAnonymousStructOrUnion() const {
1454 if (!isImplicit() || getDeclName())
1455 return false;
1456
1457 if (const RecordType *Record = getType()->getAs<RecordType>())
1458 return Record->getDecl()->isAnonymousStructOrUnion();
1459
1460 return false;
1461}
1462
1463//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001464// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001465//===----------------------------------------------------------------------===//
1466
John McCall3e11ebe2010-03-15 10:12:16 +00001467void TagDecl::Destroy(ASTContext &C) {
1468 if (hasExtInfo())
1469 C.Deallocate(getExtInfo());
1470 TypeDecl::Destroy(C);
1471}
1472
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001473SourceRange TagDecl::getSourceRange() const {
1474 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001475 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001476}
1477
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001478TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001479 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001480}
1481
Douglas Gregordee1be82009-01-17 00:42:38 +00001482void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001483 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1484 TagT->decl.setPointer(this);
1485 TagT->decl.setInt(1);
1486 }
John McCall67da35c2010-02-04 22:26:26 +00001487
1488 if (isa<CXXRecordDecl>(this)) {
1489 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1490 struct CXXRecordDecl::DefinitionData *Data =
1491 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001492 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1493 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001494 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001495}
1496
1497void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001498 assert((!isa<CXXRecordDecl>(this) ||
1499 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1500 "definition completed but not started");
1501
Douglas Gregordee1be82009-01-17 00:42:38 +00001502 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001503 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1504 assert(TagT->decl.getPointer() == this &&
1505 "Attempt to redefine a tag definition?");
1506 TagT->decl.setInt(0);
1507 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001508}
1509
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001510TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001511 if (isDefinition())
1512 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001513
1514 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001515 R != REnd; ++R)
1516 if (R->isDefinition())
1517 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001518
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001519 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001520}
1521
John McCall06f6fe8d2009-09-04 01:14:41 +00001522TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1523 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001524 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001525 case DeclSpec::TST_struct: return TK_struct;
1526 case DeclSpec::TST_class: return TK_class;
1527 case DeclSpec::TST_union: return TK_union;
1528 case DeclSpec::TST_enum: return TK_enum;
1529 }
1530}
1531
John McCall3e11ebe2010-03-15 10:12:16 +00001532void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1533 SourceRange QualifierRange) {
1534 if (Qualifier) {
1535 // Make sure the extended qualifier info is allocated.
1536 if (!hasExtInfo())
1537 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1538 // Set qualifier info.
1539 getExtInfo()->NNS = Qualifier;
1540 getExtInfo()->NNSRange = QualifierRange;
1541 }
1542 else {
1543 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1544 assert(QualifierRange.isInvalid());
1545 if (hasExtInfo()) {
1546 getASTContext().Deallocate(getExtInfo());
1547 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1548 }
1549 }
1550}
1551
Ted Kremenek21475702008-09-05 17:16:31 +00001552//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001553// EnumDecl Implementation
1554//===----------------------------------------------------------------------===//
1555
1556EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1557 IdentifierInfo *Id, SourceLocation TKL,
1558 EnumDecl *PrevDecl) {
1559 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1560 C.getTypeDeclType(Enum, PrevDecl);
1561 return Enum;
1562}
1563
1564void EnumDecl::Destroy(ASTContext& C) {
John McCall3e11ebe2010-03-15 10:12:16 +00001565 TagDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001566}
1567
Douglas Gregord5058122010-02-11 01:19:42 +00001568void EnumDecl::completeDefinition(QualType NewType,
Sebastian Redl833ef452010-01-26 22:01:41 +00001569 QualType NewPromotionType) {
1570 assert(!isDefinition() && "Cannot redefine enums!");
1571 IntegerType = NewType;
1572 PromotionType = NewPromotionType;
1573 TagDecl::completeDefinition();
1574}
1575
1576//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001577// RecordDecl Implementation
1578//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001579
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001580RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001581 IdentifierInfo *Id, RecordDecl *PrevDecl,
1582 SourceLocation TKL)
1583 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001584 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001585 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001586 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001587 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001588}
1589
1590RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001591 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001592 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001593
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001594 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001595 C.getTypeDeclType(R, PrevDecl);
1596 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001597}
1598
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001599RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001600}
1601
1602void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001603 TagDecl::Destroy(C);
1604}
1605
Douglas Gregordfcad112009-03-25 15:59:44 +00001606bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001607 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001608 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1609}
1610
Douglas Gregor91f84212008-12-11 16:49:14 +00001611/// completeDefinition - Notes that the definition of this type is now
1612/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001613void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001614 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001615 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001616}
Steve Naroffcc321422007-03-26 23:09:51 +00001617
Steve Naroff415d3d52008-10-08 17:01:13 +00001618//===----------------------------------------------------------------------===//
1619// BlockDecl Implementation
1620//===----------------------------------------------------------------------===//
1621
1622BlockDecl::~BlockDecl() {
1623}
1624
1625void BlockDecl::Destroy(ASTContext& C) {
1626 if (Body)
1627 Body->Destroy(C);
1628
1629 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1630 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001631
1632 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001633 Decl::Destroy(C);
1634}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001635
Douglas Gregord5058122010-02-11 01:19:42 +00001636void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001637 unsigned NParms) {
1638 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001639
Steve Naroffc4b30e52009-03-13 16:56:44 +00001640 // Zero params -> null pointer.
1641 if (NParms) {
1642 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001643 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001644 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1645 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1646 }
1647}
1648
1649unsigned BlockDecl::getNumParams() const {
1650 return NumParams;
1651}
Sebastian Redl833ef452010-01-26 22:01:41 +00001652
1653
1654//===----------------------------------------------------------------------===//
1655// Other Decl Allocation/Deallocation Method Implementations
1656//===----------------------------------------------------------------------===//
1657
1658TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1659 return new (C) TranslationUnitDecl(C);
1660}
1661
1662NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1663 SourceLocation L, IdentifierInfo *Id) {
1664 return new (C) NamespaceDecl(DC, L, Id);
1665}
1666
1667void NamespaceDecl::Destroy(ASTContext& C) {
1668 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1669 // together. They are all top-level Decls.
1670
1671 this->~NamespaceDecl();
John McCall3e11ebe2010-03-15 10:12:16 +00001672 Decl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001673}
1674
1675
1676ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1677 SourceLocation L, IdentifierInfo *Id, QualType T) {
1678 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1679}
1680
1681FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1682 SourceLocation L,
1683 DeclarationName N, QualType T,
1684 TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001685 StorageClass S, StorageClass SCAsWritten,
1686 bool isInline, bool hasWrittenPrototype) {
1687 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1688 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001689 New->HasWrittenPrototype = hasWrittenPrototype;
1690 return New;
1691}
1692
1693BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1694 return new (C) BlockDecl(DC, L);
1695}
1696
1697EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1698 SourceLocation L,
1699 IdentifierInfo *Id, QualType T,
1700 Expr *E, const llvm::APSInt &V) {
1701 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1702}
1703
1704void EnumConstantDecl::Destroy(ASTContext& C) {
1705 if (Init) Init->Destroy(C);
John McCall3e11ebe2010-03-15 10:12:16 +00001706 ValueDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001707}
1708
1709TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1710 SourceLocation L, IdentifierInfo *Id,
1711 TypeSourceInfo *TInfo) {
1712 return new (C) TypedefDecl(DC, L, Id, TInfo);
1713}
1714
1715// Anchor TypedefDecl's vtable here.
1716TypedefDecl::~TypedefDecl() {}
1717
1718FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1719 SourceLocation L,
1720 StringLiteral *Str) {
1721 return new (C) FileScopeAsmDecl(DC, L, Str);
1722}