blob: a2cf73849d51efe75d92184655a9c264b1733804 [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
Sebastian Redl35351a92010-01-31 22:27:38 +0000483VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
484 // C++ [basic.def]p2:
485 // A declaration is a definition unless [...] it contains the 'extern'
486 // specifier or a linkage-specification and neither an initializer [...],
487 // it declares a static data member in a class declaration [...].
488 // C++ [temp.expl.spec]p15:
489 // An explicit specialization of a static data member of a template is a
490 // definition if the declaration includes an initializer; otherwise, it is
491 // a declaration.
492 if (isStaticDataMember()) {
493 if (isOutOfLine() && (hasInit() ||
494 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
495 return Definition;
496 else
497 return DeclarationOnly;
498 }
499 // C99 6.7p5:
500 // A definition of an identifier is a declaration for that identifier that
501 // [...] causes storage to be reserved for that object.
502 // Note: that applies for all non-file-scope objects.
503 // C99 6.9.2p1:
504 // If the declaration of an identifier for an object has file scope and an
505 // initializer, the declaration is an external definition for the identifier
506 if (hasInit())
507 return Definition;
508 // AST for 'extern "C" int foo;' is annotated with 'extern'.
509 if (hasExternalStorage())
510 return DeclarationOnly;
511
512 // C99 6.9.2p2:
513 // A declaration of an object that has file scope without an initializer,
514 // and without a storage class specifier or the scs 'static', constitutes
515 // a tentative definition.
516 // No such thing in C++.
517 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
518 return TentativeDefinition;
519
520 // What's left is (in C, block-scope) declarations without initializers or
521 // external storage. These are definitions.
522 return Definition;
523}
524
Sebastian Redl35351a92010-01-31 22:27:38 +0000525VarDecl *VarDecl::getActingDefinition() {
526 DefinitionKind Kind = isThisDeclarationADefinition();
527 if (Kind != TentativeDefinition)
528 return 0;
529
530 VarDecl *LastTentative = false;
531 VarDecl *First = getFirstDeclaration();
532 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
533 I != E; ++I) {
534 Kind = (*I)->isThisDeclarationADefinition();
535 if (Kind == Definition)
536 return 0;
537 else if (Kind == TentativeDefinition)
538 LastTentative = *I;
539 }
540 return LastTentative;
541}
542
543bool VarDecl::isTentativeDefinitionNow() const {
544 DefinitionKind Kind = isThisDeclarationADefinition();
545 if (Kind != TentativeDefinition)
546 return false;
547
548 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
549 if ((*I)->isThisDeclarationADefinition() == Definition)
550 return false;
551 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000552 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000553}
554
Sebastian Redl5ca79842010-02-01 20:16:42 +0000555VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000556 VarDecl *First = getFirstDeclaration();
557 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
558 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000559 if ((*I)->isThisDeclarationADefinition() == Definition)
560 return *I;
561 }
562 return 0;
563}
564
565const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000566 redecl_iterator I = redecls_begin(), E = redecls_end();
567 while (I != E && !I->getInit())
568 ++I;
569
570 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000571 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000572 return I->getInit();
573 }
574 return 0;
575}
576
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000577bool VarDecl::isOutOfLine() const {
578 if (!isStaticDataMember())
579 return false;
580
581 if (Decl::isOutOfLine())
582 return true;
583
584 // If this static data member was instantiated from a static data member of
585 // a class template, check whether that static data member was defined
586 // out-of-line.
587 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
588 return VD->isOutOfLine();
589
590 return false;
591}
592
Douglas Gregor1d957a32009-10-27 18:42:08 +0000593VarDecl *VarDecl::getOutOfLineDefinition() {
594 if (!isStaticDataMember())
595 return 0;
596
597 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
598 RD != RDEnd; ++RD) {
599 if (RD->getLexicalDeclContext()->isFileContext())
600 return *RD;
601 }
602
603 return 0;
604}
605
Sebastian Redl833ef452010-01-26 22:01:41 +0000606void VarDecl::setInit(ASTContext &C, Expr *I) {
607 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
608 Eval->~EvaluatedStmt();
609 C.Deallocate(Eval);
610 }
611
612 Init = I;
613}
614
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000615VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000616 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000617 return cast<VarDecl>(MSI->getInstantiatedFrom());
618
619 return 0;
620}
621
Douglas Gregor3c74d412009-10-14 20:14:33 +0000622TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000623 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000624 return MSI->getTemplateSpecializationKind();
625
626 return TSK_Undeclared;
627}
628
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000629MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000630 return getASTContext().getInstantiatedFromStaticDataMember(this);
631}
632
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000633void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
634 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000635 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000636 assert(MSI && "Not an instantiated static data member?");
637 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000638 if (TSK != TSK_ExplicitSpecialization &&
639 PointOfInstantiation.isValid() &&
640 MSI->getPointOfInstantiation().isInvalid())
641 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000642}
643
Sebastian Redl833ef452010-01-26 22:01:41 +0000644//===----------------------------------------------------------------------===//
645// ParmVarDecl Implementation
646//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000647
Sebastian Redl833ef452010-01-26 22:01:41 +0000648ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
649 SourceLocation L, IdentifierInfo *Id,
650 QualType T, TypeSourceInfo *TInfo,
651 StorageClass S, Expr *DefArg) {
652 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000653}
654
Sebastian Redl833ef452010-01-26 22:01:41 +0000655Expr *ParmVarDecl::getDefaultArg() {
656 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
657 assert(!hasUninstantiatedDefaultArg() &&
658 "Default argument is not yet instantiated!");
659
660 Expr *Arg = getInit();
661 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
662 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000663
Sebastian Redl833ef452010-01-26 22:01:41 +0000664 return Arg;
665}
666
667unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
668 if (const CXXExprWithTemporaries *E =
669 dyn_cast<CXXExprWithTemporaries>(getInit()))
670 return E->getNumTemporaries();
671
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000672 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000673}
674
Sebastian Redl833ef452010-01-26 22:01:41 +0000675CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
676 assert(getNumDefaultArgTemporaries() &&
677 "Default arguments does not have any temporaries!");
678
679 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
680 return E->getTemporary(i);
681}
682
683SourceRange ParmVarDecl::getDefaultArgRange() const {
684 if (const Expr *E = getInit())
685 return E->getSourceRange();
686
687 if (hasUninstantiatedDefaultArg())
688 return getUninstantiatedDefaultArg()->getSourceRange();
689
690 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000691}
692
Nuno Lopes394ec982008-12-17 23:39:55 +0000693//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000694// FunctionDecl Implementation
695//===----------------------------------------------------------------------===//
696
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000697void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000698 if (Body && Body.isOffset())
699 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000700
701 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
702 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000703
Douglas Gregord801b062009-10-07 23:56:10 +0000704 FunctionTemplateSpecializationInfo *FTSInfo
705 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
706 if (FTSInfo)
707 C.Deallocate(FTSInfo);
708
709 MemberSpecializationInfo *MSInfo
710 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
711 if (MSInfo)
712 C.Deallocate(MSInfo);
713
Steve Naroff13ae6f42009-01-27 21:25:57 +0000714 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000715
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000716 Decl::Destroy(C);
717}
718
John McCalle1f2ec22009-09-11 06:45:03 +0000719void FunctionDecl::getNameForDiagnostic(std::string &S,
720 const PrintingPolicy &Policy,
721 bool Qualified) const {
722 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
723 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
724 if (TemplateArgs)
725 S += TemplateSpecializationType::PrintTemplateArgumentList(
726 TemplateArgs->getFlatArgumentList(),
727 TemplateArgs->flat_size(),
728 Policy);
729
730}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000731
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000732Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000733 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
734 if (I->Body) {
735 Definition = *I;
736 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000737 }
738 }
739
740 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000741}
742
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000743void FunctionDecl::setBody(Stmt *B) {
744 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000745 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000746 EndRangeLoc = B->getLocEnd();
747}
748
Douglas Gregor16618f22009-09-12 00:17:51 +0000749bool FunctionDecl::isMain() const {
750 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000751 return !Context.getLangOptions().Freestanding &&
752 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000753 getIdentifier() && getIdentifier()->isStr("main");
754}
755
Douglas Gregor16618f22009-09-12 00:17:51 +0000756bool FunctionDecl::isExternC() const {
757 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000758 // In C, any non-static, non-overloadable function has external
759 // linkage.
760 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000761 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000762
Mike Stump11289f42009-09-09 15:08:12 +0000763 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000764 DC = DC->getParent()) {
765 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
766 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000767 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000768 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000769
770 break;
771 }
772 }
773
774 return false;
775}
776
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000777bool FunctionDecl::isGlobal() const {
778 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
779 return Method->isStatic();
780
781 if (getStorageClass() == Static)
782 return false;
783
Mike Stump11289f42009-09-09 15:08:12 +0000784 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000785 DC->isNamespace();
786 DC = DC->getParent()) {
787 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
788 if (!Namespace->getDeclName())
789 return false;
790 break;
791 }
792 }
793
794 return true;
795}
796
Sebastian Redl833ef452010-01-26 22:01:41 +0000797void
798FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
799 redeclarable_base::setPreviousDeclaration(PrevDecl);
800
801 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
802 FunctionTemplateDecl *PrevFunTmpl
803 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
804 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
805 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
806 }
807}
808
809const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
810 return getFirstDeclaration();
811}
812
813FunctionDecl *FunctionDecl::getCanonicalDecl() {
814 return getFirstDeclaration();
815}
816
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000817/// \brief Returns a value indicating whether this function
818/// corresponds to a builtin function.
819///
820/// The function corresponds to a built-in function if it is
821/// declared at translation scope or within an extern "C" block and
822/// its name matches with the name of a builtin. The returned value
823/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000824/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000825/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000826unsigned FunctionDecl::getBuiltinID() const {
827 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000828 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
829 return 0;
830
831 unsigned BuiltinID = getIdentifier()->getBuiltinID();
832 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
833 return BuiltinID;
834
835 // This function has the name of a known C library
836 // function. Determine whether it actually refers to the C library
837 // function or whether it just has the same name.
838
Douglas Gregora908e7f2009-02-17 03:23:10 +0000839 // If this is a static function, it's not a builtin.
840 if (getStorageClass() == Static)
841 return 0;
842
Douglas Gregore711f702009-02-14 18:57:46 +0000843 // If this function is at translation-unit scope and we're not in
844 // C++, it refers to the C library function.
845 if (!Context.getLangOptions().CPlusPlus &&
846 getDeclContext()->isTranslationUnit())
847 return BuiltinID;
848
849 // If the function is in an extern "C" linkage specification and is
850 // not marked "overloadable", it's the real function.
851 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000852 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000853 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000854 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000855 return BuiltinID;
856
857 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000858 return 0;
859}
860
861
Chris Lattner47c0d002009-04-25 06:03:53 +0000862/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000863/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000864/// after it has been created.
865unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000866 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000867 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000868 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000869 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000870
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000871}
872
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000873void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
874 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000875 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000876 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000878 // Zero params -> null pointer.
879 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000880 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000881 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000882 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000883
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000884 // Update source range. The check below allows us to set EndRangeLoc before
885 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000886 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000887 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000888 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000889}
Chris Lattner41943152007-01-25 04:52:46 +0000890
Chris Lattner58258242008-04-10 02:22:51 +0000891/// getMinRequiredArguments - Returns the minimum number of arguments
892/// needed to call this function. This may be fewer than the number of
893/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000894/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000895unsigned FunctionDecl::getMinRequiredArguments() const {
896 unsigned NumRequiredArgs = getNumParams();
897 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000898 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000899 --NumRequiredArgs;
900
901 return NumRequiredArgs;
902}
903
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000904bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000905 // FIXME: This is not enough. Consider:
906 //
907 // inline void f();
908 // void f() { }
909 //
910 // f is inlined, but does not have inline specified.
911 // To fix this we should add an 'inline' flag to FunctionDecl.
912 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000913 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000914
915 if (isa<CXXMethodDecl>(this)) {
916 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
917 return true;
918 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000919
920 switch (getTemplateSpecializationKind()) {
921 case TSK_Undeclared:
922 case TSK_ExplicitSpecialization:
923 return false;
924
925 case TSK_ImplicitInstantiation:
926 case TSK_ExplicitInstantiationDeclaration:
927 case TSK_ExplicitInstantiationDefinition:
928 // Handle below.
929 break;
930 }
931
932 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
933 Stmt *Pattern = 0;
934 if (PatternDecl)
935 Pattern = PatternDecl->getBody(PatternDecl);
936
937 if (Pattern && PatternDecl)
938 return PatternDecl->isInlined();
939
940 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000941}
942
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000943/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000944/// definition will be externally visible.
945///
946/// Inline function definitions are always available for inlining optimizations.
947/// However, depending on the language dialect, declaration specifiers, and
948/// attributes, the definition of an inline function may or may not be
949/// "externally" visible to other translation units in the program.
950///
951/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +0000952/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000953/// inline definition becomes externally visible (C99 6.7.4p6).
954///
955/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
956/// definition, we use the GNU semantics for inline, which are nearly the
957/// opposite of C99 semantics. In particular, "inline" by itself will create
958/// an externally visible symbol, but "extern inline" will not create an
959/// externally visible symbol.
960bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
961 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000962 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000963 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +0000964
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000965 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +0000966 // GNU inline semantics. Based on a number of examples, we came up with the
967 // following heuristic: if the "inline" keyword is present on a
968 // declaration of the function but "extern" is not present on that
969 // declaration, then the symbol is externally visible. Otherwise, the GNU
970 // "extern inline" semantics applies and the symbol is not externally
971 // visible.
972 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
973 Redecl != RedeclEnd;
974 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +0000975 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000976 return true;
977 }
978
979 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000980 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000981 }
982
983 // C99 6.7.4p6:
984 // [...] If all of the file scope declarations for a function in a
985 // translation unit include the inline function specifier without extern,
986 // then the definition in that translation unit is an inline definition.
987 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
988 Redecl != RedeclEnd;
989 ++Redecl) {
990 // Only consider file-scope declarations in this test.
991 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
992 continue;
993
Douglas Gregor35b57532009-10-27 21:01:01 +0000994 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000995 return true; // Not an inline definition
996 }
997
998 // C99 6.7.4p6:
999 // An inline definition does not provide an external definition for the
1000 // function, and does not forbid an external definition in another
1001 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001002 return false;
1003}
1004
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001005/// getOverloadedOperator - Which C++ overloaded operator this
1006/// function represents, if any.
1007OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001008 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1009 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001010 else
1011 return OO_None;
1012}
1013
Alexis Huntc88db062010-01-13 09:01:02 +00001014/// getLiteralIdentifier - The literal suffix identifier this function
1015/// represents, if any.
1016const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1017 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1018 return getDeclName().getCXXLiteralIdentifier();
1019 else
1020 return 0;
1021}
1022
Douglas Gregord801b062009-10-07 23:56:10 +00001023FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001024 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001025 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1026
1027 return 0;
1028}
1029
Douglas Gregor06db9f52009-10-12 20:18:28 +00001030MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1031 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1032}
1033
Douglas Gregord801b062009-10-07 23:56:10 +00001034void
1035FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1036 TemplateSpecializationKind TSK) {
1037 assert(TemplateOrSpecialization.isNull() &&
1038 "Member function is already a specialization");
1039 MemberSpecializationInfo *Info
1040 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1041 TemplateOrSpecialization = Info;
1042}
1043
Douglas Gregorafca3b42009-10-27 20:53:28 +00001044bool FunctionDecl::isImplicitlyInstantiable() const {
1045 // If this function already has a definition or is invalid, it can't be
1046 // implicitly instantiated.
1047 if (isInvalidDecl() || getBody())
1048 return false;
1049
1050 switch (getTemplateSpecializationKind()) {
1051 case TSK_Undeclared:
1052 case TSK_ExplicitSpecialization:
1053 case TSK_ExplicitInstantiationDefinition:
1054 return false;
1055
1056 case TSK_ImplicitInstantiation:
1057 return true;
1058
1059 case TSK_ExplicitInstantiationDeclaration:
1060 // Handled below.
1061 break;
1062 }
1063
1064 // Find the actual template from which we will instantiate.
1065 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1066 Stmt *Pattern = 0;
1067 if (PatternDecl)
1068 Pattern = PatternDecl->getBody(PatternDecl);
1069
1070 // C++0x [temp.explicit]p9:
1071 // Except for inline functions, other explicit instantiation declarations
1072 // have the effect of suppressing the implicit instantiation of the entity
1073 // to which they refer.
1074 if (!Pattern || !PatternDecl)
1075 return true;
1076
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001077 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001078}
1079
1080FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1081 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1082 while (Primary->getInstantiatedFromMemberTemplate()) {
1083 // If we have hit a point where the user provided a specialization of
1084 // this template, we're done looking.
1085 if (Primary->isMemberSpecialization())
1086 break;
1087
1088 Primary = Primary->getInstantiatedFromMemberTemplate();
1089 }
1090
1091 return Primary->getTemplatedDecl();
1092 }
1093
1094 return getInstantiatedFromMemberFunction();
1095}
1096
Douglas Gregor70d83e22009-06-29 17:30:29 +00001097FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001098 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001099 = TemplateOrSpecialization
1100 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001101 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001102 }
1103 return 0;
1104}
1105
1106const TemplateArgumentList *
1107FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001108 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001109 = TemplateOrSpecialization
1110 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001111 return Info->TemplateArguments;
1112 }
1113 return 0;
1114}
1115
Mike Stump11289f42009-09-09 15:08:12 +00001116void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001117FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1118 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001119 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001120 void *InsertPos,
1121 TemplateSpecializationKind TSK) {
1122 assert(TSK != TSK_Undeclared &&
1123 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001124 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001125 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001126 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +00001127 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001128
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001129 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001130 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001131 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001132 Info->TemplateArguments = TemplateArgs;
1133 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001134
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001135 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001136 // function template specializations.
1137 if (InsertPos)
1138 Template->getSpecializations().InsertNode(Info, InsertPos);
1139 else {
1140 // Try to insert the new node. If there is an existing node, remove it
1141 // first.
1142 FunctionTemplateSpecializationInfo *Existing
1143 = Template->getSpecializations().GetOrInsertNode(Info);
1144 if (Existing) {
1145 Template->getSpecializations().RemoveNode(Existing);
1146 Template->getSpecializations().GetOrInsertNode(Info);
1147 }
1148 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001149}
1150
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001151TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001152 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001153 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001154 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001155 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001156 if (FTSInfo)
1157 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001158
Douglas Gregord801b062009-10-07 23:56:10 +00001159 MemberSpecializationInfo *MSInfo
1160 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1161 if (MSInfo)
1162 return MSInfo->getTemplateSpecializationKind();
1163
1164 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001165}
1166
Mike Stump11289f42009-09-09 15:08:12 +00001167void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001168FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1169 SourceLocation PointOfInstantiation) {
1170 if (FunctionTemplateSpecializationInfo *FTSInfo
1171 = TemplateOrSpecialization.dyn_cast<
1172 FunctionTemplateSpecializationInfo*>()) {
1173 FTSInfo->setTemplateSpecializationKind(TSK);
1174 if (TSK != TSK_ExplicitSpecialization &&
1175 PointOfInstantiation.isValid() &&
1176 FTSInfo->getPointOfInstantiation().isInvalid())
1177 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1178 } else if (MemberSpecializationInfo *MSInfo
1179 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1180 MSInfo->setTemplateSpecializationKind(TSK);
1181 if (TSK != TSK_ExplicitSpecialization &&
1182 PointOfInstantiation.isValid() &&
1183 MSInfo->getPointOfInstantiation().isInvalid())
1184 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1185 } else
1186 assert(false && "Function cannot have a template specialization kind");
1187}
1188
1189SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001190 if (FunctionTemplateSpecializationInfo *FTSInfo
1191 = TemplateOrSpecialization.dyn_cast<
1192 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001193 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001194 else if (MemberSpecializationInfo *MSInfo
1195 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001196 return MSInfo->getPointOfInstantiation();
1197
1198 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001199}
1200
Douglas Gregor6411b922009-09-11 20:15:17 +00001201bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001202 if (Decl::isOutOfLine())
1203 return true;
1204
1205 // If this function was instantiated from a member function of a
1206 // class template, check whether that member function was defined out-of-line.
1207 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1208 const FunctionDecl *Definition;
1209 if (FD->getBody(Definition))
1210 return Definition->isOutOfLine();
1211 }
1212
1213 // If this function was instantiated from a function template,
1214 // check whether that function template was defined out-of-line.
1215 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1216 const FunctionDecl *Definition;
1217 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1218 return Definition->isOutOfLine();
1219 }
1220
1221 return false;
1222}
1223
Chris Lattner59a25942008-03-31 00:36:02 +00001224//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001225// FieldDecl Implementation
1226//===----------------------------------------------------------------------===//
1227
1228FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1229 IdentifierInfo *Id, QualType T,
1230 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1231 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1232}
1233
1234bool FieldDecl::isAnonymousStructOrUnion() const {
1235 if (!isImplicit() || getDeclName())
1236 return false;
1237
1238 if (const RecordType *Record = getType()->getAs<RecordType>())
1239 return Record->getDecl()->isAnonymousStructOrUnion();
1240
1241 return false;
1242}
1243
1244//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001245// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001246//===----------------------------------------------------------------------===//
1247
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001248SourceRange TagDecl::getSourceRange() const {
1249 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001250 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001251}
1252
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001253TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001254 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001255}
1256
Douglas Gregordee1be82009-01-17 00:42:38 +00001257void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001258 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1259 TagT->decl.setPointer(this);
1260 TagT->decl.setInt(1);
1261 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001262}
1263
1264void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +00001265 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001266 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1267 assert(TagT->decl.getPointer() == this &&
1268 "Attempt to redefine a tag definition?");
1269 TagT->decl.setInt(0);
1270 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001271}
1272
Ted Kremenek21475702008-09-05 17:16:31 +00001273TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001274 if (isDefinition())
1275 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001276
1277 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001278 R != REnd; ++R)
1279 if (R->isDefinition())
1280 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001282 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001283}
1284
John McCall06f6fe8d2009-09-04 01:14:41 +00001285TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1286 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001287 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001288 case DeclSpec::TST_struct: return TK_struct;
1289 case DeclSpec::TST_class: return TK_class;
1290 case DeclSpec::TST_union: return TK_union;
1291 case DeclSpec::TST_enum: return TK_enum;
1292 }
1293}
1294
Ted Kremenek21475702008-09-05 17:16:31 +00001295//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001296// EnumDecl Implementation
1297//===----------------------------------------------------------------------===//
1298
1299EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1300 IdentifierInfo *Id, SourceLocation TKL,
1301 EnumDecl *PrevDecl) {
1302 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1303 C.getTypeDeclType(Enum, PrevDecl);
1304 return Enum;
1305}
1306
1307void EnumDecl::Destroy(ASTContext& C) {
1308 Decl::Destroy(C);
1309}
1310
1311void EnumDecl::completeDefinition(ASTContext &C,
1312 QualType NewType,
1313 QualType NewPromotionType) {
1314 assert(!isDefinition() && "Cannot redefine enums!");
1315 IntegerType = NewType;
1316 PromotionType = NewPromotionType;
1317 TagDecl::completeDefinition();
1318}
1319
1320//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001321// RecordDecl Implementation
1322//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001323
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001324RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001325 IdentifierInfo *Id, RecordDecl *PrevDecl,
1326 SourceLocation TKL)
1327 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001328 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001329 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001330 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001331 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001332}
1333
1334RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001335 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001336 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001337
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001338 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001339 C.getTypeDeclType(R, PrevDecl);
1340 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001341}
1342
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001343RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001344}
1345
1346void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001347 TagDecl::Destroy(C);
1348}
1349
Douglas Gregordfcad112009-03-25 15:59:44 +00001350bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001351 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001352 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1353}
1354
Douglas Gregor91f84212008-12-11 16:49:14 +00001355/// completeDefinition - Notes that the definition of this type is now
1356/// complete.
1357void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +00001358 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001359 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001360}
Steve Naroffcc321422007-03-26 23:09:51 +00001361
Steve Naroff415d3d52008-10-08 17:01:13 +00001362//===----------------------------------------------------------------------===//
1363// BlockDecl Implementation
1364//===----------------------------------------------------------------------===//
1365
1366BlockDecl::~BlockDecl() {
1367}
1368
1369void BlockDecl::Destroy(ASTContext& C) {
1370 if (Body)
1371 Body->Destroy(C);
1372
1373 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1374 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001375
1376 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001377 Decl::Destroy(C);
1378}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001379
1380void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1381 unsigned NParms) {
1382 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001383
Steve Naroffc4b30e52009-03-13 16:56:44 +00001384 // Zero params -> null pointer.
1385 if (NParms) {
1386 NumParams = NParms;
1387 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1388 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1389 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1390 }
1391}
1392
1393unsigned BlockDecl::getNumParams() const {
1394 return NumParams;
1395}
Sebastian Redl833ef452010-01-26 22:01:41 +00001396
1397
1398//===----------------------------------------------------------------------===//
1399// Other Decl Allocation/Deallocation Method Implementations
1400//===----------------------------------------------------------------------===//
1401
1402TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1403 return new (C) TranslationUnitDecl(C);
1404}
1405
1406NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1407 SourceLocation L, IdentifierInfo *Id) {
1408 return new (C) NamespaceDecl(DC, L, Id);
1409}
1410
1411void NamespaceDecl::Destroy(ASTContext& C) {
1412 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1413 // together. They are all top-level Decls.
1414
1415 this->~NamespaceDecl();
1416 C.Deallocate((void *)this);
1417}
1418
1419
1420ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1421 SourceLocation L, IdentifierInfo *Id, QualType T) {
1422 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1423}
1424
1425FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1426 SourceLocation L,
1427 DeclarationName N, QualType T,
1428 TypeSourceInfo *TInfo,
1429 StorageClass S, bool isInline,
1430 bool hasWrittenPrototype) {
1431 FunctionDecl *New
1432 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
1433 New->HasWrittenPrototype = hasWrittenPrototype;
1434 return New;
1435}
1436
1437BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1438 return new (C) BlockDecl(DC, L);
1439}
1440
1441EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1442 SourceLocation L,
1443 IdentifierInfo *Id, QualType T,
1444 Expr *E, const llvm::APSInt &V) {
1445 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1446}
1447
1448void EnumConstantDecl::Destroy(ASTContext& C) {
1449 if (Init) Init->Destroy(C);
1450 Decl::Destroy(C);
1451}
1452
1453TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1454 SourceLocation L, IdentifierInfo *Id,
1455 TypeSourceInfo *TInfo) {
1456 return new (C) TypedefDecl(DC, L, Id, TInfo);
1457}
1458
1459// Anchor TypedefDecl's vtable here.
1460TypedefDecl::~TypedefDecl() {}
1461
1462FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1463 SourceLocation L,
1464 StringLiteral *Str) {
1465 return new (C) FileScopeAsmDecl(DC, L, Str);
1466}