blob: f26998081f8ddc964d3ac259f10988a3760ae9ff [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"
Douglas Gregor15de72c2011-12-02 23:23:56 +000027#include "clang/Basic/Module.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000028#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000029#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000031
David Blaikie4278c652011-09-21 18:16:56 +000032#include <algorithm>
33
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattnerd3b90652008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor4421d2b2011-03-26 12:10:19 +000040static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
41 // If this declaration has an explicit visibility attribute, use it.
42 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
43 switch (A->getVisibility()) {
44 case VisibilityAttr::Default:
45 return DefaultVisibility;
46 case VisibilityAttr::Hidden:
47 return HiddenVisibility;
48 case VisibilityAttr::Protected:
49 return ProtectedVisibility;
50 }
John McCall1fb0caa2010-10-22 21:05:15 +000051 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +000052
53 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
54 // implies visibility(default).
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +000056 for (specific_attr_iterator<AvailabilityAttr>
57 A = D->specific_attr_begin<AvailabilityAttr>(),
58 AEnd = D->specific_attr_end<AvailabilityAttr>();
59 A != AEnd; ++A)
60 if ((*A)->getPlatform()->getName().equals("macosx"))
61 return DefaultVisibility;
62 }
63
64 return llvm::Optional<Visibility>();
John McCall1fb0caa2010-10-22 21:05:15 +000065}
66
John McCallaf146032010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallaf146032010-10-30 11:50:40 +000068
Benjamin Kramer752c2e92010-11-05 19:56:37 +000069namespace {
John McCall36987482010-11-02 01:45:15 +000070/// Flags controlling the computation of linkage and visibility.
71struct LVFlags {
72 bool ConsiderGlobalVisibility;
73 bool ConsiderVisibilityAttributes;
John McCall1a0918a2011-03-04 10:39:25 +000074 bool ConsiderTemplateParameterTypes;
John McCall36987482010-11-02 01:45:15 +000075
76 LVFlags() : ConsiderGlobalVisibility(true),
John McCall1a0918a2011-03-04 10:39:25 +000077 ConsiderVisibilityAttributes(true),
78 ConsiderTemplateParameterTypes(true) {
John McCall36987482010-11-02 01:45:15 +000079 }
80
Rafael Espindola62d9f112012-04-16 13:44:41 +000081 LVFlags(bool Global, bool Attributes, bool Parameters) :
82 ConsiderGlobalVisibility(Global),
83 ConsiderVisibilityAttributes(Attributes),
84 ConsiderTemplateParameterTypes(Parameters) {
85 }
86
Douglas Gregor381d34e2010-12-06 18:36:25 +000087 /// \brief Returns a set of flags that is only useful for computing the
88 /// linkage, not the visibility, of a declaration.
89 static LVFlags CreateOnlyDeclLinkage() {
Rafael Espindola62d9f112012-04-16 13:44:41 +000090 return LVFlags(false, false, false);
John McCall36987482010-11-02 01:45:15 +000091 }
Douglas Gregor89d63e52010-12-06 18:50:56 +000092};
Benjamin Kramer752c2e92010-11-05 19:56:37 +000093} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +000094
Rafael Espindola093ecc92012-01-14 00:30:36 +000095static LinkageInfo getLVForType(QualType T) {
96 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
97 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
98}
99
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000100/// \brief Get the most restrictive linkage for the types in the given
101/// template parameter list.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000102static LinkageInfo
John McCall1fb0caa2010-10-22 21:05:15 +0000103getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000104 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000105 for (TemplateParameterList::const_iterator P = Params->begin(),
106 PEnd = Params->end();
107 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000108 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
109 if (NTTP->isExpandedParameterPack()) {
110 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
111 QualType T = NTTP->getExpansionType(I);
112 if (!T->isDependentType())
Rafael Espindola093ecc92012-01-14 00:30:36 +0000113 LV.merge(getLVForType(T));
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000114 }
115 continue;
116 }
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000117
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000118 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000119 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000120 continue;
121 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000122 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000123
124 if (TemplateTemplateParmDecl *TTP
125 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000126 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000127 }
128 }
129
John McCall1fb0caa2010-10-22 21:05:15 +0000130 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000131}
132
Douglas Gregor381d34e2010-12-06 18:36:25 +0000133/// getLVForDecl - Get the linkage and visibility for the given declaration.
134static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
135
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000136/// \brief Get the most restrictive linkage for the types and
137/// declarations in the given template argument list.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000138static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
139 unsigned NumArgs,
140 LVFlags &F) {
141 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000142
143 for (unsigned I = 0; I != NumArgs; ++I) {
144 switch (Args[I].getKind()) {
145 case TemplateArgument::Null:
146 case TemplateArgument::Integral:
147 case TemplateArgument::Expression:
148 break;
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000149
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000150 case TemplateArgument::Type:
Rafael Espindola093ecc92012-01-14 00:30:36 +0000151 LV.merge(getLVForType(Args[I].getAsType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000152 break;
153
154 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000155 // The decl can validly be null as the representation of nullptr
156 // arguments, valid only in C++0x.
157 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor89d63e52010-12-06 18:50:56 +0000158 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
159 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000160 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000161 break;
162
163 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000164 case TemplateArgument::TemplateExpansion:
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000165 if (TemplateDecl *Template
Douglas Gregora7fc9012011-01-05 18:58:31 +0000166 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola093ecc92012-01-14 00:30:36 +0000167 LV.merge(getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000168 break;
169
170 case TemplateArgument::Pack:
Rafael Espindola860097c2012-02-23 04:17:32 +0000171 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
172 Args[I].pack_size(),
173 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000174 break;
175 }
176 }
177
John McCall1fb0caa2010-10-22 21:05:15 +0000178 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000179}
180
Rafael Espindola093ecc92012-01-14 00:30:36 +0000181static LinkageInfo
Douglas Gregor381d34e2010-12-06 18:36:25 +0000182getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
183 LVFlags &F) {
184 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000185}
186
John McCall6ce51ee2011-06-27 23:06:04 +0000187static bool shouldConsiderTemplateLV(const FunctionDecl *fn,
188 const FunctionTemplateSpecializationInfo *spec) {
189 return !(spec->isExplicitSpecialization() &&
190 fn->hasAttr<VisibilityAttr>());
191}
192
193static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
194 return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
195}
196
John McCall36987482010-11-02 01:45:15 +0000197static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000198 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000199 "Not a name having namespace scope");
200 ASTContext &Context = D->getASTContext();
201
202 // C++ [basic.link]p3:
203 // A name having namespace scope (3.3.6) has internal linkage if it
204 // is the name of
205 // - an object, reference, function or function template that is
206 // explicitly declared static; or,
207 // (This bullet corresponds to C99 6.2.2p3.)
208 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
209 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000210 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000211 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000212
213 // - an object or reference that is explicitly declared const
214 // and neither explicitly declared extern nor previously
215 // declared to have external linkage; or
216 // (there is no equivalent in C99)
David Blaikie4e4d0842012-03-11 07:00:24 +0000217 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000218 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000219 Var->getStorageClass() != SC_Extern &&
220 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000221 bool FoundExtern = false;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000222 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000223 PrevVar && !FoundExtern;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000224 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000225 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000226 FoundExtern = true;
227
228 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000229 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000230 }
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000231 if (Var->getStorageClass() == SC_None) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000232 const VarDecl *PrevVar = Var->getPreviousDecl();
233 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000234 if (PrevVar->getStorageClass() == SC_PrivateExtern)
235 break;
236 if (PrevVar)
237 return PrevVar->getLinkageAndVisibility();
238 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000239 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000240 // C++ [temp]p4:
241 // A non-member function template can have internal linkage; any
242 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000243 const FunctionDecl *Function = 0;
244 if (const FunctionTemplateDecl *FunTmpl
245 = dyn_cast<FunctionTemplateDecl>(D))
246 Function = FunTmpl->getTemplatedDecl();
247 else
248 Function = cast<FunctionDecl>(D);
249
250 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000251 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000252 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000253 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
254 // - a data member of an anonymous union.
255 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000256 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000257 }
258
Chandler Carruth094b6432011-02-24 19:03:39 +0000259 if (D->isInAnonymousNamespace()) {
260 const VarDecl *Var = dyn_cast<VarDecl>(D);
261 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman750dc2b2012-01-15 01:23:58 +0000262 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
263 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth094b6432011-02-24 19:03:39 +0000264 return LinkageInfo::uniqueExternal();
265 }
John McCalle7bc9722010-10-28 04:18:25 +0000266
John McCall1fb0caa2010-10-22 21:05:15 +0000267 // Set up the defaults.
268
269 // C99 6.2.2p5:
270 // If the declaration of an identifier for an object has file
271 // scope and no storage-class specifier, its linkage is
272 // external.
John McCallaf146032010-10-30 11:50:40 +0000273 LinkageInfo LV;
David Blaikie4e4d0842012-03-11 07:00:24 +0000274 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
John McCallaf146032010-10-30 11:50:40 +0000275
Douglas Gregord85b5b92009-11-25 22:24:25 +0000276 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000277
Douglas Gregord85b5b92009-11-25 22:24:25 +0000278 // A name having namespace scope has external linkage if it is the
279 // name of
280 //
281 // - an object or reference, unless it has internal linkage; or
282 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000283 // GCC applies the following optimization to variables and static
284 // data members, but not to functions:
285 //
John McCall1fb0caa2010-10-22 21:05:15 +0000286 // Modify the variable's LV by the LV of its type unless this is
287 // C or extern "C". This follows from [basic.link]p9:
288 // A type without linkage shall not be used as the type of a
289 // variable or function with external linkage unless
290 // - the entity has C language linkage, or
291 // - the entity is declared within an unnamed namespace, or
292 // - the entity is not used or is defined in the same
293 // translation unit.
294 // and [basic.link]p10:
295 // ...the types specified by all declarations referring to a
296 // given variable or function shall be identical...
297 // C does not have an equivalent rule.
298 //
John McCallac65c622010-10-26 04:59:26 +0000299 // Ignore this if we've got an explicit attribute; the user
300 // probably knows what they're doing.
301 //
John McCall1fb0caa2010-10-22 21:05:15 +0000302 // Note that we don't want to make the variable non-external
303 // because of this, but unique-external linkage suits us.
David Blaikie4e4d0842012-03-11 07:00:24 +0000304 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000305 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000306 LinkageInfo TypeLV = getLVForType(Var->getType());
307 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000308 return LinkageInfo::uniqueExternal();
Rafael Espindola2f47c362012-03-10 13:01:40 +0000309 LV.mergeVisibilityWithMin(TypeLV.visibility(),
310 TypeLV.visibilityExplicit());
John McCall110e8e52010-10-29 22:22:43 +0000311 }
312
John McCall35cebc32010-11-02 18:38:13 +0000313 if (Var->getStorageClass() == SC_PrivateExtern)
314 LV.setVisibility(HiddenVisibility, true);
315
David Blaikie4e4d0842012-03-11 07:00:24 +0000316 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000317 (Var->getStorageClass() == SC_Extern ||
318 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000319
Douglas Gregord85b5b92009-11-25 22:24:25 +0000320 // C99 6.2.2p4:
321 // For an identifier declared with the storage-class specifier
322 // extern in a scope in which a prior declaration of that
323 // identifier is visible, if the prior declaration specifies
324 // internal or external linkage, the linkage of the identifier
325 // at the later declaration is the same as the linkage
326 // specified at the prior declaration. If no prior declaration
327 // is visible, or if the prior declaration specifies no
328 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000329 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000330 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000331 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
332 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000333 }
334 }
335
Douglas Gregord85b5b92009-11-25 22:24:25 +0000336 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000337 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000338 // In theory, we can modify the function's LV by the LV of its
339 // type unless it has C linkage (see comment above about variables
340 // for justification). In practice, GCC doesn't do this, so it's
341 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000342
John McCall35cebc32010-11-02 18:38:13 +0000343 if (Function->getStorageClass() == SC_PrivateExtern)
344 LV.setVisibility(HiddenVisibility, true);
345
Douglas Gregord85b5b92009-11-25 22:24:25 +0000346 // C99 6.2.2p5:
347 // If the declaration of an identifier for a function has no
348 // storage-class specifier, its linkage is determined exactly
349 // as if it were declared with the storage-class specifier
350 // extern.
David Blaikie4e4d0842012-03-11 07:00:24 +0000351 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000352 (Function->getStorageClass() == SC_Extern ||
353 Function->getStorageClass() == SC_PrivateExtern ||
354 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000355 // C99 6.2.2p4:
356 // For an identifier declared with the storage-class specifier
357 // extern in a scope in which a prior declaration of that
358 // identifier is visible, if the prior declaration specifies
359 // internal or external linkage, the linkage of the identifier
360 // at the later declaration is the same as the linkage
361 // specified at the prior declaration. If no prior declaration
362 // is visible, or if the prior declaration specifies no
363 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000364 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000365 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000366 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
367 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000368 }
369 }
370
John McCallaf8ca372011-02-10 06:50:24 +0000371 // In C++, then if the type of the function uses a type with
372 // unique-external linkage, it's not legally usable from outside
373 // this translation unit. However, we should use the C linkage
374 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000375 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000376 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000377 Function->getType()->getLinkage() == UniqueExternalLinkage)
378 return LinkageInfo::uniqueExternal();
379
John McCall6ce51ee2011-06-27 23:06:04 +0000380 // Consider LV from the template and the template arguments unless
381 // this is an explicit specialization with a visibility attribute.
382 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000383 = Function->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000384 if (shouldConsiderTemplateLV(Function, specInfo)) {
385 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola62d9f112012-04-16 13:44:41 +0000386 LVFlags::CreateOnlyDeclLinkage()));
John McCall6ce51ee2011-06-27 23:06:04 +0000387 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola860097c2012-02-23 04:17:32 +0000388 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000389 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000390 }
391
Douglas Gregord85b5b92009-11-25 22:24:25 +0000392 // - a named class (Clause 9), or an unnamed class defined in a
393 // typedef declaration in which the class has the typedef name
394 // for linkage purposes (7.1.3); or
395 // - a named enumeration (7.2), or an unnamed enumeration
396 // defined in a typedef declaration in which the enumeration
397 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000398 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
399 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000400 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000401 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000402
John McCall1fb0caa2010-10-22 21:05:15 +0000403 // If this is a class template specialization, consider the
404 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000405 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000406 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000407 if (shouldConsiderTemplateLV(spec)) {
408 // From the template.
409 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola62d9f112012-04-16 13:44:41 +0000410 LVFlags::CreateOnlyDeclLinkage()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000411
John McCall6ce51ee2011-06-27 23:06:04 +0000412 // The arguments at which the template was instantiated.
413 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola860097c2012-02-23 04:17:32 +0000414 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000415 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000416 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000417
John McCallac65c622010-10-26 04:59:26 +0000418 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000419 if (F.ConsiderGlobalVisibility)
420 F.ConsiderGlobalVisibility =
David Blaikie4e4d0842012-03-11 07:00:24 +0000421 (Context.getLangOpts().CPlusPlus &&
John McCallac65c622010-10-26 04:59:26 +0000422 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000423
Douglas Gregord85b5b92009-11-25 22:24:25 +0000424 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000425 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000426 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000427 if (!isExternalLinkage(EnumLV.linkage()))
428 return LinkageInfo::none();
429 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000430
431 // - a template, unless it is a function template that has
432 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000433 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
434 if (F.ConsiderTemplateParameterTypes)
435 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000436
Douglas Gregord85b5b92009-11-25 22:24:25 +0000437 // - a namespace (7.3), unless it is declared within an unnamed
438 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000439 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
440 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000441
John McCall1fb0caa2010-10-22 21:05:15 +0000442 // By extension, we assign external linkage to Objective-C
443 // interfaces.
444 } else if (isa<ObjCInterfaceDecl>(D)) {
445 // fallout
446
447 // Everything not covered here has no linkage.
448 } else {
John McCallaf146032010-10-30 11:50:40 +0000449 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000450 }
451
Rafael Espindola767f7c72012-04-14 15:21:19 +0000452 if (F.ConsiderVisibilityAttributes) {
453 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
454 LV.setVisibility(*Vis, true);
455 F.ConsiderGlobalVisibility = false;
456 } else {
457 // If we're declared in a namespace with a visibility attribute,
458 // use that namespace's visibility, but don't call it explicit.
459 for (const DeclContext *DC = D->getDeclContext();
460 !isa<TranslationUnitDecl>(DC);
461 DC = DC->getParent()) {
462 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
463 if (!ND) continue;
464 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
465 LV.setVisibility(*Vis, true);
466 F.ConsiderGlobalVisibility = false;
467 break;
468 }
469 }
470 }
471 }
472
John McCall1fb0caa2010-10-22 21:05:15 +0000473 // If we ended up with non-external linkage, visibility should
474 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000475 if (LV.linkage() != ExternalLinkage)
476 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000477
John McCall1fb0caa2010-10-22 21:05:15 +0000478 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000479}
480
John McCall36987482010-11-02 01:45:15 +0000481static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000482 // Only certain class members have linkage. Note that fields don't
483 // really have linkage, but it's convenient to say they do for the
484 // purposes of calculating linkage of pointer-to-data-member
485 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000486 if (!(isa<CXXMethodDecl>(D) ||
487 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000488 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000489 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000490 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000491 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000492
John McCall36987482010-11-02 01:45:15 +0000493 LinkageInfo LV;
David Blaikie4e4d0842012-03-11 07:00:24 +0000494 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
John McCall36987482010-11-02 01:45:15 +0000495
496 // The flags we're going to use to compute the class's visibility.
497 LVFlags ClassF = F;
498
499 // If we have an explicit visibility attribute, merge that in.
500 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000501 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
502 LV.mergeVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000503
504 // Ignore global visibility later, but not this attribute.
505 F.ConsiderGlobalVisibility = false;
506
507 // Ignore both global visibility and attributes when computing our
508 // parent's visibility.
Rafael Espindola62d9f112012-04-16 13:44:41 +0000509 ClassF = LVFlags::CreateOnlyDeclLinkage();
John McCall36987482010-11-02 01:45:15 +0000510 }
511 }
John McCallaf146032010-10-30 11:50:40 +0000512
513 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000514 // linkage.
515 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
516 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000517 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000518
519 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000520 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000521 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000522
John McCall3cdfc4d2010-08-13 08:35:10 +0000523 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000524 // If the type of the function uses a type with unique-external
525 // linkage, it's not legally usable from outside this translation unit.
526 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
527 return LinkageInfo::uniqueExternal();
528
John McCall110e8e52010-10-29 22:22:43 +0000529 TemplateSpecializationKind TSK = TSK_Undeclared;
530
John McCall1fb0caa2010-10-22 21:05:15 +0000531 // If this is a method template specialization, use the linkage for
532 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000533 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000534 = MD->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000535 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola860097c2012-02-23 04:17:32 +0000536 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
537 F));
John McCall6ce51ee2011-06-27 23:06:04 +0000538 if (F.ConsiderTemplateParameterTypes)
539 LV.merge(getLVForTemplateParameterList(
540 spec->getTemplate()->getTemplateParameters()));
541 }
John McCall110e8e52010-10-29 22:22:43 +0000542
John McCall6ce51ee2011-06-27 23:06:04 +0000543 TSK = spec->getTemplateSpecializationKind();
John McCall110e8e52010-10-29 22:22:43 +0000544 } else if (MemberSpecializationInfo *MSI =
545 MD->getMemberSpecializationInfo()) {
546 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000547 }
548
John McCall110e8e52010-10-29 22:22:43 +0000549 // If we're paying attention to global visibility, apply
550 // -finline-visibility-hidden if this is an inline method.
551 //
John McCallaf146032010-10-30 11:50:40 +0000552 // Note that ConsiderGlobalVisibility doesn't yet have information
553 // about whether containing classes have visibility attributes,
554 // and that's intentional.
555 if (TSK != TSK_ExplicitInstantiationDeclaration &&
Rafael Espindolafedb6ec2011-12-27 21:15:28 +0000556 TSK != TSK_ExplicitInstantiationDefinition &&
John McCall36987482010-11-02 01:45:15 +0000557 F.ConsiderGlobalVisibility &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000558 MD->getASTContext().getLangOpts().InlineVisibilityHidden) {
John McCall66cbcf32010-11-01 01:29:57 +0000559 // InlineVisibilityHidden only applies to definitions, and
560 // isInlined() only gives meaningful answers on definitions
561 // anyway.
562 const FunctionDecl *Def = 0;
563 if (MD->hasBody(Def) && Def->isInlined())
564 LV.setVisibility(HiddenVisibility);
565 }
John McCall1fb0caa2010-10-22 21:05:15 +0000566
John McCall110e8e52010-10-29 22:22:43 +0000567 // Note that in contrast to basically every other situation, we
568 // *do* apply -fvisibility to method declarations.
569
570 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000571 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000572 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000573 if (shouldConsiderTemplateLV(spec)) {
574 // Merge template argument/parameter information for member
575 // class template specializations.
Rafael Espindola860097c2012-02-23 04:17:32 +0000576 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
577 F));
John McCall1a0918a2011-03-04 10:39:25 +0000578 if (F.ConsiderTemplateParameterTypes)
579 LV.merge(getLVForTemplateParameterList(
John McCall6ce51ee2011-06-27 23:06:04 +0000580 spec->getSpecializedTemplate()->getTemplateParameters()));
581 }
John McCall110e8e52010-10-29 22:22:43 +0000582 }
583
John McCall110e8e52010-10-29 22:22:43 +0000584 // Static data members.
585 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000586 // Modify the variable's linkage by its type, but ignore the
587 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000588 LinkageInfo TypeLV = getLVForType(VD->getType());
589 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000590 LV.mergeLinkage(UniqueExternalLinkage);
591 if (!LV.visibilityExplicit())
Rafael Espindola093ecc92012-01-14 00:30:36 +0000592 LV.mergeVisibility(TypeLV.visibility(), TypeLV.visibilityExplicit());
John McCall110e8e52010-10-29 22:22:43 +0000593 }
594
John McCall1fb0caa2010-10-22 21:05:15 +0000595 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000596}
597
John McCallf76b0922011-02-08 19:01:05 +0000598static void clearLinkageForClass(const CXXRecordDecl *record) {
599 for (CXXRecordDecl::decl_iterator
600 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
601 Decl *child = *i;
602 if (isa<NamedDecl>(child))
603 cast<NamedDecl>(child)->ClearLinkageCache();
604 }
605}
606
David Blaikie99ba9e32011-12-20 02:48:34 +0000607void NamedDecl::anchor() { }
608
John McCallf76b0922011-02-08 19:01:05 +0000609void NamedDecl::ClearLinkageCache() {
610 // Note that we can't skip clearing the linkage of children just
611 // because the parent doesn't have cached linkage: we don't cache
612 // when computing linkage for parent contexts.
613
614 HasCachedLinkage = 0;
615
616 // If we're changing the linkage of a class, we need to reset the
617 // linkage of child declarations, too.
618 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
619 clearLinkageForClass(record);
620
John McCall15e310a2011-02-19 02:53:41 +0000621 if (ClassTemplateDecl *temp =
622 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000623 // Clear linkage for the template pattern.
624 CXXRecordDecl *record = temp->getTemplatedDecl();
625 record->HasCachedLinkage = 0;
626 clearLinkageForClass(record);
627
John McCall15e310a2011-02-19 02:53:41 +0000628 // We need to clear linkage for specializations, too.
629 for (ClassTemplateDecl::spec_iterator
630 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
631 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000632 }
John McCall15e310a2011-02-19 02:53:41 +0000633
634 // Clear cached linkage for function template decls, too.
635 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000636 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
637 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000638 for (FunctionTemplateDecl::spec_iterator
639 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
640 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000641 }
John McCall15e310a2011-02-19 02:53:41 +0000642
John McCallf76b0922011-02-08 19:01:05 +0000643}
644
Douglas Gregor381d34e2010-12-06 18:36:25 +0000645Linkage NamedDecl::getLinkage() const {
646 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000647 assert(Linkage(CachedLinkage) ==
648 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000649 return Linkage(CachedLinkage);
650 }
651
652 CachedLinkage = getLVForDecl(this,
653 LVFlags::CreateOnlyDeclLinkage()).linkage();
654 HasCachedLinkage = 1;
655 return Linkage(CachedLinkage);
656}
657
John McCallaf146032010-10-30 11:50:40 +0000658LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000659 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000660 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000661 HasCachedLinkage = 1;
662 CachedLinkage = LI.linkage();
663 return LI;
John McCall0df95872010-10-29 00:29:13 +0000664}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000665
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000666llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
667 // Use the most recent declaration of a variable.
668 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregoref96ee02012-01-14 16:38:05 +0000669 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000670
671 // Use the most recent declaration of a function, and also handle
672 // function template specializations.
673 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
674 if (llvm::Optional<Visibility> V
Douglas Gregoref96ee02012-01-14 16:38:05 +0000675 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000676 return V;
677
678 // If the function is a specialization of a template with an
679 // explicit visibility attribute, use that.
680 if (FunctionTemplateSpecializationInfo *templateInfo
681 = fn->getTemplateSpecializationInfo())
682 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
683
Rafael Espindola860097c2012-02-23 04:17:32 +0000684 // If the function is a member of a specialization of a class template
685 // and the corresponding decl has explicit visibility, use that.
686 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
687 if (InstantiatedFrom)
688 return getVisibilityOf(InstantiatedFrom);
689
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000690 return llvm::Optional<Visibility>();
691 }
692
693 // Otherwise, just check the declaration itself first.
694 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
695 return V;
696
697 // If there wasn't explicit visibility there, and this is a
698 // specialization of a class template, check for visibility
699 // on the pattern.
700 if (const ClassTemplateSpecializationDecl *spec
701 = dyn_cast<ClassTemplateSpecializationDecl>(this))
702 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
703
Rafael Espindola860097c2012-02-23 04:17:32 +0000704 // If this is a member class of a specialization of a class template
705 // and the corresponding decl has explicit visibility, use that.
706 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
707 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
708 if (InstantiatedFrom)
709 return getVisibilityOf(InstantiatedFrom);
710 }
711
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000712 return llvm::Optional<Visibility>();
713}
714
John McCall36987482010-11-02 01:45:15 +0000715static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000716 // Objective-C: treat all Objective-C declarations as having external
717 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000718 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000719 default:
720 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000721 case Decl::ParmVar:
722 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000723 case Decl::TemplateTemplateParm: // count these as external
724 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000725 case Decl::ObjCAtDefsField:
726 case Decl::ObjCCategory:
727 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000728 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000729 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000730 case Decl::ObjCMethod:
731 case Decl::ObjCProperty:
732 case Decl::ObjCPropertyImpl:
733 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000734 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000735
736 case Decl::CXXRecord: {
737 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
738 if (Record->isLambda()) {
739 if (!Record->getLambdaManglingNumber()) {
740 // This lambda has no mangling number, so it's internal.
741 return LinkageInfo::internal();
742 }
743
744 // This lambda has its linkage/visibility determined by its owner.
745 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
746 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
747 if (isa<ParmVarDecl>(ContextDecl))
748 DC = ContextDecl->getDeclContext()->getRedeclContext();
749 else
750 return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags);
751 }
752
753 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
754 return getLVForDecl(ND, Flags);
755
756 return LinkageInfo::external();
757 }
758
759 break;
760 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000761 }
762
Douglas Gregord85b5b92009-11-25 22:24:25 +0000763 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000764 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000765 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000766
767 // C++ [basic.link]p5:
768 // In addition, a member function, static data member, a named
769 // class or enumeration of class scope, or an unnamed class or
770 // enumeration defined in a class-scope typedef declaration such
771 // that the class or enumeration has the typedef name for linkage
772 // purposes (7.1.3), has external linkage if the name of the class
773 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000774 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000775 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000776
777 // C++ [basic.link]p6:
778 // The name of a function declared in block scope and the name of
779 // an object declared by a block scope extern declaration have
780 // linkage. If there is a visible declaration of an entity with
781 // linkage having the same name and type, ignoring entities
782 // declared outside the innermost enclosing namespace scope, the
783 // block scope declaration declares that same entity and receives
784 // the linkage of the previous declaration. If there is more than
785 // one such matching entity, the program is ill-formed. Otherwise,
786 // if no matching entity is found, the block scope entity receives
787 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000788 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
789 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000790 if (Function->isInAnonymousNamespace() &&
791 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000792 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000793
John McCallaf146032010-10-30 11:50:40 +0000794 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000795 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000796 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
797 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000798 }
799
Douglas Gregoref96ee02012-01-14 16:38:05 +0000800 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000801 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000802 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
803 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000804 }
805
806 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000807 }
808
John McCall0df95872010-10-29 00:29:13 +0000809 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000810 if (Var->getStorageClass() == SC_Extern ||
811 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000812 if (Var->isInAnonymousNamespace() &&
813 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000814 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000815
John McCallaf146032010-10-30 11:50:40 +0000816 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000817 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000818 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000819 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000820 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
821 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000822 }
823
Douglas Gregoref96ee02012-01-14 16:38:05 +0000824 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000825 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000826 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
827 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000828 }
829
830 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000831 }
832 }
833
834 // C++ [basic.link]p6:
835 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000836 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000837}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000838
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000839std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000840 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000841}
842
843std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000844 const DeclContext *Ctx = getDeclContext();
845
846 if (Ctx->isFunctionOrMethod())
847 return getNameAsString();
848
Chris Lattner5f9e2722011-07-23 10:55:15 +0000849 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000850 ContextsTy Contexts;
851
852 // Collect contexts.
853 while (Ctx && isa<NamedDecl>(Ctx)) {
854 Contexts.push_back(Ctx);
855 Ctx = Ctx->getParent();
856 };
857
858 std::string QualName;
859 llvm::raw_string_ostream OS(QualName);
860
861 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
862 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000863 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000864 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000865 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
866 std::string TemplateArgsStr
867 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000868 TemplateArgs.data(),
869 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000870 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000871 OS << Spec->getName() << TemplateArgsStr;
872 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000873 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000874 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000875 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000876 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000877 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
878 if (!RD->getIdentifier())
879 OS << "<anonymous " << RD->getKindName() << '>';
880 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000881 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000882 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000883 const FunctionProtoType *FT = 0;
884 if (FD->hasWrittenPrototype())
885 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
886
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000887 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000888 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000889 unsigned NumParams = FD->getNumParams();
890 for (unsigned i = 0; i < NumParams; ++i) {
891 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000892 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000893 std::string Param;
894 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000895 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000896 }
897
898 if (FT->isVariadic()) {
899 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000900 OS << ", ";
901 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000902 }
903 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000904 OS << ')';
905 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000906 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000907 }
908 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000909 }
910
John McCall8472af42010-03-16 21:48:18 +0000911 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000912 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000913 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000914 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000915
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000916 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000917}
918
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000919bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000920 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
921
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000922 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
923 // We want to keep it, unless it nominates same namespace.
924 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000925 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
926 ->getOriginalNamespace() ==
927 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
928 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000929 }
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000931 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
932 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000933 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000934
Douglas Gregore53060f2009-06-25 22:08:12 +0000935 // For function templates, the underlying function declarations are linked.
936 if (const FunctionTemplateDecl *FunctionTemplate
937 = dyn_cast<FunctionTemplateDecl>(this))
938 if (const FunctionTemplateDecl *OldFunctionTemplate
939 = dyn_cast<FunctionTemplateDecl>(OldD))
940 return FunctionTemplate->getTemplatedDecl()
941 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Steve Naroff0de21fd2009-02-22 19:35:57 +0000943 // For method declarations, we keep track of redeclarations.
944 if (isa<ObjCMethodDecl>(this))
945 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000946
John McCallf36e02d2009-10-09 21:13:30 +0000947 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
948 return true;
949
John McCall9488ea12009-11-17 05:59:44 +0000950 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
951 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
952 cast<UsingShadowDecl>(OldD)->getTargetDecl();
953
Douglas Gregordc355712011-02-25 00:36:19 +0000954 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
955 ASTContext &Context = getASTContext();
956 return Context.getCanonicalNestedNameSpecifier(
957 cast<UsingDecl>(this)->getQualifier()) ==
958 Context.getCanonicalNestedNameSpecifier(
959 cast<UsingDecl>(OldD)->getQualifier());
960 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000961
Douglas Gregor7a537402012-01-03 23:26:26 +0000962 // A typedef of an Objective-C class type can replace an Objective-C class
963 // declaration or definition, and vice versa.
964 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
965 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
966 return true;
967
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000968 // For non-function declarations, if the declarations are of the
969 // same kind then this must be a redeclaration, or semantic analysis
970 // would not have given us the new declaration.
971 return this->getKind() == OldD->getKind();
972}
973
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000974bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000975 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000976}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000977
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000978NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000979 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000980 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
981 ND = UD->getTargetDecl();
982
983 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
984 return AD->getClassInterface();
985
986 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +0000987}
988
John McCall161755a2010-04-06 21:38:20 +0000989bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000990 if (!isCXXClassMember())
991 return false;
992
John McCall161755a2010-04-06 21:38:20 +0000993 const NamedDecl *D = this;
994 if (isa<UsingShadowDecl>(D))
995 D = cast<UsingShadowDecl>(D)->getTargetDecl();
996
Francois Pichet87c2e122010-11-21 06:08:52 +0000997 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000998 return true;
999 if (isa<CXXMethodDecl>(D))
1000 return cast<CXXMethodDecl>(D)->isInstance();
1001 if (isa<FunctionTemplateDecl>(D))
1002 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1003 ->getTemplatedDecl())->isInstance();
1004 return false;
1005}
1006
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +00001007//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001008// DeclaratorDecl Implementation
1009//===----------------------------------------------------------------------===//
1010
Douglas Gregor1693e152010-07-06 18:42:40 +00001011template <typename DeclT>
1012static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1013 if (decl->getNumTemplateParameterLists() > 0)
1014 return decl->getTemplateParameterList(0)->getTemplateLoc();
1015 else
1016 return decl->getInnerLocStart();
1017}
1018
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001019SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001020 TypeSourceInfo *TSI = getTypeSourceInfo();
1021 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001022 return SourceLocation();
1023}
1024
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001025void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1026 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001027 // Make sure the extended decl info is allocated.
1028 if (!hasExtInfo()) {
1029 // Save (non-extended) type source info pointer.
1030 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1031 // Allocate external info struct.
1032 DeclInfo = new (getASTContext()) ExtInfo;
1033 // Restore savedTInfo into (extended) decl info.
1034 getExtInfo()->TInfo = savedTInfo;
1035 }
1036 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001037 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001038 } else {
John McCallb6217662010-03-15 10:12:16 +00001039 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001040 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001041 if (getExtInfo()->NumTemplParamLists == 0) {
1042 // Save type source info pointer.
1043 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1044 // Deallocate the extended decl info.
1045 getASTContext().Deallocate(getExtInfo());
1046 // Restore savedTInfo into (non-extended) decl info.
1047 DeclInfo = savedTInfo;
1048 }
1049 else
1050 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001051 }
1052 }
1053}
1054
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001055void
1056DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1057 unsigned NumTPLists,
1058 TemplateParameterList **TPLists) {
1059 assert(NumTPLists > 0);
1060 // Make sure the extended decl info is allocated.
1061 if (!hasExtInfo()) {
1062 // Save (non-extended) type source info pointer.
1063 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1064 // Allocate external info struct.
1065 DeclInfo = new (getASTContext()) ExtInfo;
1066 // Restore savedTInfo into (extended) decl info.
1067 getExtInfo()->TInfo = savedTInfo;
1068 }
1069 // Set the template parameter lists info.
1070 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1071}
1072
Douglas Gregor1693e152010-07-06 18:42:40 +00001073SourceLocation DeclaratorDecl::getOuterLocStart() const {
1074 return getTemplateOrInnerLocStart(this);
1075}
1076
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001077namespace {
1078
1079// Helper function: returns true if QT is or contains a type
1080// having a postfix component.
1081bool typeIsPostfix(clang::QualType QT) {
1082 while (true) {
1083 const Type* T = QT.getTypePtr();
1084 switch (T->getTypeClass()) {
1085 default:
1086 return false;
1087 case Type::Pointer:
1088 QT = cast<PointerType>(T)->getPointeeType();
1089 break;
1090 case Type::BlockPointer:
1091 QT = cast<BlockPointerType>(T)->getPointeeType();
1092 break;
1093 case Type::MemberPointer:
1094 QT = cast<MemberPointerType>(T)->getPointeeType();
1095 break;
1096 case Type::LValueReference:
1097 case Type::RValueReference:
1098 QT = cast<ReferenceType>(T)->getPointeeType();
1099 break;
1100 case Type::PackExpansion:
1101 QT = cast<PackExpansionType>(T)->getPattern();
1102 break;
1103 case Type::Paren:
1104 case Type::ConstantArray:
1105 case Type::DependentSizedArray:
1106 case Type::IncompleteArray:
1107 case Type::VariableArray:
1108 case Type::FunctionProto:
1109 case Type::FunctionNoProto:
1110 return true;
1111 }
1112 }
1113}
1114
1115} // namespace
1116
1117SourceRange DeclaratorDecl::getSourceRange() const {
1118 SourceLocation RangeEnd = getLocation();
1119 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1120 if (typeIsPostfix(TInfo->getType()))
1121 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1122 }
1123 return SourceRange(getOuterLocStart(), RangeEnd);
1124}
1125
Abramo Bagnara9b934882010-06-12 08:15:14 +00001126void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001127QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1128 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001129 TemplateParameterList **TPLists) {
1130 assert((NumTPLists == 0 || TPLists != 0) &&
1131 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001132
1133 // Free previous template parameters (if any).
1134 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001135 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001136 TemplParamLists = 0;
1137 NumTemplParamLists = 0;
1138 }
1139 // Set info on matched template parameter lists (if any).
1140 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001141 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001142 NumTemplParamLists = NumTPLists;
1143 for (unsigned i = NumTPLists; i-- > 0; )
1144 TemplParamLists[i] = TPLists[i];
1145 }
1146}
1147
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001148//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001149// VarDecl Implementation
1150//===----------------------------------------------------------------------===//
1151
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001152const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1153 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001154 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001155 case SC_Auto: return "auto";
1156 case SC_Extern: return "extern";
1157 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1158 case SC_PrivateExtern: return "__private_extern__";
1159 case SC_Register: return "register";
1160 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001161 }
1162
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001163 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001164}
1165
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001166VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1167 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001168 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001169 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001170 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001171}
1172
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001173VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1174 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1175 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1176 QualType(), 0, SC_None, SC_None);
1177}
1178
Douglas Gregor381d34e2010-12-06 18:36:25 +00001179void VarDecl::setStorageClass(StorageClass SC) {
1180 assert(isLegalForVariable(SC));
1181 if (getStorageClass() != SC)
1182 ClearLinkageCache();
1183
John McCallf1e4fbf2011-05-01 02:13:58 +00001184 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001185}
1186
Douglas Gregor1693e152010-07-06 18:42:40 +00001187SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001188 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001189 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001190 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001191}
1192
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001193bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001194 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001195 return false;
1196
Eli Friedman750dc2b2012-01-15 01:23:58 +00001197 const DeclContext *DC = getDeclContext();
1198 if (DC->isRecord())
1199 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001200
Eli Friedman750dc2b2012-01-15 01:23:58 +00001201 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001202 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001203 return true;
1204 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001205}
1206
1207VarDecl *VarDecl::getCanonicalDecl() {
1208 return getFirstDeclaration();
1209}
1210
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001211VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1212 ASTContext &C) const
1213{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001214 // C++ [basic.def]p2:
1215 // A declaration is a definition unless [...] it contains the 'extern'
1216 // specifier or a linkage-specification and neither an initializer [...],
1217 // it declares a static data member in a class declaration [...].
1218 // C++ [temp.expl.spec]p15:
1219 // An explicit specialization of a static data member of a template is a
1220 // definition if the declaration includes an initializer; otherwise, it is
1221 // a declaration.
1222 if (isStaticDataMember()) {
1223 if (isOutOfLine() && (hasInit() ||
1224 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1225 return Definition;
1226 else
1227 return DeclarationOnly;
1228 }
1229 // C99 6.7p5:
1230 // A definition of an identifier is a declaration for that identifier that
1231 // [...] causes storage to be reserved for that object.
1232 // Note: that applies for all non-file-scope objects.
1233 // C99 6.9.2p1:
1234 // If the declaration of an identifier for an object has file scope and an
1235 // initializer, the declaration is an external definition for the identifier
1236 if (hasInit())
1237 return Definition;
1238 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1239 if (hasExternalStorage())
1240 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001241
John McCalld931b082010-08-26 03:08:43 +00001242 if (getStorageClassAsWritten() == SC_Extern ||
1243 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001244 for (const VarDecl *PrevVar = getPreviousDecl();
1245 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001246 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1247 return DeclarationOnly;
1248 }
1249 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001250 // C99 6.9.2p2:
1251 // A declaration of an object that has file scope without an initializer,
1252 // and without a storage class specifier or the scs 'static', constitutes
1253 // a tentative definition.
1254 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001255 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001256 return TentativeDefinition;
1257
1258 // What's left is (in C, block-scope) declarations without initializers or
1259 // external storage. These are definitions.
1260 return Definition;
1261}
1262
Sebastian Redle9d12b62010-01-31 22:27:38 +00001263VarDecl *VarDecl::getActingDefinition() {
1264 DefinitionKind Kind = isThisDeclarationADefinition();
1265 if (Kind != TentativeDefinition)
1266 return 0;
1267
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001268 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001269 VarDecl *First = getFirstDeclaration();
1270 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1271 I != E; ++I) {
1272 Kind = (*I)->isThisDeclarationADefinition();
1273 if (Kind == Definition)
1274 return 0;
1275 else if (Kind == TentativeDefinition)
1276 LastTentative = *I;
1277 }
1278 return LastTentative;
1279}
1280
1281bool VarDecl::isTentativeDefinitionNow() const {
1282 DefinitionKind Kind = isThisDeclarationADefinition();
1283 if (Kind != TentativeDefinition)
1284 return false;
1285
1286 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1287 if ((*I)->isThisDeclarationADefinition() == Definition)
1288 return false;
1289 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001290 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001291}
1292
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001293VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001294 VarDecl *First = getFirstDeclaration();
1295 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1296 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001297 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001298 return *I;
1299 }
1300 return 0;
1301}
1302
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001303VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001304 DefinitionKind Kind = DeclarationOnly;
1305
1306 const VarDecl *First = getFirstDeclaration();
1307 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001308 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001309 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001310 if (Kind == Definition)
1311 break;
1312 }
John McCall110e8e52010-10-29 22:22:43 +00001313
1314 return Kind;
1315}
1316
Sebastian Redl31310a22010-02-01 20:16:42 +00001317const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001318 redecl_iterator I = redecls_begin(), E = redecls_end();
1319 while (I != E && !I->getInit())
1320 ++I;
1321
1322 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001323 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001324 return I->getInit();
1325 }
1326 return 0;
1327}
1328
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001329bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001330 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001331 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001332
1333 if (!isStaticDataMember())
1334 return false;
1335
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001336 // If this static data member was instantiated from a static data member of
1337 // a class template, check whether that static data member was defined
1338 // out-of-line.
1339 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1340 return VD->isOutOfLine();
1341
1342 return false;
1343}
1344
Douglas Gregor0d035142009-10-27 18:42:08 +00001345VarDecl *VarDecl::getOutOfLineDefinition() {
1346 if (!isStaticDataMember())
1347 return 0;
1348
1349 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1350 RD != RDEnd; ++RD) {
1351 if (RD->getLexicalDeclContext()->isFileContext())
1352 return *RD;
1353 }
1354
1355 return 0;
1356}
1357
Douglas Gregor838db382010-02-11 01:19:42 +00001358void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001359 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1360 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001361 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001362 }
1363
1364 Init = I;
1365}
1366
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001367bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001368 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001369
Richard Smith16581332012-03-02 04:14:40 +00001370 if (!Lang.CPlusPlus)
1371 return false;
1372
1373 // In C++11, any variable of reference type can be used in a constant
1374 // expression if it is initialized by a constant expression.
1375 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1376 return true;
1377
1378 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001379 // not require the variable to be non-volatile, but we consider this to be a
1380 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001381 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001382 return false;
1383
1384 // In C++, const, non-volatile variables of integral or enumeration types
1385 // can be used in constant expressions.
1386 if (getType()->isIntegralOrEnumerationType())
1387 return true;
1388
Richard Smith16581332012-03-02 04:14:40 +00001389 // Additionally, in C++11, non-volatile constexpr variables can be used in
1390 // constant expressions.
1391 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001392}
1393
Richard Smith099e7f62011-12-19 06:19:21 +00001394/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1395/// form, which contains extra information on the evaluated value of the
1396/// initializer.
1397EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1398 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1399 if (!Eval) {
1400 Stmt *S = Init.get<Stmt *>();
1401 Eval = new (getASTContext()) EvaluatedStmt;
1402 Eval->Value = S;
1403 Init = Eval;
1404 }
1405 return Eval;
1406}
1407
Richard Smith2d6a5672012-01-14 04:30:29 +00001408APValue *VarDecl::evaluateValue() const {
1409 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1410 return evaluateValue(Notes);
1411}
1412
1413APValue *VarDecl::evaluateValue(
1414 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001415 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1416
1417 // We only produce notes indicating why an initializer is non-constant the
1418 // first time it is evaluated. FIXME: The notes won't always be emitted the
1419 // first time we try evaluation, so might not be produced at all.
1420 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001421 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001422
1423 const Expr *Init = cast<Expr>(Eval->Value);
1424 assert(!Init->isValueDependent());
1425
1426 if (Eval->IsEvaluating) {
1427 // FIXME: Produce a diagnostic for self-initialization.
1428 Eval->CheckedICE = true;
1429 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001430 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001431 }
1432
1433 Eval->IsEvaluating = true;
1434
1435 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1436 this, Notes);
1437
1438 // Ensure the result is an uninitialized APValue if evaluation fails.
1439 if (!Result)
1440 Eval->Evaluated = APValue();
1441
1442 Eval->IsEvaluating = false;
1443 Eval->WasEvaluated = true;
1444
1445 // In C++11, we have determined whether the initializer was a constant
1446 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001447 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001448 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001449 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001450 }
1451
Richard Smith2d6a5672012-01-14 04:30:29 +00001452 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001453}
1454
1455bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001456 // Initializers of weak variables are never ICEs.
1457 if (isWeak())
1458 return false;
1459
Richard Smith099e7f62011-12-19 06:19:21 +00001460 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1461 if (Eval->CheckedICE)
1462 // We have already checked whether this subexpression is an
1463 // integral constant expression.
1464 return Eval->IsICE;
1465
1466 const Expr *Init = cast<Expr>(Eval->Value);
1467 assert(!Init->isValueDependent());
1468
1469 // In C++11, evaluate the initializer to check whether it's a constant
1470 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001471 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001472 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1473 evaluateValue(Notes);
1474 return Eval->IsICE;
1475 }
1476
1477 // It's an ICE whether or not the definition we found is
1478 // out-of-line. See DR 721 and the discussion in Clang PR
1479 // 6206 for details.
1480
1481 if (Eval->CheckingICE)
1482 return false;
1483 Eval->CheckingICE = true;
1484
1485 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1486 Eval->CheckingICE = false;
1487 Eval->CheckedICE = true;
1488 return Eval->IsICE;
1489}
1490
Douglas Gregor03e80032011-06-21 17:03:29 +00001491bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001492 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001493
1494 const Expr *E = getInit();
1495 if (!E)
1496 return false;
1497
1498 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1499 E = Cleanups->getSubExpr();
1500
1501 return isa<MaterializeTemporaryExpr>(E);
1502}
1503
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001504VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001505 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001506 return cast<VarDecl>(MSI->getInstantiatedFrom());
1507
1508 return 0;
1509}
1510
Douglas Gregor663b5a02009-10-14 20:14:33 +00001511TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001512 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001513 return MSI->getTemplateSpecializationKind();
1514
1515 return TSK_Undeclared;
1516}
1517
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001518MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001519 return getASTContext().getInstantiatedFromStaticDataMember(this);
1520}
1521
Douglas Gregor0a897e32009-10-15 17:21:20 +00001522void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1523 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001524 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001525 assert(MSI && "Not an instantiated static data member?");
1526 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001527 if (TSK != TSK_ExplicitSpecialization &&
1528 PointOfInstantiation.isValid() &&
1529 MSI->getPointOfInstantiation().isInvalid())
1530 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001531}
1532
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001533//===----------------------------------------------------------------------===//
1534// ParmVarDecl Implementation
1535//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001536
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001537ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001538 SourceLocation StartLoc,
1539 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001540 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001541 StorageClass S, StorageClass SCAsWritten,
1542 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001543 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001544 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001545}
1546
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001547ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1548 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1549 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1550 0, QualType(), 0, SC_None, SC_None, 0);
1551}
1552
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001553SourceRange ParmVarDecl::getSourceRange() const {
1554 if (!hasInheritedDefaultArg()) {
1555 SourceRange ArgRange = getDefaultArgRange();
1556 if (ArgRange.isValid())
1557 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1558 }
1559
1560 return DeclaratorDecl::getSourceRange();
1561}
1562
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001563Expr *ParmVarDecl::getDefaultArg() {
1564 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1565 assert(!hasUninstantiatedDefaultArg() &&
1566 "Default argument is not yet instantiated!");
1567
1568 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001569 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001570 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001571
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001572 return Arg;
1573}
1574
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001575SourceRange ParmVarDecl::getDefaultArgRange() const {
1576 if (const Expr *E = getInit())
1577 return E->getSourceRange();
1578
1579 if (hasUninstantiatedDefaultArg())
1580 return getUninstantiatedDefaultArg()->getSourceRange();
1581
1582 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001583}
1584
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001585bool ParmVarDecl::isParameterPack() const {
1586 return isa<PackExpansionType>(getType());
1587}
1588
Ted Kremenekd211cb72011-10-06 05:00:56 +00001589void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1590 getASTContext().setParameterIndex(this, parameterIndex);
1591 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1592}
1593
1594unsigned ParmVarDecl::getParameterIndexLarge() const {
1595 return getASTContext().getParameterIndex(this);
1596}
1597
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001598//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001599// FunctionDecl Implementation
1600//===----------------------------------------------------------------------===//
1601
Douglas Gregorda2142f2011-02-19 18:51:44 +00001602void FunctionDecl::getNameForDiagnostic(std::string &S,
1603 const PrintingPolicy &Policy,
1604 bool Qualified) const {
1605 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1606 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1607 if (TemplateArgs)
1608 S += TemplateSpecializationType::PrintTemplateArgumentList(
1609 TemplateArgs->data(),
1610 TemplateArgs->size(),
1611 Policy);
1612
1613}
1614
Ted Kremenek9498d382010-04-29 16:49:01 +00001615bool FunctionDecl::isVariadic() const {
1616 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1617 return FT->isVariadic();
1618 return false;
1619}
1620
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001621bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1622 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001623 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001624 Definition = *I;
1625 return true;
1626 }
1627 }
1628
1629 return false;
1630}
1631
Anders Carlssonffb945f2011-05-14 23:26:09 +00001632bool FunctionDecl::hasTrivialBody() const
1633{
1634 Stmt *S = getBody();
1635 if (!S) {
1636 // Since we don't have a body for this function, we don't know if it's
1637 // trivial or not.
1638 return false;
1639 }
1640
1641 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1642 return true;
1643 return false;
1644}
1645
Sean Hunt10620eb2011-05-06 20:44:56 +00001646bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1647 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001648 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001649 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1650 return true;
1651 }
1652 }
1653
1654 return false;
1655}
1656
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001657Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001658 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1659 if (I->Body) {
1660 Definition = *I;
1661 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001662 } else if (I->IsLateTemplateParsed) {
1663 Definition = *I;
1664 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001665 }
1666 }
1667
1668 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001669}
1670
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001671void FunctionDecl::setBody(Stmt *B) {
1672 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001673 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001674 EndRangeLoc = B->getLocEnd();
1675}
1676
Douglas Gregor21386642010-09-28 21:55:22 +00001677void FunctionDecl::setPure(bool P) {
1678 IsPure = P;
1679 if (P)
1680 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1681 Parent->markedVirtualFunctionPure();
1682}
1683
Douglas Gregor48a83b52009-09-12 00:17:51 +00001684bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001685 const TranslationUnitDecl *tunit =
1686 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1687 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001688 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001689 getIdentifier() &&
1690 getIdentifier()->isStr("main");
1691}
1692
1693bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1694 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1695 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1696 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1697 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1698 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1699
1700 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1701 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1702
1703 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1704 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1705
1706 ASTContext &Context =
1707 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1708 ->getASTContext();
1709
1710 // The result type and first argument type are constant across all
1711 // these operators. The second argument must be exactly void*.
1712 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001713}
1714
Douglas Gregor48a83b52009-09-12 00:17:51 +00001715bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001716 if (getLinkage() != ExternalLinkage)
1717 return false;
1718
1719 if (getAttr<OverloadableAttr>())
1720 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001721
Chandler Carruth10aad442011-02-25 00:05:02 +00001722 const DeclContext *DC = getDeclContext();
1723 if (DC->isRecord())
1724 return false;
1725
Eli Friedman750dc2b2012-01-15 01:23:58 +00001726 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001727 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001728 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001729
Eli Friedman750dc2b2012-01-15 01:23:58 +00001730 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001731}
1732
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001733bool FunctionDecl::isGlobal() const {
1734 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1735 return Method->isStatic();
1736
John McCalld931b082010-08-26 03:08:43 +00001737 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001738 return false;
1739
Mike Stump1eb44332009-09-09 15:08:12 +00001740 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001741 DC->isNamespace();
1742 DC = DC->getParent()) {
1743 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1744 if (!Namespace->getDeclName())
1745 return false;
1746 break;
1747 }
1748 }
1749
1750 return true;
1751}
1752
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001753void
1754FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1755 redeclarable_base::setPreviousDeclaration(PrevDecl);
1756
1757 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1758 FunctionTemplateDecl *PrevFunTmpl
1759 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1760 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1761 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1762 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001763
Axel Naumannd9d137e2011-11-08 18:21:06 +00001764 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001765 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001766}
1767
1768const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1769 return getFirstDeclaration();
1770}
1771
1772FunctionDecl *FunctionDecl::getCanonicalDecl() {
1773 return getFirstDeclaration();
1774}
1775
Douglas Gregor381d34e2010-12-06 18:36:25 +00001776void FunctionDecl::setStorageClass(StorageClass SC) {
1777 assert(isLegalForFunction(SC));
1778 if (getStorageClass() != SC)
1779 ClearLinkageCache();
1780
1781 SClass = SC;
1782}
1783
Douglas Gregor3e41d602009-02-13 23:20:09 +00001784/// \brief Returns a value indicating whether this function
1785/// corresponds to a builtin function.
1786///
1787/// The function corresponds to a built-in function if it is
1788/// declared at translation scope or within an extern "C" block and
1789/// its name matches with the name of a builtin. The returned value
1790/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001791/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001792/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001793unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001794 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001795 return 0;
1796
1797 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001798 if (!BuiltinID)
1799 return 0;
1800
1801 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001802 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1803 return BuiltinID;
1804
1805 // This function has the name of a known C library
1806 // function. Determine whether it actually refers to the C library
1807 // function or whether it just has the same name.
1808
Douglas Gregor9add3172009-02-17 03:23:10 +00001809 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001810 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001811 return 0;
1812
Douglas Gregor3c385e52009-02-14 18:57:46 +00001813 // If this function is at translation-unit scope and we're not in
1814 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001815 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001816 getDeclContext()->isTranslationUnit())
1817 return BuiltinID;
1818
1819 // If the function is in an extern "C" linkage specification and is
1820 // not marked "overloadable", it's the real function.
1821 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001822 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001823 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001824 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001825 return BuiltinID;
1826
1827 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001828 return 0;
1829}
1830
1831
Chris Lattner1ad9b282009-04-25 06:03:53 +00001832/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001833/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001834/// after it has been created.
1835unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001836 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001837 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001838 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001839 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Reid Spencer5f016e22007-07-11 17:01:13 +00001841}
1842
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001843void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001844 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001846 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001849 if (!NewParamInfo.empty()) {
1850 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1851 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 }
1853}
1854
James Molloy16f1f712012-02-29 10:24:19 +00001855void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1856 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1857
1858 if (!NewDecls.empty()) {
1859 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1860 std::copy(NewDecls.begin(), NewDecls.end(), A);
1861 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1862 }
1863}
1864
Chris Lattner8123a952008-04-10 02:22:51 +00001865/// getMinRequiredArguments - Returns the minimum number of arguments
1866/// needed to call this function. This may be fewer than the number of
1867/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001868/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001869unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001870 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001871 return getNumParams();
1872
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001873 unsigned NumRequiredArgs = getNumParams();
1874
1875 // If the last parameter is a parameter pack, we don't need an argument for
1876 // it.
1877 if (NumRequiredArgs > 0 &&
1878 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1879 --NumRequiredArgs;
1880
1881 // If this parameter has a default argument, we don't need an argument for
1882 // it.
1883 while (NumRequiredArgs > 0 &&
1884 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001885 --NumRequiredArgs;
1886
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001887 // We might have parameter packs before the end. These can't be deduced,
1888 // but they can still handle multiple arguments.
1889 unsigned ArgIdx = NumRequiredArgs;
1890 while (ArgIdx > 0) {
1891 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1892 NumRequiredArgs = ArgIdx;
1893
1894 --ArgIdx;
1895 }
1896
Chris Lattner8123a952008-04-10 02:22:51 +00001897 return NumRequiredArgs;
1898}
1899
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001900bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001901 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001902 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001903
1904 if (isa<CXXMethodDecl>(this)) {
1905 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1906 return true;
1907 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001908
1909 switch (getTemplateSpecializationKind()) {
1910 case TSK_Undeclared:
1911 case TSK_ExplicitSpecialization:
1912 return false;
1913
1914 case TSK_ImplicitInstantiation:
1915 case TSK_ExplicitInstantiationDeclaration:
1916 case TSK_ExplicitInstantiationDefinition:
1917 // Handle below.
1918 break;
1919 }
1920
1921 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001922 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001923 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001924 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001925
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001926 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001927 return PatternDecl->isInlined();
1928
1929 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001930}
1931
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001932static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1933 // Only consider file-scope declarations in this test.
1934 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1935 return false;
1936
1937 // Only consider explicit declarations; the presence of a builtin for a
1938 // libcall shouldn't affect whether a definition is externally visible.
1939 if (Redecl->isImplicit())
1940 return false;
1941
1942 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1943 return true; // Not an inline definition
1944
1945 return false;
1946}
1947
Nick Lewyckydce67a72011-07-18 05:26:13 +00001948/// \brief For a function declaration in C or C++, determine whether this
1949/// declaration causes the definition to be externally visible.
1950///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001951/// Specifically, this determines if adding the current declaration to the set
1952/// of redeclarations of the given functions causes
1953/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001954bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1955 assert(!doesThisDeclarationHaveABody() &&
1956 "Must have a declaration without a body.");
1957
1958 ASTContext &Context = getASTContext();
1959
David Blaikie4e4d0842012-03-11 07:00:24 +00001960 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001961 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1962 // an externally visible definition.
1963 //
1964 // FIXME: What happens if gnu_inline gets added on after the first
1965 // declaration?
1966 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1967 return false;
1968
1969 const FunctionDecl *Prev = this;
1970 bool FoundBody = false;
1971 while ((Prev = Prev->getPreviousDecl())) {
1972 FoundBody |= Prev->Body;
1973
1974 if (Prev->Body) {
1975 // If it's not the case that both 'inline' and 'extern' are
1976 // specified on the definition, then it is always externally visible.
1977 if (!Prev->isInlineSpecified() ||
1978 Prev->getStorageClassAsWritten() != SC_Extern)
1979 return false;
1980 } else if (Prev->isInlineSpecified() &&
1981 Prev->getStorageClassAsWritten() != SC_Extern) {
1982 return false;
1983 }
1984 }
1985 return FoundBody;
1986 }
1987
David Blaikie4e4d0842012-03-11 07:00:24 +00001988 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001989 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001990
1991 // C99 6.7.4p6:
1992 // [...] If all of the file scope declarations for a function in a
1993 // translation unit include the inline function specifier without extern,
1994 // then the definition in that translation unit is an inline definition.
1995 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001996 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001997 const FunctionDecl *Prev = this;
1998 bool FoundBody = false;
1999 while ((Prev = Prev->getPreviousDecl())) {
2000 FoundBody |= Prev->Body;
2001 if (RedeclForcesDefC99(Prev))
2002 return false;
2003 }
2004 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00002005}
2006
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002007/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002008/// definition will be externally visible.
2009///
2010/// Inline function definitions are always available for inlining optimizations.
2011/// However, depending on the language dialect, declaration specifiers, and
2012/// attributes, the definition of an inline function may or may not be
2013/// "externally" visible to other translation units in the program.
2014///
2015/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002016/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002017/// inline definition becomes externally visible (C99 6.7.4p6).
2018///
2019/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2020/// definition, we use the GNU semantics for inline, which are nearly the
2021/// opposite of C99 semantics. In particular, "inline" by itself will create
2022/// an externally visible symbol, but "extern inline" will not create an
2023/// externally visible symbol.
2024bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002025 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002026 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002027 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002028
David Blaikie4e4d0842012-03-11 07:00:24 +00002029 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002030 // Note: If you change the logic here, please change
2031 // doesDeclarationForceExternallyVisibleDefinition as well.
2032 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002033 // If it's not the case that both 'inline' and 'extern' are
2034 // specified on the definition, then this inline definition is
2035 // externally visible.
2036 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2037 return true;
2038
2039 // If any declaration is 'inline' but not 'extern', then this definition
2040 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002041 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2042 Redecl != RedeclEnd;
2043 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002044 if (Redecl->isInlineSpecified() &&
2045 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002046 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002047 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002048
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002049 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002050 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002051
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002052 // C99 6.7.4p6:
2053 // [...] If all of the file scope declarations for a function in a
2054 // translation unit include the inline function specifier without extern,
2055 // then the definition in that translation unit is an inline definition.
2056 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2057 Redecl != RedeclEnd;
2058 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002059 if (RedeclForcesDefC99(*Redecl))
2060 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002061 }
2062
2063 // C99 6.7.4p6:
2064 // An inline definition does not provide an external definition for the
2065 // function, and does not forbid an external definition in another
2066 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002067 return false;
2068}
2069
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002070/// getOverloadedOperator - Which C++ overloaded operator this
2071/// function represents, if any.
2072OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002073 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2074 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002075 else
2076 return OO_None;
2077}
2078
Sean Hunta6c058d2010-01-13 09:01:02 +00002079/// getLiteralIdentifier - The literal suffix identifier this function
2080/// represents, if any.
2081const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2082 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2083 return getDeclName().getCXXLiteralIdentifier();
2084 else
2085 return 0;
2086}
2087
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002088FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2089 if (TemplateOrSpecialization.isNull())
2090 return TK_NonTemplate;
2091 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2092 return TK_FunctionTemplate;
2093 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2094 return TK_MemberSpecialization;
2095 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2096 return TK_FunctionTemplateSpecialization;
2097 if (TemplateOrSpecialization.is
2098 <DependentFunctionTemplateSpecializationInfo*>())
2099 return TK_DependentFunctionTemplateSpecialization;
2100
David Blaikieb219cfc2011-09-23 05:06:16 +00002101 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002102}
2103
Douglas Gregor2db32322009-10-07 23:56:10 +00002104FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002105 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002106 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2107
2108 return 0;
2109}
2110
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002111MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2112 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2113}
2114
Douglas Gregor2db32322009-10-07 23:56:10 +00002115void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002116FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2117 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002118 TemplateSpecializationKind TSK) {
2119 assert(TemplateOrSpecialization.isNull() &&
2120 "Member function is already a specialization");
2121 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002122 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002123 TemplateOrSpecialization = Info;
2124}
2125
Douglas Gregor3b846b62009-10-27 20:53:28 +00002126bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002127 // If the function is invalid, it can't be implicitly instantiated.
2128 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002129 return false;
2130
2131 switch (getTemplateSpecializationKind()) {
2132 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002133 case TSK_ExplicitInstantiationDefinition:
2134 return false;
2135
2136 case TSK_ImplicitInstantiation:
2137 return true;
2138
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002139 // It is possible to instantiate TSK_ExplicitSpecialization kind
2140 // if the FunctionDecl has a class scope specialization pattern.
2141 case TSK_ExplicitSpecialization:
2142 return getClassScopeSpecializationPattern() != 0;
2143
Douglas Gregor3b846b62009-10-27 20:53:28 +00002144 case TSK_ExplicitInstantiationDeclaration:
2145 // Handled below.
2146 break;
2147 }
2148
2149 // Find the actual template from which we will instantiate.
2150 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002151 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002152 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002153 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002154
2155 // C++0x [temp.explicit]p9:
2156 // Except for inline functions, other explicit instantiation declarations
2157 // have the effect of suppressing the implicit instantiation of the entity
2158 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002159 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002160 return true;
2161
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002162 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002163}
2164
2165bool FunctionDecl::isTemplateInstantiation() const {
2166 switch (getTemplateSpecializationKind()) {
2167 case TSK_Undeclared:
2168 case TSK_ExplicitSpecialization:
2169 return false;
2170 case TSK_ImplicitInstantiation:
2171 case TSK_ExplicitInstantiationDeclaration:
2172 case TSK_ExplicitInstantiationDefinition:
2173 return true;
2174 }
2175 llvm_unreachable("All TSK values handled.");
2176}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002177
2178FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002179 // Handle class scope explicit specialization special case.
2180 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2181 return getClassScopeSpecializationPattern();
2182
Douglas Gregor3b846b62009-10-27 20:53:28 +00002183 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2184 while (Primary->getInstantiatedFromMemberTemplate()) {
2185 // If we have hit a point where the user provided a specialization of
2186 // this template, we're done looking.
2187 if (Primary->isMemberSpecialization())
2188 break;
2189
2190 Primary = Primary->getInstantiatedFromMemberTemplate();
2191 }
2192
2193 return Primary->getTemplatedDecl();
2194 }
2195
2196 return getInstantiatedFromMemberFunction();
2197}
2198
Douglas Gregor16e8be22009-06-29 17:30:29 +00002199FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002200 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002201 = TemplateOrSpecialization
2202 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002203 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002204 }
2205 return 0;
2206}
2207
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002208FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2209 return getASTContext().getClassScopeSpecializationPattern(this);
2210}
2211
Douglas Gregor16e8be22009-06-29 17:30:29 +00002212const TemplateArgumentList *
2213FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002214 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002215 = TemplateOrSpecialization
2216 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002217 return Info->TemplateArguments;
2218 }
2219 return 0;
2220}
2221
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002222const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002223FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2224 if (FunctionTemplateSpecializationInfo *Info
2225 = TemplateOrSpecialization
2226 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2227 return Info->TemplateArgumentsAsWritten;
2228 }
2229 return 0;
2230}
2231
Mike Stump1eb44332009-09-09 15:08:12 +00002232void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002233FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2234 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002235 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002236 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002237 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002238 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2239 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002240 assert(TSK != TSK_Undeclared &&
2241 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002242 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002243 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002244 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002245 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2246 TemplateArgs,
2247 TemplateArgsAsWritten,
2248 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002249 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002250 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002251}
2252
John McCallaf2094e2010-04-08 09:05:18 +00002253void
2254FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2255 const UnresolvedSetImpl &Templates,
2256 const TemplateArgumentListInfo &TemplateArgs) {
2257 assert(TemplateOrSpecialization.isNull());
2258 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2259 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002260 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002261 void *Buffer = Context.Allocate(Size);
2262 DependentFunctionTemplateSpecializationInfo *Info =
2263 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2264 TemplateArgs);
2265 TemplateOrSpecialization = Info;
2266}
2267
2268DependentFunctionTemplateSpecializationInfo::
2269DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2270 const TemplateArgumentListInfo &TArgs)
2271 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2272
2273 d.NumTemplates = Ts.size();
2274 d.NumArgs = TArgs.size();
2275
2276 FunctionTemplateDecl **TsArray =
2277 const_cast<FunctionTemplateDecl**>(getTemplates());
2278 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2279 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2280
2281 TemplateArgumentLoc *ArgsArray =
2282 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2283 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2284 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2285}
2286
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002287TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002288 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002289 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002290 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002291 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002292 if (FTSInfo)
2293 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002294
Douglas Gregor2db32322009-10-07 23:56:10 +00002295 MemberSpecializationInfo *MSInfo
2296 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2297 if (MSInfo)
2298 return MSInfo->getTemplateSpecializationKind();
2299
2300 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002301}
2302
Mike Stump1eb44332009-09-09 15:08:12 +00002303void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002304FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2305 SourceLocation PointOfInstantiation) {
2306 if (FunctionTemplateSpecializationInfo *FTSInfo
2307 = TemplateOrSpecialization.dyn_cast<
2308 FunctionTemplateSpecializationInfo*>()) {
2309 FTSInfo->setTemplateSpecializationKind(TSK);
2310 if (TSK != TSK_ExplicitSpecialization &&
2311 PointOfInstantiation.isValid() &&
2312 FTSInfo->getPointOfInstantiation().isInvalid())
2313 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2314 } else if (MemberSpecializationInfo *MSInfo
2315 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2316 MSInfo->setTemplateSpecializationKind(TSK);
2317 if (TSK != TSK_ExplicitSpecialization &&
2318 PointOfInstantiation.isValid() &&
2319 MSInfo->getPointOfInstantiation().isInvalid())
2320 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2321 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002322 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002323}
2324
2325SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002326 if (FunctionTemplateSpecializationInfo *FTSInfo
2327 = TemplateOrSpecialization.dyn_cast<
2328 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002329 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002330 else if (MemberSpecializationInfo *MSInfo
2331 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002332 return MSInfo->getPointOfInstantiation();
2333
2334 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002335}
2336
Douglas Gregor9f185072009-09-11 20:15:17 +00002337bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002338 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002339 return true;
2340
2341 // If this function was instantiated from a member function of a
2342 // class template, check whether that member function was defined out-of-line.
2343 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2344 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002345 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002346 return Definition->isOutOfLine();
2347 }
2348
2349 // If this function was instantiated from a function template,
2350 // check whether that function template was defined out-of-line.
2351 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2352 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002353 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002354 return Definition->isOutOfLine();
2355 }
2356
2357 return false;
2358}
2359
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002360SourceRange FunctionDecl::getSourceRange() const {
2361 return SourceRange(getOuterLocStart(), EndRangeLoc);
2362}
2363
Anna Zaks9392d4e2012-01-18 02:45:01 +00002364unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002365 IdentifierInfo *FnInfo = getIdentifier();
2366
2367 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002368 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002369
2370 // Builtin handling.
2371 switch (getBuiltinID()) {
2372 case Builtin::BI__builtin_memset:
2373 case Builtin::BI__builtin___memset_chk:
2374 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002375 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002376
2377 case Builtin::BI__builtin_memcpy:
2378 case Builtin::BI__builtin___memcpy_chk:
2379 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002380 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002381
2382 case Builtin::BI__builtin_memmove:
2383 case Builtin::BI__builtin___memmove_chk:
2384 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002385 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002386
2387 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002388 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002389 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002390 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002391
2392 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002393 case Builtin::BImemcmp:
2394 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002395
2396 case Builtin::BI__builtin_strncpy:
2397 case Builtin::BI__builtin___strncpy_chk:
2398 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002399 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002400
2401 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002402 case Builtin::BIstrncmp:
2403 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002404
2405 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002406 case Builtin::BIstrncasecmp:
2407 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002408
2409 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002410 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002411 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002412 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002413
2414 case Builtin::BI__builtin_strndup:
2415 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002416 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002417
Anna Zaksc36bedc2012-02-01 19:08:57 +00002418 case Builtin::BI__builtin_strlen:
2419 case Builtin::BIstrlen:
2420 return Builtin::BIstrlen;
2421
Anna Zaksd9b859a2012-01-13 21:52:01 +00002422 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002423 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002424 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002425 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002426 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002427 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002428 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002429 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002430 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002431 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002432 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002433 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002434 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002435 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002436 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002437 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002438 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002439 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002440 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002441 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002442 else if (FnInfo->isStr("strlen"))
2443 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002444 }
2445 break;
2446 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002447 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002448}
2449
Chris Lattner8a934232008-03-31 00:36:02 +00002450//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002451// FieldDecl Implementation
2452//===----------------------------------------------------------------------===//
2453
Jay Foad4ba2a172011-01-12 09:06:06 +00002454FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002455 SourceLocation StartLoc, SourceLocation IdLoc,
2456 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002457 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2458 bool HasInit) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002459 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith7a614d82011-06-11 17:19:42 +00002460 BW, Mutable, HasInit);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002461}
2462
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002463FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2464 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2465 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2466 0, QualType(), 0, 0, false, false);
2467}
2468
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002469bool FieldDecl::isAnonymousStructOrUnion() const {
2470 if (!isImplicit() || getDeclName())
2471 return false;
2472
2473 if (const RecordType *Record = getType()->getAs<RecordType>())
2474 return Record->getDecl()->isAnonymousStructOrUnion();
2475
2476 return false;
2477}
2478
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002479unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2480 assert(isBitField() && "not a bitfield");
2481 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2482 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2483}
2484
John McCallba4f5d52011-01-20 07:57:12 +00002485unsigned FieldDecl::getFieldIndex() const {
2486 if (CachedFieldIndex) return CachedFieldIndex - 1;
2487
Richard Smith180f4792011-11-10 06:34:14 +00002488 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002489 const RecordDecl *RD = getParent();
2490 const FieldDecl *LastFD = 0;
2491 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smith180f4792011-11-10 06:34:14 +00002492
2493 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2494 I != E; ++I, ++Index) {
2495 (*I)->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002496
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002497 if (IsMsStruct) {
2498 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smith180f4792011-11-10 06:34:14 +00002499 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2500 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002501 continue;
2502 }
Richard Smith180f4792011-11-10 06:34:14 +00002503 LastFD = (*I);
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002504 }
John McCallba4f5d52011-01-20 07:57:12 +00002505 }
2506
Richard Smith180f4792011-11-10 06:34:14 +00002507 assert(CachedFieldIndex && "failed to find field in parent");
2508 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002509}
2510
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002511SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002512 if (const Expr *E = InitializerOrBitWidth.getPointer())
2513 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002514 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002515}
2516
Richard Smith7a614d82011-06-11 17:19:42 +00002517void FieldDecl::setInClassInitializer(Expr *Init) {
2518 assert(!InitializerOrBitWidth.getPointer() &&
2519 "bit width or initializer already set");
2520 InitializerOrBitWidth.setPointer(Init);
2521 InitializerOrBitWidth.setInt(0);
2522}
2523
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002524//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002525// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002526//===----------------------------------------------------------------------===//
2527
Douglas Gregor1693e152010-07-06 18:42:40 +00002528SourceLocation TagDecl::getOuterLocStart() const {
2529 return getTemplateOrInnerLocStart(this);
2530}
2531
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002532SourceRange TagDecl::getSourceRange() const {
2533 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002534 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002535}
2536
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002537TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002538 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002539}
2540
Richard Smith162e1c12011-04-15 14:24:37 +00002541void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2542 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002543 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002544 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002545 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002546}
2547
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002548void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002549 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002550
2551 if (isa<CXXRecordDecl>(this)) {
2552 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2553 struct CXXRecordDecl::DefinitionData *Data =
2554 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002555 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2556 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002557 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002558}
2559
2560void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002561 assert((!isa<CXXRecordDecl>(this) ||
2562 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2563 "definition completed but not started");
2564
John McCall5e1cdac2011-10-07 06:10:15 +00002565 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002566 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002567
2568 if (ASTMutationListener *L = getASTMutationListener())
2569 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002570}
2571
John McCall5e1cdac2011-10-07 06:10:15 +00002572TagDecl *TagDecl::getDefinition() const {
2573 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002574 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002575 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2576 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002577
2578 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002579 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002580 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002581 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002583 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002584}
2585
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002586void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2587 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002588 // Make sure the extended qualifier info is allocated.
2589 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002590 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002591 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002592 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002593 } else {
John McCallb6217662010-03-15 10:12:16 +00002594 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002595 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002596 if (getExtInfo()->NumTemplParamLists == 0) {
2597 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002598 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002599 }
2600 else
2601 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002602 }
2603 }
2604}
2605
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002606void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2607 unsigned NumTPLists,
2608 TemplateParameterList **TPLists) {
2609 assert(NumTPLists > 0);
2610 // Make sure the extended decl info is allocated.
2611 if (!hasExtInfo())
2612 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002613 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002614 // Set the template parameter lists info.
2615 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2616}
2617
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002618//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002619// EnumDecl Implementation
2620//===----------------------------------------------------------------------===//
2621
David Blaikie99ba9e32011-12-20 02:48:34 +00002622void EnumDecl::anchor() { }
2623
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002624EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2625 SourceLocation StartLoc, SourceLocation IdLoc,
2626 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002627 EnumDecl *PrevDecl, bool IsScoped,
2628 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002629 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002630 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002631 C.getTypeDeclType(Enum, PrevDecl);
2632 return Enum;
2633}
2634
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002635EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2636 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2637 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2638 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002639}
2640
Douglas Gregor838db382010-02-11 01:19:42 +00002641void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002642 QualType NewPromotionType,
2643 unsigned NumPositiveBits,
2644 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002645 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002646 if (!IntegerType)
2647 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002648 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002649 setNumPositiveBits(NumPositiveBits);
2650 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002651 TagDecl::completeDefinition();
2652}
2653
Richard Smith1af83c42012-03-23 03:33:32 +00002654TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2655 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2656 return MSI->getTemplateSpecializationKind();
2657
2658 return TSK_Undeclared;
2659}
2660
2661void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2662 SourceLocation PointOfInstantiation) {
2663 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2664 assert(MSI && "Not an instantiated member enumeration?");
2665 MSI->setTemplateSpecializationKind(TSK);
2666 if (TSK != TSK_ExplicitSpecialization &&
2667 PointOfInstantiation.isValid() &&
2668 MSI->getPointOfInstantiation().isInvalid())
2669 MSI->setPointOfInstantiation(PointOfInstantiation);
2670}
2671
Richard Smithf1c66b42012-03-14 23:13:10 +00002672EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2673 if (SpecializationInfo)
2674 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2675
2676 return 0;
2677}
2678
2679void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2680 TemplateSpecializationKind TSK) {
2681 assert(!SpecializationInfo && "Member enum is already a specialization");
2682 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2683}
2684
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002685//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002686// RecordDecl Implementation
2687//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002688
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002689RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2690 SourceLocation StartLoc, SourceLocation IdLoc,
2691 IdentifierInfo *Id, RecordDecl *PrevDecl)
2692 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002693 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002694 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002695 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002696 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002697 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002698}
2699
Jay Foad4ba2a172011-01-12 09:06:06 +00002700RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002701 SourceLocation StartLoc, SourceLocation IdLoc,
2702 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2703 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2704 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002705 C.getTypeDeclType(R, PrevDecl);
2706 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002707}
2708
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002709RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2710 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2711 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2712 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002713}
2714
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002715bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002716 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002717 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2718}
2719
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002720RecordDecl::field_iterator RecordDecl::field_begin() const {
2721 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2722 LoadFieldsFromExternalStorage();
2723
2724 return field_iterator(decl_iterator(FirstDecl));
2725}
2726
Douglas Gregorda2142f2011-02-19 18:51:44 +00002727/// completeDefinition - Notes that the definition of this type is now
2728/// complete.
2729void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002730 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002731 TagDecl::completeDefinition();
2732}
2733
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002734void RecordDecl::LoadFieldsFromExternalStorage() const {
2735 ExternalASTSource *Source = getASTContext().getExternalSource();
2736 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2737
2738 // Notify that we have a RecordDecl doing some initialization.
2739 ExternalASTSource::Deserializing TheFields(Source);
2740
Chris Lattner5f9e2722011-07-23 10:55:15 +00002741 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002742 LoadedFieldsFromExternalStorage = true;
2743 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2744 case ELR_Success:
2745 break;
2746
2747 case ELR_AlreadyLoaded:
2748 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002749 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002750 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002751
2752#ifndef NDEBUG
2753 // Check that all decls we got were FieldDecls.
2754 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2755 assert(isa<FieldDecl>(Decls[i]));
2756#endif
2757
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002758 if (Decls.empty())
2759 return;
2760
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002761 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2762 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002763}
2764
Steve Naroff56ee6892008-10-08 17:01:13 +00002765//===----------------------------------------------------------------------===//
2766// BlockDecl Implementation
2767//===----------------------------------------------------------------------===//
2768
David Blaikie4278c652011-09-21 18:16:56 +00002769void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002770 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Steve Naroffe78b8092009-03-13 16:56:44 +00002772 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002773 if (!NewParamInfo.empty()) {
2774 NumParams = NewParamInfo.size();
2775 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2776 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002777 }
2778}
2779
John McCall6b5a61b2011-02-07 10:33:21 +00002780void BlockDecl::setCaptures(ASTContext &Context,
2781 const Capture *begin,
2782 const Capture *end,
2783 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002784 CapturesCXXThis = capturesCXXThis;
2785
2786 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002787 NumCaptures = 0;
2788 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002789 return;
2790 }
2791
John McCall6b5a61b2011-02-07 10:33:21 +00002792 NumCaptures = end - begin;
2793
2794 // Avoid new Capture[] because we don't want to provide a default
2795 // constructor.
2796 size_t allocationSize = NumCaptures * sizeof(Capture);
2797 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2798 memcpy(buffer, begin, allocationSize);
2799 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002800}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002801
John McCall204e1332011-06-15 22:51:16 +00002802bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2803 for (capture_const_iterator
2804 i = capture_begin(), e = capture_end(); i != e; ++i)
2805 // Only auto vars can be captured, so no redeclaration worries.
2806 if (i->getVariable() == variable)
2807 return true;
2808
2809 return false;
2810}
2811
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002812SourceRange BlockDecl::getSourceRange() const {
2813 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2814}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002815
2816//===----------------------------------------------------------------------===//
2817// Other Decl Allocation/Deallocation Method Implementations
2818//===----------------------------------------------------------------------===//
2819
David Blaikie99ba9e32011-12-20 02:48:34 +00002820void TranslationUnitDecl::anchor() { }
2821
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002822TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2823 return new (C) TranslationUnitDecl(C);
2824}
2825
David Blaikie99ba9e32011-12-20 02:48:34 +00002826void LabelDecl::anchor() { }
2827
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002828LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002829 SourceLocation IdentL, IdentifierInfo *II) {
2830 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2831}
2832
2833LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2834 SourceLocation IdentL, IdentifierInfo *II,
2835 SourceLocation GnuLabelL) {
2836 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2837 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002838}
2839
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002840LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2841 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2842 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002843}
2844
David Blaikie99ba9e32011-12-20 02:48:34 +00002845void ValueDecl::anchor() { }
2846
2847void ImplicitParamDecl::anchor() { }
2848
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002849ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002850 SourceLocation IdLoc,
2851 IdentifierInfo *Id,
2852 QualType Type) {
2853 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002854}
2855
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002856ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2857 unsigned ID) {
2858 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2859 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2860}
2861
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002862FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002863 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002864 const DeclarationNameInfo &NameInfo,
2865 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002866 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002867 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002868 bool hasWrittenPrototype,
2869 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002870 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2871 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002872 isInlineSpecified,
2873 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002874 New->HasWrittenPrototype = hasWrittenPrototype;
2875 return New;
2876}
2877
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002878FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2879 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2880 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2881 DeclarationNameInfo(), QualType(), 0,
2882 SC_None, SC_None, false, false);
2883}
2884
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002885BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2886 return new (C) BlockDecl(DC, L);
2887}
2888
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002889BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2890 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2891 return new (Mem) BlockDecl(0, SourceLocation());
2892}
2893
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002894EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2895 SourceLocation L,
2896 IdentifierInfo *Id, QualType T,
2897 Expr *E, const llvm::APSInt &V) {
2898 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2899}
2900
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002901EnumConstantDecl *
2902EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2903 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2904 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2905 llvm::APSInt());
2906}
2907
David Blaikie99ba9e32011-12-20 02:48:34 +00002908void IndirectFieldDecl::anchor() { }
2909
Benjamin Kramerd9811462010-11-21 14:11:41 +00002910IndirectFieldDecl *
2911IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2912 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2913 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002914 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2915}
2916
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002917IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2918 unsigned ID) {
2919 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2920 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2921 QualType(), 0, 0);
2922}
2923
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002924SourceRange EnumConstantDecl::getSourceRange() const {
2925 SourceLocation End = getLocation();
2926 if (Init)
2927 End = Init->getLocEnd();
2928 return SourceRange(getLocation(), End);
2929}
2930
David Blaikie99ba9e32011-12-20 02:48:34 +00002931void TypeDecl::anchor() { }
2932
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002933TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002934 SourceLocation StartLoc, SourceLocation IdLoc,
2935 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2936 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002937}
2938
David Blaikie99ba9e32011-12-20 02:48:34 +00002939void TypedefNameDecl::anchor() { }
2940
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002941TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2942 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2943 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2944}
2945
Richard Smith162e1c12011-04-15 14:24:37 +00002946TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2947 SourceLocation StartLoc,
2948 SourceLocation IdLoc, IdentifierInfo *Id,
2949 TypeSourceInfo *TInfo) {
2950 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2951}
2952
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002953TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2954 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2955 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2956}
2957
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002958SourceRange TypedefDecl::getSourceRange() const {
2959 SourceLocation RangeEnd = getLocation();
2960 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2961 if (typeIsPostfix(TInfo->getType()))
2962 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2963 }
2964 return SourceRange(getLocStart(), RangeEnd);
2965}
2966
Richard Smith162e1c12011-04-15 14:24:37 +00002967SourceRange TypeAliasDecl::getSourceRange() const {
2968 SourceLocation RangeEnd = getLocStart();
2969 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2970 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2971 return SourceRange(getLocStart(), RangeEnd);
2972}
2973
David Blaikie99ba9e32011-12-20 02:48:34 +00002974void FileScopeAsmDecl::anchor() { }
2975
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002976FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002977 StringLiteral *Str,
2978 SourceLocation AsmLoc,
2979 SourceLocation RParenLoc) {
2980 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002981}
Douglas Gregor15de72c2011-12-02 23:23:56 +00002982
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002983FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2984 unsigned ID) {
2985 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2986 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2987}
2988
Douglas Gregor15de72c2011-12-02 23:23:56 +00002989//===----------------------------------------------------------------------===//
2990// ImportDecl Implementation
2991//===----------------------------------------------------------------------===//
2992
2993/// \brief Retrieve the number of module identifiers needed to name the given
2994/// module.
2995static unsigned getNumModuleIdentifiers(Module *Mod) {
2996 unsigned Result = 1;
2997 while (Mod->Parent) {
2998 Mod = Mod->Parent;
2999 ++Result;
3000 }
3001 return Result;
3002}
3003
Douglas Gregor5948ae12012-01-03 18:04:46 +00003004ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003005 Module *Imported,
3006 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003007 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003008 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003009{
3010 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3011 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3012 memcpy(StoredLocs, IdentifierLocs.data(),
3013 IdentifierLocs.size() * sizeof(SourceLocation));
3014}
3015
Douglas Gregor5948ae12012-01-03 18:04:46 +00003016ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003017 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003018 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003019 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003020{
3021 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3022}
3023
3024ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003025 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003026 ArrayRef<SourceLocation> IdentifierLocs) {
3027 void *Mem = C.Allocate(sizeof(ImportDecl) +
3028 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003029 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003030}
3031
3032ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003033 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003034 Module *Imported,
3035 SourceLocation EndLoc) {
3036 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003037 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003038 Import->setImplicit();
3039 return Import;
3040}
3041
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003042ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3043 unsigned NumLocations) {
3044 void *Mem = AllocateDeserializedDecl(C, ID,
3045 (sizeof(ImportDecl) +
3046 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003047 return new (Mem) ImportDecl(EmptyShell());
3048}
3049
3050ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3051 if (!ImportedAndComplete.getInt())
3052 return ArrayRef<SourceLocation>();
3053
3054 const SourceLocation *StoredLocs
3055 = reinterpret_cast<const SourceLocation *>(this + 1);
3056 return ArrayRef<SourceLocation>(StoredLocs,
3057 getNumModuleIdentifiers(getImportedModule()));
3058}
3059
3060SourceRange ImportDecl::getSourceRange() const {
3061 if (!ImportedAndComplete.getInt())
3062 return SourceRange(getLocation(),
3063 *reinterpret_cast<const SourceLocation *>(this + 1));
3064
3065 return SourceRange(getLocation(), getIdentifierLocs().back());
3066}