blob: da7959b16f9ffc3a1ca18268a93541910b4ef5f5 [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 {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000234 std::vector<std::string> Names;
235 std::string QualName;
236 const DeclContext *Ctx = getDeclContext();
237
238 if (Ctx->isFunctionOrMethod())
239 return getNameAsString();
240
241 while (Ctx) {
242 if (Ctx->isFunctionOrMethod())
243 // FIXME: That probably will happen, when D was member of local
244 // scope class/struct/union. How do we handle this case?
245 break;
246
Mike Stump1eb44332009-09-09 15:08:12 +0000247 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000248 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
249 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
250 std::string TemplateArgsStr
251 = TemplateSpecializationType::PrintTemplateArgumentList(
252 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000253 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000254 P);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000255 Names.push_back(Spec->getIdentifier()->getName() + TemplateArgsStr);
256 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000257 Names.push_back(ND->getNameAsString());
258 else
259 break;
260
261 Ctx = Ctx->getParent();
262 }
263
264 std::vector<std::string>::reverse_iterator
265 I = Names.rbegin(),
266 End = Names.rend();
267
268 for (; I!=End; ++I)
269 QualName += *I + "::";
270
271 QualName += getNameAsString();
272
273 return QualName;
274}
275
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000276bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000277 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
278
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000279 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
280 // We want to keep it, unless it nominates same namespace.
281 if (getKind() == Decl::UsingDirective) {
282 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
283 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000286 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
287 // For function declarations, we keep track of redeclarations.
288 return FD->getPreviousDeclaration() == OldD;
289
Douglas Gregore53060f2009-06-25 22:08:12 +0000290 // For function templates, the underlying function declarations are linked.
291 if (const FunctionTemplateDecl *FunctionTemplate
292 = dyn_cast<FunctionTemplateDecl>(this))
293 if (const FunctionTemplateDecl *OldFunctionTemplate
294 = dyn_cast<FunctionTemplateDecl>(OldD))
295 return FunctionTemplate->getTemplatedDecl()
296 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Steve Naroff0de21fd2009-02-22 19:35:57 +0000298 // For method declarations, we keep track of redeclarations.
299 if (isa<ObjCMethodDecl>(this))
300 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
John McCallf36e02d2009-10-09 21:13:30 +0000302 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
303 return true;
304
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000305 // For non-function declarations, if the declarations are of the
306 // same kind then this must be a redeclaration, or semantic analysis
307 // would not have given us the new declaration.
308 return this->getKind() == OldD->getKind();
309}
310
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000311bool NamedDecl::hasLinkage() const {
312 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
313 return VD->hasExternalStorage() || VD->isFileVarDecl();
314
315 if (isa<FunctionDecl>(this) && !isa<CXXMethodDecl>(this))
316 return true;
317
318 return false;
319}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000320
Anders Carlssone136e0e2009-06-26 06:29:23 +0000321NamedDecl *NamedDecl::getUnderlyingDecl() {
322 NamedDecl *ND = this;
323 while (true) {
324 if (UsingDecl *UD = dyn_cast<UsingDecl>(ND))
325 ND = UD->getTargetDecl();
326 else if (ObjCCompatibleAliasDecl *AD
327 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
328 return AD->getClassInterface();
329 else
330 return ND;
331 }
332}
333
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000334//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000335// DeclaratorDecl Implementation
336//===----------------------------------------------------------------------===//
337
338SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
339 if (DeclInfo)
340 return DeclInfo->getTypeLoc().getTypeSpecRange().getBegin();
341 return SourceLocation();
342}
343
344//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000345// VarDecl Implementation
346//===----------------------------------------------------------------------===//
347
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000348VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000349 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000350 StorageClass S) {
351 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000352}
353
354void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000355 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000356 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000357 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000358 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
359 Eval->~EvaluatedStmt();
360 C.Deallocate(Eval);
361 }
362 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000363 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000364 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000365}
366
367VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000368}
369
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000370SourceRange VarDecl::getSourceRange() const {
371 if (getInit())
372 return SourceRange(getLocation(), getInit()->getLocEnd());
373 return SourceRange(getLocation(), getLocation());
374}
375
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000376bool VarDecl::isOutOfLine() const {
377 if (!isStaticDataMember())
378 return false;
379
380 if (Decl::isOutOfLine())
381 return true;
382
383 // If this static data member was instantiated from a static data member of
384 // a class template, check whether that static data member was defined
385 // out-of-line.
386 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
387 return VD->isOutOfLine();
388
389 return false;
390}
391
392VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000393 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000394 return cast<VarDecl>(MSI->getInstantiatedFrom());
395
396 return 0;
397}
398
Douglas Gregor663b5a02009-10-14 20:14:33 +0000399TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000400 if (MemberSpecializationInfo *MSI
401 = getASTContext().getInstantiatedFromStaticDataMember(this))
402 return MSI->getTemplateSpecializationKind();
403
404 return TSK_Undeclared;
405}
406
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000407MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000408 return getASTContext().getInstantiatedFromStaticDataMember(this);
409}
410
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000411void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000412 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000413 assert(MSI && "Not an instantiated static data member?");
414 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000415}
416
Douglas Gregor275a3692009-03-10 23:43:53 +0000417bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
418 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
419 return false;
420
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000421 const VarDecl *Def = 0;
422 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000423 (getStorageClass() == None || getStorageClass() == Static));
424}
425
Ted Kremenek082d9362009-03-20 21:35:28 +0000426const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000427 redecl_iterator I = redecls_begin(), E = redecls_end();
428 while (I != E && !I->getInit())
429 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000430
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000431 if (I != E) {
432 Def = *I;
433 return I->getInit();
434 }
435 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000436}
437
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000438VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000439 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000440}
441
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000442//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000443// FunctionDecl Implementation
444//===----------------------------------------------------------------------===//
445
Ted Kremenek27f8a282008-05-20 00:43:19 +0000446void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000447 if (Body && Body.isOffset())
448 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000449
450 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
451 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000452
Douglas Gregor2db32322009-10-07 23:56:10 +0000453 FunctionTemplateSpecializationInfo *FTSInfo
454 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
455 if (FTSInfo)
456 C.Deallocate(FTSInfo);
457
458 MemberSpecializationInfo *MSInfo
459 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
460 if (MSInfo)
461 C.Deallocate(MSInfo);
462
Steve Naroff3e970492009-01-27 21:25:57 +0000463 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000464
Ted Kremenek27f8a282008-05-20 00:43:19 +0000465 Decl::Destroy(C);
466}
467
John McCall136a6982009-09-11 06:45:03 +0000468void FunctionDecl::getNameForDiagnostic(std::string &S,
469 const PrintingPolicy &Policy,
470 bool Qualified) const {
471 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
472 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
473 if (TemplateArgs)
474 S += TemplateSpecializationType::PrintTemplateArgumentList(
475 TemplateArgs->getFlatArgumentList(),
476 TemplateArgs->flat_size(),
477 Policy);
478
479}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000480
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000481Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000482 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
483 if (I->Body) {
484 Definition = *I;
485 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000486 }
487 }
488
489 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000490}
491
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000492void FunctionDecl::setBody(Stmt *B) {
493 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000494 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000495 EndRangeLoc = B->getLocEnd();
496}
497
Douglas Gregor48a83b52009-09-12 00:17:51 +0000498bool FunctionDecl::isMain() const {
499 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000500 return !Context.getLangOptions().Freestanding &&
501 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000502 getIdentifier() && getIdentifier()->isStr("main");
503}
504
Douglas Gregor48a83b52009-09-12 00:17:51 +0000505bool FunctionDecl::isExternC() const {
506 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000507 // In C, any non-static, non-overloadable function has external
508 // linkage.
509 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000510 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000511
Mike Stump1eb44332009-09-09 15:08:12 +0000512 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000513 DC = DC->getParent()) {
514 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
515 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000516 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000517 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000518
519 break;
520 }
521 }
522
523 return false;
524}
525
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000526bool FunctionDecl::isGlobal() const {
527 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
528 return Method->isStatic();
529
530 if (getStorageClass() == Static)
531 return false;
532
Mike Stump1eb44332009-09-09 15:08:12 +0000533 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000534 DC->isNamespace();
535 DC = DC->getParent()) {
536 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
537 if (!Namespace->getDeclName())
538 return false;
539 break;
540 }
541 }
542
543 return true;
544}
545
Douglas Gregor3e41d602009-02-13 23:20:09 +0000546/// \brief Returns a value indicating whether this function
547/// corresponds to a builtin function.
548///
549/// The function corresponds to a built-in function if it is
550/// declared at translation scope or within an extern "C" block and
551/// its name matches with the name of a builtin. The returned value
552/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000553/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000554/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000555unsigned FunctionDecl::getBuiltinID() const {
556 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000557 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
558 return 0;
559
560 unsigned BuiltinID = getIdentifier()->getBuiltinID();
561 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
562 return BuiltinID;
563
564 // This function has the name of a known C library
565 // function. Determine whether it actually refers to the C library
566 // function or whether it just has the same name.
567
Douglas Gregor9add3172009-02-17 03:23:10 +0000568 // If this is a static function, it's not a builtin.
569 if (getStorageClass() == Static)
570 return 0;
571
Douglas Gregor3c385e52009-02-14 18:57:46 +0000572 // If this function is at translation-unit scope and we're not in
573 // C++, it refers to the C library function.
574 if (!Context.getLangOptions().CPlusPlus &&
575 getDeclContext()->isTranslationUnit())
576 return BuiltinID;
577
578 // If the function is in an extern "C" linkage specification and is
579 // not marked "overloadable", it's the real function.
580 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000581 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000582 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000583 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000584 return BuiltinID;
585
586 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000587 return 0;
588}
589
590
Chris Lattner1ad9b282009-04-25 06:03:53 +0000591/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000592/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000593/// after it has been created.
594unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000595 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000596 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000597 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000598 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Reid Spencer5f016e22007-07-11 17:01:13 +0000600}
601
Ted Kremenekfc767612009-01-14 00:42:25 +0000602void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
603 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000605 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 // Zero params -> null pointer.
608 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000609 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000610 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000612
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000613 // Update source range. The check below allows us to set EndRangeLoc before
614 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000615 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000616 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 }
618}
619
Chris Lattner8123a952008-04-10 02:22:51 +0000620/// getMinRequiredArguments - Returns the minimum number of arguments
621/// needed to call this function. This may be fewer than the number of
622/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000623/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000624unsigned FunctionDecl::getMinRequiredArguments() const {
625 unsigned NumRequiredArgs = getNumParams();
626 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000627 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000628 --NumRequiredArgs;
629
630 return NumRequiredArgs;
631}
632
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000633/// \brief For an inline function definition in C, determine whether the
634/// definition will be externally visible.
635///
636/// Inline function definitions are always available for inlining optimizations.
637/// However, depending on the language dialect, declaration specifiers, and
638/// attributes, the definition of an inline function may or may not be
639/// "externally" visible to other translation units in the program.
640///
641/// In C99, inline definitions are not externally visible by default. However,
642/// if even one of the globa-scope declarations is marked "extern inline", the
643/// inline definition becomes externally visible (C99 6.7.4p6).
644///
645/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
646/// definition, we use the GNU semantics for inline, which are nearly the
647/// opposite of C99 semantics. In particular, "inline" by itself will create
648/// an externally visible symbol, but "extern inline" will not create an
649/// externally visible symbol.
650bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
651 assert(isThisDeclarationADefinition() && "Must have the function definition");
652 assert(isInline() && "Function must be inline");
653
654 if (!getASTContext().getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
655 // GNU inline semantics. Based on a number of examples, we came up with the
656 // following heuristic: if the "inline" keyword is present on a
657 // declaration of the function but "extern" is not present on that
658 // declaration, then the symbol is externally visible. Otherwise, the GNU
659 // "extern inline" semantics applies and the symbol is not externally
660 // visible.
661 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
662 Redecl != RedeclEnd;
663 ++Redecl) {
664 if (Redecl->isInline() && Redecl->getStorageClass() != Extern)
665 return true;
666 }
667
668 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000669 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000670 }
671
672 // C99 6.7.4p6:
673 // [...] If all of the file scope declarations for a function in a
674 // translation unit include the inline function specifier without extern,
675 // then the definition in that translation unit is an inline definition.
676 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
677 Redecl != RedeclEnd;
678 ++Redecl) {
679 // Only consider file-scope declarations in this test.
680 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
681 continue;
682
683 if (!Redecl->isInline() || Redecl->getStorageClass() == Extern)
684 return true; // Not an inline definition
685 }
686
687 // C99 6.7.4p6:
688 // An inline definition does not provide an external definition for the
689 // function, and does not forbid an external definition in another
690 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000691 return false;
692}
693
Mike Stump1eb44332009-09-09 15:08:12 +0000694void
Douglas Gregor127102b2009-06-29 20:59:39 +0000695FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000696 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000697
Douglas Gregor127102b2009-06-29 20:59:39 +0000698 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000699 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000700 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
701 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
702 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
703 }
704}
705
Mike Stump740256b2009-09-29 00:50:50 +0000706const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
707 return getFirstDeclaration();
708}
709
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000710FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000711 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000712}
713
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000714/// getOverloadedOperator - Which C++ overloaded operator this
715/// function represents, if any.
716OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000717 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
718 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000719 else
720 return OO_None;
721}
722
Douglas Gregor2db32322009-10-07 23:56:10 +0000723FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000724 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +0000725 return cast<FunctionDecl>(Info->getInstantiatedFrom());
726
727 return 0;
728}
729
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000730MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
731 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
732}
733
Douglas Gregor2db32322009-10-07 23:56:10 +0000734void
735FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
736 TemplateSpecializationKind TSK) {
737 assert(TemplateOrSpecialization.isNull() &&
738 "Member function is already a specialization");
739 MemberSpecializationInfo *Info
740 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
741 TemplateOrSpecialization = Info;
742}
743
Douglas Gregor16e8be22009-06-29 17:30:29 +0000744FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000745 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000746 = TemplateOrSpecialization
747 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000748 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000749 }
750 return 0;
751}
752
753const TemplateArgumentList *
754FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000755 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000756 = TemplateOrSpecialization
757 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +0000758 return Info->TemplateArguments;
759 }
760 return 0;
761}
762
Mike Stump1eb44332009-09-09 15:08:12 +0000763void
Douglas Gregor1637be72009-06-26 00:10:03 +0000764FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
765 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000766 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000767 void *InsertPos,
768 TemplateSpecializationKind TSK) {
769 assert(TSK != TSK_Undeclared &&
770 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +0000771 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000772 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000773 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000774 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregor127102b2009-06-29 20:59:39 +0000776 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000777 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000778 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +0000779 Info->TemplateArguments = TemplateArgs;
780 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Douglas Gregor127102b2009-06-29 20:59:39 +0000782 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000783 // function template specializations.
784 if (InsertPos)
785 Template->getSpecializations().InsertNode(Info, InsertPos);
786 else {
787 // Try to insert the new node. If there is an existing node, remove it
788 // first.
789 FunctionTemplateSpecializationInfo *Existing
790 = Template->getSpecializations().GetOrInsertNode(Info);
791 if (Existing) {
792 Template->getSpecializations().RemoveNode(Existing);
793 Template->getSpecializations().GetOrInsertNode(Info);
794 }
795 }
Douglas Gregor1637be72009-06-26 00:10:03 +0000796}
797
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000798TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000799 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000800 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +0000801 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000802 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +0000803 if (FTSInfo)
804 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Douglas Gregor2db32322009-10-07 23:56:10 +0000806 MemberSpecializationInfo *MSInfo
807 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
808 if (MSInfo)
809 return MSInfo->getTemplateSpecializationKind();
810
811 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000812}
813
Mike Stump1eb44332009-09-09 15:08:12 +0000814void
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000815FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Douglas Gregor2db32322009-10-07 23:56:10 +0000816 if (FunctionTemplateSpecializationInfo *FTSInfo
817 = TemplateOrSpecialization.dyn_cast<
818 FunctionTemplateSpecializationInfo*>())
819 FTSInfo->setTemplateSpecializationKind(TSK);
820 else if (MemberSpecializationInfo *MSInfo
821 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
822 MSInfo->setTemplateSpecializationKind(TSK);
823 else
824 assert(false && "Function cannot have a template specialization kind");
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000825}
826
Douglas Gregor9f185072009-09-11 20:15:17 +0000827bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +0000828 if (Decl::isOutOfLine())
829 return true;
830
831 // If this function was instantiated from a member function of a
832 // class template, check whether that member function was defined out-of-line.
833 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
834 const FunctionDecl *Definition;
835 if (FD->getBody(Definition))
836 return Definition->isOutOfLine();
837 }
838
839 // If this function was instantiated from a function template,
840 // check whether that function template was defined out-of-line.
841 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
842 const FunctionDecl *Definition;
843 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
844 return Definition->isOutOfLine();
845 }
846
847 return false;
848}
849
Chris Lattner8a934232008-03-31 00:36:02 +0000850//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000851// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000852//===----------------------------------------------------------------------===//
853
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000854SourceRange TagDecl::getSourceRange() const {
855 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000856 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000857}
858
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000859TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000860 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000861}
862
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000863void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000864 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
865 TagT->decl.setPointer(this);
866 TagT->decl.setInt(1);
867 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000868}
869
870void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000871 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000872 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
873 assert(TagT->decl.getPointer() == this &&
874 "Attempt to redefine a tag definition?");
875 TagT->decl.setInt(0);
876 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000877}
878
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000879TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000880 if (isDefinition())
881 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000882
883 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000884 R != REnd; ++R)
885 if (R->isDefinition())
886 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000888 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000889}
890
John McCallf1bbbb42009-09-04 01:14:41 +0000891TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
892 switch (TypeSpec) {
893 default: llvm::llvm_unreachable("unexpected type specifier");
894 case DeclSpec::TST_struct: return TK_struct;
895 case DeclSpec::TST_class: return TK_class;
896 case DeclSpec::TST_union: return TK_union;
897 case DeclSpec::TST_enum: return TK_enum;
898 }
899}
900
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000901//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000902// RecordDecl Implementation
903//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000904
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000905RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000906 IdentifierInfo *Id, RecordDecl *PrevDecl,
907 SourceLocation TKL)
908 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +0000909 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000910 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000911 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000912 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000913}
914
915RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000916 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000917 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000919 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000920 C.getTypeDeclType(R, PrevDecl);
921 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000922}
923
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000924RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000925}
926
927void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000928 TagDecl::Destroy(C);
929}
930
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000931bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000932 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000933 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
934}
935
Douglas Gregor44b43212008-12-11 16:49:14 +0000936/// completeDefinition - Notes that the definition of this type is now
937/// complete.
938void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000940 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000941}
942
Steve Naroff56ee6892008-10-08 17:01:13 +0000943//===----------------------------------------------------------------------===//
944// BlockDecl Implementation
945//===----------------------------------------------------------------------===//
946
947BlockDecl::~BlockDecl() {
948}
949
950void BlockDecl::Destroy(ASTContext& C) {
951 if (Body)
952 Body->Destroy(C);
953
954 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
955 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
957 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000958 Decl::Destroy(C);
959}
Steve Naroffe78b8092009-03-13 16:56:44 +0000960
961void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
962 unsigned NParms) {
963 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Steve Naroffe78b8092009-03-13 16:56:44 +0000965 // Zero params -> null pointer.
966 if (NParms) {
967 NumParams = NParms;
968 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
969 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
970 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
971 }
972}
973
974unsigned BlockDecl::getNumParams() const {
975 return NumParams;
976}