blob: 95934747d07f12cccdf346f77292fbd9bbf216d0 [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.
John McCalla93c9342009-12-07 02:54:59 +000041TypeLoc TypeSourceInfo::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,
John McCalla93c9342009-12-07 02:54:59 +000089 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +000090 StorageClass S, Expr *DefArg) {
John McCalla93c9342009-12-07 02:54:59 +000091 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, 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,
John McCalla93c9342009-12-07 02:54:59 +0000139 TypeSourceInfo *TInfo,
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
John McCalla93c9342009-12-07 02:54:59 +0000143 = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, 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,
John McCalla93c9342009-12-07 02:54:59 +0000154 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
155 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, 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,
John McCalla93c9342009-12-07 02:54:59 +0000182 TypeSourceInfo *TInfo) {
183 return new (C) TypedefDecl(DC, L, Id, TInfo);
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
John McCall842aef82009-12-09 09:09:27 +0000198void EnumDecl::completeDefinition(ASTContext &C,
199 QualType NewType,
200 QualType NewPromotionType) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000201 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000202 IntegerType = NewType;
John McCall842aef82009-12-09 09:09:27 +0000203 PromotionType = NewPromotionType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000204 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000205}
206
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000207FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000208 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000209 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000210 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000211}
212
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000213//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000214// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000215//===----------------------------------------------------------------------===//
216
Douglas Gregord85b5b92009-11-25 22:24:25 +0000217static NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
218 assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
219 "Not a name having namespace scope");
220 ASTContext &Context = D->getASTContext();
221
222 // C++ [basic.link]p3:
223 // A name having namespace scope (3.3.6) has internal linkage if it
224 // is the name of
225 // - an object, reference, function or function template that is
226 // explicitly declared static; or,
227 // (This bullet corresponds to C99 6.2.2p3.)
228 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
229 // Explicitly declared static.
230 if (Var->getStorageClass() == VarDecl::Static)
231 return NamedDecl::InternalLinkage;
232
233 // - an object or reference that is explicitly declared const
234 // and neither explicitly declared extern nor previously
235 // declared to have external linkage; or
236 // (there is no equivalent in C99)
237 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000238 Var->getType().isConstant(Context) &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000239 Var->getStorageClass() != VarDecl::Extern &&
240 Var->getStorageClass() != VarDecl::PrivateExtern) {
241 bool FoundExtern = false;
242 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
243 PrevVar && !FoundExtern;
244 PrevVar = PrevVar->getPreviousDeclaration())
245 if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
246 FoundExtern = true;
247
248 if (!FoundExtern)
249 return NamedDecl::InternalLinkage;
250 }
251 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
252 const FunctionDecl *Function = 0;
253 if (const FunctionTemplateDecl *FunTmpl
254 = dyn_cast<FunctionTemplateDecl>(D))
255 Function = FunTmpl->getTemplatedDecl();
256 else
257 Function = cast<FunctionDecl>(D);
258
259 // Explicitly declared static.
260 if (Function->getStorageClass() == FunctionDecl::Static)
261 return NamedDecl::InternalLinkage;
262 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
263 // - a data member of an anonymous union.
264 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
265 return NamedDecl::InternalLinkage;
266 }
267
268 // C++ [basic.link]p4:
269
270 // A name having namespace scope has external linkage if it is the
271 // name of
272 //
273 // - an object or reference, unless it has internal linkage; or
274 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
275 if (!Context.getLangOptions().CPlusPlus &&
276 (Var->getStorageClass() == VarDecl::Extern ||
277 Var->getStorageClass() == VarDecl::PrivateExtern)) {
278 // C99 6.2.2p4:
279 // For an identifier declared with the storage-class specifier
280 // extern in a scope in which a prior declaration of that
281 // identifier is visible, if the prior declaration specifies
282 // internal or external linkage, the linkage of the identifier
283 // at the later declaration is the same as the linkage
284 // specified at the prior declaration. If no prior declaration
285 // is visible, or if the prior declaration specifies no
286 // linkage, then the identifier has external linkage.
287 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
288 if (NamedDecl::Linkage L = PrevVar->getLinkage())
289 return L;
290 }
291 }
292
293 // C99 6.2.2p5:
294 // If the declaration of an identifier for an object has file
295 // scope and no storage-class specifier, its linkage is
296 // external.
297 return NamedDecl::ExternalLinkage;
298 }
299
300 // - a function, unless it has internal linkage; or
301 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
302 // C99 6.2.2p5:
303 // If the declaration of an identifier for a function has no
304 // storage-class specifier, its linkage is determined exactly
305 // as if it were declared with the storage-class specifier
306 // extern.
307 if (!Context.getLangOptions().CPlusPlus &&
308 (Function->getStorageClass() == FunctionDecl::Extern ||
309 Function->getStorageClass() == FunctionDecl::PrivateExtern ||
310 Function->getStorageClass() == FunctionDecl::None)) {
311 // C99 6.2.2p4:
312 // For an identifier declared with the storage-class specifier
313 // extern in a scope in which a prior declaration of that
314 // identifier is visible, if the prior declaration specifies
315 // internal or external linkage, the linkage of the identifier
316 // at the later declaration is the same as the linkage
317 // specified at the prior declaration. If no prior declaration
318 // is visible, or if the prior declaration specifies no
319 // linkage, then the identifier has external linkage.
320 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
321 if (NamedDecl::Linkage L = PrevFunc->getLinkage())
322 return L;
323 }
324 }
325
326 return NamedDecl::ExternalLinkage;
327 }
328
329 // - a named class (Clause 9), or an unnamed class defined in a
330 // typedef declaration in which the class has the typedef name
331 // for linkage purposes (7.1.3); or
332 // - a named enumeration (7.2), or an unnamed enumeration
333 // defined in a typedef declaration in which the enumeration
334 // has the typedef name for linkage purposes (7.1.3); or
335 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
336 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
337 return NamedDecl::ExternalLinkage;
338
339 // - an enumerator belonging to an enumeration with external linkage;
340 if (isa<EnumConstantDecl>(D))
341 if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
342 == NamedDecl::ExternalLinkage)
343 return NamedDecl::ExternalLinkage;
344
345 // - a template, unless it is a function template that has
346 // internal linkage (Clause 14);
347 if (isa<TemplateDecl>(D))
348 return NamedDecl::ExternalLinkage;
349
350 // - a namespace (7.3), unless it is declared within an unnamed
351 // namespace.
352 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
353 return NamedDecl::ExternalLinkage;
354
355 return NamedDecl::NoLinkage;
356}
357
358NamedDecl::Linkage NamedDecl::getLinkage() const {
359 // Handle linkage for namespace-scope names.
360 if (getDeclContext()->getLookupContext()->isFileContext())
361 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
362 return L;
363
364 // C++ [basic.link]p5:
365 // In addition, a member function, static data member, a named
366 // class or enumeration of class scope, or an unnamed class or
367 // enumeration defined in a class-scope typedef declaration such
368 // that the class or enumeration has the typedef name for linkage
369 // purposes (7.1.3), has external linkage if the name of the class
370 // has external linkage.
371 if (getDeclContext()->isRecord() &&
372 (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
373 (isa<TagDecl>(this) &&
374 (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
375 cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
376 return ExternalLinkage;
377
378 // C++ [basic.link]p6:
379 // The name of a function declared in block scope and the name of
380 // an object declared by a block scope extern declaration have
381 // linkage. If there is a visible declaration of an entity with
382 // linkage having the same name and type, ignoring entities
383 // declared outside the innermost enclosing namespace scope, the
384 // block scope declaration declares that same entity and receives
385 // the linkage of the previous declaration. If there is more than
386 // one such matching entity, the program is ill-formed. Otherwise,
387 // if no matching entity is found, the block scope entity receives
388 // external linkage.
389 if (getLexicalDeclContext()->isFunctionOrMethod()) {
390 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
391 if (Function->getPreviousDeclaration())
392 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
393 return L;
394
395 return ExternalLinkage;
396 }
397
398 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
399 if (Var->getStorageClass() == VarDecl::Extern ||
400 Var->getStorageClass() == VarDecl::PrivateExtern) {
401 if (Var->getPreviousDeclaration())
402 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
403 return L;
404
405 return ExternalLinkage;
406 }
407 }
408
409 // C++ [basic.link]p6:
410 // Names not covered by these rules have no linkage.
411 return NoLinkage;
412}
413
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000414std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000415 return getQualifiedNameAsString(getASTContext().getLangOptions());
416}
417
418std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000419 // FIXME: Collect contexts, then accumulate names to avoid unnecessary
420 // std::string thrashing.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000421 std::vector<std::string> Names;
422 std::string QualName;
423 const DeclContext *Ctx = getDeclContext();
424
425 if (Ctx->isFunctionOrMethod())
426 return getNameAsString();
427
428 while (Ctx) {
429 if (Ctx->isFunctionOrMethod())
430 // FIXME: That probably will happen, when D was member of local
431 // scope class/struct/union. How do we handle this case?
432 break;
433
Mike Stump1eb44332009-09-09 15:08:12 +0000434 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000435 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
436 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
437 std::string TemplateArgsStr
438 = TemplateSpecializationType::PrintTemplateArgumentList(
439 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000440 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000441 P);
Daniel Dunbare013d682009-10-18 20:26:12 +0000442 Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000443 } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000444 Names.push_back(ND->getNameAsString());
445 else
446 break;
447
448 Ctx = Ctx->getParent();
449 }
450
451 std::vector<std::string>::reverse_iterator
452 I = Names.rbegin(),
453 End = Names.rend();
454
455 for (; I!=End; ++I)
456 QualName += *I + "::";
457
458 QualName += getNameAsString();
459
460 return QualName;
461}
462
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000463bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000464 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
465
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000466 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
467 // We want to keep it, unless it nominates same namespace.
468 if (getKind() == Decl::UsingDirective) {
469 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
470 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000473 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
474 // For function declarations, we keep track of redeclarations.
475 return FD->getPreviousDeclaration() == OldD;
476
Douglas Gregore53060f2009-06-25 22:08:12 +0000477 // For function templates, the underlying function declarations are linked.
478 if (const FunctionTemplateDecl *FunctionTemplate
479 = dyn_cast<FunctionTemplateDecl>(this))
480 if (const FunctionTemplateDecl *OldFunctionTemplate
481 = dyn_cast<FunctionTemplateDecl>(OldD))
482 return FunctionTemplate->getTemplatedDecl()
483 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Steve Naroff0de21fd2009-02-22 19:35:57 +0000485 // For method declarations, we keep track of redeclarations.
486 if (isa<ObjCMethodDecl>(this))
487 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
John McCallf36e02d2009-10-09 21:13:30 +0000489 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
490 return true;
491
John McCall9488ea12009-11-17 05:59:44 +0000492 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
493 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
494 cast<UsingShadowDecl>(OldD)->getTargetDecl();
495
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000496 // For non-function declarations, if the declarations are of the
497 // same kind then this must be a redeclaration, or semantic analysis
498 // would not have given us the new declaration.
499 return this->getKind() == OldD->getKind();
500}
501
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000502bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000503 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000504}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000505
Anders Carlssone136e0e2009-06-26 06:29:23 +0000506NamedDecl *NamedDecl::getUnderlyingDecl() {
507 NamedDecl *ND = this;
508 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000509 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000510 ND = UD->getTargetDecl();
511 else if (ObjCCompatibleAliasDecl *AD
512 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
513 return AD->getClassInterface();
514 else
515 return ND;
516 }
517}
518
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000519//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000520// DeclaratorDecl Implementation
521//===----------------------------------------------------------------------===//
522
523SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall51bd8032009-10-18 01:05:36 +0000524 if (DeclInfo) {
525 TypeLoc TL = DeclInfo->getTypeLoc();
526 while (true) {
527 TypeLoc NextTL = TL.getNextTypeLoc();
528 if (!NextTL)
529 return TL.getSourceRange().getBegin();
530 TL = NextTL;
531 }
532 }
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000533 return SourceLocation();
534}
535
536//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000537// VarDecl Implementation
538//===----------------------------------------------------------------------===//
539
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000540VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000541 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000542 StorageClass S) {
John McCalla93c9342009-12-07 02:54:59 +0000543 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000544}
545
546void VarDecl::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000547 Expr *Init = getInit();
Douglas Gregor78d15832009-05-26 18:54:04 +0000548 if (Init) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +0000549 Init->Destroy(C);
Douglas Gregor78d15832009-05-26 18:54:04 +0000550 if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
551 Eval->~EvaluatedStmt();
552 C.Deallocate(Eval);
553 }
554 }
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000555 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000556 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000557}
558
559VarDecl::~VarDecl() {
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000560}
561
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000562SourceRange VarDecl::getSourceRange() const {
563 if (getInit())
564 return SourceRange(getLocation(), getInit()->getLocEnd());
565 return SourceRange(getLocation(), getLocation());
566}
567
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000568bool VarDecl::isOutOfLine() const {
569 if (!isStaticDataMember())
570 return false;
571
572 if (Decl::isOutOfLine())
573 return true;
574
575 // If this static data member was instantiated from a static data member of
576 // a class template, check whether that static data member was defined
577 // out-of-line.
578 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
579 return VD->isOutOfLine();
580
581 return false;
582}
583
Douglas Gregor0d035142009-10-27 18:42:08 +0000584VarDecl *VarDecl::getOutOfLineDefinition() {
585 if (!isStaticDataMember())
586 return 0;
587
588 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
589 RD != RDEnd; ++RD) {
590 if (RD->getLexicalDeclContext()->isFileContext())
591 return *RD;
592 }
593
594 return 0;
595}
596
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000597VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000598 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000599 return cast<VarDecl>(MSI->getInstantiatedFrom());
600
601 return 0;
602}
603
Douglas Gregor663b5a02009-10-14 20:14:33 +0000604TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000605 if (MemberSpecializationInfo *MSI
606 = getASTContext().getInstantiatedFromStaticDataMember(this))
607 return MSI->getTemplateSpecializationKind();
608
609 return TSK_Undeclared;
610}
611
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000612MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000613 return getASTContext().getInstantiatedFromStaticDataMember(this);
614}
615
Douglas Gregor0a897e32009-10-15 17:21:20 +0000616void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
617 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000618 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000619 assert(MSI && "Not an instantiated static data member?");
620 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000621 if (TSK != TSK_ExplicitSpecialization &&
622 PointOfInstantiation.isValid() &&
623 MSI->getPointOfInstantiation().isInvalid())
624 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000625}
626
Douglas Gregor275a3692009-03-10 23:43:53 +0000627bool VarDecl::isTentativeDefinition(ASTContext &Context) const {
628 if (!isFileVarDecl() || Context.getLangOptions().CPlusPlus)
629 return false;
630
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000631 const VarDecl *Def = 0;
632 return (!getDefinition(Def) &&
Douglas Gregor275a3692009-03-10 23:43:53 +0000633 (getStorageClass() == None || getStorageClass() == Static));
634}
635
Ted Kremenek082d9362009-03-20 21:35:28 +0000636const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000637 redecl_iterator I = redecls_begin(), E = redecls_end();
638 while (I != E && !I->getInit())
639 ++I;
Douglas Gregor275a3692009-03-10 23:43:53 +0000640
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000641 if (I != E) {
642 Def = *I;
643 return I->getInit();
644 }
645 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000646}
647
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000648VarDecl *VarDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000649 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000650}
651
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000652//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000653// FunctionDecl Implementation
654//===----------------------------------------------------------------------===//
655
Ted Kremenek27f8a282008-05-20 00:43:19 +0000656void FunctionDecl::Destroy(ASTContext& C) {
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000657 if (Body && Body.isOffset())
658 Body.get(C.getExternalSource())->Destroy(C);
Ted Kremenekb65cf412008-05-20 03:56:00 +0000659
660 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
661 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000662
Douglas Gregor2db32322009-10-07 23:56:10 +0000663 FunctionTemplateSpecializationInfo *FTSInfo
664 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
665 if (FTSInfo)
666 C.Deallocate(FTSInfo);
667
668 MemberSpecializationInfo *MSInfo
669 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
670 if (MSInfo)
671 C.Deallocate(MSInfo);
672
Steve Naroff3e970492009-01-27 21:25:57 +0000673 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000674
Ted Kremenek27f8a282008-05-20 00:43:19 +0000675 Decl::Destroy(C);
676}
677
John McCall136a6982009-09-11 06:45:03 +0000678void FunctionDecl::getNameForDiagnostic(std::string &S,
679 const PrintingPolicy &Policy,
680 bool Qualified) const {
681 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
682 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
683 if (TemplateArgs)
684 S += TemplateSpecializationType::PrintTemplateArgumentList(
685 TemplateArgs->getFlatArgumentList(),
686 TemplateArgs->flat_size(),
687 Policy);
688
689}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000690
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000691Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000692 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
693 if (I->Body) {
694 Definition = *I;
695 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000696 }
697 }
698
699 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000700}
701
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000702void FunctionDecl::setBody(Stmt *B) {
703 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000704 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000705 EndRangeLoc = B->getLocEnd();
706}
707
Douglas Gregor48a83b52009-09-12 00:17:51 +0000708bool FunctionDecl::isMain() const {
709 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000710 return !Context.getLangOptions().Freestanding &&
711 getDeclContext()->getLookupContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000712 getIdentifier() && getIdentifier()->isStr("main");
713}
714
Douglas Gregor48a83b52009-09-12 00:17:51 +0000715bool FunctionDecl::isExternC() const {
716 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000717 // In C, any non-static, non-overloadable function has external
718 // linkage.
719 if (!Context.getLangOptions().CPlusPlus)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000720 return getStorageClass() != Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000721
Mike Stump1eb44332009-09-09 15:08:12 +0000722 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000723 DC = DC->getParent()) {
724 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
725 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
Mike Stump1eb44332009-09-09 15:08:12 +0000726 return getStorageClass() != Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000727 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000728
729 break;
730 }
731 }
732
733 return false;
734}
735
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000736bool FunctionDecl::isGlobal() const {
737 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
738 return Method->isStatic();
739
740 if (getStorageClass() == Static)
741 return false;
742
Mike Stump1eb44332009-09-09 15:08:12 +0000743 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000744 DC->isNamespace();
745 DC = DC->getParent()) {
746 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
747 if (!Namespace->getDeclName())
748 return false;
749 break;
750 }
751 }
752
753 return true;
754}
755
Douglas Gregor3e41d602009-02-13 23:20:09 +0000756/// \brief Returns a value indicating whether this function
757/// corresponds to a builtin function.
758///
759/// The function corresponds to a built-in function if it is
760/// declared at translation scope or within an extern "C" block and
761/// its name matches with the name of a builtin. The returned value
762/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +0000763/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +0000764/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000765unsigned FunctionDecl::getBuiltinID() const {
766 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +0000767 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
768 return 0;
769
770 unsigned BuiltinID = getIdentifier()->getBuiltinID();
771 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
772 return BuiltinID;
773
774 // This function has the name of a known C library
775 // function. Determine whether it actually refers to the C library
776 // function or whether it just has the same name.
777
Douglas Gregor9add3172009-02-17 03:23:10 +0000778 // If this is a static function, it's not a builtin.
779 if (getStorageClass() == Static)
780 return 0;
781
Douglas Gregor3c385e52009-02-14 18:57:46 +0000782 // If this function is at translation-unit scope and we're not in
783 // C++, it refers to the C library function.
784 if (!Context.getLangOptions().CPlusPlus &&
785 getDeclContext()->isTranslationUnit())
786 return BuiltinID;
787
788 // If the function is in an extern "C" linkage specification and is
789 // not marked "overloadable", it's the real function.
790 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000791 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +0000792 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000793 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +0000794 return BuiltinID;
795
796 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +0000797 return 0;
798}
799
800
Chris Lattner1ad9b282009-04-25 06:03:53 +0000801/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +0000802/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +0000803/// after it has been created.
804unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +0000805 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +0000806 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000807 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000808 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810}
811
Ted Kremenekfc767612009-01-14 00:42:25 +0000812void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
813 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +0000815 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 // Zero params -> null pointer.
818 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000819 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000820 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000822
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +0000823 // Update source range. The check below allows us to set EndRangeLoc before
824 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +0000825 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000826 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 }
828}
829
Chris Lattner8123a952008-04-10 02:22:51 +0000830/// getMinRequiredArguments - Returns the minimum number of arguments
831/// needed to call this function. This may be fewer than the number of
832/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000833/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000834unsigned FunctionDecl::getMinRequiredArguments() const {
835 unsigned NumRequiredArgs = getNumParams();
836 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000837 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +0000838 --NumRequiredArgs;
839
840 return NumRequiredArgs;
841}
842
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000843bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000844 // FIXME: This is not enough. Consider:
845 //
846 // inline void f();
847 // void f() { }
848 //
849 // f is inlined, but does not have inline specified.
850 // To fix this we should add an 'inline' flag to FunctionDecl.
851 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000852 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +0000853
854 if (isa<CXXMethodDecl>(this)) {
855 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
856 return true;
857 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000858
859 switch (getTemplateSpecializationKind()) {
860 case TSK_Undeclared:
861 case TSK_ExplicitSpecialization:
862 return false;
863
864 case TSK_ImplicitInstantiation:
865 case TSK_ExplicitInstantiationDeclaration:
866 case TSK_ExplicitInstantiationDefinition:
867 // Handle below.
868 break;
869 }
870
871 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
872 Stmt *Pattern = 0;
873 if (PatternDecl)
874 Pattern = PatternDecl->getBody(PatternDecl);
875
876 if (Pattern && PatternDecl)
877 return PatternDecl->isInlined();
878
879 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000880}
881
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000882/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000883/// definition will be externally visible.
884///
885/// Inline function definitions are always available for inlining optimizations.
886/// However, depending on the language dialect, declaration specifiers, and
887/// attributes, the definition of an inline function may or may not be
888/// "externally" visible to other translation units in the program.
889///
890/// In C99, inline definitions are not externally visible by default. However,
891/// if even one of the globa-scope declarations is marked "extern inline", the
892/// inline definition becomes externally visible (C99 6.7.4p6).
893///
894/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
895/// definition, we use the GNU semantics for inline, which are nearly the
896/// opposite of C99 semantics. In particular, "inline" by itself will create
897/// an externally visible symbol, but "extern inline" will not create an
898/// externally visible symbol.
899bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
900 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +0000901 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000902 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000903
Douglas Gregor7d9c3c92009-10-27 23:26:40 +0000904 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000905 // GNU inline semantics. Based on a number of examples, we came up with the
906 // following heuristic: if the "inline" keyword is present on a
907 // declaration of the function but "extern" is not present on that
908 // declaration, then the symbol is externally visible. Otherwise, the GNU
909 // "extern inline" semantics applies and the symbol is not externally
910 // visible.
911 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
912 Redecl != RedeclEnd;
913 ++Redecl) {
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000914 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000915 return true;
916 }
917
918 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000919 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000920 }
921
922 // C99 6.7.4p6:
923 // [...] If all of the file scope declarations for a function in a
924 // translation unit include the inline function specifier without extern,
925 // then the definition in that translation unit is an inline definition.
926 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
927 Redecl != RedeclEnd;
928 ++Redecl) {
929 // Only consider file-scope declarations in this test.
930 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
931 continue;
932
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000933 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +0000934 return true; // Not an inline definition
935 }
936
937 // C99 6.7.4p6:
938 // An inline definition does not provide an external definition for the
939 // function, and does not forbid an external definition in another
940 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +0000941 return false;
942}
943
Mike Stump1eb44332009-09-09 15:08:12 +0000944void
Douglas Gregor127102b2009-06-29 20:59:39 +0000945FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000946 redeclarable_base::setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000947
Douglas Gregor127102b2009-06-29 20:59:39 +0000948 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000949 FunctionTemplateDecl *PrevFunTmpl
Douglas Gregor127102b2009-06-29 20:59:39 +0000950 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
951 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
952 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
953 }
954}
955
Mike Stump740256b2009-09-29 00:50:50 +0000956const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
957 return getFirstDeclaration();
958}
959
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000960FunctionDecl *FunctionDecl::getCanonicalDecl() {
Argyrios Kyrtzidisf23e8392009-07-18 08:50:13 +0000961 return getFirstDeclaration();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000962}
963
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000964/// getOverloadedOperator - Which C++ overloaded operator this
965/// function represents, if any.
966OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000967 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
968 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000969 else
970 return OO_None;
971}
972
Douglas Gregor2db32322009-10-07 23:56:10 +0000973FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000974 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +0000975 return cast<FunctionDecl>(Info->getInstantiatedFrom());
976
977 return 0;
978}
979
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000980MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
981 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
982}
983
Douglas Gregor2db32322009-10-07 23:56:10 +0000984void
985FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
986 TemplateSpecializationKind TSK) {
987 assert(TemplateOrSpecialization.isNull() &&
988 "Member function is already a specialization");
989 MemberSpecializationInfo *Info
990 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
991 TemplateOrSpecialization = Info;
992}
993
Douglas Gregor3b846b62009-10-27 20:53:28 +0000994bool FunctionDecl::isImplicitlyInstantiable() const {
995 // If this function already has a definition or is invalid, it can't be
996 // implicitly instantiated.
997 if (isInvalidDecl() || getBody())
998 return false;
999
1000 switch (getTemplateSpecializationKind()) {
1001 case TSK_Undeclared:
1002 case TSK_ExplicitSpecialization:
1003 case TSK_ExplicitInstantiationDefinition:
1004 return false;
1005
1006 case TSK_ImplicitInstantiation:
1007 return true;
1008
1009 case TSK_ExplicitInstantiationDeclaration:
1010 // Handled below.
1011 break;
1012 }
1013
1014 // Find the actual template from which we will instantiate.
1015 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
1016 Stmt *Pattern = 0;
1017 if (PatternDecl)
1018 Pattern = PatternDecl->getBody(PatternDecl);
1019
1020 // C++0x [temp.explicit]p9:
1021 // Except for inline functions, other explicit instantiation declarations
1022 // have the effect of suppressing the implicit instantiation of the entity
1023 // to which they refer.
1024 if (!Pattern || !PatternDecl)
1025 return true;
1026
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001027 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001028}
1029
1030FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1031 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1032 while (Primary->getInstantiatedFromMemberTemplate()) {
1033 // If we have hit a point where the user provided a specialization of
1034 // this template, we're done looking.
1035 if (Primary->isMemberSpecialization())
1036 break;
1037
1038 Primary = Primary->getInstantiatedFromMemberTemplate();
1039 }
1040
1041 return Primary->getTemplatedDecl();
1042 }
1043
1044 return getInstantiatedFromMemberFunction();
1045}
1046
Douglas Gregor16e8be22009-06-29 17:30:29 +00001047FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001048 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001049 = TemplateOrSpecialization
1050 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001051 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001052 }
1053 return 0;
1054}
1055
1056const TemplateArgumentList *
1057FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001058 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001059 = TemplateOrSpecialization
1060 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001061 return Info->TemplateArguments;
1062 }
1063 return 0;
1064}
1065
Mike Stump1eb44332009-09-09 15:08:12 +00001066void
Douglas Gregor1637be72009-06-26 00:10:03 +00001067FunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
1068 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001069 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001070 void *InsertPos,
1071 TemplateSpecializationKind TSK) {
1072 assert(TSK != TSK_Undeclared &&
1073 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001074 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001075 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001076 if (!Info)
Douglas Gregor16e8be22009-06-29 17:30:29 +00001077 Info = new (Context) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Douglas Gregor127102b2009-06-29 20:59:39 +00001079 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001080 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001081 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001082 Info->TemplateArguments = TemplateArgs;
1083 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Douglas Gregor127102b2009-06-29 20:59:39 +00001085 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001086 // function template specializations.
1087 if (InsertPos)
1088 Template->getSpecializations().InsertNode(Info, InsertPos);
1089 else {
1090 // Try to insert the new node. If there is an existing node, remove it
1091 // first.
1092 FunctionTemplateSpecializationInfo *Existing
1093 = Template->getSpecializations().GetOrInsertNode(Info);
1094 if (Existing) {
1095 Template->getSpecializations().RemoveNode(Existing);
1096 Template->getSpecializations().GetOrInsertNode(Info);
1097 }
1098 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001099}
1100
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001101TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001102 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001103 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001104 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001105 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001106 if (FTSInfo)
1107 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Douglas Gregor2db32322009-10-07 23:56:10 +00001109 MemberSpecializationInfo *MSInfo
1110 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1111 if (MSInfo)
1112 return MSInfo->getTemplateSpecializationKind();
1113
1114 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001115}
1116
Mike Stump1eb44332009-09-09 15:08:12 +00001117void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001118FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1119 SourceLocation PointOfInstantiation) {
1120 if (FunctionTemplateSpecializationInfo *FTSInfo
1121 = TemplateOrSpecialization.dyn_cast<
1122 FunctionTemplateSpecializationInfo*>()) {
1123 FTSInfo->setTemplateSpecializationKind(TSK);
1124 if (TSK != TSK_ExplicitSpecialization &&
1125 PointOfInstantiation.isValid() &&
1126 FTSInfo->getPointOfInstantiation().isInvalid())
1127 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1128 } else if (MemberSpecializationInfo *MSInfo
1129 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1130 MSInfo->setTemplateSpecializationKind(TSK);
1131 if (TSK != TSK_ExplicitSpecialization &&
1132 PointOfInstantiation.isValid() &&
1133 MSInfo->getPointOfInstantiation().isInvalid())
1134 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1135 } else
1136 assert(false && "Function cannot have a template specialization kind");
1137}
1138
1139SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001140 if (FunctionTemplateSpecializationInfo *FTSInfo
1141 = TemplateOrSpecialization.dyn_cast<
1142 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001143 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001144 else if (MemberSpecializationInfo *MSInfo
1145 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001146 return MSInfo->getPointOfInstantiation();
1147
1148 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001149}
1150
Douglas Gregor9f185072009-09-11 20:15:17 +00001151bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001152 if (Decl::isOutOfLine())
1153 return true;
1154
1155 // If this function was instantiated from a member function of a
1156 // class template, check whether that member function was defined out-of-line.
1157 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1158 const FunctionDecl *Definition;
1159 if (FD->getBody(Definition))
1160 return Definition->isOutOfLine();
1161 }
1162
1163 // If this function was instantiated from a function template,
1164 // check whether that function template was defined out-of-line.
1165 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1166 const FunctionDecl *Definition;
1167 if (FunTmpl->getTemplatedDecl()->getBody(Definition))
1168 return Definition->isOutOfLine();
1169 }
1170
1171 return false;
1172}
1173
Chris Lattner8a934232008-03-31 00:36:02 +00001174//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001175// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001176//===----------------------------------------------------------------------===//
1177
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001178SourceRange TagDecl::getSourceRange() const {
1179 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001180 return SourceRange(TagKeywordLoc, E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001181}
1182
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001183TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001184 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001185}
1186
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001187void TagDecl::startDefinition() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001188 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1189 TagT->decl.setPointer(this);
1190 TagT->decl.setInt(1);
1191 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001192}
1193
1194void TagDecl::completeDefinition() {
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001195 IsDefinition = true;
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001196 if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
1197 assert(TagT->decl.getPointer() == this &&
1198 "Attempt to redefine a tag definition?");
1199 TagT->decl.setInt(0);
1200 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001201}
1202
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001203TagDecl* TagDecl::getDefinition(ASTContext& C) const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001204 if (isDefinition())
1205 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001206
1207 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001208 R != REnd; ++R)
1209 if (R->isDefinition())
1210 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001212 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001213}
1214
John McCallf1bbbb42009-09-04 01:14:41 +00001215TagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1216 switch (TypeSpec) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001217 default: llvm_unreachable("unexpected type specifier");
John McCallf1bbbb42009-09-04 01:14:41 +00001218 case DeclSpec::TST_struct: return TK_struct;
1219 case DeclSpec::TST_class: return TK_class;
1220 case DeclSpec::TST_union: return TK_union;
1221 case DeclSpec::TST_enum: return TK_enum;
1222 }
1223}
1224
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001225//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001226// RecordDecl Implementation
1227//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001228
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001229RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001230 IdentifierInfo *Id, RecordDecl *PrevDecl,
1231 SourceLocation TKL)
1232 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001233 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001234 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001235 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001236 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001237}
1238
1239RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001240 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001241 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001243 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001244 C.getTypeDeclType(R, PrevDecl);
1245 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001246}
1247
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001248RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001249}
1250
1251void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +00001252 TagDecl::Destroy(C);
1253}
1254
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001255bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001256 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001257 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1258}
1259
Douglas Gregor44b43212008-12-11 16:49:14 +00001260/// completeDefinition - Notes that the definition of this type is now
1261/// complete.
1262void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001264 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001265}
1266
Steve Naroff56ee6892008-10-08 17:01:13 +00001267//===----------------------------------------------------------------------===//
1268// BlockDecl Implementation
1269//===----------------------------------------------------------------------===//
1270
1271BlockDecl::~BlockDecl() {
1272}
1273
1274void BlockDecl::Destroy(ASTContext& C) {
1275 if (Body)
1276 Body->Destroy(C);
1277
1278 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
1279 (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001280
1281 C.Deallocate(ParamInfo);
Steve Naroff56ee6892008-10-08 17:01:13 +00001282 Decl::Destroy(C);
1283}
Steve Naroffe78b8092009-03-13 16:56:44 +00001284
1285void BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1286 unsigned NParms) {
1287 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Steve Naroffe78b8092009-03-13 16:56:44 +00001289 // Zero params -> null pointer.
1290 if (NParms) {
1291 NumParams = NParms;
1292 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1293 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1294 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1295 }
1296}
1297
1298unsigned BlockDecl::getNumParams() const {
1299 return NumParams;
1300}