blob: c912af878a814d8265e64827d74237000e24f2c8 [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
Douglas Gregorc722ea42010-06-15 17:44:38 +0000570QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
571 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000572 TemplateParameterList **TPLists) {
573 assert((NumTPLists == 0 || TPLists != 0) &&
574 "Empty array of template parameters with positive size!");
575 assert((NumTPLists == 0 || NNS) &&
576 "Nonempty array of template parameters with no qualifier!");
577
578 // Free previous template parameters (if any).
579 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000580 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000581 TemplParamLists = 0;
582 NumTemplParamLists = 0;
583 }
584 // Set info on matched template parameter lists (if any).
585 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000586 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000587 NumTemplParamLists = NumTPLists;
588 for (unsigned i = NumTPLists; i-- > 0; )
589 TemplParamLists[i] = TPLists[i];
590 }
591}
592
Douglas Gregorc722ea42010-06-15 17:44:38 +0000593void QualifierInfo::Destroy(ASTContext &Context) {
594 // FIXME: Deallocate template parameter lists themselves!
595 if (TemplParamLists)
596 Context.Deallocate(TemplParamLists);
597}
598
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000599//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000600// VarDecl Implementation
601//===----------------------------------------------------------------------===//
602
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000603const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
604 switch (SC) {
605 case VarDecl::None: break;
606 case VarDecl::Auto: return "auto"; break;
607 case VarDecl::Extern: return "extern"; break;
608 case VarDecl::PrivateExtern: return "__private_extern__"; break;
609 case VarDecl::Register: return "register"; break;
610 case VarDecl::Static: return "static"; break;
611 }
612
613 assert(0 && "Invalid storage class");
614 return 0;
615}
616
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000617VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000618 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000619 StorageClass S, StorageClass SCAsWritten) {
620 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000621}
622
623void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000624 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000625 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000626 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000627 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
628 Eval->~EvaluatedStmt();
629 C.Deallocate(Eval);
630 }
631 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000632 this->~VarDecl();
John McCallb6217662010-03-15 10:12:16 +0000633 DeclaratorDecl::Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000634}
635
636VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000637}
638
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000639SourceRange VarDecl::getSourceRange() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000640 SourceLocation Start = getTypeSpecStartLoc();
641 if (Start.isInvalid())
642 Start = getLocation();
643
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000644 if (getInit())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000645 return SourceRange(Start, getInit()->getLocEnd());
646 return SourceRange(Start, getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000647}
648
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000649bool VarDecl::isExternC() const {
650 ASTContext &Context = getASTContext();
651 if (!Context.getLangOptions().CPlusPlus)
652 return (getDeclContext()->isTranslationUnit() &&
653 getStorageClass() != Static) ||
654 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
655
656 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
657 DC = DC->getParent()) {
658 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
659 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
660 return getStorageClass() != Static;
661
662 break;
663 }
664
665 if (DC->isFunctionOrMethod())
666 return false;
667 }
668
669 return false;
670}
671
672VarDecl *VarDecl::getCanonicalDecl() {
673 return getFirstDeclaration();
674}
675
Sebastian Redle9d12b62010-01-31 22:27:38 +0000676VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
677 // C++ [basic.def]p2:
678 // A declaration is a definition unless [...] it contains the 'extern'
679 // specifier or a linkage-specification and neither an initializer [...],
680 // it declares a static data member in a class declaration [...].
681 // C++ [temp.expl.spec]p15:
682 // An explicit specialization of a static data member of a template is a
683 // definition if the declaration includes an initializer; otherwise, it is
684 // a declaration.
685 if (isStaticDataMember()) {
686 if (isOutOfLine() && (hasInit() ||
687 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
688 return Definition;
689 else
690 return DeclarationOnly;
691 }
692 // C99 6.7p5:
693 // A definition of an identifier is a declaration for that identifier that
694 // [...] causes storage to be reserved for that object.
695 // Note: that applies for all non-file-scope objects.
696 // C99 6.9.2p1:
697 // If the declaration of an identifier for an object has file scope and an
698 // initializer, the declaration is an external definition for the identifier
699 if (hasInit())
700 return Definition;
701 // AST for 'extern "C" int foo;' is annotated with 'extern'.
702 if (hasExternalStorage())
703 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000704
705 if (getStorageClassAsWritten() == Extern ||
706 getStorageClassAsWritten() == PrivateExtern) {
707 for (const VarDecl *PrevVar = getPreviousDeclaration();
708 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
709 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
710 return DeclarationOnly;
711 }
712 }
Sebastian Redle9d12b62010-01-31 22:27:38 +0000713 // C99 6.9.2p2:
714 // A declaration of an object that has file scope without an initializer,
715 // and without a storage class specifier or the scs 'static', constitutes
716 // a tentative definition.
717 // No such thing in C++.
718 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
719 return TentativeDefinition;
720
721 // What's left is (in C, block-scope) declarations without initializers or
722 // external storage. These are definitions.
723 return Definition;
724}
725
Sebastian Redle9d12b62010-01-31 22:27:38 +0000726VarDecl *VarDecl::getActingDefinition() {
727 DefinitionKind Kind = isThisDeclarationADefinition();
728 if (Kind != TentativeDefinition)
729 return 0;
730
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000731 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000732 VarDecl *First = getFirstDeclaration();
733 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
734 I != E; ++I) {
735 Kind = (*I)->isThisDeclarationADefinition();
736 if (Kind == Definition)
737 return 0;
738 else if (Kind == TentativeDefinition)
739 LastTentative = *I;
740 }
741 return LastTentative;
742}
743
744bool VarDecl::isTentativeDefinitionNow() const {
745 DefinitionKind Kind = isThisDeclarationADefinition();
746 if (Kind != TentativeDefinition)
747 return false;
748
749 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
750 if ((*I)->isThisDeclarationADefinition() == Definition)
751 return false;
752 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000753 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000754}
755
Sebastian Redl31310a22010-02-01 20:16:42 +0000756VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000757 VarDecl *First = getFirstDeclaration();
758 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
759 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000760 if ((*I)->isThisDeclarationADefinition() == Definition)
761 return *I;
762 }
763 return 0;
764}
765
766const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000767 redecl_iterator I = redecls_begin(), E = redecls_end();
768 while (I != E && !I->getInit())
769 ++I;
770
771 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000772 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000773 return I->getInit();
774 }
775 return 0;
776}
777
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000778bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000779 if (Decl::isOutOfLine())
780 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000781
782 if (!isStaticDataMember())
783 return false;
784
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000785 // If this static data member was instantiated from a static data member of
786 // a class template, check whether that static data member was defined
787 // out-of-line.
788 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
789 return VD->isOutOfLine();
790
791 return false;
792}
793
Douglas Gregor0d035142009-10-27 18:42:08 +0000794VarDecl *VarDecl::getOutOfLineDefinition() {
795 if (!isStaticDataMember())
796 return 0;
797
798 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
799 RD != RDEnd; ++RD) {
800 if (RD->getLexicalDeclContext()->isFileContext())
801 return *RD;
802 }
803
804 return 0;
805}
806
Douglas Gregor838db382010-02-11 01:19:42 +0000807void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000808 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
809 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000810 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000811 }
812
813 Init = I;
814}
815
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000816VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000817 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000818 return cast<VarDecl>(MSI->getInstantiatedFrom());
819
820 return 0;
821}
822
Douglas Gregor663b5a02009-10-14 20:14:33 +0000823TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +0000824 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000825 return MSI->getTemplateSpecializationKind();
826
827 return TSK_Undeclared;
828}
829
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000830MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000831 return getASTContext().getInstantiatedFromStaticDataMember(this);
832}
833
Douglas Gregor0a897e32009-10-15 17:21:20 +0000834void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
835 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000836 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000837 assert(MSI && "Not an instantiated static data member?");
838 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000839 if (TSK != TSK_ExplicitSpecialization &&
840 PointOfInstantiation.isValid() &&
841 MSI->getPointOfInstantiation().isInvalid())
842 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000843}
844
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000845//===----------------------------------------------------------------------===//
846// ParmVarDecl Implementation
847//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +0000848
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000849ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
850 SourceLocation L, IdentifierInfo *Id,
851 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000852 StorageClass S, StorageClass SCAsWritten,
853 Expr *DefArg) {
854 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
855 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +0000856}
857
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000858Expr *ParmVarDecl::getDefaultArg() {
859 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
860 assert(!hasUninstantiatedDefaultArg() &&
861 "Default argument is not yet instantiated!");
862
863 Expr *Arg = getInit();
864 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
865 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +0000866
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000867 return Arg;
868}
869
870unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
871 if (const CXXExprWithTemporaries *E =
872 dyn_cast<CXXExprWithTemporaries>(getInit()))
873 return E->getNumTemporaries();
874
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000875 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000876}
877
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000878CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
879 assert(getNumDefaultArgTemporaries() &&
880 "Default arguments does not have any temporaries!");
881
882 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
883 return E->getTemporary(i);
884}
885
886SourceRange ParmVarDecl::getDefaultArgRange() const {
887 if (const Expr *E = getInit())
888 return E->getSourceRange();
889
890 if (hasUninstantiatedDefaultArg())
891 return getUninstantiatedDefaultArg()->getSourceRange();
892
893 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000894}
895
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000896//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000897// FunctionDecl Implementation
898//===----------------------------------------------------------------------===//
899
Ted Kremenek27f8a282008-05-20 00:43:19 +0000900void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000901 if (Body && Body.isOffset())
902 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000903
904 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
905 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000906
Douglas Gregor2db32322009-10-07 23:56:10 +0000907 FunctionTemplateSpecializationInfo *FTSInfo
908 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
909 if (FTSInfo)
910 C.Deallocate(FTSInfo);
911
912 MemberSpecializationInfo *MSInfo
913 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
914 if (MSInfo)
915 C.Deallocate(MSInfo);
916
Steve Naroff3e970492009-01-27 21:25:57 +0000917 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000918
John McCallb6217662010-03-15 10:12:16 +0000919 DeclaratorDecl::Destroy(C);
Ted Kremenek27f8a282008-05-20 00:43:19 +0000920}
921
John McCall136a6982009-09-11 06:45:03 +0000922void FunctionDecl::getNameForDiagnostic(std::string &S,
923 const PrintingPolicy &Policy,
924 bool Qualified) const {
925 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
926 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
927 if (TemplateArgs)
928 S += TemplateSpecializationType::PrintTemplateArgumentList(
929 TemplateArgs->getFlatArgumentList(),
930 TemplateArgs->flat_size(),
931 Policy);
932
933}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000934
Ted Kremenek9498d382010-04-29 16:49:01 +0000935bool FunctionDecl::isVariadic() const {
936 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
937 return FT->isVariadic();
938 return false;
939}
940
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000941Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000942 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
943 if (I->Body) {
944 Definition = *I;
945 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000946 }
947 }
948
949 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000950}
951
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000952void FunctionDecl::setBody(Stmt *B) {
953 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000954 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000955 EndRangeLoc = B->getLocEnd();
956}
957
Douglas Gregor48a83b52009-09-12 00:17:51 +0000958bool FunctionDecl::isMain() const {
959 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000960 return !Context.getLangOptions().Freestanding &&
961 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000962 getIdentifier() && getIdentifier()->isStr("main");
963}
964
Douglas Gregor48a83b52009-09-12 00:17:51 +0000965bool FunctionDecl::isExternC() const {
966 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000967 // In C, any non-static, non-overloadable function has external
968 // linkage.
969 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000970 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000971
Mike Stump1eb44332009-09-09 15:08:12 +0000972 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000973 DC = DC->getParent()) {
974 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
975 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000976 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000977 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000978
979 break;
980 }
981 }
982
983 return false;
984}
985
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000986bool FunctionDecl::isGlobal() const {
987 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
988 return Method->isStatic();
989
990 if (getStorageClass() == Static)
991 return false;
992
Mike Stump1eb44332009-09-09 15:08:12 +0000993 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000994 DC->isNamespace();
995 DC = DC->getParent()) {
996 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
997 if (!Namespace->getDeclName())
998 return false;
999 break;
1000 }
1001 }
1002
1003 return true;
1004}
1005
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001006void
1007FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1008 redeclarable_base::setPreviousDeclaration(PrevDecl);
1009
1010 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1011 FunctionTemplateDecl *PrevFunTmpl
1012 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1013 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1014 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1015 }
1016}
1017
1018const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1019 return getFirstDeclaration();
1020}
1021
1022FunctionDecl *FunctionDecl::getCanonicalDecl() {
1023 return getFirstDeclaration();
1024}
1025
Douglas Gregor3e41d602009-02-13 23:20:09 +00001026/// \brief Returns a value indicating whether this function
1027/// corresponds to a builtin function.
1028///
1029/// The function corresponds to a built-in function if it is
1030/// declared at translation scope or within an extern "C" block and
1031/// its name matches with the name of a builtin. The returned value
1032/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001033/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001034/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001035unsigned FunctionDecl::getBuiltinID() const {
1036 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001037 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1038 return 0;
1039
1040 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1041 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1042 return BuiltinID;
1043
1044 // This function has the name of a known C library
1045 // function. Determine whether it actually refers to the C library
1046 // function or whether it just has the same name.
1047
Douglas Gregor9add3172009-02-17 03:23:10 +00001048 // If this is a static function, it's not a builtin.
1049 if (getStorageClass() == Static)
1050 return 0;
1051
Douglas Gregor3c385e52009-02-14 18:57:46 +00001052 // If this function is at translation-unit scope and we're not in
1053 // C++, it refers to the C library function.
1054 if (!Context.getLangOptions().CPlusPlus &&
1055 getDeclContext()->isTranslationUnit())
1056 return BuiltinID;
1057
1058 // If the function is in an extern "C" linkage specification and is
1059 // not marked "overloadable", it's the real function.
1060 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001061 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001062 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001063 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001064 return BuiltinID;
1065
1066 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001067 return 0;
1068}
1069
1070
Chris Lattner1ad9b282009-04-25 06:03:53 +00001071/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001072/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001073/// after it has been created.
1074unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001075 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001076 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001077 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001078 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Reid Spencer5f016e22007-07-11 17:01:13 +00001080}
1081
Douglas Gregor838db382010-02-11 01:19:42 +00001082void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001084 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 // Zero params -> null pointer.
1087 if (NumParams) {
Douglas Gregor838db382010-02-11 01:19:42 +00001088 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001089 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001091
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001092 // Update source range. The check below allows us to set EndRangeLoc before
1093 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001094 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001095 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 }
1097}
1098
Chris Lattner8123a952008-04-10 02:22:51 +00001099/// getMinRequiredArguments - Returns the minimum number of arguments
1100/// needed to call this function. This may be fewer than the number of
1101/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001102/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001103unsigned FunctionDecl::getMinRequiredArguments() const {
1104 unsigned NumRequiredArgs = getNumParams();
1105 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001106 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001107 --NumRequiredArgs;
1108
1109 return NumRequiredArgs;
1110}
1111
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001112bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001113 // FIXME: This is not enough. Consider:
1114 //
1115 // inline void f();
1116 // void f() { }
1117 //
1118 // f is inlined, but does not have inline specified.
1119 // To fix this we should add an 'inline' flag to FunctionDecl.
1120 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001121 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001122
1123 if (isa<CXXMethodDecl>(this)) {
1124 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1125 return true;
1126 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001127
1128 switch (getTemplateSpecializationKind()) {
1129 case TSK_Undeclared:
1130 case TSK_ExplicitSpecialization:
1131 return false;
1132
1133 case TSK_ImplicitInstantiation:
1134 case TSK_ExplicitInstantiationDeclaration:
1135 case TSK_ExplicitInstantiationDefinition:
1136 // Handle below.
1137 break;
1138 }
1139
1140 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1141 Stmt *Pattern = 0;
1142 if (PatternDecl)
1143 Pattern = PatternDecl->getBody(PatternDecl);
1144
1145 if (Pattern && PatternDecl)
1146 return PatternDecl->isInlined();
1147
1148 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001149}
1150
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001151/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001152/// definition will be externally visible.
1153///
1154/// Inline function definitions are always available for inlining optimizations.
1155/// However, depending on the language dialect, declaration specifiers, and
1156/// attributes, the definition of an inline function may or may not be
1157/// "externally" visible to other translation units in the program.
1158///
1159/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001160/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001161/// inline definition becomes externally visible (C99 6.7.4p6).
1162///
1163/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1164/// definition, we use the GNU semantics for inline, which are nearly the
1165/// opposite of C99 semantics. In particular, "inline" by itself will create
1166/// an externally visible symbol, but "extern inline" will not create an
1167/// externally visible symbol.
1168bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1169 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001170 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001171 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001172
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001173 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001174 // GNU inline semantics. Based on a number of examples, we came up with the
1175 // following heuristic: if the "inline" keyword is present on a
1176 // declaration of the function but "extern" is not present on that
1177 // declaration, then the symbol is externally visible. Otherwise, the GNU
1178 // "extern inline" semantics applies and the symbol is not externally
1179 // visible.
1180 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1181 Redecl != RedeclEnd;
1182 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001183 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001184 return true;
1185 }
1186
1187 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001188 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001189 }
1190
1191 // C99 6.7.4p6:
1192 // [...] If all of the file scope declarations for a function in a
1193 // translation unit include the inline function specifier without extern,
1194 // then the definition in that translation unit is an inline definition.
1195 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1196 Redecl != RedeclEnd;
1197 ++Redecl) {
1198 // Only consider file-scope declarations in this test.
1199 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1200 continue;
1201
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001202 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001203 return true; // Not an inline definition
1204 }
1205
1206 // C99 6.7.4p6:
1207 // An inline definition does not provide an external definition for the
1208 // function, and does not forbid an external definition in another
1209 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001210 return false;
1211}
1212
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001213/// getOverloadedOperator - Which C++ overloaded operator this
1214/// function represents, if any.
1215OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001216 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1217 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001218 else
1219 return OO_None;
1220}
1221
Sean Hunta6c058d2010-01-13 09:01:02 +00001222/// getLiteralIdentifier - The literal suffix identifier this function
1223/// represents, if any.
1224const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1225 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1226 return getDeclName().getCXXLiteralIdentifier();
1227 else
1228 return 0;
1229}
1230
Douglas Gregor2db32322009-10-07 23:56:10 +00001231FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001232 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001233 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1234
1235 return 0;
1236}
1237
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001238MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1239 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1240}
1241
Douglas Gregor2db32322009-10-07 23:56:10 +00001242void
1243FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1244 TemplateSpecializationKind TSK) {
1245 assert(TemplateOrSpecialization.isNull() &&
1246 "Member function is already a specialization");
1247 MemberSpecializationInfo *Info
1248 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1249 TemplateOrSpecialization = Info;
1250}
1251
Douglas Gregor3b846b62009-10-27 20:53:28 +00001252bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001253 // If the function is invalid, it can't be implicitly instantiated.
1254 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001255 return false;
1256
1257 switch (getTemplateSpecializationKind()) {
1258 case TSK_Undeclared:
1259 case TSK_ExplicitSpecialization:
1260 case TSK_ExplicitInstantiationDefinition:
1261 return false;
1262
1263 case TSK_ImplicitInstantiation:
1264 return true;
1265
1266 case TSK_ExplicitInstantiationDeclaration:
1267 // Handled below.
1268 break;
1269 }
1270
1271 // Find the actual template from which we will instantiate.
1272 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1273 Stmt *Pattern = 0;
1274 if (PatternDecl)
1275 Pattern = PatternDecl->getBody(PatternDecl);
1276
1277 // C++0x [temp.explicit]p9:
1278 // Except for inline functions, other explicit instantiation declarations
1279 // have the effect of suppressing the implicit instantiation of the entity
1280 // to which they refer.
1281 if (!Pattern || !PatternDecl)
1282 return true;
1283
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001284 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001285}
1286
1287FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1288 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1289 while (Primary->getInstantiatedFromMemberTemplate()) {
1290 // If we have hit a point where the user provided a specialization of
1291 // this template, we're done looking.
1292 if (Primary->isMemberSpecialization())
1293 break;
1294
1295 Primary = Primary->getInstantiatedFromMemberTemplate();
1296 }
1297
1298 return Primary->getTemplatedDecl();
1299 }
1300
1301 return getInstantiatedFromMemberFunction();
1302}
1303
Douglas Gregor16e8be22009-06-29 17:30:29 +00001304FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001305 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001306 = TemplateOrSpecialization
1307 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001308 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001309 }
1310 return 0;
1311}
1312
1313const TemplateArgumentList *
1314FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001315 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001316 = TemplateOrSpecialization
1317 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001318 return Info->TemplateArguments;
1319 }
1320 return 0;
1321}
1322
Abramo Bagnarae03db982010-05-20 15:32:11 +00001323const TemplateArgumentListInfo *
1324FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1325 if (FunctionTemplateSpecializationInfo *Info
1326 = TemplateOrSpecialization
1327 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1328 return Info->TemplateArgumentsAsWritten;
1329 }
1330 return 0;
1331}
1332
Mike Stump1eb44332009-09-09 15:08:12 +00001333void
Douglas Gregor838db382010-02-11 01:19:42 +00001334FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001335 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001336 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001337 TemplateSpecializationKind TSK,
1338 const TemplateArgumentListInfo *TemplateArgsAsWritten) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001339 assert(TSK != TSK_Undeclared &&
1340 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001341 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001342 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001343 if (!Info)
Douglas Gregor838db382010-02-11 01:19:42 +00001344 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Douglas Gregor127102b2009-06-29 20:59:39 +00001346 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001347 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001348 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001349 Info->TemplateArguments = TemplateArgs;
Abramo Bagnarae03db982010-05-20 15:32:11 +00001350 Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
Douglas Gregor1637be72009-06-26 00:10:03 +00001351 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor127102b2009-06-29 20:59:39 +00001353 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001354 // function template specializations.
1355 if (InsertPos)
1356 Template->getSpecializations().InsertNode(Info, InsertPos);
1357 else {
1358 // Try to insert the new node. If there is an existing node, remove it
1359 // first.
1360 FunctionTemplateSpecializationInfo *Existing
1361 = Template->getSpecializations().GetOrInsertNode(Info);
1362 if (Existing) {
1363 Template->getSpecializations().RemoveNode(Existing);
1364 Template->getSpecializations().GetOrInsertNode(Info);
1365 }
1366 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001367}
1368
John McCallaf2094e2010-04-08 09:05:18 +00001369void
1370FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1371 const UnresolvedSetImpl &Templates,
1372 const TemplateArgumentListInfo &TemplateArgs) {
1373 assert(TemplateOrSpecialization.isNull());
1374 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1375 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001376 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001377 void *Buffer = Context.Allocate(Size);
1378 DependentFunctionTemplateSpecializationInfo *Info =
1379 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1380 TemplateArgs);
1381 TemplateOrSpecialization = Info;
1382}
1383
1384DependentFunctionTemplateSpecializationInfo::
1385DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1386 const TemplateArgumentListInfo &TArgs)
1387 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1388
1389 d.NumTemplates = Ts.size();
1390 d.NumArgs = TArgs.size();
1391
1392 FunctionTemplateDecl **TsArray =
1393 const_cast<FunctionTemplateDecl**>(getTemplates());
1394 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1395 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1396
1397 TemplateArgumentLoc *ArgsArray =
1398 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1399 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1400 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1401}
1402
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001403TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001404 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001405 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001406 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001407 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001408 if (FTSInfo)
1409 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregor2db32322009-10-07 23:56:10 +00001411 MemberSpecializationInfo *MSInfo
1412 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1413 if (MSInfo)
1414 return MSInfo->getTemplateSpecializationKind();
1415
1416 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001417}
1418
Mike Stump1eb44332009-09-09 15:08:12 +00001419void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001420FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1421 SourceLocation PointOfInstantiation) {
1422 if (FunctionTemplateSpecializationInfo *FTSInfo
1423 = TemplateOrSpecialization.dyn_cast<
1424 FunctionTemplateSpecializationInfo*>()) {
1425 FTSInfo->setTemplateSpecializationKind(TSK);
1426 if (TSK != TSK_ExplicitSpecialization &&
1427 PointOfInstantiation.isValid() &&
1428 FTSInfo->getPointOfInstantiation().isInvalid())
1429 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1430 } else if (MemberSpecializationInfo *MSInfo
1431 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1432 MSInfo->setTemplateSpecializationKind(TSK);
1433 if (TSK != TSK_ExplicitSpecialization &&
1434 PointOfInstantiation.isValid() &&
1435 MSInfo->getPointOfInstantiation().isInvalid())
1436 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1437 } else
1438 assert(false && "Function cannot have a template specialization kind");
1439}
1440
1441SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001442 if (FunctionTemplateSpecializationInfo *FTSInfo
1443 = TemplateOrSpecialization.dyn_cast<
1444 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001445 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001446 else if (MemberSpecializationInfo *MSInfo
1447 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001448 return MSInfo->getPointOfInstantiation();
1449
1450 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001451}
1452
Douglas Gregor9f185072009-09-11 20:15:17 +00001453bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001454 if (Decl::isOutOfLine())
1455 return true;
1456
1457 // If this function was instantiated from a member function of a
1458 // class template, check whether that member function was defined out-of-line.
1459 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1460 const FunctionDecl *Definition;
1461 if (FD->getBody(Definition))
1462 return Definition->isOutOfLine();
1463 }
1464
1465 // If this function was instantiated from a function template,
1466 // check whether that function template was defined out-of-line.
1467 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1468 const FunctionDecl *Definition;
1469 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1470 return Definition->isOutOfLine();
1471 }
1472
1473 return false;
1474}
1475
Chris Lattner8a934232008-03-31 00:36:02 +00001476//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001477// FieldDecl Implementation
1478//===----------------------------------------------------------------------===//
1479
1480FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1481 IdentifierInfo *Id, QualType T,
1482 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1483 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1484}
1485
1486bool FieldDecl::isAnonymousStructOrUnion() const {
1487 if (!isImplicit() || getDeclName())
1488 return false;
1489
1490 if (const RecordType *Record = getType()->getAs<RecordType>())
1491 return Record->getDecl()->isAnonymousStructOrUnion();
1492
1493 return false;
1494}
1495
1496//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001497// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001498//===----------------------------------------------------------------------===//
1499
John McCallb6217662010-03-15 10:12:16 +00001500void TagDecl::Destroy(ASTContext &C) {
1501 if (hasExtInfo())
1502 C.Deallocate(getExtInfo());
1503 TypeDecl::Destroy(C);
1504}
1505
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001506SourceRange TagDecl::getSourceRange() const {
1507 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001508 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001509}
1510
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001511TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001512 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001513}
1514
Douglas Gregor60e70642010-05-19 18:39:18 +00001515void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1516 TypedefDeclOrQualifier = TDD;
1517 if (TypeForDecl)
1518 TypeForDecl->ClearLinkageCache();
1519}
1520
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001521void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001522 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1523 TagT->decl.setPointer(this);
1524 TagT->decl.setInt(1);
Douglas Gregor9ffce212010-04-30 04:39:27 +00001525 } else if (InjectedClassNameType *Injected
1526 = const_cast<InjectedClassNameType *>(
1527 TypeForDecl->getAs<InjectedClassNameType>())) {
1528 Injected->Decl = cast<CXXRecordDecl>(this);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001529 }
John McCall86ff3082010-02-04 22:26:26 +00001530
1531 if (isa<CXXRecordDecl>(this)) {
1532 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1533 struct CXXRecordDecl::DefinitionData *Data =
1534 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001535 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1536 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001537 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001538}
1539
1540void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001541 assert((!isa<CXXRecordDecl>(this) ||
1542 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1543 "definition completed but not started");
1544
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001545 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001546 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1547 assert(TagT->decl.getPointer() == this &&
1548 "Attempt to redefine a tag definition?");
1549 TagT->decl.setInt(0);
Douglas Gregor9ffce212010-04-30 04:39:27 +00001550 } else if (InjectedClassNameType *Injected
1551 = const_cast<InjectedClassNameType *>(
1552 TypeForDecl->getAs<InjectedClassNameType>())) {
1553 assert(Injected->Decl == this &&
1554 "Attempt to redefine a class template definition?");
Chandler Carrutha8426972010-05-06 05:28:42 +00001555 (void)Injected;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001556 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001557}
1558
Douglas Gregor952b0172010-02-11 01:04:33 +00001559TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001560 if (isDefinition())
1561 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
1563 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001564 R != REnd; ++R)
1565 if (R->isDefinition())
1566 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001568 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001569}
1570
John McCallb6217662010-03-15 10:12:16 +00001571void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1572 SourceRange QualifierRange) {
1573 if (Qualifier) {
1574 // Make sure the extended qualifier info is allocated.
1575 if (!hasExtInfo())
1576 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1577 // Set qualifier info.
1578 getExtInfo()->NNS = Qualifier;
1579 getExtInfo()->NNSRange = QualifierRange;
1580 }
1581 else {
1582 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1583 assert(QualifierRange.isInvalid());
1584 if (hasExtInfo()) {
1585 getASTContext().Deallocate(getExtInfo());
1586 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1587 }
1588 }
1589}
1590
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001591//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001592// EnumDecl Implementation
1593//===----------------------------------------------------------------------===//
1594
1595EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1596 IdentifierInfo *Id, SourceLocation TKL,
1597 EnumDecl *PrevDecl) {
1598 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1599 C.getTypeDeclType(Enum, PrevDecl);
1600 return Enum;
1601}
1602
1603void EnumDecl::Destroy(ASTContext& C) {
John McCallb6217662010-03-15 10:12:16 +00001604 TagDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001605}
1606
Douglas Gregor838db382010-02-11 01:19:42 +00001607void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001608 QualType NewPromotionType,
1609 unsigned NumPositiveBits,
1610 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001611 assert(!isDefinition() && "Cannot redefine enums!");
1612 IntegerType = NewType;
1613 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001614 setNumPositiveBits(NumPositiveBits);
1615 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001616 TagDecl::completeDefinition();
1617}
1618
1619//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001620// RecordDecl Implementation
1621//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001622
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001623RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001624 IdentifierInfo *Id, RecordDecl *PrevDecl,
1625 SourceLocation TKL)
1626 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001627 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001628 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001629 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001630 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001631}
1632
1633RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001634 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001635 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001637 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001638 C.getTypeDeclType(R, PrevDecl);
1639 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001640}
1641
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001642RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001643}
1644
1645void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001646 TagDecl::Destroy(C);
1647}
1648
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001649bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001650 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001651 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1652}
1653
Douglas Gregor44b43212008-12-11 16:49:14 +00001654/// completeDefinition - Notes that the definition of this type is now
1655/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001656void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001658 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001659}
1660
John McCallbc365c52010-05-21 01:17:40 +00001661ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1662 // Force the decl chain to come into existence properly.
1663 if (!getNextDeclInContext()) getParent()->decls_begin();
1664
1665 assert(isAnonymousStructOrUnion());
1666 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1667 assert(D->getType()->isRecordType());
1668 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1669 return D;
1670}
1671
Steve Naroff56ee6892008-10-08 17:01:13 +00001672//===----------------------------------------------------------------------===//
1673// BlockDecl Implementation
1674//===----------------------------------------------------------------------===//
1675
1676BlockDecl::~BlockDecl() {
1677}
1678
1679void BlockDecl::Destroy(ASTContext& C) {
1680 if (Body)
1681 Body->Destroy(C);
1682
1683 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1684 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001685
1686 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001687 Decl::Destroy(C);
1688}
Steve Naroffe78b8092009-03-13 16:56:44 +00001689
Douglas Gregor838db382010-02-11 01:19:42 +00001690void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001691 unsigned NParms) {
1692 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Steve Naroffe78b8092009-03-13 16:56:44 +00001694 // Zero params -> null pointer.
1695 if (NParms) {
1696 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001697 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001698 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1699 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1700 }
1701}
1702
1703unsigned BlockDecl::getNumParams() const {
1704 return NumParams;
1705}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001706
1707
1708//===----------------------------------------------------------------------===//
1709// Other Decl Allocation/Deallocation Method Implementations
1710//===----------------------------------------------------------------------===//
1711
1712TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1713 return new (C) TranslationUnitDecl(C);
1714}
1715
1716NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1717 SourceLocation L, IdentifierInfo *Id) {
1718 return new (C) NamespaceDecl(DC, L, Id);
1719}
1720
1721void NamespaceDecl::Destroy(ASTContext& C) {
1722 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1723 // together. They are all top-level Decls.
1724
1725 this->~NamespaceDecl();
John McCallb6217662010-03-15 10:12:16 +00001726 Decl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001727}
1728
1729
1730ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1731 SourceLocation L, IdentifierInfo *Id, QualType T) {
1732 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1733}
1734
1735FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1736 SourceLocation L,
1737 DeclarationName N, QualType T,
1738 TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001739 StorageClass S, StorageClass SCAsWritten,
1740 bool isInline, bool hasWrittenPrototype) {
1741 FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
1742 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001743 New->HasWrittenPrototype = hasWrittenPrototype;
1744 return New;
1745}
1746
1747BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1748 return new (C) BlockDecl(DC, L);
1749}
1750
1751EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1752 SourceLocation L,
1753 IdentifierInfo *Id, QualType T,
1754 Expr *E, const llvm::APSInt &V) {
1755 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1756}
1757
1758void EnumConstantDecl::Destroy(ASTContext& C) {
1759 if (Init) Init->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001760 ValueDecl::Destroy(C);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001761}
1762
1763TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1764 SourceLocation L, IdentifierInfo *Id,
1765 TypeSourceInfo *TInfo) {
1766 return new (C) TypedefDecl(DC, L, Id, TInfo);
1767}
1768
1769// Anchor TypedefDecl's vtable here.
1770TypedefDecl::~TypedefDecl() {}
1771
1772FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1773 SourceLocation L,
1774 StringLiteral *Str) {
1775 return new (C) FileScopeAsmDecl(DC, L, Str);
1776}