blob: ab8de1d5feacc8246bdbd8564b1bb7eab9d3d7ab [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000024#include "clang/Basic/IdentifierTable.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000025#include "clang/Parse/DeclSpec.h"
26#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2ada0482009-02-04 17:27:36 +000027#include <vector>
Ted Kremenekce20e8f2008-05-20 00:43:19 +000028
Chris Lattner6d9a6852006-10-25 05:11:20 +000029using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000030
Chris Lattner9631e182009-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 Kyrtzidis3f79ad72009-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 Lattner9631e182009-03-04 06:34:08 +000044
Chris Lattner88f70d62008-03-15 05:43:15 +000045//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +000046// Decl Allocation/Deallocation Method Implementations
47//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +000048
Chris Lattner9631e182009-03-04 06:34:08 +000049
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000050TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +000051 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000052}
53
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000054NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
55 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000056 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000057}
58
Ted Kremenek78aa98f2008-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 Stump11289f42009-09-09 15:08:12 +000062
Ted Kremeneka08154d2008-05-24 15:09:56 +000063 this->~NamespaceDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +000064 C.Deallocate((void *)this);
Ted Kremenek78aa98f2008-05-20 04:49:55 +000065}
66
67
Chris Lattner5696e7b2008-06-17 18:05:57 +000068ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor6e6ad602009-01-20 01:17:11 +000069 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +000070 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner5696e7b2008-06-17 18:05:57 +000071}
72
Daniel Dunbarb76b7452009-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 Stump11289f42009-09-09 15:08:12 +000078 case VarDecl::PrivateExtern: return "__private_extern__"; break;
Daniel Dunbarb76b7452009-04-14 02:08:49 +000079 case VarDecl::Register: return "register"; break;
Mike Stump11289f42009-09-09 15:08:12 +000080 case VarDecl::Static: return "static"; break;
Daniel Dunbarb76b7452009-04-14 02:08:49 +000081 }
82
83 assert(0 && "Invalid storage class");
84 return 0;
85}
86
Chris Lattnerbec41342008-04-22 18:39:57 +000087ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000088 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidis60ed5602009-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 Jahaniana0befc02008-12-20 23:29:59 +000092}
93
94QualType ParmVarDecl::getOriginalType() const {
Mike Stump11289f42009-09-09 15:08:12 +000095 if (const OriginalParmVarDecl *PVD =
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000096 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahaniana0befc02008-12-20 23:29:59 +000097 return PVD->OriginalType;
98 return getType();
Chris Lattner4b08ca82008-03-15 21:10:16 +000099}
100
Douglas Gregorc732aba2009-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 Gregor31cf12c2009-05-26 18:54:04 +0000110
Douglas Gregorc732aba2009-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 Gregor31cf12c2009-05-26 18:54:04 +0000115 }
116
Douglas Gregorc732aba2009-09-11 18:44:32 +0000117 Init = I;
118}
119
Douglas Gregor16618f22009-09-12 00:17:51 +0000120bool VarDecl::isExternC() const {
121 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000122 if (!Context.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +0000123 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000124 getStorageClass() != Static) ||
125 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
126
Mike Stump11289f42009-09-09 15:08:12 +0000127 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000128 DC = DC->getParent()) {
129 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
130 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
131 return getStorageClass() != Static;
132
133 break;
134 }
135
136 if (DC->isFunctionOrMethod())
137 return false;
138 }
139
140 return false;
141}
142
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000143OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +0000144 ASTContext &C, DeclContext *DC,
145 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000146 QualType T, DeclaratorInfo *DInfo,
147 QualType OT, StorageClass S, Expr *DefArg) {
148 return new (C) OriginalParmVarDecl(DC, L, Id, T, DInfo, OT, S, DefArg);
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +0000149}
150
Chris Lattnerbec41342008-04-22 18:39:57 +0000151FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000152 SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000153 DeclarationName N, QualType T,
154 DeclaratorInfo *DInfo,
Mike Stump11289f42009-09-09 15:08:12 +0000155 StorageClass S, bool isInline,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000156 bool hasWrittenPrototype) {
Mike Stump11289f42009-09-09 15:08:12 +0000157 FunctionDecl *New
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000158 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssone0dd1d52009-05-14 21:46:00 +0000159 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor739ef0c2009-02-25 16:33:18 +0000160 return New;
Chris Lattner5072bae2008-03-15 21:24:04 +0000161}
162
Steve Naroff1d95e5a2008-10-10 01:28:17 +0000163BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000164 return new (C) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +0000165}
166
Douglas Gregor91f84212008-12-11 16:49:14 +0000167FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000168 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000169 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
170 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattneree1284a2008-03-16 00:16:02 +0000171}
172
Douglas Gregorf4d33272009-01-07 19:46:03 +0000173bool FieldDecl::isAnonymousStructOrUnion() const {
174 if (!isImplicit() || getDeclName())
175 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000176
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000177 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorf4d33272009-01-07 19:46:03 +0000178 return Record->getDecl()->isAnonymousStructOrUnion();
179
180 return false;
181}
Chris Lattner5072bae2008-03-15 21:24:04 +0000182
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000183EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
184 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000185 IdentifierInfo *Id, QualType T,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000186 Expr *E, const llvm::APSInt &V) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000187 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnera7b32872008-03-15 06:12:44 +0000188}
189
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000190void EnumConstantDecl::Destroy(ASTContext& C) {
191 if (Init) Init->Destroy(C);
192 Decl::Destroy(C);
193}
194
Chris Lattnerbec41342008-04-22 18:39:57 +0000195TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000196 SourceLocation L,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000197 IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000198 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnera7b32872008-03-15 06:12:44 +0000199}
200
Chris Lattnerbec41342008-04-22 18:39:57 +0000201EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000202 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000203 EnumDecl *PrevDecl) {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000204 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000205 C.getTypeDeclType(Enum, PrevDecl);
206 return Enum;
Chris Lattnera7b32872008-03-15 06:12:44 +0000207}
208
Ted Kremenek123f0252008-09-02 20:13:32 +0000209void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek123f0252008-09-02 20:13:32 +0000210 Decl::Destroy(C);
211}
212
Douglas Gregor91f84212008-12-11 16:49:14 +0000213void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
214 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000215 IntegerType = NewType;
Douglas Gregordee1be82009-01-17 00:42:38 +0000216 TagDecl::completeDefinition();
Douglas Gregor91f84212008-12-11 16:49:14 +0000217}
218
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000219FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000220 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000221 StringLiteral *Str) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000222 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattneree1284a2008-03-16 00:16:02 +0000223}
224
Chris Lattnera7b32872008-03-15 06:12:44 +0000225//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000226// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000227//===----------------------------------------------------------------------===//
228
Douglas Gregor2ada0482009-02-04 17:27:36 +0000229std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000230 return getQualifiedNameAsString(getASTContext().getLangOptions());
231}
232
233std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000234 std::vector<std::string> Names;
235 std::string QualName;
236 const DeclContext *Ctx = getDeclContext();
237
238 if (Ctx->isFunctionOrMethod())
239 return getNameAsString();
240
241 while (Ctx) {
242 if (Ctx->isFunctionOrMethod())
243 // FIXME: That probably will happen, when D was member of local
244 // scope class/struct/union. How do we handle this case?
245 break;
246
Mike Stump11289f42009-09-09 15:08:12 +0000247 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000248 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
249 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
250 std::string TemplateArgsStr
251 = TemplateSpecializationType::PrintTemplateArgumentList(
252 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000253 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000254 P);
Douglas Gregor85673582009-05-18 17:01:57 +0000255 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
256 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000257 Names.push_back(ND->getNameAsString());
258 else
259 break;
260
261 Ctx = Ctx->getParent();
262 }
263
264 std::vector<std::string>::reverse_iterator
265 I = Names.rbegin(),
266 End = Names.rend();
267
268 for (; I!=End; ++I)
269 QualName += *I + "::";
270
271 QualName += getNameAsString();
272
273 return QualName;
274}
275
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000276bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000277 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
278
Douglas Gregor889ceb72009-02-03 19:21:40 +0000279 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
280 // We want to keep it, unless it nominates same namespace.
281 if (getKind() == Decl::UsingDirective) {
282 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
283 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
284 }
Mike Stump11289f42009-09-09 15:08:12 +0000285
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000286 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
287 // For function declarations, we keep track of redeclarations.
288 return FD->getPreviousDeclaration() == OldD;
289
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000290 // For function templates, the underlying function declarations are linked.
291 if (const FunctionTemplateDecl *FunctionTemplate
292 = dyn_cast<FunctionTemplateDecl>(this))
293 if (const FunctionTemplateDecl *OldFunctionTemplate
294 = dyn_cast<FunctionTemplateDecl>(OldD))
295 return FunctionTemplate->getTemplatedDecl()
296 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000297
Steve Naroffc4173fa2009-02-22 19:35:57 +0000298 // For method declarations, we keep track of redeclarations.
299 if (isa<ObjCMethodDecl>(this))
300 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000301
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000302 // For non-function declarations, if the declarations are of the
303 // same kind then this must be a redeclaration, or semantic analysis
304 // would not have given us the new declaration.
305 return this->getKind() == OldD->getKind();
306}
307
Douglas Gregoreddf4332009-02-24 20:03:32 +0000308bool NamedDecl::hasLinkage() const {
309 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
310 return VD->hasExternalStorage() || VD->isFileVarDecl();
311
312 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
313 return true;
314
315 return false;
316}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000317
Anders Carlsson6915bf62009-06-26 06:29:23 +0000318NamedDecl *NamedDecl::getUnderlyingDecl() {
319 NamedDecl *ND = this;
320 while (true) {
321 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
322 ND = UD->getTargetDecl();
323 else if (ObjCCompatibleAliasDecl *AD
324 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
325 return AD->getClassInterface();
326 else
327 return ND;
328 }
329}
330
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000331//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000332// DeclaratorDecl Implementation
333//===----------------------------------------------------------------------===//
334
335SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
336 if (DeclInfo)
337 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
338 return SourceLocation();
339}
340
341//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000342// VarDecl Implementation
343//===----------------------------------------------------------------------===//
344
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000345VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000346 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000347 StorageClass S) {
348 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000349}
350
351void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000352 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000353 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000354 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000355 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
356 Eval->~EvaluatedStmt();
357 C.Deallocate(Eval);
358 }
359 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000360 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000361 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000362}
363
364VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000365}
366
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000367SourceRange VarDecl::getSourceRange() const {
368 if (getInit())
369 return SourceRange(getLocation(), getInit()->getLocEnd());
370 return SourceRange(getLocation(), getLocation());
371}
372
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000373VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
374 return getASTContext().getInstantiatedFromStaticDataMember(this);
375}
376
Douglas Gregor0760fa12009-03-10 23:43:53 +0000377bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
378 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
379 return false;
380
Douglas Gregorbeecd582009-04-21 17:11:58 +0000381 const VarDecl *Def = 0;
382 return (!getDefinition(Def) &&
Douglas Gregor0760fa12009-03-10 23:43:53 +0000383 (getStorageClass() == None || getStorageClass() == Static));
384}
385
Ted Kremenekdfd72c22009-03-20 21:35:28 +0000386const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000387 redecl_iterator I = redecls_begin(), E = redecls_end();
388 while (I != E && !I->getInit())
389 ++I;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000390
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000391 if (I != E) {
392 Def = *I;
393 return I->getInit();
394 }
395 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000396}
397
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000398VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000399 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000400}
401
Nuno Lopes394ec982008-12-17 23:39:55 +0000402//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000403// FunctionDecl Implementation
404//===----------------------------------------------------------------------===//
405
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000406void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000407 if (Body && Body.isOffset())
408 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000409
410 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
411 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000412
Steve Naroff13ae6f42009-01-27 21:25:57 +0000413 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000414
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000415 Decl::Destroy(C);
416}
417
John McCalle1f2ec22009-09-11 06:45:03 +0000418void FunctionDecl::getNameForDiagnostic(std::string &S,
419 const PrintingPolicy &Policy,
420 bool Qualified) const {
421 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
422 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
423 if (TemplateArgs)
424 S += TemplateSpecializationType::PrintTemplateArgumentList(
425 TemplateArgs->getFlatArgumentList(),
426 TemplateArgs->flat_size(),
427 Policy);
428
429}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000430
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000431Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000432 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
433 if (I->Body) {
434 Definition = *I;
435 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000436 }
437 }
438
439 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000440}
441
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000442void FunctionDecl::setBody(Stmt *B) {
443 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000444 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000445 EndRangeLoc = B->getLocEnd();
446}
447
Douglas Gregor16618f22009-09-12 00:17:51 +0000448bool FunctionDecl::isMain() const {
449 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000450 return !Context.getLangOptions().Freestanding &&
451 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000452 getIdentifier() && getIdentifier()->isStr("main");
453}
454
Douglas Gregor16618f22009-09-12 00:17:51 +0000455bool FunctionDecl::isExternC() const {
456 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000457 // In C, any non-static, non-overloadable function has external
458 // linkage.
459 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000460 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000461
Mike Stump11289f42009-09-09 15:08:12 +0000462 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000463 DC = DC->getParent()) {
464 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
465 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000466 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000467 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000468
469 break;
470 }
471 }
472
473 return false;
474}
475
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000476bool FunctionDecl::isGlobal() const {
477 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
478 return Method->isStatic();
479
480 if (getStorageClass() == Static)
481 return false;
482
Mike Stump11289f42009-09-09 15:08:12 +0000483 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000484 DC->isNamespace();
485 DC = DC->getParent()) {
486 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
487 if (!Namespace->getDeclName())
488 return false;
489 break;
490 }
491 }
492
493 return true;
494}
495
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000496/// \brief Returns a value indicating whether this function
497/// corresponds to a builtin function.
498///
499/// The function corresponds to a built-in function if it is
500/// declared at translation scope or within an extern "C" block and
501/// its name matches with the name of a builtin. The returned value
502/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000503/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000504/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregore711f702009-02-14 18:57:46 +0000505unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
506 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
507 return 0;
508
509 unsigned BuiltinID = getIdentifier()->getBuiltinID();
510 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
511 return BuiltinID;
512
513 // This function has the name of a known C library
514 // function. Determine whether it actually refers to the C library
515 // function or whether it just has the same name.
516
Douglas Gregora908e7f2009-02-17 03:23:10 +0000517 // If this is a static function, it's not a builtin.
518 if (getStorageClass() == Static)
519 return 0;
520
Douglas Gregore711f702009-02-14 18:57:46 +0000521 // If this function is at translation-unit scope and we're not in
522 // C++, it refers to the C library function.
523 if (!Context.getLangOptions().CPlusPlus &&
524 getDeclContext()->isTranslationUnit())
525 return BuiltinID;
526
527 // If the function is in an extern "C" linkage specification and is
528 // not marked "overloadable", it's the real function.
529 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000530 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000531 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000532 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000533 return BuiltinID;
534
535 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000536 return 0;
537}
538
539
Chris Lattner47c0d002009-04-25 06:03:53 +0000540/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000541/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000542/// after it has been created.
543unsigned FunctionDecl::getNumParams() const {
Chris Lattnerdfd637f22009-04-25 05:56:45 +0000544 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000545 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000546 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000547 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000549}
550
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000551void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
552 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000553 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000554 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000555
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000556 // Zero params -> null pointer.
557 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000558 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000559 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000560 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000561
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000562 // Update source range. The check below allows us to set EndRangeLoc before
563 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000564 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000565 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000566 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000567}
Chris Lattner41943152007-01-25 04:52:46 +0000568
Chris Lattner58258242008-04-10 02:22:51 +0000569/// getMinRequiredArguments - Returns the minimum number of arguments
570/// needed to call this function. This may be fewer than the number of
571/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000572/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000573unsigned FunctionDecl::getMinRequiredArguments() const {
574 unsigned NumRequiredArgs = getNumParams();
575 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000576 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000577 --NumRequiredArgs;
578
579 return NumRequiredArgs;
580}
581
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000582bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000583 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000584 return false;
585
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000586 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
587 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000588 return false;
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000589
590 return true;
591}
592
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000593bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
594 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000595 return false;
596
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000597 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
598 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000599 return true;
600
601 return false;
602}
603
Mike Stump11289f42009-09-09 15:08:12 +0000604void
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000605FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000606 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000607
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000608 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump11289f42009-09-09 15:08:12 +0000609 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000610 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
611 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
612 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
613 }
614}
615
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000616FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000617 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000618}
619
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000620/// getOverloadedOperator - Which C++ overloaded operator this
621/// function represents, if any.
622OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000623 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
624 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000625 else
626 return OO_None;
627}
628
Douglas Gregor70d83e22009-06-29 17:30:29 +0000629FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +0000630 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000631 = TemplateOrSpecialization
632 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +0000633 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +0000634 }
635 return 0;
636}
637
638const TemplateArgumentList *
639FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +0000640 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000641 = TemplateOrSpecialization
642 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
643 return Info->TemplateArguments;
644 }
645 return 0;
646}
647
Mike Stump11289f42009-09-09 15:08:12 +0000648void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000649FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
650 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000651 const TemplateArgumentList *TemplateArgs,
652 void *InsertPos) {
Mike Stump11289f42009-09-09 15:08:12 +0000653 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000654 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000655 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +0000656 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000657
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000658 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +0000659 Info->Template.setPointer(Template);
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000660 Info->Template.setInt(TSK_ImplicitInstantiation - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000661 Info->TemplateArguments = TemplateArgs;
662 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000664 // Insert this function template specialization into the set of known
665 // function template specialiations.
666 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000667}
668
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000669TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +0000670 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000671 // information object.
Mike Stump11289f42009-09-09 15:08:12 +0000672 FunctionTemplateSpecializationInfo *Info
Douglas Gregore8925db2009-06-29 22:39:32 +0000673 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
674 if (Info)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000675 return Info->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000677 if (!getInstantiatedFromMemberFunction())
678 return TSK_Undeclared;
Mike Stump11289f42009-09-09 15:08:12 +0000679
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000680 // Find the class template specialization corresponding to this instantiation
681 // of a member function.
682 const DeclContext *Parent = getDeclContext();
683 while (Parent && !isa<ClassTemplateSpecializationDecl>(Parent))
684 Parent = Parent->getParent();
Mike Stump11289f42009-09-09 15:08:12 +0000685
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000686 if (!Parent)
687 return TSK_Undeclared;
688
689 return cast<ClassTemplateSpecializationDecl>(Parent)->getSpecializationKind();
690}
691
Mike Stump11289f42009-09-09 15:08:12 +0000692void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000693FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Mike Stump11289f42009-09-09 15:08:12 +0000694 FunctionTemplateSpecializationInfo *Info
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000695 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
696 assert(Info && "Not a function template specialization");
697 Info->setTemplateSpecializationKind(TSK);
Douglas Gregore8925db2009-06-29 22:39:32 +0000698}
699
Douglas Gregor6411b922009-09-11 20:15:17 +0000700bool FunctionDecl::isOutOfLine() const {
701 // FIXME: Should we restrict this to member functions?
702 if (Decl::isOutOfLine())
703 return true;
704
705 // If this function was instantiated from a member function of a
706 // class template, check whether that member function was defined out-of-line.
707 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
708 const FunctionDecl *Definition;
709 if (FD->getBody(Definition))
710 return Definition->isOutOfLine();
711 }
712
713 // If this function was instantiated from a function template,
714 // check whether that function template was defined out-of-line.
715 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
716 const FunctionDecl *Definition;
717 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
718 return Definition->isOutOfLine();
719 }
720
721 return false;
722}
723
Chris Lattner59a25942008-03-31 00:36:02 +0000724//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000725// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000726//===----------------------------------------------------------------------===//
727
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000728SourceRange TagDecl::getSourceRange() const {
729 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000730 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000731}
732
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000733TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000734 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000735}
736
Douglas Gregordee1be82009-01-17 00:42:38 +0000737void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000738 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
739 TagT->decl.setPointer(this);
740 TagT->decl.setInt(1);
741 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000742}
743
744void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +0000745 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000746 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
747 assert(TagT->decl.getPointer() == this &&
748 "Attempt to redefine a tag definition?");
749 TagT->decl.setInt(0);
750 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000751}
752
Ted Kremenek21475702008-09-05 17:16:31 +0000753TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000754 if (isDefinition())
755 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000756
757 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000758 R != REnd; ++R)
759 if (R->isDefinition())
760 return *R;
Mike Stump11289f42009-09-09 15:08:12 +0000761
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000762 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +0000763}
764
John McCall06f6fe8d2009-09-04 01:14:41 +0000765TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
766 switch (TypeSpec) {
767 default: llvm::llvm_unreachable("unexpected type specifier");
768 case DeclSpec::TST_struct: return TK_struct;
769 case DeclSpec::TST_class: return TK_class;
770 case DeclSpec::TST_union: return TK_union;
771 case DeclSpec::TST_enum: return TK_enum;
772 }
773}
774
Ted Kremenek21475702008-09-05 17:16:31 +0000775//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000776// RecordDecl Implementation
777//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000778
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000779RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000780 IdentifierInfo *Id, RecordDecl *PrevDecl,
781 SourceLocation TKL)
782 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000783 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000784 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000785 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000786 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000787}
788
789RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000790 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000791 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +0000792
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000793 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +0000794 C.getTypeDeclType(R, PrevDecl);
795 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000796}
797
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000798RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000799}
800
801void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000802 TagDecl::Destroy(C);
803}
804
Douglas Gregordfcad112009-03-25 15:59:44 +0000805bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +0000806 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +0000807 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
808}
809
Douglas Gregor91f84212008-12-11 16:49:14 +0000810/// completeDefinition - Notes that the definition of this type is now
811/// complete.
812void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000813 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +0000814 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +0000815}
Steve Naroffcc321422007-03-26 23:09:51 +0000816
Steve Naroff415d3d52008-10-08 17:01:13 +0000817//===----------------------------------------------------------------------===//
818// BlockDecl Implementation
819//===----------------------------------------------------------------------===//
820
821BlockDecl::~BlockDecl() {
822}
823
824void BlockDecl::Destroy(ASTContext& C) {
825 if (Body)
826 Body->Destroy(C);
827
828 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
829 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000830
831 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +0000832 Decl::Destroy(C);
833}
Steve Naroffc4b30e52009-03-13 16:56:44 +0000834
835void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
836 unsigned NParms) {
837 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +0000838
Steve Naroffc4b30e52009-03-13 16:56:44 +0000839 // Zero params -> null pointer.
840 if (NParms) {
841 NumParams = NParms;
842 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
843 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
844 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
845 }
846}
847
848unsigned BlockDecl::getNumParams() const {
849 return NumParams;
850}