blob: bdc804722c4177e1d64aac9031fc453e24ff1703 [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
John McCall9488ea12009-11-17 05:59:44 +0000292 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
293 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
294 cast<UsingShadowDecl>(OldD)->getTargetDecl();
295
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000296 // For non-function declarations, if the declarations are of the
297 // same kind then this must be a redeclaration, or semantic analysis
298 // would not have given us the new declaration.
299 return this->getKind() == OldD->getKind();
300}
301
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000302bool NamedDecl::hasLinkage() const {
303 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
304 return VD->hasExternalStorage() || VD->isFileVarDecl();
305
306 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
307 return true;
308
309 return false;
310}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000311
Anders Carlssone136e0e2009-06-26 06:29:23 +0000312NamedDecl *NamedDecl::getUnderlyingDecl() {
313 NamedDecl *ND = this;
314 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000315 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000316 ND = UD->getTargetDecl();
317 else if (ObjCCompatibleAliasDecl *AD
318 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
319 return AD->getClassInterface();
320 else
321 return ND;
322 }
323}
324
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000325//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000326// DeclaratorDecl Implementation
327//===----------------------------------------------------------------------===//
328
329SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000330 if (DeclInfo) {
331 TypeLoc TL = DeclInfo->getTypeLoc();
332 while (true) {
333 TypeLoc NextTL = TL.getNextTypeLoc();
334 if (!NextTL)
335 return TL.getSourceRange().getBegin();
336 TL = NextTL;
337 }
338 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000339 return SourceLocation();
340}
341
342//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000343// VarDecl Implementation
344//===----------------------------------------------------------------------===//
345
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000346VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000347 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000348 StorageClass S) {
349 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000350}
351
352void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000353 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000354 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000355 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000356 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
357 Eval->~EvaluatedStmt();
358 C.Deallocate(Eval);
359 }
360 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000361 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000362 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000363}
364
365VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000366}
367
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000368SourceRange VarDecl::getSourceRange() const {
369 if (getInit())
370 return SourceRange(getLocation(), getInit()->getLocEnd());
371 return SourceRange(getLocation(), getLocation());
372}
373
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000374bool VarDecl::isOutOfLine() const {
375 if (!isStaticDataMember())
376 return false;
377
378 if (Decl::isOutOfLine())
379 return true;
380
381 // If this static data member was instantiated from a static data member of
382 // a class template, check whether that static data member was defined
383 // out-of-line.
384 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
385 return VD->isOutOfLine();
386
387 return false;
388}
389
Douglas Gregor0d035142009-10-27 18:42:08 +0000390VarDecl *VarDecl::getOutOfLineDefinition() {
391 if (!isStaticDataMember())
392 return 0;
393
394 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
395 RD != RDEnd; ++RD) {
396 if (RD->getLexicalDeclContext()->isFileContext())
397 return *RD;
398 }
399
400 return 0;
401}
402
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000403VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000404 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000405 return cast<VarDecl>(MSI->getInstantiatedFrom());
406
407 return 0;
408}
409
Douglas Gregor663b5a02009-10-14 20:14:33 +0000410TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000411 if (MemberSpecializationInfo *MSI
412 = getASTContext().getInstantiatedFromStaticDataMember(this))
413 return MSI->getTemplateSpecializationKind();
414
415 return TSK_Undeclared;
416}
417
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000418MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000419 return getASTContext().getInstantiatedFromStaticDataMember(this);
420}
421
Douglas Gregor0a897e32009-10-15 17:21:20 +0000422void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
423 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000424 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000425 assert(MSI && "Not an instantiated static data member?");
426 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000427 if (TSK != TSK_ExplicitSpecialization &&
428 PointOfInstantiation.isValid() &&
429 MSI->getPointOfInstantiation().isInvalid())
430 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000431}
432
Douglas Gregor275a3692009-03-10 23:43:53 +0000433bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
434 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
435 return false;
436
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000437 const VarDecl *Def = 0;
438 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000439 (getStorageClass() == None || getStorageClass() == Static));
440}
441
Ted Kremenek082d9362009-03-20 21:35:28 +0000442const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000443 redecl_iterator I = redecls_begin(), E = redecls_end();
444 while (I != E && !I->getInit())
445 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000446
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000447 if (I != E) {
448 Def = *I;
449 return I->getInit();
450 }
451 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000452}
453
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000454VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000455 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000456}
457
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000458//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000459// FunctionDecl Implementation
460//===----------------------------------------------------------------------===//
461
Ted Kremenek27f8a282008-05-20 00:43:19 +0000462void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000463 if (Body && Body.isOffset())
464 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000465
466 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
467 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000468
Douglas Gregor2db32322009-10-07 23:56:10 +0000469 FunctionTemplateSpecializationInfo *FTSInfo
470 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
471 if (FTSInfo)
472 C.Deallocate(FTSInfo);
473
474 MemberSpecializationInfo *MSInfo
475 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
476 if (MSInfo)
477 C.Deallocate(MSInfo);
478
Steve Naroff3e970492009-01-27 21:25:57 +0000479 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000480
Ted Kremenek27f8a282008-05-20 00:43:19 +0000481 Decl::Destroy(C);
482}
483
John McCall136a6982009-09-11 06:45:03 +0000484void FunctionDecl::getNameForDiagnostic(std::string &S,
485 const PrintingPolicy &Policy,
486 bool Qualified) const {
487 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
488 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
489 if (TemplateArgs)
490 S += TemplateSpecializationType::PrintTemplateArgumentList(
491 TemplateArgs->getFlatArgumentList(),
492 TemplateArgs->flat_size(),
493 Policy);
494
495}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000496
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000497Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000498 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
499 if (I->Body) {
500 Definition = *I;
501 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000502 }
503 }
504
505 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000506}
507
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000508void FunctionDecl::setBody(Stmt *B) {
509 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000510 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000511 EndRangeLoc = B->getLocEnd();
512}
513
Douglas Gregor48a83b52009-09-12 00:17:51 +0000514bool FunctionDecl::isMain() const {
515 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000516 return !Context.getLangOptions().Freestanding &&
517 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000518 getIdentifier() && getIdentifier()->isStr("main");
519}
520
Douglas Gregor48a83b52009-09-12 00:17:51 +0000521bool FunctionDecl::isExternC() const {
522 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000523 // In C, any non-static, non-overloadable function has external
524 // linkage.
525 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000526 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000527
Mike Stump1eb44332009-09-09 15:08:12 +0000528 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000529 DC = DC->getParent()) {
530 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
531 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000532 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000533 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000534
535 break;
536 }
537 }
538
539 return false;
540}
541
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000542bool FunctionDecl::isGlobal() const {
543 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
544 return Method->isStatic();
545
546 if (getStorageClass() == Static)
547 return false;
548
Mike Stump1eb44332009-09-09 15:08:12 +0000549 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000550 DC->isNamespace();
551 DC = DC->getParent()) {
552 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
553 if (!Namespace->getDeclName())
554 return false;
555 break;
556 }
557 }
558
559 return true;
560}
561
Douglas Gregor3e41d602009-02-13 23:20:09 +0000562/// \brief Returns a value indicating whether this function
563/// corresponds to a builtin function.
564///
565/// The function corresponds to a built-in function if it is
566/// declared at translation scope or within an extern "C" block and
567/// its name matches with the name of a builtin. The returned value
568/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000569/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000570/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000571unsigned FunctionDecl::getBuiltinID() const {
572 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000573 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
574 return 0;
575
576 unsigned BuiltinID = getIdentifier()->getBuiltinID();
577 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
578 return BuiltinID;
579
580 // This function has the name of a known C library
581 // function. Determine whether it actually refers to the C library
582 // function or whether it just has the same name.
583
Douglas Gregor9add3172009-02-17 03:23:10 +0000584 // If this is a static function, it's not a builtin.
585 if (getStorageClass() == Static)
586 return 0;
587
Douglas Gregor3c385e52009-02-14 18:57:46 +0000588 // If this function is at translation-unit scope and we're not in
589 // C++, it refers to the C library function.
590 if (!Context.getLangOptions().CPlusPlus &&
591 getDeclContext()->isTranslationUnit())
592 return BuiltinID;
593
594 // If the function is in an extern "C" linkage specification and is
595 // not marked "overloadable", it's the real function.
596 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000597 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000598 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000599 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000600 return BuiltinID;
601
602 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000603 return 0;
604}
605
606
Chris Lattner1ad9b282009-04-25 06:03:53 +0000607/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000608/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000609/// after it has been created.
610unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000611 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000612 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000613 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000614 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616}
617
Ted Kremenekfc767612009-01-14 00:42:25 +0000618void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
619 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000621 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 // Zero params -> null pointer.
624 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000625 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000626 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000628
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000629 // Update source range. The check below allows us to set EndRangeLoc before
630 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000631 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000632 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 }
634}
635
Chris Lattner8123a952008-04-10 02:22:51 +0000636/// getMinRequiredArguments - Returns the minimum number of arguments
637/// needed to call this function. This may be fewer than the number of
638/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000639/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000640unsigned FunctionDecl::getMinRequiredArguments() const {
641 unsigned NumRequiredArgs = getNumParams();
642 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000643 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000644 --NumRequiredArgs;
645
646 return NumRequiredArgs;
647}
648
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000649bool FunctionDecl::isInlined() const {
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000650 if (isInlineSpecified() || (isa<CXXMethodDecl>(this) && !isOutOfLine()))
651 return true;
652
653 switch (getTemplateSpecializationKind()) {
654 case TSK_Undeclared:
655 case TSK_ExplicitSpecialization:
656 return false;
657
658 case TSK_ImplicitInstantiation:
659 case TSK_ExplicitInstantiationDeclaration:
660 case TSK_ExplicitInstantiationDefinition:
661 // Handle below.
662 break;
663 }
664
665 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
666 Stmt *Pattern = 0;
667 if (PatternDecl)
668 Pattern = PatternDecl->getBody(PatternDecl);
669
670 if (Pattern && PatternDecl)
671 return PatternDecl->isInlined();
672
673 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000674}
675
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000676/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000677/// definition will be externally visible.
678///
679/// Inline function definitions are always available for inlining optimizations.
680/// However, depending on the language dialect, declaration specifiers, and
681/// attributes, the definition of an inline function may or may not be
682/// "externally" visible to other translation units in the program.
683///
684/// In C99, inline definitions are not externally visible by default. However,
685/// if even one of the globa-scope declarations is marked "extern inline", the
686/// inline definition becomes externally visible (C99 6.7.4p6).
687///
688/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
689/// definition, we use the GNU semantics for inline, which are nearly the
690/// opposite of C99 semantics. In particular, "inline" by itself will create
691/// an externally visible symbol, but "extern inline" will not create an
692/// externally visible symbol.
693bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
694 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000695 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000696 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000697
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000698 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000699 // GNU inline semantics. Based on a number of examples, we came up with the
700 // following heuristic: if the "inline" keyword is present on a
701 // declaration of the function but "extern" is not present on that
702 // declaration, then the symbol is externally visible. Otherwise, the GNU
703 // "extern inline" semantics applies and the symbol is not externally
704 // visible.
705 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
706 Redecl != RedeclEnd;
707 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000708 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000709 return true;
710 }
711
712 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000713 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000714 }
715
716 // C99 6.7.4p6:
717 // [...] If all of the file scope declarations for a function in a
718 // translation unit include the inline function specifier without extern,
719 // then the definition in that translation unit is an inline definition.
720 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
721 Redecl != RedeclEnd;
722 ++Redecl) {
723 // Only consider file-scope declarations in this test.
724 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
725 continue;
726
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000727 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000728 return true; // Not an inline definition
729 }
730
731 // C99 6.7.4p6:
732 // An inline definition does not provide an external definition for the
733 // function, and does not forbid an external definition in another
734 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000735 return false;
736}
737
Mike Stump1eb44332009-09-09 15:08:12 +0000738void
Douglas Gregor127102b2009-06-29 20:59:39 +0000739FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000740 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000741
Douglas Gregor127102b2009-06-29 20:59:39 +0000742 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000743 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000744 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
745 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
746 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
747 }
748}
749
Mike Stump740256b2009-09-29 00:50:50 +0000750const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
751 return getFirstDeclaration();
752}
753
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000754FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000755 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000756}
757
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000758/// getOverloadedOperator - Which C++ overloaded operator this
759/// function represents, if any.
760OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000761 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
762 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000763 else
764 return OO_None;
765}
766
Douglas Gregor2db32322009-10-07 23:56:10 +0000767FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000768 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +0000769 return cast<FunctionDecl>(Info->getInstantiatedFrom());
770
771 return 0;
772}
773
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000774MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
775 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
776}
777
Douglas Gregor2db32322009-10-07 23:56:10 +0000778void
779FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
780 TemplateSpecializationKind TSK) {
781 assert(TemplateOrSpecialization.isNull() &&
782 "Member function is already a specialization");
783 MemberSpecializationInfo *Info
784 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
785 TemplateOrSpecialization = Info;
786}
787
Douglas Gregor3b846b62009-10-27 20:53:28 +0000788bool FunctionDecl::isImplicitlyInstantiable() const {
789 // If this function already has a definition or is invalid, it can't be
790 // implicitly instantiated.
791 if (isInvalidDecl() || getBody())
792 return false;
793
794 switch (getTemplateSpecializationKind()) {
795 case TSK_Undeclared:
796 case TSK_ExplicitSpecialization:
797 case TSK_ExplicitInstantiationDefinition:
798 return false;
799
800 case TSK_ImplicitInstantiation:
801 return true;
802
803 case TSK_ExplicitInstantiationDeclaration:
804 // Handled below.
805 break;
806 }
807
808 // Find the actual template from which we will instantiate.
809 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
810 Stmt *Pattern = 0;
811 if (PatternDecl)
812 Pattern = PatternDecl->getBody(PatternDecl);
813
814 // C++0x [temp.explicit]p9:
815 // Except for inline functions, other explicit instantiation declarations
816 // have the effect of suppressing the implicit instantiation of the entity
817 // to which they refer.
818 if (!Pattern || !PatternDecl)
819 return true;
820
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000821 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +0000822}
823
824FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
825 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
826 while (Primary->getInstantiatedFromMemberTemplate()) {
827 // If we have hit a point where the user provided a specialization of
828 // this template, we're done looking.
829 if (Primary->isMemberSpecialization())
830 break;
831
832 Primary = Primary->getInstantiatedFromMemberTemplate();
833 }
834
835 return Primary->getTemplatedDecl();
836 }
837
838 return getInstantiatedFromMemberFunction();
839}
840
Douglas Gregor16e8be22009-06-29 17:30:29 +0000841FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000842 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000843 = TemplateOrSpecialization
844 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000845 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000846 }
847 return 0;
848}
849
850const TemplateArgumentList *
851FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000852 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000853 = TemplateOrSpecialization
854 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +0000855 return Info->TemplateArguments;
856 }
857 return 0;
858}
859
Mike Stump1eb44332009-09-09 15:08:12 +0000860void
Douglas Gregor1637be72009-06-26 00:10:03 +0000861FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
862 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000863 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000864 void *InsertPos,
865 TemplateSpecializationKind TSK) {
866 assert(TSK != TSK_Undeclared &&
867 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +0000868 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000869 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000870 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000871 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Douglas Gregor127102b2009-06-29 20:59:39 +0000873 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000874 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000875 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +0000876 Info->TemplateArguments = TemplateArgs;
877 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregor127102b2009-06-29 20:59:39 +0000879 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000880 // function template specializations.
881 if (InsertPos)
882 Template->getSpecializations().InsertNode(Info, InsertPos);
883 else {
884 // Try to insert the new node. If there is an existing node, remove it
885 // first.
886 FunctionTemplateSpecializationInfo *Existing
887 = Template->getSpecializations().GetOrInsertNode(Info);
888 if (Existing) {
889 Template->getSpecializations().RemoveNode(Existing);
890 Template->getSpecializations().GetOrInsertNode(Info);
891 }
892 }
Douglas Gregor1637be72009-06-26 00:10:03 +0000893}
894
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000895TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000896 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000897 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +0000898 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000899 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +0000900 if (FTSInfo)
901 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregor2db32322009-10-07 23:56:10 +0000903 MemberSpecializationInfo *MSInfo
904 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
905 if (MSInfo)
906 return MSInfo->getTemplateSpecializationKind();
907
908 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000909}
910
Mike Stump1eb44332009-09-09 15:08:12 +0000911void
Douglas Gregor0a897e32009-10-15 17:21:20 +0000912FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
913 SourceLocation PointOfInstantiation) {
914 if (FunctionTemplateSpecializationInfo *FTSInfo
915 = TemplateOrSpecialization.dyn_cast<
916 FunctionTemplateSpecializationInfo*>()) {
917 FTSInfo->setTemplateSpecializationKind(TSK);
918 if (TSK != TSK_ExplicitSpecialization &&
919 PointOfInstantiation.isValid() &&
920 FTSInfo->getPointOfInstantiation().isInvalid())
921 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
922 } else if (MemberSpecializationInfo *MSInfo
923 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
924 MSInfo->setTemplateSpecializationKind(TSK);
925 if (TSK != TSK_ExplicitSpecialization &&
926 PointOfInstantiation.isValid() &&
927 MSInfo->getPointOfInstantiation().isInvalid())
928 MSInfo->setPointOfInstantiation(PointOfInstantiation);
929 } else
930 assert(false && "Function cannot have a template specialization kind");
931}
932
933SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +0000934 if (FunctionTemplateSpecializationInfo *FTSInfo
935 = TemplateOrSpecialization.dyn_cast<
936 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000937 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +0000938 else if (MemberSpecializationInfo *MSInfo
939 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000940 return MSInfo->getPointOfInstantiation();
941
942 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000943}
944
Douglas Gregor9f185072009-09-11 20:15:17 +0000945bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +0000946 if (Decl::isOutOfLine())
947 return true;
948
949 // If this function was instantiated from a member function of a
950 // class template, check whether that member function was defined out-of-line.
951 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
952 const FunctionDecl *Definition;
953 if (FD->getBody(Definition))
954 return Definition->isOutOfLine();
955 }
956
957 // If this function was instantiated from a function template,
958 // check whether that function template was defined out-of-line.
959 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
960 const FunctionDecl *Definition;
961 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
962 return Definition->isOutOfLine();
963 }
964
965 return false;
966}
967
Chris Lattner8a934232008-03-31 00:36:02 +0000968//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000969// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000970//===----------------------------------------------------------------------===//
971
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000972SourceRange TagDecl::getSourceRange() const {
973 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000974 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000975}
976
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000977TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000978 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000979}
980
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000981void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000982 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
983 TagT->decl.setPointer(this);
984 TagT->decl.setInt(1);
985 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000986}
987
988void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000989 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000990 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
991 assert(TagT->decl.getPointer() == this &&
992 "Attempt to redefine a tag definition?");
993 TagT->decl.setInt(0);
994 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000995}
996
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000997TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000998 if (isDefinition())
999 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
1001 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001002 R != REnd; ++R)
1003 if (R->isDefinition())
1004 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001006 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001007}
1008
John McCallf1bbbb42009-09-04 01:14:41 +00001009TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1010 switch (TypeSpec) {
1011 default: llvm::llvm_unreachable("unexpected type specifier");
1012 case DeclSpec::TST_struct: return TK_struct;
1013 case DeclSpec::TST_class: return TK_class;
1014 case DeclSpec::TST_union: return TK_union;
1015 case DeclSpec::TST_enum: return TK_enum;
1016 }
1017}
1018
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001019//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001020// RecordDecl Implementation
1021//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001022
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001023RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001024 IdentifierInfo *Id, RecordDecl *PrevDecl,
1025 SourceLocation TKL)
1026 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001027 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001028 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001029 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001030 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001031}
1032
1033RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001034 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001035 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001037 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001038 C.getTypeDeclType(R, PrevDecl);
1039 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001040}
1041
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001042RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001043}
1044
1045void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001046 TagDecl::Destroy(C);
1047}
1048
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001049bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001050 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001051 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1052}
1053
Douglas Gregor44b43212008-12-11 16:49:14 +00001054/// completeDefinition - Notes that the definition of this type is now
1055/// complete.
1056void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001058 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001059}
1060
Steve Naroff56ee6892008-10-08 17:01:13 +00001061//===----------------------------------------------------------------------===//
1062// BlockDecl Implementation
1063//===----------------------------------------------------------------------===//
1064
1065BlockDecl::~BlockDecl() {
1066}
1067
1068void BlockDecl::Destroy(ASTContext& C) {
1069 if (Body)
1070 Body->Destroy(C);
1071
1072 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1073 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001074
1075 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001076 Decl::Destroy(C);
1077}
Steve Naroffe78b8092009-03-13 16:56:44 +00001078
1079void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1080 unsigned NParms) {
1081 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Steve Naroffe78b8092009-03-13 16:56:44 +00001083 // Zero params -> null pointer.
1084 if (NParms) {
1085 NumParams = NParms;
1086 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1087 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1088 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1089 }
1090}
1091
1092unsigned BlockDecl::getNumParams() const {
1093 return NumParams;
1094}