blob: 5b82ddd7c69f7b542c8c65b1e0cc1457be1991aa [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"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000029
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Chris Lattnerd3b90652008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCall7f1b9872010-12-18 03:30:47 +000036static const VisibilityAttr *GetExplicitVisibility(const Decl *d) {
37 // Use the most recent declaration of a variable.
38 if (const VarDecl *var = dyn_cast<VarDecl>(d))
39 return var->getMostRecentDeclaration()->getAttr<VisibilityAttr>();
40
41 // Use the most recent declaration of a function, and also handle
42 // function template specializations.
43 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(d)) {
44 if (const VisibilityAttr *attr
45 = fn->getMostRecentDeclaration()->getAttr<VisibilityAttr>())
46 return attr;
47
48 // If the function is a specialization of a template with an
49 // explicit visibility attribute, use that.
50 if (FunctionTemplateSpecializationInfo *templateInfo
51 = fn->getTemplateSpecializationInfo())
52 return templateInfo->getTemplate()->getTemplatedDecl()
53 ->getAttr<VisibilityAttr>();
54
55 return 0;
John McCalle7bc9722010-10-28 04:18:25 +000056 }
John McCall7f1b9872010-12-18 03:30:47 +000057
58 // Otherwise, just check the declaration itself first.
59 if (const VisibilityAttr *attr = d->getAttr<VisibilityAttr>())
60 return attr;
61
62 // If there wasn't explicit visibility there, and this is a
63 // specialization of a class template, check for visibility
64 // on the pattern.
65 if (const ClassTemplateSpecializationDecl *spec
66 = dyn_cast<ClassTemplateSpecializationDecl>(d))
67 return spec->getSpecializedTemplate()->getTemplatedDecl()
68 ->getAttr<VisibilityAttr>();
69
70 return 0;
John McCalle7bc9722010-10-28 04:18:25 +000071}
72
John McCall1fb0caa2010-10-22 21:05:15 +000073static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
74 switch (A->getVisibility()) {
75 case VisibilityAttr::Default:
76 return DefaultVisibility;
77 case VisibilityAttr::Hidden:
78 return HiddenVisibility;
79 case VisibilityAttr::Protected:
80 return ProtectedVisibility;
81 }
82 return DefaultVisibility;
83}
84
John McCallaf146032010-10-30 11:50:40 +000085typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall1fb0caa2010-10-22 21:05:15 +000086typedef std::pair<Linkage,Visibility> LVPair;
John McCallaf146032010-10-30 11:50:40 +000087
John McCall1fb0caa2010-10-22 21:05:15 +000088static LVPair merge(LVPair L, LVPair R) {
89 return LVPair(minLinkage(L.first, R.first),
90 minVisibility(L.second, R.second));
91}
92
John McCallaf146032010-10-30 11:50:40 +000093static LVPair merge(LVPair L, LinkageInfo R) {
94 return LVPair(minLinkage(L.first, R.linkage()),
95 minVisibility(L.second, R.visibility()));
96}
97
Benjamin Kramer752c2e92010-11-05 19:56:37 +000098namespace {
John McCall36987482010-11-02 01:45:15 +000099/// Flags controlling the computation of linkage and visibility.
100struct LVFlags {
101 bool ConsiderGlobalVisibility;
102 bool ConsiderVisibilityAttributes;
103
104 LVFlags() : ConsiderGlobalVisibility(true),
105 ConsiderVisibilityAttributes(true) {
106 }
107
Douglas Gregor381d34e2010-12-06 18:36:25 +0000108 /// \brief Returns a set of flags that is only useful for computing the
109 /// linkage, not the visibility, of a declaration.
110 static LVFlags CreateOnlyDeclLinkage() {
111 LVFlags F;
112 F.ConsiderGlobalVisibility = false;
113 F.ConsiderVisibilityAttributes = false;
114 return F;
115 }
116
John McCall36987482010-11-02 01:45:15 +0000117 /// Returns a set of flags, otherwise based on these, which ignores
118 /// off all sources of visibility except template arguments.
119 LVFlags onlyTemplateVisibility() const {
120 LVFlags F = *this;
121 F.ConsiderGlobalVisibility = false;
122 F.ConsiderVisibilityAttributes = false;
123 return F;
124 }
Douglas Gregor89d63e52010-12-06 18:50:56 +0000125};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000126} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000127
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000128/// \brief Get the most restrictive linkage for the types in the given
129/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +0000130static LVPair
131getLVForTemplateParameterList(const TemplateParameterList *Params) {
132 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000133 for (TemplateParameterList::const_iterator P = Params->begin(),
134 PEnd = Params->end();
135 P != PEnd; ++P) {
136 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
137 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000138 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000139 continue;
140 }
141
142 if (TemplateTemplateParmDecl *TTP
143 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000144 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000145 }
146 }
147
John McCall1fb0caa2010-10-22 21:05:15 +0000148 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000149}
150
Douglas Gregor381d34e2010-12-06 18:36:25 +0000151/// getLVForDecl - Get the linkage and visibility for the given declaration.
152static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
153
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000154/// \brief Get the most restrictive linkage for the types and
155/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +0000156static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000157 unsigned NumArgs,
158 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000159 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000160
161 for (unsigned I = 0; I != NumArgs; ++I) {
162 switch (Args[I].getKind()) {
163 case TemplateArgument::Null:
164 case TemplateArgument::Integral:
165 case TemplateArgument::Expression:
166 break;
167
168 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000169 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000170 break;
171
172 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000173 // The decl can validly be null as the representation of nullptr
174 // arguments, valid only in C++0x.
175 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor89d63e52010-12-06 18:50:56 +0000176 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
177 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000178 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000179 break;
180
181 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000182 case TemplateArgument::TemplateExpansion:
183 if (TemplateDecl *Template
184 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor89d63e52010-12-06 18:50:56 +0000185 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000186 break;
187
188 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000189 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000190 Args[I].pack_size(),
191 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000192 break;
193 }
194 }
195
John McCall1fb0caa2010-10-22 21:05:15 +0000196 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000197}
198
John McCallaf146032010-10-30 11:50:40 +0000199static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000200getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
201 LVFlags &F) {
202 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000203}
204
John McCall36987482010-11-02 01:45:15 +0000205static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000206 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000207 "Not a name having namespace scope");
208 ASTContext &Context = D->getASTContext();
209
210 // C++ [basic.link]p3:
211 // A name having namespace scope (3.3.6) has internal linkage if it
212 // is the name of
213 // - an object, reference, function or function template that is
214 // explicitly declared static; or,
215 // (This bullet corresponds to C99 6.2.2p3.)
216 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
217 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000218 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000219 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000220
221 // - an object or reference that is explicitly declared const
222 // and neither explicitly declared extern nor previously
223 // declared to have external linkage; or
224 // (there is no equivalent in C99)
225 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000226 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000227 Var->getStorageClass() != SC_Extern &&
228 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000229 bool FoundExtern = false;
230 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
231 PrevVar && !FoundExtern;
232 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000233 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000234 FoundExtern = true;
235
236 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000237 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000238 }
239 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000240 // C++ [temp]p4:
241 // A non-member function template can have internal linkage; any
242 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000243 const FunctionDecl *Function = 0;
244 if (const FunctionTemplateDecl *FunTmpl
245 = dyn_cast<FunctionTemplateDecl>(D))
246 Function = FunTmpl->getTemplatedDecl();
247 else
248 Function = cast<FunctionDecl>(D);
249
250 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000251 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000252 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000253 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
254 // - a data member of an anonymous union.
255 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000256 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000257 }
258
John McCall1fb0caa2010-10-22 21:05:15 +0000259 if (D->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000260 return LinkageInfo::uniqueExternal();
John McCalle7bc9722010-10-28 04:18:25 +0000261
John McCall1fb0caa2010-10-22 21:05:15 +0000262 // Set up the defaults.
263
264 // C99 6.2.2p5:
265 // If the declaration of an identifier for an object has file
266 // scope and no storage-class specifier, its linkage is
267 // external.
John McCallaf146032010-10-30 11:50:40 +0000268 LinkageInfo LV;
269
John McCall36987482010-11-02 01:45:15 +0000270 if (F.ConsiderVisibilityAttributes) {
271 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
272 LV.setVisibility(GetVisibilityFromAttr(VA), true);
273 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000274 } else {
275 // If we're declared in a namespace with a visibility attribute,
276 // use that namespace's visibility, but don't call it explicit.
277 for (const DeclContext *DC = D->getDeclContext();
278 !isa<TranslationUnitDecl>(DC);
279 DC = DC->getParent()) {
280 if (!isa<NamespaceDecl>(DC)) continue;
281 if (const VisibilityAttr *VA =
282 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
283 LV.setVisibility(GetVisibilityFromAttr(VA), false);
284 F.ConsiderGlobalVisibility = false;
285 break;
286 }
287 }
John McCall36987482010-11-02 01:45:15 +0000288 }
John McCallaf146032010-10-30 11:50:40 +0000289 }
John McCall1fb0caa2010-10-22 21:05:15 +0000290
Douglas Gregord85b5b92009-11-25 22:24:25 +0000291 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000292
Douglas Gregord85b5b92009-11-25 22:24:25 +0000293 // A name having namespace scope has external linkage if it is the
294 // name of
295 //
296 // - an object or reference, unless it has internal linkage; or
297 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000298 // GCC applies the following optimization to variables and static
299 // data members, but not to functions:
300 //
John McCall1fb0caa2010-10-22 21:05:15 +0000301 // Modify the variable's LV by the LV of its type unless this is
302 // C or extern "C". This follows from [basic.link]p9:
303 // A type without linkage shall not be used as the type of a
304 // variable or function with external linkage unless
305 // - the entity has C language linkage, or
306 // - the entity is declared within an unnamed namespace, or
307 // - the entity is not used or is defined in the same
308 // translation unit.
309 // and [basic.link]p10:
310 // ...the types specified by all declarations referring to a
311 // given variable or function shall be identical...
312 // C does not have an equivalent rule.
313 //
John McCallac65c622010-10-26 04:59:26 +0000314 // Ignore this if we've got an explicit attribute; the user
315 // probably knows what they're doing.
316 //
John McCall1fb0caa2010-10-22 21:05:15 +0000317 // Note that we don't want to make the variable non-external
318 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000319 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000320 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
321 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000322 return LinkageInfo::uniqueExternal();
323 if (!LV.visibilityExplicit())
324 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000325 }
326
John McCall35cebc32010-11-02 18:38:13 +0000327 if (Var->getStorageClass() == SC_PrivateExtern)
328 LV.setVisibility(HiddenVisibility, true);
329
Douglas Gregord85b5b92009-11-25 22:24:25 +0000330 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000331 (Var->getStorageClass() == SC_Extern ||
332 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000333
Douglas Gregord85b5b92009-11-25 22:24:25 +0000334 // C99 6.2.2p4:
335 // For an identifier declared with the storage-class specifier
336 // extern in a scope in which a prior declaration of that
337 // identifier is visible, if the prior declaration specifies
338 // internal or external linkage, the linkage of the identifier
339 // at the later declaration is the same as the linkage
340 // specified at the prior declaration. If no prior declaration
341 // is visible, or if the prior declaration specifies no
342 // linkage, then the identifier has external linkage.
343 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000344 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000345 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
346 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000347 }
348 }
349
Douglas Gregord85b5b92009-11-25 22:24:25 +0000350 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000351 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000352 // In theory, we can modify the function's LV by the LV of its
353 // type unless it has C linkage (see comment above about variables
354 // for justification). In practice, GCC doesn't do this, so it's
355 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000356
John McCall35cebc32010-11-02 18:38:13 +0000357 if (Function->getStorageClass() == SC_PrivateExtern)
358 LV.setVisibility(HiddenVisibility, true);
359
Douglas Gregord85b5b92009-11-25 22:24:25 +0000360 // C99 6.2.2p5:
361 // If the declaration of an identifier for a function has no
362 // storage-class specifier, its linkage is determined exactly
363 // as if it were declared with the storage-class specifier
364 // extern.
365 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000366 (Function->getStorageClass() == SC_Extern ||
367 Function->getStorageClass() == SC_PrivateExtern ||
368 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000369 // C99 6.2.2p4:
370 // For an identifier declared with the storage-class specifier
371 // extern in a scope in which a prior declaration of that
372 // identifier is visible, if the prior declaration specifies
373 // internal or external linkage, the linkage of the identifier
374 // at the later declaration is the same as the linkage
375 // specified at the prior declaration. If no prior declaration
376 // is visible, or if the prior declaration specifies no
377 // linkage, then the identifier has external linkage.
378 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000379 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000380 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
381 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000382 }
383 }
384
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000385 if (FunctionTemplateSpecializationInfo *SpecInfo
386 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000387 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
388 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000389 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000390 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000391 }
392
Douglas Gregord85b5b92009-11-25 22:24:25 +0000393 // - a named class (Clause 9), or an unnamed class defined in a
394 // typedef declaration in which the class has the typedef name
395 // for linkage purposes (7.1.3); or
396 // - a named enumeration (7.2), or an unnamed enumeration
397 // defined in a typedef declaration in which the enumeration
398 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000399 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
400 // Unnamed tags have no linkage.
401 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000402 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000403
John McCall1fb0caa2010-10-22 21:05:15 +0000404 // If this is a class template specialization, consider the
405 // linkage of the template and template arguments.
406 if (const ClassTemplateSpecializationDecl *Spec
407 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000408 // From the template.
409 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
410 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000411
John McCall1fb0caa2010-10-22 21:05:15 +0000412 // The arguments at which the template was instantiated.
413 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000414 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000415 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000416
John McCallac65c622010-10-26 04:59:26 +0000417 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000418 if (F.ConsiderGlobalVisibility)
419 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000420 (Context.getLangOptions().CPlusPlus &&
421 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000422
Douglas Gregord85b5b92009-11-25 22:24:25 +0000423 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000424 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000425 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000426 if (!isExternalLinkage(EnumLV.linkage()))
427 return LinkageInfo::none();
428 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000429
430 // - a template, unless it is a function template that has
431 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000432 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000433 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000434
Douglas Gregord85b5b92009-11-25 22:24:25 +0000435 // - a namespace (7.3), unless it is declared within an unnamed
436 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000437 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
438 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000439
John McCall1fb0caa2010-10-22 21:05:15 +0000440 // By extension, we assign external linkage to Objective-C
441 // interfaces.
442 } else if (isa<ObjCInterfaceDecl>(D)) {
443 // fallout
444
445 // Everything not covered here has no linkage.
446 } else {
John McCallaf146032010-10-30 11:50:40 +0000447 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000448 }
449
450 // If we ended up with non-external linkage, visibility should
451 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000452 if (LV.linkage() != ExternalLinkage)
453 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000454
455 // If we didn't end up with hidden visibility, consider attributes
456 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000457 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000458 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000459
460 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000461}
462
John McCall36987482010-11-02 01:45:15 +0000463static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000464 // Only certain class members have linkage. Note that fields don't
465 // really have linkage, but it's convenient to say they do for the
466 // purposes of calculating linkage of pointer-to-data-member
467 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000468 if (!(isa<CXXMethodDecl>(D) ||
469 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000470 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000471 (isa<TagDecl>(D) &&
472 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000473 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000474
John McCall36987482010-11-02 01:45:15 +0000475 LinkageInfo LV;
476
477 // The flags we're going to use to compute the class's visibility.
478 LVFlags ClassF = F;
479
480 // If we have an explicit visibility attribute, merge that in.
481 if (F.ConsiderVisibilityAttributes) {
482 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
483 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
484
485 // Ignore global visibility later, but not this attribute.
486 F.ConsiderGlobalVisibility = false;
487
488 // Ignore both global visibility and attributes when computing our
489 // parent's visibility.
490 ClassF = F.onlyTemplateVisibility();
491 }
492 }
John McCallaf146032010-10-30 11:50:40 +0000493
494 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000495 // linkage.
496 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
497 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000498 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000499
500 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000501 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000502 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000503
John McCall3cdfc4d2010-08-13 08:35:10 +0000504 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000505 TemplateSpecializationKind TSK = TSK_Undeclared;
506
John McCall1fb0caa2010-10-22 21:05:15 +0000507 // If this is a method template specialization, use the linkage for
508 // the template parameters and arguments.
509 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000510 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000511 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallaf146032010-10-30 11:50:40 +0000512 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000513 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000514
515 TSK = Spec->getTemplateSpecializationKind();
516 } else if (MemberSpecializationInfo *MSI =
517 MD->getMemberSpecializationInfo()) {
518 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000519 }
520
John McCall110e8e52010-10-29 22:22:43 +0000521 // If we're paying attention to global visibility, apply
522 // -finline-visibility-hidden if this is an inline method.
523 //
John McCallaf146032010-10-30 11:50:40 +0000524 // Note that ConsiderGlobalVisibility doesn't yet have information
525 // about whether containing classes have visibility attributes,
526 // and that's intentional.
527 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000528 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000529 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
530 // InlineVisibilityHidden only applies to definitions, and
531 // isInlined() only gives meaningful answers on definitions
532 // anyway.
533 const FunctionDecl *Def = 0;
534 if (MD->hasBody(Def) && Def->isInlined())
535 LV.setVisibility(HiddenVisibility);
536 }
John McCall1fb0caa2010-10-22 21:05:15 +0000537
John McCall110e8e52010-10-29 22:22:43 +0000538 // Note that in contrast to basically every other situation, we
539 // *do* apply -fvisibility to method declarations.
540
541 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000542 if (const ClassTemplateSpecializationDecl *Spec
543 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
544 // Merge template argument/parameter information for member
545 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000546 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallaf146032010-10-30 11:50:40 +0000547 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000548 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000549 }
550
John McCall110e8e52010-10-29 22:22:43 +0000551 // Static data members.
552 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000553 // Modify the variable's linkage by its type, but ignore the
554 // type's visibility unless it's a definition.
555 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
556 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000557 LV.mergeLinkage(UniqueExternalLinkage);
558 if (!LV.visibilityExplicit())
559 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000560 }
561
John McCall36987482010-11-02 01:45:15 +0000562 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000563
564 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000565 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000566 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000567 }
568
John McCall1fb0caa2010-10-22 21:05:15 +0000569 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000570}
571
Douglas Gregor381d34e2010-12-06 18:36:25 +0000572Linkage NamedDecl::getLinkage() const {
573 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000574 assert(Linkage(CachedLinkage) ==
575 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000576 return Linkage(CachedLinkage);
577 }
578
579 CachedLinkage = getLVForDecl(this,
580 LVFlags::CreateOnlyDeclLinkage()).linkage();
581 HasCachedLinkage = 1;
582 return Linkage(CachedLinkage);
583}
584
John McCallaf146032010-10-30 11:50:40 +0000585LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000586 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000587 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000588 HasCachedLinkage = 1;
589 CachedLinkage = LI.linkage();
590 return LI;
John McCall0df95872010-10-29 00:29:13 +0000591}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000592
John McCall36987482010-11-02 01:45:15 +0000593static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000594 // Objective-C: treat all Objective-C declarations as having external
595 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000596 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000597 default:
598 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000599 case Decl::TemplateTemplateParm: // count these as external
600 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000601 case Decl::ObjCAtDefsField:
602 case Decl::ObjCCategory:
603 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000604 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000605 case Decl::ObjCForwardProtocol:
606 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000607 case Decl::ObjCMethod:
608 case Decl::ObjCProperty:
609 case Decl::ObjCPropertyImpl:
610 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000611 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000612 }
613
Douglas Gregord85b5b92009-11-25 22:24:25 +0000614 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000615 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000616 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000617
618 // C++ [basic.link]p5:
619 // In addition, a member function, static data member, a named
620 // class or enumeration of class scope, or an unnamed class or
621 // enumeration defined in a class-scope typedef declaration such
622 // that the class or enumeration has the typedef name for linkage
623 // purposes (7.1.3), has external linkage if the name of the class
624 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000625 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000626 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000627
628 // C++ [basic.link]p6:
629 // The name of a function declared in block scope and the name of
630 // an object declared by a block scope extern declaration have
631 // linkage. If there is a visible declaration of an entity with
632 // linkage having the same name and type, ignoring entities
633 // declared outside the innermost enclosing namespace scope, the
634 // block scope declaration declares that same entity and receives
635 // the linkage of the previous declaration. If there is more than
636 // one such matching entity, the program is ill-formed. Otherwise,
637 // if no matching entity is found, the block scope entity receives
638 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000639 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
640 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000641 if (Function->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000642 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000643
John McCallaf146032010-10-30 11:50:40 +0000644 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000645 if (Flags.ConsiderVisibilityAttributes) {
646 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
647 LV.setVisibility(GetVisibilityFromAttr(VA));
648 }
649
John McCall1fb0caa2010-10-22 21:05:15 +0000650 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000651 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000652 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
653 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000654 }
655
656 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000657 }
658
John McCall0df95872010-10-29 00:29:13 +0000659 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000660 if (Var->getStorageClass() == SC_Extern ||
661 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000662 if (Var->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000663 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000664
John McCallaf146032010-10-30 11:50:40 +0000665 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000666 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000667 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000668 else if (Flags.ConsiderVisibilityAttributes) {
669 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
670 LV.setVisibility(GetVisibilityFromAttr(VA));
671 }
672
John McCall1fb0caa2010-10-22 21:05:15 +0000673 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000674 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000675 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
676 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000677 }
678
679 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000680 }
681 }
682
683 // C++ [basic.link]p6:
684 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000685 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000686}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000687
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000688std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000689 return getQualifiedNameAsString(getASTContext().getLangOptions());
690}
691
692std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000693 const DeclContext *Ctx = getDeclContext();
694
695 if (Ctx->isFunctionOrMethod())
696 return getNameAsString();
697
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000698 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
699 ContextsTy Contexts;
700
701 // Collect contexts.
702 while (Ctx && isa<NamedDecl>(Ctx)) {
703 Contexts.push_back(Ctx);
704 Ctx = Ctx->getParent();
705 };
706
707 std::string QualName;
708 llvm::raw_string_ostream OS(QualName);
709
710 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
711 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000712 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000713 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000714 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
715 std::string TemplateArgsStr
716 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000717 TemplateArgs.data(),
718 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000719 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000720 OS << Spec->getName() << TemplateArgsStr;
721 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000722 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000723 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000724 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000725 OS << ND;
726 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
727 if (!RD->getIdentifier())
728 OS << "<anonymous " << RD->getKindName() << '>';
729 else
730 OS << RD;
731 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000732 const FunctionProtoType *FT = 0;
733 if (FD->hasWrittenPrototype())
734 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
735
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000736 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000737 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000738 unsigned NumParams = FD->getNumParams();
739 for (unsigned i = 0; i < NumParams; ++i) {
740 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000741 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000742 std::string Param;
743 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000744 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000745 }
746
747 if (FT->isVariadic()) {
748 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000749 OS << ", ";
750 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000751 }
752 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000753 OS << ')';
754 } else {
755 OS << cast<NamedDecl>(*I);
756 }
757 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000758 }
759
John McCall8472af42010-03-16 21:48:18 +0000760 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000761 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000762 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000763 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000764
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000765 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000766}
767
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000768bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000769 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
770
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000771 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
772 // We want to keep it, unless it nominates same namespace.
773 if (getKind() == Decl::UsingDirective) {
774 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
775 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000778 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
779 // For function declarations, we keep track of redeclarations.
780 return FD->getPreviousDeclaration() == OldD;
781
Douglas Gregore53060f2009-06-25 22:08:12 +0000782 // For function templates, the underlying function declarations are linked.
783 if (const FunctionTemplateDecl *FunctionTemplate
784 = dyn_cast<FunctionTemplateDecl>(this))
785 if (const FunctionTemplateDecl *OldFunctionTemplate
786 = dyn_cast<FunctionTemplateDecl>(OldD))
787 return FunctionTemplate->getTemplatedDecl()
788 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Steve Naroff0de21fd2009-02-22 19:35:57 +0000790 // For method declarations, we keep track of redeclarations.
791 if (isa<ObjCMethodDecl>(this))
792 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
John McCallf36e02d2009-10-09 21:13:30 +0000794 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
795 return true;
796
John McCall9488ea12009-11-17 05:59:44 +0000797 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
798 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
799 cast<UsingShadowDecl>(OldD)->getTargetDecl();
800
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000801 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
802 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
803 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
804
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000805 // For non-function declarations, if the declarations are of the
806 // same kind then this must be a redeclaration, or semantic analysis
807 // would not have given us the new declaration.
808 return this->getKind() == OldD->getKind();
809}
810
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000811bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000812 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000813}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000814
Anders Carlssone136e0e2009-06-26 06:29:23 +0000815NamedDecl *NamedDecl::getUnderlyingDecl() {
816 NamedDecl *ND = this;
817 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000818 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000819 ND = UD->getTargetDecl();
820 else if (ObjCCompatibleAliasDecl *AD
821 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
822 return AD->getClassInterface();
823 else
824 return ND;
825 }
826}
827
John McCall161755a2010-04-06 21:38:20 +0000828bool NamedDecl::isCXXInstanceMember() const {
829 assert(isCXXClassMember() &&
830 "checking whether non-member is instance member");
831
832 const NamedDecl *D = this;
833 if (isa<UsingShadowDecl>(D))
834 D = cast<UsingShadowDecl>(D)->getTargetDecl();
835
Francois Pichet87c2e122010-11-21 06:08:52 +0000836 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000837 return true;
838 if (isa<CXXMethodDecl>(D))
839 return cast<CXXMethodDecl>(D)->isInstance();
840 if (isa<FunctionTemplateDecl>(D))
841 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
842 ->getTemplatedDecl())->isInstance();
843 return false;
844}
845
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000846//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000847// DeclaratorDecl Implementation
848//===----------------------------------------------------------------------===//
849
Douglas Gregor1693e152010-07-06 18:42:40 +0000850template <typename DeclT>
851static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
852 if (decl->getNumTemplateParameterLists() > 0)
853 return decl->getTemplateParameterList(0)->getTemplateLoc();
854 else
855 return decl->getInnerLocStart();
856}
857
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000858SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000859 TypeSourceInfo *TSI = getTypeSourceInfo();
860 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000861 return SourceLocation();
862}
863
John McCallb6217662010-03-15 10:12:16 +0000864void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
865 SourceRange QualifierRange) {
866 if (Qualifier) {
867 // Make sure the extended decl info is allocated.
868 if (!hasExtInfo()) {
869 // Save (non-extended) type source info pointer.
870 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
871 // Allocate external info struct.
872 DeclInfo = new (getASTContext()) ExtInfo;
873 // Restore savedTInfo into (extended) decl info.
874 getExtInfo()->TInfo = savedTInfo;
875 }
876 // Set qualifier info.
877 getExtInfo()->NNS = Qualifier;
878 getExtInfo()->NNSRange = QualifierRange;
879 }
880 else {
881 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
882 assert(QualifierRange.isInvalid());
883 if (hasExtInfo()) {
884 // Save type source info pointer.
885 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
886 // Deallocate the extended decl info.
887 getASTContext().Deallocate(getExtInfo());
888 // Restore savedTInfo into (non-extended) decl info.
889 DeclInfo = savedTInfo;
890 }
891 }
892}
893
Douglas Gregor1693e152010-07-06 18:42:40 +0000894SourceLocation DeclaratorDecl::getOuterLocStart() const {
895 return getTemplateOrInnerLocStart(this);
896}
897
Abramo Bagnara9b934882010-06-12 08:15:14 +0000898void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000899QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
900 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000901 TemplateParameterList **TPLists) {
902 assert((NumTPLists == 0 || TPLists != 0) &&
903 "Empty array of template parameters with positive size!");
904 assert((NumTPLists == 0 || NNS) &&
905 "Nonempty array of template parameters with no qualifier!");
906
907 // Free previous template parameters (if any).
908 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000909 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000910 TemplParamLists = 0;
911 NumTemplParamLists = 0;
912 }
913 // Set info on matched template parameter lists (if any).
914 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000915 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000916 NumTemplParamLists = NumTPLists;
917 for (unsigned i = NumTPLists; i-- > 0; )
918 TemplParamLists[i] = TPLists[i];
919 }
920}
921
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000922//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000923// VarDecl Implementation
924//===----------------------------------------------------------------------===//
925
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000926const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
927 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000928 case SC_None: break;
929 case SC_Auto: return "auto"; break;
930 case SC_Extern: return "extern"; break;
931 case SC_PrivateExtern: return "__private_extern__"; break;
932 case SC_Register: return "register"; break;
933 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000934 }
935
936 assert(0 && "Invalid storage class");
937 return 0;
938}
939
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000940VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000941 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000942 StorageClass S, StorageClass SCAsWritten) {
943 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000944}
945
Douglas Gregor381d34e2010-12-06 18:36:25 +0000946void VarDecl::setStorageClass(StorageClass SC) {
947 assert(isLegalForVariable(SC));
948 if (getStorageClass() != SC)
949 ClearLinkageCache();
950
951 SClass = SC;
952}
953
Douglas Gregor1693e152010-07-06 18:42:40 +0000954SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000955 SourceLocation Start = getTypeSpecStartLoc();
956 if (Start.isInvalid())
957 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000958 return Start;
959}
960
961SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000962 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000963 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
964 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000965}
966
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000967bool VarDecl::isExternC() const {
968 ASTContext &Context = getASTContext();
969 if (!Context.getLangOptions().CPlusPlus)
970 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000971 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000972 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
973
974 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
975 DC = DC->getParent()) {
976 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
977 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000978 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000979
980 break;
981 }
982
983 if (DC->isFunctionOrMethod())
984 return false;
985 }
986
987 return false;
988}
989
990VarDecl *VarDecl::getCanonicalDecl() {
991 return getFirstDeclaration();
992}
993
Sebastian Redle9d12b62010-01-31 22:27:38 +0000994VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
995 // C++ [basic.def]p2:
996 // A declaration is a definition unless [...] it contains the 'extern'
997 // specifier or a linkage-specification and neither an initializer [...],
998 // it declares a static data member in a class declaration [...].
999 // C++ [temp.expl.spec]p15:
1000 // An explicit specialization of a static data member of a template is a
1001 // definition if the declaration includes an initializer; otherwise, it is
1002 // a declaration.
1003 if (isStaticDataMember()) {
1004 if (isOutOfLine() && (hasInit() ||
1005 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1006 return Definition;
1007 else
1008 return DeclarationOnly;
1009 }
1010 // C99 6.7p5:
1011 // A definition of an identifier is a declaration for that identifier that
1012 // [...] causes storage to be reserved for that object.
1013 // Note: that applies for all non-file-scope objects.
1014 // C99 6.9.2p1:
1015 // If the declaration of an identifier for an object has file scope and an
1016 // initializer, the declaration is an external definition for the identifier
1017 if (hasInit())
1018 return Definition;
1019 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1020 if (hasExternalStorage())
1021 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001022
John McCalld931b082010-08-26 03:08:43 +00001023 if (getStorageClassAsWritten() == SC_Extern ||
1024 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001025 for (const VarDecl *PrevVar = getPreviousDeclaration();
1026 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1027 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1028 return DeclarationOnly;
1029 }
1030 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001031 // C99 6.9.2p2:
1032 // A declaration of an object that has file scope without an initializer,
1033 // and without a storage class specifier or the scs 'static', constitutes
1034 // a tentative definition.
1035 // No such thing in C++.
1036 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1037 return TentativeDefinition;
1038
1039 // What's left is (in C, block-scope) declarations without initializers or
1040 // external storage. These are definitions.
1041 return Definition;
1042}
1043
Sebastian Redle9d12b62010-01-31 22:27:38 +00001044VarDecl *VarDecl::getActingDefinition() {
1045 DefinitionKind Kind = isThisDeclarationADefinition();
1046 if (Kind != TentativeDefinition)
1047 return 0;
1048
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001049 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001050 VarDecl *First = getFirstDeclaration();
1051 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1052 I != E; ++I) {
1053 Kind = (*I)->isThisDeclarationADefinition();
1054 if (Kind == Definition)
1055 return 0;
1056 else if (Kind == TentativeDefinition)
1057 LastTentative = *I;
1058 }
1059 return LastTentative;
1060}
1061
1062bool VarDecl::isTentativeDefinitionNow() const {
1063 DefinitionKind Kind = isThisDeclarationADefinition();
1064 if (Kind != TentativeDefinition)
1065 return false;
1066
1067 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1068 if ((*I)->isThisDeclarationADefinition() == Definition)
1069 return false;
1070 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001071 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001072}
1073
Sebastian Redl31310a22010-02-01 20:16:42 +00001074VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001075 VarDecl *First = getFirstDeclaration();
1076 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1077 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001078 if ((*I)->isThisDeclarationADefinition() == Definition)
1079 return *I;
1080 }
1081 return 0;
1082}
1083
John McCall110e8e52010-10-29 22:22:43 +00001084VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1085 DefinitionKind Kind = DeclarationOnly;
1086
1087 const VarDecl *First = getFirstDeclaration();
1088 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1089 I != E; ++I)
1090 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1091
1092 return Kind;
1093}
1094
Sebastian Redl31310a22010-02-01 20:16:42 +00001095const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001096 redecl_iterator I = redecls_begin(), E = redecls_end();
1097 while (I != E && !I->getInit())
1098 ++I;
1099
1100 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001101 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001102 return I->getInit();
1103 }
1104 return 0;
1105}
1106
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001107bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001108 if (Decl::isOutOfLine())
1109 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001110
1111 if (!isStaticDataMember())
1112 return false;
1113
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001114 // If this static data member was instantiated from a static data member of
1115 // a class template, check whether that static data member was defined
1116 // out-of-line.
1117 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1118 return VD->isOutOfLine();
1119
1120 return false;
1121}
1122
Douglas Gregor0d035142009-10-27 18:42:08 +00001123VarDecl *VarDecl::getOutOfLineDefinition() {
1124 if (!isStaticDataMember())
1125 return 0;
1126
1127 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1128 RD != RDEnd; ++RD) {
1129 if (RD->getLexicalDeclContext()->isFileContext())
1130 return *RD;
1131 }
1132
1133 return 0;
1134}
1135
Douglas Gregor838db382010-02-11 01:19:42 +00001136void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001137 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1138 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001139 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001140 }
1141
1142 Init = I;
1143}
1144
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001145VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001146 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001147 return cast<VarDecl>(MSI->getInstantiatedFrom());
1148
1149 return 0;
1150}
1151
Douglas Gregor663b5a02009-10-14 20:14:33 +00001152TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001153 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001154 return MSI->getTemplateSpecializationKind();
1155
1156 return TSK_Undeclared;
1157}
1158
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001159MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001160 return getASTContext().getInstantiatedFromStaticDataMember(this);
1161}
1162
Douglas Gregor0a897e32009-10-15 17:21:20 +00001163void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1164 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001165 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001166 assert(MSI && "Not an instantiated static data member?");
1167 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001168 if (TSK != TSK_ExplicitSpecialization &&
1169 PointOfInstantiation.isValid() &&
1170 MSI->getPointOfInstantiation().isInvalid())
1171 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001172}
1173
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001174//===----------------------------------------------------------------------===//
1175// ParmVarDecl Implementation
1176//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001177
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001178ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1179 SourceLocation L, IdentifierInfo *Id,
1180 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001181 StorageClass S, StorageClass SCAsWritten,
1182 Expr *DefArg) {
1183 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1184 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001185}
1186
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001187Expr *ParmVarDecl::getDefaultArg() {
1188 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1189 assert(!hasUninstantiatedDefaultArg() &&
1190 "Default argument is not yet instantiated!");
1191
1192 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001193 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001194 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001195
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001196 return Arg;
1197}
1198
1199unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001200 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001201 return E->getNumTemporaries();
1202
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001203 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001204}
1205
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001206CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1207 assert(getNumDefaultArgTemporaries() &&
1208 "Default arguments does not have any temporaries!");
1209
John McCall4765fa02010-12-06 08:20:24 +00001210 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001211 return E->getTemporary(i);
1212}
1213
1214SourceRange ParmVarDecl::getDefaultArgRange() const {
1215 if (const Expr *E = getInit())
1216 return E->getSourceRange();
1217
1218 if (hasUninstantiatedDefaultArg())
1219 return getUninstantiatedDefaultArg()->getSourceRange();
1220
1221 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001222}
1223
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001224//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001225// FunctionDecl Implementation
1226//===----------------------------------------------------------------------===//
1227
John McCall136a6982009-09-11 06:45:03 +00001228void FunctionDecl::getNameForDiagnostic(std::string &S,
1229 const PrintingPolicy &Policy,
1230 bool Qualified) const {
1231 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1232 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1233 if (TemplateArgs)
1234 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00001235 TemplateArgs->data(),
1236 TemplateArgs->size(),
John McCall136a6982009-09-11 06:45:03 +00001237 Policy);
1238
1239}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001240
Ted Kremenek9498d382010-04-29 16:49:01 +00001241bool FunctionDecl::isVariadic() const {
1242 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1243 return FT->isVariadic();
1244 return false;
1245}
1246
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001247bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1248 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1249 if (I->Body) {
1250 Definition = *I;
1251 return true;
1252 }
1253 }
1254
1255 return false;
1256}
1257
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001258Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001259 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1260 if (I->Body) {
1261 Definition = *I;
1262 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001263 }
1264 }
1265
1266 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001267}
1268
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001269void FunctionDecl::setBody(Stmt *B) {
1270 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001271 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001272 EndRangeLoc = B->getLocEnd();
1273}
1274
Douglas Gregor21386642010-09-28 21:55:22 +00001275void FunctionDecl::setPure(bool P) {
1276 IsPure = P;
1277 if (P)
1278 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1279 Parent->markedVirtualFunctionPure();
1280}
1281
Douglas Gregor48a83b52009-09-12 00:17:51 +00001282bool FunctionDecl::isMain() const {
1283 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001284 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001285 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001286 getIdentifier() && getIdentifier()->isStr("main");
1287}
1288
Douglas Gregor48a83b52009-09-12 00:17:51 +00001289bool FunctionDecl::isExternC() const {
1290 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001291 // In C, any non-static, non-overloadable function has external
1292 // linkage.
1293 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001294 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001295
Mike Stump1eb44332009-09-09 15:08:12 +00001296 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001297 DC = DC->getParent()) {
1298 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1299 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001300 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001301 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001302
1303 break;
1304 }
Douglas Gregor45975532010-08-17 16:09:23 +00001305
1306 if (DC->isRecord())
1307 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001308 }
1309
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001310 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001311}
1312
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001313bool FunctionDecl::isGlobal() const {
1314 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1315 return Method->isStatic();
1316
John McCalld931b082010-08-26 03:08:43 +00001317 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001318 return false;
1319
Mike Stump1eb44332009-09-09 15:08:12 +00001320 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001321 DC->isNamespace();
1322 DC = DC->getParent()) {
1323 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1324 if (!Namespace->getDeclName())
1325 return false;
1326 break;
1327 }
1328 }
1329
1330 return true;
1331}
1332
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001333void
1334FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1335 redeclarable_base::setPreviousDeclaration(PrevDecl);
1336
1337 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1338 FunctionTemplateDecl *PrevFunTmpl
1339 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1340 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1341 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1342 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001343
1344 if (PrevDecl->IsInline)
1345 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001346}
1347
1348const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1349 return getFirstDeclaration();
1350}
1351
1352FunctionDecl *FunctionDecl::getCanonicalDecl() {
1353 return getFirstDeclaration();
1354}
1355
Douglas Gregor381d34e2010-12-06 18:36:25 +00001356void FunctionDecl::setStorageClass(StorageClass SC) {
1357 assert(isLegalForFunction(SC));
1358 if (getStorageClass() != SC)
1359 ClearLinkageCache();
1360
1361 SClass = SC;
1362}
1363
Douglas Gregor3e41d602009-02-13 23:20:09 +00001364/// \brief Returns a value indicating whether this function
1365/// corresponds to a builtin function.
1366///
1367/// The function corresponds to a built-in function if it is
1368/// declared at translation scope or within an extern "C" block and
1369/// its name matches with the name of a builtin. The returned value
1370/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001371/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001372/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001373unsigned FunctionDecl::getBuiltinID() const {
1374 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001375 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1376 return 0;
1377
1378 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1379 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1380 return BuiltinID;
1381
1382 // This function has the name of a known C library
1383 // function. Determine whether it actually refers to the C library
1384 // function or whether it just has the same name.
1385
Douglas Gregor9add3172009-02-17 03:23:10 +00001386 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001387 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001388 return 0;
1389
Douglas Gregor3c385e52009-02-14 18:57:46 +00001390 // If this function is at translation-unit scope and we're not in
1391 // C++, it refers to the C library function.
1392 if (!Context.getLangOptions().CPlusPlus &&
1393 getDeclContext()->isTranslationUnit())
1394 return BuiltinID;
1395
1396 // If the function is in an extern "C" linkage specification and is
1397 // not marked "overloadable", it's the real function.
1398 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001399 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001400 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001401 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001402 return BuiltinID;
1403
1404 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001405 return 0;
1406}
1407
1408
Chris Lattner1ad9b282009-04-25 06:03:53 +00001409/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001410/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001411/// after it has been created.
1412unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001413 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001414 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001415 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001416 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Reid Spencer5f016e22007-07-11 17:01:13 +00001418}
1419
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001420void FunctionDecl::setParams(ASTContext &C,
1421 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001422 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001423 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Reid Spencer5f016e22007-07-11 17:01:13 +00001425 // Zero params -> null pointer.
1426 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001427 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001428 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001430
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001431 // Update source range. The check below allows us to set EndRangeLoc before
1432 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001433 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001434 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 }
1436}
1437
Chris Lattner8123a952008-04-10 02:22:51 +00001438/// getMinRequiredArguments - Returns the minimum number of arguments
1439/// needed to call this function. This may be fewer than the number of
1440/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001441/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001442unsigned FunctionDecl::getMinRequiredArguments() const {
1443 unsigned NumRequiredArgs = getNumParams();
1444 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001445 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001446 --NumRequiredArgs;
1447
1448 return NumRequiredArgs;
1449}
1450
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001451bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001452 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001453 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001454
1455 if (isa<CXXMethodDecl>(this)) {
1456 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1457 return true;
1458 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001459
1460 switch (getTemplateSpecializationKind()) {
1461 case TSK_Undeclared:
1462 case TSK_ExplicitSpecialization:
1463 return false;
1464
1465 case TSK_ImplicitInstantiation:
1466 case TSK_ExplicitInstantiationDeclaration:
1467 case TSK_ExplicitInstantiationDefinition:
1468 // Handle below.
1469 break;
1470 }
1471
1472 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001473 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001474 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001475 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001476
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001477 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001478 return PatternDecl->isInlined();
1479
1480 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001481}
1482
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001483/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001484/// definition will be externally visible.
1485///
1486/// Inline function definitions are always available for inlining optimizations.
1487/// However, depending on the language dialect, declaration specifiers, and
1488/// attributes, the definition of an inline function may or may not be
1489/// "externally" visible to other translation units in the program.
1490///
1491/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001492/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001493/// inline definition becomes externally visible (C99 6.7.4p6).
1494///
1495/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1496/// definition, we use the GNU semantics for inline, which are nearly the
1497/// opposite of C99 semantics. In particular, "inline" by itself will create
1498/// an externally visible symbol, but "extern inline" will not create an
1499/// externally visible symbol.
1500bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1501 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001502 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001503 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001504
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001505 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001506 // If it's not the case that both 'inline' and 'extern' are
1507 // specified on the definition, then this inline definition is
1508 // externally visible.
1509 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1510 return true;
1511
1512 // If any declaration is 'inline' but not 'extern', then this definition
1513 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001514 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1515 Redecl != RedeclEnd;
1516 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001517 if (Redecl->isInlineSpecified() &&
1518 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001519 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001520 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001521
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001522 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001523 }
1524
1525 // C99 6.7.4p6:
1526 // [...] If all of the file scope declarations for a function in a
1527 // translation unit include the inline function specifier without extern,
1528 // then the definition in that translation unit is an inline definition.
1529 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1530 Redecl != RedeclEnd;
1531 ++Redecl) {
1532 // Only consider file-scope declarations in this test.
1533 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1534 continue;
1535
John McCalld931b082010-08-26 03:08:43 +00001536 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001537 return true; // Not an inline definition
1538 }
1539
1540 // C99 6.7.4p6:
1541 // An inline definition does not provide an external definition for the
1542 // function, and does not forbid an external definition in another
1543 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001544 return false;
1545}
1546
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001547/// getOverloadedOperator - Which C++ overloaded operator this
1548/// function represents, if any.
1549OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001550 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1551 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001552 else
1553 return OO_None;
1554}
1555
Sean Hunta6c058d2010-01-13 09:01:02 +00001556/// getLiteralIdentifier - The literal suffix identifier this function
1557/// represents, if any.
1558const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1559 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1560 return getDeclName().getCXXLiteralIdentifier();
1561 else
1562 return 0;
1563}
1564
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001565FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1566 if (TemplateOrSpecialization.isNull())
1567 return TK_NonTemplate;
1568 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1569 return TK_FunctionTemplate;
1570 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1571 return TK_MemberSpecialization;
1572 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1573 return TK_FunctionTemplateSpecialization;
1574 if (TemplateOrSpecialization.is
1575 <DependentFunctionTemplateSpecializationInfo*>())
1576 return TK_DependentFunctionTemplateSpecialization;
1577
1578 assert(false && "Did we miss a TemplateOrSpecialization type?");
1579 return TK_NonTemplate;
1580}
1581
Douglas Gregor2db32322009-10-07 23:56:10 +00001582FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001583 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001584 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1585
1586 return 0;
1587}
1588
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001589MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1590 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1591}
1592
Douglas Gregor2db32322009-10-07 23:56:10 +00001593void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001594FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1595 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001596 TemplateSpecializationKind TSK) {
1597 assert(TemplateOrSpecialization.isNull() &&
1598 "Member function is already a specialization");
1599 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001600 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001601 TemplateOrSpecialization = Info;
1602}
1603
Douglas Gregor3b846b62009-10-27 20:53:28 +00001604bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001605 // If the function is invalid, it can't be implicitly instantiated.
1606 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001607 return false;
1608
1609 switch (getTemplateSpecializationKind()) {
1610 case TSK_Undeclared:
1611 case TSK_ExplicitSpecialization:
1612 case TSK_ExplicitInstantiationDefinition:
1613 return false;
1614
1615 case TSK_ImplicitInstantiation:
1616 return true;
1617
1618 case TSK_ExplicitInstantiationDeclaration:
1619 // Handled below.
1620 break;
1621 }
1622
1623 // Find the actual template from which we will instantiate.
1624 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001625 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001626 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001627 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001628
1629 // C++0x [temp.explicit]p9:
1630 // Except for inline functions, other explicit instantiation declarations
1631 // have the effect of suppressing the implicit instantiation of the entity
1632 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001633 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001634 return true;
1635
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001636 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001637}
1638
1639FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1640 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1641 while (Primary->getInstantiatedFromMemberTemplate()) {
1642 // If we have hit a point where the user provided a specialization of
1643 // this template, we're done looking.
1644 if (Primary->isMemberSpecialization())
1645 break;
1646
1647 Primary = Primary->getInstantiatedFromMemberTemplate();
1648 }
1649
1650 return Primary->getTemplatedDecl();
1651 }
1652
1653 return getInstantiatedFromMemberFunction();
1654}
1655
Douglas Gregor16e8be22009-06-29 17:30:29 +00001656FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001657 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001658 = TemplateOrSpecialization
1659 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001660 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001661 }
1662 return 0;
1663}
1664
1665const TemplateArgumentList *
1666FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001667 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001668 = TemplateOrSpecialization
1669 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001670 return Info->TemplateArguments;
1671 }
1672 return 0;
1673}
1674
Abramo Bagnarae03db982010-05-20 15:32:11 +00001675const TemplateArgumentListInfo *
1676FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1677 if (FunctionTemplateSpecializationInfo *Info
1678 = TemplateOrSpecialization
1679 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1680 return Info->TemplateArgumentsAsWritten;
1681 }
1682 return 0;
1683}
1684
Mike Stump1eb44332009-09-09 15:08:12 +00001685void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001686FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1687 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001688 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001689 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001690 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001691 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1692 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001693 assert(TSK != TSK_Undeclared &&
1694 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001695 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001696 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001697 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001698 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1699 TemplateArgs,
1700 TemplateArgsAsWritten,
1701 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001702 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Douglas Gregor127102b2009-06-29 20:59:39 +00001704 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001705 // function template specializations.
1706 if (InsertPos)
1707 Template->getSpecializations().InsertNode(Info, InsertPos);
1708 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001709 // Try to insert the new node. If there is an existing node, leave it, the
1710 // set will contain the canonical decls while
1711 // FunctionTemplateDecl::findSpecialization will return
1712 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001713 FunctionTemplateSpecializationInfo *Existing
1714 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001715 (void)Existing;
1716 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1717 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001718 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001719}
1720
John McCallaf2094e2010-04-08 09:05:18 +00001721void
1722FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1723 const UnresolvedSetImpl &Templates,
1724 const TemplateArgumentListInfo &TemplateArgs) {
1725 assert(TemplateOrSpecialization.isNull());
1726 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1727 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001728 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001729 void *Buffer = Context.Allocate(Size);
1730 DependentFunctionTemplateSpecializationInfo *Info =
1731 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1732 TemplateArgs);
1733 TemplateOrSpecialization = Info;
1734}
1735
1736DependentFunctionTemplateSpecializationInfo::
1737DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1738 const TemplateArgumentListInfo &TArgs)
1739 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1740
1741 d.NumTemplates = Ts.size();
1742 d.NumArgs = TArgs.size();
1743
1744 FunctionTemplateDecl **TsArray =
1745 const_cast<FunctionTemplateDecl**>(getTemplates());
1746 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1747 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1748
1749 TemplateArgumentLoc *ArgsArray =
1750 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1751 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1752 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1753}
1754
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001755TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001756 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001757 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001758 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001759 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001760 if (FTSInfo)
1761 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Douglas Gregor2db32322009-10-07 23:56:10 +00001763 MemberSpecializationInfo *MSInfo
1764 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1765 if (MSInfo)
1766 return MSInfo->getTemplateSpecializationKind();
1767
1768 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001769}
1770
Mike Stump1eb44332009-09-09 15:08:12 +00001771void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001772FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1773 SourceLocation PointOfInstantiation) {
1774 if (FunctionTemplateSpecializationInfo *FTSInfo
1775 = TemplateOrSpecialization.dyn_cast<
1776 FunctionTemplateSpecializationInfo*>()) {
1777 FTSInfo->setTemplateSpecializationKind(TSK);
1778 if (TSK != TSK_ExplicitSpecialization &&
1779 PointOfInstantiation.isValid() &&
1780 FTSInfo->getPointOfInstantiation().isInvalid())
1781 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1782 } else if (MemberSpecializationInfo *MSInfo
1783 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1784 MSInfo->setTemplateSpecializationKind(TSK);
1785 if (TSK != TSK_ExplicitSpecialization &&
1786 PointOfInstantiation.isValid() &&
1787 MSInfo->getPointOfInstantiation().isInvalid())
1788 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1789 } else
1790 assert(false && "Function cannot have a template specialization kind");
1791}
1792
1793SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001794 if (FunctionTemplateSpecializationInfo *FTSInfo
1795 = TemplateOrSpecialization.dyn_cast<
1796 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001797 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001798 else if (MemberSpecializationInfo *MSInfo
1799 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001800 return MSInfo->getPointOfInstantiation();
1801
1802 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001803}
1804
Douglas Gregor9f185072009-09-11 20:15:17 +00001805bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001806 if (Decl::isOutOfLine())
1807 return true;
1808
1809 // If this function was instantiated from a member function of a
1810 // class template, check whether that member function was defined out-of-line.
1811 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1812 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001813 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001814 return Definition->isOutOfLine();
1815 }
1816
1817 // If this function was instantiated from a function template,
1818 // check whether that function template was defined out-of-line.
1819 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1820 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001821 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001822 return Definition->isOutOfLine();
1823 }
1824
1825 return false;
1826}
1827
Chris Lattner8a934232008-03-31 00:36:02 +00001828//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001829// FieldDecl Implementation
1830//===----------------------------------------------------------------------===//
1831
1832FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1833 IdentifierInfo *Id, QualType T,
1834 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1835 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1836}
1837
1838bool FieldDecl::isAnonymousStructOrUnion() const {
1839 if (!isImplicit() || getDeclName())
1840 return false;
1841
1842 if (const RecordType *Record = getType()->getAs<RecordType>())
1843 return Record->getDecl()->isAnonymousStructOrUnion();
1844
1845 return false;
1846}
1847
1848//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001849// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001850//===----------------------------------------------------------------------===//
1851
Douglas Gregor1693e152010-07-06 18:42:40 +00001852SourceLocation TagDecl::getOuterLocStart() const {
1853 return getTemplateOrInnerLocStart(this);
1854}
1855
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001856SourceRange TagDecl::getSourceRange() const {
1857 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001858 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001859}
1860
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001861TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001862 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001863}
1864
Douglas Gregor60e70642010-05-19 18:39:18 +00001865void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1866 TypedefDeclOrQualifier = TDD;
1867 if (TypeForDecl)
1868 TypeForDecl->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001869 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00001870}
1871
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001872void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001873 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001874
1875 if (isa<CXXRecordDecl>(this)) {
1876 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1877 struct CXXRecordDecl::DefinitionData *Data =
1878 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001879 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1880 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001881 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001882}
1883
1884void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001885 assert((!isa<CXXRecordDecl>(this) ||
1886 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1887 "definition completed but not started");
1888
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001889 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001890 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00001891
1892 if (ASTMutationListener *L = getASTMutationListener())
1893 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001894}
1895
Douglas Gregor952b0172010-02-11 01:04:33 +00001896TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001897 if (isDefinition())
1898 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001899 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1900 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001901
1902 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001903 R != REnd; ++R)
1904 if (R->isDefinition())
1905 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001907 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001908}
1909
John McCallb6217662010-03-15 10:12:16 +00001910void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1911 SourceRange QualifierRange) {
1912 if (Qualifier) {
1913 // Make sure the extended qualifier info is allocated.
1914 if (!hasExtInfo())
1915 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1916 // Set qualifier info.
1917 getExtInfo()->NNS = Qualifier;
1918 getExtInfo()->NNSRange = QualifierRange;
1919 }
1920 else {
1921 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1922 assert(QualifierRange.isInvalid());
1923 if (hasExtInfo()) {
1924 getASTContext().Deallocate(getExtInfo());
1925 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1926 }
1927 }
1928}
1929
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001930//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001931// EnumDecl Implementation
1932//===----------------------------------------------------------------------===//
1933
1934EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1935 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001936 EnumDecl *PrevDecl, bool IsScoped,
1937 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001938 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001939 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001940 C.getTypeDeclType(Enum, PrevDecl);
1941 return Enum;
1942}
1943
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001944EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001945 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001946 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001947}
1948
Douglas Gregor838db382010-02-11 01:19:42 +00001949void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001950 QualType NewPromotionType,
1951 unsigned NumPositiveBits,
1952 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001953 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001954 if (!IntegerType)
1955 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001956 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001957 setNumPositiveBits(NumPositiveBits);
1958 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001959 TagDecl::completeDefinition();
1960}
1961
1962//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001963// RecordDecl Implementation
1964//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001965
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001966RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001967 IdentifierInfo *Id, RecordDecl *PrevDecl,
1968 SourceLocation TKL)
1969 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001970 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001971 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001972 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001973 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001974 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001975}
1976
1977RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001978 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001979 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001981 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001982 C.getTypeDeclType(R, PrevDecl);
1983 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001984}
1985
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001986RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1987 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1988 SourceLocation());
1989}
1990
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001991bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001992 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001993 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1994}
1995
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001996RecordDecl::field_iterator RecordDecl::field_begin() const {
1997 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1998 LoadFieldsFromExternalStorage();
1999
2000 return field_iterator(decl_iterator(FirstDecl));
2001}
2002
Douglas Gregor44b43212008-12-11 16:49:14 +00002003/// completeDefinition - Notes that the definition of this type is now
2004/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00002005void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002007 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00002008}
2009
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002010void RecordDecl::LoadFieldsFromExternalStorage() const {
2011 ExternalASTSource *Source = getASTContext().getExternalSource();
2012 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2013
2014 // Notify that we have a RecordDecl doing some initialization.
2015 ExternalASTSource::Deserializing TheFields(Source);
2016
2017 llvm::SmallVector<Decl*, 64> Decls;
2018 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2019 return;
2020
2021#ifndef NDEBUG
2022 // Check that all decls we got were FieldDecls.
2023 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2024 assert(isa<FieldDecl>(Decls[i]));
2025#endif
2026
2027 LoadedFieldsFromExternalStorage = true;
2028
2029 if (Decls.empty())
2030 return;
2031
2032 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2033}
2034
Steve Naroff56ee6892008-10-08 17:01:13 +00002035//===----------------------------------------------------------------------===//
2036// BlockDecl Implementation
2037//===----------------------------------------------------------------------===//
2038
Douglas Gregor838db382010-02-11 01:19:42 +00002039void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002040 unsigned NParms) {
2041 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Steve Naroffe78b8092009-03-13 16:56:44 +00002043 // Zero params -> null pointer.
2044 if (NParms) {
2045 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002046 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002047 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2048 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2049 }
2050}
2051
2052unsigned BlockDecl::getNumParams() const {
2053 return NumParams;
2054}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002055
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002056SourceRange BlockDecl::getSourceRange() const {
2057 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2058}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002059
2060//===----------------------------------------------------------------------===//
2061// Other Decl Allocation/Deallocation Method Implementations
2062//===----------------------------------------------------------------------===//
2063
2064TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2065 return new (C) TranslationUnitDecl(C);
2066}
2067
2068NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2069 SourceLocation L, IdentifierInfo *Id) {
2070 return new (C) NamespaceDecl(DC, L, Id);
2071}
2072
Douglas Gregor06c91932010-10-27 19:49:05 +00002073NamespaceDecl *NamespaceDecl::getNextNamespace() {
2074 return dyn_cast_or_null<NamespaceDecl>(
2075 NextNamespace.get(getASTContext().getExternalSource()));
2076}
2077
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002078ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2079 SourceLocation L, IdentifierInfo *Id, QualType T) {
2080 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2081}
2082
2083FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002084 const DeclarationNameInfo &NameInfo,
2085 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002086 StorageClass S, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002087 bool isInlineSpecified,
2088 bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002089 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor8f150942010-12-09 16:59:22 +00002090 S, SCAsWritten, isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002091 New->HasWrittenPrototype = hasWrittenPrototype;
2092 return New;
2093}
2094
2095BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2096 return new (C) BlockDecl(DC, L);
2097}
2098
2099EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2100 SourceLocation L,
2101 IdentifierInfo *Id, QualType T,
2102 Expr *E, const llvm::APSInt &V) {
2103 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2104}
2105
Benjamin Kramerd9811462010-11-21 14:11:41 +00002106IndirectFieldDecl *
2107IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2108 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2109 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002110 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2111}
2112
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002113SourceRange EnumConstantDecl::getSourceRange() const {
2114 SourceLocation End = getLocation();
2115 if (Init)
2116 End = Init->getLocEnd();
2117 return SourceRange(getLocation(), End);
2118}
2119
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002120TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2121 SourceLocation L, IdentifierInfo *Id,
2122 TypeSourceInfo *TInfo) {
2123 return new (C) TypedefDecl(DC, L, Id, TInfo);
2124}
2125
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002126FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2127 SourceLocation L,
2128 StringLiteral *Str) {
2129 return new (C) FileScopeAsmDecl(DC, L, Str);
2130}