blob: cb3ec1f487da4e9afc432e1b1f8bbc9f0e1d28ee [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"
Daniel Dunbarde300732008-08-11 04:54:23 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregor09be81b2009-02-04 17:27:36 +000023#include <vector>
Ted Kremenekafdf8112008-05-20 00:43:19 +000024
Chris Lattner4b009652007-07-25 00:24:17 +000025using namespace clang;
26
Chris Lattner16ded572009-03-04 06:34:08 +000027void Attr::Destroy(ASTContext &C) {
28 if (Next) {
29 Next->Destroy(C);
30 Next = 0;
31 }
32 this->~Attr();
33 C.Deallocate((void*)this);
34}
35
36
Chris Lattnera8344c32008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000038// Decl Allocation/Deallocation Method Implementations
39//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000040
Chris Lattner16ded572009-03-04 06:34:08 +000041
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000042TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff5abb0282009-01-27 21:25:57 +000043 return new (C) TranslationUnitDecl();
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000044}
45
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000046NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
47 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000048 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000049}
50
Ted Kremenek5be49242008-05-20 04:49:55 +000051void NamespaceDecl::Destroy(ASTContext& C) {
52 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
53 // together. They are all top-level Decls.
54
Ted Kremenek0f433842008-05-24 15:09:56 +000055 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000056 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000057}
58
59
Chris Lattner8c7c6a12008-06-17 18:05:57 +000060ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000061 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000062 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000063}
64
Daniel Dunbarcaf78fb2009-04-14 02:08:49 +000065const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
66 switch (SC) {
67 case VarDecl::None: break;
68 case VarDecl::Auto: return "auto"; break;
69 case VarDecl::Extern: return "extern"; break;
70 case VarDecl::PrivateExtern: return "__private_extern__"; break;
71 case VarDecl::Register: return "register"; break;
72 case VarDecl::Static: return "static"; break;
73 }
74
75 assert(0 && "Invalid storage class");
76 return 0;
77}
78
Chris Lattneref87a202008-04-22 18:39:57 +000079ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000080 SourceLocation L, IdentifierInfo *Id,
81 QualType T, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000082 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000083 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000084}
85
86QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000087 if (const OriginalParmVarDecl *PVD =
88 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000089 return PVD->OriginalType;
90 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000091}
92
Douglas Gregor4833ff02009-05-26 18:54:04 +000093void VarDecl::setInit(ASTContext &C, Expr *I) {
94 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
95 Eval->~EvaluatedStmt();
96 C.Deallocate(Eval);
97 }
98
99 Init = I;
100 }
101
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000102bool VarDecl::isExternC(ASTContext &Context) const {
103 if (!Context.getLangOptions().CPlusPlus)
104 return (getDeclContext()->isTranslationUnit() &&
105 getStorageClass() != Static) ||
106 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
107
108 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
109 DC = DC->getParent()) {
110 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
111 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
112 return getStorageClass() != Static;
113
114 break;
115 }
116
117 if (DC->isFunctionOrMethod())
118 return false;
119 }
120
121 return false;
122}
123
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000124OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000125 ASTContext &C, DeclContext *DC,
126 SourceLocation L, IdentifierInfo *Id,
127 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000128 Expr *DefArg) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000129 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000130}
131
Chris Lattneref87a202008-04-22 18:39:57 +0000132FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000133 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +0000134 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000135 StorageClass S, bool isInline,
Anders Carlssond9d6d502009-05-14 21:46:00 +0000136 bool hasWrittenPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +0000137 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000138 FunctionDecl *New
139 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
140 TypeSpecStartLoc);
Anders Carlssond9d6d502009-05-14 21:46:00 +0000141 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000142 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000143}
144
Steve Naroff52059382008-10-10 01:28:17 +0000145BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000146 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000147}
148
Douglas Gregor8acb7272008-12-11 16:49:14 +0000149FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
150 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000151 bool Mutable) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000152 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +0000153}
154
Douglas Gregorc7f01612009-01-07 19:46:03 +0000155bool FieldDecl::isAnonymousStructOrUnion() const {
156 if (!isImplicit() || getDeclName())
157 return false;
158
159 if (const RecordType *Record = getType()->getAsRecordType())
160 return Record->getDecl()->isAnonymousStructOrUnion();
161
162 return false;
163}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000164
Chris Lattnereee57c02008-04-04 06:12:32 +0000165EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
166 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000167 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000168 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000169 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000170}
171
Ted Kremenek5be49242008-05-20 04:49:55 +0000172void EnumConstantDecl::Destroy(ASTContext& C) {
173 if (Init) Init->Destroy(C);
174 Decl::Destroy(C);
175}
176
Chris Lattneref87a202008-04-22 18:39:57 +0000177TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000178 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000179 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000180 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000181}
182
Chris Lattneref87a202008-04-22 18:39:57 +0000183EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000184 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000185 EnumDecl *PrevDecl) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000186 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000187 C.getTypeDeclType(Enum, PrevDecl);
188 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000189}
190
Ted Kremenek25d8be12008-09-02 20:13:32 +0000191void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000192 Decl::Destroy(C);
193}
194
Douglas Gregor8acb7272008-12-11 16:49:14 +0000195void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
196 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000197 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000198 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000199}
200
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000201FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000202 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000203 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000204 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000205}
206
Chris Lattnere4650482008-03-15 06:12:44 +0000207//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000208// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000209//===----------------------------------------------------------------------===//
210
Douglas Gregor09be81b2009-02-04 17:27:36 +0000211std::string NamedDecl::getQualifiedNameAsString() const {
212 std::vector<std::string> Names;
213 std::string QualName;
214 const DeclContext *Ctx = getDeclContext();
215
216 if (Ctx->isFunctionOrMethod())
217 return getNameAsString();
218
219 while (Ctx) {
220 if (Ctx->isFunctionOrMethod())
221 // FIXME: That probably will happen, when D was member of local
222 // scope class/struct/union. How do we handle this case?
223 break;
224
Douglas Gregorb12249d2009-05-18 17:01:57 +0000225 if (const ClassTemplateSpecializationDecl *Spec
226 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
227 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000228 PrintingPolicy Policy;
229 Policy.CPlusPlus = true;
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
Steve Naroffd85ba922009-02-22 19:35:57 +0000271 // For method declarations, we keep track of redeclarations.
272 if (isa<ObjCMethodDecl>(this))
273 return false;
274
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000275 // For non-function declarations, if the declarations are of the
276 // same kind then this must be a redeclaration, or semantic analysis
277 // would not have given us the new declaration.
278 return this->getKind() == OldD->getKind();
279}
280
Douglas Gregor1c52c632009-02-24 20:03:32 +0000281bool NamedDecl::hasLinkage() const {
282 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
283 return VD->hasExternalStorage() || VD->isFileVarDecl();
284
285 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
286 return true;
287
288 return false;
289}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000290
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000291//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000292// VarDecl Implementation
293//===----------------------------------------------------------------------===//
294
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000295VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
296 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000297 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000298 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000299}
300
301void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000302 Expr *Init = getInit();
Douglas Gregor4833ff02009-05-26 18:54:04 +0000303 if (Init) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000304 Init->Destroy(C);
Douglas Gregor4833ff02009-05-26 18:54:04 +0000305 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
306 Eval->~EvaluatedStmt();
307 C.Deallocate(Eval);
308 }
309 }
Nuno Lopesc98406e2008-12-17 23:39:55 +0000310 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000311 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000312}
313
314VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000315}
316
Douglas Gregor2f728b22009-03-10 23:43:53 +0000317bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
318 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
319 return false;
320
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000321 const VarDecl *Def = 0;
322 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000323 (getStorageClass() == None || getStorageClass() == Static));
324}
325
Ted Kremenek51787c72009-03-20 21:35:28 +0000326const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Douglas Gregor2f728b22009-03-10 23:43:53 +0000327 Def = this;
328 while (Def && !Def->getInit())
329 Def = Def->getPreviousDeclaration();
330
331 return Def? Def->getInit() : 0;
332}
333
Nuno Lopesc98406e2008-12-17 23:39:55 +0000334//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000335// FunctionDecl Implementation
336//===----------------------------------------------------------------------===//
337
Ted Kremenekafdf8112008-05-20 00:43:19 +0000338void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000339 if (Body && Body.isOffset())
340 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000341
342 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
343 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000344
Steve Naroff5abb0282009-01-27 21:25:57 +0000345 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000346
Ted Kremenekafdf8112008-05-20 00:43:19 +0000347 Decl::Destroy(C);
348}
349
350
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000351Stmt *FunctionDecl::getBody(ASTContext &Context,
352 const FunctionDecl *&Definition) const {
Douglas Gregor42214c52008-04-21 02:02:58 +0000353 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
354 if (FD->Body) {
355 Definition = FD;
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000356 return FD->Body.get(Context.getExternalSource());
Douglas Gregor42214c52008-04-21 02:02:58 +0000357 }
358 }
359
360 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000361}
362
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000363Stmt *FunctionDecl::getBodyIfAvailable() const {
Douglas Gregore3241e92009-04-18 00:02:19 +0000364 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000365 if (FD->Body && !FD->Body.isOffset()) {
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000366 return FD->Body.get(0);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000367 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000368 }
369
370 return 0;
371}
372
Douglas Gregoraf682022009-02-24 01:23:02 +0000373bool FunctionDecl::isMain() const {
374 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
375 getIdentifier() && getIdentifier()->isStr("main");
376}
377
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000378bool FunctionDecl::isExternC(ASTContext &Context) const {
379 // In C, any non-static, non-overloadable function has external
380 // linkage.
381 if (!Context.getLangOptions().CPlusPlus)
382 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
383
384 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
385 DC = DC->getParent()) {
386 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
387 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
388 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
389
390 break;
391 }
392 }
393
394 return false;
395}
396
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000397bool FunctionDecl::isGlobal() const {
398 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
399 return Method->isStatic();
400
401 if (getStorageClass() == Static)
402 return false;
403
404 for (const DeclContext *DC = getDeclContext();
405 DC->isNamespace();
406 DC = DC->getParent()) {
407 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
408 if (!Namespace->getDeclName())
409 return false;
410 break;
411 }
412 }
413
414 return true;
415}
416
Douglas Gregor411889e2009-02-13 23:20:09 +0000417/// \brief Returns a value indicating whether this function
418/// corresponds to a builtin function.
419///
420/// The function corresponds to a built-in function if it is
421/// declared at translation scope or within an extern "C" block and
422/// its name matches with the name of a builtin. The returned value
423/// will be 0 for functions that do not correspond to a builtin, a
424/// value of type \c Builtin::ID if in the target-independent range
425/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000426unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
427 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
428 return 0;
429
430 unsigned BuiltinID = getIdentifier()->getBuiltinID();
431 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
432 return BuiltinID;
433
434 // This function has the name of a known C library
435 // function. Determine whether it actually refers to the C library
436 // function or whether it just has the same name.
437
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000438 // If this is a static function, it's not a builtin.
439 if (getStorageClass() == Static)
440 return 0;
441
Douglas Gregorb5af7382009-02-14 18:57:46 +0000442 // If this function is at translation-unit scope and we're not in
443 // C++, it refers to the C library function.
444 if (!Context.getLangOptions().CPlusPlus &&
445 getDeclContext()->isTranslationUnit())
446 return BuiltinID;
447
448 // If the function is in an extern "C" linkage specification and is
449 // not marked "overloadable", it's the real function.
450 if (isa<LinkageSpecDecl>(getDeclContext()) &&
451 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
452 == LinkageSpecDecl::lang_c &&
453 !getAttr<OverloadableAttr>())
454 return BuiltinID;
455
456 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000457 return 0;
458}
459
460
Chris Lattnerd2112022009-04-25 06:03:53 +0000461/// getNumParams - Return the number of parameters this function must have
Chris Lattnerc13d4732009-04-25 06:12:16 +0000462/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattnerd2112022009-04-25 06:03:53 +0000463/// after it has been created.
464unsigned FunctionDecl::getNumParams() const {
Chris Lattner9d957cb2009-04-25 05:56:45 +0000465 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000466 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000467 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000468 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner9d957cb2009-04-25 05:56:45 +0000469
Chris Lattner4b009652007-07-25 00:24:17 +0000470}
471
Ted Kremenek8494c962009-01-14 00:42:25 +0000472void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
473 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000474 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattnerc13d4732009-04-25 06:12:16 +0000475 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000476
477 // Zero params -> null pointer.
478 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000479 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000480 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000481 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
482 }
483}
484
Chris Lattner97316c02008-04-10 02:22:51 +0000485/// getMinRequiredArguments - Returns the minimum number of arguments
486/// needed to call this function. This may be fewer than the number of
487/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000488/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000489unsigned FunctionDecl::getMinRequiredArguments() const {
490 unsigned NumRequiredArgs = getNumParams();
491 while (NumRequiredArgs > 0
492 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
493 --NumRequiredArgs;
494
495 return NumRequiredArgs;
496}
497
Douglas Gregor67e11442009-04-28 06:37:30 +0000498bool FunctionDecl::hasActiveGNUInlineAttribute() const {
499 if (!isInline() || !hasAttr<GNUInlineAttr>())
500 return false;
501
502 for (const FunctionDecl *FD = getPreviousDeclaration(); FD;
503 FD = FD->getPreviousDeclaration()) {
504 if (FD->isInline() && !FD->hasAttr<GNUInlineAttr>())
505 return false;
506 }
507
508 return true;
509}
510
511bool FunctionDecl::isExternGNUInline() const {
512 if (!hasActiveGNUInlineAttribute())
513 return false;
514
515 for (const FunctionDecl *FD = this; FD; FD = FD->getPreviousDeclaration())
516 if (FD->getStorageClass() == Extern && FD->hasAttr<GNUInlineAttr>())
517 return true;
518
519 return false;
520}
521
Douglas Gregore60e5d32008-11-06 22:13:31 +0000522/// getOverloadedOperator - Which C++ overloaded operator this
523/// function represents, if any.
524OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000525 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
526 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000527 else
528 return OO_None;
529}
530
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000531//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000532// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000533//===----------------------------------------------------------------------===//
534
Douglas Gregor98b27542009-01-17 00:42:38 +0000535void TagDecl::startDefinition() {
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000536 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
537 TagT->decl.setPointer(this);
538 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor98b27542009-01-17 00:42:38 +0000539}
540
541void TagDecl::completeDefinition() {
542 assert((!TypeForDecl ||
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000543 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor98b27542009-01-17 00:42:38 +0000544 "Attempt to redefine a tag definition?");
545 IsDefinition = true;
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000546 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
547 TagT->decl.setPointer(this);
548 TagT->decl.setInt(0);
Douglas Gregor98b27542009-01-17 00:42:38 +0000549}
550
Ted Kremenek46a837c2008-09-05 17:16:31 +0000551TagDecl* TagDecl::getDefinition(ASTContext& C) const {
552 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000553 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek46a837c2008-09-05 17:16:31 +0000554 return D->isDefinition() ? D : 0;
555}
556
557//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000558// RecordDecl Implementation
559//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000560
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000561RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000562 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000563 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000564 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000565 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000566 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000567}
568
569RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000570 SourceLocation L, IdentifierInfo *Id,
571 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000572
Steve Naroff5abb0282009-01-27 21:25:57 +0000573 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000574 C.getTypeDeclType(R, PrevDecl);
575 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000576}
577
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000578RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000579}
580
581void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000582 TagDecl::Destroy(C);
583}
584
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000585bool RecordDecl::isInjectedClassName() const {
586 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
587 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
588}
589
Douglas Gregor8acb7272008-12-11 16:49:14 +0000590/// completeDefinition - Notes that the definition of this type is now
591/// complete.
592void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000593 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000594 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000595}
596
Steve Naroff9ac456d2008-10-08 17:01:13 +0000597//===----------------------------------------------------------------------===//
598// BlockDecl Implementation
599//===----------------------------------------------------------------------===//
600
601BlockDecl::~BlockDecl() {
602}
603
604void BlockDecl::Destroy(ASTContext& C) {
605 if (Body)
606 Body->Destroy(C);
607
608 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
609 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000610
611 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000612 Decl::Destroy(C);
613}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000614
615void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
616 unsigned NParms) {
617 assert(ParamInfo == 0 && "Already has param info!");
618
619 // Zero params -> null pointer.
620 if (NParms) {
621 NumParams = NParms;
622 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
623 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
624 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
625 }
626}
627
628unsigned BlockDecl::getNumParams() const {
629 return NumParams;
630}