blob: ae1df920125dc66ff2a2851d3e918b63e3fe2f66 [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"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Stmt.h"
Nuno Lopesc98406e2008-12-17 23:39:55 +000020#include "clang/AST/Expr.h"
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000021#include "clang/AST/PrettyPrinter.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000023#include "clang/Basic/IdentifierTable.h"
Douglas Gregor09be81b2009-02-04 17:27:36 +000024#include <vector>
Ted Kremenekafdf8112008-05-20 00:43:19 +000025
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
Chris Lattner16ded572009-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 Lattnera8344c32008-03-15 05:43:15 +000038//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000039// Decl Allocation/Deallocation Method Implementations
40//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000041
Chris Lattner16ded572009-03-04 06:34:08 +000042
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000043TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argiris Kirtzidis2a6dca12009-06-29 17:38:40 +000044 return new (C) TranslationUnitDecl(C);
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000045}
46
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000047NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
48 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000049 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000050}
51
Ted Kremenek5be49242008-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 Kremenek0f433842008-05-24 15:09:56 +000056 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000057 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000058}
59
60
Chris Lattner8c7c6a12008-06-17 18:05:57 +000061ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000062 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000063 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000064}
65
Daniel Dunbarcaf78fb2009-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 Lattneref87a202008-04-22 18:39:57 +000080ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000081 SourceLocation L, IdentifierInfo *Id,
82 QualType T, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000083 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000084 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000085}
86
87QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000088 if (const OriginalParmVarDecl *PVD =
89 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000090 return PVD->OriginalType;
91 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000092}
93
Douglas Gregor4833ff02009-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 Gregore6b5d1d2009-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 Gregor469fc9a2009-02-02 23:39:07 +0000125OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000126 ASTContext &C, DeclContext *DC,
127 SourceLocation L, IdentifierInfo *Id,
128 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000129 Expr *DefArg) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000130 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000131}
132
Chris Lattneref87a202008-04-22 18:39:57 +0000133FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000134 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +0000135 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000136 StorageClass S, bool isInline,
Anders Carlssond9d6d502009-05-14 21:46:00 +0000137 bool hasWrittenPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +0000138 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000139 FunctionDecl *New
140 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
141 TypeSpecStartLoc);
Anders Carlssond9d6d502009-05-14 21:46:00 +0000142 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000143 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000144}
145
Steve Naroff52059382008-10-10 01:28:17 +0000146BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000147 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000148}
149
Douglas Gregor8acb7272008-12-11 16:49:14 +0000150FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
151 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000152 bool Mutable) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000153 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +0000154}
155
Douglas Gregorc7f01612009-01-07 19:46:03 +0000156bool FieldDecl::isAnonymousStructOrUnion() const {
157 if (!isImplicit() || getDeclName())
158 return false;
159
160 if (const RecordType *Record = getType()->getAsRecordType())
161 return Record->getDecl()->isAnonymousStructOrUnion();
162
163 return false;
164}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000165
Chris Lattnereee57c02008-04-04 06:12:32 +0000166EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
167 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000168 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000169 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000170 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000171}
172
Ted Kremenek5be49242008-05-20 04:49:55 +0000173void EnumConstantDecl::Destroy(ASTContext& C) {
174 if (Init) Init->Destroy(C);
175 Decl::Destroy(C);
176}
177
Chris Lattneref87a202008-04-22 18:39:57 +0000178TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000179 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000180 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000181 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000182}
183
Chris Lattneref87a202008-04-22 18:39:57 +0000184EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000185 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000186 EnumDecl *PrevDecl) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000187 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000188 C.getTypeDeclType(Enum, PrevDecl);
189 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000190}
191
Ted Kremenek25d8be12008-09-02 20:13:32 +0000192void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000193 Decl::Destroy(C);
194}
195
Douglas Gregor8acb7272008-12-11 16:49:14 +0000196void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
197 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000198 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000199 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000200}
201
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000202FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000203 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000204 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000205 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000206}
207
Chris Lattnere4650482008-03-15 06:12:44 +0000208//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000209// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000210//===----------------------------------------------------------------------===//
211
Douglas Gregor09be81b2009-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 Gregorb12249d2009-05-18 17:01:57 +0000226 if (const ClassTemplateSpecializationDecl *Spec
227 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
228 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Chris Lattner7099c782009-06-30 01:26:17 +0000229 PrintingPolicy Policy(getASTContext().getLangOptions());
Douglas Gregorb12249d2009-05-18 17:01:57 +0000230 std::string TemplateArgsStr
231 = TemplateSpecializationType::PrintTemplateArgumentList(
232 TemplateArgs.getFlatArgumentList(),
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000233 TemplateArgs.flat_size(),
234 Policy);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000235 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
236 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor09be81b2009-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 Gregoraf8ad2b2009-01-20 01:17:11 +0000257bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000258 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
259
Douglas Gregor7a7be652009-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 Gregor6e71edc2008-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 Gregorb60eb752009-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 Naroffd85ba922009-02-22 19:35:57 +0000279 // For method declarations, we keep track of redeclarations.
280 if (isa<ObjCMethodDecl>(this))
281 return false;
282
Douglas Gregor6e71edc2008-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 Gregor1c52c632009-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 Gregoraf8ad2b2009-01-20 01:17:11 +0000298
Anders Carlssondf5ab842009-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
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000312//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000313// VarDecl Implementation
314//===----------------------------------------------------------------------===//
315
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000316VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
317 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000318 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000319 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000320}
321
322void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000323 Expr *Init = getInit();
Douglas Gregor4833ff02009-05-26 18:54:04 +0000324 if (Init) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000325 Init->Destroy(C);
Douglas Gregor4833ff02009-05-26 18:54:04 +0000326 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
327 Eval->~EvaluatedStmt();
328 C.Deallocate(Eval);
329 }
330 }
Nuno Lopesc98406e2008-12-17 23:39:55 +0000331 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000332 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000333}
334
335VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000336}
337
Argiris Kirtzidis545473e2009-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 Gregor2f728b22009-03-10 23:43:53 +0000344bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
345 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
346 return false;
347
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000348 const VarDecl *Def = 0;
349 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000350 (getStorageClass() == None || getStorageClass() == Static));
351}
352
Ted Kremenek51787c72009-03-20 21:35:28 +0000353const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Douglas Gregor2f728b22009-03-10 23:43:53 +0000354 Def = this;
355 while (Def && !Def->getInit())
356 Def = Def->getPreviousDeclaration();
357
358 return Def? Def->getInit() : 0;
359}
360
Argiris Kirtzidis349185b2009-07-14 03:20:08 +0000361void VarDecl::setPreviousDeclaration(VarDecl *PrevDecl) {
362 if (PrevDecl) {
363 // Point to previous.
364 PreviousDeclaration.setPointer(PrevDecl);
365 PreviousDeclaration.setInt(0);
366
367 // First one will point to this one as latest.
368 VarDecl *First = PrevDecl->getFirstDeclaration();
369 assert(First->PreviousDeclaration.getInt() == 1 && "Expected first");
370 First->PreviousDeclaration.setPointer(this);
371 } else {
372 // This is first.
373 PreviousDeclaration.setPointer(this);
374 PreviousDeclaration.setInt(1);
375 }
376}
377
378VarDecl *VarDecl::getFirstDeclaration() {
379 VarDecl *First = this;
Argiris Kirtzidis9f000a32009-07-14 03:19:57 +0000380 while (First->getPreviousDeclaration())
381 First = First->getPreviousDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000382
Argiris Kirtzidis9f000a32009-07-14 03:19:57 +0000383 return First;
384}
385
386Decl *VarDecl::getPrimaryDecl() const {
387 return const_cast<VarDecl *>(getFirstDeclaration());
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000388}
389
Nuno Lopesc98406e2008-12-17 23:39:55 +0000390//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000391// FunctionDecl Implementation
392//===----------------------------------------------------------------------===//
393
Ted Kremenekafdf8112008-05-20 00:43:19 +0000394void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000395 if (Body && Body.isOffset())
396 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000397
398 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
399 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000400
Steve Naroff5abb0282009-01-27 21:25:57 +0000401 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000402
Ted Kremenekafdf8112008-05-20 00:43:19 +0000403 Decl::Destroy(C);
404}
405
406
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000407Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argiris Kirtzidis349185b2009-07-14 03:20:08 +0000408 for (const FunctionDecl *FD = this;
409 FD != 0; FD = FD->getPreviousDeclaration()) {
Douglas Gregor42214c52008-04-21 02:02:58 +0000410 if (FD->Body) {
411 Definition = FD;
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000412 return FD->Body.get(getASTContext().getExternalSource());
Douglas Gregor42214c52008-04-21 02:02:58 +0000413 }
414 }
415
416 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000417}
418
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000419Stmt *FunctionDecl::getBodyIfAvailable() const {
Argiris Kirtzidis349185b2009-07-14 03:20:08 +0000420 for (const FunctionDecl *FD = this;
421 FD != 0; FD = FD->getPreviousDeclaration()) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000422 if (FD->Body && !FD->Body.isOffset()) {
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000423 return FD->Body.get(0);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000424 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000425 }
426
427 return 0;
428}
429
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000430void FunctionDecl::setBody(Stmt *B) {
431 Body = B;
Argiris Kirtzidis25822a02009-06-22 17:13:31 +0000432 if (B)
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000433 EndRangeLoc = B->getLocEnd();
434}
435
Douglas Gregoraf682022009-02-24 01:23:02 +0000436bool FunctionDecl::isMain() const {
437 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
438 getIdentifier() && getIdentifier()->isStr("main");
439}
440
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000441bool FunctionDecl::isExternC(ASTContext &Context) const {
442 // In C, any non-static, non-overloadable function has external
443 // linkage.
444 if (!Context.getLangOptions().CPlusPlus)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000445 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000446
447 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
448 DC = DC->getParent()) {
449 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
450 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000451 return getStorageClass() != Static &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000452 !getAttr<OverloadableAttr>();
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000453
454 break;
455 }
456 }
457
458 return false;
459}
460
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000461bool FunctionDecl::isGlobal() const {
462 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
463 return Method->isStatic();
464
465 if (getStorageClass() == Static)
466 return false;
467
468 for (const DeclContext *DC = getDeclContext();
469 DC->isNamespace();
470 DC = DC->getParent()) {
471 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
472 if (!Namespace->getDeclName())
473 return false;
474 break;
475 }
476 }
477
478 return true;
479}
480
Douglas Gregor411889e2009-02-13 23:20:09 +0000481/// \brief Returns a value indicating whether this function
482/// corresponds to a builtin function.
483///
484/// The function corresponds to a built-in function if it is
485/// declared at translation scope or within an extern "C" block and
486/// its name matches with the name of a builtin. The returned value
487/// will be 0 for functions that do not correspond to a builtin, a
488/// value of type \c Builtin::ID if in the target-independent range
489/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000490unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
491 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
492 return 0;
493
494 unsigned BuiltinID = getIdentifier()->getBuiltinID();
495 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
496 return BuiltinID;
497
498 // This function has the name of a known C library
499 // function. Determine whether it actually refers to the C library
500 // function or whether it just has the same name.
501
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000502 // If this is a static function, it's not a builtin.
503 if (getStorageClass() == Static)
504 return 0;
505
Douglas Gregorb5af7382009-02-14 18:57:46 +0000506 // If this function is at translation-unit scope and we're not in
507 // C++, it refers to the C library function.
508 if (!Context.getLangOptions().CPlusPlus &&
509 getDeclContext()->isTranslationUnit())
510 return BuiltinID;
511
512 // If the function is in an extern "C" linkage specification and is
513 // not marked "overloadable", it's the real function.
514 if (isa<LinkageSpecDecl>(getDeclContext()) &&
515 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
516 == LinkageSpecDecl::lang_c &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000517 !getAttr<OverloadableAttr>())
Douglas Gregorb5af7382009-02-14 18:57:46 +0000518 return BuiltinID;
519
520 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000521 return 0;
522}
523
524
Chris Lattnerd2112022009-04-25 06:03:53 +0000525/// getNumParams - Return the number of parameters this function must have
Chris Lattnerc13d4732009-04-25 06:12:16 +0000526/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattnerd2112022009-04-25 06:03:53 +0000527/// after it has been created.
528unsigned FunctionDecl::getNumParams() const {
Chris Lattner9d957cb2009-04-25 05:56:45 +0000529 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000530 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000531 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000532 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner9d957cb2009-04-25 05:56:45 +0000533
Chris Lattner4b009652007-07-25 00:24:17 +0000534}
535
Ted Kremenek8494c962009-01-14 00:42:25 +0000536void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
537 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000538 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattnerc13d4732009-04-25 06:12:16 +0000539 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000540
541 // Zero params -> null pointer.
542 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000543 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000544 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000545 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000546
Argiris Kirtzidisae78f1f2009-06-23 00:42:00 +0000547 // Update source range. The check below allows us to set EndRangeLoc before
548 // setting the parameters.
Argiris Kirtzidis0dba8f22009-06-23 00:42:15 +0000549 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000550 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner4b009652007-07-25 00:24:17 +0000551 }
552}
553
Chris Lattner97316c02008-04-10 02:22:51 +0000554/// getMinRequiredArguments - Returns the minimum number of arguments
555/// needed to call this function. This may be fewer than the number of
556/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000557/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000558unsigned FunctionDecl::getMinRequiredArguments() const {
559 unsigned NumRequiredArgs = getNumParams();
560 while (NumRequiredArgs > 0
Anders Carlssond2e57d92009-06-06 04:14:07 +0000561 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner97316c02008-04-10 02:22:51 +0000562 --NumRequiredArgs;
563
564 return NumRequiredArgs;
565}
566
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000567bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000568 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000569 return false;
570
571 for (const FunctionDecl *FD = getPreviousDeclaration(); FD;
572 FD = FD->getPreviousDeclaration()) {
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000573 if (FD->isInline() && !FD->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000574 return false;
575 }
576
577 return true;
578}
579
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000580bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
581 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor67e11442009-04-28 06:37:30 +0000582 return false;
583
584 for (const FunctionDecl *FD = this; FD; FD = FD->getPreviousDeclaration())
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000585 if (FD->getStorageClass() == Extern && FD->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000586 return true;
587
588 return false;
589}
590
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000591void
592FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argiris Kirtzidis349185b2009-07-14 03:20:08 +0000593 if (PrevDecl) {
594 // Point to previous.
595 PreviousDeclaration.setPointer(PrevDecl);
596 PreviousDeclaration.setInt(0);
597
598 // First one will point to this one as latest.
599 FunctionDecl *First = PrevDecl->getFirstDeclaration();
600 assert(First->PreviousDeclaration.getInt() == 1 && "Expected first");
601 First->PreviousDeclaration.setPointer(this);
602 } else {
603 // This is first.
604 PreviousDeclaration.setPointer(this);
605 PreviousDeclaration.setInt(1);
606 }
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000607
608 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
609 FunctionTemplateDecl *PrevFunTmpl
610 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
611 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
612 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
613 }
614}
615
Argiris Kirtzidis349185b2009-07-14 03:20:08 +0000616FunctionDecl *FunctionDecl::getFirstDeclaration() {
617 FunctionDecl *First = this;
Argiris Kirtzidis9f000a32009-07-14 03:19:57 +0000618 while (First->getPreviousDeclaration())
619 First = First->getPreviousDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000620
Argiris Kirtzidis9f000a32009-07-14 03:19:57 +0000621 return First;
622}
623
624Decl *FunctionDecl::getPrimaryDecl() const {
625 return const_cast<FunctionDecl *>(getFirstDeclaration());
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000626}
627
Douglas Gregore60e5d32008-11-06 22:13:31 +0000628/// getOverloadedOperator - Which C++ overloaded operator this
629/// function represents, if any.
630OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000631 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
632 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000633 else
634 return OO_None;
635}
636
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000637FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
638 if (FunctionTemplateSpecializationInfo *Info
639 = TemplateOrSpecialization
640 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor69678062009-06-29 22:39:32 +0000641 return Info->Template.getPointer();
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000642 }
643 return 0;
644}
645
646const TemplateArgumentList *
647FunctionDecl::getTemplateSpecializationArgs() const {
648 if (FunctionTemplateSpecializationInfo *Info
649 = TemplateOrSpecialization
650 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
651 return Info->TemplateArguments;
652 }
653 return 0;
654}
655
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000656void
657FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
658 FunctionTemplateDecl *Template,
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000659 const TemplateArgumentList *TemplateArgs,
660 void *InsertPos) {
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000661 FunctionTemplateSpecializationInfo *Info
662 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000663 if (!Info)
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000664 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000665
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000666 Info->Function = this;
Douglas Gregor69678062009-06-29 22:39:32 +0000667 Info->Template.setPointer(Template);
668 Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000669 Info->TemplateArguments = TemplateArgs;
670 TemplateOrSpecialization = Info;
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000671
672 // Insert this function template specialization into the set of known
673 // function template specialiations.
674 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000675}
676
Douglas Gregor69678062009-06-29 22:39:32 +0000677bool FunctionDecl::isExplicitSpecialization() const {
678 // FIXME: check this property for explicit specializations of member
679 // functions of class templates.
680 FunctionTemplateSpecializationInfo *Info
681 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
682 if (!Info)
683 return false;
684
685 return Info->isExplicitSpecialization();
686}
687
688void FunctionDecl::setExplicitSpecialization(bool ES) {
689 // FIXME: set this property for explicit specializations of member functions
690 // of class templates.
691 FunctionTemplateSpecializationInfo *Info
692 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
693 if (Info)
694 Info->setExplicitSpecialization(ES);
695}
696
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000697//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000698// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000699//===----------------------------------------------------------------------===//
700
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000701SourceRange TagDecl::getSourceRange() const {
702 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
703 return SourceRange(getLocation(), E);
704}
705
Douglas Gregor98b27542009-01-17 00:42:38 +0000706void TagDecl::startDefinition() {
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000707 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
708 TagT->decl.setPointer(this);
709 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor98b27542009-01-17 00:42:38 +0000710}
711
712void TagDecl::completeDefinition() {
713 assert((!TypeForDecl ||
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000714 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor98b27542009-01-17 00:42:38 +0000715 "Attempt to redefine a tag definition?");
716 IsDefinition = true;
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000717 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
718 TagT->decl.setPointer(this);
719 TagT->decl.setInt(0);
Douglas Gregor98b27542009-01-17 00:42:38 +0000720}
721
Ted Kremenek46a837c2008-09-05 17:16:31 +0000722TagDecl* TagDecl::getDefinition(ASTContext& C) const {
723 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000724 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek46a837c2008-09-05 17:16:31 +0000725 return D->isDefinition() ? D : 0;
726}
727
728//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000729// RecordDecl Implementation
730//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000731
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000732RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000733 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000734 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000735 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000736 AnonymousStructOrUnion = false;
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000737 HasObjectMember = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000738 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000739}
740
741RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000742 SourceLocation L, IdentifierInfo *Id,
743 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000744
Steve Naroff5abb0282009-01-27 21:25:57 +0000745 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000746 C.getTypeDeclType(R, PrevDecl);
747 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000748}
749
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000750RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000751}
752
753void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000754 TagDecl::Destroy(C);
755}
756
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000757bool RecordDecl::isInjectedClassName() const {
758 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
759 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
760}
761
Douglas Gregor8acb7272008-12-11 16:49:14 +0000762/// completeDefinition - Notes that the definition of this type is now
763/// complete.
764void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000765 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000766 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000767}
768
Steve Naroff9ac456d2008-10-08 17:01:13 +0000769//===----------------------------------------------------------------------===//
770// BlockDecl Implementation
771//===----------------------------------------------------------------------===//
772
773BlockDecl::~BlockDecl() {
774}
775
776void BlockDecl::Destroy(ASTContext& C) {
777 if (Body)
778 Body->Destroy(C);
779
780 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
781 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000782
783 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000784 Decl::Destroy(C);
785}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000786
787void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
788 unsigned NParms) {
789 assert(ParamInfo == 0 && "Already has param info!");
790
791 // Zero params -> null pointer.
792 if (NParms) {
793 NumParams = NParms;
794 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
795 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
796 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
797 }
798}
799
800unsigned BlockDecl::getNumParams() const {
801 return NumParams;
802}