blob: eaf69f0197c8ed0a1ba3da16e14999eed70b3d58 [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 Lattner0b2b6e12009-03-04 06:34:08 +000025void Attr::Destroy(ASTContext &C) {
26 if (Next) {
27 Next->Destroy(C);
28 Next = 0;
29 }
30 this->~Attr();
31 C.Deallocate((void*)this);
32}
33
34
Chris Lattnerd3b90652008-03-15 05:43:15 +000035//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000036// Decl Allocation/Deallocation Method Implementations
37//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000038
Chris Lattner0b2b6e12009-03-04 06:34:08 +000039
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000040TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff3e970492009-01-27 21:25:57 +000041 return new (C) TranslationUnitDecl();
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000042}
43
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000044NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
45 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000046 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000047}
48
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000049void NamespaceDecl::Destroy(ASTContext& C) {
50 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
51 // together. They are all top-level Decls.
52
Ted Kremenekebf27b12008-05-24 15:09:56 +000053 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000054 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000055}
56
57
Chris Lattner41110242008-06-17 18:05:57 +000058ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000059 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000060 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000061}
62
Chris Lattner9fdf9c62008-04-22 18:39:57 +000063ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000064 SourceLocation L, IdentifierInfo *Id,
65 QualType T, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000066 Expr *DefArg) {
Steve Naroff3e970492009-01-27 21:25:57 +000067 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000068}
69
70QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor64650af2009-02-02 23:39:07 +000071 if (const OriginalParmVarDecl *PVD =
72 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000073 return PVD->OriginalType;
74 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000075}
76
Douglas Gregor63935192009-03-02 00:19:53 +000077bool VarDecl::isExternC(ASTContext &Context) const {
78 if (!Context.getLangOptions().CPlusPlus)
79 return (getDeclContext()->isTranslationUnit() &&
80 getStorageClass() != Static) ||
81 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
82
83 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
84 DC = DC->getParent()) {
85 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
86 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
87 return getStorageClass() != Static;
88
89 break;
90 }
91
92 if (DC->isFunctionOrMethod())
93 return false;
94 }
95
96 return false;
97}
98
Douglas Gregor64650af2009-02-02 23:39:07 +000099OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000100 ASTContext &C, DeclContext *DC,
101 SourceLocation L, IdentifierInfo *Id,
102 QualType T, QualType OT, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000103 Expr *DefArg) {
Douglas Gregor64650af2009-02-02 23:39:07 +0000104 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000105}
106
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000107FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000108 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000109 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000110 StorageClass S, bool isInline,
Douglas Gregor2224f842009-02-25 16:33:18 +0000111 bool hasPrototype,
Steve Naroff0eb07bf2008-10-03 00:02:03 +0000112 SourceLocation TypeSpecStartLoc) {
Douglas Gregor2224f842009-02-25 16:33:18 +0000113 FunctionDecl *New
114 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
115 TypeSpecStartLoc);
116 New->HasPrototype = hasPrototype;
117 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000118}
119
Steve Naroff090276f2008-10-10 01:28:17 +0000120BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000121 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000122}
123
Douglas Gregor44b43212008-12-11 16:49:14 +0000124FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
125 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000126 bool Mutable) {
Steve Naroff3e970492009-01-27 21:25:57 +0000127 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000128}
129
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000130bool FieldDecl::isAnonymousStructOrUnion() const {
131 if (!isImplicit() || getDeclName())
132 return false;
133
134 if (const RecordType *Record = getType()->getAsRecordType())
135 return Record->getDecl()->isAnonymousStructOrUnion();
136
137 return false;
138}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000139
Chris Lattner0ed844b2008-04-04 06:12:32 +0000140EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
141 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000142 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000143 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000144 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000145}
146
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000147void EnumConstantDecl::Destroy(ASTContext& C) {
148 if (Init) Init->Destroy(C);
149 Decl::Destroy(C);
150}
151
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000152TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000153 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000154 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000155 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000156}
157
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000158EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000159 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000160 EnumDecl *PrevDecl) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000161 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000162 C.getTypeDeclType(Enum, PrevDecl);
163 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000164}
165
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000166void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000167 Decl::Destroy(C);
168}
169
Douglas Gregor44b43212008-12-11 16:49:14 +0000170void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
171 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000172 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000173 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000174}
175
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000176FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000177 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000178 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000179 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000180}
181
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000182//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000183// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000184//===----------------------------------------------------------------------===//
185
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000186std::string NamedDecl::getQualifiedNameAsString() const {
187 std::vector<std::string> Names;
188 std::string QualName;
189 const DeclContext *Ctx = getDeclContext();
190
191 if (Ctx->isFunctionOrMethod())
192 return getNameAsString();
193
194 while (Ctx) {
195 if (Ctx->isFunctionOrMethod())
196 // FIXME: That probably will happen, when D was member of local
197 // scope class/struct/union. How do we handle this case?
198 break;
199
200 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
201 Names.push_back(ND->getNameAsString());
202 else
203 break;
204
205 Ctx = Ctx->getParent();
206 }
207
208 std::vector<std::string>::reverse_iterator
209 I = Names.rbegin(),
210 End = Names.rend();
211
212 for (; I!=End; ++I)
213 QualName += *I + "::";
214
215 QualName += getNameAsString();
216
217 return QualName;
218}
219
220
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000221bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000222 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
223
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000224 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
225 // We want to keep it, unless it nominates same namespace.
226 if (getKind() == Decl::UsingDirective) {
227 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
228 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
229 }
230
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000231 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
232 // For function declarations, we keep track of redeclarations.
233 return FD->getPreviousDeclaration() == OldD;
234
Steve Naroff0de21fd2009-02-22 19:35:57 +0000235 // For method declarations, we keep track of redeclarations.
236 if (isa<ObjCMethodDecl>(this))
237 return false;
238
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000239 // For non-function declarations, if the declarations are of the
240 // same kind then this must be a redeclaration, or semantic analysis
241 // would not have given us the new declaration.
242 return this->getKind() == OldD->getKind();
243}
244
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000245bool NamedDecl::hasLinkage() const {
246 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
247 return VD->hasExternalStorage() || VD->isFileVarDecl();
248
249 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
250 return true;
251
252 return false;
253}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000254
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000255//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000256// VarDecl Implementation
257//===----------------------------------------------------------------------===//
258
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000259VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
260 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000261 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000262 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000263}
264
265void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000266 Expr *Init = getInit();
267 if (Init)
268 Init->Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000269 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000270 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000271}
272
273VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000274}
275
276//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000277// FunctionDecl Implementation
278//===----------------------------------------------------------------------===//
279
Ted Kremenek27f8a282008-05-20 00:43:19 +0000280void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000281 if (Body)
282 Body->Destroy(C);
283
284 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
285 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000286
Steve Naroff3e970492009-01-27 21:25:57 +0000287 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000288
Ted Kremenek27f8a282008-05-20 00:43:19 +0000289 Decl::Destroy(C);
290}
291
292
Douglas Gregorf0097952008-04-21 02:02:58 +0000293Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
294 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
295 if (FD->Body) {
296 Definition = FD;
297 return FD->Body;
298 }
299 }
300
301 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302}
303
Douglas Gregor04495c82009-02-24 01:23:02 +0000304bool FunctionDecl::isMain() const {
305 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
306 getIdentifier() && getIdentifier()->isStr("main");
307}
308
Douglas Gregor63935192009-03-02 00:19:53 +0000309bool FunctionDecl::isExternC(ASTContext &Context) const {
310 // In C, any non-static, non-overloadable function has external
311 // linkage.
312 if (!Context.getLangOptions().CPlusPlus)
313 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
314
315 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
316 DC = DC->getParent()) {
317 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
318 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
319 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
320
321 break;
322 }
323 }
324
325 return false;
326}
327
Douglas Gregor3e41d602009-02-13 23:20:09 +0000328/// \brief Returns a value indicating whether this function
329/// corresponds to a builtin function.
330///
331/// The function corresponds to a built-in function if it is
332/// declared at translation scope or within an extern "C" block and
333/// its name matches with the name of a builtin. The returned value
334/// will be 0 for functions that do not correspond to a builtin, a
335/// value of type \c Builtin::ID if in the target-independent range
336/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000337unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
338 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
339 return 0;
340
341 unsigned BuiltinID = getIdentifier()->getBuiltinID();
342 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
343 return BuiltinID;
344
345 // This function has the name of a known C library
346 // function. Determine whether it actually refers to the C library
347 // function or whether it just has the same name.
348
Douglas Gregor9add3172009-02-17 03:23:10 +0000349 // If this is a static function, it's not a builtin.
350 if (getStorageClass() == Static)
351 return 0;
352
Douglas Gregor3c385e52009-02-14 18:57:46 +0000353 // If this function is at translation-unit scope and we're not in
354 // C++, it refers to the C library function.
355 if (!Context.getLangOptions().CPlusPlus &&
356 getDeclContext()->isTranslationUnit())
357 return BuiltinID;
358
359 // If the function is in an extern "C" linkage specification and is
360 // not marked "overloadable", it's the real function.
361 if (isa<LinkageSpecDecl>(getDeclContext()) &&
362 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
363 == LinkageSpecDecl::lang_c &&
364 !getAttr<OverloadableAttr>())
365 return BuiltinID;
366
367 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000368 return 0;
369}
370
371
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000372// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
373static unsigned getNumTypeParams(QualType T) {
374 const FunctionType *FT = T->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000375 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000376 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000377 return cast<FunctionProtoType>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000378}
379
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000380unsigned FunctionDecl::getNumParams() const {
381 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
382 if (!ParamInfo)
383 return 0;
384
385 return getNumTypeParams(getType());
386}
387
Ted Kremenekfc767612009-01-14 00:42:25 +0000388void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
389 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000391 assert(NumParams == getNumTypeParams(getType()) &&
392 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000393
394 // Zero params -> null pointer.
395 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000396 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000397 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
399 }
400}
401
Chris Lattner8123a952008-04-10 02:22:51 +0000402/// getMinRequiredArguments - Returns the minimum number of arguments
403/// needed to call this function. This may be fewer than the number of
404/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000405/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000406unsigned FunctionDecl::getMinRequiredArguments() const {
407 unsigned NumRequiredArgs = getNumParams();
408 while (NumRequiredArgs > 0
409 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
410 --NumRequiredArgs;
411
412 return NumRequiredArgs;
413}
414
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000415/// getOverloadedOperator - Which C++ overloaded operator this
416/// function represents, if any.
417OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000418 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
419 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000420 else
421 return OO_None;
422}
423
Chris Lattner8a934232008-03-31 00:36:02 +0000424//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000425// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000426//===----------------------------------------------------------------------===//
427
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000428void TagDecl::startDefinition() {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000429 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
430 TagT->decl.setPointer(this);
431 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000432}
433
434void TagDecl::completeDefinition() {
435 assert((!TypeForDecl ||
Douglas Gregorfc705b82009-02-26 22:19:44 +0000436 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000437 "Attempt to redefine a tag definition?");
438 IsDefinition = true;
Douglas Gregorfc705b82009-02-26 22:19:44 +0000439 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
440 TagT->decl.setPointer(this);
441 TagT->decl.setInt(0);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000442}
443
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000444TagDecl* TagDecl::getDefinition(ASTContext& C) const {
445 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregorfc705b82009-02-26 22:19:44 +0000446 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000447 return D->isDefinition() ? D : 0;
448}
449
450//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000451// RecordDecl Implementation
452//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000453
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000454RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000455 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000456 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000457 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000458 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000459 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000460}
461
462RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000463 SourceLocation L, IdentifierInfo *Id,
464 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000465
Steve Naroff3e970492009-01-27 21:25:57 +0000466 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000467 C.getTypeDeclType(R, PrevDecl);
468 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000469}
470
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000471RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000472}
473
474void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000475 TagDecl::Destroy(C);
476}
477
Douglas Gregor44b43212008-12-11 16:49:14 +0000478/// completeDefinition - Notes that the definition of this type is now
479/// complete.
480void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000482 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000483}
484
Steve Naroff56ee6892008-10-08 17:01:13 +0000485//===----------------------------------------------------------------------===//
486// BlockDecl Implementation
487//===----------------------------------------------------------------------===//
488
489BlockDecl::~BlockDecl() {
490}
491
492void BlockDecl::Destroy(ASTContext& C) {
493 if (Body)
494 Body->Destroy(C);
495
496 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
497 (*I)->Destroy(C);
498
499 Decl::Destroy(C);
500}