blob: 86c5719bb204a71b94996dcdbb8dc04fea0add22 [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
John McCall9f3059a2009-10-09 21:13:30 +0000302 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
303 return true;
304
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000305 // For non-function declarations, if the declarations are of the
306 // same kind then this must be a redeclaration, or semantic analysis
307 // would not have given us the new declaration.
308 return this->getKind() == OldD->getKind();
309}
310
Douglas Gregoreddf4332009-02-24 20:03:32 +0000311bool NamedDecl::hasLinkage() const {
312 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
313 return VD->hasExternalStorage() || VD->isFileVarDecl();
314
315 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
316 return true;
317
318 return false;
319}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000320
Anders Carlsson6915bf62009-06-26 06:29:23 +0000321NamedDecl *NamedDecl::getUnderlyingDecl() {
322 NamedDecl *ND = this;
323 while (true) {
324 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
325 ND = UD->getTargetDecl();
326 else if (ObjCCompatibleAliasDecl *AD
327 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
328 return AD->getClassInterface();
329 else
330 return ND;
331 }
332}
333
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000334//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000335// DeclaratorDecl Implementation
336//===----------------------------------------------------------------------===//
337
338SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
339 if (DeclInfo)
340 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
341 return SourceLocation();
342}
343
344//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000345// VarDecl Implementation
346//===----------------------------------------------------------------------===//
347
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000348VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000349 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000350 StorageClass S) {
351 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000352}
353
354void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000355 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000356 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000357 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000358 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
359 Eval->~EvaluatedStmt();
360 C.Deallocate(Eval);
361 }
362 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000363 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000364 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000365}
366
367VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000368}
369
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000370SourceRange VarDecl::getSourceRange() const {
371 if (getInit())
372 return SourceRange(getLocation(), getInit()->getLocEnd());
373 return SourceRange(getLocation(), getLocation());
374}
375
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000376VarDecl *VarDecl::getInstantiatedFromStaticDataMember() {
Douglas Gregor86d142a2009-10-08 07:24:58 +0000377 if (MemberSpecializationInfo *MSI
378 = getASTContext().getInstantiatedFromStaticDataMember(this))
379 return cast<VarDecl>(MSI->getInstantiatedFrom());
380
381 return 0;
382}
383
384TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() {
385 if (MemberSpecializationInfo *MSI
386 = getASTContext().getInstantiatedFromStaticDataMember(this))
387 return MSI->getTemplateSpecializationKind();
388
389 return TSK_Undeclared;
390}
391
392void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
393 MemberSpecializationInfo *MSI
394 = getASTContext().getInstantiatedFromStaticDataMember(this);
395 assert(MSI && "Not an instantiated static data member?");
396 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000397}
398
Douglas Gregor0760fa12009-03-10 23:43:53 +0000399bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
400 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
401 return false;
402
Douglas Gregorbeecd582009-04-21 17:11:58 +0000403 const VarDecl *Def = 0;
404 return (!getDefinition(Def) &&
Douglas Gregor0760fa12009-03-10 23:43:53 +0000405 (getStorageClass() == None || getStorageClass() == Static));
406}
407
Ted Kremenekdfd72c22009-03-20 21:35:28 +0000408const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000409 redecl_iterator I = redecls_begin(), E = redecls_end();
410 while (I != E && !I->getInit())
411 ++I;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000412
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000413 if (I != E) {
414 Def = *I;
415 return I->getInit();
416 }
417 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000418}
419
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000420VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000421 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000422}
423
Nuno Lopes394ec982008-12-17 23:39:55 +0000424//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000425// FunctionDecl Implementation
426//===----------------------------------------------------------------------===//
427
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000428void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000429 if (Body && Body.isOffset())
430 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000431
432 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
433 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000434
Douglas Gregord801b062009-10-07 23:56:10 +0000435 FunctionTemplateSpecializationInfo *FTSInfo
436 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
437 if (FTSInfo)
438 C.Deallocate(FTSInfo);
439
440 MemberSpecializationInfo *MSInfo
441 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
442 if (MSInfo)
443 C.Deallocate(MSInfo);
444
Steve Naroff13ae6f42009-01-27 21:25:57 +0000445 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000446
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000447 Decl::Destroy(C);
448}
449
John McCalle1f2ec22009-09-11 06:45:03 +0000450void FunctionDecl::getNameForDiagnostic(std::string &S,
451 const PrintingPolicy &Policy,
452 bool Qualified) const {
453 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
454 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
455 if (TemplateArgs)
456 S += TemplateSpecializationType::PrintTemplateArgumentList(
457 TemplateArgs->getFlatArgumentList(),
458 TemplateArgs->flat_size(),
459 Policy);
460
461}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000462
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000463Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000464 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
465 if (I->Body) {
466 Definition = *I;
467 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000468 }
469 }
470
471 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000472}
473
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000474void FunctionDecl::setBody(Stmt *B) {
475 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000476 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000477 EndRangeLoc = B->getLocEnd();
478}
479
Douglas Gregor16618f22009-09-12 00:17:51 +0000480bool FunctionDecl::isMain() const {
481 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000482 return !Context.getLangOptions().Freestanding &&
483 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000484 getIdentifier() && getIdentifier()->isStr("main");
485}
486
Douglas Gregor16618f22009-09-12 00:17:51 +0000487bool FunctionDecl::isExternC() const {
488 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000489 // In C, any non-static, non-overloadable function has external
490 // linkage.
491 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000492 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000493
Mike Stump11289f42009-09-09 15:08:12 +0000494 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000495 DC = DC->getParent()) {
496 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
497 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000498 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000499 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000500
501 break;
502 }
503 }
504
505 return false;
506}
507
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000508bool FunctionDecl::isGlobal() const {
509 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
510 return Method->isStatic();
511
512 if (getStorageClass() == Static)
513 return false;
514
Mike Stump11289f42009-09-09 15:08:12 +0000515 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000516 DC->isNamespace();
517 DC = DC->getParent()) {
518 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
519 if (!Namespace->getDeclName())
520 return false;
521 break;
522 }
523 }
524
525 return true;
526}
527
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000528/// \brief Returns a value indicating whether this function
529/// corresponds to a builtin function.
530///
531/// The function corresponds to a built-in function if it is
532/// declared at translation scope or within an extern "C" block and
533/// its name matches with the name of a builtin. The returned value
534/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000535/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000536/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000537unsigned FunctionDecl::getBuiltinID() const {
538 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000539 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
540 return 0;
541
542 unsigned BuiltinID = getIdentifier()->getBuiltinID();
543 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
544 return BuiltinID;
545
546 // This function has the name of a known C library
547 // function. Determine whether it actually refers to the C library
548 // function or whether it just has the same name.
549
Douglas Gregora908e7f2009-02-17 03:23:10 +0000550 // If this is a static function, it's not a builtin.
551 if (getStorageClass() == Static)
552 return 0;
553
Douglas Gregore711f702009-02-14 18:57:46 +0000554 // If this function is at translation-unit scope and we're not in
555 // C++, it refers to the C library function.
556 if (!Context.getLangOptions().CPlusPlus &&
557 getDeclContext()->isTranslationUnit())
558 return BuiltinID;
559
560 // If the function is in an extern "C" linkage specification and is
561 // not marked "overloadable", it's the real function.
562 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000563 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000564 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000565 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000566 return BuiltinID;
567
568 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000569 return 0;
570}
571
572
Chris Lattner47c0d002009-04-25 06:03:53 +0000573/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000574/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000575/// after it has been created.
576unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000577 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000578 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000579 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000580 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000581
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000582}
583
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000584void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
585 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000586 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000587 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000589 // Zero params -> null pointer.
590 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000591 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000592 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000593 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000594
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000595 // Update source range. The check below allows us to set EndRangeLoc before
596 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000597 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000598 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000599 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000600}
Chris Lattner41943152007-01-25 04:52:46 +0000601
Chris Lattner58258242008-04-10 02:22:51 +0000602/// getMinRequiredArguments - Returns the minimum number of arguments
603/// needed to call this function. This may be fewer than the number of
604/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000605/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000606unsigned FunctionDecl::getMinRequiredArguments() const {
607 unsigned NumRequiredArgs = getNumParams();
608 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000609 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000610 --NumRequiredArgs;
611
612 return NumRequiredArgs;
613}
614
Douglas Gregor299d76e2009-09-13 07:46:26 +0000615/// \brief For an inline function definition in C, determine whether the
616/// definition will be externally visible.
617///
618/// Inline function definitions are always available for inlining optimizations.
619/// However, depending on the language dialect, declaration specifiers, and
620/// attributes, the definition of an inline function may or may not be
621/// "externally" visible to other translation units in the program.
622///
623/// In C99, inline definitions are not externally visible by default. However,
624/// if even one of the globa-scope declarations is marked "extern inline", the
625/// inline definition becomes externally visible (C99 6.7.4p6).
626///
627/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
628/// definition, we use the GNU semantics for inline, which are nearly the
629/// opposite of C99 semantics. In particular, "inline" by itself will create
630/// an externally visible symbol, but "extern inline" will not create an
631/// externally visible symbol.
632bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
633 assert(isThisDeclarationADefinition() && "Must have the function definition");
634 assert(isInline() && "Function must be inline");
635
636 if (!getASTContext().getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
637 // GNU inline semantics. Based on a number of examples, we came up with the
638 // following heuristic: if the "inline" keyword is present on a
639 // declaration of the function but "extern" is not present on that
640 // declaration, then the symbol is externally visible. Otherwise, the GNU
641 // "extern inline" semantics applies and the symbol is not externally
642 // visible.
643 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
644 Redecl != RedeclEnd;
645 ++Redecl) {
646 if (Redecl->isInline() && Redecl->getStorageClass() != Extern)
647 return true;
648 }
649
650 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000651 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000652 }
653
654 // C99 6.7.4p6:
655 // [...] If all of the file scope declarations for a function in a
656 // translation unit include the inline function specifier without extern,
657 // then the definition in that translation unit is an inline definition.
658 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
659 Redecl != RedeclEnd;
660 ++Redecl) {
661 // Only consider file-scope declarations in this test.
662 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
663 continue;
664
665 if (!Redecl->isInline() || Redecl->getStorageClass() == Extern)
666 return true; // Not an inline definition
667 }
668
669 // C99 6.7.4p6:
670 // An inline definition does not provide an external definition for the
671 // function, and does not forbid an external definition in another
672 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000673 return false;
674}
675
Mike Stump11289f42009-09-09 15:08:12 +0000676void
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000677FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000678 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000679
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000680 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump11289f42009-09-09 15:08:12 +0000681 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000682 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
683 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
684 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
685 }
686}
687
Mike Stumpe7a2b482009-09-29 00:50:50 +0000688const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
689 return getFirstDeclaration();
690}
691
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000692FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000693 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000694}
695
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000696/// getOverloadedOperator - Which C++ overloaded operator this
697/// function represents, if any.
698OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000699 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
700 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000701 else
702 return OO_None;
703}
704
Douglas Gregord801b062009-10-07 23:56:10 +0000705FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
706 if (MemberSpecializationInfo *Info
707 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
708 return cast<FunctionDecl>(Info->getInstantiatedFrom());
709
710 return 0;
711}
712
713void
714FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
715 TemplateSpecializationKind TSK) {
716 assert(TemplateOrSpecialization.isNull() &&
717 "Member function is already a specialization");
718 MemberSpecializationInfo *Info
719 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
720 TemplateOrSpecialization = Info;
721}
722
Douglas Gregor70d83e22009-06-29 17:30:29 +0000723FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +0000724 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000725 = TemplateOrSpecialization
726 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +0000727 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +0000728 }
729 return 0;
730}
731
732const TemplateArgumentList *
733FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +0000734 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000735 = TemplateOrSpecialization
736 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
737 return Info->TemplateArguments;
738 }
739 return 0;
740}
741
Mike Stump11289f42009-09-09 15:08:12 +0000742void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000743FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
744 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000745 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000746 void *InsertPos,
747 TemplateSpecializationKind TSK) {
748 assert(TSK != TSK_Undeclared &&
749 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +0000750 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000751 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000752 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +0000753 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000754
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000755 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +0000756 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000757 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000758 Info->TemplateArguments = TemplateArgs;
759 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +0000760
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000761 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000762 // function template specializations.
763 if (InsertPos)
764 Template->getSpecializations().InsertNode(Info, InsertPos);
765 else {
766 // Try to insert the new node. If there is an existing node, remove it
767 // first.
768 FunctionTemplateSpecializationInfo *Existing
769 = Template->getSpecializations().GetOrInsertNode(Info);
770 if (Existing) {
771 Template->getSpecializations().RemoveNode(Existing);
772 Template->getSpecializations().GetOrInsertNode(Info);
773 }
774 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000775}
776
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000777TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +0000778 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000779 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +0000780 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +0000781 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +0000782 if (FTSInfo)
783 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +0000784
Douglas Gregord801b062009-10-07 23:56:10 +0000785 MemberSpecializationInfo *MSInfo
786 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
787 if (MSInfo)
788 return MSInfo->getTemplateSpecializationKind();
789
790 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000791}
792
Mike Stump11289f42009-09-09 15:08:12 +0000793void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000794FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Douglas Gregord801b062009-10-07 23:56:10 +0000795 if (FunctionTemplateSpecializationInfo *FTSInfo
796 = TemplateOrSpecialization.dyn_cast<
797 FunctionTemplateSpecializationInfo*>())
798 FTSInfo->setTemplateSpecializationKind(TSK);
799 else if (MemberSpecializationInfo *MSInfo
800 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
801 MSInfo->setTemplateSpecializationKind(TSK);
802 else
803 assert(false && "Function cannot have a template specialization kind");
Douglas Gregore8925db2009-06-29 22:39:32 +0000804}
805
Douglas Gregor6411b922009-09-11 20:15:17 +0000806bool FunctionDecl::isOutOfLine() const {
807 // FIXME: Should we restrict this to member functions?
808 if (Decl::isOutOfLine())
809 return true;
810
811 // If this function was instantiated from a member function of a
812 // class template, check whether that member function was defined out-of-line.
813 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
814 const FunctionDecl *Definition;
815 if (FD->getBody(Definition))
816 return Definition->isOutOfLine();
817 }
818
819 // If this function was instantiated from a function template,
820 // check whether that function template was defined out-of-line.
821 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
822 const FunctionDecl *Definition;
823 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
824 return Definition->isOutOfLine();
825 }
826
827 return false;
828}
829
Chris Lattner59a25942008-03-31 00:36:02 +0000830//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000831// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000832//===----------------------------------------------------------------------===//
833
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000834SourceRange TagDecl::getSourceRange() const {
835 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000836 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000837}
838
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000839TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000840 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000841}
842
Douglas Gregordee1be82009-01-17 00:42:38 +0000843void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000844 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
845 TagT->decl.setPointer(this);
846 TagT->decl.setInt(1);
847 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000848}
849
850void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +0000851 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000852 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
853 assert(TagT->decl.getPointer() == this &&
854 "Attempt to redefine a tag definition?");
855 TagT->decl.setInt(0);
856 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000857}
858
Ted Kremenek21475702008-09-05 17:16:31 +0000859TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000860 if (isDefinition())
861 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000862
863 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000864 R != REnd; ++R)
865 if (R->isDefinition())
866 return *R;
Mike Stump11289f42009-09-09 15:08:12 +0000867
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000868 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +0000869}
870
John McCall06f6fe8d2009-09-04 01:14:41 +0000871TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
872 switch (TypeSpec) {
873 default: llvm::llvm_unreachable("unexpected type specifier");
874 case DeclSpec::TST_struct: return TK_struct;
875 case DeclSpec::TST_class: return TK_class;
876 case DeclSpec::TST_union: return TK_union;
877 case DeclSpec::TST_enum: return TK_enum;
878 }
879}
880
Ted Kremenek21475702008-09-05 17:16:31 +0000881//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000882// RecordDecl Implementation
883//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000884
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000885RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000886 IdentifierInfo *Id, RecordDecl *PrevDecl,
887 SourceLocation TKL)
888 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000889 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000890 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000891 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000892 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000893}
894
895RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000896 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000897 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +0000898
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000899 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +0000900 C.getTypeDeclType(R, PrevDecl);
901 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000902}
903
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000904RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000905}
906
907void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000908 TagDecl::Destroy(C);
909}
910
Douglas Gregordfcad112009-03-25 15:59:44 +0000911bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +0000912 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +0000913 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
914}
915
Douglas Gregor91f84212008-12-11 16:49:14 +0000916/// completeDefinition - Notes that the definition of this type is now
917/// complete.
918void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000919 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +0000920 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +0000921}
Steve Naroffcc321422007-03-26 23:09:51 +0000922
Steve Naroff415d3d52008-10-08 17:01:13 +0000923//===----------------------------------------------------------------------===//
924// BlockDecl Implementation
925//===----------------------------------------------------------------------===//
926
927BlockDecl::~BlockDecl() {
928}
929
930void BlockDecl::Destroy(ASTContext& C) {
931 if (Body)
932 Body->Destroy(C);
933
934 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
935 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000936
937 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +0000938 Decl::Destroy(C);
939}
Steve Naroffc4b30e52009-03-13 16:56:44 +0000940
941void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
942 unsigned NParms) {
943 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +0000944
Steve Naroffc4b30e52009-03-13 16:56:44 +0000945 // Zero params -> null pointer.
946 if (NParms) {
947 NumParams = NParms;
948 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
949 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
950 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
951 }
952}
953
954unsigned BlockDecl::getNumParams() const {
955 return NumParams;
956}