blob: d270a958f0aefabd4c8f3386d7141acb1d1fe368 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000022#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000024#include "clang/Basic/IdentifierTable.h"
John McCallf1bbbb42009-09-04 01:14:41 +000025#include "clang/Parse/DeclSpec.h"
26#include "llvm/Support/ErrorHandling.h"
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000027#include <vector>
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattner0b2b6e12009-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 Kyrtzidisb17166c2009-08-19 01:27:32 +000040/// \brief Return the TypeLoc wrapper for the type source info.
41TypeLoc DeclaratorInfo::getTypeLoc() const {
Argyrios Kyrtzidisb7354712009-09-29 19:40:20 +000042 return TypeLoc(Ty, (void*)(this + 1));
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000043}
Chris Lattner0b2b6e12009-03-04 06:34:08 +000044
Chris Lattnerd3b90652008-03-15 05:43:15 +000045//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000046// Decl Allocation/Deallocation Method Implementations
47//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattner0b2b6e12009-03-04 06:34:08 +000049
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000050TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +000051 return new (C) TranslationUnitDecl(C);
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000052}
53
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000054NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
55 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000056 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000057}
58
Ted Kremenekd1ac17a2008-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 Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenekebf27b12008-05-24 15:09:56 +000063 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000065}
66
67
Chris Lattner41110242008-06-17 18:05:57 +000068ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000069 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000070 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000071}
72
Daniel Dunbarb286a782009-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 Stump1eb44332009-09-09 15:08:12 +000078 case VarDecl::PrivateExtern: return "__private_extern__"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000079 case VarDecl::Register: return "register"; break;
Mike Stump1eb44332009-09-09 15:08:12 +000080 case VarDecl::Static: return "static"; break;
Daniel Dunbarb286a782009-04-14 02:08:49 +000081 }
82
83 assert(0 && "Invalid storage class");
84 return 0;
85}
86
Chris Lattner9fdf9c62008-04-22 18:39:57 +000087ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000088 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-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 Jahanian4306d3c2008-12-20 23:29:59 +000092}
93
94QualType ParmVarDecl::getOriginalType() const {
Mike Stump1eb44332009-09-09 15:08:12 +000095 if (const OriginalParmVarDecl *PVD =
Douglas Gregor64650af2009-02-02 23:39:07 +000096 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000097 return PVD->OriginalType;
98 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000099}
100
Douglas Gregor6cc15182009-09-11 18:44:32 +0000101SourceRange ParmVarDecl::getDefaultArgRange() const {
102 if (const Expr *E = getInit())
103 return E->getSourceRange();
104
105 if (const Expr *E = getUninstantiatedDefaultArg())
106 return E->getSourceRange();
107
108 return SourceRange();
109}
Douglas Gregor78d15832009-05-26 18:54:04 +0000110
Douglas Gregor6cc15182009-09-11 18:44:32 +0000111void VarDecl::setInit(ASTContext &C, Expr *I) {
112 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
113 Eval->~EvaluatedStmt();
114 C.Deallocate(Eval);
Douglas Gregor78d15832009-05-26 18:54:04 +0000115 }
116
Douglas Gregor6cc15182009-09-11 18:44:32 +0000117 Init = I;
118}
119
Douglas Gregor48a83b52009-09-12 00:17:51 +0000120bool VarDecl::isExternC() const {
121 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000122 if (!Context.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000123 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000124 getStorageClass() != Static) ||
125 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
126
Mike Stump1eb44332009-09-09 15:08:12 +0000127 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000128 DC = DC->getParent()) {
129 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
130 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
131 return getStorageClass() != Static;
132
133 break;
134 }
135
136 if (DC->isFunctionOrMethod())
137 return false;
138 }
139
140 return false;
141}
142
Douglas Gregor64650af2009-02-02 23:39:07 +0000143OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000144 ASTContext &C, DeclContext *DC,
145 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000146 QualType T, DeclaratorInfo *DInfo,
147 QualType OT, StorageClass S, Expr *DefArg) {
148 return new (C) OriginalParmVarDecl(DC, L, Id, T, DInfo, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000149}
150
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000151FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000152 SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000153 DeclarationName N, QualType T,
154 DeclaratorInfo *DInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000155 StorageClass S, bool isInline,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000156 bool hasWrittenPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +0000157 FunctionDecl *New
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000158 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssona75e8532009-05-14 21:46:00 +0000159 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor2224f842009-02-25 16:33:18 +0000160 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000161}
162
Steve Naroff090276f2008-10-10 01:28:17 +0000163BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000164 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000165}
166
Douglas Gregor44b43212008-12-11 16:49:14 +0000167FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000168 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000169 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
170 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000171}
172
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000173bool FieldDecl::isAnonymousStructOrUnion() const {
174 if (!isImplicit() || getDeclName())
175 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek6217b802009-07-29 21:53:49 +0000177 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000178 return Record->getDecl()->isAnonymousStructOrUnion();
179
180 return false;
181}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000182
Chris Lattner0ed844b2008-04-04 06:12:32 +0000183EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
184 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000185 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000186 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000187 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000188}
189
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000190void EnumConstantDecl::Destroy(ASTContext& C) {
191 if (Init) Init->Destroy(C);
192 Decl::Destroy(C);
193}
194
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000195TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000196 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000197 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000198 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000199}
200
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000201EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000202 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000203 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000204 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000205 C.getTypeDeclType(Enum, PrevDecl);
206 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000207}
208
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000209void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000210 Decl::Destroy(C);
211}
212
Douglas Gregor44b43212008-12-11 16:49:14 +0000213void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
214 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000215 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000216 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000217}
218
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000219FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000220 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000221 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000222 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000223}
224
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000225//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000226// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000227//===----------------------------------------------------------------------===//
228
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000229std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000230 return getQualifiedNameAsString(getASTContext().getLangOptions());
231}
232
233std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000234 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
235 // std::string thrashing.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000236 std::vector<std::string> Names;
237 std::string QualName;
238 const DeclContext *Ctx = getDeclContext();
239
240 if (Ctx->isFunctionOrMethod())
241 return getNameAsString();
242
243 while (Ctx) {
244 if (Ctx->isFunctionOrMethod())
245 // FIXME: That probably will happen, when D was member of local
246 // scope class/struct/union. How do we handle this case?
247 break;
248
Mike Stump1eb44332009-09-09 15:08:12 +0000249 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000250 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
251 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
252 std::string TemplateArgsStr
253 = TemplateSpecializationType::PrintTemplateArgumentList(
254 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000255 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000256 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000257 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000258 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000259 Names.push_back(ND->getNameAsString());
260 else
261 break;
262
263 Ctx = Ctx->getParent();
264 }
265
266 std::vector<std::string>::reverse_iterator
267 I = Names.rbegin(),
268 End = Names.rend();
269
270 for (; I!=End; ++I)
271 QualName += *I + "::";
272
273 QualName += getNameAsString();
274
275 return QualName;
276}
277
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000278bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000279 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
280
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000281 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
282 // We want to keep it, unless it nominates same namespace.
283 if (getKind() == Decl::UsingDirective) {
284 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
285 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000288 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
289 // For function declarations, we keep track of redeclarations.
290 return FD->getPreviousDeclaration() == OldD;
291
Douglas Gregore53060f2009-06-25 22:08:12 +0000292 // For function templates, the underlying function declarations are linked.
293 if (const FunctionTemplateDecl *FunctionTemplate
294 = dyn_cast<FunctionTemplateDecl>(this))
295 if (const FunctionTemplateDecl *OldFunctionTemplate
296 = dyn_cast<FunctionTemplateDecl>(OldD))
297 return FunctionTemplate->getTemplatedDecl()
298 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Steve Naroff0de21fd2009-02-22 19:35:57 +0000300 // For method declarations, we keep track of redeclarations.
301 if (isa<ObjCMethodDecl>(this))
302 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
John McCallf36e02d2009-10-09 21:13:30 +0000304 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
305 return true;
306
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000307 // For non-function declarations, if the declarations are of the
308 // same kind then this must be a redeclaration, or semantic analysis
309 // would not have given us the new declaration.
310 return this->getKind() == OldD->getKind();
311}
312
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000313bool NamedDecl::hasLinkage() const {
314 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
315 return VD->hasExternalStorage() || VD->isFileVarDecl();
316
317 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
318 return true;
319
320 return false;
321}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000322
Anders Carlssone136e0e2009-06-26 06:29:23 +0000323NamedDecl *NamedDecl::getUnderlyingDecl() {
324 NamedDecl *ND = this;
325 while (true) {
326 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
327 ND = UD->getTargetDecl();
328 else if (ObjCCompatibleAliasDecl *AD
329 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
330 return AD->getClassInterface();
331 else
332 return ND;
333 }
334}
335
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000336//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000337// DeclaratorDecl Implementation
338//===----------------------------------------------------------------------===//
339
340SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000341 if (DeclInfo) {
342 TypeLoc TL = DeclInfo->getTypeLoc();
343 while (true) {
344 TypeLoc NextTL = TL.getNextTypeLoc();
345 if (!NextTL)
346 return TL.getSourceRange().getBegin();
347 TL = NextTL;
348 }
349 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000350 return SourceLocation();
351}
352
353//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000354// VarDecl Implementation
355//===----------------------------------------------------------------------===//
356
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000357VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000358 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000359 StorageClass S) {
360 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000361}
362
363void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000364 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000365 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000366 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000367 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
368 Eval->~EvaluatedStmt();
369 C.Deallocate(Eval);
370 }
371 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000372 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000373 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000374}
375
376VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000377}
378
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000379SourceRange VarDecl::getSourceRange() const {
380 if (getInit())
381 return SourceRange(getLocation(), getInit()->getLocEnd());
382 return SourceRange(getLocation(), getLocation());
383}
384
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000385bool VarDecl::isOutOfLine() const {
386 if (!isStaticDataMember())
387 return false;
388
389 if (Decl::isOutOfLine())
390 return true;
391
392 // If this static data member was instantiated from a static data member of
393 // a class template, check whether that static data member was defined
394 // out-of-line.
395 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
396 return VD->isOutOfLine();
397
398 return false;
399}
400
401VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000402 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000403 return cast<VarDecl>(MSI->getInstantiatedFrom());
404
405 return 0;
406}
407
Douglas Gregor663b5a02009-10-14 20:14:33 +0000408TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000409 if (MemberSpecializationInfo *MSI
410 = getASTContext().getInstantiatedFromStaticDataMember(this))
411 return MSI->getTemplateSpecializationKind();
412
413 return TSK_Undeclared;
414}
415
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000416MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000417 return getASTContext().getInstantiatedFromStaticDataMember(this);
418}
419
Douglas Gregor0a897e32009-10-15 17:21:20 +0000420void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
421 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000422 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000423 assert(MSI && "Not an instantiated static data member?");
424 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000425 if (TSK != TSK_ExplicitSpecialization &&
426 PointOfInstantiation.isValid() &&
427 MSI->getPointOfInstantiation().isInvalid())
428 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000429}
430
Douglas Gregor275a3692009-03-10 23:43:53 +0000431bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
432 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
433 return false;
434
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000435 const VarDecl *Def = 0;
436 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000437 (getStorageClass() == None || getStorageClass() == Static));
438}
439
Ted Kremenek082d9362009-03-20 21:35:28 +0000440const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000441 redecl_iterator I = redecls_begin(), E = redecls_end();
442 while (I != E && !I->getInit())
443 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000444
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000445 if (I != E) {
446 Def = *I;
447 return I->getInit();
448 }
449 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000450}
451
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000452VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000453 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000454}
455
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000456//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000457// FunctionDecl Implementation
458//===----------------------------------------------------------------------===//
459
Ted Kremenek27f8a282008-05-20 00:43:19 +0000460void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000461 if (Body && Body.isOffset())
462 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000463
464 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
465 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000466
Douglas Gregor2db32322009-10-07 23:56:10 +0000467 FunctionTemplateSpecializationInfo *FTSInfo
468 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
469 if (FTSInfo)
470 C.Deallocate(FTSInfo);
471
472 MemberSpecializationInfo *MSInfo
473 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
474 if (MSInfo)
475 C.Deallocate(MSInfo);
476
Steve Naroff3e970492009-01-27 21:25:57 +0000477 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000478
Ted Kremenek27f8a282008-05-20 00:43:19 +0000479 Decl::Destroy(C);
480}
481
John McCall136a6982009-09-11 06:45:03 +0000482void FunctionDecl::getNameForDiagnostic(std::string &S,
483 const PrintingPolicy &Policy,
484 bool Qualified) const {
485 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
486 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
487 if (TemplateArgs)
488 S += TemplateSpecializationType::PrintTemplateArgumentList(
489 TemplateArgs->getFlatArgumentList(),
490 TemplateArgs->flat_size(),
491 Policy);
492
493}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000494
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000495Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000496 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
497 if (I->Body) {
498 Definition = *I;
499 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000500 }
501 }
502
503 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000504}
505
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000506void FunctionDecl::setBody(Stmt *B) {
507 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000508 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000509 EndRangeLoc = B->getLocEnd();
510}
511
Douglas Gregor48a83b52009-09-12 00:17:51 +0000512bool FunctionDecl::isMain() const {
513 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000514 return !Context.getLangOptions().Freestanding &&
515 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000516 getIdentifier() && getIdentifier()->isStr("main");
517}
518
Douglas Gregor48a83b52009-09-12 00:17:51 +0000519bool FunctionDecl::isExternC() const {
520 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000521 // In C, any non-static, non-overloadable function has external
522 // linkage.
523 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000524 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000525
Mike Stump1eb44332009-09-09 15:08:12 +0000526 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000527 DC = DC->getParent()) {
528 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
529 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000530 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000531 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000532
533 break;
534 }
535 }
536
537 return false;
538}
539
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000540bool FunctionDecl::isGlobal() const {
541 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
542 return Method->isStatic();
543
544 if (getStorageClass() == Static)
545 return false;
546
Mike Stump1eb44332009-09-09 15:08:12 +0000547 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000548 DC->isNamespace();
549 DC = DC->getParent()) {
550 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
551 if (!Namespace->getDeclName())
552 return false;
553 break;
554 }
555 }
556
557 return true;
558}
559
Douglas Gregor3e41d602009-02-13 23:20:09 +0000560/// \brief Returns a value indicating whether this function
561/// corresponds to a builtin function.
562///
563/// The function corresponds to a built-in function if it is
564/// declared at translation scope or within an extern "C" block and
565/// its name matches with the name of a builtin. The returned value
566/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000567/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000568/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000569unsigned FunctionDecl::getBuiltinID() const {
570 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000571 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
572 return 0;
573
574 unsigned BuiltinID = getIdentifier()->getBuiltinID();
575 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
576 return BuiltinID;
577
578 // This function has the name of a known C library
579 // function. Determine whether it actually refers to the C library
580 // function or whether it just has the same name.
581
Douglas Gregor9add3172009-02-17 03:23:10 +0000582 // If this is a static function, it's not a builtin.
583 if (getStorageClass() == Static)
584 return 0;
585
Douglas Gregor3c385e52009-02-14 18:57:46 +0000586 // If this function is at translation-unit scope and we're not in
587 // C++, it refers to the C library function.
588 if (!Context.getLangOptions().CPlusPlus &&
589 getDeclContext()->isTranslationUnit())
590 return BuiltinID;
591
592 // If the function is in an extern "C" linkage specification and is
593 // not marked "overloadable", it's the real function.
594 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000595 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000596 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000597 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000598 return BuiltinID;
599
600 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000601 return 0;
602}
603
604
Chris Lattner1ad9b282009-04-25 06:03:53 +0000605/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000606/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000607/// after it has been created.
608unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000609 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000610 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000611 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000612 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614}
615
Ted Kremenekfc767612009-01-14 00:42:25 +0000616void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
617 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000619 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 // Zero params -> null pointer.
622 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000623 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000624 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000626
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000627 // Update source range. The check below allows us to set EndRangeLoc before
628 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000629 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000630 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 }
632}
633
Chris Lattner8123a952008-04-10 02:22:51 +0000634/// getMinRequiredArguments - Returns the minimum number of arguments
635/// needed to call this function. This may be fewer than the number of
636/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000637/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000638unsigned FunctionDecl::getMinRequiredArguments() const {
639 unsigned NumRequiredArgs = getNumParams();
640 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000641 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000642 --NumRequiredArgs;
643
644 return NumRequiredArgs;
645}
646
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000647/// \brief For an inline function definition in C, determine whether the
648/// definition will be externally visible.
649///
650/// Inline function definitions are always available for inlining optimizations.
651/// However, depending on the language dialect, declaration specifiers, and
652/// attributes, the definition of an inline function may or may not be
653/// "externally" visible to other translation units in the program.
654///
655/// In C99, inline definitions are not externally visible by default. However,
656/// if even one of the globa-scope declarations is marked "extern inline", the
657/// inline definition becomes externally visible (C99 6.7.4p6).
658///
659/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
660/// definition, we use the GNU semantics for inline, which are nearly the
661/// opposite of C99 semantics. In particular, "inline" by itself will create
662/// an externally visible symbol, but "extern inline" will not create an
663/// externally visible symbol.
664bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
665 assert(isThisDeclarationADefinition() && "Must have the function definition");
666 assert(isInline() && "Function must be inline");
667
668 if (!getASTContext().getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
669 // GNU inline semantics. Based on a number of examples, we came up with the
670 // following heuristic: if the "inline" keyword is present on a
671 // declaration of the function but "extern" is not present on that
672 // declaration, then the symbol is externally visible. Otherwise, the GNU
673 // "extern inline" semantics applies and the symbol is not externally
674 // visible.
675 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
676 Redecl != RedeclEnd;
677 ++Redecl) {
678 if (Redecl->isInline() && Redecl->getStorageClass() != Extern)
679 return true;
680 }
681
682 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000683 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000684 }
685
686 // C99 6.7.4p6:
687 // [...] If all of the file scope declarations for a function in a
688 // translation unit include the inline function specifier without extern,
689 // then the definition in that translation unit is an inline definition.
690 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
691 Redecl != RedeclEnd;
692 ++Redecl) {
693 // Only consider file-scope declarations in this test.
694 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
695 continue;
696
697 if (!Redecl->isInline() || Redecl->getStorageClass() == Extern)
698 return true; // Not an inline definition
699 }
700
701 // C99 6.7.4p6:
702 // An inline definition does not provide an external definition for the
703 // function, and does not forbid an external definition in another
704 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000705 return false;
706}
707
Mike Stump1eb44332009-09-09 15:08:12 +0000708void
Douglas Gregor127102b2009-06-29 20:59:39 +0000709FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000710 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000711
Douglas Gregor127102b2009-06-29 20:59:39 +0000712 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000713 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000714 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
715 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
716 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
717 }
718}
719
Mike Stump740256b2009-09-29 00:50:50 +0000720const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
721 return getFirstDeclaration();
722}
723
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000724FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000725 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000726}
727
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000728/// getOverloadedOperator - Which C++ overloaded operator this
729/// function represents, if any.
730OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000731 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
732 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000733 else
734 return OO_None;
735}
736
Douglas Gregor2db32322009-10-07 23:56:10 +0000737FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000738 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +0000739 return cast<FunctionDecl>(Info->getInstantiatedFrom());
740
741 return 0;
742}
743
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000744MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
745 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
746}
747
Douglas Gregor2db32322009-10-07 23:56:10 +0000748void
749FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
750 TemplateSpecializationKind TSK) {
751 assert(TemplateOrSpecialization.isNull() &&
752 "Member function is already a specialization");
753 MemberSpecializationInfo *Info
754 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
755 TemplateOrSpecialization = Info;
756}
757
Douglas Gregor16e8be22009-06-29 17:30:29 +0000758FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000759 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000760 = TemplateOrSpecialization
761 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000762 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000763 }
764 return 0;
765}
766
767const TemplateArgumentList *
768FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000769 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000770 = TemplateOrSpecialization
771 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +0000772 return Info->TemplateArguments;
773 }
774 return 0;
775}
776
Mike Stump1eb44332009-09-09 15:08:12 +0000777void
Douglas Gregor1637be72009-06-26 00:10:03 +0000778FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
779 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000780 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000781 void *InsertPos,
782 TemplateSpecializationKind TSK) {
783 assert(TSK != TSK_Undeclared &&
784 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +0000785 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000786 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000787 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000788 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Douglas Gregor127102b2009-06-29 20:59:39 +0000790 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000791 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000792 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +0000793 Info->TemplateArguments = TemplateArgs;
794 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Douglas Gregor127102b2009-06-29 20:59:39 +0000796 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000797 // function template specializations.
798 if (InsertPos)
799 Template->getSpecializations().InsertNode(Info, InsertPos);
800 else {
801 // Try to insert the new node. If there is an existing node, remove it
802 // first.
803 FunctionTemplateSpecializationInfo *Existing
804 = Template->getSpecializations().GetOrInsertNode(Info);
805 if (Existing) {
806 Template->getSpecializations().RemoveNode(Existing);
807 Template->getSpecializations().GetOrInsertNode(Info);
808 }
809 }
Douglas Gregor1637be72009-06-26 00:10:03 +0000810}
811
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000812TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000813 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000814 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +0000815 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000816 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +0000817 if (FTSInfo)
818 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregor2db32322009-10-07 23:56:10 +0000820 MemberSpecializationInfo *MSInfo
821 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
822 if (MSInfo)
823 return MSInfo->getTemplateSpecializationKind();
824
825 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000826}
827
Mike Stump1eb44332009-09-09 15:08:12 +0000828void
Douglas Gregor0a897e32009-10-15 17:21:20 +0000829FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
830 SourceLocation PointOfInstantiation) {
831 if (FunctionTemplateSpecializationInfo *FTSInfo
832 = TemplateOrSpecialization.dyn_cast<
833 FunctionTemplateSpecializationInfo*>()) {
834 FTSInfo->setTemplateSpecializationKind(TSK);
835 if (TSK != TSK_ExplicitSpecialization &&
836 PointOfInstantiation.isValid() &&
837 FTSInfo->getPointOfInstantiation().isInvalid())
838 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
839 } else if (MemberSpecializationInfo *MSInfo
840 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
841 MSInfo->setTemplateSpecializationKind(TSK);
842 if (TSK != TSK_ExplicitSpecialization &&
843 PointOfInstantiation.isValid() &&
844 MSInfo->getPointOfInstantiation().isInvalid())
845 MSInfo->setPointOfInstantiation(PointOfInstantiation);
846 } else
847 assert(false && "Function cannot have a template specialization kind");
848}
849
850SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +0000851 if (FunctionTemplateSpecializationInfo *FTSInfo
852 = TemplateOrSpecialization.dyn_cast<
853 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000854 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +0000855 else if (MemberSpecializationInfo *MSInfo
856 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000857 return MSInfo->getPointOfInstantiation();
858
859 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000860}
861
Douglas Gregor9f185072009-09-11 20:15:17 +0000862bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +0000863 if (Decl::isOutOfLine())
864 return true;
865
866 // If this function was instantiated from a member function of a
867 // class template, check whether that member function was defined out-of-line.
868 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
869 const FunctionDecl *Definition;
870 if (FD->getBody(Definition))
871 return Definition->isOutOfLine();
872 }
873
874 // If this function was instantiated from a function template,
875 // check whether that function template was defined out-of-line.
876 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
877 const FunctionDecl *Definition;
878 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
879 return Definition->isOutOfLine();
880 }
881
882 return false;
883}
884
Chris Lattner8a934232008-03-31 00:36:02 +0000885//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000886// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000887//===----------------------------------------------------------------------===//
888
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000889SourceRange TagDecl::getSourceRange() const {
890 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000891 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000892}
893
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000894TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000895 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000896}
897
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000898void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000899 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
900 TagT->decl.setPointer(this);
901 TagT->decl.setInt(1);
902 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000903}
904
905void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000906 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000907 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
908 assert(TagT->decl.getPointer() == this &&
909 "Attempt to redefine a tag definition?");
910 TagT->decl.setInt(0);
911 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000912}
913
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000914TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000915 if (isDefinition())
916 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000917
918 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000919 R != REnd; ++R)
920 if (R->isDefinition())
921 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000923 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000924}
925
John McCallf1bbbb42009-09-04 01:14:41 +0000926TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
927 switch (TypeSpec) {
928 default: llvm::llvm_unreachable("unexpected type specifier");
929 case DeclSpec::TST_struct: return TK_struct;
930 case DeclSpec::TST_class: return TK_class;
931 case DeclSpec::TST_union: return TK_union;
932 case DeclSpec::TST_enum: return TK_enum;
933 }
934}
935
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000936//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000937// RecordDecl Implementation
938//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000939
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000940RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000941 IdentifierInfo *Id, RecordDecl *PrevDecl,
942 SourceLocation TKL)
943 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +0000944 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000945 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000946 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000947 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000948}
949
950RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000951 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000952 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000954 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000955 C.getTypeDeclType(R, PrevDecl);
956 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000957}
958
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000959RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000960}
961
962void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000963 TagDecl::Destroy(C);
964}
965
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000966bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000967 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000968 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
969}
970
Douglas Gregor44b43212008-12-11 16:49:14 +0000971/// completeDefinition - Notes that the definition of this type is now
972/// complete.
973void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000975 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000976}
977
Steve Naroff56ee6892008-10-08 17:01:13 +0000978//===----------------------------------------------------------------------===//
979// BlockDecl Implementation
980//===----------------------------------------------------------------------===//
981
982BlockDecl::~BlockDecl() {
983}
984
985void BlockDecl::Destroy(ASTContext& C) {
986 if (Body)
987 Body->Destroy(C);
988
989 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
990 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
992 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000993 Decl::Destroy(C);
994}
Steve Naroffe78b8092009-03-13 16:56:44 +0000995
996void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
997 unsigned NParms) {
998 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Steve Naroffe78b8092009-03-13 16:56:44 +00001000 // Zero params -> null pointer.
1001 if (NParms) {
1002 NumParams = NParms;
1003 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1004 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1005 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1006 }
1007}
1008
1009unsigned BlockDecl::getNumParams() const {
1010 return NumParams;
1011}