blob: cad7f1f88a85b0e747f0dfa4d0dad85e7013628d [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"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000026#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000027#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattnerd3b90652008-03-15 05:43:15 +000031//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000032// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000033//===----------------------------------------------------------------------===//
34
Douglas Gregor0b6bc8b2010-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 Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000114 return InternalLinkage;
Douglas Gregord85b5b92009-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 Friedmane9d65542009-11-26 03:04:01 +0000121 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000128 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000129 FoundExtern = true;
130
131 if (!FoundExtern)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000132 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000133 }
134 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-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 Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000147 return InternalLinkage;
Douglas Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000151 return InternalLinkage;
Douglas Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000174 if (Linkage L = PrevVar->getLinkage())
Douglas Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000183 if (Var->isInAnonymousNamespace())
184 return UniqueExternalLinkage;
185
186 return ExternalLinkage;
Douglas Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000210 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000211 return L;
212 }
213 }
214
Douglas Gregor0b6bc8b2010-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 Gregord85b5b92009-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 Gregor0b6bc8b2010-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 Gregord85b5b92009-11-25 22:24:25 +0000256
257 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor0b6bc8b2010-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 Gregord85b5b92009-11-25 22:24:25 +0000263
264 // - a template, unless it is a function template that has
265 // internal linkage (Clause 14);
Douglas Gregor0b6bc8b2010-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 Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000277 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000278
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000279 return NoLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000280}
281
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000282Linkage NamedDecl::getLinkage() const {
Ted Kremenekbecc3082010-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 Kremenekbecc3082010-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 Gregord85b5b92009-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 Gregor0b6bc8b2010-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 Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000343 if (Function->isInAnonymousNamespace())
344 return UniqueExternalLinkage;
345
Douglas Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000356 if (Var->isInAnonymousNamespace())
357 return UniqueExternalLinkage;
358
Douglas Gregord85b5b92009-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000366 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000367
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000368std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000369 return getQualifiedNameAsString(getASTContext().getLangOptions());
370}
371
372std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000373 const DeclContext *Ctx = getDeclContext();
374
375 if (Ctx->isFunctionOrMethod())
376 return getNameAsString();
377
Benjamin Kramer68eebbb2010-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 Stump1eb44332009-09-09 15:08:12 +0000392 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000393 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000394 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
395 std::string TemplateArgsStr
396 = TemplateSpecializationType::PrintTemplateArgumentList(
397 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000398 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000399 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000400 OS << Spec->getName() << TemplateArgsStr;
401 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000402 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000403 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000404 else
Benjamin Kramer68eebbb2010-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 Weinig3521d012009-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 Kramer68eebbb2010-04-28 14:33:51 +0000416 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000417 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000418 unsigned NumParams = FD->getNumParams();
419 for (unsigned i = 0; i < NumParams; ++i) {
420 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000421 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000422 std::string Param;
423 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000424 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000425 }
426
427 if (FT->isVariadic()) {
428 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000429 OS << ", ";
430 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000431 }
432 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000433 OS << ')';
434 } else {
435 OS << cast<NamedDecl>(*I);
436 }
437 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000438 }
439
John McCall8472af42010-03-16 21:48:18 +0000440 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000441 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000442 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000443 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000444
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000445 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000446}
447
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000448bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000449 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
450
Douglas Gregor2a3009a2009-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 Stump1eb44332009-09-09 15:08:12 +0000457
Douglas Gregor6ed40e32008-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 Gregore53060f2009-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 Stump1eb44332009-09-09 15:08:12 +0000469
Steve Naroff0de21fd2009-02-22 19:35:57 +0000470 // For method declarations, we keep track of redeclarations.
471 if (isa<ObjCMethodDecl>(this))
472 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
John McCallf36e02d2009-10-09 21:13:30 +0000474 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
475 return true;
476
John McCall9488ea12009-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 Gregor6ed40e32008-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 Gregord6f7e9d2009-02-24 20:03:32 +0000487bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000488 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000489}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000490
Anders Carlssone136e0e2009-06-26 06:29:23 +0000491NamedDecl *NamedDecl::getUnderlyingDecl() {
492 NamedDecl *ND = this;
493 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000494 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-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 McCall161755a2010-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 Kyrtzidis52393042008-11-09 23:41:00 +0000522//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000523// DeclaratorDecl Implementation
524//===----------------------------------------------------------------------===//
525
John McCallb6217662010-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 Kyrtzidisa5d82002009-08-21 00:31:54 +0000533SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000534 TypeSourceInfo *TSI = getTypeSourceInfo();
535 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000536 return SourceLocation();
537}
538
John McCallb6217662010-03-15 10:12:16 +0000539void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
540 SourceRange QualifierRange) {
541 if (Qualifier) {
542 // Make sure the extended decl info is allocated.
543 if (!hasExtInfo()) {
544 // Save (non-extended) type source info pointer.
545 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
546 // Allocate external info struct.
547 DeclInfo = new (getASTContext()) ExtInfo;
548 // Restore savedTInfo into (extended) decl info.
549 getExtInfo()->TInfo = savedTInfo;
550 }
551 // Set qualifier info.
552 getExtInfo()->NNS = Qualifier;
553 getExtInfo()->NNSRange = QualifierRange;
554 }
555 else {
556 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
557 assert(QualifierRange.isInvalid());
558 if (hasExtInfo()) {
559 // Save type source info pointer.
560 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
561 // Deallocate the extended decl info.
562 getASTContext().Deallocate(getExtInfo());
563 // Restore savedTInfo into (non-extended) decl info.
564 DeclInfo = savedTInfo;
565 }
566 }
567}
568
Abramo Bagnara9b934882010-06-12 08:15:14 +0000569void
570QualifierInfo::setTemplateParameterListsInfo(unsigned NumTPLists,
571 TemplateParameterList **TPLists) {
572 assert((NumTPLists == 0 || TPLists != 0) &&
573 "Empty array of template parameters with positive size!");
574 assert((NumTPLists == 0 || NNS) &&
575 "Nonempty array of template parameters with no qualifier!");
576
577 // Free previous template parameters (if any).
578 if (NumTemplParamLists > 0) {
579 delete[] TemplParamLists;
580 TemplParamLists = 0;
581 NumTemplParamLists = 0;
582 }
583 // Set info on matched template parameter lists (if any).
584 if (NumTPLists > 0) {
585 TemplParamLists = new TemplateParameterList*[NumTPLists];
586 NumTemplParamLists = NumTPLists;
587 for (unsigned i = NumTPLists; i-- > 0; )
588 TemplParamLists[i] = TPLists[i];
589 }
590}
591
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000592//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000593// VarDecl Implementation
594//===----------------------------------------------------------------------===//
595
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000596const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
597 switch (SC) {
598 case VarDecl::None: break;
599 case VarDecl::Auto: return "auto"; break;
600 case VarDecl::Extern: return "extern"; break;
601 case VarDecl::PrivateExtern: return "__private_extern__"; break;
602 case VarDecl::Register: return "register"; break;
603 case VarDecl::Static: return "static"; break;
604 }
605
606 assert(0 && "Invalid storage class");
607 return 0;
608}
609
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000610VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000611 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000612 StorageClass S, StorageClass SCAsWritten) {
613 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000614}
615
616void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000617 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000618 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000619 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000620 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
621 Eval->~EvaluatedStmt();
622 C.Deallocate(Eval);
623 }
624 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000625 this->~VarDecl();
John McCallb6217662010-03-15 10:12:16 +0000626 DeclaratorDecl::Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000627}
628
629VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000630}
631
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000632SourceRange VarDecl::getSourceRange() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000633 SourceLocation Start = getTypeSpecStartLoc();
634 if (Start.isInvalid())
635 Start = getLocation();
636
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000637 if (getInit())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000638 return SourceRange(Start, getInit()->getLocEnd());
639 return SourceRange(Start, getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000640}
641
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000642bool VarDecl::isExternC() const {
643 ASTContext &Context = getASTContext();
644 if (!Context.getLangOptions().CPlusPlus)
645 return (getDeclContext()->isTranslationUnit() &&
646 getStorageClass() != Static) ||
647 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
648
649 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
650 DC = DC->getParent()) {
651 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
652 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
653 return getStorageClass() != Static;
654
655 break;
656 }
657
658 if (DC->isFunctionOrMethod())
659 return false;
660 }
661
662 return false;
663}
664
665VarDecl *VarDecl::getCanonicalDecl() {
666 return getFirstDeclaration();
667}
668
Sebastian Redle9d12b62010-01-31 22:27:38 +0000669VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
670 // C++ [basic.def]p2:
671 // A declaration is a definition unless [...] it contains the 'extern'
672 // specifier or a linkage-specification and neither an initializer [...],
673 // it declares a static data member in a class declaration [...].
674 // C++ [temp.expl.spec]p15:
675 // An explicit specialization of a static data member of a template is a
676 // definition if the declaration includes an initializer; otherwise, it is
677 // a declaration.
678 if (isStaticDataMember()) {
679 if (isOutOfLine() && (hasInit() ||
680 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
681 return Definition;
682 else
683 return DeclarationOnly;
684 }
685 // C99 6.7p5:
686 // A definition of an identifier is a declaration for that identifier that
687 // [...] causes storage to be reserved for that object.
688 // Note: that applies for all non-file-scope objects.
689 // C99 6.9.2p1:
690 // If the declaration of an identifier for an object has file scope and an
691 // initializer, the declaration is an external definition for the identifier
692 if (hasInit())
693 return Definition;
694 // AST for 'extern "C" int foo;' is annotated with 'extern'.
695 if (hasExternalStorage())
696 return DeclarationOnly;
697
698 // C99 6.9.2p2:
699 // A declaration of an object that has file scope without an initializer,
700 // and without a storage class specifier or the scs 'static', constitutes
701 // a tentative definition.
702 // No such thing in C++.
703 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
704 return TentativeDefinition;
705
706 // What's left is (in C, block-scope) declarations without initializers or
707 // external storage. These are definitions.
708 return Definition;
709}
710
Sebastian Redle9d12b62010-01-31 22:27:38 +0000711VarDecl *VarDecl::getActingDefinition() {
712 DefinitionKind Kind = isThisDeclarationADefinition();
713 if (Kind != TentativeDefinition)
714 return 0;
715
716 VarDecl *LastTentative = false;
717 VarDecl *First = getFirstDeclaration();
718 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
719 I != E; ++I) {
720 Kind = (*I)->isThisDeclarationADefinition();
721 if (Kind == Definition)
722 return 0;
723 else if (Kind == TentativeDefinition)
724 LastTentative = *I;
725 }
726 return LastTentative;
727}
728
729bool VarDecl::isTentativeDefinitionNow() const {
730 DefinitionKind Kind = isThisDeclarationADefinition();
731 if (Kind != TentativeDefinition)
732 return false;
733
734 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
735 if ((*I)->isThisDeclarationADefinition() == Definition)
736 return false;
737 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000738 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000739}
740
Sebastian Redl31310a22010-02-01 20:16:42 +0000741VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000742 VarDecl *First = getFirstDeclaration();
743 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
744 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000745 if ((*I)->isThisDeclarationADefinition() == Definition)
746 return *I;
747 }
748 return 0;
749}
750
751const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000752 redecl_iterator I = redecls_begin(), E = redecls_end();
753 while (I != E && !I->getInit())
754 ++I;
755
756 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000757 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000758 return I->getInit();
759 }
760 return 0;
761}
762
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000763bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000764 if (Decl::isOutOfLine())
765 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000766
767 if (!isStaticDataMember())
768 return false;
769
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000770 // If this static data member was instantiated from a static data member of
771 // a class template, check whether that static data member was defined
772 // out-of-line.
773 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
774 return VD->isOutOfLine();
775
776 return false;
777}
778
Douglas Gregor0d035142009-10-27 18:42:08 +0000779VarDecl *VarDecl::getOutOfLineDefinition() {
780 if (!isStaticDataMember())
781 return 0;
782
783 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
784 RD != RDEnd; ++RD) {
785 if (RD->getLexicalDeclContext()->isFileContext())
786 return *RD;
787 }
788
789 return 0;
790}
791
Douglas Gregor838db382010-02-11 01:19:42 +0000792void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000793 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
794 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000795 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000796 }
797
798 Init = I;
799}
800
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000801VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000802 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000803 return cast<VarDecl>(MSI->getInstantiatedFrom());
804
805 return 0;
806}
807
Douglas Gregor663b5a02009-10-14 20:14:33 +0000808TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +0000809 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000810 return MSI->getTemplateSpecializationKind();
811
812 return TSK_Undeclared;
813}
814
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000815MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000816 return getASTContext().getInstantiatedFromStaticDataMember(this);
817}
818
Douglas Gregor0a897e32009-10-15 17:21:20 +0000819void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
820 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000821 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000822 assert(MSI && "Not an instantiated static data member?");
823 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000824 if (TSK != TSK_ExplicitSpecialization &&
825 PointOfInstantiation.isValid() &&
826 MSI->getPointOfInstantiation().isInvalid())
827 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000828}
829
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000830//===----------------------------------------------------------------------===//
831// ParmVarDecl Implementation
832//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +0000833
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000834ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
835 SourceLocation L, IdentifierInfo *Id,
836 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000837 StorageClass S, StorageClass SCAsWritten,
838 Expr *DefArg) {
839 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
840 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +0000841}
842
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000843Expr *ParmVarDecl::getDefaultArg() {
844 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
845 assert(!hasUninstantiatedDefaultArg() &&
846 "Default argument is not yet instantiated!");
847
848 Expr *Arg = getInit();
849 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
850 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +0000851
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000852 return Arg;
853}
854
855unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
856 if (const CXXExprWithTemporaries *E =
857 dyn_cast<CXXExprWithTemporaries>(getInit()))
858 return E->getNumTemporaries();
859
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000860 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000861}
862
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000863CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
864 assert(getNumDefaultArgTemporaries() &&
865 "Default arguments does not have any temporaries!");
866
867 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
868 return E->getTemporary(i);
869}
870
871SourceRange ParmVarDecl::getDefaultArgRange() const {
872 if (const Expr *E = getInit())
873 return E->getSourceRange();
874
875 if (hasUninstantiatedDefaultArg())
876 return getUninstantiatedDefaultArg()->getSourceRange();
877
878 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000879}
880
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000881//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000882// FunctionDecl Implementation
883//===----------------------------------------------------------------------===//
884
Ted Kremenek27f8a282008-05-20 00:43:19 +0000885void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000886 if (Body && Body.isOffset())
887 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000888
889 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
890 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000891
Douglas Gregor2db32322009-10-07 23:56:10 +0000892 FunctionTemplateSpecializationInfo *FTSInfo
893 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
894 if (FTSInfo)
895 C.Deallocate(FTSInfo);
896
897 MemberSpecializationInfo *MSInfo
898 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
899 if (MSInfo)
900 C.Deallocate(MSInfo);
901
Steve Naroff3e970492009-01-27 21:25:57 +0000902 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000903
John McCallb6217662010-03-15 10:12:16 +0000904 DeclaratorDecl::Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +0000905}
906
John McCall136a6982009-09-11 06:45:03 +0000907void FunctionDecl::getNameForDiagnostic(std::string &S,
908 const PrintingPolicy &Policy,
909 bool Qualified) const {
910 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
911 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
912 if (TemplateArgs)
913 S += TemplateSpecializationType::PrintTemplateArgumentList(
914 TemplateArgs->getFlatArgumentList(),
915 TemplateArgs->flat_size(),
916 Policy);
917
918}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000919
Ted Kremenek9498d382010-04-29 16:49:01 +0000920bool FunctionDecl::isVariadic() const {
921 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
922 return FT->isVariadic();
923 return false;
924}
925
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000926Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000927 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
928 if (I->Body) {
929 Definition = *I;
930 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000931 }
932 }
933
934 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000935}
936
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000937void FunctionDecl::setBody(Stmt *B) {
938 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000939 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000940 EndRangeLoc = B->getLocEnd();
941}
942
Douglas Gregor48a83b52009-09-12 00:17:51 +0000943bool FunctionDecl::isMain() const {
944 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000945 return !Context.getLangOptions().Freestanding &&
946 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000947 getIdentifier() && getIdentifier()->isStr("main");
948}
949
Douglas Gregor48a83b52009-09-12 00:17:51 +0000950bool FunctionDecl::isExternC() const {
951 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000952 // In C, any non-static, non-overloadable function has external
953 // linkage.
954 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000955 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000956
Mike Stump1eb44332009-09-09 15:08:12 +0000957 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000958 DC = DC->getParent()) {
959 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
960 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000961 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000962 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000963
964 break;
965 }
966 }
967
968 return false;
969}
970
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000971bool FunctionDecl::isGlobal() const {
972 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
973 return Method->isStatic();
974
975 if (getStorageClass() == Static)
976 return false;
977
Mike Stump1eb44332009-09-09 15:08:12 +0000978 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000979 DC->isNamespace();
980 DC = DC->getParent()) {
981 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
982 if (!Namespace->getDeclName())
983 return false;
984 break;
985 }
986 }
987
988 return true;
989}
990
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000991void
992FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
993 redeclarable_base::setPreviousDeclaration(PrevDecl);
994
995 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
996 FunctionTemplateDecl *PrevFunTmpl
997 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
998 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
999 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1000 }
1001}
1002
1003const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1004 return getFirstDeclaration();
1005}
1006
1007FunctionDecl *FunctionDecl::getCanonicalDecl() {
1008 return getFirstDeclaration();
1009}
1010
Douglas Gregor3e41d602009-02-13 23:20:09 +00001011/// \brief Returns a value indicating whether this function
1012/// corresponds to a builtin function.
1013///
1014/// The function corresponds to a built-in function if it is
1015/// declared at translation scope or within an extern "C" block and
1016/// its name matches with the name of a builtin. The returned value
1017/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001018/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001019/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001020unsigned FunctionDecl::getBuiltinID() const {
1021 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001022 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1023 return 0;
1024
1025 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1026 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1027 return BuiltinID;
1028
1029 // This function has the name of a known C library
1030 // function. Determine whether it actually refers to the C library
1031 // function or whether it just has the same name.
1032
Douglas Gregor9add3172009-02-17 03:23:10 +00001033 // If this is a static function, it's not a builtin.
1034 if (getStorageClass() == Static)
1035 return 0;
1036
Douglas Gregor3c385e52009-02-14 18:57:46 +00001037 // If this function is at translation-unit scope and we're not in
1038 // C++, it refers to the C library function.
1039 if (!Context.getLangOptions().CPlusPlus &&
1040 getDeclContext()->isTranslationUnit())
1041 return BuiltinID;
1042
1043 // If the function is in an extern "C" linkage specification and is
1044 // not marked "overloadable", it's the real function.
1045 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001046 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001047 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001048 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001049 return BuiltinID;
1050
1051 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001052 return 0;
1053}
1054
1055
Chris Lattner1ad9b282009-04-25 06:03:53 +00001056/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001057/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001058/// after it has been created.
1059unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001060 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001061 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001062 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001063 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Reid Spencer5f016e22007-07-11 17:01:13 +00001065}
1066
Douglas Gregor838db382010-02-11 01:19:42 +00001067void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001069 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 // Zero params -> null pointer.
1072 if (NumParams) {
Douglas Gregor838db382010-02-11 01:19:42 +00001073 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001074 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001076
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001077 // Update source range. The check below allows us to set EndRangeLoc before
1078 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001079 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001080 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 }
1082}
1083
Chris Lattner8123a952008-04-10 02:22:51 +00001084/// getMinRequiredArguments - Returns the minimum number of arguments
1085/// needed to call this function. This may be fewer than the number of
1086/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001087/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001088unsigned FunctionDecl::getMinRequiredArguments() const {
1089 unsigned NumRequiredArgs = getNumParams();
1090 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001091 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001092 --NumRequiredArgs;
1093
1094 return NumRequiredArgs;
1095}
1096
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001097bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001098 // FIXME: This is not enough. Consider:
1099 //
1100 // inline void f();
1101 // void f() { }
1102 //
1103 // f is inlined, but does not have inline specified.
1104 // To fix this we should add an 'inline' flag to FunctionDecl.
1105 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001106 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001107
1108 if (isa<CXXMethodDecl>(this)) {
1109 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1110 return true;
1111 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001112
1113 switch (getTemplateSpecializationKind()) {
1114 case TSK_Undeclared:
1115 case TSK_ExplicitSpecialization:
1116 return false;
1117
1118 case TSK_ImplicitInstantiation:
1119 case TSK_ExplicitInstantiationDeclaration:
1120 case TSK_ExplicitInstantiationDefinition:
1121 // Handle below.
1122 break;
1123 }
1124
1125 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1126 Stmt *Pattern = 0;
1127 if (PatternDecl)
1128 Pattern = PatternDecl->getBody(PatternDecl);
1129
1130 if (Pattern && PatternDecl)
1131 return PatternDecl->isInlined();
1132
1133 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001134}
1135
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001136/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001137/// definition will be externally visible.
1138///
1139/// Inline function definitions are always available for inlining optimizations.
1140/// However, depending on the language dialect, declaration specifiers, and
1141/// attributes, the definition of an inline function may or may not be
1142/// "externally" visible to other translation units in the program.
1143///
1144/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001145/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001146/// inline definition becomes externally visible (C99 6.7.4p6).
1147///
1148/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1149/// definition, we use the GNU semantics for inline, which are nearly the
1150/// opposite of C99 semantics. In particular, "inline" by itself will create
1151/// an externally visible symbol, but "extern inline" will not create an
1152/// externally visible symbol.
1153bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1154 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001155 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001156 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001157
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001158 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001159 // GNU inline semantics. Based on a number of examples, we came up with the
1160 // following heuristic: if the "inline" keyword is present on a
1161 // declaration of the function but "extern" is not present on that
1162 // declaration, then the symbol is externally visible. Otherwise, the GNU
1163 // "extern inline" semantics applies and the symbol is not externally
1164 // visible.
1165 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1166 Redecl != RedeclEnd;
1167 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001168 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001169 return true;
1170 }
1171
1172 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001173 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001174 }
1175
1176 // C99 6.7.4p6:
1177 // [...] If all of the file scope declarations for a function in a
1178 // translation unit include the inline function specifier without extern,
1179 // then the definition in that translation unit is an inline definition.
1180 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1181 Redecl != RedeclEnd;
1182 ++Redecl) {
1183 // Only consider file-scope declarations in this test.
1184 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1185 continue;
1186
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001187 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001188 return true; // Not an inline definition
1189 }
1190
1191 // C99 6.7.4p6:
1192 // An inline definition does not provide an external definition for the
1193 // function, and does not forbid an external definition in another
1194 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001195 return false;
1196}
1197
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001198/// getOverloadedOperator - Which C++ overloaded operator this
1199/// function represents, if any.
1200OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001201 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1202 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001203 else
1204 return OO_None;
1205}
1206
Sean Hunta6c058d2010-01-13 09:01:02 +00001207/// getLiteralIdentifier - The literal suffix identifier this function
1208/// represents, if any.
1209const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1210 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1211 return getDeclName().getCXXLiteralIdentifier();
1212 else
1213 return 0;
1214}
1215
Douglas Gregor2db32322009-10-07 23:56:10 +00001216FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001217 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001218 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1219
1220 return 0;
1221}
1222
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001223MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1224 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1225}
1226
Douglas Gregor2db32322009-10-07 23:56:10 +00001227void
1228FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1229 TemplateSpecializationKind TSK) {
1230 assert(TemplateOrSpecialization.isNull() &&
1231 "Member function is already a specialization");
1232 MemberSpecializationInfo *Info
1233 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1234 TemplateOrSpecialization = Info;
1235}
1236
Douglas Gregor3b846b62009-10-27 20:53:28 +00001237bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001238 // If the function is invalid, it can't be implicitly instantiated.
1239 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001240 return false;
1241
1242 switch (getTemplateSpecializationKind()) {
1243 case TSK_Undeclared:
1244 case TSK_ExplicitSpecialization:
1245 case TSK_ExplicitInstantiationDefinition:
1246 return false;
1247
1248 case TSK_ImplicitInstantiation:
1249 return true;
1250
1251 case TSK_ExplicitInstantiationDeclaration:
1252 // Handled below.
1253 break;
1254 }
1255
1256 // Find the actual template from which we will instantiate.
1257 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1258 Stmt *Pattern = 0;
1259 if (PatternDecl)
1260 Pattern = PatternDecl->getBody(PatternDecl);
1261
1262 // C++0x [temp.explicit]p9:
1263 // Except for inline functions, other explicit instantiation declarations
1264 // have the effect of suppressing the implicit instantiation of the entity
1265 // to which they refer.
1266 if (!Pattern || !PatternDecl)
1267 return true;
1268
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001269 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001270}
1271
1272FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1273 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1274 while (Primary->getInstantiatedFromMemberTemplate()) {
1275 // If we have hit a point where the user provided a specialization of
1276 // this template, we're done looking.
1277 if (Primary->isMemberSpecialization())
1278 break;
1279
1280 Primary = Primary->getInstantiatedFromMemberTemplate();
1281 }
1282
1283 return Primary->getTemplatedDecl();
1284 }
1285
1286 return getInstantiatedFromMemberFunction();
1287}
1288
Douglas Gregor16e8be22009-06-29 17:30:29 +00001289FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001290 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001291 = TemplateOrSpecialization
1292 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001293 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001294 }
1295 return 0;
1296}
1297
1298const TemplateArgumentList *
1299FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001300 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001301 = TemplateOrSpecialization
1302 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001303 return Info->TemplateArguments;
1304 }
1305 return 0;
1306}
1307
Abramo Bagnarae03db982010-05-20 15:32:11 +00001308const TemplateArgumentListInfo *
1309FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1310 if (FunctionTemplateSpecializationInfo *Info
1311 = TemplateOrSpecialization
1312 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1313 return Info->TemplateArgumentsAsWritten;
1314 }
1315 return 0;
1316}
1317
Mike Stump1eb44332009-09-09 15:08:12 +00001318void
Douglas Gregor838db382010-02-11 01:19:42 +00001319FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001320 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001321 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001322 TemplateSpecializationKind TSK,
1323 const TemplateArgumentListInfo *TemplateArgsAsWritten) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001324 assert(TSK != TSK_Undeclared &&
1325 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001326 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001327 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001328 if (!Info)
Douglas Gregor838db382010-02-11 01:19:42 +00001329 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Douglas Gregor127102b2009-06-29 20:59:39 +00001331 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001332 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001333 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001334 Info->TemplateArguments = TemplateArgs;
Abramo Bagnarae03db982010-05-20 15:32:11 +00001335 Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
Douglas Gregor1637be72009-06-26 00:10:03 +00001336 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Douglas Gregor127102b2009-06-29 20:59:39 +00001338 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001339 // function template specializations.
1340 if (InsertPos)
1341 Template->getSpecializations().InsertNode(Info, InsertPos);
1342 else {
1343 // Try to insert the new node. If there is an existing node, remove it
1344 // first.
1345 FunctionTemplateSpecializationInfo *Existing
1346 = Template->getSpecializations().GetOrInsertNode(Info);
1347 if (Existing) {
1348 Template->getSpecializations().RemoveNode(Existing);
1349 Template->getSpecializations().GetOrInsertNode(Info);
1350 }
1351 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001352}
1353
John McCallaf2094e2010-04-08 09:05:18 +00001354void
1355FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1356 const UnresolvedSetImpl &Templates,
1357 const TemplateArgumentListInfo &TemplateArgs) {
1358 assert(TemplateOrSpecialization.isNull());
1359 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1360 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001361 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001362 void *Buffer = Context.Allocate(Size);
1363 DependentFunctionTemplateSpecializationInfo *Info =
1364 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1365 TemplateArgs);
1366 TemplateOrSpecialization = Info;
1367}
1368
1369DependentFunctionTemplateSpecializationInfo::
1370DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1371 const TemplateArgumentListInfo &TArgs)
1372 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1373
1374 d.NumTemplates = Ts.size();
1375 d.NumArgs = TArgs.size();
1376
1377 FunctionTemplateDecl **TsArray =
1378 const_cast<FunctionTemplateDecl**>(getTemplates());
1379 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1380 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1381
1382 TemplateArgumentLoc *ArgsArray =
1383 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1384 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1385 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1386}
1387
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001388TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001389 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001390 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001391 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001392 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001393 if (FTSInfo)
1394 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Douglas Gregor2db32322009-10-07 23:56:10 +00001396 MemberSpecializationInfo *MSInfo
1397 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1398 if (MSInfo)
1399 return MSInfo->getTemplateSpecializationKind();
1400
1401 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001402}
1403
Mike Stump1eb44332009-09-09 15:08:12 +00001404void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001405FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1406 SourceLocation PointOfInstantiation) {
1407 if (FunctionTemplateSpecializationInfo *FTSInfo
1408 = TemplateOrSpecialization.dyn_cast<
1409 FunctionTemplateSpecializationInfo*>()) {
1410 FTSInfo->setTemplateSpecializationKind(TSK);
1411 if (TSK != TSK_ExplicitSpecialization &&
1412 PointOfInstantiation.isValid() &&
1413 FTSInfo->getPointOfInstantiation().isInvalid())
1414 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1415 } else if (MemberSpecializationInfo *MSInfo
1416 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1417 MSInfo->setTemplateSpecializationKind(TSK);
1418 if (TSK != TSK_ExplicitSpecialization &&
1419 PointOfInstantiation.isValid() &&
1420 MSInfo->getPointOfInstantiation().isInvalid())
1421 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1422 } else
1423 assert(false && "Function cannot have a template specialization kind");
1424}
1425
1426SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001427 if (FunctionTemplateSpecializationInfo *FTSInfo
1428 = TemplateOrSpecialization.dyn_cast<
1429 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001430 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001431 else if (MemberSpecializationInfo *MSInfo
1432 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001433 return MSInfo->getPointOfInstantiation();
1434
1435 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001436}
1437
Douglas Gregor9f185072009-09-11 20:15:17 +00001438bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001439 if (Decl::isOutOfLine())
1440 return true;
1441
1442 // If this function was instantiated from a member function of a
1443 // class template, check whether that member function was defined out-of-line.
1444 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1445 const FunctionDecl *Definition;
1446 if (FD->getBody(Definition))
1447 return Definition->isOutOfLine();
1448 }
1449
1450 // If this function was instantiated from a function template,
1451 // check whether that function template was defined out-of-line.
1452 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1453 const FunctionDecl *Definition;
1454 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1455 return Definition->isOutOfLine();
1456 }
1457
1458 return false;
1459}
1460
Chris Lattner8a934232008-03-31 00:36:02 +00001461//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001462// FieldDecl Implementation
1463//===----------------------------------------------------------------------===//
1464
1465FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1466 IdentifierInfo *Id, QualType T,
1467 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1468 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1469}
1470
1471bool FieldDecl::isAnonymousStructOrUnion() const {
1472 if (!isImplicit() || getDeclName())
1473 return false;
1474
1475 if (const RecordType *Record = getType()->getAs<RecordType>())
1476 return Record->getDecl()->isAnonymousStructOrUnion();
1477
1478 return false;
1479}
1480
1481//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001482// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001483//===----------------------------------------------------------------------===//
1484
John McCallb6217662010-03-15 10:12:16 +00001485void TagDecl::Destroy(ASTContext &C) {
1486 if (hasExtInfo())
1487 C.Deallocate(getExtInfo());
1488 TypeDecl::Destroy(C);
1489}
1490
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001491SourceRange TagDecl::getSourceRange() const {
1492 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001493 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001494}
1495
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001496TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001497 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001498}
1499
Douglas Gregor60e70642010-05-19 18:39:18 +00001500void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1501 TypedefDeclOrQualifier = TDD;
1502 if (TypeForDecl)
1503 TypeForDecl->ClearLinkageCache();
1504}
1505
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001506void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001507 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1508 TagT->decl.setPointer(this);
1509 TagT->decl.setInt(1);
Douglas Gregor9ffce212010-04-30 04:39:27 +00001510 } else if (InjectedClassNameType *Injected
1511 = const_cast<InjectedClassNameType *>(
1512 TypeForDecl->getAs<InjectedClassNameType>())) {
1513 Injected->Decl = cast<CXXRecordDecl>(this);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001514 }
John McCall86ff3082010-02-04 22:26:26 +00001515
1516 if (isa<CXXRecordDecl>(this)) {
1517 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1518 struct CXXRecordDecl::DefinitionData *Data =
1519 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001520 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1521 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001522 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001523}
1524
1525void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001526 assert((!isa<CXXRecordDecl>(this) ||
1527 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1528 "definition completed but not started");
1529
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001530 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001531 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1532 assert(TagT->decl.getPointer() == this &&
1533 "Attempt to redefine a tag definition?");
1534 TagT->decl.setInt(0);
Douglas Gregor9ffce212010-04-30 04:39:27 +00001535 } else if (InjectedClassNameType *Injected
1536 = const_cast<InjectedClassNameType *>(
1537 TypeForDecl->getAs<InjectedClassNameType>())) {
1538 assert(Injected->Decl == this &&
1539 "Attempt to redefine a class template definition?");
Chandler Carrutha8426972010-05-06 05:28:42 +00001540 (void)Injected;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001541 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001542}
1543
Douglas Gregor952b0172010-02-11 01:04:33 +00001544TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001545 if (isDefinition())
1546 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001547
1548 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001549 R != REnd; ++R)
1550 if (R->isDefinition())
1551 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001553 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001554}
1555
John McCallb6217662010-03-15 10:12:16 +00001556void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1557 SourceRange QualifierRange) {
1558 if (Qualifier) {
1559 // Make sure the extended qualifier info is allocated.
1560 if (!hasExtInfo())
1561 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1562 // Set qualifier info.
1563 getExtInfo()->NNS = Qualifier;
1564 getExtInfo()->NNSRange = QualifierRange;
1565 }
1566 else {
1567 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1568 assert(QualifierRange.isInvalid());
1569 if (hasExtInfo()) {
1570 getASTContext().Deallocate(getExtInfo());
1571 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1572 }
1573 }
1574}
1575
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001576//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001577// EnumDecl Implementation
1578//===----------------------------------------------------------------------===//
1579
1580EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1581 IdentifierInfo *Id, SourceLocation TKL,
1582 EnumDecl *PrevDecl) {
1583 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1584 C.getTypeDeclType(Enum, PrevDecl);
1585 return Enum;
1586}
1587
1588void EnumDecl::Destroy(ASTContext& C) {
John McCallb6217662010-03-15 10:12:16 +00001589 TagDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001590}
1591
Douglas Gregor838db382010-02-11 01:19:42 +00001592void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001593 QualType NewPromotionType,
1594 unsigned NumPositiveBits,
1595 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001596 assert(!isDefinition() && "Cannot redefine enums!");
1597 IntegerType = NewType;
1598 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001599 setNumPositiveBits(NumPositiveBits);
1600 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001601 TagDecl::completeDefinition();
1602}
1603
1604//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001605// RecordDecl Implementation
1606//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001607
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001608RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001609 IdentifierInfo *Id, RecordDecl *PrevDecl,
1610 SourceLocation TKL)
1611 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001612 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001613 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001614 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001615 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001616}
1617
1618RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001619 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001620 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001622 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001623 C.getTypeDeclType(R, PrevDecl);
1624 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001625}
1626
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001627RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001628}
1629
1630void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001631 TagDecl::Destroy(C);
1632}
1633
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001634bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001635 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001636 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1637}
1638
Douglas Gregor44b43212008-12-11 16:49:14 +00001639/// completeDefinition - Notes that the definition of this type is now
1640/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001641void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001643 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001644}
1645
John McCallbc365c52010-05-21 01:17:40 +00001646ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1647 // Force the decl chain to come into existence properly.
1648 if (!getNextDeclInContext()) getParent()->decls_begin();
1649
1650 assert(isAnonymousStructOrUnion());
1651 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1652 assert(D->getType()->isRecordType());
1653 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1654 return D;
1655}
1656
Steve Naroff56ee6892008-10-08 17:01:13 +00001657//===----------------------------------------------------------------------===//
1658// BlockDecl Implementation
1659//===----------------------------------------------------------------------===//
1660
1661BlockDecl::~BlockDecl() {
1662}
1663
1664void BlockDecl::Destroy(ASTContext& C) {
1665 if (Body)
1666 Body->Destroy(C);
1667
1668 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1669 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001670
1671 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001672 Decl::Destroy(C);
1673}
Steve Naroffe78b8092009-03-13 16:56:44 +00001674
Douglas Gregor838db382010-02-11 01:19:42 +00001675void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001676 unsigned NParms) {
1677 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Steve Naroffe78b8092009-03-13 16:56:44 +00001679 // Zero params -> null pointer.
1680 if (NParms) {
1681 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001682 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001683 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1684 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1685 }
1686}
1687
1688unsigned BlockDecl::getNumParams() const {
1689 return NumParams;
1690}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001691
1692
1693//===----------------------------------------------------------------------===//
1694// Other Decl Allocation/Deallocation Method Implementations
1695//===----------------------------------------------------------------------===//
1696
1697TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1698 return new (C) TranslationUnitDecl(C);
1699}
1700
1701NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1702 SourceLocation L, IdentifierInfo *Id) {
1703 return new (C) NamespaceDecl(DC, L, Id);
1704}
1705
1706void NamespaceDecl::Destroy(ASTContext& C) {
1707 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1708 // together. They are all top-level Decls.
1709
1710 this->~NamespaceDecl();
John McCallb6217662010-03-15 10:12:16 +00001711 Decl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001712}
1713
1714
1715ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1716 SourceLocation L, IdentifierInfo *Id, QualType T) {
1717 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1718}
1719
1720FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1721 SourceLocation L,
1722 DeclarationName N, QualType T,
1723 TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001724 StorageClass S, StorageClass SCAsWritten,
1725 bool isInline, bool hasWrittenPrototype) {
1726 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1727 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001728 New->HasWrittenPrototype = hasWrittenPrototype;
1729 return New;
1730}
1731
1732BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1733 return new (C) BlockDecl(DC, L);
1734}
1735
1736EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1737 SourceLocation L,
1738 IdentifierInfo *Id, QualType T,
1739 Expr *E, const llvm::APSInt &V) {
1740 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1741}
1742
1743void EnumConstantDecl::Destroy(ASTContext& C) {
1744 if (Init) Init->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001745 ValueDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001746}
1747
1748TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1749 SourceLocation L, IdentifierInfo *Id,
1750 TypeSourceInfo *TInfo) {
1751 return new (C) TypedefDecl(DC, L, Id, TInfo);
1752}
1753
1754// Anchor TypedefDecl's vtable here.
1755TypedefDecl::~TypedefDecl() {}
1756
1757FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1758 SourceLocation L,
1759 StringLiteral *Str) {
1760 return new (C) FileScopeAsmDecl(DC, L, Str);
1761}