blob: ca1ae63ff85fafbc4e42f0296e00d1857c83de41 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000024#include "clang/Basic/IdentifierTable.h"
John McCallf1bbbb42009-09-04 01:14:41 +000025#include "clang/Parse/DeclSpec.h"
26#include "llvm/Support/ErrorHandling.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000027#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattner0b2b6e12009-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
Argyrios Kyrtzidisb17166c2009-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 Lattner0b2b6e12009-03-04 06:34:08 +000044
Chris Lattnerd3b90652008-03-15 05:43:15 +000045//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000046// Decl Allocation/Deallocation Method Implementations
47//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000048
Chris Lattner0b2b6e12009-03-04 06:34:08 +000049
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000050TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +000051 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000052}
53
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000054NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
55 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000056 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000057}
58
Ted Kremenekd1ac17a2008-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 Kremenekebf27b12008-05-24 15:09:56 +000063 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000065}
66
67
Chris Lattner41110242008-06-17 18:05:57 +000068ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000069 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000070 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000071}
72
Daniel Dunbarb286a782009-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 Lattner9fdf9c62008-04-22 18:39:57 +000087ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000088 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-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 Jahanian4306d3c2008-12-20 23:29:59 +000092}
93
94QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor64650af2009-02-02 23:39:07 +000095 if (const OriginalParmVarDecl *PVD =
96 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000097 return PVD->OriginalType;
98 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000099}
100
Douglas Gregor78d15832009-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 Gregor63935192009-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 Gregor64650af2009-02-02 23:39:07 +0000132OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000133 ASTContext &C, DeclContext *DC,
134 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-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 Jahanian73da9e42008-12-20 20:56:12 +0000138}
139
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000140FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000141 SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000142 DeclarationName N, QualType T,
143 DeclaratorInfo *DInfo,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000144 StorageClass S, bool isInline,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000145 bool hasWrittenPrototype) {
Douglas Gregor2224f842009-02-25 16:33:18 +0000146 FunctionDecl *New
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000147 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssona75e8532009-05-14 21:46:00 +0000148 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor2224f842009-02-25 16:33:18 +0000149 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000150}
151
Steve Naroff090276f2008-10-10 01:28:17 +0000152BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000153 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000154}
155
Douglas Gregor44b43212008-12-11 16:49:14 +0000156FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000157 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidisa5d82002009-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 Lattner8e25d862008-03-16 00:16:02 +0000160}
161
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000162bool FieldDecl::isAnonymousStructOrUnion() const {
163 if (!isImplicit() || getDeclName())
164 return false;
165
Ted Kremenek6217b802009-07-29 21:53:49 +0000166 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000167 return Record->getDecl()->isAnonymousStructOrUnion();
168
169 return false;
170}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000171
Chris Lattner0ed844b2008-04-04 06:12:32 +0000172EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
173 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000174 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000175 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000176 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000177}
178
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000179void EnumConstantDecl::Destroy(ASTContext& C) {
180 if (Init) Init->Destroy(C);
181 Decl::Destroy(C);
182}
183
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000184TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000185 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000186 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000187 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000188}
189
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000190EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000191 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000192 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000193 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000194 C.getTypeDeclType(Enum, PrevDecl);
195 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000196}
197
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000198void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000199 Decl::Destroy(C);
200}
201
Douglas Gregor44b43212008-12-11 16:49:14 +0000202void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
203 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000204 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000205 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000206}
207
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000208FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000209 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000210 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000211 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000212}
213
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000214//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000215// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000216//===----------------------------------------------------------------------===//
217
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000218std::string NamedDecl::getQualifiedNameAsString() const {
219 std::vector<std::string> Names;
220 std::string QualName;
221 const DeclContext *Ctx = getDeclContext();
222
223 if (Ctx->isFunctionOrMethod())
224 return getNameAsString();
225
226 while (Ctx) {
227 if (Ctx->isFunctionOrMethod())
228 // FIXME: That probably will happen, when D was member of local
229 // scope class/struct/union. How do we handle this case?
230 break;
231
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000232 if (const ClassTemplateSpecializationDecl *Spec
233 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
234 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Chris Lattnere4f21422009-06-30 01:26:17 +0000235 PrintingPolicy Policy(getASTContext().getLangOptions());
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000236 std::string TemplateArgsStr
237 = TemplateSpecializationType::PrintTemplateArgumentList(
238 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000239 TemplateArgs.flat_size(),
240 Policy);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000241 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
242 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000243 Names.push_back(ND->getNameAsString());
244 else
245 break;
246
247 Ctx = Ctx->getParent();
248 }
249
250 std::vector<std::string>::reverse_iterator
251 I = Names.rbegin(),
252 End = Names.rend();
253
254 for (; I!=End; ++I)
255 QualName += *I + "::";
256
257 QualName += getNameAsString();
258
259 return QualName;
260}
261
262
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000263bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000264 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
265
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000266 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
267 // We want to keep it, unless it nominates same namespace.
268 if (getKind() == Decl::UsingDirective) {
269 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
270 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
271 }
272
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000273 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
274 // For function declarations, we keep track of redeclarations.
275 return FD->getPreviousDeclaration() == OldD;
276
Douglas Gregore53060f2009-06-25 22:08:12 +0000277 // For function templates, the underlying function declarations are linked.
278 if (const FunctionTemplateDecl *FunctionTemplate
279 = dyn_cast<FunctionTemplateDecl>(this))
280 if (const FunctionTemplateDecl *OldFunctionTemplate
281 = dyn_cast<FunctionTemplateDecl>(OldD))
282 return FunctionTemplate->getTemplatedDecl()
283 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
284
Steve Naroff0de21fd2009-02-22 19:35:57 +0000285 // For method declarations, we keep track of redeclarations.
286 if (isa<ObjCMethodDecl>(this))
287 return false;
288
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000289 // For non-function declarations, if the declarations are of the
290 // same kind then this must be a redeclaration, or semantic analysis
291 // would not have given us the new declaration.
292 return this->getKind() == OldD->getKind();
293}
294
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000295bool NamedDecl::hasLinkage() const {
296 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
297 return VD->hasExternalStorage() || VD->isFileVarDecl();
298
299 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
300 return true;
301
302 return false;
303}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000304
Anders Carlssone136e0e2009-06-26 06:29:23 +0000305NamedDecl *NamedDecl::getUnderlyingDecl() {
306 NamedDecl *ND = this;
307 while (true) {
308 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
309 ND = UD->getTargetDecl();
310 else if (ObjCCompatibleAliasDecl *AD
311 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
312 return AD->getClassInterface();
313 else
314 return ND;
315 }
316}
317
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000318//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000319// DeclaratorDecl Implementation
320//===----------------------------------------------------------------------===//
321
322SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
323 if (DeclInfo)
324 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
325 return SourceLocation();
326}
327
328//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000329// VarDecl Implementation
330//===----------------------------------------------------------------------===//
331
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000332VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000333 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000334 StorageClass S) {
335 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000336}
337
338void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000339 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000340 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000341 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000342 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
343 Eval->~EvaluatedStmt();
344 C.Deallocate(Eval);
345 }
346 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000347 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000348 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000349}
350
351VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000352}
353
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000354SourceRange VarDecl::getSourceRange() const {
355 if (getInit())
356 return SourceRange(getLocation(), getInit()->getLocEnd());
357 return SourceRange(getLocation(), getLocation());
358}
359
Douglas Gregor7caa6822009-07-24 20:34:43 +0000360VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
361 return getASTContext().getInstantiatedFromStaticDataMember(this);
362}
363
Douglas Gregor275a3692009-03-10 23:43:53 +0000364bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
365 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
366 return false;
367
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000368 const VarDecl *Def = 0;
369 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000370 (getStorageClass() == None || getStorageClass() == Static));
371}
372
Ted Kremenek082d9362009-03-20 21:35:28 +0000373const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000374 redecl_iterator I = redecls_begin(), E = redecls_end();
375 while (I != E && !I->getInit())
376 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000377
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000378 if (I != E) {
379 Def = *I;
380 return I->getInit();
381 }
382 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000383}
384
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000385VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000386 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000387}
388
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000389//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000390// FunctionDecl Implementation
391//===----------------------------------------------------------------------===//
392
Ted Kremenek27f8a282008-05-20 00:43:19 +0000393void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000394 if (Body && Body.isOffset())
395 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000396
397 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
398 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000399
Steve Naroff3e970492009-01-27 21:25:57 +0000400 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000401
Ted Kremenek27f8a282008-05-20 00:43:19 +0000402 Decl::Destroy(C);
403}
404
405
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000406Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000407 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
408 if (I->Body) {
409 Definition = *I;
410 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000411 }
412 }
413
414 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000415}
416
Sebastian Redld3a413d2009-04-26 20:35:05 +0000417Stmt *FunctionDecl::getBodyIfAvailable() const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000418 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
419 if (I->Body && !I->Body.isOffset()) {
420 return I->Body.get(0);
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000421 }
Douglas Gregor72971342009-04-18 00:02:19 +0000422 }
423
424 return 0;
425}
426
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000427void FunctionDecl::setBody(Stmt *B) {
428 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000429 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000430 EndRangeLoc = B->getLocEnd();
431}
432
John McCall07a5c222009-08-15 02:09:25 +0000433bool FunctionDecl::isMain(ASTContext &Context) const {
434 return !Context.getLangOptions().Freestanding &&
435 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000436 getIdentifier() && getIdentifier()->isStr("main");
437}
438
Douglas Gregor63935192009-03-02 00:19:53 +0000439bool FunctionDecl::isExternC(ASTContext &Context) const {
440 // In C, any non-static, non-overloadable function has external
441 // linkage.
442 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000443 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000444
445 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
446 DC = DC->getParent()) {
447 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
448 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000449 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000450 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000451
452 break;
453 }
454 }
455
456 return false;
457}
458
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000459bool FunctionDecl::isGlobal() const {
460 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
461 return Method->isStatic();
462
463 if (getStorageClass() == Static)
464 return false;
465
466 for (const DeclContext *DC = getDeclContext();
467 DC->isNamespace();
468 DC = DC->getParent()) {
469 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
470 if (!Namespace->getDeclName())
471 return false;
472 break;
473 }
474 }
475
476 return true;
477}
478
Douglas Gregor3e41d602009-02-13 23:20:09 +0000479/// \brief Returns a value indicating whether this function
480/// corresponds to a builtin function.
481///
482/// The function corresponds to a built-in function if it is
483/// declared at translation scope or within an extern "C" block and
484/// its name matches with the name of a builtin. The returned value
485/// will be 0 for functions that do not correspond to a builtin, a
486/// value of type \c Builtin::ID if in the target-independent range
487/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000488unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
489 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
490 return 0;
491
492 unsigned BuiltinID = getIdentifier()->getBuiltinID();
493 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
494 return BuiltinID;
495
496 // This function has the name of a known C library
497 // function. Determine whether it actually refers to the C library
498 // function or whether it just has the same name.
499
Douglas Gregor9add3172009-02-17 03:23:10 +0000500 // If this is a static function, it's not a builtin.
501 if (getStorageClass() == Static)
502 return 0;
503
Douglas Gregor3c385e52009-02-14 18:57:46 +0000504 // If this function is at translation-unit scope and we're not in
505 // C++, it refers to the C library function.
506 if (!Context.getLangOptions().CPlusPlus &&
507 getDeclContext()->isTranslationUnit())
508 return BuiltinID;
509
510 // If the function is in an extern "C" linkage specification and is
511 // not marked "overloadable", it's the real function.
512 if (isa<LinkageSpecDecl>(getDeclContext()) &&
513 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
514 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000515 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000516 return BuiltinID;
517
518 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000519 return 0;
520}
521
522
Chris Lattner1ad9b282009-04-25 06:03:53 +0000523/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000524/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000525/// after it has been created.
526unsigned FunctionDecl::getNumParams() const {
Chris Lattner11ddb7d2009-04-25 05:56:45 +0000527 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000528 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000529 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000530 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner11ddb7d2009-04-25 05:56:45 +0000531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532}
533
Ted Kremenekfc767612009-01-14 00:42:25 +0000534void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
535 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000537 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000538
539 // Zero params -> null pointer.
540 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000541 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000542 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000544
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000545 // Update source range. The check below allows us to set EndRangeLoc before
546 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000547 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000548 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 }
550}
551
Chris Lattner8123a952008-04-10 02:22:51 +0000552/// getMinRequiredArguments - Returns the minimum number of arguments
553/// needed to call this function. This may be fewer than the number of
554/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000555/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000556unsigned FunctionDecl::getMinRequiredArguments() const {
557 unsigned NumRequiredArgs = getNumParams();
558 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000559 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000560 --NumRequiredArgs;
561
562 return NumRequiredArgs;
563}
564
Douglas Gregor68584ed2009-06-18 16:11:24 +0000565bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000566 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000567 return false;
568
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000569 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
570 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000571 return false;
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000572
573 return true;
574}
575
Douglas Gregor68584ed2009-06-18 16:11:24 +0000576bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
577 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000578 return false;
579
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000580 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
581 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000582 return true;
583
584 return false;
585}
586
Douglas Gregor127102b2009-06-29 20:59:39 +0000587void
588FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000589 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000590
Douglas Gregor127102b2009-06-29 20:59:39 +0000591 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
592 FunctionTemplateDecl *PrevFunTmpl
593 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
594 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
595 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
596 }
597}
598
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000599FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000600 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000601}
602
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000603/// getOverloadedOperator - Which C++ overloaded operator this
604/// function represents, if any.
605OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000606 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
607 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000608 else
609 return OO_None;
610}
611
Douglas Gregor16e8be22009-06-29 17:30:29 +0000612FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
613 if (FunctionTemplateSpecializationInfo *Info
614 = TemplateOrSpecialization
615 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000616 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000617 }
618 return 0;
619}
620
621const TemplateArgumentList *
622FunctionDecl::getTemplateSpecializationArgs() const {
623 if (FunctionTemplateSpecializationInfo *Info
624 = TemplateOrSpecialization
625 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
626 return Info->TemplateArguments;
627 }
628 return 0;
629}
630
Douglas Gregor1637be72009-06-26 00:10:03 +0000631void
632FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
633 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000634 const TemplateArgumentList *TemplateArgs,
635 void *InsertPos) {
Douglas Gregor16e8be22009-06-29 17:30:29 +0000636 FunctionTemplateSpecializationInfo *Info
637 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000638 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000639 Info = new (Context) FunctionTemplateSpecializationInfo;
Douglas Gregor1637be72009-06-26 00:10:03 +0000640
Douglas Gregor127102b2009-06-29 20:59:39 +0000641 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000642 Info->Template.setPointer(Template);
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000643 Info->Template.setInt(TSK_ImplicitInstantiation - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +0000644 Info->TemplateArguments = TemplateArgs;
645 TemplateOrSpecialization = Info;
Douglas Gregor127102b2009-06-29 20:59:39 +0000646
647 // Insert this function template specialization into the set of known
648 // function template specialiations.
649 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +0000650}
651
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000652TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
653 // For a function template specialization, query the specialization
654 // information object.
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000655 FunctionTemplateSpecializationInfo *Info
656 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
657 if (Info)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000658 return Info->getTemplateSpecializationKind();
659
660 if (!getInstantiatedFromMemberFunction())
661 return TSK_Undeclared;
662
663 // Find the class template specialization corresponding to this instantiation
664 // of a member function.
665 const DeclContext *Parent = getDeclContext();
666 while (Parent && !isa<ClassTemplateSpecializationDecl>(Parent))
667 Parent = Parent->getParent();
668
669 if (!Parent)
670 return TSK_Undeclared;
671
672 return cast<ClassTemplateSpecializationDecl>(Parent)->getSpecializationKind();
673}
674
675void
676FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
677 FunctionTemplateSpecializationInfo *Info
678 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
679 assert(Info && "Not a function template specialization");
680 Info->setTemplateSpecializationKind(TSK);
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000681}
682
Chris Lattner8a934232008-03-31 00:36:02 +0000683//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000684// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000685//===----------------------------------------------------------------------===//
686
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000687SourceRange TagDecl::getSourceRange() const {
688 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000689 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000690}
691
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000692TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000693 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000694}
695
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000696void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000697 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
698 TagT->decl.setPointer(this);
699 TagT->decl.setInt(1);
700 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000701}
702
703void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000704 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000705 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
706 assert(TagT->decl.getPointer() == this &&
707 "Attempt to redefine a tag definition?");
708 TagT->decl.setInt(0);
709 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000710}
711
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000712TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000713 if (isDefinition())
714 return const_cast<TagDecl *>(this);
715
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000716 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
717 R != REnd; ++R)
718 if (R->isDefinition())
719 return *R;
720
721 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000722}
723
John McCallf1bbbb42009-09-04 01:14:41 +0000724TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
725 switch (TypeSpec) {
726 default: llvm::llvm_unreachable("unexpected type specifier");
727 case DeclSpec::TST_struct: return TK_struct;
728 case DeclSpec::TST_class: return TK_class;
729 case DeclSpec::TST_union: return TK_union;
730 case DeclSpec::TST_enum: return TK_enum;
731 }
732}
733
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000734//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000735// RecordDecl Implementation
736//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000737
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000738RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000739 IdentifierInfo *Id, RecordDecl *PrevDecl,
740 SourceLocation TKL)
741 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +0000742 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000743 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000744 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000745 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000746}
747
748RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000749 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000750 SourceLocation TKL, RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000751
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000752 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000753 C.getTypeDeclType(R, PrevDecl);
754 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000755}
756
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000757RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000758}
759
760void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000761 TagDecl::Destroy(C);
762}
763
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000764bool RecordDecl::isInjectedClassName() const {
765 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
766 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
767}
768
Douglas Gregor44b43212008-12-11 16:49:14 +0000769/// completeDefinition - Notes that the definition of this type is now
770/// complete.
771void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000773 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000774}
775
Steve Naroff56ee6892008-10-08 17:01:13 +0000776//===----------------------------------------------------------------------===//
777// BlockDecl Implementation
778//===----------------------------------------------------------------------===//
779
780BlockDecl::~BlockDecl() {
781}
782
783void BlockDecl::Destroy(ASTContext& C) {
784 if (Body)
785 Body->Destroy(C);
786
787 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
788 (*I)->Destroy(C);
Ted Kremenek879d27a2009-03-13 23:17:24 +0000789
790 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000791 Decl::Destroy(C);
792}
Steve Naroffe78b8092009-03-13 16:56:44 +0000793
794void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
795 unsigned NParms) {
796 assert(ParamInfo == 0 && "Already has param info!");
797
798 // Zero params -> null pointer.
799 if (NParms) {
800 NumParams = NParms;
801 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
802 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
803 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
804 }
805}
806
807unsigned BlockDecl::getNumParams() const {
808 return NumParams;
809}