blob: c62c1e2b0ead1a3b1207c3277d75ebcad055d109 [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"
Douglas Gregor9054f982009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnere4650482008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Stmt.h"
Nuno Lopesc98406e2008-12-17 23:39:55 +000020#include "clang/AST/Expr.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000021#include "clang/Basic/IdentifierTable.h"
Douglas Gregor09be81b2009-02-04 17:27:36 +000022#include <vector>
Ted Kremenekafdf8112008-05-20 00:43:19 +000023
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
Chris Lattner16ded572009-03-04 06:34:08 +000026void Attr::Destroy(ASTContext &C) {
27 if (Next) {
28 Next->Destroy(C);
29 Next = 0;
30 }
31 this->~Attr();
32 C.Deallocate((void*)this);
33}
34
35
Chris Lattnera8344c32008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000037// Decl Allocation/Deallocation Method Implementations
38//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000039
Chris Lattner16ded572009-03-04 06:34:08 +000040
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000041TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff5abb0282009-01-27 21:25:57 +000042 return new (C) TranslationUnitDecl();
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000043}
44
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000045NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
46 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff5abb0282009-01-27 21:25:57 +000047 return new (C) NamespaceDecl(DC, L, Id);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000048}
49
Ted Kremenek5be49242008-05-20 04:49:55 +000050void NamespaceDecl::Destroy(ASTContext& C) {
51 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
52 // together. They are all top-level Decls.
53
Ted Kremenek0f433842008-05-24 15:09:56 +000054 this->~NamespaceDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +000055 C.Deallocate((void *)this);
Ted Kremenek5be49242008-05-20 04:49:55 +000056}
57
58
Chris Lattner8c7c6a12008-06-17 18:05:57 +000059ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000060 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +000061 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000062}
63
Daniel Dunbarcaf78fb2009-04-14 02:08:49 +000064const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
65 switch (SC) {
66 case VarDecl::None: break;
67 case VarDecl::Auto: return "auto"; break;
68 case VarDecl::Extern: return "extern"; break;
69 case VarDecl::PrivateExtern: return "__private_extern__"; break;
70 case VarDecl::Register: return "register"; break;
71 case VarDecl::Static: return "static"; break;
72 }
73
74 assert(0 && "Invalid storage class");
75 return 0;
76}
77
Chris Lattneref87a202008-04-22 18:39:57 +000078ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000079 SourceLocation L, IdentifierInfo *Id,
80 QualType T, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000081 Expr *DefArg) {
Steve Naroff5abb0282009-01-27 21:25:57 +000082 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000083}
84
85QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000086 if (const OriginalParmVarDecl *PVD =
87 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniane26cb432008-12-20 23:29:59 +000088 return PVD->OriginalType;
89 return getType();
Chris Lattner48d225c2008-03-15 21:10:16 +000090}
91
Douglas Gregore6b5d1d2009-03-02 00:19:53 +000092bool VarDecl::isExternC(ASTContext &Context) const {
93 if (!Context.getLangOptions().CPlusPlus)
94 return (getDeclContext()->isTranslationUnit() &&
95 getStorageClass() != Static) ||
96 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
97
98 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
99 DC = DC->getParent()) {
100 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
101 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
102 return getStorageClass() != Static;
103
104 break;
105 }
106
107 if (DC->isFunctionOrMethod())
108 return false;
109 }
110
111 return false;
112}
113
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000114OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000115 ASTContext &C, DeclContext *DC,
116 SourceLocation L, IdentifierInfo *Id,
117 QualType T, QualType OT, StorageClass S,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000118 Expr *DefArg) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +0000119 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian160e8812008-12-20 20:56:12 +0000120}
121
Chris Lattneref87a202008-04-22 18:39:57 +0000122FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000123 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +0000124 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000125 StorageClass S, bool isInline,
Anders Carlssond9d6d502009-05-14 21:46:00 +0000126 bool hasWrittenPrototype,
Steve Naroff71cd7762008-10-03 00:02:03 +0000127 SourceLocation TypeSpecStartLoc) {
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000128 FunctionDecl *New
129 = new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
130 TypeSpecStartLoc);
Anders Carlssond9d6d502009-05-14 21:46:00 +0000131 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor1f88aa72009-02-25 16:33:18 +0000132 return New;
Chris Lattner4c7802b2008-03-15 21:24:04 +0000133}
134
Steve Naroff52059382008-10-10 01:28:17 +0000135BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000136 return new (C) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000137}
138
Douglas Gregor8acb7272008-12-11 16:49:14 +0000139FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
140 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000141 bool Mutable) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000142 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner81db64a2008-03-16 00:16:02 +0000143}
144
Douglas Gregorc7f01612009-01-07 19:46:03 +0000145bool FieldDecl::isAnonymousStructOrUnion() const {
146 if (!isImplicit() || getDeclName())
147 return false;
148
149 if (const RecordType *Record = getType()->getAsRecordType())
150 return Record->getDecl()->isAnonymousStructOrUnion();
151
152 return false;
153}
Chris Lattner4c7802b2008-03-15 21:24:04 +0000154
Chris Lattnereee57c02008-04-04 06:12:32 +0000155EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
156 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000157 IdentifierInfo *Id, QualType T,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000158 Expr *E, const llvm::APSInt &V) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000159 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnere4650482008-03-15 06:12:44 +0000160}
161
Ted Kremenek5be49242008-05-20 04:49:55 +0000162void EnumConstantDecl::Destroy(ASTContext& C) {
163 if (Init) Init->Destroy(C);
164 Decl::Destroy(C);
165}
166
Chris Lattneref87a202008-04-22 18:39:57 +0000167TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000168 SourceLocation L,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000169 IdentifierInfo *Id, QualType T) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000170 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnere4650482008-03-15 06:12:44 +0000171}
172
Chris Lattneref87a202008-04-22 18:39:57 +0000173EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000174 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000175 EnumDecl *PrevDecl) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000176 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregorae644892008-12-15 16:32:14 +0000177 C.getTypeDeclType(Enum, PrevDecl);
178 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000179}
180
Ted Kremenek25d8be12008-09-02 20:13:32 +0000181void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000182 Decl::Destroy(C);
183}
184
Douglas Gregor8acb7272008-12-11 16:49:14 +0000185void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
186 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000187 IntegerType = NewType;
Douglas Gregor98b27542009-01-17 00:42:38 +0000188 TagDecl::completeDefinition();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000189}
190
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000191FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000192 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000193 StringLiteral *Str) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000194 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner81db64a2008-03-16 00:16:02 +0000195}
196
Chris Lattnere4650482008-03-15 06:12:44 +0000197//===----------------------------------------------------------------------===//
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000198// NamedDecl Implementation
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000199//===----------------------------------------------------------------------===//
200
Douglas Gregor09be81b2009-02-04 17:27:36 +0000201std::string NamedDecl::getQualifiedNameAsString() const {
202 std::vector<std::string> Names;
203 std::string QualName;
204 const DeclContext *Ctx = getDeclContext();
205
206 if (Ctx->isFunctionOrMethod())
207 return getNameAsString();
208
209 while (Ctx) {
210 if (Ctx->isFunctionOrMethod())
211 // FIXME: That probably will happen, when D was member of local
212 // scope class/struct/union. How do we handle this case?
213 break;
214
215 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
216 Names.push_back(ND->getNameAsString());
217 else
218 break;
219
220 Ctx = Ctx->getParent();
221 }
222
223 std::vector<std::string>::reverse_iterator
224 I = Names.rbegin(),
225 End = Names.rend();
226
227 for (; I!=End; ++I)
228 QualName += *I + "::";
229
230 QualName += getNameAsString();
231
232 return QualName;
233}
234
235
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000236bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000237 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
238
Douglas Gregor7a7be652009-02-03 19:21:40 +0000239 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
240 // We want to keep it, unless it nominates same namespace.
241 if (getKind() == Decl::UsingDirective) {
242 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
243 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
244 }
245
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000246 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
247 // For function declarations, we keep track of redeclarations.
248 return FD->getPreviousDeclaration() == OldD;
249
Steve Naroffd85ba922009-02-22 19:35:57 +0000250 // For method declarations, we keep track of redeclarations.
251 if (isa<ObjCMethodDecl>(this))
252 return false;
253
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000254 // For non-function declarations, if the declarations are of the
255 // same kind then this must be a redeclaration, or semantic analysis
256 // would not have given us the new declaration.
257 return this->getKind() == OldD->getKind();
258}
259
Douglas Gregor1c52c632009-02-24 20:03:32 +0000260bool NamedDecl::hasLinkage() const {
261 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
262 return VD->hasExternalStorage() || VD->isFileVarDecl();
263
264 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
265 return true;
266
267 return false;
268}
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000269
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000270//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000271// VarDecl Implementation
272//===----------------------------------------------------------------------===//
273
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000274VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
275 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopesc98406e2008-12-17 23:39:55 +0000276 SourceLocation TypeSpecStartLoc) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000277 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000278}
279
280void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl2ee55612009-02-05 15:12:41 +0000281 Expr *Init = getInit();
282 if (Init)
283 Init->Destroy(C);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000284 this->~VarDecl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000285 C.Deallocate((void *)this);
Nuno Lopesc98406e2008-12-17 23:39:55 +0000286}
287
288VarDecl::~VarDecl() {
Nuno Lopesc98406e2008-12-17 23:39:55 +0000289}
290
Douglas Gregor2f728b22009-03-10 23:43:53 +0000291bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
292 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
293 return false;
294
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000295 const VarDecl *Def = 0;
296 return (!getDefinition(Def) &&
Douglas Gregor2f728b22009-03-10 23:43:53 +0000297 (getStorageClass() == None || getStorageClass() == Static));
298}
299
Ted Kremenek51787c72009-03-20 21:35:28 +0000300const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Douglas Gregor2f728b22009-03-10 23:43:53 +0000301 Def = this;
302 while (Def && !Def->getInit())
303 Def = Def->getPreviousDeclaration();
304
305 return Def? Def->getInit() : 0;
306}
307
Nuno Lopesc98406e2008-12-17 23:39:55 +0000308//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000309// FunctionDecl Implementation
310//===----------------------------------------------------------------------===//
311
Ted Kremenekafdf8112008-05-20 00:43:19 +0000312void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000313 if (Body && Body.isOffset())
314 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenek345b93d2008-05-20 03:56:00 +0000315
316 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
317 (*I)->Destroy(C);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000318
Steve Naroff5abb0282009-01-27 21:25:57 +0000319 C.Deallocate(ParamInfo);
Nuno Lopescb8cc5b2009-01-18 19:57:27 +0000320
Ted Kremenekafdf8112008-05-20 00:43:19 +0000321 Decl::Destroy(C);
322}
323
324
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000325Stmt *FunctionDecl::getBody(ASTContext &Context,
326 const FunctionDecl *&Definition) const {
Douglas Gregor42214c52008-04-21 02:02:58 +0000327 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
328 if (FD->Body) {
329 Definition = FD;
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000330 return FD->Body.get(Context.getExternalSource());
Douglas Gregor42214c52008-04-21 02:02:58 +0000331 }
332 }
333
334 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000335}
336
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000337Stmt *FunctionDecl::getBodyIfAvailable() const {
Douglas Gregore3241e92009-04-18 00:02:19 +0000338 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000339 if (FD->Body && !FD->Body.isOffset()) {
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000340 return FD->Body.get(0);
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000341 }
Douglas Gregore3241e92009-04-18 00:02:19 +0000342 }
343
344 return 0;
345}
346
Douglas Gregoraf682022009-02-24 01:23:02 +0000347bool FunctionDecl::isMain() const {
348 return getDeclContext()->getLookupContext()->isTranslationUnit() &&
349 getIdentifier() && getIdentifier()->isStr("main");
350}
351
Douglas Gregore6b5d1d2009-03-02 00:19:53 +0000352bool FunctionDecl::isExternC(ASTContext &Context) const {
353 // In C, any non-static, non-overloadable function has external
354 // linkage.
355 if (!Context.getLangOptions().CPlusPlus)
356 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
357
358 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
359 DC = DC->getParent()) {
360 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
361 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
362 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
363
364 break;
365 }
366 }
367
368 return false;
369}
370
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000371bool FunctionDecl::isGlobal() const {
372 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
373 return Method->isStatic();
374
375 if (getStorageClass() == Static)
376 return false;
377
378 for (const DeclContext *DC = getDeclContext();
379 DC->isNamespace();
380 DC = DC->getParent()) {
381 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
382 if (!Namespace->getDeclName())
383 return false;
384 break;
385 }
386 }
387
388 return true;
389}
390
Douglas Gregor411889e2009-02-13 23:20:09 +0000391/// \brief Returns a value indicating whether this function
392/// corresponds to a builtin function.
393///
394/// The function corresponds to a built-in function if it is
395/// declared at translation scope or within an extern "C" block and
396/// its name matches with the name of a builtin. The returned value
397/// will be 0 for functions that do not correspond to a builtin, a
398/// value of type \c Builtin::ID if in the target-independent range
399/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000400unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
401 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
402 return 0;
403
404 unsigned BuiltinID = getIdentifier()->getBuiltinID();
405 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
406 return BuiltinID;
407
408 // This function has the name of a known C library
409 // function. Determine whether it actually refers to the C library
410 // function or whether it just has the same name.
411
Douglas Gregor4d6b1022009-02-17 03:23:10 +0000412 // If this is a static function, it's not a builtin.
413 if (getStorageClass() == Static)
414 return 0;
415
Douglas Gregorb5af7382009-02-14 18:57:46 +0000416 // If this function is at translation-unit scope and we're not in
417 // C++, it refers to the C library function.
418 if (!Context.getLangOptions().CPlusPlus &&
419 getDeclContext()->isTranslationUnit())
420 return BuiltinID;
421
422 // If the function is in an extern "C" linkage specification and is
423 // not marked "overloadable", it's the real function.
424 if (isa<LinkageSpecDecl>(getDeclContext()) &&
425 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
426 == LinkageSpecDecl::lang_c &&
427 !getAttr<OverloadableAttr>())
428 return BuiltinID;
429
430 // Not a builtin
Douglas Gregor411889e2009-02-13 23:20:09 +0000431 return 0;
432}
433
434
Chris Lattnerd2112022009-04-25 06:03:53 +0000435/// getNumParams - Return the number of parameters this function must have
Chris Lattnerc13d4732009-04-25 06:12:16 +0000436/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattnerd2112022009-04-25 06:03:53 +0000437/// after it has been created.
438unsigned FunctionDecl::getNumParams() const {
Chris Lattner9d957cb2009-04-25 05:56:45 +0000439 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +0000440 if (isa<FunctionNoProtoType>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000441 return 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000442 return cast<FunctionProtoType>(FT)->getNumArgs();
Chris Lattner9d957cb2009-04-25 05:56:45 +0000443
Chris Lattner4b009652007-07-25 00:24:17 +0000444}
445
Ted Kremenek8494c962009-01-14 00:42:25 +0000446void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
447 unsigned NumParams) {
Chris Lattner4b009652007-07-25 00:24:17 +0000448 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattnerc13d4732009-04-25 06:12:16 +0000449 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000450
451 // Zero params -> null pointer.
452 if (NumParams) {
Steve Naroff207b9ec2009-01-27 23:20:32 +0000453 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek8494c962009-01-14 00:42:25 +0000454 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner4b009652007-07-25 00:24:17 +0000455 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
456 }
457}
458
Chris Lattner97316c02008-04-10 02:22:51 +0000459/// getMinRequiredArguments - Returns the minimum number of arguments
460/// needed to call this function. This may be fewer than the number of
461/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000462/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000463unsigned FunctionDecl::getMinRequiredArguments() const {
464 unsigned NumRequiredArgs = getNumParams();
465 while (NumRequiredArgs > 0
466 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
467 --NumRequiredArgs;
468
469 return NumRequiredArgs;
470}
471
Douglas Gregor67e11442009-04-28 06:37:30 +0000472bool FunctionDecl::hasActiveGNUInlineAttribute() const {
473 if (!isInline() || !hasAttr<GNUInlineAttr>())
474 return false;
475
476 for (const FunctionDecl *FD = getPreviousDeclaration(); FD;
477 FD = FD->getPreviousDeclaration()) {
478 if (FD->isInline() && !FD->hasAttr<GNUInlineAttr>())
479 return false;
480 }
481
482 return true;
483}
484
485bool FunctionDecl::isExternGNUInline() const {
486 if (!hasActiveGNUInlineAttribute())
487 return false;
488
489 for (const FunctionDecl *FD = this; FD; FD = FD->getPreviousDeclaration())
490 if (FD->getStorageClass() == Extern && FD->hasAttr<GNUInlineAttr>())
491 return true;
492
493 return false;
494}
495
Douglas Gregore60e5d32008-11-06 22:13:31 +0000496/// getOverloadedOperator - Which C++ overloaded operator this
497/// function represents, if any.
498OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000499 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
500 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000501 else
502 return OO_None;
503}
504
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000505//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000506// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000507//===----------------------------------------------------------------------===//
508
Douglas Gregor9054f982009-05-10 22:57:19 +0000509bool TagDecl::isDependentType() const {
Douglas Gregor3eb20702009-05-11 19:58:34 +0000510 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
511 if (Record->getDescribedClassTemplate())
512 return true;
513
Douglas Gregor9054f982009-05-10 22:57:19 +0000514 if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(getDeclContext()))
515 return TD->isDependentType();
516
517 // FIXME: Tag types declared function templates are dependent types.
518 // FIXME: Look through block scopes.
519 return false;
520}
521
Douglas Gregor98b27542009-01-17 00:42:38 +0000522void TagDecl::startDefinition() {
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000523 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
524 TagT->decl.setPointer(this);
525 TagT->getAsTagType()->decl.setInt(1);
Douglas Gregor98b27542009-01-17 00:42:38 +0000526}
527
528void TagDecl::completeDefinition() {
529 assert((!TypeForDecl ||
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000530 TypeForDecl->getAsTagType()->decl.getPointer() == this) &&
Douglas Gregor98b27542009-01-17 00:42:38 +0000531 "Attempt to redefine a tag definition?");
532 IsDefinition = true;
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000533 TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType());
534 TagT->decl.setPointer(this);
535 TagT->decl.setInt(0);
Douglas Gregor98b27542009-01-17 00:42:38 +0000536}
537
Ted Kremenek46a837c2008-09-05 17:16:31 +0000538TagDecl* TagDecl::getDefinition(ASTContext& C) const {
539 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000540 TagDecl* D = cast<TagDecl>(T->getAsTagType()->getDecl());
Ted Kremenek46a837c2008-09-05 17:16:31 +0000541 return D->isDefinition() ? D : 0;
542}
543
544//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000545// RecordDecl Implementation
546//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000547
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000548RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000549 IdentifierInfo *Id)
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000550 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000551 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000552 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000553 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000554}
555
556RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000557 SourceLocation L, IdentifierInfo *Id,
558 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000559
Steve Naroff5abb0282009-01-27 21:25:57 +0000560 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000561 C.getTypeDeclType(R, PrevDecl);
562 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000563}
564
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000565RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000566}
567
568void RecordDecl::Destroy(ASTContext& C) {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000569 TagDecl::Destroy(C);
570}
571
Douglas Gregor43bfaaf2009-03-25 15:59:44 +0000572bool RecordDecl::isInjectedClassName() const {
573 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
574 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
575}
576
Douglas Gregor8acb7272008-12-11 16:49:14 +0000577/// completeDefinition - Notes that the definition of this type is now
578/// complete.
579void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000580 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor98b27542009-01-17 00:42:38 +0000581 TagDecl::completeDefinition();
Chris Lattner4b009652007-07-25 00:24:17 +0000582}
583
Steve Naroff9ac456d2008-10-08 17:01:13 +0000584//===----------------------------------------------------------------------===//
585// BlockDecl Implementation
586//===----------------------------------------------------------------------===//
587
588BlockDecl::~BlockDecl() {
589}
590
591void BlockDecl::Destroy(ASTContext& C) {
592 if (Body)
593 Body->Destroy(C);
594
595 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
596 (*I)->Destroy(C);
Ted Kremenek4a71fb12009-03-13 23:17:24 +0000597
598 C.Deallocate(ParamInfo);
Steve Naroff9ac456d2008-10-08 17:01:13 +0000599 Decl::Destroy(C);
600}
Steve Naroff494cb0f2009-03-13 16:56:44 +0000601
602void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
603 unsigned NParms) {
604 assert(ParamInfo == 0 && "Already has param info!");
605
606 // Zero params -> null pointer.
607 if (NParms) {
608 NumParams = NParms;
609 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
610 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
611 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
612 }
613}
614
615unsigned BlockDecl::getNumParams() const {
616 return NumParams;
617}