blob: 4e07b957a7d05f180fe254a0629bd5fece069303 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Argiris Kirtzidise7dfca12008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor7a7be652009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffd85ba922009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor9054f982009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnere4650482008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argiris Kirtzidisf1a75052009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopesc98406e2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000024#include "clang/Basic/IdentifierTable.h"
John McCallfd025182009-09-04 01:14:41 +000025#include "clang/Parse/DeclSpec.h"
26#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09be81b2009-02-04 17:27:36 +000027#include <vector>
Ted Kremenekafdf8112008-05-20 00:43:19 +000028
Chris Lattner4b009652007-07-25 00:24:17 +000029using namespace clang;
30
Chris Lattner16ded572009-03-04 06:34:08 +000031void Attr::Destroy(ASTContext &C) {
32 if (Next) {
33 Next->Destroy(C);
34 Next = 0;
35 }
36 this->~Attr();
37 C.Deallocate((void*)this);
38}
39
Argiris Kirtzidisf1a75052009-08-19 01:27:32 +000040/// \brief Return the TypeLoc wrapper for the type source info.
41TypeLoc DeclaratorInfo::getTypeLoc() const {
42 return TypeLoc::Create(Ty, (void*)(this + 1));
43}
Chris Lattner16ded572009-03-04 06:34:08 +000044
Chris Lattnera8344c32008-03-15 05:43:15 +000045//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000046// Decl Allocation/Deallocation Method Implementations
47//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000048
Chris Lattner16ded572009-03-04 06:34:08 +000049
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000050TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argiris Kirtzidis2a6dca12009-06-29 17:38:40 +000051 return new (C) TranslationUnitDecl(C);
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000052}
53
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000054NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
55 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000056 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000057}
58
Ted Kremenek5be49242008-05-20 04:49:55 +000059void NamespaceDecl::Destroy(ASTContext& C) {
60 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
61 // together. They are all top-level Decls.
62
Ted Kremenek0f433842008-05-24 15:09:56 +000063 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000064 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000065}
66
67
Chris Lattner8c7c6a12008-06-17 18:05:57 +000068ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000069 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000070 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000071}
72
Daniel Dunbarcaf78fb2009-04-14 02:08:49 +000073const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
74 switch (SC) {
75 case VarDecl::None: break;
76 case VarDecl::Auto: return "auto"; break;
77 case VarDecl::Extern: return "extern"; break;
78 case VarDecl::PrivateExtern: return "__private_extern__"; break;
79 case VarDecl::Register: return "register"; break;
80 case VarDecl::Static: return "static"; break;
81 }
82
83 assert(0 && "Invalid storage class");
84 return 0;
85}
86
Chris Lattneref87a202008-04-22 18:39:57 +000087ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000088 SourceLocation L, IdentifierInfo *Id,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +000089 QualType T, DeclaratorInfo *DInfo,
90 StorageClass S, Expr *DefArg) {
91 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, DInfo, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000092}
93
94QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000095 if (const OriginalParmVarDecl *PVD =
96 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000097 return PVD->OriginalType;
98 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000099}
100
Douglas Gregor4833ff02009-05-26 18:54:04 +0000101void VarDecl::setInit(ASTContext &C, Expr *I) {
102 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
103 Eval->~EvaluatedStmt();
104 C.Deallocate(Eval);
105 }
106
107 Init = I;
108 }
109
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000110bool VarDecl::isExternC(ASTContext &Context) const {
111 if (!Context.getLangOptions().CPlusPlus)
112 return (getDeclContext()->isTranslationUnit() &&
113 getStorageClass() != Static) ||
114 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
115
116 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
117 DC = DC->getParent()) {
118 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
119 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
120 return getStorageClass() != Static;
121
122 break;
123 }
124
125 if (DC->isFunctionOrMethod())
126 return false;
127 }
128
129 return false;
130}
131
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000132OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000133 ASTContext &C, DeclContext *DC,
134 SourceLocation L, IdentifierInfo *Id,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000135 QualType T, DeclaratorInfo *DInfo,
136 QualType OT, StorageClass S, Expr *DefArg) {
137 return new (C) OriginalParmVarDecl(DC, L, Id, T, DInfo, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000138}
139
Chris Lattneref87a202008-04-22 18:39:57 +0000140FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000141 SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000142 DeclarationName N, QualType T,
143 DeclaratorInfo *DInfo,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000144 StorageClass S, bool isInline,
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000145 bool hasWrittenPrototype) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000146 FunctionDecl *New
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000147 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssond9d6d502009-05-14 21:46:00 +0000148 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000149 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000150}
151
Steve Naroff52059382008-10-10 01:28:17 +0000152BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000153 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000154}
155
Douglas Gregor8acb7272008-12-11 16:49:14 +0000156FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000157 IdentifierInfo *Id, QualType T,
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000158 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
159 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +0000160}
161
Douglas Gregorc7f01612009-01-07 19:46:03 +0000162bool FieldDecl::isAnonymousStructOrUnion() const {
163 if (!isImplicit() || getDeclName())
164 return false;
165
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000166 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorc7f01612009-01-07 19:46:03 +0000167 return Record->getDecl()->isAnonymousStructOrUnion();
168
169 return false;
170}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000171
Chris Lattnereee57c02008-04-04 06:12:32 +0000172EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
173 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000174 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000175 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000176 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000177}
178
Ted Kremenek5be49242008-05-20 04:49:55 +0000179void EnumConstantDecl::Destroy(ASTContext& C) {
180 if (Init) Init->Destroy(C);
181 Decl::Destroy(C);
182}
183
Chris Lattneref87a202008-04-22 18:39:57 +0000184TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000185 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000186 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000187 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000188}
189
Chris Lattneref87a202008-04-22 18:39:57 +0000190EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000191 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorae644892008-12-15 16:32:14 +0000192 EnumDecl *PrevDecl) {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000193 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorae644892008-12-15 16:32:14 +0000194 C.getTypeDeclType(Enum, PrevDecl);
195 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000196}
197
Ted Kremenek25d8be12008-09-02 20:13:32 +0000198void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000199 Decl::Destroy(C);
200}
201
Douglas Gregor8acb7272008-12-11 16:49:14 +0000202void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
203 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000204 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000205 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000206}
207
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000208FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000209 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000210 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000211 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000212}
213
Chris Lattnere4650482008-03-15 06:12:44 +0000214//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000215// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000216//===----------------------------------------------------------------------===//
217
Douglas Gregor09be81b2009-02-04 17:27:36 +0000218std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2c087d52009-09-08 18:24:21 +0000219 return getQualifiedNameAsString(getASTContext().getLangOptions());
220}
221
222std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor09be81b2009-02-04 17:27:36 +0000223 std::vector<std::string> Names;
224 std::string QualName;
225 const DeclContext *Ctx = getDeclContext();
226
227 if (Ctx->isFunctionOrMethod())
228 return getNameAsString();
229
230 while (Ctx) {
231 if (Ctx->isFunctionOrMethod())
232 // FIXME: That probably will happen, when D was member of local
233 // scope class/struct/union. How do we handle this case?
234 break;
235
Douglas Gregorb12249d2009-05-18 17:01:57 +0000236 if (const ClassTemplateSpecializationDecl *Spec
237 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
238 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
239 std::string TemplateArgsStr
240 = TemplateSpecializationType::PrintTemplateArgumentList(
241 TemplateArgs.getFlatArgumentList(),
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000242 TemplateArgs.flat_size(),
Anders Carlsson2c087d52009-09-08 18:24:21 +0000243 P);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000244 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
245 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor09be81b2009-02-04 17:27:36 +0000246 Names.push_back(ND->getNameAsString());
247 else
248 break;
249
250 Ctx = Ctx->getParent();
251 }
252
253 std::vector<std::string>::reverse_iterator
254 I = Names.rbegin(),
255 End = Names.rend();
256
257 for (; I!=End; ++I)
258 QualName += *I + "::";
259
260 QualName += getNameAsString();
261
262 return QualName;
263}
264
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000265bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000266 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
267
Douglas Gregor7a7be652009-02-03 19:21:40 +0000268 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
269 // We want to keep it, unless it nominates same namespace.
270 if (getKind() == Decl::UsingDirective) {
271 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
272 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
273 }
274
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000275 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
276 // For function declarations, we keep track of redeclarations.
277 return FD->getPreviousDeclaration() == OldD;
278
Douglas Gregorb60eb752009-06-25 22:08:12 +0000279 // For function templates, the underlying function declarations are linked.
280 if (const FunctionTemplateDecl *FunctionTemplate
281 = dyn_cast<FunctionTemplateDecl>(this))
282 if (const FunctionTemplateDecl *OldFunctionTemplate
283 = dyn_cast<FunctionTemplateDecl>(OldD))
284 return FunctionTemplate->getTemplatedDecl()
285 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
286
Steve Naroffd85ba922009-02-22 19:35:57 +0000287 // For method declarations, we keep track of redeclarations.
288 if (isa<ObjCMethodDecl>(this))
289 return false;
290
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000291 // For non-function declarations, if the declarations are of the
292 // same kind then this must be a redeclaration, or semantic analysis
293 // would not have given us the new declaration.
294 return this->getKind() == OldD->getKind();
295}
296
Douglas Gregor1c52c632009-02-24 20:03:32 +0000297bool NamedDecl::hasLinkage() const {
298 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
299 return VD->hasExternalStorage() || VD->isFileVarDecl();
300
301 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
302 return true;
303
304 return false;
305}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000306
Anders Carlssondf5ab842009-06-26 06:29:23 +0000307NamedDecl *NamedDecl::getUnderlyingDecl() {
308 NamedDecl *ND = this;
309 while (true) {
310 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
311 ND = UD->getTargetDecl();
312 else if (ObjCCompatibleAliasDecl *AD
313 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
314 return AD->getClassInterface();
315 else
316 return ND;
317 }
318}
319
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000320//===----------------------------------------------------------------------===//
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000321// DeclaratorDecl Implementation
322//===----------------------------------------------------------------------===//
323
324SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
325 if (DeclInfo)
326 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
327 return SourceLocation();
328}
329
330//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000331// VarDecl Implementation
332//===----------------------------------------------------------------------===//
333
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000334VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000335 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argiris Kirtzidis42556e42009-08-21 00:31:54 +0000336 StorageClass S) {
337 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000338}
339
340void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000341 Expr *Init = getInit();
Douglas Gregor4833ff02009-05-26 18:54:04 +0000342 if (Init) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000343 Init->Destroy(C);
Douglas Gregor4833ff02009-05-26 18:54:04 +0000344 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
345 Eval->~EvaluatedStmt();
346 C.Deallocate(Eval);
347 }
348 }
Nuno Lopesc98406e2008-12-17 23:39:55 +0000349 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000350 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000351}
352
353VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000354}
355
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000356SourceRange VarDecl::getSourceRange() const {
357 if (getInit())
358 return SourceRange(getLocation(), getInit()->getLocEnd());
359 return SourceRange(getLocation(), getLocation());
360}
361
Douglas Gregor181fe792009-07-24 20:34:43 +0000362VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
363 return getASTContext().getInstantiatedFromStaticDataMember(this);
364}
365
Douglas Gregor2f728b22009-03-10 23:43:53 +0000366bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
367 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
368 return false;
369
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000370 const VarDecl *Def = 0;
371 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000372 (getStorageClass() == None || getStorageClass() == Static));
373}
374
Ted Kremenek51787c72009-03-20 21:35:28 +0000375const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000376 redecl_iterator I = redecls_begin(), E = redecls_end();
377 while (I != E && !I->getInit())
378 ++I;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000379
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000380 if (I != E) {
381 Def = *I;
382 return I->getInit();
383 }
384 return 0;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000385}
386
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000387VarDecl *VarDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000388 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000389}
390
Nuno Lopesc98406e2008-12-17 23:39:55 +0000391//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000392// FunctionDecl Implementation
393//===----------------------------------------------------------------------===//
394
Ted Kremenekafdf8112008-05-20 00:43:19 +0000395void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000396 if (Body && Body.isOffset())
397 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000398
399 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
400 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000401
Steve Naroff5abb0282009-01-27 21:25:57 +0000402 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000403
Ted Kremenekafdf8112008-05-20 00:43:19 +0000404 Decl::Destroy(C);
405}
406
407
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000408Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000409 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
410 if (I->Body) {
411 Definition = *I;
412 return I->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 Kirtzidis1d2f3a32009-07-14 03:20:21 +0000420 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
421 if (I->Body && !I->Body.isOffset()) {
422 return I->Body.get(0);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000423 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000424 }
425
426 return 0;
427}
428
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000429void FunctionDecl::setBody(Stmt *B) {
430 Body = B;
Argiris Kirtzidis25822a02009-06-22 17:13:31 +0000431 if (B)
Argiris Kirtzidis545473e2009-06-20 08:09:14 +0000432 EndRangeLoc = B->getLocEnd();
433}
434
John McCallcb6dd8a2009-08-15 02:09:25 +0000435bool FunctionDecl::isMain(ASTContext &Context) const {
436 return !Context.getLangOptions().Freestanding &&
437 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregoraf682022009-02-24 01:23:02 +0000438 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
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000571 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
572 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000573 return false;
Douglas Gregor67e11442009-04-28 06:37:30 +0000574
575 return true;
576}
577
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000578bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
579 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor67e11442009-04-28 06:37:30 +0000580 return false;
581
Argiris Kirtzidis1d2f3a32009-07-14 03:20:21 +0000582 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
583 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor67e11442009-04-28 06:37:30 +0000584 return true;
585
586 return false;
587}
588
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000589void
590FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argiris Kirtzidis4253ec52009-07-18 08:50:35 +0000591 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000592
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000593 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
594 FunctionTemplateDecl *PrevFunTmpl
595 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
596 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
597 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
598 }
599}
600
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000601FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argiris Kirtzidis80ad73b2009-07-18 08:50:13 +0000602 return getFirstDeclaration();
Argiris Kirtzidis5ea54f62009-07-05 22:21:56 +0000603}
604
Douglas Gregore60e5d32008-11-06 22:13:31 +0000605/// getOverloadedOperator - Which C++ overloaded operator this
606/// function represents, if any.
607OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000608 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
609 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000610 else
611 return OO_None;
612}
613
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000614FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
615 if (FunctionTemplateSpecializationInfo *Info
616 = TemplateOrSpecialization
617 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor69678062009-06-29 22:39:32 +0000618 return Info->Template.getPointer();
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000619 }
620 return 0;
621}
622
623const TemplateArgumentList *
624FunctionDecl::getTemplateSpecializationArgs() const {
625 if (FunctionTemplateSpecializationInfo *Info
626 = TemplateOrSpecialization
627 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
628 return Info->TemplateArguments;
629 }
630 return 0;
631}
632
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000633void
634FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
635 FunctionTemplateDecl *Template,
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000636 const TemplateArgumentList *TemplateArgs,
637 void *InsertPos) {
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000638 FunctionTemplateSpecializationInfo *Info
639 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000640 if (!Info)
Douglas Gregor2ed1a542009-06-29 17:30:29 +0000641 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000642
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000643 Info->Function = this;
Douglas Gregor69678062009-06-29 22:39:32 +0000644 Info->Template.setPointer(Template);
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000645 Info->Template.setInt(TSK_ImplicitInstantiation - 1);
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000646 Info->TemplateArguments = TemplateArgs;
647 TemplateOrSpecialization = Info;
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000648
649 // Insert this function template specialization into the set of known
650 // function template specialiations.
651 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000652}
653
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000654TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
655 // For a function template specialization, query the specialization
656 // information object.
Douglas Gregor69678062009-06-29 22:39:32 +0000657 FunctionTemplateSpecializationInfo *Info
658 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
659 if (Info)
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000660 return Info->getTemplateSpecializationKind();
661
662 if (!getInstantiatedFromMemberFunction())
663 return TSK_Undeclared;
664
665 // Find the class template specialization corresponding to this instantiation
666 // of a member function.
667 const DeclContext *Parent = getDeclContext();
668 while (Parent && !isa<ClassTemplateSpecializationDecl>(Parent))
669 Parent = Parent->getParent();
670
671 if (!Parent)
672 return TSK_Undeclared;
673
674 return cast<ClassTemplateSpecializationDecl>(Parent)->getSpecializationKind();
675}
676
677void
678FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
679 FunctionTemplateSpecializationInfo *Info
680 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
681 assert(Info && "Not a function template specialization");
682 Info->setTemplateSpecializationKind(TSK);
Douglas Gregor69678062009-06-29 22:39:32 +0000683}
684
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000685//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000686// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000687//===----------------------------------------------------------------------===//
688
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000689SourceRange TagDecl::getSourceRange() const {
690 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000691 return SourceRange(TagKeywordLoc, E);
Argiris Kirtzidisb929dcc2009-07-14 03:17:17 +0000692}
693
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000694TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000695 return getFirstDeclaration();
Argiris Kirtzidise3fb7852009-07-18 00:34:07 +0000696}
697
Douglas Gregor98b27542009-01-17 00:42:38 +0000698void TagDecl::startDefinition() {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000699 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
700 TagT->decl.setPointer(this);
701 TagT->decl.setInt(1);
702 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000703}
704
705void TagDecl::completeDefinition() {
Douglas Gregor98b27542009-01-17 00:42:38 +0000706 IsDefinition = true;
Douglas Gregor19e567a2009-07-29 23:36:44 +0000707 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
708 assert(TagT->decl.getPointer() == this &&
709 "Attempt to redefine a tag definition?");
710 TagT->decl.setInt(0);
711 }
Douglas Gregor98b27542009-01-17 00:42:38 +0000712}
713
Ted Kremenek46a837c2008-09-05 17:16:31 +0000714TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor19e567a2009-07-29 23:36:44 +0000715 if (isDefinition())
716 return const_cast<TagDecl *>(this);
717
Douglas Gregor19e567a2009-07-29 23:36:44 +0000718 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
719 R != REnd; ++R)
720 if (R->isDefinition())
721 return *R;
722
723 return 0;
Ted Kremenek46a837c2008-09-05 17:16:31 +0000724}
725
John McCallfd025182009-09-04 01:14:41 +0000726TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
727 switch (TypeSpec) {
728 default: llvm::llvm_unreachable("unexpected type specifier");
729 case DeclSpec::TST_struct: return TK_struct;
730 case DeclSpec::TST_class: return TK_class;
731 case DeclSpec::TST_union: return TK_union;
732 case DeclSpec::TST_enum: return TK_enum;
733 }
734}
735
Ted Kremenek46a837c2008-09-05 17:16:31 +0000736//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000737// RecordDecl Implementation
738//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000739
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000740RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor19e567a2009-07-29 23:36:44 +0000741 IdentifierInfo *Id, RecordDecl *PrevDecl,
742 SourceLocation TKL)
743 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000744 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000745 AnonymousStructOrUnion = false;
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000746 HasObjectMember = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000747 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000748}
749
750RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000751 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor9060d0e2009-07-21 14:46:17 +0000752 SourceLocation TKL, RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000753
Douglas Gregor19e567a2009-07-29 23:36:44 +0000754 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000755 C.getTypeDeclType(R, PrevDecl);
756 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000757}
758
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000759RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000760}
761
762void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000763 TagDecl::Destroy(C);
764}
765
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000766bool RecordDecl::isInjectedClassName() const {
767 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
768 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
769}
770
Douglas Gregor8acb7272008-12-11 16:49:14 +0000771/// completeDefinition - Notes that the definition of this type is now
772/// complete.
773void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000774 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000775 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000776}
777
Steve Naroff9ac456d2008-10-08 17:01:13 +0000778//===----------------------------------------------------------------------===//
779// BlockDecl Implementation
780//===----------------------------------------------------------------------===//
781
782BlockDecl::~BlockDecl() {
783}
784
785void BlockDecl::Destroy(ASTContext& C) {
786 if (Body)
787 Body->Destroy(C);
788
789 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
790 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000791
792 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000793 Decl::Destroy(C);
794}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000795
796void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
797 unsigned NParms) {
798 assert(ParamInfo == 0 && "Already has param info!");
799
800 // Zero params -> null pointer.
801 if (NParms) {
802 NumParams = NParms;
803 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
804 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
805 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
806 }
807}
808
809unsigned BlockDecl::getNumParams() const {
810 return NumParams;
811}