blob: 1ebfbf79dacff87a83048ee0884c68b73b9fd4c0 [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 Gregore6b5d1d2009-03-02 00:19:53 +000066bool VarDecl::isExternC(ASTContext &Context) const {
67 if (!Context.getLangOptions().CPlusPlus)
68 return (getDeclContext()->isTranslationUnit() &&
69 getStorageClass() != Static) ||
70 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
71
72 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
73 DC = DC->getParent()) {
74 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
75 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
76 return getStorageClass() != Static;
77
78 break;
79 }
80
81 if (DC->isFunctionOrMethod())
82 return false;
83 }
84
85 return false;
86}
87
Douglas Gregor469fc9a2009-02-02 23:39:07 +000088OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +000089 ASTContext &C, DeclContext *DC,
90 SourceLocation L, IdentifierInfo *Id,
91 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000092 Expr *DefArg) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000093 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +000094}
95
Chris Lattneref87a202008-04-22 18:39:57 +000096FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000097 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +000098 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +000099 StorageClass S, bool isInline,
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000100 bool hasPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +0000101 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000102 FunctionDecl *New
103 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
104 TypeSpecStartLoc);
105 New->HasPrototype = hasPrototype;
106 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000107}
108
Steve Naroff52059382008-10-10 01:28:17 +0000109BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000110 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000111}
112
Douglas Gregor8acb7272008-12-11 16:49:14 +0000113FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
114 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000115 bool Mutable) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000116 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +0000117}
118
Douglas Gregorc7f01612009-01-07 19:46:03 +0000119bool FieldDecl::isAnonymousStructOrUnion() const {
120 if (!isImplicit() || getDeclName())
121 return false;
122
123 if (const RecordType *Record = getType()->getAsRecordType())
124 return Record->getDecl()->isAnonymousStructOrUnion();
125
126 return false;
127}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000128
Chris Lattnereee57c02008-04-04 06:12:32 +0000129EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
130 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000131 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000132 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000133 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000134}
135
Ted Kremenek5be49242008-05-20 04:49:55 +0000136void EnumConstantDecl::Destroy(ASTContext& C) {
137 if (Init) Init->Destroy(C);
138 Decl::Destroy(C);
139}
140
Chris Lattneref87a202008-04-22 18:39:57 +0000141TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000142 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000143 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000144 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000145}
146
Chris Lattneref87a202008-04-22 18:39:57 +0000147EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000148 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000149 EnumDecl *PrevDecl) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000150 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000151 C.getTypeDeclType(Enum, PrevDecl);
152 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000153}
154
Ted Kremenek25d8be12008-09-02 20:13:32 +0000155void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000156 Decl::Destroy(C);
157}
158
Douglas Gregor8acb7272008-12-11 16:49:14 +0000159void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
160 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000161 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000162 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000163}
164
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000165FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000166 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000167 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000168 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000169}
170
Chris Lattnere4650482008-03-15 06:12:44 +0000171//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000172// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000173//===----------------------------------------------------------------------===//
174
Douglas Gregor09be81b2009-02-04 17:27:36 +0000175std::string NamedDecl::getQualifiedNameAsString() const {
176 std::vector<std::string> Names;
177 std::string QualName;
178 const DeclContext *Ctx = getDeclContext();
179
180 if (Ctx->isFunctionOrMethod())
181 return getNameAsString();
182
183 while (Ctx) {
184 if (Ctx->isFunctionOrMethod())
185 // FIXME: That probably will happen, when D was member of local
186 // scope class/struct/union. How do we handle this case?
187 break;
188
189 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
190 Names.push_back(ND->getNameAsString());
191 else
192 break;
193
194 Ctx = Ctx->getParent();
195 }
196
197 std::vector<std::string>::reverse_iterator
198 I = Names.rbegin(),
199 End = Names.rend();
200
201 for (; I!=End; ++I)
202 QualName += *I + "::";
203
204 QualName += getNameAsString();
205
206 return QualName;
207}
208
209
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000210bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000211 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
212
Douglas Gregor7a7be652009-02-03 19:21:40 +0000213 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
214 // We want to keep it, unless it nominates same namespace.
215 if (getKind() == Decl::UsingDirective) {
216 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
217 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
218 }
219
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000220 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
221 // For function declarations, we keep track of redeclarations.
222 return FD->getPreviousDeclaration() == OldD;
223
Steve Naroffd85ba922009-02-22 19:35:57 +0000224 // For method declarations, we keep track of redeclarations.
225 if (isa<ObjCMethodDecl>(this))
226 return false;
227
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000228 // For non-function declarations, if the declarations are of the
229 // same kind then this must be a redeclaration, or semantic analysis
230 // would not have given us the new declaration.
231 return this->getKind() == OldD->getKind();
232}
233
Douglas Gregor1c52c632009-02-24 20:03:32 +0000234bool NamedDecl::hasLinkage() const {
235 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
236 return VD->hasExternalStorage() || VD->isFileVarDecl();
237
238 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
239 return true;
240
241 return false;
242}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000243
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000244//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000245// VarDecl Implementation
246//===----------------------------------------------------------------------===//
247
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000248VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
249 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000250 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000251 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000252}
253
254void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000255 Expr *Init = getInit();
256 if (Init)
257 Init->Destroy(C);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000258 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000259 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000260}
261
262VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000263}
264
265//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000266// FunctionDecl Implementation
267//===----------------------------------------------------------------------===//
268
Ted Kremenekafdf8112008-05-20 00:43:19 +0000269void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000270 if (Body)
271 Body->Destroy(C);
272
273 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
274 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000275
Steve Naroff5abb0282009-01-27 21:25:57 +0000276 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000277
Ted Kremenekafdf8112008-05-20 00:43:19 +0000278 Decl::Destroy(C);
279}
280
281
Douglas Gregor42214c52008-04-21 02:02:58 +0000282Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
283 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
284 if (FD->Body) {
285 Definition = FD;
286 return FD->Body;
287 }
288 }
289
290 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000291}
292
Douglas Gregoraf682022009-02-24 01:23:02 +0000293bool FunctionDecl::isMain() const {
294 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
295 getIdentifier() && getIdentifier()->isStr("main");
296}
297
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000298bool FunctionDecl::isExternC(ASTContext &Context) const {
299 // In C, any non-static, non-overloadable function has external
300 // linkage.
301 if (!Context.getLangOptions().CPlusPlus)
302 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
303
304 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
305 DC = DC->getParent()) {
306 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
307 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
308 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
309
310 break;
311 }
312 }
313
314 return false;
315}
316
Douglas Gregor411889e2009-02-13 23:20:09 +0000317/// \brief Returns a value indicating whether this function
318/// corresponds to a builtin function.
319///
320/// The function corresponds to a built-in function if it is
321/// declared at translation scope or within an extern "C" block and
322/// its name matches with the name of a builtin. The returned value
323/// will be 0 for functions that do not correspond to a builtin, a
324/// value of type \c Builtin::ID if in the target-independent range
325/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000326unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
327 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
328 return 0;
329
330 unsigned BuiltinID = getIdentifier()->getBuiltinID();
331 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
332 return BuiltinID;
333
334 // This function has the name of a known C library
335 // function. Determine whether it actually refers to the C library
336 // function or whether it just has the same name.
337
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000338 // If this is a static function, it's not a builtin.
339 if (getStorageClass() == Static)
340 return 0;
341
Douglas Gregorb5af7382009-02-14 18:57:46 +0000342 // If this function is at translation-unit scope and we're not in
343 // C++, it refers to the C library function.
344 if (!Context.getLangOptions().CPlusPlus &&
345 getDeclContext()->isTranslationUnit())
346 return BuiltinID;
347
348 // If the function is in an extern "C" linkage specification and is
349 // not marked "overloadable", it's the real function.
350 if (isa<LinkageSpecDecl>(getDeclContext()) &&
351 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
352 == LinkageSpecDecl::lang_c &&
353 !getAttr<OverloadableAttr>())
354 return BuiltinID;
355
356 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000357 return 0;
358}
359
360
Ted Kremeneka6ded352008-10-29 18:41:34 +0000361// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
362static unsigned getNumTypeParams(QualType T) {
363 const FunctionType *FT = T->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000364 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000365 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000366 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000367}
368
Ted Kremeneka6ded352008-10-29 18:41:34 +0000369unsigned FunctionDecl::getNumParams() const {
370 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
371 if (!ParamInfo)
372 return 0;
373
374 return getNumTypeParams(getType());
375}
376
Ted Kremenek8494c962009-01-14 00:42:25 +0000377void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
378 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000379 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000380 assert(NumParams == getNumTypeParams(getType()) &&
381 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000382
383 // Zero params -> null pointer.
384 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000385 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000386 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000387 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
388 }
389}
390
Chris Lattner97316c02008-04-10 02:22:51 +0000391/// getMinRequiredArguments - Returns the minimum number of arguments
392/// needed to call this function. This may be fewer than the number of
393/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000394/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000395unsigned FunctionDecl::getMinRequiredArguments() const {
396 unsigned NumRequiredArgs = getNumParams();
397 while (NumRequiredArgs > 0
398 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
399 --NumRequiredArgs;
400
401 return NumRequiredArgs;
402}
403
Douglas Gregore60e5d32008-11-06 22:13:31 +0000404/// getOverloadedOperator - Which C++ overloaded operator this
405/// function represents, if any.
406OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000407 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
408 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000409 else
410 return OO_None;
411}
412
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000413//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000414// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000415//===----------------------------------------------------------------------===//
416
Douglas Gregor98b27542009-01-17 00:42:38 +0000417void TagDecl::startDefinition() {
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000418 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
419 TagT->decl.setPointer(this);
420 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor98b27542009-01-17 00:42:38 +0000421}
422
423void TagDecl::completeDefinition() {
424 assert((!TypeForDecl ||
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000425 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor98b27542009-01-17 00:42:38 +0000426 "Attempt to redefine a tag definition?");
427 IsDefinition = true;
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000428 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
429 TagT->decl.setPointer(this);
430 TagT->decl.setInt(0);
Douglas Gregor98b27542009-01-17 00:42:38 +0000431}
432
Ted Kremenek46a837c2008-09-05 17:16:31 +0000433TagDecl* TagDecl::getDefinition(ASTContext& C) const {
434 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000435 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek46a837c2008-09-05 17:16:31 +0000436 return D->isDefinition() ? D : 0;
437}
438
439//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000440// RecordDecl Implementation
441//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000442
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000443RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000444 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000445 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000446 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000447 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000448 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000449}
450
451RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000452 SourceLocation L, IdentifierInfo *Id,
453 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000454
Steve Naroff5abb0282009-01-27 21:25:57 +0000455 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000456 C.getTypeDeclType(R, PrevDecl);
457 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000458}
459
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000460RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000461}
462
463void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000464 TagDecl::Destroy(C);
465}
466
Douglas Gregor8acb7272008-12-11 16:49:14 +0000467/// completeDefinition - Notes that the definition of this type is now
468/// complete.
469void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000470 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000471 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000472}
473
Steve Naroff9ac456d2008-10-08 17:01:13 +0000474//===----------------------------------------------------------------------===//
475// BlockDecl Implementation
476//===----------------------------------------------------------------------===//
477
478BlockDecl::~BlockDecl() {
479}
480
481void BlockDecl::Destroy(ASTContext& C) {
482 if (Body)
483 Body->Destroy(C);
484
485 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
486 (*I)->Destroy(C);
487
488 Decl::Destroy(C);
489}