blob: dc9fb59e3090c79064a328577c7cf760939e5d1c [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 {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000289 // Handle linkage for namespace-scope names.
290 if (getDeclContext()->getLookupContext()->isFileContext())
291 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
292 return L;
293
294 // C++ [basic.link]p5:
295 // In addition, a member function, static data member, a named
296 // class or enumeration of class scope, or an unnamed class or
297 // enumeration defined in a class-scope typedef declaration such
298 // that the class or enumeration has the typedef name for linkage
299 // purposes (7.1.3), has external linkage if the name of the class
300 // has external linkage.
301 if (getDeclContext()->isRecord() &&
302 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
303 (isa<TagDecl>(this) &&
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000304 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
305 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
306 if (isExternalLinkage(L))
307 return L;
308 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000309
310 // C++ [basic.link]p6:
311 // The name of a function declared in block scope and the name of
312 // an object declared by a block scope extern declaration have
313 // linkage. If there is a visible declaration of an entity with
314 // linkage having the same name and type, ignoring entities
315 // declared outside the innermost enclosing namespace scope, the
316 // block scope declaration declares that same entity and receives
317 // the linkage of the previous declaration. If there is more than
318 // one such matching entity, the program is ill-formed. Otherwise,
319 // if no matching entity is found, the block scope entity receives
320 // external linkage.
321 if (getLexicalDeclContext()->isFunctionOrMethod()) {
322 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
323 if (Function->getPreviousDeclaration())
324 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
325 return L;
326
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000327 if (Function->isInAnonymousNamespace())
328 return UniqueExternalLinkage;
329
Douglas Gregorf73b2822009-11-25 22:24:25 +0000330 return ExternalLinkage;
331 }
332
333 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
334 if (Var->getStorageClass() == VarDecl::Extern ||
335 Var->getStorageClass() == VarDecl::PrivateExtern) {
336 if (Var->getPreviousDeclaration())
337 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
338 return L;
339
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000340 if (Var->isInAnonymousNamespace())
341 return UniqueExternalLinkage;
342
Douglas Gregorf73b2822009-11-25 22:24:25 +0000343 return ExternalLinkage;
344 }
345 }
346
347 // C++ [basic.link]p6:
348 // Names not covered by these rules have no linkage.
349 return NoLinkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000350 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000351
Douglas Gregor2ada0482009-02-04 17:27:36 +0000352std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000353 return getQualifiedNameAsString(getASTContext().getLangOptions());
354}
355
356std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000357 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
358 // std::string thrashing.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000359 std::vector<std::string> Names;
360 std::string QualName;
361 const DeclContext *Ctx = getDeclContext();
362
363 if (Ctx->isFunctionOrMethod())
364 return getNameAsString();
365
366 while (Ctx) {
Mike Stump11289f42009-09-09 15:08:12 +0000367 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000368 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
369 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
370 std::string TemplateArgsStr
371 = TemplateSpecializationType::PrintTemplateArgumentList(
372 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000373 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000374 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000375 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig07d211e2009-12-24 23:15:03 +0000376 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
377 if (ND->isAnonymousNamespace())
378 Names.push_back("<anonymous namespace>");
379 else
380 Names.push_back(ND->getNameAsString());
381 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
382 if (!RD->getIdentifier()) {
383 std::string RecordString = "<anonymous ";
384 RecordString += RD->getKindName();
385 RecordString += ">";
386 Names.push_back(RecordString);
387 } else {
388 Names.push_back(RD->getNameAsString());
389 }
Sam Weinigb999f682009-12-28 03:19:38 +0000390 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
391 std::string Proto = FD->getNameAsString();
392
393 const FunctionProtoType *FT = 0;
394 if (FD->hasWrittenPrototype())
395 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
396
397 Proto += "(";
398 if (FT) {
399 llvm::raw_string_ostream POut(Proto);
400 unsigned NumParams = FD->getNumParams();
401 for (unsigned i = 0; i < NumParams; ++i) {
402 if (i)
403 POut << ", ";
404 std::string Param;
405 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
406 POut << Param;
407 }
408
409 if (FT->isVariadic()) {
410 if (NumParams > 0)
411 POut << ", ";
412 POut << "...";
413 }
414 }
415 Proto += ")";
416
417 Names.push_back(Proto);
Douglas Gregor85673582009-05-18 17:01:57 +0000418 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000419 Names.push_back(ND->getNameAsString());
420 else
421 break;
422
423 Ctx = Ctx->getParent();
424 }
425
426 std::vector<std::string>::reverse_iterator
427 I = Names.rbegin(),
428 End = Names.rend();
429
430 for (; I!=End; ++I)
431 QualName += *I + "::";
432
John McCalla2a3f7d2010-03-16 21:48:18 +0000433 if (getDeclName())
434 QualName += getNameAsString();
435 else
436 QualName += "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000437
438 return QualName;
439}
440
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000441bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000442 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
443
Douglas Gregor889ceb72009-02-03 19:21:40 +0000444 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
445 // We want to keep it, unless it nominates same namespace.
446 if (getKind() == Decl::UsingDirective) {
447 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
448 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000451 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
452 // For function declarations, we keep track of redeclarations.
453 return FD->getPreviousDeclaration() == OldD;
454
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000455 // For function templates, the underlying function declarations are linked.
456 if (const FunctionTemplateDecl *FunctionTemplate
457 = dyn_cast<FunctionTemplateDecl>(this))
458 if (const FunctionTemplateDecl *OldFunctionTemplate
459 = dyn_cast<FunctionTemplateDecl>(OldD))
460 return FunctionTemplate->getTemplatedDecl()
461 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000462
Steve Naroffc4173fa2009-02-22 19:35:57 +0000463 // For method declarations, we keep track of redeclarations.
464 if (isa<ObjCMethodDecl>(this))
465 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000466
John McCall9f3059a2009-10-09 21:13:30 +0000467 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
468 return true;
469
John McCall3f746822009-11-17 05:59:44 +0000470 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
471 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
472 cast<UsingShadowDecl>(OldD)->getTargetDecl();
473
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000474 // For non-function declarations, if the declarations are of the
475 // same kind then this must be a redeclaration, or semantic analysis
476 // would not have given us the new declaration.
477 return this->getKind() == OldD->getKind();
478}
479
Douglas Gregoreddf4332009-02-24 20:03:32 +0000480bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000481 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000482}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000483
Anders Carlsson6915bf62009-06-26 06:29:23 +0000484NamedDecl *NamedDecl::getUnderlyingDecl() {
485 NamedDecl *ND = this;
486 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000487 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000488 ND = UD->getTargetDecl();
489 else if (ObjCCompatibleAliasDecl *AD
490 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
491 return AD->getClassInterface();
492 else
493 return ND;
494 }
495}
496
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000497//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000498// DeclaratorDecl Implementation
499//===----------------------------------------------------------------------===//
500
John McCall3e11ebe2010-03-15 10:12:16 +0000501DeclaratorDecl::~DeclaratorDecl() {}
502void DeclaratorDecl::Destroy(ASTContext &C) {
503 if (hasExtInfo())
504 C.Deallocate(getExtInfo());
505 ValueDecl::Destroy(C);
506}
507
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000508SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000509 if (DeclInfo) {
John McCall3e11ebe2010-03-15 10:12:16 +0000510 TypeLoc TL = getTypeSourceInfo()->getTypeLoc();
John McCall17001972009-10-18 01:05:36 +0000511 while (true) {
512 TypeLoc NextTL = TL.getNextTypeLoc();
513 if (!NextTL)
514 return TL.getSourceRange().getBegin();
515 TL = NextTL;
516 }
517 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000518 return SourceLocation();
519}
520
John McCall3e11ebe2010-03-15 10:12:16 +0000521void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
522 SourceRange QualifierRange) {
523 if (Qualifier) {
524 // Make sure the extended decl info is allocated.
525 if (!hasExtInfo()) {
526 // Save (non-extended) type source info pointer.
527 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
528 // Allocate external info struct.
529 DeclInfo = new (getASTContext()) ExtInfo;
530 // Restore savedTInfo into (extended) decl info.
531 getExtInfo()->TInfo = savedTInfo;
532 }
533 // Set qualifier info.
534 getExtInfo()->NNS = Qualifier;
535 getExtInfo()->NNSRange = QualifierRange;
536 }
537 else {
538 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
539 assert(QualifierRange.isInvalid());
540 if (hasExtInfo()) {
541 // Save type source info pointer.
542 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
543 // Deallocate the extended decl info.
544 getASTContext().Deallocate(getExtInfo());
545 // Restore savedTInfo into (non-extended) decl info.
546 DeclInfo = savedTInfo;
547 }
548 }
549}
550
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000551//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000552// VarDecl Implementation
553//===----------------------------------------------------------------------===//
554
Sebastian Redl833ef452010-01-26 22:01:41 +0000555const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
556 switch (SC) {
557 case VarDecl::None: break;
558 case VarDecl::Auto: return "auto"; break;
559 case VarDecl::Extern: return "extern"; break;
560 case VarDecl::PrivateExtern: return "__private_extern__"; break;
561 case VarDecl::Register: return "register"; break;
562 case VarDecl::Static: return "static"; break;
563 }
564
565 assert(0 && "Invalid storage class");
566 return 0;
567}
568
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000569VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000570 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000571 StorageClass S) {
John McCallbcd03502009-12-07 02:54:59 +0000572 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000573}
574
575void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000576 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000577 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000578 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000579 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
580 Eval->~EvaluatedStmt();
581 C.Deallocate(Eval);
582 }
583 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000584 this->~VarDecl();
John McCall3e11ebe2010-03-15 10:12:16 +0000585 DeclaratorDecl::Destroy(C);
Nuno Lopes394ec982008-12-17 23:39:55 +0000586}
587
588VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000589}
590
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000591SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000592 SourceLocation Start = getTypeSpecStartLoc();
593 if (Start.isInvalid())
594 Start = getLocation();
595
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000596 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000597 return SourceRange(Start, getInit()->getLocEnd());
598 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000599}
600
Sebastian Redl833ef452010-01-26 22:01:41 +0000601bool VarDecl::isExternC() const {
602 ASTContext &Context = getASTContext();
603 if (!Context.getLangOptions().CPlusPlus)
604 return (getDeclContext()->isTranslationUnit() &&
605 getStorageClass() != Static) ||
606 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
607
608 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
609 DC = DC->getParent()) {
610 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
611 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
612 return getStorageClass() != Static;
613
614 break;
615 }
616
617 if (DC->isFunctionOrMethod())
618 return false;
619 }
620
621 return false;
622}
623
624VarDecl *VarDecl::getCanonicalDecl() {
625 return getFirstDeclaration();
626}
627
Sebastian Redl35351a92010-01-31 22:27:38 +0000628VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
629 // C++ [basic.def]p2:
630 // A declaration is a definition unless [...] it contains the 'extern'
631 // specifier or a linkage-specification and neither an initializer [...],
632 // it declares a static data member in a class declaration [...].
633 // C++ [temp.expl.spec]p15:
634 // An explicit specialization of a static data member of a template is a
635 // definition if the declaration includes an initializer; otherwise, it is
636 // a declaration.
637 if (isStaticDataMember()) {
638 if (isOutOfLine() && (hasInit() ||
639 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
640 return Definition;
641 else
642 return DeclarationOnly;
643 }
644 // C99 6.7p5:
645 // A definition of an identifier is a declaration for that identifier that
646 // [...] causes storage to be reserved for that object.
647 // Note: that applies for all non-file-scope objects.
648 // C99 6.9.2p1:
649 // If the declaration of an identifier for an object has file scope and an
650 // initializer, the declaration is an external definition for the identifier
651 if (hasInit())
652 return Definition;
653 // AST for 'extern "C" int foo;' is annotated with 'extern'.
654 if (hasExternalStorage())
655 return DeclarationOnly;
656
657 // C99 6.9.2p2:
658 // A declaration of an object that has file scope without an initializer,
659 // and without a storage class specifier or the scs 'static', constitutes
660 // a tentative definition.
661 // No such thing in C++.
662 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
663 return TentativeDefinition;
664
665 // What's left is (in C, block-scope) declarations without initializers or
666 // external storage. These are definitions.
667 return Definition;
668}
669
Sebastian Redl35351a92010-01-31 22:27:38 +0000670VarDecl *VarDecl::getActingDefinition() {
671 DefinitionKind Kind = isThisDeclarationADefinition();
672 if (Kind != TentativeDefinition)
673 return 0;
674
675 VarDecl *LastTentative = false;
676 VarDecl *First = getFirstDeclaration();
677 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
678 I != E; ++I) {
679 Kind = (*I)->isThisDeclarationADefinition();
680 if (Kind == Definition)
681 return 0;
682 else if (Kind == TentativeDefinition)
683 LastTentative = *I;
684 }
685 return LastTentative;
686}
687
688bool VarDecl::isTentativeDefinitionNow() const {
689 DefinitionKind Kind = isThisDeclarationADefinition();
690 if (Kind != TentativeDefinition)
691 return false;
692
693 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
694 if ((*I)->isThisDeclarationADefinition() == Definition)
695 return false;
696 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000697 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000698}
699
Sebastian Redl5ca79842010-02-01 20:16:42 +0000700VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000701 VarDecl *First = getFirstDeclaration();
702 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
703 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000704 if ((*I)->isThisDeclarationADefinition() == Definition)
705 return *I;
706 }
707 return 0;
708}
709
710const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000711 redecl_iterator I = redecls_begin(), E = redecls_end();
712 while (I != E && !I->getInit())
713 ++I;
714
715 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000716 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000717 return I->getInit();
718 }
719 return 0;
720}
721
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000722bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000723 if (Decl::isOutOfLine())
724 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000725
726 if (!isStaticDataMember())
727 return false;
728
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000729 // If this static data member was instantiated from a static data member of
730 // a class template, check whether that static data member was defined
731 // out-of-line.
732 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
733 return VD->isOutOfLine();
734
735 return false;
736}
737
Douglas Gregor1d957a32009-10-27 18:42:08 +0000738VarDecl *VarDecl::getOutOfLineDefinition() {
739 if (!isStaticDataMember())
740 return 0;
741
742 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
743 RD != RDEnd; ++RD) {
744 if (RD->getLexicalDeclContext()->isFileContext())
745 return *RD;
746 }
747
748 return 0;
749}
750
Douglas Gregord5058122010-02-11 01:19:42 +0000751void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000752 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
753 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000754 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000755 }
756
757 Init = I;
758}
759
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000760VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000761 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000762 return cast<VarDecl>(MSI->getInstantiatedFrom());
763
764 return 0;
765}
766
Douglas Gregor3c74d412009-10-14 20:14:33 +0000767TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000768 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000769 return MSI->getTemplateSpecializationKind();
770
771 return TSK_Undeclared;
772}
773
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000774MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000775 return getASTContext().getInstantiatedFromStaticDataMember(this);
776}
777
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000778void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
779 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000780 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000781 assert(MSI && "Not an instantiated static data member?");
782 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000783 if (TSK != TSK_ExplicitSpecialization &&
784 PointOfInstantiation.isValid() &&
785 MSI->getPointOfInstantiation().isInvalid())
786 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000787}
788
Sebastian Redl833ef452010-01-26 22:01:41 +0000789//===----------------------------------------------------------------------===//
790// ParmVarDecl Implementation
791//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000792
Sebastian Redl833ef452010-01-26 22:01:41 +0000793ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
794 SourceLocation L, IdentifierInfo *Id,
795 QualType T, TypeSourceInfo *TInfo,
796 StorageClass S, Expr *DefArg) {
797 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000798}
799
Sebastian Redl833ef452010-01-26 22:01:41 +0000800Expr *ParmVarDecl::getDefaultArg() {
801 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
802 assert(!hasUninstantiatedDefaultArg() &&
803 "Default argument is not yet instantiated!");
804
805 Expr *Arg = getInit();
806 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
807 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000808
Sebastian Redl833ef452010-01-26 22:01:41 +0000809 return Arg;
810}
811
812unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
813 if (const CXXExprWithTemporaries *E =
814 dyn_cast<CXXExprWithTemporaries>(getInit()))
815 return E->getNumTemporaries();
816
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000817 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000818}
819
Sebastian Redl833ef452010-01-26 22:01:41 +0000820CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
821 assert(getNumDefaultArgTemporaries() &&
822 "Default arguments does not have any temporaries!");
823
824 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
825 return E->getTemporary(i);
826}
827
828SourceRange ParmVarDecl::getDefaultArgRange() const {
829 if (const Expr *E = getInit())
830 return E->getSourceRange();
831
832 if (hasUninstantiatedDefaultArg())
833 return getUninstantiatedDefaultArg()->getSourceRange();
834
835 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000836}
837
Nuno Lopes394ec982008-12-17 23:39:55 +0000838//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000839// FunctionDecl Implementation
840//===----------------------------------------------------------------------===//
841
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000842void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000843 if (Body && Body.isOffset())
844 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000845
846 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
847 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000848
Douglas Gregord801b062009-10-07 23:56:10 +0000849 FunctionTemplateSpecializationInfo *FTSInfo
850 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
851 if (FTSInfo)
852 C.Deallocate(FTSInfo);
853
854 MemberSpecializationInfo *MSInfo
855 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
856 if (MSInfo)
857 C.Deallocate(MSInfo);
858
Steve Naroff13ae6f42009-01-27 21:25:57 +0000859 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000860
John McCall3e11ebe2010-03-15 10:12:16 +0000861 DeclaratorDecl::Destroy(C);
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000862}
863
John McCalle1f2ec22009-09-11 06:45:03 +0000864void FunctionDecl::getNameForDiagnostic(std::string &S,
865 const PrintingPolicy &Policy,
866 bool Qualified) const {
867 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
868 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
869 if (TemplateArgs)
870 S += TemplateSpecializationType::PrintTemplateArgumentList(
871 TemplateArgs->getFlatArgumentList(),
872 TemplateArgs->flat_size(),
873 Policy);
874
875}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000876
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000877Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000878 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
879 if (I->Body) {
880 Definition = *I;
881 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000882 }
883 }
884
885 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000886}
887
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000888void FunctionDecl::setBody(Stmt *B) {
889 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000890 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000891 EndRangeLoc = B->getLocEnd();
892}
893
Douglas Gregor16618f22009-09-12 00:17:51 +0000894bool FunctionDecl::isMain() const {
895 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000896 return !Context.getLangOptions().Freestanding &&
897 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000898 getIdentifier() && getIdentifier()->isStr("main");
899}
900
Douglas Gregor16618f22009-09-12 00:17:51 +0000901bool FunctionDecl::isExternC() const {
902 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000903 // In C, any non-static, non-overloadable function has external
904 // linkage.
905 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000906 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000907
Mike Stump11289f42009-09-09 15:08:12 +0000908 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000909 DC = DC->getParent()) {
910 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
911 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000912 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000913 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000914
915 break;
916 }
917 }
918
919 return false;
920}
921
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000922bool FunctionDecl::isGlobal() const {
923 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
924 return Method->isStatic();
925
926 if (getStorageClass() == Static)
927 return false;
928
Mike Stump11289f42009-09-09 15:08:12 +0000929 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000930 DC->isNamespace();
931 DC = DC->getParent()) {
932 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
933 if (!Namespace->getDeclName())
934 return false;
935 break;
936 }
937 }
938
939 return true;
940}
941
Sebastian Redl833ef452010-01-26 22:01:41 +0000942void
943FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
944 redeclarable_base::setPreviousDeclaration(PrevDecl);
945
946 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
947 FunctionTemplateDecl *PrevFunTmpl
948 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
949 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
950 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
951 }
952}
953
954const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
955 return getFirstDeclaration();
956}
957
958FunctionDecl *FunctionDecl::getCanonicalDecl() {
959 return getFirstDeclaration();
960}
961
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000962/// \brief Returns a value indicating whether this function
963/// corresponds to a builtin function.
964///
965/// The function corresponds to a built-in function if it is
966/// declared at translation scope or within an extern "C" block and
967/// its name matches with the name of a builtin. The returned value
968/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000969/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000970/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000971unsigned FunctionDecl::getBuiltinID() const {
972 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000973 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
974 return 0;
975
976 unsigned BuiltinID = getIdentifier()->getBuiltinID();
977 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
978 return BuiltinID;
979
980 // This function has the name of a known C library
981 // function. Determine whether it actually refers to the C library
982 // function or whether it just has the same name.
983
Douglas Gregora908e7f2009-02-17 03:23:10 +0000984 // If this is a static function, it's not a builtin.
985 if (getStorageClass() == Static)
986 return 0;
987
Douglas Gregore711f702009-02-14 18:57:46 +0000988 // If this function is at translation-unit scope and we're not in
989 // C++, it refers to the C library function.
990 if (!Context.getLangOptions().CPlusPlus &&
991 getDeclContext()->isTranslationUnit())
992 return BuiltinID;
993
994 // If the function is in an extern "C" linkage specification and is
995 // not marked "overloadable", it's the real function.
996 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000997 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000998 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000999 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001000 return BuiltinID;
1001
1002 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001003 return 0;
1004}
1005
1006
Chris Lattner47c0d002009-04-25 06:03:53 +00001007/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001008/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001009/// after it has been created.
1010unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001011 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001012 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001013 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001014 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001016}
1017
Douglas Gregord5058122010-02-11 01:19:42 +00001018void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001019 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001020 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001021
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001022 // Zero params -> null pointer.
1023 if (NumParams) {
Douglas Gregord5058122010-02-11 01:19:42 +00001024 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001025 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001026 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001027
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001028 // Update source range. The check below allows us to set EndRangeLoc before
1029 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001030 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001031 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001032 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001033}
Chris Lattner41943152007-01-25 04:52:46 +00001034
Chris Lattner58258242008-04-10 02:22:51 +00001035/// getMinRequiredArguments - Returns the minimum number of arguments
1036/// needed to call this function. This may be fewer than the number of
1037/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001038/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001039unsigned FunctionDecl::getMinRequiredArguments() const {
1040 unsigned NumRequiredArgs = getNumParams();
1041 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001042 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001043 --NumRequiredArgs;
1044
1045 return NumRequiredArgs;
1046}
1047
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001048bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001049 // FIXME: This is not enough. Consider:
1050 //
1051 // inline void f();
1052 // void f() { }
1053 //
1054 // f is inlined, but does not have inline specified.
1055 // To fix this we should add an 'inline' flag to FunctionDecl.
1056 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001057 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001058
1059 if (isa<CXXMethodDecl>(this)) {
1060 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1061 return true;
1062 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001063
1064 switch (getTemplateSpecializationKind()) {
1065 case TSK_Undeclared:
1066 case TSK_ExplicitSpecialization:
1067 return false;
1068
1069 case TSK_ImplicitInstantiation:
1070 case TSK_ExplicitInstantiationDeclaration:
1071 case TSK_ExplicitInstantiationDefinition:
1072 // Handle below.
1073 break;
1074 }
1075
1076 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1077 Stmt *Pattern = 0;
1078 if (PatternDecl)
1079 Pattern = PatternDecl->getBody(PatternDecl);
1080
1081 if (Pattern && PatternDecl)
1082 return PatternDecl->isInlined();
1083
1084 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001085}
1086
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001087/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001088/// definition will be externally visible.
1089///
1090/// Inline function definitions are always available for inlining optimizations.
1091/// However, depending on the language dialect, declaration specifiers, and
1092/// attributes, the definition of an inline function may or may not be
1093/// "externally" visible to other translation units in the program.
1094///
1095/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001096/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001097/// inline definition becomes externally visible (C99 6.7.4p6).
1098///
1099/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1100/// definition, we use the GNU semantics for inline, which are nearly the
1101/// opposite of C99 semantics. In particular, "inline" by itself will create
1102/// an externally visible symbol, but "extern inline" will not create an
1103/// externally visible symbol.
1104bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1105 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001106 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001107 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001108
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001109 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001110 // GNU inline semantics. Based on a number of examples, we came up with the
1111 // following heuristic: if the "inline" keyword is present on a
1112 // declaration of the function but "extern" is not present on that
1113 // declaration, then the symbol is externally visible. Otherwise, the GNU
1114 // "extern inline" semantics applies and the symbol is not externally
1115 // visible.
1116 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1117 Redecl != RedeclEnd;
1118 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001119 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001120 return true;
1121 }
1122
1123 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001124 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001125 }
1126
1127 // C99 6.7.4p6:
1128 // [...] If all of the file scope declarations for a function in a
1129 // translation unit include the inline function specifier without extern,
1130 // then the definition in that translation unit is an inline definition.
1131 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1132 Redecl != RedeclEnd;
1133 ++Redecl) {
1134 // Only consider file-scope declarations in this test.
1135 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1136 continue;
1137
Douglas Gregor35b57532009-10-27 21:01:01 +00001138 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001139 return true; // Not an inline definition
1140 }
1141
1142 // C99 6.7.4p6:
1143 // An inline definition does not provide an external definition for the
1144 // function, and does not forbid an external definition in another
1145 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001146 return false;
1147}
1148
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001149/// getOverloadedOperator - Which C++ overloaded operator this
1150/// function represents, if any.
1151OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001152 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1153 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001154 else
1155 return OO_None;
1156}
1157
Alexis Huntc88db062010-01-13 09:01:02 +00001158/// getLiteralIdentifier - The literal suffix identifier this function
1159/// represents, if any.
1160const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1161 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1162 return getDeclName().getCXXLiteralIdentifier();
1163 else
1164 return 0;
1165}
1166
Douglas Gregord801b062009-10-07 23:56:10 +00001167FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001168 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001169 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1170
1171 return 0;
1172}
1173
Douglas Gregor06db9f52009-10-12 20:18:28 +00001174MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1175 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1176}
1177
Douglas Gregord801b062009-10-07 23:56:10 +00001178void
1179FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1180 TemplateSpecializationKind TSK) {
1181 assert(TemplateOrSpecialization.isNull() &&
1182 "Member function is already a specialization");
1183 MemberSpecializationInfo *Info
1184 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1185 TemplateOrSpecialization = Info;
1186}
1187
Douglas Gregorafca3b42009-10-27 20:53:28 +00001188bool FunctionDecl::isImplicitlyInstantiable() const {
1189 // If this function already has a definition or is invalid, it can't be
1190 // implicitly instantiated.
1191 if (isInvalidDecl() || getBody())
1192 return false;
1193
1194 switch (getTemplateSpecializationKind()) {
1195 case TSK_Undeclared:
1196 case TSK_ExplicitSpecialization:
1197 case TSK_ExplicitInstantiationDefinition:
1198 return false;
1199
1200 case TSK_ImplicitInstantiation:
1201 return true;
1202
1203 case TSK_ExplicitInstantiationDeclaration:
1204 // Handled below.
1205 break;
1206 }
1207
1208 // Find the actual template from which we will instantiate.
1209 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1210 Stmt *Pattern = 0;
1211 if (PatternDecl)
1212 Pattern = PatternDecl->getBody(PatternDecl);
1213
1214 // C++0x [temp.explicit]p9:
1215 // Except for inline functions, other explicit instantiation declarations
1216 // have the effect of suppressing the implicit instantiation of the entity
1217 // to which they refer.
1218 if (!Pattern || !PatternDecl)
1219 return true;
1220
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001221 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001222}
1223
1224FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1225 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1226 while (Primary->getInstantiatedFromMemberTemplate()) {
1227 // If we have hit a point where the user provided a specialization of
1228 // this template, we're done looking.
1229 if (Primary->isMemberSpecialization())
1230 break;
1231
1232 Primary = Primary->getInstantiatedFromMemberTemplate();
1233 }
1234
1235 return Primary->getTemplatedDecl();
1236 }
1237
1238 return getInstantiatedFromMemberFunction();
1239}
1240
Douglas Gregor70d83e22009-06-29 17:30:29 +00001241FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001242 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001243 = TemplateOrSpecialization
1244 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001245 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001246 }
1247 return 0;
1248}
1249
1250const TemplateArgumentList *
1251FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001252 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001253 = TemplateOrSpecialization
1254 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001255 return Info->TemplateArguments;
1256 }
1257 return 0;
1258}
1259
Mike Stump11289f42009-09-09 15:08:12 +00001260void
Douglas Gregord5058122010-02-11 01:19:42 +00001261FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001262 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001263 void *InsertPos,
1264 TemplateSpecializationKind TSK) {
1265 assert(TSK != TSK_Undeclared &&
1266 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001267 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001268 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001269 if (!Info)
Douglas Gregord5058122010-02-11 01:19:42 +00001270 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001271
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001272 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001273 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001274 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001275 Info->TemplateArguments = TemplateArgs;
1276 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001277
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001278 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001279 // function template specializations.
1280 if (InsertPos)
1281 Template->getSpecializations().InsertNode(Info, InsertPos);
1282 else {
1283 // Try to insert the new node. If there is an existing node, remove it
1284 // first.
1285 FunctionTemplateSpecializationInfo *Existing
1286 = Template->getSpecializations().GetOrInsertNode(Info);
1287 if (Existing) {
1288 Template->getSpecializations().RemoveNode(Existing);
1289 Template->getSpecializations().GetOrInsertNode(Info);
1290 }
1291 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001292}
1293
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001294TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001295 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001296 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001297 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001298 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001299 if (FTSInfo)
1300 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregord801b062009-10-07 23:56:10 +00001302 MemberSpecializationInfo *MSInfo
1303 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1304 if (MSInfo)
1305 return MSInfo->getTemplateSpecializationKind();
1306
1307 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001308}
1309
Mike Stump11289f42009-09-09 15:08:12 +00001310void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001311FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1312 SourceLocation PointOfInstantiation) {
1313 if (FunctionTemplateSpecializationInfo *FTSInfo
1314 = TemplateOrSpecialization.dyn_cast<
1315 FunctionTemplateSpecializationInfo*>()) {
1316 FTSInfo->setTemplateSpecializationKind(TSK);
1317 if (TSK != TSK_ExplicitSpecialization &&
1318 PointOfInstantiation.isValid() &&
1319 FTSInfo->getPointOfInstantiation().isInvalid())
1320 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1321 } else if (MemberSpecializationInfo *MSInfo
1322 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1323 MSInfo->setTemplateSpecializationKind(TSK);
1324 if (TSK != TSK_ExplicitSpecialization &&
1325 PointOfInstantiation.isValid() &&
1326 MSInfo->getPointOfInstantiation().isInvalid())
1327 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1328 } else
1329 assert(false && "Function cannot have a template specialization kind");
1330}
1331
1332SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001333 if (FunctionTemplateSpecializationInfo *FTSInfo
1334 = TemplateOrSpecialization.dyn_cast<
1335 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001336 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001337 else if (MemberSpecializationInfo *MSInfo
1338 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001339 return MSInfo->getPointOfInstantiation();
1340
1341 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001342}
1343
Douglas Gregor6411b922009-09-11 20:15:17 +00001344bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001345 if (Decl::isOutOfLine())
1346 return true;
1347
1348 // If this function was instantiated from a member function of a
1349 // class template, check whether that member function was defined out-of-line.
1350 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1351 const FunctionDecl *Definition;
1352 if (FD->getBody(Definition))
1353 return Definition->isOutOfLine();
1354 }
1355
1356 // If this function was instantiated from a function template,
1357 // check whether that function template was defined out-of-line.
1358 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1359 const FunctionDecl *Definition;
1360 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1361 return Definition->isOutOfLine();
1362 }
1363
1364 return false;
1365}
1366
Chris Lattner59a25942008-03-31 00:36:02 +00001367//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001368// FieldDecl Implementation
1369//===----------------------------------------------------------------------===//
1370
1371FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1372 IdentifierInfo *Id, QualType T,
1373 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1374 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1375}
1376
1377bool FieldDecl::isAnonymousStructOrUnion() const {
1378 if (!isImplicit() || getDeclName())
1379 return false;
1380
1381 if (const RecordType *Record = getType()->getAs<RecordType>())
1382 return Record->getDecl()->isAnonymousStructOrUnion();
1383
1384 return false;
1385}
1386
1387//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001388// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001389//===----------------------------------------------------------------------===//
1390
John McCall3e11ebe2010-03-15 10:12:16 +00001391void TagDecl::Destroy(ASTContext &C) {
1392 if (hasExtInfo())
1393 C.Deallocate(getExtInfo());
1394 TypeDecl::Destroy(C);
1395}
1396
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001397SourceRange TagDecl::getSourceRange() const {
1398 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001399 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001400}
1401
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001402TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001403 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001404}
1405
Douglas Gregordee1be82009-01-17 00:42:38 +00001406void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001407 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1408 TagT->decl.setPointer(this);
1409 TagT->decl.setInt(1);
1410 }
John McCall67da35c2010-02-04 22:26:26 +00001411
1412 if (isa<CXXRecordDecl>(this)) {
1413 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1414 struct CXXRecordDecl::DefinitionData *Data =
1415 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001416 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1417 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001418 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001419}
1420
1421void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001422 assert((!isa<CXXRecordDecl>(this) ||
1423 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1424 "definition completed but not started");
1425
Douglas Gregordee1be82009-01-17 00:42:38 +00001426 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001427 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1428 assert(TagT->decl.getPointer() == this &&
1429 "Attempt to redefine a tag definition?");
1430 TagT->decl.setInt(0);
1431 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001432}
1433
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001434TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001435 if (isDefinition())
1436 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001437
1438 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001439 R != REnd; ++R)
1440 if (R->isDefinition())
1441 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001442
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001443 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001444}
1445
John McCall06f6fe8d2009-09-04 01:14:41 +00001446TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1447 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001448 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001449 case DeclSpec::TST_struct: return TK_struct;
1450 case DeclSpec::TST_class: return TK_class;
1451 case DeclSpec::TST_union: return TK_union;
1452 case DeclSpec::TST_enum: return TK_enum;
1453 }
1454}
1455
John McCall3e11ebe2010-03-15 10:12:16 +00001456void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1457 SourceRange QualifierRange) {
1458 if (Qualifier) {
1459 // Make sure the extended qualifier info is allocated.
1460 if (!hasExtInfo())
1461 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1462 // Set qualifier info.
1463 getExtInfo()->NNS = Qualifier;
1464 getExtInfo()->NNSRange = QualifierRange;
1465 }
1466 else {
1467 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1468 assert(QualifierRange.isInvalid());
1469 if (hasExtInfo()) {
1470 getASTContext().Deallocate(getExtInfo());
1471 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1472 }
1473 }
1474}
1475
Ted Kremenek21475702008-09-05 17:16:31 +00001476//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001477// EnumDecl Implementation
1478//===----------------------------------------------------------------------===//
1479
1480EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1481 IdentifierInfo *Id, SourceLocation TKL,
1482 EnumDecl *PrevDecl) {
1483 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1484 C.getTypeDeclType(Enum, PrevDecl);
1485 return Enum;
1486}
1487
1488void EnumDecl::Destroy(ASTContext& C) {
John McCall3e11ebe2010-03-15 10:12:16 +00001489 TagDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001490}
1491
Douglas Gregord5058122010-02-11 01:19:42 +00001492void EnumDecl::completeDefinition(QualType NewType,
Sebastian Redl833ef452010-01-26 22:01:41 +00001493 QualType NewPromotionType) {
1494 assert(!isDefinition() && "Cannot redefine enums!");
1495 IntegerType = NewType;
1496 PromotionType = NewPromotionType;
1497 TagDecl::completeDefinition();
1498}
1499
1500//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001501// RecordDecl Implementation
1502//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001503
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001504RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001505 IdentifierInfo *Id, RecordDecl *PrevDecl,
1506 SourceLocation TKL)
1507 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001508 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001509 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001510 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001511 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001512}
1513
1514RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001515 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001516 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001517
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001518 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001519 C.getTypeDeclType(R, PrevDecl);
1520 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001521}
1522
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001523RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001524}
1525
1526void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001527 TagDecl::Destroy(C);
1528}
1529
Douglas Gregordfcad112009-03-25 15:59:44 +00001530bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001531 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001532 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1533}
1534
Douglas Gregor91f84212008-12-11 16:49:14 +00001535/// completeDefinition - Notes that the definition of this type is now
1536/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001537void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001538 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001539 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001540}
Steve Naroffcc321422007-03-26 23:09:51 +00001541
Steve Naroff415d3d52008-10-08 17:01:13 +00001542//===----------------------------------------------------------------------===//
1543// BlockDecl Implementation
1544//===----------------------------------------------------------------------===//
1545
1546BlockDecl::~BlockDecl() {
1547}
1548
1549void BlockDecl::Destroy(ASTContext& C) {
1550 if (Body)
1551 Body->Destroy(C);
1552
1553 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1554 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001555
1556 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001557 Decl::Destroy(C);
1558}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001559
Douglas Gregord5058122010-02-11 01:19:42 +00001560void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001561 unsigned NParms) {
1562 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001563
Steve Naroffc4b30e52009-03-13 16:56:44 +00001564 // Zero params -> null pointer.
1565 if (NParms) {
1566 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001567 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001568 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1569 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1570 }
1571}
1572
1573unsigned BlockDecl::getNumParams() const {
1574 return NumParams;
1575}
Sebastian Redl833ef452010-01-26 22:01:41 +00001576
1577
1578//===----------------------------------------------------------------------===//
1579// Other Decl Allocation/Deallocation Method Implementations
1580//===----------------------------------------------------------------------===//
1581
1582TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1583 return new (C) TranslationUnitDecl(C);
1584}
1585
1586NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1587 SourceLocation L, IdentifierInfo *Id) {
1588 return new (C) NamespaceDecl(DC, L, Id);
1589}
1590
1591void NamespaceDecl::Destroy(ASTContext& C) {
1592 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1593 // together. They are all top-level Decls.
1594
1595 this->~NamespaceDecl();
John McCall3e11ebe2010-03-15 10:12:16 +00001596 Decl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001597}
1598
1599
1600ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1601 SourceLocation L, IdentifierInfo *Id, QualType T) {
1602 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1603}
1604
1605FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1606 SourceLocation L,
1607 DeclarationName N, QualType T,
1608 TypeSourceInfo *TInfo,
1609 StorageClass S, bool isInline,
1610 bool hasWrittenPrototype) {
1611 FunctionDecl *New
1612 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
1613 New->HasWrittenPrototype = hasWrittenPrototype;
1614 return New;
1615}
1616
1617BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1618 return new (C) BlockDecl(DC, L);
1619}
1620
1621EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1622 SourceLocation L,
1623 IdentifierInfo *Id, QualType T,
1624 Expr *E, const llvm::APSInt &V) {
1625 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1626}
1627
1628void EnumConstantDecl::Destroy(ASTContext& C) {
1629 if (Init) Init->Destroy(C);
John McCall3e11ebe2010-03-15 10:12:16 +00001630 ValueDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001631}
1632
1633TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1634 SourceLocation L, IdentifierInfo *Id,
1635 TypeSourceInfo *TInfo) {
1636 return new (C) TypedefDecl(DC, L, Id, TInfo);
1637}
1638
1639// Anchor TypedefDecl's vtable here.
1640TypedefDecl::~TypedefDecl() {}
1641
1642FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1643 SourceLocation L,
1644 StringLiteral *Str) {
1645 return new (C) FileScopeAsmDecl(DC, L, Str);
1646}