blob: fc805451cba0e15ba6efbbe017053d617d7e2780 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
John McCallf1bbbb42009-09-04 01:14:41 +000026#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000031/// \brief Return the TypeLoc wrapper for the type source info.
John McCalla93c9342009-12-07 02:54:59 +000032TypeLoc TypeSourceInfo::getTypeLoc() const {
Argyrios Kyrtzidisb7354712009-09-29 19:40:20 +000033 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000034}
Chris Lattner0b2b6e12009-03-04 06:34:08 +000035
Chris Lattnerd3b90652008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000040/// \brief Get the most restrictive linkage for the types in the given
41/// template parameter list.
42static Linkage
43getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
44 Linkage L = ExternalLinkage;
45 for (TemplateParameterList::const_iterator P = Params->begin(),
46 PEnd = Params->end();
47 P != PEnd; ++P) {
48 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
49 if (!NTTP->getType()->isDependentType()) {
50 L = minLinkage(L, NTTP->getType()->getLinkage());
51 continue;
52 }
53
54 if (TemplateTemplateParmDecl *TTP
55 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
56 L = minLinkage(L,
57 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
58 }
59 }
60
61 return L;
62}
63
64/// \brief Get the most restrictive linkage for the types and
65/// declarations in the given template argument list.
66static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
67 unsigned NumArgs) {
68 Linkage L = ExternalLinkage;
69
70 for (unsigned I = 0; I != NumArgs; ++I) {
71 switch (Args[I].getKind()) {
72 case TemplateArgument::Null:
73 case TemplateArgument::Integral:
74 case TemplateArgument::Expression:
75 break;
76
77 case TemplateArgument::Type:
78 L = minLinkage(L, Args[I].getAsType()->getLinkage());
79 break;
80
81 case TemplateArgument::Declaration:
82 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
83 L = minLinkage(L, ND->getLinkage());
84 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
85 L = minLinkage(L, VD->getType()->getLinkage());
86 break;
87
88 case TemplateArgument::Template:
89 if (TemplateDecl *Template
90 = Args[I].getAsTemplate().getAsTemplateDecl())
91 L = minLinkage(L, Template->getLinkage());
92 break;
93
94 case TemplateArgument::Pack:
95 L = minLinkage(L,
96 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
97 Args[I].pack_size()));
98 break;
99 }
100 }
101
102 return L;
103}
104
105static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000106 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
107 "Not a name having namespace scope");
108 ASTContext &Context = D->getASTContext();
109
110 // C++ [basic.link]p3:
111 // A name having namespace scope (3.3.6) has internal linkage if it
112 // is the name of
113 // - an object, reference, function or function template that is
114 // explicitly declared static; or,
115 // (This bullet corresponds to C99 6.2.2p3.)
116 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
117 // Explicitly declared static.
118 if (Var->getStorageClass() == VarDecl::Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000119 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000120
121 // - an object or reference that is explicitly declared const
122 // and neither explicitly declared extern nor previously
123 // declared to have external linkage; or
124 // (there is no equivalent in C99)
125 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000126 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000127 Var->getStorageClass() != VarDecl::Extern &&
128 Var->getStorageClass() != VarDecl::PrivateExtern) {
129 bool FoundExtern = false;
130 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
131 PrevVar && !FoundExtern;
132 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000133 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000134 FoundExtern = true;
135
136 if (!FoundExtern)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000137 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000138 }
139 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000140 // C++ [temp]p4:
141 // A non-member function template can have internal linkage; any
142 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000143 const FunctionDecl *Function = 0;
144 if (const FunctionTemplateDecl *FunTmpl
145 = dyn_cast<FunctionTemplateDecl>(D))
146 Function = FunTmpl->getTemplatedDecl();
147 else
148 Function = cast<FunctionDecl>(D);
149
150 // Explicitly declared static.
151 if (Function->getStorageClass() == FunctionDecl::Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000152 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000153 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
154 // - a data member of an anonymous union.
155 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000156 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000157 }
158
159 // C++ [basic.link]p4:
160
161 // A name having namespace scope has external linkage if it is the
162 // name of
163 //
164 // - an object or reference, unless it has internal linkage; or
165 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
166 if (!Context.getLangOptions().CPlusPlus &&
167 (Var->getStorageClass() == VarDecl::Extern ||
168 Var->getStorageClass() == VarDecl::PrivateExtern)) {
169 // C99 6.2.2p4:
170 // For an identifier declared with the storage-class specifier
171 // extern in a scope in which a prior declaration of that
172 // identifier is visible, if the prior declaration specifies
173 // internal or external linkage, the linkage of the identifier
174 // at the later declaration is the same as the linkage
175 // specified at the prior declaration. If no prior declaration
176 // is visible, or if the prior declaration specifies no
177 // linkage, then the identifier has external linkage.
178 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000179 if (Linkage L = PrevVar->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000180 return L;
181 }
182 }
183
184 // C99 6.2.2p5:
185 // If the declaration of an identifier for an object has file
186 // scope and no storage-class specifier, its linkage is
187 // external.
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000188 if (Var->isInAnonymousNamespace())
189 return UniqueExternalLinkage;
190
191 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000192 }
193
194 // - a function, unless it has internal linkage; or
195 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
196 // C99 6.2.2p5:
197 // If the declaration of an identifier for a function has no
198 // storage-class specifier, its linkage is determined exactly
199 // as if it were declared with the storage-class specifier
200 // extern.
201 if (!Context.getLangOptions().CPlusPlus &&
202 (Function->getStorageClass() == FunctionDecl::Extern ||
203 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
204 Function->getStorageClass() == FunctionDecl::None)) {
205 // C99 6.2.2p4:
206 // For an identifier declared with the storage-class specifier
207 // extern in a scope in which a prior declaration of that
208 // identifier is visible, if the prior declaration specifies
209 // internal or external linkage, the linkage of the identifier
210 // at the later declaration is the same as the linkage
211 // specified at the prior declaration. If no prior declaration
212 // is visible, or if the prior declaration specifies no
213 // linkage, then the identifier has external linkage.
214 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000215 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000216 return L;
217 }
218 }
219
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000220 if (Function->isInAnonymousNamespace())
221 return UniqueExternalLinkage;
222
223 if (FunctionTemplateSpecializationInfo *SpecInfo
224 = Function->getTemplateSpecializationInfo()) {
225 Linkage L = SpecInfo->getTemplate()->getLinkage();
226 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
227 L = minLinkage(L,
228 getLinkageForTemplateArgumentList(
229 TemplateArgs.getFlatArgumentList(),
230 TemplateArgs.flat_size()));
231 return L;
232 }
233
234 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000235 }
236
237 // - a named class (Clause 9), or an unnamed class defined in a
238 // typedef declaration in which the class has the typedef name
239 // for linkage purposes (7.1.3); or
240 // - a named enumeration (7.2), or an unnamed enumeration
241 // defined in a typedef declaration in which the enumeration
242 // has the typedef name for linkage purposes (7.1.3); or
243 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000244 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
245 if (Tag->isInAnonymousNamespace())
246 return UniqueExternalLinkage;
247
248 // If this is a class template specialization, consider the
249 // linkage of the template and template arguments.
250 if (const ClassTemplateSpecializationDecl *Spec
251 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
252 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
253 Linkage L = getLinkageForTemplateArgumentList(
254 TemplateArgs.getFlatArgumentList(),
255 TemplateArgs.flat_size());
256 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
257 }
258
259 return ExternalLinkage;
260 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000261
262 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000263 if (isa<EnumConstantDecl>(D)) {
264 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
265 if (isExternalLinkage(L))
266 return L;
267 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000268
269 // - a template, unless it is a function template that has
270 // internal linkage (Clause 14);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000271 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
272 if (D->isInAnonymousNamespace())
273 return UniqueExternalLinkage;
274
275 return getLinkageForTemplateParameterList(
276 Template->getTemplateParameters());
277 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000278
279 // - a namespace (7.3), unless it is declared within an unnamed
280 // namespace.
281 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000282 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000283
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000284 return NoLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000285}
286
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000287Linkage NamedDecl::getLinkage() const {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000288
289 // Objective-C: treat all Objective-C declarations as having external
290 // linkage.
291 switch (getKind()) {
292 default:
293 break;
294 case Decl::ObjCAtDefsField:
295 case Decl::ObjCCategory:
296 case Decl::ObjCCategoryImpl:
297 case Decl::ObjCClass:
298 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000299 case Decl::ObjCForwardProtocol:
300 case Decl::ObjCImplementation:
301 case Decl::ObjCInterface:
302 case Decl::ObjCIvar:
303 case Decl::ObjCMethod:
304 case Decl::ObjCProperty:
305 case Decl::ObjCPropertyImpl:
306 case Decl::ObjCProtocol:
307 return ExternalLinkage;
308 }
309
Douglas Gregord85b5b92009-11-25 22:24:25 +0000310 // Handle linkage for namespace-scope names.
311 if (getDeclContext()->getLookupContext()->isFileContext())
312 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
313 return L;
314
315 // C++ [basic.link]p5:
316 // In addition, a member function, static data member, a named
317 // class or enumeration of class scope, or an unnamed class or
318 // enumeration defined in a class-scope typedef declaration such
319 // that the class or enumeration has the typedef name for linkage
320 // purposes (7.1.3), has external linkage if the name of the class
321 // has external linkage.
322 if (getDeclContext()->isRecord() &&
323 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
324 (isa<TagDecl>(this) &&
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000325 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
326 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
327 if (isExternalLinkage(L))
328 return L;
329 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000330
331 // C++ [basic.link]p6:
332 // The name of a function declared in block scope and the name of
333 // an object declared by a block scope extern declaration have
334 // linkage. If there is a visible declaration of an entity with
335 // linkage having the same name and type, ignoring entities
336 // declared outside the innermost enclosing namespace scope, the
337 // block scope declaration declares that same entity and receives
338 // the linkage of the previous declaration. If there is more than
339 // one such matching entity, the program is ill-formed. Otherwise,
340 // if no matching entity is found, the block scope entity receives
341 // external linkage.
342 if (getLexicalDeclContext()->isFunctionOrMethod()) {
343 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
344 if (Function->getPreviousDeclaration())
345 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
346 return L;
347
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000348 if (Function->isInAnonymousNamespace())
349 return UniqueExternalLinkage;
350
Douglas Gregord85b5b92009-11-25 22:24:25 +0000351 return ExternalLinkage;
352 }
353
354 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
355 if (Var->getStorageClass() == VarDecl::Extern ||
356 Var->getStorageClass() == VarDecl::PrivateExtern) {
357 if (Var->getPreviousDeclaration())
358 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
359 return L;
360
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000361 if (Var->isInAnonymousNamespace())
362 return UniqueExternalLinkage;
363
Douglas Gregord85b5b92009-11-25 22:24:25 +0000364 return ExternalLinkage;
365 }
366 }
367
368 // C++ [basic.link]p6:
369 // Names not covered by these rules have no linkage.
370 return NoLinkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000371 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000372
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000373std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000374 return getQualifiedNameAsString(getASTContext().getLangOptions());
375}
376
377std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000378 const DeclContext *Ctx = getDeclContext();
379
380 if (Ctx->isFunctionOrMethod())
381 return getNameAsString();
382
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000383 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
384 ContextsTy Contexts;
385
386 // Collect contexts.
387 while (Ctx && isa<NamedDecl>(Ctx)) {
388 Contexts.push_back(Ctx);
389 Ctx = Ctx->getParent();
390 };
391
392 std::string QualName;
393 llvm::raw_string_ostream OS(QualName);
394
395 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
396 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000397 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000398 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000399 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
400 std::string TemplateArgsStr
401 = TemplateSpecializationType::PrintTemplateArgumentList(
402 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000403 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000404 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000405 OS << Spec->getName() << TemplateArgsStr;
406 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000407 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000408 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000409 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000410 OS << ND;
411 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
412 if (!RD->getIdentifier())
413 OS << "<anonymous " << RD->getKindName() << '>';
414 else
415 OS << RD;
416 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000417 const FunctionProtoType *FT = 0;
418 if (FD->hasWrittenPrototype())
419 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
420
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000421 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000422 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000423 unsigned NumParams = FD->getNumParams();
424 for (unsigned i = 0; i < NumParams; ++i) {
425 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000426 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000427 std::string Param;
428 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000429 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000430 }
431
432 if (FT->isVariadic()) {
433 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000434 OS << ", ";
435 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000436 }
437 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000438 OS << ')';
439 } else {
440 OS << cast<NamedDecl>(*I);
441 }
442 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000443 }
444
John McCall8472af42010-03-16 21:48:18 +0000445 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000446 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000447 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000448 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000449
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000450 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000451}
452
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000453bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000454 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
455
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000456 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
457 // We want to keep it, unless it nominates same namespace.
458 if (getKind() == Decl::UsingDirective) {
459 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
460 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000463 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
464 // For function declarations, we keep track of redeclarations.
465 return FD->getPreviousDeclaration() == OldD;
466
Douglas Gregore53060f2009-06-25 22:08:12 +0000467 // For function templates, the underlying function declarations are linked.
468 if (const FunctionTemplateDecl *FunctionTemplate
469 = dyn_cast<FunctionTemplateDecl>(this))
470 if (const FunctionTemplateDecl *OldFunctionTemplate
471 = dyn_cast<FunctionTemplateDecl>(OldD))
472 return FunctionTemplate->getTemplatedDecl()
473 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Steve Naroff0de21fd2009-02-22 19:35:57 +0000475 // For method declarations, we keep track of redeclarations.
476 if (isa<ObjCMethodDecl>(this))
477 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
John McCallf36e02d2009-10-09 21:13:30 +0000479 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
480 return true;
481
John McCall9488ea12009-11-17 05:59:44 +0000482 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
483 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
484 cast<UsingShadowDecl>(OldD)->getTargetDecl();
485
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000486 // For non-function declarations, if the declarations are of the
487 // same kind then this must be a redeclaration, or semantic analysis
488 // would not have given us the new declaration.
489 return this->getKind() == OldD->getKind();
490}
491
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000492bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000493 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000494}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000495
Anders Carlssone136e0e2009-06-26 06:29:23 +0000496NamedDecl *NamedDecl::getUnderlyingDecl() {
497 NamedDecl *ND = this;
498 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000499 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000500 ND = UD->getTargetDecl();
501 else if (ObjCCompatibleAliasDecl *AD
502 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
503 return AD->getClassInterface();
504 else
505 return ND;
506 }
507}
508
John McCall161755a2010-04-06 21:38:20 +0000509bool NamedDecl::isCXXInstanceMember() const {
510 assert(isCXXClassMember() &&
511 "checking whether non-member is instance member");
512
513 const NamedDecl *D = this;
514 if (isa<UsingShadowDecl>(D))
515 D = cast<UsingShadowDecl>(D)->getTargetDecl();
516
517 if (isa<FieldDecl>(D))
518 return true;
519 if (isa<CXXMethodDecl>(D))
520 return cast<CXXMethodDecl>(D)->isInstance();
521 if (isa<FunctionTemplateDecl>(D))
522 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
523 ->getTemplatedDecl())->isInstance();
524 return false;
525}
526
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000527//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000528// DeclaratorDecl Implementation
529//===----------------------------------------------------------------------===//
530
John McCallb6217662010-03-15 10:12:16 +0000531DeclaratorDecl::~DeclaratorDecl() {}
532void DeclaratorDecl::Destroy(ASTContext &C) {
533 if (hasExtInfo())
534 C.Deallocate(getExtInfo());
535 ValueDecl::Destroy(C);
536}
537
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000538SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000539 if (DeclInfo) {
John McCallb6217662010-03-15 10:12:16 +0000540 TypeLoc TL = getTypeSourceInfo()->getTypeLoc();
John McCall51bd8032009-10-18 01:05:36 +0000541 while (true) {
542 TypeLoc NextTL = TL.getNextTypeLoc();
543 if (!NextTL)
544 return TL.getSourceRange().getBegin();
545 TL = NextTL;
546 }
547 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000548 return SourceLocation();
549}
550
John McCallb6217662010-03-15 10:12:16 +0000551void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
552 SourceRange QualifierRange) {
553 if (Qualifier) {
554 // Make sure the extended decl info is allocated.
555 if (!hasExtInfo()) {
556 // Save (non-extended) type source info pointer.
557 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
558 // Allocate external info struct.
559 DeclInfo = new (getASTContext()) ExtInfo;
560 // Restore savedTInfo into (extended) decl info.
561 getExtInfo()->TInfo = savedTInfo;
562 }
563 // Set qualifier info.
564 getExtInfo()->NNS = Qualifier;
565 getExtInfo()->NNSRange = QualifierRange;
566 }
567 else {
568 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
569 assert(QualifierRange.isInvalid());
570 if (hasExtInfo()) {
571 // Save type source info pointer.
572 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
573 // Deallocate the extended decl info.
574 getASTContext().Deallocate(getExtInfo());
575 // Restore savedTInfo into (non-extended) decl info.
576 DeclInfo = savedTInfo;
577 }
578 }
579}
580
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000581//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000582// VarDecl Implementation
583//===----------------------------------------------------------------------===//
584
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000585const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
586 switch (SC) {
587 case VarDecl::None: break;
588 case VarDecl::Auto: return "auto"; break;
589 case VarDecl::Extern: return "extern"; break;
590 case VarDecl::PrivateExtern: return "__private_extern__"; break;
591 case VarDecl::Register: return "register"; break;
592 case VarDecl::Static: return "static"; break;
593 }
594
595 assert(0 && "Invalid storage class");
596 return 0;
597}
598
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000599VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000600 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000601 StorageClass S, StorageClass SCAsWritten) {
602 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000603}
604
605void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000606 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000607 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000608 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000609 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
610 Eval->~EvaluatedStmt();
611 C.Deallocate(Eval);
612 }
613 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000614 this->~VarDecl();
John McCallb6217662010-03-15 10:12:16 +0000615 DeclaratorDecl::Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000616}
617
618VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000619}
620
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000621SourceRange VarDecl::getSourceRange() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000622 SourceLocation Start = getTypeSpecStartLoc();
623 if (Start.isInvalid())
624 Start = getLocation();
625
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000626 if (getInit())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000627 return SourceRange(Start, getInit()->getLocEnd());
628 return SourceRange(Start, getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000629}
630
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000631bool VarDecl::isExternC() const {
632 ASTContext &Context = getASTContext();
633 if (!Context.getLangOptions().CPlusPlus)
634 return (getDeclContext()->isTranslationUnit() &&
635 getStorageClass() != Static) ||
636 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
637
638 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
639 DC = DC->getParent()) {
640 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
641 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
642 return getStorageClass() != Static;
643
644 break;
645 }
646
647 if (DC->isFunctionOrMethod())
648 return false;
649 }
650
651 return false;
652}
653
654VarDecl *VarDecl::getCanonicalDecl() {
655 return getFirstDeclaration();
656}
657
Sebastian Redle9d12b62010-01-31 22:27:38 +0000658VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
659 // C++ [basic.def]p2:
660 // A declaration is a definition unless [...] it contains the 'extern'
661 // specifier or a linkage-specification and neither an initializer [...],
662 // it declares a static data member in a class declaration [...].
663 // C++ [temp.expl.spec]p15:
664 // An explicit specialization of a static data member of a template is a
665 // definition if the declaration includes an initializer; otherwise, it is
666 // a declaration.
667 if (isStaticDataMember()) {
668 if (isOutOfLine() && (hasInit() ||
669 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
670 return Definition;
671 else
672 return DeclarationOnly;
673 }
674 // C99 6.7p5:
675 // A definition of an identifier is a declaration for that identifier that
676 // [...] causes storage to be reserved for that object.
677 // Note: that applies for all non-file-scope objects.
678 // C99 6.9.2p1:
679 // If the declaration of an identifier for an object has file scope and an
680 // initializer, the declaration is an external definition for the identifier
681 if (hasInit())
682 return Definition;
683 // AST for 'extern "C" int foo;' is annotated with 'extern'.
684 if (hasExternalStorage())
685 return DeclarationOnly;
686
687 // C99 6.9.2p2:
688 // A declaration of an object that has file scope without an initializer,
689 // and without a storage class specifier or the scs 'static', constitutes
690 // a tentative definition.
691 // No such thing in C++.
692 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
693 return TentativeDefinition;
694
695 // What's left is (in C, block-scope) declarations without initializers or
696 // external storage. These are definitions.
697 return Definition;
698}
699
Sebastian Redle9d12b62010-01-31 22:27:38 +0000700VarDecl *VarDecl::getActingDefinition() {
701 DefinitionKind Kind = isThisDeclarationADefinition();
702 if (Kind != TentativeDefinition)
703 return 0;
704
705 VarDecl *LastTentative = false;
706 VarDecl *First = getFirstDeclaration();
707 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
708 I != E; ++I) {
709 Kind = (*I)->isThisDeclarationADefinition();
710 if (Kind == Definition)
711 return 0;
712 else if (Kind == TentativeDefinition)
713 LastTentative = *I;
714 }
715 return LastTentative;
716}
717
718bool VarDecl::isTentativeDefinitionNow() const {
719 DefinitionKind Kind = isThisDeclarationADefinition();
720 if (Kind != TentativeDefinition)
721 return false;
722
723 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
724 if ((*I)->isThisDeclarationADefinition() == Definition)
725 return false;
726 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000727 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000728}
729
Sebastian Redl31310a22010-02-01 20:16:42 +0000730VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000731 VarDecl *First = getFirstDeclaration();
732 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
733 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000734 if ((*I)->isThisDeclarationADefinition() == Definition)
735 return *I;
736 }
737 return 0;
738}
739
740const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000741 redecl_iterator I = redecls_begin(), E = redecls_end();
742 while (I != E && !I->getInit())
743 ++I;
744
745 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000746 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000747 return I->getInit();
748 }
749 return 0;
750}
751
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000752bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000753 if (Decl::isOutOfLine())
754 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000755
756 if (!isStaticDataMember())
757 return false;
758
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000759 // If this static data member was instantiated from a static data member of
760 // a class template, check whether that static data member was defined
761 // out-of-line.
762 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
763 return VD->isOutOfLine();
764
765 return false;
766}
767
Douglas Gregor0d035142009-10-27 18:42:08 +0000768VarDecl *VarDecl::getOutOfLineDefinition() {
769 if (!isStaticDataMember())
770 return 0;
771
772 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
773 RD != RDEnd; ++RD) {
774 if (RD->getLexicalDeclContext()->isFileContext())
775 return *RD;
776 }
777
778 return 0;
779}
780
Douglas Gregor838db382010-02-11 01:19:42 +0000781void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000782 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
783 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000784 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000785 }
786
787 Init = I;
788}
789
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000790VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000791 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000792 return cast<VarDecl>(MSI->getInstantiatedFrom());
793
794 return 0;
795}
796
Douglas Gregor663b5a02009-10-14 20:14:33 +0000797TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +0000798 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000799 return MSI->getTemplateSpecializationKind();
800
801 return TSK_Undeclared;
802}
803
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000804MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000805 return getASTContext().getInstantiatedFromStaticDataMember(this);
806}
807
Douglas Gregor0a897e32009-10-15 17:21:20 +0000808void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
809 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000810 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000811 assert(MSI && "Not an instantiated static data member?");
812 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000813 if (TSK != TSK_ExplicitSpecialization &&
814 PointOfInstantiation.isValid() &&
815 MSI->getPointOfInstantiation().isInvalid())
816 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000817}
818
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000819//===----------------------------------------------------------------------===//
820// ParmVarDecl Implementation
821//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +0000822
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000823ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
824 SourceLocation L, IdentifierInfo *Id,
825 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000826 StorageClass S, StorageClass SCAsWritten,
827 Expr *DefArg) {
828 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
829 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +0000830}
831
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000832Expr *ParmVarDecl::getDefaultArg() {
833 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
834 assert(!hasUninstantiatedDefaultArg() &&
835 "Default argument is not yet instantiated!");
836
837 Expr *Arg = getInit();
838 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
839 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +0000840
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000841 return Arg;
842}
843
844unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
845 if (const CXXExprWithTemporaries *E =
846 dyn_cast<CXXExprWithTemporaries>(getInit()))
847 return E->getNumTemporaries();
848
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000849 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000850}
851
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000852CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
853 assert(getNumDefaultArgTemporaries() &&
854 "Default arguments does not have any temporaries!");
855
856 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
857 return E->getTemporary(i);
858}
859
860SourceRange ParmVarDecl::getDefaultArgRange() const {
861 if (const Expr *E = getInit())
862 return E->getSourceRange();
863
864 if (hasUninstantiatedDefaultArg())
865 return getUninstantiatedDefaultArg()->getSourceRange();
866
867 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000868}
869
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000870//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000871// FunctionDecl Implementation
872//===----------------------------------------------------------------------===//
873
Ted Kremenek27f8a282008-05-20 00:43:19 +0000874void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000875 if (Body && Body.isOffset())
876 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000877
878 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
879 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000880
Douglas Gregor2db32322009-10-07 23:56:10 +0000881 FunctionTemplateSpecializationInfo *FTSInfo
882 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
883 if (FTSInfo)
884 C.Deallocate(FTSInfo);
885
886 MemberSpecializationInfo *MSInfo
887 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
888 if (MSInfo)
889 C.Deallocate(MSInfo);
890
Steve Naroff3e970492009-01-27 21:25:57 +0000891 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000892
John McCallb6217662010-03-15 10:12:16 +0000893 DeclaratorDecl::Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +0000894}
895
John McCall136a6982009-09-11 06:45:03 +0000896void FunctionDecl::getNameForDiagnostic(std::string &S,
897 const PrintingPolicy &Policy,
898 bool Qualified) const {
899 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
900 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
901 if (TemplateArgs)
902 S += TemplateSpecializationType::PrintTemplateArgumentList(
903 TemplateArgs->getFlatArgumentList(),
904 TemplateArgs->flat_size(),
905 Policy);
906
907}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000908
Ted Kremenek9498d382010-04-29 16:49:01 +0000909bool FunctionDecl::isVariadic() const {
910 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
911 return FT->isVariadic();
912 return false;
913}
914
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000915Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000916 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
917 if (I->Body) {
918 Definition = *I;
919 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000920 }
921 }
922
923 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000924}
925
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000926void FunctionDecl::setBody(Stmt *B) {
927 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000928 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000929 EndRangeLoc = B->getLocEnd();
930}
931
Douglas Gregor48a83b52009-09-12 00:17:51 +0000932bool FunctionDecl::isMain() const {
933 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000934 return !Context.getLangOptions().Freestanding &&
935 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000936 getIdentifier() && getIdentifier()->isStr("main");
937}
938
Douglas Gregor48a83b52009-09-12 00:17:51 +0000939bool FunctionDecl::isExternC() const {
940 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000941 // In C, any non-static, non-overloadable function has external
942 // linkage.
943 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000944 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000945
Mike Stump1eb44332009-09-09 15:08:12 +0000946 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000947 DC = DC->getParent()) {
948 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
949 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000950 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000951 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000952
953 break;
954 }
955 }
956
957 return false;
958}
959
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000960bool FunctionDecl::isGlobal() const {
961 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
962 return Method->isStatic();
963
964 if (getStorageClass() == Static)
965 return false;
966
Mike Stump1eb44332009-09-09 15:08:12 +0000967 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000968 DC->isNamespace();
969 DC = DC->getParent()) {
970 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
971 if (!Namespace->getDeclName())
972 return false;
973 break;
974 }
975 }
976
977 return true;
978}
979
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000980void
981FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
982 redeclarable_base::setPreviousDeclaration(PrevDecl);
983
984 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
985 FunctionTemplateDecl *PrevFunTmpl
986 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
987 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
988 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
989 }
990}
991
992const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
993 return getFirstDeclaration();
994}
995
996FunctionDecl *FunctionDecl::getCanonicalDecl() {
997 return getFirstDeclaration();
998}
999
Douglas Gregor3e41d602009-02-13 23:20:09 +00001000/// \brief Returns a value indicating whether this function
1001/// corresponds to a builtin function.
1002///
1003/// The function corresponds to a built-in function if it is
1004/// declared at translation scope or within an extern "C" block and
1005/// its name matches with the name of a builtin. The returned value
1006/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001007/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001008/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001009unsigned FunctionDecl::getBuiltinID() const {
1010 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001011 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1012 return 0;
1013
1014 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1015 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1016 return BuiltinID;
1017
1018 // This function has the name of a known C library
1019 // function. Determine whether it actually refers to the C library
1020 // function or whether it just has the same name.
1021
Douglas Gregor9add3172009-02-17 03:23:10 +00001022 // If this is a static function, it's not a builtin.
1023 if (getStorageClass() == Static)
1024 return 0;
1025
Douglas Gregor3c385e52009-02-14 18:57:46 +00001026 // If this function is at translation-unit scope and we're not in
1027 // C++, it refers to the C library function.
1028 if (!Context.getLangOptions().CPlusPlus &&
1029 getDeclContext()->isTranslationUnit())
1030 return BuiltinID;
1031
1032 // If the function is in an extern "C" linkage specification and is
1033 // not marked "overloadable", it's the real function.
1034 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001035 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001036 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001037 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001038 return BuiltinID;
1039
1040 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001041 return 0;
1042}
1043
1044
Chris Lattner1ad9b282009-04-25 06:03:53 +00001045/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001046/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001047/// after it has been created.
1048unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001049 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001050 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001051 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001052 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Reid Spencer5f016e22007-07-11 17:01:13 +00001054}
1055
Douglas Gregor838db382010-02-11 01:19:42 +00001056void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001058 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 // Zero params -> null pointer.
1061 if (NumParams) {
Douglas Gregor838db382010-02-11 01:19:42 +00001062 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001063 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001065
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001066 // Update source range. The check below allows us to set EndRangeLoc before
1067 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001068 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001069 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 }
1071}
1072
Chris Lattner8123a952008-04-10 02:22:51 +00001073/// getMinRequiredArguments - Returns the minimum number of arguments
1074/// needed to call this function. This may be fewer than the number of
1075/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001076/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001077unsigned FunctionDecl::getMinRequiredArguments() const {
1078 unsigned NumRequiredArgs = getNumParams();
1079 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001080 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001081 --NumRequiredArgs;
1082
1083 return NumRequiredArgs;
1084}
1085
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001086bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001087 // FIXME: This is not enough. Consider:
1088 //
1089 // inline void f();
1090 // void f() { }
1091 //
1092 // f is inlined, but does not have inline specified.
1093 // To fix this we should add an 'inline' flag to FunctionDecl.
1094 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001095 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001096
1097 if (isa<CXXMethodDecl>(this)) {
1098 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1099 return true;
1100 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001101
1102 switch (getTemplateSpecializationKind()) {
1103 case TSK_Undeclared:
1104 case TSK_ExplicitSpecialization:
1105 return false;
1106
1107 case TSK_ImplicitInstantiation:
1108 case TSK_ExplicitInstantiationDeclaration:
1109 case TSK_ExplicitInstantiationDefinition:
1110 // Handle below.
1111 break;
1112 }
1113
1114 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1115 Stmt *Pattern = 0;
1116 if (PatternDecl)
1117 Pattern = PatternDecl->getBody(PatternDecl);
1118
1119 if (Pattern && PatternDecl)
1120 return PatternDecl->isInlined();
1121
1122 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001123}
1124
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001125/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001126/// definition will be externally visible.
1127///
1128/// Inline function definitions are always available for inlining optimizations.
1129/// However, depending on the language dialect, declaration specifiers, and
1130/// attributes, the definition of an inline function may or may not be
1131/// "externally" visible to other translation units in the program.
1132///
1133/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001134/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001135/// inline definition becomes externally visible (C99 6.7.4p6).
1136///
1137/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1138/// definition, we use the GNU semantics for inline, which are nearly the
1139/// opposite of C99 semantics. In particular, "inline" by itself will create
1140/// an externally visible symbol, but "extern inline" will not create an
1141/// externally visible symbol.
1142bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1143 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001144 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001145 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001146
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001147 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001148 // GNU inline semantics. Based on a number of examples, we came up with the
1149 // following heuristic: if the "inline" keyword is present on a
1150 // declaration of the function but "extern" is not present on that
1151 // declaration, then the symbol is externally visible. Otherwise, the GNU
1152 // "extern inline" semantics applies and the symbol is not externally
1153 // visible.
1154 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1155 Redecl != RedeclEnd;
1156 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001157 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001158 return true;
1159 }
1160
1161 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001162 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001163 }
1164
1165 // C99 6.7.4p6:
1166 // [...] If all of the file scope declarations for a function in a
1167 // translation unit include the inline function specifier without extern,
1168 // then the definition in that translation unit is an inline definition.
1169 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1170 Redecl != RedeclEnd;
1171 ++Redecl) {
1172 // Only consider file-scope declarations in this test.
1173 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1174 continue;
1175
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001176 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001177 return true; // Not an inline definition
1178 }
1179
1180 // C99 6.7.4p6:
1181 // An inline definition does not provide an external definition for the
1182 // function, and does not forbid an external definition in another
1183 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001184 return false;
1185}
1186
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001187/// getOverloadedOperator - Which C++ overloaded operator this
1188/// function represents, if any.
1189OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001190 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1191 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001192 else
1193 return OO_None;
1194}
1195
Sean Hunta6c058d2010-01-13 09:01:02 +00001196/// getLiteralIdentifier - The literal suffix identifier this function
1197/// represents, if any.
1198const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1199 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1200 return getDeclName().getCXXLiteralIdentifier();
1201 else
1202 return 0;
1203}
1204
Douglas Gregor2db32322009-10-07 23:56:10 +00001205FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001206 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001207 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1208
1209 return 0;
1210}
1211
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001212MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1213 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1214}
1215
Douglas Gregor2db32322009-10-07 23:56:10 +00001216void
1217FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1218 TemplateSpecializationKind TSK) {
1219 assert(TemplateOrSpecialization.isNull() &&
1220 "Member function is already a specialization");
1221 MemberSpecializationInfo *Info
1222 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1223 TemplateOrSpecialization = Info;
1224}
1225
Douglas Gregor3b846b62009-10-27 20:53:28 +00001226bool FunctionDecl::isImplicitlyInstantiable() const {
1227 // If this function already has a definition or is invalid, it can't be
1228 // implicitly instantiated.
1229 if (isInvalidDecl() || getBody())
1230 return false;
1231
1232 switch (getTemplateSpecializationKind()) {
1233 case TSK_Undeclared:
1234 case TSK_ExplicitSpecialization:
1235 case TSK_ExplicitInstantiationDefinition:
1236 return false;
1237
1238 case TSK_ImplicitInstantiation:
1239 return true;
1240
1241 case TSK_ExplicitInstantiationDeclaration:
1242 // Handled below.
1243 break;
1244 }
1245
1246 // Find the actual template from which we will instantiate.
1247 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1248 Stmt *Pattern = 0;
1249 if (PatternDecl)
1250 Pattern = PatternDecl->getBody(PatternDecl);
1251
1252 // C++0x [temp.explicit]p9:
1253 // Except for inline functions, other explicit instantiation declarations
1254 // have the effect of suppressing the implicit instantiation of the entity
1255 // to which they refer.
1256 if (!Pattern || !PatternDecl)
1257 return true;
1258
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001259 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001260}
1261
1262FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1263 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1264 while (Primary->getInstantiatedFromMemberTemplate()) {
1265 // If we have hit a point where the user provided a specialization of
1266 // this template, we're done looking.
1267 if (Primary->isMemberSpecialization())
1268 break;
1269
1270 Primary = Primary->getInstantiatedFromMemberTemplate();
1271 }
1272
1273 return Primary->getTemplatedDecl();
1274 }
1275
1276 return getInstantiatedFromMemberFunction();
1277}
1278
Douglas Gregor16e8be22009-06-29 17:30:29 +00001279FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001280 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001281 = TemplateOrSpecialization
1282 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001283 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001284 }
1285 return 0;
1286}
1287
1288const TemplateArgumentList *
1289FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001290 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001291 = TemplateOrSpecialization
1292 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001293 return Info->TemplateArguments;
1294 }
1295 return 0;
1296}
1297
Mike Stump1eb44332009-09-09 15:08:12 +00001298void
Douglas Gregor838db382010-02-11 01:19:42 +00001299FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001300 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001301 void *InsertPos,
1302 TemplateSpecializationKind TSK) {
1303 assert(TSK != TSK_Undeclared &&
1304 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001305 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001306 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001307 if (!Info)
Douglas Gregor838db382010-02-11 01:19:42 +00001308 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Douglas Gregor127102b2009-06-29 20:59:39 +00001310 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001311 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001312 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001313 Info->TemplateArguments = TemplateArgs;
1314 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Douglas Gregor127102b2009-06-29 20:59:39 +00001316 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001317 // function template specializations.
1318 if (InsertPos)
1319 Template->getSpecializations().InsertNode(Info, InsertPos);
1320 else {
1321 // Try to insert the new node. If there is an existing node, remove it
1322 // first.
1323 FunctionTemplateSpecializationInfo *Existing
1324 = Template->getSpecializations().GetOrInsertNode(Info);
1325 if (Existing) {
1326 Template->getSpecializations().RemoveNode(Existing);
1327 Template->getSpecializations().GetOrInsertNode(Info);
1328 }
1329 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001330}
1331
John McCallaf2094e2010-04-08 09:05:18 +00001332void
1333FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1334 const UnresolvedSetImpl &Templates,
1335 const TemplateArgumentListInfo &TemplateArgs) {
1336 assert(TemplateOrSpecialization.isNull());
1337 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1338 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001339 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001340 void *Buffer = Context.Allocate(Size);
1341 DependentFunctionTemplateSpecializationInfo *Info =
1342 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1343 TemplateArgs);
1344 TemplateOrSpecialization = Info;
1345}
1346
1347DependentFunctionTemplateSpecializationInfo::
1348DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1349 const TemplateArgumentListInfo &TArgs)
1350 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1351
1352 d.NumTemplates = Ts.size();
1353 d.NumArgs = TArgs.size();
1354
1355 FunctionTemplateDecl **TsArray =
1356 const_cast<FunctionTemplateDecl**>(getTemplates());
1357 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1358 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1359
1360 TemplateArgumentLoc *ArgsArray =
1361 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1362 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1363 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1364}
1365
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001366TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001367 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001368 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001369 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001370 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001371 if (FTSInfo)
1372 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Douglas Gregor2db32322009-10-07 23:56:10 +00001374 MemberSpecializationInfo *MSInfo
1375 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1376 if (MSInfo)
1377 return MSInfo->getTemplateSpecializationKind();
1378
1379 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001380}
1381
Mike Stump1eb44332009-09-09 15:08:12 +00001382void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001383FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1384 SourceLocation PointOfInstantiation) {
1385 if (FunctionTemplateSpecializationInfo *FTSInfo
1386 = TemplateOrSpecialization.dyn_cast<
1387 FunctionTemplateSpecializationInfo*>()) {
1388 FTSInfo->setTemplateSpecializationKind(TSK);
1389 if (TSK != TSK_ExplicitSpecialization &&
1390 PointOfInstantiation.isValid() &&
1391 FTSInfo->getPointOfInstantiation().isInvalid())
1392 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1393 } else if (MemberSpecializationInfo *MSInfo
1394 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1395 MSInfo->setTemplateSpecializationKind(TSK);
1396 if (TSK != TSK_ExplicitSpecialization &&
1397 PointOfInstantiation.isValid() &&
1398 MSInfo->getPointOfInstantiation().isInvalid())
1399 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1400 } else
1401 assert(false && "Function cannot have a template specialization kind");
1402}
1403
1404SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001405 if (FunctionTemplateSpecializationInfo *FTSInfo
1406 = TemplateOrSpecialization.dyn_cast<
1407 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001408 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001409 else if (MemberSpecializationInfo *MSInfo
1410 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001411 return MSInfo->getPointOfInstantiation();
1412
1413 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001414}
1415
Douglas Gregor9f185072009-09-11 20:15:17 +00001416bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001417 if (Decl::isOutOfLine())
1418 return true;
1419
1420 // If this function was instantiated from a member function of a
1421 // class template, check whether that member function was defined out-of-line.
1422 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1423 const FunctionDecl *Definition;
1424 if (FD->getBody(Definition))
1425 return Definition->isOutOfLine();
1426 }
1427
1428 // If this function was instantiated from a function template,
1429 // check whether that function template was defined out-of-line.
1430 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1431 const FunctionDecl *Definition;
1432 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1433 return Definition->isOutOfLine();
1434 }
1435
1436 return false;
1437}
1438
Chris Lattner8a934232008-03-31 00:36:02 +00001439//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001440// FieldDecl Implementation
1441//===----------------------------------------------------------------------===//
1442
1443FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1444 IdentifierInfo *Id, QualType T,
1445 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1446 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1447}
1448
1449bool FieldDecl::isAnonymousStructOrUnion() const {
1450 if (!isImplicit() || getDeclName())
1451 return false;
1452
1453 if (const RecordType *Record = getType()->getAs<RecordType>())
1454 return Record->getDecl()->isAnonymousStructOrUnion();
1455
1456 return false;
1457}
1458
1459//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001460// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001461//===----------------------------------------------------------------------===//
1462
John McCallb6217662010-03-15 10:12:16 +00001463void TagDecl::Destroy(ASTContext &C) {
1464 if (hasExtInfo())
1465 C.Deallocate(getExtInfo());
1466 TypeDecl::Destroy(C);
1467}
1468
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001469SourceRange TagDecl::getSourceRange() const {
1470 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001471 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001472}
1473
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001474TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001475 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001476}
1477
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001478void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001479 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1480 TagT->decl.setPointer(this);
1481 TagT->decl.setInt(1);
1482 }
John McCall86ff3082010-02-04 22:26:26 +00001483
1484 if (isa<CXXRecordDecl>(this)) {
1485 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1486 struct CXXRecordDecl::DefinitionData *Data =
1487 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001488 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1489 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001490 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001491}
1492
1493void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001494 assert((!isa<CXXRecordDecl>(this) ||
1495 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1496 "definition completed but not started");
1497
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001498 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001499 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1500 assert(TagT->decl.getPointer() == this &&
1501 "Attempt to redefine a tag definition?");
1502 TagT->decl.setInt(0);
1503 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001504}
1505
Douglas Gregor952b0172010-02-11 01:04:33 +00001506TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001507 if (isDefinition())
1508 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001509
1510 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001511 R != REnd; ++R)
1512 if (R->isDefinition())
1513 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001515 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001516}
1517
John McCallf1bbbb42009-09-04 01:14:41 +00001518TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1519 switch (TypeSpec) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001520 default: llvm_unreachable("unexpected type specifier");
John McCallf1bbbb42009-09-04 01:14:41 +00001521 case DeclSpec::TST_struct: return TK_struct;
1522 case DeclSpec::TST_class: return TK_class;
1523 case DeclSpec::TST_union: return TK_union;
1524 case DeclSpec::TST_enum: return TK_enum;
1525 }
1526}
1527
John McCallb6217662010-03-15 10:12:16 +00001528void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1529 SourceRange QualifierRange) {
1530 if (Qualifier) {
1531 // Make sure the extended qualifier info is allocated.
1532 if (!hasExtInfo())
1533 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1534 // Set qualifier info.
1535 getExtInfo()->NNS = Qualifier;
1536 getExtInfo()->NNSRange = QualifierRange;
1537 }
1538 else {
1539 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1540 assert(QualifierRange.isInvalid());
1541 if (hasExtInfo()) {
1542 getASTContext().Deallocate(getExtInfo());
1543 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1544 }
1545 }
1546}
1547
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001548//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001549// EnumDecl Implementation
1550//===----------------------------------------------------------------------===//
1551
1552EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1553 IdentifierInfo *Id, SourceLocation TKL,
1554 EnumDecl *PrevDecl) {
1555 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1556 C.getTypeDeclType(Enum, PrevDecl);
1557 return Enum;
1558}
1559
1560void EnumDecl::Destroy(ASTContext& C) {
John McCallb6217662010-03-15 10:12:16 +00001561 TagDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001562}
1563
Douglas Gregor838db382010-02-11 01:19:42 +00001564void EnumDecl::completeDefinition(QualType NewType,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001565 QualType NewPromotionType) {
1566 assert(!isDefinition() && "Cannot redefine enums!");
1567 IntegerType = NewType;
1568 PromotionType = NewPromotionType;
1569 TagDecl::completeDefinition();
1570}
1571
1572//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001573// RecordDecl Implementation
1574//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001575
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001576RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001577 IdentifierInfo *Id, RecordDecl *PrevDecl,
1578 SourceLocation TKL)
1579 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001580 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001581 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001582 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001583 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001584}
1585
1586RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001587 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001588 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001590 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001591 C.getTypeDeclType(R, PrevDecl);
1592 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001593}
1594
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001595RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001596}
1597
1598void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001599 TagDecl::Destroy(C);
1600}
1601
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001602bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001603 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001604 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1605}
1606
Douglas Gregor44b43212008-12-11 16:49:14 +00001607/// completeDefinition - Notes that the definition of this type is now
1608/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001609void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001610 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001611 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001612}
1613
Steve Naroff56ee6892008-10-08 17:01:13 +00001614//===----------------------------------------------------------------------===//
1615// BlockDecl Implementation
1616//===----------------------------------------------------------------------===//
1617
1618BlockDecl::~BlockDecl() {
1619}
1620
1621void BlockDecl::Destroy(ASTContext& C) {
1622 if (Body)
1623 Body->Destroy(C);
1624
1625 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1626 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001627
1628 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001629 Decl::Destroy(C);
1630}
Steve Naroffe78b8092009-03-13 16:56:44 +00001631
Douglas Gregor838db382010-02-11 01:19:42 +00001632void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001633 unsigned NParms) {
1634 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Steve Naroffe78b8092009-03-13 16:56:44 +00001636 // Zero params -> null pointer.
1637 if (NParms) {
1638 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001639 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001640 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1641 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1642 }
1643}
1644
1645unsigned BlockDecl::getNumParams() const {
1646 return NumParams;
1647}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001648
1649
1650//===----------------------------------------------------------------------===//
1651// Other Decl Allocation/Deallocation Method Implementations
1652//===----------------------------------------------------------------------===//
1653
1654TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1655 return new (C) TranslationUnitDecl(C);
1656}
1657
1658NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1659 SourceLocation L, IdentifierInfo *Id) {
1660 return new (C) NamespaceDecl(DC, L, Id);
1661}
1662
1663void NamespaceDecl::Destroy(ASTContext& C) {
1664 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1665 // together. They are all top-level Decls.
1666
1667 this->~NamespaceDecl();
John McCallb6217662010-03-15 10:12:16 +00001668 Decl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001669}
1670
1671
1672ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1673 SourceLocation L, IdentifierInfo *Id, QualType T) {
1674 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1675}
1676
1677FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1678 SourceLocation L,
1679 DeclarationName N, QualType T,
1680 TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001681 StorageClass S, StorageClass SCAsWritten,
1682 bool isInline, bool hasWrittenPrototype) {
1683 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1684 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001685 New->HasWrittenPrototype = hasWrittenPrototype;
1686 return New;
1687}
1688
1689BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1690 return new (C) BlockDecl(DC, L);
1691}
1692
1693EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1694 SourceLocation L,
1695 IdentifierInfo *Id, QualType T,
1696 Expr *E, const llvm::APSInt &V) {
1697 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1698}
1699
1700void EnumConstantDecl::Destroy(ASTContext& C) {
1701 if (Init) Init->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001702 ValueDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001703}
1704
1705TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1706 SourceLocation L, IdentifierInfo *Id,
1707 TypeSourceInfo *TInfo) {
1708 return new (C) TypedefDecl(DC, L, Id, TInfo);
1709}
1710
1711// Anchor TypedefDecl's vtable here.
1712TypedefDecl::~TypedefDecl() {}
1713
1714FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1715 SourceLocation L,
1716 StringLiteral *Str) {
1717 return new (C) FileScopeAsmDecl(DC, L, Str);
1718}