blob: d75d355fdcf0da4e5069a5bcc70d2cd33267147b [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
525const VarDecl *VarDecl::getActingDefinition() const {
526 return const_cast<VarDecl*>(this)->getActingDefinition();
527}
528
529VarDecl *VarDecl::getActingDefinition() {
530 DefinitionKind Kind = isThisDeclarationADefinition();
531 if (Kind != TentativeDefinition)
532 return 0;
533
534 VarDecl *LastTentative = false;
535 VarDecl *First = getFirstDeclaration();
536 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
537 I != E; ++I) {
538 Kind = (*I)->isThisDeclarationADefinition();
539 if (Kind == Definition)
540 return 0;
541 else if (Kind == TentativeDefinition)
542 LastTentative = *I;
543 }
544 return LastTentative;
545}
546
547bool VarDecl::isTentativeDefinitionNow() const {
548 DefinitionKind Kind = isThisDeclarationADefinition();
549 if (Kind != TentativeDefinition)
550 return false;
551
552 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
553 if ((*I)->isThisDeclarationADefinition() == Definition)
554 return false;
555 }
556 return true;
557}
558
Sebastian Redl833ef452010-01-26 22:01:41 +0000559const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
560 redecl_iterator I = redecls_begin(), E = redecls_end();
561 while (I != E && !I->getInit())
562 ++I;
563
564 if (I != E) {
565 Def = *I;
566 return I->getInit();
567 }
568 return 0;
569}
570
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000571bool VarDecl::isOutOfLine() const {
572 if (!isStaticDataMember())
573 return false;
574
575 if (Decl::isOutOfLine())
576 return true;
577
578 // If this static data member was instantiated from a static data member of
579 // a class template, check whether that static data member was defined
580 // out-of-line.
581 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
582 return VD->isOutOfLine();
583
584 return false;
585}
586
Douglas Gregor1d957a32009-10-27 18:42:08 +0000587VarDecl *VarDecl::getOutOfLineDefinition() {
588 if (!isStaticDataMember())
589 return 0;
590
591 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
592 RD != RDEnd; ++RD) {
593 if (RD->getLexicalDeclContext()->isFileContext())
594 return *RD;
595 }
596
597 return 0;
598}
599
Sebastian Redl833ef452010-01-26 22:01:41 +0000600void VarDecl::setInit(ASTContext &C, Expr *I) {
601 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
602 Eval->~EvaluatedStmt();
603 C.Deallocate(Eval);
604 }
605
606 Init = I;
607}
608
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000609VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000610 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000611 return cast<VarDecl>(MSI->getInstantiatedFrom());
612
613 return 0;
614}
615
Douglas Gregor3c74d412009-10-14 20:14:33 +0000616TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000617 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000618 return MSI->getTemplateSpecializationKind();
619
620 return TSK_Undeclared;
621}
622
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000623MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000624 return getASTContext().getInstantiatedFromStaticDataMember(this);
625}
626
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000627void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
628 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000629 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000630 assert(MSI && "Not an instantiated static data member?");
631 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000632 if (TSK != TSK_ExplicitSpecialization &&
633 PointOfInstantiation.isValid() &&
634 MSI->getPointOfInstantiation().isInvalid())
635 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000636}
637
Sebastian Redl833ef452010-01-26 22:01:41 +0000638//===----------------------------------------------------------------------===//
639// ParmVarDecl Implementation
640//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000641
Sebastian Redl833ef452010-01-26 22:01:41 +0000642ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
643 SourceLocation L, IdentifierInfo *Id,
644 QualType T, TypeSourceInfo *TInfo,
645 StorageClass S, Expr *DefArg) {
646 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000647}
648
Sebastian Redl833ef452010-01-26 22:01:41 +0000649Expr *ParmVarDecl::getDefaultArg() {
650 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
651 assert(!hasUninstantiatedDefaultArg() &&
652 "Default argument is not yet instantiated!");
653
654 Expr *Arg = getInit();
655 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
656 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000657
Sebastian Redl833ef452010-01-26 22:01:41 +0000658 return Arg;
659}
660
661unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
662 if (const CXXExprWithTemporaries *E =
663 dyn_cast<CXXExprWithTemporaries>(getInit()))
664 return E->getNumTemporaries();
665
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000666 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000667}
668
Sebastian Redl833ef452010-01-26 22:01:41 +0000669CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
670 assert(getNumDefaultArgTemporaries() &&
671 "Default arguments does not have any temporaries!");
672
673 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
674 return E->getTemporary(i);
675}
676
677SourceRange ParmVarDecl::getDefaultArgRange() const {
678 if (const Expr *E = getInit())
679 return E->getSourceRange();
680
681 if (hasUninstantiatedDefaultArg())
682 return getUninstantiatedDefaultArg()->getSourceRange();
683
684 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000685}
686
Nuno Lopes394ec982008-12-17 23:39:55 +0000687//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000688// FunctionDecl Implementation
689//===----------------------------------------------------------------------===//
690
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000691void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000692 if (Body && Body.isOffset())
693 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000694
695 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
696 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000697
Douglas Gregord801b062009-10-07 23:56:10 +0000698 FunctionTemplateSpecializationInfo *FTSInfo
699 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
700 if (FTSInfo)
701 C.Deallocate(FTSInfo);
702
703 MemberSpecializationInfo *MSInfo
704 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
705 if (MSInfo)
706 C.Deallocate(MSInfo);
707
Steve Naroff13ae6f42009-01-27 21:25:57 +0000708 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000709
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000710 Decl::Destroy(C);
711}
712
John McCalle1f2ec22009-09-11 06:45:03 +0000713void FunctionDecl::getNameForDiagnostic(std::string &S,
714 const PrintingPolicy &Policy,
715 bool Qualified) const {
716 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
717 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
718 if (TemplateArgs)
719 S += TemplateSpecializationType::PrintTemplateArgumentList(
720 TemplateArgs->getFlatArgumentList(),
721 TemplateArgs->flat_size(),
722 Policy);
723
724}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000725
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000726Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000727 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
728 if (I->Body) {
729 Definition = *I;
730 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000731 }
732 }
733
734 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000735}
736
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000737void FunctionDecl::setBody(Stmt *B) {
738 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000739 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000740 EndRangeLoc = B->getLocEnd();
741}
742
Douglas Gregor16618f22009-09-12 00:17:51 +0000743bool FunctionDecl::isMain() const {
744 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000745 return !Context.getLangOptions().Freestanding &&
746 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000747 getIdentifier() && getIdentifier()->isStr("main");
748}
749
Douglas Gregor16618f22009-09-12 00:17:51 +0000750bool FunctionDecl::isExternC() const {
751 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000752 // In C, any non-static, non-overloadable function has external
753 // linkage.
754 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000755 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000756
Mike Stump11289f42009-09-09 15:08:12 +0000757 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000758 DC = DC->getParent()) {
759 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
760 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000761 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000762 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000763
764 break;
765 }
766 }
767
768 return false;
769}
770
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000771bool FunctionDecl::isGlobal() const {
772 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
773 return Method->isStatic();
774
775 if (getStorageClass() == Static)
776 return false;
777
Mike Stump11289f42009-09-09 15:08:12 +0000778 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000779 DC->isNamespace();
780 DC = DC->getParent()) {
781 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
782 if (!Namespace->getDeclName())
783 return false;
784 break;
785 }
786 }
787
788 return true;
789}
790
Sebastian Redl833ef452010-01-26 22:01:41 +0000791void
792FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
793 redeclarable_base::setPreviousDeclaration(PrevDecl);
794
795 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
796 FunctionTemplateDecl *PrevFunTmpl
797 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
798 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
799 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
800 }
801}
802
803const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
804 return getFirstDeclaration();
805}
806
807FunctionDecl *FunctionDecl::getCanonicalDecl() {
808 return getFirstDeclaration();
809}
810
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000811/// \brief Returns a value indicating whether this function
812/// corresponds to a builtin function.
813///
814/// The function corresponds to a built-in function if it is
815/// declared at translation scope or within an extern "C" block and
816/// its name matches with the name of a builtin. The returned value
817/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000818/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000819/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000820unsigned FunctionDecl::getBuiltinID() const {
821 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000822 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
823 return 0;
824
825 unsigned BuiltinID = getIdentifier()->getBuiltinID();
826 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
827 return BuiltinID;
828
829 // This function has the name of a known C library
830 // function. Determine whether it actually refers to the C library
831 // function or whether it just has the same name.
832
Douglas Gregora908e7f2009-02-17 03:23:10 +0000833 // If this is a static function, it's not a builtin.
834 if (getStorageClass() == Static)
835 return 0;
836
Douglas Gregore711f702009-02-14 18:57:46 +0000837 // If this function is at translation-unit scope and we're not in
838 // C++, it refers to the C library function.
839 if (!Context.getLangOptions().CPlusPlus &&
840 getDeclContext()->isTranslationUnit())
841 return BuiltinID;
842
843 // If the function is in an extern "C" linkage specification and is
844 // not marked "overloadable", it's the real function.
845 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000846 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000847 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000848 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000849 return BuiltinID;
850
851 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000852 return 0;
853}
854
855
Chris Lattner47c0d002009-04-25 06:03:53 +0000856/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000857/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000858/// after it has been created.
859unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000860 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000861 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000862 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000863 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000865}
866
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000867void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
868 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000869 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000870 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000872 // Zero params -> null pointer.
873 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000874 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000875 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000876 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000877
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000878 // Update source range. The check below allows us to set EndRangeLoc before
879 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000880 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000881 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000882 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000883}
Chris Lattner41943152007-01-25 04:52:46 +0000884
Chris Lattner58258242008-04-10 02:22:51 +0000885/// getMinRequiredArguments - Returns the minimum number of arguments
886/// needed to call this function. This may be fewer than the number of
887/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000888/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000889unsigned FunctionDecl::getMinRequiredArguments() const {
890 unsigned NumRequiredArgs = getNumParams();
891 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000892 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000893 --NumRequiredArgs;
894
895 return NumRequiredArgs;
896}
897
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000898bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000899 // FIXME: This is not enough. Consider:
900 //
901 // inline void f();
902 // void f() { }
903 //
904 // f is inlined, but does not have inline specified.
905 // To fix this we should add an 'inline' flag to FunctionDecl.
906 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000907 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000908
909 if (isa<CXXMethodDecl>(this)) {
910 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
911 return true;
912 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000913
914 switch (getTemplateSpecializationKind()) {
915 case TSK_Undeclared:
916 case TSK_ExplicitSpecialization:
917 return false;
918
919 case TSK_ImplicitInstantiation:
920 case TSK_ExplicitInstantiationDeclaration:
921 case TSK_ExplicitInstantiationDefinition:
922 // Handle below.
923 break;
924 }
925
926 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
927 Stmt *Pattern = 0;
928 if (PatternDecl)
929 Pattern = PatternDecl->getBody(PatternDecl);
930
931 if (Pattern && PatternDecl)
932 return PatternDecl->isInlined();
933
934 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000935}
936
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000937/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000938/// definition will be externally visible.
939///
940/// Inline function definitions are always available for inlining optimizations.
941/// However, depending on the language dialect, declaration specifiers, and
942/// attributes, the definition of an inline function may or may not be
943/// "externally" visible to other translation units in the program.
944///
945/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +0000946/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000947/// inline definition becomes externally visible (C99 6.7.4p6).
948///
949/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
950/// definition, we use the GNU semantics for inline, which are nearly the
951/// opposite of C99 semantics. In particular, "inline" by itself will create
952/// an externally visible symbol, but "extern inline" will not create an
953/// externally visible symbol.
954bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
955 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000956 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000957 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +0000958
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000959 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +0000960 // GNU inline semantics. Based on a number of examples, we came up with the
961 // following heuristic: if the "inline" keyword is present on a
962 // declaration of the function but "extern" is not present on that
963 // declaration, then the symbol is externally visible. Otherwise, the GNU
964 // "extern inline" semantics applies and the symbol is not externally
965 // visible.
966 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
967 Redecl != RedeclEnd;
968 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +0000969 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000970 return true;
971 }
972
973 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000974 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000975 }
976
977 // C99 6.7.4p6:
978 // [...] If all of the file scope declarations for a function in a
979 // translation unit include the inline function specifier without extern,
980 // then the definition in that translation unit is an inline definition.
981 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
982 Redecl != RedeclEnd;
983 ++Redecl) {
984 // Only consider file-scope declarations in this test.
985 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
986 continue;
987
Douglas Gregor35b57532009-10-27 21:01:01 +0000988 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000989 return true; // Not an inline definition
990 }
991
992 // C99 6.7.4p6:
993 // An inline definition does not provide an external definition for the
994 // function, and does not forbid an external definition in another
995 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000996 return false;
997}
998
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000999/// getOverloadedOperator - Which C++ overloaded operator this
1000/// function represents, if any.
1001OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001002 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1003 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001004 else
1005 return OO_None;
1006}
1007
Alexis Huntc88db062010-01-13 09:01:02 +00001008/// getLiteralIdentifier - The literal suffix identifier this function
1009/// represents, if any.
1010const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1011 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1012 return getDeclName().getCXXLiteralIdentifier();
1013 else
1014 return 0;
1015}
1016
Douglas Gregord801b062009-10-07 23:56:10 +00001017FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001018 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001019 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1020
1021 return 0;
1022}
1023
Douglas Gregor06db9f52009-10-12 20:18:28 +00001024MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1025 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1026}
1027
Douglas Gregord801b062009-10-07 23:56:10 +00001028void
1029FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1030 TemplateSpecializationKind TSK) {
1031 assert(TemplateOrSpecialization.isNull() &&
1032 "Member function is already a specialization");
1033 MemberSpecializationInfo *Info
1034 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1035 TemplateOrSpecialization = Info;
1036}
1037
Douglas Gregorafca3b42009-10-27 20:53:28 +00001038bool FunctionDecl::isImplicitlyInstantiable() const {
1039 // If this function already has a definition or is invalid, it can't be
1040 // implicitly instantiated.
1041 if (isInvalidDecl() || getBody())
1042 return false;
1043
1044 switch (getTemplateSpecializationKind()) {
1045 case TSK_Undeclared:
1046 case TSK_ExplicitSpecialization:
1047 case TSK_ExplicitInstantiationDefinition:
1048 return false;
1049
1050 case TSK_ImplicitInstantiation:
1051 return true;
1052
1053 case TSK_ExplicitInstantiationDeclaration:
1054 // Handled below.
1055 break;
1056 }
1057
1058 // Find the actual template from which we will instantiate.
1059 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1060 Stmt *Pattern = 0;
1061 if (PatternDecl)
1062 Pattern = PatternDecl->getBody(PatternDecl);
1063
1064 // C++0x [temp.explicit]p9:
1065 // Except for inline functions, other explicit instantiation declarations
1066 // have the effect of suppressing the implicit instantiation of the entity
1067 // to which they refer.
1068 if (!Pattern || !PatternDecl)
1069 return true;
1070
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001071 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001072}
1073
1074FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1075 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1076 while (Primary->getInstantiatedFromMemberTemplate()) {
1077 // If we have hit a point where the user provided a specialization of
1078 // this template, we're done looking.
1079 if (Primary->isMemberSpecialization())
1080 break;
1081
1082 Primary = Primary->getInstantiatedFromMemberTemplate();
1083 }
1084
1085 return Primary->getTemplatedDecl();
1086 }
1087
1088 return getInstantiatedFromMemberFunction();
1089}
1090
Douglas Gregor70d83e22009-06-29 17:30:29 +00001091FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001092 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001093 = TemplateOrSpecialization
1094 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001095 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001096 }
1097 return 0;
1098}
1099
1100const TemplateArgumentList *
1101FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001102 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001103 = TemplateOrSpecialization
1104 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001105 return Info->TemplateArguments;
1106 }
1107 return 0;
1108}
1109
Mike Stump11289f42009-09-09 15:08:12 +00001110void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001111FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1112 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001113 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001114 void *InsertPos,
1115 TemplateSpecializationKind TSK) {
1116 assert(TSK != TSK_Undeclared &&
1117 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001118 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001119 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001120 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +00001121 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001123 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001124 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001125 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001126 Info->TemplateArguments = TemplateArgs;
1127 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001128
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001129 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001130 // function template specializations.
1131 if (InsertPos)
1132 Template->getSpecializations().InsertNode(Info, InsertPos);
1133 else {
1134 // Try to insert the new node. If there is an existing node, remove it
1135 // first.
1136 FunctionTemplateSpecializationInfo *Existing
1137 = Template->getSpecializations().GetOrInsertNode(Info);
1138 if (Existing) {
1139 Template->getSpecializations().RemoveNode(Existing);
1140 Template->getSpecializations().GetOrInsertNode(Info);
1141 }
1142 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001143}
1144
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001145TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001146 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001147 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001148 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001149 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001150 if (FTSInfo)
1151 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001152
Douglas Gregord801b062009-10-07 23:56:10 +00001153 MemberSpecializationInfo *MSInfo
1154 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1155 if (MSInfo)
1156 return MSInfo->getTemplateSpecializationKind();
1157
1158 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001159}
1160
Mike Stump11289f42009-09-09 15:08:12 +00001161void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001162FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1163 SourceLocation PointOfInstantiation) {
1164 if (FunctionTemplateSpecializationInfo *FTSInfo
1165 = TemplateOrSpecialization.dyn_cast<
1166 FunctionTemplateSpecializationInfo*>()) {
1167 FTSInfo->setTemplateSpecializationKind(TSK);
1168 if (TSK != TSK_ExplicitSpecialization &&
1169 PointOfInstantiation.isValid() &&
1170 FTSInfo->getPointOfInstantiation().isInvalid())
1171 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1172 } else if (MemberSpecializationInfo *MSInfo
1173 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1174 MSInfo->setTemplateSpecializationKind(TSK);
1175 if (TSK != TSK_ExplicitSpecialization &&
1176 PointOfInstantiation.isValid() &&
1177 MSInfo->getPointOfInstantiation().isInvalid())
1178 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1179 } else
1180 assert(false && "Function cannot have a template specialization kind");
1181}
1182
1183SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001184 if (FunctionTemplateSpecializationInfo *FTSInfo
1185 = TemplateOrSpecialization.dyn_cast<
1186 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001187 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001188 else if (MemberSpecializationInfo *MSInfo
1189 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001190 return MSInfo->getPointOfInstantiation();
1191
1192 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001193}
1194
Douglas Gregor6411b922009-09-11 20:15:17 +00001195bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001196 if (Decl::isOutOfLine())
1197 return true;
1198
1199 // If this function was instantiated from a member function of a
1200 // class template, check whether that member function was defined out-of-line.
1201 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1202 const FunctionDecl *Definition;
1203 if (FD->getBody(Definition))
1204 return Definition->isOutOfLine();
1205 }
1206
1207 // If this function was instantiated from a function template,
1208 // check whether that function template was defined out-of-line.
1209 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1210 const FunctionDecl *Definition;
1211 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1212 return Definition->isOutOfLine();
1213 }
1214
1215 return false;
1216}
1217
Chris Lattner59a25942008-03-31 00:36:02 +00001218//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001219// FieldDecl Implementation
1220//===----------------------------------------------------------------------===//
1221
1222FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1223 IdentifierInfo *Id, QualType T,
1224 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1225 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1226}
1227
1228bool FieldDecl::isAnonymousStructOrUnion() const {
1229 if (!isImplicit() || getDeclName())
1230 return false;
1231
1232 if (const RecordType *Record = getType()->getAs<RecordType>())
1233 return Record->getDecl()->isAnonymousStructOrUnion();
1234
1235 return false;
1236}
1237
1238//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001239// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001240//===----------------------------------------------------------------------===//
1241
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001242SourceRange TagDecl::getSourceRange() const {
1243 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001244 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001245}
1246
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001247TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001248 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001249}
1250
Douglas Gregordee1be82009-01-17 00:42:38 +00001251void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001252 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1253 TagT->decl.setPointer(this);
1254 TagT->decl.setInt(1);
1255 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001256}
1257
1258void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +00001259 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001260 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1261 assert(TagT->decl.getPointer() == this &&
1262 "Attempt to redefine a tag definition?");
1263 TagT->decl.setInt(0);
1264 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001265}
1266
Ted Kremenek21475702008-09-05 17:16:31 +00001267TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001268 if (isDefinition())
1269 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001270
1271 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001272 R != REnd; ++R)
1273 if (R->isDefinition())
1274 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001275
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001276 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001277}
1278
John McCall06f6fe8d2009-09-04 01:14:41 +00001279TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1280 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001281 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001282 case DeclSpec::TST_struct: return TK_struct;
1283 case DeclSpec::TST_class: return TK_class;
1284 case DeclSpec::TST_union: return TK_union;
1285 case DeclSpec::TST_enum: return TK_enum;
1286 }
1287}
1288
Ted Kremenek21475702008-09-05 17:16:31 +00001289//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001290// EnumDecl Implementation
1291//===----------------------------------------------------------------------===//
1292
1293EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1294 IdentifierInfo *Id, SourceLocation TKL,
1295 EnumDecl *PrevDecl) {
1296 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1297 C.getTypeDeclType(Enum, PrevDecl);
1298 return Enum;
1299}
1300
1301void EnumDecl::Destroy(ASTContext& C) {
1302 Decl::Destroy(C);
1303}
1304
1305void EnumDecl::completeDefinition(ASTContext &C,
1306 QualType NewType,
1307 QualType NewPromotionType) {
1308 assert(!isDefinition() && "Cannot redefine enums!");
1309 IntegerType = NewType;
1310 PromotionType = NewPromotionType;
1311 TagDecl::completeDefinition();
1312}
1313
1314//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001315// RecordDecl Implementation
1316//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001317
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001318RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001319 IdentifierInfo *Id, RecordDecl *PrevDecl,
1320 SourceLocation TKL)
1321 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001322 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001323 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001324 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001325 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001326}
1327
1328RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001329 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001330 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001331
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001332 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001333 C.getTypeDeclType(R, PrevDecl);
1334 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001335}
1336
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001337RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001338}
1339
1340void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001341 TagDecl::Destroy(C);
1342}
1343
Douglas Gregordfcad112009-03-25 15:59:44 +00001344bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001345 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001346 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1347}
1348
Douglas Gregor91f84212008-12-11 16:49:14 +00001349/// completeDefinition - Notes that the definition of this type is now
1350/// complete.
1351void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +00001352 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001353 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001354}
Steve Naroffcc321422007-03-26 23:09:51 +00001355
Steve Naroff415d3d52008-10-08 17:01:13 +00001356//===----------------------------------------------------------------------===//
1357// BlockDecl Implementation
1358//===----------------------------------------------------------------------===//
1359
1360BlockDecl::~BlockDecl() {
1361}
1362
1363void BlockDecl::Destroy(ASTContext& C) {
1364 if (Body)
1365 Body->Destroy(C);
1366
1367 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1368 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001369
1370 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001371 Decl::Destroy(C);
1372}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001373
1374void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1375 unsigned NParms) {
1376 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001377
Steve Naroffc4b30e52009-03-13 16:56:44 +00001378 // Zero params -> null pointer.
1379 if (NParms) {
1380 NumParams = NParms;
1381 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1382 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1383 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1384 }
1385}
1386
1387unsigned BlockDecl::getNumParams() const {
1388 return NumParams;
1389}
Sebastian Redl833ef452010-01-26 22:01:41 +00001390
1391
1392//===----------------------------------------------------------------------===//
1393// Other Decl Allocation/Deallocation Method Implementations
1394//===----------------------------------------------------------------------===//
1395
1396TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1397 return new (C) TranslationUnitDecl(C);
1398}
1399
1400NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1401 SourceLocation L, IdentifierInfo *Id) {
1402 return new (C) NamespaceDecl(DC, L, Id);
1403}
1404
1405void NamespaceDecl::Destroy(ASTContext& C) {
1406 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1407 // together. They are all top-level Decls.
1408
1409 this->~NamespaceDecl();
1410 C.Deallocate((void *)this);
1411}
1412
1413
1414ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1415 SourceLocation L, IdentifierInfo *Id, QualType T) {
1416 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1417}
1418
1419FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1420 SourceLocation L,
1421 DeclarationName N, QualType T,
1422 TypeSourceInfo *TInfo,
1423 StorageClass S, bool isInline,
1424 bool hasWrittenPrototype) {
1425 FunctionDecl *New
1426 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
1427 New->HasWrittenPrototype = hasWrittenPrototype;
1428 return New;
1429}
1430
1431BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1432 return new (C) BlockDecl(DC, L);
1433}
1434
1435EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1436 SourceLocation L,
1437 IdentifierInfo *Id, QualType T,
1438 Expr *E, const llvm::APSInt &V) {
1439 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1440}
1441
1442void EnumConstantDecl::Destroy(ASTContext& C) {
1443 if (Init) Init->Destroy(C);
1444 Decl::Destroy(C);
1445}
1446
1447TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1448 SourceLocation L, IdentifierInfo *Id,
1449 TypeSourceInfo *TInfo) {
1450 return new (C) TypedefDecl(DC, L, Id, TInfo);
1451}
1452
1453// Anchor TypedefDecl's vtable here.
1454TypedefDecl::~TypedefDecl() {}
1455
1456FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1457 SourceLocation L,
1458 StringLiteral *Str) {
1459 return new (C) FileScopeAsmDecl(DC, L, Str);
1460}