blob: c813a47f6b43ec7dbbde9e35c5c4873644462cd9 [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"
Abramo Bagnara6150c882010-05-11 21:36:43 +000026#include "clang/Basic/Specifiers.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000027#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000028
Chris Lattner6d9a6852006-10-25 05:11:20 +000029using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000030
Chris Lattner88f70d62008-03-15 05:43:15 +000031//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000032// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000033//===----------------------------------------------------------------------===//
34
Douglas Gregor7dc5c172010-02-03 09:33:45 +000035/// \brief Get the most restrictive linkage for the types in the given
36/// template parameter list.
37static Linkage
38getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
39 Linkage L = ExternalLinkage;
40 for (TemplateParameterList::const_iterator P = Params->begin(),
41 PEnd = Params->end();
42 P != PEnd; ++P) {
43 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
44 if (!NTTP->getType()->isDependentType()) {
45 L = minLinkage(L, NTTP->getType()->getLinkage());
46 continue;
47 }
48
49 if (TemplateTemplateParmDecl *TTP
50 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
51 L = minLinkage(L,
52 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
53 }
54 }
55
56 return L;
57}
58
59/// \brief Get the most restrictive linkage for the types and
60/// declarations in the given template argument list.
61static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
62 unsigned NumArgs) {
63 Linkage L = ExternalLinkage;
64
65 for (unsigned I = 0; I != NumArgs; ++I) {
66 switch (Args[I].getKind()) {
67 case TemplateArgument::Null:
68 case TemplateArgument::Integral:
69 case TemplateArgument::Expression:
70 break;
71
72 case TemplateArgument::Type:
73 L = minLinkage(L, Args[I].getAsType()->getLinkage());
74 break;
75
76 case TemplateArgument::Declaration:
77 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
78 L = minLinkage(L, ND->getLinkage());
79 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
80 L = minLinkage(L, VD->getType()->getLinkage());
81 break;
82
83 case TemplateArgument::Template:
84 if (TemplateDecl *Template
85 = Args[I].getAsTemplate().getAsTemplateDecl())
86 L = minLinkage(L, Template->getLinkage());
87 break;
88
89 case TemplateArgument::Pack:
90 L = minLinkage(L,
91 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
92 Args[I].pack_size()));
93 break;
94 }
95 }
96
97 return L;
98}
99
100static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000101 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
102 "Not a name having namespace scope");
103 ASTContext &Context = D->getASTContext();
104
105 // C++ [basic.link]p3:
106 // A name having namespace scope (3.3.6) has internal linkage if it
107 // is the name of
108 // - an object, reference, function or function template that is
109 // explicitly declared static; or,
110 // (This bullet corresponds to C99 6.2.2p3.)
111 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
112 // Explicitly declared static.
113 if (Var->getStorageClass() == VarDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000114 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000115
116 // - an object or reference that is explicitly declared const
117 // and neither explicitly declared extern nor previously
118 // declared to have external linkage; or
119 // (there is no equivalent in C99)
120 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000121 Var->getType().isConstant(Context) &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000122 Var->getStorageClass() != VarDecl::Extern &&
123 Var->getStorageClass() != VarDecl::PrivateExtern) {
124 bool FoundExtern = false;
125 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
126 PrevVar && !FoundExtern;
127 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000128 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000129 FoundExtern = true;
130
131 if (!FoundExtern)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000132 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000133 }
134 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 // C++ [temp]p4:
136 // A non-member function template can have internal linkage; any
137 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000138 const FunctionDecl *Function = 0;
139 if (const FunctionTemplateDecl *FunTmpl
140 = dyn_cast<FunctionTemplateDecl>(D))
141 Function = FunTmpl->getTemplatedDecl();
142 else
143 Function = cast<FunctionDecl>(D);
144
145 // Explicitly declared static.
146 if (Function->getStorageClass() == FunctionDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000147 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000148 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
149 // - a data member of an anonymous union.
150 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000151 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000152 }
153
154 // C++ [basic.link]p4:
155
156 // A name having namespace scope has external linkage if it is the
157 // name of
158 //
159 // - an object or reference, unless it has internal linkage; or
160 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
161 if (!Context.getLangOptions().CPlusPlus &&
162 (Var->getStorageClass() == VarDecl::Extern ||
163 Var->getStorageClass() == VarDecl::PrivateExtern)) {
164 // C99 6.2.2p4:
165 // For an identifier declared with the storage-class specifier
166 // extern in a scope in which a prior declaration of that
167 // identifier is visible, if the prior declaration specifies
168 // internal or external linkage, the linkage of the identifier
169 // at the later declaration is the same as the linkage
170 // specified at the prior declaration. If no prior declaration
171 // is visible, or if the prior declaration specifies no
172 // linkage, then the identifier has external linkage.
173 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000174 if (Linkage L = PrevVar->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000175 return L;
176 }
177 }
178
179 // C99 6.2.2p5:
180 // If the declaration of an identifier for an object has file
181 // scope and no storage-class specifier, its linkage is
182 // external.
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000183 if (Var->isInAnonymousNamespace())
184 return UniqueExternalLinkage;
185
186 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000187 }
188
189 // - a function, unless it has internal linkage; or
190 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
191 // C99 6.2.2p5:
192 // If the declaration of an identifier for a function has no
193 // storage-class specifier, its linkage is determined exactly
194 // as if it were declared with the storage-class specifier
195 // extern.
196 if (!Context.getLangOptions().CPlusPlus &&
197 (Function->getStorageClass() == FunctionDecl::Extern ||
198 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
199 Function->getStorageClass() == FunctionDecl::None)) {
200 // C99 6.2.2p4:
201 // For an identifier declared with the storage-class specifier
202 // extern in a scope in which a prior declaration of that
203 // identifier is visible, if the prior declaration specifies
204 // internal or external linkage, the linkage of the identifier
205 // at the later declaration is the same as the linkage
206 // specified at the prior declaration. If no prior declaration
207 // is visible, or if the prior declaration specifies no
208 // linkage, then the identifier has external linkage.
209 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000210 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000211 return L;
212 }
213 }
214
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000215 if (Function->isInAnonymousNamespace())
216 return UniqueExternalLinkage;
217
218 if (FunctionTemplateSpecializationInfo *SpecInfo
219 = Function->getTemplateSpecializationInfo()) {
220 Linkage L = SpecInfo->getTemplate()->getLinkage();
221 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
222 L = minLinkage(L,
223 getLinkageForTemplateArgumentList(
224 TemplateArgs.getFlatArgumentList(),
225 TemplateArgs.flat_size()));
226 return L;
227 }
228
229 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 }
231
232 // - a named class (Clause 9), or an unnamed class defined in a
233 // typedef declaration in which the class has the typedef name
234 // for linkage purposes (7.1.3); or
235 // - a named enumeration (7.2), or an unnamed enumeration
236 // defined in a typedef declaration in which the enumeration
237 // has the typedef name for linkage purposes (7.1.3); or
238 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000239 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
240 if (Tag->isInAnonymousNamespace())
241 return UniqueExternalLinkage;
242
243 // If this is a class template specialization, consider the
244 // linkage of the template and template arguments.
245 if (const ClassTemplateSpecializationDecl *Spec
246 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
247 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
248 Linkage L = getLinkageForTemplateArgumentList(
249 TemplateArgs.getFlatArgumentList(),
250 TemplateArgs.flat_size());
251 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
252 }
253
254 return ExternalLinkage;
255 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000256
257 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000258 if (isa<EnumConstantDecl>(D)) {
259 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
260 if (isExternalLinkage(L))
261 return L;
262 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000263
264 // - a template, unless it is a function template that has
265 // internal linkage (Clause 14);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000266 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
267 if (D->isInAnonymousNamespace())
268 return UniqueExternalLinkage;
269
270 return getLinkageForTemplateParameterList(
271 Template->getTemplateParameters());
272 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000273
274 // - a namespace (7.3), unless it is declared within an unnamed
275 // namespace.
276 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000277 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000278
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000279 return NoLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000280}
281
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000282Linkage NamedDecl::getLinkage() const {
Ted Kremenek926d8602010-04-20 23:15:35 +0000283
284 // Objective-C: treat all Objective-C declarations as having external
285 // linkage.
286 switch (getKind()) {
287 default:
288 break;
289 case Decl::ObjCAtDefsField:
290 case Decl::ObjCCategory:
291 case Decl::ObjCCategoryImpl:
292 case Decl::ObjCClass:
293 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000294 case Decl::ObjCForwardProtocol:
295 case Decl::ObjCImplementation:
296 case Decl::ObjCInterface:
297 case Decl::ObjCIvar:
298 case Decl::ObjCMethod:
299 case Decl::ObjCProperty:
300 case Decl::ObjCPropertyImpl:
301 case Decl::ObjCProtocol:
302 return ExternalLinkage;
303 }
304
Douglas Gregorf73b2822009-11-25 22:24:25 +0000305 // Handle linkage for namespace-scope names.
306 if (getDeclContext()->getLookupContext()->isFileContext())
307 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
308 return L;
309
310 // C++ [basic.link]p5:
311 // In addition, a member function, static data member, a named
312 // class or enumeration of class scope, or an unnamed class or
313 // enumeration defined in a class-scope typedef declaration such
314 // that the class or enumeration has the typedef name for linkage
315 // purposes (7.1.3), has external linkage if the name of the class
316 // has external linkage.
317 if (getDeclContext()->isRecord() &&
318 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
319 (isa<TagDecl>(this) &&
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000320 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
321 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
322 if (isExternalLinkage(L))
323 return L;
324 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000325
326 // C++ [basic.link]p6:
327 // The name of a function declared in block scope and the name of
328 // an object declared by a block scope extern declaration have
329 // linkage. If there is a visible declaration of an entity with
330 // linkage having the same name and type, ignoring entities
331 // declared outside the innermost enclosing namespace scope, the
332 // block scope declaration declares that same entity and receives
333 // the linkage of the previous declaration. If there is more than
334 // one such matching entity, the program is ill-formed. Otherwise,
335 // if no matching entity is found, the block scope entity receives
336 // external linkage.
337 if (getLexicalDeclContext()->isFunctionOrMethod()) {
338 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
339 if (Function->getPreviousDeclaration())
340 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
341 return L;
342
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000343 if (Function->isInAnonymousNamespace())
344 return UniqueExternalLinkage;
345
Douglas Gregorf73b2822009-11-25 22:24:25 +0000346 return ExternalLinkage;
347 }
348
349 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
350 if (Var->getStorageClass() == VarDecl::Extern ||
351 Var->getStorageClass() == VarDecl::PrivateExtern) {
352 if (Var->getPreviousDeclaration())
353 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
354 return L;
355
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000356 if (Var->isInAnonymousNamespace())
357 return UniqueExternalLinkage;
358
Douglas Gregorf73b2822009-11-25 22:24:25 +0000359 return ExternalLinkage;
360 }
361 }
362
363 // C++ [basic.link]p6:
364 // Names not covered by these rules have no linkage.
365 return NoLinkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000366 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000367
Douglas Gregor2ada0482009-02-04 17:27:36 +0000368std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000369 return getQualifiedNameAsString(getASTContext().getLangOptions());
370}
371
372std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000373 const DeclContext *Ctx = getDeclContext();
374
375 if (Ctx->isFunctionOrMethod())
376 return getNameAsString();
377
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000378 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
379 ContextsTy Contexts;
380
381 // Collect contexts.
382 while (Ctx && isa<NamedDecl>(Ctx)) {
383 Contexts.push_back(Ctx);
384 Ctx = Ctx->getParent();
385 };
386
387 std::string QualName;
388 llvm::raw_string_ostream OS(QualName);
389
390 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
391 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000392 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000393 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000394 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
395 std::string TemplateArgsStr
396 = TemplateSpecializationType::PrintTemplateArgumentList(
397 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000398 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000399 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000400 OS << Spec->getName() << TemplateArgsStr;
401 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000402 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000403 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000404 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000405 OS << ND;
406 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
407 if (!RD->getIdentifier())
408 OS << "<anonymous " << RD->getKindName() << '>';
409 else
410 OS << RD;
411 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000412 const FunctionProtoType *FT = 0;
413 if (FD->hasWrittenPrototype())
414 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
415
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000416 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000417 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000418 unsigned NumParams = FD->getNumParams();
419 for (unsigned i = 0; i < NumParams; ++i) {
420 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000421 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000422 std::string Param;
423 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000424 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000425 }
426
427 if (FT->isVariadic()) {
428 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000429 OS << ", ";
430 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000431 }
432 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000433 OS << ')';
434 } else {
435 OS << cast<NamedDecl>(*I);
436 }
437 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000438 }
439
John McCalla2a3f7d2010-03-16 21:48:18 +0000440 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000441 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000442 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000443 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000444
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000445 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000446}
447
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000448bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000449 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
450
Douglas Gregor889ceb72009-02-03 19:21:40 +0000451 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
452 // We want to keep it, unless it nominates same namespace.
453 if (getKind() == Decl::UsingDirective) {
454 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
455 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
459 // For function declarations, we keep track of redeclarations.
460 return FD->getPreviousDeclaration() == OldD;
461
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000462 // For function templates, the underlying function declarations are linked.
463 if (const FunctionTemplateDecl *FunctionTemplate
464 = dyn_cast<FunctionTemplateDecl>(this))
465 if (const FunctionTemplateDecl *OldFunctionTemplate
466 = dyn_cast<FunctionTemplateDecl>(OldD))
467 return FunctionTemplate->getTemplatedDecl()
468 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000469
Steve Naroffc4173fa2009-02-22 19:35:57 +0000470 // For method declarations, we keep track of redeclarations.
471 if (isa<ObjCMethodDecl>(this))
472 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000473
John McCall9f3059a2009-10-09 21:13:30 +0000474 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
475 return true;
476
John McCall3f746822009-11-17 05:59:44 +0000477 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
478 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
479 cast<UsingShadowDecl>(OldD)->getTargetDecl();
480
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000481 // For non-function declarations, if the declarations are of the
482 // same kind then this must be a redeclaration, or semantic analysis
483 // would not have given us the new declaration.
484 return this->getKind() == OldD->getKind();
485}
486
Douglas Gregoreddf4332009-02-24 20:03:32 +0000487bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000488 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000489}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000490
Anders Carlsson6915bf62009-06-26 06:29:23 +0000491NamedDecl *NamedDecl::getUnderlyingDecl() {
492 NamedDecl *ND = this;
493 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000494 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000495 ND = UD->getTargetDecl();
496 else if (ObjCCompatibleAliasDecl *AD
497 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
498 return AD->getClassInterface();
499 else
500 return ND;
501 }
502}
503
John McCalla8ae2222010-04-06 21:38:20 +0000504bool NamedDecl::isCXXInstanceMember() const {
505 assert(isCXXClassMember() &&
506 "checking whether non-member is instance member");
507
508 const NamedDecl *D = this;
509 if (isa<UsingShadowDecl>(D))
510 D = cast<UsingShadowDecl>(D)->getTargetDecl();
511
512 if (isa<FieldDecl>(D))
513 return true;
514 if (isa<CXXMethodDecl>(D))
515 return cast<CXXMethodDecl>(D)->isInstance();
516 if (isa<FunctionTemplateDecl>(D))
517 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
518 ->getTemplatedDecl())->isInstance();
519 return false;
520}
521
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000522//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000523// DeclaratorDecl Implementation
524//===----------------------------------------------------------------------===//
525
John McCall3e11ebe2010-03-15 10:12:16 +0000526DeclaratorDecl::~DeclaratorDecl() {}
527void DeclaratorDecl::Destroy(ASTContext &C) {
528 if (hasExtInfo())
529 C.Deallocate(getExtInfo());
530 ValueDecl::Destroy(C);
531}
532
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000533SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000534 if (DeclInfo) {
John McCall3e11ebe2010-03-15 10:12:16 +0000535 TypeLoc TL = getTypeSourceInfo()->getTypeLoc();
John McCall17001972009-10-18 01:05:36 +0000536 while (true) {
537 TypeLoc NextTL = TL.getNextTypeLoc();
538 if (!NextTL)
539 return TL.getSourceRange().getBegin();
540 TL = NextTL;
541 }
542 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000543 return SourceLocation();
544}
545
John McCall3e11ebe2010-03-15 10:12:16 +0000546void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
547 SourceRange QualifierRange) {
548 if (Qualifier) {
549 // Make sure the extended decl info is allocated.
550 if (!hasExtInfo()) {
551 // Save (non-extended) type source info pointer.
552 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
553 // Allocate external info struct.
554 DeclInfo = new (getASTContext()) ExtInfo;
555 // Restore savedTInfo into (extended) decl info.
556 getExtInfo()->TInfo = savedTInfo;
557 }
558 // Set qualifier info.
559 getExtInfo()->NNS = Qualifier;
560 getExtInfo()->NNSRange = QualifierRange;
561 }
562 else {
563 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
564 assert(QualifierRange.isInvalid());
565 if (hasExtInfo()) {
566 // Save type source info pointer.
567 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
568 // Deallocate the extended decl info.
569 getASTContext().Deallocate(getExtInfo());
570 // Restore savedTInfo into (non-extended) decl info.
571 DeclInfo = savedTInfo;
572 }
573 }
574}
575
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000576//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000577// VarDecl Implementation
578//===----------------------------------------------------------------------===//
579
Sebastian Redl833ef452010-01-26 22:01:41 +0000580const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
581 switch (SC) {
582 case VarDecl::None: break;
583 case VarDecl::Auto: return "auto"; break;
584 case VarDecl::Extern: return "extern"; break;
585 case VarDecl::PrivateExtern: return "__private_extern__"; break;
586 case VarDecl::Register: return "register"; break;
587 case VarDecl::Static: return "static"; break;
588 }
589
590 assert(0 && "Invalid storage class");
591 return 0;
592}
593
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000594VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000595 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000596 StorageClass S, StorageClass SCAsWritten) {
597 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000598}
599
600void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000601 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000602 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000603 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000604 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
605 Eval->~EvaluatedStmt();
606 C.Deallocate(Eval);
607 }
608 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000609 this->~VarDecl();
John McCall3e11ebe2010-03-15 10:12:16 +0000610 DeclaratorDecl::Destroy(C);
Nuno Lopes394ec982008-12-17 23:39:55 +0000611}
612
613VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000614}
615
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000616SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000617 SourceLocation Start = getTypeSpecStartLoc();
618 if (Start.isInvalid())
619 Start = getLocation();
620
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000621 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000622 return SourceRange(Start, getInit()->getLocEnd());
623 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000624}
625
Sebastian Redl833ef452010-01-26 22:01:41 +0000626bool VarDecl::isExternC() const {
627 ASTContext &Context = getASTContext();
628 if (!Context.getLangOptions().CPlusPlus)
629 return (getDeclContext()->isTranslationUnit() &&
630 getStorageClass() != Static) ||
631 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
632
633 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
634 DC = DC->getParent()) {
635 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
636 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
637 return getStorageClass() != Static;
638
639 break;
640 }
641
642 if (DC->isFunctionOrMethod())
643 return false;
644 }
645
646 return false;
647}
648
649VarDecl *VarDecl::getCanonicalDecl() {
650 return getFirstDeclaration();
651}
652
Sebastian Redl35351a92010-01-31 22:27:38 +0000653VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
654 // C++ [basic.def]p2:
655 // A declaration is a definition unless [...] it contains the 'extern'
656 // specifier or a linkage-specification and neither an initializer [...],
657 // it declares a static data member in a class declaration [...].
658 // C++ [temp.expl.spec]p15:
659 // An explicit specialization of a static data member of a template is a
660 // definition if the declaration includes an initializer; otherwise, it is
661 // a declaration.
662 if (isStaticDataMember()) {
663 if (isOutOfLine() && (hasInit() ||
664 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
665 return Definition;
666 else
667 return DeclarationOnly;
668 }
669 // C99 6.7p5:
670 // A definition of an identifier is a declaration for that identifier that
671 // [...] causes storage to be reserved for that object.
672 // Note: that applies for all non-file-scope objects.
673 // C99 6.9.2p1:
674 // If the declaration of an identifier for an object has file scope and an
675 // initializer, the declaration is an external definition for the identifier
676 if (hasInit())
677 return Definition;
678 // AST for 'extern "C" int foo;' is annotated with 'extern'.
679 if (hasExternalStorage())
680 return DeclarationOnly;
681
682 // C99 6.9.2p2:
683 // A declaration of an object that has file scope without an initializer,
684 // and without a storage class specifier or the scs 'static', constitutes
685 // a tentative definition.
686 // No such thing in C++.
687 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
688 return TentativeDefinition;
689
690 // What's left is (in C, block-scope) declarations without initializers or
691 // external storage. These are definitions.
692 return Definition;
693}
694
Sebastian Redl35351a92010-01-31 22:27:38 +0000695VarDecl *VarDecl::getActingDefinition() {
696 DefinitionKind Kind = isThisDeclarationADefinition();
697 if (Kind != TentativeDefinition)
698 return 0;
699
700 VarDecl *LastTentative = false;
701 VarDecl *First = getFirstDeclaration();
702 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
703 I != E; ++I) {
704 Kind = (*I)->isThisDeclarationADefinition();
705 if (Kind == Definition)
706 return 0;
707 else if (Kind == TentativeDefinition)
708 LastTentative = *I;
709 }
710 return LastTentative;
711}
712
713bool VarDecl::isTentativeDefinitionNow() const {
714 DefinitionKind Kind = isThisDeclarationADefinition();
715 if (Kind != TentativeDefinition)
716 return false;
717
718 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
719 if ((*I)->isThisDeclarationADefinition() == Definition)
720 return false;
721 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000722 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000723}
724
Sebastian Redl5ca79842010-02-01 20:16:42 +0000725VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000726 VarDecl *First = getFirstDeclaration();
727 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
728 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000729 if ((*I)->isThisDeclarationADefinition() == Definition)
730 return *I;
731 }
732 return 0;
733}
734
735const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000736 redecl_iterator I = redecls_begin(), E = redecls_end();
737 while (I != E && !I->getInit())
738 ++I;
739
740 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000741 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000742 return I->getInit();
743 }
744 return 0;
745}
746
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000747bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000748 if (Decl::isOutOfLine())
749 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000750
751 if (!isStaticDataMember())
752 return false;
753
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000754 // If this static data member was instantiated from a static data member of
755 // a class template, check whether that static data member was defined
756 // out-of-line.
757 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
758 return VD->isOutOfLine();
759
760 return false;
761}
762
Douglas Gregor1d957a32009-10-27 18:42:08 +0000763VarDecl *VarDecl::getOutOfLineDefinition() {
764 if (!isStaticDataMember())
765 return 0;
766
767 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
768 RD != RDEnd; ++RD) {
769 if (RD->getLexicalDeclContext()->isFileContext())
770 return *RD;
771 }
772
773 return 0;
774}
775
Douglas Gregord5058122010-02-11 01:19:42 +0000776void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000777 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
778 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000779 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000780 }
781
782 Init = I;
783}
784
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000785VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000786 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000787 return cast<VarDecl>(MSI->getInstantiatedFrom());
788
789 return 0;
790}
791
Douglas Gregor3c74d412009-10-14 20:14:33 +0000792TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000793 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000794 return MSI->getTemplateSpecializationKind();
795
796 return TSK_Undeclared;
797}
798
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000799MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000800 return getASTContext().getInstantiatedFromStaticDataMember(this);
801}
802
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000803void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
804 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000805 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000806 assert(MSI && "Not an instantiated static data member?");
807 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000808 if (TSK != TSK_ExplicitSpecialization &&
809 PointOfInstantiation.isValid() &&
810 MSI->getPointOfInstantiation().isInvalid())
811 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000812}
813
Sebastian Redl833ef452010-01-26 22:01:41 +0000814//===----------------------------------------------------------------------===//
815// ParmVarDecl Implementation
816//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000817
Sebastian Redl833ef452010-01-26 22:01:41 +0000818ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
819 SourceLocation L, IdentifierInfo *Id,
820 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000821 StorageClass S, StorageClass SCAsWritten,
822 Expr *DefArg) {
823 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
824 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000825}
826
Sebastian Redl833ef452010-01-26 22:01:41 +0000827Expr *ParmVarDecl::getDefaultArg() {
828 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
829 assert(!hasUninstantiatedDefaultArg() &&
830 "Default argument is not yet instantiated!");
831
832 Expr *Arg = getInit();
833 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
834 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000835
Sebastian Redl833ef452010-01-26 22:01:41 +0000836 return Arg;
837}
838
839unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
840 if (const CXXExprWithTemporaries *E =
841 dyn_cast<CXXExprWithTemporaries>(getInit()))
842 return E->getNumTemporaries();
843
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000844 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000845}
846
Sebastian Redl833ef452010-01-26 22:01:41 +0000847CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
848 assert(getNumDefaultArgTemporaries() &&
849 "Default arguments does not have any temporaries!");
850
851 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
852 return E->getTemporary(i);
853}
854
855SourceRange ParmVarDecl::getDefaultArgRange() const {
856 if (const Expr *E = getInit())
857 return E->getSourceRange();
858
859 if (hasUninstantiatedDefaultArg())
860 return getUninstantiatedDefaultArg()->getSourceRange();
861
862 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000863}
864
Nuno Lopes394ec982008-12-17 23:39:55 +0000865//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000866// FunctionDecl Implementation
867//===----------------------------------------------------------------------===//
868
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000869void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000870 if (Body && Body.isOffset())
871 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000872
873 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
874 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000875
Douglas Gregord801b062009-10-07 23:56:10 +0000876 FunctionTemplateSpecializationInfo *FTSInfo
877 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
878 if (FTSInfo)
879 C.Deallocate(FTSInfo);
880
881 MemberSpecializationInfo *MSInfo
882 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
883 if (MSInfo)
884 C.Deallocate(MSInfo);
885
Steve Naroff13ae6f42009-01-27 21:25:57 +0000886 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000887
John McCall3e11ebe2010-03-15 10:12:16 +0000888 DeclaratorDecl::Destroy(C);
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000889}
890
John McCalle1f2ec22009-09-11 06:45:03 +0000891void FunctionDecl::getNameForDiagnostic(std::string &S,
892 const PrintingPolicy &Policy,
893 bool Qualified) const {
894 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
895 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
896 if (TemplateArgs)
897 S += TemplateSpecializationType::PrintTemplateArgumentList(
898 TemplateArgs->getFlatArgumentList(),
899 TemplateArgs->flat_size(),
900 Policy);
901
902}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000903
Ted Kremenek186a0742010-04-29 16:49:01 +0000904bool FunctionDecl::isVariadic() const {
905 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
906 return FT->isVariadic();
907 return false;
908}
909
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000910Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000911 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
912 if (I->Body) {
913 Definition = *I;
914 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000915 }
916 }
917
918 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000919}
920
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000921void FunctionDecl::setBody(Stmt *B) {
922 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000923 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000924 EndRangeLoc = B->getLocEnd();
925}
926
Douglas Gregor16618f22009-09-12 00:17:51 +0000927bool FunctionDecl::isMain() const {
928 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000929 return !Context.getLangOptions().Freestanding &&
930 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000931 getIdentifier() && getIdentifier()->isStr("main");
932}
933
Douglas Gregor16618f22009-09-12 00:17:51 +0000934bool FunctionDecl::isExternC() const {
935 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000936 // In C, any non-static, non-overloadable function has external
937 // linkage.
938 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000939 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000940
Mike Stump11289f42009-09-09 15:08:12 +0000941 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000942 DC = DC->getParent()) {
943 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
944 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000945 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000946 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000947
948 break;
949 }
950 }
951
952 return false;
953}
954
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000955bool FunctionDecl::isGlobal() const {
956 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
957 return Method->isStatic();
958
959 if (getStorageClass() == Static)
960 return false;
961
Mike Stump11289f42009-09-09 15:08:12 +0000962 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000963 DC->isNamespace();
964 DC = DC->getParent()) {
965 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
966 if (!Namespace->getDeclName())
967 return false;
968 break;
969 }
970 }
971
972 return true;
973}
974
Sebastian Redl833ef452010-01-26 22:01:41 +0000975void
976FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
977 redeclarable_base::setPreviousDeclaration(PrevDecl);
978
979 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
980 FunctionTemplateDecl *PrevFunTmpl
981 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
982 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
983 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
984 }
985}
986
987const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
988 return getFirstDeclaration();
989}
990
991FunctionDecl *FunctionDecl::getCanonicalDecl() {
992 return getFirstDeclaration();
993}
994
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000995/// \brief Returns a value indicating whether this function
996/// corresponds to a builtin function.
997///
998/// The function corresponds to a built-in function if it is
999/// declared at translation scope or within an extern "C" block and
1000/// its name matches with the name of a builtin. The returned value
1001/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001002/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001003/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001004unsigned FunctionDecl::getBuiltinID() const {
1005 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001006 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1007 return 0;
1008
1009 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1010 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1011 return BuiltinID;
1012
1013 // This function has the name of a known C library
1014 // function. Determine whether it actually refers to the C library
1015 // function or whether it just has the same name.
1016
Douglas Gregora908e7f2009-02-17 03:23:10 +00001017 // If this is a static function, it's not a builtin.
1018 if (getStorageClass() == Static)
1019 return 0;
1020
Douglas Gregore711f702009-02-14 18:57:46 +00001021 // If this function is at translation-unit scope and we're not in
1022 // C++, it refers to the C library function.
1023 if (!Context.getLangOptions().CPlusPlus &&
1024 getDeclContext()->isTranslationUnit())
1025 return BuiltinID;
1026
1027 // If the function is in an extern "C" linkage specification and is
1028 // not marked "overloadable", it's the real function.
1029 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001030 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001031 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001032 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001033 return BuiltinID;
1034
1035 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001036 return 0;
1037}
1038
1039
Chris Lattner47c0d002009-04-25 06:03:53 +00001040/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001041/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001042/// after it has been created.
1043unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001044 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001045 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001046 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001047 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001049}
1050
Douglas Gregord5058122010-02-11 01:19:42 +00001051void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001052 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001053 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001054
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001055 // Zero params -> null pointer.
1056 if (NumParams) {
Douglas Gregord5058122010-02-11 01:19:42 +00001057 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001058 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001059 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001060
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001061 // Update source range. The check below allows us to set EndRangeLoc before
1062 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001063 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001064 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001065 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001066}
Chris Lattner41943152007-01-25 04:52:46 +00001067
Chris Lattner58258242008-04-10 02:22:51 +00001068/// getMinRequiredArguments - Returns the minimum number of arguments
1069/// needed to call this function. This may be fewer than the number of
1070/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001071/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001072unsigned FunctionDecl::getMinRequiredArguments() const {
1073 unsigned NumRequiredArgs = getNumParams();
1074 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001075 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001076 --NumRequiredArgs;
1077
1078 return NumRequiredArgs;
1079}
1080
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001081bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001082 // FIXME: This is not enough. Consider:
1083 //
1084 // inline void f();
1085 // void f() { }
1086 //
1087 // f is inlined, but does not have inline specified.
1088 // To fix this we should add an 'inline' flag to FunctionDecl.
1089 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001090 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001091
1092 if (isa<CXXMethodDecl>(this)) {
1093 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1094 return true;
1095 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001096
1097 switch (getTemplateSpecializationKind()) {
1098 case TSK_Undeclared:
1099 case TSK_ExplicitSpecialization:
1100 return false;
1101
1102 case TSK_ImplicitInstantiation:
1103 case TSK_ExplicitInstantiationDeclaration:
1104 case TSK_ExplicitInstantiationDefinition:
1105 // Handle below.
1106 break;
1107 }
1108
1109 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1110 Stmt *Pattern = 0;
1111 if (PatternDecl)
1112 Pattern = PatternDecl->getBody(PatternDecl);
1113
1114 if (Pattern && PatternDecl)
1115 return PatternDecl->isInlined();
1116
1117 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001118}
1119
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001120/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001121/// definition will be externally visible.
1122///
1123/// Inline function definitions are always available for inlining optimizations.
1124/// However, depending on the language dialect, declaration specifiers, and
1125/// attributes, the definition of an inline function may or may not be
1126/// "externally" visible to other translation units in the program.
1127///
1128/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001129/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001130/// inline definition becomes externally visible (C99 6.7.4p6).
1131///
1132/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1133/// definition, we use the GNU semantics for inline, which are nearly the
1134/// opposite of C99 semantics. In particular, "inline" by itself will create
1135/// an externally visible symbol, but "extern inline" will not create an
1136/// externally visible symbol.
1137bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1138 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001139 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001140 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001141
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001142 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001143 // GNU inline semantics. Based on a number of examples, we came up with the
1144 // following heuristic: if the "inline" keyword is present on a
1145 // declaration of the function but "extern" is not present on that
1146 // declaration, then the symbol is externally visible. Otherwise, the GNU
1147 // "extern inline" semantics applies and the symbol is not externally
1148 // visible.
1149 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1150 Redecl != RedeclEnd;
1151 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001152 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001153 return true;
1154 }
1155
1156 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001157 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001158 }
1159
1160 // C99 6.7.4p6:
1161 // [...] If all of the file scope declarations for a function in a
1162 // translation unit include the inline function specifier without extern,
1163 // then the definition in that translation unit is an inline definition.
1164 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1165 Redecl != RedeclEnd;
1166 ++Redecl) {
1167 // Only consider file-scope declarations in this test.
1168 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1169 continue;
1170
Douglas Gregor35b57532009-10-27 21:01:01 +00001171 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001172 return true; // Not an inline definition
1173 }
1174
1175 // C99 6.7.4p6:
1176 // An inline definition does not provide an external definition for the
1177 // function, and does not forbid an external definition in another
1178 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001179 return false;
1180}
1181
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001182/// getOverloadedOperator - Which C++ overloaded operator this
1183/// function represents, if any.
1184OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001185 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1186 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001187 else
1188 return OO_None;
1189}
1190
Alexis Huntc88db062010-01-13 09:01:02 +00001191/// getLiteralIdentifier - The literal suffix identifier this function
1192/// represents, if any.
1193const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1194 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1195 return getDeclName().getCXXLiteralIdentifier();
1196 else
1197 return 0;
1198}
1199
Douglas Gregord801b062009-10-07 23:56:10 +00001200FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001201 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001202 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1203
1204 return 0;
1205}
1206
Douglas Gregor06db9f52009-10-12 20:18:28 +00001207MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1208 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1209}
1210
Douglas Gregord801b062009-10-07 23:56:10 +00001211void
1212FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1213 TemplateSpecializationKind TSK) {
1214 assert(TemplateOrSpecialization.isNull() &&
1215 "Member function is already a specialization");
1216 MemberSpecializationInfo *Info
1217 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1218 TemplateOrSpecialization = Info;
1219}
1220
Douglas Gregorafca3b42009-10-27 20:53:28 +00001221bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001222 // If the function is invalid, it can't be implicitly instantiated.
1223 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001224 return false;
1225
1226 switch (getTemplateSpecializationKind()) {
1227 case TSK_Undeclared:
1228 case TSK_ExplicitSpecialization:
1229 case TSK_ExplicitInstantiationDefinition:
1230 return false;
1231
1232 case TSK_ImplicitInstantiation:
1233 return true;
1234
1235 case TSK_ExplicitInstantiationDeclaration:
1236 // Handled below.
1237 break;
1238 }
1239
1240 // Find the actual template from which we will instantiate.
1241 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1242 Stmt *Pattern = 0;
1243 if (PatternDecl)
1244 Pattern = PatternDecl->getBody(PatternDecl);
1245
1246 // C++0x [temp.explicit]p9:
1247 // Except for inline functions, other explicit instantiation declarations
1248 // have the effect of suppressing the implicit instantiation of the entity
1249 // to which they refer.
1250 if (!Pattern || !PatternDecl)
1251 return true;
1252
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001253 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001254}
1255
1256FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1257 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1258 while (Primary->getInstantiatedFromMemberTemplate()) {
1259 // If we have hit a point where the user provided a specialization of
1260 // this template, we're done looking.
1261 if (Primary->isMemberSpecialization())
1262 break;
1263
1264 Primary = Primary->getInstantiatedFromMemberTemplate();
1265 }
1266
1267 return Primary->getTemplatedDecl();
1268 }
1269
1270 return getInstantiatedFromMemberFunction();
1271}
1272
Douglas Gregor70d83e22009-06-29 17:30:29 +00001273FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001274 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001275 = TemplateOrSpecialization
1276 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001277 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001278 }
1279 return 0;
1280}
1281
1282const TemplateArgumentList *
1283FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001284 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001285 = TemplateOrSpecialization
1286 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001287 return Info->TemplateArguments;
1288 }
1289 return 0;
1290}
1291
Mike Stump11289f42009-09-09 15:08:12 +00001292void
Douglas Gregord5058122010-02-11 01:19:42 +00001293FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001294 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001295 void *InsertPos,
1296 TemplateSpecializationKind TSK) {
1297 assert(TSK != TSK_Undeclared &&
1298 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001299 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001300 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001301 if (!Info)
Douglas Gregord5058122010-02-11 01:19:42 +00001302 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001303
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001304 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001305 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001306 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001307 Info->TemplateArguments = TemplateArgs;
1308 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001309
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001310 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001311 // function template specializations.
1312 if (InsertPos)
1313 Template->getSpecializations().InsertNode(Info, InsertPos);
1314 else {
1315 // Try to insert the new node. If there is an existing node, remove it
1316 // first.
1317 FunctionTemplateSpecializationInfo *Existing
1318 = Template->getSpecializations().GetOrInsertNode(Info);
1319 if (Existing) {
1320 Template->getSpecializations().RemoveNode(Existing);
1321 Template->getSpecializations().GetOrInsertNode(Info);
1322 }
1323 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001324}
1325
John McCallb9c78482010-04-08 09:05:18 +00001326void
1327FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1328 const UnresolvedSetImpl &Templates,
1329 const TemplateArgumentListInfo &TemplateArgs) {
1330 assert(TemplateOrSpecialization.isNull());
1331 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1332 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001333 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001334 void *Buffer = Context.Allocate(Size);
1335 DependentFunctionTemplateSpecializationInfo *Info =
1336 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1337 TemplateArgs);
1338 TemplateOrSpecialization = Info;
1339}
1340
1341DependentFunctionTemplateSpecializationInfo::
1342DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1343 const TemplateArgumentListInfo &TArgs)
1344 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1345
1346 d.NumTemplates = Ts.size();
1347 d.NumArgs = TArgs.size();
1348
1349 FunctionTemplateDecl **TsArray =
1350 const_cast<FunctionTemplateDecl**>(getTemplates());
1351 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1352 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1353
1354 TemplateArgumentLoc *ArgsArray =
1355 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1356 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1357 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1358}
1359
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001360TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001361 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001362 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001363 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001364 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001365 if (FTSInfo)
1366 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001367
Douglas Gregord801b062009-10-07 23:56:10 +00001368 MemberSpecializationInfo *MSInfo
1369 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1370 if (MSInfo)
1371 return MSInfo->getTemplateSpecializationKind();
1372
1373 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001374}
1375
Mike Stump11289f42009-09-09 15:08:12 +00001376void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001377FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1378 SourceLocation PointOfInstantiation) {
1379 if (FunctionTemplateSpecializationInfo *FTSInfo
1380 = TemplateOrSpecialization.dyn_cast<
1381 FunctionTemplateSpecializationInfo*>()) {
1382 FTSInfo->setTemplateSpecializationKind(TSK);
1383 if (TSK != TSK_ExplicitSpecialization &&
1384 PointOfInstantiation.isValid() &&
1385 FTSInfo->getPointOfInstantiation().isInvalid())
1386 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1387 } else if (MemberSpecializationInfo *MSInfo
1388 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1389 MSInfo->setTemplateSpecializationKind(TSK);
1390 if (TSK != TSK_ExplicitSpecialization &&
1391 PointOfInstantiation.isValid() &&
1392 MSInfo->getPointOfInstantiation().isInvalid())
1393 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1394 } else
1395 assert(false && "Function cannot have a template specialization kind");
1396}
1397
1398SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001399 if (FunctionTemplateSpecializationInfo *FTSInfo
1400 = TemplateOrSpecialization.dyn_cast<
1401 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001402 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001403 else if (MemberSpecializationInfo *MSInfo
1404 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001405 return MSInfo->getPointOfInstantiation();
1406
1407 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001408}
1409
Douglas Gregor6411b922009-09-11 20:15:17 +00001410bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001411 if (Decl::isOutOfLine())
1412 return true;
1413
1414 // If this function was instantiated from a member function of a
1415 // class template, check whether that member function was defined out-of-line.
1416 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1417 const FunctionDecl *Definition;
1418 if (FD->getBody(Definition))
1419 return Definition->isOutOfLine();
1420 }
1421
1422 // If this function was instantiated from a function template,
1423 // check whether that function template was defined out-of-line.
1424 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1425 const FunctionDecl *Definition;
1426 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1427 return Definition->isOutOfLine();
1428 }
1429
1430 return false;
1431}
1432
Chris Lattner59a25942008-03-31 00:36:02 +00001433//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001434// FieldDecl Implementation
1435//===----------------------------------------------------------------------===//
1436
1437FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1438 IdentifierInfo *Id, QualType T,
1439 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1440 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1441}
1442
1443bool FieldDecl::isAnonymousStructOrUnion() const {
1444 if (!isImplicit() || getDeclName())
1445 return false;
1446
1447 if (const RecordType *Record = getType()->getAs<RecordType>())
1448 return Record->getDecl()->isAnonymousStructOrUnion();
1449
1450 return false;
1451}
1452
1453//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001454// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001455//===----------------------------------------------------------------------===//
1456
John McCall3e11ebe2010-03-15 10:12:16 +00001457void TagDecl::Destroy(ASTContext &C) {
1458 if (hasExtInfo())
1459 C.Deallocate(getExtInfo());
1460 TypeDecl::Destroy(C);
1461}
1462
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001463SourceRange TagDecl::getSourceRange() const {
1464 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001465 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001466}
1467
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001468TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001469 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001470}
1471
Douglas Gregora72a4e32010-05-19 18:39:18 +00001472void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1473 TypedefDeclOrQualifier = TDD;
1474 if (TypeForDecl)
1475 TypeForDecl->ClearLinkageCache();
1476}
1477
Douglas Gregordee1be82009-01-17 00:42:38 +00001478void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-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);
Douglas Gregor1e13c5a2010-04-30 04:39:27 +00001482 } else if (InjectedClassNameType *Injected
1483 = const_cast<InjectedClassNameType *>(
1484 TypeForDecl->getAs<InjectedClassNameType>())) {
1485 Injected->Decl = cast<CXXRecordDecl>(this);
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001486 }
John McCall67da35c2010-02-04 22:26:26 +00001487
1488 if (isa<CXXRecordDecl>(this)) {
1489 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1490 struct CXXRecordDecl::DefinitionData *Data =
1491 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001492 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1493 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001494 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001495}
1496
1497void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001498 assert((!isa<CXXRecordDecl>(this) ||
1499 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1500 "definition completed but not started");
1501
Douglas Gregordee1be82009-01-17 00:42:38 +00001502 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001503 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1504 assert(TagT->decl.getPointer() == this &&
1505 "Attempt to redefine a tag definition?");
1506 TagT->decl.setInt(0);
Douglas Gregor1e13c5a2010-04-30 04:39:27 +00001507 } else if (InjectedClassNameType *Injected
1508 = const_cast<InjectedClassNameType *>(
1509 TypeForDecl->getAs<InjectedClassNameType>())) {
1510 assert(Injected->Decl == this &&
1511 "Attempt to redefine a class template definition?");
Chandler Carruth2998b542010-05-06 05:28:42 +00001512 (void)Injected;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001513 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001514}
1515
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001516TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001517 if (isDefinition())
1518 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001519
1520 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001521 R != REnd; ++R)
1522 if (R->isDefinition())
1523 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001524
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001525 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001526}
1527
John McCall3e11ebe2010-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 Kremenek21475702008-09-05 17:16:31 +00001548//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-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 McCall3e11ebe2010-03-15 10:12:16 +00001561 TagDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001562}
1563
Douglas Gregord5058122010-02-11 01:19:42 +00001564void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001565 QualType NewPromotionType,
1566 unsigned NumPositiveBits,
1567 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001568 assert(!isDefinition() && "Cannot redefine enums!");
1569 IntegerType = NewType;
1570 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001571 setNumPositiveBits(NumPositiveBits);
1572 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001573 TagDecl::completeDefinition();
1574}
1575
1576//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001577// RecordDecl Implementation
1578//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001579
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001580RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001581 IdentifierInfo *Id, RecordDecl *PrevDecl,
1582 SourceLocation TKL)
1583 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001584 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001585 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001586 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001587 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001588}
1589
1590RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001591 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001592 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001593
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001594 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001595 C.getTypeDeclType(R, PrevDecl);
1596 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001597}
1598
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001599RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001600}
1601
1602void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001603 TagDecl::Destroy(C);
1604}
1605
Douglas Gregordfcad112009-03-25 15:59:44 +00001606bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001607 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001608 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1609}
1610
Douglas Gregor91f84212008-12-11 16:49:14 +00001611/// completeDefinition - Notes that the definition of this type is now
1612/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001613void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001614 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001615 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001616}
Steve Naroffcc321422007-03-26 23:09:51 +00001617
Steve Naroff415d3d52008-10-08 17:01:13 +00001618//===----------------------------------------------------------------------===//
1619// BlockDecl Implementation
1620//===----------------------------------------------------------------------===//
1621
1622BlockDecl::~BlockDecl() {
1623}
1624
1625void BlockDecl::Destroy(ASTContext& C) {
1626 if (Body)
1627 Body->Destroy(C);
1628
1629 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1630 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001631
1632 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001633 Decl::Destroy(C);
1634}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001635
Douglas Gregord5058122010-02-11 01:19:42 +00001636void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001637 unsigned NParms) {
1638 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001639
Steve Naroffc4b30e52009-03-13 16:56:44 +00001640 // Zero params -> null pointer.
1641 if (NParms) {
1642 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001643 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001644 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1645 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1646 }
1647}
1648
1649unsigned BlockDecl::getNumParams() const {
1650 return NumParams;
1651}
Sebastian Redl833ef452010-01-26 22:01:41 +00001652
1653
1654//===----------------------------------------------------------------------===//
1655// Other Decl Allocation/Deallocation Method Implementations
1656//===----------------------------------------------------------------------===//
1657
1658TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1659 return new (C) TranslationUnitDecl(C);
1660}
1661
1662NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1663 SourceLocation L, IdentifierInfo *Id) {
1664 return new (C) NamespaceDecl(DC, L, Id);
1665}
1666
1667void NamespaceDecl::Destroy(ASTContext& C) {
1668 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1669 // together. They are all top-level Decls.
1670
1671 this->~NamespaceDecl();
John McCall3e11ebe2010-03-15 10:12:16 +00001672 Decl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001673}
1674
1675
1676ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1677 SourceLocation L, IdentifierInfo *Id, QualType T) {
1678 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1679}
1680
1681FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1682 SourceLocation L,
1683 DeclarationName N, QualType T,
1684 TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001685 StorageClass S, StorageClass SCAsWritten,
1686 bool isInline, bool hasWrittenPrototype) {
1687 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1688 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001689 New->HasWrittenPrototype = hasWrittenPrototype;
1690 return New;
1691}
1692
1693BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1694 return new (C) BlockDecl(DC, L);
1695}
1696
1697EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1698 SourceLocation L,
1699 IdentifierInfo *Id, QualType T,
1700 Expr *E, const llvm::APSInt &V) {
1701 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1702}
1703
1704void EnumConstantDecl::Destroy(ASTContext& C) {
1705 if (Init) Init->Destroy(C);
John McCall3e11ebe2010-03-15 10:12:16 +00001706 ValueDecl::Destroy(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00001707}
1708
1709TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1710 SourceLocation L, IdentifierInfo *Id,
1711 TypeSourceInfo *TInfo) {
1712 return new (C) TypedefDecl(DC, L, Id, TInfo);
1713}
1714
1715// Anchor TypedefDecl's vtable here.
1716TypedefDecl::~TypedefDecl() {}
1717
1718FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1719 SourceLocation L,
1720 StringLiteral *Str) {
1721 return new (C) FileScopeAsmDecl(DC, L, Str);
1722}