blob: 16936428f39486fe29fac1fcc650da67c8bf3305 [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
Mike Stump11289f42009-09-09 15:08:12 +0000101void VarDecl::setInit(ASTContext &C, Expr *I) {
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000102 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
103 Eval->~EvaluatedStmt();
104 C.Deallocate(Eval);
105 }
106
107 Init = I;
108 }
109
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000110bool VarDecl::isExternC(ASTContext &Context) const {
111 if (!Context.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +0000112 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000113 getStorageClass() != Static) ||
114 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
115
Mike Stump11289f42009-09-09 15:08:12 +0000116 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000117 DC = DC->getParent()) {
118 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
119 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
120 return getStorageClass() != Static;
121
122 break;
123 }
124
125 if (DC->isFunctionOrMethod())
126 return false;
127 }
128
129 return false;
130}
131
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000132OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +0000133 ASTContext &C, DeclContext *DC,
134 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000135 QualType T, DeclaratorInfo *DInfo,
136 QualType OT, StorageClass S, Expr *DefArg) {
137 return new (C) OriginalParmVarDecl(DC, L, Id, T, DInfo, OT, S, DefArg);
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +0000138}
139
Chris Lattnerbec41342008-04-22 18:39:57 +0000140FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000141 SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000142 DeclarationName N, QualType T,
143 DeclaratorInfo *DInfo,
Mike Stump11289f42009-09-09 15:08:12 +0000144 StorageClass S, bool isInline,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000145 bool hasWrittenPrototype) {
Mike Stump11289f42009-09-09 15:08:12 +0000146 FunctionDecl *New
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000147 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssone0dd1d52009-05-14 21:46:00 +0000148 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor739ef0c2009-02-25 16:33:18 +0000149 return New;
Chris Lattner5072bae2008-03-15 21:24:04 +0000150}
151
Steve Naroff1d95e5a2008-10-10 01:28:17 +0000152BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000153 return new (C) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +0000154}
155
Douglas Gregor91f84212008-12-11 16:49:14 +0000156FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000157 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000158 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
159 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattneree1284a2008-03-16 00:16:02 +0000160}
161
Douglas Gregorf4d33272009-01-07 19:46:03 +0000162bool FieldDecl::isAnonymousStructOrUnion() const {
163 if (!isImplicit() || getDeclName())
164 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000166 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorf4d33272009-01-07 19:46:03 +0000167 return Record->getDecl()->isAnonymousStructOrUnion();
168
169 return false;
170}
Chris Lattner5072bae2008-03-15 21:24:04 +0000171
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000172EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
173 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000174 IdentifierInfo *Id, QualType T,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000175 Expr *E, const llvm::APSInt &V) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000176 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnera7b32872008-03-15 06:12:44 +0000177}
178
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000179void EnumConstantDecl::Destroy(ASTContext& C) {
180 if (Init) Init->Destroy(C);
181 Decl::Destroy(C);
182}
183
Chris Lattnerbec41342008-04-22 18:39:57 +0000184TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000185 SourceLocation L,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000186 IdentifierInfo *Id, QualType T) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000187 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattnera7b32872008-03-15 06:12:44 +0000188}
189
Chris Lattnerbec41342008-04-22 18:39:57 +0000190EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000191 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000192 EnumDecl *PrevDecl) {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000193 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000194 C.getTypeDeclType(Enum, PrevDecl);
195 return Enum;
Chris Lattnera7b32872008-03-15 06:12:44 +0000196}
197
Ted Kremenek123f0252008-09-02 20:13:32 +0000198void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek123f0252008-09-02 20:13:32 +0000199 Decl::Destroy(C);
200}
201
Douglas Gregor91f84212008-12-11 16:49:14 +0000202void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
203 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000204 IntegerType = NewType;
Douglas Gregordee1be82009-01-17 00:42:38 +0000205 TagDecl::completeDefinition();
Douglas Gregor91f84212008-12-11 16:49:14 +0000206}
207
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000208FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000209 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000210 StringLiteral *Str) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000211 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattneree1284a2008-03-16 00:16:02 +0000212}
213
Chris Lattnera7b32872008-03-15 06:12:44 +0000214//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000215// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000216//===----------------------------------------------------------------------===//
217
Douglas Gregor2ada0482009-02-04 17:27:36 +0000218std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000219 return getQualifiedNameAsString(getASTContext().getLangOptions());
220}
221
222std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000223 std::vector<std::string> Names;
224 std::string QualName;
225 const DeclContext *Ctx = getDeclContext();
226
227 if (Ctx->isFunctionOrMethod())
228 return getNameAsString();
229
230 while (Ctx) {
231 if (Ctx->isFunctionOrMethod())
232 // FIXME: That probably will happen, when D was member of local
233 // scope class/struct/union. How do we handle this case?
234 break;
235
Mike Stump11289f42009-09-09 15:08:12 +0000236 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000237 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
238 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
239 std::string TemplateArgsStr
240 = TemplateSpecializationType::PrintTemplateArgumentList(
241 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000242 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000243 P);
Douglas Gregor85673582009-05-18 17:01:57 +0000244 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
245 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000246 Names.push_back(ND->getNameAsString());
247 else
248 break;
249
250 Ctx = Ctx->getParent();
251 }
252
253 std::vector<std::string>::reverse_iterator
254 I = Names.rbegin(),
255 End = Names.rend();
256
257 for (; I!=End; ++I)
258 QualName += *I + "::";
259
260 QualName += getNameAsString();
261
262 return QualName;
263}
264
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000265bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000266 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
267
Douglas Gregor889ceb72009-02-03 19:21:40 +0000268 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
269 // We want to keep it, unless it nominates same namespace.
270 if (getKind() == Decl::UsingDirective) {
271 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
272 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000275 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
276 // For function declarations, we keep track of redeclarations.
277 return FD->getPreviousDeclaration() == OldD;
278
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000279 // For function templates, the underlying function declarations are linked.
280 if (const FunctionTemplateDecl *FunctionTemplate
281 = dyn_cast<FunctionTemplateDecl>(this))
282 if (const FunctionTemplateDecl *OldFunctionTemplate
283 = dyn_cast<FunctionTemplateDecl>(OldD))
284 return FunctionTemplate->getTemplatedDecl()
285 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000286
Steve Naroffc4173fa2009-02-22 19:35:57 +0000287 // For method declarations, we keep track of redeclarations.
288 if (isa<ObjCMethodDecl>(this))
289 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000290
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000291 // For non-function declarations, if the declarations are of the
292 // same kind then this must be a redeclaration, or semantic analysis
293 // would not have given us the new declaration.
294 return this->getKind() == OldD->getKind();
295}
296
Douglas Gregoreddf4332009-02-24 20:03:32 +0000297bool NamedDecl::hasLinkage() const {
298 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
299 return VD->hasExternalStorage() || VD->isFileVarDecl();
300
301 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
302 return true;
303
304 return false;
305}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000306
Anders Carlsson6915bf62009-06-26 06:29:23 +0000307NamedDecl *NamedDecl::getUnderlyingDecl() {
308 NamedDecl *ND = this;
309 while (true) {
310 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
311 ND = UD->getTargetDecl();
312 else if (ObjCCompatibleAliasDecl *AD
313 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
314 return AD->getClassInterface();
315 else
316 return ND;
317 }
318}
319
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000320//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000321// DeclaratorDecl Implementation
322//===----------------------------------------------------------------------===//
323
324SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
325 if (DeclInfo)
326 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
327 return SourceLocation();
328}
329
330//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000331// VarDecl Implementation
332//===----------------------------------------------------------------------===//
333
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000334VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000335 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000336 StorageClass S) {
337 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000338}
339
340void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000341 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000342 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000343 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000344 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
345 Eval->~EvaluatedStmt();
346 C.Deallocate(Eval);
347 }
348 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000349 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000350 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000351}
352
353VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000354}
355
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000356SourceRange VarDecl::getSourceRange() const {
357 if (getInit())
358 return SourceRange(getLocation(), getInit()->getLocEnd());
359 return SourceRange(getLocation(), getLocation());
360}
361
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000362VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
363 return getASTContext().getInstantiatedFromStaticDataMember(this);
364}
365
Douglas Gregor0760fa12009-03-10 23:43:53 +0000366bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
367 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
368 return false;
369
Douglas Gregorbeecd582009-04-21 17:11:58 +0000370 const VarDecl *Def = 0;
371 return (!getDefinition(Def) &&
Douglas Gregor0760fa12009-03-10 23:43:53 +0000372 (getStorageClass() == None || getStorageClass() == Static));
373}
374
Ted Kremenekdfd72c22009-03-20 21:35:28 +0000375const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000376 redecl_iterator I = redecls_begin(), E = redecls_end();
377 while (I != E && !I->getInit())
378 ++I;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000379
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000380 if (I != E) {
381 Def = *I;
382 return I->getInit();
383 }
384 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000385}
386
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000387VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000388 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000389}
390
Nuno Lopes394ec982008-12-17 23:39:55 +0000391//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000392// FunctionDecl Implementation
393//===----------------------------------------------------------------------===//
394
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000395void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000396 if (Body && Body.isOffset())
397 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000398
399 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
400 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000401
Steve Naroff13ae6f42009-01-27 21:25:57 +0000402 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000403
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000404 Decl::Destroy(C);
405}
406
John McCalle1f2ec22009-09-11 06:45:03 +0000407void FunctionDecl::getNameForDiagnostic(std::string &S,
408 const PrintingPolicy &Policy,
409 bool Qualified) const {
410 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
411 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
412 if (TemplateArgs)
413 S += TemplateSpecializationType::PrintTemplateArgumentList(
414 TemplateArgs->getFlatArgumentList(),
415 TemplateArgs->flat_size(),
416 Policy);
417
418}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000419
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000420Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000421 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
422 if (I->Body) {
423 Definition = *I;
424 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000425 }
426 }
427
428 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000429}
430
Sebastian Redla7b98a72009-04-26 20:35:05 +0000431Stmt *FunctionDecl::getBodyIfAvailable() 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 && !I->Body.isOffset()) {
434 return I->Body.get(0);
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000435 }
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000436 }
437
438 return 0;
439}
440
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000441void FunctionDecl::setBody(Stmt *B) {
442 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000443 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000444 EndRangeLoc = B->getLocEnd();
445}
446
John McCalldeb84482009-08-15 02:09:25 +0000447bool FunctionDecl::isMain(ASTContext &Context) const {
448 return !Context.getLangOptions().Freestanding &&
449 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000450 getIdentifier() && getIdentifier()->isStr("main");
451}
452
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000453bool FunctionDecl::isExternC(ASTContext &Context) const {
454 // In C, any non-static, non-overloadable function has external
455 // linkage.
456 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000457 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000458
Mike Stump11289f42009-09-09 15:08:12 +0000459 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000460 DC = DC->getParent()) {
461 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
462 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000463 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000464 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000465
466 break;
467 }
468 }
469
470 return false;
471}
472
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000473bool FunctionDecl::isGlobal() const {
474 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
475 return Method->isStatic();
476
477 if (getStorageClass() == Static)
478 return false;
479
Mike Stump11289f42009-09-09 15:08:12 +0000480 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000481 DC->isNamespace();
482 DC = DC->getParent()) {
483 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
484 if (!Namespace->getDeclName())
485 return false;
486 break;
487 }
488 }
489
490 return true;
491}
492
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000493/// \brief Returns a value indicating whether this function
494/// corresponds to a builtin function.
495///
496/// The function corresponds to a built-in function if it is
497/// declared at translation scope or within an extern "C" block and
498/// its name matches with the name of a builtin. The returned value
499/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000500/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000501/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregore711f702009-02-14 18:57:46 +0000502unsigned FunctionDecl::getBuiltinID(ASTContext &Context) const {
503 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
504 return 0;
505
506 unsigned BuiltinID = getIdentifier()->getBuiltinID();
507 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
508 return BuiltinID;
509
510 // This function has the name of a known C library
511 // function. Determine whether it actually refers to the C library
512 // function or whether it just has the same name.
513
Douglas Gregora908e7f2009-02-17 03:23:10 +0000514 // If this is a static function, it's not a builtin.
515 if (getStorageClass() == Static)
516 return 0;
517
Douglas Gregore711f702009-02-14 18:57:46 +0000518 // If this function is at translation-unit scope and we're not in
519 // C++, it refers to the C library function.
520 if (!Context.getLangOptions().CPlusPlus &&
521 getDeclContext()->isTranslationUnit())
522 return BuiltinID;
523
524 // If the function is in an extern "C" linkage specification and is
525 // not marked "overloadable", it's the real function.
526 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000527 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000528 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000529 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000530 return BuiltinID;
531
532 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000533 return 0;
534}
535
536
Chris Lattner47c0d002009-04-25 06:03:53 +0000537/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000538/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000539/// after it has been created.
540unsigned FunctionDecl::getNumParams() const {
Chris Lattnerdfd637f22009-04-25 05:56:45 +0000541 const FunctionType *FT = getType()->getAsFunctionType();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000542 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000543 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000544 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000545
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000546}
547
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000548void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
549 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000550 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000551 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000553 // Zero params -> null pointer.
554 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000555 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000556 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000557 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000558
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000559 // Update source range. The check below allows us to set EndRangeLoc before
560 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000561 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000562 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000563 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000564}
Chris Lattner41943152007-01-25 04:52:46 +0000565
Chris Lattner58258242008-04-10 02:22:51 +0000566/// getMinRequiredArguments - Returns the minimum number of arguments
567/// needed to call this function. This may be fewer than the number of
568/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000569/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000570unsigned FunctionDecl::getMinRequiredArguments() const {
571 unsigned NumRequiredArgs = getNumParams();
572 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000573 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000574 --NumRequiredArgs;
575
576 return NumRequiredArgs;
577}
578
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000579bool FunctionDecl::hasActiveGNUInlineAttribute(ASTContext &Context) const {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000580 if (!isInline() || !hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000581 return false;
582
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000583 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
584 if (I->isInline() && !I->hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000585 return false;
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000586
587 return true;
588}
589
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000590bool FunctionDecl::isExternGNUInline(ASTContext &Context) const {
591 if (!hasActiveGNUInlineAttribute(Context))
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000592 return false;
593
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000594 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
595 if (I->getStorageClass() == Extern && I->hasAttr<GNUInlineAttr>())
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000596 return true;
597
598 return false;
599}
600
Mike Stump11289f42009-09-09 15:08:12 +0000601void
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000602FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000603 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000604
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000605 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump11289f42009-09-09 15:08:12 +0000606 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000607 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
608 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
609 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
610 }
611}
612
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000613FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000614 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000615}
616
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000617/// getOverloadedOperator - Which C++ overloaded operator this
618/// function represents, if any.
619OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000620 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
621 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000622 else
623 return OO_None;
624}
625
Douglas Gregor70d83e22009-06-29 17:30:29 +0000626FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +0000627 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000628 = TemplateOrSpecialization
629 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +0000630 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +0000631 }
632 return 0;
633}
634
635const TemplateArgumentList *
636FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +0000637 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000638 = TemplateOrSpecialization
639 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
640 return Info->TemplateArguments;
641 }
642 return 0;
643}
644
Mike Stump11289f42009-09-09 15:08:12 +0000645void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000646FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
647 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000648 const TemplateArgumentList *TemplateArgs,
649 void *InsertPos) {
Mike Stump11289f42009-09-09 15:08:12 +0000650 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000651 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000652 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +0000653 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000654
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000655 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +0000656 Info->Template.setPointer(Template);
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000657 Info->Template.setInt(TSK_ImplicitInstantiation - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000658 Info->TemplateArguments = TemplateArgs;
659 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000661 // Insert this function template specialization into the set of known
662 // function template specialiations.
663 Template->getSpecializations().InsertNode(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000664}
665
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000666TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +0000667 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000668 // information object.
Mike Stump11289f42009-09-09 15:08:12 +0000669 FunctionTemplateSpecializationInfo *Info
Douglas Gregore8925db2009-06-29 22:39:32 +0000670 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
671 if (Info)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000672 return Info->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +0000673
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000674 if (!getInstantiatedFromMemberFunction())
675 return TSK_Undeclared;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000677 // Find the class template specialization corresponding to this instantiation
678 // of a member function.
679 const DeclContext *Parent = getDeclContext();
680 while (Parent && !isa<ClassTemplateSpecializationDecl>(Parent))
681 Parent = Parent->getParent();
Mike Stump11289f42009-09-09 15:08:12 +0000682
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000683 if (!Parent)
684 return TSK_Undeclared;
685
686 return cast<ClassTemplateSpecializationDecl>(Parent)->getSpecializationKind();
687}
688
Mike Stump11289f42009-09-09 15:08:12 +0000689void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000690FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Mike Stump11289f42009-09-09 15:08:12 +0000691 FunctionTemplateSpecializationInfo *Info
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000692 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
693 assert(Info && "Not a function template specialization");
694 Info->setTemplateSpecializationKind(TSK);
Douglas Gregore8925db2009-06-29 22:39:32 +0000695}
696
Chris Lattner59a25942008-03-31 00:36:02 +0000697//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000698// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000699//===----------------------------------------------------------------------===//
700
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000701SourceRange TagDecl::getSourceRange() const {
702 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000703 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000704}
705
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000706TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000707 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000708}
709
Douglas Gregordee1be82009-01-17 00:42:38 +0000710void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000711 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
712 TagT->decl.setPointer(this);
713 TagT->decl.setInt(1);
714 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000715}
716
717void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +0000718 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000719 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
720 assert(TagT->decl.getPointer() == this &&
721 "Attempt to redefine a tag definition?");
722 TagT->decl.setInt(0);
723 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000724}
725
Ted Kremenek21475702008-09-05 17:16:31 +0000726TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000727 if (isDefinition())
728 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000729
730 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000731 R != REnd; ++R)
732 if (R->isDefinition())
733 return *R;
Mike Stump11289f42009-09-09 15:08:12 +0000734
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000735 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +0000736}
737
John McCall06f6fe8d2009-09-04 01:14:41 +0000738TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
739 switch (TypeSpec) {
740 default: llvm::llvm_unreachable("unexpected type specifier");
741 case DeclSpec::TST_struct: return TK_struct;
742 case DeclSpec::TST_class: return TK_class;
743 case DeclSpec::TST_union: return TK_union;
744 case DeclSpec::TST_enum: return TK_enum;
745 }
746}
747
Ted Kremenek21475702008-09-05 17:16:31 +0000748//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000749// RecordDecl Implementation
750//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000751
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000752RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000753 IdentifierInfo *Id, RecordDecl *PrevDecl,
754 SourceLocation TKL)
755 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000756 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000757 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000758 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000759 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000760}
761
762RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000763 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000764 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +0000765
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000766 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +0000767 C.getTypeDeclType(R, PrevDecl);
768 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000769}
770
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000771RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000772}
773
774void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000775 TagDecl::Destroy(C);
776}
777
Douglas Gregordfcad112009-03-25 15:59:44 +0000778bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +0000779 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +0000780 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
781}
782
Douglas Gregor91f84212008-12-11 16:49:14 +0000783/// completeDefinition - Notes that the definition of this type is now
784/// complete.
785void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000786 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +0000787 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +0000788}
Steve Naroffcc321422007-03-26 23:09:51 +0000789
Steve Naroff415d3d52008-10-08 17:01:13 +0000790//===----------------------------------------------------------------------===//
791// BlockDecl Implementation
792//===----------------------------------------------------------------------===//
793
794BlockDecl::~BlockDecl() {
795}
796
797void BlockDecl::Destroy(ASTContext& C) {
798 if (Body)
799 Body->Destroy(C);
800
801 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
802 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000803
804 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +0000805 Decl::Destroy(C);
806}
Steve Naroffc4b30e52009-03-13 16:56:44 +0000807
808void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
809 unsigned NParms) {
810 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +0000811
Steve Naroffc4b30e52009-03-13 16:56:44 +0000812 // Zero params -> null pointer.
813 if (NParms) {
814 NumParams = NParms;
815 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
816 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
817 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
818 }
819}
820
821unsigned BlockDecl::getNumParams() const {
822 return NumParams;
823}