blob: 2268f647990ed3bf11dc5725f9cbff56ed6e763e [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 {
Rafael Espindola0f905902012-04-16 18:25:01 +000072 const bool ConsiderGlobalVisibility;
73 const bool ConsiderVisibilityAttributes;
74 const 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
Rafael Espindolae9836a22012-04-16 18:46:26 +0000276 if (F.ConsiderVisibilityAttributes) {
277 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000278 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000279 } else {
280 // If we're declared in a namespace with a visibility attribute,
281 // use that namespace's visibility, but don't call it explicit.
282 for (const DeclContext *DC = D->getDeclContext();
283 !isa<TranslationUnitDecl>(DC);
284 DC = DC->getParent()) {
285 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
286 if (!ND) continue;
287 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000288 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000289 break;
290 }
291 }
292 }
293 }
294
Douglas Gregord85b5b92009-11-25 22:24:25 +0000295 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000296
Douglas Gregord85b5b92009-11-25 22:24:25 +0000297 // A name having namespace scope has external linkage if it is the
298 // name of
299 //
300 // - an object or reference, unless it has internal linkage; or
301 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000302 // GCC applies the following optimization to variables and static
303 // data members, but not to functions:
304 //
John McCall1fb0caa2010-10-22 21:05:15 +0000305 // Modify the variable's LV by the LV of its type unless this is
306 // C or extern "C". This follows from [basic.link]p9:
307 // A type without linkage shall not be used as the type of a
308 // variable or function with external linkage unless
309 // - the entity has C language linkage, or
310 // - the entity is declared within an unnamed namespace, or
311 // - the entity is not used or is defined in the same
312 // translation unit.
313 // and [basic.link]p10:
314 // ...the types specified by all declarations referring to a
315 // given variable or function shall be identical...
316 // C does not have an equivalent rule.
317 //
John McCallac65c622010-10-26 04:59:26 +0000318 // Ignore this if we've got an explicit attribute; the user
319 // probably knows what they're doing.
320 //
John McCall1fb0caa2010-10-22 21:05:15 +0000321 // Note that we don't want to make the variable non-external
322 // because of this, but unique-external linkage suits us.
David Blaikie4e4d0842012-03-11 07:00:24 +0000323 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000324 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000325 LinkageInfo TypeLV = getLVForType(Var->getType());
326 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000327 return LinkageInfo::uniqueExternal();
Rafael Espindola54881cb2012-04-17 18:47:20 +0000328 LV.mergeVisibilityWithMin(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000329 }
330
John McCall35cebc32010-11-02 18:38:13 +0000331 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000332 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000333
David Blaikie4e4d0842012-03-11 07:00:24 +0000334 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000335 (Var->getStorageClass() == SC_Extern ||
336 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000337
Douglas Gregord85b5b92009-11-25 22:24:25 +0000338 // C99 6.2.2p4:
339 // For an identifier declared with the storage-class specifier
340 // extern in a scope in which a prior declaration of that
341 // identifier is visible, if the prior declaration specifies
342 // internal or external linkage, the linkage of the identifier
343 // at the later declaration is the same as the linkage
344 // specified at the prior declaration. If no prior declaration
345 // is visible, or if the prior declaration specifies no
346 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000347 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000348 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000349 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
350 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000351 }
352 }
353
Douglas Gregord85b5b92009-11-25 22:24:25 +0000354 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000355 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000356 // In theory, we can modify the function's LV by the LV of its
357 // type unless it has C linkage (see comment above about variables
358 // for justification). In practice, GCC doesn't do this, so it's
359 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000360
John McCall35cebc32010-11-02 18:38:13 +0000361 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000362 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000363
Douglas Gregord85b5b92009-11-25 22:24:25 +0000364 // C99 6.2.2p5:
365 // If the declaration of an identifier for a function has no
366 // storage-class specifier, its linkage is determined exactly
367 // as if it were declared with the storage-class specifier
368 // extern.
David Blaikie4e4d0842012-03-11 07:00:24 +0000369 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000370 (Function->getStorageClass() == SC_Extern ||
371 Function->getStorageClass() == SC_PrivateExtern ||
372 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000373 // C99 6.2.2p4:
374 // For an identifier declared with the storage-class specifier
375 // extern in a scope in which a prior declaration of that
376 // identifier is visible, if the prior declaration specifies
377 // internal or external linkage, the linkage of the identifier
378 // at the later declaration is the same as the linkage
379 // specified at the prior declaration. If no prior declaration
380 // is visible, or if the prior declaration specifies no
381 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000382 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000383 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000384 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
385 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000386 }
387 }
388
John McCallaf8ca372011-02-10 06:50:24 +0000389 // In C++, then if the type of the function uses a type with
390 // unique-external linkage, it's not legally usable from outside
391 // this translation unit. However, we should use the C linkage
392 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000393 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000394 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000395 Function->getType()->getLinkage() == UniqueExternalLinkage)
396 return LinkageInfo::uniqueExternal();
397
John McCall6ce51ee2011-06-27 23:06:04 +0000398 // Consider LV from the template and the template arguments unless
399 // this is an explicit specialization with a visibility attribute.
400 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000401 = Function->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000402 if (shouldConsiderTemplateLV(Function, specInfo)) {
403 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola62d9f112012-04-16 13:44:41 +0000404 LVFlags::CreateOnlyDeclLinkage()));
John McCall6ce51ee2011-06-27 23:06:04 +0000405 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola860097c2012-02-23 04:17:32 +0000406 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000407 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000408 }
409
Douglas Gregord85b5b92009-11-25 22:24:25 +0000410 // - a named class (Clause 9), or an unnamed class defined in a
411 // typedef declaration in which the class has the typedef name
412 // for linkage purposes (7.1.3); or
413 // - a named enumeration (7.2), or an unnamed enumeration
414 // defined in a typedef declaration in which the enumeration
415 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000416 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
417 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000418 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000419 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000420
John McCall1fb0caa2010-10-22 21:05:15 +0000421 // If this is a class template specialization, consider the
422 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000423 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000424 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000425 if (shouldConsiderTemplateLV(spec)) {
426 // From the template.
427 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola62d9f112012-04-16 13:44:41 +0000428 LVFlags::CreateOnlyDeclLinkage()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000429
John McCall6ce51ee2011-06-27 23:06:04 +0000430 // The arguments at which the template was instantiated.
431 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola860097c2012-02-23 04:17:32 +0000432 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000433 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000434 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000435
436 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000437 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000438 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000439 if (!isExternalLinkage(EnumLV.linkage()))
440 return LinkageInfo::none();
441 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000442
443 // - a template, unless it is a function template that has
444 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000445 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
446 if (F.ConsiderTemplateParameterTypes)
447 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000448
Douglas Gregord85b5b92009-11-25 22:24:25 +0000449 // - a namespace (7.3), unless it is declared within an unnamed
450 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000451 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
452 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000453
John McCall1fb0caa2010-10-22 21:05:15 +0000454 // By extension, we assign external linkage to Objective-C
455 // interfaces.
456 } else if (isa<ObjCInterfaceDecl>(D)) {
457 // fallout
458
459 // Everything not covered here has no linkage.
460 } else {
John McCallaf146032010-10-30 11:50:40 +0000461 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000462 }
463
464 // If we ended up with non-external linkage, visibility should
465 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000466 if (LV.linkage() != ExternalLinkage)
467 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000468
John McCall1fb0caa2010-10-22 21:05:15 +0000469 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000470}
471
John McCall36987482010-11-02 01:45:15 +0000472static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000473 // Only certain class members have linkage. Note that fields don't
474 // really have linkage, but it's convenient to say they do for the
475 // purposes of calculating linkage of pointer-to-data-member
476 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000477 if (!(isa<CXXMethodDecl>(D) ||
478 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000479 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000480 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000481 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000482 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000483
John McCall36987482010-11-02 01:45:15 +0000484 LinkageInfo LV;
David Blaikie4e4d0842012-03-11 07:00:24 +0000485 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
John McCall36987482010-11-02 01:45:15 +0000486
Rafael Espindola0f905902012-04-16 18:25:01 +0000487 bool DHasExplicitVisibility = false;
John McCall36987482010-11-02 01:45:15 +0000488 // If we have an explicit visibility attribute, merge that in.
489 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000490 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
491 LV.mergeVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000492
Rafael Espindola0f905902012-04-16 18:25:01 +0000493 DHasExplicitVisibility = true;
John McCall36987482010-11-02 01:45:15 +0000494 }
495 }
Rafael Espindola0f905902012-04-16 18:25:01 +0000496 // Ignore both global visibility and attributes when computing our
497 // parent's visibility if we already have an explicit one.
498 LVFlags ClassF = DHasExplicitVisibility ?
499 LVFlags::CreateOnlyDeclLinkage() : F;
500
501 // If we're paying attention to global visibility, apply
502 // -finline-visibility-hidden if this is an inline method.
503 //
504 // Note that we do this before merging information about
505 // the class visibility.
506 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
507 TemplateSpecializationKind TSK = TSK_Undeclared;
508 if (FunctionTemplateSpecializationInfo *spec
509 = MD->getTemplateSpecializationInfo()) {
510 TSK = spec->getTemplateSpecializationKind();
511 } else if (MemberSpecializationInfo *MSI =
512 MD->getMemberSpecializationInfo()) {
513 TSK = MSI->getTemplateSpecializationKind();
514 }
515
516 const FunctionDecl *Def = 0;
517 // InlineVisibilityHidden only applies to definitions, and
518 // isInlined() only gives meaningful answers on definitions
519 // anyway.
520 if (TSK != TSK_ExplicitInstantiationDeclaration &&
521 TSK != TSK_ExplicitInstantiationDefinition &&
522 F.ConsiderGlobalVisibility &&
523 !LV.visibilityExplicit() &&
524 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
525 MD->hasBody(Def) && Def->isInlined())
526 LV.mergeVisibility(HiddenVisibility, true);
527 }
John McCallaf146032010-10-30 11:50:40 +0000528
529 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000530 // linkage.
531 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
532 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000533 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000534
535 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000536 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000537 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000538
John McCall3cdfc4d2010-08-13 08:35:10 +0000539 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000540 // If the type of the function uses a type with unique-external
541 // linkage, it's not legally usable from outside this translation unit.
542 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
543 return LinkageInfo::uniqueExternal();
544
John McCall1fb0caa2010-10-22 21:05:15 +0000545 // If this is a method template specialization, use the linkage for
546 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000547 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000548 = MD->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000549 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola860097c2012-02-23 04:17:32 +0000550 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
551 F));
John McCall6ce51ee2011-06-27 23:06:04 +0000552 if (F.ConsiderTemplateParameterTypes)
553 LV.merge(getLVForTemplateParameterList(
554 spec->getTemplate()->getTemplateParameters()));
555 }
John McCall66cbcf32010-11-01 01:29:57 +0000556 }
John McCall1fb0caa2010-10-22 21:05:15 +0000557
John McCall110e8e52010-10-29 22:22:43 +0000558 // Note that in contrast to basically every other situation, we
559 // *do* apply -fvisibility to method declarations.
560
561 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000562 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000563 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000564 if (shouldConsiderTemplateLV(spec)) {
565 // Merge template argument/parameter information for member
566 // class template specializations.
Rafael Espindola860097c2012-02-23 04:17:32 +0000567 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
568 F));
John McCall1a0918a2011-03-04 10:39:25 +0000569 if (F.ConsiderTemplateParameterTypes)
570 LV.merge(getLVForTemplateParameterList(
John McCall6ce51ee2011-06-27 23:06:04 +0000571 spec->getSpecializedTemplate()->getTemplateParameters()));
572 }
John McCall110e8e52010-10-29 22:22:43 +0000573 }
574
John McCall110e8e52010-10-29 22:22:43 +0000575 // Static data members.
576 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000577 // Modify the variable's linkage by its type, but ignore the
578 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000579 LinkageInfo TypeLV = getLVForType(VD->getType());
580 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000581 LV.mergeLinkage(UniqueExternalLinkage);
582 if (!LV.visibilityExplicit())
Rafael Espindola54881cb2012-04-17 18:47:20 +0000583 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000584 }
585
John McCall1fb0caa2010-10-22 21:05:15 +0000586 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000587}
588
John McCallf76b0922011-02-08 19:01:05 +0000589static void clearLinkageForClass(const CXXRecordDecl *record) {
590 for (CXXRecordDecl::decl_iterator
591 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
592 Decl *child = *i;
593 if (isa<NamedDecl>(child))
594 cast<NamedDecl>(child)->ClearLinkageCache();
595 }
596}
597
David Blaikie99ba9e32011-12-20 02:48:34 +0000598void NamedDecl::anchor() { }
599
John McCallf76b0922011-02-08 19:01:05 +0000600void NamedDecl::ClearLinkageCache() {
601 // Note that we can't skip clearing the linkage of children just
602 // because the parent doesn't have cached linkage: we don't cache
603 // when computing linkage for parent contexts.
604
605 HasCachedLinkage = 0;
606
607 // If we're changing the linkage of a class, we need to reset the
608 // linkage of child declarations, too.
609 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
610 clearLinkageForClass(record);
611
John McCall15e310a2011-02-19 02:53:41 +0000612 if (ClassTemplateDecl *temp =
613 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000614 // Clear linkage for the template pattern.
615 CXXRecordDecl *record = temp->getTemplatedDecl();
616 record->HasCachedLinkage = 0;
617 clearLinkageForClass(record);
618
John McCall15e310a2011-02-19 02:53:41 +0000619 // We need to clear linkage for specializations, too.
620 for (ClassTemplateDecl::spec_iterator
621 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
622 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000623 }
John McCall15e310a2011-02-19 02:53:41 +0000624
625 // Clear cached linkage for function template decls, too.
626 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000627 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
628 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000629 for (FunctionTemplateDecl::spec_iterator
630 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
631 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000632 }
John McCall15e310a2011-02-19 02:53:41 +0000633
John McCallf76b0922011-02-08 19:01:05 +0000634}
635
Douglas Gregor381d34e2010-12-06 18:36:25 +0000636Linkage NamedDecl::getLinkage() const {
637 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000638 assert(Linkage(CachedLinkage) ==
639 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000640 return Linkage(CachedLinkage);
641 }
642
643 CachedLinkage = getLVForDecl(this,
644 LVFlags::CreateOnlyDeclLinkage()).linkage();
645 HasCachedLinkage = 1;
646 return Linkage(CachedLinkage);
647}
648
John McCallaf146032010-10-30 11:50:40 +0000649LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000650 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000651 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000652 HasCachedLinkage = 1;
653 CachedLinkage = LI.linkage();
654 return LI;
John McCall0df95872010-10-29 00:29:13 +0000655}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000656
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000657llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
658 // Use the most recent declaration of a variable.
659 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregoref96ee02012-01-14 16:38:05 +0000660 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000661
662 // Use the most recent declaration of a function, and also handle
663 // function template specializations.
664 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
665 if (llvm::Optional<Visibility> V
Douglas Gregoref96ee02012-01-14 16:38:05 +0000666 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000667 return V;
668
669 // If the function is a specialization of a template with an
670 // explicit visibility attribute, use that.
671 if (FunctionTemplateSpecializationInfo *templateInfo
672 = fn->getTemplateSpecializationInfo())
673 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
674
Rafael Espindola860097c2012-02-23 04:17:32 +0000675 // If the function is a member of a specialization of a class template
676 // and the corresponding decl has explicit visibility, use that.
677 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
678 if (InstantiatedFrom)
679 return getVisibilityOf(InstantiatedFrom);
680
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000681 return llvm::Optional<Visibility>();
682 }
683
684 // Otherwise, just check the declaration itself first.
685 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
686 return V;
687
688 // If there wasn't explicit visibility there, and this is a
689 // specialization of a class template, check for visibility
690 // on the pattern.
691 if (const ClassTemplateSpecializationDecl *spec
692 = dyn_cast<ClassTemplateSpecializationDecl>(this))
693 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
694
Rafael Espindola860097c2012-02-23 04:17:32 +0000695 // If this is a member class of a specialization of a class template
696 // and the corresponding decl has explicit visibility, use that.
697 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
698 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
699 if (InstantiatedFrom)
700 return getVisibilityOf(InstantiatedFrom);
701 }
702
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000703 return llvm::Optional<Visibility>();
704}
705
John McCall36987482010-11-02 01:45:15 +0000706static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000707 // Objective-C: treat all Objective-C declarations as having external
708 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000709 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000710 default:
711 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000712 case Decl::ParmVar:
713 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000714 case Decl::TemplateTemplateParm: // count these as external
715 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000716 case Decl::ObjCAtDefsField:
717 case Decl::ObjCCategory:
718 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000719 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000720 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000721 case Decl::ObjCMethod:
722 case Decl::ObjCProperty:
723 case Decl::ObjCPropertyImpl:
724 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000725 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000726
727 case Decl::CXXRecord: {
728 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
729 if (Record->isLambda()) {
730 if (!Record->getLambdaManglingNumber()) {
731 // This lambda has no mangling number, so it's internal.
732 return LinkageInfo::internal();
733 }
734
735 // This lambda has its linkage/visibility determined by its owner.
736 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
737 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
738 if (isa<ParmVarDecl>(ContextDecl))
739 DC = ContextDecl->getDeclContext()->getRedeclContext();
740 else
741 return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags);
742 }
743
744 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
745 return getLVForDecl(ND, Flags);
746
747 return LinkageInfo::external();
748 }
749
750 break;
751 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000752 }
753
Douglas Gregord85b5b92009-11-25 22:24:25 +0000754 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000755 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000756 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000757
758 // C++ [basic.link]p5:
759 // In addition, a member function, static data member, a named
760 // class or enumeration of class scope, or an unnamed class or
761 // enumeration defined in a class-scope typedef declaration such
762 // that the class or enumeration has the typedef name for linkage
763 // purposes (7.1.3), has external linkage if the name of the class
764 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000765 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000766 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000767
768 // C++ [basic.link]p6:
769 // The name of a function declared in block scope and the name of
770 // an object declared by a block scope extern declaration have
771 // linkage. If there is a visible declaration of an entity with
772 // linkage having the same name and type, ignoring entities
773 // declared outside the innermost enclosing namespace scope, the
774 // block scope declaration declares that same entity and receives
775 // the linkage of the previous declaration. If there is more than
776 // one such matching entity, the program is ill-formed. Otherwise,
777 // if no matching entity is found, the block scope entity receives
778 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000779 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
780 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000781 if (Function->isInAnonymousNamespace() &&
782 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000783 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000784
John McCallaf146032010-10-30 11:50:40 +0000785 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000786 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000787 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000788 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000789 }
790
Douglas Gregoref96ee02012-01-14 16:38:05 +0000791 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000792 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000793 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
794 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000795 }
796
797 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000798 }
799
John McCall0df95872010-10-29 00:29:13 +0000800 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000801 if (Var->getStorageClass() == SC_Extern ||
802 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000803 if (Var->isInAnonymousNamespace() &&
804 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000805 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000806
John McCallaf146032010-10-30 11:50:40 +0000807 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000808 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000809 LV.mergeVisibility(HiddenVisibility, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000810 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000811 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000812 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000813 }
814
Douglas Gregoref96ee02012-01-14 16:38:05 +0000815 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000816 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000817 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
818 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000819 }
820
821 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000822 }
823 }
824
825 // C++ [basic.link]p6:
826 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000827 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000828}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000829
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000830std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000831 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000832}
833
834std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000835 const DeclContext *Ctx = getDeclContext();
836
837 if (Ctx->isFunctionOrMethod())
838 return getNameAsString();
839
Chris Lattner5f9e2722011-07-23 10:55:15 +0000840 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000841 ContextsTy Contexts;
842
843 // Collect contexts.
844 while (Ctx && isa<NamedDecl>(Ctx)) {
845 Contexts.push_back(Ctx);
846 Ctx = Ctx->getParent();
847 };
848
849 std::string QualName;
850 llvm::raw_string_ostream OS(QualName);
851
852 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
853 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000854 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000855 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000856 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
857 std::string TemplateArgsStr
858 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000859 TemplateArgs.data(),
860 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000861 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000862 OS << Spec->getName() << TemplateArgsStr;
863 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000864 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000865 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000866 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000867 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000868 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
869 if (!RD->getIdentifier())
870 OS << "<anonymous " << RD->getKindName() << '>';
871 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000872 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000873 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000874 const FunctionProtoType *FT = 0;
875 if (FD->hasWrittenPrototype())
876 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
877
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000878 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000879 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000880 unsigned NumParams = FD->getNumParams();
881 for (unsigned i = 0; i < NumParams; ++i) {
882 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000883 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000884 std::string Param;
885 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000886 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000887 }
888
889 if (FT->isVariadic()) {
890 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000891 OS << ", ";
892 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000893 }
894 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000895 OS << ')';
896 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000897 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000898 }
899 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000900 }
901
John McCall8472af42010-03-16 21:48:18 +0000902 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000903 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000904 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000905 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000906
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000907 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000908}
909
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000910bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000911 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
912
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000913 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
914 // We want to keep it, unless it nominates same namespace.
915 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000916 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
917 ->getOriginalNamespace() ==
918 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
919 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000922 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
923 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000924 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000925
Douglas Gregore53060f2009-06-25 22:08:12 +0000926 // For function templates, the underlying function declarations are linked.
927 if (const FunctionTemplateDecl *FunctionTemplate
928 = dyn_cast<FunctionTemplateDecl>(this))
929 if (const FunctionTemplateDecl *OldFunctionTemplate
930 = dyn_cast<FunctionTemplateDecl>(OldD))
931 return FunctionTemplate->getTemplatedDecl()
932 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Steve Naroff0de21fd2009-02-22 19:35:57 +0000934 // For method declarations, we keep track of redeclarations.
935 if (isa<ObjCMethodDecl>(this))
936 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
John McCallf36e02d2009-10-09 21:13:30 +0000938 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
939 return true;
940
John McCall9488ea12009-11-17 05:59:44 +0000941 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
942 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
943 cast<UsingShadowDecl>(OldD)->getTargetDecl();
944
Douglas Gregordc355712011-02-25 00:36:19 +0000945 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
946 ASTContext &Context = getASTContext();
947 return Context.getCanonicalNestedNameSpecifier(
948 cast<UsingDecl>(this)->getQualifier()) ==
949 Context.getCanonicalNestedNameSpecifier(
950 cast<UsingDecl>(OldD)->getQualifier());
951 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000952
Douglas Gregor7a537402012-01-03 23:26:26 +0000953 // A typedef of an Objective-C class type can replace an Objective-C class
954 // declaration or definition, and vice versa.
955 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
956 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
957 return true;
958
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000959 // For non-function declarations, if the declarations are of the
960 // same kind then this must be a redeclaration, or semantic analysis
961 // would not have given us the new declaration.
962 return this->getKind() == OldD->getKind();
963}
964
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000965bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000966 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000967}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000968
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000969NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000970 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000971 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
972 ND = UD->getTargetDecl();
973
974 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
975 return AD->getClassInterface();
976
977 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +0000978}
979
John McCall161755a2010-04-06 21:38:20 +0000980bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000981 if (!isCXXClassMember())
982 return false;
983
John McCall161755a2010-04-06 21:38:20 +0000984 const NamedDecl *D = this;
985 if (isa<UsingShadowDecl>(D))
986 D = cast<UsingShadowDecl>(D)->getTargetDecl();
987
Francois Pichet87c2e122010-11-21 06:08:52 +0000988 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000989 return true;
990 if (isa<CXXMethodDecl>(D))
991 return cast<CXXMethodDecl>(D)->isInstance();
992 if (isa<FunctionTemplateDecl>(D))
993 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
994 ->getTemplatedDecl())->isInstance();
995 return false;
996}
997
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000998//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000999// DeclaratorDecl Implementation
1000//===----------------------------------------------------------------------===//
1001
Douglas Gregor1693e152010-07-06 18:42:40 +00001002template <typename DeclT>
1003static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1004 if (decl->getNumTemplateParameterLists() > 0)
1005 return decl->getTemplateParameterList(0)->getTemplateLoc();
1006 else
1007 return decl->getInnerLocStart();
1008}
1009
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001010SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001011 TypeSourceInfo *TSI = getTypeSourceInfo();
1012 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001013 return SourceLocation();
1014}
1015
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001016void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1017 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001018 // Make sure the extended decl info is allocated.
1019 if (!hasExtInfo()) {
1020 // Save (non-extended) type source info pointer.
1021 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1022 // Allocate external info struct.
1023 DeclInfo = new (getASTContext()) ExtInfo;
1024 // Restore savedTInfo into (extended) decl info.
1025 getExtInfo()->TInfo = savedTInfo;
1026 }
1027 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001028 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001029 } else {
John McCallb6217662010-03-15 10:12:16 +00001030 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001031 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001032 if (getExtInfo()->NumTemplParamLists == 0) {
1033 // Save type source info pointer.
1034 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1035 // Deallocate the extended decl info.
1036 getASTContext().Deallocate(getExtInfo());
1037 // Restore savedTInfo into (non-extended) decl info.
1038 DeclInfo = savedTInfo;
1039 }
1040 else
1041 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001042 }
1043 }
1044}
1045
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001046void
1047DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1048 unsigned NumTPLists,
1049 TemplateParameterList **TPLists) {
1050 assert(NumTPLists > 0);
1051 // Make sure the extended decl info is allocated.
1052 if (!hasExtInfo()) {
1053 // Save (non-extended) type source info pointer.
1054 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1055 // Allocate external info struct.
1056 DeclInfo = new (getASTContext()) ExtInfo;
1057 // Restore savedTInfo into (extended) decl info.
1058 getExtInfo()->TInfo = savedTInfo;
1059 }
1060 // Set the template parameter lists info.
1061 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1062}
1063
Douglas Gregor1693e152010-07-06 18:42:40 +00001064SourceLocation DeclaratorDecl::getOuterLocStart() const {
1065 return getTemplateOrInnerLocStart(this);
1066}
1067
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001068namespace {
1069
1070// Helper function: returns true if QT is or contains a type
1071// having a postfix component.
1072bool typeIsPostfix(clang::QualType QT) {
1073 while (true) {
1074 const Type* T = QT.getTypePtr();
1075 switch (T->getTypeClass()) {
1076 default:
1077 return false;
1078 case Type::Pointer:
1079 QT = cast<PointerType>(T)->getPointeeType();
1080 break;
1081 case Type::BlockPointer:
1082 QT = cast<BlockPointerType>(T)->getPointeeType();
1083 break;
1084 case Type::MemberPointer:
1085 QT = cast<MemberPointerType>(T)->getPointeeType();
1086 break;
1087 case Type::LValueReference:
1088 case Type::RValueReference:
1089 QT = cast<ReferenceType>(T)->getPointeeType();
1090 break;
1091 case Type::PackExpansion:
1092 QT = cast<PackExpansionType>(T)->getPattern();
1093 break;
1094 case Type::Paren:
1095 case Type::ConstantArray:
1096 case Type::DependentSizedArray:
1097 case Type::IncompleteArray:
1098 case Type::VariableArray:
1099 case Type::FunctionProto:
1100 case Type::FunctionNoProto:
1101 return true;
1102 }
1103 }
1104}
1105
1106} // namespace
1107
1108SourceRange DeclaratorDecl::getSourceRange() const {
1109 SourceLocation RangeEnd = getLocation();
1110 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1111 if (typeIsPostfix(TInfo->getType()))
1112 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1113 }
1114 return SourceRange(getOuterLocStart(), RangeEnd);
1115}
1116
Abramo Bagnara9b934882010-06-12 08:15:14 +00001117void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001118QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1119 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001120 TemplateParameterList **TPLists) {
1121 assert((NumTPLists == 0 || TPLists != 0) &&
1122 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001123
1124 // Free previous template parameters (if any).
1125 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001126 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001127 TemplParamLists = 0;
1128 NumTemplParamLists = 0;
1129 }
1130 // Set info on matched template parameter lists (if any).
1131 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001132 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001133 NumTemplParamLists = NumTPLists;
1134 for (unsigned i = NumTPLists; i-- > 0; )
1135 TemplParamLists[i] = TPLists[i];
1136 }
1137}
1138
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001139//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001140// VarDecl Implementation
1141//===----------------------------------------------------------------------===//
1142
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001143const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1144 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001145 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001146 case SC_Auto: return "auto";
1147 case SC_Extern: return "extern";
1148 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1149 case SC_PrivateExtern: return "__private_extern__";
1150 case SC_Register: return "register";
1151 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001152 }
1153
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001154 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001155}
1156
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001157VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1158 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001159 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001160 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001161 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001162}
1163
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001164VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1165 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1166 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1167 QualType(), 0, SC_None, SC_None);
1168}
1169
Douglas Gregor381d34e2010-12-06 18:36:25 +00001170void VarDecl::setStorageClass(StorageClass SC) {
1171 assert(isLegalForVariable(SC));
1172 if (getStorageClass() != SC)
1173 ClearLinkageCache();
1174
John McCallf1e4fbf2011-05-01 02:13:58 +00001175 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001176}
1177
Douglas Gregor1693e152010-07-06 18:42:40 +00001178SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001179 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001180 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001181 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001182}
1183
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001184bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001185 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001186 return false;
1187
Eli Friedman750dc2b2012-01-15 01:23:58 +00001188 const DeclContext *DC = getDeclContext();
1189 if (DC->isRecord())
1190 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001191
Eli Friedman750dc2b2012-01-15 01:23:58 +00001192 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001193 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001194 return true;
1195 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001196}
1197
1198VarDecl *VarDecl::getCanonicalDecl() {
1199 return getFirstDeclaration();
1200}
1201
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001202VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1203 ASTContext &C) const
1204{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001205 // C++ [basic.def]p2:
1206 // A declaration is a definition unless [...] it contains the 'extern'
1207 // specifier or a linkage-specification and neither an initializer [...],
1208 // it declares a static data member in a class declaration [...].
1209 // C++ [temp.expl.spec]p15:
1210 // An explicit specialization of a static data member of a template is a
1211 // definition if the declaration includes an initializer; otherwise, it is
1212 // a declaration.
1213 if (isStaticDataMember()) {
1214 if (isOutOfLine() && (hasInit() ||
1215 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1216 return Definition;
1217 else
1218 return DeclarationOnly;
1219 }
1220 // C99 6.7p5:
1221 // A definition of an identifier is a declaration for that identifier that
1222 // [...] causes storage to be reserved for that object.
1223 // Note: that applies for all non-file-scope objects.
1224 // C99 6.9.2p1:
1225 // If the declaration of an identifier for an object has file scope and an
1226 // initializer, the declaration is an external definition for the identifier
1227 if (hasInit())
1228 return Definition;
1229 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1230 if (hasExternalStorage())
1231 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001232
John McCalld931b082010-08-26 03:08:43 +00001233 if (getStorageClassAsWritten() == SC_Extern ||
1234 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001235 for (const VarDecl *PrevVar = getPreviousDecl();
1236 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001237 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1238 return DeclarationOnly;
1239 }
1240 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001241 // C99 6.9.2p2:
1242 // A declaration of an object that has file scope without an initializer,
1243 // and without a storage class specifier or the scs 'static', constitutes
1244 // a tentative definition.
1245 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001246 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001247 return TentativeDefinition;
1248
1249 // What's left is (in C, block-scope) declarations without initializers or
1250 // external storage. These are definitions.
1251 return Definition;
1252}
1253
Sebastian Redle9d12b62010-01-31 22:27:38 +00001254VarDecl *VarDecl::getActingDefinition() {
1255 DefinitionKind Kind = isThisDeclarationADefinition();
1256 if (Kind != TentativeDefinition)
1257 return 0;
1258
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001259 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001260 VarDecl *First = getFirstDeclaration();
1261 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1262 I != E; ++I) {
1263 Kind = (*I)->isThisDeclarationADefinition();
1264 if (Kind == Definition)
1265 return 0;
1266 else if (Kind == TentativeDefinition)
1267 LastTentative = *I;
1268 }
1269 return LastTentative;
1270}
1271
1272bool VarDecl::isTentativeDefinitionNow() const {
1273 DefinitionKind Kind = isThisDeclarationADefinition();
1274 if (Kind != TentativeDefinition)
1275 return false;
1276
1277 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1278 if ((*I)->isThisDeclarationADefinition() == Definition)
1279 return false;
1280 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001281 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001282}
1283
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001284VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001285 VarDecl *First = getFirstDeclaration();
1286 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1287 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001288 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001289 return *I;
1290 }
1291 return 0;
1292}
1293
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001294VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001295 DefinitionKind Kind = DeclarationOnly;
1296
1297 const VarDecl *First = getFirstDeclaration();
1298 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001299 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001300 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001301 if (Kind == Definition)
1302 break;
1303 }
John McCall110e8e52010-10-29 22:22:43 +00001304
1305 return Kind;
1306}
1307
Sebastian Redl31310a22010-02-01 20:16:42 +00001308const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001309 redecl_iterator I = redecls_begin(), E = redecls_end();
1310 while (I != E && !I->getInit())
1311 ++I;
1312
1313 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001314 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001315 return I->getInit();
1316 }
1317 return 0;
1318}
1319
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001320bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001321 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001322 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001323
1324 if (!isStaticDataMember())
1325 return false;
1326
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001327 // If this static data member was instantiated from a static data member of
1328 // a class template, check whether that static data member was defined
1329 // out-of-line.
1330 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1331 return VD->isOutOfLine();
1332
1333 return false;
1334}
1335
Douglas Gregor0d035142009-10-27 18:42:08 +00001336VarDecl *VarDecl::getOutOfLineDefinition() {
1337 if (!isStaticDataMember())
1338 return 0;
1339
1340 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1341 RD != RDEnd; ++RD) {
1342 if (RD->getLexicalDeclContext()->isFileContext())
1343 return *RD;
1344 }
1345
1346 return 0;
1347}
1348
Douglas Gregor838db382010-02-11 01:19:42 +00001349void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001350 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1351 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001352 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001353 }
1354
1355 Init = I;
1356}
1357
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001358bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001359 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001360
Richard Smith16581332012-03-02 04:14:40 +00001361 if (!Lang.CPlusPlus)
1362 return false;
1363
1364 // In C++11, any variable of reference type can be used in a constant
1365 // expression if it is initialized by a constant expression.
1366 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1367 return true;
1368
1369 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001370 // not require the variable to be non-volatile, but we consider this to be a
1371 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001372 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001373 return false;
1374
1375 // In C++, const, non-volatile variables of integral or enumeration types
1376 // can be used in constant expressions.
1377 if (getType()->isIntegralOrEnumerationType())
1378 return true;
1379
Richard Smith16581332012-03-02 04:14:40 +00001380 // Additionally, in C++11, non-volatile constexpr variables can be used in
1381 // constant expressions.
1382 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001383}
1384
Richard Smith099e7f62011-12-19 06:19:21 +00001385/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1386/// form, which contains extra information on the evaluated value of the
1387/// initializer.
1388EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1389 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1390 if (!Eval) {
1391 Stmt *S = Init.get<Stmt *>();
1392 Eval = new (getASTContext()) EvaluatedStmt;
1393 Eval->Value = S;
1394 Init = Eval;
1395 }
1396 return Eval;
1397}
1398
Richard Smith2d6a5672012-01-14 04:30:29 +00001399APValue *VarDecl::evaluateValue() const {
1400 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1401 return evaluateValue(Notes);
1402}
1403
1404APValue *VarDecl::evaluateValue(
1405 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001406 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1407
1408 // We only produce notes indicating why an initializer is non-constant the
1409 // first time it is evaluated. FIXME: The notes won't always be emitted the
1410 // first time we try evaluation, so might not be produced at all.
1411 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001412 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001413
1414 const Expr *Init = cast<Expr>(Eval->Value);
1415 assert(!Init->isValueDependent());
1416
1417 if (Eval->IsEvaluating) {
1418 // FIXME: Produce a diagnostic for self-initialization.
1419 Eval->CheckedICE = true;
1420 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001421 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001422 }
1423
1424 Eval->IsEvaluating = true;
1425
1426 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1427 this, Notes);
1428
1429 // Ensure the result is an uninitialized APValue if evaluation fails.
1430 if (!Result)
1431 Eval->Evaluated = APValue();
1432
1433 Eval->IsEvaluating = false;
1434 Eval->WasEvaluated = true;
1435
1436 // In C++11, we have determined whether the initializer was a constant
1437 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001438 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001439 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001440 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001441 }
1442
Richard Smith2d6a5672012-01-14 04:30:29 +00001443 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001444}
1445
1446bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001447 // Initializers of weak variables are never ICEs.
1448 if (isWeak())
1449 return false;
1450
Richard Smith099e7f62011-12-19 06:19:21 +00001451 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1452 if (Eval->CheckedICE)
1453 // We have already checked whether this subexpression is an
1454 // integral constant expression.
1455 return Eval->IsICE;
1456
1457 const Expr *Init = cast<Expr>(Eval->Value);
1458 assert(!Init->isValueDependent());
1459
1460 // In C++11, evaluate the initializer to check whether it's a constant
1461 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001462 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001463 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1464 evaluateValue(Notes);
1465 return Eval->IsICE;
1466 }
1467
1468 // It's an ICE whether or not the definition we found is
1469 // out-of-line. See DR 721 and the discussion in Clang PR
1470 // 6206 for details.
1471
1472 if (Eval->CheckingICE)
1473 return false;
1474 Eval->CheckingICE = true;
1475
1476 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1477 Eval->CheckingICE = false;
1478 Eval->CheckedICE = true;
1479 return Eval->IsICE;
1480}
1481
Douglas Gregor03e80032011-06-21 17:03:29 +00001482bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001483 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001484
1485 const Expr *E = getInit();
1486 if (!E)
1487 return false;
1488
1489 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1490 E = Cleanups->getSubExpr();
1491
1492 return isa<MaterializeTemporaryExpr>(E);
1493}
1494
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001495VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001496 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001497 return cast<VarDecl>(MSI->getInstantiatedFrom());
1498
1499 return 0;
1500}
1501
Douglas Gregor663b5a02009-10-14 20:14:33 +00001502TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001503 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001504 return MSI->getTemplateSpecializationKind();
1505
1506 return TSK_Undeclared;
1507}
1508
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001509MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001510 return getASTContext().getInstantiatedFromStaticDataMember(this);
1511}
1512
Douglas Gregor0a897e32009-10-15 17:21:20 +00001513void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1514 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001515 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001516 assert(MSI && "Not an instantiated static data member?");
1517 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001518 if (TSK != TSK_ExplicitSpecialization &&
1519 PointOfInstantiation.isValid() &&
1520 MSI->getPointOfInstantiation().isInvalid())
1521 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001522}
1523
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001524//===----------------------------------------------------------------------===//
1525// ParmVarDecl Implementation
1526//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001527
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001528ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001529 SourceLocation StartLoc,
1530 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001531 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001532 StorageClass S, StorageClass SCAsWritten,
1533 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001534 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001535 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001536}
1537
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001538ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1539 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1540 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1541 0, QualType(), 0, SC_None, SC_None, 0);
1542}
1543
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001544SourceRange ParmVarDecl::getSourceRange() const {
1545 if (!hasInheritedDefaultArg()) {
1546 SourceRange ArgRange = getDefaultArgRange();
1547 if (ArgRange.isValid())
1548 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1549 }
1550
1551 return DeclaratorDecl::getSourceRange();
1552}
1553
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001554Expr *ParmVarDecl::getDefaultArg() {
1555 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1556 assert(!hasUninstantiatedDefaultArg() &&
1557 "Default argument is not yet instantiated!");
1558
1559 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001560 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001561 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001562
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001563 return Arg;
1564}
1565
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001566SourceRange ParmVarDecl::getDefaultArgRange() const {
1567 if (const Expr *E = getInit())
1568 return E->getSourceRange();
1569
1570 if (hasUninstantiatedDefaultArg())
1571 return getUninstantiatedDefaultArg()->getSourceRange();
1572
1573 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001574}
1575
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001576bool ParmVarDecl::isParameterPack() const {
1577 return isa<PackExpansionType>(getType());
1578}
1579
Ted Kremenekd211cb72011-10-06 05:00:56 +00001580void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1581 getASTContext().setParameterIndex(this, parameterIndex);
1582 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1583}
1584
1585unsigned ParmVarDecl::getParameterIndexLarge() const {
1586 return getASTContext().getParameterIndex(this);
1587}
1588
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001589//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001590// FunctionDecl Implementation
1591//===----------------------------------------------------------------------===//
1592
Douglas Gregorda2142f2011-02-19 18:51:44 +00001593void FunctionDecl::getNameForDiagnostic(std::string &S,
1594 const PrintingPolicy &Policy,
1595 bool Qualified) const {
1596 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1597 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1598 if (TemplateArgs)
1599 S += TemplateSpecializationType::PrintTemplateArgumentList(
1600 TemplateArgs->data(),
1601 TemplateArgs->size(),
1602 Policy);
1603
1604}
1605
Ted Kremenek9498d382010-04-29 16:49:01 +00001606bool FunctionDecl::isVariadic() const {
1607 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1608 return FT->isVariadic();
1609 return false;
1610}
1611
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001612bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1613 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001614 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001615 Definition = *I;
1616 return true;
1617 }
1618 }
1619
1620 return false;
1621}
1622
Anders Carlssonffb945f2011-05-14 23:26:09 +00001623bool FunctionDecl::hasTrivialBody() const
1624{
1625 Stmt *S = getBody();
1626 if (!S) {
1627 // Since we don't have a body for this function, we don't know if it's
1628 // trivial or not.
1629 return false;
1630 }
1631
1632 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1633 return true;
1634 return false;
1635}
1636
Sean Hunt10620eb2011-05-06 20:44:56 +00001637bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1638 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001639 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001640 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1641 return true;
1642 }
1643 }
1644
1645 return false;
1646}
1647
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001648Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001649 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1650 if (I->Body) {
1651 Definition = *I;
1652 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001653 } else if (I->IsLateTemplateParsed) {
1654 Definition = *I;
1655 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001656 }
1657 }
1658
1659 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001660}
1661
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001662void FunctionDecl::setBody(Stmt *B) {
1663 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001664 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001665 EndRangeLoc = B->getLocEnd();
1666}
1667
Douglas Gregor21386642010-09-28 21:55:22 +00001668void FunctionDecl::setPure(bool P) {
1669 IsPure = P;
1670 if (P)
1671 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1672 Parent->markedVirtualFunctionPure();
1673}
1674
Douglas Gregor48a83b52009-09-12 00:17:51 +00001675bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001676 const TranslationUnitDecl *tunit =
1677 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1678 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001679 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001680 getIdentifier() &&
1681 getIdentifier()->isStr("main");
1682}
1683
1684bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1685 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1686 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1687 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1688 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1689 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1690
1691 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1692 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1693
1694 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1695 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1696
1697 ASTContext &Context =
1698 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1699 ->getASTContext();
1700
1701 // The result type and first argument type are constant across all
1702 // these operators. The second argument must be exactly void*.
1703 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001704}
1705
Douglas Gregor48a83b52009-09-12 00:17:51 +00001706bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001707 if (getLinkage() != ExternalLinkage)
1708 return false;
1709
1710 if (getAttr<OverloadableAttr>())
1711 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001712
Chandler Carruth10aad442011-02-25 00:05:02 +00001713 const DeclContext *DC = getDeclContext();
1714 if (DC->isRecord())
1715 return false;
1716
Eli Friedman750dc2b2012-01-15 01:23:58 +00001717 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001718 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001719 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001720
Eli Friedman750dc2b2012-01-15 01:23:58 +00001721 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001722}
1723
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001724bool FunctionDecl::isGlobal() const {
1725 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1726 return Method->isStatic();
1727
John McCalld931b082010-08-26 03:08:43 +00001728 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001729 return false;
1730
Mike Stump1eb44332009-09-09 15:08:12 +00001731 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001732 DC->isNamespace();
1733 DC = DC->getParent()) {
1734 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1735 if (!Namespace->getDeclName())
1736 return false;
1737 break;
1738 }
1739 }
1740
1741 return true;
1742}
1743
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001744void
1745FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1746 redeclarable_base::setPreviousDeclaration(PrevDecl);
1747
1748 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1749 FunctionTemplateDecl *PrevFunTmpl
1750 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1751 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1752 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1753 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001754
Axel Naumannd9d137e2011-11-08 18:21:06 +00001755 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001756 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001757}
1758
1759const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1760 return getFirstDeclaration();
1761}
1762
1763FunctionDecl *FunctionDecl::getCanonicalDecl() {
1764 return getFirstDeclaration();
1765}
1766
Douglas Gregor381d34e2010-12-06 18:36:25 +00001767void FunctionDecl::setStorageClass(StorageClass SC) {
1768 assert(isLegalForFunction(SC));
1769 if (getStorageClass() != SC)
1770 ClearLinkageCache();
1771
1772 SClass = SC;
1773}
1774
Douglas Gregor3e41d602009-02-13 23:20:09 +00001775/// \brief Returns a value indicating whether this function
1776/// corresponds to a builtin function.
1777///
1778/// The function corresponds to a built-in function if it is
1779/// declared at translation scope or within an extern "C" block and
1780/// its name matches with the name of a builtin. The returned value
1781/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001782/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001783/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001784unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001785 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001786 return 0;
1787
1788 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001789 if (!BuiltinID)
1790 return 0;
1791
1792 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001793 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1794 return BuiltinID;
1795
1796 // This function has the name of a known C library
1797 // function. Determine whether it actually refers to the C library
1798 // function or whether it just has the same name.
1799
Douglas Gregor9add3172009-02-17 03:23:10 +00001800 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001801 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001802 return 0;
1803
Douglas Gregor3c385e52009-02-14 18:57:46 +00001804 // If this function is at translation-unit scope and we're not in
1805 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001806 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001807 getDeclContext()->isTranslationUnit())
1808 return BuiltinID;
1809
1810 // If the function is in an extern "C" linkage specification and is
1811 // not marked "overloadable", it's the real function.
1812 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001813 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001814 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001815 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001816 return BuiltinID;
1817
1818 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001819 return 0;
1820}
1821
1822
Chris Lattner1ad9b282009-04-25 06:03:53 +00001823/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001824/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001825/// after it has been created.
1826unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001827 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001828 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001829 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001830 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Reid Spencer5f016e22007-07-11 17:01:13 +00001832}
1833
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001834void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001835 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001837 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Reid Spencer5f016e22007-07-11 17:01:13 +00001839 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001840 if (!NewParamInfo.empty()) {
1841 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1842 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001843 }
1844}
1845
James Molloy16f1f712012-02-29 10:24:19 +00001846void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1847 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1848
1849 if (!NewDecls.empty()) {
1850 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1851 std::copy(NewDecls.begin(), NewDecls.end(), A);
1852 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1853 }
1854}
1855
Chris Lattner8123a952008-04-10 02:22:51 +00001856/// getMinRequiredArguments - Returns the minimum number of arguments
1857/// needed to call this function. This may be fewer than the number of
1858/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001859/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001860unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001861 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001862 return getNumParams();
1863
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001864 unsigned NumRequiredArgs = getNumParams();
1865
1866 // If the last parameter is a parameter pack, we don't need an argument for
1867 // it.
1868 if (NumRequiredArgs > 0 &&
1869 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1870 --NumRequiredArgs;
1871
1872 // If this parameter has a default argument, we don't need an argument for
1873 // it.
1874 while (NumRequiredArgs > 0 &&
1875 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001876 --NumRequiredArgs;
1877
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001878 // We might have parameter packs before the end. These can't be deduced,
1879 // but they can still handle multiple arguments.
1880 unsigned ArgIdx = NumRequiredArgs;
1881 while (ArgIdx > 0) {
1882 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1883 NumRequiredArgs = ArgIdx;
1884
1885 --ArgIdx;
1886 }
1887
Chris Lattner8123a952008-04-10 02:22:51 +00001888 return NumRequiredArgs;
1889}
1890
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001891bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001892 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001893 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001894
1895 if (isa<CXXMethodDecl>(this)) {
1896 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1897 return true;
1898 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001899
1900 switch (getTemplateSpecializationKind()) {
1901 case TSK_Undeclared:
1902 case TSK_ExplicitSpecialization:
1903 return false;
1904
1905 case TSK_ImplicitInstantiation:
1906 case TSK_ExplicitInstantiationDeclaration:
1907 case TSK_ExplicitInstantiationDefinition:
1908 // Handle below.
1909 break;
1910 }
1911
1912 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001913 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001914 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001915 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001916
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001917 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001918 return PatternDecl->isInlined();
1919
1920 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001921}
1922
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001923static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1924 // Only consider file-scope declarations in this test.
1925 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1926 return false;
1927
1928 // Only consider explicit declarations; the presence of a builtin for a
1929 // libcall shouldn't affect whether a definition is externally visible.
1930 if (Redecl->isImplicit())
1931 return false;
1932
1933 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1934 return true; // Not an inline definition
1935
1936 return false;
1937}
1938
Nick Lewyckydce67a72011-07-18 05:26:13 +00001939/// \brief For a function declaration in C or C++, determine whether this
1940/// declaration causes the definition to be externally visible.
1941///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001942/// Specifically, this determines if adding the current declaration to the set
1943/// of redeclarations of the given functions causes
1944/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001945bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1946 assert(!doesThisDeclarationHaveABody() &&
1947 "Must have a declaration without a body.");
1948
1949 ASTContext &Context = getASTContext();
1950
David Blaikie4e4d0842012-03-11 07:00:24 +00001951 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001952 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1953 // an externally visible definition.
1954 //
1955 // FIXME: What happens if gnu_inline gets added on after the first
1956 // declaration?
1957 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1958 return false;
1959
1960 const FunctionDecl *Prev = this;
1961 bool FoundBody = false;
1962 while ((Prev = Prev->getPreviousDecl())) {
1963 FoundBody |= Prev->Body;
1964
1965 if (Prev->Body) {
1966 // If it's not the case that both 'inline' and 'extern' are
1967 // specified on the definition, then it is always externally visible.
1968 if (!Prev->isInlineSpecified() ||
1969 Prev->getStorageClassAsWritten() != SC_Extern)
1970 return false;
1971 } else if (Prev->isInlineSpecified() &&
1972 Prev->getStorageClassAsWritten() != SC_Extern) {
1973 return false;
1974 }
1975 }
1976 return FoundBody;
1977 }
1978
David Blaikie4e4d0842012-03-11 07:00:24 +00001979 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001980 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001981
1982 // C99 6.7.4p6:
1983 // [...] If all of the file scope declarations for a function in a
1984 // translation unit include the inline function specifier without extern,
1985 // then the definition in that translation unit is an inline definition.
1986 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001987 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001988 const FunctionDecl *Prev = this;
1989 bool FoundBody = false;
1990 while ((Prev = Prev->getPreviousDecl())) {
1991 FoundBody |= Prev->Body;
1992 if (RedeclForcesDefC99(Prev))
1993 return false;
1994 }
1995 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00001996}
1997
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001998/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001999/// definition will be externally visible.
2000///
2001/// Inline function definitions are always available for inlining optimizations.
2002/// However, depending on the language dialect, declaration specifiers, and
2003/// attributes, the definition of an inline function may or may not be
2004/// "externally" visible to other translation units in the program.
2005///
2006/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002007/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002008/// inline definition becomes externally visible (C99 6.7.4p6).
2009///
2010/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2011/// definition, we use the GNU semantics for inline, which are nearly the
2012/// opposite of C99 semantics. In particular, "inline" by itself will create
2013/// an externally visible symbol, but "extern inline" will not create an
2014/// externally visible symbol.
2015bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002016 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002017 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002018 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002019
David Blaikie4e4d0842012-03-11 07:00:24 +00002020 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002021 // Note: If you change the logic here, please change
2022 // doesDeclarationForceExternallyVisibleDefinition as well.
2023 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002024 // If it's not the case that both 'inline' and 'extern' are
2025 // specified on the definition, then this inline definition is
2026 // externally visible.
2027 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2028 return true;
2029
2030 // If any declaration is 'inline' but not 'extern', then this definition
2031 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002032 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2033 Redecl != RedeclEnd;
2034 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002035 if (Redecl->isInlineSpecified() &&
2036 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002037 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002038 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002039
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002040 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002041 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002042
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002043 // C99 6.7.4p6:
2044 // [...] If all of the file scope declarations for a function in a
2045 // translation unit include the inline function specifier without extern,
2046 // then the definition in that translation unit is an inline definition.
2047 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2048 Redecl != RedeclEnd;
2049 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002050 if (RedeclForcesDefC99(*Redecl))
2051 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002052 }
2053
2054 // C99 6.7.4p6:
2055 // An inline definition does not provide an external definition for the
2056 // function, and does not forbid an external definition in another
2057 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002058 return false;
2059}
2060
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002061/// getOverloadedOperator - Which C++ overloaded operator this
2062/// function represents, if any.
2063OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002064 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2065 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002066 else
2067 return OO_None;
2068}
2069
Sean Hunta6c058d2010-01-13 09:01:02 +00002070/// getLiteralIdentifier - The literal suffix identifier this function
2071/// represents, if any.
2072const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2073 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2074 return getDeclName().getCXXLiteralIdentifier();
2075 else
2076 return 0;
2077}
2078
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002079FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2080 if (TemplateOrSpecialization.isNull())
2081 return TK_NonTemplate;
2082 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2083 return TK_FunctionTemplate;
2084 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2085 return TK_MemberSpecialization;
2086 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2087 return TK_FunctionTemplateSpecialization;
2088 if (TemplateOrSpecialization.is
2089 <DependentFunctionTemplateSpecializationInfo*>())
2090 return TK_DependentFunctionTemplateSpecialization;
2091
David Blaikieb219cfc2011-09-23 05:06:16 +00002092 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002093}
2094
Douglas Gregor2db32322009-10-07 23:56:10 +00002095FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002096 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002097 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2098
2099 return 0;
2100}
2101
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002102MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2103 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2104}
2105
Douglas Gregor2db32322009-10-07 23:56:10 +00002106void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002107FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2108 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002109 TemplateSpecializationKind TSK) {
2110 assert(TemplateOrSpecialization.isNull() &&
2111 "Member function is already a specialization");
2112 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002113 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002114 TemplateOrSpecialization = Info;
2115}
2116
Douglas Gregor3b846b62009-10-27 20:53:28 +00002117bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002118 // If the function is invalid, it can't be implicitly instantiated.
2119 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002120 return false;
2121
2122 switch (getTemplateSpecializationKind()) {
2123 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002124 case TSK_ExplicitInstantiationDefinition:
2125 return false;
2126
2127 case TSK_ImplicitInstantiation:
2128 return true;
2129
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002130 // It is possible to instantiate TSK_ExplicitSpecialization kind
2131 // if the FunctionDecl has a class scope specialization pattern.
2132 case TSK_ExplicitSpecialization:
2133 return getClassScopeSpecializationPattern() != 0;
2134
Douglas Gregor3b846b62009-10-27 20:53:28 +00002135 case TSK_ExplicitInstantiationDeclaration:
2136 // Handled below.
2137 break;
2138 }
2139
2140 // Find the actual template from which we will instantiate.
2141 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002142 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002143 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002144 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002145
2146 // C++0x [temp.explicit]p9:
2147 // Except for inline functions, other explicit instantiation declarations
2148 // have the effect of suppressing the implicit instantiation of the entity
2149 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002150 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002151 return true;
2152
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002153 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002154}
2155
2156bool FunctionDecl::isTemplateInstantiation() const {
2157 switch (getTemplateSpecializationKind()) {
2158 case TSK_Undeclared:
2159 case TSK_ExplicitSpecialization:
2160 return false;
2161 case TSK_ImplicitInstantiation:
2162 case TSK_ExplicitInstantiationDeclaration:
2163 case TSK_ExplicitInstantiationDefinition:
2164 return true;
2165 }
2166 llvm_unreachable("All TSK values handled.");
2167}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002168
2169FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002170 // Handle class scope explicit specialization special case.
2171 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2172 return getClassScopeSpecializationPattern();
2173
Douglas Gregor3b846b62009-10-27 20:53:28 +00002174 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2175 while (Primary->getInstantiatedFromMemberTemplate()) {
2176 // If we have hit a point where the user provided a specialization of
2177 // this template, we're done looking.
2178 if (Primary->isMemberSpecialization())
2179 break;
2180
2181 Primary = Primary->getInstantiatedFromMemberTemplate();
2182 }
2183
2184 return Primary->getTemplatedDecl();
2185 }
2186
2187 return getInstantiatedFromMemberFunction();
2188}
2189
Douglas Gregor16e8be22009-06-29 17:30:29 +00002190FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002191 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002192 = TemplateOrSpecialization
2193 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002194 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002195 }
2196 return 0;
2197}
2198
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002199FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2200 return getASTContext().getClassScopeSpecializationPattern(this);
2201}
2202
Douglas Gregor16e8be22009-06-29 17:30:29 +00002203const TemplateArgumentList *
2204FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002205 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002206 = TemplateOrSpecialization
2207 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002208 return Info->TemplateArguments;
2209 }
2210 return 0;
2211}
2212
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002213const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002214FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2215 if (FunctionTemplateSpecializationInfo *Info
2216 = TemplateOrSpecialization
2217 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2218 return Info->TemplateArgumentsAsWritten;
2219 }
2220 return 0;
2221}
2222
Mike Stump1eb44332009-09-09 15:08:12 +00002223void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002224FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2225 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002226 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002227 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002228 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002229 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2230 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002231 assert(TSK != TSK_Undeclared &&
2232 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002233 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002234 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002235 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002236 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2237 TemplateArgs,
2238 TemplateArgsAsWritten,
2239 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002240 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002241 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002242}
2243
John McCallaf2094e2010-04-08 09:05:18 +00002244void
2245FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2246 const UnresolvedSetImpl &Templates,
2247 const TemplateArgumentListInfo &TemplateArgs) {
2248 assert(TemplateOrSpecialization.isNull());
2249 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2250 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002251 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002252 void *Buffer = Context.Allocate(Size);
2253 DependentFunctionTemplateSpecializationInfo *Info =
2254 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2255 TemplateArgs);
2256 TemplateOrSpecialization = Info;
2257}
2258
2259DependentFunctionTemplateSpecializationInfo::
2260DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2261 const TemplateArgumentListInfo &TArgs)
2262 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2263
2264 d.NumTemplates = Ts.size();
2265 d.NumArgs = TArgs.size();
2266
2267 FunctionTemplateDecl **TsArray =
2268 const_cast<FunctionTemplateDecl**>(getTemplates());
2269 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2270 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2271
2272 TemplateArgumentLoc *ArgsArray =
2273 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2274 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2275 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2276}
2277
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002278TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002279 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002280 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002281 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002282 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002283 if (FTSInfo)
2284 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Douglas Gregor2db32322009-10-07 23:56:10 +00002286 MemberSpecializationInfo *MSInfo
2287 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2288 if (MSInfo)
2289 return MSInfo->getTemplateSpecializationKind();
2290
2291 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002292}
2293
Mike Stump1eb44332009-09-09 15:08:12 +00002294void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002295FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2296 SourceLocation PointOfInstantiation) {
2297 if (FunctionTemplateSpecializationInfo *FTSInfo
2298 = TemplateOrSpecialization.dyn_cast<
2299 FunctionTemplateSpecializationInfo*>()) {
2300 FTSInfo->setTemplateSpecializationKind(TSK);
2301 if (TSK != TSK_ExplicitSpecialization &&
2302 PointOfInstantiation.isValid() &&
2303 FTSInfo->getPointOfInstantiation().isInvalid())
2304 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2305 } else if (MemberSpecializationInfo *MSInfo
2306 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2307 MSInfo->setTemplateSpecializationKind(TSK);
2308 if (TSK != TSK_ExplicitSpecialization &&
2309 PointOfInstantiation.isValid() &&
2310 MSInfo->getPointOfInstantiation().isInvalid())
2311 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2312 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002313 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002314}
2315
2316SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002317 if (FunctionTemplateSpecializationInfo *FTSInfo
2318 = TemplateOrSpecialization.dyn_cast<
2319 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002320 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002321 else if (MemberSpecializationInfo *MSInfo
2322 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002323 return MSInfo->getPointOfInstantiation();
2324
2325 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002326}
2327
Douglas Gregor9f185072009-09-11 20:15:17 +00002328bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002329 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002330 return true;
2331
2332 // If this function was instantiated from a member function of a
2333 // class template, check whether that member function was defined out-of-line.
2334 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2335 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002336 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002337 return Definition->isOutOfLine();
2338 }
2339
2340 // If this function was instantiated from a function template,
2341 // check whether that function template was defined out-of-line.
2342 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2343 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002344 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002345 return Definition->isOutOfLine();
2346 }
2347
2348 return false;
2349}
2350
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002351SourceRange FunctionDecl::getSourceRange() const {
2352 return SourceRange(getOuterLocStart(), EndRangeLoc);
2353}
2354
Anna Zaks9392d4e2012-01-18 02:45:01 +00002355unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002356 IdentifierInfo *FnInfo = getIdentifier();
2357
2358 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002359 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002360
2361 // Builtin handling.
2362 switch (getBuiltinID()) {
2363 case Builtin::BI__builtin_memset:
2364 case Builtin::BI__builtin___memset_chk:
2365 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002366 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002367
2368 case Builtin::BI__builtin_memcpy:
2369 case Builtin::BI__builtin___memcpy_chk:
2370 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002371 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002372
2373 case Builtin::BI__builtin_memmove:
2374 case Builtin::BI__builtin___memmove_chk:
2375 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002376 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002377
2378 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002379 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002380 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002381 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002382
2383 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002384 case Builtin::BImemcmp:
2385 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002386
2387 case Builtin::BI__builtin_strncpy:
2388 case Builtin::BI__builtin___strncpy_chk:
2389 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002390 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002391
2392 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002393 case Builtin::BIstrncmp:
2394 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002395
2396 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002397 case Builtin::BIstrncasecmp:
2398 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002399
2400 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002401 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002402 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002403 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002404
2405 case Builtin::BI__builtin_strndup:
2406 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002407 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002408
Anna Zaksc36bedc2012-02-01 19:08:57 +00002409 case Builtin::BI__builtin_strlen:
2410 case Builtin::BIstrlen:
2411 return Builtin::BIstrlen;
2412
Anna Zaksd9b859a2012-01-13 21:52:01 +00002413 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002414 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002415 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002416 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002417 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002418 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002419 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002420 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002421 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002422 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002423 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002424 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002425 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002426 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002427 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002428 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002429 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002430 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002431 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002432 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002433 else if (FnInfo->isStr("strlen"))
2434 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002435 }
2436 break;
2437 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002438 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002439}
2440
Chris Lattner8a934232008-03-31 00:36:02 +00002441//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002442// FieldDecl Implementation
2443//===----------------------------------------------------------------------===//
2444
Jay Foad4ba2a172011-01-12 09:06:06 +00002445FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002446 SourceLocation StartLoc, SourceLocation IdLoc,
2447 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002448 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2449 bool HasInit) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002450 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith7a614d82011-06-11 17:19:42 +00002451 BW, Mutable, HasInit);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002452}
2453
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002454FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2455 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2456 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2457 0, QualType(), 0, 0, false, false);
2458}
2459
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002460bool FieldDecl::isAnonymousStructOrUnion() const {
2461 if (!isImplicit() || getDeclName())
2462 return false;
2463
2464 if (const RecordType *Record = getType()->getAs<RecordType>())
2465 return Record->getDecl()->isAnonymousStructOrUnion();
2466
2467 return false;
2468}
2469
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002470unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2471 assert(isBitField() && "not a bitfield");
2472 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2473 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2474}
2475
John McCallba4f5d52011-01-20 07:57:12 +00002476unsigned FieldDecl::getFieldIndex() const {
2477 if (CachedFieldIndex) return CachedFieldIndex - 1;
2478
Richard Smith180f4792011-11-10 06:34:14 +00002479 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002480 const RecordDecl *RD = getParent();
2481 const FieldDecl *LastFD = 0;
2482 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smith180f4792011-11-10 06:34:14 +00002483
2484 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2485 I != E; ++I, ++Index) {
2486 (*I)->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002487
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002488 if (IsMsStruct) {
2489 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smith180f4792011-11-10 06:34:14 +00002490 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2491 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002492 continue;
2493 }
Richard Smith180f4792011-11-10 06:34:14 +00002494 LastFD = (*I);
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002495 }
John McCallba4f5d52011-01-20 07:57:12 +00002496 }
2497
Richard Smith180f4792011-11-10 06:34:14 +00002498 assert(CachedFieldIndex && "failed to find field in parent");
2499 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002500}
2501
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002502SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002503 if (const Expr *E = InitializerOrBitWidth.getPointer())
2504 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002505 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002506}
2507
Richard Smith7a614d82011-06-11 17:19:42 +00002508void FieldDecl::setInClassInitializer(Expr *Init) {
2509 assert(!InitializerOrBitWidth.getPointer() &&
2510 "bit width or initializer already set");
2511 InitializerOrBitWidth.setPointer(Init);
2512 InitializerOrBitWidth.setInt(0);
2513}
2514
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002515//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002516// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002517//===----------------------------------------------------------------------===//
2518
Douglas Gregor1693e152010-07-06 18:42:40 +00002519SourceLocation TagDecl::getOuterLocStart() const {
2520 return getTemplateOrInnerLocStart(this);
2521}
2522
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002523SourceRange TagDecl::getSourceRange() const {
2524 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002525 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002526}
2527
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002528TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002529 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002530}
2531
Richard Smith162e1c12011-04-15 14:24:37 +00002532void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2533 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002534 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002535 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002536 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002537}
2538
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002539void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002540 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002541
2542 if (isa<CXXRecordDecl>(this)) {
2543 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2544 struct CXXRecordDecl::DefinitionData *Data =
2545 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002546 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2547 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002548 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002549}
2550
2551void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002552 assert((!isa<CXXRecordDecl>(this) ||
2553 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2554 "definition completed but not started");
2555
John McCall5e1cdac2011-10-07 06:10:15 +00002556 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002557 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002558
2559 if (ASTMutationListener *L = getASTMutationListener())
2560 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002561}
2562
John McCall5e1cdac2011-10-07 06:10:15 +00002563TagDecl *TagDecl::getDefinition() const {
2564 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002565 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002566 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2567 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002568
2569 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002570 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002571 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002572 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002573
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002574 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002575}
2576
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002577void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2578 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002579 // Make sure the extended qualifier info is allocated.
2580 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002581 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002582 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002583 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002584 } else {
John McCallb6217662010-03-15 10:12:16 +00002585 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002586 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002587 if (getExtInfo()->NumTemplParamLists == 0) {
2588 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002589 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002590 }
2591 else
2592 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002593 }
2594 }
2595}
2596
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002597void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2598 unsigned NumTPLists,
2599 TemplateParameterList **TPLists) {
2600 assert(NumTPLists > 0);
2601 // Make sure the extended decl info is allocated.
2602 if (!hasExtInfo())
2603 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002604 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002605 // Set the template parameter lists info.
2606 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2607}
2608
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002609//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002610// EnumDecl Implementation
2611//===----------------------------------------------------------------------===//
2612
David Blaikie99ba9e32011-12-20 02:48:34 +00002613void EnumDecl::anchor() { }
2614
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002615EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2616 SourceLocation StartLoc, SourceLocation IdLoc,
2617 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002618 EnumDecl *PrevDecl, bool IsScoped,
2619 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002620 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002621 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002622 C.getTypeDeclType(Enum, PrevDecl);
2623 return Enum;
2624}
2625
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002626EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2627 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2628 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2629 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002630}
2631
Douglas Gregor838db382010-02-11 01:19:42 +00002632void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002633 QualType NewPromotionType,
2634 unsigned NumPositiveBits,
2635 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002636 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002637 if (!IntegerType)
2638 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002639 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002640 setNumPositiveBits(NumPositiveBits);
2641 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002642 TagDecl::completeDefinition();
2643}
2644
Richard Smith1af83c42012-03-23 03:33:32 +00002645TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2646 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2647 return MSI->getTemplateSpecializationKind();
2648
2649 return TSK_Undeclared;
2650}
2651
2652void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2653 SourceLocation PointOfInstantiation) {
2654 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2655 assert(MSI && "Not an instantiated member enumeration?");
2656 MSI->setTemplateSpecializationKind(TSK);
2657 if (TSK != TSK_ExplicitSpecialization &&
2658 PointOfInstantiation.isValid() &&
2659 MSI->getPointOfInstantiation().isInvalid())
2660 MSI->setPointOfInstantiation(PointOfInstantiation);
2661}
2662
Richard Smithf1c66b42012-03-14 23:13:10 +00002663EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2664 if (SpecializationInfo)
2665 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2666
2667 return 0;
2668}
2669
2670void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2671 TemplateSpecializationKind TSK) {
2672 assert(!SpecializationInfo && "Member enum is already a specialization");
2673 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2674}
2675
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002676//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002677// RecordDecl Implementation
2678//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002679
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002680RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2681 SourceLocation StartLoc, SourceLocation IdLoc,
2682 IdentifierInfo *Id, RecordDecl *PrevDecl)
2683 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002684 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002685 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002686 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002687 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002688 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002689}
2690
Jay Foad4ba2a172011-01-12 09:06:06 +00002691RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002692 SourceLocation StartLoc, SourceLocation IdLoc,
2693 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2694 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2695 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002696 C.getTypeDeclType(R, PrevDecl);
2697 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002698}
2699
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002700RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2701 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2702 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2703 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002704}
2705
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002706bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002707 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002708 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2709}
2710
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002711RecordDecl::field_iterator RecordDecl::field_begin() const {
2712 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2713 LoadFieldsFromExternalStorage();
2714
2715 return field_iterator(decl_iterator(FirstDecl));
2716}
2717
Douglas Gregorda2142f2011-02-19 18:51:44 +00002718/// completeDefinition - Notes that the definition of this type is now
2719/// complete.
2720void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002721 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002722 TagDecl::completeDefinition();
2723}
2724
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002725void RecordDecl::LoadFieldsFromExternalStorage() const {
2726 ExternalASTSource *Source = getASTContext().getExternalSource();
2727 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2728
2729 // Notify that we have a RecordDecl doing some initialization.
2730 ExternalASTSource::Deserializing TheFields(Source);
2731
Chris Lattner5f9e2722011-07-23 10:55:15 +00002732 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002733 LoadedFieldsFromExternalStorage = true;
2734 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2735 case ELR_Success:
2736 break;
2737
2738 case ELR_AlreadyLoaded:
2739 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002740 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002741 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002742
2743#ifndef NDEBUG
2744 // Check that all decls we got were FieldDecls.
2745 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2746 assert(isa<FieldDecl>(Decls[i]));
2747#endif
2748
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002749 if (Decls.empty())
2750 return;
2751
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002752 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2753 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002754}
2755
Steve Naroff56ee6892008-10-08 17:01:13 +00002756//===----------------------------------------------------------------------===//
2757// BlockDecl Implementation
2758//===----------------------------------------------------------------------===//
2759
David Blaikie4278c652011-09-21 18:16:56 +00002760void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002761 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002762
Steve Naroffe78b8092009-03-13 16:56:44 +00002763 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002764 if (!NewParamInfo.empty()) {
2765 NumParams = NewParamInfo.size();
2766 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2767 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002768 }
2769}
2770
John McCall6b5a61b2011-02-07 10:33:21 +00002771void BlockDecl::setCaptures(ASTContext &Context,
2772 const Capture *begin,
2773 const Capture *end,
2774 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002775 CapturesCXXThis = capturesCXXThis;
2776
2777 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002778 NumCaptures = 0;
2779 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002780 return;
2781 }
2782
John McCall6b5a61b2011-02-07 10:33:21 +00002783 NumCaptures = end - begin;
2784
2785 // Avoid new Capture[] because we don't want to provide a default
2786 // constructor.
2787 size_t allocationSize = NumCaptures * sizeof(Capture);
2788 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2789 memcpy(buffer, begin, allocationSize);
2790 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002791}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002792
John McCall204e1332011-06-15 22:51:16 +00002793bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2794 for (capture_const_iterator
2795 i = capture_begin(), e = capture_end(); i != e; ++i)
2796 // Only auto vars can be captured, so no redeclaration worries.
2797 if (i->getVariable() == variable)
2798 return true;
2799
2800 return false;
2801}
2802
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002803SourceRange BlockDecl::getSourceRange() const {
2804 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2805}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002806
2807//===----------------------------------------------------------------------===//
2808// Other Decl Allocation/Deallocation Method Implementations
2809//===----------------------------------------------------------------------===//
2810
David Blaikie99ba9e32011-12-20 02:48:34 +00002811void TranslationUnitDecl::anchor() { }
2812
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002813TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2814 return new (C) TranslationUnitDecl(C);
2815}
2816
David Blaikie99ba9e32011-12-20 02:48:34 +00002817void LabelDecl::anchor() { }
2818
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002819LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002820 SourceLocation IdentL, IdentifierInfo *II) {
2821 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2822}
2823
2824LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2825 SourceLocation IdentL, IdentifierInfo *II,
2826 SourceLocation GnuLabelL) {
2827 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2828 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002829}
2830
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002831LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2832 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2833 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002834}
2835
David Blaikie99ba9e32011-12-20 02:48:34 +00002836void ValueDecl::anchor() { }
2837
2838void ImplicitParamDecl::anchor() { }
2839
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002840ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002841 SourceLocation IdLoc,
2842 IdentifierInfo *Id,
2843 QualType Type) {
2844 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002845}
2846
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002847ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2848 unsigned ID) {
2849 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2850 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2851}
2852
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002853FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002854 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002855 const DeclarationNameInfo &NameInfo,
2856 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002857 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002858 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002859 bool hasWrittenPrototype,
2860 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002861 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2862 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002863 isInlineSpecified,
2864 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002865 New->HasWrittenPrototype = hasWrittenPrototype;
2866 return New;
2867}
2868
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002869FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2870 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2871 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2872 DeclarationNameInfo(), QualType(), 0,
2873 SC_None, SC_None, false, false);
2874}
2875
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002876BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2877 return new (C) BlockDecl(DC, L);
2878}
2879
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002880BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2881 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2882 return new (Mem) BlockDecl(0, SourceLocation());
2883}
2884
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002885EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2886 SourceLocation L,
2887 IdentifierInfo *Id, QualType T,
2888 Expr *E, const llvm::APSInt &V) {
2889 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2890}
2891
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002892EnumConstantDecl *
2893EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2894 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2895 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2896 llvm::APSInt());
2897}
2898
David Blaikie99ba9e32011-12-20 02:48:34 +00002899void IndirectFieldDecl::anchor() { }
2900
Benjamin Kramerd9811462010-11-21 14:11:41 +00002901IndirectFieldDecl *
2902IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2903 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2904 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002905 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2906}
2907
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002908IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2909 unsigned ID) {
2910 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2911 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2912 QualType(), 0, 0);
2913}
2914
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002915SourceRange EnumConstantDecl::getSourceRange() const {
2916 SourceLocation End = getLocation();
2917 if (Init)
2918 End = Init->getLocEnd();
2919 return SourceRange(getLocation(), End);
2920}
2921
David Blaikie99ba9e32011-12-20 02:48:34 +00002922void TypeDecl::anchor() { }
2923
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002924TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002925 SourceLocation StartLoc, SourceLocation IdLoc,
2926 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2927 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002928}
2929
David Blaikie99ba9e32011-12-20 02:48:34 +00002930void TypedefNameDecl::anchor() { }
2931
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002932TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2933 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2934 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2935}
2936
Richard Smith162e1c12011-04-15 14:24:37 +00002937TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2938 SourceLocation StartLoc,
2939 SourceLocation IdLoc, IdentifierInfo *Id,
2940 TypeSourceInfo *TInfo) {
2941 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2942}
2943
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002944TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2945 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2946 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2947}
2948
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002949SourceRange TypedefDecl::getSourceRange() const {
2950 SourceLocation RangeEnd = getLocation();
2951 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2952 if (typeIsPostfix(TInfo->getType()))
2953 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2954 }
2955 return SourceRange(getLocStart(), RangeEnd);
2956}
2957
Richard Smith162e1c12011-04-15 14:24:37 +00002958SourceRange TypeAliasDecl::getSourceRange() const {
2959 SourceLocation RangeEnd = getLocStart();
2960 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2961 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2962 return SourceRange(getLocStart(), RangeEnd);
2963}
2964
David Blaikie99ba9e32011-12-20 02:48:34 +00002965void FileScopeAsmDecl::anchor() { }
2966
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002967FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002968 StringLiteral *Str,
2969 SourceLocation AsmLoc,
2970 SourceLocation RParenLoc) {
2971 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002972}
Douglas Gregor15de72c2011-12-02 23:23:56 +00002973
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002974FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2975 unsigned ID) {
2976 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2977 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2978}
2979
Douglas Gregor15de72c2011-12-02 23:23:56 +00002980//===----------------------------------------------------------------------===//
2981// ImportDecl Implementation
2982//===----------------------------------------------------------------------===//
2983
2984/// \brief Retrieve the number of module identifiers needed to name the given
2985/// module.
2986static unsigned getNumModuleIdentifiers(Module *Mod) {
2987 unsigned Result = 1;
2988 while (Mod->Parent) {
2989 Mod = Mod->Parent;
2990 ++Result;
2991 }
2992 return Result;
2993}
2994
Douglas Gregor5948ae12012-01-03 18:04:46 +00002995ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00002996 Module *Imported,
2997 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00002998 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00002999 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003000{
3001 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3002 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3003 memcpy(StoredLocs, IdentifierLocs.data(),
3004 IdentifierLocs.size() * sizeof(SourceLocation));
3005}
3006
Douglas Gregor5948ae12012-01-03 18:04:46 +00003007ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003008 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003009 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003010 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003011{
3012 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3013}
3014
3015ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003016 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003017 ArrayRef<SourceLocation> IdentifierLocs) {
3018 void *Mem = C.Allocate(sizeof(ImportDecl) +
3019 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003020 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003021}
3022
3023ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003024 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003025 Module *Imported,
3026 SourceLocation EndLoc) {
3027 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003028 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003029 Import->setImplicit();
3030 return Import;
3031}
3032
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003033ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3034 unsigned NumLocations) {
3035 void *Mem = AllocateDeserializedDecl(C, ID,
3036 (sizeof(ImportDecl) +
3037 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003038 return new (Mem) ImportDecl(EmptyShell());
3039}
3040
3041ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3042 if (!ImportedAndComplete.getInt())
3043 return ArrayRef<SourceLocation>();
3044
3045 const SourceLocation *StoredLocs
3046 = reinterpret_cast<const SourceLocation *>(this + 1);
3047 return ArrayRef<SourceLocation>(StoredLocs,
3048 getNumModuleIdentifiers(getImportedModule()));
3049}
3050
3051SourceRange ImportDecl::getSourceRange() const {
3052 if (!ImportedAndComplete.getInt())
3053 return SourceRange(getLocation(),
3054 *reinterpret_cast<const SourceLocation *>(this + 1));
3055
3056 return SourceRange(getLocation(), getIdentifierLocs().back());
3057}