blob: a5c9fa4bac87f4a0365f914adb3f95f0835b2259 [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 {
Argyrios Kyrtzidis1b7c4ca2009-09-29 19:40:20 +000042 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000043}
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
Douglas Gregord801b062009-10-07 23:56:10 +0000413 FunctionTemplateSpecializationInfo *FTSInfo
414 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
415 if (FTSInfo)
416 C.Deallocate(FTSInfo);
417
418 MemberSpecializationInfo *MSInfo
419 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
420 if (MSInfo)
421 C.Deallocate(MSInfo);
422
Steve Naroff13ae6f42009-01-27 21:25:57 +0000423 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000424
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000425 Decl::Destroy(C);
426}
427
John McCalle1f2ec22009-09-11 06:45:03 +0000428void FunctionDecl::getNameForDiagnostic(std::string &S,
429 const PrintingPolicy &Policy,
430 bool Qualified) const {
431 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
432 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
433 if (TemplateArgs)
434 S += TemplateSpecializationType::PrintTemplateArgumentList(
435 TemplateArgs->getFlatArgumentList(),
436 TemplateArgs->flat_size(),
437 Policy);
438
439}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000440
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000441Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000442 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
443 if (I->Body) {
444 Definition = *I;
445 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000446 }
447 }
448
449 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000450}
451
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000452void FunctionDecl::setBody(Stmt *B) {
453 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000454 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000455 EndRangeLoc = B->getLocEnd();
456}
457
Douglas Gregor16618f22009-09-12 00:17:51 +0000458bool FunctionDecl::isMain() const {
459 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000460 return !Context.getLangOptions().Freestanding &&
461 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000462 getIdentifier() && getIdentifier()->isStr("main");
463}
464
Douglas Gregor16618f22009-09-12 00:17:51 +0000465bool FunctionDecl::isExternC() const {
466 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000467 // In C, any non-static, non-overloadable function has external
468 // linkage.
469 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000470 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000471
Mike Stump11289f42009-09-09 15:08:12 +0000472 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000473 DC = DC->getParent()) {
474 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
475 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000476 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000477 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000478
479 break;
480 }
481 }
482
483 return false;
484}
485
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000486bool FunctionDecl::isGlobal() const {
487 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
488 return Method->isStatic();
489
490 if (getStorageClass() == Static)
491 return false;
492
Mike Stump11289f42009-09-09 15:08:12 +0000493 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000494 DC->isNamespace();
495 DC = DC->getParent()) {
496 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
497 if (!Namespace->getDeclName())
498 return false;
499 break;
500 }
501 }
502
503 return true;
504}
505
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000506/// \brief Returns a value indicating whether this function
507/// corresponds to a builtin function.
508///
509/// The function corresponds to a built-in function if it is
510/// declared at translation scope or within an extern "C" block and
511/// its name matches with the name of a builtin. The returned value
512/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000513/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000514/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000515unsigned FunctionDecl::getBuiltinID() const {
516 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000517 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
518 return 0;
519
520 unsigned BuiltinID = getIdentifier()->getBuiltinID();
521 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
522 return BuiltinID;
523
524 // This function has the name of a known C library
525 // function. Determine whether it actually refers to the C library
526 // function or whether it just has the same name.
527
Douglas Gregora908e7f2009-02-17 03:23:10 +0000528 // If this is a static function, it's not a builtin.
529 if (getStorageClass() == Static)
530 return 0;
531
Douglas Gregore711f702009-02-14 18:57:46 +0000532 // If this function is at translation-unit scope and we're not in
533 // C++, it refers to the C library function.
534 if (!Context.getLangOptions().CPlusPlus &&
535 getDeclContext()->isTranslationUnit())
536 return BuiltinID;
537
538 // If the function is in an extern "C" linkage specification and is
539 // not marked "overloadable", it's the real function.
540 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000541 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000542 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000543 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000544 return BuiltinID;
545
546 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000547 return 0;
548}
549
550
Chris Lattner47c0d002009-04-25 06:03:53 +0000551/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000552/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000553/// after it has been created.
554unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000555 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000556 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000557 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000558 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000560}
561
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000562void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
563 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000564 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000565 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000567 // Zero params -> null pointer.
568 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000569 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000570 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000571 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000572
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000573 // Update source range. The check below allows us to set EndRangeLoc before
574 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000575 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000576 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000577 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000578}
Chris Lattner41943152007-01-25 04:52:46 +0000579
Chris Lattner58258242008-04-10 02:22:51 +0000580/// getMinRequiredArguments - Returns the minimum number of arguments
581/// needed to call this function. This may be fewer than the number of
582/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000583/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000584unsigned FunctionDecl::getMinRequiredArguments() const {
585 unsigned NumRequiredArgs = getNumParams();
586 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000587 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000588 --NumRequiredArgs;
589
590 return NumRequiredArgs;
591}
592
Douglas Gregor299d76e2009-09-13 07:46:26 +0000593/// \brief For an inline function definition in C, determine whether the
594/// definition will be externally visible.
595///
596/// Inline function definitions are always available for inlining optimizations.
597/// However, depending on the language dialect, declaration specifiers, and
598/// attributes, the definition of an inline function may or may not be
599/// "externally" visible to other translation units in the program.
600///
601/// In C99, inline definitions are not externally visible by default. However,
602/// if even one of the globa-scope declarations is marked "extern inline", the
603/// inline definition becomes externally visible (C99 6.7.4p6).
604///
605/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
606/// definition, we use the GNU semantics for inline, which are nearly the
607/// opposite of C99 semantics. In particular, "inline" by itself will create
608/// an externally visible symbol, but "extern inline" will not create an
609/// externally visible symbol.
610bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
611 assert(isThisDeclarationADefinition() && "Must have the function definition");
612 assert(isInline() && "Function must be inline");
613
614 if (!getASTContext().getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
615 // GNU inline semantics. Based on a number of examples, we came up with the
616 // following heuristic: if the "inline" keyword is present on a
617 // declaration of the function but "extern" is not present on that
618 // declaration, then the symbol is externally visible. Otherwise, the GNU
619 // "extern inline" semantics applies and the symbol is not externally
620 // visible.
621 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
622 Redecl != RedeclEnd;
623 ++Redecl) {
624 if (Redecl->isInline() && Redecl->getStorageClass() != Extern)
625 return true;
626 }
627
628 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000629 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000630 }
631
632 // C99 6.7.4p6:
633 // [...] If all of the file scope declarations for a function in a
634 // translation unit include the inline function specifier without extern,
635 // then the definition in that translation unit is an inline definition.
636 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
637 Redecl != RedeclEnd;
638 ++Redecl) {
639 // Only consider file-scope declarations in this test.
640 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
641 continue;
642
643 if (!Redecl->isInline() || Redecl->getStorageClass() == Extern)
644 return true; // Not an inline definition
645 }
646
647 // C99 6.7.4p6:
648 // An inline definition does not provide an external definition for the
649 // function, and does not forbid an external definition in another
650 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000651 return false;
652}
653
Mike Stump11289f42009-09-09 15:08:12 +0000654void
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000655FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000656 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000657
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000658 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump11289f42009-09-09 15:08:12 +0000659 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000660 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
661 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
662 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
663 }
664}
665
Mike Stumpe7a2b482009-09-29 00:50:50 +0000666const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
667 return getFirstDeclaration();
668}
669
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000670FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000671 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000672}
673
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000674/// getOverloadedOperator - Which C++ overloaded operator this
675/// function represents, if any.
676OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000677 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
678 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000679 else
680 return OO_None;
681}
682
Douglas Gregord801b062009-10-07 23:56:10 +0000683FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
684 if (MemberSpecializationInfo *Info
685 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
686 return cast<FunctionDecl>(Info->getInstantiatedFrom());
687
688 return 0;
689}
690
691void
692FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
693 TemplateSpecializationKind TSK) {
694 assert(TemplateOrSpecialization.isNull() &&
695 "Member function is already a specialization");
696 MemberSpecializationInfo *Info
697 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
698 TemplateOrSpecialization = Info;
699}
700
Douglas Gregor70d83e22009-06-29 17:30:29 +0000701FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +0000702 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000703 = TemplateOrSpecialization
704 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +0000705 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +0000706 }
707 return 0;
708}
709
710const TemplateArgumentList *
711FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +0000712 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000713 = TemplateOrSpecialization
714 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
715 return Info->TemplateArguments;
716 }
717 return 0;
718}
719
Mike Stump11289f42009-09-09 15:08:12 +0000720void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000721FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
722 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000723 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000724 void *InsertPos,
725 TemplateSpecializationKind TSK) {
726 assert(TSK != TSK_Undeclared &&
727 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +0000728 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000729 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000730 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +0000731 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000732
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000733 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +0000734 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000735 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000736 Info->TemplateArguments = TemplateArgs;
737 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000739 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000740 // function template specializations.
741 if (InsertPos)
742 Template->getSpecializations().InsertNode(Info, InsertPos);
743 else {
744 // Try to insert the new node. If there is an existing node, remove it
745 // first.
746 FunctionTemplateSpecializationInfo *Existing
747 = Template->getSpecializations().GetOrInsertNode(Info);
748 if (Existing) {
749 Template->getSpecializations().RemoveNode(Existing);
750 Template->getSpecializations().GetOrInsertNode(Info);
751 }
752 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000753}
754
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000755TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +0000756 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000757 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +0000758 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +0000759 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +0000760 if (FTSInfo)
761 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +0000762
Douglas Gregord801b062009-10-07 23:56:10 +0000763 MemberSpecializationInfo *MSInfo
764 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
765 if (MSInfo)
766 return MSInfo->getTemplateSpecializationKind();
767
768 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000769}
770
Mike Stump11289f42009-09-09 15:08:12 +0000771void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000772FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Douglas Gregord801b062009-10-07 23:56:10 +0000773 if (FunctionTemplateSpecializationInfo *FTSInfo
774 = TemplateOrSpecialization.dyn_cast<
775 FunctionTemplateSpecializationInfo*>())
776 FTSInfo->setTemplateSpecializationKind(TSK);
777 else if (MemberSpecializationInfo *MSInfo
778 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
779 MSInfo->setTemplateSpecializationKind(TSK);
780 else
781 assert(false && "Function cannot have a template specialization kind");
Douglas Gregore8925db2009-06-29 22:39:32 +0000782}
783
Douglas Gregor6411b922009-09-11 20:15:17 +0000784bool FunctionDecl::isOutOfLine() const {
785 // FIXME: Should we restrict this to member functions?
786 if (Decl::isOutOfLine())
787 return true;
788
789 // If this function was instantiated from a member function of a
790 // class template, check whether that member function was defined out-of-line.
791 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
792 const FunctionDecl *Definition;
793 if (FD->getBody(Definition))
794 return Definition->isOutOfLine();
795 }
796
797 // If this function was instantiated from a function template,
798 // check whether that function template was defined out-of-line.
799 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
800 const FunctionDecl *Definition;
801 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
802 return Definition->isOutOfLine();
803 }
804
805 return false;
806}
807
Chris Lattner59a25942008-03-31 00:36:02 +0000808//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000809// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000810//===----------------------------------------------------------------------===//
811
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000812SourceRange TagDecl::getSourceRange() const {
813 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000814 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000815}
816
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000817TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000818 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000819}
820
Douglas Gregordee1be82009-01-17 00:42:38 +0000821void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000822 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
823 TagT->decl.setPointer(this);
824 TagT->decl.setInt(1);
825 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000826}
827
828void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +0000829 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000830 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
831 assert(TagT->decl.getPointer() == this &&
832 "Attempt to redefine a tag definition?");
833 TagT->decl.setInt(0);
834 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000835}
836
Ted Kremenek21475702008-09-05 17:16:31 +0000837TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000838 if (isDefinition())
839 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000840
841 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000842 R != REnd; ++R)
843 if (R->isDefinition())
844 return *R;
Mike Stump11289f42009-09-09 15:08:12 +0000845
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000846 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +0000847}
848
John McCall06f6fe8d2009-09-04 01:14:41 +0000849TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
850 switch (TypeSpec) {
851 default: llvm::llvm_unreachable("unexpected type specifier");
852 case DeclSpec::TST_struct: return TK_struct;
853 case DeclSpec::TST_class: return TK_class;
854 case DeclSpec::TST_union: return TK_union;
855 case DeclSpec::TST_enum: return TK_enum;
856 }
857}
858
Ted Kremenek21475702008-09-05 17:16:31 +0000859//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000860// RecordDecl Implementation
861//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000862
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000863RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000864 IdentifierInfo *Id, RecordDecl *PrevDecl,
865 SourceLocation TKL)
866 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000867 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000868 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000869 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000870 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000871}
872
873RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000874 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000875 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +0000876
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000877 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +0000878 C.getTypeDeclType(R, PrevDecl);
879 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000880}
881
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000882RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000883}
884
885void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000886 TagDecl::Destroy(C);
887}
888
Douglas Gregordfcad112009-03-25 15:59:44 +0000889bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +0000890 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +0000891 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
892}
893
Douglas Gregor91f84212008-12-11 16:49:14 +0000894/// completeDefinition - Notes that the definition of this type is now
895/// complete.
896void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000897 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +0000898 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +0000899}
Steve Naroffcc321422007-03-26 23:09:51 +0000900
Steve Naroff415d3d52008-10-08 17:01:13 +0000901//===----------------------------------------------------------------------===//
902// BlockDecl Implementation
903//===----------------------------------------------------------------------===//
904
905BlockDecl::~BlockDecl() {
906}
907
908void BlockDecl::Destroy(ASTContext& C) {
909 if (Body)
910 Body->Destroy(C);
911
912 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
913 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000914
915 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +0000916 Decl::Destroy(C);
917}
Steve Naroffc4b30e52009-03-13 16:56:44 +0000918
919void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
920 unsigned NParms) {
921 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +0000922
Steve Naroffc4b30e52009-03-13 16:56:44 +0000923 // Zero params -> null pointer.
924 if (NParms) {
925 NumParams = NParms;
926 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
927 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
928 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
929 }
930}
931
932unsigned BlockDecl::getNumParams() const {
933 return NumParams;
934}