blob: a6996a4bfe5cc2cb69960a721439eb0da10e873f [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"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000024#include "clang/Basic/IdentifierTable.h"
John McCallf1bbbb42009-09-04 01:14:41 +000025#include "clang/Parse/DeclSpec.h"
26#include "llvm/Support/ErrorHandling.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000027#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattner0b2b6e12009-03-04 06:34:08 +000031void Attr::Destroy(ASTContext &C) {
32 if (Next) {
33 Next->Destroy(C);
34 Next = 0;
35 }
36 this->~Attr();
37 C.Deallocate((void*)this);
38}
39
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000040/// \brief Return the TypeLoc wrapper for the type source info.
41TypeLoc DeclaratorInfo::getTypeLoc() const {
Argyrios Kyrtzidisb7354712009-09-29 19:40:20 +000042 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000043}
Chris Lattner0b2b6e12009-03-04 06:34:08 +000044
Chris Lattnerd3b90652008-03-15 05:43:15 +000045//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000046// Decl Allocation/Deallocation Method Implementations
47//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattner0b2b6e12009-03-04 06:34:08 +000049
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000050TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +000051 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000052}
53
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000054NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
55 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000056 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000057}
58
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000059void NamespaceDecl::Destroy(ASTContext& C) {
60 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
61 // together. They are all top-level Decls.
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenekebf27b12008-05-24 15:09:56 +000063 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000065}
66
67
Chris Lattner41110242008-06-17 18:05:57 +000068ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000069 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000070 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000071}
72
Daniel Dunbarb286a782009-04-14 02:08:49 +000073const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
74 switch (SC) {
75 case VarDecl::None: break;
76 case VarDecl::Auto: return "auto"; break;
77 case VarDecl::Extern: return "extern"; break;
Mike Stump1eb44332009-09-09 15:08:12 +000078 case VarDecl::PrivateExtern: return "__private_extern__"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000079 case VarDecl::Register: return "register"; break;
Mike Stump1eb44332009-09-09 15:08:12 +000080 case VarDecl::Static: return "static"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000081 }
82
83 assert(0 && "Invalid storage class");
84 return 0;
85}
86
Chris Lattner9fdf9c62008-04-22 18:39:57 +000087ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000088 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +000089 QualType T, DeclaratorInfo *DInfo,
90 StorageClass S, Expr *DefArg) {
91 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, DInfo, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000092}
93
Douglas Gregor6cc15182009-09-11 18:44:32 +000094SourceRange ParmVarDecl::getDefaultArgRange() const {
95 if (const Expr *E = getInit())
96 return E->getSourceRange();
97
98 if (const Expr *E = getUninstantiatedDefaultArg())
99 return E->getSourceRange();
100
101 return SourceRange();
102}
Douglas Gregor78d15832009-05-26 18:54:04 +0000103
Douglas Gregor6cc15182009-09-11 18:44:32 +0000104void VarDecl::setInit(ASTContext &C, Expr *I) {
105 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
106 Eval->~EvaluatedStmt();
107 C.Deallocate(Eval);
Douglas Gregor78d15832009-05-26 18:54:04 +0000108 }
109
Douglas Gregor6cc15182009-09-11 18:44:32 +0000110 Init = I;
111}
112
Douglas Gregor48a83b52009-09-12 00:17:51 +0000113bool VarDecl::isExternC() const {
114 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000115 if (!Context.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000116 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000117 getStorageClass() != Static) ||
118 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
119
Mike Stump1eb44332009-09-09 15:08:12 +0000120 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000121 DC = DC->getParent()) {
122 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
123 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
124 return getStorageClass() != Static;
125
126 break;
127 }
128
129 if (DC->isFunctionOrMethod())
130 return false;
131 }
132
133 return false;
134}
135
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000136FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000137 SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000138 DeclarationName N, QualType T,
139 DeclaratorInfo *DInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000140 StorageClass S, bool isInline,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000141 bool hasWrittenPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +0000142 FunctionDecl *New
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000143 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssona75e8532009-05-14 21:46:00 +0000144 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor2224f842009-02-25 16:33:18 +0000145 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000146}
147
Steve Naroff090276f2008-10-10 01:28:17 +0000148BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000149 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000150}
151
Douglas Gregor44b43212008-12-11 16:49:14 +0000152FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000153 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000154 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
155 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000156}
157
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000158bool FieldDecl::isAnonymousStructOrUnion() const {
159 if (!isImplicit() || getDeclName())
160 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenek6217b802009-07-29 21:53:49 +0000162 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000163 return Record->getDecl()->isAnonymousStructOrUnion();
164
165 return false;
166}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000167
Chris Lattner0ed844b2008-04-04 06:12:32 +0000168EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
169 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000170 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000171 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000172 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000173}
174
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000175void EnumConstantDecl::Destroy(ASTContext& C) {
176 if (Init) Init->Destroy(C);
177 Decl::Destroy(C);
178}
179
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000180TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
John McCallba6a9bd2009-10-24 08:00:42 +0000181 SourceLocation L, IdentifierInfo *Id,
182 DeclaratorInfo *DInfo) {
183 return new (C) TypedefDecl(DC, L, Id, DInfo);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000184}
185
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000186EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000187 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000188 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000189 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000190 C.getTypeDeclType(Enum, PrevDecl);
191 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000192}
193
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000194void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000195 Decl::Destroy(C);
196}
197
Douglas Gregor44b43212008-12-11 16:49:14 +0000198void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
199 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000200 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000201 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000202}
203
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000204FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000205 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000206 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000207 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000208}
209
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000210//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000212//===----------------------------------------------------------------------===//
213
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000214std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000215 return getQualifiedNameAsString(getASTContext().getLangOptions());
216}
217
218std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000219 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
220 // std::string thrashing.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000221 std::vector<std::string> Names;
222 std::string QualName;
223 const DeclContext *Ctx = getDeclContext();
224
225 if (Ctx->isFunctionOrMethod())
226 return getNameAsString();
227
228 while (Ctx) {
229 if (Ctx->isFunctionOrMethod())
230 // FIXME: That probably will happen, when D was member of local
231 // scope class/struct/union. How do we handle this case?
232 break;
233
Mike Stump1eb44332009-09-09 15:08:12 +0000234 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000235 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
236 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
237 std::string TemplateArgsStr
238 = TemplateSpecializationType::PrintTemplateArgumentList(
239 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000240 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000241 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000242 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000243 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000244 Names.push_back(ND->getNameAsString());
245 else
246 break;
247
248 Ctx = Ctx->getParent();
249 }
250
251 std::vector<std::string>::reverse_iterator
252 I = Names.rbegin(),
253 End = Names.rend();
254
255 for (; I!=End; ++I)
256 QualName += *I + "::";
257
258 QualName += getNameAsString();
259
260 return QualName;
261}
262
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000263bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000264 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
265
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000266 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
267 // We want to keep it, unless it nominates same namespace.
268 if (getKind() == Decl::UsingDirective) {
269 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
270 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
271 }
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000273 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
274 // For function declarations, we keep track of redeclarations.
275 return FD->getPreviousDeclaration() == OldD;
276
Douglas Gregore53060f2009-06-25 22:08:12 +0000277 // For function templates, the underlying function declarations are linked.
278 if (const FunctionTemplateDecl *FunctionTemplate
279 = dyn_cast<FunctionTemplateDecl>(this))
280 if (const FunctionTemplateDecl *OldFunctionTemplate
281 = dyn_cast<FunctionTemplateDecl>(OldD))
282 return FunctionTemplate->getTemplatedDecl()
283 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Steve Naroff0de21fd2009-02-22 19:35:57 +0000285 // For method declarations, we keep track of redeclarations.
286 if (isa<ObjCMethodDecl>(this))
287 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
John McCallf36e02d2009-10-09 21:13:30 +0000289 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
290 return true;
291
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000292 // For non-function declarations, if the declarations are of the
293 // same kind then this must be a redeclaration, or semantic analysis
294 // would not have given us the new declaration.
295 return this->getKind() == OldD->getKind();
296}
297
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000298bool NamedDecl::hasLinkage() const {
299 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
300 return VD->hasExternalStorage() || VD->isFileVarDecl();
301
302 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
303 return true;
304
305 return false;
306}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000307
Anders Carlssone136e0e2009-06-26 06:29:23 +0000308NamedDecl *NamedDecl::getUnderlyingDecl() {
309 NamedDecl *ND = this;
310 while (true) {
311 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
312 ND = UD->getTargetDecl();
313 else if (ObjCCompatibleAliasDecl *AD
314 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
315 return AD->getClassInterface();
316 else
317 return ND;
318 }
319}
320
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000321//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000322// DeclaratorDecl Implementation
323//===----------------------------------------------------------------------===//
324
325SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000326 if (DeclInfo) {
327 TypeLoc TL = DeclInfo->getTypeLoc();
328 while (true) {
329 TypeLoc NextTL = TL.getNextTypeLoc();
330 if (!NextTL)
331 return TL.getSourceRange().getBegin();
332 TL = NextTL;
333 }
334 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000335 return SourceLocation();
336}
337
338//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000339// VarDecl Implementation
340//===----------------------------------------------------------------------===//
341
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000342VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000343 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000344 StorageClass S) {
345 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000346}
347
348void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000349 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000350 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000351 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000352 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
353 Eval->~EvaluatedStmt();
354 C.Deallocate(Eval);
355 }
356 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000357 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000358 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000359}
360
361VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000362}
363
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000364SourceRange VarDecl::getSourceRange() const {
365 if (getInit())
366 return SourceRange(getLocation(), getInit()->getLocEnd());
367 return SourceRange(getLocation(), getLocation());
368}
369
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000370bool VarDecl::isOutOfLine() const {
371 if (!isStaticDataMember())
372 return false;
373
374 if (Decl::isOutOfLine())
375 return true;
376
377 // If this static data member was instantiated from a static data member of
378 // a class template, check whether that static data member was defined
379 // out-of-line.
380 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
381 return VD->isOutOfLine();
382
383 return false;
384}
385
Douglas Gregor0d035142009-10-27 18:42:08 +0000386VarDecl *VarDecl::getOutOfLineDefinition() {
387 if (!isStaticDataMember())
388 return 0;
389
390 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
391 RD != RDEnd; ++RD) {
392 if (RD->getLexicalDeclContext()->isFileContext())
393 return *RD;
394 }
395
396 return 0;
397}
398
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000399VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000400 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000401 return cast<VarDecl>(MSI->getInstantiatedFrom());
402
403 return 0;
404}
405
Douglas Gregor663b5a02009-10-14 20:14:33 +0000406TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000407 if (MemberSpecializationInfo *MSI
408 = getASTContext().getInstantiatedFromStaticDataMember(this))
409 return MSI->getTemplateSpecializationKind();
410
411 return TSK_Undeclared;
412}
413
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000414MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000415 return getASTContext().getInstantiatedFromStaticDataMember(this);
416}
417
Douglas Gregor0a897e32009-10-15 17:21:20 +0000418void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
419 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000420 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000421 assert(MSI && "Not an instantiated static data member?");
422 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000423 if (TSK != TSK_ExplicitSpecialization &&
424 PointOfInstantiation.isValid() &&
425 MSI->getPointOfInstantiation().isInvalid())
426 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000427}
428
Douglas Gregor275a3692009-03-10 23:43:53 +0000429bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
430 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
431 return false;
432
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000433 const VarDecl *Def = 0;
434 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000435 (getStorageClass() == None || getStorageClass() == Static));
436}
437
Ted Kremenek082d9362009-03-20 21:35:28 +0000438const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000439 redecl_iterator I = redecls_begin(), E = redecls_end();
440 while (I != E && !I->getInit())
441 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000442
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000443 if (I != E) {
444 Def = *I;
445 return I->getInit();
446 }
447 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000448}
449
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000450VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000451 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000452}
453
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000454//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000455// FunctionDecl Implementation
456//===----------------------------------------------------------------------===//
457
Ted Kremenek27f8a282008-05-20 00:43:19 +0000458void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000459 if (Body && Body.isOffset())
460 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000461
462 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
463 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000464
Douglas Gregor2db32322009-10-07 23:56:10 +0000465 FunctionTemplateSpecializationInfo *FTSInfo
466 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
467 if (FTSInfo)
468 C.Deallocate(FTSInfo);
469
470 MemberSpecializationInfo *MSInfo
471 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
472 if (MSInfo)
473 C.Deallocate(MSInfo);
474
Steve Naroff3e970492009-01-27 21:25:57 +0000475 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000476
Ted Kremenek27f8a282008-05-20 00:43:19 +0000477 Decl::Destroy(C);
478}
479
John McCall136a6982009-09-11 06:45:03 +0000480void FunctionDecl::getNameForDiagnostic(std::string &S,
481 const PrintingPolicy &Policy,
482 bool Qualified) const {
483 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
484 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
485 if (TemplateArgs)
486 S += TemplateSpecializationType::PrintTemplateArgumentList(
487 TemplateArgs->getFlatArgumentList(),
488 TemplateArgs->flat_size(),
489 Policy);
490
491}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000492
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000493Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000494 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
495 if (I->Body) {
496 Definition = *I;
497 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000498 }
499 }
500
501 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000502}
503
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000504void FunctionDecl::setBody(Stmt *B) {
505 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000506 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000507 EndRangeLoc = B->getLocEnd();
508}
509
Douglas Gregor48a83b52009-09-12 00:17:51 +0000510bool FunctionDecl::isMain() const {
511 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000512 return !Context.getLangOptions().Freestanding &&
513 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000514 getIdentifier() && getIdentifier()->isStr("main");
515}
516
Douglas Gregor48a83b52009-09-12 00:17:51 +0000517bool FunctionDecl::isExternC() const {
518 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000519 // In C, any non-static, non-overloadable function has external
520 // linkage.
521 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000522 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000523
Mike Stump1eb44332009-09-09 15:08:12 +0000524 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000525 DC = DC->getParent()) {
526 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
527 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000528 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000529 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000530
531 break;
532 }
533 }
534
535 return false;
536}
537
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000538bool FunctionDecl::isGlobal() const {
539 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
540 return Method->isStatic();
541
542 if (getStorageClass() == Static)
543 return false;
544
Mike Stump1eb44332009-09-09 15:08:12 +0000545 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000546 DC->isNamespace();
547 DC = DC->getParent()) {
548 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
549 if (!Namespace->getDeclName())
550 return false;
551 break;
552 }
553 }
554
555 return true;
556}
557
Douglas Gregor3e41d602009-02-13 23:20:09 +0000558/// \brief Returns a value indicating whether this function
559/// corresponds to a builtin function.
560///
561/// The function corresponds to a built-in function if it is
562/// declared at translation scope or within an extern "C" block and
563/// its name matches with the name of a builtin. The returned value
564/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000565/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000566/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000567unsigned FunctionDecl::getBuiltinID() const {
568 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000569 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
570 return 0;
571
572 unsigned BuiltinID = getIdentifier()->getBuiltinID();
573 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
574 return BuiltinID;
575
576 // This function has the name of a known C library
577 // function. Determine whether it actually refers to the C library
578 // function or whether it just has the same name.
579
Douglas Gregor9add3172009-02-17 03:23:10 +0000580 // If this is a static function, it's not a builtin.
581 if (getStorageClass() == Static)
582 return 0;
583
Douglas Gregor3c385e52009-02-14 18:57:46 +0000584 // If this function is at translation-unit scope and we're not in
585 // C++, it refers to the C library function.
586 if (!Context.getLangOptions().CPlusPlus &&
587 getDeclContext()->isTranslationUnit())
588 return BuiltinID;
589
590 // If the function is in an extern "C" linkage specification and is
591 // not marked "overloadable", it's the real function.
592 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000593 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000594 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000595 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000596 return BuiltinID;
597
598 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000599 return 0;
600}
601
602
Chris Lattner1ad9b282009-04-25 06:03:53 +0000603/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000604/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000605/// after it has been created.
606unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000607 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000608 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000609 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000610 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612}
613
Ted Kremenekfc767612009-01-14 00:42:25 +0000614void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
615 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000617 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 // Zero params -> null pointer.
620 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000621 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000622 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000624
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000625 // Update source range. The check below allows us to set EndRangeLoc before
626 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000627 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000628 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 }
630}
631
Chris Lattner8123a952008-04-10 02:22:51 +0000632/// getMinRequiredArguments - Returns the minimum number of arguments
633/// needed to call this function. This may be fewer than the number of
634/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000635/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000636unsigned FunctionDecl::getMinRequiredArguments() const {
637 unsigned NumRequiredArgs = getNumParams();
638 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000639 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000640 --NumRequiredArgs;
641
642 return NumRequiredArgs;
643}
644
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000645bool FunctionDecl::isInlined() const {
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000646 if (isInlineSpecified() || (isa<CXXMethodDecl>(this) && !isOutOfLine()))
647 return true;
648
649 switch (getTemplateSpecializationKind()) {
650 case TSK_Undeclared:
651 case TSK_ExplicitSpecialization:
652 return false;
653
654 case TSK_ImplicitInstantiation:
655 case TSK_ExplicitInstantiationDeclaration:
656 case TSK_ExplicitInstantiationDefinition:
657 // Handle below.
658 break;
659 }
660
661 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
662 Stmt *Pattern = 0;
663 if (PatternDecl)
664 Pattern = PatternDecl->getBody(PatternDecl);
665
666 if (Pattern && PatternDecl)
667 return PatternDecl->isInlined();
668
669 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000670}
671
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000672/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000673/// definition will be externally visible.
674///
675/// Inline function definitions are always available for inlining optimizations.
676/// However, depending on the language dialect, declaration specifiers, and
677/// attributes, the definition of an inline function may or may not be
678/// "externally" visible to other translation units in the program.
679///
680/// In C99, inline definitions are not externally visible by default. However,
681/// if even one of the globa-scope declarations is marked "extern inline", the
682/// inline definition becomes externally visible (C99 6.7.4p6).
683///
684/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
685/// definition, we use the GNU semantics for inline, which are nearly the
686/// opposite of C99 semantics. In particular, "inline" by itself will create
687/// an externally visible symbol, but "extern inline" will not create an
688/// externally visible symbol.
689bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
690 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000691 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000692 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000693
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000694 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000695 // GNU inline semantics. Based on a number of examples, we came up with the
696 // following heuristic: if the "inline" keyword is present on a
697 // declaration of the function but "extern" is not present on that
698 // declaration, then the symbol is externally visible. Otherwise, the GNU
699 // "extern inline" semantics applies and the symbol is not externally
700 // visible.
701 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
702 Redecl != RedeclEnd;
703 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000704 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000705 return true;
706 }
707
708 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000709 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000710 }
711
712 // C99 6.7.4p6:
713 // [...] If all of the file scope declarations for a function in a
714 // translation unit include the inline function specifier without extern,
715 // then the definition in that translation unit is an inline definition.
716 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
717 Redecl != RedeclEnd;
718 ++Redecl) {
719 // Only consider file-scope declarations in this test.
720 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
721 continue;
722
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000723 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000724 return true; // Not an inline definition
725 }
726
727 // C99 6.7.4p6:
728 // An inline definition does not provide an external definition for the
729 // function, and does not forbid an external definition in another
730 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000731 return false;
732}
733
Mike Stump1eb44332009-09-09 15:08:12 +0000734void
Douglas Gregor127102b2009-06-29 20:59:39 +0000735FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000736 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000737
Douglas Gregor127102b2009-06-29 20:59:39 +0000738 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000739 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000740 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
741 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
742 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
743 }
744}
745
Mike Stump740256b2009-09-29 00:50:50 +0000746const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
747 return getFirstDeclaration();
748}
749
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000750FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000751 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000752}
753
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000754/// getOverloadedOperator - Which C++ overloaded operator this
755/// function represents, if any.
756OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000757 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
758 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000759 else
760 return OO_None;
761}
762
Douglas Gregor2db32322009-10-07 23:56:10 +0000763FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000764 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +0000765 return cast<FunctionDecl>(Info->getInstantiatedFrom());
766
767 return 0;
768}
769
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000770MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
771 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
772}
773
Douglas Gregor2db32322009-10-07 23:56:10 +0000774void
775FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
776 TemplateSpecializationKind TSK) {
777 assert(TemplateOrSpecialization.isNull() &&
778 "Member function is already a specialization");
779 MemberSpecializationInfo *Info
780 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
781 TemplateOrSpecialization = Info;
782}
783
Douglas Gregor3b846b62009-10-27 20:53:28 +0000784bool FunctionDecl::isImplicitlyInstantiable() const {
785 // If this function already has a definition or is invalid, it can't be
786 // implicitly instantiated.
787 if (isInvalidDecl() || getBody())
788 return false;
789
790 switch (getTemplateSpecializationKind()) {
791 case TSK_Undeclared:
792 case TSK_ExplicitSpecialization:
793 case TSK_ExplicitInstantiationDefinition:
794 return false;
795
796 case TSK_ImplicitInstantiation:
797 return true;
798
799 case TSK_ExplicitInstantiationDeclaration:
800 // Handled below.
801 break;
802 }
803
804 // Find the actual template from which we will instantiate.
805 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
806 Stmt *Pattern = 0;
807 if (PatternDecl)
808 Pattern = PatternDecl->getBody(PatternDecl);
809
810 // C++0x [temp.explicit]p9:
811 // Except for inline functions, other explicit instantiation declarations
812 // have the effect of suppressing the implicit instantiation of the entity
813 // to which they refer.
814 if (!Pattern || !PatternDecl)
815 return true;
816
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000817 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +0000818}
819
820FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
821 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
822 while (Primary->getInstantiatedFromMemberTemplate()) {
823 // If we have hit a point where the user provided a specialization of
824 // this template, we're done looking.
825 if (Primary->isMemberSpecialization())
826 break;
827
828 Primary = Primary->getInstantiatedFromMemberTemplate();
829 }
830
831 return Primary->getTemplatedDecl();
832 }
833
834 return getInstantiatedFromMemberFunction();
835}
836
Douglas Gregor16e8be22009-06-29 17:30:29 +0000837FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000838 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000839 = TemplateOrSpecialization
840 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000841 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000842 }
843 return 0;
844}
845
846const TemplateArgumentList *
847FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000848 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000849 = TemplateOrSpecialization
850 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +0000851 return Info->TemplateArguments;
852 }
853 return 0;
854}
855
Mike Stump1eb44332009-09-09 15:08:12 +0000856void
Douglas Gregor1637be72009-06-26 00:10:03 +0000857FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
858 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000859 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000860 void *InsertPos,
861 TemplateSpecializationKind TSK) {
862 assert(TSK != TSK_Undeclared &&
863 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +0000864 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000865 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000866 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000867 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Douglas Gregor127102b2009-06-29 20:59:39 +0000869 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000870 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000871 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +0000872 Info->TemplateArguments = TemplateArgs;
873 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor127102b2009-06-29 20:59:39 +0000875 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000876 // function template specializations.
877 if (InsertPos)
878 Template->getSpecializations().InsertNode(Info, InsertPos);
879 else {
880 // Try to insert the new node. If there is an existing node, remove it
881 // first.
882 FunctionTemplateSpecializationInfo *Existing
883 = Template->getSpecializations().GetOrInsertNode(Info);
884 if (Existing) {
885 Template->getSpecializations().RemoveNode(Existing);
886 Template->getSpecializations().GetOrInsertNode(Info);
887 }
888 }
Douglas Gregor1637be72009-06-26 00:10:03 +0000889}
890
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000891TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000892 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000893 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +0000894 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000895 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +0000896 if (FTSInfo)
897 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregor2db32322009-10-07 23:56:10 +0000899 MemberSpecializationInfo *MSInfo
900 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
901 if (MSInfo)
902 return MSInfo->getTemplateSpecializationKind();
903
904 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000905}
906
Mike Stump1eb44332009-09-09 15:08:12 +0000907void
Douglas Gregor0a897e32009-10-15 17:21:20 +0000908FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
909 SourceLocation PointOfInstantiation) {
910 if (FunctionTemplateSpecializationInfo *FTSInfo
911 = TemplateOrSpecialization.dyn_cast<
912 FunctionTemplateSpecializationInfo*>()) {
913 FTSInfo->setTemplateSpecializationKind(TSK);
914 if (TSK != TSK_ExplicitSpecialization &&
915 PointOfInstantiation.isValid() &&
916 FTSInfo->getPointOfInstantiation().isInvalid())
917 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
918 } else if (MemberSpecializationInfo *MSInfo
919 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
920 MSInfo->setTemplateSpecializationKind(TSK);
921 if (TSK != TSK_ExplicitSpecialization &&
922 PointOfInstantiation.isValid() &&
923 MSInfo->getPointOfInstantiation().isInvalid())
924 MSInfo->setPointOfInstantiation(PointOfInstantiation);
925 } else
926 assert(false && "Function cannot have a template specialization kind");
927}
928
929SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +0000930 if (FunctionTemplateSpecializationInfo *FTSInfo
931 = TemplateOrSpecialization.dyn_cast<
932 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000933 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +0000934 else if (MemberSpecializationInfo *MSInfo
935 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000936 return MSInfo->getPointOfInstantiation();
937
938 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000939}
940
Douglas Gregor9f185072009-09-11 20:15:17 +0000941bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +0000942 if (Decl::isOutOfLine())
943 return true;
944
945 // If this function was instantiated from a member function of a
946 // class template, check whether that member function was defined out-of-line.
947 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
948 const FunctionDecl *Definition;
949 if (FD->getBody(Definition))
950 return Definition->isOutOfLine();
951 }
952
953 // If this function was instantiated from a function template,
954 // check whether that function template was defined out-of-line.
955 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
956 const FunctionDecl *Definition;
957 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
958 return Definition->isOutOfLine();
959 }
960
961 return false;
962}
963
Chris Lattner8a934232008-03-31 00:36:02 +0000964//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000965// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000966//===----------------------------------------------------------------------===//
967
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000968SourceRange TagDecl::getSourceRange() const {
969 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000970 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000971}
972
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000973TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000974 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000975}
976
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000977void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000978 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
979 TagT->decl.setPointer(this);
980 TagT->decl.setInt(1);
981 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000982}
983
984void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000985 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000986 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
987 assert(TagT->decl.getPointer() == this &&
988 "Attempt to redefine a tag definition?");
989 TagT->decl.setInt(0);
990 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000991}
992
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000993TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000994 if (isDefinition())
995 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000996
997 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000998 R != REnd; ++R)
999 if (R->isDefinition())
1000 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001002 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001003}
1004
John McCallf1bbbb42009-09-04 01:14:41 +00001005TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1006 switch (TypeSpec) {
1007 default: llvm::llvm_unreachable("unexpected type specifier");
1008 case DeclSpec::TST_struct: return TK_struct;
1009 case DeclSpec::TST_class: return TK_class;
1010 case DeclSpec::TST_union: return TK_union;
1011 case DeclSpec::TST_enum: return TK_enum;
1012 }
1013}
1014
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001015//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001016// RecordDecl Implementation
1017//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001018
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001019RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001020 IdentifierInfo *Id, RecordDecl *PrevDecl,
1021 SourceLocation TKL)
1022 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001023 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001024 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001025 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001026 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001027}
1028
1029RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001030 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001031 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001033 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001034 C.getTypeDeclType(R, PrevDecl);
1035 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001036}
1037
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001038RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001039}
1040
1041void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001042 TagDecl::Destroy(C);
1043}
1044
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001045bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001046 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001047 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1048}
1049
Douglas Gregor44b43212008-12-11 16:49:14 +00001050/// completeDefinition - Notes that the definition of this type is now
1051/// complete.
1052void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001054 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001055}
1056
Steve Naroff56ee6892008-10-08 17:01:13 +00001057//===----------------------------------------------------------------------===//
1058// BlockDecl Implementation
1059//===----------------------------------------------------------------------===//
1060
1061BlockDecl::~BlockDecl() {
1062}
1063
1064void BlockDecl::Destroy(ASTContext& C) {
1065 if (Body)
1066 Body->Destroy(C);
1067
1068 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1069 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
1071 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001072 Decl::Destroy(C);
1073}
Steve Naroffe78b8092009-03-13 16:56:44 +00001074
1075void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1076 unsigned NParms) {
1077 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Steve Naroffe78b8092009-03-13 16:56:44 +00001079 // Zero params -> null pointer.
1080 if (NParms) {
1081 NumParams = NParms;
1082 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1083 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1084 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1085 }
1086}
1087
1088unsigned BlockDecl::getNumParams() const {
1089 return NumParams;
1090}