blob: b7be02d74533e8bdba82ffb31553a7c2306da79e [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"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000026#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000027#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattnerd3b90652008-03-15 05:43:15 +000031//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000032// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000033//===----------------------------------------------------------------------===//
34
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000035/// \brief Get the most restrictive linkage for the types in the given
36/// template parameter list.
37static Linkage
38getLinkageForTemplateParameterList(const TemplateParameterList *Params) {
39 Linkage L = ExternalLinkage;
40 for (TemplateParameterList::const_iterator P = Params->begin(),
41 PEnd = Params->end();
42 P != PEnd; ++P) {
43 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
44 if (!NTTP->getType()->isDependentType()) {
45 L = minLinkage(L, NTTP->getType()->getLinkage());
46 continue;
47 }
48
49 if (TemplateTemplateParmDecl *TTP
50 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
51 L = minLinkage(L,
52 getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
53 }
54 }
55
56 return L;
57}
58
59/// \brief Get the most restrictive linkage for the types and
60/// declarations in the given template argument list.
61static Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
62 unsigned NumArgs) {
63 Linkage L = ExternalLinkage;
64
65 for (unsigned I = 0; I != NumArgs; ++I) {
66 switch (Args[I].getKind()) {
67 case TemplateArgument::Null:
68 case TemplateArgument::Integral:
69 case TemplateArgument::Expression:
70 break;
71
72 case TemplateArgument::Type:
73 L = minLinkage(L, Args[I].getAsType()->getLinkage());
74 break;
75
76 case TemplateArgument::Declaration:
77 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
78 L = minLinkage(L, ND->getLinkage());
79 if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
80 L = minLinkage(L, VD->getType()->getLinkage());
81 break;
82
83 case TemplateArgument::Template:
84 if (TemplateDecl *Template
85 = Args[I].getAsTemplate().getAsTemplateDecl())
86 L = minLinkage(L, Template->getLinkage());
87 break;
88
89 case TemplateArgument::Pack:
90 L = minLinkage(L,
91 getLinkageForTemplateArgumentList(Args[I].pack_begin(),
92 Args[I].pack_size()));
93 break;
94 }
95 }
96
97 return L;
98}
99
John McCall3cdfc4d2010-08-13 08:35:10 +0000100static Linkage
101getLinkageForTemplateArgumentList(const TemplateArgumentList &TArgs) {
102 return getLinkageForTemplateArgumentList(TArgs.getFlatArgumentList(),
103 TArgs.flat_size());
104}
105
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000106static Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000107 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000108 "Not a name having namespace scope");
109 ASTContext &Context = D->getASTContext();
110
111 // C++ [basic.link]p3:
112 // A name having namespace scope (3.3.6) has internal linkage if it
113 // is the name of
114 // - an object, reference, function or function template that is
115 // explicitly declared static; or,
116 // (This bullet corresponds to C99 6.2.2p3.)
117 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
118 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000119 if (Var->getStorageClass() == SC_Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000120 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000121
122 // - an object or reference that is explicitly declared const
123 // and neither explicitly declared extern nor previously
124 // declared to have external linkage; or
125 // (there is no equivalent in C99)
126 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000127 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000128 Var->getStorageClass() != SC_Extern &&
129 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000130 bool FoundExtern = false;
131 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
132 PrevVar && !FoundExtern;
133 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000134 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000135 FoundExtern = true;
136
137 if (!FoundExtern)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000138 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000139 }
140 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000141 // C++ [temp]p4:
142 // A non-member function template can have internal linkage; any
143 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000144 const FunctionDecl *Function = 0;
145 if (const FunctionTemplateDecl *FunTmpl
146 = dyn_cast<FunctionTemplateDecl>(D))
147 Function = FunTmpl->getTemplatedDecl();
148 else
149 Function = cast<FunctionDecl>(D);
150
151 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000152 if (Function->getStorageClass() == SC_Static)
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000153 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000154 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
155 // - a data member of an anonymous union.
156 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000157 return InternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000158 }
159
160 // C++ [basic.link]p4:
161
162 // A name having namespace scope has external linkage if it is the
163 // name of
164 //
165 // - an object or reference, unless it has internal linkage; or
166 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
167 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000168 (Var->getStorageClass() == SC_Extern ||
169 Var->getStorageClass() == SC_PrivateExtern)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000170 // C99 6.2.2p4:
171 // For an identifier declared with the storage-class specifier
172 // extern in a scope in which a prior declaration of that
173 // identifier is visible, if the prior declaration specifies
174 // internal or external linkage, the linkage of the identifier
175 // at the later declaration is the same as the linkage
176 // specified at the prior declaration. If no prior declaration
177 // is visible, or if the prior declaration specifies no
178 // linkage, then the identifier has external linkage.
179 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000180 if (Linkage L = PrevVar->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000181 return L;
182 }
183 }
184
185 // C99 6.2.2p5:
186 // If the declaration of an identifier for an object has file
187 // scope and no storage-class specifier, its linkage is
188 // external.
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000189 if (Var->isInAnonymousNamespace())
190 return UniqueExternalLinkage;
191
192 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000193 }
194
195 // - a function, unless it has internal linkage; or
196 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
197 // C99 6.2.2p5:
198 // If the declaration of an identifier for a function has no
199 // storage-class specifier, its linkage is determined exactly
200 // as if it were declared with the storage-class specifier
201 // extern.
202 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000203 (Function->getStorageClass() == SC_Extern ||
204 Function->getStorageClass() == SC_PrivateExtern ||
205 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000206 // C99 6.2.2p4:
207 // For an identifier declared with the storage-class specifier
208 // extern in a scope in which a prior declaration of that
209 // identifier is visible, if the prior declaration specifies
210 // internal or external linkage, the linkage of the identifier
211 // at the later declaration is the same as the linkage
212 // specified at the prior declaration. If no prior declaration
213 // is visible, or if the prior declaration specifies no
214 // linkage, then the identifier has external linkage.
215 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000216 if (Linkage L = PrevFunc->getLinkage())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000217 return L;
218 }
219 }
220
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000221 if (Function->isInAnonymousNamespace())
222 return UniqueExternalLinkage;
223
224 if (FunctionTemplateSpecializationInfo *SpecInfo
225 = Function->getTemplateSpecializationInfo()) {
226 Linkage L = SpecInfo->getTemplate()->getLinkage();
227 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall3cdfc4d2010-08-13 08:35:10 +0000228 L = minLinkage(L, getLinkageForTemplateArgumentList(TemplateArgs));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000229 return L;
230 }
231
232 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000233 }
234
235 // - a named class (Clause 9), or an unnamed class defined in a
236 // typedef declaration in which the class has the typedef name
237 // for linkage purposes (7.1.3); or
238 // - a named enumeration (7.2), or an unnamed enumeration
239 // defined in a typedef declaration in which the enumeration
240 // has the typedef name for linkage purposes (7.1.3); or
241 if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000242 if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
243 if (Tag->isInAnonymousNamespace())
244 return UniqueExternalLinkage;
245
246 // If this is a class template specialization, consider the
247 // linkage of the template and template arguments.
248 if (const ClassTemplateSpecializationDecl *Spec
249 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
250 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
John McCall3cdfc4d2010-08-13 08:35:10 +0000251 Linkage L = getLinkageForTemplateArgumentList(TemplateArgs);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000252 return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
253 }
254
255 return ExternalLinkage;
256 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000257
258 // - an enumerator belonging to an enumeration with external linkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000259 if (isa<EnumConstantDecl>(D)) {
260 Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
261 if (isExternalLinkage(L))
262 return L;
263 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000264
265 // - a template, unless it is a function template that has
266 // internal linkage (Clause 14);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000267 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
268 if (D->isInAnonymousNamespace())
269 return UniqueExternalLinkage;
270
271 return getLinkageForTemplateParameterList(
272 Template->getTemplateParameters());
273 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000274
275 // - a namespace (7.3), unless it is declared within an unnamed
276 // namespace.
277 if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000278 return ExternalLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000279
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000280 return NoLinkage;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000281}
282
John McCall3cdfc4d2010-08-13 08:35:10 +0000283static Linkage getLinkageForClassMember(const NamedDecl *D) {
284 if (!(isa<CXXMethodDecl>(D) ||
285 isa<VarDecl>(D) ||
286 (isa<TagDecl>(D) &&
287 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
288 return NoLinkage;
289
290 // Class members only have linkage if their class has external linkage.
291 Linkage L = cast<RecordDecl>(D->getDeclContext())->getLinkage();
292 if (!isExternalLinkage(L)) return NoLinkage;
293
294 // If the class already has unique-external linkage, we can't improve.
295 if (L == UniqueExternalLinkage) return UniqueExternalLinkage;
296
297 // If this is a method template specialization, use the linkage for
298 // the template parameters and arguments.
299 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
300 if (FunctionTemplateSpecializationInfo *SpecInfo
301 = MD->getTemplateSpecializationInfo()) {
302 Linkage ArgLinkage =
303 getLinkageForTemplateArgumentList(*SpecInfo->TemplateArguments);
304 Linkage ParamLinkage =
305 getLinkageForTemplateParameterList(
306 SpecInfo->getTemplate()->getTemplateParameters());
307 return minLinkage(ArgLinkage, ParamLinkage);
308 }
309
310 // Similarly for member class template specializations.
311 } else if (const ClassTemplateSpecializationDecl *Spec
312 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
313 Linkage ArgLinkage =
314 getLinkageForTemplateArgumentList(Spec->getTemplateArgs());
315 Linkage ParamLinkage =
316 getLinkageForTemplateParameterList(
317 Spec->getSpecializedTemplate()->getTemplateParameters());
318 return minLinkage(ArgLinkage, ParamLinkage);
319 }
320
321 return ExternalLinkage;
322}
323
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000324Linkage NamedDecl::getLinkage() const {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000325
326 // Objective-C: treat all Objective-C declarations as having external
327 // linkage.
328 switch (getKind()) {
329 default:
330 break;
331 case Decl::ObjCAtDefsField:
332 case Decl::ObjCCategory:
333 case Decl::ObjCCategoryImpl:
334 case Decl::ObjCClass:
335 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000336 case Decl::ObjCForwardProtocol:
337 case Decl::ObjCImplementation:
338 case Decl::ObjCInterface:
339 case Decl::ObjCIvar:
340 case Decl::ObjCMethod:
341 case Decl::ObjCProperty:
342 case Decl::ObjCPropertyImpl:
343 case Decl::ObjCProtocol:
344 return ExternalLinkage;
345 }
346
Douglas Gregord85b5b92009-11-25 22:24:25 +0000347 // Handle linkage for namespace-scope names.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000348 if (getDeclContext()->getRedeclContext()->isFileContext())
Douglas Gregord85b5b92009-11-25 22:24:25 +0000349 if (Linkage L = getLinkageForNamespaceScopeDecl(this))
350 return L;
351
352 // C++ [basic.link]p5:
353 // In addition, a member function, static data member, a named
354 // class or enumeration of class scope, or an unnamed class or
355 // enumeration defined in a class-scope typedef declaration such
356 // that the class or enumeration has the typedef name for linkage
357 // purposes (7.1.3), has external linkage if the name of the class
358 // has external linkage.
John McCall3cdfc4d2010-08-13 08:35:10 +0000359 if (getDeclContext()->isRecord())
360 return getLinkageForClassMember(this);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000361
362 // C++ [basic.link]p6:
363 // The name of a function declared in block scope and the name of
364 // an object declared by a block scope extern declaration have
365 // linkage. If there is a visible declaration of an entity with
366 // linkage having the same name and type, ignoring entities
367 // declared outside the innermost enclosing namespace scope, the
368 // block scope declaration declares that same entity and receives
369 // the linkage of the previous declaration. If there is more than
370 // one such matching entity, the program is ill-formed. Otherwise,
371 // if no matching entity is found, the block scope entity receives
372 // external linkage.
373 if (getLexicalDeclContext()->isFunctionOrMethod()) {
374 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
375 if (Function->getPreviousDeclaration())
376 if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
377 return L;
378
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000379 if (Function->isInAnonymousNamespace())
380 return UniqueExternalLinkage;
381
Douglas Gregord85b5b92009-11-25 22:24:25 +0000382 return ExternalLinkage;
383 }
384
385 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
John McCalld931b082010-08-26 03:08:43 +0000386 if (Var->getStorageClass() == SC_Extern ||
387 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000388 if (Var->getPreviousDeclaration())
389 if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
390 return L;
391
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000392 if (Var->isInAnonymousNamespace())
393 return UniqueExternalLinkage;
394
Douglas Gregord85b5b92009-11-25 22:24:25 +0000395 return ExternalLinkage;
396 }
397 }
398
399 // C++ [basic.link]p6:
400 // Names not covered by these rules have no linkage.
401 return NoLinkage;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000402 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000403
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000404std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000405 return getQualifiedNameAsString(getASTContext().getLangOptions());
406}
407
408std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000409 const DeclContext *Ctx = getDeclContext();
410
411 if (Ctx->isFunctionOrMethod())
412 return getNameAsString();
413
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000414 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
415 ContextsTy Contexts;
416
417 // Collect contexts.
418 while (Ctx && isa<NamedDecl>(Ctx)) {
419 Contexts.push_back(Ctx);
420 Ctx = Ctx->getParent();
421 };
422
423 std::string QualName;
424 llvm::raw_string_ostream OS(QualName);
425
426 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
427 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000428 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000429 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000430 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
431 std::string TemplateArgsStr
432 = TemplateSpecializationType::PrintTemplateArgumentList(
433 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000434 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000435 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000436 OS << Spec->getName() << TemplateArgsStr;
437 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000438 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000439 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000440 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000441 OS << ND;
442 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
443 if (!RD->getIdentifier())
444 OS << "<anonymous " << RD->getKindName() << '>';
445 else
446 OS << RD;
447 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000448 const FunctionProtoType *FT = 0;
449 if (FD->hasWrittenPrototype())
450 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
451
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000452 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000453 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000454 unsigned NumParams = FD->getNumParams();
455 for (unsigned i = 0; i < NumParams; ++i) {
456 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000457 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000458 std::string Param;
459 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000460 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000461 }
462
463 if (FT->isVariadic()) {
464 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000465 OS << ", ";
466 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000467 }
468 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000469 OS << ')';
470 } else {
471 OS << cast<NamedDecl>(*I);
472 }
473 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000474 }
475
John McCall8472af42010-03-16 21:48:18 +0000476 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000477 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000478 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000479 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000480
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000481 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000482}
483
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000484bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000485 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
486
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000487 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
488 // We want to keep it, unless it nominates same namespace.
489 if (getKind() == Decl::UsingDirective) {
490 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
491 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000494 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
495 // For function declarations, we keep track of redeclarations.
496 return FD->getPreviousDeclaration() == OldD;
497
Douglas Gregore53060f2009-06-25 22:08:12 +0000498 // For function templates, the underlying function declarations are linked.
499 if (const FunctionTemplateDecl *FunctionTemplate
500 = dyn_cast<FunctionTemplateDecl>(this))
501 if (const FunctionTemplateDecl *OldFunctionTemplate
502 = dyn_cast<FunctionTemplateDecl>(OldD))
503 return FunctionTemplate->getTemplatedDecl()
504 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Steve Naroff0de21fd2009-02-22 19:35:57 +0000506 // For method declarations, we keep track of redeclarations.
507 if (isa<ObjCMethodDecl>(this))
508 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
John McCallf36e02d2009-10-09 21:13:30 +0000510 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
511 return true;
512
John McCall9488ea12009-11-17 05:59:44 +0000513 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
514 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
515 cast<UsingShadowDecl>(OldD)->getTargetDecl();
516
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000517 // For non-function declarations, if the declarations are of the
518 // same kind then this must be a redeclaration, or semantic analysis
519 // would not have given us the new declaration.
520 return this->getKind() == OldD->getKind();
521}
522
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000523bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000524 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000525}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000526
Anders Carlssone136e0e2009-06-26 06:29:23 +0000527NamedDecl *NamedDecl::getUnderlyingDecl() {
528 NamedDecl *ND = this;
529 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000530 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000531 ND = UD->getTargetDecl();
532 else if (ObjCCompatibleAliasDecl *AD
533 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
534 return AD->getClassInterface();
535 else
536 return ND;
537 }
538}
539
John McCall161755a2010-04-06 21:38:20 +0000540bool NamedDecl::isCXXInstanceMember() const {
541 assert(isCXXClassMember() &&
542 "checking whether non-member is instance member");
543
544 const NamedDecl *D = this;
545 if (isa<UsingShadowDecl>(D))
546 D = cast<UsingShadowDecl>(D)->getTargetDecl();
547
548 if (isa<FieldDecl>(D))
549 return true;
550 if (isa<CXXMethodDecl>(D))
551 return cast<CXXMethodDecl>(D)->isInstance();
552 if (isa<FunctionTemplateDecl>(D))
553 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
554 ->getTemplatedDecl())->isInstance();
555 return false;
556}
557
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000558//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000559// DeclaratorDecl Implementation
560//===----------------------------------------------------------------------===//
561
Douglas Gregor1693e152010-07-06 18:42:40 +0000562template <typename DeclT>
563static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
564 if (decl->getNumTemplateParameterLists() > 0)
565 return decl->getTemplateParameterList(0)->getTemplateLoc();
566 else
567 return decl->getInnerLocStart();
568}
569
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000570SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000571 TypeSourceInfo *TSI = getTypeSourceInfo();
572 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000573 return SourceLocation();
574}
575
John McCallb6217662010-03-15 10:12:16 +0000576void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
577 SourceRange QualifierRange) {
578 if (Qualifier) {
579 // Make sure the extended decl info is allocated.
580 if (!hasExtInfo()) {
581 // Save (non-extended) type source info pointer.
582 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
583 // Allocate external info struct.
584 DeclInfo = new (getASTContext()) ExtInfo;
585 // Restore savedTInfo into (extended) decl info.
586 getExtInfo()->TInfo = savedTInfo;
587 }
588 // Set qualifier info.
589 getExtInfo()->NNS = Qualifier;
590 getExtInfo()->NNSRange = QualifierRange;
591 }
592 else {
593 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
594 assert(QualifierRange.isInvalid());
595 if (hasExtInfo()) {
596 // Save type source info pointer.
597 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
598 // Deallocate the extended decl info.
599 getASTContext().Deallocate(getExtInfo());
600 // Restore savedTInfo into (non-extended) decl info.
601 DeclInfo = savedTInfo;
602 }
603 }
604}
605
Douglas Gregor1693e152010-07-06 18:42:40 +0000606SourceLocation DeclaratorDecl::getOuterLocStart() const {
607 return getTemplateOrInnerLocStart(this);
608}
609
Abramo Bagnara9b934882010-06-12 08:15:14 +0000610void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000611QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
612 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000613 TemplateParameterList **TPLists) {
614 assert((NumTPLists == 0 || TPLists != 0) &&
615 "Empty array of template parameters with positive size!");
616 assert((NumTPLists == 0 || NNS) &&
617 "Nonempty array of template parameters with no qualifier!");
618
619 // Free previous template parameters (if any).
620 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000621 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000622 TemplParamLists = 0;
623 NumTemplParamLists = 0;
624 }
625 // Set info on matched template parameter lists (if any).
626 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000627 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000628 NumTemplParamLists = NumTPLists;
629 for (unsigned i = NumTPLists; i-- > 0; )
630 TemplParamLists[i] = TPLists[i];
631 }
632}
633
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000634//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000635// VarDecl Implementation
636//===----------------------------------------------------------------------===//
637
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000638const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
639 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000640 case SC_None: break;
641 case SC_Auto: return "auto"; break;
642 case SC_Extern: return "extern"; break;
643 case SC_PrivateExtern: return "__private_extern__"; break;
644 case SC_Register: return "register"; break;
645 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000646 }
647
648 assert(0 && "Invalid storage class");
649 return 0;
650}
651
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000652VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000653 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000654 StorageClass S, StorageClass SCAsWritten) {
655 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000656}
657
Douglas Gregor1693e152010-07-06 18:42:40 +0000658SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000659 SourceLocation Start = getTypeSpecStartLoc();
660 if (Start.isInvalid())
661 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000662 return Start;
663}
664
665SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000666 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000667 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
668 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000669}
670
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000671bool VarDecl::isExternC() const {
672 ASTContext &Context = getASTContext();
673 if (!Context.getLangOptions().CPlusPlus)
674 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000675 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000676 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
677
678 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
679 DC = DC->getParent()) {
680 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
681 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000682 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000683
684 break;
685 }
686
687 if (DC->isFunctionOrMethod())
688 return false;
689 }
690
691 return false;
692}
693
694VarDecl *VarDecl::getCanonicalDecl() {
695 return getFirstDeclaration();
696}
697
Sebastian Redle9d12b62010-01-31 22:27:38 +0000698VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
699 // C++ [basic.def]p2:
700 // A declaration is a definition unless [...] it contains the 'extern'
701 // specifier or a linkage-specification and neither an initializer [...],
702 // it declares a static data member in a class declaration [...].
703 // C++ [temp.expl.spec]p15:
704 // An explicit specialization of a static data member of a template is a
705 // definition if the declaration includes an initializer; otherwise, it is
706 // a declaration.
707 if (isStaticDataMember()) {
708 if (isOutOfLine() && (hasInit() ||
709 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
710 return Definition;
711 else
712 return DeclarationOnly;
713 }
714 // C99 6.7p5:
715 // A definition of an identifier is a declaration for that identifier that
716 // [...] causes storage to be reserved for that object.
717 // Note: that applies for all non-file-scope objects.
718 // C99 6.9.2p1:
719 // If the declaration of an identifier for an object has file scope and an
720 // initializer, the declaration is an external definition for the identifier
721 if (hasInit())
722 return Definition;
723 // AST for 'extern "C" int foo;' is annotated with 'extern'.
724 if (hasExternalStorage())
725 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000726
John McCalld931b082010-08-26 03:08:43 +0000727 if (getStorageClassAsWritten() == SC_Extern ||
728 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000729 for (const VarDecl *PrevVar = getPreviousDeclaration();
730 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
731 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
732 return DeclarationOnly;
733 }
734 }
Sebastian Redle9d12b62010-01-31 22:27:38 +0000735 // C99 6.9.2p2:
736 // A declaration of an object that has file scope without an initializer,
737 // and without a storage class specifier or the scs 'static', constitutes
738 // a tentative definition.
739 // No such thing in C++.
740 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
741 return TentativeDefinition;
742
743 // What's left is (in C, block-scope) declarations without initializers or
744 // external storage. These are definitions.
745 return Definition;
746}
747
Sebastian Redle9d12b62010-01-31 22:27:38 +0000748VarDecl *VarDecl::getActingDefinition() {
749 DefinitionKind Kind = isThisDeclarationADefinition();
750 if (Kind != TentativeDefinition)
751 return 0;
752
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000753 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000754 VarDecl *First = getFirstDeclaration();
755 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
756 I != E; ++I) {
757 Kind = (*I)->isThisDeclarationADefinition();
758 if (Kind == Definition)
759 return 0;
760 else if (Kind == TentativeDefinition)
761 LastTentative = *I;
762 }
763 return LastTentative;
764}
765
766bool VarDecl::isTentativeDefinitionNow() const {
767 DefinitionKind Kind = isThisDeclarationADefinition();
768 if (Kind != TentativeDefinition)
769 return false;
770
771 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
772 if ((*I)->isThisDeclarationADefinition() == Definition)
773 return false;
774 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000775 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000776}
777
Sebastian Redl31310a22010-02-01 20:16:42 +0000778VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000779 VarDecl *First = getFirstDeclaration();
780 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
781 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000782 if ((*I)->isThisDeclarationADefinition() == Definition)
783 return *I;
784 }
785 return 0;
786}
787
788const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000789 redecl_iterator I = redecls_begin(), E = redecls_end();
790 while (I != E && !I->getInit())
791 ++I;
792
793 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000794 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000795 return I->getInit();
796 }
797 return 0;
798}
799
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000800bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000801 if (Decl::isOutOfLine())
802 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000803
804 if (!isStaticDataMember())
805 return false;
806
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000807 // If this static data member was instantiated from a static data member of
808 // a class template, check whether that static data member was defined
809 // out-of-line.
810 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
811 return VD->isOutOfLine();
812
813 return false;
814}
815
Douglas Gregor0d035142009-10-27 18:42:08 +0000816VarDecl *VarDecl::getOutOfLineDefinition() {
817 if (!isStaticDataMember())
818 return 0;
819
820 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
821 RD != RDEnd; ++RD) {
822 if (RD->getLexicalDeclContext()->isFileContext())
823 return *RD;
824 }
825
826 return 0;
827}
828
Douglas Gregor838db382010-02-11 01:19:42 +0000829void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000830 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
831 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000832 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000833 }
834
835 Init = I;
836}
837
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000838VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000839 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000840 return cast<VarDecl>(MSI->getInstantiatedFrom());
841
842 return 0;
843}
844
Douglas Gregor663b5a02009-10-14 20:14:33 +0000845TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +0000846 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000847 return MSI->getTemplateSpecializationKind();
848
849 return TSK_Undeclared;
850}
851
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000852MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000853 return getASTContext().getInstantiatedFromStaticDataMember(this);
854}
855
Douglas Gregor0a897e32009-10-15 17:21:20 +0000856void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
857 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000858 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000859 assert(MSI && "Not an instantiated static data member?");
860 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000861 if (TSK != TSK_ExplicitSpecialization &&
862 PointOfInstantiation.isValid() &&
863 MSI->getPointOfInstantiation().isInvalid())
864 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000865}
866
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000867//===----------------------------------------------------------------------===//
868// ParmVarDecl Implementation
869//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +0000870
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000871ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
872 SourceLocation L, IdentifierInfo *Id,
873 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000874 StorageClass S, StorageClass SCAsWritten,
875 Expr *DefArg) {
876 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
877 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +0000878}
879
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000880Expr *ParmVarDecl::getDefaultArg() {
881 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
882 assert(!hasUninstantiatedDefaultArg() &&
883 "Default argument is not yet instantiated!");
884
885 Expr *Arg = getInit();
886 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
887 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +0000888
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000889 return Arg;
890}
891
892unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
893 if (const CXXExprWithTemporaries *E =
894 dyn_cast<CXXExprWithTemporaries>(getInit()))
895 return E->getNumTemporaries();
896
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000897 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +0000898}
899
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000900CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
901 assert(getNumDefaultArgTemporaries() &&
902 "Default arguments does not have any temporaries!");
903
904 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
905 return E->getTemporary(i);
906}
907
908SourceRange ParmVarDecl::getDefaultArgRange() const {
909 if (const Expr *E = getInit())
910 return E->getSourceRange();
911
912 if (hasUninstantiatedDefaultArg())
913 return getUninstantiatedDefaultArg()->getSourceRange();
914
915 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000916}
917
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000918//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000919// FunctionDecl Implementation
920//===----------------------------------------------------------------------===//
921
John McCall136a6982009-09-11 06:45:03 +0000922void FunctionDecl::getNameForDiagnostic(std::string &S,
923 const PrintingPolicy &Policy,
924 bool Qualified) const {
925 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
926 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
927 if (TemplateArgs)
928 S += TemplateSpecializationType::PrintTemplateArgumentList(
929 TemplateArgs->getFlatArgumentList(),
930 TemplateArgs->flat_size(),
931 Policy);
932
933}
Ted Kremenek27f8a282008-05-20 00:43:19 +0000934
Ted Kremenek9498d382010-04-29 16:49:01 +0000935bool FunctionDecl::isVariadic() const {
936 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
937 return FT->isVariadic();
938 return false;
939}
940
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000941bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
942 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
943 if (I->Body) {
944 Definition = *I;
945 return true;
946 }
947 }
948
949 return false;
950}
951
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000952Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +0000953 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
954 if (I->Body) {
955 Definition = *I;
956 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +0000957 }
958 }
959
960 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000961}
962
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000963void FunctionDecl::setBody(Stmt *B) {
964 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +0000965 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000966 EndRangeLoc = B->getLocEnd();
967}
968
Douglas Gregor48a83b52009-09-12 00:17:51 +0000969bool FunctionDecl::isMain() const {
970 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +0000971 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000972 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +0000973 getIdentifier() && getIdentifier()->isStr("main");
974}
975
Douglas Gregor48a83b52009-09-12 00:17:51 +0000976bool FunctionDecl::isExternC() const {
977 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +0000978 // In C, any non-static, non-overloadable function has external
979 // linkage.
980 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +0000981 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000982
Mike Stump1eb44332009-09-09 15:08:12 +0000983 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +0000984 DC = DC->getParent()) {
985 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
986 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000987 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000988 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +0000989
990 break;
991 }
Douglas Gregor45975532010-08-17 16:09:23 +0000992
993 if (DC->isRecord())
994 break;
Douglas Gregor63935192009-03-02 00:19:53 +0000995 }
996
997 return false;
998}
999
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001000bool FunctionDecl::isGlobal() const {
1001 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1002 return Method->isStatic();
1003
John McCalld931b082010-08-26 03:08:43 +00001004 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001005 return false;
1006
Mike Stump1eb44332009-09-09 15:08:12 +00001007 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001008 DC->isNamespace();
1009 DC = DC->getParent()) {
1010 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1011 if (!Namespace->getDeclName())
1012 return false;
1013 break;
1014 }
1015 }
1016
1017 return true;
1018}
1019
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001020void
1021FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1022 redeclarable_base::setPreviousDeclaration(PrevDecl);
1023
1024 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1025 FunctionTemplateDecl *PrevFunTmpl
1026 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1027 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1028 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1029 }
1030}
1031
1032const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1033 return getFirstDeclaration();
1034}
1035
1036FunctionDecl *FunctionDecl::getCanonicalDecl() {
1037 return getFirstDeclaration();
1038}
1039
Douglas Gregor3e41d602009-02-13 23:20:09 +00001040/// \brief Returns a value indicating whether this function
1041/// corresponds to a builtin function.
1042///
1043/// The function corresponds to a built-in function if it is
1044/// declared at translation scope or within an extern "C" block and
1045/// its name matches with the name of a builtin. The returned value
1046/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001047/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001048/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001049unsigned FunctionDecl::getBuiltinID() const {
1050 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001051 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1052 return 0;
1053
1054 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1055 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1056 return BuiltinID;
1057
1058 // This function has the name of a known C library
1059 // function. Determine whether it actually refers to the C library
1060 // function or whether it just has the same name.
1061
Douglas Gregor9add3172009-02-17 03:23:10 +00001062 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001063 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001064 return 0;
1065
Douglas Gregor3c385e52009-02-14 18:57:46 +00001066 // If this function is at translation-unit scope and we're not in
1067 // C++, it refers to the C library function.
1068 if (!Context.getLangOptions().CPlusPlus &&
1069 getDeclContext()->isTranslationUnit())
1070 return BuiltinID;
1071
1072 // If the function is in an extern "C" linkage specification and is
1073 // not marked "overloadable", it's the real function.
1074 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001075 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001076 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001077 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001078 return BuiltinID;
1079
1080 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001081 return 0;
1082}
1083
1084
Chris Lattner1ad9b282009-04-25 06:03:53 +00001085/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001086/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001087/// after it has been created.
1088unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001089 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001090 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001091 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001092 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Reid Spencer5f016e22007-07-11 17:01:13 +00001094}
1095
Douglas Gregor838db382010-02-11 01:19:42 +00001096void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001098 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 // Zero params -> null pointer.
1101 if (NumParams) {
Douglas Gregor838db382010-02-11 01:19:42 +00001102 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001103 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001105
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001106 // Update source range. The check below allows us to set EndRangeLoc before
1107 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001108 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001109 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 }
1111}
1112
Chris Lattner8123a952008-04-10 02:22:51 +00001113/// getMinRequiredArguments - Returns the minimum number of arguments
1114/// needed to call this function. This may be fewer than the number of
1115/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001116/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001117unsigned FunctionDecl::getMinRequiredArguments() const {
1118 unsigned NumRequiredArgs = getNumParams();
1119 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001120 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001121 --NumRequiredArgs;
1122
1123 return NumRequiredArgs;
1124}
1125
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001126bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001127 // FIXME: This is not enough. Consider:
1128 //
1129 // inline void f();
1130 // void f() { }
1131 //
1132 // f is inlined, but does not have inline specified.
1133 // To fix this we should add an 'inline' flag to FunctionDecl.
1134 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001135 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001136
1137 if (isa<CXXMethodDecl>(this)) {
1138 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1139 return true;
1140 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001141
1142 switch (getTemplateSpecializationKind()) {
1143 case TSK_Undeclared:
1144 case TSK_ExplicitSpecialization:
1145 return false;
1146
1147 case TSK_ImplicitInstantiation:
1148 case TSK_ExplicitInstantiationDeclaration:
1149 case TSK_ExplicitInstantiationDefinition:
1150 // Handle below.
1151 break;
1152 }
1153
1154 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001155 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001156 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001157 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001158
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001159 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001160 return PatternDecl->isInlined();
1161
1162 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001163}
1164
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001165/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001166/// definition will be externally visible.
1167///
1168/// Inline function definitions are always available for inlining optimizations.
1169/// However, depending on the language dialect, declaration specifiers, and
1170/// attributes, the definition of an inline function may or may not be
1171/// "externally" visible to other translation units in the program.
1172///
1173/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001174/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001175/// inline definition becomes externally visible (C99 6.7.4p6).
1176///
1177/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1178/// definition, we use the GNU semantics for inline, which are nearly the
1179/// opposite of C99 semantics. In particular, "inline" by itself will create
1180/// an externally visible symbol, but "extern inline" will not create an
1181/// externally visible symbol.
1182bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1183 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001184 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001185 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001186
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001187 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001188 // GNU inline semantics. Based on a number of examples, we came up with the
1189 // following heuristic: if the "inline" keyword is present on a
1190 // declaration of the function but "extern" is not present on that
1191 // declaration, then the symbol is externally visible. Otherwise, the GNU
1192 // "extern inline" semantics applies and the symbol is not externally
1193 // visible.
1194 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1195 Redecl != RedeclEnd;
1196 ++Redecl) {
John McCalld931b082010-08-26 03:08:43 +00001197 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001198 return true;
1199 }
1200
1201 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001202 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001203 }
1204
1205 // C99 6.7.4p6:
1206 // [...] If all of the file scope declarations for a function in a
1207 // translation unit include the inline function specifier without extern,
1208 // then the definition in that translation unit is an inline definition.
1209 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1210 Redecl != RedeclEnd;
1211 ++Redecl) {
1212 // Only consider file-scope declarations in this test.
1213 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1214 continue;
1215
John McCalld931b082010-08-26 03:08:43 +00001216 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001217 return true; // Not an inline definition
1218 }
1219
1220 // C99 6.7.4p6:
1221 // An inline definition does not provide an external definition for the
1222 // function, and does not forbid an external definition in another
1223 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001224 return false;
1225}
1226
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001227/// getOverloadedOperator - Which C++ overloaded operator this
1228/// function represents, if any.
1229OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001230 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1231 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001232 else
1233 return OO_None;
1234}
1235
Sean Hunta6c058d2010-01-13 09:01:02 +00001236/// getLiteralIdentifier - The literal suffix identifier this function
1237/// represents, if any.
1238const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1239 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1240 return getDeclName().getCXXLiteralIdentifier();
1241 else
1242 return 0;
1243}
1244
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001245FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1246 if (TemplateOrSpecialization.isNull())
1247 return TK_NonTemplate;
1248 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1249 return TK_FunctionTemplate;
1250 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1251 return TK_MemberSpecialization;
1252 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1253 return TK_FunctionTemplateSpecialization;
1254 if (TemplateOrSpecialization.is
1255 <DependentFunctionTemplateSpecializationInfo*>())
1256 return TK_DependentFunctionTemplateSpecialization;
1257
1258 assert(false && "Did we miss a TemplateOrSpecialization type?");
1259 return TK_NonTemplate;
1260}
1261
Douglas Gregor2db32322009-10-07 23:56:10 +00001262FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001263 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001264 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1265
1266 return 0;
1267}
1268
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001269MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1270 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1271}
1272
Douglas Gregor2db32322009-10-07 23:56:10 +00001273void
1274FunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
1275 TemplateSpecializationKind TSK) {
1276 assert(TemplateOrSpecialization.isNull() &&
1277 "Member function is already a specialization");
1278 MemberSpecializationInfo *Info
1279 = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
1280 TemplateOrSpecialization = Info;
1281}
1282
Douglas Gregor3b846b62009-10-27 20:53:28 +00001283bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001284 // If the function is invalid, it can't be implicitly instantiated.
1285 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001286 return false;
1287
1288 switch (getTemplateSpecializationKind()) {
1289 case TSK_Undeclared:
1290 case TSK_ExplicitSpecialization:
1291 case TSK_ExplicitInstantiationDefinition:
1292 return false;
1293
1294 case TSK_ImplicitInstantiation:
1295 return true;
1296
1297 case TSK_ExplicitInstantiationDeclaration:
1298 // Handled below.
1299 break;
1300 }
1301
1302 // Find the actual template from which we will instantiate.
1303 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001304 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001305 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001306 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001307
1308 // C++0x [temp.explicit]p9:
1309 // Except for inline functions, other explicit instantiation declarations
1310 // have the effect of suppressing the implicit instantiation of the entity
1311 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001312 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001313 return true;
1314
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001315 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001316}
1317
1318FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1319 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1320 while (Primary->getInstantiatedFromMemberTemplate()) {
1321 // If we have hit a point where the user provided a specialization of
1322 // this template, we're done looking.
1323 if (Primary->isMemberSpecialization())
1324 break;
1325
1326 Primary = Primary->getInstantiatedFromMemberTemplate();
1327 }
1328
1329 return Primary->getTemplatedDecl();
1330 }
1331
1332 return getInstantiatedFromMemberFunction();
1333}
1334
Douglas Gregor16e8be22009-06-29 17:30:29 +00001335FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001336 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001337 = TemplateOrSpecialization
1338 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001339 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001340 }
1341 return 0;
1342}
1343
1344const TemplateArgumentList *
1345FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001346 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001347 = TemplateOrSpecialization
1348 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001349 return Info->TemplateArguments;
1350 }
1351 return 0;
1352}
1353
Abramo Bagnarae03db982010-05-20 15:32:11 +00001354const TemplateArgumentListInfo *
1355FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1356 if (FunctionTemplateSpecializationInfo *Info
1357 = TemplateOrSpecialization
1358 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1359 return Info->TemplateArgumentsAsWritten;
1360 }
1361 return 0;
1362}
1363
Mike Stump1eb44332009-09-09 15:08:12 +00001364void
Douglas Gregor838db382010-02-11 01:19:42 +00001365FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001366 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001367 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001368 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001369 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1370 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001371 assert(TSK != TSK_Undeclared &&
1372 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001373 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001374 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001375 if (!Info)
Douglas Gregor838db382010-02-11 01:19:42 +00001376 Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Douglas Gregor127102b2009-06-29 20:59:39 +00001378 Info->Function = this;
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001379 Info->Template.setPointer(Template);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001380 Info->Template.setInt(TSK - 1);
Douglas Gregor1637be72009-06-26 00:10:03 +00001381 Info->TemplateArguments = TemplateArgs;
Abramo Bagnarae03db982010-05-20 15:32:11 +00001382 Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001383 Info->PointOfInstantiation = PointOfInstantiation;
Douglas Gregor1637be72009-06-26 00:10:03 +00001384 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregor127102b2009-06-29 20:59:39 +00001386 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001387 // function template specializations.
1388 if (InsertPos)
1389 Template->getSpecializations().InsertNode(Info, InsertPos);
1390 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001391 // Try to insert the new node. If there is an existing node, leave it, the
1392 // set will contain the canonical decls while
1393 // FunctionTemplateDecl::findSpecialization will return
1394 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001395 FunctionTemplateSpecializationInfo *Existing
1396 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001397 (void)Existing;
1398 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1399 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001400 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001401}
1402
John McCallaf2094e2010-04-08 09:05:18 +00001403void
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001404FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1405 unsigned NumTemplateArgs,
1406 const TemplateArgument *TemplateArgs,
1407 TemplateSpecializationKind TSK,
1408 unsigned NumTemplateArgsAsWritten,
1409 TemplateArgumentLoc *TemplateArgsAsWritten,
1410 SourceLocation LAngleLoc,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001411 SourceLocation RAngleLoc,
1412 SourceLocation PointOfInstantiation) {
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001413 ASTContext &Ctx = getASTContext();
1414 TemplateArgumentList *TemplArgs
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +00001415 = new (Ctx) TemplateArgumentList(Ctx, TemplateArgs, NumTemplateArgs);
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001416 TemplateArgumentListInfo *TemplArgsInfo
1417 = new (Ctx) TemplateArgumentListInfo(LAngleLoc, RAngleLoc);
1418 for (unsigned i=0; i != NumTemplateArgsAsWritten; ++i)
1419 TemplArgsInfo->addArgument(TemplateArgsAsWritten[i]);
1420
1421 setFunctionTemplateSpecialization(Template, TemplArgs, /*InsertPos=*/0, TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001422 TemplArgsInfo, PointOfInstantiation);
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001423}
1424
1425void
John McCallaf2094e2010-04-08 09:05:18 +00001426FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1427 const UnresolvedSetImpl &Templates,
1428 const TemplateArgumentListInfo &TemplateArgs) {
1429 assert(TemplateOrSpecialization.isNull());
1430 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1431 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001432 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001433 void *Buffer = Context.Allocate(Size);
1434 DependentFunctionTemplateSpecializationInfo *Info =
1435 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1436 TemplateArgs);
1437 TemplateOrSpecialization = Info;
1438}
1439
1440DependentFunctionTemplateSpecializationInfo::
1441DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1442 const TemplateArgumentListInfo &TArgs)
1443 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1444
1445 d.NumTemplates = Ts.size();
1446 d.NumArgs = TArgs.size();
1447
1448 FunctionTemplateDecl **TsArray =
1449 const_cast<FunctionTemplateDecl**>(getTemplates());
1450 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1451 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1452
1453 TemplateArgumentLoc *ArgsArray =
1454 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1455 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1456 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1457}
1458
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001459TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001460 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001461 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001462 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001463 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001464 if (FTSInfo)
1465 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Douglas Gregor2db32322009-10-07 23:56:10 +00001467 MemberSpecializationInfo *MSInfo
1468 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1469 if (MSInfo)
1470 return MSInfo->getTemplateSpecializationKind();
1471
1472 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001473}
1474
Mike Stump1eb44332009-09-09 15:08:12 +00001475void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001476FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1477 SourceLocation PointOfInstantiation) {
1478 if (FunctionTemplateSpecializationInfo *FTSInfo
1479 = TemplateOrSpecialization.dyn_cast<
1480 FunctionTemplateSpecializationInfo*>()) {
1481 FTSInfo->setTemplateSpecializationKind(TSK);
1482 if (TSK != TSK_ExplicitSpecialization &&
1483 PointOfInstantiation.isValid() &&
1484 FTSInfo->getPointOfInstantiation().isInvalid())
1485 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1486 } else if (MemberSpecializationInfo *MSInfo
1487 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1488 MSInfo->setTemplateSpecializationKind(TSK);
1489 if (TSK != TSK_ExplicitSpecialization &&
1490 PointOfInstantiation.isValid() &&
1491 MSInfo->getPointOfInstantiation().isInvalid())
1492 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1493 } else
1494 assert(false && "Function cannot have a template specialization kind");
1495}
1496
1497SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001498 if (FunctionTemplateSpecializationInfo *FTSInfo
1499 = TemplateOrSpecialization.dyn_cast<
1500 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001501 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001502 else if (MemberSpecializationInfo *MSInfo
1503 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001504 return MSInfo->getPointOfInstantiation();
1505
1506 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001507}
1508
Douglas Gregor9f185072009-09-11 20:15:17 +00001509bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001510 if (Decl::isOutOfLine())
1511 return true;
1512
1513 // If this function was instantiated from a member function of a
1514 // class template, check whether that member function was defined out-of-line.
1515 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1516 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001517 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001518 return Definition->isOutOfLine();
1519 }
1520
1521 // If this function was instantiated from a function template,
1522 // check whether that function template was defined out-of-line.
1523 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1524 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001525 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001526 return Definition->isOutOfLine();
1527 }
1528
1529 return false;
1530}
1531
Chris Lattner8a934232008-03-31 00:36:02 +00001532//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001533// FieldDecl Implementation
1534//===----------------------------------------------------------------------===//
1535
1536FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1537 IdentifierInfo *Id, QualType T,
1538 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1539 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1540}
1541
1542bool FieldDecl::isAnonymousStructOrUnion() const {
1543 if (!isImplicit() || getDeclName())
1544 return false;
1545
1546 if (const RecordType *Record = getType()->getAs<RecordType>())
1547 return Record->getDecl()->isAnonymousStructOrUnion();
1548
1549 return false;
1550}
1551
1552//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001553// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001554//===----------------------------------------------------------------------===//
1555
Douglas Gregor1693e152010-07-06 18:42:40 +00001556SourceLocation TagDecl::getOuterLocStart() const {
1557 return getTemplateOrInnerLocStart(this);
1558}
1559
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001560SourceRange TagDecl::getSourceRange() const {
1561 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001562 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001563}
1564
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001565TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001566 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001567}
1568
Douglas Gregor60e70642010-05-19 18:39:18 +00001569void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1570 TypedefDeclOrQualifier = TDD;
1571 if (TypeForDecl)
1572 TypeForDecl->ClearLinkageCache();
1573}
1574
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001575void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001576 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001577
1578 if (isa<CXXRecordDecl>(this)) {
1579 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1580 struct CXXRecordDecl::DefinitionData *Data =
1581 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001582 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1583 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001584 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001585}
1586
1587void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001588 assert((!isa<CXXRecordDecl>(this) ||
1589 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1590 "definition completed but not started");
1591
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001592 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001593 IsBeingDefined = false;
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001594}
1595
Douglas Gregor952b0172010-02-11 01:04:33 +00001596TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001597 if (isDefinition())
1598 return const_cast<TagDecl *>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001599
1600 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001601 R != REnd; ++R)
1602 if (R->isDefinition())
1603 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001605 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001606}
1607
John McCallb6217662010-03-15 10:12:16 +00001608void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1609 SourceRange QualifierRange) {
1610 if (Qualifier) {
1611 // Make sure the extended qualifier info is allocated.
1612 if (!hasExtInfo())
1613 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1614 // Set qualifier info.
1615 getExtInfo()->NNS = Qualifier;
1616 getExtInfo()->NNSRange = QualifierRange;
1617 }
1618 else {
1619 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1620 assert(QualifierRange.isInvalid());
1621 if (hasExtInfo()) {
1622 getASTContext().Deallocate(getExtInfo());
1623 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1624 }
1625 }
1626}
1627
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001628//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001629// EnumDecl Implementation
1630//===----------------------------------------------------------------------===//
1631
1632EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1633 IdentifierInfo *Id, SourceLocation TKL,
1634 EnumDecl *PrevDecl) {
1635 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
1636 C.getTypeDeclType(Enum, PrevDecl);
1637 return Enum;
1638}
1639
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001640EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
1641 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation());
1642}
1643
Douglas Gregor838db382010-02-11 01:19:42 +00001644void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001645 QualType NewPromotionType,
1646 unsigned NumPositiveBits,
1647 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001648 assert(!isDefinition() && "Cannot redefine enums!");
1649 IntegerType = NewType;
1650 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001651 setNumPositiveBits(NumPositiveBits);
1652 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001653 TagDecl::completeDefinition();
1654}
1655
1656//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001657// RecordDecl Implementation
1658//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001659
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001660RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001661 IdentifierInfo *Id, RecordDecl *PrevDecl,
1662 SourceLocation TKL)
1663 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001664 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001665 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001666 HasObjectMember = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001667 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001668}
1669
1670RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001671 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001672 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001674 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001675 C.getTypeDeclType(R, PrevDecl);
1676 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001677}
1678
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001679RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1680 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1681 SourceLocation());
1682}
1683
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001684bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001685 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001686 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1687}
1688
Douglas Gregor44b43212008-12-11 16:49:14 +00001689/// completeDefinition - Notes that the definition of this type is now
1690/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001691void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001693 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001694}
1695
John McCallbc365c52010-05-21 01:17:40 +00001696ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1697 // Force the decl chain to come into existence properly.
1698 if (!getNextDeclInContext()) getParent()->decls_begin();
1699
1700 assert(isAnonymousStructOrUnion());
1701 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1702 assert(D->getType()->isRecordType());
1703 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1704 return D;
1705}
1706
Steve Naroff56ee6892008-10-08 17:01:13 +00001707//===----------------------------------------------------------------------===//
1708// BlockDecl Implementation
1709//===----------------------------------------------------------------------===//
1710
Douglas Gregor838db382010-02-11 01:19:42 +00001711void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001712 unsigned NParms) {
1713 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Steve Naroffe78b8092009-03-13 16:56:44 +00001715 // Zero params -> null pointer.
1716 if (NParms) {
1717 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001718 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001719 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1720 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1721 }
1722}
1723
1724unsigned BlockDecl::getNumParams() const {
1725 return NumParams;
1726}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001727
1728
1729//===----------------------------------------------------------------------===//
1730// Other Decl Allocation/Deallocation Method Implementations
1731//===----------------------------------------------------------------------===//
1732
1733TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1734 return new (C) TranslationUnitDecl(C);
1735}
1736
1737NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1738 SourceLocation L, IdentifierInfo *Id) {
1739 return new (C) NamespaceDecl(DC, L, Id);
1740}
1741
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001742ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1743 SourceLocation L, IdentifierInfo *Id, QualType T) {
1744 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1745}
1746
1747FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00001748 const DeclarationNameInfo &NameInfo,
1749 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001750 StorageClass S, StorageClass SCAsWritten,
1751 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001752 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001753 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001754 New->HasWrittenPrototype = hasWrittenPrototype;
1755 return New;
1756}
1757
1758BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1759 return new (C) BlockDecl(DC, L);
1760}
1761
1762EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1763 SourceLocation L,
1764 IdentifierInfo *Id, QualType T,
1765 Expr *E, const llvm::APSInt &V) {
1766 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1767}
1768
Douglas Gregor8e7139c2010-09-01 20:41:53 +00001769SourceRange EnumConstantDecl::getSourceRange() const {
1770 SourceLocation End = getLocation();
1771 if (Init)
1772 End = Init->getLocEnd();
1773 return SourceRange(getLocation(), End);
1774}
1775
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001776TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1777 SourceLocation L, IdentifierInfo *Id,
1778 TypeSourceInfo *TInfo) {
1779 return new (C) TypedefDecl(DC, L, Id, TInfo);
1780}
1781
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001782FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1783 SourceLocation L,
1784 StringLiteral *Str) {
1785 return new (C) FileScopeAsmDecl(DC, L, Str);
1786}