blob: b11f61757dbd4beca5b93525bb7f79000fe6bf1e [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:
300 case Decl::ObjCContainer:
301 case Decl::ObjCForwardProtocol:
302 case Decl::ObjCImplementation:
303 case Decl::ObjCInterface:
304 case Decl::ObjCIvar:
305 case Decl::ObjCMethod:
306 case Decl::ObjCProperty:
307 case Decl::ObjCPropertyImpl:
308 case Decl::ObjCProtocol:
309 return ExternalLinkage;
310 }
311
Douglas Gregorf73b2822009-11-25 22:24:25 +0000312 // Handle linkage for namespace-scope names.
313 if (getDeclContext()->getLookupContext()->isFileContext())
314 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
315 return L;
316
317 // C++ [basic.link]p5:
318 // In addition, a member function, static data member, a named
319 // class or enumeration of class scope, or an unnamed class or
320 // enumeration defined in a class-scope typedef declaration such
321 // that the class or enumeration has the typedef name for linkage
322 // purposes (7.1.3), has external linkage if the name of the class
323 // has external linkage.
324 if (getDeclContext()->isRecord() &&
325 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
326 (isa<TagDecl>(this) &&
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000327 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
328 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
329 if (isExternalLinkage(L))
330 return L;
331 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000332
333 // C++ [basic.link]p6:
334 // The name of a function declared in block scope and the name of
335 // an object declared by a block scope extern declaration have
336 // linkage. If there is a visible declaration of an entity with
337 // linkage having the same name and type, ignoring entities
338 // declared outside the innermost enclosing namespace scope, the
339 // block scope declaration declares that same entity and receives
340 // the linkage of the previous declaration. If there is more than
341 // one such matching entity, the program is ill-formed. Otherwise,
342 // if no matching entity is found, the block scope entity receives
343 // external linkage.
344 if (getLexicalDeclContext()->isFunctionOrMethod()) {
345 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
346 if (Function->getPreviousDeclaration())
347 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
348 return L;
349
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000350 if (Function->isInAnonymousNamespace())
351 return UniqueExternalLinkage;
352
Douglas Gregorf73b2822009-11-25 22:24:25 +0000353 return ExternalLinkage;
354 }
355
356 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
357 if (Var->getStorageClass() == VarDecl::Extern ||
358 Var->getStorageClass() == VarDecl::PrivateExtern) {
359 if (Var->getPreviousDeclaration())
360 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
361 return L;
362
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000363 if (Var->isInAnonymousNamespace())
364 return UniqueExternalLinkage;
365
Douglas Gregorf73b2822009-11-25 22:24:25 +0000366 return ExternalLinkage;
367 }
368 }
369
370 // C++ [basic.link]p6:
371 // Names not covered by these rules have no linkage.
372 return NoLinkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000373 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000374
Douglas Gregor2ada0482009-02-04 17:27:36 +0000375std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000376 return getQualifiedNameAsString(getASTContext().getLangOptions());
377}
378
379std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000380 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
381 // std::string thrashing.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000382 std::vector<std::string> Names;
383 std::string QualName;
384 const DeclContext *Ctx = getDeclContext();
385
386 if (Ctx->isFunctionOrMethod())
387 return getNameAsString();
388
389 while (Ctx) {
Mike Stump11289f42009-09-09 15:08:12 +0000390 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000391 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
392 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
393 std::string TemplateArgsStr
394 = TemplateSpecializationType::PrintTemplateArgumentList(
395 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000396 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000397 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000398 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig07d211e2009-12-24 23:15:03 +0000399 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
400 if (ND->isAnonymousNamespace())
401 Names.push_back("<anonymous namespace>");
402 else
403 Names.push_back(ND->getNameAsString());
404 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
405 if (!RD->getIdentifier()) {
406 std::string RecordString = "<anonymous ";
407 RecordString += RD->getKindName();
408 RecordString += ">";
409 Names.push_back(RecordString);
410 } else {
411 Names.push_back(RD->getNameAsString());
412 }
Sam Weinigb999f682009-12-28 03:19:38 +0000413 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
414 std::string Proto = FD->getNameAsString();
415
416 const FunctionProtoType *FT = 0;
417 if (FD->hasWrittenPrototype())
418 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
419
420 Proto += "(";
421 if (FT) {
422 llvm::raw_string_ostream POut(Proto);
423 unsigned NumParams = FD->getNumParams();
424 for (unsigned i = 0; i < NumParams; ++i) {
425 if (i)
426 POut << ", ";
427 std::string Param;
428 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
429 POut << Param;
430 }
431
432 if (FT->isVariadic()) {
433 if (NumParams > 0)
434 POut << ", ";
435 POut << "...";
436 }
437 }
438 Proto += ")";
439
440 Names.push_back(Proto);
Douglas Gregor85673582009-05-18 17:01:57 +0000441 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000442 Names.push_back(ND->getNameAsString());
443 else
444 break;
445
446 Ctx = Ctx->getParent();
447 }
448
449 std::vector<std::string>::reverse_iterator
450 I = Names.rbegin(),
451 End = Names.rend();
452
453 for (; I!=End; ++I)
454 QualName += *I + "::";
455
John McCalla2a3f7d2010-03-16 21:48:18 +0000456 if (getDeclName())
457 QualName += getNameAsString();
458 else
459 QualName += "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000460
461 return QualName;
462}
463
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000464bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000465 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
466
Douglas Gregor889ceb72009-02-03 19:21:40 +0000467 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
468 // We want to keep it, unless it nominates same namespace.
469 if (getKind() == Decl::UsingDirective) {
470 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
471 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000474 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
475 // For function declarations, we keep track of redeclarations.
476 return FD->getPreviousDeclaration() == OldD;
477
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000478 // For function templates, the underlying function declarations are linked.
479 if (const FunctionTemplateDecl *FunctionTemplate
480 = dyn_cast<FunctionTemplateDecl>(this))
481 if (const FunctionTemplateDecl *OldFunctionTemplate
482 = dyn_cast<FunctionTemplateDecl>(OldD))
483 return FunctionTemplate->getTemplatedDecl()
484 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000485
Steve Naroffc4173fa2009-02-22 19:35:57 +0000486 // For method declarations, we keep track of redeclarations.
487 if (isa<ObjCMethodDecl>(this))
488 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000489
John McCall9f3059a2009-10-09 21:13:30 +0000490 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
491 return true;
492
John McCall3f746822009-11-17 05:59:44 +0000493 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
494 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
495 cast<UsingShadowDecl>(OldD)->getTargetDecl();
496
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000497 // For non-function declarations, if the declarations are of the
498 // same kind then this must be a redeclaration, or semantic analysis
499 // would not have given us the new declaration.
500 return this->getKind() == OldD->getKind();
501}
502
Douglas Gregoreddf4332009-02-24 20:03:32 +0000503bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000504 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000505}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000506
Anders Carlsson6915bf62009-06-26 06:29:23 +0000507NamedDecl *NamedDecl::getUnderlyingDecl() {
508 NamedDecl *ND = this;
509 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000510 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000511 ND = UD->getTargetDecl();
512 else if (ObjCCompatibleAliasDecl *AD
513 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
514 return AD->getClassInterface();
515 else
516 return ND;
517 }
518}
519
John McCalla8ae2222010-04-06 21:38:20 +0000520bool NamedDecl::isCXXInstanceMember() const {
521 assert(isCXXClassMember() &&
522 "checking whether non-member is instance member");
523
524 const NamedDecl *D = this;
525 if (isa<UsingShadowDecl>(D))
526 D = cast<UsingShadowDecl>(D)->getTargetDecl();
527
528 if (isa<FieldDecl>(D))
529 return true;
530 if (isa<CXXMethodDecl>(D))
531 return cast<CXXMethodDecl>(D)->isInstance();
532 if (isa<FunctionTemplateDecl>(D))
533 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
534 ->getTemplatedDecl())->isInstance();
535 return false;
536}
537
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000538//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000539// DeclaratorDecl Implementation
540//===----------------------------------------------------------------------===//
541
John McCall3e11ebe2010-03-15 10:12:16 +0000542DeclaratorDecl::~DeclaratorDecl() {}
543void DeclaratorDecl::Destroy(ASTContext &C) {
544 if (hasExtInfo())
545 C.Deallocate(getExtInfo());
546 ValueDecl::Destroy(C);
547}
548
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000549SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000550 if (DeclInfo) {
John McCall3e11ebe2010-03-15 10:12:16 +0000551 TypeLoc TL = getTypeSourceInfo()->getTypeLoc();
John McCall17001972009-10-18 01:05:36 +0000552 while (true) {
553 TypeLoc NextTL = TL.getNextTypeLoc();
554 if (!NextTL)
555 return TL.getSourceRange().getBegin();
556 TL = NextTL;
557 }
558 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000559 return SourceLocation();
560}
561
John McCall3e11ebe2010-03-15 10:12:16 +0000562void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
563 SourceRange QualifierRange) {
564 if (Qualifier) {
565 // Make sure the extended decl info is allocated.
566 if (!hasExtInfo()) {
567 // Save (non-extended) type source info pointer.
568 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
569 // Allocate external info struct.
570 DeclInfo = new (getASTContext()) ExtInfo;
571 // Restore savedTInfo into (extended) decl info.
572 getExtInfo()->TInfo = savedTInfo;
573 }
574 // Set qualifier info.
575 getExtInfo()->NNS = Qualifier;
576 getExtInfo()->NNSRange = QualifierRange;
577 }
578 else {
579 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
580 assert(QualifierRange.isInvalid());
581 if (hasExtInfo()) {
582 // Save type source info pointer.
583 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
584 // Deallocate the extended decl info.
585 getASTContext().Deallocate(getExtInfo());
586 // Restore savedTInfo into (non-extended) decl info.
587 DeclInfo = savedTInfo;
588 }
589 }
590}
591
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000592//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000593// VarDecl Implementation
594//===----------------------------------------------------------------------===//
595
Sebastian Redl833ef452010-01-26 22:01:41 +0000596const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
597 switch (SC) {
598 case VarDecl::None: break;
599 case VarDecl::Auto: return "auto"; break;
600 case VarDecl::Extern: return "extern"; break;
601 case VarDecl::PrivateExtern: return "__private_extern__"; break;
602 case VarDecl::Register: return "register"; break;
603 case VarDecl::Static: return "static"; break;
604 }
605
606 assert(0 && "Invalid storage class");
607 return 0;
608}
609
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000610VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000611 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000612 StorageClass S, StorageClass SCAsWritten) {
613 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000614}
615
616void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000617 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000618 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000619 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000620 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
621 Eval->~EvaluatedStmt();
622 C.Deallocate(Eval);
623 }
624 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000625 this->~VarDecl();
John McCall3e11ebe2010-03-15 10:12:16 +0000626 DeclaratorDecl::Destroy(C);
Nuno Lopes394ec982008-12-17 23:39:55 +0000627}
628
629VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000630}
631
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000632SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000633 SourceLocation Start = getTypeSpecStartLoc();
634 if (Start.isInvalid())
635 Start = getLocation();
636
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000637 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000638 return SourceRange(Start, getInit()->getLocEnd());
639 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000640}
641
Sebastian Redl833ef452010-01-26 22:01:41 +0000642bool VarDecl::isExternC() const {
643 ASTContext &Context = getASTContext();
644 if (!Context.getLangOptions().CPlusPlus)
645 return (getDeclContext()->isTranslationUnit() &&
646 getStorageClass() != Static) ||
647 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
648
649 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
650 DC = DC->getParent()) {
651 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
652 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
653 return getStorageClass() != Static;
654
655 break;
656 }
657
658 if (DC->isFunctionOrMethod())
659 return false;
660 }
661
662 return false;
663}
664
665VarDecl *VarDecl::getCanonicalDecl() {
666 return getFirstDeclaration();
667}
668
Sebastian Redl35351a92010-01-31 22:27:38 +0000669VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
670 // C++ [basic.def]p2:
671 // A declaration is a definition unless [...] it contains the 'extern'
672 // specifier or a linkage-specification and neither an initializer [...],
673 // it declares a static data member in a class declaration [...].
674 // C++ [temp.expl.spec]p15:
675 // An explicit specialization of a static data member of a template is a
676 // definition if the declaration includes an initializer; otherwise, it is
677 // a declaration.
678 if (isStaticDataMember()) {
679 if (isOutOfLine() && (hasInit() ||
680 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
681 return Definition;
682 else
683 return DeclarationOnly;
684 }
685 // C99 6.7p5:
686 // A definition of an identifier is a declaration for that identifier that
687 // [...] causes storage to be reserved for that object.
688 // Note: that applies for all non-file-scope objects.
689 // C99 6.9.2p1:
690 // If the declaration of an identifier for an object has file scope and an
691 // initializer, the declaration is an external definition for the identifier
692 if (hasInit())
693 return Definition;
694 // AST for 'extern "C" int foo;' is annotated with 'extern'.
695 if (hasExternalStorage())
696 return DeclarationOnly;
697
698 // C99 6.9.2p2:
699 // A declaration of an object that has file scope without an initializer,
700 // and without a storage class specifier or the scs 'static', constitutes
701 // a tentative definition.
702 // No such thing in C++.
703 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
704 return TentativeDefinition;
705
706 // What's left is (in C, block-scope) declarations without initializers or
707 // external storage. These are definitions.
708 return Definition;
709}
710
Sebastian Redl35351a92010-01-31 22:27:38 +0000711VarDecl *VarDecl::getActingDefinition() {
712 DefinitionKind Kind = isThisDeclarationADefinition();
713 if (Kind != TentativeDefinition)
714 return 0;
715
716 VarDecl *LastTentative = false;
717 VarDecl *First = getFirstDeclaration();
718 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
719 I != E; ++I) {
720 Kind = (*I)->isThisDeclarationADefinition();
721 if (Kind == Definition)
722 return 0;
723 else if (Kind == TentativeDefinition)
724 LastTentative = *I;
725 }
726 return LastTentative;
727}
728
729bool VarDecl::isTentativeDefinitionNow() const {
730 DefinitionKind Kind = isThisDeclarationADefinition();
731 if (Kind != TentativeDefinition)
732 return false;
733
734 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
735 if ((*I)->isThisDeclarationADefinition() == Definition)
736 return false;
737 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000738 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000739}
740
Sebastian Redl5ca79842010-02-01 20:16:42 +0000741VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000742 VarDecl *First = getFirstDeclaration();
743 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
744 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000745 if ((*I)->isThisDeclarationADefinition() == Definition)
746 return *I;
747 }
748 return 0;
749}
750
751const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000752 redecl_iterator I = redecls_begin(), E = redecls_end();
753 while (I != E && !I->getInit())
754 ++I;
755
756 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000757 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000758 return I->getInit();
759 }
760 return 0;
761}
762
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000763bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000764 if (Decl::isOutOfLine())
765 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000766
767 if (!isStaticDataMember())
768 return false;
769
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000770 // If this static data member was instantiated from a static data member of
771 // a class template, check whether that static data member was defined
772 // out-of-line.
773 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
774 return VD->isOutOfLine();
775
776 return false;
777}
778
Douglas Gregor1d957a32009-10-27 18:42:08 +0000779VarDecl *VarDecl::getOutOfLineDefinition() {
780 if (!isStaticDataMember())
781 return 0;
782
783 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
784 RD != RDEnd; ++RD) {
785 if (RD->getLexicalDeclContext()->isFileContext())
786 return *RD;
787 }
788
789 return 0;
790}
791
Douglas Gregord5058122010-02-11 01:19:42 +0000792void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000793 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
794 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000795 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000796 }
797
798 Init = I;
799}
800
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000801VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000802 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000803 return cast<VarDecl>(MSI->getInstantiatedFrom());
804
805 return 0;
806}
807
Douglas Gregor3c74d412009-10-14 20:14:33 +0000808TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000809 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000810 return MSI->getTemplateSpecializationKind();
811
812 return TSK_Undeclared;
813}
814
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000815MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000816 return getASTContext().getInstantiatedFromStaticDataMember(this);
817}
818
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000819void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
820 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000821 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000822 assert(MSI && "Not an instantiated static data member?");
823 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000824 if (TSK != TSK_ExplicitSpecialization &&
825 PointOfInstantiation.isValid() &&
826 MSI->getPointOfInstantiation().isInvalid())
827 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000828}
829
Sebastian Redl833ef452010-01-26 22:01:41 +0000830//===----------------------------------------------------------------------===//
831// ParmVarDecl Implementation
832//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000833
Sebastian Redl833ef452010-01-26 22:01:41 +0000834ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
835 SourceLocation L, IdentifierInfo *Id,
836 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000837 StorageClass S, StorageClass SCAsWritten,
838 Expr *DefArg) {
839 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
840 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000841}
842
Sebastian Redl833ef452010-01-26 22:01:41 +0000843Expr *ParmVarDecl::getDefaultArg() {
844 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
845 assert(!hasUninstantiatedDefaultArg() &&
846 "Default argument is not yet instantiated!");
847
848 Expr *Arg = getInit();
849 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
850 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000851
Sebastian Redl833ef452010-01-26 22:01:41 +0000852 return Arg;
853}
854
855unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
856 if (const CXXExprWithTemporaries *E =
857 dyn_cast<CXXExprWithTemporaries>(getInit()))
858 return E->getNumTemporaries();
859
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000860 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000861}
862
Sebastian Redl833ef452010-01-26 22:01:41 +0000863CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
864 assert(getNumDefaultArgTemporaries() &&
865 "Default arguments does not have any temporaries!");
866
867 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
868 return E->getTemporary(i);
869}
870
871SourceRange ParmVarDecl::getDefaultArgRange() const {
872 if (const Expr *E = getInit())
873 return E->getSourceRange();
874
875 if (hasUninstantiatedDefaultArg())
876 return getUninstantiatedDefaultArg()->getSourceRange();
877
878 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000879}
880
Nuno Lopes394ec982008-12-17 23:39:55 +0000881//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000882// FunctionDecl Implementation
883//===----------------------------------------------------------------------===//
884
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000885void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000886 if (Body && Body.isOffset())
887 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000888
889 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
890 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000891
Douglas Gregord801b062009-10-07 23:56:10 +0000892 FunctionTemplateSpecializationInfo *FTSInfo
893 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
894 if (FTSInfo)
895 C.Deallocate(FTSInfo);
896
897 MemberSpecializationInfo *MSInfo
898 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
899 if (MSInfo)
900 C.Deallocate(MSInfo);
901
Steve Naroff13ae6f42009-01-27 21:25:57 +0000902 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000903
John McCall3e11ebe2010-03-15 10:12:16 +0000904 DeclaratorDecl::Destroy(C);
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000905}
906
John McCalle1f2ec22009-09-11 06:45:03 +0000907void FunctionDecl::getNameForDiagnostic(std::string &S,
908 const PrintingPolicy &Policy,
909 bool Qualified) const {
910 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
911 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
912 if (TemplateArgs)
913 S += TemplateSpecializationType::PrintTemplateArgumentList(
914 TemplateArgs->getFlatArgumentList(),
915 TemplateArgs->flat_size(),
916 Policy);
917
918}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000919
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000920Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000921 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
922 if (I->Body) {
923 Definition = *I;
924 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000925 }
926 }
927
928 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000929}
930
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000931void FunctionDecl::setBody(Stmt *B) {
932 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000933 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000934 EndRangeLoc = B->getLocEnd();
935}
936
Douglas Gregor16618f22009-09-12 00:17:51 +0000937bool FunctionDecl::isMain() const {
938 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000939 return !Context.getLangOptions().Freestanding &&
940 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000941 getIdentifier() && getIdentifier()->isStr("main");
942}
943
Douglas Gregor16618f22009-09-12 00:17:51 +0000944bool FunctionDecl::isExternC() const {
945 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000946 // In C, any non-static, non-overloadable function has external
947 // linkage.
948 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000949 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000950
Mike Stump11289f42009-09-09 15:08:12 +0000951 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000952 DC = DC->getParent()) {
953 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
954 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000955 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000956 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000957
958 break;
959 }
960 }
961
962 return false;
963}
964
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000965bool FunctionDecl::isGlobal() const {
966 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
967 return Method->isStatic();
968
969 if (getStorageClass() == Static)
970 return false;
971
Mike Stump11289f42009-09-09 15:08:12 +0000972 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000973 DC->isNamespace();
974 DC = DC->getParent()) {
975 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
976 if (!Namespace->getDeclName())
977 return false;
978 break;
979 }
980 }
981
982 return true;
983}
984
Sebastian Redl833ef452010-01-26 22:01:41 +0000985void
986FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
987 redeclarable_base::setPreviousDeclaration(PrevDecl);
988
989 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
990 FunctionTemplateDecl *PrevFunTmpl
991 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
992 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
993 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
994 }
995}
996
997const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
998 return getFirstDeclaration();
999}
1000
1001FunctionDecl *FunctionDecl::getCanonicalDecl() {
1002 return getFirstDeclaration();
1003}
1004
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001005/// \brief Returns a value indicating whether this function
1006/// corresponds to a builtin function.
1007///
1008/// The function corresponds to a built-in function if it is
1009/// declared at translation scope or within an extern "C" block and
1010/// its name matches with the name of a builtin. The returned value
1011/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001012/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001013/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001014unsigned FunctionDecl::getBuiltinID() const {
1015 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001016 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1017 return 0;
1018
1019 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1020 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1021 return BuiltinID;
1022
1023 // This function has the name of a known C library
1024 // function. Determine whether it actually refers to the C library
1025 // function or whether it just has the same name.
1026
Douglas Gregora908e7f2009-02-17 03:23:10 +00001027 // If this is a static function, it's not a builtin.
1028 if (getStorageClass() == Static)
1029 return 0;
1030
Douglas Gregore711f702009-02-14 18:57:46 +00001031 // If this function is at translation-unit scope and we're not in
1032 // C++, it refers to the C library function.
1033 if (!Context.getLangOptions().CPlusPlus &&
1034 getDeclContext()->isTranslationUnit())
1035 return BuiltinID;
1036
1037 // If the function is in an extern "C" linkage specification and is
1038 // not marked "overloadable", it's the real function.
1039 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001040 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001041 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001042 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001043 return BuiltinID;
1044
1045 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001046 return 0;
1047}
1048
1049
Chris Lattner47c0d002009-04-25 06:03:53 +00001050/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001051/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001052/// after it has been created.
1053unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001054 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001055 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001056 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001057 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001058
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001059}
1060
Douglas Gregord5058122010-02-11 01:19:42 +00001061void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001062 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001063 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001065 // Zero params -> null pointer.
1066 if (NumParams) {
Douglas Gregord5058122010-02-11 01:19:42 +00001067 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001068 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001069 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001070
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001071 // Update source range. The check below allows us to set EndRangeLoc before
1072 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001073 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001074 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001075 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001076}
Chris Lattner41943152007-01-25 04:52:46 +00001077
Chris Lattner58258242008-04-10 02:22:51 +00001078/// getMinRequiredArguments - Returns the minimum number of arguments
1079/// needed to call this function. This may be fewer than the number of
1080/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001081/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001082unsigned FunctionDecl::getMinRequiredArguments() const {
1083 unsigned NumRequiredArgs = getNumParams();
1084 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001085 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001086 --NumRequiredArgs;
1087
1088 return NumRequiredArgs;
1089}
1090
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001091bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001092 // FIXME: This is not enough. Consider:
1093 //
1094 // inline void f();
1095 // void f() { }
1096 //
1097 // f is inlined, but does not have inline specified.
1098 // To fix this we should add an 'inline' flag to FunctionDecl.
1099 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001100 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001101
1102 if (isa<CXXMethodDecl>(this)) {
1103 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1104 return true;
1105 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001106
1107 switch (getTemplateSpecializationKind()) {
1108 case TSK_Undeclared:
1109 case TSK_ExplicitSpecialization:
1110 return false;
1111
1112 case TSK_ImplicitInstantiation:
1113 case TSK_ExplicitInstantiationDeclaration:
1114 case TSK_ExplicitInstantiationDefinition:
1115 // Handle below.
1116 break;
1117 }
1118
1119 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1120 Stmt *Pattern = 0;
1121 if (PatternDecl)
1122 Pattern = PatternDecl->getBody(PatternDecl);
1123
1124 if (Pattern && PatternDecl)
1125 return PatternDecl->isInlined();
1126
1127 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001128}
1129
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001130/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001131/// definition will be externally visible.
1132///
1133/// Inline function definitions are always available for inlining optimizations.
1134/// However, depending on the language dialect, declaration specifiers, and
1135/// attributes, the definition of an inline function may or may not be
1136/// "externally" visible to other translation units in the program.
1137///
1138/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001139/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001140/// inline definition becomes externally visible (C99 6.7.4p6).
1141///
1142/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1143/// definition, we use the GNU semantics for inline, which are nearly the
1144/// opposite of C99 semantics. In particular, "inline" by itself will create
1145/// an externally visible symbol, but "extern inline" will not create an
1146/// externally visible symbol.
1147bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1148 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001149 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001150 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001151
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001152 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001153 // GNU inline semantics. Based on a number of examples, we came up with the
1154 // following heuristic: if the "inline" keyword is present on a
1155 // declaration of the function but "extern" is not present on that
1156 // declaration, then the symbol is externally visible. Otherwise, the GNU
1157 // "extern inline" semantics applies and the symbol is not externally
1158 // visible.
1159 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1160 Redecl != RedeclEnd;
1161 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001162 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001163 return true;
1164 }
1165
1166 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001167 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001168 }
1169
1170 // C99 6.7.4p6:
1171 // [...] If all of the file scope declarations for a function in a
1172 // translation unit include the inline function specifier without extern,
1173 // then the definition in that translation unit is an inline definition.
1174 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1175 Redecl != RedeclEnd;
1176 ++Redecl) {
1177 // Only consider file-scope declarations in this test.
1178 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1179 continue;
1180
Douglas Gregor35b57532009-10-27 21:01:01 +00001181 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001182 return true; // Not an inline definition
1183 }
1184
1185 // C99 6.7.4p6:
1186 // An inline definition does not provide an external definition for the
1187 // function, and does not forbid an external definition in another
1188 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001189 return false;
1190}
1191
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001192/// getOverloadedOperator - Which C++ overloaded operator this
1193/// function represents, if any.
1194OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001195 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1196 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001197 else
1198 return OO_None;
1199}
1200
Alexis Huntc88db062010-01-13 09:01:02 +00001201/// getLiteralIdentifier - The literal suffix identifier this function
1202/// represents, if any.
1203const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1204 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1205 return getDeclName().getCXXLiteralIdentifier();
1206 else
1207 return 0;
1208}
1209
Douglas Gregord801b062009-10-07 23:56:10 +00001210FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001211 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001212 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1213
1214 return 0;
1215}
1216
Douglas Gregor06db9f52009-10-12 20:18:28 +00001217MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1218 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1219}
1220
Douglas Gregord801b062009-10-07 23:56:10 +00001221void
1222FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1223 TemplateSpecializationKind TSK) {
1224 assert(TemplateOrSpecialization.isNull() &&
1225 "Member function is already a specialization");
1226 MemberSpecializationInfo *Info
1227 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1228 TemplateOrSpecialization = Info;
1229}
1230
Douglas Gregorafca3b42009-10-27 20:53:28 +00001231bool FunctionDecl::isImplicitlyInstantiable() const {
1232 // If this function already has a definition or is invalid, it can't be
1233 // implicitly instantiated.
1234 if (isInvalidDecl() || getBody())
1235 return false;
1236
1237 switch (getTemplateSpecializationKind()) {
1238 case TSK_Undeclared:
1239 case TSK_ExplicitSpecialization:
1240 case TSK_ExplicitInstantiationDefinition:
1241 return false;
1242
1243 case TSK_ImplicitInstantiation:
1244 return true;
1245
1246 case TSK_ExplicitInstantiationDeclaration:
1247 // Handled below.
1248 break;
1249 }
1250
1251 // Find the actual template from which we will instantiate.
1252 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1253 Stmt *Pattern = 0;
1254 if (PatternDecl)
1255 Pattern = PatternDecl->getBody(PatternDecl);
1256
1257 // C++0x [temp.explicit]p9:
1258 // Except for inline functions, other explicit instantiation declarations
1259 // have the effect of suppressing the implicit instantiation of the entity
1260 // to which they refer.
1261 if (!Pattern || !PatternDecl)
1262 return true;
1263
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001264 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001265}
1266
1267FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1268 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1269 while (Primary->getInstantiatedFromMemberTemplate()) {
1270 // If we have hit a point where the user provided a specialization of
1271 // this template, we're done looking.
1272 if (Primary->isMemberSpecialization())
1273 break;
1274
1275 Primary = Primary->getInstantiatedFromMemberTemplate();
1276 }
1277
1278 return Primary->getTemplatedDecl();
1279 }
1280
1281 return getInstantiatedFromMemberFunction();
1282}
1283
Douglas Gregor70d83e22009-06-29 17:30:29 +00001284FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001285 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001286 = TemplateOrSpecialization
1287 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001288 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001289 }
1290 return 0;
1291}
1292
1293const TemplateArgumentList *
1294FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001295 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001296 = TemplateOrSpecialization
1297 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001298 return Info->TemplateArguments;
1299 }
1300 return 0;
1301}
1302
Mike Stump11289f42009-09-09 15:08:12 +00001303void
Douglas Gregord5058122010-02-11 01:19:42 +00001304FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001305 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001306 void *InsertPos,
1307 TemplateSpecializationKind TSK) {
1308 assert(TSK != TSK_Undeclared &&
1309 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001310 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001311 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001312 if (!Info)
Douglas Gregord5058122010-02-11 01:19:42 +00001313 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001314
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001315 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001316 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001317 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001318 Info->TemplateArguments = TemplateArgs;
1319 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001320
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001321 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001322 // function template specializations.
1323 if (InsertPos)
1324 Template->getSpecializations().InsertNode(Info, InsertPos);
1325 else {
1326 // Try to insert the new node. If there is an existing node, remove it
1327 // first.
1328 FunctionTemplateSpecializationInfo *Existing
1329 = Template->getSpecializations().GetOrInsertNode(Info);
1330 if (Existing) {
1331 Template->getSpecializations().RemoveNode(Existing);
1332 Template->getSpecializations().GetOrInsertNode(Info);
1333 }
1334 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001335}
1336
John McCallb9c78482010-04-08 09:05:18 +00001337void
1338FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1339 const UnresolvedSetImpl &Templates,
1340 const TemplateArgumentListInfo &TemplateArgs) {
1341 assert(TemplateOrSpecialization.isNull());
1342 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1343 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001344 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001345 void *Buffer = Context.Allocate(Size);
1346 DependentFunctionTemplateSpecializationInfo *Info =
1347 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1348 TemplateArgs);
1349 TemplateOrSpecialization = Info;
1350}
1351
1352DependentFunctionTemplateSpecializationInfo::
1353DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1354 const TemplateArgumentListInfo &TArgs)
1355 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1356
1357 d.NumTemplates = Ts.size();
1358 d.NumArgs = TArgs.size();
1359
1360 FunctionTemplateDecl **TsArray =
1361 const_cast<FunctionTemplateDecl**>(getTemplates());
1362 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1363 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1364
1365 TemplateArgumentLoc *ArgsArray =
1366 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1367 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1368 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1369}
1370
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001371TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001372 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001373 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001374 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001375 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001376 if (FTSInfo)
1377 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregord801b062009-10-07 23:56:10 +00001379 MemberSpecializationInfo *MSInfo
1380 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1381 if (MSInfo)
1382 return MSInfo->getTemplateSpecializationKind();
1383
1384 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001385}
1386
Mike Stump11289f42009-09-09 15:08:12 +00001387void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001388FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1389 SourceLocation PointOfInstantiation) {
1390 if (FunctionTemplateSpecializationInfo *FTSInfo
1391 = TemplateOrSpecialization.dyn_cast<
1392 FunctionTemplateSpecializationInfo*>()) {
1393 FTSInfo->setTemplateSpecializationKind(TSK);
1394 if (TSK != TSK_ExplicitSpecialization &&
1395 PointOfInstantiation.isValid() &&
1396 FTSInfo->getPointOfInstantiation().isInvalid())
1397 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1398 } else if (MemberSpecializationInfo *MSInfo
1399 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1400 MSInfo->setTemplateSpecializationKind(TSK);
1401 if (TSK != TSK_ExplicitSpecialization &&
1402 PointOfInstantiation.isValid() &&
1403 MSInfo->getPointOfInstantiation().isInvalid())
1404 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1405 } else
1406 assert(false && "Function cannot have a template specialization kind");
1407}
1408
1409SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001410 if (FunctionTemplateSpecializationInfo *FTSInfo
1411 = TemplateOrSpecialization.dyn_cast<
1412 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001413 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001414 else if (MemberSpecializationInfo *MSInfo
1415 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001416 return MSInfo->getPointOfInstantiation();
1417
1418 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001419}
1420
Douglas Gregor6411b922009-09-11 20:15:17 +00001421bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001422 if (Decl::isOutOfLine())
1423 return true;
1424
1425 // If this function was instantiated from a member function of a
1426 // class template, check whether that member function was defined out-of-line.
1427 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1428 const FunctionDecl *Definition;
1429 if (FD->getBody(Definition))
1430 return Definition->isOutOfLine();
1431 }
1432
1433 // If this function was instantiated from a function template,
1434 // check whether that function template was defined out-of-line.
1435 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1436 const FunctionDecl *Definition;
1437 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1438 return Definition->isOutOfLine();
1439 }
1440
1441 return false;
1442}
1443
Chris Lattner59a25942008-03-31 00:36:02 +00001444//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001445// FieldDecl Implementation
1446//===----------------------------------------------------------------------===//
1447
1448FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1449 IdentifierInfo *Id, QualType T,
1450 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1451 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1452}
1453
1454bool FieldDecl::isAnonymousStructOrUnion() const {
1455 if (!isImplicit() || getDeclName())
1456 return false;
1457
1458 if (const RecordType *Record = getType()->getAs<RecordType>())
1459 return Record->getDecl()->isAnonymousStructOrUnion();
1460
1461 return false;
1462}
1463
1464//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001465// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001466//===----------------------------------------------------------------------===//
1467
John McCall3e11ebe2010-03-15 10:12:16 +00001468void TagDecl::Destroy(ASTContext &C) {
1469 if (hasExtInfo())
1470 C.Deallocate(getExtInfo());
1471 TypeDecl::Destroy(C);
1472}
1473
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001474SourceRange TagDecl::getSourceRange() const {
1475 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001476 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001477}
1478
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001479TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001480 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001481}
1482
Douglas Gregordee1be82009-01-17 00:42:38 +00001483void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001484 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1485 TagT->decl.setPointer(this);
1486 TagT->decl.setInt(1);
1487 }
John McCall67da35c2010-02-04 22:26:26 +00001488
1489 if (isa<CXXRecordDecl>(this)) {
1490 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1491 struct CXXRecordDecl::DefinitionData *Data =
1492 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001493 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1494 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001495 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001496}
1497
1498void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001499 assert((!isa<CXXRecordDecl>(this) ||
1500 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1501 "definition completed but not started");
1502
Douglas Gregordee1be82009-01-17 00:42:38 +00001503 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001504 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1505 assert(TagT->decl.getPointer() == this &&
1506 "Attempt to redefine a tag definition?");
1507 TagT->decl.setInt(0);
1508 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001509}
1510
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001511TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001512 if (isDefinition())
1513 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001514
1515 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001516 R != REnd; ++R)
1517 if (R->isDefinition())
1518 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001519
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001520 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001521}
1522
John McCall06f6fe8d2009-09-04 01:14:41 +00001523TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1524 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001525 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001526 case DeclSpec::TST_struct: return TK_struct;
1527 case DeclSpec::TST_class: return TK_class;
1528 case DeclSpec::TST_union: return TK_union;
1529 case DeclSpec::TST_enum: return TK_enum;
1530 }
1531}
1532
John McCall3e11ebe2010-03-15 10:12:16 +00001533void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1534 SourceRange QualifierRange) {
1535 if (Qualifier) {
1536 // Make sure the extended qualifier info is allocated.
1537 if (!hasExtInfo())
1538 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1539 // Set qualifier info.
1540 getExtInfo()->NNS = Qualifier;
1541 getExtInfo()->NNSRange = QualifierRange;
1542 }
1543 else {
1544 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1545 assert(QualifierRange.isInvalid());
1546 if (hasExtInfo()) {
1547 getASTContext().Deallocate(getExtInfo());
1548 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1549 }
1550 }
1551}
1552
Ted Kremenek21475702008-09-05 17:16:31 +00001553//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001554// EnumDecl Implementation
1555//===----------------------------------------------------------------------===//
1556
1557EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1558 IdentifierInfo *Id, SourceLocation TKL,
1559 EnumDecl *PrevDecl) {
1560 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1561 C.getTypeDeclType(Enum, PrevDecl);
1562 return Enum;
1563}
1564
1565void EnumDecl::Destroy(ASTContext& C) {
John McCall3e11ebe2010-03-15 10:12:16 +00001566 TagDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001567}
1568
Douglas Gregord5058122010-02-11 01:19:42 +00001569void EnumDecl::completeDefinition(QualType NewType,
Sebastian Redl833ef452010-01-26 22:01:41 +00001570 QualType NewPromotionType) {
1571 assert(!isDefinition() && "Cannot redefine enums!");
1572 IntegerType = NewType;
1573 PromotionType = NewPromotionType;
1574 TagDecl::completeDefinition();
1575}
1576
1577//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001578// RecordDecl Implementation
1579//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001580
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001581RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001582 IdentifierInfo *Id, RecordDecl *PrevDecl,
1583 SourceLocation TKL)
1584 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001585 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001586 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001587 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001588 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001589}
1590
1591RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001592 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001593 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001594
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001595 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001596 C.getTypeDeclType(R, PrevDecl);
1597 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001598}
1599
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001600RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001601}
1602
1603void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001604 TagDecl::Destroy(C);
1605}
1606
Douglas Gregordfcad112009-03-25 15:59:44 +00001607bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001608 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001609 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1610}
1611
Douglas Gregor91f84212008-12-11 16:49:14 +00001612/// completeDefinition - Notes that the definition of this type is now
1613/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001614void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001615 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001616 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001617}
Steve Naroffcc321422007-03-26 23:09:51 +00001618
Steve Naroff415d3d52008-10-08 17:01:13 +00001619//===----------------------------------------------------------------------===//
1620// BlockDecl Implementation
1621//===----------------------------------------------------------------------===//
1622
1623BlockDecl::~BlockDecl() {
1624}
1625
1626void BlockDecl::Destroy(ASTContext& C) {
1627 if (Body)
1628 Body->Destroy(C);
1629
1630 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1631 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001632
1633 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001634 Decl::Destroy(C);
1635}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001636
Douglas Gregord5058122010-02-11 01:19:42 +00001637void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001638 unsigned NParms) {
1639 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001640
Steve Naroffc4b30e52009-03-13 16:56:44 +00001641 // Zero params -> null pointer.
1642 if (NParms) {
1643 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001644 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001645 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1646 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1647 }
1648}
1649
1650unsigned BlockDecl::getNumParams() const {
1651 return NumParams;
1652}
Sebastian Redl833ef452010-01-26 22:01:41 +00001653
1654
1655//===----------------------------------------------------------------------===//
1656// Other Decl Allocation/Deallocation Method Implementations
1657//===----------------------------------------------------------------------===//
1658
1659TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1660 return new (C) TranslationUnitDecl(C);
1661}
1662
1663NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1664 SourceLocation L, IdentifierInfo *Id) {
1665 return new (C) NamespaceDecl(DC, L, Id);
1666}
1667
1668void NamespaceDecl::Destroy(ASTContext& C) {
1669 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1670 // together. They are all top-level Decls.
1671
1672 this->~NamespaceDecl();
John McCall3e11ebe2010-03-15 10:12:16 +00001673 Decl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001674}
1675
1676
1677ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1678 SourceLocation L, IdentifierInfo *Id, QualType T) {
1679 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1680}
1681
1682FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1683 SourceLocation L,
1684 DeclarationName N, QualType T,
1685 TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001686 StorageClass S, StorageClass SCAsWritten,
1687 bool isInline, bool hasWrittenPrototype) {
1688 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1689 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001690 New->HasWrittenPrototype = hasWrittenPrototype;
1691 return New;
1692}
1693
1694BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1695 return new (C) BlockDecl(DC, L);
1696}
1697
1698EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1699 SourceLocation L,
1700 IdentifierInfo *Id, QualType T,
1701 Expr *E, const llvm::APSInt &V) {
1702 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1703}
1704
1705void EnumConstantDecl::Destroy(ASTContext& C) {
1706 if (Init) Init->Destroy(C);
John McCall3e11ebe2010-03-15 10:12:16 +00001707 ValueDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001708}
1709
1710TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1711 SourceLocation L, IdentifierInfo *Id,
1712 TypeSourceInfo *TInfo) {
1713 return new (C) TypedefDecl(DC, L, Id, TInfo);
1714}
1715
1716// Anchor TypedefDecl's vtable here.
1717TypedefDecl::~TypedefDecl() {}
1718
1719FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1720 SourceLocation L,
1721 StringLiteral *Str) {
1722 return new (C) FileScopeAsmDecl(DC, L, Str);
1723}