blob: 9d7d3d087cc269da8161163d37c907b6e3767537 [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 Gregor0a897e32009-10-15 17:21:20 +0000411void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
412 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000413 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000414 assert(MSI && "Not an instantiated static data member?");
415 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000416 if (TSK != TSK_ExplicitSpecialization &&
417 PointOfInstantiation.isValid() &&
418 MSI->getPointOfInstantiation().isInvalid())
419 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000420}
421
Douglas Gregor275a3692009-03-10 23:43:53 +0000422bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
423 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
424 return false;
425
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000426 const VarDecl *Def = 0;
427 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000428 (getStorageClass() == None || getStorageClass() == Static));
429}
430
Ted Kremenek082d9362009-03-20 21:35:28 +0000431const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000432 redecl_iterator I = redecls_begin(), E = redecls_end();
433 while (I != E && !I->getInit())
434 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000435
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000436 if (I != E) {
437 Def = *I;
438 return I->getInit();
439 }
440 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000441}
442
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000443VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000444 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000445}
446
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000447//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000448// FunctionDecl Implementation
449//===----------------------------------------------------------------------===//
450
Ted Kremenek27f8a282008-05-20 00:43:19 +0000451void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000452 if (Body && Body.isOffset())
453 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000454
455 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
456 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000457
Douglas Gregor2db32322009-10-07 23:56:10 +0000458 FunctionTemplateSpecializationInfo *FTSInfo
459 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
460 if (FTSInfo)
461 C.Deallocate(FTSInfo);
462
463 MemberSpecializationInfo *MSInfo
464 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
465 if (MSInfo)
466 C.Deallocate(MSInfo);
467
Steve Naroff3e970492009-01-27 21:25:57 +0000468 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000469
Ted Kremenek27f8a282008-05-20 00:43:19 +0000470 Decl::Destroy(C);
471}
472
John McCall136a6982009-09-11 06:45:03 +0000473void FunctionDecl::getNameForDiagnostic(std::string &S,
474 const PrintingPolicy &Policy,
475 bool Qualified) const {
476 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
477 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
478 if (TemplateArgs)
479 S += TemplateSpecializationType::PrintTemplateArgumentList(
480 TemplateArgs->getFlatArgumentList(),
481 TemplateArgs->flat_size(),
482 Policy);
483
484}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000485
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000486Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000487 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
488 if (I->Body) {
489 Definition = *I;
490 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000491 }
492 }
493
494 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000495}
496
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000497void FunctionDecl::setBody(Stmt *B) {
498 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000499 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000500 EndRangeLoc = B->getLocEnd();
501}
502
Douglas Gregor48a83b52009-09-12 00:17:51 +0000503bool FunctionDecl::isMain() const {
504 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000505 return !Context.getLangOptions().Freestanding &&
506 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000507 getIdentifier() && getIdentifier()->isStr("main");
508}
509
Douglas Gregor48a83b52009-09-12 00:17:51 +0000510bool FunctionDecl::isExternC() const {
511 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000512 // In C, any non-static, non-overloadable function has external
513 // linkage.
514 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000515 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000516
Mike Stump1eb44332009-09-09 15:08:12 +0000517 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000518 DC = DC->getParent()) {
519 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
520 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000521 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000522 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000523
524 break;
525 }
526 }
527
528 return false;
529}
530
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000531bool FunctionDecl::isGlobal() const {
532 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
533 return Method->isStatic();
534
535 if (getStorageClass() == Static)
536 return false;
537
Mike Stump1eb44332009-09-09 15:08:12 +0000538 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000539 DC->isNamespace();
540 DC = DC->getParent()) {
541 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
542 if (!Namespace->getDeclName())
543 return false;
544 break;
545 }
546 }
547
548 return true;
549}
550
Douglas Gregor3e41d602009-02-13 23:20:09 +0000551/// \brief Returns a value indicating whether this function
552/// corresponds to a builtin function.
553///
554/// The function corresponds to a built-in function if it is
555/// declared at translation scope or within an extern "C" block and
556/// its name matches with the name of a builtin. The returned value
557/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000558/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000559/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000560unsigned FunctionDecl::getBuiltinID() const {
561 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000562 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
563 return 0;
564
565 unsigned BuiltinID = getIdentifier()->getBuiltinID();
566 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
567 return BuiltinID;
568
569 // This function has the name of a known C library
570 // function. Determine whether it actually refers to the C library
571 // function or whether it just has the same name.
572
Douglas Gregor9add3172009-02-17 03:23:10 +0000573 // If this is a static function, it's not a builtin.
574 if (getStorageClass() == Static)
575 return 0;
576
Douglas Gregor3c385e52009-02-14 18:57:46 +0000577 // If this function is at translation-unit scope and we're not in
578 // C++, it refers to the C library function.
579 if (!Context.getLangOptions().CPlusPlus &&
580 getDeclContext()->isTranslationUnit())
581 return BuiltinID;
582
583 // If the function is in an extern "C" linkage specification and is
584 // not marked "overloadable", it's the real function.
585 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000586 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000587 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000588 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000589 return BuiltinID;
590
591 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000592 return 0;
593}
594
595
Chris Lattner1ad9b282009-04-25 06:03:53 +0000596/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000597/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000598/// after it has been created.
599unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000600 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000601 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000602 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000603 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605}
606
Ted Kremenekfc767612009-01-14 00:42:25 +0000607void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
608 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000610 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // Zero params -> null pointer.
613 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000614 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000615 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000617
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000618 // Update source range. The check below allows us to set EndRangeLoc before
619 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000620 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000621 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 }
623}
624
Chris Lattner8123a952008-04-10 02:22:51 +0000625/// getMinRequiredArguments - Returns the minimum number of arguments
626/// needed to call this function. This may be fewer than the number of
627/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000628/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000629unsigned FunctionDecl::getMinRequiredArguments() const {
630 unsigned NumRequiredArgs = getNumParams();
631 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000632 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000633 --NumRequiredArgs;
634
635 return NumRequiredArgs;
636}
637
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000638/// \brief For an inline function definition in C, determine whether the
639/// definition will be externally visible.
640///
641/// Inline function definitions are always available for inlining optimizations.
642/// However, depending on the language dialect, declaration specifiers, and
643/// attributes, the definition of an inline function may or may not be
644/// "externally" visible to other translation units in the program.
645///
646/// In C99, inline definitions are not externally visible by default. However,
647/// if even one of the globa-scope declarations is marked "extern inline", the
648/// inline definition becomes externally visible (C99 6.7.4p6).
649///
650/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
651/// definition, we use the GNU semantics for inline, which are nearly the
652/// opposite of C99 semantics. In particular, "inline" by itself will create
653/// an externally visible symbol, but "extern inline" will not create an
654/// externally visible symbol.
655bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
656 assert(isThisDeclarationADefinition() && "Must have the function definition");
657 assert(isInline() && "Function must be inline");
658
659 if (!getASTContext().getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
660 // GNU inline semantics. Based on a number of examples, we came up with the
661 // following heuristic: if the "inline" keyword is present on a
662 // declaration of the function but "extern" is not present on that
663 // declaration, then the symbol is externally visible. Otherwise, the GNU
664 // "extern inline" semantics applies and the symbol is not externally
665 // visible.
666 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
667 Redecl != RedeclEnd;
668 ++Redecl) {
669 if (Redecl->isInline() && Redecl->getStorageClass() != Extern)
670 return true;
671 }
672
673 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000674 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000675 }
676
677 // C99 6.7.4p6:
678 // [...] If all of the file scope declarations for a function in a
679 // translation unit include the inline function specifier without extern,
680 // then the definition in that translation unit is an inline definition.
681 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
682 Redecl != RedeclEnd;
683 ++Redecl) {
684 // Only consider file-scope declarations in this test.
685 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
686 continue;
687
688 if (!Redecl->isInline() || Redecl->getStorageClass() == Extern)
689 return true; // Not an inline definition
690 }
691
692 // C99 6.7.4p6:
693 // An inline definition does not provide an external definition for the
694 // function, and does not forbid an external definition in another
695 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000696 return false;
697}
698
Mike Stump1eb44332009-09-09 15:08:12 +0000699void
Douglas Gregor127102b2009-06-29 20:59:39 +0000700FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000701 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000702
Douglas Gregor127102b2009-06-29 20:59:39 +0000703 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000704 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000705 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
706 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
707 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
708 }
709}
710
Mike Stump740256b2009-09-29 00:50:50 +0000711const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
712 return getFirstDeclaration();
713}
714
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000715FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000716 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000717}
718
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000719/// getOverloadedOperator - Which C++ overloaded operator this
720/// function represents, if any.
721OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000722 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
723 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000724 else
725 return OO_None;
726}
727
Douglas Gregor2db32322009-10-07 23:56:10 +0000728FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000729 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +0000730 return cast<FunctionDecl>(Info->getInstantiatedFrom());
731
732 return 0;
733}
734
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000735MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
736 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
737}
738
Douglas Gregor2db32322009-10-07 23:56:10 +0000739void
740FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
741 TemplateSpecializationKind TSK) {
742 assert(TemplateOrSpecialization.isNull() &&
743 "Member function is already a specialization");
744 MemberSpecializationInfo *Info
745 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
746 TemplateOrSpecialization = Info;
747}
748
Douglas Gregor16e8be22009-06-29 17:30:29 +0000749FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000750 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000751 = TemplateOrSpecialization
752 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000753 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +0000754 }
755 return 0;
756}
757
758const TemplateArgumentList *
759FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000760 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000761 = TemplateOrSpecialization
762 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +0000763 return Info->TemplateArguments;
764 }
765 return 0;
766}
767
Mike Stump1eb44332009-09-09 15:08:12 +0000768void
Douglas Gregor1637be72009-06-26 00:10:03 +0000769FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
770 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +0000771 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000772 void *InsertPos,
773 TemplateSpecializationKind TSK) {
774 assert(TSK != TSK_Undeclared &&
775 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +0000776 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +0000777 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +0000778 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +0000779 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Douglas Gregor127102b2009-06-29 20:59:39 +0000781 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000782 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000783 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +0000784 Info->TemplateArguments = TemplateArgs;
785 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Douglas Gregor127102b2009-06-29 20:59:39 +0000787 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000788 // function template specializations.
789 if (InsertPos)
790 Template->getSpecializations().InsertNode(Info, InsertPos);
791 else {
792 // Try to insert the new node. If there is an existing node, remove it
793 // first.
794 FunctionTemplateSpecializationInfo *Existing
795 = Template->getSpecializations().GetOrInsertNode(Info);
796 if (Existing) {
797 Template->getSpecializations().RemoveNode(Existing);
798 Template->getSpecializations().GetOrInsertNode(Info);
799 }
800 }
Douglas Gregor1637be72009-06-26 00:10:03 +0000801}
802
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000803TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000804 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000805 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +0000806 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000807 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +0000808 if (FTSInfo)
809 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Douglas Gregor2db32322009-10-07 23:56:10 +0000811 MemberSpecializationInfo *MSInfo
812 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
813 if (MSInfo)
814 return MSInfo->getTemplateSpecializationKind();
815
816 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000817}
818
Mike Stump1eb44332009-09-09 15:08:12 +0000819void
Douglas Gregor0a897e32009-10-15 17:21:20 +0000820FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
821 SourceLocation PointOfInstantiation) {
822 if (FunctionTemplateSpecializationInfo *FTSInfo
823 = TemplateOrSpecialization.dyn_cast<
824 FunctionTemplateSpecializationInfo*>()) {
825 FTSInfo->setTemplateSpecializationKind(TSK);
826 if (TSK != TSK_ExplicitSpecialization &&
827 PointOfInstantiation.isValid() &&
828 FTSInfo->getPointOfInstantiation().isInvalid())
829 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
830 } else if (MemberSpecializationInfo *MSInfo
831 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
832 MSInfo->setTemplateSpecializationKind(TSK);
833 if (TSK != TSK_ExplicitSpecialization &&
834 PointOfInstantiation.isValid() &&
835 MSInfo->getPointOfInstantiation().isInvalid())
836 MSInfo->setPointOfInstantiation(PointOfInstantiation);
837 } else
838 assert(false && "Function cannot have a template specialization kind");
839}
840
841SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +0000842 if (FunctionTemplateSpecializationInfo *FTSInfo
843 = TemplateOrSpecialization.dyn_cast<
844 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000845 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +0000846 else if (MemberSpecializationInfo *MSInfo
847 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +0000848 return MSInfo->getPointOfInstantiation();
849
850 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000851}
852
Douglas Gregor9f185072009-09-11 20:15:17 +0000853bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +0000854 if (Decl::isOutOfLine())
855 return true;
856
857 // If this function was instantiated from a member function of a
858 // class template, check whether that member function was defined out-of-line.
859 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
860 const FunctionDecl *Definition;
861 if (FD->getBody(Definition))
862 return Definition->isOutOfLine();
863 }
864
865 // If this function was instantiated from a function template,
866 // check whether that function template was defined out-of-line.
867 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
868 const FunctionDecl *Definition;
869 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
870 return Definition->isOutOfLine();
871 }
872
873 return false;
874}
875
Chris Lattner8a934232008-03-31 00:36:02 +0000876//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000877// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000878//===----------------------------------------------------------------------===//
879
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000880SourceRange TagDecl::getSourceRange() const {
881 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000882 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +0000883}
884
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000885TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000886 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000887}
888
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000889void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000890 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
891 TagT->decl.setPointer(this);
892 TagT->decl.setInt(1);
893 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000894}
895
896void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000897 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000898 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
899 assert(TagT->decl.getPointer() == this &&
900 "Attempt to redefine a tag definition?");
901 TagT->decl.setInt(0);
902 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000903}
904
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000905TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000906 if (isDefinition())
907 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000908
909 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000910 R != REnd; ++R)
911 if (R->isDefinition())
912 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000914 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000915}
916
John McCallf1bbbb42009-09-04 01:14:41 +0000917TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
918 switch (TypeSpec) {
919 default: llvm::llvm_unreachable("unexpected type specifier");
920 case DeclSpec::TST_struct: return TK_struct;
921 case DeclSpec::TST_class: return TK_class;
922 case DeclSpec::TST_union: return TK_union;
923 case DeclSpec::TST_enum: return TK_enum;
924 }
925}
926
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000927//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000928// RecordDecl Implementation
929//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000930
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000931RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000932 IdentifierInfo *Id, RecordDecl *PrevDecl,
933 SourceLocation TKL)
934 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +0000935 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000936 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000937 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000938 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000939}
940
941RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000942 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000943 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000945 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000946 C.getTypeDeclType(R, PrevDecl);
947 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000948}
949
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000950RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000951}
952
953void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000954 TagDecl::Destroy(C);
955}
956
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000957bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000958 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +0000959 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
960}
961
Douglas Gregor44b43212008-12-11 16:49:14 +0000962/// completeDefinition - Notes that the definition of this type is now
963/// complete.
964void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000966 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000967}
968
Steve Naroff56ee6892008-10-08 17:01:13 +0000969//===----------------------------------------------------------------------===//
970// BlockDecl Implementation
971//===----------------------------------------------------------------------===//
972
973BlockDecl::~BlockDecl() {
974}
975
976void BlockDecl::Destroy(ASTContext& C) {
977 if (Body)
978 Body->Destroy(C);
979
980 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
981 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
983 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +0000984 Decl::Destroy(C);
985}
Steve Naroffe78b8092009-03-13 16:56:44 +0000986
987void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
988 unsigned NParms) {
989 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Steve Naroffe78b8092009-03-13 16:56:44 +0000991 // Zero params -> null pointer.
992 if (NParms) {
993 NumParams = NParms;
994 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
995 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
996 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
997 }
998}
999
1000unsigned BlockDecl::getNumParams() const {
1001 return NumParams;
1002}