blob: 46f7117f2ce6684ef1fe1f61e4abf19db388a533 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000026#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2ada0482009-02-04 17:27:36 +000028#include <vector>
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Chris Lattner9631e182009-03-04 06:34:08 +000032void Attr::Destroy(ASTContext &C) {
33 if (Next) {
34 Next->Destroy(C);
35 Next = 0;
36 }
37 this->~Attr();
38 C.Deallocate((void*)this);
39}
40
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000041/// \brief Return the TypeLoc wrapper for the type source info.
John McCallbcd03502009-12-07 02:54:59 +000042TypeLoc TypeSourceInfo::getTypeLoc() const {
Argyrios Kyrtzidis1b7c4ca2009-09-29 19:40:20 +000043 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000044}
Chris Lattner9631e182009-03-04 06:34:08 +000045
Chris Lattner88f70d62008-03-15 05:43:15 +000046//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000047// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000048//===----------------------------------------------------------------------===//
49
Douglas Gregorf73b2822009-11-25 22:24:25 +000050static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
51 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
52 "Not a name having namespace scope");
53 ASTContext &Context = D->getASTContext();
54
55 // C++ [basic.link]p3:
56 // A name having namespace scope (3.3.6) has internal linkage if it
57 // is the name of
58 // - an object, reference, function or function template that is
59 // explicitly declared static; or,
60 // (This bullet corresponds to C99 6.2.2p3.)
61 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
62 // Explicitly declared static.
63 if (Var->getStorageClass() == VarDecl::Static)
64 return NamedDecl::InternalLinkage;
65
66 // - an object or reference that is explicitly declared const
67 // and neither explicitly declared extern nor previously
68 // declared to have external linkage; or
69 // (there is no equivalent in C99)
70 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +000071 Var->getType().isConstant(Context) &&
Douglas Gregorf73b2822009-11-25 22:24:25 +000072 Var->getStorageClass() != VarDecl::Extern &&
73 Var->getStorageClass() != VarDecl::PrivateExtern) {
74 bool FoundExtern = false;
75 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
76 PrevVar && !FoundExtern;
77 PrevVar = PrevVar->getPreviousDeclaration())
78 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
79 FoundExtern = true;
80
81 if (!FoundExtern)
82 return NamedDecl::InternalLinkage;
83 }
84 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
85 const FunctionDecl *Function = 0;
86 if (const FunctionTemplateDecl *FunTmpl
87 = dyn_cast<FunctionTemplateDecl>(D))
88 Function = FunTmpl->getTemplatedDecl();
89 else
90 Function = cast<FunctionDecl>(D);
91
92 // Explicitly declared static.
93 if (Function->getStorageClass() == FunctionDecl::Static)
94 return NamedDecl::InternalLinkage;
95 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
96 // - a data member of an anonymous union.
97 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
98 return NamedDecl::InternalLinkage;
99 }
100
101 // C++ [basic.link]p4:
102
103 // A name having namespace scope has external linkage if it is the
104 // name of
105 //
106 // - an object or reference, unless it has internal linkage; or
107 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
108 if (!Context.getLangOptions().CPlusPlus &&
109 (Var->getStorageClass() == VarDecl::Extern ||
110 Var->getStorageClass() == VarDecl::PrivateExtern)) {
111 // C99 6.2.2p4:
112 // For an identifier declared with the storage-class specifier
113 // extern in a scope in which a prior declaration of that
114 // identifier is visible, if the prior declaration specifies
115 // internal or external linkage, the linkage of the identifier
116 // at the later declaration is the same as the linkage
117 // specified at the prior declaration. If no prior declaration
118 // is visible, or if the prior declaration specifies no
119 // linkage, then the identifier has external linkage.
120 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
121 if (NamedDecl::Linkage L = PrevVar->getLinkage())
122 return L;
123 }
124 }
125
126 // C99 6.2.2p5:
127 // If the declaration of an identifier for an object has file
128 // scope and no storage-class specifier, its linkage is
129 // external.
130 return NamedDecl::ExternalLinkage;
131 }
132
133 // - a function, unless it has internal linkage; or
134 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
135 // C99 6.2.2p5:
136 // If the declaration of an identifier for a function has no
137 // storage-class specifier, its linkage is determined exactly
138 // as if it were declared with the storage-class specifier
139 // extern.
140 if (!Context.getLangOptions().CPlusPlus &&
141 (Function->getStorageClass() == FunctionDecl::Extern ||
142 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
143 Function->getStorageClass() == FunctionDecl::None)) {
144 // C99 6.2.2p4:
145 // For an identifier declared with the storage-class specifier
146 // extern in a scope in which a prior declaration of that
147 // identifier is visible, if the prior declaration specifies
148 // internal or external linkage, the linkage of the identifier
149 // at the later declaration is the same as the linkage
150 // specified at the prior declaration. If no prior declaration
151 // is visible, or if the prior declaration specifies no
152 // linkage, then the identifier has external linkage.
153 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
154 if (NamedDecl::Linkage L = PrevFunc->getLinkage())
155 return L;
156 }
157 }
158
159 return NamedDecl::ExternalLinkage;
160 }
161
162 // - a named class (Clause 9), or an unnamed class defined in a
163 // typedef declaration in which the class has the typedef name
164 // for linkage purposes (7.1.3); or
165 // - a named enumeration (7.2), or an unnamed enumeration
166 // defined in a typedef declaration in which the enumeration
167 // has the typedef name for linkage purposes (7.1.3); or
168 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
169 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
170 return NamedDecl::ExternalLinkage;
171
172 // - an enumerator belonging to an enumeration with external linkage;
173 if (isa<EnumConstantDecl>(D))
174 if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
175 == NamedDecl::ExternalLinkage)
176 return NamedDecl::ExternalLinkage;
177
178 // - a template, unless it is a function template that has
179 // internal linkage (Clause 14);
180 if (isa<TemplateDecl>(D))
181 return NamedDecl::ExternalLinkage;
182
183 // - a namespace (7.3), unless it is declared within an unnamed
184 // namespace.
185 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
186 return NamedDecl::ExternalLinkage;
187
188 return NamedDecl::NoLinkage;
189}
190
191NamedDecl::Linkage NamedDecl::getLinkage() const {
192 // Handle linkage for namespace-scope names.
193 if (getDeclContext()->getLookupContext()->isFileContext())
194 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
195 return L;
196
197 // C++ [basic.link]p5:
198 // In addition, a member function, static data member, a named
199 // class or enumeration of class scope, or an unnamed class or
200 // enumeration defined in a class-scope typedef declaration such
201 // that the class or enumeration has the typedef name for linkage
202 // purposes (7.1.3), has external linkage if the name of the class
203 // has external linkage.
204 if (getDeclContext()->isRecord() &&
205 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
206 (isa<TagDecl>(this) &&
207 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
208 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
209 return ExternalLinkage;
210
211 // C++ [basic.link]p6:
212 // The name of a function declared in block scope and the name of
213 // an object declared by a block scope extern declaration have
214 // linkage. If there is a visible declaration of an entity with
215 // linkage having the same name and type, ignoring entities
216 // declared outside the innermost enclosing namespace scope, the
217 // block scope declaration declares that same entity and receives
218 // the linkage of the previous declaration. If there is more than
219 // one such matching entity, the program is ill-formed. Otherwise,
220 // if no matching entity is found, the block scope entity receives
221 // external linkage.
222 if (getLexicalDeclContext()->isFunctionOrMethod()) {
223 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
224 if (Function->getPreviousDeclaration())
225 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
226 return L;
227
228 return ExternalLinkage;
229 }
230
231 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
232 if (Var->getStorageClass() == VarDecl::Extern ||
233 Var->getStorageClass() == VarDecl::PrivateExtern) {
234 if (Var->getPreviousDeclaration())
235 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
236 return L;
237
238 return ExternalLinkage;
239 }
240 }
241
242 // C++ [basic.link]p6:
243 // Names not covered by these rules have no linkage.
244 return NoLinkage;
245}
246
Douglas Gregor2ada0482009-02-04 17:27:36 +0000247std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000248 return getQualifiedNameAsString(getASTContext().getLangOptions());
249}
250
251std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000252 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
253 // std::string thrashing.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000254 std::vector<std::string> Names;
255 std::string QualName;
256 const DeclContext *Ctx = getDeclContext();
257
258 if (Ctx->isFunctionOrMethod())
259 return getNameAsString();
260
261 while (Ctx) {
Mike Stump11289f42009-09-09 15:08:12 +0000262 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000263 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
264 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
265 std::string TemplateArgsStr
266 = TemplateSpecializationType::PrintTemplateArgumentList(
267 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000268 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000269 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000270 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig07d211e2009-12-24 23:15:03 +0000271 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
272 if (ND->isAnonymousNamespace())
273 Names.push_back("<anonymous namespace>");
274 else
275 Names.push_back(ND->getNameAsString());
276 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
277 if (!RD->getIdentifier()) {
278 std::string RecordString = "<anonymous ";
279 RecordString += RD->getKindName();
280 RecordString += ">";
281 Names.push_back(RecordString);
282 } else {
283 Names.push_back(RD->getNameAsString());
284 }
Sam Weinigb999f682009-12-28 03:19:38 +0000285 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
286 std::string Proto = FD->getNameAsString();
287
288 const FunctionProtoType *FT = 0;
289 if (FD->hasWrittenPrototype())
290 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
291
292 Proto += "(";
293 if (FT) {
294 llvm::raw_string_ostream POut(Proto);
295 unsigned NumParams = FD->getNumParams();
296 for (unsigned i = 0; i < NumParams; ++i) {
297 if (i)
298 POut << ", ";
299 std::string Param;
300 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
301 POut << Param;
302 }
303
304 if (FT->isVariadic()) {
305 if (NumParams > 0)
306 POut << ", ";
307 POut << "...";
308 }
309 }
310 Proto += ")";
311
312 Names.push_back(Proto);
Douglas Gregor85673582009-05-18 17:01:57 +0000313 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000314 Names.push_back(ND->getNameAsString());
315 else
316 break;
317
318 Ctx = Ctx->getParent();
319 }
320
321 std::vector<std::string>::reverse_iterator
322 I = Names.rbegin(),
323 End = Names.rend();
324
325 for (; I!=End; ++I)
326 QualName += *I + "::";
327
328 QualName += getNameAsString();
329
330 return QualName;
331}
332
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000333bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000334 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
335
Douglas Gregor889ceb72009-02-03 19:21:40 +0000336 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
337 // We want to keep it, unless it nominates same namespace.
338 if (getKind() == Decl::UsingDirective) {
339 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
340 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
341 }
Mike Stump11289f42009-09-09 15:08:12 +0000342
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000343 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
344 // For function declarations, we keep track of redeclarations.
345 return FD->getPreviousDeclaration() == OldD;
346
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000347 // For function templates, the underlying function declarations are linked.
348 if (const FunctionTemplateDecl *FunctionTemplate
349 = dyn_cast<FunctionTemplateDecl>(this))
350 if (const FunctionTemplateDecl *OldFunctionTemplate
351 = dyn_cast<FunctionTemplateDecl>(OldD))
352 return FunctionTemplate->getTemplatedDecl()
353 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000354
Steve Naroffc4173fa2009-02-22 19:35:57 +0000355 // For method declarations, we keep track of redeclarations.
356 if (isa<ObjCMethodDecl>(this))
357 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000358
John McCall9f3059a2009-10-09 21:13:30 +0000359 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
360 return true;
361
John McCall3f746822009-11-17 05:59:44 +0000362 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
363 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
364 cast<UsingShadowDecl>(OldD)->getTargetDecl();
365
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000366 // For non-function declarations, if the declarations are of the
367 // same kind then this must be a redeclaration, or semantic analysis
368 // would not have given us the new declaration.
369 return this->getKind() == OldD->getKind();
370}
371
Douglas Gregoreddf4332009-02-24 20:03:32 +0000372bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000373 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000374}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000375
Anders Carlsson6915bf62009-06-26 06:29:23 +0000376NamedDecl *NamedDecl::getUnderlyingDecl() {
377 NamedDecl *ND = this;
378 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000379 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000380 ND = UD->getTargetDecl();
381 else if (ObjCCompatibleAliasDecl *AD
382 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
383 return AD->getClassInterface();
384 else
385 return ND;
386 }
387}
388
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000389//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000390// DeclaratorDecl Implementation
391//===----------------------------------------------------------------------===//
392
393SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000394 if (DeclInfo) {
395 TypeLoc TL = DeclInfo->getTypeLoc();
396 while (true) {
397 TypeLoc NextTL = TL.getNextTypeLoc();
398 if (!NextTL)
399 return TL.getSourceRange().getBegin();
400 TL = NextTL;
401 }
402 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000403 return SourceLocation();
404}
405
406//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000407// VarDecl Implementation
408//===----------------------------------------------------------------------===//
409
Sebastian Redl833ef452010-01-26 22:01:41 +0000410const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
411 switch (SC) {
412 case VarDecl::None: break;
413 case VarDecl::Auto: return "auto"; break;
414 case VarDecl::Extern: return "extern"; break;
415 case VarDecl::PrivateExtern: return "__private_extern__"; break;
416 case VarDecl::Register: return "register"; break;
417 case VarDecl::Static: return "static"; break;
418 }
419
420 assert(0 && "Invalid storage class");
421 return 0;
422}
423
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000424VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000425 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000426 StorageClass S) {
John McCallbcd03502009-12-07 02:54:59 +0000427 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000428}
429
430void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000431 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000432 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000433 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000434 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
435 Eval->~EvaluatedStmt();
436 C.Deallocate(Eval);
437 }
438 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000439 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000440 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000441}
442
443VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000444}
445
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000446SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000447 SourceLocation Start = getTypeSpecStartLoc();
448 if (Start.isInvalid())
449 Start = getLocation();
450
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000451 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000452 return SourceRange(Start, getInit()->getLocEnd());
453 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000454}
455
Sebastian Redl833ef452010-01-26 22:01:41 +0000456bool VarDecl::isExternC() const {
457 ASTContext &Context = getASTContext();
458 if (!Context.getLangOptions().CPlusPlus)
459 return (getDeclContext()->isTranslationUnit() &&
460 getStorageClass() != Static) ||
461 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
462
463 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
464 DC = DC->getParent()) {
465 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
466 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
467 return getStorageClass() != Static;
468
469 break;
470 }
471
472 if (DC->isFunctionOrMethod())
473 return false;
474 }
475
476 return false;
477}
478
479VarDecl *VarDecl::getCanonicalDecl() {
480 return getFirstDeclaration();
481}
482
483const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
484 redecl_iterator I = redecls_begin(), E = redecls_end();
485 while (I != E && !I->getInit())
486 ++I;
487
488 if (I != E) {
489 Def = *I;
490 return I->getInit();
491 }
492 return 0;
493}
494
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000495bool VarDecl::isOutOfLine() const {
496 if (!isStaticDataMember())
497 return false;
498
499 if (Decl::isOutOfLine())
500 return true;
501
502 // If this static data member was instantiated from a static data member of
503 // a class template, check whether that static data member was defined
504 // out-of-line.
505 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
506 return VD->isOutOfLine();
507
508 return false;
509}
510
Douglas Gregor1d957a32009-10-27 18:42:08 +0000511VarDecl *VarDecl::getOutOfLineDefinition() {
512 if (!isStaticDataMember())
513 return 0;
514
515 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
516 RD != RDEnd; ++RD) {
517 if (RD->getLexicalDeclContext()->isFileContext())
518 return *RD;
519 }
520
521 return 0;
522}
523
Sebastian Redl833ef452010-01-26 22:01:41 +0000524bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
525 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
526 return false;
527
528 const VarDecl *Def = 0;
529 return (!getDefinition(Def) &&
530 (getStorageClass() == None || getStorageClass() == Static));
531}
532
533void VarDecl::setInit(ASTContext &C, Expr *I) {
534 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
535 Eval->~EvaluatedStmt();
536 C.Deallocate(Eval);
537 }
538
539 Init = I;
540}
541
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000542VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000543 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000544 return cast<VarDecl>(MSI->getInstantiatedFrom());
545
546 return 0;
547}
548
Douglas Gregor3c74d412009-10-14 20:14:33 +0000549TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor86d142a2009-10-08 07:24:58 +0000550 if (MemberSpecializationInfo *MSI
551 = getASTContext().getInstantiatedFromStaticDataMember(this))
552 return MSI->getTemplateSpecializationKind();
553
554 return TSK_Undeclared;
555}
556
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000557MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000558 return getASTContext().getInstantiatedFromStaticDataMember(this);
559}
560
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000561void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
562 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000563 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000564 assert(MSI && "Not an instantiated static data member?");
565 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000566 if (TSK != TSK_ExplicitSpecialization &&
567 PointOfInstantiation.isValid() &&
568 MSI->getPointOfInstantiation().isInvalid())
569 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000570}
571
Sebastian Redl833ef452010-01-26 22:01:41 +0000572//===----------------------------------------------------------------------===//
573// ParmVarDecl Implementation
574//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000575
Sebastian Redl833ef452010-01-26 22:01:41 +0000576ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
577 SourceLocation L, IdentifierInfo *Id,
578 QualType T, TypeSourceInfo *TInfo,
579 StorageClass S, Expr *DefArg) {
580 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000581}
582
Sebastian Redl833ef452010-01-26 22:01:41 +0000583Expr *ParmVarDecl::getDefaultArg() {
584 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
585 assert(!hasUninstantiatedDefaultArg() &&
586 "Default argument is not yet instantiated!");
587
588 Expr *Arg = getInit();
589 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
590 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000591
Sebastian Redl833ef452010-01-26 22:01:41 +0000592 return Arg;
593}
594
595unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
596 if (const CXXExprWithTemporaries *E =
597 dyn_cast<CXXExprWithTemporaries>(getInit()))
598 return E->getNumTemporaries();
599
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000600 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000601}
602
Sebastian Redl833ef452010-01-26 22:01:41 +0000603CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
604 assert(getNumDefaultArgTemporaries() &&
605 "Default arguments does not have any temporaries!");
606
607 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
608 return E->getTemporary(i);
609}
610
611SourceRange ParmVarDecl::getDefaultArgRange() const {
612 if (const Expr *E = getInit())
613 return E->getSourceRange();
614
615 if (hasUninstantiatedDefaultArg())
616 return getUninstantiatedDefaultArg()->getSourceRange();
617
618 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000619}
620
Nuno Lopes394ec982008-12-17 23:39:55 +0000621//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000622// FunctionDecl Implementation
623//===----------------------------------------------------------------------===//
624
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000625void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000626 if (Body && Body.isOffset())
627 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000628
629 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
630 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000631
Douglas Gregord801b062009-10-07 23:56:10 +0000632 FunctionTemplateSpecializationInfo *FTSInfo
633 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
634 if (FTSInfo)
635 C.Deallocate(FTSInfo);
636
637 MemberSpecializationInfo *MSInfo
638 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
639 if (MSInfo)
640 C.Deallocate(MSInfo);
641
Steve Naroff13ae6f42009-01-27 21:25:57 +0000642 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000643
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000644 Decl::Destroy(C);
645}
646
John McCalle1f2ec22009-09-11 06:45:03 +0000647void FunctionDecl::getNameForDiagnostic(std::string &S,
648 const PrintingPolicy &Policy,
649 bool Qualified) const {
650 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
651 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
652 if (TemplateArgs)
653 S += TemplateSpecializationType::PrintTemplateArgumentList(
654 TemplateArgs->getFlatArgumentList(),
655 TemplateArgs->flat_size(),
656 Policy);
657
658}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000659
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000660Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000661 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
662 if (I->Body) {
663 Definition = *I;
664 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000665 }
666 }
667
668 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000669}
670
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000671void FunctionDecl::setBody(Stmt *B) {
672 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000673 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000674 EndRangeLoc = B->getLocEnd();
675}
676
Douglas Gregor16618f22009-09-12 00:17:51 +0000677bool FunctionDecl::isMain() const {
678 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000679 return !Context.getLangOptions().Freestanding &&
680 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000681 getIdentifier() && getIdentifier()->isStr("main");
682}
683
Douglas Gregor16618f22009-09-12 00:17:51 +0000684bool FunctionDecl::isExternC() const {
685 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000686 // In C, any non-static, non-overloadable function has external
687 // linkage.
688 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000689 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000690
Mike Stump11289f42009-09-09 15:08:12 +0000691 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000692 DC = DC->getParent()) {
693 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
694 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000695 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000696 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000697
698 break;
699 }
700 }
701
702 return false;
703}
704
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000705bool FunctionDecl::isGlobal() const {
706 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
707 return Method->isStatic();
708
709 if (getStorageClass() == Static)
710 return false;
711
Mike Stump11289f42009-09-09 15:08:12 +0000712 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000713 DC->isNamespace();
714 DC = DC->getParent()) {
715 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
716 if (!Namespace->getDeclName())
717 return false;
718 break;
719 }
720 }
721
722 return true;
723}
724
Sebastian Redl833ef452010-01-26 22:01:41 +0000725void
726FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
727 redeclarable_base::setPreviousDeclaration(PrevDecl);
728
729 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
730 FunctionTemplateDecl *PrevFunTmpl
731 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
732 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
733 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
734 }
735}
736
737const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
738 return getFirstDeclaration();
739}
740
741FunctionDecl *FunctionDecl::getCanonicalDecl() {
742 return getFirstDeclaration();
743}
744
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000745/// \brief Returns a value indicating whether this function
746/// corresponds to a builtin function.
747///
748/// The function corresponds to a built-in function if it is
749/// declared at translation scope or within an extern "C" block and
750/// its name matches with the name of a builtin. The returned value
751/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000752/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000753/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000754unsigned FunctionDecl::getBuiltinID() const {
755 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000756 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
757 return 0;
758
759 unsigned BuiltinID = getIdentifier()->getBuiltinID();
760 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
761 return BuiltinID;
762
763 // This function has the name of a known C library
764 // function. Determine whether it actually refers to the C library
765 // function or whether it just has the same name.
766
Douglas Gregora908e7f2009-02-17 03:23:10 +0000767 // If this is a static function, it's not a builtin.
768 if (getStorageClass() == Static)
769 return 0;
770
Douglas Gregore711f702009-02-14 18:57:46 +0000771 // If this function is at translation-unit scope and we're not in
772 // C++, it refers to the C library function.
773 if (!Context.getLangOptions().CPlusPlus &&
774 getDeclContext()->isTranslationUnit())
775 return BuiltinID;
776
777 // If the function is in an extern "C" linkage specification and is
778 // not marked "overloadable", it's the real function.
779 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000780 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000781 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000782 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000783 return BuiltinID;
784
785 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000786 return 0;
787}
788
789
Chris Lattner47c0d002009-04-25 06:03:53 +0000790/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000791/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000792/// after it has been created.
793unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000794 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000795 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000796 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000797 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000799}
800
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000801void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
802 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000803 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000804 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000805
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000806 // Zero params -> null pointer.
807 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000808 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000809 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000810 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000811
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000812 // Update source range. The check below allows us to set EndRangeLoc before
813 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000814 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000815 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000816 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000817}
Chris Lattner41943152007-01-25 04:52:46 +0000818
Chris Lattner58258242008-04-10 02:22:51 +0000819/// getMinRequiredArguments - Returns the minimum number of arguments
820/// needed to call this function. This may be fewer than the number of
821/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000822/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000823unsigned FunctionDecl::getMinRequiredArguments() const {
824 unsigned NumRequiredArgs = getNumParams();
825 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000826 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000827 --NumRequiredArgs;
828
829 return NumRequiredArgs;
830}
831
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000832bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000833 // FIXME: This is not enough. Consider:
834 //
835 // inline void f();
836 // void f() { }
837 //
838 // f is inlined, but does not have inline specified.
839 // To fix this we should add an 'inline' flag to FunctionDecl.
840 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000841 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000842
843 if (isa<CXXMethodDecl>(this)) {
844 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
845 return true;
846 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000847
848 switch (getTemplateSpecializationKind()) {
849 case TSK_Undeclared:
850 case TSK_ExplicitSpecialization:
851 return false;
852
853 case TSK_ImplicitInstantiation:
854 case TSK_ExplicitInstantiationDeclaration:
855 case TSK_ExplicitInstantiationDefinition:
856 // Handle below.
857 break;
858 }
859
860 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
861 Stmt *Pattern = 0;
862 if (PatternDecl)
863 Pattern = PatternDecl->getBody(PatternDecl);
864
865 if (Pattern && PatternDecl)
866 return PatternDecl->isInlined();
867
868 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000869}
870
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000871/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000872/// definition will be externally visible.
873///
874/// Inline function definitions are always available for inlining optimizations.
875/// However, depending on the language dialect, declaration specifiers, and
876/// attributes, the definition of an inline function may or may not be
877/// "externally" visible to other translation units in the program.
878///
879/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +0000880/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000881/// inline definition becomes externally visible (C99 6.7.4p6).
882///
883/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
884/// definition, we use the GNU semantics for inline, which are nearly the
885/// opposite of C99 semantics. In particular, "inline" by itself will create
886/// an externally visible symbol, but "extern inline" will not create an
887/// externally visible symbol.
888bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
889 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000890 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000891 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +0000892
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000893 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +0000894 // GNU inline semantics. Based on a number of examples, we came up with the
895 // following heuristic: if the "inline" keyword is present on a
896 // declaration of the function but "extern" is not present on that
897 // declaration, then the symbol is externally visible. Otherwise, the GNU
898 // "extern inline" semantics applies and the symbol is not externally
899 // visible.
900 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
901 Redecl != RedeclEnd;
902 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +0000903 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000904 return true;
905 }
906
907 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000908 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000909 }
910
911 // C99 6.7.4p6:
912 // [...] If all of the file scope declarations for a function in a
913 // translation unit include the inline function specifier without extern,
914 // then the definition in that translation unit is an inline definition.
915 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
916 Redecl != RedeclEnd;
917 ++Redecl) {
918 // Only consider file-scope declarations in this test.
919 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
920 continue;
921
Douglas Gregor35b57532009-10-27 21:01:01 +0000922 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000923 return true; // Not an inline definition
924 }
925
926 // C99 6.7.4p6:
927 // An inline definition does not provide an external definition for the
928 // function, and does not forbid an external definition in another
929 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000930 return false;
931}
932
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000933/// getOverloadedOperator - Which C++ overloaded operator this
934/// function represents, if any.
935OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000936 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
937 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000938 else
939 return OO_None;
940}
941
Alexis Huntc88db062010-01-13 09:01:02 +0000942/// getLiteralIdentifier - The literal suffix identifier this function
943/// represents, if any.
944const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
945 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
946 return getDeclName().getCXXLiteralIdentifier();
947 else
948 return 0;
949}
950
Douglas Gregord801b062009-10-07 23:56:10 +0000951FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000952 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +0000953 return cast<FunctionDecl>(Info->getInstantiatedFrom());
954
955 return 0;
956}
957
Douglas Gregor06db9f52009-10-12 20:18:28 +0000958MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
959 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
960}
961
Douglas Gregord801b062009-10-07 23:56:10 +0000962void
963FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
964 TemplateSpecializationKind TSK) {
965 assert(TemplateOrSpecialization.isNull() &&
966 "Member function is already a specialization");
967 MemberSpecializationInfo *Info
968 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
969 TemplateOrSpecialization = Info;
970}
971
Douglas Gregorafca3b42009-10-27 20:53:28 +0000972bool FunctionDecl::isImplicitlyInstantiable() const {
973 // If this function already has a definition or is invalid, it can't be
974 // implicitly instantiated.
975 if (isInvalidDecl() || getBody())
976 return false;
977
978 switch (getTemplateSpecializationKind()) {
979 case TSK_Undeclared:
980 case TSK_ExplicitSpecialization:
981 case TSK_ExplicitInstantiationDefinition:
982 return false;
983
984 case TSK_ImplicitInstantiation:
985 return true;
986
987 case TSK_ExplicitInstantiationDeclaration:
988 // Handled below.
989 break;
990 }
991
992 // Find the actual template from which we will instantiate.
993 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
994 Stmt *Pattern = 0;
995 if (PatternDecl)
996 Pattern = PatternDecl->getBody(PatternDecl);
997
998 // C++0x [temp.explicit]p9:
999 // Except for inline functions, other explicit instantiation declarations
1000 // have the effect of suppressing the implicit instantiation of the entity
1001 // to which they refer.
1002 if (!Pattern || !PatternDecl)
1003 return true;
1004
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001005 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001006}
1007
1008FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1009 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1010 while (Primary->getInstantiatedFromMemberTemplate()) {
1011 // If we have hit a point where the user provided a specialization of
1012 // this template, we're done looking.
1013 if (Primary->isMemberSpecialization())
1014 break;
1015
1016 Primary = Primary->getInstantiatedFromMemberTemplate();
1017 }
1018
1019 return Primary->getTemplatedDecl();
1020 }
1021
1022 return getInstantiatedFromMemberFunction();
1023}
1024
Douglas Gregor70d83e22009-06-29 17:30:29 +00001025FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001026 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001027 = TemplateOrSpecialization
1028 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001029 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001030 }
1031 return 0;
1032}
1033
1034const TemplateArgumentList *
1035FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001036 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001037 = TemplateOrSpecialization
1038 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001039 return Info->TemplateArguments;
1040 }
1041 return 0;
1042}
1043
Mike Stump11289f42009-09-09 15:08:12 +00001044void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001045FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1046 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001047 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001048 void *InsertPos,
1049 TemplateSpecializationKind TSK) {
1050 assert(TSK != TSK_Undeclared &&
1051 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001052 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001053 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001054 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +00001055 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001056
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001057 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001058 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001059 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001060 Info->TemplateArguments = TemplateArgs;
1061 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001062
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001063 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001064 // function template specializations.
1065 if (InsertPos)
1066 Template->getSpecializations().InsertNode(Info, InsertPos);
1067 else {
1068 // Try to insert the new node. If there is an existing node, remove it
1069 // first.
1070 FunctionTemplateSpecializationInfo *Existing
1071 = Template->getSpecializations().GetOrInsertNode(Info);
1072 if (Existing) {
1073 Template->getSpecializations().RemoveNode(Existing);
1074 Template->getSpecializations().GetOrInsertNode(Info);
1075 }
1076 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001077}
1078
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001079TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001080 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001081 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001082 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001083 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001084 if (FTSInfo)
1085 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001086
Douglas Gregord801b062009-10-07 23:56:10 +00001087 MemberSpecializationInfo *MSInfo
1088 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1089 if (MSInfo)
1090 return MSInfo->getTemplateSpecializationKind();
1091
1092 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001093}
1094
Mike Stump11289f42009-09-09 15:08:12 +00001095void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001096FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1097 SourceLocation PointOfInstantiation) {
1098 if (FunctionTemplateSpecializationInfo *FTSInfo
1099 = TemplateOrSpecialization.dyn_cast<
1100 FunctionTemplateSpecializationInfo*>()) {
1101 FTSInfo->setTemplateSpecializationKind(TSK);
1102 if (TSK != TSK_ExplicitSpecialization &&
1103 PointOfInstantiation.isValid() &&
1104 FTSInfo->getPointOfInstantiation().isInvalid())
1105 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1106 } else if (MemberSpecializationInfo *MSInfo
1107 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1108 MSInfo->setTemplateSpecializationKind(TSK);
1109 if (TSK != TSK_ExplicitSpecialization &&
1110 PointOfInstantiation.isValid() &&
1111 MSInfo->getPointOfInstantiation().isInvalid())
1112 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1113 } else
1114 assert(false && "Function cannot have a template specialization kind");
1115}
1116
1117SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001118 if (FunctionTemplateSpecializationInfo *FTSInfo
1119 = TemplateOrSpecialization.dyn_cast<
1120 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001121 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001122 else if (MemberSpecializationInfo *MSInfo
1123 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001124 return MSInfo->getPointOfInstantiation();
1125
1126 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001127}
1128
Douglas Gregor6411b922009-09-11 20:15:17 +00001129bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001130 if (Decl::isOutOfLine())
1131 return true;
1132
1133 // If this function was instantiated from a member function of a
1134 // class template, check whether that member function was defined out-of-line.
1135 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1136 const FunctionDecl *Definition;
1137 if (FD->getBody(Definition))
1138 return Definition->isOutOfLine();
1139 }
1140
1141 // If this function was instantiated from a function template,
1142 // check whether that function template was defined out-of-line.
1143 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1144 const FunctionDecl *Definition;
1145 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1146 return Definition->isOutOfLine();
1147 }
1148
1149 return false;
1150}
1151
Chris Lattner59a25942008-03-31 00:36:02 +00001152//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001153// FieldDecl Implementation
1154//===----------------------------------------------------------------------===//
1155
1156FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1157 IdentifierInfo *Id, QualType T,
1158 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1159 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1160}
1161
1162bool FieldDecl::isAnonymousStructOrUnion() const {
1163 if (!isImplicit() || getDeclName())
1164 return false;
1165
1166 if (const RecordType *Record = getType()->getAs<RecordType>())
1167 return Record->getDecl()->isAnonymousStructOrUnion();
1168
1169 return false;
1170}
1171
1172//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001173// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001174//===----------------------------------------------------------------------===//
1175
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001176SourceRange TagDecl::getSourceRange() const {
1177 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001178 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001179}
1180
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001181TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001182 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001183}
1184
Douglas Gregordee1be82009-01-17 00:42:38 +00001185void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001186 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1187 TagT->decl.setPointer(this);
1188 TagT->decl.setInt(1);
1189 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001190}
1191
1192void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +00001193 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001194 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1195 assert(TagT->decl.getPointer() == this &&
1196 "Attempt to redefine a tag definition?");
1197 TagT->decl.setInt(0);
1198 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001199}
1200
Ted Kremenek21475702008-09-05 17:16:31 +00001201TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001202 if (isDefinition())
1203 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001204
1205 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001206 R != REnd; ++R)
1207 if (R->isDefinition())
1208 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001209
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001210 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001211}
1212
John McCall06f6fe8d2009-09-04 01:14:41 +00001213TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1214 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001215 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001216 case DeclSpec::TST_struct: return TK_struct;
1217 case DeclSpec::TST_class: return TK_class;
1218 case DeclSpec::TST_union: return TK_union;
1219 case DeclSpec::TST_enum: return TK_enum;
1220 }
1221}
1222
Ted Kremenek21475702008-09-05 17:16:31 +00001223//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001224// EnumDecl Implementation
1225//===----------------------------------------------------------------------===//
1226
1227EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1228 IdentifierInfo *Id, SourceLocation TKL,
1229 EnumDecl *PrevDecl) {
1230 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1231 C.getTypeDeclType(Enum, PrevDecl);
1232 return Enum;
1233}
1234
1235void EnumDecl::Destroy(ASTContext& C) {
1236 Decl::Destroy(C);
1237}
1238
1239void EnumDecl::completeDefinition(ASTContext &C,
1240 QualType NewType,
1241 QualType NewPromotionType) {
1242 assert(!isDefinition() && "Cannot redefine enums!");
1243 IntegerType = NewType;
1244 PromotionType = NewPromotionType;
1245 TagDecl::completeDefinition();
1246}
1247
1248//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001249// RecordDecl Implementation
1250//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001251
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001252RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001253 IdentifierInfo *Id, RecordDecl *PrevDecl,
1254 SourceLocation TKL)
1255 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001256 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001257 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001258 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001259 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001260}
1261
1262RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001263 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001264 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001265
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001266 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001267 C.getTypeDeclType(R, PrevDecl);
1268 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001269}
1270
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001271RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001272}
1273
1274void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001275 TagDecl::Destroy(C);
1276}
1277
Douglas Gregordfcad112009-03-25 15:59:44 +00001278bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001279 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001280 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1281}
1282
Douglas Gregor91f84212008-12-11 16:49:14 +00001283/// completeDefinition - Notes that the definition of this type is now
1284/// complete.
1285void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +00001286 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001287 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001288}
Steve Naroffcc321422007-03-26 23:09:51 +00001289
Steve Naroff415d3d52008-10-08 17:01:13 +00001290//===----------------------------------------------------------------------===//
1291// BlockDecl Implementation
1292//===----------------------------------------------------------------------===//
1293
1294BlockDecl::~BlockDecl() {
1295}
1296
1297void BlockDecl::Destroy(ASTContext& C) {
1298 if (Body)
1299 Body->Destroy(C);
1300
1301 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1302 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001303
1304 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001305 Decl::Destroy(C);
1306}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001307
1308void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1309 unsigned NParms) {
1310 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001311
Steve Naroffc4b30e52009-03-13 16:56:44 +00001312 // Zero params -> null pointer.
1313 if (NParms) {
1314 NumParams = NParms;
1315 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1316 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1317 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1318 }
1319}
1320
1321unsigned BlockDecl::getNumParams() const {
1322 return NumParams;
1323}
Sebastian Redl833ef452010-01-26 22:01:41 +00001324
1325
1326//===----------------------------------------------------------------------===//
1327// Other Decl Allocation/Deallocation Method Implementations
1328//===----------------------------------------------------------------------===//
1329
1330TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1331 return new (C) TranslationUnitDecl(C);
1332}
1333
1334NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1335 SourceLocation L, IdentifierInfo *Id) {
1336 return new (C) NamespaceDecl(DC, L, Id);
1337}
1338
1339void NamespaceDecl::Destroy(ASTContext& C) {
1340 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1341 // together. They are all top-level Decls.
1342
1343 this->~NamespaceDecl();
1344 C.Deallocate((void *)this);
1345}
1346
1347
1348ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1349 SourceLocation L, IdentifierInfo *Id, QualType T) {
1350 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1351}
1352
1353FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1354 SourceLocation L,
1355 DeclarationName N, QualType T,
1356 TypeSourceInfo *TInfo,
1357 StorageClass S, bool isInline,
1358 bool hasWrittenPrototype) {
1359 FunctionDecl *New
1360 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
1361 New->HasWrittenPrototype = hasWrittenPrototype;
1362 return New;
1363}
1364
1365BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1366 return new (C) BlockDecl(DC, L);
1367}
1368
1369EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1370 SourceLocation L,
1371 IdentifierInfo *Id, QualType T,
1372 Expr *E, const llvm::APSInt &V) {
1373 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1374}
1375
1376void EnumConstantDecl::Destroy(ASTContext& C) {
1377 if (Init) Init->Destroy(C);
1378 Decl::Destroy(C);
1379}
1380
1381TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1382 SourceLocation L, IdentifierInfo *Id,
1383 TypeSourceInfo *TInfo) {
1384 return new (C) TypedefDecl(DC, L, Id, TInfo);
1385}
1386
1387// Anchor TypedefDecl's vtable here.
1388TypedefDecl::~TypedefDecl() {}
1389
1390FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1391 SourceLocation L,
1392 StringLiteral *Str) {
1393 return new (C) FileScopeAsmDecl(DC, L, Str);
1394}