blob: b41fae86b92fb4893cc40aaf6bf1436b63b74743 [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,
Anders Carlssond9d6d502009-05-14 21:46:00 +0000143 bool hasWrittenPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +0000144 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000145 FunctionDecl *New
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000146 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline,
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000147 TypeSpecStartLoc);
Anders Carlssond9d6d502009-05-14 21:46:00 +0000148 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000149 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000150}
151
Steve Naroff52059382008-10-10 01:28:17 +0000152BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000153 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000154}
155
Douglas Gregor8acb7272008-12-11 16:49:14 +0000156FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000157 IdentifierInfo *Id, QualType T,
158 DeclaratorInfo *DInfo, Expr *BW,
Steve Naroffb79574e2009-07-14 14:58:18 +0000159 bool Mutable, SourceLocation TSSL) {
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000160 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable, TSSL);
Chris Lattner81db64a2008-03-16 00:16:02 +0000161}
162
Douglas Gregorc7f01612009-01-07 19:46:03 +0000163bool FieldDecl::isAnonymousStructOrUnion() const {
164 if (!isImplicit() || getDeclName())
165 return false;
166
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000167 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorc7f01612009-01-07 19:46:03 +0000168 return Record->getDecl()->isAnonymousStructOrUnion();
169
170 return false;
171}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000172
Chris Lattnereee57c02008-04-04 06:12:32 +0000173EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
174 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000175 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000176 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000177 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000178}
179
Ted Kremenek5be49242008-05-20 04:49:55 +0000180void EnumConstantDecl::Destroy(ASTContext& C) {
181 if (Init) Init->Destroy(C);
182 Decl::Destroy(C);
183}
184
Chris Lattneref87a202008-04-22 18:39:57 +0000185TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000186 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000187 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000188 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000189}
190
Chris Lattneref87a202008-04-22 18:39:57 +0000191EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000192 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorae644892008-12-15 16:32:14 +0000193 EnumDecl *PrevDecl) {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000194 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorae644892008-12-15 16:32:14 +0000195 C.getTypeDeclType(Enum, PrevDecl);
196 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000197}
198
Ted Kremenek25d8be12008-09-02 20:13:32 +0000199void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000200 Decl::Destroy(C);
201}
202
Douglas Gregor8acb7272008-12-11 16:49:14 +0000203void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
204 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000205 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000206 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000207}
208
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000209FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000210 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000211 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000212 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000213}
214
Chris Lattnere4650482008-03-15 06:12:44 +0000215//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000216// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000217//===----------------------------------------------------------------------===//
218
Douglas Gregor09be81b2009-02-04 17:27:36 +0000219std::string NamedDecl::getQualifiedNameAsString() const {
220 std::vector<std::string> Names;
221 std::string QualName;
222 const DeclContext *Ctx = getDeclContext();
223
224 if (Ctx->isFunctionOrMethod())
225 return getNameAsString();
226
227 while (Ctx) {
228 if (Ctx->isFunctionOrMethod())
229 // FIXME: That probably will happen, when D was member of local
230 // scope class/struct/union. How do we handle this case?
231 break;
232
Douglas Gregorb12249d2009-05-18 17:01:57 +0000233 if (const ClassTemplateSpecializationDecl *Spec
234 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
235 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Chris Lattner7099c782009-06-30 01:26:17 +0000236 PrintingPolicy Policy(getASTContext().getLangOptions());
Douglas Gregorb12249d2009-05-18 17:01:57 +0000237 std::string TemplateArgsStr
238 = TemplateSpecializationType::PrintTemplateArgumentList(
239 TemplateArgs.getFlatArgumentList(),
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000240 TemplateArgs.flat_size(),
241 Policy);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000242 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
243 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor09be81b2009-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
263
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000264bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000265 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
266
Douglas Gregor7a7be652009-02-03 19:21:40 +0000267 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
268 // We want to keep it, unless it nominates same namespace.
269 if (getKind() == Decl::UsingDirective) {
270 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
271 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
272 }
273
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000274 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
275 // For function declarations, we keep track of redeclarations.
276 return FD->getPreviousDeclaration() == OldD;
277
Douglas Gregorb60eb752009-06-25 22:08:12 +0000278 // For function templates, the underlying function declarations are linked.
279 if (const FunctionTemplateDecl *FunctionTemplate
280 = dyn_cast<FunctionTemplateDecl>(this))
281 if (const FunctionTemplateDecl *OldFunctionTemplate
282 = dyn_cast<FunctionTemplateDecl>(OldD))
283 return FunctionTemplate->getTemplatedDecl()
284 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
285
Steve Naroffd85ba922009-02-22 19:35:57 +0000286 // For method declarations, we keep track of redeclarations.
287 if (isa<ObjCMethodDecl>(this))
288 return false;
289
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000290 // For non-function declarations, if the declarations are of the
291 // same kind then this must be a redeclaration, or semantic analysis
292 // would not have given us the new declaration.
293 return this->getKind() == OldD->getKind();
294}
295
Douglas Gregor1c52c632009-02-24 20:03:32 +0000296bool NamedDecl::hasLinkage() const {
297 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
298 return VD->hasExternalStorage() || VD->isFileVarDecl();
299
300 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
301 return true;
302
303 return false;
304}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000305
Anders Carlssondf5ab842009-06-26 06:29:23 +0000306NamedDecl *NamedDecl::getUnderlyingDecl() {
307 NamedDecl *ND = this;
308 while (true) {
309 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
310 ND = UD->getTargetDecl();
311 else if (ObjCCompatibleAliasDecl *AD
312 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
313 return AD->getClassInterface();
314 else
315 return ND;
316 }
317}
318
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000319//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000320// VarDecl Implementation
321//===----------------------------------------------------------------------===//
322
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000323VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000324 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
325 StorageClass S, SourceLocation TypeSpecStartLoc) {
326 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000327}
328
329void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000330 Expr *Init = getInit();
Douglas Gregor4833ff02009-05-26 18:54:04 +0000331 if (Init) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000332 Init->Destroy(C);
Douglas Gregor4833ff02009-05-26 18:54:04 +0000333 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
334 Eval->~EvaluatedStmt();
335 C.Deallocate(Eval);
336 }
337 }
Nuno Lopesc98406e2008-12-17 23:39:55 +0000338 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000339 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000340}
341
342VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000343}
344
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000345SourceRange VarDecl::getSourceRange() const {
346 if (getInit())
347 return SourceRange(getLocation(), getInit()->getLocEnd());
348 return SourceRange(getLocation(), getLocation());
349}
350
Douglas Gregor181fe792009-07-24 20:34:43 +0000351VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
352 return getASTContext().getInstantiatedFromStaticDataMember(this);
353}
354
Douglas Gregor2f728b22009-03-10 23:43:53 +0000355bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
356 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
357 return false;
358
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000359 const VarDecl *Def = 0;
360 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000361 (getStorageClass() == None || getStorageClass() == Static));
362}
363
Ted Kremenek51787c72009-03-20 21:35:28 +0000364const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000365 redecl_iterator I = redecls_begin(), E = redecls_end();
366 while (I != E && !I->getInit())
367 ++I;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000368
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000369 if (I != E) {
370 Def = *I;
371 return I->getInit();
372 }
373 return 0;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000374}
375
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000376VarDecl *VarDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000377 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000378}
379
Nuno Lopesc98406e2008-12-17 23:39:55 +0000380//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000381// FunctionDecl Implementation
382//===----------------------------------------------------------------------===//
383
Ted Kremenekafdf8112008-05-20 00:43:19 +0000384void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000385 if (Body && Body.isOffset())
386 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000387
388 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
389 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000390
Steve Naroff5abb0282009-01-27 21:25:57 +0000391 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000392
Ted Kremenekafdf8112008-05-20 00:43:19 +0000393 Decl::Destroy(C);
394}
395
396
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000397Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000398 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
399 if (I->Body) {
400 Definition = *I;
401 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor42214c52008-04-21 02:02:58 +0000402 }
403 }
404
405 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000406}
407
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000408Stmt *FunctionDecl::getBodyIfAvailable() const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000409 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
410 if (I->Body && !I->Body.isOffset()) {
411 return I->Body.get(0);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000412 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000413 }
414
415 return 0;
416}
417
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000418void FunctionDecl::setBody(Stmt *B) {
419 Body = B;
Argiris Kirtzidis25822a02009-06-22 17:13:31 +0000420 if (B)
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000421 EndRangeLoc = B->getLocEnd();
422}
423
John McCallcb6dd8a2009-08-15 02:09:25 +0000424bool FunctionDecl::isMain(ASTContext &Context) const {
425 return !Context.getLangOptions().Freestanding &&
426 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregoraf682022009-02-24 01:23:02 +0000427 getIdentifier() && getIdentifier()->isStr("main");
428}
429
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000430bool FunctionDecl::isExternC(ASTContext &Context) const {
431 // In C, any non-static, non-overloadable function has external
432 // linkage.
433 if (!Context.getLangOptions().CPlusPlus)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000434 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000435
436 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
437 DC = DC->getParent()) {
438 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
439 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000440 return getStorageClass() != Static &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000441 !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000442
443 break;
444 }
445 }
446
447 return false;
448}
449
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000450bool FunctionDecl::isGlobal() const {
451 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
452 return Method->isStatic();
453
454 if (getStorageClass() == Static)
455 return false;
456
457 for (const DeclContext *DC = getDeclContext();
458 DC->isNamespace();
459 DC = DC->getParent()) {
460 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
461 if (!Namespace->getDeclName())
462 return false;
463 break;
464 }
465 }
466
467 return true;
468}
469
Douglas Gregor411889e2009-02-13 23:20:09 +0000470/// \brief Returns a value indicating whether this function
471/// corresponds to a builtin function.
472///
473/// The function corresponds to a built-in function if it is
474/// declared at translation scope or within an extern "C" block and
475/// its name matches with the name of a builtin. The returned value
476/// will be 0 for functions that do not correspond to a builtin, a
477/// value of type \c Builtin::ID if in the target-independent range
478/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000479unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
480 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
481 return 0;
482
483 unsigned BuiltinID = getIdentifier()->getBuiltinID();
484 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
485 return BuiltinID;
486
487 // This function has the name of a known C library
488 // function. Determine whether it actually refers to the C library
489 // function or whether it just has the same name.
490
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000491 // If this is a static function, it's not a builtin.
492 if (getStorageClass() == Static)
493 return 0;
494
Douglas Gregorb5af7382009-02-14 18:57:46 +0000495 // If this function is at translation-unit scope and we're not in
496 // C++, it refers to the C library function.
497 if (!Context.getLangOptions().CPlusPlus &&
498 getDeclContext()->isTranslationUnit())
499 return BuiltinID;
500
501 // If the function is in an extern "C" linkage specification and is
502 // not marked "overloadable", it's the real function.
503 if (isa<LinkageSpecDecl>(getDeclContext()) &&
504 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
505 == LinkageSpecDecl::lang_c &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000506 !getAttr<OverloadableAttr>())
Douglas Gregorb5af7382009-02-14 18:57:46 +0000507 return BuiltinID;
508
509 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000510 return 0;
511}
512
513
Chris Lattnerd2112022009-04-25 06:03:53 +0000514/// getNumParams - Return the number of parameters this function must have
Chris Lattnerc13d4732009-04-25 06:12:16 +0000515/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattnerd2112022009-04-25 06:03:53 +0000516/// after it has been created.
517unsigned FunctionDecl::getNumParams() const {
Chris Lattner9d957cb2009-04-25 05:56:45 +0000518 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000519 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000520 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000521 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner9d957cb2009-04-25 05:56:45 +0000522
Chris Lattner4b009652007-07-25 00:24:17 +0000523}
524
Ted Kremenek8494c962009-01-14 00:42:25 +0000525void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
526 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000527 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattnerc13d4732009-04-25 06:12:16 +0000528 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000529
530 // Zero params -> null pointer.
531 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000532 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000533 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000534 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000535
Argiris Kirtzidisae78f1f2009-06-23 00:42:00 +0000536 // Update source range. The check below allows us to set EndRangeLoc before
537 // setting the parameters.
Argiris Kirtzidis0dba8f22009-06-23 00:42:15 +0000538 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000539 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner4b009652007-07-25 00:24:17 +0000540 }
541}
542
Chris Lattner97316c02008-04-10 02:22:51 +0000543/// getMinRequiredArguments - Returns the minimum number of arguments
544/// needed to call this function. This may be fewer than the number of
545/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000546/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000547unsigned FunctionDecl::getMinRequiredArguments() const {
548 unsigned NumRequiredArgs = getNumParams();
549 while (NumRequiredArgs > 0
Anders Carlssond2e57d92009-06-06 04:14:07 +0000550 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner97316c02008-04-10 02:22:51 +0000551 --NumRequiredArgs;
552
553 return NumRequiredArgs;
554}
555
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000556bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000557 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000558 return false;
559
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000560 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
561 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000562 return false;
Douglas Gregor67e11442009-04-28 06:37:30 +0000563
564 return true;
565}
566
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000567bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
568 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor67e11442009-04-28 06:37:30 +0000569 return false;
570
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000571 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
572 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000573 return true;
574
575 return false;
576}
577
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000578void
579FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argiris Kirtzidis4253ec52009-07-18 08:50:35 +0000580 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000581
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000582 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
583 FunctionTemplateDecl *PrevFunTmpl
584 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
585 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
586 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
587 }
588}
589
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000590FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000591 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000592}
593
Douglas Gregore60e5d32008-11-06 22:13:31 +0000594/// getOverloadedOperator - Which C++ overloaded operator this
595/// function represents, if any.
596OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000597 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
598 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000599 else
600 return OO_None;
601}
602
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000603FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
604 if (FunctionTemplateSpecializationInfo *Info
605 = TemplateOrSpecialization
606 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor69678062009-06-29 22:39:32 +0000607 return Info->Template.getPointer();
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000608 }
609 return 0;
610}
611
612const TemplateArgumentList *
613FunctionDecl::getTemplateSpecializationArgs() const {
614 if (FunctionTemplateSpecializationInfo *Info
615 = TemplateOrSpecialization
616 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
617 return Info->TemplateArguments;
618 }
619 return 0;
620}
621
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000622void
623FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
624 FunctionTemplateDecl *Template,
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000625 const TemplateArgumentList *TemplateArgs,
626 void *InsertPos) {
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000627 FunctionTemplateSpecializationInfo *Info
628 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000629 if (!Info)
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000630 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000631
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000632 Info->Function = this;
Douglas Gregor69678062009-06-29 22:39:32 +0000633 Info->Template.setPointer(Template);
634 Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000635 Info->TemplateArguments = TemplateArgs;
636 TemplateOrSpecialization = Info;
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000637
638 // Insert this function template specialization into the set of known
639 // function template specialiations.
640 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000641}
642
Douglas Gregor69678062009-06-29 22:39:32 +0000643bool FunctionDecl::isExplicitSpecialization() const {
644 // FIXME: check this property for explicit specializations of member
645 // functions of class templates.
646 FunctionTemplateSpecializationInfo *Info
647 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
648 if (!Info)
649 return false;
650
651 return Info->isExplicitSpecialization();
652}
653
654void FunctionDecl::setExplicitSpecialization(bool ES) {
655 // FIXME: set this property for explicit specializations of member functions
656 // of class templates.
657 FunctionTemplateSpecializationInfo *Info
658 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
659 if (Info)
660 Info->setExplicitSpecialization(ES);
661}
662
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000663//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000664// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000665//===----------------------------------------------------------------------===//
666
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000667SourceRange TagDecl::getSourceRange() const {
668 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000669 return SourceRange(TagKeywordLoc, E);
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000670}
671
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000672TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000673 return getFirstDeclaration();
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000674}
675
Douglas Gregor98b27542009-01-17 00:42:38 +0000676void TagDecl::startDefinition() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000677 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
678 TagT->decl.setPointer(this);
679 TagT->decl.setInt(1);
680 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000681}
682
683void TagDecl::completeDefinition() {
Douglas Gregor98b27542009-01-17 00:42:38 +0000684 IsDefinition = true;
Douglas Gregor19e567a2009-07-29 23:36:44 +0000685 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
686 assert(TagT->decl.getPointer() == this &&
687 "Attempt to redefine a tag definition?");
688 TagT->decl.setInt(0);
689 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000690}
691
Ted Kremenek46a837c2008-09-05 17:16:31 +0000692TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000693 if (isDefinition())
694 return const_cast<TagDecl *>(this);
695
Douglas Gregor19e567a2009-07-29 23:36:44 +0000696 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
697 R != REnd; ++R)
698 if (R->isDefinition())
699 return *R;
700
701 return 0;
Ted Kremenek46a837c2008-09-05 17:16:31 +0000702}
703
704//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000705// RecordDecl Implementation
706//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000707
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000708RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor19e567a2009-07-29 23:36:44 +0000709 IdentifierInfo *Id, RecordDecl *PrevDecl,
710 SourceLocation TKL)
711 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000712 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000713 AnonymousStructOrUnion = false;
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000714 HasObjectMember = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000715 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000716}
717
718RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000719 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000720 SourceLocation TKL, RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000721
Douglas Gregor19e567a2009-07-29 23:36:44 +0000722 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000723 C.getTypeDeclType(R, PrevDecl);
724 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000725}
726
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000727RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000728}
729
730void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000731 TagDecl::Destroy(C);
732}
733
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000734bool RecordDecl::isInjectedClassName() const {
735 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
736 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
737}
738
Douglas Gregor8acb7272008-12-11 16:49:14 +0000739/// completeDefinition - Notes that the definition of this type is now
740/// complete.
741void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000742 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000743 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000744}
745
Steve Naroff9ac456d2008-10-08 17:01:13 +0000746//===----------------------------------------------------------------------===//
747// BlockDecl Implementation
748//===----------------------------------------------------------------------===//
749
750BlockDecl::~BlockDecl() {
751}
752
753void BlockDecl::Destroy(ASTContext& C) {
754 if (Body)
755 Body->Destroy(C);
756
757 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
758 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000759
760 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000761 Decl::Destroy(C);
762}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000763
764void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
765 unsigned NParms) {
766 assert(ParamInfo == 0 && "Already has param info!");
767
768 // Zero params -> null pointer.
769 if (NParms) {
770 NumParams = NParms;
771 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
772 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
773 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
774 }
775}
776
777unsigned BlockDecl::getNumParams() const {
778 return NumParams;
779}