blob: 0326b34960fc281fdd8760223218fb07f8834ab8 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Argiris Kirtzidise7dfca12008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor7a7be652009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffd85ba922009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Chris Lattnere4650482008-03-15 06:12:44 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Stmt.h"
Nuno Lopesc98406e2008-12-17 23:39:55 +000019#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/Basic/IdentifierTable.h"
Douglas Gregor09be81b2009-02-04 17:27:36 +000021#include <vector>
Ted Kremenekafdf8112008-05-20 00:43:19 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
Chris Lattner16ded572009-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 Lattnera8344c32008-03-15 05:43:15 +000035//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000036// Decl Allocation/Deallocation Method Implementations
37//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000038
Chris Lattner16ded572009-03-04 06:34:08 +000039
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000040TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff5abb0282009-01-27 21:25:57 +000041 return new (C) TranslationUnitDecl();
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000042}
43
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000044NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
45 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000046 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000047}
48
Ted Kremenek5be49242008-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 Kremenek0f433842008-05-24 15:09:56 +000053 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000054 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000055}
56
57
Chris Lattner8c7c6a12008-06-17 18:05:57 +000058ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000059 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000060 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000061}
62
Daniel Dunbarcaf78fb2009-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 Lattneref87a202008-04-22 18:39:57 +000077ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000078 SourceLocation L, IdentifierInfo *Id,
79 QualType T, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000080 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000081 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000082}
83
84QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000085 if (const OriginalParmVarDecl *PVD =
86 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000087 return PVD->OriginalType;
88 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000089}
90
Douglas Gregore6b5d1d2009-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 Gregor469fc9a2009-02-02 23:39:07 +0000113OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000114 ASTContext &C, DeclContext *DC,
115 SourceLocation L, IdentifierInfo *Id,
116 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000117 Expr *DefArg) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000118 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000119}
120
Chris Lattneref87a202008-04-22 18:39:57 +0000121FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000122 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +0000123 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000124 StorageClass S, bool isInline,
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000125 bool hasPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +0000126 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-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 Lattner4c7802b2008-03-15 21:24:04 +0000132}
133
Steve Naroff52059382008-10-10 01:28:17 +0000134BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000135 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000136}
137
Douglas Gregor8acb7272008-12-11 16:49:14 +0000138FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
139 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000140 bool Mutable) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000141 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +0000142}
143
Douglas Gregorc7f01612009-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 Lattner4c7802b2008-03-15 21:24:04 +0000153
Chris Lattnereee57c02008-04-04 06:12:32 +0000154EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
155 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000156 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000157 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000158 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000159}
160
Ted Kremenek5be49242008-05-20 04:49:55 +0000161void EnumConstantDecl::Destroy(ASTContext& C) {
162 if (Init) Init->Destroy(C);
163 Decl::Destroy(C);
164}
165
Chris Lattneref87a202008-04-22 18:39:57 +0000166TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000167 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000168 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000169 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000170}
171
Chris Lattneref87a202008-04-22 18:39:57 +0000172EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000173 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000174 EnumDecl *PrevDecl) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000175 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000176 C.getTypeDeclType(Enum, PrevDecl);
177 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000178}
179
Ted Kremenek25d8be12008-09-02 20:13:32 +0000180void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000181 Decl::Destroy(C);
182}
183
Douglas Gregor8acb7272008-12-11 16:49:14 +0000184void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
185 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000186 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000187 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000188}
189
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000190FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000191 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000192 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000193 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000194}
195
Chris Lattnere4650482008-03-15 06:12:44 +0000196//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000197// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000198//===----------------------------------------------------------------------===//
199
Douglas Gregor09be81b2009-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 Gregoraf8ad2b2009-01-20 01:17:11 +0000235bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000236 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
237
Douglas Gregor7a7be652009-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 Gregor6e71edc2008-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 Naroffd85ba922009-02-22 19:35:57 +0000249 // For method declarations, we keep track of redeclarations.
250 if (isa<ObjCMethodDecl>(this))
251 return false;
252
Douglas Gregor6e71edc2008-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 Gregor1c52c632009-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 Gregoraf8ad2b2009-01-20 01:17:11 +0000268
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000269//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000270// VarDecl Implementation
271//===----------------------------------------------------------------------===//
272
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000273VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
274 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000275 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000276 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000277}
278
279void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000280 Expr *Init = getInit();
281 if (Init)
282 Init->Destroy(C);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000283 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000284 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000285}
286
287VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000288}
289
Douglas Gregor2f728b22009-03-10 23:43:53 +0000290bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
291 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
292 return false;
293
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000294 const VarDecl *Def = 0;
295 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000296 (getStorageClass() == None || getStorageClass() == Static));
297}
298
Ted Kremenek51787c72009-03-20 21:35:28 +0000299const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Douglas Gregor2f728b22009-03-10 23:43:53 +0000300 Def = this;
301 while (Def && !Def->getInit())
302 Def = Def->getPreviousDeclaration();
303
304 return Def? Def->getInit() : 0;
305}
306
Nuno Lopesc98406e2008-12-17 23:39:55 +0000307//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000308// FunctionDecl Implementation
309//===----------------------------------------------------------------------===//
310
Ted Kremenekafdf8112008-05-20 00:43:19 +0000311void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000312 if (Body && Body.isOffset())
313 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000314
315 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
316 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000317
Steve Naroff5abb0282009-01-27 21:25:57 +0000318 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000319
Ted Kremenekafdf8112008-05-20 00:43:19 +0000320 Decl::Destroy(C);
321}
322
323
Douglas Gregore3241e92009-04-18 00:02:19 +0000324CompoundStmt *FunctionDecl::getBody(ASTContext &Context,
325 const FunctionDecl *&Definition) const {
Douglas Gregor42214c52008-04-21 02:02:58 +0000326 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
327 if (FD->Body) {
328 Definition = FD;
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000329 return cast<CompoundStmt>(FD->Body.get(Context.getExternalSource()));
Douglas Gregor42214c52008-04-21 02:02:58 +0000330 }
331 }
332
333 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000334}
335
Douglas Gregore3241e92009-04-18 00:02:19 +0000336CompoundStmt *FunctionDecl::getBodyIfAvailable() const {
337 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000338 if (FD->Body && !FD->Body.isOffset()) {
339 return cast<CompoundStmt>(FD->Body.get(0));
340 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000341 }
342
343 return 0;
344}
345
Douglas Gregoraf682022009-02-24 01:23:02 +0000346bool FunctionDecl::isMain() const {
347 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
348 getIdentifier() && getIdentifier()->isStr("main");
349}
350
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000351bool FunctionDecl::isExternC(ASTContext &Context) const {
352 // In C, any non-static, non-overloadable function has external
353 // linkage.
354 if (!Context.getLangOptions().CPlusPlus)
355 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
356
357 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
358 DC = DC->getParent()) {
359 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
360 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
361 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
362
363 break;
364 }
365 }
366
367 return false;
368}
369
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000370bool FunctionDecl::isGlobal() const {
371 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
372 return Method->isStatic();
373
374 if (getStorageClass() == Static)
375 return false;
376
377 for (const DeclContext *DC = getDeclContext();
378 DC->isNamespace();
379 DC = DC->getParent()) {
380 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
381 if (!Namespace->getDeclName())
382 return false;
383 break;
384 }
385 }
386
387 return true;
388}
389
Douglas Gregor411889e2009-02-13 23:20:09 +0000390/// \brief Returns a value indicating whether this function
391/// corresponds to a builtin function.
392///
393/// The function corresponds to a built-in function if it is
394/// declared at translation scope or within an extern "C" block and
395/// its name matches with the name of a builtin. The returned value
396/// will be 0 for functions that do not correspond to a builtin, a
397/// value of type \c Builtin::ID if in the target-independent range
398/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000399unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
400 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
401 return 0;
402
403 unsigned BuiltinID = getIdentifier()->getBuiltinID();
404 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
405 return BuiltinID;
406
407 // This function has the name of a known C library
408 // function. Determine whether it actually refers to the C library
409 // function or whether it just has the same name.
410
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000411 // If this is a static function, it's not a builtin.
412 if (getStorageClass() == Static)
413 return 0;
414
Douglas Gregorb5af7382009-02-14 18:57:46 +0000415 // If this function is at translation-unit scope and we're not in
416 // C++, it refers to the C library function.
417 if (!Context.getLangOptions().CPlusPlus &&
418 getDeclContext()->isTranslationUnit())
419 return BuiltinID;
420
421 // If the function is in an extern "C" linkage specification and is
422 // not marked "overloadable", it's the real function.
423 if (isa<LinkageSpecDecl>(getDeclContext()) &&
424 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
425 == LinkageSpecDecl::lang_c &&
426 !getAttr<OverloadableAttr>())
427 return BuiltinID;
428
429 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000430 return 0;
431}
432
433
Ted Kremeneka6ded352008-10-29 18:41:34 +0000434// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
435static unsigned getNumTypeParams(QualType T) {
436 const FunctionType *FT = T->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000437 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000438 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000439 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000440}
441
Ted Kremeneka6ded352008-10-29 18:41:34 +0000442unsigned FunctionDecl::getNumParams() const {
443 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
444 if (!ParamInfo)
445 return 0;
446
447 return getNumTypeParams(getType());
448}
449
Ted Kremenek8494c962009-01-14 00:42:25 +0000450void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
451 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000452 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000453 assert(NumParams == getNumTypeParams(getType()) &&
454 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000455
456 // Zero params -> null pointer.
457 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000458 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000459 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000460 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
461 }
462}
463
Chris Lattner97316c02008-04-10 02:22:51 +0000464/// getMinRequiredArguments - Returns the minimum number of arguments
465/// needed to call this function. This may be fewer than the number of
466/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000467/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000468unsigned FunctionDecl::getMinRequiredArguments() const {
469 unsigned NumRequiredArgs = getNumParams();
470 while (NumRequiredArgs > 0
471 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
472 --NumRequiredArgs;
473
474 return NumRequiredArgs;
475}
476
Douglas Gregore60e5d32008-11-06 22:13:31 +0000477/// getOverloadedOperator - Which C++ overloaded operator this
478/// function represents, if any.
479OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000480 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
481 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000482 else
483 return OO_None;
484}
485
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000486//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000487// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000488//===----------------------------------------------------------------------===//
489
Douglas Gregor98b27542009-01-17 00:42:38 +0000490void TagDecl::startDefinition() {
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000491 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
492 TagT->decl.setPointer(this);
493 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor98b27542009-01-17 00:42:38 +0000494}
495
496void TagDecl::completeDefinition() {
497 assert((!TypeForDecl ||
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000498 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor98b27542009-01-17 00:42:38 +0000499 "Attempt to redefine a tag definition?");
500 IsDefinition = true;
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000501 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
502 TagT->decl.setPointer(this);
503 TagT->decl.setInt(0);
Douglas Gregor98b27542009-01-17 00:42:38 +0000504}
505
Ted Kremenek46a837c2008-09-05 17:16:31 +0000506TagDecl* TagDecl::getDefinition(ASTContext& C) const {
507 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000508 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek46a837c2008-09-05 17:16:31 +0000509 return D->isDefinition() ? D : 0;
510}
511
512//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000513// RecordDecl Implementation
514//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000515
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000516RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000517 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000518 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000519 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000520 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000521 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000522}
523
524RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000525 SourceLocation L, IdentifierInfo *Id,
526 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000527
Steve Naroff5abb0282009-01-27 21:25:57 +0000528 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000529 C.getTypeDeclType(R, PrevDecl);
530 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000531}
532
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000533RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000534}
535
536void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000537 TagDecl::Destroy(C);
538}
539
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000540bool RecordDecl::isInjectedClassName() const {
541 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
542 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
543}
544
Douglas Gregor8acb7272008-12-11 16:49:14 +0000545/// completeDefinition - Notes that the definition of this type is now
546/// complete.
547void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000548 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000549 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000550}
551
Steve Naroff9ac456d2008-10-08 17:01:13 +0000552//===----------------------------------------------------------------------===//
553// BlockDecl Implementation
554//===----------------------------------------------------------------------===//
555
556BlockDecl::~BlockDecl() {
557}
558
559void BlockDecl::Destroy(ASTContext& C) {
560 if (Body)
561 Body->Destroy(C);
562
563 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
564 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000565
566 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000567 Decl::Destroy(C);
568}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000569
570void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
571 unsigned NParms) {
572 assert(ParamInfo == 0 && "Already has param info!");
573
574 // Zero params -> null pointer.
575 if (NParms) {
576 NumParams = NParms;
577 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
578 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
579 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
580 }
581}
582
583unsigned BlockDecl::getNumParams() const {
584 return NumParams;
585}