blob: 572d76ff72d20e963f512a80206ccd9abc9abd73 [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
Douglas Gregor6cc15182009-09-11 18:44:32 +000094SourceRange ParmVarDecl::getDefaultArgRange() const {
95 if (const Expr *E = getInit())
96 return E->getSourceRange();
97
98 if (const Expr *E = getUninstantiatedDefaultArg())
99 return E->getSourceRange();
100
101 return SourceRange();
102}
Douglas Gregor78d15832009-05-26 18:54:04 +0000103
Douglas Gregor6cc15182009-09-11 18:44:32 +0000104void VarDecl::setInit(ASTContext &C, Expr *I) {
105 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
106 Eval->~EvaluatedStmt();
107 C.Deallocate(Eval);
Douglas Gregor78d15832009-05-26 18:54:04 +0000108 }
109
Douglas Gregor6cc15182009-09-11 18:44:32 +0000110 Init = I;
111}
112
Douglas Gregor48a83b52009-09-12 00:17:51 +0000113bool VarDecl::isExternC() const {
114 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000115 if (!Context.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000116 return (getDeclContext()->isTranslationUnit() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000117 getStorageClass() != Static) ||
118 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
119
Mike Stump1eb44332009-09-09 15:08:12 +0000120 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000121 DC = DC->getParent()) {
122 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
123 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
124 return getStorageClass() != Static;
125
126 break;
127 }
128
129 if (DC->isFunctionOrMethod())
130 return false;
131 }
132
133 return false;
134}
135
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000136FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000137 SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000138 DeclarationName N, QualType T,
139 DeclaratorInfo *DInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000140 StorageClass S, bool isInline,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000141 bool hasWrittenPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +0000142 FunctionDecl *New
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000143 = new (C) FunctionDecl(Function, DC, L, N, T, DInfo, S, isInline);
Anders Carlssona75e8532009-05-14 21:46:00 +0000144 New->HasWrittenPrototype = hasWrittenPrototype;
Douglas Gregor2224f842009-02-25 16:33:18 +0000145 return New;
Chris Lattnera98e58d2008-03-15 21:24:04 +0000146}
147
Steve Naroff090276f2008-10-10 01:28:17 +0000148BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +0000149 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +0000150}
151
Douglas Gregor44b43212008-12-11 16:49:14 +0000152FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000153 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000154 DeclaratorInfo *DInfo, Expr *BW, bool Mutable) {
155 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, DInfo, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +0000156}
157
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000158bool FieldDecl::isAnonymousStructOrUnion() const {
159 if (!isImplicit() || getDeclName())
160 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenek6217b802009-07-29 21:53:49 +0000162 if (const RecordType *Record = getType()->getAs<RecordType>())
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000163 return Record->getDecl()->isAnonymousStructOrUnion();
164
165 return false;
166}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000167
Chris Lattner0ed844b2008-04-04 06:12:32 +0000168EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
169 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000170 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000171 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000172 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000173}
174
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000175void EnumConstantDecl::Destroy(ASTContext& C) {
176 if (Init) Init->Destroy(C);
177 Decl::Destroy(C);
178}
179
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000180TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
John McCallba6a9bd2009-10-24 08:00:42 +0000181 SourceLocation L, IdentifierInfo *Id,
182 DeclaratorInfo *DInfo) {
183 return new (C) TypedefDecl(DC, L, Id, DInfo);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000184}
185
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000186EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000187 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000188 EnumDecl *PrevDecl) {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000189 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000190 C.getTypeDeclType(Enum, PrevDecl);
191 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000192}
193
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000194void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000195 Decl::Destroy(C);
196}
197
Douglas Gregor44b43212008-12-11 16:49:14 +0000198void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
199 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000200 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000201 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000202}
203
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000204FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000205 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000206 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000207 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000208}
209
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000210//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000212//===----------------------------------------------------------------------===//
213
Douglas Gregord85b5b92009-11-25 22:24:25 +0000214static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
215 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
216 "Not a name having namespace scope");
217 ASTContext &Context = D->getASTContext();
218
219 // C++ [basic.link]p3:
220 // A name having namespace scope (3.3.6) has internal linkage if it
221 // is the name of
222 // - an object, reference, function or function template that is
223 // explicitly declared static; or,
224 // (This bullet corresponds to C99 6.2.2p3.)
225 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
226 // Explicitly declared static.
227 if (Var->getStorageClass() == VarDecl::Static)
228 return NamedDecl::InternalLinkage;
229
230 // - an object or reference that is explicitly declared const
231 // and neither explicitly declared extern nor previously
232 // declared to have external linkage; or
233 // (there is no equivalent in C99)
234 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000235 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000236 Var->getStorageClass() != VarDecl::Extern &&
237 Var->getStorageClass() != VarDecl::PrivateExtern) {
238 bool FoundExtern = false;
239 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
240 PrevVar && !FoundExtern;
241 PrevVar = PrevVar->getPreviousDeclaration())
242 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
243 FoundExtern = true;
244
245 if (!FoundExtern)
246 return NamedDecl::InternalLinkage;
247 }
248 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
249 const FunctionDecl *Function = 0;
250 if (const FunctionTemplateDecl *FunTmpl
251 = dyn_cast<FunctionTemplateDecl>(D))
252 Function = FunTmpl->getTemplatedDecl();
253 else
254 Function = cast<FunctionDecl>(D);
255
256 // Explicitly declared static.
257 if (Function->getStorageClass() == FunctionDecl::Static)
258 return NamedDecl::InternalLinkage;
259 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
260 // - a data member of an anonymous union.
261 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
262 return NamedDecl::InternalLinkage;
263 }
264
265 // C++ [basic.link]p4:
266
267 // A name having namespace scope has external linkage if it is the
268 // name of
269 //
270 // - an object or reference, unless it has internal linkage; or
271 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
272 if (!Context.getLangOptions().CPlusPlus &&
273 (Var->getStorageClass() == VarDecl::Extern ||
274 Var->getStorageClass() == VarDecl::PrivateExtern)) {
275 // C99 6.2.2p4:
276 // For an identifier declared with the storage-class specifier
277 // extern in a scope in which a prior declaration of that
278 // identifier is visible, if the prior declaration specifies
279 // internal or external linkage, the linkage of the identifier
280 // at the later declaration is the same as the linkage
281 // specified at the prior declaration. If no prior declaration
282 // is visible, or if the prior declaration specifies no
283 // linkage, then the identifier has external linkage.
284 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
285 if (NamedDecl::Linkage L = PrevVar->getLinkage())
286 return L;
287 }
288 }
289
290 // C99 6.2.2p5:
291 // If the declaration of an identifier for an object has file
292 // scope and no storage-class specifier, its linkage is
293 // external.
294 return NamedDecl::ExternalLinkage;
295 }
296
297 // - a function, unless it has internal linkage; or
298 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
299 // C99 6.2.2p5:
300 // If the declaration of an identifier for a function has no
301 // storage-class specifier, its linkage is determined exactly
302 // as if it were declared with the storage-class specifier
303 // extern.
304 if (!Context.getLangOptions().CPlusPlus &&
305 (Function->getStorageClass() == FunctionDecl::Extern ||
306 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
307 Function->getStorageClass() == FunctionDecl::None)) {
308 // C99 6.2.2p4:
309 // For an identifier declared with the storage-class specifier
310 // extern in a scope in which a prior declaration of that
311 // identifier is visible, if the prior declaration specifies
312 // internal or external linkage, the linkage of the identifier
313 // at the later declaration is the same as the linkage
314 // specified at the prior declaration. If no prior declaration
315 // is visible, or if the prior declaration specifies no
316 // linkage, then the identifier has external linkage.
317 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
318 if (NamedDecl::Linkage L = PrevFunc->getLinkage())
319 return L;
320 }
321 }
322
323 return NamedDecl::ExternalLinkage;
324 }
325
326 // - a named class (Clause 9), or an unnamed class defined in a
327 // typedef declaration in which the class has the typedef name
328 // for linkage purposes (7.1.3); or
329 // - a named enumeration (7.2), or an unnamed enumeration
330 // defined in a typedef declaration in which the enumeration
331 // has the typedef name for linkage purposes (7.1.3); or
332 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
333 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
334 return NamedDecl::ExternalLinkage;
335
336 // - an enumerator belonging to an enumeration with external linkage;
337 if (isa<EnumConstantDecl>(D))
338 if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
339 == NamedDecl::ExternalLinkage)
340 return NamedDecl::ExternalLinkage;
341
342 // - a template, unless it is a function template that has
343 // internal linkage (Clause 14);
344 if (isa<TemplateDecl>(D))
345 return NamedDecl::ExternalLinkage;
346
347 // - a namespace (7.3), unless it is declared within an unnamed
348 // namespace.
349 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
350 return NamedDecl::ExternalLinkage;
351
352 return NamedDecl::NoLinkage;
353}
354
355NamedDecl::Linkage NamedDecl::getLinkage() const {
356 // Handle linkage for namespace-scope names.
357 if (getDeclContext()->getLookupContext()->isFileContext())
358 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
359 return L;
360
361 // C++ [basic.link]p5:
362 // In addition, a member function, static data member, a named
363 // class or enumeration of class scope, or an unnamed class or
364 // enumeration defined in a class-scope typedef declaration such
365 // that the class or enumeration has the typedef name for linkage
366 // purposes (7.1.3), has external linkage if the name of the class
367 // has external linkage.
368 if (getDeclContext()->isRecord() &&
369 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
370 (isa<TagDecl>(this) &&
371 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
372 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
373 return ExternalLinkage;
374
375 // C++ [basic.link]p6:
376 // The name of a function declared in block scope and the name of
377 // an object declared by a block scope extern declaration have
378 // linkage. If there is a visible declaration of an entity with
379 // linkage having the same name and type, ignoring entities
380 // declared outside the innermost enclosing namespace scope, the
381 // block scope declaration declares that same entity and receives
382 // the linkage of the previous declaration. If there is more than
383 // one such matching entity, the program is ill-formed. Otherwise,
384 // if no matching entity is found, the block scope entity receives
385 // external linkage.
386 if (getLexicalDeclContext()->isFunctionOrMethod()) {
387 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
388 if (Function->getPreviousDeclaration())
389 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
390 return L;
391
392 return ExternalLinkage;
393 }
394
395 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
396 if (Var->getStorageClass() == VarDecl::Extern ||
397 Var->getStorageClass() == VarDecl::PrivateExtern) {
398 if (Var->getPreviousDeclaration())
399 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
400 return L;
401
402 return ExternalLinkage;
403 }
404 }
405
406 // C++ [basic.link]p6:
407 // Names not covered by these rules have no linkage.
408 return NoLinkage;
409}
410
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000411std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000412 return getQualifiedNameAsString(getASTContext().getLangOptions());
413}
414
415std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000416 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
417 // std::string thrashing.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000418 std::vector<std::string> Names;
419 std::string QualName;
420 const DeclContext *Ctx = getDeclContext();
421
422 if (Ctx->isFunctionOrMethod())
423 return getNameAsString();
424
425 while (Ctx) {
426 if (Ctx->isFunctionOrMethod())
427 // FIXME: That probably will happen, when D was member of local
428 // scope class/struct/union. How do we handle this case?
429 break;
430
Mike Stump1eb44332009-09-09 15:08:12 +0000431 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000432 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
433 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
434 std::string TemplateArgsStr
435 = TemplateSpecializationType::PrintTemplateArgumentList(
436 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000437 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000438 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000439 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000440 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000441 Names.push_back(ND->getNameAsString());
442 else
443 break;
444
445 Ctx = Ctx->getParent();
446 }
447
448 std::vector<std::string>::reverse_iterator
449 I = Names.rbegin(),
450 End = Names.rend();
451
452 for (; I!=End; ++I)
453 QualName += *I + "::";
454
455 QualName += getNameAsString();
456
457 return QualName;
458}
459
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000460bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000461 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
462
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000463 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
464 // We want to keep it, unless it nominates same namespace.
465 if (getKind() == Decl::UsingDirective) {
466 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
467 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000470 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
471 // For function declarations, we keep track of redeclarations.
472 return FD->getPreviousDeclaration() == OldD;
473
Douglas Gregore53060f2009-06-25 22:08:12 +0000474 // For function templates, the underlying function declarations are linked.
475 if (const FunctionTemplateDecl *FunctionTemplate
476 = dyn_cast<FunctionTemplateDecl>(this))
477 if (const FunctionTemplateDecl *OldFunctionTemplate
478 = dyn_cast<FunctionTemplateDecl>(OldD))
479 return FunctionTemplate->getTemplatedDecl()
480 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Steve Naroff0de21fd2009-02-22 19:35:57 +0000482 // For method declarations, we keep track of redeclarations.
483 if (isa<ObjCMethodDecl>(this))
484 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
John McCallf36e02d2009-10-09 21:13:30 +0000486 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
487 return true;
488
John McCall9488ea12009-11-17 05:59:44 +0000489 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
490 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
491 cast<UsingShadowDecl>(OldD)->getTargetDecl();
492
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000493 // For non-function declarations, if the declarations are of the
494 // same kind then this must be a redeclaration, or semantic analysis
495 // would not have given us the new declaration.
496 return this->getKind() == OldD->getKind();
497}
498
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000499bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000500 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000501}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000502
Anders Carlssone136e0e2009-06-26 06:29:23 +0000503NamedDecl *NamedDecl::getUnderlyingDecl() {
504 NamedDecl *ND = this;
505 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000506 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000507 ND = UD->getTargetDecl();
508 else if (ObjCCompatibleAliasDecl *AD
509 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
510 return AD->getClassInterface();
511 else
512 return ND;
513 }
514}
515
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000516//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000517// DeclaratorDecl Implementation
518//===----------------------------------------------------------------------===//
519
520SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000521 if (DeclInfo) {
522 TypeLoc TL = DeclInfo->getTypeLoc();
523 while (true) {
524 TypeLoc NextTL = TL.getNextTypeLoc();
525 if (!NextTL)
526 return TL.getSourceRange().getBegin();
527 TL = NextTL;
528 }
529 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000530 return SourceLocation();
531}
532
533//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000534// VarDecl Implementation
535//===----------------------------------------------------------------------===//
536
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000537VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000538 IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000539 StorageClass S) {
540 return new (C) VarDecl(Var, DC, L, Id, T, DInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000541}
542
543void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000544 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000545 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000546 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000547 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
548 Eval->~EvaluatedStmt();
549 C.Deallocate(Eval);
550 }
551 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000552 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000553 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000554}
555
556VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000557}
558
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000559SourceRange VarDecl::getSourceRange() const {
560 if (getInit())
561 return SourceRange(getLocation(), getInit()->getLocEnd());
562 return SourceRange(getLocation(), getLocation());
563}
564
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000565bool VarDecl::isOutOfLine() const {
566 if (!isStaticDataMember())
567 return false;
568
569 if (Decl::isOutOfLine())
570 return true;
571
572 // If this static data member was instantiated from a static data member of
573 // a class template, check whether that static data member was defined
574 // out-of-line.
575 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
576 return VD->isOutOfLine();
577
578 return false;
579}
580
Douglas Gregor0d035142009-10-27 18:42:08 +0000581VarDecl *VarDecl::getOutOfLineDefinition() {
582 if (!isStaticDataMember())
583 return 0;
584
585 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
586 RD != RDEnd; ++RD) {
587 if (RD->getLexicalDeclContext()->isFileContext())
588 return *RD;
589 }
590
591 return 0;
592}
593
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000594VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000595 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000596 return cast<VarDecl>(MSI->getInstantiatedFrom());
597
598 return 0;
599}
600
Douglas Gregor663b5a02009-10-14 20:14:33 +0000601TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000602 if (MemberSpecializationInfo *MSI
603 = getASTContext().getInstantiatedFromStaticDataMember(this))
604 return MSI->getTemplateSpecializationKind();
605
606 return TSK_Undeclared;
607}
608
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000609MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000610 return getASTContext().getInstantiatedFromStaticDataMember(this);
611}
612
Douglas Gregor0a897e32009-10-15 17:21:20 +0000613void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
614 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000615 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000616 assert(MSI && "Not an instantiated static data member?");
617 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000618 if (TSK != TSK_ExplicitSpecialization &&
619 PointOfInstantiation.isValid() &&
620 MSI->getPointOfInstantiation().isInvalid())
621 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000622}
623
Douglas Gregor275a3692009-03-10 23:43:53 +0000624bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
625 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
626 return false;
627
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000628 const VarDecl *Def = 0;
629 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000630 (getStorageClass() == None || getStorageClass() == Static));
631}
632
Ted Kremenek082d9362009-03-20 21:35:28 +0000633const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000634 redecl_iterator I = redecls_begin(), E = redecls_end();
635 while (I != E && !I->getInit())
636 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000637
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000638 if (I != E) {
639 Def = *I;
640 return I->getInit();
641 }
642 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000643}
644
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000645VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000646 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000647}
648
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000649//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000650// FunctionDecl Implementation
651//===----------------------------------------------------------------------===//
652
Ted Kremenek27f8a282008-05-20 00:43:19 +0000653void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000654 if (Body && Body.isOffset())
655 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000656
657 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
658 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000659
Douglas Gregor2db32322009-10-07 23:56:10 +0000660 FunctionTemplateSpecializationInfo *FTSInfo
661 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
662 if (FTSInfo)
663 C.Deallocate(FTSInfo);
664
665 MemberSpecializationInfo *MSInfo
666 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
667 if (MSInfo)
668 C.Deallocate(MSInfo);
669
Steve Naroff3e970492009-01-27 21:25:57 +0000670 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000671
Ted Kremenek27f8a282008-05-20 00:43:19 +0000672 Decl::Destroy(C);
673}
674
John McCall136a6982009-09-11 06:45:03 +0000675void FunctionDecl::getNameForDiagnostic(std::string &S,
676 const PrintingPolicy &Policy,
677 bool Qualified) const {
678 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
679 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
680 if (TemplateArgs)
681 S += TemplateSpecializationType::PrintTemplateArgumentList(
682 TemplateArgs->getFlatArgumentList(),
683 TemplateArgs->flat_size(),
684 Policy);
685
686}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000687
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000688Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000689 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
690 if (I->Body) {
691 Definition = *I;
692 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000693 }
694 }
695
696 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000697}
698
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000699void FunctionDecl::setBody(Stmt *B) {
700 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000701 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000702 EndRangeLoc = B->getLocEnd();
703}
704
Douglas Gregor48a83b52009-09-12 00:17:51 +0000705bool FunctionDecl::isMain() const {
706 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000707 return !Context.getLangOptions().Freestanding &&
708 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000709 getIdentifier() && getIdentifier()->isStr("main");
710}
711
Douglas Gregor48a83b52009-09-12 00:17:51 +0000712bool FunctionDecl::isExternC() const {
713 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000714 // In C, any non-static, non-overloadable function has external
715 // linkage.
716 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000717 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000718
Mike Stump1eb44332009-09-09 15:08:12 +0000719 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000720 DC = DC->getParent()) {
721 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
722 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000723 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000724 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000725
726 break;
727 }
728 }
729
730 return false;
731}
732
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000733bool FunctionDecl::isGlobal() const {
734 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
735 return Method->isStatic();
736
737 if (getStorageClass() == Static)
738 return false;
739
Mike Stump1eb44332009-09-09 15:08:12 +0000740 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000741 DC->isNamespace();
742 DC = DC->getParent()) {
743 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
744 if (!Namespace->getDeclName())
745 return false;
746 break;
747 }
748 }
749
750 return true;
751}
752
Douglas Gregor3e41d602009-02-13 23:20:09 +0000753/// \brief Returns a value indicating whether this function
754/// corresponds to a builtin function.
755///
756/// The function corresponds to a built-in function if it is
757/// declared at translation scope or within an extern "C" block and
758/// its name matches with the name of a builtin. The returned value
759/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000760/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000761/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000762unsigned FunctionDecl::getBuiltinID() const {
763 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000764 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
765 return 0;
766
767 unsigned BuiltinID = getIdentifier()->getBuiltinID();
768 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
769 return BuiltinID;
770
771 // This function has the name of a known C library
772 // function. Determine whether it actually refers to the C library
773 // function or whether it just has the same name.
774
Douglas Gregor9add3172009-02-17 03:23:10 +0000775 // If this is a static function, it's not a builtin.
776 if (getStorageClass() == Static)
777 return 0;
778
Douglas Gregor3c385e52009-02-14 18:57:46 +0000779 // If this function is at translation-unit scope and we're not in
780 // C++, it refers to the C library function.
781 if (!Context.getLangOptions().CPlusPlus &&
782 getDeclContext()->isTranslationUnit())
783 return BuiltinID;
784
785 // If the function is in an extern "C" linkage specification and is
786 // not marked "overloadable", it's the real function.
787 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000788 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000789 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000790 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000791 return BuiltinID;
792
793 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000794 return 0;
795}
796
797
Chris Lattner1ad9b282009-04-25 06:03:53 +0000798/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000799/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000800/// after it has been created.
801unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000802 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000803 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000804 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000805 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807}
808
Ted Kremenekfc767612009-01-14 00:42:25 +0000809void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
810 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000812 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 // Zero params -> null pointer.
815 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000816 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000817 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000819
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000820 // Update source range. The check below allows us to set EndRangeLoc before
821 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000822 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000823 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 }
825}
826
Chris Lattner8123a952008-04-10 02:22:51 +0000827/// getMinRequiredArguments - Returns the minimum number of arguments
828/// needed to call this function. This may be fewer than the number of
829/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000830/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000831unsigned FunctionDecl::getMinRequiredArguments() const {
832 unsigned NumRequiredArgs = getNumParams();
833 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000834 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000835 --NumRequiredArgs;
836
837 return NumRequiredArgs;
838}
839
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000840bool FunctionDecl::isInlined() const {
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000841 if (isInlineSpecified() || (isa<CXXMethodDecl>(this) && !isOutOfLine()))
842 return true;
843
844 switch (getTemplateSpecializationKind()) {
845 case TSK_Undeclared:
846 case TSK_ExplicitSpecialization:
847 return false;
848
849 case TSK_ImplicitInstantiation:
850 case TSK_ExplicitInstantiationDeclaration:
851 case TSK_ExplicitInstantiationDefinition:
852 // Handle below.
853 break;
854 }
855
856 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
857 Stmt *Pattern = 0;
858 if (PatternDecl)
859 Pattern = PatternDecl->getBody(PatternDecl);
860
861 if (Pattern && PatternDecl)
862 return PatternDecl->isInlined();
863
864 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000865}
866
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000867/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000868/// definition will be externally visible.
869///
870/// Inline function definitions are always available for inlining optimizations.
871/// However, depending on the language dialect, declaration specifiers, and
872/// attributes, the definition of an inline function may or may not be
873/// "externally" visible to other translation units in the program.
874///
875/// In C99, inline definitions are not externally visible by default. However,
876/// if even one of the globa-scope declarations is marked "extern inline", the
877/// inline definition becomes externally visible (C99 6.7.4p6).
878///
879/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
880/// definition, we use the GNU semantics for inline, which are nearly the
881/// opposite of C99 semantics. In particular, "inline" by itself will create
882/// an externally visible symbol, but "extern inline" will not create an
883/// externally visible symbol.
884bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
885 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000886 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000887 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000888
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000889 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000890 // GNU inline semantics. Based on a number of examples, we came up with the
891 // following heuristic: if the "inline" keyword is present on a
892 // declaration of the function but "extern" is not present on that
893 // declaration, then the symbol is externally visible. Otherwise, the GNU
894 // "extern inline" semantics applies and the symbol is not externally
895 // visible.
896 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
897 Redecl != RedeclEnd;
898 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000899 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000900 return true;
901 }
902
903 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000904 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000905 }
906
907 // C99 6.7.4p6:
908 // [...] If all of the file scope declarations for a function in a
909 // translation unit include the inline function specifier without extern,
910 // then the definition in that translation unit is an inline definition.
911 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
912 Redecl != RedeclEnd;
913 ++Redecl) {
914 // Only consider file-scope declarations in this test.
915 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
916 continue;
917
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000918 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000919 return true; // Not an inline definition
920 }
921
922 // C99 6.7.4p6:
923 // An inline definition does not provide an external definition for the
924 // function, and does not forbid an external definition in another
925 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000926 return false;
927}
928
Mike Stump1eb44332009-09-09 15:08:12 +0000929void
Douglas Gregor127102b2009-06-29 20:59:39 +0000930FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000931 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000932
Douglas Gregor127102b2009-06-29 20:59:39 +0000933 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000934 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000935 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
936 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
937 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
938 }
939}
940
Mike Stump740256b2009-09-29 00:50:50 +0000941const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
942 return getFirstDeclaration();
943}
944
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000945FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000946 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000947}
948
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000949/// getOverloadedOperator - Which C++ overloaded operator this
950/// function represents, if any.
951OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000952 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
953 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000954 else
955 return OO_None;
956}
957
Douglas Gregor2db32322009-10-07 23:56:10 +0000958FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000959 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +0000960 return cast<FunctionDecl>(Info->getInstantiatedFrom());
961
962 return 0;
963}
964
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000965MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
966 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
967}
968
Douglas Gregor2db32322009-10-07 23:56:10 +0000969void
970FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
971 TemplateSpecializationKind TSK) {
972 assert(TemplateOrSpecialization.isNull() &&
973 "Member function is already a specialization");
974 MemberSpecializationInfo *Info
975 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
976 TemplateOrSpecialization = Info;
977}
978
Douglas Gregor3b846b62009-10-27 20:53:28 +0000979bool FunctionDecl::isImplicitlyInstantiable() const {
980 // If this function already has a definition or is invalid, it can't be
981 // implicitly instantiated.
982 if (isInvalidDecl() || getBody())
983 return false;
984
985 switch (getTemplateSpecializationKind()) {
986 case TSK_Undeclared:
987 case TSK_ExplicitSpecialization:
988 case TSK_ExplicitInstantiationDefinition:
989 return false;
990
991 case TSK_ImplicitInstantiation:
992 return true;
993
994 case TSK_ExplicitInstantiationDeclaration:
995 // Handled below.
996 break;
997 }
998
999 // Find the actual template from which we will instantiate.
1000 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1001 Stmt *Pattern = 0;
1002 if (PatternDecl)
1003 Pattern = PatternDecl->getBody(PatternDecl);
1004
1005 // C++0x [temp.explicit]p9:
1006 // Except for inline functions, other explicit instantiation declarations
1007 // have the effect of suppressing the implicit instantiation of the entity
1008 // to which they refer.
1009 if (!Pattern || !PatternDecl)
1010 return true;
1011
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001012 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001013}
1014
1015FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1016 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1017 while (Primary->getInstantiatedFromMemberTemplate()) {
1018 // If we have hit a point where the user provided a specialization of
1019 // this template, we're done looking.
1020 if (Primary->isMemberSpecialization())
1021 break;
1022
1023 Primary = Primary->getInstantiatedFromMemberTemplate();
1024 }
1025
1026 return Primary->getTemplatedDecl();
1027 }
1028
1029 return getInstantiatedFromMemberFunction();
1030}
1031
Douglas Gregor16e8be22009-06-29 17:30:29 +00001032FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001033 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001034 = TemplateOrSpecialization
1035 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001036 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001037 }
1038 return 0;
1039}
1040
1041const TemplateArgumentList *
1042FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001043 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001044 = TemplateOrSpecialization
1045 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001046 return Info->TemplateArguments;
1047 }
1048 return 0;
1049}
1050
Mike Stump1eb44332009-09-09 15:08:12 +00001051void
Douglas Gregor1637be72009-06-26 00:10:03 +00001052FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1053 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001054 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001055 void *InsertPos,
1056 TemplateSpecializationKind TSK) {
1057 assert(TSK != TSK_Undeclared &&
1058 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001059 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001060 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001061 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +00001062 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Douglas Gregor127102b2009-06-29 20:59:39 +00001064 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001065 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001066 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001067 Info->TemplateArguments = TemplateArgs;
1068 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Douglas Gregor127102b2009-06-29 20:59:39 +00001070 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001071 // function template specializations.
1072 if (InsertPos)
1073 Template->getSpecializations().InsertNode(Info, InsertPos);
1074 else {
1075 // Try to insert the new node. If there is an existing node, remove it
1076 // first.
1077 FunctionTemplateSpecializationInfo *Existing
1078 = Template->getSpecializations().GetOrInsertNode(Info);
1079 if (Existing) {
1080 Template->getSpecializations().RemoveNode(Existing);
1081 Template->getSpecializations().GetOrInsertNode(Info);
1082 }
1083 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001084}
1085
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001086TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001087 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001088 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001089 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001090 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001091 if (FTSInfo)
1092 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregor2db32322009-10-07 23:56:10 +00001094 MemberSpecializationInfo *MSInfo
1095 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1096 if (MSInfo)
1097 return MSInfo->getTemplateSpecializationKind();
1098
1099 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001100}
1101
Mike Stump1eb44332009-09-09 15:08:12 +00001102void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001103FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1104 SourceLocation PointOfInstantiation) {
1105 if (FunctionTemplateSpecializationInfo *FTSInfo
1106 = TemplateOrSpecialization.dyn_cast<
1107 FunctionTemplateSpecializationInfo*>()) {
1108 FTSInfo->setTemplateSpecializationKind(TSK);
1109 if (TSK != TSK_ExplicitSpecialization &&
1110 PointOfInstantiation.isValid() &&
1111 FTSInfo->getPointOfInstantiation().isInvalid())
1112 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1113 } else if (MemberSpecializationInfo *MSInfo
1114 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1115 MSInfo->setTemplateSpecializationKind(TSK);
1116 if (TSK != TSK_ExplicitSpecialization &&
1117 PointOfInstantiation.isValid() &&
1118 MSInfo->getPointOfInstantiation().isInvalid())
1119 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1120 } else
1121 assert(false && "Function cannot have a template specialization kind");
1122}
1123
1124SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001125 if (FunctionTemplateSpecializationInfo *FTSInfo
1126 = TemplateOrSpecialization.dyn_cast<
1127 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001128 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001129 else if (MemberSpecializationInfo *MSInfo
1130 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001131 return MSInfo->getPointOfInstantiation();
1132
1133 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001134}
1135
Douglas Gregor9f185072009-09-11 20:15:17 +00001136bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001137 if (Decl::isOutOfLine())
1138 return true;
1139
1140 // If this function was instantiated from a member function of a
1141 // class template, check whether that member function was defined out-of-line.
1142 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1143 const FunctionDecl *Definition;
1144 if (FD->getBody(Definition))
1145 return Definition->isOutOfLine();
1146 }
1147
1148 // If this function was instantiated from a function template,
1149 // check whether that function template was defined out-of-line.
1150 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1151 const FunctionDecl *Definition;
1152 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1153 return Definition->isOutOfLine();
1154 }
1155
1156 return false;
1157}
1158
Chris Lattner8a934232008-03-31 00:36:02 +00001159//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001160// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001161//===----------------------------------------------------------------------===//
1162
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001163SourceRange TagDecl::getSourceRange() const {
1164 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001165 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001166}
1167
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001168TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001169 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001170}
1171
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001172void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001173 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1174 TagT->decl.setPointer(this);
1175 TagT->decl.setInt(1);
1176 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001177}
1178
1179void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001180 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001181 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1182 assert(TagT->decl.getPointer() == this &&
1183 "Attempt to redefine a tag definition?");
1184 TagT->decl.setInt(0);
1185 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001186}
1187
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001188TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001189 if (isDefinition())
1190 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001191
1192 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001193 R != REnd; ++R)
1194 if (R->isDefinition())
1195 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001197 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001198}
1199
John McCallf1bbbb42009-09-04 01:14:41 +00001200TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1201 switch (TypeSpec) {
1202 default: llvm::llvm_unreachable("unexpected type specifier");
1203 case DeclSpec::TST_struct: return TK_struct;
1204 case DeclSpec::TST_class: return TK_class;
1205 case DeclSpec::TST_union: return TK_union;
1206 case DeclSpec::TST_enum: return TK_enum;
1207 }
1208}
1209
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001210//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001211// RecordDecl Implementation
1212//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001213
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001214RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001215 IdentifierInfo *Id, RecordDecl *PrevDecl,
1216 SourceLocation TKL)
1217 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001218 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001219 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001220 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001221 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001222}
1223
1224RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001225 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001226 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001228 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001229 C.getTypeDeclType(R, PrevDecl);
1230 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001231}
1232
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001233RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001234}
1235
1236void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001237 TagDecl::Destroy(C);
1238}
1239
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001240bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001241 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001242 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1243}
1244
Douglas Gregor44b43212008-12-11 16:49:14 +00001245/// completeDefinition - Notes that the definition of this type is now
1246/// complete.
1247void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001249 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001250}
1251
Steve Naroff56ee6892008-10-08 17:01:13 +00001252//===----------------------------------------------------------------------===//
1253// BlockDecl Implementation
1254//===----------------------------------------------------------------------===//
1255
1256BlockDecl::~BlockDecl() {
1257}
1258
1259void BlockDecl::Destroy(ASTContext& C) {
1260 if (Body)
1261 Body->Destroy(C);
1262
1263 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1264 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001265
1266 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001267 Decl::Destroy(C);
1268}
Steve Naroffe78b8092009-03-13 16:56:44 +00001269
1270void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1271 unsigned NParms) {
1272 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Steve Naroffe78b8092009-03-13 16:56:44 +00001274 // Zero params -> null pointer.
1275 if (NParms) {
1276 NumParams = NParms;
1277 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1278 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1279 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1280 }
1281}
1282
1283unsigned BlockDecl::getNumParams() const {
1284 return NumParams;
1285}