blob: e77661a9fbd4e83e48e17c20a146e68f22a5fd99 [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
John McCall5126fd02009-12-30 00:31:22 +0000215// Anchor TypedefDecl's vtable here.
216TypedefDecl::~TypedefDecl() {}
217
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000218EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000219 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000220 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000221 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000222 C.getTypeDeclType(Enum, PrevDecl);
223 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000224}
225
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000226void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000227 Decl::Destroy(C);
228}
229
John McCall842aef82009-12-09 09:09:27 +0000230void EnumDecl::completeDefinition(ASTContext &C,
231 QualType NewType,
232 QualType NewPromotionType) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000233 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000234 IntegerType = NewType;
John McCall842aef82009-12-09 09:09:27 +0000235 PromotionType = NewPromotionType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000236 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000237}
238
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000239FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000240 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000241 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000242 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000243}
244
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000245//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000246// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000247//===----------------------------------------------------------------------===//
248
Douglas Gregord85b5b92009-11-25 22:24:25 +0000249static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
250 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
251 "Not a name having namespace scope");
252 ASTContext &Context = D->getASTContext();
253
254 // C++ [basic.link]p3:
255 // A name having namespace scope (3.3.6) has internal linkage if it
256 // is the name of
257 // - an object, reference, function or function template that is
258 // explicitly declared static; or,
259 // (This bullet corresponds to C99 6.2.2p3.)
260 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
261 // Explicitly declared static.
262 if (Var->getStorageClass() == VarDecl::Static)
263 return NamedDecl::InternalLinkage;
264
265 // - an object or reference that is explicitly declared const
266 // and neither explicitly declared extern nor previously
267 // declared to have external linkage; or
268 // (there is no equivalent in C99)
269 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000270 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000271 Var->getStorageClass() != VarDecl::Extern &&
272 Var->getStorageClass() != VarDecl::PrivateExtern) {
273 bool FoundExtern = false;
274 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
275 PrevVar && !FoundExtern;
276 PrevVar = PrevVar->getPreviousDeclaration())
277 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
278 FoundExtern = true;
279
280 if (!FoundExtern)
281 return NamedDecl::InternalLinkage;
282 }
283 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
284 const FunctionDecl *Function = 0;
285 if (const FunctionTemplateDecl *FunTmpl
286 = dyn_cast<FunctionTemplateDecl>(D))
287 Function = FunTmpl->getTemplatedDecl();
288 else
289 Function = cast<FunctionDecl>(D);
290
291 // Explicitly declared static.
292 if (Function->getStorageClass() == FunctionDecl::Static)
293 return NamedDecl::InternalLinkage;
294 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
295 // - a data member of an anonymous union.
296 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
297 return NamedDecl::InternalLinkage;
298 }
299
300 // C++ [basic.link]p4:
301
302 // A name having namespace scope has external linkage if it is the
303 // name of
304 //
305 // - an object or reference, unless it has internal linkage; or
306 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
307 if (!Context.getLangOptions().CPlusPlus &&
308 (Var->getStorageClass() == VarDecl::Extern ||
309 Var->getStorageClass() == VarDecl::PrivateExtern)) {
310 // C99 6.2.2p4:
311 // For an identifier declared with the storage-class specifier
312 // extern in a scope in which a prior declaration of that
313 // identifier is visible, if the prior declaration specifies
314 // internal or external linkage, the linkage of the identifier
315 // at the later declaration is the same as the linkage
316 // specified at the prior declaration. If no prior declaration
317 // is visible, or if the prior declaration specifies no
318 // linkage, then the identifier has external linkage.
319 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
320 if (NamedDecl::Linkage L = PrevVar->getLinkage())
321 return L;
322 }
323 }
324
325 // C99 6.2.2p5:
326 // If the declaration of an identifier for an object has file
327 // scope and no storage-class specifier, its linkage is
328 // external.
329 return NamedDecl::ExternalLinkage;
330 }
331
332 // - a function, unless it has internal linkage; or
333 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
334 // C99 6.2.2p5:
335 // If the declaration of an identifier for a function has no
336 // storage-class specifier, its linkage is determined exactly
337 // as if it were declared with the storage-class specifier
338 // extern.
339 if (!Context.getLangOptions().CPlusPlus &&
340 (Function->getStorageClass() == FunctionDecl::Extern ||
341 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
342 Function->getStorageClass() == FunctionDecl::None)) {
343 // C99 6.2.2p4:
344 // For an identifier declared with the storage-class specifier
345 // extern in a scope in which a prior declaration of that
346 // identifier is visible, if the prior declaration specifies
347 // internal or external linkage, the linkage of the identifier
348 // at the later declaration is the same as the linkage
349 // specified at the prior declaration. If no prior declaration
350 // is visible, or if the prior declaration specifies no
351 // linkage, then the identifier has external linkage.
352 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
353 if (NamedDecl::Linkage L = PrevFunc->getLinkage())
354 return L;
355 }
356 }
357
358 return NamedDecl::ExternalLinkage;
359 }
360
361 // - a named class (Clause 9), or an unnamed class defined in a
362 // typedef declaration in which the class has the typedef name
363 // for linkage purposes (7.1.3); or
364 // - a named enumeration (7.2), or an unnamed enumeration
365 // defined in a typedef declaration in which the enumeration
366 // has the typedef name for linkage purposes (7.1.3); or
367 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
368 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
369 return NamedDecl::ExternalLinkage;
370
371 // - an enumerator belonging to an enumeration with external linkage;
372 if (isa<EnumConstantDecl>(D))
373 if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
374 == NamedDecl::ExternalLinkage)
375 return NamedDecl::ExternalLinkage;
376
377 // - a template, unless it is a function template that has
378 // internal linkage (Clause 14);
379 if (isa<TemplateDecl>(D))
380 return NamedDecl::ExternalLinkage;
381
382 // - a namespace (7.3), unless it is declared within an unnamed
383 // namespace.
384 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
385 return NamedDecl::ExternalLinkage;
386
387 return NamedDecl::NoLinkage;
388}
389
390NamedDecl::Linkage NamedDecl::getLinkage() const {
391 // Handle linkage for namespace-scope names.
392 if (getDeclContext()->getLookupContext()->isFileContext())
393 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
394 return L;
395
396 // C++ [basic.link]p5:
397 // In addition, a member function, static data member, a named
398 // class or enumeration of class scope, or an unnamed class or
399 // enumeration defined in a class-scope typedef declaration such
400 // that the class or enumeration has the typedef name for linkage
401 // purposes (7.1.3), has external linkage if the name of the class
402 // has external linkage.
403 if (getDeclContext()->isRecord() &&
404 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
405 (isa<TagDecl>(this) &&
406 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
407 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
408 return ExternalLinkage;
409
410 // C++ [basic.link]p6:
411 // The name of a function declared in block scope and the name of
412 // an object declared by a block scope extern declaration have
413 // linkage. If there is a visible declaration of an entity with
414 // linkage having the same name and type, ignoring entities
415 // declared outside the innermost enclosing namespace scope, the
416 // block scope declaration declares that same entity and receives
417 // the linkage of the previous declaration. If there is more than
418 // one such matching entity, the program is ill-formed. Otherwise,
419 // if no matching entity is found, the block scope entity receives
420 // external linkage.
421 if (getLexicalDeclContext()->isFunctionOrMethod()) {
422 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
423 if (Function->getPreviousDeclaration())
424 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
425 return L;
426
427 return ExternalLinkage;
428 }
429
430 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
431 if (Var->getStorageClass() == VarDecl::Extern ||
432 Var->getStorageClass() == VarDecl::PrivateExtern) {
433 if (Var->getPreviousDeclaration())
434 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
435 return L;
436
437 return ExternalLinkage;
438 }
439 }
440
441 // C++ [basic.link]p6:
442 // Names not covered by these rules have no linkage.
443 return NoLinkage;
444}
445
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000446std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000447 return getQualifiedNameAsString(getASTContext().getLangOptions());
448}
449
450std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000451 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
452 // std::string thrashing.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000453 std::vector<std::string> Names;
454 std::string QualName;
455 const DeclContext *Ctx = getDeclContext();
456
457 if (Ctx->isFunctionOrMethod())
458 return getNameAsString();
459
460 while (Ctx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000461 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000462 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
463 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
464 std::string TemplateArgsStr
465 = TemplateSpecializationType::PrintTemplateArgumentList(
466 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000467 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000468 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000469 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig6be11202009-12-24 23:15:03 +0000470 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
471 if (ND->isAnonymousNamespace())
472 Names.push_back("<anonymous namespace>");
473 else
474 Names.push_back(ND->getNameAsString());
475 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
476 if (!RD->getIdentifier()) {
477 std::string RecordString = "<anonymous ";
478 RecordString += RD->getKindName();
479 RecordString += ">";
480 Names.push_back(RecordString);
481 } else {
482 Names.push_back(RD->getNameAsString());
483 }
Sam Weinig3521d012009-12-28 03:19:38 +0000484 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
485 std::string Proto = FD->getNameAsString();
486
487 const FunctionProtoType *FT = 0;
488 if (FD->hasWrittenPrototype())
489 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
490
491 Proto += "(";
492 if (FT) {
493 llvm::raw_string_ostream POut(Proto);
494 unsigned NumParams = FD->getNumParams();
495 for (unsigned i = 0; i < NumParams; ++i) {
496 if (i)
497 POut << ", ";
498 std::string Param;
499 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
500 POut << Param;
501 }
502
503 if (FT->isVariadic()) {
504 if (NumParams > 0)
505 POut << ", ";
506 POut << "...";
507 }
508 }
509 Proto += ")";
510
511 Names.push_back(Proto);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000512 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000513 Names.push_back(ND->getNameAsString());
514 else
515 break;
516
517 Ctx = Ctx->getParent();
518 }
519
520 std::vector<std::string>::reverse_iterator
521 I = Names.rbegin(),
522 End = Names.rend();
523
524 for (; I!=End; ++I)
525 QualName += *I + "::";
526
527 QualName += getNameAsString();
528
529 return QualName;
530}
531
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000532bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000533 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
534
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000535 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
536 // We want to keep it, unless it nominates same namespace.
537 if (getKind() == Decl::UsingDirective) {
538 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
539 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000542 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
543 // For function declarations, we keep track of redeclarations.
544 return FD->getPreviousDeclaration() == OldD;
545
Douglas Gregore53060f2009-06-25 22:08:12 +0000546 // For function templates, the underlying function declarations are linked.
547 if (const FunctionTemplateDecl *FunctionTemplate
548 = dyn_cast<FunctionTemplateDecl>(this))
549 if (const FunctionTemplateDecl *OldFunctionTemplate
550 = dyn_cast<FunctionTemplateDecl>(OldD))
551 return FunctionTemplate->getTemplatedDecl()
552 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Steve Naroff0de21fd2009-02-22 19:35:57 +0000554 // For method declarations, we keep track of redeclarations.
555 if (isa<ObjCMethodDecl>(this))
556 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
John McCallf36e02d2009-10-09 21:13:30 +0000558 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
559 return true;
560
John McCall9488ea12009-11-17 05:59:44 +0000561 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
562 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
563 cast<UsingShadowDecl>(OldD)->getTargetDecl();
564
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000565 // For non-function declarations, if the declarations are of the
566 // same kind then this must be a redeclaration, or semantic analysis
567 // would not have given us the new declaration.
568 return this->getKind() == OldD->getKind();
569}
570
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000571bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000572 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000573}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000574
Anders Carlssone136e0e2009-06-26 06:29:23 +0000575NamedDecl *NamedDecl::getUnderlyingDecl() {
576 NamedDecl *ND = this;
577 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000578 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000579 ND = UD->getTargetDecl();
580 else if (ObjCCompatibleAliasDecl *AD
581 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
582 return AD->getClassInterface();
583 else
584 return ND;
585 }
586}
587
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000588//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000589// DeclaratorDecl Implementation
590//===----------------------------------------------------------------------===//
591
592SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000593 if (DeclInfo) {
594 TypeLoc TL = DeclInfo->getTypeLoc();
595 while (true) {
596 TypeLoc NextTL = TL.getNextTypeLoc();
597 if (!NextTL)
598 return TL.getSourceRange().getBegin();
599 TL = NextTL;
600 }
601 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000602 return SourceLocation();
603}
604
605//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000606// VarDecl Implementation
607//===----------------------------------------------------------------------===//
608
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000609VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000610 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000611 StorageClass S) {
John McCalla93c9342009-12-07 02:54:59 +0000612 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000613}
614
615void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000616 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000617 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000618 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000619 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
620 Eval->~EvaluatedStmt();
621 C.Deallocate(Eval);
622 }
623 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000624 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000625 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000626}
627
628VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000629}
630
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000631SourceRange VarDecl::getSourceRange() const {
632 if (getInit())
633 return SourceRange(getLocation(), getInit()->getLocEnd());
634 return SourceRange(getLocation(), getLocation());
635}
636
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000637bool VarDecl::isOutOfLine() const {
638 if (!isStaticDataMember())
639 return false;
640
641 if (Decl::isOutOfLine())
642 return true;
643
644 // If this static data member was instantiated from a static data member of
645 // a class template, check whether that static data member was defined
646 // out-of-line.
647 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
648 return VD->isOutOfLine();
649
650 return false;
651}
652
Douglas Gregor0d035142009-10-27 18:42:08 +0000653VarDecl *VarDecl::getOutOfLineDefinition() {
654 if (!isStaticDataMember())
655 return 0;
656
657 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
658 RD != RDEnd; ++RD) {
659 if (RD->getLexicalDeclContext()->isFileContext())
660 return *RD;
661 }
662
663 return 0;
664}
665
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000666VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000667 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000668 return cast<VarDecl>(MSI->getInstantiatedFrom());
669
670 return 0;
671}
672
Douglas Gregor663b5a02009-10-14 20:14:33 +0000673TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000674 if (MemberSpecializationInfo *MSI
675 = getASTContext().getInstantiatedFromStaticDataMember(this))
676 return MSI->getTemplateSpecializationKind();
677
678 return TSK_Undeclared;
679}
680
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000681MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000682 return getASTContext().getInstantiatedFromStaticDataMember(this);
683}
684
Douglas Gregor0a897e32009-10-15 17:21:20 +0000685void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
686 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000687 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000688 assert(MSI && "Not an instantiated static data member?");
689 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000690 if (TSK != TSK_ExplicitSpecialization &&
691 PointOfInstantiation.isValid() &&
692 MSI->getPointOfInstantiation().isInvalid())
693 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000694}
695
Douglas Gregor275a3692009-03-10 23:43:53 +0000696bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
697 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
698 return false;
699
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000700 const VarDecl *Def = 0;
701 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000702 (getStorageClass() == None || getStorageClass() == Static));
703}
704
Ted Kremenek082d9362009-03-20 21:35:28 +0000705const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000706 redecl_iterator I = redecls_begin(), E = redecls_end();
707 while (I != E && !I->getInit())
708 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000709
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000710 if (I != E) {
711 Def = *I;
712 return I->getInit();
713 }
714 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000715}
716
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000717VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000718 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000719}
720
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000721//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000722// FunctionDecl Implementation
723//===----------------------------------------------------------------------===//
724
Ted Kremenek27f8a282008-05-20 00:43:19 +0000725void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000726 if (Body && Body.isOffset())
727 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000728
729 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
730 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000731
Douglas Gregor2db32322009-10-07 23:56:10 +0000732 FunctionTemplateSpecializationInfo *FTSInfo
733 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
734 if (FTSInfo)
735 C.Deallocate(FTSInfo);
736
737 MemberSpecializationInfo *MSInfo
738 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
739 if (MSInfo)
740 C.Deallocate(MSInfo);
741
Steve Naroff3e970492009-01-27 21:25:57 +0000742 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000743
Ted Kremenek27f8a282008-05-20 00:43:19 +0000744 Decl::Destroy(C);
745}
746
John McCall136a6982009-09-11 06:45:03 +0000747void FunctionDecl::getNameForDiagnostic(std::string &S,
748 const PrintingPolicy &Policy,
749 bool Qualified) const {
750 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
751 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
752 if (TemplateArgs)
753 S += TemplateSpecializationType::PrintTemplateArgumentList(
754 TemplateArgs->getFlatArgumentList(),
755 TemplateArgs->flat_size(),
756 Policy);
757
758}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000759
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000760Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000761 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
762 if (I->Body) {
763 Definition = *I;
764 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000765 }
766 }
767
768 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000769}
770
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000771void FunctionDecl::setBody(Stmt *B) {
772 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000773 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000774 EndRangeLoc = B->getLocEnd();
775}
776
Douglas Gregor48a83b52009-09-12 00:17:51 +0000777bool FunctionDecl::isMain() const {
778 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000779 return !Context.getLangOptions().Freestanding &&
780 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000781 getIdentifier() && getIdentifier()->isStr("main");
782}
783
Douglas Gregor48a83b52009-09-12 00:17:51 +0000784bool FunctionDecl::isExternC() const {
785 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000786 // In C, any non-static, non-overloadable function has external
787 // linkage.
788 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000789 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000790
Mike Stump1eb44332009-09-09 15:08:12 +0000791 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000792 DC = DC->getParent()) {
793 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
794 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000795 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000796 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000797
798 break;
799 }
800 }
801
802 return false;
803}
804
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000805bool FunctionDecl::isGlobal() const {
806 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
807 return Method->isStatic();
808
809 if (getStorageClass() == Static)
810 return false;
811
Mike Stump1eb44332009-09-09 15:08:12 +0000812 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000813 DC->isNamespace();
814 DC = DC->getParent()) {
815 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
816 if (!Namespace->getDeclName())
817 return false;
818 break;
819 }
820 }
821
822 return true;
823}
824
Douglas Gregor3e41d602009-02-13 23:20:09 +0000825/// \brief Returns a value indicating whether this function
826/// corresponds to a builtin function.
827///
828/// The function corresponds to a built-in function if it is
829/// declared at translation scope or within an extern "C" block and
830/// its name matches with the name of a builtin. The returned value
831/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000832/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000833/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000834unsigned FunctionDecl::getBuiltinID() const {
835 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000836 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
837 return 0;
838
839 unsigned BuiltinID = getIdentifier()->getBuiltinID();
840 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
841 return BuiltinID;
842
843 // This function has the name of a known C library
844 // function. Determine whether it actually refers to the C library
845 // function or whether it just has the same name.
846
Douglas Gregor9add3172009-02-17 03:23:10 +0000847 // If this is a static function, it's not a builtin.
848 if (getStorageClass() == Static)
849 return 0;
850
Douglas Gregor3c385e52009-02-14 18:57:46 +0000851 // If this function is at translation-unit scope and we're not in
852 // C++, it refers to the C library function.
853 if (!Context.getLangOptions().CPlusPlus &&
854 getDeclContext()->isTranslationUnit())
855 return BuiltinID;
856
857 // If the function is in an extern "C" linkage specification and is
858 // not marked "overloadable", it's the real function.
859 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000860 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000861 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000862 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000863 return BuiltinID;
864
865 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000866 return 0;
867}
868
869
Chris Lattner1ad9b282009-04-25 06:03:53 +0000870/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000871/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000872/// after it has been created.
873unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000874 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000875 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000876 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000877 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879}
880
Ted Kremenekfc767612009-01-14 00:42:25 +0000881void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
882 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000884 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 // Zero params -> null pointer.
887 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000888 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000889 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000891
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000892 // Update source range. The check below allows us to set EndRangeLoc before
893 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000894 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000895 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 }
897}
898
Chris Lattner8123a952008-04-10 02:22:51 +0000899/// getMinRequiredArguments - Returns the minimum number of arguments
900/// needed to call this function. This may be fewer than the number of
901/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000902/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000903unsigned FunctionDecl::getMinRequiredArguments() const {
904 unsigned NumRequiredArgs = getNumParams();
905 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000906 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000907 --NumRequiredArgs;
908
909 return NumRequiredArgs;
910}
911
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000912bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000913 // FIXME: This is not enough. Consider:
914 //
915 // inline void f();
916 // void f() { }
917 //
918 // f is inlined, but does not have inline specified.
919 // To fix this we should add an 'inline' flag to FunctionDecl.
920 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000921 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000922
923 if (isa<CXXMethodDecl>(this)) {
924 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
925 return true;
926 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000927
928 switch (getTemplateSpecializationKind()) {
929 case TSK_Undeclared:
930 case TSK_ExplicitSpecialization:
931 return false;
932
933 case TSK_ImplicitInstantiation:
934 case TSK_ExplicitInstantiationDeclaration:
935 case TSK_ExplicitInstantiationDefinition:
936 // Handle below.
937 break;
938 }
939
940 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
941 Stmt *Pattern = 0;
942 if (PatternDecl)
943 Pattern = PatternDecl->getBody(PatternDecl);
944
945 if (Pattern && PatternDecl)
946 return PatternDecl->isInlined();
947
948 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000949}
950
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000951/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000952/// definition will be externally visible.
953///
954/// Inline function definitions are always available for inlining optimizations.
955/// However, depending on the language dialect, declaration specifiers, and
956/// attributes, the definition of an inline function may or may not be
957/// "externally" visible to other translation units in the program.
958///
959/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +0000960/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000961/// inline definition becomes externally visible (C99 6.7.4p6).
962///
963/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
964/// definition, we use the GNU semantics for inline, which are nearly the
965/// opposite of C99 semantics. In particular, "inline" by itself will create
966/// an externally visible symbol, but "extern inline" will not create an
967/// externally visible symbol.
968bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
969 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000970 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000971 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000972
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000973 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000974 // GNU inline semantics. Based on a number of examples, we came up with the
975 // following heuristic: if the "inline" keyword is present on a
976 // declaration of the function but "extern" is not present on that
977 // declaration, then the symbol is externally visible. Otherwise, the GNU
978 // "extern inline" semantics applies and the symbol is not externally
979 // visible.
980 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
981 Redecl != RedeclEnd;
982 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000983 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000984 return true;
985 }
986
987 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000988 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000989 }
990
991 // C99 6.7.4p6:
992 // [...] If all of the file scope declarations for a function in a
993 // translation unit include the inline function specifier without extern,
994 // then the definition in that translation unit is an inline definition.
995 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
996 Redecl != RedeclEnd;
997 ++Redecl) {
998 // Only consider file-scope declarations in this test.
999 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1000 continue;
1001
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001002 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001003 return true; // Not an inline definition
1004 }
1005
1006 // C99 6.7.4p6:
1007 // An inline definition does not provide an external definition for the
1008 // function, and does not forbid an external definition in another
1009 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001010 return false;
1011}
1012
Mike Stump1eb44332009-09-09 15:08:12 +00001013void
Douglas Gregor127102b2009-06-29 20:59:39 +00001014FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +00001015 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +00001016
Douglas Gregor127102b2009-06-29 20:59:39 +00001017 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001018 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +00001019 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1020 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1021 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1022 }
1023}
1024
Mike Stump740256b2009-09-29 00:50:50 +00001025const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1026 return getFirstDeclaration();
1027}
1028
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001029FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +00001030 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001031}
1032
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001033/// getOverloadedOperator - Which C++ overloaded operator this
1034/// function represents, if any.
1035OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001036 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1037 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001038 else
1039 return OO_None;
1040}
1041
Sean Hunta6c058d2010-01-13 09:01:02 +00001042/// getLiteralIdentifier - The literal suffix identifier this function
1043/// represents, if any.
1044const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1045 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1046 return getDeclName().getCXXLiteralIdentifier();
1047 else
1048 return 0;
1049}
1050
Douglas Gregor2db32322009-10-07 23:56:10 +00001051FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001052 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001053 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1054
1055 return 0;
1056}
1057
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001058MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1059 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1060}
1061
Douglas Gregor2db32322009-10-07 23:56:10 +00001062void
1063FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1064 TemplateSpecializationKind TSK) {
1065 assert(TemplateOrSpecialization.isNull() &&
1066 "Member function is already a specialization");
1067 MemberSpecializationInfo *Info
1068 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1069 TemplateOrSpecialization = Info;
1070}
1071
Douglas Gregor3b846b62009-10-27 20:53:28 +00001072bool FunctionDecl::isImplicitlyInstantiable() const {
1073 // If this function already has a definition or is invalid, it can't be
1074 // implicitly instantiated.
1075 if (isInvalidDecl() || getBody())
1076 return false;
1077
1078 switch (getTemplateSpecializationKind()) {
1079 case TSK_Undeclared:
1080 case TSK_ExplicitSpecialization:
1081 case TSK_ExplicitInstantiationDefinition:
1082 return false;
1083
1084 case TSK_ImplicitInstantiation:
1085 return true;
1086
1087 case TSK_ExplicitInstantiationDeclaration:
1088 // Handled below.
1089 break;
1090 }
1091
1092 // Find the actual template from which we will instantiate.
1093 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1094 Stmt *Pattern = 0;
1095 if (PatternDecl)
1096 Pattern = PatternDecl->getBody(PatternDecl);
1097
1098 // C++0x [temp.explicit]p9:
1099 // Except for inline functions, other explicit instantiation declarations
1100 // have the effect of suppressing the implicit instantiation of the entity
1101 // to which they refer.
1102 if (!Pattern || !PatternDecl)
1103 return true;
1104
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001105 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001106}
1107
1108FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1109 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1110 while (Primary->getInstantiatedFromMemberTemplate()) {
1111 // If we have hit a point where the user provided a specialization of
1112 // this template, we're done looking.
1113 if (Primary->isMemberSpecialization())
1114 break;
1115
1116 Primary = Primary->getInstantiatedFromMemberTemplate();
1117 }
1118
1119 return Primary->getTemplatedDecl();
1120 }
1121
1122 return getInstantiatedFromMemberFunction();
1123}
1124
Douglas Gregor16e8be22009-06-29 17:30:29 +00001125FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001126 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001127 = TemplateOrSpecialization
1128 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001129 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001130 }
1131 return 0;
1132}
1133
1134const TemplateArgumentList *
1135FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001136 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001137 = TemplateOrSpecialization
1138 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001139 return Info->TemplateArguments;
1140 }
1141 return 0;
1142}
1143
Mike Stump1eb44332009-09-09 15:08:12 +00001144void
Douglas Gregor1637be72009-06-26 00:10:03 +00001145FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1146 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001147 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001148 void *InsertPos,
1149 TemplateSpecializationKind TSK) {
1150 assert(TSK != TSK_Undeclared &&
1151 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001152 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001153 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001154 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +00001155 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Douglas Gregor127102b2009-06-29 20:59:39 +00001157 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001158 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001159 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001160 Info->TemplateArguments = TemplateArgs;
1161 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregor127102b2009-06-29 20:59:39 +00001163 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001164 // function template specializations.
1165 if (InsertPos)
1166 Template->getSpecializations().InsertNode(Info, InsertPos);
1167 else {
1168 // Try to insert the new node. If there is an existing node, remove it
1169 // first.
1170 FunctionTemplateSpecializationInfo *Existing
1171 = Template->getSpecializations().GetOrInsertNode(Info);
1172 if (Existing) {
1173 Template->getSpecializations().RemoveNode(Existing);
1174 Template->getSpecializations().GetOrInsertNode(Info);
1175 }
1176 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001177}
1178
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001179TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001180 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001181 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001182 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001183 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001184 if (FTSInfo)
1185 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Douglas Gregor2db32322009-10-07 23:56:10 +00001187 MemberSpecializationInfo *MSInfo
1188 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1189 if (MSInfo)
1190 return MSInfo->getTemplateSpecializationKind();
1191
1192 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001193}
1194
Mike Stump1eb44332009-09-09 15:08:12 +00001195void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001196FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1197 SourceLocation PointOfInstantiation) {
1198 if (FunctionTemplateSpecializationInfo *FTSInfo
1199 = TemplateOrSpecialization.dyn_cast<
1200 FunctionTemplateSpecializationInfo*>()) {
1201 FTSInfo->setTemplateSpecializationKind(TSK);
1202 if (TSK != TSK_ExplicitSpecialization &&
1203 PointOfInstantiation.isValid() &&
1204 FTSInfo->getPointOfInstantiation().isInvalid())
1205 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1206 } else if (MemberSpecializationInfo *MSInfo
1207 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1208 MSInfo->setTemplateSpecializationKind(TSK);
1209 if (TSK != TSK_ExplicitSpecialization &&
1210 PointOfInstantiation.isValid() &&
1211 MSInfo->getPointOfInstantiation().isInvalid())
1212 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1213 } else
1214 assert(false && "Function cannot have a template specialization kind");
1215}
1216
1217SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001218 if (FunctionTemplateSpecializationInfo *FTSInfo
1219 = TemplateOrSpecialization.dyn_cast<
1220 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001221 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001222 else if (MemberSpecializationInfo *MSInfo
1223 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001224 return MSInfo->getPointOfInstantiation();
1225
1226 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001227}
1228
Douglas Gregor9f185072009-09-11 20:15:17 +00001229bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001230 if (Decl::isOutOfLine())
1231 return true;
1232
1233 // If this function was instantiated from a member function of a
1234 // class template, check whether that member function was defined out-of-line.
1235 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1236 const FunctionDecl *Definition;
1237 if (FD->getBody(Definition))
1238 return Definition->isOutOfLine();
1239 }
1240
1241 // If this function was instantiated from a function template,
1242 // check whether that function template was defined out-of-line.
1243 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1244 const FunctionDecl *Definition;
1245 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1246 return Definition->isOutOfLine();
1247 }
1248
1249 return false;
1250}
1251
Chris Lattner8a934232008-03-31 00:36:02 +00001252//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001253// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001254//===----------------------------------------------------------------------===//
1255
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001256SourceRange TagDecl::getSourceRange() const {
1257 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001258 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001259}
1260
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001261TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001262 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001263}
1264
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001265void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001266 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1267 TagT->decl.setPointer(this);
1268 TagT->decl.setInt(1);
1269 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001270}
1271
1272void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001273 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001274 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1275 assert(TagT->decl.getPointer() == this &&
1276 "Attempt to redefine a tag definition?");
1277 TagT->decl.setInt(0);
1278 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001279}
1280
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001281TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001282 if (isDefinition())
1283 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001284
1285 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001286 R != REnd; ++R)
1287 if (R->isDefinition())
1288 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001290 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001291}
1292
John McCallf1bbbb42009-09-04 01:14:41 +00001293TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1294 switch (TypeSpec) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001295 default: llvm_unreachable("unexpected type specifier");
John McCallf1bbbb42009-09-04 01:14:41 +00001296 case DeclSpec::TST_struct: return TK_struct;
1297 case DeclSpec::TST_class: return TK_class;
1298 case DeclSpec::TST_union: return TK_union;
1299 case DeclSpec::TST_enum: return TK_enum;
1300 }
1301}
1302
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001303//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001304// RecordDecl Implementation
1305//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001306
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001307RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001308 IdentifierInfo *Id, RecordDecl *PrevDecl,
1309 SourceLocation TKL)
1310 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001311 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001312 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001313 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001314 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001315}
1316
1317RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001318 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001319 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001321 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001322 C.getTypeDeclType(R, PrevDecl);
1323 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001324}
1325
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001326RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001327}
1328
1329void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001330 TagDecl::Destroy(C);
1331}
1332
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001333bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001334 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001335 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1336}
1337
Douglas Gregor44b43212008-12-11 16:49:14 +00001338/// completeDefinition - Notes that the definition of this type is now
1339/// complete.
1340void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001342 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001343}
1344
Steve Naroff56ee6892008-10-08 17:01:13 +00001345//===----------------------------------------------------------------------===//
1346// BlockDecl Implementation
1347//===----------------------------------------------------------------------===//
1348
1349BlockDecl::~BlockDecl() {
1350}
1351
1352void BlockDecl::Destroy(ASTContext& C) {
1353 if (Body)
1354 Body->Destroy(C);
1355
1356 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1357 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
1359 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001360 Decl::Destroy(C);
1361}
Steve Naroffe78b8092009-03-13 16:56:44 +00001362
1363void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1364 unsigned NParms) {
1365 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Steve Naroffe78b8092009-03-13 16:56:44 +00001367 // Zero params -> null pointer.
1368 if (NParms) {
1369 NumParams = NParms;
1370 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1371 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1372 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1373 }
1374}
1375
1376unsigned BlockDecl::getNumParams() const {
1377 return NumParams;
1378}