blob: 19212a3e813ebcc11de48bda3dfa47c914980532 [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
Douglas Gregor275a3692009-03-10 23:43:53 +0000276bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
277 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
278 return false;
279
280 return (!getInit() &&
281 (getStorageClass() == None || getStorageClass() == Static));
282}
283
284const Expr *VarDecl::getDefinition(const VarDecl *&Def) {
285 Def = this;
286 while (Def && !Def->getInit())
287 Def = Def->getPreviousDeclaration();
288
289 return Def? Def->getInit() : 0;
290}
291
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000292//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000293// FunctionDecl Implementation
294//===----------------------------------------------------------------------===//
295
Ted Kremenek27f8a282008-05-20 00:43:19 +0000296void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000297 if (Body)
298 Body->Destroy(C);
299
300 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
301 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000302
Steve Naroff3e970492009-01-27 21:25:57 +0000303 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000304
Ted Kremenek27f8a282008-05-20 00:43:19 +0000305 Decl::Destroy(C);
306}
307
308
Ted Kremenekeaab2062009-03-12 18:33:24 +0000309CompoundStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Douglas Gregorf0097952008-04-21 02:02:58 +0000310 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
311 if (FD->Body) {
312 Definition = FD;
Ted Kremenekeaab2062009-03-12 18:33:24 +0000313 return cast<CompoundStmt>(FD->Body);
Douglas Gregorf0097952008-04-21 02:02:58 +0000314 }
315 }
316
317 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000318}
319
Douglas Gregor04495c82009-02-24 01:23:02 +0000320bool FunctionDecl::isMain() const {
321 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
322 getIdentifier() && getIdentifier()->isStr("main");
323}
324
Douglas Gregor63935192009-03-02 00:19:53 +0000325bool FunctionDecl::isExternC(ASTContext &Context) const {
326 // In C, any non-static, non-overloadable function has external
327 // linkage.
328 if (!Context.getLangOptions().CPlusPlus)
329 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
330
331 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
332 DC = DC->getParent()) {
333 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
334 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
335 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
336
337 break;
338 }
339 }
340
341 return false;
342}
343
Douglas Gregor3e41d602009-02-13 23:20:09 +0000344/// \brief Returns a value indicating whether this function
345/// corresponds to a builtin function.
346///
347/// The function corresponds to a built-in function if it is
348/// declared at translation scope or within an extern "C" block and
349/// its name matches with the name of a builtin. The returned value
350/// will be 0 for functions that do not correspond to a builtin, a
351/// value of type \c Builtin::ID if in the target-independent range
352/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000353unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
354 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
355 return 0;
356
357 unsigned BuiltinID = getIdentifier()->getBuiltinID();
358 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
359 return BuiltinID;
360
361 // This function has the name of a known C library
362 // function. Determine whether it actually refers to the C library
363 // function or whether it just has the same name.
364
Douglas Gregor9add3172009-02-17 03:23:10 +0000365 // If this is a static function, it's not a builtin.
366 if (getStorageClass() == Static)
367 return 0;
368
Douglas Gregor3c385e52009-02-14 18:57:46 +0000369 // If this function is at translation-unit scope and we're not in
370 // C++, it refers to the C library function.
371 if (!Context.getLangOptions().CPlusPlus &&
372 getDeclContext()->isTranslationUnit())
373 return BuiltinID;
374
375 // If the function is in an extern "C" linkage specification and is
376 // not marked "overloadable", it's the real function.
377 if (isa<LinkageSpecDecl>(getDeclContext()) &&
378 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
379 == LinkageSpecDecl::lang_c &&
380 !getAttr<OverloadableAttr>())
381 return BuiltinID;
382
383 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000384 return 0;
385}
386
387
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000388// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
389static unsigned getNumTypeParams(QualType T) {
390 const FunctionType *FT = T->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000391 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000392 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000393 return cast<FunctionProtoType>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000394}
395
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000396unsigned FunctionDecl::getNumParams() const {
397 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
398 if (!ParamInfo)
399 return 0;
400
401 return getNumTypeParams(getType());
402}
403
Ted Kremenekfc767612009-01-14 00:42:25 +0000404void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
405 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000407 assert(NumParams == getNumTypeParams(getType()) &&
408 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000409
410 // Zero params -> null pointer.
411 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000412 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000413 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
415 }
416}
417
Chris Lattner8123a952008-04-10 02:22:51 +0000418/// getMinRequiredArguments - Returns the minimum number of arguments
419/// needed to call this function. This may be fewer than the number of
420/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000421/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000422unsigned FunctionDecl::getMinRequiredArguments() const {
423 unsigned NumRequiredArgs = getNumParams();
424 while (NumRequiredArgs > 0
425 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
426 --NumRequiredArgs;
427
428 return NumRequiredArgs;
429}
430
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000431/// getOverloadedOperator - Which C++ overloaded operator this
432/// function represents, if any.
433OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000434 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
435 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000436 else
437 return OO_None;
438}
439
Chris Lattner8a934232008-03-31 00:36:02 +0000440//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000441// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000442//===----------------------------------------------------------------------===//
443
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000444void TagDecl::startDefinition() {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000445 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
446 TagT->decl.setPointer(this);
447 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000448}
449
450void TagDecl::completeDefinition() {
451 assert((!TypeForDecl ||
Douglas Gregorfc705b82009-02-26 22:19:44 +0000452 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000453 "Attempt to redefine a tag definition?");
454 IsDefinition = true;
Douglas Gregorfc705b82009-02-26 22:19:44 +0000455 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
456 TagT->decl.setPointer(this);
457 TagT->decl.setInt(0);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000458}
459
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000460TagDecl* TagDecl::getDefinition(ASTContext& C) const {
461 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregorfc705b82009-02-26 22:19:44 +0000462 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000463 return D->isDefinition() ? D : 0;
464}
465
466//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000467// RecordDecl Implementation
468//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000469
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000470RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000471 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000472 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000473 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000474 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000475 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000476}
477
478RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000479 SourceLocation L, IdentifierInfo *Id,
480 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000481
Steve Naroff3e970492009-01-27 21:25:57 +0000482 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000483 C.getTypeDeclType(R, PrevDecl);
484 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000485}
486
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000487RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000488}
489
490void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000491 TagDecl::Destroy(C);
492}
493
Douglas Gregor44b43212008-12-11 16:49:14 +0000494/// completeDefinition - Notes that the definition of this type is now
495/// complete.
496void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000498 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000499}
500
Steve Naroff56ee6892008-10-08 17:01:13 +0000501//===----------------------------------------------------------------------===//
502// BlockDecl Implementation
503//===----------------------------------------------------------------------===//
504
505BlockDecl::~BlockDecl() {
506}
507
508void BlockDecl::Destroy(ASTContext& C) {
509 if (Body)
510 Body->Destroy(C);
511
512 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
513 (*I)->Destroy(C);
Ted Kremenek879d27a2009-03-13 23:17:24 +0000514
515 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000516 Decl::Destroy(C);
517}
Steve Naroffe78b8092009-03-13 16:56:44 +0000518
519void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
520 unsigned NParms) {
521 assert(ParamInfo == 0 && "Already has param info!");
522
523 // Zero params -> null pointer.
524 if (NParms) {
525 NumParams = NParms;
526 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
527 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
528 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
529 }
530}
531
532unsigned BlockDecl::getNumParams() const {
533 return NumParams;
534}