blob: 6d2f2301ba40d10696ab1d7b38ce6fd70f747947 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000020#include "clang/AST/Expr.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000021#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000023#include "clang/Basic/IdentifierTable.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000024#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Chris Lattner0b2b6e12009-03-04 06:34:08 +000028void Attr::Destroy(ASTContext &C) {
29 if (Next) {
30 Next->Destroy(C);
31 Next = 0;
32 }
33 this->~Attr();
34 C.Deallocate((void*)this);
35}
36
37
Chris Lattnerd3b90652008-03-15 05:43:15 +000038//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000039// Decl Allocation/Deallocation Method Implementations
40//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000041
Chris Lattner0b2b6e12009-03-04 06:34:08 +000042
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000043TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +000044 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000045}
46
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000047NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
48 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000049 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000050}
51
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000052void NamespaceDecl::Destroy(ASTContext& C) {
53 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
54 // together. They are all top-level Decls.
55
Ted Kremenekebf27b12008-05-24 15:09:56 +000056 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000057 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000058}
59
60
Chris Lattner41110242008-06-17 18:05:57 +000061ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000062 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000063 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000064}
65
Daniel Dunbarb286a782009-04-14 02:08:49 +000066const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
67 switch (SC) {
68 case VarDecl::None: break;
69 case VarDecl::Auto: return "auto"; break;
70 case VarDecl::Extern: return "extern"; break;
71 case VarDecl::PrivateExtern: return "__private_extern__"; break;
72 case VarDecl::Register: return "register"; break;
73 case VarDecl::Static: return "static"; break;
74 }
75
76 assert(0 && "Invalid storage class");
77 return 0;
78}
79
Chris Lattner9fdf9c62008-04-22 18:39:57 +000080ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000081 SourceLocation L, IdentifierInfo *Id,
82 QualType T, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000083 Expr *DefArg) {
Steve Naroff3e970492009-01-27 21:25:57 +000084 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000085}
86
87QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor64650af2009-02-02 23:39:07 +000088 if (const OriginalParmVarDecl *PVD =
89 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000090 return PVD->OriginalType;
91 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000092}
93
Douglas Gregor78d15832009-05-26 18:54:04 +000094void VarDecl::setInit(ASTContext &C, Expr *I) {
95 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
96 Eval->~EvaluatedStmt();
97 C.Deallocate(Eval);
98 }
99
100 Init = I;
101 }
102
Douglas Gregor63935192009-03-02 00:19:53 +0000103bool VarDecl::isExternC(ASTContext &Context) const {
104 if (!Context.getLangOptions().CPlusPlus)
105 return (getDeclContext()->isTranslationUnit() &&
106 getStorageClass() != Static) ||
107 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
108
109 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
110 DC = DC->getParent()) {
111 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
112 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
113 return getStorageClass() != Static;
114
115 break;
116 }
117
118 if (DC->isFunctionOrMethod())
119 return false;
120 }
121
122 return false;
123}
124
Douglas Gregor64650af2009-02-02 23:39:07 +0000125OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000126 ASTContext &C, DeclContext *DC,
127 SourceLocation L, IdentifierInfo *Id,
128 QualType T, QualType OT, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000129 Expr *DefArg) {
Douglas Gregor64650af2009-02-02 23:39:07 +0000130 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000131}
132
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000133FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000134 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000135 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000136 StorageClass S, bool isInline,
Anders Carlssona75e8532009-05-14 21:46:00 +0000137 bool hasWrittenPrototype,
Steve Naroff0eb07bf2008-10-03 00:02:03 +0000138 SourceLocation TypeSpecStartLoc) {
Douglas Gregor2224f842009-02-25 16:33:18 +0000139 FunctionDecl *New
140 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
141 TypeSpecStartLoc);
Anders Carlssona75e8532009-05-14 21:46:00 +0000142 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor2224f842009-02-25 16:33:18 +0000143 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000144}
145
Steve Naroff090276f2008-10-10 01:28:17 +0000146BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000147 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000148}
149
Douglas Gregor44b43212008-12-11 16:49:14 +0000150FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
151 IdentifierInfo *Id, QualType T, Expr *BW,
Steve Naroffea218b82009-07-14 14:58:18 +0000152 bool Mutable, SourceLocation TSSL) {
153 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, TSSL);
Chris Lattner8e25d862008-03-16 00:16:02 +0000154}
155
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000156bool FieldDecl::isAnonymousStructOrUnion() const {
157 if (!isImplicit() || getDeclName())
158 return false;
159
Ted Kremenek6217b802009-07-29 21:53:49 +0000160 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000161 return Record->getDecl()->isAnonymousStructOrUnion();
162
163 return false;
164}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000165
Chris Lattner0ed844b2008-04-04 06:12:32 +0000166EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
167 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000168 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000169 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000170 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000171}
172
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000173void EnumConstantDecl::Destroy(ASTContext& C) {
174 if (Init) Init->Destroy(C);
175 Decl::Destroy(C);
176}
177
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000178TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000179 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000180 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000181 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000182}
183
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000184EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000185 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000186 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000187 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000188 C.getTypeDeclType(Enum, PrevDecl);
189 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000190}
191
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000192void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000193 Decl::Destroy(C);
194}
195
Douglas Gregor44b43212008-12-11 16:49:14 +0000196void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
197 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000198 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000199 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000200}
201
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000202FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000203 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000204 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000205 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000206}
207
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000208//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000209// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000210//===----------------------------------------------------------------------===//
211
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000212std::string NamedDecl::getQualifiedNameAsString() const {
213 std::vector<std::string> Names;
214 std::string QualName;
215 const DeclContext *Ctx = getDeclContext();
216
217 if (Ctx->isFunctionOrMethod())
218 return getNameAsString();
219
220 while (Ctx) {
221 if (Ctx->isFunctionOrMethod())
222 // FIXME: That probably will happen, when D was member of local
223 // scope class/struct/union. How do we handle this case?
224 break;
225
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000226 if (const ClassTemplateSpecializationDecl *Spec
227 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
228 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Chris Lattnere4f21422009-06-30 01:26:17 +0000229 PrintingPolicy Policy(getASTContext().getLangOptions());
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000230 std::string TemplateArgsStr
231 = TemplateSpecializationType::PrintTemplateArgumentList(
232 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000233 TemplateArgs.flat_size(),
234 Policy);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000235 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
236 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000237 Names.push_back(ND->getNameAsString());
238 else
239 break;
240
241 Ctx = Ctx->getParent();
242 }
243
244 std::vector<std::string>::reverse_iterator
245 I = Names.rbegin(),
246 End = Names.rend();
247
248 for (; I!=End; ++I)
249 QualName += *I + "::";
250
251 QualName += getNameAsString();
252
253 return QualName;
254}
255
256
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000257bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000258 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
259
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000260 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
261 // We want to keep it, unless it nominates same namespace.
262 if (getKind() == Decl::UsingDirective) {
263 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
264 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
265 }
266
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000267 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
268 // For function declarations, we keep track of redeclarations.
269 return FD->getPreviousDeclaration() == OldD;
270
Douglas Gregore53060f2009-06-25 22:08:12 +0000271 // For function templates, the underlying function declarations are linked.
272 if (const FunctionTemplateDecl *FunctionTemplate
273 = dyn_cast<FunctionTemplateDecl>(this))
274 if (const FunctionTemplateDecl *OldFunctionTemplate
275 = dyn_cast<FunctionTemplateDecl>(OldD))
276 return FunctionTemplate->getTemplatedDecl()
277 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
278
Steve Naroff0de21fd2009-02-22 19:35:57 +0000279 // For method declarations, we keep track of redeclarations.
280 if (isa<ObjCMethodDecl>(this))
281 return false;
282
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000283 // For non-function declarations, if the declarations are of the
284 // same kind then this must be a redeclaration, or semantic analysis
285 // would not have given us the new declaration.
286 return this->getKind() == OldD->getKind();
287}
288
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000289bool NamedDecl::hasLinkage() const {
290 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
291 return VD->hasExternalStorage() || VD->isFileVarDecl();
292
293 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
294 return true;
295
296 return false;
297}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000298
Anders Carlssone136e0e2009-06-26 06:29:23 +0000299NamedDecl *NamedDecl::getUnderlyingDecl() {
300 NamedDecl *ND = this;
301 while (true) {
302 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
303 ND = UD->getTargetDecl();
304 else if (ObjCCompatibleAliasDecl *AD
305 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
306 return AD->getClassInterface();
307 else
308 return ND;
309 }
310}
311
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000312//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000313// VarDecl Implementation
314//===----------------------------------------------------------------------===//
315
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000316VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
317 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000318 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000319 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000320}
321
322void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000323 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000324 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000325 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000326 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
327 Eval->~EvaluatedStmt();
328 C.Deallocate(Eval);
329 }
330 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000331 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000332 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000333}
334
335VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000336}
337
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000338SourceRange VarDecl::getSourceRange() const {
339 if (getInit())
340 return SourceRange(getLocation(), getInit()->getLocEnd());
341 return SourceRange(getLocation(), getLocation());
342}
343
Douglas Gregor7caa6822009-07-24 20:34:43 +0000344VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
345 return getASTContext().getInstantiatedFromStaticDataMember(this);
346}
347
Douglas Gregor275a3692009-03-10 23:43:53 +0000348bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
349 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
350 return false;
351
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000352 const VarDecl *Def = 0;
353 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000354 (getStorageClass() == None || getStorageClass() == Static));
355}
356
Ted Kremenek082d9362009-03-20 21:35:28 +0000357const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000358 redecl_iterator I = redecls_begin(), E = redecls_end();
359 while (I != E && !I->getInit())
360 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000361
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000362 if (I != E) {
363 Def = *I;
364 return I->getInit();
365 }
366 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000367}
368
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000369VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000370 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000371}
372
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000373//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000374// FunctionDecl Implementation
375//===----------------------------------------------------------------------===//
376
Ted Kremenek27f8a282008-05-20 00:43:19 +0000377void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000378 if (Body && Body.isOffset())
379 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000380
381 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
382 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000383
Steve Naroff3e970492009-01-27 21:25:57 +0000384 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000385
Ted Kremenek27f8a282008-05-20 00:43:19 +0000386 Decl::Destroy(C);
387}
388
389
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000390Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000391 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
392 if (I->Body) {
393 Definition = *I;
394 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000395 }
396 }
397
398 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
400
Sebastian Redld3a413d2009-04-26 20:35:05 +0000401Stmt *FunctionDecl::getBodyIfAvailable() const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000402 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
403 if (I->Body && !I->Body.isOffset()) {
404 return I->Body.get(0);
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000405 }
Douglas Gregor72971342009-04-18 00:02:19 +0000406 }
407
408 return 0;
409}
410
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000411void FunctionDecl::setBody(Stmt *B) {
412 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000413 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000414 EndRangeLoc = B->getLocEnd();
415}
416
Douglas Gregor04495c82009-02-24 01:23:02 +0000417bool FunctionDecl::isMain() const {
418 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
419 getIdentifier() && getIdentifier()->isStr("main");
420}
421
Douglas Gregor63935192009-03-02 00:19:53 +0000422bool FunctionDecl::isExternC(ASTContext &Context) const {
423 // In C, any non-static, non-overloadable function has external
424 // linkage.
425 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000426 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000427
428 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
429 DC = DC->getParent()) {
430 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
431 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000432 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000433 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000434
435 break;
436 }
437 }
438
439 return false;
440}
441
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000442bool FunctionDecl::isGlobal() const {
443 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
444 return Method->isStatic();
445
446 if (getStorageClass() == Static)
447 return false;
448
449 for (const DeclContext *DC = getDeclContext();
450 DC->isNamespace();
451 DC = DC->getParent()) {
452 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
453 if (!Namespace->getDeclName())
454 return false;
455 break;
456 }
457 }
458
459 return true;
460}
461
Douglas Gregor3e41d602009-02-13 23:20:09 +0000462/// \brief Returns a value indicating whether this function
463/// corresponds to a builtin function.
464///
465/// The function corresponds to a built-in function if it is
466/// declared at translation scope or within an extern "C" block and
467/// its name matches with the name of a builtin. The returned value
468/// will be 0 for functions that do not correspond to a builtin, a
469/// value of type \c Builtin::ID if in the target-independent range
470/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000471unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
472 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
473 return 0;
474
475 unsigned BuiltinID = getIdentifier()->getBuiltinID();
476 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
477 return BuiltinID;
478
479 // This function has the name of a known C library
480 // function. Determine whether it actually refers to the C library
481 // function or whether it just has the same name.
482
Douglas Gregor9add3172009-02-17 03:23:10 +0000483 // If this is a static function, it's not a builtin.
484 if (getStorageClass() == Static)
485 return 0;
486
Douglas Gregor3c385e52009-02-14 18:57:46 +0000487 // If this function is at translation-unit scope and we're not in
488 // C++, it refers to the C library function.
489 if (!Context.getLangOptions().CPlusPlus &&
490 getDeclContext()->isTranslationUnit())
491 return BuiltinID;
492
493 // If the function is in an extern "C" linkage specification and is
494 // not marked "overloadable", it's the real function.
495 if (isa<LinkageSpecDecl>(getDeclContext()) &&
496 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
497 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000498 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000499 return BuiltinID;
500
501 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000502 return 0;
503}
504
505
Chris Lattner1ad9b282009-04-25 06:03:53 +0000506/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000507/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000508/// after it has been created.
509unsigned FunctionDecl::getNumParams() const {
Chris Lattner11ddb7d2009-04-25 05:56:45 +0000510 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000511 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000512 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000513 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner11ddb7d2009-04-25 05:56:45 +0000514
Reid Spencer5f016e22007-07-11 17:01:13 +0000515}
516
Ted Kremenekfc767612009-01-14 00:42:25 +0000517void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
518 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000520 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000521
522 // Zero params -> null pointer.
523 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000524 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000525 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000527
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000528 // Update source range. The check below allows us to set EndRangeLoc before
529 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000530 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000531 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 }
533}
534
Chris Lattner8123a952008-04-10 02:22:51 +0000535/// getMinRequiredArguments - Returns the minimum number of arguments
536/// needed to call this function. This may be fewer than the number of
537/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000538/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000539unsigned FunctionDecl::getMinRequiredArguments() const {
540 unsigned NumRequiredArgs = getNumParams();
541 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000542 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000543 --NumRequiredArgs;
544
545 return NumRequiredArgs;
546}
547
Douglas Gregor68584ed2009-06-18 16:11:24 +0000548bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000549 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000550 return false;
551
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000552 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
553 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000554 return false;
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000555
556 return true;
557}
558
Douglas Gregor68584ed2009-06-18 16:11:24 +0000559bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
560 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000561 return false;
562
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000563 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
564 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000565 return true;
566
567 return false;
568}
569
Douglas Gregor127102b2009-06-29 20:59:39 +0000570void
571FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000572 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000573
Douglas Gregor127102b2009-06-29 20:59:39 +0000574 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
575 FunctionTemplateDecl *PrevFunTmpl
576 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
577 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
578 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
579 }
580}
581
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000582FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000583 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000584}
585
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000586/// getOverloadedOperator - Which C++ overloaded operator this
587/// function represents, if any.
588OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000589 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
590 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000591 else
592 return OO_None;
593}
594
Douglas Gregor16e8be22009-06-29 17:30:29 +0000595FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
596 if (FunctionTemplateSpecializationInfo *Info
597 = TemplateOrSpecialization
598 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000599 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000600 }
601 return 0;
602}
603
604const TemplateArgumentList *
605FunctionDecl::getTemplateSpecializationArgs() const {
606 if (FunctionTemplateSpecializationInfo *Info
607 = TemplateOrSpecialization
608 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
609 return Info->TemplateArguments;
610 }
611 return 0;
612}
613
Douglas Gregor1637be72009-06-26 00:10:03 +0000614void
615FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
616 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000617 const TemplateArgumentList *TemplateArgs,
618 void *InsertPos) {
Douglas Gregor16e8be22009-06-29 17:30:29 +0000619 FunctionTemplateSpecializationInfo *Info
620 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000621 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000622 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor1637be72009-06-26 00:10:03 +0000623
Douglas Gregor127102b2009-06-29 20:59:39 +0000624 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000625 Info->Template.setPointer(Template);
626 Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
Douglas Gregor1637be72009-06-26 00:10:03 +0000627 Info->TemplateArguments = TemplateArgs;
628 TemplateOrSpecialization = Info;
Douglas Gregor127102b2009-06-29 20:59:39 +0000629
630 // Insert this function template specialization into the set of known
631 // function template specialiations.
632 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +0000633}
634
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000635bool FunctionDecl::isExplicitSpecialization() const {
636 // FIXME: check this property for explicit specializations of member
637 // functions of class templates.
638 FunctionTemplateSpecializationInfo *Info
639 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
640 if (!Info)
641 return false;
642
643 return Info->isExplicitSpecialization();
644}
645
646void FunctionDecl::setExplicitSpecialization(bool ES) {
647 // FIXME: set this property for explicit specializations of member functions
648 // of class templates.
649 FunctionTemplateSpecializationInfo *Info
650 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
651 if (Info)
652 Info->setExplicitSpecialization(ES);
653}
654
Chris Lattner8a934232008-03-31 00:36:02 +0000655//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000656// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000657//===----------------------------------------------------------------------===//
658
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000659SourceRange TagDecl::getSourceRange() const {
660 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000661 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000662}
663
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000664TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000665 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000666}
667
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000668void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000669 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
670 TagT->decl.setPointer(this);
671 TagT->decl.setInt(1);
672 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000673}
674
675void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000676 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000677 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
678 assert(TagT->decl.getPointer() == this &&
679 "Attempt to redefine a tag definition?");
680 TagT->decl.setInt(0);
681 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000682}
683
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000684TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000685 if (isDefinition())
686 return const_cast<TagDecl *>(this);
687
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000688 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
689 R != REnd; ++R)
690 if (R->isDefinition())
691 return *R;
692
693 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000694}
695
696//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000697// RecordDecl Implementation
698//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000699
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000700RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000701 IdentifierInfo *Id, RecordDecl *PrevDecl,
702 SourceLocation TKL)
703 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +0000704 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000705 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000706 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000707 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000708}
709
710RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000711 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000712 SourceLocation TKL, RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000713
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000714 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000715 C.getTypeDeclType(R, PrevDecl);
716 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000717}
718
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000719RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000720}
721
722void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000723 TagDecl::Destroy(C);
724}
725
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000726bool RecordDecl::isInjectedClassName() const {
727 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
728 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
729}
730
Douglas Gregor44b43212008-12-11 16:49:14 +0000731/// completeDefinition - Notes that the definition of this type is now
732/// complete.
733void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000735 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000736}
737
Steve Naroff56ee6892008-10-08 17:01:13 +0000738//===----------------------------------------------------------------------===//
739// BlockDecl Implementation
740//===----------------------------------------------------------------------===//
741
742BlockDecl::~BlockDecl() {
743}
744
745void BlockDecl::Destroy(ASTContext& C) {
746 if (Body)
747 Body->Destroy(C);
748
749 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
750 (*I)->Destroy(C);
Ted Kremenek879d27a2009-03-13 23:17:24 +0000751
752 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000753 Decl::Destroy(C);
754}
Steve Naroffe78b8092009-03-13 16:56:44 +0000755
756void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
757 unsigned NParms) {
758 assert(ParamInfo == 0 && "Already has param info!");
759
760 // Zero params -> null pointer.
761 if (NParms) {
762 NumParams = NParms;
763 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
764 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
765 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
766 }
767}
768
769unsigned BlockDecl::getNumParams() const {
770 return NumParams;
771}