blob: 28d0a8357e2ce12940047ed2f2b818b25b869623 [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
Douglas Gregor1d957a32009-10-27 18:42:08 +0000386VarDecl *VarDecl::getOutOfLineDefinition() {
387 if (!isStaticDataMember())
388 return 0;
389
390 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
391 RD != RDEnd; ++RD) {
392 if (RD->getLexicalDeclContext()->isFileContext())
393 return *RD;
394 }
395
396 return 0;
397}
398
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000399VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000400 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000401 return cast<VarDecl>(MSI->getInstantiatedFrom());
402
403 return 0;
404}
405
Douglas Gregor3c74d412009-10-14 20:14:33 +0000406TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor86d142a2009-10-08 07:24:58 +0000407 if (MemberSpecializationInfo *MSI
408 = getASTContext().getInstantiatedFromStaticDataMember(this))
409 return MSI->getTemplateSpecializationKind();
410
411 return TSK_Undeclared;
412}
413
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000414MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000415 return getASTContext().getInstantiatedFromStaticDataMember(this);
416}
417
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000418void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
419 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000420 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000421 assert(MSI && "Not an instantiated static data member?");
422 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000423 if (TSK != TSK_ExplicitSpecialization &&
424 PointOfInstantiation.isValid() &&
425 MSI->getPointOfInstantiation().isInvalid())
426 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000427}
428
Douglas Gregor0760fa12009-03-10 23:43:53 +0000429bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
430 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
431 return false;
432
Douglas Gregorbeecd582009-04-21 17:11:58 +0000433 const VarDecl *Def = 0;
434 return (!getDefinition(Def) &&
Douglas Gregor0760fa12009-03-10 23:43:53 +0000435 (getStorageClass() == None || getStorageClass() == Static));
436}
437
Ted Kremenekdfd72c22009-03-20 21:35:28 +0000438const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000439 redecl_iterator I = redecls_begin(), E = redecls_end();
440 while (I != E && !I->getInit())
441 ++I;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000442
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000443 if (I != E) {
444 Def = *I;
445 return I->getInit();
446 }
447 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000448}
449
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000450VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000451 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000452}
453
Nuno Lopes394ec982008-12-17 23:39:55 +0000454//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000455// FunctionDecl Implementation
456//===----------------------------------------------------------------------===//
457
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000458void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor3c3aa612009-04-18 00:07:54 +0000459 if (Body && Body.isOffset())
460 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000461
462 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
463 (*I)->Destroy(C);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000464
Douglas Gregord801b062009-10-07 23:56:10 +0000465 FunctionTemplateSpecializationInfo *FTSInfo
466 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
467 if (FTSInfo)
468 C.Deallocate(FTSInfo);
469
470 MemberSpecializationInfo *MSInfo
471 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
472 if (MSInfo)
473 C.Deallocate(MSInfo);
474
Steve Naroff13ae6f42009-01-27 21:25:57 +0000475 C.Deallocate(ParamInfo);
Nuno Lopes9018ca72009-01-18 19:57:27 +0000476
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000477 Decl::Destroy(C);
478}
479
John McCalle1f2ec22009-09-11 06:45:03 +0000480void FunctionDecl::getNameForDiagnostic(std::string &S,
481 const PrintingPolicy &Policy,
482 bool Qualified) const {
483 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
484 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
485 if (TemplateArgs)
486 S += TemplateSpecializationType::PrintTemplateArgumentList(
487 TemplateArgs->getFlatArgumentList(),
488 TemplateArgs->flat_size(),
489 Policy);
490
491}
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000492
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000493Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +0000494 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
495 if (I->Body) {
496 Definition = *I;
497 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +0000498 }
499 }
500
501 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000502}
503
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000504void FunctionDecl::setBody(Stmt *B) {
505 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +0000506 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000507 EndRangeLoc = B->getLocEnd();
508}
509
Douglas Gregor16618f22009-09-12 00:17:51 +0000510bool FunctionDecl::isMain() const {
511 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +0000512 return !Context.getLangOptions().Freestanding &&
513 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +0000514 getIdentifier() && getIdentifier()->isStr("main");
515}
516
Douglas Gregor16618f22009-09-12 00:17:51 +0000517bool FunctionDecl::isExternC() const {
518 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000519 // In C, any non-static, non-overloadable function has external
520 // linkage.
521 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000522 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000523
Mike Stump11289f42009-09-09 15:08:12 +0000524 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000525 DC = DC->getParent()) {
526 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
527 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump11289f42009-09-09 15:08:12 +0000528 return getStorageClass() != Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000529 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +0000530
531 break;
532 }
533 }
534
535 return false;
536}
537
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000538bool FunctionDecl::isGlobal() const {
539 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
540 return Method->isStatic();
541
542 if (getStorageClass() == Static)
543 return false;
544
Mike Stump11289f42009-09-09 15:08:12 +0000545 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +0000546 DC->isNamespace();
547 DC = DC->getParent()) {
548 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
549 if (!Namespace->getDeclName())
550 return false;
551 break;
552 }
553 }
554
555 return true;
556}
557
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000558/// \brief Returns a value indicating whether this function
559/// corresponds to a builtin function.
560///
561/// The function corresponds to a built-in function if it is
562/// declared at translation scope or within an extern "C" block and
563/// its name matches with the name of a builtin. The returned value
564/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +0000565/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000566/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +0000567unsigned FunctionDecl::getBuiltinID() const {
568 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +0000569 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
570 return 0;
571
572 unsigned BuiltinID = getIdentifier()->getBuiltinID();
573 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
574 return BuiltinID;
575
576 // This function has the name of a known C library
577 // function. Determine whether it actually refers to the C library
578 // function or whether it just has the same name.
579
Douglas Gregora908e7f2009-02-17 03:23:10 +0000580 // If this is a static function, it's not a builtin.
581 if (getStorageClass() == Static)
582 return 0;
583
Douglas Gregore711f702009-02-14 18:57:46 +0000584 // If this function is at translation-unit scope and we're not in
585 // C++, it refers to the C library function.
586 if (!Context.getLangOptions().CPlusPlus &&
587 getDeclContext()->isTranslationUnit())
588 return BuiltinID;
589
590 // If the function is in an extern "C" linkage specification and is
591 // not marked "overloadable", it's the real function.
592 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +0000593 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +0000594 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000595 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +0000596 return BuiltinID;
597
598 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +0000599 return 0;
600}
601
602
Chris Lattner47c0d002009-04-25 06:03:53 +0000603/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +0000604/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +0000605/// after it has been created.
606unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +0000607 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000608 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000609 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000610 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000612}
613
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000614void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
615 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000616 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +0000617 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000618
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000619 // Zero params -> null pointer.
620 if (NumParams) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +0000621 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000622 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000623 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000624
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +0000625 // Update source range. The check below allows us to set EndRangeLoc before
626 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +0000627 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000628 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000629 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000630}
Chris Lattner41943152007-01-25 04:52:46 +0000631
Chris Lattner58258242008-04-10 02:22:51 +0000632/// getMinRequiredArguments - Returns the minimum number of arguments
633/// needed to call this function. This may be fewer than the number of
634/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000635/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000636unsigned FunctionDecl::getMinRequiredArguments() const {
637 unsigned NumRequiredArgs = getNumParams();
638 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +0000639 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +0000640 --NumRequiredArgs;
641
642 return NumRequiredArgs;
643}
644
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000645bool FunctionDecl::isInlined() const {
646 return isInlineSpecified() || (isa<CXXMethodDecl>(this) && !isOutOfLine());
647}
648
Douglas Gregor299d76e2009-09-13 07:46:26 +0000649/// \brief For an inline function definition in C, determine whether the
650/// definition will be externally visible.
651///
652/// Inline function definitions are always available for inlining optimizations.
653/// However, depending on the language dialect, declaration specifiers, and
654/// attributes, the definition of an inline function may or may not be
655/// "externally" visible to other translation units in the program.
656///
657/// In C99, inline definitions are not externally visible by default. However,
658/// if even one of the globa-scope declarations is marked "extern inline", the
659/// inline definition becomes externally visible (C99 6.7.4p6).
660///
661/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
662/// definition, we use the GNU semantics for inline, which are nearly the
663/// opposite of C99 semantics. In particular, "inline" by itself will create
664/// an externally visible symbol, but "extern inline" will not create an
665/// externally visible symbol.
666bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
667 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000668 assert(isInlined() && "Function must be inline");
Douglas Gregor299d76e2009-09-13 07:46:26 +0000669
670 if (!getASTContext().getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
671 // GNU inline semantics. Based on a number of examples, we came up with the
672 // following heuristic: if the "inline" keyword is present on a
673 // declaration of the function but "extern" is not present on that
674 // declaration, then the symbol is externally visible. Otherwise, the GNU
675 // "extern inline" semantics applies and the symbol is not externally
676 // visible.
677 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
678 Redecl != RedeclEnd;
679 ++Redecl) {
Douglas Gregor35b57532009-10-27 21:01:01 +0000680 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000681 return true;
682 }
683
684 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000685 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +0000686 }
687
688 // C99 6.7.4p6:
689 // [...] If all of the file scope declarations for a function in a
690 // translation unit include the inline function specifier without extern,
691 // then the definition in that translation unit is an inline definition.
692 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
693 Redecl != RedeclEnd;
694 ++Redecl) {
695 // Only consider file-scope declarations in this test.
696 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
697 continue;
698
Douglas Gregor35b57532009-10-27 21:01:01 +0000699 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +0000700 return true; // Not an inline definition
701 }
702
703 // C99 6.7.4p6:
704 // An inline definition does not provide an external definition for the
705 // function, and does not forbid an external definition in another
706 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +0000707 return false;
708}
709
Mike Stump11289f42009-09-09 15:08:12 +0000710void
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000711FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000712 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000713
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000714 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump11289f42009-09-09 15:08:12 +0000715 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000716 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
717 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
718 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
719 }
720}
721
Mike Stumpe7a2b482009-09-29 00:50:50 +0000722const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
723 return getFirstDeclaration();
724}
725
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000726FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +0000727 return getFirstDeclaration();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +0000728}
729
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000730/// getOverloadedOperator - Which C++ overloaded operator this
731/// function represents, if any.
732OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000733 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
734 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000735 else
736 return OO_None;
737}
738
Douglas Gregord801b062009-10-07 23:56:10 +0000739FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000740 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +0000741 return cast<FunctionDecl>(Info->getInstantiatedFrom());
742
743 return 0;
744}
745
Douglas Gregor06db9f52009-10-12 20:18:28 +0000746MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
747 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
748}
749
Douglas Gregord801b062009-10-07 23:56:10 +0000750void
751FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
752 TemplateSpecializationKind TSK) {
753 assert(TemplateOrSpecialization.isNull() &&
754 "Member function is already a specialization");
755 MemberSpecializationInfo *Info
756 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
757 TemplateOrSpecialization = Info;
758}
759
Douglas Gregorafca3b42009-10-27 20:53:28 +0000760bool FunctionDecl::isImplicitlyInstantiable() const {
761 // If this function already has a definition or is invalid, it can't be
762 // implicitly instantiated.
763 if (isInvalidDecl() || getBody())
764 return false;
765
766 switch (getTemplateSpecializationKind()) {
767 case TSK_Undeclared:
768 case TSK_ExplicitSpecialization:
769 case TSK_ExplicitInstantiationDefinition:
770 return false;
771
772 case TSK_ImplicitInstantiation:
773 return true;
774
775 case TSK_ExplicitInstantiationDeclaration:
776 // Handled below.
777 break;
778 }
779
780 // Find the actual template from which we will instantiate.
781 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
782 Stmt *Pattern = 0;
783 if (PatternDecl)
784 Pattern = PatternDecl->getBody(PatternDecl);
785
786 // C++0x [temp.explicit]p9:
787 // Except for inline functions, other explicit instantiation declarations
788 // have the effect of suppressing the implicit instantiation of the entity
789 // to which they refer.
790 if (!Pattern || !PatternDecl)
791 return true;
792
Douglas Gregor583dcaf2009-10-27 21:11:48 +0000793 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +0000794}
795
796FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
797 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
798 while (Primary->getInstantiatedFromMemberTemplate()) {
799 // If we have hit a point where the user provided a specialization of
800 // this template, we're done looking.
801 if (Primary->isMemberSpecialization())
802 break;
803
804 Primary = Primary->getInstantiatedFromMemberTemplate();
805 }
806
807 return Primary->getTemplatedDecl();
808 }
809
810 return getInstantiatedFromMemberFunction();
811}
812
Douglas Gregor70d83e22009-06-29 17:30:29 +0000813FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +0000814 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000815 = TemplateOrSpecialization
816 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +0000817 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +0000818 }
819 return 0;
820}
821
822const TemplateArgumentList *
823FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +0000824 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +0000825 = TemplateOrSpecialization
826 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +0000827 return Info->TemplateArguments;
828 }
829 return 0;
830}
831
Mike Stump11289f42009-09-09 15:08:12 +0000832void
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000833FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
834 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000835 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000836 void *InsertPos,
837 TemplateSpecializationKind TSK) {
838 assert(TSK != TSK_Undeclared &&
839 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +0000840 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +0000841 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000842 if (!Info)
Douglas Gregor70d83e22009-06-29 17:30:29 +0000843 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000844
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000845 Info->Function = this;
Douglas Gregore8925db2009-06-29 22:39:32 +0000846 Info->Template.setPointer(Template);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000847 Info->Template.setInt(TSK - 1);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000848 Info->TemplateArguments = TemplateArgs;
849 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +0000850
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000851 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000852 // function template specializations.
853 if (InsertPos)
854 Template->getSpecializations().InsertNode(Info, InsertPos);
855 else {
856 // Try to insert the new node. If there is an existing node, remove it
857 // first.
858 FunctionTemplateSpecializationInfo *Existing
859 = Template->getSpecializations().GetOrInsertNode(Info);
860 if (Existing) {
861 Template->getSpecializations().RemoveNode(Existing);
862 Template->getSpecializations().GetOrInsertNode(Info);
863 }
864 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000865}
866
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000867TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +0000868 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000869 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +0000870 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +0000871 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +0000872 if (FTSInfo)
873 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +0000874
Douglas Gregord801b062009-10-07 23:56:10 +0000875 MemberSpecializationInfo *MSInfo
876 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
877 if (MSInfo)
878 return MSInfo->getTemplateSpecializationKind();
879
880 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000881}
882
Mike Stump11289f42009-09-09 15:08:12 +0000883void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000884FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
885 SourceLocation PointOfInstantiation) {
886 if (FunctionTemplateSpecializationInfo *FTSInfo
887 = TemplateOrSpecialization.dyn_cast<
888 FunctionTemplateSpecializationInfo*>()) {
889 FTSInfo->setTemplateSpecializationKind(TSK);
890 if (TSK != TSK_ExplicitSpecialization &&
891 PointOfInstantiation.isValid() &&
892 FTSInfo->getPointOfInstantiation().isInvalid())
893 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
894 } else if (MemberSpecializationInfo *MSInfo
895 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
896 MSInfo->setTemplateSpecializationKind(TSK);
897 if (TSK != TSK_ExplicitSpecialization &&
898 PointOfInstantiation.isValid() &&
899 MSInfo->getPointOfInstantiation().isInvalid())
900 MSInfo->setPointOfInstantiation(PointOfInstantiation);
901 } else
902 assert(false && "Function cannot have a template specialization kind");
903}
904
905SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +0000906 if (FunctionTemplateSpecializationInfo *FTSInfo
907 = TemplateOrSpecialization.dyn_cast<
908 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000909 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +0000910 else if (MemberSpecializationInfo *MSInfo
911 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000912 return MSInfo->getPointOfInstantiation();
913
914 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +0000915}
916
Douglas Gregor6411b922009-09-11 20:15:17 +0000917bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +0000918 if (Decl::isOutOfLine())
919 return true;
920
921 // If this function was instantiated from a member function of a
922 // class template, check whether that member function was defined out-of-line.
923 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
924 const FunctionDecl *Definition;
925 if (FD->getBody(Definition))
926 return Definition->isOutOfLine();
927 }
928
929 // If this function was instantiated from a function template,
930 // check whether that function template was defined out-of-line.
931 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
932 const FunctionDecl *Definition;
933 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
934 return Definition->isOutOfLine();
935 }
936
937 return false;
938}
939
Chris Lattner59a25942008-03-31 00:36:02 +0000940//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000941// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000942//===----------------------------------------------------------------------===//
943
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000944SourceRange TagDecl::getSourceRange() const {
945 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000946 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +0000947}
948
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000949TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000950 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +0000951}
952
Douglas Gregordee1be82009-01-17 00:42:38 +0000953void TagDecl::startDefinition() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000954 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
955 TagT->decl.setPointer(this);
956 TagT->decl.setInt(1);
957 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000958}
959
960void TagDecl::completeDefinition() {
Douglas Gregordee1be82009-01-17 00:42:38 +0000961 IsDefinition = true;
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000962 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
963 assert(TagT->decl.getPointer() == this &&
964 "Attempt to redefine a tag definition?");
965 TagT->decl.setInt(0);
966 }
Douglas Gregordee1be82009-01-17 00:42:38 +0000967}
968
Ted Kremenek21475702008-09-05 17:16:31 +0000969TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000970 if (isDefinition())
971 return const_cast<TagDecl *>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000972
973 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000974 R != REnd; ++R)
975 if (R->isDefinition())
976 return *R;
Mike Stump11289f42009-09-09 15:08:12 +0000977
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000978 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +0000979}
980
John McCall06f6fe8d2009-09-04 01:14:41 +0000981TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
982 switch (TypeSpec) {
983 default: llvm::llvm_unreachable("unexpected type specifier");
984 case DeclSpec::TST_struct: return TK_struct;
985 case DeclSpec::TST_class: return TK_class;
986 case DeclSpec::TST_union: return TK_union;
987 case DeclSpec::TST_enum: return TK_enum;
988 }
989}
990
Ted Kremenek21475702008-09-05 17:16:31 +0000991//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000992// RecordDecl Implementation
993//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000994
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000995RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000996 IdentifierInfo *Id, RecordDecl *PrevDecl,
997 SourceLocation TKL)
998 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000999 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001000 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001001 HasObjectMember = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001002 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001003}
1004
1005RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001006 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001007 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001008
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001009 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001010 C.getTypeDeclType(R, PrevDecl);
1011 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001012}
1013
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001014RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001015}
1016
1017void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00001018 TagDecl::Destroy(C);
1019}
1020
Douglas Gregordfcad112009-03-25 15:59:44 +00001021bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001022 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001023 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1024}
1025
Douglas Gregor91f84212008-12-11 16:49:14 +00001026/// completeDefinition - Notes that the definition of this type is now
1027/// complete.
1028void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +00001029 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001030 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001031}
Steve Naroffcc321422007-03-26 23:09:51 +00001032
Steve Naroff415d3d52008-10-08 17:01:13 +00001033//===----------------------------------------------------------------------===//
1034// BlockDecl Implementation
1035//===----------------------------------------------------------------------===//
1036
1037BlockDecl::~BlockDecl() {
1038}
1039
1040void BlockDecl::Destroy(ASTContext& C) {
1041 if (Body)
1042 Body->Destroy(C);
1043
1044 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1045 (*I)->Destroy(C);
Mike Stump11289f42009-09-09 15:08:12 +00001046
1047 C.Deallocate(ParamInfo);
Steve Naroff415d3d52008-10-08 17:01:13 +00001048 Decl::Destroy(C);
1049}
Steve Naroffc4b30e52009-03-13 16:56:44 +00001050
1051void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1052 unsigned NParms) {
1053 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001054
Steve Naroffc4b30e52009-03-13 16:56:44 +00001055 // Zero params -> null pointer.
1056 if (NParms) {
1057 NumParams = NParms;
1058 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1059 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1060 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1061 }
1062}
1063
1064unsigned BlockDecl::getNumParams() const {
1065 return NumParams;
1066}