blob: a7d8216b0058305941c74f2a293daa0d0dbfdb00 [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"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000024#include "clang/Basic/IdentifierTable.h"
John McCallf1bbbb42009-09-04 01:14:41 +000025#include "clang/Parse/DeclSpec.h"
26#include "llvm/Support/ErrorHandling.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000027#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattner0b2b6e12009-03-04 06:34:08 +000031void Attr::Destroy(ASTContext &C) {
32 if (Next) {
33 Next->Destroy(C);
34 Next = 0;
35 }
36 this->~Attr();
37 C.Deallocate((void*)this);
38}
39
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000040/// \brief Return the TypeLoc wrapper for the type source info.
41TypeLoc DeclaratorInfo::getTypeLoc() const {
42 return TypeLoc::Create(Ty, (void*)(this + 1));
43}
Chris Lattner0b2b6e12009-03-04 06:34:08 +000044
Chris Lattnerd3b90652008-03-15 05:43:15 +000045//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000046// Decl Allocation/Deallocation Method Implementations
47//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattner0b2b6e12009-03-04 06:34:08 +000049
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000050TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +000051 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000052}
53
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000054NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
55 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000056 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000057}
58
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000059void NamespaceDecl::Destroy(ASTContext& C) {
60 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
61 // together. They are all top-level Decls.
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenekebf27b12008-05-24 15:09:56 +000063 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000065}
66
67
Chris Lattner41110242008-06-17 18:05:57 +000068ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000069 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000070 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000071}
72
Daniel Dunbarb286a782009-04-14 02:08:49 +000073const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
74 switch (SC) {
75 case VarDecl::None: break;
76 case VarDecl::Auto: return "auto"; break;
77 case VarDecl::Extern: return "extern"; break;
Mike Stump1eb44332009-09-09 15:08:12 +000078 case VarDecl::PrivateExtern: return "__private_extern__"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000079 case VarDecl::Register: return "register"; break;
Mike Stump1eb44332009-09-09 15:08:12 +000080 case VarDecl::Static: return "static"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000081 }
82
83 assert(0 && "Invalid storage class");
84 return 0;
85}
86
Chris Lattner9fdf9c62008-04-22 18:39:57 +000087ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000088 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +000089 QualType T, DeclaratorInfo *DInfo,
90 StorageClass S, Expr *DefArg) {
91 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, DInfo, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000092}
93
94QualType ParmVarDecl::getOriginalType() const {
Mike Stump1eb44332009-09-09 15:08:12 +000095 if (const OriginalParmVarDecl *PVD =
Douglas Gregor64650af2009-02-02 23:39:07 +000096 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000097 return PVD->OriginalType;
98 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000099}
100
Douglas Gregor6cc15182009-09-11 18:44:32 +0000101SourceRange ParmVarDecl::getDefaultArgRange() const {
102 if (const Expr *E = getInit())
103 return E->getSourceRange();
104
105 if (const Expr *E = getUninstantiatedDefaultArg())
106 return E->getSourceRange();
107
108 return SourceRange();
109}
Douglas Gregor78d15832009-05-26 18:54:04 +0000110
Douglas Gregor6cc15182009-09-11 18:44:32 +0000111void VarDecl::setInit(ASTContext &C, Expr *I) {
112 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
113 Eval->~EvaluatedStmt();
114 C.Deallocate(Eval);
Douglas Gregor78d15832009-05-26 18:54:04 +0000115 }
116
Douglas Gregor6cc15182009-09-11 18:44:32 +0000117 Init = I;
118}
119
Douglas Gregor63935192009-03-02 00:19:53 +0000120bool VarDecl::isExternC(ASTContext &Context) const {
121 if (!Context.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000122 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000123 getStorageClass() != Static) ||
124 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
125
Mike Stump1eb44332009-09-09 15:08:12 +0000126 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000127 DC = DC->getParent()) {
128 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
129 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
130 return getStorageClass() != Static;
131
132 break;
133 }
134
135 if (DC->isFunctionOrMethod())
136 return false;
137 }
138
139 return false;
140}
141
Douglas Gregor64650af2009-02-02 23:39:07 +0000142OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000143 ASTContext &C, DeclContext *DC,
144 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000145 QualType T, DeclaratorInfo *DInfo,
146 QualType OT, StorageClass S, Expr *DefArg) {
147 return new (C) OriginalParmVarDecl(DC, L, Id, T, DInfo, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000148}
149
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000150FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000151 SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000152 DeclarationName N, QualType T,
153 DeclaratorInfo *DInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000154 StorageClass S, bool isInline,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000155 bool hasWrittenPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +0000156 FunctionDecl *New
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000157 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssona75e8532009-05-14 21:46:00 +0000158 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor2224f842009-02-25 16:33:18 +0000159 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000160}
161
Steve Naroff090276f2008-10-10 01:28:17 +0000162BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000163 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000164}
165
Douglas Gregor44b43212008-12-11 16:49:14 +0000166FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000167 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000168 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
169 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000170}
171
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000172bool FieldDecl::isAnonymousStructOrUnion() const {
173 if (!isImplicit() || getDeclName())
174 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek6217b802009-07-29 21:53:49 +0000176 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000177 return Record->getDecl()->isAnonymousStructOrUnion();
178
179 return false;
180}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000181
Chris Lattner0ed844b2008-04-04 06:12:32 +0000182EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
183 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000184 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000185 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000186 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000187}
188
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000189void EnumConstantDecl::Destroy(ASTContext& C) {
190 if (Init) Init->Destroy(C);
191 Decl::Destroy(C);
192}
193
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000194TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000195 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000196 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000197 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000198}
199
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000200EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000201 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000202 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000203 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000204 C.getTypeDeclType(Enum, PrevDecl);
205 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000206}
207
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000208void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000209 Decl::Destroy(C);
210}
211
Douglas Gregor44b43212008-12-11 16:49:14 +0000212void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
213 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000214 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000215 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000216}
217
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000218FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000219 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000220 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000221 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000222}
223
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000224//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000225// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000226//===----------------------------------------------------------------------===//
227
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000228std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000229 return getQualifiedNameAsString(getASTContext().getLangOptions());
230}
231
232std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000233 std::vector<std::string> Names;
234 std::string QualName;
235 const DeclContext *Ctx = getDeclContext();
236
237 if (Ctx->isFunctionOrMethod())
238 return getNameAsString();
239
240 while (Ctx) {
241 if (Ctx->isFunctionOrMethod())
242 // FIXME: That probably will happen, when D was member of local
243 // scope class/struct/union. How do we handle this case?
244 break;
245
Mike Stump1eb44332009-09-09 15:08:12 +0000246 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000247 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
248 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
249 std::string TemplateArgsStr
250 = TemplateSpecializationType::PrintTemplateArgumentList(
251 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000252 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000253 P);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000254 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
255 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000256 Names.push_back(ND->getNameAsString());
257 else
258 break;
259
260 Ctx = Ctx->getParent();
261 }
262
263 std::vector<std::string>::reverse_iterator
264 I = Names.rbegin(),
265 End = Names.rend();
266
267 for (; I!=End; ++I)
268 QualName += *I + "::";
269
270 QualName += getNameAsString();
271
272 return QualName;
273}
274
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000275bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000276 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
277
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000278 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
279 // We want to keep it, unless it nominates same namespace.
280 if (getKind() == Decl::UsingDirective) {
281 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
282 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000285 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
286 // For function declarations, we keep track of redeclarations.
287 return FD->getPreviousDeclaration() == OldD;
288
Douglas Gregore53060f2009-06-25 22:08:12 +0000289 // For function templates, the underlying function declarations are linked.
290 if (const FunctionTemplateDecl *FunctionTemplate
291 = dyn_cast<FunctionTemplateDecl>(this))
292 if (const FunctionTemplateDecl *OldFunctionTemplate
293 = dyn_cast<FunctionTemplateDecl>(OldD))
294 return FunctionTemplate->getTemplatedDecl()
295 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Steve Naroff0de21fd2009-02-22 19:35:57 +0000297 // For method declarations, we keep track of redeclarations.
298 if (isa<ObjCMethodDecl>(this))
299 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000301 // For non-function declarations, if the declarations are of the
302 // same kind then this must be a redeclaration, or semantic analysis
303 // would not have given us the new declaration.
304 return this->getKind() == OldD->getKind();
305}
306
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000307bool NamedDecl::hasLinkage() const {
308 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
309 return VD->hasExternalStorage() || VD->isFileVarDecl();
310
311 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
312 return true;
313
314 return false;
315}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000316
Anders Carlssone136e0e2009-06-26 06:29:23 +0000317NamedDecl *NamedDecl::getUnderlyingDecl() {
318 NamedDecl *ND = this;
319 while (true) {
320 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
321 ND = UD->getTargetDecl();
322 else if (ObjCCompatibleAliasDecl *AD
323 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
324 return AD->getClassInterface();
325 else
326 return ND;
327 }
328}
329
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000330//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000331// DeclaratorDecl Implementation
332//===----------------------------------------------------------------------===//
333
334SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
335 if (DeclInfo)
336 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
337 return SourceLocation();
338}
339
340//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000341// VarDecl Implementation
342//===----------------------------------------------------------------------===//
343
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000344VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000345 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000346 StorageClass S) {
347 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000348}
349
350void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000351 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000352 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000353 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000354 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
355 Eval->~EvaluatedStmt();
356 C.Deallocate(Eval);
357 }
358 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000359 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000360 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000361}
362
363VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000364}
365
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000366SourceRange VarDecl::getSourceRange() const {
367 if (getInit())
368 return SourceRange(getLocation(), getInit()->getLocEnd());
369 return SourceRange(getLocation(), getLocation());
370}
371
Douglas Gregor7caa6822009-07-24 20:34:43 +0000372VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
373 return getASTContext().getInstantiatedFromStaticDataMember(this);
374}
375
Douglas Gregor275a3692009-03-10 23:43:53 +0000376bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
377 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
378 return false;
379
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000380 const VarDecl *Def = 0;
381 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000382 (getStorageClass() == None || getStorageClass() == Static));
383}
384
Ted Kremenek082d9362009-03-20 21:35:28 +0000385const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000386 redecl_iterator I = redecls_begin(), E = redecls_end();
387 while (I != E && !I->getInit())
388 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000389
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000390 if (I != E) {
391 Def = *I;
392 return I->getInit();
393 }
394 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000395}
396
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000397VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000398 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000399}
400
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000401//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000402// FunctionDecl Implementation
403//===----------------------------------------------------------------------===//
404
Ted Kremenek27f8a282008-05-20 00:43:19 +0000405void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000406 if (Body && Body.isOffset())
407 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000408
409 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
410 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000411
Steve Naroff3e970492009-01-27 21:25:57 +0000412 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000413
Ted Kremenek27f8a282008-05-20 00:43:19 +0000414 Decl::Destroy(C);
415}
416
John McCall136a6982009-09-11 06:45:03 +0000417void FunctionDecl::getNameForDiagnostic(std::string &S,
418 const PrintingPolicy &Policy,
419 bool Qualified) const {
420 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
421 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
422 if (TemplateArgs)
423 S += TemplateSpecializationType::PrintTemplateArgumentList(
424 TemplateArgs->getFlatArgumentList(),
425 TemplateArgs->flat_size(),
426 Policy);
427
428}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000429
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000430Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000431 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
432 if (I->Body) {
433 Definition = *I;
434 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000435 }
436 }
437
438 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000439}
440
Sebastian Redld3a413d2009-04-26 20:35:05 +0000441Stmt *FunctionDecl::getBodyIfAvailable() const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000442 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
443 if (I->Body && !I->Body.isOffset()) {
444 return I->Body.get(0);
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000445 }
Douglas Gregor72971342009-04-18 00:02:19 +0000446 }
447
448 return 0;
449}
450
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000451void FunctionDecl::setBody(Stmt *B) {
452 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000453 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000454 EndRangeLoc = B->getLocEnd();
455}
456
John McCall07a5c222009-08-15 02:09:25 +0000457bool FunctionDecl::isMain(ASTContext &Context) const {
458 return !Context.getLangOptions().Freestanding &&
459 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000460 getIdentifier() && getIdentifier()->isStr("main");
461}
462
Douglas Gregor63935192009-03-02 00:19:53 +0000463bool FunctionDecl::isExternC(ASTContext &Context) const {
464 // In C, any non-static, non-overloadable function has external
465 // linkage.
466 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000467 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000468
Mike Stump1eb44332009-09-09 15:08:12 +0000469 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000470 DC = DC->getParent()) {
471 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
472 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000473 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000474 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000475
476 break;
477 }
478 }
479
480 return false;
481}
482
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000483bool FunctionDecl::isGlobal() const {
484 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
485 return Method->isStatic();
486
487 if (getStorageClass() == Static)
488 return false;
489
Mike Stump1eb44332009-09-09 15:08:12 +0000490 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000491 DC->isNamespace();
492 DC = DC->getParent()) {
493 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
494 if (!Namespace->getDeclName())
495 return false;
496 break;
497 }
498 }
499
500 return true;
501}
502
Douglas Gregor3e41d602009-02-13 23:20:09 +0000503/// \brief Returns a value indicating whether this function
504/// corresponds to a builtin function.
505///
506/// The function corresponds to a built-in function if it is
507/// declared at translation scope or within an extern "C" block and
508/// its name matches with the name of a builtin. The returned value
509/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000510/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000511/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000512unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
513 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
514 return 0;
515
516 unsigned BuiltinID = getIdentifier()->getBuiltinID();
517 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
518 return BuiltinID;
519
520 // This function has the name of a known C library
521 // function. Determine whether it actually refers to the C library
522 // function or whether it just has the same name.
523
Douglas Gregor9add3172009-02-17 03:23:10 +0000524 // If this is a static function, it's not a builtin.
525 if (getStorageClass() == Static)
526 return 0;
527
Douglas Gregor3c385e52009-02-14 18:57:46 +0000528 // If this function is at translation-unit scope and we're not in
529 // C++, it refers to the C library function.
530 if (!Context.getLangOptions().CPlusPlus &&
531 getDeclContext()->isTranslationUnit())
532 return BuiltinID;
533
534 // If the function is in an extern "C" linkage specification and is
535 // not marked "overloadable", it's the real function.
536 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000537 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000538 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000539 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000540 return BuiltinID;
541
542 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000543 return 0;
544}
545
546
Chris Lattner1ad9b282009-04-25 06:03:53 +0000547/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000548/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000549/// after it has been created.
550unsigned FunctionDecl::getNumParams() const {
Chris Lattner11ddb7d2009-04-25 05:56:45 +0000551 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000552 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000553 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000554 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556}
557
Ted Kremenekfc767612009-01-14 00:42:25 +0000558void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
559 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000561 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 // Zero params -> null pointer.
564 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000565 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000566 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000568
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000569 // Update source range. The check below allows us to set EndRangeLoc before
570 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000571 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000572 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 }
574}
575
Chris Lattner8123a952008-04-10 02:22:51 +0000576/// getMinRequiredArguments - Returns the minimum number of arguments
577/// needed to call this function. This may be fewer than the number of
578/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000579/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000580unsigned FunctionDecl::getMinRequiredArguments() const {
581 unsigned NumRequiredArgs = getNumParams();
582 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000583 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000584 --NumRequiredArgs;
585
586 return NumRequiredArgs;
587}
588
Douglas Gregor68584ed2009-06-18 16:11:24 +0000589bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000590 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000591 return false;
592
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000593 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
594 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000595 return false;
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000596
597 return true;
598}
599
Douglas Gregor68584ed2009-06-18 16:11:24 +0000600bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
601 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000602 return false;
603
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000604 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
605 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000606 return true;
607
608 return false;
609}
610
Mike Stump1eb44332009-09-09 15:08:12 +0000611void
Douglas Gregor127102b2009-06-29 20:59:39 +0000612FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000613 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000614
Douglas Gregor127102b2009-06-29 20:59:39 +0000615 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000616 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000617 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
618 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
619 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
620 }
621}
622
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000623FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000624 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000625}
626
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000627/// getOverloadedOperator - Which C++ overloaded operator this
628/// function represents, if any.
629OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000630 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
631 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000632 else
633 return OO_None;
634}
635
Douglas Gregor16e8be22009-06-29 17:30:29 +0000636FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000637 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000638 = TemplateOrSpecialization
639 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000640 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000641 }
642 return 0;
643}
644
645const TemplateArgumentList *
646FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000647 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000648 = TemplateOrSpecialization
649 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
650 return Info->TemplateArguments;
651 }
652 return 0;
653}
654
Mike Stump1eb44332009-09-09 15:08:12 +0000655void
Douglas Gregor1637be72009-06-26 00:10:03 +0000656FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
657 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000658 const TemplateArgumentList *TemplateArgs,
659 void *InsertPos) {
Mike Stump1eb44332009-09-09 15:08:12 +0000660 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000661 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000662 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000663 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregor127102b2009-06-29 20:59:39 +0000665 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000666 Info->Template.setPointer(Template);
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000667 Info->Template.setInt(TSK_ImplicitInstantiation - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +0000668 Info->TemplateArguments = TemplateArgs;
669 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Douglas Gregor127102b2009-06-29 20:59:39 +0000671 // Insert this function template specialization into the set of known
672 // function template specialiations.
673 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +0000674}
675
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000676TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000677 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000678 // information object.
Mike Stump1eb44332009-09-09 15:08:12 +0000679 FunctionTemplateSpecializationInfo *Info
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000680 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
681 if (Info)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000682 return Info->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000684 if (!getInstantiatedFromMemberFunction())
685 return TSK_Undeclared;
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000687 // Find the class template specialization corresponding to this instantiation
688 // of a member function.
689 const DeclContext *Parent = getDeclContext();
690 while (Parent && !isa<ClassTemplateSpecializationDecl>(Parent))
691 Parent = Parent->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000693 if (!Parent)
694 return TSK_Undeclared;
695
696 return cast<ClassTemplateSpecializationDecl>(Parent)->getSpecializationKind();
697}
698
Mike Stump1eb44332009-09-09 15:08:12 +0000699void
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000700FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Mike Stump1eb44332009-09-09 15:08:12 +0000701 FunctionTemplateSpecializationInfo *Info
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000702 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
703 assert(Info && "Not a function template specialization");
704 Info->setTemplateSpecializationKind(TSK);
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000705}
706
Douglas Gregor9f185072009-09-11 20:15:17 +0000707bool FunctionDecl::isOutOfLine() const {
708 // FIXME: Should we restrict this to member functions?
709 if (Decl::isOutOfLine())
710 return true;
711
712 // If this function was instantiated from a member function of a
713 // class template, check whether that member function was defined out-of-line.
714 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
715 const FunctionDecl *Definition;
716 if (FD->getBody(Definition))
717 return Definition->isOutOfLine();
718 }
719
720 // If this function was instantiated from a function template,
721 // check whether that function template was defined out-of-line.
722 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
723 const FunctionDecl *Definition;
724 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
725 return Definition->isOutOfLine();
726 }
727
728 return false;
729}
730
Chris Lattner8a934232008-03-31 00:36:02 +0000731//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000732// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000733//===----------------------------------------------------------------------===//
734
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000735SourceRange TagDecl::getSourceRange() const {
736 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000737 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000738}
739
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000740TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000741 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000742}
743
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000744void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000745 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
746 TagT->decl.setPointer(this);
747 TagT->decl.setInt(1);
748 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000749}
750
751void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000752 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000753 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
754 assert(TagT->decl.getPointer() == this &&
755 "Attempt to redefine a tag definition?");
756 TagT->decl.setInt(0);
757 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000758}
759
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000760TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000761 if (isDefinition())
762 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
764 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000765 R != REnd; ++R)
766 if (R->isDefinition())
767 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000769 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000770}
771
John McCallf1bbbb42009-09-04 01:14:41 +0000772TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
773 switch (TypeSpec) {
774 default: llvm::llvm_unreachable("unexpected type specifier");
775 case DeclSpec::TST_struct: return TK_struct;
776 case DeclSpec::TST_class: return TK_class;
777 case DeclSpec::TST_union: return TK_union;
778 case DeclSpec::TST_enum: return TK_enum;
779 }
780}
781
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000782//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000783// RecordDecl Implementation
784//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000785
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000786RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000787 IdentifierInfo *Id, RecordDecl *PrevDecl,
788 SourceLocation TKL)
789 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +0000790 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000791 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000792 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000793 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000794}
795
796RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000797 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000798 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000800 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000801 C.getTypeDeclType(R, PrevDecl);
802 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000803}
804
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000805RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000806}
807
808void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000809 TagDecl::Destroy(C);
810}
811
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000812bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000813 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000814 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
815}
816
Douglas Gregor44b43212008-12-11 16:49:14 +0000817/// completeDefinition - Notes that the definition of this type is now
818/// complete.
819void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000821 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000822}
823
Steve Naroff56ee6892008-10-08 17:01:13 +0000824//===----------------------------------------------------------------------===//
825// BlockDecl Implementation
826//===----------------------------------------------------------------------===//
827
828BlockDecl::~BlockDecl() {
829}
830
831void BlockDecl::Destroy(ASTContext& C) {
832 if (Body)
833 Body->Destroy(C);
834
835 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
836 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
838 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000839 Decl::Destroy(C);
840}
Steve Naroffe78b8092009-03-13 16:56:44 +0000841
842void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
843 unsigned NParms) {
844 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Steve Naroffe78b8092009-03-13 16:56:44 +0000846 // Zero params -> null pointer.
847 if (NParms) {
848 NumParams = NParms;
849 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
850 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
851 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
852 }
853}
854
855unsigned BlockDecl::getNumParams() const {
856 return NumParams;
857}