blob: 794b14a1f4a1fb1be920ec4323bc86fde9c061ac [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000026#include "clang/Parse/DeclSpec.h"
27#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2ada0482009-02-04 17:27:36 +000028#include <vector>
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Chris Lattner9631e182009-03-04 06:34:08 +000032void Attr::Destroy(ASTContext &C) {
33 if (Next) {
34 Next->Destroy(C);
35 Next = 0;
36 }
37 this->~Attr();
38 C.Deallocate((void*)this);
39}
40
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000041/// \brief Return the TypeLoc wrapper for the type source info.
John McCallbcd03502009-12-07 02:54:59 +000042TypeLoc TypeSourceInfo::getTypeLoc() const {
Argyrios Kyrtzidis1b7c4ca2009-09-29 19:40:20 +000043 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000044}
Chris Lattner9631e182009-03-04 06:34:08 +000045
Chris Lattner88f70d62008-03-15 05:43:15 +000046//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +000047// Decl Allocation/Deallocation Method Implementations
48//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +000049
Chris Lattner9631e182009-03-04 06:34:08 +000050
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000051TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +000052 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000053}
54
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000055NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
56 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000057 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000058}
59
Ted Kremenek78aa98f2008-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 Stump11289f42009-09-09 15:08:12 +000063
Ted Kremeneka08154d2008-05-24 15:09:56 +000064 this->~NamespaceDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +000065 C.Deallocate((void *)this);
Ted Kremenek78aa98f2008-05-20 04:49:55 +000066}
67
68
Chris Lattner5696e7b2008-06-17 18:05:57 +000069ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000070 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000071 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner5696e7b2008-06-17 18:05:57 +000072}
73
Daniel Dunbarb76b7452009-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 Stump11289f42009-09-09 15:08:12 +000079 case VarDecl::PrivateExtern: return "__private_extern__"; break;
Daniel Dunbarb76b7452009-04-14 02:08:49 +000080 case VarDecl::Register: return "register"; break;
Mike Stump11289f42009-09-09 15:08:12 +000081 case VarDecl::Static: return "static"; break;
Daniel Dunbarb76b7452009-04-14 02:08:49 +000082 }
83
84 assert(0 && "Invalid storage class");
85 return 0;
86}
87
Chris Lattnerbec41342008-04-22 18:39:57 +000088ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000089 SourceLocation L, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +000090 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +000091 StorageClass S, Expr *DefArg) {
John McCallbcd03502009-12-07 02:54:59 +000092 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
Fariborz Jahaniana0befc02008-12-20 23:29:59 +000093}
94
Anders Carlsson714d0962009-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 Gregorc732aba2009-09-11 18:44:32 +0000123SourceRange ParmVarDecl::getDefaultArgRange() const {
124 if (const Expr *E = getInit())
125 return E->getSourceRange();
126
Anders Carlsson7e0b2072009-12-13 17:53:43 +0000127 if (hasUninstantiatedDefaultArg())
128 return getUninstantiatedDefaultArg()->getSourceRange();
Douglas Gregorc732aba2009-09-11 18:44:32 +0000129
130 return SourceRange();
131}
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000132
Douglas Gregorc732aba2009-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 Gregor31cf12c2009-05-26 18:54:04 +0000137 }
138
Douglas Gregorc732aba2009-09-11 18:44:32 +0000139 Init = I;
140}
141
Douglas Gregor16618f22009-09-12 00:17:51 +0000142bool VarDecl::isExternC() const {
143 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000144 if (!Context.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +0000145 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000146 getStorageClass() != Static) ||
147 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
148
Mike Stump11289f42009-09-09 15:08:12 +0000149 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-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 Lattnerbec41342008-04-22 18:39:57 +0000165FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000166 SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000167 DeclarationName N, QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000168 TypeSourceInfo *TInfo,
Mike Stump11289f42009-09-09 15:08:12 +0000169 StorageClass S, bool isInline,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000170 bool hasWrittenPrototype) {
Mike Stump11289f42009-09-09 15:08:12 +0000171 FunctionDecl *New
John McCallbcd03502009-12-07 02:54:59 +0000172 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
Anders Carlssone0dd1d52009-05-14 21:46:00 +0000173 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor739ef0c2009-02-25 16:33:18 +0000174 return New;
Chris Lattner5072bae2008-03-15 21:24:04 +0000175}
176
Steve Naroff1d95e5a2008-10-10 01:28:17 +0000177BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000178 return new (C) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +0000179}
180
Douglas Gregor91f84212008-12-11 16:49:14 +0000181FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000182 IdentifierInfo *Id, QualType T,
John McCallbcd03502009-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 Lattneree1284a2008-03-16 00:16:02 +0000185}
186
Douglas Gregorf4d33272009-01-07 19:46:03 +0000187bool FieldDecl::isAnonymousStructOrUnion() const {
188 if (!isImplicit() || getDeclName())
189 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000190
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000191 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorf4d33272009-01-07 19:46:03 +0000192 return Record->getDecl()->isAnonymousStructOrUnion();
193
194 return false;
195}
Chris Lattner5072bae2008-03-15 21:24:04 +0000196
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000197EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
198 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000199 IdentifierInfo *Id, QualType T,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000200 Expr *E, const llvm::APSInt &V) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000201 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnera7b32872008-03-15 06:12:44 +0000202}
203
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000204void EnumConstantDecl::Destroy(ASTContext& C) {
205 if (Init) Init->Destroy(C);
206 Decl::Destroy(C);
207}
208
Chris Lattnerbec41342008-04-22 18:39:57 +0000209TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
John McCall703a3f82009-10-24 08:00:42 +0000210 SourceLocation L, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +0000211 TypeSourceInfo *TInfo) {
212 return new (C) TypedefDecl(DC, L, Id, TInfo);
Chris Lattnera7b32872008-03-15 06:12:44 +0000213}
214
John McCall91f1a022009-12-30 00:31:22 +0000215// Anchor TypedefDecl's vtable here.
216TypedefDecl::~TypedefDecl() {}
217
Chris Lattnerbec41342008-04-22 18:39:57 +0000218EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000219 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000220 EnumDecl *PrevDecl) {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000221 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000222 C.getTypeDeclType(Enum, PrevDecl);
223 return Enum;
Chris Lattnera7b32872008-03-15 06:12:44 +0000224}
225
Ted Kremenek123f0252008-09-02 20:13:32 +0000226void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek123f0252008-09-02 20:13:32 +0000227 Decl::Destroy(C);
228}
229
John McCall56774992009-12-09 09:09:27 +0000230void EnumDecl::completeDefinition(ASTContext &C,
231 QualType NewType,
232 QualType NewPromotionType) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000233 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000234 IntegerType = NewType;
John McCall56774992009-12-09 09:09:27 +0000235 PromotionType = NewPromotionType;
Douglas Gregordee1be82009-01-17 00:42:38 +0000236 TagDecl::completeDefinition();
Douglas Gregor91f84212008-12-11 16:49:14 +0000237}
238
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000239FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000240 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000241 StringLiteral *Str) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000242 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattneree1284a2008-03-16 00:16:02 +0000243}
244
Chris Lattnera7b32872008-03-15 06:12:44 +0000245//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000246// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000247//===----------------------------------------------------------------------===//
248
Douglas Gregorf73b2822009-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 Friedmanf873c2f2009-11-26 03:04:01 +0000270 Var->getType().isConstant(Context) &&
Douglas Gregorf73b2822009-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 Gregor2ada0482009-02-04 17:27:36 +0000446std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000447 return getQualifiedNameAsString(getASTContext().getLangOptions());
448}
449
450std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000451 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
452 // std::string thrashing.
Douglas Gregor2ada0482009-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 Stump11289f42009-09-09 15:08:12 +0000461 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-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 Gregor7de59662009-05-29 20:38:28 +0000467 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000468 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000469 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Sam Weinig07d211e2009-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 Weinigb999f682009-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 Gregor85673582009-05-18 17:01:57 +0000512 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-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 Gregor6e6ad602009-01-20 01:17:11 +0000532bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000533 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
534
Douglas Gregor889ceb72009-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 Stump11289f42009-09-09 15:08:12 +0000541
Douglas Gregor8b9ccca2008-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 Gregorad3f2fc2009-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 Stump11289f42009-09-09 15:08:12 +0000553
Steve Naroffc4173fa2009-02-22 19:35:57 +0000554 // For method declarations, we keep track of redeclarations.
555 if (isa<ObjCMethodDecl>(this))
556 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000557
John McCall9f3059a2009-10-09 21:13:30 +0000558 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
559 return true;
560
John McCall3f746822009-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 Gregor8b9ccca2008-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 Gregoreddf4332009-02-24 20:03:32 +0000571bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000572 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000573}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000574
Anders Carlsson6915bf62009-06-26 06:29:23 +0000575NamedDecl *NamedDecl::getUnderlyingDecl() {
576 NamedDecl *ND = this;
577 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000578 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-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 Kyrtzidis9e59b572008-11-09 23:41:00 +0000588//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000589// DeclaratorDecl Implementation
590//===----------------------------------------------------------------------===//
591
592SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-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 Kyrtzidis6032ef12009-08-21 00:31:54 +0000602 return SourceLocation();
603}
604
605//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000606// VarDecl Implementation
607//===----------------------------------------------------------------------===//
608
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000609VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000610 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000611 StorageClass S) {
John McCallbcd03502009-12-07 02:54:59 +0000612 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000613}
614
615void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000616 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000617 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000618 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000619 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
620 Eval->~EvaluatedStmt();
621 C.Deallocate(Eval);
622 }
623 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000624 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000625 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000626}
627
628VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000629}
630
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000631SourceRange VarDecl::getSourceRange() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000632 SourceLocation Start = getTypeSpecStartLoc();
633 if (Start.isInvalid())
634 Start = getLocation();
635
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000636 if (getInit())
Douglas Gregor562c1f92010-01-22 19:49:59 +0000637 return SourceRange(Start, getInit()->getLocEnd());
638 return SourceRange(Start, getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000639}
640
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000641bool VarDecl::isOutOfLine() const {
642 if (!isStaticDataMember())
643 return false;
644
645 if (Decl::isOutOfLine())
646 return true;
647
648 // If this static data member was instantiated from a static data member of
649 // a class template, check whether that static data member was defined
650 // out-of-line.
651 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
652 return VD->isOutOfLine();
653
654 return false;
655}
656
Douglas Gregor1d957a32009-10-27 18:42:08 +0000657VarDecl *VarDecl::getOutOfLineDefinition() {
658 if (!isStaticDataMember())
659 return 0;
660
661 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
662 RD != RDEnd; ++RD) {
663 if (RD->getLexicalDeclContext()->isFileContext())
664 return *RD;
665 }
666
667 return 0;
668}
669
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000670VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000671 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000672 return cast<VarDecl>(MSI->getInstantiatedFrom());
673
674 return 0;
675}
676
Douglas Gregor3c74d412009-10-14 20:14:33 +0000677TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor86d142a2009-10-08 07:24:58 +0000678 if (MemberSpecializationInfo *MSI
679 = getASTContext().getInstantiatedFromStaticDataMember(this))
680 return MSI->getTemplateSpecializationKind();
681
682 return TSK_Undeclared;
683}
684
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000685MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000686 return getASTContext().getInstantiatedFromStaticDataMember(this);
687}
688
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000689void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
690 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000691 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000692 assert(MSI && "Not an instantiated static data member?");
693 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000694 if (TSK != TSK_ExplicitSpecialization &&
695 PointOfInstantiation.isValid() &&
696 MSI->getPointOfInstantiation().isInvalid())
697 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000698}
699
Douglas Gregor0760fa12009-03-10 23:43:53 +0000700bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
701 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
702 return false;
703
Douglas Gregorbeecd582009-04-21 17:11:58 +0000704 const VarDecl *Def = 0;
705 return (!getDefinition(Def) &&
Douglas Gregor0760fa12009-03-10 23:43:53 +0000706 (getStorageClass() == None || getStorageClass() == Static));
707}
708
Ted Kremenekdfd72c22009-03-20 21:35:28 +0000709const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000710 redecl_iterator I = redecls_begin(), E = redecls_end();
711 while (I != E && !I->getInit())
712 ++I;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000713
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000714 if (I != E) {
715 Def = *I;
716 return I->getInit();
717 }
718 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000719}
720
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000721VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000722 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000723}
724
Nuno Lopes394ec982008-12-17 23:39:55 +0000725//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000726// FunctionDecl Implementation
727//===----------------------------------------------------------------------===//
728
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000729void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000730 if (Body && Body.isOffset())
731 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000732
733 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
734 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000735
Douglas Gregord801b062009-10-07 23:56:10 +0000736 FunctionTemplateSpecializationInfo *FTSInfo
737 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
738 if (FTSInfo)
739 C.Deallocate(FTSInfo);
740
741 MemberSpecializationInfo *MSInfo
742 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
743 if (MSInfo)
744 C.Deallocate(MSInfo);
745
Steve Naroff13ae6f42009-01-27 21:25:57 +0000746 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000747
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000748 Decl::Destroy(C);
749}
750
John McCalle1f2ec22009-09-11 06:45:03 +0000751void FunctionDecl::getNameForDiagnostic(std::string &S,
752 const PrintingPolicy &Policy,
753 bool Qualified) const {
754 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
755 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
756 if (TemplateArgs)
757 S += TemplateSpecializationType::PrintTemplateArgumentList(
758 TemplateArgs->getFlatArgumentList(),
759 TemplateArgs->flat_size(),
760 Policy);
761
762}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000763
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000764Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000765 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
766 if (I->Body) {
767 Definition = *I;
768 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000769 }
770 }
771
772 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000773}
774
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000775void FunctionDecl::setBody(Stmt *B) {
776 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000777 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000778 EndRangeLoc = B->getLocEnd();
779}
780
Douglas Gregor16618f22009-09-12 00:17:51 +0000781bool FunctionDecl::isMain() const {
782 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000783 return !Context.getLangOptions().Freestanding &&
784 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000785 getIdentifier() && getIdentifier()->isStr("main");
786}
787
Douglas Gregor16618f22009-09-12 00:17:51 +0000788bool FunctionDecl::isExternC() const {
789 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000790 // In C, any non-static, non-overloadable function has external
791 // linkage.
792 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000793 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000794
Mike Stump11289f42009-09-09 15:08:12 +0000795 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000796 DC = DC->getParent()) {
797 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
798 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000799 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000800 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000801
802 break;
803 }
804 }
805
806 return false;
807}
808
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000809bool FunctionDecl::isGlobal() const {
810 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
811 return Method->isStatic();
812
813 if (getStorageClass() == Static)
814 return false;
815
Mike Stump11289f42009-09-09 15:08:12 +0000816 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000817 DC->isNamespace();
818 DC = DC->getParent()) {
819 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
820 if (!Namespace->getDeclName())
821 return false;
822 break;
823 }
824 }
825
826 return true;
827}
828
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000829/// \brief Returns a value indicating whether this function
830/// corresponds to a builtin function.
831///
832/// The function corresponds to a built-in function if it is
833/// declared at translation scope or within an extern "C" block and
834/// its name matches with the name of a builtin. The returned value
835/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000836/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000837/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000838unsigned FunctionDecl::getBuiltinID() const {
839 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000840 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
841 return 0;
842
843 unsigned BuiltinID = getIdentifier()->getBuiltinID();
844 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
845 return BuiltinID;
846
847 // This function has the name of a known C library
848 // function. Determine whether it actually refers to the C library
849 // function or whether it just has the same name.
850
Douglas Gregora908e7f2009-02-17 03:23:10 +0000851 // If this is a static function, it's not a builtin.
852 if (getStorageClass() == Static)
853 return 0;
854
Douglas Gregore711f702009-02-14 18:57:46 +0000855 // If this function is at translation-unit scope and we're not in
856 // C++, it refers to the C library function.
857 if (!Context.getLangOptions().CPlusPlus &&
858 getDeclContext()->isTranslationUnit())
859 return BuiltinID;
860
861 // If the function is in an extern "C" linkage specification and is
862 // not marked "overloadable", it's the real function.
863 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000864 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000865 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000866 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000867 return BuiltinID;
868
869 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000870 return 0;
871}
872
873
Chris Lattner47c0d002009-04-25 06:03:53 +0000874/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000875/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000876/// after it has been created.
877unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000878 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000879 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000880 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000881 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000883}
884
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000885void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
886 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000887 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000888 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000889
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000890 // Zero params -> null pointer.
891 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000892 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000893 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000894 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000895
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000896 // Update source range. The check below allows us to set EndRangeLoc before
897 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000898 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000899 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000900 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000901}
Chris Lattner41943152007-01-25 04:52:46 +0000902
Chris Lattner58258242008-04-10 02:22:51 +0000903/// getMinRequiredArguments - Returns the minimum number of arguments
904/// needed to call this function. This may be fewer than the number of
905/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000906/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000907unsigned FunctionDecl::getMinRequiredArguments() const {
908 unsigned NumRequiredArgs = getNumParams();
909 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000910 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000911 --NumRequiredArgs;
912
913 return NumRequiredArgs;
914}
915
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000916bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000917 // FIXME: This is not enough. Consider:
918 //
919 // inline void f();
920 // void f() { }
921 //
922 // f is inlined, but does not have inline specified.
923 // To fix this we should add an 'inline' flag to FunctionDecl.
924 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000925 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +0000926
927 if (isa<CXXMethodDecl>(this)) {
928 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
929 return true;
930 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000931
932 switch (getTemplateSpecializationKind()) {
933 case TSK_Undeclared:
934 case TSK_ExplicitSpecialization:
935 return false;
936
937 case TSK_ImplicitInstantiation:
938 case TSK_ExplicitInstantiationDeclaration:
939 case TSK_ExplicitInstantiationDefinition:
940 // Handle below.
941 break;
942 }
943
944 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
945 Stmt *Pattern = 0;
946 if (PatternDecl)
947 Pattern = PatternDecl->getBody(PatternDecl);
948
949 if (Pattern && PatternDecl)
950 return PatternDecl->isInlined();
951
952 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000953}
954
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000955/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000956/// definition will be externally visible.
957///
958/// Inline function definitions are always available for inlining optimizations.
959/// However, depending on the language dialect, declaration specifiers, and
960/// attributes, the definition of an inline function may or may not be
961/// "externally" visible to other translation units in the program.
962///
963/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +0000964/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +0000965/// inline definition becomes externally visible (C99 6.7.4p6).
966///
967/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
968/// definition, we use the GNU semantics for inline, which are nearly the
969/// opposite of C99 semantics. In particular, "inline" by itself will create
970/// an externally visible symbol, but "extern inline" will not create an
971/// externally visible symbol.
972bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
973 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000974 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000975 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +0000976
Douglas Gregorb7e5c842009-10-27 23:26:40 +0000977 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +0000978 // GNU inline semantics. Based on a number of examples, we came up with the
979 // following heuristic: if the "inline" keyword is present on a
980 // declaration of the function but "extern" is not present on that
981 // declaration, then the symbol is externally visible. Otherwise, the GNU
982 // "extern inline" semantics applies and the symbol is not externally
983 // visible.
984 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
985 Redecl != RedeclEnd;
986 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +0000987 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000988 return true;
989 }
990
991 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000992 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000993 }
994
995 // C99 6.7.4p6:
996 // [...] If all of the file scope declarations for a function in a
997 // translation unit include the inline function specifier without extern,
998 // then the definition in that translation unit is an inline definition.
999 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1000 Redecl != RedeclEnd;
1001 ++Redecl) {
1002 // Only consider file-scope declarations in this test.
1003 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1004 continue;
1005
Douglas Gregor35b57532009-10-27 21:01:01 +00001006 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001007 return true; // Not an inline definition
1008 }
1009
1010 // C99 6.7.4p6:
1011 // An inline definition does not provide an external definition for the
1012 // function, and does not forbid an external definition in another
1013 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001014 return false;
1015}
1016
Mike Stump11289f42009-09-09 15:08:12 +00001017void
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001018FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +00001019 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +00001020
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001021 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump11289f42009-09-09 15:08:12 +00001022 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001023 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1024 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1025 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1026 }
1027}
1028
Mike Stumpe7a2b482009-09-29 00:50:50 +00001029const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1030 return getFirstDeclaration();
1031}
1032
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001033FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +00001034 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001035}
1036
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001037/// getOverloadedOperator - Which C++ overloaded operator this
1038/// function represents, if any.
1039OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001040 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1041 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001042 else
1043 return OO_None;
1044}
1045
Alexis Huntc88db062010-01-13 09:01:02 +00001046/// getLiteralIdentifier - The literal suffix identifier this function
1047/// represents, if any.
1048const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1049 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1050 return getDeclName().getCXXLiteralIdentifier();
1051 else
1052 return 0;
1053}
1054
Douglas Gregord801b062009-10-07 23:56:10 +00001055FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001056 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001057 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1058
1059 return 0;
1060}
1061
Douglas Gregor06db9f52009-10-12 20:18:28 +00001062MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1063 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1064}
1065
Douglas Gregord801b062009-10-07 23:56:10 +00001066void
1067FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1068 TemplateSpecializationKind TSK) {
1069 assert(TemplateOrSpecialization.isNull() &&
1070 "Member function is already a specialization");
1071 MemberSpecializationInfo *Info
1072 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1073 TemplateOrSpecialization = Info;
1074}
1075
Douglas Gregorafca3b42009-10-27 20:53:28 +00001076bool FunctionDecl::isImplicitlyInstantiable() const {
1077 // If this function already has a definition or is invalid, it can't be
1078 // implicitly instantiated.
1079 if (isInvalidDecl() || getBody())
1080 return false;
1081
1082 switch (getTemplateSpecializationKind()) {
1083 case TSK_Undeclared:
1084 case TSK_ExplicitSpecialization:
1085 case TSK_ExplicitInstantiationDefinition:
1086 return false;
1087
1088 case TSK_ImplicitInstantiation:
1089 return true;
1090
1091 case TSK_ExplicitInstantiationDeclaration:
1092 // Handled below.
1093 break;
1094 }
1095
1096 // Find the actual template from which we will instantiate.
1097 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1098 Stmt *Pattern = 0;
1099 if (PatternDecl)
1100 Pattern = PatternDecl->getBody(PatternDecl);
1101
1102 // C++0x [temp.explicit]p9:
1103 // Except for inline functions, other explicit instantiation declarations
1104 // have the effect of suppressing the implicit instantiation of the entity
1105 // to which they refer.
1106 if (!Pattern || !PatternDecl)
1107 return true;
1108
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001109 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001110}
1111
1112FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1113 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1114 while (Primary->getInstantiatedFromMemberTemplate()) {
1115 // If we have hit a point where the user provided a specialization of
1116 // this template, we're done looking.
1117 if (Primary->isMemberSpecialization())
1118 break;
1119
1120 Primary = Primary->getInstantiatedFromMemberTemplate();
1121 }
1122
1123 return Primary->getTemplatedDecl();
1124 }
1125
1126 return getInstantiatedFromMemberFunction();
1127}
1128
Douglas Gregor70d83e22009-06-29 17:30:29 +00001129FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001130 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001131 = TemplateOrSpecialization
1132 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001133 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001134 }
1135 return 0;
1136}
1137
1138const TemplateArgumentList *
1139FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001140 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001141 = TemplateOrSpecialization
1142 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001143 return Info->TemplateArguments;
1144 }
1145 return 0;
1146}
1147
Mike Stump11289f42009-09-09 15:08:12 +00001148void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001149FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1150 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001151 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001152 void *InsertPos,
1153 TemplateSpecializationKind TSK) {
1154 assert(TSK != TSK_Undeclared &&
1155 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001156 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001157 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001158 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +00001159 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001160
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001161 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +00001162 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001163 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001164 Info->TemplateArguments = TemplateArgs;
1165 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001166
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001167 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001168 // function template specializations.
1169 if (InsertPos)
1170 Template->getSpecializations().InsertNode(Info, InsertPos);
1171 else {
1172 // Try to insert the new node. If there is an existing node, remove it
1173 // first.
1174 FunctionTemplateSpecializationInfo *Existing
1175 = Template->getSpecializations().GetOrInsertNode(Info);
1176 if (Existing) {
1177 Template->getSpecializations().RemoveNode(Existing);
1178 Template->getSpecializations().GetOrInsertNode(Info);
1179 }
1180 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001181}
1182
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001183TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001184 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001185 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001186 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001187 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001188 if (FTSInfo)
1189 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001190
Douglas Gregord801b062009-10-07 23:56:10 +00001191 MemberSpecializationInfo *MSInfo
1192 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1193 if (MSInfo)
1194 return MSInfo->getTemplateSpecializationKind();
1195
1196 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001197}
1198
Mike Stump11289f42009-09-09 15:08:12 +00001199void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001200FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1201 SourceLocation PointOfInstantiation) {
1202 if (FunctionTemplateSpecializationInfo *FTSInfo
1203 = TemplateOrSpecialization.dyn_cast<
1204 FunctionTemplateSpecializationInfo*>()) {
1205 FTSInfo->setTemplateSpecializationKind(TSK);
1206 if (TSK != TSK_ExplicitSpecialization &&
1207 PointOfInstantiation.isValid() &&
1208 FTSInfo->getPointOfInstantiation().isInvalid())
1209 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1210 } else if (MemberSpecializationInfo *MSInfo
1211 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1212 MSInfo->setTemplateSpecializationKind(TSK);
1213 if (TSK != TSK_ExplicitSpecialization &&
1214 PointOfInstantiation.isValid() &&
1215 MSInfo->getPointOfInstantiation().isInvalid())
1216 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1217 } else
1218 assert(false && "Function cannot have a template specialization kind");
1219}
1220
1221SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001222 if (FunctionTemplateSpecializationInfo *FTSInfo
1223 = TemplateOrSpecialization.dyn_cast<
1224 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001225 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001226 else if (MemberSpecializationInfo *MSInfo
1227 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001228 return MSInfo->getPointOfInstantiation();
1229
1230 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001231}
1232
Douglas Gregor6411b922009-09-11 20:15:17 +00001233bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001234 if (Decl::isOutOfLine())
1235 return true;
1236
1237 // If this function was instantiated from a member function of a
1238 // class template, check whether that member function was defined out-of-line.
1239 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1240 const FunctionDecl *Definition;
1241 if (FD->getBody(Definition))
1242 return Definition->isOutOfLine();
1243 }
1244
1245 // If this function was instantiated from a function template,
1246 // check whether that function template was defined out-of-line.
1247 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1248 const FunctionDecl *Definition;
1249 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1250 return Definition->isOutOfLine();
1251 }
1252
1253 return false;
1254}
1255
Chris Lattner59a25942008-03-31 00:36:02 +00001256//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001257// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001258//===----------------------------------------------------------------------===//
1259
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001260SourceRange TagDecl::getSourceRange() const {
1261 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001262 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001263}
1264
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001265TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001266 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001267}
1268
Douglas Gregordee1be82009-01-17 00:42:38 +00001269void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001270 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1271 TagT->decl.setPointer(this);
1272 TagT->decl.setInt(1);
1273 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001274}
1275
1276void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +00001277 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001278 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1279 assert(TagT->decl.getPointer() == this &&
1280 "Attempt to redefine a tag definition?");
1281 TagT->decl.setInt(0);
1282 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001283}
1284
Ted Kremenek21475702008-09-05 17:16:31 +00001285TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001286 if (isDefinition())
1287 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001288
1289 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001290 R != REnd; ++R)
1291 if (R->isDefinition())
1292 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001293
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001294 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001295}
1296
John McCall06f6fe8d2009-09-04 01:14:41 +00001297TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1298 switch (TypeSpec) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001299 default: llvm_unreachable("unexpected type specifier");
John McCall06f6fe8d2009-09-04 01:14:41 +00001300 case DeclSpec::TST_struct: return TK_struct;
1301 case DeclSpec::TST_class: return TK_class;
1302 case DeclSpec::TST_union: return TK_union;
1303 case DeclSpec::TST_enum: return TK_enum;
1304 }
1305}
1306
Ted Kremenek21475702008-09-05 17:16:31 +00001307//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001308// RecordDecl Implementation
1309//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001310
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001311RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001312 IdentifierInfo *Id, RecordDecl *PrevDecl,
1313 SourceLocation TKL)
1314 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001315 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001316 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001317 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001318 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001319}
1320
1321RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001322 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001323 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001324
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001325 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001326 C.getTypeDeclType(R, PrevDecl);
1327 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001328}
1329
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001330RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001331}
1332
1333void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001334 TagDecl::Destroy(C);
1335}
1336
Douglas Gregordfcad112009-03-25 15:59:44 +00001337bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001338 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001339 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1340}
1341
Douglas Gregor91f84212008-12-11 16:49:14 +00001342/// completeDefinition - Notes that the definition of this type is now
1343/// complete.
1344void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +00001345 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001346 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001347}
Steve Naroffcc321422007-03-26 23:09:51 +00001348
Steve Naroff415d3d52008-10-08 17:01:13 +00001349//===----------------------------------------------------------------------===//
1350// BlockDecl Implementation
1351//===----------------------------------------------------------------------===//
1352
1353BlockDecl::~BlockDecl() {
1354}
1355
1356void BlockDecl::Destroy(ASTContext& C) {
1357 if (Body)
1358 Body->Destroy(C);
1359
1360 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1361 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001362
1363 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001364 Decl::Destroy(C);
1365}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001366
1367void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1368 unsigned NParms) {
1369 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001370
Steve Naroffc4b30e52009-03-13 16:56:44 +00001371 // Zero params -> null pointer.
1372 if (NParms) {
1373 NumParams = NParms;
1374 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1375 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1376 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1377 }
1378}
1379
1380unsigned BlockDecl::getNumParams() const {
1381 return NumParams;
1382}