blob: 0a6f28d3fb7a29ef5e7601c96e22ea0b2e1ec620 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000019#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000020#include "clang/AST/Expr.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000021#include "clang/AST/PrettyPrinter.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000023#include "clang/Basic/IdentifierTable.h"
Douglas Gregor2ada0482009-02-04 17:27:36 +000024#include <vector>
Ted Kremenekce20e8f2008-05-20 00:43:19 +000025
Chris Lattner6d9a6852006-10-25 05:11:20 +000026using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000027
Chris Lattner9631e182009-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 Lattner88f70d62008-03-15 05:43:15 +000038//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +000039// Decl Allocation/Deallocation Method Implementations
40//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000041
Chris Lattner9631e182009-03-04 06:34:08 +000042
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000043TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +000044 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000045}
46
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000047NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
48 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000049 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000050}
51
Ted Kremenek78aa98f2008-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 Kremeneka08154d2008-05-24 15:09:56 +000056 this->~NamespaceDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +000057 C.Deallocate((void *)this);
Ted Kremenek78aa98f2008-05-20 04:49:55 +000058}
59
60
Chris Lattner5696e7b2008-06-17 18:05:57 +000061ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000062 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000063 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner5696e7b2008-06-17 18:05:57 +000064}
65
Daniel Dunbarb76b7452009-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 Lattnerbec41342008-04-22 18:39:57 +000080ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000081 SourceLocation L, IdentifierInfo *Id,
82 QualType T, StorageClass S,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000083 Expr *DefArg) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000084 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniana0befc02008-12-20 23:29:59 +000085}
86
87QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000088 if (const OriginalParmVarDecl *PVD =
89 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniana0befc02008-12-20 23:29:59 +000090 return PVD->OriginalType;
91 return getType();
Chris Lattner4b08ca82008-03-15 21:10:16 +000092}
93
Douglas Gregor31cf12c2009-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 Gregor5a80bd12009-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 Gregor8bd3c2e2009-02-02 23:39:07 +0000125OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +0000126 ASTContext &C, DeclContext *DC,
127 SourceLocation L, IdentifierInfo *Id,
128 QualType T, QualType OT, StorageClass S,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000129 Expr *DefArg) {
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000130 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +0000131}
132
Chris Lattnerbec41342008-04-22 18:39:57 +0000133FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000134 SourceLocation L,
Douglas Gregor92751d42008-11-17 22:58:34 +0000135 DeclarationName N, QualType T,
Chris Lattner5072bae2008-03-15 21:24:04 +0000136 StorageClass S, bool isInline,
Anders Carlssone0dd1d52009-05-14 21:46:00 +0000137 bool hasWrittenPrototype,
Steve Naroff22315692008-10-03 00:02:03 +0000138 SourceLocation TypeSpecStartLoc) {
Douglas Gregor739ef0c2009-02-25 16:33:18 +0000139 FunctionDecl *New
140 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
141 TypeSpecStartLoc);
Anders Carlssone0dd1d52009-05-14 21:46:00 +0000142 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor739ef0c2009-02-25 16:33:18 +0000143 return New;
Chris Lattner5072bae2008-03-15 21:24:04 +0000144}
145
Steve Naroff1d95e5a2008-10-10 01:28:17 +0000146BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000147 return new (C) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +0000148}
149
Douglas Gregor91f84212008-12-11 16:49:14 +0000150FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
151 IdentifierInfo *Id, QualType T, Expr *BW,
Steve Naroff5ec6ff72009-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 Lattneree1284a2008-03-16 00:16:02 +0000154}
155
Douglas Gregorf4d33272009-01-07 19:46:03 +0000156bool FieldDecl::isAnonymousStructOrUnion() const {
157 if (!isImplicit() || getDeclName())
158 return false;
159
Ted Kremenek8a286fb2009-07-17 17:50:17 +0000160 if (const RecordType *Record = getType()->getAsRecordType())
Douglas Gregorf4d33272009-01-07 19:46:03 +0000161 return Record->getDecl()->isAnonymousStructOrUnion();
162
163 return false;
164}
Chris Lattner5072bae2008-03-15 21:24:04 +0000165
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000166EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
167 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000168 IdentifierInfo *Id, QualType T,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000169 Expr *E, const llvm::APSInt &V) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000170 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnera7b32872008-03-15 06:12:44 +0000171}
172
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000173void EnumConstantDecl::Destroy(ASTContext& C) {
174 if (Init) Init->Destroy(C);
175 Decl::Destroy(C);
176}
177
Chris Lattnerbec41342008-04-22 18:39:57 +0000178TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000179 SourceLocation L,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000180 IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000181 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnera7b32872008-03-15 06:12:44 +0000182}
183
Chris Lattnerbec41342008-04-22 18:39:57 +0000184EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000185 IdentifierInfo *Id,
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000186 EnumDecl *PrevDecl) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000187 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000188 C.getTypeDeclType(Enum, PrevDecl);
189 return Enum;
Chris Lattnera7b32872008-03-15 06:12:44 +0000190}
191
Ted Kremenek123f0252008-09-02 20:13:32 +0000192void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek123f0252008-09-02 20:13:32 +0000193 Decl::Destroy(C);
194}
195
Douglas Gregor91f84212008-12-11 16:49:14 +0000196void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
197 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000198 IntegerType = NewType;
Douglas Gregordee1be82009-01-17 00:42:38 +0000199 TagDecl::completeDefinition();
Douglas Gregor91f84212008-12-11 16:49:14 +0000200}
201
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000202FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000203 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000204 StringLiteral *Str) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000205 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattneree1284a2008-03-16 00:16:02 +0000206}
207
Chris Lattnera7b32872008-03-15 06:12:44 +0000208//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000209// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000210//===----------------------------------------------------------------------===//
211
Douglas Gregor2ada0482009-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 Gregor85673582009-05-18 17:01:57 +0000226 if (const ClassTemplateSpecializationDecl *Spec
227 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
228 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Chris Lattnerc61089a2009-06-30 01:26:17 +0000229 PrintingPolicy Policy(getASTContext().getLangOptions());
Douglas Gregor85673582009-05-18 17:01:57 +0000230 std::string TemplateArgsStr
231 = TemplateSpecializationType::PrintTemplateArgumentList(
232 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000233 TemplateArgs.flat_size(),
234 Policy);
Douglas Gregor85673582009-05-18 17:01:57 +0000235 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
236 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-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 Gregor6e6ad602009-01-20 01:17:11 +0000257bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000258 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
259
Douglas Gregor889ceb72009-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 Gregor8b9ccca2008-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 Gregorad3f2fc2009-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 Naroffc4173fa2009-02-22 19:35:57 +0000279 // For method declarations, we keep track of redeclarations.
280 if (isa<ObjCMethodDecl>(this))
281 return false;
282
Douglas Gregor8b9ccca2008-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 Gregoreddf4332009-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 Gregor6e6ad602009-01-20 01:17:11 +0000298
Anders Carlsson6915bf62009-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 Kyrtzidis9e59b572008-11-09 23:41:00 +0000312//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000313// VarDecl Implementation
314//===----------------------------------------------------------------------===//
315
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000316VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
317 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes394ec982008-12-17 23:39:55 +0000318 SourceLocation TypeSpecStartLoc) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000319 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes394ec982008-12-17 23:39:55 +0000320}
321
322void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000323 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000324 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000325 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000326 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
327 Eval->~EvaluatedStmt();
328 C.Deallocate(Eval);
329 }
330 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000331 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000332 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000333}
334
335VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000336}
337
Argyrios Kyrtzidisa3aeb5a2009-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 Gregor0760fa12009-03-10 23:43:53 +0000344bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
345 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
346 return false;
347
Douglas Gregorbeecd582009-04-21 17:11:58 +0000348 const VarDecl *Def = 0;
349 return (!getDefinition(Def) &&
Douglas Gregor0760fa12009-03-10 23:43:53 +0000350 (getStorageClass() == None || getStorageClass() == Static));
351}
352
Ted Kremenekdfd72c22009-03-20 21:35:28 +0000353const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000354 redecl_iterator I = redecls_begin(), E = redecls_end();
355 while (I != E && !I->getInit())
356 ++I;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000357
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000358 if (I != E) {
359 Def = *I;
360 return I->getInit();
361 }
362 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000363}
364
Argyrios Kyrtzidis0736c5c2009-07-14 03:20:08 +0000365void VarDecl::setPreviousDeclaration(VarDecl *PrevDecl) {
366 if (PrevDecl) {
367 // Point to previous.
368 PreviousDeclaration.setPointer(PrevDecl);
369 PreviousDeclaration.setInt(0);
370
371 // First one will point to this one as latest.
372 VarDecl *First = PrevDecl->getFirstDeclaration();
373 assert(First->PreviousDeclaration.getInt() == 1 && "Expected first");
374 First->PreviousDeclaration.setPointer(this);
375 } else {
376 // This is first.
377 PreviousDeclaration.setPointer(this);
378 PreviousDeclaration.setInt(1);
379 }
380}
381
382VarDecl *VarDecl::getFirstDeclaration() {
383 VarDecl *First = this;
Argyrios Kyrtzidisef17c072009-07-14 03:19:57 +0000384 while (First->getPreviousDeclaration())
385 First = First->getPreviousDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000386
Argyrios Kyrtzidisef17c072009-07-14 03:19:57 +0000387 return First;
388}
389
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000390VarDecl *VarDecl::getCanonicalDecl() {
391 VarDecl *Var = this;
392 while (Var->getPreviousDeclaration())
393 Var = Var->getPreviousDeclaration();
394 return Var;
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000395}
396
Nuno Lopes394ec982008-12-17 23:39:55 +0000397//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000398// FunctionDecl Implementation
399//===----------------------------------------------------------------------===//
400
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000401void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000402 if (Body && Body.isOffset())
403 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000404
405 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
406 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000407
Steve Naroff13ae6f42009-01-27 21:25:57 +0000408 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000409
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000410 Decl::Destroy(C);
411}
412
413
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000414Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000415 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
416 if (I->Body) {
417 Definition = *I;
418 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000419 }
420 }
421
422 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000423}
424
Sebastian Redla7b98a72009-04-26 20:35:05 +0000425Stmt *FunctionDecl::getBodyIfAvailable() const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000426 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
427 if (I->Body && !I->Body.isOffset()) {
428 return I->Body.get(0);
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000429 }
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000430 }
431
432 return 0;
433}
434
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000435void FunctionDecl::setBody(Stmt *B) {
436 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000437 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000438 EndRangeLoc = B->getLocEnd();
439}
440
Douglas Gregore62c0a42009-02-24 01:23:02 +0000441bool FunctionDecl::isMain() const {
442 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
443 getIdentifier() && getIdentifier()->isStr("main");
444}
445
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000446bool FunctionDecl::isExternC(ASTContext &Context) const {
447 // In C, any non-static, non-overloadable function has external
448 // linkage.
449 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000450 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000451
452 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
453 DC = DC->getParent()) {
454 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
455 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000456 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000457 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000458
459 break;
460 }
461 }
462
463 return false;
464}
465
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000466bool FunctionDecl::isGlobal() const {
467 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
468 return Method->isStatic();
469
470 if (getStorageClass() == Static)
471 return false;
472
473 for (const DeclContext *DC = getDeclContext();
474 DC->isNamespace();
475 DC = DC->getParent()) {
476 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
477 if (!Namespace->getDeclName())
478 return false;
479 break;
480 }
481 }
482
483 return true;
484}
485
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000486/// \brief Returns a value indicating whether this function
487/// corresponds to a builtin function.
488///
489/// The function corresponds to a built-in function if it is
490/// declared at translation scope or within an extern "C" block and
491/// its name matches with the name of a builtin. The returned value
492/// will be 0 for functions that do not correspond to a builtin, a
493/// value of type \c Builtin::ID if in the target-independent range
494/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregore711f702009-02-14 18:57:46 +0000495unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
496 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
497 return 0;
498
499 unsigned BuiltinID = getIdentifier()->getBuiltinID();
500 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
501 return BuiltinID;
502
503 // This function has the name of a known C library
504 // function. Determine whether it actually refers to the C library
505 // function or whether it just has the same name.
506
Douglas Gregora908e7f2009-02-17 03:23:10 +0000507 // If this is a static function, it's not a builtin.
508 if (getStorageClass() == Static)
509 return 0;
510
Douglas Gregore711f702009-02-14 18:57:46 +0000511 // If this function is at translation-unit scope and we're not in
512 // C++, it refers to the C library function.
513 if (!Context.getLangOptions().CPlusPlus &&
514 getDeclContext()->isTranslationUnit())
515 return BuiltinID;
516
517 // If the function is in an extern "C" linkage specification and is
518 // not marked "overloadable", it's the real function.
519 if (isa<LinkageSpecDecl>(getDeclContext()) &&
520 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
521 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000522 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000523 return BuiltinID;
524
525 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000526 return 0;
527}
528
529
Chris Lattner47c0d002009-04-25 06:03:53 +0000530/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000531/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000532/// after it has been created.
533unsigned FunctionDecl::getNumParams() const {
Chris Lattnerdfd637f22009-04-25 05:56:45 +0000534 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000535 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000536 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000537 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattnerdfd637f22009-04-25 05:56:45 +0000538
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000539}
540
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000541void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
542 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000543 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000544 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000545
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000546 // Zero params -> null pointer.
547 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000548 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000549 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000550 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000551
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000552 // Update source range. The check below allows us to set EndRangeLoc before
553 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000554 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000555 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000556 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000557}
Chris Lattner41943152007-01-25 04:52:46 +0000558
Chris Lattner58258242008-04-10 02:22:51 +0000559/// getMinRequiredArguments - Returns the minimum number of arguments
560/// needed to call this function. This may be fewer than the number of
561/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000562/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000563unsigned FunctionDecl::getMinRequiredArguments() const {
564 unsigned NumRequiredArgs = getNumParams();
565 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000566 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000567 --NumRequiredArgs;
568
569 return NumRequiredArgs;
570}
571
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000572bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000573 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000574 return false;
575
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000576 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
577 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000578 return false;
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000579
580 return true;
581}
582
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000583bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
584 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000585 return false;
586
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000587 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
588 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000589 return true;
590
591 return false;
592}
593
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000594void
595FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis0736c5c2009-07-14 03:20:08 +0000596 if (PrevDecl) {
597 // Point to previous.
598 PreviousDeclaration.setPointer(PrevDecl);
599 PreviousDeclaration.setInt(0);
600
601 // First one will point to this one as latest.
602 FunctionDecl *First = PrevDecl->getFirstDeclaration();
603 assert(First->PreviousDeclaration.getInt() == 1 && "Expected first");
604 First->PreviousDeclaration.setPointer(this);
605 } else {
606 // This is first.
607 PreviousDeclaration.setPointer(this);
608 PreviousDeclaration.setInt(1);
609 }
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000610
611 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
612 FunctionTemplateDecl *PrevFunTmpl
613 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
614 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
615 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
616 }
617}
618
Argyrios Kyrtzidis0736c5c2009-07-14 03:20:08 +0000619FunctionDecl *FunctionDecl::getFirstDeclaration() {
620 FunctionDecl *First = this;
Argyrios Kyrtzidisef17c072009-07-14 03:19:57 +0000621 while (First->getPreviousDeclaration())
622 First = First->getPreviousDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000623
Argyrios Kyrtzidisef17c072009-07-14 03:19:57 +0000624 return First;
625}
626
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000627FunctionDecl *FunctionDecl::getCanonicalDecl() {
628 FunctionDecl *FD = this;
629 while (FD->getPreviousDeclaration())
630 FD = FD->getPreviousDeclaration();
631 return FD;
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000632}
633
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000634/// getOverloadedOperator - Which C++ overloaded operator this
635/// function represents, if any.
636OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000637 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
638 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000639 else
640 return OO_None;
641}
642
Douglas Gregor70d83e22009-06-29 17:30:29 +0000643FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
644 if (FunctionTemplateSpecializationInfo *Info
645 = TemplateOrSpecialization
646 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +0000647 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +0000648 }
649 return 0;
650}
651
652const TemplateArgumentList *
653FunctionDecl::getTemplateSpecializationArgs() const {
654 if (FunctionTemplateSpecializationInfo *Info
655 = TemplateOrSpecialization
656 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
657 return Info->TemplateArguments;
658 }
659 return 0;
660}
661
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000662void
663FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
664 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000665 const TemplateArgumentList *TemplateArgs,
666 void *InsertPos) {
Douglas Gregor70d83e22009-06-29 17:30:29 +0000667 FunctionTemplateSpecializationInfo *Info
668 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000669 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +0000670 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000671
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000672 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +0000673 Info->Template.setPointer(Template);
674 Info->Template.setInt(0); // Implicit instantiation, unless told otherwise
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000675 Info->TemplateArguments = TemplateArgs;
676 TemplateOrSpecialization = Info;
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000677
678 // Insert this function template specialization into the set of known
679 // function template specialiations.
680 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000681}
682
Douglas Gregore8925db2009-06-29 22:39:32 +0000683bool FunctionDecl::isExplicitSpecialization() const {
684 // FIXME: check this property for explicit specializations of member
685 // functions of class templates.
686 FunctionTemplateSpecializationInfo *Info
687 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
688 if (!Info)
689 return false;
690
691 return Info->isExplicitSpecialization();
692}
693
694void FunctionDecl::setExplicitSpecialization(bool ES) {
695 // FIXME: set this property for explicit specializations of member functions
696 // of class templates.
697 FunctionTemplateSpecializationInfo *Info
698 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
699 if (Info)
700 Info->setExplicitSpecialization(ES);
701}
702
Chris Lattner59a25942008-03-31 00:36:02 +0000703//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000704// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000705//===----------------------------------------------------------------------===//
706
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000707SourceRange TagDecl::getSourceRange() const {
708 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
709 return SourceRange(getLocation(), E);
710}
711
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000712TagDecl* TagDecl::getCanonicalDecl() {
713 Type *T = getTypeForDecl();
714 if (T == 0)
715 T = getASTContext().getTagDeclType(this).getTypePtr();
716
717 return cast<TagDecl>(cast<TagType>(T->getCanonicalTypeInternal())->getDecl());
718}
719
Douglas Gregordee1be82009-01-17 00:42:38 +0000720void TagDecl::startDefinition() {
Ted Kremenek8a286fb2009-07-17 17:50:17 +0000721 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
Douglas Gregord56a91e2009-02-26 22:19:44 +0000722 TagT->decl.setPointer(this);
Ted Kremenek8a286fb2009-07-17 17:50:17 +0000723 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregordee1be82009-01-17 00:42:38 +0000724}
725
726void TagDecl::completeDefinition() {
727 assert((!TypeForDecl ||
Ted Kremenek8a286fb2009-07-17 17:50:17 +0000728 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregordee1be82009-01-17 00:42:38 +0000729 "Attempt to redefine a tag definition?");
730 IsDefinition = true;
Ted Kremenek8a286fb2009-07-17 17:50:17 +0000731 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
Douglas Gregord56a91e2009-02-26 22:19:44 +0000732 TagT->decl.setPointer(this);
733 TagT->decl.setInt(0);
Douglas Gregordee1be82009-01-17 00:42:38 +0000734}
735
Ted Kremenek21475702008-09-05 17:16:31 +0000736TagDecl* TagDecl::getDefinition(ASTContext& C) const {
737 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Ted Kremenek8a286fb2009-07-17 17:50:17 +0000738 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek21475702008-09-05 17:16:31 +0000739 return D->isDefinition() ? D : 0;
740}
741
742//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000743// RecordDecl Implementation
744//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000745
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000746RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek47923c72008-09-05 01:34:33 +0000747 IdentifierInfo *Id)
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000748 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000749 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000750 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000751 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000752 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000753}
754
755RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000756 SourceLocation L, IdentifierInfo *Id,
757 RecordDecl* PrevDecl) {
Ted Kremenek47923c72008-09-05 01:34:33 +0000758
Steve Naroff13ae6f42009-01-27 21:25:57 +0000759 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek21475702008-09-05 17:16:31 +0000760 C.getTypeDeclType(R, PrevDecl);
761 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000762}
763
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000764RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000765}
766
767void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000768 TagDecl::Destroy(C);
769}
770
Douglas Gregordfcad112009-03-25 15:59:44 +0000771bool RecordDecl::isInjectedClassName() const {
772 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
773 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
774}
775
Douglas Gregor91f84212008-12-11 16:49:14 +0000776/// completeDefinition - Notes that the definition of this type is now
777/// complete.
778void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000779 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +0000780 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +0000781}
Steve Naroffcc321422007-03-26 23:09:51 +0000782
Steve Naroff415d3d52008-10-08 17:01:13 +0000783//===----------------------------------------------------------------------===//
784// BlockDecl Implementation
785//===----------------------------------------------------------------------===//
786
787BlockDecl::~BlockDecl() {
788}
789
790void BlockDecl::Destroy(ASTContext& C) {
791 if (Body)
792 Body->Destroy(C);
793
794 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
795 (*I)->Destroy(C);
Ted Kremenek6483bc22009-03-13 23:17:24 +0000796
797 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +0000798 Decl::Destroy(C);
799}
Steve Naroffc4b30e52009-03-13 16:56:44 +0000800
801void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
802 unsigned NParms) {
803 assert(ParamInfo == 0 && "Already has param info!");
804
805 // Zero params -> null pointer.
806 if (NParms) {
807 NumParams = NParms;
808 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
809 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
810 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
811 }
812}
813
814unsigned BlockDecl::getNumParams() const {
815 return NumParams;
816}