blob: c3e976a080a06713bc1b0d21b2087061e4bf69dc [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 Gregor7dc5c172010-02-03 09:33:45 +000050/// \brief Get the most restrictive linkage for the types in the given
51/// template parameter list.
52static Linkage
53getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
54 Linkage L = ExternalLinkage;
55 for (TemplateParameterList::const_iterator P = Params->begin(),
56 PEnd = Params->end();
57 P != PEnd; ++P) {
58 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
59 if (!NTTP->getType()->isDependentType()) {
60 L = minLinkage(L, NTTP->getType()->getLinkage());
61 continue;
62 }
63
64 if (TemplateTemplateParmDecl *TTP
65 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
66 L = minLinkage(L,
67 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
68 }
69 }
70
71 return L;
72}
73
74/// \brief Get the most restrictive linkage for the types and
75/// declarations in the given template argument list.
76static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
77 unsigned NumArgs) {
78 Linkage L = ExternalLinkage;
79
80 for (unsigned I = 0; I != NumArgs; ++I) {
81 switch (Args[I].getKind()) {
82 case TemplateArgument::Null:
83 case TemplateArgument::Integral:
84 case TemplateArgument::Expression:
85 break;
86
87 case TemplateArgument::Type:
88 L = minLinkage(L, Args[I].getAsType()->getLinkage());
89 break;
90
91 case TemplateArgument::Declaration:
92 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
93 L = minLinkage(L, ND->getLinkage());
94 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
95 L = minLinkage(L, VD->getType()->getLinkage());
96 break;
97
98 case TemplateArgument::Template:
99 if (TemplateDecl *Template
100 = Args[I].getAsTemplate().getAsTemplateDecl())
101 L = minLinkage(L, Template->getLinkage());
102 break;
103
104 case TemplateArgument::Pack:
105 L = minLinkage(L,
106 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
107 Args[I].pack_size()));
108 break;
109 }
110 }
111
112 return L;
113}
114
115static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000116 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
117 "Not a name having namespace scope");
118 ASTContext &Context = D->getASTContext();
119
120 // C++ [basic.link]p3:
121 // A name having namespace scope (3.3.6) has internal linkage if it
122 // is the name of
123 // - an object, reference, function or function template that is
124 // explicitly declared static; or,
125 // (This bullet corresponds to C99 6.2.2p3.)
126 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
127 // Explicitly declared static.
128 if (Var->getStorageClass() == VarDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000129 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000130
131 // - an object or reference that is explicitly declared const
132 // and neither explicitly declared extern nor previously
133 // declared to have external linkage; or
134 // (there is no equivalent in C99)
135 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000136 Var->getType().isConstant(Context) &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000137 Var->getStorageClass() != VarDecl::Extern &&
138 Var->getStorageClass() != VarDecl::PrivateExtern) {
139 bool FoundExtern = false;
140 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
141 PrevVar && !FoundExtern;
142 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000143 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000144 FoundExtern = true;
145
146 if (!FoundExtern)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000147 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000148 }
149 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000150 // C++ [temp]p4:
151 // A non-member function template can have internal linkage; any
152 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000153 const FunctionDecl *Function = 0;
154 if (const FunctionTemplateDecl *FunTmpl
155 = dyn_cast<FunctionTemplateDecl>(D))
156 Function = FunTmpl->getTemplatedDecl();
157 else
158 Function = cast<FunctionDecl>(D);
159
160 // Explicitly declared static.
161 if (Function->getStorageClass() == FunctionDecl::Static)
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000162 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000163 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
164 // - a data member of an anonymous union.
165 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000166 return InternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000167 }
168
169 // C++ [basic.link]p4:
170
171 // A name having namespace scope has external linkage if it is the
172 // name of
173 //
174 // - an object or reference, unless it has internal linkage; or
175 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
176 if (!Context.getLangOptions().CPlusPlus &&
177 (Var->getStorageClass() == VarDecl::Extern ||
178 Var->getStorageClass() == VarDecl::PrivateExtern)) {
179 // C99 6.2.2p4:
180 // For an identifier declared with the storage-class specifier
181 // extern in a scope in which a prior declaration of that
182 // identifier is visible, if the prior declaration specifies
183 // internal or external linkage, the linkage of the identifier
184 // at the later declaration is the same as the linkage
185 // specified at the prior declaration. If no prior declaration
186 // is visible, or if the prior declaration specifies no
187 // linkage, then the identifier has external linkage.
188 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000189 if (Linkage L = PrevVar->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000190 return L;
191 }
192 }
193
194 // C99 6.2.2p5:
195 // If the declaration of an identifier for an object has file
196 // scope and no storage-class specifier, its linkage is
197 // external.
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000198 if (Var->isInAnonymousNamespace())
199 return UniqueExternalLinkage;
200
201 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000202 }
203
204 // - a function, unless it has internal linkage; or
205 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
206 // C99 6.2.2p5:
207 // If the declaration of an identifier for a function has no
208 // storage-class specifier, its linkage is determined exactly
209 // as if it were declared with the storage-class specifier
210 // extern.
211 if (!Context.getLangOptions().CPlusPlus &&
212 (Function->getStorageClass() == FunctionDecl::Extern ||
213 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
214 Function->getStorageClass() == FunctionDecl::None)) {
215 // C99 6.2.2p4:
216 // For an identifier declared with the storage-class specifier
217 // extern in a scope in which a prior declaration of that
218 // identifier is visible, if the prior declaration specifies
219 // internal or external linkage, the linkage of the identifier
220 // at the later declaration is the same as the linkage
221 // specified at the prior declaration. If no prior declaration
222 // is visible, or if the prior declaration specifies no
223 // linkage, then the identifier has external linkage.
224 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000225 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregorf73b2822009-11-25 22:24:25 +0000226 return L;
227 }
228 }
229
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000230 if (Function->isInAnonymousNamespace())
231 return UniqueExternalLinkage;
232
233 if (FunctionTemplateSpecializationInfo *SpecInfo
234 = Function->getTemplateSpecializationInfo()) {
235 Linkage L = SpecInfo->getTemplate()->getLinkage();
236 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
237 L = minLinkage(L,
238 getLinkageForTemplateArgumentList(
239 TemplateArgs.getFlatArgumentList(),
240 TemplateArgs.flat_size()));
241 return L;
242 }
243
244 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000245 }
246
247 // - a named class (Clause 9), or an unnamed class defined in a
248 // typedef declaration in which the class has the typedef name
249 // for linkage purposes (7.1.3); or
250 // - a named enumeration (7.2), or an unnamed enumeration
251 // defined in a typedef declaration in which the enumeration
252 // has the typedef name for linkage purposes (7.1.3); or
253 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000254 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
255 if (Tag->isInAnonymousNamespace())
256 return UniqueExternalLinkage;
257
258 // If this is a class template specialization, consider the
259 // linkage of the template and template arguments.
260 if (const ClassTemplateSpecializationDecl *Spec
261 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
262 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
263 Linkage L = getLinkageForTemplateArgumentList(
264 TemplateArgs.getFlatArgumentList(),
265 TemplateArgs.flat_size());
266 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
267 }
268
269 return ExternalLinkage;
270 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000271
272 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000273 if (isa<EnumConstantDecl>(D)) {
274 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
275 if (isExternalLinkage(L))
276 return L;
277 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000278
279 // - a template, unless it is a function template that has
280 // internal linkage (Clause 14);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000281 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
282 if (D->isInAnonymousNamespace())
283 return UniqueExternalLinkage;
284
285 return getLinkageForTemplateParameterList(
286 Template->getTemplateParameters());
287 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000288
289 // - a namespace (7.3), unless it is declared within an unnamed
290 // namespace.
291 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000292 return ExternalLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000293
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000294 return NoLinkage;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000295}
296
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000297Linkage NamedDecl::getLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000298 // Handle linkage for namespace-scope names.
299 if (getDeclContext()->getLookupContext()->isFileContext())
300 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
301 return L;
302
303 // C++ [basic.link]p5:
304 // In addition, a member function, static data member, a named
305 // class or enumeration of class scope, or an unnamed class or
306 // enumeration defined in a class-scope typedef declaration such
307 // that the class or enumeration has the typedef name for linkage
308 // purposes (7.1.3), has external linkage if the name of the class
309 // has external linkage.
310 if (getDeclContext()->isRecord() &&
311 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
312 (isa<TagDecl>(this) &&
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000313 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
314 Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
315 if (isExternalLinkage(L))
316 return L;
317 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000318
319 // C++ [basic.link]p6:
320 // The name of a function declared in block scope and the name of
321 // an object declared by a block scope extern declaration have
322 // linkage. If there is a visible declaration of an entity with
323 // linkage having the same name and type, ignoring entities
324 // declared outside the innermost enclosing namespace scope, the
325 // block scope declaration declares that same entity and receives
326 // the linkage of the previous declaration. If there is more than
327 // one such matching entity, the program is ill-formed. Otherwise,
328 // if no matching entity is found, the block scope entity receives
329 // external linkage.
330 if (getLexicalDeclContext()->isFunctionOrMethod()) {
331 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
332 if (Function->getPreviousDeclaration())
333 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
334 return L;
335
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000336 if (Function->isInAnonymousNamespace())
337 return UniqueExternalLinkage;
338
Douglas Gregorf73b2822009-11-25 22:24:25 +0000339 return ExternalLinkage;
340 }
341
342 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
343 if (Var->getStorageClass() == VarDecl::Extern ||
344 Var->getStorageClass() == VarDecl::PrivateExtern) {
345 if (Var->getPreviousDeclaration())
346 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
347 return L;
348
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000349 if (Var->isInAnonymousNamespace())
350 return UniqueExternalLinkage;
351
Douglas Gregorf73b2822009-11-25 22:24:25 +0000352 return ExternalLinkage;
353 }
354 }
355
356 // C++ [basic.link]p6:
357 // Names not covered by these rules have no linkage.
358 return NoLinkage;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000359 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000360
Douglas Gregor2ada0482009-02-04 17:27:36 +0000361std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000362 return getQualifiedNameAsString(getASTContext().getLangOptions());
363}
364
365std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000366 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
367 // std::string thrashing.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000368 std::vector<std::string> Names;
369 std::string QualName;
370 const DeclContext *Ctx = getDeclContext();
371
372 if (Ctx->isFunctionOrMethod())
373 return getNameAsString();
374
375 while (Ctx) {
Mike Stump11289f42009-09-09 15:08:12 +0000376 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000377 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
378 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
379 std::string TemplateArgsStr
380 = TemplateSpecializationType::PrintTemplateArgumentList(
381 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000382 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000383 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000384 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig07d211e2009-12-24 23:15:03 +0000385 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
386 if (ND->isAnonymousNamespace())
387 Names.push_back("<anonymous namespace>");
388 else
389 Names.push_back(ND->getNameAsString());
390 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
391 if (!RD->getIdentifier()) {
392 std::string RecordString = "<anonymous ";
393 RecordString += RD->getKindName();
394 RecordString += ">";
395 Names.push_back(RecordString);
396 } else {
397 Names.push_back(RD->getNameAsString());
398 }
Sam Weinigb999f682009-12-28 03:19:38 +0000399 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
400 std::string Proto = FD->getNameAsString();
401
402 const FunctionProtoType *FT = 0;
403 if (FD->hasWrittenPrototype())
404 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
405
406 Proto += "(";
407 if (FT) {
408 llvm::raw_string_ostream POut(Proto);
409 unsigned NumParams = FD->getNumParams();
410 for (unsigned i = 0; i < NumParams; ++i) {
411 if (i)
412 POut << ", ";
413 std::string Param;
414 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
415 POut << Param;
416 }
417
418 if (FT->isVariadic()) {
419 if (NumParams > 0)
420 POut << ", ";
421 POut << "...";
422 }
423 }
424 Proto += ")";
425
426 Names.push_back(Proto);
Douglas Gregor85673582009-05-18 17:01:57 +0000427 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000428 Names.push_back(ND->getNameAsString());
429 else
430 break;
431
432 Ctx = Ctx->getParent();
433 }
434
435 std::vector<std::string>::reverse_iterator
436 I = Names.rbegin(),
437 End = Names.rend();
438
439 for (; I!=End; ++I)
440 QualName += *I + "::";
441
442 QualName += getNameAsString();
443
444 return QualName;
445}
446
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000447bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000448 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
449
Douglas Gregor889ceb72009-02-03 19:21:40 +0000450 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
451 // We want to keep it, unless it nominates same namespace.
452 if (getKind() == Decl::UsingDirective) {
453 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
454 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000457 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
458 // For function declarations, we keep track of redeclarations.
459 return FD->getPreviousDeclaration() == OldD;
460
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000461 // For function templates, the underlying function declarations are linked.
462 if (const FunctionTemplateDecl *FunctionTemplate
463 = dyn_cast<FunctionTemplateDecl>(this))
464 if (const FunctionTemplateDecl *OldFunctionTemplate
465 = dyn_cast<FunctionTemplateDecl>(OldD))
466 return FunctionTemplate->getTemplatedDecl()
467 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000468
Steve Naroffc4173fa2009-02-22 19:35:57 +0000469 // For method declarations, we keep track of redeclarations.
470 if (isa<ObjCMethodDecl>(this))
471 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000472
John McCall9f3059a2009-10-09 21:13:30 +0000473 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
474 return true;
475
John McCall3f746822009-11-17 05:59:44 +0000476 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
477 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
478 cast<UsingShadowDecl>(OldD)->getTargetDecl();
479
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000480 // For non-function declarations, if the declarations are of the
481 // same kind then this must be a redeclaration, or semantic analysis
482 // would not have given us the new declaration.
483 return this->getKind() == OldD->getKind();
484}
485
Douglas Gregoreddf4332009-02-24 20:03:32 +0000486bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000487 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000488}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000489
Anders Carlsson6915bf62009-06-26 06:29:23 +0000490NamedDecl *NamedDecl::getUnderlyingDecl() {
491 NamedDecl *ND = this;
492 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000493 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000494 ND = UD->getTargetDecl();
495 else if (ObjCCompatibleAliasDecl *AD
496 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
497 return AD->getClassInterface();
498 else
499 return ND;
500 }
501}
502
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000503//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000504// DeclaratorDecl Implementation
505//===----------------------------------------------------------------------===//
506
507SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000508 if (DeclInfo) {
509 TypeLoc TL = DeclInfo->getTypeLoc();
510 while (true) {
511 TypeLoc NextTL = TL.getNextTypeLoc();
512 if (!NextTL)
513 return TL.getSourceRange().getBegin();
514 TL = NextTL;
515 }
516 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000517 return SourceLocation();
518}
519
520//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000521// VarDecl Implementation
522//===----------------------------------------------------------------------===//
523
Sebastian Redl833ef452010-01-26 22:01:41 +0000524const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
525 switch (SC) {
526 case VarDecl::None: break;
527 case VarDecl::Auto: return "auto"; break;
528 case VarDecl::Extern: return "extern"; break;
529 case VarDecl::PrivateExtern: return "__private_extern__"; break;
530 case VarDecl::Register: return "register"; break;
531 case VarDecl::Static: return "static"; break;
532 }
533
534 assert(0 && "Invalid storage class");
535 return 0;
536}
537
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000538VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000539 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000540 StorageClass S) {
John McCallbcd03502009-12-07 02:54:59 +0000541 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000542}
543
544void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000545 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000546 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000547 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000548 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
549 Eval->~EvaluatedStmt();
550 C.Deallocate(Eval);
551 }
552 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000553 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000554 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000555}
556
557VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000558}
559
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000560SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000561 SourceLocation Start = getTypeSpecStartLoc();
562 if (Start.isInvalid())
563 Start = getLocation();
564
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000565 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000566 return SourceRange(Start, getInit()->getLocEnd());
567 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000568}
569
Sebastian Redl833ef452010-01-26 22:01:41 +0000570bool VarDecl::isExternC() const {
571 ASTContext &Context = getASTContext();
572 if (!Context.getLangOptions().CPlusPlus)
573 return (getDeclContext()->isTranslationUnit() &&
574 getStorageClass() != Static) ||
575 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
576
577 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
578 DC = DC->getParent()) {
579 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
580 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
581 return getStorageClass() != Static;
582
583 break;
584 }
585
586 if (DC->isFunctionOrMethod())
587 return false;
588 }
589
590 return false;
591}
592
593VarDecl *VarDecl::getCanonicalDecl() {
594 return getFirstDeclaration();
595}
596
Sebastian Redl35351a92010-01-31 22:27:38 +0000597VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
598 // C++ [basic.def]p2:
599 // A declaration is a definition unless [...] it contains the 'extern'
600 // specifier or a linkage-specification and neither an initializer [...],
601 // it declares a static data member in a class declaration [...].
602 // C++ [temp.expl.spec]p15:
603 // An explicit specialization of a static data member of a template is a
604 // definition if the declaration includes an initializer; otherwise, it is
605 // a declaration.
606 if (isStaticDataMember()) {
607 if (isOutOfLine() && (hasInit() ||
608 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
609 return Definition;
610 else
611 return DeclarationOnly;
612 }
613 // C99 6.7p5:
614 // A definition of an identifier is a declaration for that identifier that
615 // [...] causes storage to be reserved for that object.
616 // Note: that applies for all non-file-scope objects.
617 // C99 6.9.2p1:
618 // If the declaration of an identifier for an object has file scope and an
619 // initializer, the declaration is an external definition for the identifier
620 if (hasInit())
621 return Definition;
622 // AST for 'extern "C" int foo;' is annotated with 'extern'.
623 if (hasExternalStorage())
624 return DeclarationOnly;
625
626 // C99 6.9.2p2:
627 // A declaration of an object that has file scope without an initializer,
628 // and without a storage class specifier or the scs 'static', constitutes
629 // a tentative definition.
630 // No such thing in C++.
631 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
632 return TentativeDefinition;
633
634 // What's left is (in C, block-scope) declarations without initializers or
635 // external storage. These are definitions.
636 return Definition;
637}
638
Sebastian Redl35351a92010-01-31 22:27:38 +0000639VarDecl *VarDecl::getActingDefinition() {
640 DefinitionKind Kind = isThisDeclarationADefinition();
641 if (Kind != TentativeDefinition)
642 return 0;
643
644 VarDecl *LastTentative = false;
645 VarDecl *First = getFirstDeclaration();
646 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
647 I != E; ++I) {
648 Kind = (*I)->isThisDeclarationADefinition();
649 if (Kind == Definition)
650 return 0;
651 else if (Kind == TentativeDefinition)
652 LastTentative = *I;
653 }
654 return LastTentative;
655}
656
657bool VarDecl::isTentativeDefinitionNow() const {
658 DefinitionKind Kind = isThisDeclarationADefinition();
659 if (Kind != TentativeDefinition)
660 return false;
661
662 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
663 if ((*I)->isThisDeclarationADefinition() == Definition)
664 return false;
665 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000666 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000667}
668
Sebastian Redl5ca79842010-02-01 20:16:42 +0000669VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000670 VarDecl *First = getFirstDeclaration();
671 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
672 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000673 if ((*I)->isThisDeclarationADefinition() == Definition)
674 return *I;
675 }
676 return 0;
677}
678
679const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000680 redecl_iterator I = redecls_begin(), E = redecls_end();
681 while (I != E && !I->getInit())
682 ++I;
683
684 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000685 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000686 return I->getInit();
687 }
688 return 0;
689}
690
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000691bool VarDecl::isOutOfLine() const {
692 if (!isStaticDataMember())
693 return false;
694
695 if (Decl::isOutOfLine())
696 return true;
697
698 // If this static data member was instantiated from a static data member of
699 // a class template, check whether that static data member was defined
700 // out-of-line.
701 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
702 return VD->isOutOfLine();
703
704 return false;
705}
706
Douglas Gregor1d957a32009-10-27 18:42:08 +0000707VarDecl *VarDecl::getOutOfLineDefinition() {
708 if (!isStaticDataMember())
709 return 0;
710
711 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
712 RD != RDEnd; ++RD) {
713 if (RD->getLexicalDeclContext()->isFileContext())
714 return *RD;
715 }
716
717 return 0;
718}
719
Sebastian Redl833ef452010-01-26 22:01:41 +0000720void VarDecl::setInit(ASTContext &C, Expr *I) {
721 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
722 Eval->~EvaluatedStmt();
723 C.Deallocate(Eval);
724 }
725
726 Init = I;
727}
728
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000729VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000730 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000731 return cast<VarDecl>(MSI->getInstantiatedFrom());
732
733 return 0;
734}
735
Douglas Gregor3c74d412009-10-14 20:14:33 +0000736TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000737 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000738 return MSI->getTemplateSpecializationKind();
739
740 return TSK_Undeclared;
741}
742
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000743MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000744 return getASTContext().getInstantiatedFromStaticDataMember(this);
745}
746
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000747void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
748 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000749 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000750 assert(MSI && "Not an instantiated static data member?");
751 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000752 if (TSK != TSK_ExplicitSpecialization &&
753 PointOfInstantiation.isValid() &&
754 MSI->getPointOfInstantiation().isInvalid())
755 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000756}
757
Sebastian Redl833ef452010-01-26 22:01:41 +0000758//===----------------------------------------------------------------------===//
759// ParmVarDecl Implementation
760//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +0000761
Sebastian Redl833ef452010-01-26 22:01:41 +0000762ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
763 SourceLocation L, IdentifierInfo *Id,
764 QualType T, TypeSourceInfo *TInfo,
765 StorageClass S, Expr *DefArg) {
766 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +0000767}
768
Sebastian Redl833ef452010-01-26 22:01:41 +0000769Expr *ParmVarDecl::getDefaultArg() {
770 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
771 assert(!hasUninstantiatedDefaultArg() &&
772 "Default argument is not yet instantiated!");
773
774 Expr *Arg = getInit();
775 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
776 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +0000777
Sebastian Redl833ef452010-01-26 22:01:41 +0000778 return Arg;
779}
780
781unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
782 if (const CXXExprWithTemporaries *E =
783 dyn_cast<CXXExprWithTemporaries>(getInit()))
784 return E->getNumTemporaries();
785
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000786 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000787}
788
Sebastian Redl833ef452010-01-26 22:01:41 +0000789CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
790 assert(getNumDefaultArgTemporaries() &&
791 "Default arguments does not have any temporaries!");
792
793 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
794 return E->getTemporary(i);
795}
796
797SourceRange ParmVarDecl::getDefaultArgRange() const {
798 if (const Expr *E = getInit())
799 return E->getSourceRange();
800
801 if (hasUninstantiatedDefaultArg())
802 return getUninstantiatedDefaultArg()->getSourceRange();
803
804 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000805}
806
Nuno Lopes394ec982008-12-17 23:39:55 +0000807//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000808// FunctionDecl Implementation
809//===----------------------------------------------------------------------===//
810
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000811void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000812 if (Body && Body.isOffset())
813 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000814
815 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
816 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000817
Douglas Gregord801b062009-10-07 23:56:10 +0000818 FunctionTemplateSpecializationInfo *FTSInfo
819 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
820 if (FTSInfo)
821 C.Deallocate(FTSInfo);
822
823 MemberSpecializationInfo *MSInfo
824 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
825 if (MSInfo)
826 C.Deallocate(MSInfo);
827
Steve Naroff13ae6f42009-01-27 21:25:57 +0000828 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000829
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000830 Decl::Destroy(C);
831}
832
John McCalle1f2ec22009-09-11 06:45:03 +0000833void FunctionDecl::getNameForDiagnostic(std::string &S,
834 const PrintingPolicy &Policy,
835 bool Qualified) const {
836 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
837 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
838 if (TemplateArgs)
839 S += TemplateSpecializationType::PrintTemplateArgumentList(
840 TemplateArgs->getFlatArgumentList(),
841 TemplateArgs->flat_size(),
842 Policy);
843
844}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000845
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000846Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000847 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
848 if (I->Body) {
849 Definition = *I;
850 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000851 }
852 }
853
854 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000855}
856
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000857void FunctionDecl::setBody(Stmt *B) {
858 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000859 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000860 EndRangeLoc = B->getLocEnd();
861}
862
Douglas Gregor16618f22009-09-12 00:17:51 +0000863bool FunctionDecl::isMain() const {
864 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000865 return !Context.getLangOptions().Freestanding &&
866 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000867 getIdentifier() && getIdentifier()->isStr("main");
868}
869
Douglas Gregor16618f22009-09-12 00:17:51 +0000870bool FunctionDecl::isExternC() const {
871 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000872 // In C, any non-static, non-overloadable function has external
873 // linkage.
874 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000875 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000876
Mike Stump11289f42009-09-09 15:08:12 +0000877 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000878 DC = DC->getParent()) {
879 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
880 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000881 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000882 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000883
884 break;
885 }
886 }
887
888 return false;
889}
890
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000891bool FunctionDecl::isGlobal() const {
892 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
893 return Method->isStatic();
894
895 if (getStorageClass() == Static)
896 return false;
897
Mike Stump11289f42009-09-09 15:08:12 +0000898 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000899 DC->isNamespace();
900 DC = DC->getParent()) {
901 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
902 if (!Namespace->getDeclName())
903 return false;
904 break;
905 }
906 }
907
908 return true;
909}
910
Sebastian Redl833ef452010-01-26 22:01:41 +0000911void
912FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
913 redeclarable_base::setPreviousDeclaration(PrevDecl);
914
915 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
916 FunctionTemplateDecl *PrevFunTmpl
917 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
918 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
919 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
920 }
921}
922
923const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
924 return getFirstDeclaration();
925}
926
927FunctionDecl *FunctionDecl::getCanonicalDecl() {
928 return getFirstDeclaration();
929}
930
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000931/// \brief Returns a value indicating whether this function
932/// corresponds to a builtin function.
933///
934/// The function corresponds to a built-in function if it is
935/// declared at translation scope or within an extern "C" block and
936/// its name matches with the name of a builtin. The returned value
937/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000938/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000939/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000940unsigned FunctionDecl::getBuiltinID() const {
941 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000942 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
943 return 0;
944
945 unsigned BuiltinID = getIdentifier()->getBuiltinID();
946 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
947 return BuiltinID;
948
949 // This function has the name of a known C library
950 // function. Determine whether it actually refers to the C library
951 // function or whether it just has the same name.
952
Douglas Gregora908e7f2009-02-17 03:23:10 +0000953 // If this is a static function, it's not a builtin.
954 if (getStorageClass() == Static)
955 return 0;
956
Douglas Gregore711f702009-02-14 18:57:46 +0000957 // If this function is at translation-unit scope and we're not in
958 // C++, it refers to the C library function.
959 if (!Context.getLangOptions().CPlusPlus &&
960 getDeclContext()->isTranslationUnit())
961 return BuiltinID;
962
963 // If the function is in an extern "C" linkage specification and is
964 // not marked "overloadable", it's the real function.
965 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000966 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000967 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000968 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000969 return BuiltinID;
970
971 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000972 return 0;
973}
974
975
Chris Lattner47c0d002009-04-25 06:03:53 +0000976/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000977/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000978/// after it has been created.
979unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000980 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000981 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000982 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000983 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000985}
986
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000987void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
988 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000989 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000990 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000991
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000992 // Zero params -> null pointer.
993 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000994 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000995 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000996 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000997
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000998 // Update source range. The check below allows us to set EndRangeLoc before
999 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001000 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001001 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001002 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001003}
Chris Lattner41943152007-01-25 04:52:46 +00001004
Chris Lattner58258242008-04-10 02:22:51 +00001005/// getMinRequiredArguments - Returns the minimum number of arguments
1006/// needed to call this function. This may be fewer than the number of
1007/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001008/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001009unsigned FunctionDecl::getMinRequiredArguments() const {
1010 unsigned NumRequiredArgs = getNumParams();
1011 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001012 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001013 --NumRequiredArgs;
1014
1015 return NumRequiredArgs;
1016}
1017
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001018bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001019 // FIXME: This is not enough. Consider:
1020 //
1021 // inline void f();
1022 // void f() { }
1023 //
1024 // f is inlined, but does not have inline specified.
1025 // To fix this we should add an 'inline' flag to FunctionDecl.
1026 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001027 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001028
1029 if (isa<CXXMethodDecl>(this)) {
1030 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1031 return true;
1032 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001033
1034 switch (getTemplateSpecializationKind()) {
1035 case TSK_Undeclared:
1036 case TSK_ExplicitSpecialization:
1037 return false;
1038
1039 case TSK_ImplicitInstantiation:
1040 case TSK_ExplicitInstantiationDeclaration:
1041 case TSK_ExplicitInstantiationDefinition:
1042 // Handle below.
1043 break;
1044 }
1045
1046 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1047 Stmt *Pattern = 0;
1048 if (PatternDecl)
1049 Pattern = PatternDecl->getBody(PatternDecl);
1050
1051 if (Pattern && PatternDecl)
1052 return PatternDecl->isInlined();
1053
1054 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001055}
1056
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001057/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001058/// definition will be externally visible.
1059///
1060/// Inline function definitions are always available for inlining optimizations.
1061/// However, depending on the language dialect, declaration specifiers, and
1062/// attributes, the definition of an inline function may or may not be
1063/// "externally" visible to other translation units in the program.
1064///
1065/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001066/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001067/// inline definition becomes externally visible (C99 6.7.4p6).
1068///
1069/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1070/// definition, we use the GNU semantics for inline, which are nearly the
1071/// opposite of C99 semantics. In particular, "inline" by itself will create
1072/// an externally visible symbol, but "extern inline" will not create an
1073/// externally visible symbol.
1074bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1075 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001076 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001077 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001078
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001079 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001080 // GNU inline semantics. Based on a number of examples, we came up with the
1081 // following heuristic: if the "inline" keyword is present on a
1082 // declaration of the function but "extern" is not present on that
1083 // declaration, then the symbol is externally visible. Otherwise, the GNU
1084 // "extern inline" semantics applies and the symbol is not externally
1085 // visible.
1086 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1087 Redecl != RedeclEnd;
1088 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +00001089 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001090 return true;
1091 }
1092
1093 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001094 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001095 }
1096
1097 // C99 6.7.4p6:
1098 // [...] If all of the file scope declarations for a function in a
1099 // translation unit include the inline function specifier without extern,
1100 // then the definition in that translation unit is an inline definition.
1101 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1102 Redecl != RedeclEnd;
1103 ++Redecl) {
1104 // Only consider file-scope declarations in this test.
1105 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1106 continue;
1107
Douglas Gregor35b57532009-10-27 21:01:01 +00001108 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001109 return true; // Not an inline definition
1110 }
1111
1112 // C99 6.7.4p6:
1113 // An inline definition does not provide an external definition for the
1114 // function, and does not forbid an external definition in another
1115 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001116 return false;
1117}
1118
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001119/// getOverloadedOperator - Which C++ overloaded operator this
1120/// function represents, if any.
1121OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001122 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1123 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001124 else
1125 return OO_None;
1126}
1127
Alexis Huntc88db062010-01-13 09:01:02 +00001128/// getLiteralIdentifier - The literal suffix identifier this function
1129/// represents, if any.
1130const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1131 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1132 return getDeclName().getCXXLiteralIdentifier();
1133 else
1134 return 0;
1135}
1136
Douglas Gregord801b062009-10-07 23:56:10 +00001137FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001138 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001139 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1140
1141 return 0;
1142}
1143
Douglas Gregor06db9f52009-10-12 20:18:28 +00001144MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1145 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1146}
1147
Douglas Gregord801b062009-10-07 23:56:10 +00001148void
1149FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1150 TemplateSpecializationKind TSK) {
1151 assert(TemplateOrSpecialization.isNull() &&
1152 "Member function is already a specialization");
1153 MemberSpecializationInfo *Info
1154 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1155 TemplateOrSpecialization = Info;
1156}
1157
Douglas Gregorafca3b42009-10-27 20:53:28 +00001158bool FunctionDecl::isImplicitlyInstantiable() const {
1159 // If this function already has a definition or is invalid, it can't be
1160 // implicitly instantiated.
1161 if (isInvalidDecl() || getBody())
1162 return false;
1163
1164 switch (getTemplateSpecializationKind()) {
1165 case TSK_Undeclared:
1166 case TSK_ExplicitSpecialization:
1167 case TSK_ExplicitInstantiationDefinition:
1168 return false;
1169
1170 case TSK_ImplicitInstantiation:
1171 return true;
1172
1173 case TSK_ExplicitInstantiationDeclaration:
1174 // Handled below.
1175 break;
1176 }
1177
1178 // Find the actual template from which we will instantiate.
1179 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1180 Stmt *Pattern = 0;
1181 if (PatternDecl)
1182 Pattern = PatternDecl->getBody(PatternDecl);
1183
1184 // C++0x [temp.explicit]p9:
1185 // Except for inline functions, other explicit instantiation declarations
1186 // have the effect of suppressing the implicit instantiation of the entity
1187 // to which they refer.
1188 if (!Pattern || !PatternDecl)
1189 return true;
1190
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001191 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001192}
1193
1194FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1195 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1196 while (Primary->getInstantiatedFromMemberTemplate()) {
1197 // If we have hit a point where the user provided a specialization of
1198 // this template, we're done looking.
1199 if (Primary->isMemberSpecialization())
1200 break;
1201
1202 Primary = Primary->getInstantiatedFromMemberTemplate();
1203 }
1204
1205 return Primary->getTemplatedDecl();
1206 }
1207
1208 return getInstantiatedFromMemberFunction();
1209}
1210
Douglas Gregor70d83e22009-06-29 17:30:29 +00001211FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001212 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001213 = TemplateOrSpecialization
1214 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001215 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001216 }
1217 return 0;
1218}
1219
1220const TemplateArgumentList *
1221FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001222 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001223 = TemplateOrSpecialization
1224 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001225 return Info->TemplateArguments;
1226 }
1227 return 0;
1228}
1229
Mike Stump11289f42009-09-09 15:08:12 +00001230void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001231FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1232 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001233 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001234 void *InsertPos,
1235 TemplateSpecializationKind TSK) {
1236 assert(TSK != TSK_Undeclared &&
1237 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001238 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001239 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001240 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +00001241 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001242
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001243 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001244 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001245 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001246 Info->TemplateArguments = TemplateArgs;
1247 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001248
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001249 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001250 // function template specializations.
1251 if (InsertPos)
1252 Template->getSpecializations().InsertNode(Info, InsertPos);
1253 else {
1254 // Try to insert the new node. If there is an existing node, remove it
1255 // first.
1256 FunctionTemplateSpecializationInfo *Existing
1257 = Template->getSpecializations().GetOrInsertNode(Info);
1258 if (Existing) {
1259 Template->getSpecializations().RemoveNode(Existing);
1260 Template->getSpecializations().GetOrInsertNode(Info);
1261 }
1262 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001263}
1264
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001265TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001266 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001267 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001268 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001269 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001270 if (FTSInfo)
1271 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001272
Douglas Gregord801b062009-10-07 23:56:10 +00001273 MemberSpecializationInfo *MSInfo
1274 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1275 if (MSInfo)
1276 return MSInfo->getTemplateSpecializationKind();
1277
1278 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001279}
1280
Mike Stump11289f42009-09-09 15:08:12 +00001281void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001282FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1283 SourceLocation PointOfInstantiation) {
1284 if (FunctionTemplateSpecializationInfo *FTSInfo
1285 = TemplateOrSpecialization.dyn_cast<
1286 FunctionTemplateSpecializationInfo*>()) {
1287 FTSInfo->setTemplateSpecializationKind(TSK);
1288 if (TSK != TSK_ExplicitSpecialization &&
1289 PointOfInstantiation.isValid() &&
1290 FTSInfo->getPointOfInstantiation().isInvalid())
1291 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1292 } else if (MemberSpecializationInfo *MSInfo
1293 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1294 MSInfo->setTemplateSpecializationKind(TSK);
1295 if (TSK != TSK_ExplicitSpecialization &&
1296 PointOfInstantiation.isValid() &&
1297 MSInfo->getPointOfInstantiation().isInvalid())
1298 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1299 } else
1300 assert(false && "Function cannot have a template specialization kind");
1301}
1302
1303SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001304 if (FunctionTemplateSpecializationInfo *FTSInfo
1305 = TemplateOrSpecialization.dyn_cast<
1306 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001307 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001308 else if (MemberSpecializationInfo *MSInfo
1309 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001310 return MSInfo->getPointOfInstantiation();
1311
1312 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001313}
1314
Douglas Gregor6411b922009-09-11 20:15:17 +00001315bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001316 if (Decl::isOutOfLine())
1317 return true;
1318
1319 // If this function was instantiated from a member function of a
1320 // class template, check whether that member function was defined out-of-line.
1321 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1322 const FunctionDecl *Definition;
1323 if (FD->getBody(Definition))
1324 return Definition->isOutOfLine();
1325 }
1326
1327 // If this function was instantiated from a function template,
1328 // check whether that function template was defined out-of-line.
1329 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1330 const FunctionDecl *Definition;
1331 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1332 return Definition->isOutOfLine();
1333 }
1334
1335 return false;
1336}
1337
Chris Lattner59a25942008-03-31 00:36:02 +00001338//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001339// FieldDecl Implementation
1340//===----------------------------------------------------------------------===//
1341
1342FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1343 IdentifierInfo *Id, QualType T,
1344 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1345 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1346}
1347
1348bool FieldDecl::isAnonymousStructOrUnion() const {
1349 if (!isImplicit() || getDeclName())
1350 return false;
1351
1352 if (const RecordType *Record = getType()->getAs<RecordType>())
1353 return Record->getDecl()->isAnonymousStructOrUnion();
1354
1355 return false;
1356}
1357
1358//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001359// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001360//===----------------------------------------------------------------------===//
1361
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001362SourceRange TagDecl::getSourceRange() const {
1363 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001364 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001365}
1366
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001367TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001368 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001369}
1370
Douglas Gregordee1be82009-01-17 00:42:38 +00001371void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001372 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1373 TagT->decl.setPointer(this);
1374 TagT->decl.setInt(1);
1375 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001376}
1377
1378void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +00001379 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001380 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1381 assert(TagT->decl.getPointer() == this &&
1382 "Attempt to redefine a tag definition?");
1383 TagT->decl.setInt(0);
1384 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001385}
1386
Ted Kremenek21475702008-09-05 17:16:31 +00001387TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001388 if (isDefinition())
1389 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001390
1391 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001392 R != REnd; ++R)
1393 if (R->isDefinition())
1394 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001395
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001396 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001397}
1398
John McCall06f6fe8d2009-09-04 01:14:41 +00001399TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1400 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001401 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001402 case DeclSpec::TST_struct: return TK_struct;
1403 case DeclSpec::TST_class: return TK_class;
1404 case DeclSpec::TST_union: return TK_union;
1405 case DeclSpec::TST_enum: return TK_enum;
1406 }
1407}
1408
Ted Kremenek21475702008-09-05 17:16:31 +00001409//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001410// EnumDecl Implementation
1411//===----------------------------------------------------------------------===//
1412
1413EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1414 IdentifierInfo *Id, SourceLocation TKL,
1415 EnumDecl *PrevDecl) {
1416 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1417 C.getTypeDeclType(Enum, PrevDecl);
1418 return Enum;
1419}
1420
1421void EnumDecl::Destroy(ASTContext& C) {
1422 Decl::Destroy(C);
1423}
1424
1425void EnumDecl::completeDefinition(ASTContext &C,
1426 QualType NewType,
1427 QualType NewPromotionType) {
1428 assert(!isDefinition() && "Cannot redefine enums!");
1429 IntegerType = NewType;
1430 PromotionType = NewPromotionType;
1431 TagDecl::completeDefinition();
1432}
1433
1434//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001435// RecordDecl Implementation
1436//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001437
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001438RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001439 IdentifierInfo *Id, RecordDecl *PrevDecl,
1440 SourceLocation TKL)
1441 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001442 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001443 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001444 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001445 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001446}
1447
1448RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001449 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001450 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001451
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001452 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001453 C.getTypeDeclType(R, PrevDecl);
1454 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001455}
1456
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001457RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001458}
1459
1460void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001461 TagDecl::Destroy(C);
1462}
1463
Douglas Gregordfcad112009-03-25 15:59:44 +00001464bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001465 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001466 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1467}
1468
Douglas Gregor91f84212008-12-11 16:49:14 +00001469/// completeDefinition - Notes that the definition of this type is now
1470/// complete.
1471void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +00001472 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001473 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001474}
Steve Naroffcc321422007-03-26 23:09:51 +00001475
Steve Naroff415d3d52008-10-08 17:01:13 +00001476//===----------------------------------------------------------------------===//
1477// BlockDecl Implementation
1478//===----------------------------------------------------------------------===//
1479
1480BlockDecl::~BlockDecl() {
1481}
1482
1483void BlockDecl::Destroy(ASTContext& C) {
1484 if (Body)
1485 Body->Destroy(C);
1486
1487 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1488 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001489
1490 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001491 Decl::Destroy(C);
1492}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001493
1494void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1495 unsigned NParms) {
1496 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001497
Steve Naroffc4b30e52009-03-13 16:56:44 +00001498 // Zero params -> null pointer.
1499 if (NParms) {
1500 NumParams = NParms;
1501 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1502 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1503 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1504 }
1505}
1506
1507unsigned BlockDecl::getNumParams() const {
1508 return NumParams;
1509}
Sebastian Redl833ef452010-01-26 22:01:41 +00001510
1511
1512//===----------------------------------------------------------------------===//
1513// Other Decl Allocation/Deallocation Method Implementations
1514//===----------------------------------------------------------------------===//
1515
1516TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1517 return new (C) TranslationUnitDecl(C);
1518}
1519
1520NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1521 SourceLocation L, IdentifierInfo *Id) {
1522 return new (C) NamespaceDecl(DC, L, Id);
1523}
1524
1525void NamespaceDecl::Destroy(ASTContext& C) {
1526 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
1527 // together. They are all top-level Decls.
1528
1529 this->~NamespaceDecl();
1530 C.Deallocate((void *)this);
1531}
1532
1533
1534ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1535 SourceLocation L, IdentifierInfo *Id, QualType T) {
1536 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1537}
1538
1539FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
1540 SourceLocation L,
1541 DeclarationName N, QualType T,
1542 TypeSourceInfo *TInfo,
1543 StorageClass S, bool isInline,
1544 bool hasWrittenPrototype) {
1545 FunctionDecl *New
1546 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
1547 New->HasWrittenPrototype = hasWrittenPrototype;
1548 return New;
1549}
1550
1551BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1552 return new (C) BlockDecl(DC, L);
1553}
1554
1555EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1556 SourceLocation L,
1557 IdentifierInfo *Id, QualType T,
1558 Expr *E, const llvm::APSInt &V) {
1559 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1560}
1561
1562void EnumConstantDecl::Destroy(ASTContext& C) {
1563 if (Init) Init->Destroy(C);
1564 Decl::Destroy(C);
1565}
1566
1567TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1568 SourceLocation L, IdentifierInfo *Id,
1569 TypeSourceInfo *TInfo) {
1570 return new (C) TypedefDecl(DC, L, Id, TInfo);
1571}
1572
1573// Anchor TypedefDecl's vtable here.
1574TypedefDecl::~TypedefDecl() {}
1575
1576FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1577 SourceLocation L,
1578 StringLiteral *Str) {
1579 return new (C) FileScopeAsmDecl(DC, L, Str);
1580}