blob: ab654920238735badf9e3a56f62ecd8798ec93ff [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"
Chris Lattnere4650482008-03-15 06:12:44 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Stmt.h"
Nuno Lopesc98406e2008-12-17 23:39:55 +000019#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/Basic/IdentifierTable.h"
Douglas Gregor09be81b2009-02-04 17:27:36 +000021#include <vector>
Ted Kremenekafdf8112008-05-20 00:43:19 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
Chris Lattnera8344c32008-03-15 05:43:15 +000025//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000026// Decl Allocation/Deallocation Method Implementations
27//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000028
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000029TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff5abb0282009-01-27 21:25:57 +000030 return new (C) TranslationUnitDecl();
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000031}
32
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000033NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
34 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000035 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000036}
37
Ted Kremenek5be49242008-05-20 04:49:55 +000038void NamespaceDecl::Destroy(ASTContext& C) {
39 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
40 // together. They are all top-level Decls.
41
Ted Kremenek0f433842008-05-24 15:09:56 +000042 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000043 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000044}
45
46
Chris Lattner8c7c6a12008-06-17 18:05:57 +000047ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000048 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000049 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000050}
51
Chris Lattneref87a202008-04-22 18:39:57 +000052ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000053 SourceLocation L, IdentifierInfo *Id,
54 QualType T, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000055 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000056 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000057}
58
59QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000060 if (const OriginalParmVarDecl *PVD =
61 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000062 return PVD->OriginalType;
63 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000064}
65
Douglas Gregor469fc9a2009-02-02 23:39:07 +000066OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +000067 ASTContext &C, DeclContext *DC,
68 SourceLocation L, IdentifierInfo *Id,
69 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000070 Expr *DefArg) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000071 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +000072}
73
Chris Lattneref87a202008-04-22 18:39:57 +000074FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000075 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +000076 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +000077 StorageClass S, bool isInline,
Douglas Gregor1f88aa72009-02-25 16:33:18 +000078 bool hasPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +000079 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +000080 FunctionDecl *New
81 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
82 TypeSpecStartLoc);
83 New->HasPrototype = hasPrototype;
84 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +000085}
86
Steve Naroff52059382008-10-10 01:28:17 +000087BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +000088 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +000089}
90
Douglas Gregor8acb7272008-12-11 16:49:14 +000091FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
92 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000093 bool Mutable) {
Steve Naroff5abb0282009-01-27 21:25:57 +000094 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +000095}
96
Douglas Gregorc7f01612009-01-07 19:46:03 +000097bool FieldDecl::isAnonymousStructOrUnion() const {
98 if (!isImplicit() || getDeclName())
99 return false;
100
101 if (const RecordType *Record = getType()->getAsRecordType())
102 return Record->getDecl()->isAnonymousStructOrUnion();
103
104 return false;
105}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000106
Chris Lattnereee57c02008-04-04 06:12:32 +0000107EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
108 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000109 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000110 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000111 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000112}
113
Ted Kremenek5be49242008-05-20 04:49:55 +0000114void EnumConstantDecl::Destroy(ASTContext& C) {
115 if (Init) Init->Destroy(C);
116 Decl::Destroy(C);
117}
118
Chris Lattneref87a202008-04-22 18:39:57 +0000119TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000120 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000121 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000122 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000123}
124
Chris Lattneref87a202008-04-22 18:39:57 +0000125EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000126 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000127 EnumDecl *PrevDecl) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000128 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000129 C.getTypeDeclType(Enum, PrevDecl);
130 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000131}
132
Ted Kremenek25d8be12008-09-02 20:13:32 +0000133void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000134 Decl::Destroy(C);
135}
136
Douglas Gregor8acb7272008-12-11 16:49:14 +0000137void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
138 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000139 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000140 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000141}
142
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000143FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000144 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000145 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000146 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000147}
148
Chris Lattnere4650482008-03-15 06:12:44 +0000149//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000150// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000151//===----------------------------------------------------------------------===//
152
Douglas Gregor09be81b2009-02-04 17:27:36 +0000153std::string NamedDecl::getQualifiedNameAsString() const {
154 std::vector<std::string> Names;
155 std::string QualName;
156 const DeclContext *Ctx = getDeclContext();
157
158 if (Ctx->isFunctionOrMethod())
159 return getNameAsString();
160
161 while (Ctx) {
162 if (Ctx->isFunctionOrMethod())
163 // FIXME: That probably will happen, when D was member of local
164 // scope class/struct/union. How do we handle this case?
165 break;
166
167 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
168 Names.push_back(ND->getNameAsString());
169 else
170 break;
171
172 Ctx = Ctx->getParent();
173 }
174
175 std::vector<std::string>::reverse_iterator
176 I = Names.rbegin(),
177 End = Names.rend();
178
179 for (; I!=End; ++I)
180 QualName += *I + "::";
181
182 QualName += getNameAsString();
183
184 return QualName;
185}
186
187
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000188bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000189 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
190
Douglas Gregor7a7be652009-02-03 19:21:40 +0000191 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
192 // We want to keep it, unless it nominates same namespace.
193 if (getKind() == Decl::UsingDirective) {
194 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
195 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
196 }
197
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000198 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
199 // For function declarations, we keep track of redeclarations.
200 return FD->getPreviousDeclaration() == OldD;
201
Steve Naroffd85ba922009-02-22 19:35:57 +0000202 // For method declarations, we keep track of redeclarations.
203 if (isa<ObjCMethodDecl>(this))
204 return false;
205
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000206 // For non-function declarations, if the declarations are of the
207 // same kind then this must be a redeclaration, or semantic analysis
208 // would not have given us the new declaration.
209 return this->getKind() == OldD->getKind();
210}
211
Douglas Gregor1c52c632009-02-24 20:03:32 +0000212bool NamedDecl::hasLinkage() const {
213 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
214 return VD->hasExternalStorage() || VD->isFileVarDecl();
215
216 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
217 return true;
218
219 return false;
220}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000221
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000222//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000223// VarDecl Implementation
224//===----------------------------------------------------------------------===//
225
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000226VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
227 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000228 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000229 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000230}
231
232void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000233 Expr *Init = getInit();
234 if (Init)
235 Init->Destroy(C);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000236 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000237 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000238}
239
240VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000241}
242
243//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000244// FunctionDecl Implementation
245//===----------------------------------------------------------------------===//
246
Ted Kremenekafdf8112008-05-20 00:43:19 +0000247void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000248 if (Body)
249 Body->Destroy(C);
250
251 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
252 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000253
Steve Naroff5abb0282009-01-27 21:25:57 +0000254 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000255
Ted Kremenekafdf8112008-05-20 00:43:19 +0000256 Decl::Destroy(C);
257}
258
259
Douglas Gregor42214c52008-04-21 02:02:58 +0000260Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
261 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
262 if (FD->Body) {
263 Definition = FD;
264 return FD->Body;
265 }
266 }
267
268 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000269}
270
Douglas Gregoraf682022009-02-24 01:23:02 +0000271bool FunctionDecl::isMain() const {
272 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
273 getIdentifier() && getIdentifier()->isStr("main");
274}
275
Douglas Gregor411889e2009-02-13 23:20:09 +0000276/// \brief Returns a value indicating whether this function
277/// corresponds to a builtin function.
278///
279/// The function corresponds to a built-in function if it is
280/// declared at translation scope or within an extern "C" block and
281/// its name matches with the name of a builtin. The returned value
282/// will be 0 for functions that do not correspond to a builtin, a
283/// value of type \c Builtin::ID if in the target-independent range
284/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000285unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
286 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
287 return 0;
288
289 unsigned BuiltinID = getIdentifier()->getBuiltinID();
290 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
291 return BuiltinID;
292
293 // This function has the name of a known C library
294 // function. Determine whether it actually refers to the C library
295 // function or whether it just has the same name.
296
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000297 // If this is a static function, it's not a builtin.
298 if (getStorageClass() == Static)
299 return 0;
300
Douglas Gregorb5af7382009-02-14 18:57:46 +0000301 // If this function is at translation-unit scope and we're not in
302 // C++, it refers to the C library function.
303 if (!Context.getLangOptions().CPlusPlus &&
304 getDeclContext()->isTranslationUnit())
305 return BuiltinID;
306
307 // If the function is in an extern "C" linkage specification and is
308 // not marked "overloadable", it's the real function.
309 if (isa<LinkageSpecDecl>(getDeclContext()) &&
310 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
311 == LinkageSpecDecl::lang_c &&
312 !getAttr<OverloadableAttr>())
313 return BuiltinID;
314
315 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000316 return 0;
317}
318
319
Ted Kremeneka6ded352008-10-29 18:41:34 +0000320// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
321static unsigned getNumTypeParams(QualType T) {
322 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnere8733592008-04-06 23:09:52 +0000323 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000324 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000325 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000326}
327
Ted Kremeneka6ded352008-10-29 18:41:34 +0000328unsigned FunctionDecl::getNumParams() const {
329 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
330 if (!ParamInfo)
331 return 0;
332
333 return getNumTypeParams(getType());
334}
335
Ted Kremenek8494c962009-01-14 00:42:25 +0000336void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
337 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000338 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000339 assert(NumParams == getNumTypeParams(getType()) &&
340 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000341
342 // Zero params -> null pointer.
343 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000344 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000345 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000346 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
347 }
348}
349
Chris Lattner97316c02008-04-10 02:22:51 +0000350/// getMinRequiredArguments - Returns the minimum number of arguments
351/// needed to call this function. This may be fewer than the number of
352/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000353/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000354unsigned FunctionDecl::getMinRequiredArguments() const {
355 unsigned NumRequiredArgs = getNumParams();
356 while (NumRequiredArgs > 0
357 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
358 --NumRequiredArgs;
359
360 return NumRequiredArgs;
361}
362
Douglas Gregore60e5d32008-11-06 22:13:31 +0000363/// getOverloadedOperator - Which C++ overloaded operator this
364/// function represents, if any.
365OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000366 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
367 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000368 else
369 return OO_None;
370}
371
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000372//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000373// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000374//===----------------------------------------------------------------------===//
375
Douglas Gregor98b27542009-01-17 00:42:38 +0000376void TagDecl::startDefinition() {
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000377 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
378 TagT->decl.setPointer(this);
379 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor98b27542009-01-17 00:42:38 +0000380}
381
382void TagDecl::completeDefinition() {
383 assert((!TypeForDecl ||
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000384 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor98b27542009-01-17 00:42:38 +0000385 "Attempt to redefine a tag definition?");
386 IsDefinition = true;
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000387 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
388 TagT->decl.setPointer(this);
389 TagT->decl.setInt(0);
Douglas Gregor98b27542009-01-17 00:42:38 +0000390}
391
Ted Kremenek46a837c2008-09-05 17:16:31 +0000392TagDecl* TagDecl::getDefinition(ASTContext& C) const {
393 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000394 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek46a837c2008-09-05 17:16:31 +0000395 return D->isDefinition() ? D : 0;
396}
397
398//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000399// RecordDecl Implementation
400//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000401
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000402RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000403 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000404 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000405 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000406 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000407 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000408}
409
410RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000411 SourceLocation L, IdentifierInfo *Id,
412 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000413
Steve Naroff5abb0282009-01-27 21:25:57 +0000414 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000415 C.getTypeDeclType(R, PrevDecl);
416 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000417}
418
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000419RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000420}
421
422void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000423 TagDecl::Destroy(C);
424}
425
Douglas Gregor8acb7272008-12-11 16:49:14 +0000426/// completeDefinition - Notes that the definition of this type is now
427/// complete.
428void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000429 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000430 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000431}
432
Steve Naroff9ac456d2008-10-08 17:01:13 +0000433//===----------------------------------------------------------------------===//
434// BlockDecl Implementation
435//===----------------------------------------------------------------------===//
436
437BlockDecl::~BlockDecl() {
438}
439
440void BlockDecl::Destroy(ASTContext& C) {
441 if (Body)
442 Body->Destroy(C);
443
444 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
445 (*I)->Destroy(C);
446
447 Decl::Destroy(C);
448}