blob: fc19e61a0997deddea59088880238bc0536c2d7a [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Argiris Kirtzidise7dfca12008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor7a7be652009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffd85ba922009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor9054f982009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnere4650482008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argiris Kirtzidisf1a75052009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopesc98406e2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000024#include "clang/Basic/IdentifierTable.h"
Douglas Gregor09be81b2009-02-04 17:27:36 +000025#include <vector>
Ted Kremenekafdf8112008-05-20 00:43:19 +000026
Chris Lattner4b009652007-07-25 00:24:17 +000027using namespace clang;
28
Chris Lattner16ded572009-03-04 06:34:08 +000029void Attr::Destroy(ASTContext &C) {
30 if (Next) {
31 Next->Destroy(C);
32 Next = 0;
33 }
34 this->~Attr();
35 C.Deallocate((void*)this);
36}
37
Argiris Kirtzidisf1a75052009-08-19 01:27:32 +000038/// \brief Return the TypeLoc wrapper for the type source info.
39TypeLoc DeclaratorInfo::getTypeLoc() const {
40 return TypeLoc::Create(Ty, (void*)(this + 1));
41}
Chris Lattner16ded572009-03-04 06:34:08 +000042
Chris Lattnera8344c32008-03-15 05:43:15 +000043//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000044// Decl Allocation/Deallocation Method Implementations
45//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000046
Chris Lattner16ded572009-03-04 06:34:08 +000047
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000048TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argiris Kirtzidis2a6dca12009-06-29 17:38:40 +000049 return new (C) TranslationUnitDecl(C);
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000050}
51
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000052NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
53 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000054 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000055}
56
Ted Kremenek5be49242008-05-20 04:49:55 +000057void NamespaceDecl::Destroy(ASTContext& C) {
58 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
59 // together. They are all top-level Decls.
60
Ted Kremenek0f433842008-05-24 15:09:56 +000061 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000062 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000063}
64
65
Chris Lattner8c7c6a12008-06-17 18:05:57 +000066ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000067 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000068 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000069}
70
Daniel Dunbarcaf78fb2009-04-14 02:08:49 +000071const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
72 switch (SC) {
73 case VarDecl::None: break;
74 case VarDecl::Auto: return "auto"; break;
75 case VarDecl::Extern: return "extern"; break;
76 case VarDecl::PrivateExtern: return "__private_extern__"; break;
77 case VarDecl::Register: return "register"; break;
78 case VarDecl::Static: return "static"; break;
79 }
80
81 assert(0 && "Invalid storage class");
82 return 0;
83}
84
Chris Lattneref87a202008-04-22 18:39:57 +000085ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000086 SourceLocation L, IdentifierInfo *Id,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +000087 QualType T, DeclaratorInfo *DInfo,
88 StorageClass S, Expr *DefArg) {
89 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, DInfo, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000090}
91
92QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000093 if (const OriginalParmVarDecl *PVD =
94 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000095 return PVD->OriginalType;
96 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000097}
98
Douglas Gregor4833ff02009-05-26 18:54:04 +000099void VarDecl::setInit(ASTContext &C, Expr *I) {
100 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
101 Eval->~EvaluatedStmt();
102 C.Deallocate(Eval);
103 }
104
105 Init = I;
106 }
107
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000108bool VarDecl::isExternC(ASTContext &Context) const {
109 if (!Context.getLangOptions().CPlusPlus)
110 return (getDeclContext()->isTranslationUnit() &&
111 getStorageClass() != Static) ||
112 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
113
114 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
115 DC = DC->getParent()) {
116 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
117 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
118 return getStorageClass() != Static;
119
120 break;
121 }
122
123 if (DC->isFunctionOrMethod())
124 return false;
125 }
126
127 return false;
128}
129
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000130OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000131 ASTContext &C, DeclContext *DC,
132 SourceLocation L, IdentifierInfo *Id,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000133 QualType T, DeclaratorInfo *DInfo,
134 QualType OT, StorageClass S, Expr *DefArg) {
135 return new (C) OriginalParmVarDecl(DC, L, Id, T, DInfo, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000136}
137
Chris Lattneref87a202008-04-22 18:39:57 +0000138FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000139 SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000140 DeclarationName N, QualType T,
141 DeclaratorInfo *DInfo,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000142 StorageClass S, bool isInline,
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000143 bool hasWrittenPrototype) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000144 FunctionDecl *New
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000145 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssond9d6d502009-05-14 21:46:00 +0000146 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000147 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000148}
149
Steve Naroff52059382008-10-10 01:28:17 +0000150BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000151 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000152}
153
Douglas Gregor8acb7272008-12-11 16:49:14 +0000154FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000155 IdentifierInfo *Id, QualType T,
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000156 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
157 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +0000158}
159
Douglas Gregorc7f01612009-01-07 19:46:03 +0000160bool FieldDecl::isAnonymousStructOrUnion() const {
161 if (!isImplicit() || getDeclName())
162 return false;
163
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000164 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorc7f01612009-01-07 19:46:03 +0000165 return Record->getDecl()->isAnonymousStructOrUnion();
166
167 return false;
168}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000169
Chris Lattnereee57c02008-04-04 06:12:32 +0000170EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
171 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000172 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000173 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000174 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000175}
176
Ted Kremenek5be49242008-05-20 04:49:55 +0000177void EnumConstantDecl::Destroy(ASTContext& C) {
178 if (Init) Init->Destroy(C);
179 Decl::Destroy(C);
180}
181
Chris Lattneref87a202008-04-22 18:39:57 +0000182TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000183 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000184 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000185 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000186}
187
Chris Lattneref87a202008-04-22 18:39:57 +0000188EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000189 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorae644892008-12-15 16:32:14 +0000190 EnumDecl *PrevDecl) {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000191 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorae644892008-12-15 16:32:14 +0000192 C.getTypeDeclType(Enum, PrevDecl);
193 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000194}
195
Ted Kremenek25d8be12008-09-02 20:13:32 +0000196void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000197 Decl::Destroy(C);
198}
199
Douglas Gregor8acb7272008-12-11 16:49:14 +0000200void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
201 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000202 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000203 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000204}
205
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000206FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000207 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000208 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000209 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000210}
211
Chris Lattnere4650482008-03-15 06:12:44 +0000212//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000213// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000214//===----------------------------------------------------------------------===//
215
Douglas Gregor09be81b2009-02-04 17:27:36 +0000216std::string NamedDecl::getQualifiedNameAsString() const {
217 std::vector<std::string> Names;
218 std::string QualName;
219 const DeclContext *Ctx = getDeclContext();
220
221 if (Ctx->isFunctionOrMethod())
222 return getNameAsString();
223
224 while (Ctx) {
225 if (Ctx->isFunctionOrMethod())
226 // FIXME: That probably will happen, when D was member of local
227 // scope class/struct/union. How do we handle this case?
228 break;
229
Douglas Gregorb12249d2009-05-18 17:01:57 +0000230 if (const ClassTemplateSpecializationDecl *Spec
231 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
232 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Chris Lattner7099c782009-06-30 01:26:17 +0000233 PrintingPolicy Policy(getASTContext().getLangOptions());
Douglas Gregorb12249d2009-05-18 17:01:57 +0000234 std::string TemplateArgsStr
235 = TemplateSpecializationType::PrintTemplateArgumentList(
236 TemplateArgs.getFlatArgumentList(),
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000237 TemplateArgs.flat_size(),
238 Policy);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000239 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
240 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor09be81b2009-02-04 17:27:36 +0000241 Names.push_back(ND->getNameAsString());
242 else
243 break;
244
245 Ctx = Ctx->getParent();
246 }
247
248 std::vector<std::string>::reverse_iterator
249 I = Names.rbegin(),
250 End = Names.rend();
251
252 for (; I!=End; ++I)
253 QualName += *I + "::";
254
255 QualName += getNameAsString();
256
257 return QualName;
258}
259
260
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000261bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000262 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
263
Douglas Gregor7a7be652009-02-03 19:21:40 +0000264 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
265 // We want to keep it, unless it nominates same namespace.
266 if (getKind() == Decl::UsingDirective) {
267 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
268 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
269 }
270
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000271 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
272 // For function declarations, we keep track of redeclarations.
273 return FD->getPreviousDeclaration() == OldD;
274
Douglas Gregorb60eb752009-06-25 22:08:12 +0000275 // For function templates, the underlying function declarations are linked.
276 if (const FunctionTemplateDecl *FunctionTemplate
277 = dyn_cast<FunctionTemplateDecl>(this))
278 if (const FunctionTemplateDecl *OldFunctionTemplate
279 = dyn_cast<FunctionTemplateDecl>(OldD))
280 return FunctionTemplate->getTemplatedDecl()
281 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
282
Steve Naroffd85ba922009-02-22 19:35:57 +0000283 // For method declarations, we keep track of redeclarations.
284 if (isa<ObjCMethodDecl>(this))
285 return false;
286
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000287 // For non-function declarations, if the declarations are of the
288 // same kind then this must be a redeclaration, or semantic analysis
289 // would not have given us the new declaration.
290 return this->getKind() == OldD->getKind();
291}
292
Douglas Gregor1c52c632009-02-24 20:03:32 +0000293bool NamedDecl::hasLinkage() const {
294 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
295 return VD->hasExternalStorage() || VD->isFileVarDecl();
296
297 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
298 return true;
299
300 return false;
301}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000302
Anders Carlssondf5ab842009-06-26 06:29:23 +0000303NamedDecl *NamedDecl::getUnderlyingDecl() {
304 NamedDecl *ND = this;
305 while (true) {
306 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
307 ND = UD->getTargetDecl();
308 else if (ObjCCompatibleAliasDecl *AD
309 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
310 return AD->getClassInterface();
311 else
312 return ND;
313 }
314}
315
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000316//===----------------------------------------------------------------------===//
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000317// DeclaratorDecl Implementation
318//===----------------------------------------------------------------------===//
319
320SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
321 if (DeclInfo)
322 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
323 return SourceLocation();
324}
325
326//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000327// VarDecl Implementation
328//===----------------------------------------------------------------------===//
329
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000330VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000331 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000332 StorageClass S) {
333 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000334}
335
336void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000337 Expr *Init = getInit();
Douglas Gregor4833ff02009-05-26 18:54:04 +0000338 if (Init) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000339 Init->Destroy(C);
Douglas Gregor4833ff02009-05-26 18:54:04 +0000340 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
341 Eval->~EvaluatedStmt();
342 C.Deallocate(Eval);
343 }
344 }
Nuno Lopesc98406e2008-12-17 23:39:55 +0000345 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000346 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000347}
348
349VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000350}
351
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000352SourceRange VarDecl::getSourceRange() const {
353 if (getInit())
354 return SourceRange(getLocation(), getInit()->getLocEnd());
355 return SourceRange(getLocation(), getLocation());
356}
357
Douglas Gregor181fe792009-07-24 20:34:43 +0000358VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
359 return getASTContext().getInstantiatedFromStaticDataMember(this);
360}
361
Douglas Gregor2f728b22009-03-10 23:43:53 +0000362bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
363 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
364 return false;
365
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000366 const VarDecl *Def = 0;
367 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000368 (getStorageClass() == None || getStorageClass() == Static));
369}
370
Ted Kremenek51787c72009-03-20 21:35:28 +0000371const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000372 redecl_iterator I = redecls_begin(), E = redecls_end();
373 while (I != E && !I->getInit())
374 ++I;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000375
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000376 if (I != E) {
377 Def = *I;
378 return I->getInit();
379 }
380 return 0;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000381}
382
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000383VarDecl *VarDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000384 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000385}
386
Nuno Lopesc98406e2008-12-17 23:39:55 +0000387//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000388// FunctionDecl Implementation
389//===----------------------------------------------------------------------===//
390
Ted Kremenekafdf8112008-05-20 00:43:19 +0000391void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000392 if (Body && Body.isOffset())
393 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000394
395 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
396 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000397
Steve Naroff5abb0282009-01-27 21:25:57 +0000398 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000399
Ted Kremenekafdf8112008-05-20 00:43:19 +0000400 Decl::Destroy(C);
401}
402
403
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000404Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000405 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
406 if (I->Body) {
407 Definition = *I;
408 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor42214c52008-04-21 02:02:58 +0000409 }
410 }
411
412 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000413}
414
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000415Stmt *FunctionDecl::getBodyIfAvailable() const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000416 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
417 if (I->Body && !I->Body.isOffset()) {
418 return I->Body.get(0);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000419 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000420 }
421
422 return 0;
423}
424
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000425void FunctionDecl::setBody(Stmt *B) {
426 Body = B;
Argiris Kirtzidis25822a02009-06-22 17:13:31 +0000427 if (B)
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000428 EndRangeLoc = B->getLocEnd();
429}
430
John McCallcb6dd8a2009-08-15 02:09:25 +0000431bool FunctionDecl::isMain(ASTContext &Context) const {
432 return !Context.getLangOptions().Freestanding &&
433 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregoraf682022009-02-24 01:23:02 +0000434 getIdentifier() && getIdentifier()->isStr("main");
435}
436
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000437bool FunctionDecl::isExternC(ASTContext &Context) const {
438 // In C, any non-static, non-overloadable function has external
439 // linkage.
440 if (!Context.getLangOptions().CPlusPlus)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000441 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000442
443 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
444 DC = DC->getParent()) {
445 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
446 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000447 return getStorageClass() != Static &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000448 !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000449
450 break;
451 }
452 }
453
454 return false;
455}
456
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000457bool FunctionDecl::isGlobal() const {
458 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
459 return Method->isStatic();
460
461 if (getStorageClass() == Static)
462 return false;
463
464 for (const DeclContext *DC = getDeclContext();
465 DC->isNamespace();
466 DC = DC->getParent()) {
467 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
468 if (!Namespace->getDeclName())
469 return false;
470 break;
471 }
472 }
473
474 return true;
475}
476
Douglas Gregor411889e2009-02-13 23:20:09 +0000477/// \brief Returns a value indicating whether this function
478/// corresponds to a builtin function.
479///
480/// The function corresponds to a built-in function if it is
481/// declared at translation scope or within an extern "C" block and
482/// its name matches with the name of a builtin. The returned value
483/// will be 0 for functions that do not correspond to a builtin, a
484/// value of type \c Builtin::ID if in the target-independent range
485/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000486unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
487 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
488 return 0;
489
490 unsigned BuiltinID = getIdentifier()->getBuiltinID();
491 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
492 return BuiltinID;
493
494 // This function has the name of a known C library
495 // function. Determine whether it actually refers to the C library
496 // function or whether it just has the same name.
497
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000498 // If this is a static function, it's not a builtin.
499 if (getStorageClass() == Static)
500 return 0;
501
Douglas Gregorb5af7382009-02-14 18:57:46 +0000502 // If this function is at translation-unit scope and we're not in
503 // C++, it refers to the C library function.
504 if (!Context.getLangOptions().CPlusPlus &&
505 getDeclContext()->isTranslationUnit())
506 return BuiltinID;
507
508 // If the function is in an extern "C" linkage specification and is
509 // not marked "overloadable", it's the real function.
510 if (isa<LinkageSpecDecl>(getDeclContext()) &&
511 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
512 == LinkageSpecDecl::lang_c &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000513 !getAttr<OverloadableAttr>())
Douglas Gregorb5af7382009-02-14 18:57:46 +0000514 return BuiltinID;
515
516 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000517 return 0;
518}
519
520
Chris Lattnerd2112022009-04-25 06:03:53 +0000521/// getNumParams - Return the number of parameters this function must have
Chris Lattnerc13d4732009-04-25 06:12:16 +0000522/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattnerd2112022009-04-25 06:03:53 +0000523/// after it has been created.
524unsigned FunctionDecl::getNumParams() const {
Chris Lattner9d957cb2009-04-25 05:56:45 +0000525 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000526 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000527 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000528 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner9d957cb2009-04-25 05:56:45 +0000529
Chris Lattner4b009652007-07-25 00:24:17 +0000530}
531
Ted Kremenek8494c962009-01-14 00:42:25 +0000532void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
533 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000534 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattnerc13d4732009-04-25 06:12:16 +0000535 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000536
537 // Zero params -> null pointer.
538 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000539 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000540 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000541 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000542
Argiris Kirtzidisae78f1f2009-06-23 00:42:00 +0000543 // Update source range. The check below allows us to set EndRangeLoc before
544 // setting the parameters.
Argiris Kirtzidis0dba8f22009-06-23 00:42:15 +0000545 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000546 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner4b009652007-07-25 00:24:17 +0000547 }
548}
549
Chris Lattner97316c02008-04-10 02:22:51 +0000550/// getMinRequiredArguments - Returns the minimum number of arguments
551/// needed to call this function. This may be fewer than the number of
552/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000553/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000554unsigned FunctionDecl::getMinRequiredArguments() const {
555 unsigned NumRequiredArgs = getNumParams();
556 while (NumRequiredArgs > 0
Anders Carlssond2e57d92009-06-06 04:14:07 +0000557 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner97316c02008-04-10 02:22:51 +0000558 --NumRequiredArgs;
559
560 return NumRequiredArgs;
561}
562
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000563bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000564 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000565 return false;
566
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000567 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
568 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000569 return false;
Douglas Gregor67e11442009-04-28 06:37:30 +0000570
571 return true;
572}
573
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000574bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
575 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor67e11442009-04-28 06:37:30 +0000576 return false;
577
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000578 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
579 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000580 return true;
581
582 return false;
583}
584
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000585void
586FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argiris Kirtzidis4253ec52009-07-18 08:50:35 +0000587 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000588
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000589 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
590 FunctionTemplateDecl *PrevFunTmpl
591 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
592 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
593 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
594 }
595}
596
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000597FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000598 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000599}
600
Douglas Gregore60e5d32008-11-06 22:13:31 +0000601/// getOverloadedOperator - Which C++ overloaded operator this
602/// function represents, if any.
603OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000604 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
605 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000606 else
607 return OO_None;
608}
609
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000610FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
611 if (FunctionTemplateSpecializationInfo *Info
612 = TemplateOrSpecialization
613 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor69678062009-06-29 22:39:32 +0000614 return Info->Template.getPointer();
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000615 }
616 return 0;
617}
618
619const TemplateArgumentList *
620FunctionDecl::getTemplateSpecializationArgs() const {
621 if (FunctionTemplateSpecializationInfo *Info
622 = TemplateOrSpecialization
623 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
624 return Info->TemplateArguments;
625 }
626 return 0;
627}
628
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000629void
630FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
631 FunctionTemplateDecl *Template,
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000632 const TemplateArgumentList *TemplateArgs,
633 void *InsertPos) {
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000634 FunctionTemplateSpecializationInfo *Info
635 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000636 if (!Info)
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000637 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000638
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000639 Info->Function = this;
Douglas Gregor69678062009-06-29 22:39:32 +0000640 Info->Template.setPointer(Template);
641 Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000642 Info->TemplateArguments = TemplateArgs;
643 TemplateOrSpecialization = Info;
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000644
645 // Insert this function template specialization into the set of known
646 // function template specialiations.
647 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000648}
649
Douglas Gregor69678062009-06-29 22:39:32 +0000650bool FunctionDecl::isExplicitSpecialization() const {
651 // FIXME: check this property for explicit specializations of member
652 // functions of class templates.
653 FunctionTemplateSpecializationInfo *Info
654 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
655 if (!Info)
656 return false;
657
658 return Info->isExplicitSpecialization();
659}
660
661void FunctionDecl::setExplicitSpecialization(bool ES) {
662 // FIXME: set this property for explicit specializations of member functions
663 // of class templates.
664 FunctionTemplateSpecializationInfo *Info
665 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
666 if (Info)
667 Info->setExplicitSpecialization(ES);
668}
669
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000670//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000671// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000672//===----------------------------------------------------------------------===//
673
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000674SourceRange TagDecl::getSourceRange() const {
675 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000676 return SourceRange(TagKeywordLoc, E);
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000677}
678
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000679TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000680 return getFirstDeclaration();
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000681}
682
Douglas Gregor98b27542009-01-17 00:42:38 +0000683void TagDecl::startDefinition() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000684 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
685 TagT->decl.setPointer(this);
686 TagT->decl.setInt(1);
687 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000688}
689
690void TagDecl::completeDefinition() {
Douglas Gregor98b27542009-01-17 00:42:38 +0000691 IsDefinition = true;
Douglas Gregor19e567a2009-07-29 23:36:44 +0000692 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
693 assert(TagT->decl.getPointer() == this &&
694 "Attempt to redefine a tag definition?");
695 TagT->decl.setInt(0);
696 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000697}
698
Ted Kremenek46a837c2008-09-05 17:16:31 +0000699TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000700 if (isDefinition())
701 return const_cast<TagDecl *>(this);
702
Douglas Gregor19e567a2009-07-29 23:36:44 +0000703 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
704 R != REnd; ++R)
705 if (R->isDefinition())
706 return *R;
707
708 return 0;
Ted Kremenek46a837c2008-09-05 17:16:31 +0000709}
710
711//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000712// RecordDecl Implementation
713//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000714
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000715RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor19e567a2009-07-29 23:36:44 +0000716 IdentifierInfo *Id, RecordDecl *PrevDecl,
717 SourceLocation TKL)
718 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000719 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000720 AnonymousStructOrUnion = false;
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000721 HasObjectMember = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000722 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000723}
724
725RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000726 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000727 SourceLocation TKL, RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000728
Douglas Gregor19e567a2009-07-29 23:36:44 +0000729 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000730 C.getTypeDeclType(R, PrevDecl);
731 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000732}
733
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000734RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000735}
736
737void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000738 TagDecl::Destroy(C);
739}
740
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000741bool RecordDecl::isInjectedClassName() const {
742 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
743 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
744}
745
Douglas Gregor8acb7272008-12-11 16:49:14 +0000746/// completeDefinition - Notes that the definition of this type is now
747/// complete.
748void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000749 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000750 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000751}
752
Steve Naroff9ac456d2008-10-08 17:01:13 +0000753//===----------------------------------------------------------------------===//
754// BlockDecl Implementation
755//===----------------------------------------------------------------------===//
756
757BlockDecl::~BlockDecl() {
758}
759
760void BlockDecl::Destroy(ASTContext& C) {
761 if (Body)
762 Body->Destroy(C);
763
764 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
765 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000766
767 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000768 Decl::Destroy(C);
769}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000770
771void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
772 unsigned NParms) {
773 assert(ParamInfo == 0 && "Already has param info!");
774
775 // Zero params -> null pointer.
776 if (NParms) {
777 NumParams = NParms;
778 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
779 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
780 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
781 }
782}
783
784unsigned BlockDecl::getNumParams() const {
785 return NumParams;
786}