blob: 02a26d49f04f4097c0a195ec8c13f84f75fa9bac [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) {
Mike Stump1eb44332009-09-09 15:08:12 +0000458 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000459 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
460 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
461 std::string TemplateArgsStr
462 = TemplateSpecializationType::PrintTemplateArgumentList(
463 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000464 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000465 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000466 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig6be11202009-12-24 23:15:03 +0000467 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
468 if (ND->isAnonymousNamespace())
469 Names.push_back("<anonymous namespace>");
470 else
471 Names.push_back(ND->getNameAsString());
472 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
473 if (!RD->getIdentifier()) {
474 std::string RecordString = "<anonymous ";
475 RecordString += RD->getKindName();
476 RecordString += ">";
477 Names.push_back(RecordString);
478 } else {
479 Names.push_back(RD->getNameAsString());
480 }
Sam Weinig3521d012009-12-28 03:19:38 +0000481 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
482 std::string Proto = FD->getNameAsString();
483
484 const FunctionProtoType *FT = 0;
485 if (FD->hasWrittenPrototype())
486 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
487
488 Proto += "(";
489 if (FT) {
490 llvm::raw_string_ostream POut(Proto);
491 unsigned NumParams = FD->getNumParams();
492 for (unsigned i = 0; i < NumParams; ++i) {
493 if (i)
494 POut << ", ";
495 std::string Param;
496 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
497 POut << Param;
498 }
499
500 if (FT->isVariadic()) {
501 if (NumParams > 0)
502 POut << ", ";
503 POut << "...";
504 }
505 }
506 Proto += ")";
507
508 Names.push_back(Proto);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000509 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000510 Names.push_back(ND->getNameAsString());
511 else
512 break;
513
514 Ctx = Ctx->getParent();
515 }
516
517 std::vector<std::string>::reverse_iterator
518 I = Names.rbegin(),
519 End = Names.rend();
520
521 for (; I!=End; ++I)
522 QualName += *I + "::";
523
524 QualName += getNameAsString();
525
526 return QualName;
527}
528
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000529bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000530 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
531
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000532 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
533 // We want to keep it, unless it nominates same namespace.
534 if (getKind() == Decl::UsingDirective) {
535 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
536 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000539 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
540 // For function declarations, we keep track of redeclarations.
541 return FD->getPreviousDeclaration() == OldD;
542
Douglas Gregore53060f2009-06-25 22:08:12 +0000543 // For function templates, the underlying function declarations are linked.
544 if (const FunctionTemplateDecl *FunctionTemplate
545 = dyn_cast<FunctionTemplateDecl>(this))
546 if (const FunctionTemplateDecl *OldFunctionTemplate
547 = dyn_cast<FunctionTemplateDecl>(OldD))
548 return FunctionTemplate->getTemplatedDecl()
549 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Steve Naroff0de21fd2009-02-22 19:35:57 +0000551 // For method declarations, we keep track of redeclarations.
552 if (isa<ObjCMethodDecl>(this))
553 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
John McCallf36e02d2009-10-09 21:13:30 +0000555 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
556 return true;
557
John McCall9488ea12009-11-17 05:59:44 +0000558 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
559 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
560 cast<UsingShadowDecl>(OldD)->getTargetDecl();
561
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000562 // For non-function declarations, if the declarations are of the
563 // same kind then this must be a redeclaration, or semantic analysis
564 // would not have given us the new declaration.
565 return this->getKind() == OldD->getKind();
566}
567
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000568bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000569 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000570}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000571
Anders Carlssone136e0e2009-06-26 06:29:23 +0000572NamedDecl *NamedDecl::getUnderlyingDecl() {
573 NamedDecl *ND = this;
574 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000575 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000576 ND = UD->getTargetDecl();
577 else if (ObjCCompatibleAliasDecl *AD
578 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
579 return AD->getClassInterface();
580 else
581 return ND;
582 }
583}
584
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000585//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000586// DeclaratorDecl Implementation
587//===----------------------------------------------------------------------===//
588
589SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000590 if (DeclInfo) {
591 TypeLoc TL = DeclInfo->getTypeLoc();
592 while (true) {
593 TypeLoc NextTL = TL.getNextTypeLoc();
594 if (!NextTL)
595 return TL.getSourceRange().getBegin();
596 TL = NextTL;
597 }
598 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000599 return SourceLocation();
600}
601
602//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000603// VarDecl Implementation
604//===----------------------------------------------------------------------===//
605
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000606VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000607 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000608 StorageClass S) {
John McCalla93c9342009-12-07 02:54:59 +0000609 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000610}
611
612void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000613 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000614 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000615 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000616 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
617 Eval->~EvaluatedStmt();
618 C.Deallocate(Eval);
619 }
620 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000621 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000622 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000623}
624
625VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000626}
627
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000628SourceRange VarDecl::getSourceRange() const {
629 if (getInit())
630 return SourceRange(getLocation(), getInit()->getLocEnd());
631 return SourceRange(getLocation(), getLocation());
632}
633
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000634bool VarDecl::isOutOfLine() const {
635 if (!isStaticDataMember())
636 return false;
637
638 if (Decl::isOutOfLine())
639 return true;
640
641 // If this static data member was instantiated from a static data member of
642 // a class template, check whether that static data member was defined
643 // out-of-line.
644 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
645 return VD->isOutOfLine();
646
647 return false;
648}
649
Douglas Gregor0d035142009-10-27 18:42:08 +0000650VarDecl *VarDecl::getOutOfLineDefinition() {
651 if (!isStaticDataMember())
652 return 0;
653
654 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
655 RD != RDEnd; ++RD) {
656 if (RD->getLexicalDeclContext()->isFileContext())
657 return *RD;
658 }
659
660 return 0;
661}
662
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000663VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000664 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000665 return cast<VarDecl>(MSI->getInstantiatedFrom());
666
667 return 0;
668}
669
Douglas Gregor663b5a02009-10-14 20:14:33 +0000670TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000671 if (MemberSpecializationInfo *MSI
672 = getASTContext().getInstantiatedFromStaticDataMember(this))
673 return MSI->getTemplateSpecializationKind();
674
675 return TSK_Undeclared;
676}
677
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000678MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000679 return getASTContext().getInstantiatedFromStaticDataMember(this);
680}
681
Douglas Gregor0a897e32009-10-15 17:21:20 +0000682void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
683 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000684 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000685 assert(MSI && "Not an instantiated static data member?");
686 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000687 if (TSK != TSK_ExplicitSpecialization &&
688 PointOfInstantiation.isValid() &&
689 MSI->getPointOfInstantiation().isInvalid())
690 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000691}
692
Douglas Gregor275a3692009-03-10 23:43:53 +0000693bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
694 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
695 return false;
696
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000697 const VarDecl *Def = 0;
698 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000699 (getStorageClass() == None || getStorageClass() == Static));
700}
701
Ted Kremenek082d9362009-03-20 21:35:28 +0000702const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000703 redecl_iterator I = redecls_begin(), E = redecls_end();
704 while (I != E && !I->getInit())
705 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000706
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000707 if (I != E) {
708 Def = *I;
709 return I->getInit();
710 }
711 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000712}
713
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000714VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000715 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000716}
717
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000718//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000719// FunctionDecl Implementation
720//===----------------------------------------------------------------------===//
721
Ted Kremenek27f8a282008-05-20 00:43:19 +0000722void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000723 if (Body && Body.isOffset())
724 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000725
726 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
727 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000728
Douglas Gregor2db32322009-10-07 23:56:10 +0000729 FunctionTemplateSpecializationInfo *FTSInfo
730 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
731 if (FTSInfo)
732 C.Deallocate(FTSInfo);
733
734 MemberSpecializationInfo *MSInfo
735 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
736 if (MSInfo)
737 C.Deallocate(MSInfo);
738
Steve Naroff3e970492009-01-27 21:25:57 +0000739 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000740
Ted Kremenek27f8a282008-05-20 00:43:19 +0000741 Decl::Destroy(C);
742}
743
John McCall136a6982009-09-11 06:45:03 +0000744void FunctionDecl::getNameForDiagnostic(std::string &S,
745 const PrintingPolicy &Policy,
746 bool Qualified) const {
747 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
748 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
749 if (TemplateArgs)
750 S += TemplateSpecializationType::PrintTemplateArgumentList(
751 TemplateArgs->getFlatArgumentList(),
752 TemplateArgs->flat_size(),
753 Policy);
754
755}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000756
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000757Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000758 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
759 if (I->Body) {
760 Definition = *I;
761 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000762 }
763 }
764
765 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000766}
767
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000768void FunctionDecl::setBody(Stmt *B) {
769 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000770 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000771 EndRangeLoc = B->getLocEnd();
772}
773
Douglas Gregor48a83b52009-09-12 00:17:51 +0000774bool FunctionDecl::isMain() const {
775 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000776 return !Context.getLangOptions().Freestanding &&
777 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000778 getIdentifier() && getIdentifier()->isStr("main");
779}
780
Douglas Gregor48a83b52009-09-12 00:17:51 +0000781bool FunctionDecl::isExternC() const {
782 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000783 // In C, any non-static, non-overloadable function has external
784 // linkage.
785 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000786 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000787
Mike Stump1eb44332009-09-09 15:08:12 +0000788 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000789 DC = DC->getParent()) {
790 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
791 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000792 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000793 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000794
795 break;
796 }
797 }
798
799 return false;
800}
801
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000802bool FunctionDecl::isGlobal() const {
803 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
804 return Method->isStatic();
805
806 if (getStorageClass() == Static)
807 return false;
808
Mike Stump1eb44332009-09-09 15:08:12 +0000809 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000810 DC->isNamespace();
811 DC = DC->getParent()) {
812 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
813 if (!Namespace->getDeclName())
814 return false;
815 break;
816 }
817 }
818
819 return true;
820}
821
Douglas Gregor3e41d602009-02-13 23:20:09 +0000822/// \brief Returns a value indicating whether this function
823/// corresponds to a builtin function.
824///
825/// The function corresponds to a built-in function if it is
826/// declared at translation scope or within an extern "C" block and
827/// its name matches with the name of a builtin. The returned value
828/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000829/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000830/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000831unsigned FunctionDecl::getBuiltinID() const {
832 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000833 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
834 return 0;
835
836 unsigned BuiltinID = getIdentifier()->getBuiltinID();
837 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
838 return BuiltinID;
839
840 // This function has the name of a known C library
841 // function. Determine whether it actually refers to the C library
842 // function or whether it just has the same name.
843
Douglas Gregor9add3172009-02-17 03:23:10 +0000844 // If this is a static function, it's not a builtin.
845 if (getStorageClass() == Static)
846 return 0;
847
Douglas Gregor3c385e52009-02-14 18:57:46 +0000848 // If this function is at translation-unit scope and we're not in
849 // C++, it refers to the C library function.
850 if (!Context.getLangOptions().CPlusPlus &&
851 getDeclContext()->isTranslationUnit())
852 return BuiltinID;
853
854 // If the function is in an extern "C" linkage specification and is
855 // not marked "overloadable", it's the real function.
856 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000857 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000858 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000859 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000860 return BuiltinID;
861
862 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000863 return 0;
864}
865
866
Chris Lattner1ad9b282009-04-25 06:03:53 +0000867/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000868/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000869/// after it has been created.
870unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000871 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000872 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000873 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000874 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876}
877
Ted Kremenekfc767612009-01-14 00:42:25 +0000878void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
879 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000881 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 // Zero params -> null pointer.
884 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000885 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000886 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000888
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000889 // Update source range. The check below allows us to set EndRangeLoc before
890 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000891 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000892 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 }
894}
895
Chris Lattner8123a952008-04-10 02:22:51 +0000896/// getMinRequiredArguments - Returns the minimum number of arguments
897/// needed to call this function. This may be fewer than the number of
898/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000899/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000900unsigned FunctionDecl::getMinRequiredArguments() const {
901 unsigned NumRequiredArgs = getNumParams();
902 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000903 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000904 --NumRequiredArgs;
905
906 return NumRequiredArgs;
907}
908
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000909bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000910 // FIXME: This is not enough. Consider:
911 //
912 // inline void f();
913 // void f() { }
914 //
915 // f is inlined, but does not have inline specified.
916 // To fix this we should add an 'inline' flag to FunctionDecl.
917 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000918 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000919
920 if (isa<CXXMethodDecl>(this)) {
921 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
922 return true;
923 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000924
925 switch (getTemplateSpecializationKind()) {
926 case TSK_Undeclared:
927 case TSK_ExplicitSpecialization:
928 return false;
929
930 case TSK_ImplicitInstantiation:
931 case TSK_ExplicitInstantiationDeclaration:
932 case TSK_ExplicitInstantiationDefinition:
933 // Handle below.
934 break;
935 }
936
937 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
938 Stmt *Pattern = 0;
939 if (PatternDecl)
940 Pattern = PatternDecl->getBody(PatternDecl);
941
942 if (Pattern && PatternDecl)
943 return PatternDecl->isInlined();
944
945 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000946}
947
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000948/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000949/// definition will be externally visible.
950///
951/// Inline function definitions are always available for inlining optimizations.
952/// However, depending on the language dialect, declaration specifiers, and
953/// attributes, the definition of an inline function may or may not be
954/// "externally" visible to other translation units in the program.
955///
956/// In C99, inline definitions are not externally visible by default. However,
957/// if even one of the globa-scope declarations is marked "extern inline", the
958/// inline definition becomes externally visible (C99 6.7.4p6).
959///
960/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
961/// definition, we use the GNU semantics for inline, which are nearly the
962/// opposite of C99 semantics. In particular, "inline" by itself will create
963/// an externally visible symbol, but "extern inline" will not create an
964/// externally visible symbol.
965bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
966 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000967 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000968 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000969
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000970 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000971 // GNU inline semantics. Based on a number of examples, we came up with the
972 // following heuristic: if the "inline" keyword is present on a
973 // declaration of the function but "extern" is not present on that
974 // declaration, then the symbol is externally visible. Otherwise, the GNU
975 // "extern inline" semantics applies and the symbol is not externally
976 // visible.
977 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
978 Redecl != RedeclEnd;
979 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000980 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000981 return true;
982 }
983
984 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000985 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000986 }
987
988 // C99 6.7.4p6:
989 // [...] If all of the file scope declarations for a function in a
990 // translation unit include the inline function specifier without extern,
991 // then the definition in that translation unit is an inline definition.
992 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
993 Redecl != RedeclEnd;
994 ++Redecl) {
995 // Only consider file-scope declarations in this test.
996 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
997 continue;
998
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000999 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001000 return true; // Not an inline definition
1001 }
1002
1003 // C99 6.7.4p6:
1004 // An inline definition does not provide an external definition for the
1005 // function, and does not forbid an external definition in another
1006 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001007 return false;
1008}
1009
Mike Stump1eb44332009-09-09 15:08:12 +00001010void
Douglas Gregor127102b2009-06-29 20:59:39 +00001011FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +00001012 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +00001013
Douglas Gregor127102b2009-06-29 20:59:39 +00001014 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001015 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +00001016 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1017 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1018 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1019 }
1020}
1021
Mike Stump740256b2009-09-29 00:50:50 +00001022const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1023 return getFirstDeclaration();
1024}
1025
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001026FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +00001027 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001028}
1029
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001030/// getOverloadedOperator - Which C++ overloaded operator this
1031/// function represents, if any.
1032OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001033 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1034 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001035 else
1036 return OO_None;
1037}
1038
Douglas Gregor2db32322009-10-07 23:56:10 +00001039FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001040 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001041 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1042
1043 return 0;
1044}
1045
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001046MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1047 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1048}
1049
Douglas Gregor2db32322009-10-07 23:56:10 +00001050void
1051FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1052 TemplateSpecializationKind TSK) {
1053 assert(TemplateOrSpecialization.isNull() &&
1054 "Member function is already a specialization");
1055 MemberSpecializationInfo *Info
1056 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1057 TemplateOrSpecialization = Info;
1058}
1059
Douglas Gregor3b846b62009-10-27 20:53:28 +00001060bool FunctionDecl::isImplicitlyInstantiable() const {
1061 // If this function already has a definition or is invalid, it can't be
1062 // implicitly instantiated.
1063 if (isInvalidDecl() || getBody())
1064 return false;
1065
1066 switch (getTemplateSpecializationKind()) {
1067 case TSK_Undeclared:
1068 case TSK_ExplicitSpecialization:
1069 case TSK_ExplicitInstantiationDefinition:
1070 return false;
1071
1072 case TSK_ImplicitInstantiation:
1073 return true;
1074
1075 case TSK_ExplicitInstantiationDeclaration:
1076 // Handled below.
1077 break;
1078 }
1079
1080 // Find the actual template from which we will instantiate.
1081 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1082 Stmt *Pattern = 0;
1083 if (PatternDecl)
1084 Pattern = PatternDecl->getBody(PatternDecl);
1085
1086 // C++0x [temp.explicit]p9:
1087 // Except for inline functions, other explicit instantiation declarations
1088 // have the effect of suppressing the implicit instantiation of the entity
1089 // to which they refer.
1090 if (!Pattern || !PatternDecl)
1091 return true;
1092
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001093 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001094}
1095
1096FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1097 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1098 while (Primary->getInstantiatedFromMemberTemplate()) {
1099 // If we have hit a point where the user provided a specialization of
1100 // this template, we're done looking.
1101 if (Primary->isMemberSpecialization())
1102 break;
1103
1104 Primary = Primary->getInstantiatedFromMemberTemplate();
1105 }
1106
1107 return Primary->getTemplatedDecl();
1108 }
1109
1110 return getInstantiatedFromMemberFunction();
1111}
1112
Douglas Gregor16e8be22009-06-29 17:30:29 +00001113FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001114 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001115 = TemplateOrSpecialization
1116 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001117 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001118 }
1119 return 0;
1120}
1121
1122const TemplateArgumentList *
1123FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001124 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001125 = TemplateOrSpecialization
1126 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001127 return Info->TemplateArguments;
1128 }
1129 return 0;
1130}
1131
Mike Stump1eb44332009-09-09 15:08:12 +00001132void
Douglas Gregor1637be72009-06-26 00:10:03 +00001133FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1134 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001135 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001136 void *InsertPos,
1137 TemplateSpecializationKind TSK) {
1138 assert(TSK != TSK_Undeclared &&
1139 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001140 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001141 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001142 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +00001143 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Douglas Gregor127102b2009-06-29 20:59:39 +00001145 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001146 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001147 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001148 Info->TemplateArguments = TemplateArgs;
1149 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Douglas Gregor127102b2009-06-29 20:59:39 +00001151 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001152 // function template specializations.
1153 if (InsertPos)
1154 Template->getSpecializations().InsertNode(Info, InsertPos);
1155 else {
1156 // Try to insert the new node. If there is an existing node, remove it
1157 // first.
1158 FunctionTemplateSpecializationInfo *Existing
1159 = Template->getSpecializations().GetOrInsertNode(Info);
1160 if (Existing) {
1161 Template->getSpecializations().RemoveNode(Existing);
1162 Template->getSpecializations().GetOrInsertNode(Info);
1163 }
1164 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001165}
1166
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001167TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001168 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001169 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001170 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001171 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001172 if (FTSInfo)
1173 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregor2db32322009-10-07 23:56:10 +00001175 MemberSpecializationInfo *MSInfo
1176 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1177 if (MSInfo)
1178 return MSInfo->getTemplateSpecializationKind();
1179
1180 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001181}
1182
Mike Stump1eb44332009-09-09 15:08:12 +00001183void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001184FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1185 SourceLocation PointOfInstantiation) {
1186 if (FunctionTemplateSpecializationInfo *FTSInfo
1187 = TemplateOrSpecialization.dyn_cast<
1188 FunctionTemplateSpecializationInfo*>()) {
1189 FTSInfo->setTemplateSpecializationKind(TSK);
1190 if (TSK != TSK_ExplicitSpecialization &&
1191 PointOfInstantiation.isValid() &&
1192 FTSInfo->getPointOfInstantiation().isInvalid())
1193 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1194 } else if (MemberSpecializationInfo *MSInfo
1195 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1196 MSInfo->setTemplateSpecializationKind(TSK);
1197 if (TSK != TSK_ExplicitSpecialization &&
1198 PointOfInstantiation.isValid() &&
1199 MSInfo->getPointOfInstantiation().isInvalid())
1200 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1201 } else
1202 assert(false && "Function cannot have a template specialization kind");
1203}
1204
1205SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001206 if (FunctionTemplateSpecializationInfo *FTSInfo
1207 = TemplateOrSpecialization.dyn_cast<
1208 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001209 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001210 else if (MemberSpecializationInfo *MSInfo
1211 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001212 return MSInfo->getPointOfInstantiation();
1213
1214 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001215}
1216
Douglas Gregor9f185072009-09-11 20:15:17 +00001217bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001218 if (Decl::isOutOfLine())
1219 return true;
1220
1221 // If this function was instantiated from a member function of a
1222 // class template, check whether that member function was defined out-of-line.
1223 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1224 const FunctionDecl *Definition;
1225 if (FD->getBody(Definition))
1226 return Definition->isOutOfLine();
1227 }
1228
1229 // If this function was instantiated from a function template,
1230 // check whether that function template was defined out-of-line.
1231 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1232 const FunctionDecl *Definition;
1233 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1234 return Definition->isOutOfLine();
1235 }
1236
1237 return false;
1238}
1239
Chris Lattner8a934232008-03-31 00:36:02 +00001240//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001241// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001242//===----------------------------------------------------------------------===//
1243
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001244SourceRange TagDecl::getSourceRange() const {
1245 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001246 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001247}
1248
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001249TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001250 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001251}
1252
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001253void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001254 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1255 TagT->decl.setPointer(this);
1256 TagT->decl.setInt(1);
1257 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001258}
1259
1260void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001261 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001262 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1263 assert(TagT->decl.getPointer() == this &&
1264 "Attempt to redefine a tag definition?");
1265 TagT->decl.setInt(0);
1266 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001267}
1268
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001269TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001270 if (isDefinition())
1271 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001272
1273 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001274 R != REnd; ++R)
1275 if (R->isDefinition())
1276 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001278 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001279}
1280
John McCallf1bbbb42009-09-04 01:14:41 +00001281TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1282 switch (TypeSpec) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001283 default: llvm_unreachable("unexpected type specifier");
John McCallf1bbbb42009-09-04 01:14:41 +00001284 case DeclSpec::TST_struct: return TK_struct;
1285 case DeclSpec::TST_class: return TK_class;
1286 case DeclSpec::TST_union: return TK_union;
1287 case DeclSpec::TST_enum: return TK_enum;
1288 }
1289}
1290
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001291//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001292// RecordDecl Implementation
1293//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001294
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001295RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001296 IdentifierInfo *Id, RecordDecl *PrevDecl,
1297 SourceLocation TKL)
1298 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001299 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001300 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001301 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001302 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001303}
1304
1305RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001306 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001307 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001309 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001310 C.getTypeDeclType(R, PrevDecl);
1311 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001312}
1313
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001314RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001315}
1316
1317void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001318 TagDecl::Destroy(C);
1319}
1320
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001321bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001322 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001323 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1324}
1325
Douglas Gregor44b43212008-12-11 16:49:14 +00001326/// completeDefinition - Notes that the definition of this type is now
1327/// complete.
1328void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001330 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001331}
1332
Steve Naroff56ee6892008-10-08 17:01:13 +00001333//===----------------------------------------------------------------------===//
1334// BlockDecl Implementation
1335//===----------------------------------------------------------------------===//
1336
1337BlockDecl::~BlockDecl() {
1338}
1339
1340void BlockDecl::Destroy(ASTContext& C) {
1341 if (Body)
1342 Body->Destroy(C);
1343
1344 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1345 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001346
1347 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001348 Decl::Destroy(C);
1349}
Steve Naroffe78b8092009-03-13 16:56:44 +00001350
1351void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1352 unsigned NParms) {
1353 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Steve Naroffe78b8092009-03-13 16:56:44 +00001355 // Zero params -> null pointer.
1356 if (NParms) {
1357 NumParams = NParms;
1358 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1359 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1360 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1361 }
1362}
1363
1364unsigned BlockDecl::getNumParams() const {
1365 return NumParams;
1366}