blob: 1ebfbf79dacff87a83048ee0884c68b73b9fd4c0 [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 Gregor63935192009-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 Gregor64650af2009-02-02 23:39:07 +000088OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000089 ASTContext &C, DeclContext *DC,
90 SourceLocation L, IdentifierInfo *Id,
91 QualType T, QualType OT, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000092 Expr *DefArg) {
Douglas Gregor64650af2009-02-02 23:39:07 +000093 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000094}
95
Chris Lattner9fdf9c62008-04-22 18:39:57 +000096FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000097 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +000098 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +000099 StorageClass S, bool isInline,
Douglas Gregor2224f842009-02-25 16:33:18 +0000100 bool hasPrototype,
Steve Naroff0eb07bf2008-10-03 00:02:03 +0000101 SourceLocation TypeSpecStartLoc) {
Douglas Gregor2224f842009-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 Lattnera98e58d2008-03-15 21:24:04 +0000107}
108
Steve Naroff090276f2008-10-10 01:28:17 +0000109BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000110 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000111}
112
Douglas Gregor44b43212008-12-11 16:49:14 +0000113FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
114 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000115 bool Mutable) {
Steve Naroff3e970492009-01-27 21:25:57 +0000116 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000117}
118
Douglas Gregor6b3945f2009-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 Lattnera98e58d2008-03-15 21:24:04 +0000128
Chris Lattner0ed844b2008-04-04 06:12:32 +0000129EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
130 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000131 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000132 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000133 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000134}
135
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000136void EnumConstantDecl::Destroy(ASTContext& C) {
137 if (Init) Init->Destroy(C);
138 Decl::Destroy(C);
139}
140
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000141TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000142 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000143 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000144 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000145}
146
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000147EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000148 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000149 EnumDecl *PrevDecl) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000150 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000151 C.getTypeDeclType(Enum, PrevDecl);
152 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000153}
154
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000155void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000156 Decl::Destroy(C);
157}
158
Douglas Gregor44b43212008-12-11 16:49:14 +0000159void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
160 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000161 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000162 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000163}
164
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000165FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000166 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000167 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000168 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000169}
170
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000171//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000172// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000173//===----------------------------------------------------------------------===//
174
Douglas Gregor47b9a1c2009-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 Gregor4afa39d2009-01-20 01:17:11 +0000210bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000211 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
212
Douglas Gregor2a3009a2009-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 Gregor6ed40e32008-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 Naroff0de21fd2009-02-22 19:35:57 +0000224 // For method declarations, we keep track of redeclarations.
225 if (isa<ObjCMethodDecl>(this))
226 return false;
227
Douglas Gregor6ed40e32008-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 Gregord6f7e9d2009-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 Gregor4afa39d2009-01-20 01:17:11 +0000243
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000244//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000245// VarDecl Implementation
246//===----------------------------------------------------------------------===//
247
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000248VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
249 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000250 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000251 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000252}
253
254void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000255 Expr *Init = getInit();
256 if (Init)
257 Init->Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000258 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000259 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000260}
261
262VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000263}
264
265//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000266// FunctionDecl Implementation
267//===----------------------------------------------------------------------===//
268
Ted Kremenek27f8a282008-05-20 00:43:19 +0000269void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-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 Lopes460b0ac2009-01-18 19:57:27 +0000275
Steve Naroff3e970492009-01-27 21:25:57 +0000276 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000277
Ted Kremenek27f8a282008-05-20 00:43:19 +0000278 Decl::Destroy(C);
279}
280
281
Douglas Gregorf0097952008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
292
Douglas Gregor04495c82009-02-24 01:23:02 +0000293bool FunctionDecl::isMain() const {
294 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
295 getIdentifier() && getIdentifier()->isStr("main");
296}
297
Douglas Gregor63935192009-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 Gregor3e41d602009-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 Gregor3c385e52009-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 Gregor9add3172009-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 Gregor3c385e52009-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 Gregor3e41d602009-02-13 23:20:09 +0000357 return 0;
358}
359
360
Ted Kremenek4f03fd62008-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 Gregor72564e72009-02-26 23:50:07 +0000364 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000365 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000366 return cast<FunctionProtoType>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000367}
368
Ted Kremenek4f03fd62008-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 Kremenekfc767612009-01-14 00:42:25 +0000377void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
378 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000380 assert(NumParams == getNumTypeParams(getType()) &&
381 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000382
383 // Zero params -> null pointer.
384 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000385 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000386 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
388 }
389}
390
Chris Lattner8123a952008-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 Lattner9e979552008-04-12 23:52:44 +0000394/// arguments (in C++).
Chris Lattner8123a952008-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 Gregor1cd1b1e2008-11-06 22:13:31 +0000404/// getOverloadedOperator - Which C++ overloaded operator this
405/// function represents, if any.
406OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000407 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
408 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000409 else
410 return OO_None;
411}
412
Chris Lattner8a934232008-03-31 00:36:02 +0000413//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000414// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000415//===----------------------------------------------------------------------===//
416
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000417void TagDecl::startDefinition() {
Douglas Gregorfc705b82009-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 Gregor0b7a1582009-01-17 00:42:38 +0000421}
422
423void TagDecl::completeDefinition() {
424 assert((!TypeForDecl ||
Douglas Gregorfc705b82009-02-26 22:19:44 +0000425 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000426 "Attempt to redefine a tag definition?");
427 IsDefinition = true;
Douglas Gregorfc705b82009-02-26 22:19:44 +0000428 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
429 TagT->decl.setPointer(this);
430 TagT->decl.setInt(0);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000431}
432
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000433TagDecl* TagDecl::getDefinition(ASTContext& C) const {
434 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregorfc705b82009-02-26 22:19:44 +0000435 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000436 return D->isDefinition() ? D : 0;
437}
438
439//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000440// RecordDecl Implementation
441//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000442
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000443RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000444 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000445 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000446 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000447 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000448 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000449}
450
451RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000452 SourceLocation L, IdentifierInfo *Id,
453 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000454
Steve Naroff3e970492009-01-27 21:25:57 +0000455 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000456 C.getTypeDeclType(R, PrevDecl);
457 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000458}
459
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000460RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000461}
462
463void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000464 TagDecl::Destroy(C);
465}
466
Douglas Gregor44b43212008-12-11 16:49:14 +0000467/// completeDefinition - Notes that the definition of this type is now
468/// complete.
469void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000471 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000472}
473
Steve Naroff56ee6892008-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}