blob: 8bda32398fc5971625aeea5216f654249281265b [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
Daniel Dunbarb286a782009-04-14 02:08:49 +000063const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
64 switch (SC) {
65 case VarDecl::None: break;
66 case VarDecl::Auto: return "auto"; break;
67 case VarDecl::Extern: return "extern"; break;
68 case VarDecl::PrivateExtern: return "__private_extern__"; break;
69 case VarDecl::Register: return "register"; break;
70 case VarDecl::Static: return "static"; break;
71 }
72
73 assert(0 && "Invalid storage class");
74 return 0;
75}
76
Chris Lattner9fdf9c62008-04-22 18:39:57 +000077ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000078 SourceLocation L, IdentifierInfo *Id,
79 QualType T, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000080 Expr *DefArg) {
Steve Naroff3e970492009-01-27 21:25:57 +000081 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000082}
83
84QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor64650af2009-02-02 23:39:07 +000085 if (const OriginalParmVarDecl *PVD =
86 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000087 return PVD->OriginalType;
88 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000089}
90
Douglas Gregor63935192009-03-02 00:19:53 +000091bool VarDecl::isExternC(ASTContext &Context) const {
92 if (!Context.getLangOptions().CPlusPlus)
93 return (getDeclContext()->isTranslationUnit() &&
94 getStorageClass() != Static) ||
95 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
96
97 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
98 DC = DC->getParent()) {
99 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
100 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
101 return getStorageClass() != Static;
102
103 break;
104 }
105
106 if (DC->isFunctionOrMethod())
107 return false;
108 }
109
110 return false;
111}
112
Douglas Gregor64650af2009-02-02 23:39:07 +0000113OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000114 ASTContext &C, DeclContext *DC,
115 SourceLocation L, IdentifierInfo *Id,
116 QualType T, QualType OT, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000117 Expr *DefArg) {
Douglas Gregor64650af2009-02-02 23:39:07 +0000118 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000119}
120
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000121FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000122 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000123 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000124 StorageClass S, bool isInline,
Douglas Gregor2224f842009-02-25 16:33:18 +0000125 bool hasPrototype,
Steve Naroff0eb07bf2008-10-03 00:02:03 +0000126 SourceLocation TypeSpecStartLoc) {
Douglas Gregor2224f842009-02-25 16:33:18 +0000127 FunctionDecl *New
128 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
129 TypeSpecStartLoc);
130 New->HasPrototype = hasPrototype;
131 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000132}
133
Steve Naroff090276f2008-10-10 01:28:17 +0000134BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000135 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000136}
137
Douglas Gregor44b43212008-12-11 16:49:14 +0000138FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
139 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000140 bool Mutable) {
Steve Naroff3e970492009-01-27 21:25:57 +0000141 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000142}
143
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000144bool FieldDecl::isAnonymousStructOrUnion() const {
145 if (!isImplicit() || getDeclName())
146 return false;
147
148 if (const RecordType *Record = getType()->getAsRecordType())
149 return Record->getDecl()->isAnonymousStructOrUnion();
150
151 return false;
152}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000153
Chris Lattner0ed844b2008-04-04 06:12:32 +0000154EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
155 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000156 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000157 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000158 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000159}
160
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000161void EnumConstantDecl::Destroy(ASTContext& C) {
162 if (Init) Init->Destroy(C);
163 Decl::Destroy(C);
164}
165
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000166TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000167 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000168 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000169 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000170}
171
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000172EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000173 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000174 EnumDecl *PrevDecl) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000175 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000176 C.getTypeDeclType(Enum, PrevDecl);
177 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000178}
179
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000180void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000181 Decl::Destroy(C);
182}
183
Douglas Gregor44b43212008-12-11 16:49:14 +0000184void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
185 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000186 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000187 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000188}
189
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000190FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000191 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000192 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000193 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000194}
195
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000196//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000197// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000198//===----------------------------------------------------------------------===//
199
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000200std::string NamedDecl::getQualifiedNameAsString() const {
201 std::vector<std::string> Names;
202 std::string QualName;
203 const DeclContext *Ctx = getDeclContext();
204
205 if (Ctx->isFunctionOrMethod())
206 return getNameAsString();
207
208 while (Ctx) {
209 if (Ctx->isFunctionOrMethod())
210 // FIXME: That probably will happen, when D was member of local
211 // scope class/struct/union. How do we handle this case?
212 break;
213
214 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
215 Names.push_back(ND->getNameAsString());
216 else
217 break;
218
219 Ctx = Ctx->getParent();
220 }
221
222 std::vector<std::string>::reverse_iterator
223 I = Names.rbegin(),
224 End = Names.rend();
225
226 for (; I!=End; ++I)
227 QualName += *I + "::";
228
229 QualName += getNameAsString();
230
231 return QualName;
232}
233
234
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000235bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000236 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
237
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000238 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
239 // We want to keep it, unless it nominates same namespace.
240 if (getKind() == Decl::UsingDirective) {
241 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
242 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
243 }
244
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000245 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
246 // For function declarations, we keep track of redeclarations.
247 return FD->getPreviousDeclaration() == OldD;
248
Steve Naroff0de21fd2009-02-22 19:35:57 +0000249 // For method declarations, we keep track of redeclarations.
250 if (isa<ObjCMethodDecl>(this))
251 return false;
252
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000253 // For non-function declarations, if the declarations are of the
254 // same kind then this must be a redeclaration, or semantic analysis
255 // would not have given us the new declaration.
256 return this->getKind() == OldD->getKind();
257}
258
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000259bool NamedDecl::hasLinkage() const {
260 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
261 return VD->hasExternalStorage() || VD->isFileVarDecl();
262
263 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
264 return true;
265
266 return false;
267}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000268
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000269//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000270// VarDecl Implementation
271//===----------------------------------------------------------------------===//
272
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000273VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
274 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000275 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000276 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000277}
278
279void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000280 Expr *Init = getInit();
281 if (Init)
282 Init->Destroy(C);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000283 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000284 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000285}
286
287VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000288}
289
Douglas Gregor275a3692009-03-10 23:43:53 +0000290bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
291 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
292 return false;
293
294 return (!getInit() &&
295 (getStorageClass() == None || getStorageClass() == Static));
296}
297
Ted Kremenek082d9362009-03-20 21:35:28 +0000298const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Douglas Gregor275a3692009-03-10 23:43:53 +0000299 Def = this;
300 while (Def && !Def->getInit())
301 Def = Def->getPreviousDeclaration();
302
303 return Def? Def->getInit() : 0;
304}
305
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000306//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000307// FunctionDecl Implementation
308//===----------------------------------------------------------------------===//
309
Ted Kremenek27f8a282008-05-20 00:43:19 +0000310void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000311 if (Body && Body.isOffset())
312 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000313
314 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
315 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000316
Steve Naroff3e970492009-01-27 21:25:57 +0000317 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000318
Ted Kremenek27f8a282008-05-20 00:43:19 +0000319 Decl::Destroy(C);
320}
321
322
Douglas Gregor72971342009-04-18 00:02:19 +0000323CompoundStmt *FunctionDecl::getBody(ASTContext &Context,
324 const FunctionDecl *&Definition) const {
Douglas Gregorf0097952008-04-21 02:02:58 +0000325 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
326 if (FD->Body) {
327 Definition = FD;
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000328 return cast<CompoundStmt>(FD->Body.get(Context.getExternalSource()));
Douglas Gregorf0097952008-04-21 02:02:58 +0000329 }
330 }
331
332 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000333}
334
Douglas Gregor72971342009-04-18 00:02:19 +0000335CompoundStmt *FunctionDecl::getBodyIfAvailable() const {
336 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000337 if (FD->Body && !FD->Body.isOffset()) {
338 return cast<CompoundStmt>(FD->Body.get(0));
339 }
Douglas Gregor72971342009-04-18 00:02:19 +0000340 }
341
342 return 0;
343}
344
Douglas Gregor04495c82009-02-24 01:23:02 +0000345bool FunctionDecl::isMain() const {
346 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
347 getIdentifier() && getIdentifier()->isStr("main");
348}
349
Douglas Gregor63935192009-03-02 00:19:53 +0000350bool FunctionDecl::isExternC(ASTContext &Context) const {
351 // In C, any non-static, non-overloadable function has external
352 // linkage.
353 if (!Context.getLangOptions().CPlusPlus)
354 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
355
356 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
357 DC = DC->getParent()) {
358 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
359 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
360 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
361
362 break;
363 }
364 }
365
366 return false;
367}
368
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000369bool FunctionDecl::isGlobal() const {
370 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
371 return Method->isStatic();
372
373 if (getStorageClass() == Static)
374 return false;
375
376 for (const DeclContext *DC = getDeclContext();
377 DC->isNamespace();
378 DC = DC->getParent()) {
379 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
380 if (!Namespace->getDeclName())
381 return false;
382 break;
383 }
384 }
385
386 return true;
387}
388
Douglas Gregor3e41d602009-02-13 23:20:09 +0000389/// \brief Returns a value indicating whether this function
390/// corresponds to a builtin function.
391///
392/// The function corresponds to a built-in function if it is
393/// declared at translation scope or within an extern "C" block and
394/// its name matches with the name of a builtin. The returned value
395/// will be 0 for functions that do not correspond to a builtin, a
396/// value of type \c Builtin::ID if in the target-independent range
397/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000398unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
399 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
400 return 0;
401
402 unsigned BuiltinID = getIdentifier()->getBuiltinID();
403 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
404 return BuiltinID;
405
406 // This function has the name of a known C library
407 // function. Determine whether it actually refers to the C library
408 // function or whether it just has the same name.
409
Douglas Gregor9add3172009-02-17 03:23:10 +0000410 // If this is a static function, it's not a builtin.
411 if (getStorageClass() == Static)
412 return 0;
413
Douglas Gregor3c385e52009-02-14 18:57:46 +0000414 // If this function is at translation-unit scope and we're not in
415 // C++, it refers to the C library function.
416 if (!Context.getLangOptions().CPlusPlus &&
417 getDeclContext()->isTranslationUnit())
418 return BuiltinID;
419
420 // If the function is in an extern "C" linkage specification and is
421 // not marked "overloadable", it's the real function.
422 if (isa<LinkageSpecDecl>(getDeclContext()) &&
423 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
424 == LinkageSpecDecl::lang_c &&
425 !getAttr<OverloadableAttr>())
426 return BuiltinID;
427
428 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000429 return 0;
430}
431
432
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000433// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
434static unsigned getNumTypeParams(QualType T) {
435 const FunctionType *FT = T->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000436 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000437 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000438 return cast<FunctionProtoType>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000439}
440
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000441unsigned FunctionDecl::getNumParams() const {
442 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
443 if (!ParamInfo)
444 return 0;
445
446 return getNumTypeParams(getType());
447}
448
Ted Kremenekfc767612009-01-14 00:42:25 +0000449void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
450 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000452 assert(NumParams == getNumTypeParams(getType()) &&
453 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000454
455 // Zero params -> null pointer.
456 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000457 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000458 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
460 }
461}
462
Chris Lattner8123a952008-04-10 02:22:51 +0000463/// getMinRequiredArguments - Returns the minimum number of arguments
464/// needed to call this function. This may be fewer than the number of
465/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000466/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000467unsigned FunctionDecl::getMinRequiredArguments() const {
468 unsigned NumRequiredArgs = getNumParams();
469 while (NumRequiredArgs > 0
470 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
471 --NumRequiredArgs;
472
473 return NumRequiredArgs;
474}
475
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000476/// getOverloadedOperator - Which C++ overloaded operator this
477/// function represents, if any.
478OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000479 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
480 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000481 else
482 return OO_None;
483}
484
Chris Lattner8a934232008-03-31 00:36:02 +0000485//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000486// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000487//===----------------------------------------------------------------------===//
488
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000489void TagDecl::startDefinition() {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000490 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
491 TagT->decl.setPointer(this);
492 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000493}
494
495void TagDecl::completeDefinition() {
496 assert((!TypeForDecl ||
Douglas Gregorfc705b82009-02-26 22:19:44 +0000497 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000498 "Attempt to redefine a tag definition?");
499 IsDefinition = true;
Douglas Gregorfc705b82009-02-26 22:19:44 +0000500 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
501 TagT->decl.setPointer(this);
502 TagT->decl.setInt(0);
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000503}
504
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000505TagDecl* TagDecl::getDefinition(ASTContext& C) const {
506 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregorfc705b82009-02-26 22:19:44 +0000507 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000508 return D->isDefinition() ? D : 0;
509}
510
511//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000512// RecordDecl Implementation
513//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000514
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000515RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000516 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000517 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000518 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000519 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000520 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000521}
522
523RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000524 SourceLocation L, IdentifierInfo *Id,
525 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000526
Steve Naroff3e970492009-01-27 21:25:57 +0000527 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000528 C.getTypeDeclType(R, PrevDecl);
529 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000530}
531
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000532RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000533}
534
535void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000536 TagDecl::Destroy(C);
537}
538
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000539bool RecordDecl::isInjectedClassName() const {
540 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
541 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
542}
543
Douglas Gregor44b43212008-12-11 16:49:14 +0000544/// completeDefinition - Notes that the definition of this type is now
545/// complete.
546void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000548 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000549}
550
Steve Naroff56ee6892008-10-08 17:01:13 +0000551//===----------------------------------------------------------------------===//
552// BlockDecl Implementation
553//===----------------------------------------------------------------------===//
554
555BlockDecl::~BlockDecl() {
556}
557
558void BlockDecl::Destroy(ASTContext& C) {
559 if (Body)
560 Body->Destroy(C);
561
562 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
563 (*I)->Destroy(C);
Ted Kremenek879d27a2009-03-13 23:17:24 +0000564
565 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000566 Decl::Destroy(C);
567}
Steve Naroffe78b8092009-03-13 16:56:44 +0000568
569void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
570 unsigned NParms) {
571 assert(ParamInfo == 0 && "Already has param info!");
572
573 // Zero params -> null pointer.
574 if (NParms) {
575 NumParams = NParms;
576 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
577 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
578 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
579 }
580}
581
582unsigned BlockDecl::getNumParams() const {
583 return NumParams;
584}