blob: b30b86980f6cfb68e9c5135f458c18f55538e759 [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"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000019#include "clang/AST/Expr.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/Basic/IdentifierTable.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000021#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Chris Lattnerd3b90652008-03-15 05:43:15 +000025//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000026// Decl Allocation/Deallocation Method Implementations
27//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000028
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000029TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff3e970492009-01-27 21:25:57 +000030 return new (C) TranslationUnitDecl();
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000031}
32
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000033NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
34 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000035 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000036}
37
Ted Kremenekd1ac17a2008-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 Kremenekebf27b12008-05-24 15:09:56 +000042 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000043 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000044}
45
46
Chris Lattner41110242008-06-17 18:05:57 +000047ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000048 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000049 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000050}
51
Chris Lattner9fdf9c62008-04-22 18:39:57 +000052ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000053 SourceLocation L, IdentifierInfo *Id,
54 QualType T, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000055 Expr *DefArg) {
Steve Naroff3e970492009-01-27 21:25:57 +000056 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000057}
58
59QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor64650af2009-02-02 23:39:07 +000060 if (const OriginalParmVarDecl *PVD =
61 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000062 return PVD->OriginalType;
63 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000064}
65
Douglas Gregor64650af2009-02-02 23:39:07 +000066OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000067 ASTContext &C, DeclContext *DC,
68 SourceLocation L, IdentifierInfo *Id,
69 QualType T, QualType OT, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000070 Expr *DefArg) {
Douglas Gregor64650af2009-02-02 23:39:07 +000071 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000072}
73
Chris Lattner9fdf9c62008-04-22 18:39:57 +000074FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000075 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +000076 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +000077 StorageClass S, bool isInline,
Douglas Gregor2224f842009-02-25 16:33:18 +000078 bool hasPrototype,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000079 SourceLocation TypeSpecStartLoc) {
Douglas Gregor2224f842009-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 Lattnera98e58d2008-03-15 21:24:04 +000085}
86
Steve Naroff090276f2008-10-10 01:28:17 +000087BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +000088 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +000089}
90
Douglas Gregor44b43212008-12-11 16:49:14 +000091FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
92 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000093 bool Mutable) {
Steve Naroff3e970492009-01-27 21:25:57 +000094 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +000095}
96
Douglas Gregor6b3945f2009-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 Lattnera98e58d2008-03-15 21:24:04 +0000106
Chris Lattner0ed844b2008-04-04 06:12:32 +0000107EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
108 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000109 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000110 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000111 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000112}
113
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000114void EnumConstantDecl::Destroy(ASTContext& C) {
115 if (Init) Init->Destroy(C);
116 Decl::Destroy(C);
117}
118
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000119TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000120 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000121 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000122 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000123}
124
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000125EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000126 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000127 EnumDecl *PrevDecl) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000128 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000129 C.getTypeDeclType(Enum, PrevDecl);
130 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000131}
132
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000133void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000134 Decl::Destroy(C);
135}
136
Douglas Gregor44b43212008-12-11 16:49:14 +0000137void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
138 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000139 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000140 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000141}
142
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000143FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000144 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000145 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000146 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000147}
148
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000149//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000150// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000151//===----------------------------------------------------------------------===//
152
Douglas Gregor47b9a1c2009-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 Gregor4afa39d2009-01-20 01:17:11 +0000188bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000189 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
190
Douglas Gregor2a3009a2009-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 Gregor6ed40e32008-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 Naroff0de21fd2009-02-22 19:35:57 +0000202 // For method declarations, we keep track of redeclarations.
203 if (isa<ObjCMethodDecl>(this))
204 return false;
205
Douglas Gregor6ed40e32008-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 Gregord6f7e9d2009-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 Gregor4afa39d2009-01-20 01:17:11 +0000221
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000222//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000223// VarDecl Implementation
224//===----------------------------------------------------------------------===//
225
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000226VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
227 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000228 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000229 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000230}
231
232void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000233 Expr *Init = getInit();
234 if (Init)
235 Init->Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000236 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000237 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000238}
239
240VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000241}
242
243//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000244// FunctionDecl Implementation
245//===----------------------------------------------------------------------===//
246
Ted Kremenek27f8a282008-05-20 00:43:19 +0000247void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-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 Lopes460b0ac2009-01-18 19:57:27 +0000253
Steve Naroff3e970492009-01-27 21:25:57 +0000254 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000255
Ted Kremenek27f8a282008-05-20 00:43:19 +0000256 Decl::Destroy(C);
257}
258
259
Douglas Gregorf0097952008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000269}
270
Douglas Gregor04495c82009-02-24 01:23:02 +0000271bool FunctionDecl::isMain() const {
272 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
273 getIdentifier() && getIdentifier()->isStr("main");
274}
275
Douglas Gregor3e41d602009-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 Gregor3c385e52009-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 Gregor9add3172009-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 Gregor3c385e52009-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 Gregor3e41d602009-02-13 23:20:09 +0000316 return 0;
317}
318
319
Ted Kremenek4f03fd62008-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();
Douglas Gregor72564e72009-02-26 23:50:07 +0000323 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000324 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000325 return cast<FunctionProtoType>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327
Ted Kremenek4f03fd62008-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 Kremenekfc767612009-01-14 00:42:25 +0000336void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
337 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000339 assert(NumParams == getNumTypeParams(getType()) &&
340 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000341
342 // Zero params -> null pointer.
343 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000344 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000345 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
347 }
348}
349
Chris Lattner8123a952008-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 Lattner9e979552008-04-12 23:52:44 +0000353/// arguments (in C++).
Chris Lattner8123a952008-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 Gregor1cd1b1e2008-11-06 22:13:31 +0000363/// getOverloadedOperator - Which C++ overloaded operator this
364/// function represents, if any.
365OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000366 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
367 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000368 else
369 return OO_None;
370}
371
Chris Lattner8a934232008-03-31 00:36:02 +0000372//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000373// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000374//===----------------------------------------------------------------------===//
375
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000376void TagDecl::startDefinition() {
Douglas Gregorfc705b82009-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 Gregor0b7a1582009-01-17 00:42:38 +0000380}
381
382void TagDecl::completeDefinition() {
383 assert((!TypeForDecl ||
Douglas Gregorfc705b82009-02-26 22:19:44 +0000384 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000385 "Attempt to redefine a tag definition?");
386 IsDefinition = true;
Douglas Gregorfc705b82009-02-26 22:19:44 +0000387 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
388 TagT->decl.setPointer(this);
389 TagT->decl.setInt(0);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000390}
391
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000392TagDecl* TagDecl::getDefinition(ASTContext& C) const {
393 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregorfc705b82009-02-26 22:19:44 +0000394 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000395 return D->isDefinition() ? D : 0;
396}
397
398//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000399// RecordDecl Implementation
400//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000401
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000402RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000403 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000404 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000405 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000406 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000407 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000408}
409
410RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000411 SourceLocation L, IdentifierInfo *Id,
412 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000413
Steve Naroff3e970492009-01-27 21:25:57 +0000414 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000415 C.getTypeDeclType(R, PrevDecl);
416 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000417}
418
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000419RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000420}
421
422void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000423 TagDecl::Destroy(C);
424}
425
Douglas Gregor44b43212008-12-11 16:49:14 +0000426/// completeDefinition - Notes that the definition of this type is now
427/// complete.
428void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000430 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000431}
432
Steve Naroff56ee6892008-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}