blob: 220b0fb1c7bdb5672345bb75fbbe2b515bdbde33 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
John McCallf1bbbb42009-09-04 01:14:41 +000026#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000028#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000029
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Chris Lattner0b2b6e12009-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 Kyrtzidisb17166c2009-08-19 01:27:32 +000041/// \brief Return the TypeLoc wrapper for the type source info.
John McCalla93c9342009-12-07 02:54:59 +000042TypeLoc TypeSourceInfo::getTypeLoc() const {
Argyrios Kyrtzidisb7354712009-09-29 19:40:20 +000043 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000044}
Chris Lattner0b2b6e12009-03-04 06:34:08 +000045
Chris Lattnerd3b90652008-03-15 05:43:15 +000046//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000047// Decl Allocation/Deallocation Method Implementations
48//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattner0b2b6e12009-03-04 06:34:08 +000050
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000051TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +000052 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000053}
54
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000055NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
56 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000057 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000058}
59
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000060void NamespaceDecl::Destroy(ASTContext& C) {
61 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
62 // together. They are all top-level Decls.
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenekebf27b12008-05-24 15:09:56 +000064 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000065 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000066}
67
68
Chris Lattner41110242008-06-17 18:05:57 +000069ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000070 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000071 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000072}
73
Daniel Dunbarb286a782009-04-14 02:08:49 +000074const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
75 switch (SC) {
76 case VarDecl::None: break;
77 case VarDecl::Auto: return "auto"; break;
78 case VarDecl::Extern: return "extern"; break;
Mike Stump1eb44332009-09-09 15:08:12 +000079 case VarDecl::PrivateExtern: return "__private_extern__"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000080 case VarDecl::Register: return "register"; break;
Mike Stump1eb44332009-09-09 15:08:12 +000081 case VarDecl::Static: return "static"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000082 }
83
84 assert(0 && "Invalid storage class");
85 return 0;
86}
87
Chris Lattner9fdf9c62008-04-22 18:39:57 +000088ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000089 SourceLocation L, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +000090 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +000091 StorageClass S, Expr *DefArg) {
John McCalla93c9342009-12-07 02:54:59 +000092 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000093}
94
Anders Carlsson337cba42009-12-15 19:16:31 +000095Expr *ParmVarDecl::getDefaultArg() {
96 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
97 assert(!hasUninstantiatedDefaultArg() &&
98 "Default argument is not yet instantiated!");
99
100 Expr *Arg = getInit();
101 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
102 return E->getSubExpr();
103
104 return Arg;
105}
106
107unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
108 if (const CXXExprWithTemporaries *E =
109 dyn_cast<CXXExprWithTemporaries>(getInit()))
110 return E->getNumTemporaries();
111
112 return 0;
113}
114
115CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
116 assert(getNumDefaultArgTemporaries() &&
117 "Default arguments does not have any temporaries!");
118
119 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
120 return E->getTemporary(i);
121}
122
Douglas Gregor6cc15182009-09-11 18:44:32 +0000123SourceRange ParmVarDecl::getDefaultArgRange() const {
124 if (const Expr *E = getInit())
125 return E->getSourceRange();
126
Anders Carlsson156c78e2009-12-13 17:53:43 +0000127 if (hasUninstantiatedDefaultArg())
128 return getUninstantiatedDefaultArg()->getSourceRange();
Douglas Gregor6cc15182009-09-11 18:44:32 +0000129
130 return SourceRange();
131}
Douglas Gregor78d15832009-05-26 18:54:04 +0000132
Douglas Gregor6cc15182009-09-11 18:44:32 +0000133void VarDecl::setInit(ASTContext &C, Expr *I) {
134 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
135 Eval->~EvaluatedStmt();
136 C.Deallocate(Eval);
Douglas Gregor78d15832009-05-26 18:54:04 +0000137 }
138
Douglas Gregor6cc15182009-09-11 18:44:32 +0000139 Init = I;
140}
141
Douglas Gregor48a83b52009-09-12 00:17:51 +0000142bool VarDecl::isExternC() const {
143 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000144 if (!Context.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000145 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000146 getStorageClass() != Static) ||
147 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
148
Mike Stump1eb44332009-09-09 15:08:12 +0000149 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000150 DC = DC->getParent()) {
151 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
152 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
153 return getStorageClass() != Static;
154
155 break;
156 }
157
158 if (DC->isFunctionOrMethod())
159 return false;
160 }
161
162 return false;
163}
164
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000165FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000166 SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000167 DeclarationName N, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000168 TypeSourceInfo *TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000169 StorageClass S, bool isInline,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000170 bool hasWrittenPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +0000171 FunctionDecl *New
John McCalla93c9342009-12-07 02:54:59 +0000172 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
Anders Carlssona75e8532009-05-14 21:46:00 +0000173 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor2224f842009-02-25 16:33:18 +0000174 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000175}
176
Steve Naroff090276f2008-10-10 01:28:17 +0000177BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000178 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000179}
180
Douglas Gregor44b43212008-12-11 16:49:14 +0000181FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000182 IdentifierInfo *Id, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000183 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
184 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000185}
186
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000187bool FieldDecl::isAnonymousStructOrUnion() const {
188 if (!isImplicit() || getDeclName())
189 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenek6217b802009-07-29 21:53:49 +0000191 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000192 return Record->getDecl()->isAnonymousStructOrUnion();
193
194 return false;
195}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000196
Chris Lattner0ed844b2008-04-04 06:12:32 +0000197EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
198 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000199 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000200 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000201 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000202}
203
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000204void EnumConstantDecl::Destroy(ASTContext& C) {
205 if (Init) Init->Destroy(C);
206 Decl::Destroy(C);
207}
208
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000209TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
John McCallba6a9bd2009-10-24 08:00:42 +0000210 SourceLocation L, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000211 TypeSourceInfo *TInfo) {
212 return new (C) TypedefDecl(DC, L, Id, TInfo);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000213}
214
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000215EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000216 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000217 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000218 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000219 C.getTypeDeclType(Enum, PrevDecl);
220 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000221}
222
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000223void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000224 Decl::Destroy(C);
225}
226
John McCall842aef82009-12-09 09:09:27 +0000227void EnumDecl::completeDefinition(ASTContext &C,
228 QualType NewType,
229 QualType NewPromotionType) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000230 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000231 IntegerType = NewType;
John McCall842aef82009-12-09 09:09:27 +0000232 PromotionType = NewPromotionType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000233 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000234}
235
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000236FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000237 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000238 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000239 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000240}
241
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000242//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000243// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000244//===----------------------------------------------------------------------===//
245
Douglas Gregord85b5b92009-11-25 22:24:25 +0000246static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
247 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
248 "Not a name having namespace scope");
249 ASTContext &Context = D->getASTContext();
250
251 // C++ [basic.link]p3:
252 // A name having namespace scope (3.3.6) has internal linkage if it
253 // is the name of
254 // - an object, reference, function or function template that is
255 // explicitly declared static; or,
256 // (This bullet corresponds to C99 6.2.2p3.)
257 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
258 // Explicitly declared static.
259 if (Var->getStorageClass() == VarDecl::Static)
260 return NamedDecl::InternalLinkage;
261
262 // - an object or reference that is explicitly declared const
263 // and neither explicitly declared extern nor previously
264 // declared to have external linkage; or
265 // (there is no equivalent in C99)
266 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000267 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000268 Var->getStorageClass() != VarDecl::Extern &&
269 Var->getStorageClass() != VarDecl::PrivateExtern) {
270 bool FoundExtern = false;
271 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
272 PrevVar && !FoundExtern;
273 PrevVar = PrevVar->getPreviousDeclaration())
274 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
275 FoundExtern = true;
276
277 if (!FoundExtern)
278 return NamedDecl::InternalLinkage;
279 }
280 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
281 const FunctionDecl *Function = 0;
282 if (const FunctionTemplateDecl *FunTmpl
283 = dyn_cast<FunctionTemplateDecl>(D))
284 Function = FunTmpl->getTemplatedDecl();
285 else
286 Function = cast<FunctionDecl>(D);
287
288 // Explicitly declared static.
289 if (Function->getStorageClass() == FunctionDecl::Static)
290 return NamedDecl::InternalLinkage;
291 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
292 // - a data member of an anonymous union.
293 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
294 return NamedDecl::InternalLinkage;
295 }
296
297 // C++ [basic.link]p4:
298
299 // A name having namespace scope has external linkage if it is the
300 // name of
301 //
302 // - an object or reference, unless it has internal linkage; or
303 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
304 if (!Context.getLangOptions().CPlusPlus &&
305 (Var->getStorageClass() == VarDecl::Extern ||
306 Var->getStorageClass() == VarDecl::PrivateExtern)) {
307 // C99 6.2.2p4:
308 // For an identifier declared with the storage-class specifier
309 // extern in a scope in which a prior declaration of that
310 // identifier is visible, if the prior declaration specifies
311 // internal or external linkage, the linkage of the identifier
312 // at the later declaration is the same as the linkage
313 // specified at the prior declaration. If no prior declaration
314 // is visible, or if the prior declaration specifies no
315 // linkage, then the identifier has external linkage.
316 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
317 if (NamedDecl::Linkage L = PrevVar->getLinkage())
318 return L;
319 }
320 }
321
322 // C99 6.2.2p5:
323 // If the declaration of an identifier for an object has file
324 // scope and no storage-class specifier, its linkage is
325 // external.
326 return NamedDecl::ExternalLinkage;
327 }
328
329 // - a function, unless it has internal linkage; or
330 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
331 // C99 6.2.2p5:
332 // If the declaration of an identifier for a function has no
333 // storage-class specifier, its linkage is determined exactly
334 // as if it were declared with the storage-class specifier
335 // extern.
336 if (!Context.getLangOptions().CPlusPlus &&
337 (Function->getStorageClass() == FunctionDecl::Extern ||
338 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
339 Function->getStorageClass() == FunctionDecl::None)) {
340 // C99 6.2.2p4:
341 // For an identifier declared with the storage-class specifier
342 // extern in a scope in which a prior declaration of that
343 // identifier is visible, if the prior declaration specifies
344 // internal or external linkage, the linkage of the identifier
345 // at the later declaration is the same as the linkage
346 // specified at the prior declaration. If no prior declaration
347 // is visible, or if the prior declaration specifies no
348 // linkage, then the identifier has external linkage.
349 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
350 if (NamedDecl::Linkage L = PrevFunc->getLinkage())
351 return L;
352 }
353 }
354
355 return NamedDecl::ExternalLinkage;
356 }
357
358 // - a named class (Clause 9), or an unnamed class defined in a
359 // typedef declaration in which the class has the typedef name
360 // for linkage purposes (7.1.3); or
361 // - a named enumeration (7.2), or an unnamed enumeration
362 // defined in a typedef declaration in which the enumeration
363 // has the typedef name for linkage purposes (7.1.3); or
364 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
365 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
366 return NamedDecl::ExternalLinkage;
367
368 // - an enumerator belonging to an enumeration with external linkage;
369 if (isa<EnumConstantDecl>(D))
370 if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
371 == NamedDecl::ExternalLinkage)
372 return NamedDecl::ExternalLinkage;
373
374 // - a template, unless it is a function template that has
375 // internal linkage (Clause 14);
376 if (isa<TemplateDecl>(D))
377 return NamedDecl::ExternalLinkage;
378
379 // - a namespace (7.3), unless it is declared within an unnamed
380 // namespace.
381 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
382 return NamedDecl::ExternalLinkage;
383
384 return NamedDecl::NoLinkage;
385}
386
387NamedDecl::Linkage NamedDecl::getLinkage() const {
388 // Handle linkage for namespace-scope names.
389 if (getDeclContext()->getLookupContext()->isFileContext())
390 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
391 return L;
392
393 // C++ [basic.link]p5:
394 // In addition, a member function, static data member, a named
395 // class or enumeration of class scope, or an unnamed class or
396 // enumeration defined in a class-scope typedef declaration such
397 // that the class or enumeration has the typedef name for linkage
398 // purposes (7.1.3), has external linkage if the name of the class
399 // has external linkage.
400 if (getDeclContext()->isRecord() &&
401 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
402 (isa<TagDecl>(this) &&
403 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
404 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
405 return ExternalLinkage;
406
407 // C++ [basic.link]p6:
408 // The name of a function declared in block scope and the name of
409 // an object declared by a block scope extern declaration have
410 // linkage. If there is a visible declaration of an entity with
411 // linkage having the same name and type, ignoring entities
412 // declared outside the innermost enclosing namespace scope, the
413 // block scope declaration declares that same entity and receives
414 // the linkage of the previous declaration. If there is more than
415 // one such matching entity, the program is ill-formed. Otherwise,
416 // if no matching entity is found, the block scope entity receives
417 // external linkage.
418 if (getLexicalDeclContext()->isFunctionOrMethod()) {
419 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
420 if (Function->getPreviousDeclaration())
421 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
422 return L;
423
424 return ExternalLinkage;
425 }
426
427 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
428 if (Var->getStorageClass() == VarDecl::Extern ||
429 Var->getStorageClass() == VarDecl::PrivateExtern) {
430 if (Var->getPreviousDeclaration())
431 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
432 return L;
433
434 return ExternalLinkage;
435 }
436 }
437
438 // C++ [basic.link]p6:
439 // Names not covered by these rules have no linkage.
440 return NoLinkage;
441}
442
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000443std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000444 return getQualifiedNameAsString(getASTContext().getLangOptions());
445}
446
447std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000448 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
449 // std::string thrashing.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000450 std::vector<std::string> Names;
451 std::string QualName;
452 const DeclContext *Ctx = getDeclContext();
453
454 if (Ctx->isFunctionOrMethod())
455 return getNameAsString();
456
457 while (Ctx) {
458 if (Ctx->isFunctionOrMethod())
459 // FIXME: That probably will happen, when D was member of local
460 // scope class/struct/union. How do we handle this case?
461 break;
462
Mike Stump1eb44332009-09-09 15:08:12 +0000463 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000464 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
465 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
466 std::string TemplateArgsStr
467 = TemplateSpecializationType::PrintTemplateArgumentList(
468 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000469 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000470 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000471 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig6be11202009-12-24 23:15:03 +0000472 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
473 if (ND->isAnonymousNamespace())
474 Names.push_back("<anonymous namespace>");
475 else
476 Names.push_back(ND->getNameAsString());
477 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
478 if (!RD->getIdentifier()) {
479 std::string RecordString = "<anonymous ";
480 RecordString += RD->getKindName();
481 RecordString += ">";
482 Names.push_back(RecordString);
483 } else {
484 Names.push_back(RD->getNameAsString());
485 }
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000486 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000487 Names.push_back(ND->getNameAsString());
488 else
489 break;
490
491 Ctx = Ctx->getParent();
492 }
493
494 std::vector<std::string>::reverse_iterator
495 I = Names.rbegin(),
496 End = Names.rend();
497
498 for (; I!=End; ++I)
499 QualName += *I + "::";
500
501 QualName += getNameAsString();
502
503 return QualName;
504}
505
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000506bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000507 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
508
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000509 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
510 // We want to keep it, unless it nominates same namespace.
511 if (getKind() == Decl::UsingDirective) {
512 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
513 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000516 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
517 // For function declarations, we keep track of redeclarations.
518 return FD->getPreviousDeclaration() == OldD;
519
Douglas Gregore53060f2009-06-25 22:08:12 +0000520 // For function templates, the underlying function declarations are linked.
521 if (const FunctionTemplateDecl *FunctionTemplate
522 = dyn_cast<FunctionTemplateDecl>(this))
523 if (const FunctionTemplateDecl *OldFunctionTemplate
524 = dyn_cast<FunctionTemplateDecl>(OldD))
525 return FunctionTemplate->getTemplatedDecl()
526 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Steve Naroff0de21fd2009-02-22 19:35:57 +0000528 // For method declarations, we keep track of redeclarations.
529 if (isa<ObjCMethodDecl>(this))
530 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
John McCallf36e02d2009-10-09 21:13:30 +0000532 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
533 return true;
534
John McCall9488ea12009-11-17 05:59:44 +0000535 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
536 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
537 cast<UsingShadowDecl>(OldD)->getTargetDecl();
538
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000539 // For non-function declarations, if the declarations are of the
540 // same kind then this must be a redeclaration, or semantic analysis
541 // would not have given us the new declaration.
542 return this->getKind() == OldD->getKind();
543}
544
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000545bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000546 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000547}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000548
Anders Carlssone136e0e2009-06-26 06:29:23 +0000549NamedDecl *NamedDecl::getUnderlyingDecl() {
550 NamedDecl *ND = this;
551 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000552 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000553 ND = UD->getTargetDecl();
554 else if (ObjCCompatibleAliasDecl *AD
555 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
556 return AD->getClassInterface();
557 else
558 return ND;
559 }
560}
561
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000562//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000563// DeclaratorDecl Implementation
564//===----------------------------------------------------------------------===//
565
566SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000567 if (DeclInfo) {
568 TypeLoc TL = DeclInfo->getTypeLoc();
569 while (true) {
570 TypeLoc NextTL = TL.getNextTypeLoc();
571 if (!NextTL)
572 return TL.getSourceRange().getBegin();
573 TL = NextTL;
574 }
575 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000576 return SourceLocation();
577}
578
579//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000580// VarDecl Implementation
581//===----------------------------------------------------------------------===//
582
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000583VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000584 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000585 StorageClass S) {
John McCalla93c9342009-12-07 02:54:59 +0000586 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000587}
588
589void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000590 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000591 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000592 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000593 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
594 Eval->~EvaluatedStmt();
595 C.Deallocate(Eval);
596 }
597 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000598 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000599 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000600}
601
602VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000603}
604
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000605SourceRange VarDecl::getSourceRange() const {
606 if (getInit())
607 return SourceRange(getLocation(), getInit()->getLocEnd());
608 return SourceRange(getLocation(), getLocation());
609}
610
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000611bool VarDecl::isOutOfLine() const {
612 if (!isStaticDataMember())
613 return false;
614
615 if (Decl::isOutOfLine())
616 return true;
617
618 // If this static data member was instantiated from a static data member of
619 // a class template, check whether that static data member was defined
620 // out-of-line.
621 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
622 return VD->isOutOfLine();
623
624 return false;
625}
626
Douglas Gregor0d035142009-10-27 18:42:08 +0000627VarDecl *VarDecl::getOutOfLineDefinition() {
628 if (!isStaticDataMember())
629 return 0;
630
631 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
632 RD != RDEnd; ++RD) {
633 if (RD->getLexicalDeclContext()->isFileContext())
634 return *RD;
635 }
636
637 return 0;
638}
639
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000640VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000641 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000642 return cast<VarDecl>(MSI->getInstantiatedFrom());
643
644 return 0;
645}
646
Douglas Gregor663b5a02009-10-14 20:14:33 +0000647TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000648 if (MemberSpecializationInfo *MSI
649 = getASTContext().getInstantiatedFromStaticDataMember(this))
650 return MSI->getTemplateSpecializationKind();
651
652 return TSK_Undeclared;
653}
654
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000655MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000656 return getASTContext().getInstantiatedFromStaticDataMember(this);
657}
658
Douglas Gregor0a897e32009-10-15 17:21:20 +0000659void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
660 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000661 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000662 assert(MSI && "Not an instantiated static data member?");
663 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000664 if (TSK != TSK_ExplicitSpecialization &&
665 PointOfInstantiation.isValid() &&
666 MSI->getPointOfInstantiation().isInvalid())
667 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000668}
669
Douglas Gregor275a3692009-03-10 23:43:53 +0000670bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
671 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
672 return false;
673
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000674 const VarDecl *Def = 0;
675 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000676 (getStorageClass() == None || getStorageClass() == Static));
677}
678
Ted Kremenek082d9362009-03-20 21:35:28 +0000679const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000680 redecl_iterator I = redecls_begin(), E = redecls_end();
681 while (I != E && !I->getInit())
682 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000683
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000684 if (I != E) {
685 Def = *I;
686 return I->getInit();
687 }
688 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000689}
690
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000691VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000692 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000693}
694
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000695//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000696// FunctionDecl Implementation
697//===----------------------------------------------------------------------===//
698
Ted Kremenek27f8a282008-05-20 00:43:19 +0000699void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000700 if (Body && Body.isOffset())
701 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000702
703 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
704 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000705
Douglas Gregor2db32322009-10-07 23:56:10 +0000706 FunctionTemplateSpecializationInfo *FTSInfo
707 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
708 if (FTSInfo)
709 C.Deallocate(FTSInfo);
710
711 MemberSpecializationInfo *MSInfo
712 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
713 if (MSInfo)
714 C.Deallocate(MSInfo);
715
Steve Naroff3e970492009-01-27 21:25:57 +0000716 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000717
Ted Kremenek27f8a282008-05-20 00:43:19 +0000718 Decl::Destroy(C);
719}
720
John McCall136a6982009-09-11 06:45:03 +0000721void FunctionDecl::getNameForDiagnostic(std::string &S,
722 const PrintingPolicy &Policy,
723 bool Qualified) const {
724 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
725 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
726 if (TemplateArgs)
727 S += TemplateSpecializationType::PrintTemplateArgumentList(
728 TemplateArgs->getFlatArgumentList(),
729 TemplateArgs->flat_size(),
730 Policy);
731
732}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000733
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000734Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000735 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
736 if (I->Body) {
737 Definition = *I;
738 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000739 }
740 }
741
742 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000743}
744
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000745void FunctionDecl::setBody(Stmt *B) {
746 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000747 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000748 EndRangeLoc = B->getLocEnd();
749}
750
Douglas Gregor48a83b52009-09-12 00:17:51 +0000751bool FunctionDecl::isMain() const {
752 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000753 return !Context.getLangOptions().Freestanding &&
754 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000755 getIdentifier() && getIdentifier()->isStr("main");
756}
757
Douglas Gregor48a83b52009-09-12 00:17:51 +0000758bool FunctionDecl::isExternC() const {
759 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000760 // In C, any non-static, non-overloadable function has external
761 // linkage.
762 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000763 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000764
Mike Stump1eb44332009-09-09 15:08:12 +0000765 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000766 DC = DC->getParent()) {
767 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
768 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000769 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000770 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000771
772 break;
773 }
774 }
775
776 return false;
777}
778
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000779bool FunctionDecl::isGlobal() const {
780 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
781 return Method->isStatic();
782
783 if (getStorageClass() == Static)
784 return false;
785
Mike Stump1eb44332009-09-09 15:08:12 +0000786 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000787 DC->isNamespace();
788 DC = DC->getParent()) {
789 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
790 if (!Namespace->getDeclName())
791 return false;
792 break;
793 }
794 }
795
796 return true;
797}
798
Douglas Gregor3e41d602009-02-13 23:20:09 +0000799/// \brief Returns a value indicating whether this function
800/// corresponds to a builtin function.
801///
802/// The function corresponds to a built-in function if it is
803/// declared at translation scope or within an extern "C" block and
804/// its name matches with the name of a builtin. The returned value
805/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000806/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000807/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000808unsigned FunctionDecl::getBuiltinID() const {
809 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000810 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
811 return 0;
812
813 unsigned BuiltinID = getIdentifier()->getBuiltinID();
814 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
815 return BuiltinID;
816
817 // This function has the name of a known C library
818 // function. Determine whether it actually refers to the C library
819 // function or whether it just has the same name.
820
Douglas Gregor9add3172009-02-17 03:23:10 +0000821 // If this is a static function, it's not a builtin.
822 if (getStorageClass() == Static)
823 return 0;
824
Douglas Gregor3c385e52009-02-14 18:57:46 +0000825 // If this function is at translation-unit scope and we're not in
826 // C++, it refers to the C library function.
827 if (!Context.getLangOptions().CPlusPlus &&
828 getDeclContext()->isTranslationUnit())
829 return BuiltinID;
830
831 // If the function is in an extern "C" linkage specification and is
832 // not marked "overloadable", it's the real function.
833 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000834 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000835 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000836 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000837 return BuiltinID;
838
839 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000840 return 0;
841}
842
843
Chris Lattner1ad9b282009-04-25 06:03:53 +0000844/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000845/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000846/// after it has been created.
847unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000848 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000849 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000850 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000851 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853}
854
Ted Kremenekfc767612009-01-14 00:42:25 +0000855void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
856 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000858 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 // Zero params -> null pointer.
861 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000862 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000863 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000865
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000866 // Update source range. The check below allows us to set EndRangeLoc before
867 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000868 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000869 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 }
871}
872
Chris Lattner8123a952008-04-10 02:22:51 +0000873/// getMinRequiredArguments - Returns the minimum number of arguments
874/// needed to call this function. This may be fewer than the number of
875/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000876/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000877unsigned FunctionDecl::getMinRequiredArguments() const {
878 unsigned NumRequiredArgs = getNumParams();
879 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000880 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000881 --NumRequiredArgs;
882
883 return NumRequiredArgs;
884}
885
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000886bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000887 // FIXME: This is not enough. Consider:
888 //
889 // inline void f();
890 // void f() { }
891 //
892 // f is inlined, but does not have inline specified.
893 // To fix this we should add an 'inline' flag to FunctionDecl.
894 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000895 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000896
897 if (isa<CXXMethodDecl>(this)) {
898 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
899 return true;
900 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000901
902 switch (getTemplateSpecializationKind()) {
903 case TSK_Undeclared:
904 case TSK_ExplicitSpecialization:
905 return false;
906
907 case TSK_ImplicitInstantiation:
908 case TSK_ExplicitInstantiationDeclaration:
909 case TSK_ExplicitInstantiationDefinition:
910 // Handle below.
911 break;
912 }
913
914 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
915 Stmt *Pattern = 0;
916 if (PatternDecl)
917 Pattern = PatternDecl->getBody(PatternDecl);
918
919 if (Pattern && PatternDecl)
920 return PatternDecl->isInlined();
921
922 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000923}
924
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000925/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000926/// definition will be externally visible.
927///
928/// Inline function definitions are always available for inlining optimizations.
929/// However, depending on the language dialect, declaration specifiers, and
930/// attributes, the definition of an inline function may or may not be
931/// "externally" visible to other translation units in the program.
932///
933/// In C99, inline definitions are not externally visible by default. However,
934/// if even one of the globa-scope declarations is marked "extern inline", the
935/// inline definition becomes externally visible (C99 6.7.4p6).
936///
937/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
938/// definition, we use the GNU semantics for inline, which are nearly the
939/// opposite of C99 semantics. In particular, "inline" by itself will create
940/// an externally visible symbol, but "extern inline" will not create an
941/// externally visible symbol.
942bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
943 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000944 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000945 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000946
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000947 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000948 // GNU inline semantics. Based on a number of examples, we came up with the
949 // following heuristic: if the "inline" keyword is present on a
950 // declaration of the function but "extern" is not present on that
951 // declaration, then the symbol is externally visible. Otherwise, the GNU
952 // "extern inline" semantics applies and the symbol is not externally
953 // visible.
954 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
955 Redecl != RedeclEnd;
956 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000957 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000958 return true;
959 }
960
961 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000962 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000963 }
964
965 // C99 6.7.4p6:
966 // [...] If all of the file scope declarations for a function in a
967 // translation unit include the inline function specifier without extern,
968 // then the definition in that translation unit is an inline definition.
969 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
970 Redecl != RedeclEnd;
971 ++Redecl) {
972 // Only consider file-scope declarations in this test.
973 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
974 continue;
975
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000976 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000977 return true; // Not an inline definition
978 }
979
980 // C99 6.7.4p6:
981 // An inline definition does not provide an external definition for the
982 // function, and does not forbid an external definition in another
983 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000984 return false;
985}
986
Mike Stump1eb44332009-09-09 15:08:12 +0000987void
Douglas Gregor127102b2009-06-29 20:59:39 +0000988FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000989 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000990
Douglas Gregor127102b2009-06-29 20:59:39 +0000991 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000992 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000993 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
994 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
995 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
996 }
997}
998
Mike Stump740256b2009-09-29 00:50:50 +0000999const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1000 return getFirstDeclaration();
1001}
1002
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001003FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +00001004 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001005}
1006
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001007/// getOverloadedOperator - Which C++ overloaded operator this
1008/// function represents, if any.
1009OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001010 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1011 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001012 else
1013 return OO_None;
1014}
1015
Douglas Gregor2db32322009-10-07 23:56:10 +00001016FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001017 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001018 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1019
1020 return 0;
1021}
1022
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001023MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1024 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1025}
1026
Douglas Gregor2db32322009-10-07 23:56:10 +00001027void
1028FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1029 TemplateSpecializationKind TSK) {
1030 assert(TemplateOrSpecialization.isNull() &&
1031 "Member function is already a specialization");
1032 MemberSpecializationInfo *Info
1033 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1034 TemplateOrSpecialization = Info;
1035}
1036
Douglas Gregor3b846b62009-10-27 20:53:28 +00001037bool FunctionDecl::isImplicitlyInstantiable() const {
1038 // If this function already has a definition or is invalid, it can't be
1039 // implicitly instantiated.
1040 if (isInvalidDecl() || getBody())
1041 return false;
1042
1043 switch (getTemplateSpecializationKind()) {
1044 case TSK_Undeclared:
1045 case TSK_ExplicitSpecialization:
1046 case TSK_ExplicitInstantiationDefinition:
1047 return false;
1048
1049 case TSK_ImplicitInstantiation:
1050 return true;
1051
1052 case TSK_ExplicitInstantiationDeclaration:
1053 // Handled below.
1054 break;
1055 }
1056
1057 // Find the actual template from which we will instantiate.
1058 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1059 Stmt *Pattern = 0;
1060 if (PatternDecl)
1061 Pattern = PatternDecl->getBody(PatternDecl);
1062
1063 // C++0x [temp.explicit]p9:
1064 // Except for inline functions, other explicit instantiation declarations
1065 // have the effect of suppressing the implicit instantiation of the entity
1066 // to which they refer.
1067 if (!Pattern || !PatternDecl)
1068 return true;
1069
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001070 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001071}
1072
1073FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1074 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1075 while (Primary->getInstantiatedFromMemberTemplate()) {
1076 // If we have hit a point where the user provided a specialization of
1077 // this template, we're done looking.
1078 if (Primary->isMemberSpecialization())
1079 break;
1080
1081 Primary = Primary->getInstantiatedFromMemberTemplate();
1082 }
1083
1084 return Primary->getTemplatedDecl();
1085 }
1086
1087 return getInstantiatedFromMemberFunction();
1088}
1089
Douglas Gregor16e8be22009-06-29 17:30:29 +00001090FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001091 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001092 = TemplateOrSpecialization
1093 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001094 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001095 }
1096 return 0;
1097}
1098
1099const TemplateArgumentList *
1100FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001101 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001102 = TemplateOrSpecialization
1103 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001104 return Info->TemplateArguments;
1105 }
1106 return 0;
1107}
1108
Mike Stump1eb44332009-09-09 15:08:12 +00001109void
Douglas Gregor1637be72009-06-26 00:10:03 +00001110FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1111 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001112 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001113 void *InsertPos,
1114 TemplateSpecializationKind TSK) {
1115 assert(TSK != TSK_Undeclared &&
1116 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001117 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001118 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001119 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +00001120 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregor127102b2009-06-29 20:59:39 +00001122 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001123 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001124 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001125 Info->TemplateArguments = TemplateArgs;
1126 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Douglas Gregor127102b2009-06-29 20:59:39 +00001128 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001129 // function template specializations.
1130 if (InsertPos)
1131 Template->getSpecializations().InsertNode(Info, InsertPos);
1132 else {
1133 // Try to insert the new node. If there is an existing node, remove it
1134 // first.
1135 FunctionTemplateSpecializationInfo *Existing
1136 = Template->getSpecializations().GetOrInsertNode(Info);
1137 if (Existing) {
1138 Template->getSpecializations().RemoveNode(Existing);
1139 Template->getSpecializations().GetOrInsertNode(Info);
1140 }
1141 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001142}
1143
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001144TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001145 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001146 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001147 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001148 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001149 if (FTSInfo)
1150 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Douglas Gregor2db32322009-10-07 23:56:10 +00001152 MemberSpecializationInfo *MSInfo
1153 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1154 if (MSInfo)
1155 return MSInfo->getTemplateSpecializationKind();
1156
1157 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001158}
1159
Mike Stump1eb44332009-09-09 15:08:12 +00001160void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001161FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1162 SourceLocation PointOfInstantiation) {
1163 if (FunctionTemplateSpecializationInfo *FTSInfo
1164 = TemplateOrSpecialization.dyn_cast<
1165 FunctionTemplateSpecializationInfo*>()) {
1166 FTSInfo->setTemplateSpecializationKind(TSK);
1167 if (TSK != TSK_ExplicitSpecialization &&
1168 PointOfInstantiation.isValid() &&
1169 FTSInfo->getPointOfInstantiation().isInvalid())
1170 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1171 } else if (MemberSpecializationInfo *MSInfo
1172 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1173 MSInfo->setTemplateSpecializationKind(TSK);
1174 if (TSK != TSK_ExplicitSpecialization &&
1175 PointOfInstantiation.isValid() &&
1176 MSInfo->getPointOfInstantiation().isInvalid())
1177 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1178 } else
1179 assert(false && "Function cannot have a template specialization kind");
1180}
1181
1182SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001183 if (FunctionTemplateSpecializationInfo *FTSInfo
1184 = TemplateOrSpecialization.dyn_cast<
1185 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001186 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001187 else if (MemberSpecializationInfo *MSInfo
1188 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001189 return MSInfo->getPointOfInstantiation();
1190
1191 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001192}
1193
Douglas Gregor9f185072009-09-11 20:15:17 +00001194bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001195 if (Decl::isOutOfLine())
1196 return true;
1197
1198 // If this function was instantiated from a member function of a
1199 // class template, check whether that member function was defined out-of-line.
1200 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1201 const FunctionDecl *Definition;
1202 if (FD->getBody(Definition))
1203 return Definition->isOutOfLine();
1204 }
1205
1206 // If this function was instantiated from a function template,
1207 // check whether that function template was defined out-of-line.
1208 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1209 const FunctionDecl *Definition;
1210 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1211 return Definition->isOutOfLine();
1212 }
1213
1214 return false;
1215}
1216
Chris Lattner8a934232008-03-31 00:36:02 +00001217//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001218// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001219//===----------------------------------------------------------------------===//
1220
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001221SourceRange TagDecl::getSourceRange() const {
1222 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001223 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001224}
1225
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001226TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001227 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001228}
1229
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001230void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001231 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1232 TagT->decl.setPointer(this);
1233 TagT->decl.setInt(1);
1234 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001235}
1236
1237void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001238 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001239 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1240 assert(TagT->decl.getPointer() == this &&
1241 "Attempt to redefine a tag definition?");
1242 TagT->decl.setInt(0);
1243 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001244}
1245
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001246TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001247 if (isDefinition())
1248 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001249
1250 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001251 R != REnd; ++R)
1252 if (R->isDefinition())
1253 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001255 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001256}
1257
John McCallf1bbbb42009-09-04 01:14:41 +00001258TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1259 switch (TypeSpec) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001260 default: llvm_unreachable("unexpected type specifier");
John McCallf1bbbb42009-09-04 01:14:41 +00001261 case DeclSpec::TST_struct: return TK_struct;
1262 case DeclSpec::TST_class: return TK_class;
1263 case DeclSpec::TST_union: return TK_union;
1264 case DeclSpec::TST_enum: return TK_enum;
1265 }
1266}
1267
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001268//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001269// RecordDecl Implementation
1270//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001271
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001272RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001273 IdentifierInfo *Id, RecordDecl *PrevDecl,
1274 SourceLocation TKL)
1275 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001276 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001277 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001278 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001279 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001280}
1281
1282RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001283 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001284 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001286 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001287 C.getTypeDeclType(R, PrevDecl);
1288 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001289}
1290
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001291RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001292}
1293
1294void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001295 TagDecl::Destroy(C);
1296}
1297
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001298bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001299 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001300 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1301}
1302
Douglas Gregor44b43212008-12-11 16:49:14 +00001303/// completeDefinition - Notes that the definition of this type is now
1304/// complete.
1305void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001307 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001308}
1309
Steve Naroff56ee6892008-10-08 17:01:13 +00001310//===----------------------------------------------------------------------===//
1311// BlockDecl Implementation
1312//===----------------------------------------------------------------------===//
1313
1314BlockDecl::~BlockDecl() {
1315}
1316
1317void BlockDecl::Destroy(ASTContext& C) {
1318 if (Body)
1319 Body->Destroy(C);
1320
1321 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1322 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001323
1324 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001325 Decl::Destroy(C);
1326}
Steve Naroffe78b8092009-03-13 16:56:44 +00001327
1328void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1329 unsigned NParms) {
1330 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Steve Naroffe78b8092009-03-13 16:56:44 +00001332 // Zero params -> null pointer.
1333 if (NParms) {
1334 NumParams = NParms;
1335 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1336 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1337 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1338 }
1339}
1340
1341unsigned BlockDecl::getNumParams() const {
1342 return NumParams;
1343}