blob: f5acb2c9aab8026b78a11af309838326c6a30577 [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,
87 QualType T, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000088 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000089 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, 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,
133 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000134 Expr *DefArg) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000135 return new (C) OriginalParmVarDecl(DC, L, Id, T, 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,
Douglas Gregor6704b312008-11-17 22:58:34 +0000140 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000141 StorageClass S, bool isInline,
Anders Carlssond9d6d502009-05-14 21:46:00 +0000142 bool hasWrittenPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +0000143 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000144 FunctionDecl *New
145 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
146 TypeSpecStartLoc);
Anders Carlssond9d6d502009-05-14 21:46:00 +0000147 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000148 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000149}
150
Steve Naroff52059382008-10-10 01:28:17 +0000151BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000152 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000153}
154
Douglas Gregor8acb7272008-12-11 16:49:14 +0000155FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
156 IdentifierInfo *Id, QualType T, Expr *BW,
Steve Naroffb79574e2009-07-14 14:58:18 +0000157 bool Mutable, SourceLocation TSSL) {
158 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, TSSL);
Chris Lattner81db64a2008-03-16 00:16:02 +0000159}
160
Douglas Gregorc7f01612009-01-07 19:46:03 +0000161bool FieldDecl::isAnonymousStructOrUnion() const {
162 if (!isImplicit() || getDeclName())
163 return false;
164
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000165 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorc7f01612009-01-07 19:46:03 +0000166 return Record->getDecl()->isAnonymousStructOrUnion();
167
168 return false;
169}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000170
Chris Lattnereee57c02008-04-04 06:12:32 +0000171EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
172 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000173 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000174 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000175 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000176}
177
Ted Kremenek5be49242008-05-20 04:49:55 +0000178void EnumConstantDecl::Destroy(ASTContext& C) {
179 if (Init) Init->Destroy(C);
180 Decl::Destroy(C);
181}
182
Chris Lattneref87a202008-04-22 18:39:57 +0000183TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000184 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000185 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000186 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000187}
188
Chris Lattneref87a202008-04-22 18:39:57 +0000189EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000190 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorae644892008-12-15 16:32:14 +0000191 EnumDecl *PrevDecl) {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000192 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorae644892008-12-15 16:32:14 +0000193 C.getTypeDeclType(Enum, PrevDecl);
194 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000195}
196
Ted Kremenek25d8be12008-09-02 20:13:32 +0000197void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000198 Decl::Destroy(C);
199}
200
Douglas Gregor8acb7272008-12-11 16:49:14 +0000201void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
202 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000203 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000204 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000205}
206
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000207FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000208 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000209 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000210 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000211}
212
Chris Lattnere4650482008-03-15 06:12:44 +0000213//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000214// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000215//===----------------------------------------------------------------------===//
216
Douglas Gregor09be81b2009-02-04 17:27:36 +0000217std::string NamedDecl::getQualifiedNameAsString() const {
218 std::vector<std::string> Names;
219 std::string QualName;
220 const DeclContext *Ctx = getDeclContext();
221
222 if (Ctx->isFunctionOrMethod())
223 return getNameAsString();
224
225 while (Ctx) {
226 if (Ctx->isFunctionOrMethod())
227 // FIXME: That probably will happen, when D was member of local
228 // scope class/struct/union. How do we handle this case?
229 break;
230
Douglas Gregorb12249d2009-05-18 17:01:57 +0000231 if (const ClassTemplateSpecializationDecl *Spec
232 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
233 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Chris Lattner7099c782009-06-30 01:26:17 +0000234 PrintingPolicy Policy(getASTContext().getLangOptions());
Douglas Gregorb12249d2009-05-18 17:01:57 +0000235 std::string TemplateArgsStr
236 = TemplateSpecializationType::PrintTemplateArgumentList(
237 TemplateArgs.getFlatArgumentList(),
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000238 TemplateArgs.flat_size(),
239 Policy);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000240 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
241 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor09be81b2009-02-04 17:27:36 +0000242 Names.push_back(ND->getNameAsString());
243 else
244 break;
245
246 Ctx = Ctx->getParent();
247 }
248
249 std::vector<std::string>::reverse_iterator
250 I = Names.rbegin(),
251 End = Names.rend();
252
253 for (; I!=End; ++I)
254 QualName += *I + "::";
255
256 QualName += getNameAsString();
257
258 return QualName;
259}
260
261
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000262bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000263 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
264
Douglas Gregor7a7be652009-02-03 19:21:40 +0000265 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
266 // We want to keep it, unless it nominates same namespace.
267 if (getKind() == Decl::UsingDirective) {
268 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
269 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
270 }
271
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000272 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
273 // For function declarations, we keep track of redeclarations.
274 return FD->getPreviousDeclaration() == OldD;
275
Douglas Gregorb60eb752009-06-25 22:08:12 +0000276 // For function templates, the underlying function declarations are linked.
277 if (const FunctionTemplateDecl *FunctionTemplate
278 = dyn_cast<FunctionTemplateDecl>(this))
279 if (const FunctionTemplateDecl *OldFunctionTemplate
280 = dyn_cast<FunctionTemplateDecl>(OldD))
281 return FunctionTemplate->getTemplatedDecl()
282 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
283
Steve Naroffd85ba922009-02-22 19:35:57 +0000284 // For method declarations, we keep track of redeclarations.
285 if (isa<ObjCMethodDecl>(this))
286 return false;
287
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000288 // For non-function declarations, if the declarations are of the
289 // same kind then this must be a redeclaration, or semantic analysis
290 // would not have given us the new declaration.
291 return this->getKind() == OldD->getKind();
292}
293
Douglas Gregor1c52c632009-02-24 20:03:32 +0000294bool NamedDecl::hasLinkage() const {
295 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
296 return VD->hasExternalStorage() || VD->isFileVarDecl();
297
298 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
299 return true;
300
301 return false;
302}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000303
Anders Carlssondf5ab842009-06-26 06:29:23 +0000304NamedDecl *NamedDecl::getUnderlyingDecl() {
305 NamedDecl *ND = this;
306 while (true) {
307 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
308 ND = UD->getTargetDecl();
309 else if (ObjCCompatibleAliasDecl *AD
310 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
311 return AD->getClassInterface();
312 else
313 return ND;
314 }
315}
316
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000317//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000318// VarDecl Implementation
319//===----------------------------------------------------------------------===//
320
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000321VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
322 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000323 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000324 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000325}
326
327void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000328 Expr *Init = getInit();
Douglas Gregor4833ff02009-05-26 18:54:04 +0000329 if (Init) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000330 Init->Destroy(C);
Douglas Gregor4833ff02009-05-26 18:54:04 +0000331 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
332 Eval->~EvaluatedStmt();
333 C.Deallocate(Eval);
334 }
335 }
Nuno Lopesc98406e2008-12-17 23:39:55 +0000336 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000337 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000338}
339
340VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000341}
342
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000343SourceRange VarDecl::getSourceRange() const {
344 if (getInit())
345 return SourceRange(getLocation(), getInit()->getLocEnd());
346 return SourceRange(getLocation(), getLocation());
347}
348
Douglas Gregor181fe792009-07-24 20:34:43 +0000349VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
350 return getASTContext().getInstantiatedFromStaticDataMember(this);
351}
352
Douglas Gregor2f728b22009-03-10 23:43:53 +0000353bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
354 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
355 return false;
356
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000357 const VarDecl *Def = 0;
358 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000359 (getStorageClass() == None || getStorageClass() == Static));
360}
361
Ted Kremenek51787c72009-03-20 21:35:28 +0000362const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000363 redecl_iterator I = redecls_begin(), E = redecls_end();
364 while (I != E && !I->getInit())
365 ++I;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000366
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000367 if (I != E) {
368 Def = *I;
369 return I->getInit();
370 }
371 return 0;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000372}
373
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000374VarDecl *VarDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000375 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000376}
377
Nuno Lopesc98406e2008-12-17 23:39:55 +0000378//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000379// FunctionDecl Implementation
380//===----------------------------------------------------------------------===//
381
Ted Kremenekafdf8112008-05-20 00:43:19 +0000382void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000383 if (Body && Body.isOffset())
384 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000385
386 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
387 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000388
Steve Naroff5abb0282009-01-27 21:25:57 +0000389 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000390
Ted Kremenekafdf8112008-05-20 00:43:19 +0000391 Decl::Destroy(C);
392}
393
394
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000395Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000396 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
397 if (I->Body) {
398 Definition = *I;
399 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor42214c52008-04-21 02:02:58 +0000400 }
401 }
402
403 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000404}
405
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000406Stmt *FunctionDecl::getBodyIfAvailable() const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000407 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
408 if (I->Body && !I->Body.isOffset()) {
409 return I->Body.get(0);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000410 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000411 }
412
413 return 0;
414}
415
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000416void FunctionDecl::setBody(Stmt *B) {
417 Body = B;
Argiris Kirtzidis25822a02009-06-22 17:13:31 +0000418 if (B)
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000419 EndRangeLoc = B->getLocEnd();
420}
421
John McCallcb6dd8a2009-08-15 02:09:25 +0000422bool FunctionDecl::isMain(ASTContext &Context) const {
423 return !Context.getLangOptions().Freestanding &&
424 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregoraf682022009-02-24 01:23:02 +0000425 getIdentifier() && getIdentifier()->isStr("main");
426}
427
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000428bool FunctionDecl::isExternC(ASTContext &Context) const {
429 // In C, any non-static, non-overloadable function has external
430 // linkage.
431 if (!Context.getLangOptions().CPlusPlus)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000432 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000433
434 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
435 DC = DC->getParent()) {
436 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
437 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000438 return getStorageClass() != Static &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000439 !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000440
441 break;
442 }
443 }
444
445 return false;
446}
447
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000448bool FunctionDecl::isGlobal() const {
449 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
450 return Method->isStatic();
451
452 if (getStorageClass() == Static)
453 return false;
454
455 for (const DeclContext *DC = getDeclContext();
456 DC->isNamespace();
457 DC = DC->getParent()) {
458 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
459 if (!Namespace->getDeclName())
460 return false;
461 break;
462 }
463 }
464
465 return true;
466}
467
Douglas Gregor411889e2009-02-13 23:20:09 +0000468/// \brief Returns a value indicating whether this function
469/// corresponds to a builtin function.
470///
471/// The function corresponds to a built-in function if it is
472/// declared at translation scope or within an extern "C" block and
473/// its name matches with the name of a builtin. The returned value
474/// will be 0 for functions that do not correspond to a builtin, a
475/// value of type \c Builtin::ID if in the target-independent range
476/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000477unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
478 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
479 return 0;
480
481 unsigned BuiltinID = getIdentifier()->getBuiltinID();
482 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
483 return BuiltinID;
484
485 // This function has the name of a known C library
486 // function. Determine whether it actually refers to the C library
487 // function or whether it just has the same name.
488
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000489 // If this is a static function, it's not a builtin.
490 if (getStorageClass() == Static)
491 return 0;
492
Douglas Gregorb5af7382009-02-14 18:57:46 +0000493 // If this function is at translation-unit scope and we're not in
494 // C++, it refers to the C library function.
495 if (!Context.getLangOptions().CPlusPlus &&
496 getDeclContext()->isTranslationUnit())
497 return BuiltinID;
498
499 // If the function is in an extern "C" linkage specification and is
500 // not marked "overloadable", it's the real function.
501 if (isa<LinkageSpecDecl>(getDeclContext()) &&
502 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
503 == LinkageSpecDecl::lang_c &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000504 !getAttr<OverloadableAttr>())
Douglas Gregorb5af7382009-02-14 18:57:46 +0000505 return BuiltinID;
506
507 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000508 return 0;
509}
510
511
Chris Lattnerd2112022009-04-25 06:03:53 +0000512/// getNumParams - Return the number of parameters this function must have
Chris Lattnerc13d4732009-04-25 06:12:16 +0000513/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattnerd2112022009-04-25 06:03:53 +0000514/// after it has been created.
515unsigned FunctionDecl::getNumParams() const {
Chris Lattner9d957cb2009-04-25 05:56:45 +0000516 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000517 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000518 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000519 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner9d957cb2009-04-25 05:56:45 +0000520
Chris Lattner4b009652007-07-25 00:24:17 +0000521}
522
Ted Kremenek8494c962009-01-14 00:42:25 +0000523void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
524 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000525 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattnerc13d4732009-04-25 06:12:16 +0000526 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000527
528 // Zero params -> null pointer.
529 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000530 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000531 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000532 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000533
Argiris Kirtzidisae78f1f2009-06-23 00:42:00 +0000534 // Update source range. The check below allows us to set EndRangeLoc before
535 // setting the parameters.
Argiris Kirtzidis0dba8f22009-06-23 00:42:15 +0000536 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000537 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner4b009652007-07-25 00:24:17 +0000538 }
539}
540
Chris Lattner97316c02008-04-10 02:22:51 +0000541/// getMinRequiredArguments - Returns the minimum number of arguments
542/// needed to call this function. This may be fewer than the number of
543/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000544/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000545unsigned FunctionDecl::getMinRequiredArguments() const {
546 unsigned NumRequiredArgs = getNumParams();
547 while (NumRequiredArgs > 0
Anders Carlssond2e57d92009-06-06 04:14:07 +0000548 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner97316c02008-04-10 02:22:51 +0000549 --NumRequiredArgs;
550
551 return NumRequiredArgs;
552}
553
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000554bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000555 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000556 return false;
557
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000558 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
559 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000560 return false;
Douglas Gregor67e11442009-04-28 06:37:30 +0000561
562 return true;
563}
564
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000565bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
566 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor67e11442009-04-28 06:37:30 +0000567 return false;
568
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000569 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
570 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000571 return true;
572
573 return false;
574}
575
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000576void
577FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argiris Kirtzidis4253ec52009-07-18 08:50:35 +0000578 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000579
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000580 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
581 FunctionTemplateDecl *PrevFunTmpl
582 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
583 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
584 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
585 }
586}
587
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000588FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000589 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000590}
591
Douglas Gregore60e5d32008-11-06 22:13:31 +0000592/// getOverloadedOperator - Which C++ overloaded operator this
593/// function represents, if any.
594OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000595 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
596 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000597 else
598 return OO_None;
599}
600
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000601FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
602 if (FunctionTemplateSpecializationInfo *Info
603 = TemplateOrSpecialization
604 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor69678062009-06-29 22:39:32 +0000605 return Info->Template.getPointer();
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000606 }
607 return 0;
608}
609
610const TemplateArgumentList *
611FunctionDecl::getTemplateSpecializationArgs() const {
612 if (FunctionTemplateSpecializationInfo *Info
613 = TemplateOrSpecialization
614 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
615 return Info->TemplateArguments;
616 }
617 return 0;
618}
619
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000620void
621FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
622 FunctionTemplateDecl *Template,
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000623 const TemplateArgumentList *TemplateArgs,
624 void *InsertPos) {
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000625 FunctionTemplateSpecializationInfo *Info
626 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000627 if (!Info)
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000628 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000629
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000630 Info->Function = this;
Douglas Gregor69678062009-06-29 22:39:32 +0000631 Info->Template.setPointer(Template);
632 Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000633 Info->TemplateArguments = TemplateArgs;
634 TemplateOrSpecialization = Info;
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000635
636 // Insert this function template specialization into the set of known
637 // function template specialiations.
638 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000639}
640
Douglas Gregor69678062009-06-29 22:39:32 +0000641bool FunctionDecl::isExplicitSpecialization() const {
642 // FIXME: check this property for explicit specializations of member
643 // functions of class templates.
644 FunctionTemplateSpecializationInfo *Info
645 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
646 if (!Info)
647 return false;
648
649 return Info->isExplicitSpecialization();
650}
651
652void FunctionDecl::setExplicitSpecialization(bool ES) {
653 // FIXME: set this property for explicit specializations of member functions
654 // of class templates.
655 FunctionTemplateSpecializationInfo *Info
656 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
657 if (Info)
658 Info->setExplicitSpecialization(ES);
659}
660
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000661//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000662// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000663//===----------------------------------------------------------------------===//
664
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000665SourceRange TagDecl::getSourceRange() const {
666 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000667 return SourceRange(TagKeywordLoc, E);
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000668}
669
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000670TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000671 return getFirstDeclaration();
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000672}
673
Douglas Gregor98b27542009-01-17 00:42:38 +0000674void TagDecl::startDefinition() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000675 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
676 TagT->decl.setPointer(this);
677 TagT->decl.setInt(1);
678 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000679}
680
681void TagDecl::completeDefinition() {
Douglas Gregor98b27542009-01-17 00:42:38 +0000682 IsDefinition = true;
Douglas Gregor19e567a2009-07-29 23:36:44 +0000683 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
684 assert(TagT->decl.getPointer() == this &&
685 "Attempt to redefine a tag definition?");
686 TagT->decl.setInt(0);
687 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000688}
689
Ted Kremenek46a837c2008-09-05 17:16:31 +0000690TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000691 if (isDefinition())
692 return const_cast<TagDecl *>(this);
693
Douglas Gregor19e567a2009-07-29 23:36:44 +0000694 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
695 R != REnd; ++R)
696 if (R->isDefinition())
697 return *R;
698
699 return 0;
Ted Kremenek46a837c2008-09-05 17:16:31 +0000700}
701
702//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000703// RecordDecl Implementation
704//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000705
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000706RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor19e567a2009-07-29 23:36:44 +0000707 IdentifierInfo *Id, RecordDecl *PrevDecl,
708 SourceLocation TKL)
709 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000710 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000711 AnonymousStructOrUnion = false;
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000712 HasObjectMember = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000713 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000714}
715
716RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000717 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000718 SourceLocation TKL, RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000719
Douglas Gregor19e567a2009-07-29 23:36:44 +0000720 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000721 C.getTypeDeclType(R, PrevDecl);
722 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000723}
724
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000725RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000726}
727
728void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000729 TagDecl::Destroy(C);
730}
731
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000732bool RecordDecl::isInjectedClassName() const {
733 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
734 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
735}
736
Douglas Gregor8acb7272008-12-11 16:49:14 +0000737/// completeDefinition - Notes that the definition of this type is now
738/// complete.
739void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000740 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000741 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000742}
743
Steve Naroff9ac456d2008-10-08 17:01:13 +0000744//===----------------------------------------------------------------------===//
745// BlockDecl Implementation
746//===----------------------------------------------------------------------===//
747
748BlockDecl::~BlockDecl() {
749}
750
751void BlockDecl::Destroy(ASTContext& C) {
752 if (Body)
753 Body->Destroy(C);
754
755 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
756 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000757
758 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000759 Decl::Destroy(C);
760}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000761
762void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
763 unsigned NParms) {
764 assert(ParamInfo == 0 && "Already has param info!");
765
766 // Zero params -> null pointer.
767 if (NParms) {
768 NumParams = NParms;
769 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
770 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
771 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
772 }
773}
774
775unsigned BlockDecl::getNumParams() const {
776 return NumParams;
777}