blob: 903d9438ac3aeae187ed11780a424e618bcf2af4 [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
Douglas Gregorc732aba2009-09-11 18:44:32 +000094SourceRange ParmVarDecl::getDefaultArgRange() const {
95 if (const Expr *E = getInit())
96 return E->getSourceRange();
97
98 if (const Expr *E = getUninstantiatedDefaultArg())
99 return E->getSourceRange();
100
101 return SourceRange();
102}
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000103
Douglas Gregorc732aba2009-09-11 18:44:32 +0000104void VarDecl::setInit(ASTContext &C, Expr *I) {
105 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
106 Eval->~EvaluatedStmt();
107 C.Deallocate(Eval);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000108 }
109
Douglas Gregorc732aba2009-09-11 18:44:32 +0000110 Init = I;
111}
112
Douglas Gregor16618f22009-09-12 00:17:51 +0000113bool VarDecl::isExternC() const {
114 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000115 if (!Context.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +0000116 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000117 getStorageClass() != Static) ||
118 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
119
Mike Stump11289f42009-09-09 15:08:12 +0000120 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000121 DC = DC->getParent()) {
122 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
123 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
124 return getStorageClass() != Static;
125
126 break;
127 }
128
129 if (DC->isFunctionOrMethod())
130 return false;
131 }
132
133 return false;
134}
135
Chris Lattnerbec41342008-04-22 18:39:57 +0000136FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump11289f42009-09-09 15:08:12 +0000137 SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000138 DeclarationName N, QualType T,
139 DeclaratorInfo *DInfo,
Mike Stump11289f42009-09-09 15:08:12 +0000140 StorageClass S, bool isInline,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000141 bool hasWrittenPrototype) {
Mike Stump11289f42009-09-09 15:08:12 +0000142 FunctionDecl *New
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000143 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssone0dd1d52009-05-14 21:46:00 +0000144 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor739ef0c2009-02-25 16:33:18 +0000145 return New;
Chris Lattner5072bae2008-03-15 21:24:04 +0000146}
147
Steve Naroff1d95e5a2008-10-10 01:28:17 +0000148BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000149 return new (C) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +0000150}
151
Douglas Gregor91f84212008-12-11 16:49:14 +0000152FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000153 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000154 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
155 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattneree1284a2008-03-16 00:16:02 +0000156}
157
Douglas Gregorf4d33272009-01-07 19:46:03 +0000158bool FieldDecl::isAnonymousStructOrUnion() const {
159 if (!isImplicit() || getDeclName())
160 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000161
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000162 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregorf4d33272009-01-07 19:46:03 +0000163 return Record->getDecl()->isAnonymousStructOrUnion();
164
165 return false;
166}
Chris Lattner5072bae2008-03-15 21:24:04 +0000167
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000168EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
169 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000170 IdentifierInfo *Id, QualType T,
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000171 Expr *E, const llvm::APSInt &V) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000172 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattnera7b32872008-03-15 06:12:44 +0000173}
174
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000175void EnumConstantDecl::Destroy(ASTContext& C) {
176 if (Init) Init->Destroy(C);
177 Decl::Destroy(C);
178}
179
Chris Lattnerbec41342008-04-22 18:39:57 +0000180TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
John McCall703a3f82009-10-24 08:00:42 +0000181 SourceLocation L, IdentifierInfo *Id,
182 DeclaratorInfo *DInfo) {
183 return new (C) TypedefDecl(DC, L, Id, DInfo);
Chris Lattnera7b32872008-03-15 06:12:44 +0000184}
185
Chris Lattnerbec41342008-04-22 18:39:57 +0000186EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000187 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000188 EnumDecl *PrevDecl) {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000189 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000190 C.getTypeDeclType(Enum, PrevDecl);
191 return Enum;
Chris Lattnera7b32872008-03-15 06:12:44 +0000192}
193
Ted Kremenek123f0252008-09-02 20:13:32 +0000194void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek123f0252008-09-02 20:13:32 +0000195 Decl::Destroy(C);
196}
197
Douglas Gregor91f84212008-12-11 16:49:14 +0000198void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
199 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000200 IntegerType = NewType;
Douglas Gregordee1be82009-01-17 00:42:38 +0000201 TagDecl::completeDefinition();
Douglas Gregor91f84212008-12-11 16:49:14 +0000202}
203
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000204FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000205 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000206 StringLiteral *Str) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000207 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattneree1284a2008-03-16 00:16:02 +0000208}
209
Chris Lattnera7b32872008-03-15 06:12:44 +0000210//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000211// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000212//===----------------------------------------------------------------------===//
213
Douglas Gregor2ada0482009-02-04 17:27:36 +0000214std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000215 return getQualifiedNameAsString(getASTContext().getLangOptions());
216}
217
218std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000219 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
220 // std::string thrashing.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000221 std::vector<std::string> Names;
222 std::string QualName;
223 const DeclContext *Ctx = getDeclContext();
224
225 if (Ctx->isFunctionOrMethod())
226 return getNameAsString();
227
228 while (Ctx) {
229 if (Ctx->isFunctionOrMethod())
230 // FIXME: That probably will happen, when D was member of local
231 // scope class/struct/union. How do we handle this case?
232 break;
233
Mike Stump11289f42009-09-09 15:08:12 +0000234 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregor85673582009-05-18 17:01:57 +0000235 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
236 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
237 std::string TemplateArgsStr
238 = TemplateSpecializationType::PrintTemplateArgumentList(
239 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000240 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000241 P);
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000242 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Douglas Gregor85673582009-05-18 17:01:57 +0000243 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor2ada0482009-02-04 17:27:36 +0000244 Names.push_back(ND->getNameAsString());
245 else
246 break;
247
248 Ctx = Ctx->getParent();
249 }
250
251 std::vector<std::string>::reverse_iterator
252 I = Names.rbegin(),
253 End = Names.rend();
254
255 for (; I!=End; ++I)
256 QualName += *I + "::";
257
258 QualName += getNameAsString();
259
260 return QualName;
261}
262
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000263bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000264 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
265
Douglas Gregor889ceb72009-02-03 19:21:40 +0000266 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
267 // We want to keep it, unless it nominates same namespace.
268 if (getKind() == Decl::UsingDirective) {
269 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
270 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
271 }
Mike Stump11289f42009-09-09 15:08:12 +0000272
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000273 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
274 // For function declarations, we keep track of redeclarations.
275 return FD->getPreviousDeclaration() == OldD;
276
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000277 // For function templates, the underlying function declarations are linked.
278 if (const FunctionTemplateDecl *FunctionTemplate
279 = dyn_cast<FunctionTemplateDecl>(this))
280 if (const FunctionTemplateDecl *OldFunctionTemplate
281 = dyn_cast<FunctionTemplateDecl>(OldD))
282 return FunctionTemplate->getTemplatedDecl()
283 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000284
Steve Naroffc4173fa2009-02-22 19:35:57 +0000285 // For method declarations, we keep track of redeclarations.
286 if (isa<ObjCMethodDecl>(this))
287 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000288
John McCall9f3059a2009-10-09 21:13:30 +0000289 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
290 return true;
291
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000292 // For non-function declarations, if the declarations are of the
293 // same kind then this must be a redeclaration, or semantic analysis
294 // would not have given us the new declaration.
295 return this->getKind() == OldD->getKind();
296}
297
Douglas Gregoreddf4332009-02-24 20:03:32 +0000298bool NamedDecl::hasLinkage() const {
299 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
300 return VD->hasExternalStorage() || VD->isFileVarDecl();
301
302 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
303 return true;
304
305 return false;
306}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000307
Anders Carlsson6915bf62009-06-26 06:29:23 +0000308NamedDecl *NamedDecl::getUnderlyingDecl() {
309 NamedDecl *ND = this;
310 while (true) {
311 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
312 ND = UD->getTargetDecl();
313 else if (ObjCCompatibleAliasDecl *AD
314 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
315 return AD->getClassInterface();
316 else
317 return ND;
318 }
319}
320
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000321//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000322// DeclaratorDecl Implementation
323//===----------------------------------------------------------------------===//
324
325SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall17001972009-10-18 01:05:36 +0000326 if (DeclInfo) {
327 TypeLoc TL = DeclInfo->getTypeLoc();
328 while (true) {
329 TypeLoc NextTL = TL.getNextTypeLoc();
330 if (!NextTL)
331 return TL.getSourceRange().getBegin();
332 TL = NextTL;
333 }
334 }
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000335 return SourceLocation();
336}
337
338//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000339// VarDecl Implementation
340//===----------------------------------------------------------------------===//
341
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000342VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000343 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000344 StorageClass S) {
345 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000346}
347
348void VarDecl::Destroy(ASTContext& C) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000349 Expr *Init = getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000350 if (Init) {
Sebastian Redl0fb63472009-02-05 15:12:41 +0000351 Init->Destroy(C);
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000352 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
353 Eval->~EvaluatedStmt();
354 C.Deallocate(Eval);
355 }
356 }
Nuno Lopes394ec982008-12-17 23:39:55 +0000357 this->~VarDecl();
Steve Naroff13ae6f42009-01-27 21:25:57 +0000358 C.Deallocate((void *)this);
Nuno Lopes394ec982008-12-17 23:39:55 +0000359}
360
361VarDecl::~VarDecl() {
Nuno Lopes394ec982008-12-17 23:39:55 +0000362}
363
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000364SourceRange VarDecl::getSourceRange() const {
365 if (getInit())
366 return SourceRange(getLocation(), getInit()->getLocEnd());
367 return SourceRange(getLocation(), getLocation());
368}
369
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000370bool VarDecl::isOutOfLine() const {
371 if (!isStaticDataMember())
372 return false;
373
374 if (Decl::isOutOfLine())
375 return true;
376
377 // If this static data member was instantiated from a static data member of
378 // a class template, check whether that static data member was defined
379 // out-of-line.
380 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
381 return VD->isOutOfLine();
382
383 return false;
384}
385
386VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000387 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000388 return cast<VarDecl>(MSI->getInstantiatedFrom());
389
390 return 0;
391}
392
Douglas Gregor3c74d412009-10-14 20:14:33 +0000393TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor86d142a2009-10-08 07:24:58 +0000394 if (MemberSpecializationInfo *MSI
395 = getASTContext().getInstantiatedFromStaticDataMember(this))
396 return MSI->getTemplateSpecializationKind();
397
398 return TSK_Undeclared;
399}
400
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000401MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000402 return getASTContext().getInstantiatedFromStaticDataMember(this);
403}
404
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000405void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
406 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000407 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000408 assert(MSI && "Not an instantiated static data member?");
409 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000410 if (TSK != TSK_ExplicitSpecialization &&
411 PointOfInstantiation.isValid() &&
412 MSI->getPointOfInstantiation().isInvalid())
413 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000414}
415
Douglas Gregor0760fa12009-03-10 23:43:53 +0000416bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
417 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
418 return false;
419
Douglas Gregorbeecd582009-04-21 17:11:58 +0000420 const VarDecl *Def = 0;
421 return (!getDefinition(Def) &&
Douglas Gregor0760fa12009-03-10 23:43:53 +0000422 (getStorageClass() == None || getStorageClass() == Static));
423}
424
Ted Kremenekdfd72c22009-03-20 21:35:28 +0000425const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000426 redecl_iterator I = redecls_begin(), E = redecls_end();
427 while (I != E && !I->getInit())
428 ++I;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000429
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000430 if (I != E) {
431 Def = *I;
432 return I->getInit();
433 }
434 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000435}
436
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000437VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000438 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000439}
440
Nuno Lopes394ec982008-12-17 23:39:55 +0000441//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000442// FunctionDecl Implementation
443//===----------------------------------------------------------------------===//
444
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000445void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000446 if (Body && Body.isOffset())
447 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000448
449 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
450 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000451
Douglas Gregord801b062009-10-07 23:56:10 +0000452 FunctionTemplateSpecializationInfo *FTSInfo
453 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
454 if (FTSInfo)
455 C.Deallocate(FTSInfo);
456
457 MemberSpecializationInfo *MSInfo
458 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
459 if (MSInfo)
460 C.Deallocate(MSInfo);
461
Steve Naroff13ae6f42009-01-27 21:25:57 +0000462 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000463
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000464 Decl::Destroy(C);
465}
466
John McCalle1f2ec22009-09-11 06:45:03 +0000467void FunctionDecl::getNameForDiagnostic(std::string &S,
468 const PrintingPolicy &Policy,
469 bool Qualified) const {
470 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
471 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
472 if (TemplateArgs)
473 S += TemplateSpecializationType::PrintTemplateArgumentList(
474 TemplateArgs->getFlatArgumentList(),
475 TemplateArgs->flat_size(),
476 Policy);
477
478}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000479
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000480Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000481 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
482 if (I->Body) {
483 Definition = *I;
484 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000485 }
486 }
487
488 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000489}
490
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000491void FunctionDecl::setBody(Stmt *B) {
492 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000493 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000494 EndRangeLoc = B->getLocEnd();
495}
496
Douglas Gregor16618f22009-09-12 00:17:51 +0000497bool FunctionDecl::isMain() const {
498 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000499 return !Context.getLangOptions().Freestanding &&
500 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000501 getIdentifier() && getIdentifier()->isStr("main");
502}
503
Douglas Gregor16618f22009-09-12 00:17:51 +0000504bool FunctionDecl::isExternC() const {
505 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000506 // In C, any non-static, non-overloadable function has external
507 // linkage.
508 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000509 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000510
Mike Stump11289f42009-09-09 15:08:12 +0000511 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000512 DC = DC->getParent()) {
513 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
514 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000515 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000516 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000517
518 break;
519 }
520 }
521
522 return false;
523}
524
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000525bool FunctionDecl::isGlobal() const {
526 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
527 return Method->isStatic();
528
529 if (getStorageClass() == Static)
530 return false;
531
Mike Stump11289f42009-09-09 15:08:12 +0000532 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000533 DC->isNamespace();
534 DC = DC->getParent()) {
535 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
536 if (!Namespace->getDeclName())
537 return false;
538 break;
539 }
540 }
541
542 return true;
543}
544
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000545/// \brief Returns a value indicating whether this function
546/// corresponds to a builtin function.
547///
548/// The function corresponds to a built-in function if it is
549/// declared at translation scope or within an extern "C" block and
550/// its name matches with the name of a builtin. The returned value
551/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000552/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000553/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000554unsigned FunctionDecl::getBuiltinID() const {
555 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000556 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
557 return 0;
558
559 unsigned BuiltinID = getIdentifier()->getBuiltinID();
560 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
561 return BuiltinID;
562
563 // This function has the name of a known C library
564 // function. Determine whether it actually refers to the C library
565 // function or whether it just has the same name.
566
Douglas Gregora908e7f2009-02-17 03:23:10 +0000567 // If this is a static function, it's not a builtin.
568 if (getStorageClass() == Static)
569 return 0;
570
Douglas Gregore711f702009-02-14 18:57:46 +0000571 // If this function is at translation-unit scope and we're not in
572 // C++, it refers to the C library function.
573 if (!Context.getLangOptions().CPlusPlus &&
574 getDeclContext()->isTranslationUnit())
575 return BuiltinID;
576
577 // If the function is in an extern "C" linkage specification and is
578 // not marked "overloadable", it's the real function.
579 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000580 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000581 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000582 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000583 return BuiltinID;
584
585 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000586 return 0;
587}
588
589
Chris Lattner47c0d002009-04-25 06:03:53 +0000590/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000591/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000592/// after it has been created.
593unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000594 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000595 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000596 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000597 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000598
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000599}
600
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000601void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
602 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000603 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000604 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000605
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000606 // Zero params -> null pointer.
607 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000608 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000609 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000610 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000611
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000612 // Update source range. The check below allows us to set EndRangeLoc before
613 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000614 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000615 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000616 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000617}
Chris Lattner41943152007-01-25 04:52:46 +0000618
Chris Lattner58258242008-04-10 02:22:51 +0000619/// getMinRequiredArguments - Returns the minimum number of arguments
620/// needed to call this function. This may be fewer than the number of
621/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000622/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000623unsigned FunctionDecl::getMinRequiredArguments() const {
624 unsigned NumRequiredArgs = getNumParams();
625 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000626 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000627 --NumRequiredArgs;
628
629 return NumRequiredArgs;
630}
631
Douglas Gregor299d76e2009-09-13 07:46:26 +0000632/// \brief For an inline function definition in C, determine whether the
633/// definition will be externally visible.
634///
635/// Inline function definitions are always available for inlining optimizations.
636/// However, depending on the language dialect, declaration specifiers, and
637/// attributes, the definition of an inline function may or may not be
638/// "externally" visible to other translation units in the program.
639///
640/// In C99, inline definitions are not externally visible by default. However,
641/// if even one of the globa-scope declarations is marked "extern inline", the
642/// inline definition becomes externally visible (C99 6.7.4p6).
643///
644/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
645/// definition, we use the GNU semantics for inline, which are nearly the
646/// opposite of C99 semantics. In particular, "inline" by itself will create
647/// an externally visible symbol, but "extern inline" will not create an
648/// externally visible symbol.
649bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
650 assert(isThisDeclarationADefinition() && "Must have the function definition");
651 assert(isInline() && "Function must be inline");
652
653 if (!getASTContext().getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
654 // GNU inline semantics. Based on a number of examples, we came up with the
655 // following heuristic: if the "inline" keyword is present on a
656 // declaration of the function but "extern" is not present on that
657 // declaration, then the symbol is externally visible. Otherwise, the GNU
658 // "extern inline" semantics applies and the symbol is not externally
659 // visible.
660 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
661 Redecl != RedeclEnd;
662 ++Redecl) {
663 if (Redecl->isInline() && Redecl->getStorageClass() != Extern)
664 return true;
665 }
666
667 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000668 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000669 }
670
671 // C99 6.7.4p6:
672 // [...] If all of the file scope declarations for a function in a
673 // translation unit include the inline function specifier without extern,
674 // then the definition in that translation unit is an inline definition.
675 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
676 Redecl != RedeclEnd;
677 ++Redecl) {
678 // Only consider file-scope declarations in this test.
679 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
680 continue;
681
682 if (!Redecl->isInline() || Redecl->getStorageClass() == Extern)
683 return true; // Not an inline definition
684 }
685
686 // C99 6.7.4p6:
687 // An inline definition does not provide an external definition for the
688 // function, and does not forbid an external definition in another
689 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000690 return false;
691}
692
Mike Stump11289f42009-09-09 15:08:12 +0000693void
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000694FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000695 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000696
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000697 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump11289f42009-09-09 15:08:12 +0000698 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000699 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
700 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
701 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
702 }
703}
704
Mike Stumpe7a2b482009-09-29 00:50:50 +0000705const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
706 return getFirstDeclaration();
707}
708
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000709FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000710 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000711}
712
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000713/// getOverloadedOperator - Which C++ overloaded operator this
714/// function represents, if any.
715OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000716 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
717 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000718 else
719 return OO_None;
720}
721
Douglas Gregord801b062009-10-07 23:56:10 +0000722FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000723 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +0000724 return cast<FunctionDecl>(Info->getInstantiatedFrom());
725
726 return 0;
727}
728
Douglas Gregor06db9f52009-10-12 20:18:28 +0000729MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
730 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
731}
732
Douglas Gregord801b062009-10-07 23:56:10 +0000733void
734FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
735 TemplateSpecializationKind TSK) {
736 assert(TemplateOrSpecialization.isNull() &&
737 "Member function is already a specialization");
738 MemberSpecializationInfo *Info
739 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
740 TemplateOrSpecialization = Info;
741}
742
Douglas Gregor70d83e22009-06-29 17:30:29 +0000743FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +0000744 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000745 = TemplateOrSpecialization
746 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +0000747 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +0000748 }
749 return 0;
750}
751
752const TemplateArgumentList *
753FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +0000754 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +0000755 = TemplateOrSpecialization
756 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +0000757 return Info->TemplateArguments;
758 }
759 return 0;
760}
761
Mike Stump11289f42009-09-09 15:08:12 +0000762void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000763FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
764 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000765 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000766 void *InsertPos,
767 TemplateSpecializationKind TSK) {
768 assert(TSK != TSK_Undeclared &&
769 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +0000770 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000771 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000772 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +0000773 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000774
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000775 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +0000776 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000777 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000778 Info->TemplateArguments = TemplateArgs;
779 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +0000780
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000781 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000782 // function template specializations.
783 if (InsertPos)
784 Template->getSpecializations().InsertNode(Info, InsertPos);
785 else {
786 // Try to insert the new node. If there is an existing node, remove it
787 // first.
788 FunctionTemplateSpecializationInfo *Existing
789 = Template->getSpecializations().GetOrInsertNode(Info);
790 if (Existing) {
791 Template->getSpecializations().RemoveNode(Existing);
792 Template->getSpecializations().GetOrInsertNode(Info);
793 }
794 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000795}
796
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000797TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +0000798 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000799 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +0000800 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +0000801 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +0000802 if (FTSInfo)
803 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +0000804
Douglas Gregord801b062009-10-07 23:56:10 +0000805 MemberSpecializationInfo *MSInfo
806 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
807 if (MSInfo)
808 return MSInfo->getTemplateSpecializationKind();
809
810 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000811}
812
Mike Stump11289f42009-09-09 15:08:12 +0000813void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000814FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
815 SourceLocation PointOfInstantiation) {
816 if (FunctionTemplateSpecializationInfo *FTSInfo
817 = TemplateOrSpecialization.dyn_cast<
818 FunctionTemplateSpecializationInfo*>()) {
819 FTSInfo->setTemplateSpecializationKind(TSK);
820 if (TSK != TSK_ExplicitSpecialization &&
821 PointOfInstantiation.isValid() &&
822 FTSInfo->getPointOfInstantiation().isInvalid())
823 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
824 } else if (MemberSpecializationInfo *MSInfo
825 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
826 MSInfo->setTemplateSpecializationKind(TSK);
827 if (TSK != TSK_ExplicitSpecialization &&
828 PointOfInstantiation.isValid() &&
829 MSInfo->getPointOfInstantiation().isInvalid())
830 MSInfo->setPointOfInstantiation(PointOfInstantiation);
831 } else
832 assert(false && "Function cannot have a template specialization kind");
833}
834
835SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +0000836 if (FunctionTemplateSpecializationInfo *FTSInfo
837 = TemplateOrSpecialization.dyn_cast<
838 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000839 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +0000840 else if (MemberSpecializationInfo *MSInfo
841 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000842 return MSInfo->getPointOfInstantiation();
843
844 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +0000845}
846
Douglas Gregor6411b922009-09-11 20:15:17 +0000847bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +0000848 if (Decl::isOutOfLine())
849 return true;
850
851 // If this function was instantiated from a member function of a
852 // class template, check whether that member function was defined out-of-line.
853 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
854 const FunctionDecl *Definition;
855 if (FD->getBody(Definition))
856 return Definition->isOutOfLine();
857 }
858
859 // If this function was instantiated from a function template,
860 // check whether that function template was defined out-of-line.
861 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
862 const FunctionDecl *Definition;
863 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
864 return Definition->isOutOfLine();
865 }
866
867 return false;
868}
869
Chris Lattner59a25942008-03-31 00:36:02 +0000870//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000871// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000872//===----------------------------------------------------------------------===//
873
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000874SourceRange TagDecl::getSourceRange() const {
875 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000876 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000877}
878
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000879TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000880 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000881}
882
Douglas Gregordee1be82009-01-17 00:42:38 +0000883void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000884 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
885 TagT->decl.setPointer(this);
886 TagT->decl.setInt(1);
887 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000888}
889
890void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +0000891 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000892 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
893 assert(TagT->decl.getPointer() == this &&
894 "Attempt to redefine a tag definition?");
895 TagT->decl.setInt(0);
896 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000897}
898
Ted Kremenek21475702008-09-05 17:16:31 +0000899TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000900 if (isDefinition())
901 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000902
903 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000904 R != REnd; ++R)
905 if (R->isDefinition())
906 return *R;
Mike Stump11289f42009-09-09 15:08:12 +0000907
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000908 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +0000909}
910
John McCall06f6fe8d2009-09-04 01:14:41 +0000911TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
912 switch (TypeSpec) {
913 default: llvm::llvm_unreachable("unexpected type specifier");
914 case DeclSpec::TST_struct: return TK_struct;
915 case DeclSpec::TST_class: return TK_class;
916 case DeclSpec::TST_union: return TK_union;
917 case DeclSpec::TST_enum: return TK_enum;
918 }
919}
920
Ted Kremenek21475702008-09-05 17:16:31 +0000921//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000922// RecordDecl Implementation
923//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000924
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000925RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000926 IdentifierInfo *Id, RecordDecl *PrevDecl,
927 SourceLocation TKL)
928 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000929 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000930 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000931 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000932 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000933}
934
935RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000936 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000937 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +0000938
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000939 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +0000940 C.getTypeDeclType(R, PrevDecl);
941 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000942}
943
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000944RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000945}
946
947void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000948 TagDecl::Destroy(C);
949}
950
Douglas Gregordfcad112009-03-25 15:59:44 +0000951bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +0000952 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +0000953 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
954}
955
Douglas Gregor91f84212008-12-11 16:49:14 +0000956/// completeDefinition - Notes that the definition of this type is now
957/// complete.
958void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000959 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +0000960 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +0000961}
Steve Naroffcc321422007-03-26 23:09:51 +0000962
Steve Naroff415d3d52008-10-08 17:01:13 +0000963//===----------------------------------------------------------------------===//
964// BlockDecl Implementation
965//===----------------------------------------------------------------------===//
966
967BlockDecl::~BlockDecl() {
968}
969
970void BlockDecl::Destroy(ASTContext& C) {
971 if (Body)
972 Body->Destroy(C);
973
974 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
975 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +0000976
977 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +0000978 Decl::Destroy(C);
979}
Steve Naroffc4b30e52009-03-13 16:56:44 +0000980
981void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
982 unsigned NParms) {
983 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +0000984
Steve Naroffc4b30e52009-03-13 16:56:44 +0000985 // Zero params -> null pointer.
986 if (NParms) {
987 NumParams = NParms;
988 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
989 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
990 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
991 }
992}
993
994unsigned BlockDecl::getNumParams() const {
995 return NumParams;
996}