blob: b2d9af2361ee5580b07e4b9d14b9d7e68522358b [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;
John McCall1a0918a2011-03-04 10:39:25 +0000103 bool ConsiderTemplateParameterTypes;
John McCall36987482010-11-02 01:45:15 +0000104
105 LVFlags() : ConsiderGlobalVisibility(true),
John McCall1a0918a2011-03-04 10:39:25 +0000106 ConsiderVisibilityAttributes(true),
107 ConsiderTemplateParameterTypes(true) {
John McCall36987482010-11-02 01:45:15 +0000108 }
109
Douglas Gregor381d34e2010-12-06 18:36:25 +0000110 /// \brief Returns a set of flags that is only useful for computing the
111 /// linkage, not the visibility, of a declaration.
112 static LVFlags CreateOnlyDeclLinkage() {
113 LVFlags F;
114 F.ConsiderGlobalVisibility = false;
115 F.ConsiderVisibilityAttributes = false;
John McCall1a0918a2011-03-04 10:39:25 +0000116 F.ConsiderTemplateParameterTypes = false;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000117 return F;
118 }
119
John McCall36987482010-11-02 01:45:15 +0000120 /// Returns a set of flags, otherwise based on these, which ignores
121 /// off all sources of visibility except template arguments.
122 LVFlags onlyTemplateVisibility() const {
123 LVFlags F = *this;
124 F.ConsiderGlobalVisibility = false;
125 F.ConsiderVisibilityAttributes = false;
John McCall1a0918a2011-03-04 10:39:25 +0000126 F.ConsiderTemplateParameterTypes = false;
John McCall36987482010-11-02 01:45:15 +0000127 return F;
128 }
Douglas Gregor89d63e52010-12-06 18:50:56 +0000129};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000130} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000131
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000132/// \brief Get the most restrictive linkage for the types in the given
133/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +0000134static LVPair
135getLVForTemplateParameterList(const TemplateParameterList *Params) {
136 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000137 for (TemplateParameterList::const_iterator P = Params->begin(),
138 PEnd = Params->end();
139 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000140 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
141 if (NTTP->isExpandedParameterPack()) {
142 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
143 QualType T = NTTP->getExpansionType(I);
144 if (!T->isDependentType())
145 LV = merge(LV, T->getLinkageAndVisibility());
146 }
147 continue;
148 }
149
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000150 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000151 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000152 continue;
153 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000154 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000155
156 if (TemplateTemplateParmDecl *TTP
157 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000158 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000159 }
160 }
161
John McCall1fb0caa2010-10-22 21:05:15 +0000162 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000163}
164
Douglas Gregor381d34e2010-12-06 18:36:25 +0000165/// getLVForDecl - Get the linkage and visibility for the given declaration.
166static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
167
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000168/// \brief Get the most restrictive linkage for the types and
169/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +0000170static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000171 unsigned NumArgs,
172 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000173 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000174
175 for (unsigned I = 0; I != NumArgs; ++I) {
176 switch (Args[I].getKind()) {
177 case TemplateArgument::Null:
178 case TemplateArgument::Integral:
179 case TemplateArgument::Expression:
180 break;
181
182 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000183 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000184 break;
185
186 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000187 // The decl can validly be null as the representation of nullptr
188 // arguments, valid only in C++0x.
189 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor89d63e52010-12-06 18:50:56 +0000190 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
191 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000192 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000193 break;
194
195 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000196 case TemplateArgument::TemplateExpansion:
197 if (TemplateDecl *Template
198 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor89d63e52010-12-06 18:50:56 +0000199 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000200 break;
201
202 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000203 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000204 Args[I].pack_size(),
205 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000206 break;
207 }
208 }
209
John McCall1fb0caa2010-10-22 21:05:15 +0000210 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000211}
212
John McCallaf146032010-10-30 11:50:40 +0000213static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000214getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
215 LVFlags &F) {
216 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000217}
218
John McCall36987482010-11-02 01:45:15 +0000219static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000220 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000221 "Not a name having namespace scope");
222 ASTContext &Context = D->getASTContext();
223
224 // C++ [basic.link]p3:
225 // A name having namespace scope (3.3.6) has internal linkage if it
226 // is the name of
227 // - an object, reference, function or function template that is
228 // explicitly declared static; or,
229 // (This bullet corresponds to C99 6.2.2p3.)
230 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
231 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000232 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000233 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000234
235 // - an object or reference that is explicitly declared const
236 // and neither explicitly declared extern nor previously
237 // declared to have external linkage; or
238 // (there is no equivalent in C99)
239 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000240 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000241 Var->getStorageClass() != SC_Extern &&
242 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000243 bool FoundExtern = false;
244 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
245 PrevVar && !FoundExtern;
246 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000247 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000248 FoundExtern = true;
249
250 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000251 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000252 }
253 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000254 // C++ [temp]p4:
255 // A non-member function template can have internal linkage; any
256 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000257 const FunctionDecl *Function = 0;
258 if (const FunctionTemplateDecl *FunTmpl
259 = dyn_cast<FunctionTemplateDecl>(D))
260 Function = FunTmpl->getTemplatedDecl();
261 else
262 Function = cast<FunctionDecl>(D);
263
264 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000265 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000266 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000267 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
268 // - a data member of an anonymous union.
269 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000270 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000271 }
272
Chandler Carruth094b6432011-02-24 19:03:39 +0000273 if (D->isInAnonymousNamespace()) {
274 const VarDecl *Var = dyn_cast<VarDecl>(D);
275 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
276 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
277 return LinkageInfo::uniqueExternal();
278 }
John McCalle7bc9722010-10-28 04:18:25 +0000279
John McCall1fb0caa2010-10-22 21:05:15 +0000280 // Set up the defaults.
281
282 // C99 6.2.2p5:
283 // If the declaration of an identifier for an object has file
284 // scope and no storage-class specifier, its linkage is
285 // external.
John McCallaf146032010-10-30 11:50:40 +0000286 LinkageInfo LV;
287
John McCall36987482010-11-02 01:45:15 +0000288 if (F.ConsiderVisibilityAttributes) {
289 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
290 LV.setVisibility(GetVisibilityFromAttr(VA), true);
291 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000292 } else {
293 // If we're declared in a namespace with a visibility attribute,
294 // use that namespace's visibility, but don't call it explicit.
295 for (const DeclContext *DC = D->getDeclContext();
296 !isa<TranslationUnitDecl>(DC);
297 DC = DC->getParent()) {
298 if (!isa<NamespaceDecl>(DC)) continue;
299 if (const VisibilityAttr *VA =
300 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
301 LV.setVisibility(GetVisibilityFromAttr(VA), false);
302 F.ConsiderGlobalVisibility = false;
303 break;
304 }
305 }
John McCall36987482010-11-02 01:45:15 +0000306 }
John McCallaf146032010-10-30 11:50:40 +0000307 }
John McCall1fb0caa2010-10-22 21:05:15 +0000308
Douglas Gregord85b5b92009-11-25 22:24:25 +0000309 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000310
Douglas Gregord85b5b92009-11-25 22:24:25 +0000311 // A name having namespace scope has external linkage if it is the
312 // name of
313 //
314 // - an object or reference, unless it has internal linkage; or
315 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000316 // GCC applies the following optimization to variables and static
317 // data members, but not to functions:
318 //
John McCall1fb0caa2010-10-22 21:05:15 +0000319 // Modify the variable's LV by the LV of its type unless this is
320 // C or extern "C". This follows from [basic.link]p9:
321 // A type without linkage shall not be used as the type of a
322 // variable or function with external linkage unless
323 // - the entity has C language linkage, or
324 // - the entity is declared within an unnamed namespace, or
325 // - the entity is not used or is defined in the same
326 // translation unit.
327 // and [basic.link]p10:
328 // ...the types specified by all declarations referring to a
329 // given variable or function shall be identical...
330 // C does not have an equivalent rule.
331 //
John McCallac65c622010-10-26 04:59:26 +0000332 // Ignore this if we've got an explicit attribute; the user
333 // probably knows what they're doing.
334 //
John McCall1fb0caa2010-10-22 21:05:15 +0000335 // Note that we don't want to make the variable non-external
336 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000337 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000338 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
339 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000340 return LinkageInfo::uniqueExternal();
341 if (!LV.visibilityExplicit())
342 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000343 }
344
John McCall35cebc32010-11-02 18:38:13 +0000345 if (Var->getStorageClass() == SC_PrivateExtern)
346 LV.setVisibility(HiddenVisibility, true);
347
Douglas Gregord85b5b92009-11-25 22:24:25 +0000348 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000349 (Var->getStorageClass() == SC_Extern ||
350 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000351
Douglas Gregord85b5b92009-11-25 22:24:25 +0000352 // C99 6.2.2p4:
353 // For an identifier declared with the storage-class specifier
354 // extern in a scope in which a prior declaration of that
355 // identifier is visible, if the prior declaration specifies
356 // internal or external linkage, the linkage of the identifier
357 // at the later declaration is the same as the linkage
358 // specified at the prior declaration. If no prior declaration
359 // is visible, or if the prior declaration specifies no
360 // linkage, then the identifier has external linkage.
361 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000362 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000363 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
364 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000365 }
366 }
367
Douglas Gregord85b5b92009-11-25 22:24:25 +0000368 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000369 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000370 // In theory, we can modify the function's LV by the LV of its
371 // type unless it has C linkage (see comment above about variables
372 // for justification). In practice, GCC doesn't do this, so it's
373 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000374
John McCall35cebc32010-11-02 18:38:13 +0000375 if (Function->getStorageClass() == SC_PrivateExtern)
376 LV.setVisibility(HiddenVisibility, true);
377
Douglas Gregord85b5b92009-11-25 22:24:25 +0000378 // C99 6.2.2p5:
379 // If the declaration of an identifier for a function has no
380 // storage-class specifier, its linkage is determined exactly
381 // as if it were declared with the storage-class specifier
382 // extern.
383 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000384 (Function->getStorageClass() == SC_Extern ||
385 Function->getStorageClass() == SC_PrivateExtern ||
386 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000387 // C99 6.2.2p4:
388 // For an identifier declared with the storage-class specifier
389 // extern in a scope in which a prior declaration of that
390 // identifier is visible, if the prior declaration specifies
391 // internal or external linkage, the linkage of the identifier
392 // at the later declaration is the same as the linkage
393 // specified at the prior declaration. If no prior declaration
394 // is visible, or if the prior declaration specifies no
395 // linkage, then the identifier has external linkage.
396 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000397 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000398 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
399 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000400 }
401 }
402
John McCallaf8ca372011-02-10 06:50:24 +0000403 // In C++, then if the type of the function uses a type with
404 // unique-external linkage, it's not legally usable from outside
405 // this translation unit. However, we should use the C linkage
406 // rules instead for extern "C" declarations.
407 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
408 Function->getType()->getLinkage() == UniqueExternalLinkage)
409 return LinkageInfo::uniqueExternal();
410
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000411 if (FunctionTemplateSpecializationInfo *SpecInfo
412 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000413 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
414 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000415 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000416 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000417 }
418
Douglas Gregord85b5b92009-11-25 22:24:25 +0000419 // - a named class (Clause 9), or an unnamed class defined in a
420 // typedef declaration in which the class has the typedef name
421 // for linkage purposes (7.1.3); or
422 // - a named enumeration (7.2), or an unnamed enumeration
423 // defined in a typedef declaration in which the enumeration
424 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000425 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
426 // Unnamed tags have no linkage.
427 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000428 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000429
John McCall1fb0caa2010-10-22 21:05:15 +0000430 // If this is a class template specialization, consider the
431 // linkage of the template and template arguments.
432 if (const ClassTemplateSpecializationDecl *Spec
433 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000434 // From the template.
435 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
436 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000437
John McCall1fb0caa2010-10-22 21:05:15 +0000438 // The arguments at which the template was instantiated.
439 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000440 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000441 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000442
John McCallac65c622010-10-26 04:59:26 +0000443 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000444 if (F.ConsiderGlobalVisibility)
445 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000446 (Context.getLangOptions().CPlusPlus &&
447 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000448
Douglas Gregord85b5b92009-11-25 22:24:25 +0000449 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000450 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000451 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000452 if (!isExternalLinkage(EnumLV.linkage()))
453 return LinkageInfo::none();
454 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000455
456 // - a template, unless it is a function template that has
457 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000458 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
459 if (F.ConsiderTemplateParameterTypes)
460 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000461
Douglas Gregord85b5b92009-11-25 22:24:25 +0000462 // - a namespace (7.3), unless it is declared within an unnamed
463 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000464 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
465 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000466
John McCall1fb0caa2010-10-22 21:05:15 +0000467 // By extension, we assign external linkage to Objective-C
468 // interfaces.
469 } else if (isa<ObjCInterfaceDecl>(D)) {
470 // fallout
471
472 // Everything not covered here has no linkage.
473 } else {
John McCallaf146032010-10-30 11:50:40 +0000474 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000475 }
476
477 // If we ended up with non-external linkage, visibility should
478 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000479 if (LV.linkage() != ExternalLinkage)
480 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000481
482 // If we didn't end up with hidden visibility, consider attributes
483 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000484 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000485 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000486
487 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000488}
489
John McCall36987482010-11-02 01:45:15 +0000490static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000491 // Only certain class members have linkage. Note that fields don't
492 // really have linkage, but it's convenient to say they do for the
493 // purposes of calculating linkage of pointer-to-data-member
494 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000495 if (!(isa<CXXMethodDecl>(D) ||
496 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000497 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000498 (isa<TagDecl>(D) &&
499 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000500 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000501
John McCall36987482010-11-02 01:45:15 +0000502 LinkageInfo LV;
503
504 // The flags we're going to use to compute the class's visibility.
505 LVFlags ClassF = F;
506
507 // If we have an explicit visibility attribute, merge that in.
508 if (F.ConsiderVisibilityAttributes) {
509 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
510 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
511
512 // Ignore global visibility later, but not this attribute.
513 F.ConsiderGlobalVisibility = false;
514
515 // Ignore both global visibility and attributes when computing our
516 // parent's visibility.
517 ClassF = F.onlyTemplateVisibility();
518 }
519 }
John McCallaf146032010-10-30 11:50:40 +0000520
521 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000522 // linkage.
523 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
524 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000525 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000526
527 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000528 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000529 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000530
John McCall3cdfc4d2010-08-13 08:35:10 +0000531 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000532 // If the type of the function uses a type with unique-external
533 // linkage, it's not legally usable from outside this translation unit.
534 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
535 return LinkageInfo::uniqueExternal();
536
John McCall110e8e52010-10-29 22:22:43 +0000537 TemplateSpecializationKind TSK = TSK_Undeclared;
538
John McCall1fb0caa2010-10-22 21:05:15 +0000539 // If this is a method template specialization, use the linkage for
540 // the template parameters and arguments.
541 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000542 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000543 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCall1a0918a2011-03-04 10:39:25 +0000544 if (F.ConsiderTemplateParameterTypes)
545 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000546 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000547
548 TSK = Spec->getTemplateSpecializationKind();
549 } else if (MemberSpecializationInfo *MSI =
550 MD->getMemberSpecializationInfo()) {
551 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000552 }
553
John McCall110e8e52010-10-29 22:22:43 +0000554 // If we're paying attention to global visibility, apply
555 // -finline-visibility-hidden if this is an inline method.
556 //
John McCallaf146032010-10-30 11:50:40 +0000557 // Note that ConsiderGlobalVisibility doesn't yet have information
558 // about whether containing classes have visibility attributes,
559 // and that's intentional.
560 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000561 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000562 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
563 // InlineVisibilityHidden only applies to definitions, and
564 // isInlined() only gives meaningful answers on definitions
565 // anyway.
566 const FunctionDecl *Def = 0;
567 if (MD->hasBody(Def) && Def->isInlined())
568 LV.setVisibility(HiddenVisibility);
569 }
John McCall1fb0caa2010-10-22 21:05:15 +0000570
John McCall110e8e52010-10-29 22:22:43 +0000571 // Note that in contrast to basically every other situation, we
572 // *do* apply -fvisibility to method declarations.
573
574 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000575 if (const ClassTemplateSpecializationDecl *Spec
576 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
577 // Merge template argument/parameter information for member
578 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000579 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCall1a0918a2011-03-04 10:39:25 +0000580 if (F.ConsiderTemplateParameterTypes)
581 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000582 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000583 }
584
John McCall110e8e52010-10-29 22:22:43 +0000585 // Static data members.
586 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000587 // Modify the variable's linkage by its type, but ignore the
588 // type's visibility unless it's a definition.
589 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
590 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000591 LV.mergeLinkage(UniqueExternalLinkage);
592 if (!LV.visibilityExplicit())
593 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000594 }
595
John McCall36987482010-11-02 01:45:15 +0000596 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000597
598 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000599 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000600 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000601 }
602
John McCall1fb0caa2010-10-22 21:05:15 +0000603 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000604}
605
John McCallf76b0922011-02-08 19:01:05 +0000606static void clearLinkageForClass(const CXXRecordDecl *record) {
607 for (CXXRecordDecl::decl_iterator
608 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
609 Decl *child = *i;
610 if (isa<NamedDecl>(child))
611 cast<NamedDecl>(child)->ClearLinkageCache();
612 }
613}
614
615void NamedDecl::ClearLinkageCache() {
616 // Note that we can't skip clearing the linkage of children just
617 // because the parent doesn't have cached linkage: we don't cache
618 // when computing linkage for parent contexts.
619
620 HasCachedLinkage = 0;
621
622 // If we're changing the linkage of a class, we need to reset the
623 // linkage of child declarations, too.
624 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
625 clearLinkageForClass(record);
626
John McCall15e310a2011-02-19 02:53:41 +0000627 if (ClassTemplateDecl *temp =
628 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000629 // Clear linkage for the template pattern.
630 CXXRecordDecl *record = temp->getTemplatedDecl();
631 record->HasCachedLinkage = 0;
632 clearLinkageForClass(record);
633
John McCall15e310a2011-02-19 02:53:41 +0000634 // We need to clear linkage for specializations, too.
635 for (ClassTemplateDecl::spec_iterator
636 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
637 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000638 }
John McCall15e310a2011-02-19 02:53:41 +0000639
640 // Clear cached linkage for function template decls, too.
641 if (FunctionTemplateDecl *temp =
642 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
643 for (FunctionTemplateDecl::spec_iterator
644 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
645 i->ClearLinkageCache();
646
John McCallf76b0922011-02-08 19:01:05 +0000647}
648
Douglas Gregor381d34e2010-12-06 18:36:25 +0000649Linkage NamedDecl::getLinkage() const {
650 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000651 assert(Linkage(CachedLinkage) ==
652 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000653 return Linkage(CachedLinkage);
654 }
655
656 CachedLinkage = getLVForDecl(this,
657 LVFlags::CreateOnlyDeclLinkage()).linkage();
658 HasCachedLinkage = 1;
659 return Linkage(CachedLinkage);
660}
661
John McCallaf146032010-10-30 11:50:40 +0000662LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000663 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000664 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000665 HasCachedLinkage = 1;
666 CachedLinkage = LI.linkage();
667 return LI;
John McCall0df95872010-10-29 00:29:13 +0000668}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000669
John McCall36987482010-11-02 01:45:15 +0000670static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000671 // Objective-C: treat all Objective-C declarations as having external
672 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000673 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000674 default:
675 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000676 case Decl::TemplateTemplateParm: // count these as external
677 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000678 case Decl::ObjCAtDefsField:
679 case Decl::ObjCCategory:
680 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000681 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000682 case Decl::ObjCForwardProtocol:
683 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000684 case Decl::ObjCMethod:
685 case Decl::ObjCProperty:
686 case Decl::ObjCPropertyImpl:
687 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000688 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000689 }
690
Douglas Gregord85b5b92009-11-25 22:24:25 +0000691 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000692 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000693 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000694
695 // C++ [basic.link]p5:
696 // In addition, a member function, static data member, a named
697 // class or enumeration of class scope, or an unnamed class or
698 // enumeration defined in a class-scope typedef declaration such
699 // that the class or enumeration has the typedef name for linkage
700 // purposes (7.1.3), has external linkage if the name of the class
701 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000702 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000703 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000704
705 // C++ [basic.link]p6:
706 // The name of a function declared in block scope and the name of
707 // an object declared by a block scope extern declaration have
708 // linkage. If there is a visible declaration of an entity with
709 // linkage having the same name and type, ignoring entities
710 // declared outside the innermost enclosing namespace scope, the
711 // block scope declaration declares that same entity and receives
712 // the linkage of the previous declaration. If there is more than
713 // one such matching entity, the program is ill-formed. Otherwise,
714 // if no matching entity is found, the block scope entity receives
715 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000716 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
717 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000718 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000719 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000720
John McCallaf146032010-10-30 11:50:40 +0000721 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000722 if (Flags.ConsiderVisibilityAttributes) {
723 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
724 LV.setVisibility(GetVisibilityFromAttr(VA));
725 }
726
John McCall1fb0caa2010-10-22 21:05:15 +0000727 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000728 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000729 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
730 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000731 }
732
733 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000734 }
735
John McCall0df95872010-10-29 00:29:13 +0000736 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000737 if (Var->getStorageClass() == SC_Extern ||
738 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000739 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000740 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000741
John McCallaf146032010-10-30 11:50:40 +0000742 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000743 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000744 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000745 else if (Flags.ConsiderVisibilityAttributes) {
746 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
747 LV.setVisibility(GetVisibilityFromAttr(VA));
748 }
749
John McCall1fb0caa2010-10-22 21:05:15 +0000750 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000751 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000752 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
753 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000754 }
755
756 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000757 }
758 }
759
760 // C++ [basic.link]p6:
761 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000762 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000763}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000764
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000765std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000766 return getQualifiedNameAsString(getASTContext().getLangOptions());
767}
768
769std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000770 const DeclContext *Ctx = getDeclContext();
771
772 if (Ctx->isFunctionOrMethod())
773 return getNameAsString();
774
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000775 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
776 ContextsTy Contexts;
777
778 // Collect contexts.
779 while (Ctx && isa<NamedDecl>(Ctx)) {
780 Contexts.push_back(Ctx);
781 Ctx = Ctx->getParent();
782 };
783
784 std::string QualName;
785 llvm::raw_string_ostream OS(QualName);
786
787 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
788 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000789 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000790 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000791 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
792 std::string TemplateArgsStr
793 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000794 TemplateArgs.data(),
795 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000796 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000797 OS << Spec->getName() << TemplateArgsStr;
798 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000799 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000800 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000801 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000802 OS << ND;
803 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
804 if (!RD->getIdentifier())
805 OS << "<anonymous " << RD->getKindName() << '>';
806 else
807 OS << RD;
808 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000809 const FunctionProtoType *FT = 0;
810 if (FD->hasWrittenPrototype())
811 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
812
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000813 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000814 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000815 unsigned NumParams = FD->getNumParams();
816 for (unsigned i = 0; i < NumParams; ++i) {
817 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000818 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000819 std::string Param;
820 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000821 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000822 }
823
824 if (FT->isVariadic()) {
825 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000826 OS << ", ";
827 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000828 }
829 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000830 OS << ')';
831 } else {
832 OS << cast<NamedDecl>(*I);
833 }
834 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000835 }
836
John McCall8472af42010-03-16 21:48:18 +0000837 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000838 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000839 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000840 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000841
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000842 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000843}
844
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000845bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000846 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
847
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000848 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
849 // We want to keep it, unless it nominates same namespace.
850 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000851 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
852 ->getOriginalNamespace() ==
853 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
854 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000857 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
858 // For function declarations, we keep track of redeclarations.
859 return FD->getPreviousDeclaration() == OldD;
860
Douglas Gregore53060f2009-06-25 22:08:12 +0000861 // For function templates, the underlying function declarations are linked.
862 if (const FunctionTemplateDecl *FunctionTemplate
863 = dyn_cast<FunctionTemplateDecl>(this))
864 if (const FunctionTemplateDecl *OldFunctionTemplate
865 = dyn_cast<FunctionTemplateDecl>(OldD))
866 return FunctionTemplate->getTemplatedDecl()
867 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Steve Naroff0de21fd2009-02-22 19:35:57 +0000869 // For method declarations, we keep track of redeclarations.
870 if (isa<ObjCMethodDecl>(this))
871 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
John McCallf36e02d2009-10-09 21:13:30 +0000873 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
874 return true;
875
John McCall9488ea12009-11-17 05:59:44 +0000876 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
877 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
878 cast<UsingShadowDecl>(OldD)->getTargetDecl();
879
Douglas Gregordc355712011-02-25 00:36:19 +0000880 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
881 ASTContext &Context = getASTContext();
882 return Context.getCanonicalNestedNameSpecifier(
883 cast<UsingDecl>(this)->getQualifier()) ==
884 Context.getCanonicalNestedNameSpecifier(
885 cast<UsingDecl>(OldD)->getQualifier());
886 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000887
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000888 // For non-function declarations, if the declarations are of the
889 // same kind then this must be a redeclaration, or semantic analysis
890 // would not have given us the new declaration.
891 return this->getKind() == OldD->getKind();
892}
893
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000894bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000895 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000896}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000897
Anders Carlssone136e0e2009-06-26 06:29:23 +0000898NamedDecl *NamedDecl::getUnderlyingDecl() {
899 NamedDecl *ND = this;
900 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000901 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000902 ND = UD->getTargetDecl();
903 else if (ObjCCompatibleAliasDecl *AD
904 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
905 return AD->getClassInterface();
906 else
907 return ND;
908 }
909}
910
John McCall161755a2010-04-06 21:38:20 +0000911bool NamedDecl::isCXXInstanceMember() const {
912 assert(isCXXClassMember() &&
913 "checking whether non-member is instance member");
914
915 const NamedDecl *D = this;
916 if (isa<UsingShadowDecl>(D))
917 D = cast<UsingShadowDecl>(D)->getTargetDecl();
918
Francois Pichet87c2e122010-11-21 06:08:52 +0000919 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000920 return true;
921 if (isa<CXXMethodDecl>(D))
922 return cast<CXXMethodDecl>(D)->isInstance();
923 if (isa<FunctionTemplateDecl>(D))
924 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
925 ->getTemplatedDecl())->isInstance();
926 return false;
927}
928
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000929//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000930// DeclaratorDecl Implementation
931//===----------------------------------------------------------------------===//
932
Douglas Gregor1693e152010-07-06 18:42:40 +0000933template <typename DeclT>
934static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
935 if (decl->getNumTemplateParameterLists() > 0)
936 return decl->getTemplateParameterList(0)->getTemplateLoc();
937 else
938 return decl->getInnerLocStart();
939}
940
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000941SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000942 TypeSourceInfo *TSI = getTypeSourceInfo();
943 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000944 return SourceLocation();
945}
946
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000947void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
948 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +0000949 // Make sure the extended decl info is allocated.
950 if (!hasExtInfo()) {
951 // Save (non-extended) type source info pointer.
952 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
953 // Allocate external info struct.
954 DeclInfo = new (getASTContext()) ExtInfo;
955 // Restore savedTInfo into (extended) decl info.
956 getExtInfo()->TInfo = savedTInfo;
957 }
958 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000959 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +0000960 }
961 else {
962 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +0000963 if (hasExtInfo()) {
964 // Save type source info pointer.
965 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
966 // Deallocate the extended decl info.
967 getASTContext().Deallocate(getExtInfo());
968 // Restore savedTInfo into (non-extended) decl info.
969 DeclInfo = savedTInfo;
970 }
971 }
972}
973
Douglas Gregor1693e152010-07-06 18:42:40 +0000974SourceLocation DeclaratorDecl::getOuterLocStart() const {
975 return getTemplateOrInnerLocStart(this);
976}
977
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000978namespace {
979
980// Helper function: returns true if QT is or contains a type
981// having a postfix component.
982bool typeIsPostfix(clang::QualType QT) {
983 while (true) {
984 const Type* T = QT.getTypePtr();
985 switch (T->getTypeClass()) {
986 default:
987 return false;
988 case Type::Pointer:
989 QT = cast<PointerType>(T)->getPointeeType();
990 break;
991 case Type::BlockPointer:
992 QT = cast<BlockPointerType>(T)->getPointeeType();
993 break;
994 case Type::MemberPointer:
995 QT = cast<MemberPointerType>(T)->getPointeeType();
996 break;
997 case Type::LValueReference:
998 case Type::RValueReference:
999 QT = cast<ReferenceType>(T)->getPointeeType();
1000 break;
1001 case Type::PackExpansion:
1002 QT = cast<PackExpansionType>(T)->getPattern();
1003 break;
1004 case Type::Paren:
1005 case Type::ConstantArray:
1006 case Type::DependentSizedArray:
1007 case Type::IncompleteArray:
1008 case Type::VariableArray:
1009 case Type::FunctionProto:
1010 case Type::FunctionNoProto:
1011 return true;
1012 }
1013 }
1014}
1015
1016} // namespace
1017
1018SourceRange DeclaratorDecl::getSourceRange() const {
1019 SourceLocation RangeEnd = getLocation();
1020 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1021 if (typeIsPostfix(TInfo->getType()))
1022 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1023 }
1024 return SourceRange(getOuterLocStart(), RangeEnd);
1025}
1026
Abramo Bagnara9b934882010-06-12 08:15:14 +00001027void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001028QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1029 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001030 TemplateParameterList **TPLists) {
1031 assert((NumTPLists == 0 || TPLists != 0) &&
1032 "Empty array of template parameters with positive size!");
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001033 assert((NumTPLists == 0 || QualifierLoc) &&
Abramo Bagnara9b934882010-06-12 08:15:14 +00001034 "Nonempty array of template parameters with no qualifier!");
1035
1036 // Free previous template parameters (if any).
1037 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001038 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001039 TemplParamLists = 0;
1040 NumTemplParamLists = 0;
1041 }
1042 // Set info on matched template parameter lists (if any).
1043 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001044 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001045 NumTemplParamLists = NumTPLists;
1046 for (unsigned i = NumTPLists; i-- > 0; )
1047 TemplParamLists[i] = TPLists[i];
1048 }
1049}
1050
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001051//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001052// VarDecl Implementation
1053//===----------------------------------------------------------------------===//
1054
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001055const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1056 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +00001057 case SC_None: break;
1058 case SC_Auto: return "auto"; break;
1059 case SC_Extern: return "extern"; break;
1060 case SC_PrivateExtern: return "__private_extern__"; break;
1061 case SC_Register: return "register"; break;
1062 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001063 }
1064
1065 assert(0 && "Invalid storage class");
1066 return 0;
1067}
1068
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001069VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1070 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001071 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001072 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001073 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001074}
1075
Douglas Gregor381d34e2010-12-06 18:36:25 +00001076void VarDecl::setStorageClass(StorageClass SC) {
1077 assert(isLegalForVariable(SC));
1078 if (getStorageClass() != SC)
1079 ClearLinkageCache();
1080
1081 SClass = SC;
1082}
1083
Douglas Gregor1693e152010-07-06 18:42:40 +00001084SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001085 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001086 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001087 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001088}
1089
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001090bool VarDecl::isExternC() const {
1091 ASTContext &Context = getASTContext();
1092 if (!Context.getLangOptions().CPlusPlus)
1093 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +00001094 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001095 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1096
Chandler Carruth10aad442011-02-25 00:05:02 +00001097 const DeclContext *DC = getDeclContext();
1098 if (DC->isFunctionOrMethod())
1099 return false;
1100
1101 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001102 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1103 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001104 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001105
1106 break;
1107 }
1108
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001109 }
1110
1111 return false;
1112}
1113
1114VarDecl *VarDecl::getCanonicalDecl() {
1115 return getFirstDeclaration();
1116}
1117
Sebastian Redle9d12b62010-01-31 22:27:38 +00001118VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1119 // C++ [basic.def]p2:
1120 // A declaration is a definition unless [...] it contains the 'extern'
1121 // specifier or a linkage-specification and neither an initializer [...],
1122 // it declares a static data member in a class declaration [...].
1123 // C++ [temp.expl.spec]p15:
1124 // An explicit specialization of a static data member of a template is a
1125 // definition if the declaration includes an initializer; otherwise, it is
1126 // a declaration.
1127 if (isStaticDataMember()) {
1128 if (isOutOfLine() && (hasInit() ||
1129 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1130 return Definition;
1131 else
1132 return DeclarationOnly;
1133 }
1134 // C99 6.7p5:
1135 // A definition of an identifier is a declaration for that identifier that
1136 // [...] causes storage to be reserved for that object.
1137 // Note: that applies for all non-file-scope objects.
1138 // C99 6.9.2p1:
1139 // If the declaration of an identifier for an object has file scope and an
1140 // initializer, the declaration is an external definition for the identifier
1141 if (hasInit())
1142 return Definition;
1143 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1144 if (hasExternalStorage())
1145 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001146
John McCalld931b082010-08-26 03:08:43 +00001147 if (getStorageClassAsWritten() == SC_Extern ||
1148 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001149 for (const VarDecl *PrevVar = getPreviousDeclaration();
1150 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1151 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1152 return DeclarationOnly;
1153 }
1154 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001155 // C99 6.9.2p2:
1156 // A declaration of an object that has file scope without an initializer,
1157 // and without a storage class specifier or the scs 'static', constitutes
1158 // a tentative definition.
1159 // No such thing in C++.
1160 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1161 return TentativeDefinition;
1162
1163 // What's left is (in C, block-scope) declarations without initializers or
1164 // external storage. These are definitions.
1165 return Definition;
1166}
1167
Sebastian Redle9d12b62010-01-31 22:27:38 +00001168VarDecl *VarDecl::getActingDefinition() {
1169 DefinitionKind Kind = isThisDeclarationADefinition();
1170 if (Kind != TentativeDefinition)
1171 return 0;
1172
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001173 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001174 VarDecl *First = getFirstDeclaration();
1175 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1176 I != E; ++I) {
1177 Kind = (*I)->isThisDeclarationADefinition();
1178 if (Kind == Definition)
1179 return 0;
1180 else if (Kind == TentativeDefinition)
1181 LastTentative = *I;
1182 }
1183 return LastTentative;
1184}
1185
1186bool VarDecl::isTentativeDefinitionNow() const {
1187 DefinitionKind Kind = isThisDeclarationADefinition();
1188 if (Kind != TentativeDefinition)
1189 return false;
1190
1191 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1192 if ((*I)->isThisDeclarationADefinition() == Definition)
1193 return false;
1194 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001195 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001196}
1197
Sebastian Redl31310a22010-02-01 20:16:42 +00001198VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001199 VarDecl *First = getFirstDeclaration();
1200 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1201 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001202 if ((*I)->isThisDeclarationADefinition() == Definition)
1203 return *I;
1204 }
1205 return 0;
1206}
1207
John McCall110e8e52010-10-29 22:22:43 +00001208VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1209 DefinitionKind Kind = DeclarationOnly;
1210
1211 const VarDecl *First = getFirstDeclaration();
1212 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1213 I != E; ++I)
1214 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1215
1216 return Kind;
1217}
1218
Sebastian Redl31310a22010-02-01 20:16:42 +00001219const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001220 redecl_iterator I = redecls_begin(), E = redecls_end();
1221 while (I != E && !I->getInit())
1222 ++I;
1223
1224 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001225 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001226 return I->getInit();
1227 }
1228 return 0;
1229}
1230
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001231bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001232 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001233 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001234
1235 if (!isStaticDataMember())
1236 return false;
1237
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001238 // If this static data member was instantiated from a static data member of
1239 // a class template, check whether that static data member was defined
1240 // out-of-line.
1241 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1242 return VD->isOutOfLine();
1243
1244 return false;
1245}
1246
Douglas Gregor0d035142009-10-27 18:42:08 +00001247VarDecl *VarDecl::getOutOfLineDefinition() {
1248 if (!isStaticDataMember())
1249 return 0;
1250
1251 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1252 RD != RDEnd; ++RD) {
1253 if (RD->getLexicalDeclContext()->isFileContext())
1254 return *RD;
1255 }
1256
1257 return 0;
1258}
1259
Douglas Gregor838db382010-02-11 01:19:42 +00001260void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001261 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1262 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001263 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001264 }
1265
1266 Init = I;
1267}
1268
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001269VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001270 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001271 return cast<VarDecl>(MSI->getInstantiatedFrom());
1272
1273 return 0;
1274}
1275
Douglas Gregor663b5a02009-10-14 20:14:33 +00001276TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001277 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001278 return MSI->getTemplateSpecializationKind();
1279
1280 return TSK_Undeclared;
1281}
1282
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001283MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001284 return getASTContext().getInstantiatedFromStaticDataMember(this);
1285}
1286
Douglas Gregor0a897e32009-10-15 17:21:20 +00001287void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1288 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001289 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001290 assert(MSI && "Not an instantiated static data member?");
1291 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001292 if (TSK != TSK_ExplicitSpecialization &&
1293 PointOfInstantiation.isValid() &&
1294 MSI->getPointOfInstantiation().isInvalid())
1295 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001296}
1297
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001298//===----------------------------------------------------------------------===//
1299// ParmVarDecl Implementation
1300//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001301
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001302ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001303 SourceLocation StartLoc,
1304 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001305 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001306 StorageClass S, StorageClass SCAsWritten,
1307 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001308 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001309 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001310}
1311
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001312Expr *ParmVarDecl::getDefaultArg() {
1313 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1314 assert(!hasUninstantiatedDefaultArg() &&
1315 "Default argument is not yet instantiated!");
1316
1317 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001318 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001319 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001320
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001321 return Arg;
1322}
1323
1324unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001325 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001326 return E->getNumTemporaries();
1327
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001328 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001329}
1330
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001331CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1332 assert(getNumDefaultArgTemporaries() &&
1333 "Default arguments does not have any temporaries!");
1334
John McCall4765fa02010-12-06 08:20:24 +00001335 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001336 return E->getTemporary(i);
1337}
1338
1339SourceRange ParmVarDecl::getDefaultArgRange() const {
1340 if (const Expr *E = getInit())
1341 return E->getSourceRange();
1342
1343 if (hasUninstantiatedDefaultArg())
1344 return getUninstantiatedDefaultArg()->getSourceRange();
1345
1346 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001347}
1348
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001349bool ParmVarDecl::isParameterPack() const {
1350 return isa<PackExpansionType>(getType());
1351}
1352
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001353//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001354// FunctionDecl Implementation
1355//===----------------------------------------------------------------------===//
1356
Douglas Gregorda2142f2011-02-19 18:51:44 +00001357void FunctionDecl::getNameForDiagnostic(std::string &S,
1358 const PrintingPolicy &Policy,
1359 bool Qualified) const {
1360 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1361 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1362 if (TemplateArgs)
1363 S += TemplateSpecializationType::PrintTemplateArgumentList(
1364 TemplateArgs->data(),
1365 TemplateArgs->size(),
1366 Policy);
1367
1368}
1369
Ted Kremenek9498d382010-04-29 16:49:01 +00001370bool FunctionDecl::isVariadic() const {
1371 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1372 return FT->isVariadic();
1373 return false;
1374}
1375
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001376bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1377 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1378 if (I->Body) {
1379 Definition = *I;
1380 return true;
1381 }
1382 }
1383
1384 return false;
1385}
1386
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001387Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001388 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1389 if (I->Body) {
1390 Definition = *I;
1391 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001392 }
1393 }
1394
1395 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001396}
1397
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001398void FunctionDecl::setBody(Stmt *B) {
1399 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001400 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001401 EndRangeLoc = B->getLocEnd();
1402}
1403
Douglas Gregor21386642010-09-28 21:55:22 +00001404void FunctionDecl::setPure(bool P) {
1405 IsPure = P;
1406 if (P)
1407 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1408 Parent->markedVirtualFunctionPure();
1409}
1410
Douglas Gregor48a83b52009-09-12 00:17:51 +00001411bool FunctionDecl::isMain() const {
1412 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001413 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001414 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001415 getIdentifier() && getIdentifier()->isStr("main");
1416}
1417
Douglas Gregor48a83b52009-09-12 00:17:51 +00001418bool FunctionDecl::isExternC() const {
1419 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001420 // In C, any non-static, non-overloadable function has external
1421 // linkage.
1422 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001423 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001424
Chandler Carruth10aad442011-02-25 00:05:02 +00001425 const DeclContext *DC = getDeclContext();
1426 if (DC->isRecord())
1427 return false;
1428
1429 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor63935192009-03-02 00:19:53 +00001430 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1431 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001432 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001433 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001434
1435 break;
1436 }
1437 }
1438
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001439 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001440}
1441
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001442bool FunctionDecl::isGlobal() const {
1443 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1444 return Method->isStatic();
1445
John McCalld931b082010-08-26 03:08:43 +00001446 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001447 return false;
1448
Mike Stump1eb44332009-09-09 15:08:12 +00001449 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001450 DC->isNamespace();
1451 DC = DC->getParent()) {
1452 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1453 if (!Namespace->getDeclName())
1454 return false;
1455 break;
1456 }
1457 }
1458
1459 return true;
1460}
1461
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001462void
1463FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1464 redeclarable_base::setPreviousDeclaration(PrevDecl);
1465
1466 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1467 FunctionTemplateDecl *PrevFunTmpl
1468 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1469 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1470 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1471 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001472
1473 if (PrevDecl->IsInline)
1474 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001475}
1476
1477const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1478 return getFirstDeclaration();
1479}
1480
1481FunctionDecl *FunctionDecl::getCanonicalDecl() {
1482 return getFirstDeclaration();
1483}
1484
Douglas Gregor381d34e2010-12-06 18:36:25 +00001485void FunctionDecl::setStorageClass(StorageClass SC) {
1486 assert(isLegalForFunction(SC));
1487 if (getStorageClass() != SC)
1488 ClearLinkageCache();
1489
1490 SClass = SC;
1491}
1492
Douglas Gregor3e41d602009-02-13 23:20:09 +00001493/// \brief Returns a value indicating whether this function
1494/// corresponds to a builtin function.
1495///
1496/// The function corresponds to a built-in function if it is
1497/// declared at translation scope or within an extern "C" block and
1498/// its name matches with the name of a builtin. The returned value
1499/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001500/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001501/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001502unsigned FunctionDecl::getBuiltinID() const {
1503 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001504 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1505 return 0;
1506
1507 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1508 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1509 return BuiltinID;
1510
1511 // This function has the name of a known C library
1512 // function. Determine whether it actually refers to the C library
1513 // function or whether it just has the same name.
1514
Douglas Gregor9add3172009-02-17 03:23:10 +00001515 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001516 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001517 return 0;
1518
Douglas Gregor3c385e52009-02-14 18:57:46 +00001519 // If this function is at translation-unit scope and we're not in
1520 // C++, it refers to the C library function.
1521 if (!Context.getLangOptions().CPlusPlus &&
1522 getDeclContext()->isTranslationUnit())
1523 return BuiltinID;
1524
1525 // If the function is in an extern "C" linkage specification and is
1526 // not marked "overloadable", it's the real function.
1527 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001528 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001529 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001530 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001531 return BuiltinID;
1532
1533 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001534 return 0;
1535}
1536
1537
Chris Lattner1ad9b282009-04-25 06:03:53 +00001538/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001539/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001540/// after it has been created.
1541unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001542 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001543 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001544 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001545 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Reid Spencer5f016e22007-07-11 17:01:13 +00001547}
1548
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001549void FunctionDecl::setParams(ASTContext &C,
1550 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001552 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 // Zero params -> null pointer.
1555 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001556 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001557 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001559
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001560 // Update source range. The check below allows us to set EndRangeLoc before
1561 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001562 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001563 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 }
1565}
1566
Chris Lattner8123a952008-04-10 02:22:51 +00001567/// getMinRequiredArguments - Returns the minimum number of arguments
1568/// needed to call this function. This may be fewer than the number of
1569/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001570/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001571unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001572 if (!getASTContext().getLangOptions().CPlusPlus)
1573 return getNumParams();
1574
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001575 unsigned NumRequiredArgs = getNumParams();
1576
1577 // If the last parameter is a parameter pack, we don't need an argument for
1578 // it.
1579 if (NumRequiredArgs > 0 &&
1580 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1581 --NumRequiredArgs;
1582
1583 // If this parameter has a default argument, we don't need an argument for
1584 // it.
1585 while (NumRequiredArgs > 0 &&
1586 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001587 --NumRequiredArgs;
1588
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001589 // We might have parameter packs before the end. These can't be deduced,
1590 // but they can still handle multiple arguments.
1591 unsigned ArgIdx = NumRequiredArgs;
1592 while (ArgIdx > 0) {
1593 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1594 NumRequiredArgs = ArgIdx;
1595
1596 --ArgIdx;
1597 }
1598
Chris Lattner8123a952008-04-10 02:22:51 +00001599 return NumRequiredArgs;
1600}
1601
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001602bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001603 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001604 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001605
1606 if (isa<CXXMethodDecl>(this)) {
1607 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1608 return true;
1609 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001610
1611 switch (getTemplateSpecializationKind()) {
1612 case TSK_Undeclared:
1613 case TSK_ExplicitSpecialization:
1614 return false;
1615
1616 case TSK_ImplicitInstantiation:
1617 case TSK_ExplicitInstantiationDeclaration:
1618 case TSK_ExplicitInstantiationDefinition:
1619 // Handle below.
1620 break;
1621 }
1622
1623 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001624 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001625 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001626 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001627
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001628 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001629 return PatternDecl->isInlined();
1630
1631 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001632}
1633
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001634/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001635/// definition will be externally visible.
1636///
1637/// Inline function definitions are always available for inlining optimizations.
1638/// However, depending on the language dialect, declaration specifiers, and
1639/// attributes, the definition of an inline function may or may not be
1640/// "externally" visible to other translation units in the program.
1641///
1642/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001643/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001644/// inline definition becomes externally visible (C99 6.7.4p6).
1645///
1646/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1647/// definition, we use the GNU semantics for inline, which are nearly the
1648/// opposite of C99 semantics. In particular, "inline" by itself will create
1649/// an externally visible symbol, but "extern inline" will not create an
1650/// externally visible symbol.
1651bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1652 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001653 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001654 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001655
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001656 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001657 // If it's not the case that both 'inline' and 'extern' are
1658 // specified on the definition, then this inline definition is
1659 // externally visible.
1660 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1661 return true;
1662
1663 // If any declaration is 'inline' but not 'extern', then this definition
1664 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001665 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1666 Redecl != RedeclEnd;
1667 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001668 if (Redecl->isInlineSpecified() &&
1669 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001670 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001671 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001672
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001673 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001674 }
1675
1676 // C99 6.7.4p6:
1677 // [...] If all of the file scope declarations for a function in a
1678 // translation unit include the inline function specifier without extern,
1679 // then the definition in that translation unit is an inline definition.
1680 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1681 Redecl != RedeclEnd;
1682 ++Redecl) {
1683 // Only consider file-scope declarations in this test.
1684 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1685 continue;
1686
John McCalld931b082010-08-26 03:08:43 +00001687 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001688 return true; // Not an inline definition
1689 }
1690
1691 // C99 6.7.4p6:
1692 // An inline definition does not provide an external definition for the
1693 // function, and does not forbid an external definition in another
1694 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001695 return false;
1696}
1697
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001698/// getOverloadedOperator - Which C++ overloaded operator this
1699/// function represents, if any.
1700OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001701 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1702 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001703 else
1704 return OO_None;
1705}
1706
Sean Hunta6c058d2010-01-13 09:01:02 +00001707/// getLiteralIdentifier - The literal suffix identifier this function
1708/// represents, if any.
1709const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1710 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1711 return getDeclName().getCXXLiteralIdentifier();
1712 else
1713 return 0;
1714}
1715
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001716FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1717 if (TemplateOrSpecialization.isNull())
1718 return TK_NonTemplate;
1719 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1720 return TK_FunctionTemplate;
1721 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1722 return TK_MemberSpecialization;
1723 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1724 return TK_FunctionTemplateSpecialization;
1725 if (TemplateOrSpecialization.is
1726 <DependentFunctionTemplateSpecializationInfo*>())
1727 return TK_DependentFunctionTemplateSpecialization;
1728
1729 assert(false && "Did we miss a TemplateOrSpecialization type?");
1730 return TK_NonTemplate;
1731}
1732
Douglas Gregor2db32322009-10-07 23:56:10 +00001733FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001734 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001735 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1736
1737 return 0;
1738}
1739
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001740MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1741 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1742}
1743
Douglas Gregor2db32322009-10-07 23:56:10 +00001744void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001745FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1746 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001747 TemplateSpecializationKind TSK) {
1748 assert(TemplateOrSpecialization.isNull() &&
1749 "Member function is already a specialization");
1750 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001751 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001752 TemplateOrSpecialization = Info;
1753}
1754
Douglas Gregor3b846b62009-10-27 20:53:28 +00001755bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001756 // If the function is invalid, it can't be implicitly instantiated.
1757 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001758 return false;
1759
1760 switch (getTemplateSpecializationKind()) {
1761 case TSK_Undeclared:
1762 case TSK_ExplicitSpecialization:
1763 case TSK_ExplicitInstantiationDefinition:
1764 return false;
1765
1766 case TSK_ImplicitInstantiation:
1767 return true;
1768
1769 case TSK_ExplicitInstantiationDeclaration:
1770 // Handled below.
1771 break;
1772 }
1773
1774 // Find the actual template from which we will instantiate.
1775 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001776 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001777 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001778 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001779
1780 // C++0x [temp.explicit]p9:
1781 // Except for inline functions, other explicit instantiation declarations
1782 // have the effect of suppressing the implicit instantiation of the entity
1783 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001784 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001785 return true;
1786
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001787 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001788}
1789
1790FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1791 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1792 while (Primary->getInstantiatedFromMemberTemplate()) {
1793 // If we have hit a point where the user provided a specialization of
1794 // this template, we're done looking.
1795 if (Primary->isMemberSpecialization())
1796 break;
1797
1798 Primary = Primary->getInstantiatedFromMemberTemplate();
1799 }
1800
1801 return Primary->getTemplatedDecl();
1802 }
1803
1804 return getInstantiatedFromMemberFunction();
1805}
1806
Douglas Gregor16e8be22009-06-29 17:30:29 +00001807FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001808 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001809 = TemplateOrSpecialization
1810 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001811 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001812 }
1813 return 0;
1814}
1815
1816const TemplateArgumentList *
1817FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001818 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001819 = TemplateOrSpecialization
1820 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001821 return Info->TemplateArguments;
1822 }
1823 return 0;
1824}
1825
Abramo Bagnarae03db982010-05-20 15:32:11 +00001826const TemplateArgumentListInfo *
1827FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1828 if (FunctionTemplateSpecializationInfo *Info
1829 = TemplateOrSpecialization
1830 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1831 return Info->TemplateArgumentsAsWritten;
1832 }
1833 return 0;
1834}
1835
Mike Stump1eb44332009-09-09 15:08:12 +00001836void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001837FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1838 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001839 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001840 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001841 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001842 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1843 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001844 assert(TSK != TSK_Undeclared &&
1845 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001846 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001847 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001848 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001849 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1850 TemplateArgs,
1851 TemplateArgsAsWritten,
1852 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001853 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Douglas Gregor127102b2009-06-29 20:59:39 +00001855 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001856 // function template specializations.
1857 if (InsertPos)
1858 Template->getSpecializations().InsertNode(Info, InsertPos);
1859 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001860 // Try to insert the new node. If there is an existing node, leave it, the
1861 // set will contain the canonical decls while
1862 // FunctionTemplateDecl::findSpecialization will return
1863 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001864 FunctionTemplateSpecializationInfo *Existing
1865 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001866 (void)Existing;
1867 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1868 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001869 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001870}
1871
John McCallaf2094e2010-04-08 09:05:18 +00001872void
1873FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1874 const UnresolvedSetImpl &Templates,
1875 const TemplateArgumentListInfo &TemplateArgs) {
1876 assert(TemplateOrSpecialization.isNull());
1877 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1878 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001879 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001880 void *Buffer = Context.Allocate(Size);
1881 DependentFunctionTemplateSpecializationInfo *Info =
1882 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1883 TemplateArgs);
1884 TemplateOrSpecialization = Info;
1885}
1886
1887DependentFunctionTemplateSpecializationInfo::
1888DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1889 const TemplateArgumentListInfo &TArgs)
1890 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1891
1892 d.NumTemplates = Ts.size();
1893 d.NumArgs = TArgs.size();
1894
1895 FunctionTemplateDecl **TsArray =
1896 const_cast<FunctionTemplateDecl**>(getTemplates());
1897 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1898 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1899
1900 TemplateArgumentLoc *ArgsArray =
1901 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1902 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1903 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1904}
1905
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001906TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001907 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001908 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001909 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001910 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001911 if (FTSInfo)
1912 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Douglas Gregor2db32322009-10-07 23:56:10 +00001914 MemberSpecializationInfo *MSInfo
1915 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1916 if (MSInfo)
1917 return MSInfo->getTemplateSpecializationKind();
1918
1919 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001920}
1921
Mike Stump1eb44332009-09-09 15:08:12 +00001922void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001923FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1924 SourceLocation PointOfInstantiation) {
1925 if (FunctionTemplateSpecializationInfo *FTSInfo
1926 = TemplateOrSpecialization.dyn_cast<
1927 FunctionTemplateSpecializationInfo*>()) {
1928 FTSInfo->setTemplateSpecializationKind(TSK);
1929 if (TSK != TSK_ExplicitSpecialization &&
1930 PointOfInstantiation.isValid() &&
1931 FTSInfo->getPointOfInstantiation().isInvalid())
1932 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1933 } else if (MemberSpecializationInfo *MSInfo
1934 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1935 MSInfo->setTemplateSpecializationKind(TSK);
1936 if (TSK != TSK_ExplicitSpecialization &&
1937 PointOfInstantiation.isValid() &&
1938 MSInfo->getPointOfInstantiation().isInvalid())
1939 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1940 } else
1941 assert(false && "Function cannot have a template specialization kind");
1942}
1943
1944SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001945 if (FunctionTemplateSpecializationInfo *FTSInfo
1946 = TemplateOrSpecialization.dyn_cast<
1947 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001948 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001949 else if (MemberSpecializationInfo *MSInfo
1950 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001951 return MSInfo->getPointOfInstantiation();
1952
1953 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001954}
1955
Douglas Gregor9f185072009-09-11 20:15:17 +00001956bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001957 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00001958 return true;
1959
1960 // If this function was instantiated from a member function of a
1961 // class template, check whether that member function was defined out-of-line.
1962 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1963 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001964 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001965 return Definition->isOutOfLine();
1966 }
1967
1968 // If this function was instantiated from a function template,
1969 // check whether that function template was defined out-of-line.
1970 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1971 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001972 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001973 return Definition->isOutOfLine();
1974 }
1975
1976 return false;
1977}
1978
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001979SourceRange FunctionDecl::getSourceRange() const {
1980 return SourceRange(getOuterLocStart(), EndRangeLoc);
1981}
1982
Chris Lattner8a934232008-03-31 00:36:02 +00001983//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001984// FieldDecl Implementation
1985//===----------------------------------------------------------------------===//
1986
Jay Foad4ba2a172011-01-12 09:06:06 +00001987FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001988 SourceLocation StartLoc, SourceLocation IdLoc,
1989 IdentifierInfo *Id, QualType T,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001990 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001991 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
1992 BW, Mutable);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001993}
1994
1995bool FieldDecl::isAnonymousStructOrUnion() const {
1996 if (!isImplicit() || getDeclName())
1997 return false;
1998
1999 if (const RecordType *Record = getType()->getAs<RecordType>())
2000 return Record->getDecl()->isAnonymousStructOrUnion();
2001
2002 return false;
2003}
2004
John McCallba4f5d52011-01-20 07:57:12 +00002005unsigned FieldDecl::getFieldIndex() const {
2006 if (CachedFieldIndex) return CachedFieldIndex - 1;
2007
2008 unsigned index = 0;
2009 RecordDecl::field_iterator
2010 i = getParent()->field_begin(), e = getParent()->field_end();
2011 while (true) {
2012 assert(i != e && "failed to find field in parent!");
2013 if (*i == this)
2014 break;
2015
2016 ++i;
2017 ++index;
2018 }
2019
2020 CachedFieldIndex = index + 1;
2021 return index;
2022}
2023
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002024SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002025 if (isBitField())
2026 return SourceRange(getInnerLocStart(), BitWidth->getLocEnd());
2027 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002028}
2029
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002030//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002031// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002032//===----------------------------------------------------------------------===//
2033
Douglas Gregor1693e152010-07-06 18:42:40 +00002034SourceLocation TagDecl::getOuterLocStart() const {
2035 return getTemplateOrInnerLocStart(this);
2036}
2037
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002038SourceRange TagDecl::getSourceRange() const {
2039 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002040 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002041}
2042
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002043TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002044 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002045}
2046
Douglas Gregor60e70642010-05-19 18:39:18 +00002047void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
2048 TypedefDeclOrQualifier = TDD;
2049 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002050 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002051 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002052}
2053
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002054void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002055 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002056
2057 if (isa<CXXRecordDecl>(this)) {
2058 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2059 struct CXXRecordDecl::DefinitionData *Data =
2060 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002061 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2062 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002063 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002064}
2065
2066void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002067 assert((!isa<CXXRecordDecl>(this) ||
2068 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2069 "definition completed but not started");
2070
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002071 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002072 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002073
2074 if (ASTMutationListener *L = getASTMutationListener())
2075 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002076}
2077
Douglas Gregor952b0172010-02-11 01:04:33 +00002078TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002079 if (isDefinition())
2080 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002081 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2082 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002083
2084 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002085 R != REnd; ++R)
2086 if (R->isDefinition())
2087 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002089 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002090}
2091
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002092void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2093 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002094 // Make sure the extended qualifier info is allocated.
2095 if (!hasExtInfo())
2096 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2097 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002098 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002099 }
2100 else {
2101 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002102 if (hasExtInfo()) {
2103 getASTContext().Deallocate(getExtInfo());
2104 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2105 }
2106 }
2107}
2108
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002109//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002110// EnumDecl Implementation
2111//===----------------------------------------------------------------------===//
2112
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002113EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2114 SourceLocation StartLoc, SourceLocation IdLoc,
2115 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002116 EnumDecl *PrevDecl, bool IsScoped,
2117 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002118 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002119 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002120 C.getTypeDeclType(Enum, PrevDecl);
2121 return Enum;
2122}
2123
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002124EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002125 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002126 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002127}
2128
Douglas Gregor838db382010-02-11 01:19:42 +00002129void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002130 QualType NewPromotionType,
2131 unsigned NumPositiveBits,
2132 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002133 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002134 if (!IntegerType)
2135 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002136 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002137 setNumPositiveBits(NumPositiveBits);
2138 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002139 TagDecl::completeDefinition();
2140}
2141
2142//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002143// RecordDecl Implementation
2144//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002145
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002146RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2147 SourceLocation StartLoc, SourceLocation IdLoc,
2148 IdentifierInfo *Id, RecordDecl *PrevDecl)
2149 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002150 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002151 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002152 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002153 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002154 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002155}
2156
Jay Foad4ba2a172011-01-12 09:06:06 +00002157RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002158 SourceLocation StartLoc, SourceLocation IdLoc,
2159 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2160 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2161 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002162 C.getTypeDeclType(R, PrevDecl);
2163 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002164}
2165
Jay Foad4ba2a172011-01-12 09:06:06 +00002166RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002167 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2168 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002169}
2170
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002171bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002172 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002173 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2174}
2175
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002176RecordDecl::field_iterator RecordDecl::field_begin() const {
2177 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2178 LoadFieldsFromExternalStorage();
2179
2180 return field_iterator(decl_iterator(FirstDecl));
2181}
2182
Douglas Gregorda2142f2011-02-19 18:51:44 +00002183/// completeDefinition - Notes that the definition of this type is now
2184/// complete.
2185void RecordDecl::completeDefinition() {
2186 assert(!isDefinition() && "Cannot redefine record!");
2187 TagDecl::completeDefinition();
2188}
2189
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002190void RecordDecl::LoadFieldsFromExternalStorage() const {
2191 ExternalASTSource *Source = getASTContext().getExternalSource();
2192 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2193
2194 // Notify that we have a RecordDecl doing some initialization.
2195 ExternalASTSource::Deserializing TheFields(Source);
2196
2197 llvm::SmallVector<Decl*, 64> Decls;
2198 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2199 return;
2200
2201#ifndef NDEBUG
2202 // Check that all decls we got were FieldDecls.
2203 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2204 assert(isa<FieldDecl>(Decls[i]));
2205#endif
2206
2207 LoadedFieldsFromExternalStorage = true;
2208
2209 if (Decls.empty())
2210 return;
2211
2212 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2213}
2214
Steve Naroff56ee6892008-10-08 17:01:13 +00002215//===----------------------------------------------------------------------===//
2216// BlockDecl Implementation
2217//===----------------------------------------------------------------------===//
2218
Douglas Gregor838db382010-02-11 01:19:42 +00002219void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002220 unsigned NParms) {
2221 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Steve Naroffe78b8092009-03-13 16:56:44 +00002223 // Zero params -> null pointer.
2224 if (NParms) {
2225 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002226 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002227 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2228 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2229 }
2230}
2231
John McCall6b5a61b2011-02-07 10:33:21 +00002232void BlockDecl::setCaptures(ASTContext &Context,
2233 const Capture *begin,
2234 const Capture *end,
2235 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002236 CapturesCXXThis = capturesCXXThis;
2237
2238 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002239 NumCaptures = 0;
2240 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002241 return;
2242 }
2243
John McCall6b5a61b2011-02-07 10:33:21 +00002244 NumCaptures = end - begin;
2245
2246 // Avoid new Capture[] because we don't want to provide a default
2247 // constructor.
2248 size_t allocationSize = NumCaptures * sizeof(Capture);
2249 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2250 memcpy(buffer, begin, allocationSize);
2251 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002252}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002253
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002254SourceRange BlockDecl::getSourceRange() const {
2255 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2256}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002257
2258//===----------------------------------------------------------------------===//
2259// Other Decl Allocation/Deallocation Method Implementations
2260//===----------------------------------------------------------------------===//
2261
2262TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2263 return new (C) TranslationUnitDecl(C);
2264}
2265
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002266LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002267 SourceLocation IdentL, IdentifierInfo *II) {
2268 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2269}
2270
2271LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2272 SourceLocation IdentL, IdentifierInfo *II,
2273 SourceLocation GnuLabelL) {
2274 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2275 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002276}
2277
2278
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002279NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002280 SourceLocation StartLoc,
2281 SourceLocation IdLoc, IdentifierInfo *Id) {
2282 return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002283}
2284
Douglas Gregor06c91932010-10-27 19:49:05 +00002285NamespaceDecl *NamespaceDecl::getNextNamespace() {
2286 return dyn_cast_or_null<NamespaceDecl>(
2287 NextNamespace.get(getASTContext().getExternalSource()));
2288}
2289
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002290ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002291 SourceLocation IdLoc,
2292 IdentifierInfo *Id,
2293 QualType Type) {
2294 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002295}
2296
2297FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002298 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002299 const DeclarationNameInfo &NameInfo,
2300 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002301 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002302 bool isInlineSpecified,
2303 bool hasWrittenPrototype) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002304 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2305 T, TInfo, SC, SCAsWritten,
2306 isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002307 New->HasWrittenPrototype = hasWrittenPrototype;
2308 return New;
2309}
2310
2311BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2312 return new (C) BlockDecl(DC, L);
2313}
2314
2315EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2316 SourceLocation L,
2317 IdentifierInfo *Id, QualType T,
2318 Expr *E, const llvm::APSInt &V) {
2319 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2320}
2321
Benjamin Kramerd9811462010-11-21 14:11:41 +00002322IndirectFieldDecl *
2323IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2324 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2325 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002326 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2327}
2328
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002329SourceRange EnumConstantDecl::getSourceRange() const {
2330 SourceLocation End = getLocation();
2331 if (Init)
2332 End = Init->getLocEnd();
2333 return SourceRange(getLocation(), End);
2334}
2335
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002336TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002337 SourceLocation StartLoc, SourceLocation IdLoc,
2338 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2339 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002340}
2341
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002342SourceRange TypedefDecl::getSourceRange() const {
2343 SourceLocation RangeEnd = getLocation();
2344 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2345 if (typeIsPostfix(TInfo->getType()))
2346 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2347 }
2348 return SourceRange(getLocStart(), RangeEnd);
2349}
2350
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002351FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002352 StringLiteral *Str,
2353 SourceLocation AsmLoc,
2354 SourceLocation RParenLoc) {
2355 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002356}