blob: a3196b22f07c60e3239d4d0d4f1381c49f9d9e5c [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()) {
278 LV.setVisibility(*Vis, true);
279 } 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()) {
288 LV.setVisibility(*Vis, true);
289 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 Espindola2f47c362012-03-10 13:01:40 +0000328 LV.mergeVisibilityWithMin(TypeLV.visibility(),
329 TypeLV.visibilityExplicit());
John McCall110e8e52010-10-29 22:22:43 +0000330 }
331
John McCall35cebc32010-11-02 18:38:13 +0000332 if (Var->getStorageClass() == SC_PrivateExtern)
333 LV.setVisibility(HiddenVisibility, true);
334
David Blaikie4e4d0842012-03-11 07:00:24 +0000335 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000336 (Var->getStorageClass() == SC_Extern ||
337 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000338
Douglas Gregord85b5b92009-11-25 22:24:25 +0000339 // C99 6.2.2p4:
340 // For an identifier declared with the storage-class specifier
341 // extern in a scope in which a prior declaration of that
342 // identifier is visible, if the prior declaration specifies
343 // internal or external linkage, the linkage of the identifier
344 // at the later declaration is the same as the linkage
345 // specified at the prior declaration. If no prior declaration
346 // is visible, or if the prior declaration specifies no
347 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000348 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000349 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000350 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
351 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000352 }
353 }
354
Douglas Gregord85b5b92009-11-25 22:24:25 +0000355 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000356 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000357 // In theory, we can modify the function's LV by the LV of its
358 // type unless it has C linkage (see comment above about variables
359 // for justification). In practice, GCC doesn't do this, so it's
360 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000361
John McCall35cebc32010-11-02 18:38:13 +0000362 if (Function->getStorageClass() == SC_PrivateExtern)
363 LV.setVisibility(HiddenVisibility, true);
364
Douglas Gregord85b5b92009-11-25 22:24:25 +0000365 // C99 6.2.2p5:
366 // If the declaration of an identifier for a function has no
367 // storage-class specifier, its linkage is determined exactly
368 // as if it were declared with the storage-class specifier
369 // extern.
David Blaikie4e4d0842012-03-11 07:00:24 +0000370 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000371 (Function->getStorageClass() == SC_Extern ||
372 Function->getStorageClass() == SC_PrivateExtern ||
373 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000374 // C99 6.2.2p4:
375 // For an identifier declared with the storage-class specifier
376 // extern in a scope in which a prior declaration of that
377 // identifier is visible, if the prior declaration specifies
378 // internal or external linkage, the linkage of the identifier
379 // at the later declaration is the same as the linkage
380 // specified at the prior declaration. If no prior declaration
381 // is visible, or if the prior declaration specifies no
382 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000383 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000384 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000385 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
386 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000387 }
388 }
389
John McCallaf8ca372011-02-10 06:50:24 +0000390 // In C++, then if the type of the function uses a type with
391 // unique-external linkage, it's not legally usable from outside
392 // this translation unit. However, we should use the C linkage
393 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000394 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000395 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000396 Function->getType()->getLinkage() == UniqueExternalLinkage)
397 return LinkageInfo::uniqueExternal();
398
John McCall6ce51ee2011-06-27 23:06:04 +0000399 // Consider LV from the template and the template arguments unless
400 // this is an explicit specialization with a visibility attribute.
401 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000402 = Function->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000403 if (shouldConsiderTemplateLV(Function, specInfo)) {
404 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola62d9f112012-04-16 13:44:41 +0000405 LVFlags::CreateOnlyDeclLinkage()));
John McCall6ce51ee2011-06-27 23:06:04 +0000406 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola860097c2012-02-23 04:17:32 +0000407 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000408 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000409 }
410
Douglas Gregord85b5b92009-11-25 22:24:25 +0000411 // - a named class (Clause 9), or an unnamed class defined in a
412 // typedef declaration in which the class has the typedef name
413 // for linkage purposes (7.1.3); or
414 // - a named enumeration (7.2), or an unnamed enumeration
415 // defined in a typedef declaration in which the enumeration
416 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000417 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
418 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000419 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000420 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000421
John McCall1fb0caa2010-10-22 21:05:15 +0000422 // If this is a class template specialization, consider the
423 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000424 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000425 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000426 if (shouldConsiderTemplateLV(spec)) {
427 // From the template.
428 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola62d9f112012-04-16 13:44:41 +0000429 LVFlags::CreateOnlyDeclLinkage()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000430
John McCall6ce51ee2011-06-27 23:06:04 +0000431 // The arguments at which the template was instantiated.
432 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola860097c2012-02-23 04:17:32 +0000433 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000434 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000435 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000436
437 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000438 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000439 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000440 if (!isExternalLinkage(EnumLV.linkage()))
441 return LinkageInfo::none();
442 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000443
444 // - a template, unless it is a function template that has
445 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000446 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
447 if (F.ConsiderTemplateParameterTypes)
448 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000449
Douglas Gregord85b5b92009-11-25 22:24:25 +0000450 // - a namespace (7.3), unless it is declared within an unnamed
451 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000452 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
453 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000454
John McCall1fb0caa2010-10-22 21:05:15 +0000455 // By extension, we assign external linkage to Objective-C
456 // interfaces.
457 } else if (isa<ObjCInterfaceDecl>(D)) {
458 // fallout
459
460 // Everything not covered here has no linkage.
461 } else {
John McCallaf146032010-10-30 11:50:40 +0000462 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000463 }
464
465 // If we ended up with non-external linkage, visibility should
466 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000467 if (LV.linkage() != ExternalLinkage)
468 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000469
John McCall1fb0caa2010-10-22 21:05:15 +0000470 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000471}
472
John McCall36987482010-11-02 01:45:15 +0000473static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000474 // Only certain class members have linkage. Note that fields don't
475 // really have linkage, but it's convenient to say they do for the
476 // purposes of calculating linkage of pointer-to-data-member
477 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000478 if (!(isa<CXXMethodDecl>(D) ||
479 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000480 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000481 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000482 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000483 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000484
John McCall36987482010-11-02 01:45:15 +0000485 LinkageInfo LV;
David Blaikie4e4d0842012-03-11 07:00:24 +0000486 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
John McCall36987482010-11-02 01:45:15 +0000487
Rafael Espindola0f905902012-04-16 18:25:01 +0000488 bool DHasExplicitVisibility = false;
John McCall36987482010-11-02 01:45:15 +0000489 // If we have an explicit visibility attribute, merge that in.
490 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000491 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
492 LV.mergeVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000493
Rafael Espindola0f905902012-04-16 18:25:01 +0000494 DHasExplicitVisibility = true;
John McCall36987482010-11-02 01:45:15 +0000495 }
496 }
Rafael Espindola0f905902012-04-16 18:25:01 +0000497 // Ignore both global visibility and attributes when computing our
498 // parent's visibility if we already have an explicit one.
499 LVFlags ClassF = DHasExplicitVisibility ?
500 LVFlags::CreateOnlyDeclLinkage() : F;
501
502 // If we're paying attention to global visibility, apply
503 // -finline-visibility-hidden if this is an inline method.
504 //
505 // Note that we do this before merging information about
506 // the class visibility.
507 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
508 TemplateSpecializationKind TSK = TSK_Undeclared;
509 if (FunctionTemplateSpecializationInfo *spec
510 = MD->getTemplateSpecializationInfo()) {
511 TSK = spec->getTemplateSpecializationKind();
512 } else if (MemberSpecializationInfo *MSI =
513 MD->getMemberSpecializationInfo()) {
514 TSK = MSI->getTemplateSpecializationKind();
515 }
516
517 const FunctionDecl *Def = 0;
518 // InlineVisibilityHidden only applies to definitions, and
519 // isInlined() only gives meaningful answers on definitions
520 // anyway.
521 if (TSK != TSK_ExplicitInstantiationDeclaration &&
522 TSK != TSK_ExplicitInstantiationDefinition &&
523 F.ConsiderGlobalVisibility &&
524 !LV.visibilityExplicit() &&
525 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
526 MD->hasBody(Def) && Def->isInlined())
527 LV.mergeVisibility(HiddenVisibility, true);
528 }
John McCallaf146032010-10-30 11:50:40 +0000529
530 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000531 // linkage.
532 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
533 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000534 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000535
536 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000537 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000538 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000539
John McCall3cdfc4d2010-08-13 08:35:10 +0000540 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000541 // If the type of the function uses a type with unique-external
542 // linkage, it's not legally usable from outside this translation unit.
543 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
544 return LinkageInfo::uniqueExternal();
545
John McCall1fb0caa2010-10-22 21:05:15 +0000546 // If this is a method template specialization, use the linkage for
547 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000548 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000549 = MD->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000550 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola860097c2012-02-23 04:17:32 +0000551 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
552 F));
John McCall6ce51ee2011-06-27 23:06:04 +0000553 if (F.ConsiderTemplateParameterTypes)
554 LV.merge(getLVForTemplateParameterList(
555 spec->getTemplate()->getTemplateParameters()));
556 }
John McCall66cbcf32010-11-01 01:29:57 +0000557 }
John McCall1fb0caa2010-10-22 21:05:15 +0000558
John McCall110e8e52010-10-29 22:22:43 +0000559 // Note that in contrast to basically every other situation, we
560 // *do* apply -fvisibility to method declarations.
561
562 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000563 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000564 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000565 if (shouldConsiderTemplateLV(spec)) {
566 // Merge template argument/parameter information for member
567 // class template specializations.
Rafael Espindola860097c2012-02-23 04:17:32 +0000568 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
569 F));
John McCall1a0918a2011-03-04 10:39:25 +0000570 if (F.ConsiderTemplateParameterTypes)
571 LV.merge(getLVForTemplateParameterList(
John McCall6ce51ee2011-06-27 23:06:04 +0000572 spec->getSpecializedTemplate()->getTemplateParameters()));
573 }
John McCall110e8e52010-10-29 22:22:43 +0000574 }
575
John McCall110e8e52010-10-29 22:22:43 +0000576 // Static data members.
577 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000578 // Modify the variable's linkage by its type, but ignore the
579 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000580 LinkageInfo TypeLV = getLVForType(VD->getType());
581 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000582 LV.mergeLinkage(UniqueExternalLinkage);
583 if (!LV.visibilityExplicit())
Rafael Espindola093ecc92012-01-14 00:30:36 +0000584 LV.mergeVisibility(TypeLV.visibility(), TypeLV.visibilityExplicit());
John McCall110e8e52010-10-29 22:22:43 +0000585 }
586
John McCall1fb0caa2010-10-22 21:05:15 +0000587 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000588}
589
John McCallf76b0922011-02-08 19:01:05 +0000590static void clearLinkageForClass(const CXXRecordDecl *record) {
591 for (CXXRecordDecl::decl_iterator
592 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
593 Decl *child = *i;
594 if (isa<NamedDecl>(child))
595 cast<NamedDecl>(child)->ClearLinkageCache();
596 }
597}
598
David Blaikie99ba9e32011-12-20 02:48:34 +0000599void NamedDecl::anchor() { }
600
John McCallf76b0922011-02-08 19:01:05 +0000601void NamedDecl::ClearLinkageCache() {
602 // Note that we can't skip clearing the linkage of children just
603 // because the parent doesn't have cached linkage: we don't cache
604 // when computing linkage for parent contexts.
605
606 HasCachedLinkage = 0;
607
608 // If we're changing the linkage of a class, we need to reset the
609 // linkage of child declarations, too.
610 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
611 clearLinkageForClass(record);
612
John McCall15e310a2011-02-19 02:53:41 +0000613 if (ClassTemplateDecl *temp =
614 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000615 // Clear linkage for the template pattern.
616 CXXRecordDecl *record = temp->getTemplatedDecl();
617 record->HasCachedLinkage = 0;
618 clearLinkageForClass(record);
619
John McCall15e310a2011-02-19 02:53:41 +0000620 // We need to clear linkage for specializations, too.
621 for (ClassTemplateDecl::spec_iterator
622 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
623 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000624 }
John McCall15e310a2011-02-19 02:53:41 +0000625
626 // Clear cached linkage for function template decls, too.
627 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000628 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
629 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000630 for (FunctionTemplateDecl::spec_iterator
631 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
632 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000633 }
John McCall15e310a2011-02-19 02:53:41 +0000634
John McCallf76b0922011-02-08 19:01:05 +0000635}
636
Douglas Gregor381d34e2010-12-06 18:36:25 +0000637Linkage NamedDecl::getLinkage() const {
638 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000639 assert(Linkage(CachedLinkage) ==
640 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000641 return Linkage(CachedLinkage);
642 }
643
644 CachedLinkage = getLVForDecl(this,
645 LVFlags::CreateOnlyDeclLinkage()).linkage();
646 HasCachedLinkage = 1;
647 return Linkage(CachedLinkage);
648}
649
John McCallaf146032010-10-30 11:50:40 +0000650LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000651 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000652 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000653 HasCachedLinkage = 1;
654 CachedLinkage = LI.linkage();
655 return LI;
John McCall0df95872010-10-29 00:29:13 +0000656}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000657
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000658llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
659 // Use the most recent declaration of a variable.
660 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregoref96ee02012-01-14 16:38:05 +0000661 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000662
663 // Use the most recent declaration of a function, and also handle
664 // function template specializations.
665 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
666 if (llvm::Optional<Visibility> V
Douglas Gregoref96ee02012-01-14 16:38:05 +0000667 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000668 return V;
669
670 // If the function is a specialization of a template with an
671 // explicit visibility attribute, use that.
672 if (FunctionTemplateSpecializationInfo *templateInfo
673 = fn->getTemplateSpecializationInfo())
674 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
675
Rafael Espindola860097c2012-02-23 04:17:32 +0000676 // If the function is a member of a specialization of a class template
677 // and the corresponding decl has explicit visibility, use that.
678 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
679 if (InstantiatedFrom)
680 return getVisibilityOf(InstantiatedFrom);
681
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000682 return llvm::Optional<Visibility>();
683 }
684
685 // Otherwise, just check the declaration itself first.
686 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
687 return V;
688
689 // If there wasn't explicit visibility there, and this is a
690 // specialization of a class template, check for visibility
691 // on the pattern.
692 if (const ClassTemplateSpecializationDecl *spec
693 = dyn_cast<ClassTemplateSpecializationDecl>(this))
694 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
695
Rafael Espindola860097c2012-02-23 04:17:32 +0000696 // If this is a member class of a specialization of a class template
697 // and the corresponding decl has explicit visibility, use that.
698 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
699 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
700 if (InstantiatedFrom)
701 return getVisibilityOf(InstantiatedFrom);
702 }
703
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000704 return llvm::Optional<Visibility>();
705}
706
John McCall36987482010-11-02 01:45:15 +0000707static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000708 // Objective-C: treat all Objective-C declarations as having external
709 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000710 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000711 default:
712 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000713 case Decl::ParmVar:
714 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000715 case Decl::TemplateTemplateParm: // count these as external
716 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000717 case Decl::ObjCAtDefsField:
718 case Decl::ObjCCategory:
719 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000720 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000721 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000722 case Decl::ObjCMethod:
723 case Decl::ObjCProperty:
724 case Decl::ObjCPropertyImpl:
725 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000726 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000727
728 case Decl::CXXRecord: {
729 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
730 if (Record->isLambda()) {
731 if (!Record->getLambdaManglingNumber()) {
732 // This lambda has no mangling number, so it's internal.
733 return LinkageInfo::internal();
734 }
735
736 // This lambda has its linkage/visibility determined by its owner.
737 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
738 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
739 if (isa<ParmVarDecl>(ContextDecl))
740 DC = ContextDecl->getDeclContext()->getRedeclContext();
741 else
742 return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags);
743 }
744
745 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
746 return getLVForDecl(ND, Flags);
747
748 return LinkageInfo::external();
749 }
750
751 break;
752 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000753 }
754
Douglas Gregord85b5b92009-11-25 22:24:25 +0000755 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000756 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000757 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000758
759 // C++ [basic.link]p5:
760 // In addition, a member function, static data member, a named
761 // class or enumeration of class scope, or an unnamed class or
762 // enumeration defined in a class-scope typedef declaration such
763 // that the class or enumeration has the typedef name for linkage
764 // purposes (7.1.3), has external linkage if the name of the class
765 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000766 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000767 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000768
769 // C++ [basic.link]p6:
770 // The name of a function declared in block scope and the name of
771 // an object declared by a block scope extern declaration have
772 // linkage. If there is a visible declaration of an entity with
773 // linkage having the same name and type, ignoring entities
774 // declared outside the innermost enclosing namespace scope, the
775 // block scope declaration declares that same entity and receives
776 // the linkage of the previous declaration. If there is more than
777 // one such matching entity, the program is ill-formed. Otherwise,
778 // if no matching entity is found, the block scope entity receives
779 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000780 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
781 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000782 if (Function->isInAnonymousNamespace() &&
783 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000784 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000785
John McCallaf146032010-10-30 11:50:40 +0000786 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000787 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000788 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
789 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000790 }
791
Douglas Gregoref96ee02012-01-14 16:38:05 +0000792 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000793 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000794 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
795 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000796 }
797
798 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000799 }
800
John McCall0df95872010-10-29 00:29:13 +0000801 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000802 if (Var->getStorageClass() == SC_Extern ||
803 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000804 if (Var->isInAnonymousNamespace() &&
805 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000806 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000807
John McCallaf146032010-10-30 11:50:40 +0000808 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000809 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000810 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000811 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000812 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
813 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000814 }
815
Douglas Gregoref96ee02012-01-14 16:38:05 +0000816 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000817 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000818 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
819 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000820 }
821
822 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000823 }
824 }
825
826 // C++ [basic.link]p6:
827 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000828 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000829}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000830
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000831std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000832 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000833}
834
835std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000836 const DeclContext *Ctx = getDeclContext();
837
838 if (Ctx->isFunctionOrMethod())
839 return getNameAsString();
840
Chris Lattner5f9e2722011-07-23 10:55:15 +0000841 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000842 ContextsTy Contexts;
843
844 // Collect contexts.
845 while (Ctx && isa<NamedDecl>(Ctx)) {
846 Contexts.push_back(Ctx);
847 Ctx = Ctx->getParent();
848 };
849
850 std::string QualName;
851 llvm::raw_string_ostream OS(QualName);
852
853 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
854 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000855 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000856 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000857 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
858 std::string TemplateArgsStr
859 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000860 TemplateArgs.data(),
861 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000862 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000863 OS << Spec->getName() << TemplateArgsStr;
864 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000865 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000866 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000867 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000868 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000869 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
870 if (!RD->getIdentifier())
871 OS << "<anonymous " << RD->getKindName() << '>';
872 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000873 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000874 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000875 const FunctionProtoType *FT = 0;
876 if (FD->hasWrittenPrototype())
877 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
878
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000879 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000880 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000881 unsigned NumParams = FD->getNumParams();
882 for (unsigned i = 0; i < NumParams; ++i) {
883 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000884 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000885 std::string Param;
886 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000887 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000888 }
889
890 if (FT->isVariadic()) {
891 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000892 OS << ", ";
893 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000894 }
895 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000896 OS << ')';
897 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000898 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000899 }
900 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000901 }
902
John McCall8472af42010-03-16 21:48:18 +0000903 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000904 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000905 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000906 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000907
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000908 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000909}
910
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000911bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000912 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
913
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000914 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
915 // We want to keep it, unless it nominates same namespace.
916 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000917 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
918 ->getOriginalNamespace() ==
919 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
920 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000923 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
924 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000925 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000926
Douglas Gregore53060f2009-06-25 22:08:12 +0000927 // For function templates, the underlying function declarations are linked.
928 if (const FunctionTemplateDecl *FunctionTemplate
929 = dyn_cast<FunctionTemplateDecl>(this))
930 if (const FunctionTemplateDecl *OldFunctionTemplate
931 = dyn_cast<FunctionTemplateDecl>(OldD))
932 return FunctionTemplate->getTemplatedDecl()
933 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Steve Naroff0de21fd2009-02-22 19:35:57 +0000935 // For method declarations, we keep track of redeclarations.
936 if (isa<ObjCMethodDecl>(this))
937 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
John McCallf36e02d2009-10-09 21:13:30 +0000939 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
940 return true;
941
John McCall9488ea12009-11-17 05:59:44 +0000942 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
943 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
944 cast<UsingShadowDecl>(OldD)->getTargetDecl();
945
Douglas Gregordc355712011-02-25 00:36:19 +0000946 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
947 ASTContext &Context = getASTContext();
948 return Context.getCanonicalNestedNameSpecifier(
949 cast<UsingDecl>(this)->getQualifier()) ==
950 Context.getCanonicalNestedNameSpecifier(
951 cast<UsingDecl>(OldD)->getQualifier());
952 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000953
Douglas Gregor7a537402012-01-03 23:26:26 +0000954 // A typedef of an Objective-C class type can replace an Objective-C class
955 // declaration or definition, and vice versa.
956 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
957 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
958 return true;
959
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000960 // For non-function declarations, if the declarations are of the
961 // same kind then this must be a redeclaration, or semantic analysis
962 // would not have given us the new declaration.
963 return this->getKind() == OldD->getKind();
964}
965
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000966bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000967 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000968}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000969
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000970NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000971 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000972 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
973 ND = UD->getTargetDecl();
974
975 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
976 return AD->getClassInterface();
977
978 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +0000979}
980
John McCall161755a2010-04-06 21:38:20 +0000981bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000982 if (!isCXXClassMember())
983 return false;
984
John McCall161755a2010-04-06 21:38:20 +0000985 const NamedDecl *D = this;
986 if (isa<UsingShadowDecl>(D))
987 D = cast<UsingShadowDecl>(D)->getTargetDecl();
988
Francois Pichet87c2e122010-11-21 06:08:52 +0000989 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000990 return true;
991 if (isa<CXXMethodDecl>(D))
992 return cast<CXXMethodDecl>(D)->isInstance();
993 if (isa<FunctionTemplateDecl>(D))
994 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
995 ->getTemplatedDecl())->isInstance();
996 return false;
997}
998
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000999//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001000// DeclaratorDecl Implementation
1001//===----------------------------------------------------------------------===//
1002
Douglas Gregor1693e152010-07-06 18:42:40 +00001003template <typename DeclT>
1004static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1005 if (decl->getNumTemplateParameterLists() > 0)
1006 return decl->getTemplateParameterList(0)->getTemplateLoc();
1007 else
1008 return decl->getInnerLocStart();
1009}
1010
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001011SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001012 TypeSourceInfo *TSI = getTypeSourceInfo();
1013 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001014 return SourceLocation();
1015}
1016
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001017void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1018 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001019 // Make sure the extended decl info is allocated.
1020 if (!hasExtInfo()) {
1021 // Save (non-extended) type source info pointer.
1022 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1023 // Allocate external info struct.
1024 DeclInfo = new (getASTContext()) ExtInfo;
1025 // Restore savedTInfo into (extended) decl info.
1026 getExtInfo()->TInfo = savedTInfo;
1027 }
1028 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001029 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001030 } else {
John McCallb6217662010-03-15 10:12:16 +00001031 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001032 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001033 if (getExtInfo()->NumTemplParamLists == 0) {
1034 // Save type source info pointer.
1035 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1036 // Deallocate the extended decl info.
1037 getASTContext().Deallocate(getExtInfo());
1038 // Restore savedTInfo into (non-extended) decl info.
1039 DeclInfo = savedTInfo;
1040 }
1041 else
1042 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001043 }
1044 }
1045}
1046
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001047void
1048DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1049 unsigned NumTPLists,
1050 TemplateParameterList **TPLists) {
1051 assert(NumTPLists > 0);
1052 // Make sure the extended decl info is allocated.
1053 if (!hasExtInfo()) {
1054 // Save (non-extended) type source info pointer.
1055 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1056 // Allocate external info struct.
1057 DeclInfo = new (getASTContext()) ExtInfo;
1058 // Restore savedTInfo into (extended) decl info.
1059 getExtInfo()->TInfo = savedTInfo;
1060 }
1061 // Set the template parameter lists info.
1062 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1063}
1064
Douglas Gregor1693e152010-07-06 18:42:40 +00001065SourceLocation DeclaratorDecl::getOuterLocStart() const {
1066 return getTemplateOrInnerLocStart(this);
1067}
1068
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001069namespace {
1070
1071// Helper function: returns true if QT is or contains a type
1072// having a postfix component.
1073bool typeIsPostfix(clang::QualType QT) {
1074 while (true) {
1075 const Type* T = QT.getTypePtr();
1076 switch (T->getTypeClass()) {
1077 default:
1078 return false;
1079 case Type::Pointer:
1080 QT = cast<PointerType>(T)->getPointeeType();
1081 break;
1082 case Type::BlockPointer:
1083 QT = cast<BlockPointerType>(T)->getPointeeType();
1084 break;
1085 case Type::MemberPointer:
1086 QT = cast<MemberPointerType>(T)->getPointeeType();
1087 break;
1088 case Type::LValueReference:
1089 case Type::RValueReference:
1090 QT = cast<ReferenceType>(T)->getPointeeType();
1091 break;
1092 case Type::PackExpansion:
1093 QT = cast<PackExpansionType>(T)->getPattern();
1094 break;
1095 case Type::Paren:
1096 case Type::ConstantArray:
1097 case Type::DependentSizedArray:
1098 case Type::IncompleteArray:
1099 case Type::VariableArray:
1100 case Type::FunctionProto:
1101 case Type::FunctionNoProto:
1102 return true;
1103 }
1104 }
1105}
1106
1107} // namespace
1108
1109SourceRange DeclaratorDecl::getSourceRange() const {
1110 SourceLocation RangeEnd = getLocation();
1111 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1112 if (typeIsPostfix(TInfo->getType()))
1113 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1114 }
1115 return SourceRange(getOuterLocStart(), RangeEnd);
1116}
1117
Abramo Bagnara9b934882010-06-12 08:15:14 +00001118void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001119QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1120 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001121 TemplateParameterList **TPLists) {
1122 assert((NumTPLists == 0 || TPLists != 0) &&
1123 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001124
1125 // Free previous template parameters (if any).
1126 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001127 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001128 TemplParamLists = 0;
1129 NumTemplParamLists = 0;
1130 }
1131 // Set info on matched template parameter lists (if any).
1132 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001133 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001134 NumTemplParamLists = NumTPLists;
1135 for (unsigned i = NumTPLists; i-- > 0; )
1136 TemplParamLists[i] = TPLists[i];
1137 }
1138}
1139
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001140//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001141// VarDecl Implementation
1142//===----------------------------------------------------------------------===//
1143
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001144const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1145 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001146 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001147 case SC_Auto: return "auto";
1148 case SC_Extern: return "extern";
1149 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1150 case SC_PrivateExtern: return "__private_extern__";
1151 case SC_Register: return "register";
1152 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001153 }
1154
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001155 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001156}
1157
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001158VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1159 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001160 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001161 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001162 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001163}
1164
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001165VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1166 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1167 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1168 QualType(), 0, SC_None, SC_None);
1169}
1170
Douglas Gregor381d34e2010-12-06 18:36:25 +00001171void VarDecl::setStorageClass(StorageClass SC) {
1172 assert(isLegalForVariable(SC));
1173 if (getStorageClass() != SC)
1174 ClearLinkageCache();
1175
John McCallf1e4fbf2011-05-01 02:13:58 +00001176 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001177}
1178
Douglas Gregor1693e152010-07-06 18:42:40 +00001179SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001180 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001181 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001182 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001183}
1184
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001185bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001186 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001187 return false;
1188
Eli Friedman750dc2b2012-01-15 01:23:58 +00001189 const DeclContext *DC = getDeclContext();
1190 if (DC->isRecord())
1191 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001192
Eli Friedman750dc2b2012-01-15 01:23:58 +00001193 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001194 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001195 return true;
1196 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001197}
1198
1199VarDecl *VarDecl::getCanonicalDecl() {
1200 return getFirstDeclaration();
1201}
1202
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001203VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1204 ASTContext &C) const
1205{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001206 // C++ [basic.def]p2:
1207 // A declaration is a definition unless [...] it contains the 'extern'
1208 // specifier or a linkage-specification and neither an initializer [...],
1209 // it declares a static data member in a class declaration [...].
1210 // C++ [temp.expl.spec]p15:
1211 // An explicit specialization of a static data member of a template is a
1212 // definition if the declaration includes an initializer; otherwise, it is
1213 // a declaration.
1214 if (isStaticDataMember()) {
1215 if (isOutOfLine() && (hasInit() ||
1216 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1217 return Definition;
1218 else
1219 return DeclarationOnly;
1220 }
1221 // C99 6.7p5:
1222 // A definition of an identifier is a declaration for that identifier that
1223 // [...] causes storage to be reserved for that object.
1224 // Note: that applies for all non-file-scope objects.
1225 // C99 6.9.2p1:
1226 // If the declaration of an identifier for an object has file scope and an
1227 // initializer, the declaration is an external definition for the identifier
1228 if (hasInit())
1229 return Definition;
1230 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1231 if (hasExternalStorage())
1232 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001233
John McCalld931b082010-08-26 03:08:43 +00001234 if (getStorageClassAsWritten() == SC_Extern ||
1235 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001236 for (const VarDecl *PrevVar = getPreviousDecl();
1237 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001238 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1239 return DeclarationOnly;
1240 }
1241 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001242 // C99 6.9.2p2:
1243 // A declaration of an object that has file scope without an initializer,
1244 // and without a storage class specifier or the scs 'static', constitutes
1245 // a tentative definition.
1246 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001247 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001248 return TentativeDefinition;
1249
1250 // What's left is (in C, block-scope) declarations without initializers or
1251 // external storage. These are definitions.
1252 return Definition;
1253}
1254
Sebastian Redle9d12b62010-01-31 22:27:38 +00001255VarDecl *VarDecl::getActingDefinition() {
1256 DefinitionKind Kind = isThisDeclarationADefinition();
1257 if (Kind != TentativeDefinition)
1258 return 0;
1259
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001260 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001261 VarDecl *First = getFirstDeclaration();
1262 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1263 I != E; ++I) {
1264 Kind = (*I)->isThisDeclarationADefinition();
1265 if (Kind == Definition)
1266 return 0;
1267 else if (Kind == TentativeDefinition)
1268 LastTentative = *I;
1269 }
1270 return LastTentative;
1271}
1272
1273bool VarDecl::isTentativeDefinitionNow() const {
1274 DefinitionKind Kind = isThisDeclarationADefinition();
1275 if (Kind != TentativeDefinition)
1276 return false;
1277
1278 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1279 if ((*I)->isThisDeclarationADefinition() == Definition)
1280 return false;
1281 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001282 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001283}
1284
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001285VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001286 VarDecl *First = getFirstDeclaration();
1287 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1288 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001289 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001290 return *I;
1291 }
1292 return 0;
1293}
1294
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001295VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001296 DefinitionKind Kind = DeclarationOnly;
1297
1298 const VarDecl *First = getFirstDeclaration();
1299 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001300 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001301 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001302 if (Kind == Definition)
1303 break;
1304 }
John McCall110e8e52010-10-29 22:22:43 +00001305
1306 return Kind;
1307}
1308
Sebastian Redl31310a22010-02-01 20:16:42 +00001309const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001310 redecl_iterator I = redecls_begin(), E = redecls_end();
1311 while (I != E && !I->getInit())
1312 ++I;
1313
1314 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001315 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001316 return I->getInit();
1317 }
1318 return 0;
1319}
1320
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001321bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001322 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001323 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001324
1325 if (!isStaticDataMember())
1326 return false;
1327
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001328 // If this static data member was instantiated from a static data member of
1329 // a class template, check whether that static data member was defined
1330 // out-of-line.
1331 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1332 return VD->isOutOfLine();
1333
1334 return false;
1335}
1336
Douglas Gregor0d035142009-10-27 18:42:08 +00001337VarDecl *VarDecl::getOutOfLineDefinition() {
1338 if (!isStaticDataMember())
1339 return 0;
1340
1341 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1342 RD != RDEnd; ++RD) {
1343 if (RD->getLexicalDeclContext()->isFileContext())
1344 return *RD;
1345 }
1346
1347 return 0;
1348}
1349
Douglas Gregor838db382010-02-11 01:19:42 +00001350void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001351 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1352 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001353 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001354 }
1355
1356 Init = I;
1357}
1358
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001359bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001360 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001361
Richard Smith16581332012-03-02 04:14:40 +00001362 if (!Lang.CPlusPlus)
1363 return false;
1364
1365 // In C++11, any variable of reference type can be used in a constant
1366 // expression if it is initialized by a constant expression.
1367 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1368 return true;
1369
1370 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001371 // not require the variable to be non-volatile, but we consider this to be a
1372 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001373 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001374 return false;
1375
1376 // In C++, const, non-volatile variables of integral or enumeration types
1377 // can be used in constant expressions.
1378 if (getType()->isIntegralOrEnumerationType())
1379 return true;
1380
Richard Smith16581332012-03-02 04:14:40 +00001381 // Additionally, in C++11, non-volatile constexpr variables can be used in
1382 // constant expressions.
1383 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001384}
1385
Richard Smith099e7f62011-12-19 06:19:21 +00001386/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1387/// form, which contains extra information on the evaluated value of the
1388/// initializer.
1389EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1390 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1391 if (!Eval) {
1392 Stmt *S = Init.get<Stmt *>();
1393 Eval = new (getASTContext()) EvaluatedStmt;
1394 Eval->Value = S;
1395 Init = Eval;
1396 }
1397 return Eval;
1398}
1399
Richard Smith2d6a5672012-01-14 04:30:29 +00001400APValue *VarDecl::evaluateValue() const {
1401 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1402 return evaluateValue(Notes);
1403}
1404
1405APValue *VarDecl::evaluateValue(
1406 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001407 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1408
1409 // We only produce notes indicating why an initializer is non-constant the
1410 // first time it is evaluated. FIXME: The notes won't always be emitted the
1411 // first time we try evaluation, so might not be produced at all.
1412 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001413 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001414
1415 const Expr *Init = cast<Expr>(Eval->Value);
1416 assert(!Init->isValueDependent());
1417
1418 if (Eval->IsEvaluating) {
1419 // FIXME: Produce a diagnostic for self-initialization.
1420 Eval->CheckedICE = true;
1421 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001422 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001423 }
1424
1425 Eval->IsEvaluating = true;
1426
1427 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1428 this, Notes);
1429
1430 // Ensure the result is an uninitialized APValue if evaluation fails.
1431 if (!Result)
1432 Eval->Evaluated = APValue();
1433
1434 Eval->IsEvaluating = false;
1435 Eval->WasEvaluated = true;
1436
1437 // In C++11, we have determined whether the initializer was a constant
1438 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001439 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001440 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001441 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001442 }
1443
Richard Smith2d6a5672012-01-14 04:30:29 +00001444 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001445}
1446
1447bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001448 // Initializers of weak variables are never ICEs.
1449 if (isWeak())
1450 return false;
1451
Richard Smith099e7f62011-12-19 06:19:21 +00001452 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1453 if (Eval->CheckedICE)
1454 // We have already checked whether this subexpression is an
1455 // integral constant expression.
1456 return Eval->IsICE;
1457
1458 const Expr *Init = cast<Expr>(Eval->Value);
1459 assert(!Init->isValueDependent());
1460
1461 // In C++11, evaluate the initializer to check whether it's a constant
1462 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001463 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001464 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1465 evaluateValue(Notes);
1466 return Eval->IsICE;
1467 }
1468
1469 // It's an ICE whether or not the definition we found is
1470 // out-of-line. See DR 721 and the discussion in Clang PR
1471 // 6206 for details.
1472
1473 if (Eval->CheckingICE)
1474 return false;
1475 Eval->CheckingICE = true;
1476
1477 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1478 Eval->CheckingICE = false;
1479 Eval->CheckedICE = true;
1480 return Eval->IsICE;
1481}
1482
Douglas Gregor03e80032011-06-21 17:03:29 +00001483bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001484 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001485
1486 const Expr *E = getInit();
1487 if (!E)
1488 return false;
1489
1490 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1491 E = Cleanups->getSubExpr();
1492
1493 return isa<MaterializeTemporaryExpr>(E);
1494}
1495
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001496VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001497 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001498 return cast<VarDecl>(MSI->getInstantiatedFrom());
1499
1500 return 0;
1501}
1502
Douglas Gregor663b5a02009-10-14 20:14:33 +00001503TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001504 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001505 return MSI->getTemplateSpecializationKind();
1506
1507 return TSK_Undeclared;
1508}
1509
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001510MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001511 return getASTContext().getInstantiatedFromStaticDataMember(this);
1512}
1513
Douglas Gregor0a897e32009-10-15 17:21:20 +00001514void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1515 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001516 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001517 assert(MSI && "Not an instantiated static data member?");
1518 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001519 if (TSK != TSK_ExplicitSpecialization &&
1520 PointOfInstantiation.isValid() &&
1521 MSI->getPointOfInstantiation().isInvalid())
1522 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001523}
1524
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001525//===----------------------------------------------------------------------===//
1526// ParmVarDecl Implementation
1527//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001528
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001529ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001530 SourceLocation StartLoc,
1531 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001532 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001533 StorageClass S, StorageClass SCAsWritten,
1534 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001535 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001536 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001537}
1538
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001539ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1540 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1541 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1542 0, QualType(), 0, SC_None, SC_None, 0);
1543}
1544
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001545SourceRange ParmVarDecl::getSourceRange() const {
1546 if (!hasInheritedDefaultArg()) {
1547 SourceRange ArgRange = getDefaultArgRange();
1548 if (ArgRange.isValid())
1549 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1550 }
1551
1552 return DeclaratorDecl::getSourceRange();
1553}
1554
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001555Expr *ParmVarDecl::getDefaultArg() {
1556 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1557 assert(!hasUninstantiatedDefaultArg() &&
1558 "Default argument is not yet instantiated!");
1559
1560 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001561 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001562 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001563
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001564 return Arg;
1565}
1566
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001567SourceRange ParmVarDecl::getDefaultArgRange() const {
1568 if (const Expr *E = getInit())
1569 return E->getSourceRange();
1570
1571 if (hasUninstantiatedDefaultArg())
1572 return getUninstantiatedDefaultArg()->getSourceRange();
1573
1574 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001575}
1576
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001577bool ParmVarDecl::isParameterPack() const {
1578 return isa<PackExpansionType>(getType());
1579}
1580
Ted Kremenekd211cb72011-10-06 05:00:56 +00001581void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1582 getASTContext().setParameterIndex(this, parameterIndex);
1583 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1584}
1585
1586unsigned ParmVarDecl::getParameterIndexLarge() const {
1587 return getASTContext().getParameterIndex(this);
1588}
1589
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001590//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001591// FunctionDecl Implementation
1592//===----------------------------------------------------------------------===//
1593
Douglas Gregorda2142f2011-02-19 18:51:44 +00001594void FunctionDecl::getNameForDiagnostic(std::string &S,
1595 const PrintingPolicy &Policy,
1596 bool Qualified) const {
1597 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1598 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1599 if (TemplateArgs)
1600 S += TemplateSpecializationType::PrintTemplateArgumentList(
1601 TemplateArgs->data(),
1602 TemplateArgs->size(),
1603 Policy);
1604
1605}
1606
Ted Kremenek9498d382010-04-29 16:49:01 +00001607bool FunctionDecl::isVariadic() const {
1608 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1609 return FT->isVariadic();
1610 return false;
1611}
1612
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001613bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1614 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001615 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001616 Definition = *I;
1617 return true;
1618 }
1619 }
1620
1621 return false;
1622}
1623
Anders Carlssonffb945f2011-05-14 23:26:09 +00001624bool FunctionDecl::hasTrivialBody() const
1625{
1626 Stmt *S = getBody();
1627 if (!S) {
1628 // Since we don't have a body for this function, we don't know if it's
1629 // trivial or not.
1630 return false;
1631 }
1632
1633 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1634 return true;
1635 return false;
1636}
1637
Sean Hunt10620eb2011-05-06 20:44:56 +00001638bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1639 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001640 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001641 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1642 return true;
1643 }
1644 }
1645
1646 return false;
1647}
1648
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001649Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001650 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1651 if (I->Body) {
1652 Definition = *I;
1653 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001654 } else if (I->IsLateTemplateParsed) {
1655 Definition = *I;
1656 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001657 }
1658 }
1659
1660 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001661}
1662
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001663void FunctionDecl::setBody(Stmt *B) {
1664 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001665 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001666 EndRangeLoc = B->getLocEnd();
1667}
1668
Douglas Gregor21386642010-09-28 21:55:22 +00001669void FunctionDecl::setPure(bool P) {
1670 IsPure = P;
1671 if (P)
1672 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1673 Parent->markedVirtualFunctionPure();
1674}
1675
Douglas Gregor48a83b52009-09-12 00:17:51 +00001676bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001677 const TranslationUnitDecl *tunit =
1678 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1679 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001680 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001681 getIdentifier() &&
1682 getIdentifier()->isStr("main");
1683}
1684
1685bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1686 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1687 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1688 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1689 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1690 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1691
1692 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1693 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1694
1695 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1696 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1697
1698 ASTContext &Context =
1699 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1700 ->getASTContext();
1701
1702 // The result type and first argument type are constant across all
1703 // these operators. The second argument must be exactly void*.
1704 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001705}
1706
Douglas Gregor48a83b52009-09-12 00:17:51 +00001707bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001708 if (getLinkage() != ExternalLinkage)
1709 return false;
1710
1711 if (getAttr<OverloadableAttr>())
1712 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001713
Chandler Carruth10aad442011-02-25 00:05:02 +00001714 const DeclContext *DC = getDeclContext();
1715 if (DC->isRecord())
1716 return false;
1717
Eli Friedman750dc2b2012-01-15 01:23:58 +00001718 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001719 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001720 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001721
Eli Friedman750dc2b2012-01-15 01:23:58 +00001722 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001723}
1724
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001725bool FunctionDecl::isGlobal() const {
1726 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1727 return Method->isStatic();
1728
John McCalld931b082010-08-26 03:08:43 +00001729 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001730 return false;
1731
Mike Stump1eb44332009-09-09 15:08:12 +00001732 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001733 DC->isNamespace();
1734 DC = DC->getParent()) {
1735 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1736 if (!Namespace->getDeclName())
1737 return false;
1738 break;
1739 }
1740 }
1741
1742 return true;
1743}
1744
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001745void
1746FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1747 redeclarable_base::setPreviousDeclaration(PrevDecl);
1748
1749 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1750 FunctionTemplateDecl *PrevFunTmpl
1751 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1752 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1753 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1754 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001755
Axel Naumannd9d137e2011-11-08 18:21:06 +00001756 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001757 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001758}
1759
1760const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1761 return getFirstDeclaration();
1762}
1763
1764FunctionDecl *FunctionDecl::getCanonicalDecl() {
1765 return getFirstDeclaration();
1766}
1767
Douglas Gregor381d34e2010-12-06 18:36:25 +00001768void FunctionDecl::setStorageClass(StorageClass SC) {
1769 assert(isLegalForFunction(SC));
1770 if (getStorageClass() != SC)
1771 ClearLinkageCache();
1772
1773 SClass = SC;
1774}
1775
Douglas Gregor3e41d602009-02-13 23:20:09 +00001776/// \brief Returns a value indicating whether this function
1777/// corresponds to a builtin function.
1778///
1779/// The function corresponds to a built-in function if it is
1780/// declared at translation scope or within an extern "C" block and
1781/// its name matches with the name of a builtin. The returned value
1782/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001783/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001784/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001785unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001786 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001787 return 0;
1788
1789 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001790 if (!BuiltinID)
1791 return 0;
1792
1793 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001794 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1795 return BuiltinID;
1796
1797 // This function has the name of a known C library
1798 // function. Determine whether it actually refers to the C library
1799 // function or whether it just has the same name.
1800
Douglas Gregor9add3172009-02-17 03:23:10 +00001801 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001802 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001803 return 0;
1804
Douglas Gregor3c385e52009-02-14 18:57:46 +00001805 // If this function is at translation-unit scope and we're not in
1806 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001807 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001808 getDeclContext()->isTranslationUnit())
1809 return BuiltinID;
1810
1811 // If the function is in an extern "C" linkage specification and is
1812 // not marked "overloadable", it's the real function.
1813 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001814 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001815 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001816 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001817 return BuiltinID;
1818
1819 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001820 return 0;
1821}
1822
1823
Chris Lattner1ad9b282009-04-25 06:03:53 +00001824/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001825/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001826/// after it has been created.
1827unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001828 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001829 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001830 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001831 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Reid Spencer5f016e22007-07-11 17:01:13 +00001833}
1834
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001835void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001836 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001838 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001841 if (!NewParamInfo.empty()) {
1842 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1843 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001844 }
1845}
1846
James Molloy16f1f712012-02-29 10:24:19 +00001847void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1848 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1849
1850 if (!NewDecls.empty()) {
1851 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1852 std::copy(NewDecls.begin(), NewDecls.end(), A);
1853 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1854 }
1855}
1856
Chris Lattner8123a952008-04-10 02:22:51 +00001857/// getMinRequiredArguments - Returns the minimum number of arguments
1858/// needed to call this function. This may be fewer than the number of
1859/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001860/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001861unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001862 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001863 return getNumParams();
1864
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001865 unsigned NumRequiredArgs = getNumParams();
1866
1867 // If the last parameter is a parameter pack, we don't need an argument for
1868 // it.
1869 if (NumRequiredArgs > 0 &&
1870 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1871 --NumRequiredArgs;
1872
1873 // If this parameter has a default argument, we don't need an argument for
1874 // it.
1875 while (NumRequiredArgs > 0 &&
1876 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001877 --NumRequiredArgs;
1878
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001879 // We might have parameter packs before the end. These can't be deduced,
1880 // but they can still handle multiple arguments.
1881 unsigned ArgIdx = NumRequiredArgs;
1882 while (ArgIdx > 0) {
1883 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1884 NumRequiredArgs = ArgIdx;
1885
1886 --ArgIdx;
1887 }
1888
Chris Lattner8123a952008-04-10 02:22:51 +00001889 return NumRequiredArgs;
1890}
1891
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001892bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001893 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001894 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001895
1896 if (isa<CXXMethodDecl>(this)) {
1897 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1898 return true;
1899 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001900
1901 switch (getTemplateSpecializationKind()) {
1902 case TSK_Undeclared:
1903 case TSK_ExplicitSpecialization:
1904 return false;
1905
1906 case TSK_ImplicitInstantiation:
1907 case TSK_ExplicitInstantiationDeclaration:
1908 case TSK_ExplicitInstantiationDefinition:
1909 // Handle below.
1910 break;
1911 }
1912
1913 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001914 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001915 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001916 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001917
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001918 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001919 return PatternDecl->isInlined();
1920
1921 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001922}
1923
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001924static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1925 // Only consider file-scope declarations in this test.
1926 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1927 return false;
1928
1929 // Only consider explicit declarations; the presence of a builtin for a
1930 // libcall shouldn't affect whether a definition is externally visible.
1931 if (Redecl->isImplicit())
1932 return false;
1933
1934 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1935 return true; // Not an inline definition
1936
1937 return false;
1938}
1939
Nick Lewyckydce67a72011-07-18 05:26:13 +00001940/// \brief For a function declaration in C or C++, determine whether this
1941/// declaration causes the definition to be externally visible.
1942///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001943/// Specifically, this determines if adding the current declaration to the set
1944/// of redeclarations of the given functions causes
1945/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001946bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1947 assert(!doesThisDeclarationHaveABody() &&
1948 "Must have a declaration without a body.");
1949
1950 ASTContext &Context = getASTContext();
1951
David Blaikie4e4d0842012-03-11 07:00:24 +00001952 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001953 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1954 // an externally visible definition.
1955 //
1956 // FIXME: What happens if gnu_inline gets added on after the first
1957 // declaration?
1958 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1959 return false;
1960
1961 const FunctionDecl *Prev = this;
1962 bool FoundBody = false;
1963 while ((Prev = Prev->getPreviousDecl())) {
1964 FoundBody |= Prev->Body;
1965
1966 if (Prev->Body) {
1967 // If it's not the case that both 'inline' and 'extern' are
1968 // specified on the definition, then it is always externally visible.
1969 if (!Prev->isInlineSpecified() ||
1970 Prev->getStorageClassAsWritten() != SC_Extern)
1971 return false;
1972 } else if (Prev->isInlineSpecified() &&
1973 Prev->getStorageClassAsWritten() != SC_Extern) {
1974 return false;
1975 }
1976 }
1977 return FoundBody;
1978 }
1979
David Blaikie4e4d0842012-03-11 07:00:24 +00001980 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001981 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001982
1983 // C99 6.7.4p6:
1984 // [...] If all of the file scope declarations for a function in a
1985 // translation unit include the inline function specifier without extern,
1986 // then the definition in that translation unit is an inline definition.
1987 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001988 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001989 const FunctionDecl *Prev = this;
1990 bool FoundBody = false;
1991 while ((Prev = Prev->getPreviousDecl())) {
1992 FoundBody |= Prev->Body;
1993 if (RedeclForcesDefC99(Prev))
1994 return false;
1995 }
1996 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00001997}
1998
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001999/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002000/// definition will be externally visible.
2001///
2002/// Inline function definitions are always available for inlining optimizations.
2003/// However, depending on the language dialect, declaration specifiers, and
2004/// attributes, the definition of an inline function may or may not be
2005/// "externally" visible to other translation units in the program.
2006///
2007/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002008/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002009/// inline definition becomes externally visible (C99 6.7.4p6).
2010///
2011/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2012/// definition, we use the GNU semantics for inline, which are nearly the
2013/// opposite of C99 semantics. In particular, "inline" by itself will create
2014/// an externally visible symbol, but "extern inline" will not create an
2015/// externally visible symbol.
2016bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002017 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002018 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002019 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002020
David Blaikie4e4d0842012-03-11 07:00:24 +00002021 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002022 // Note: If you change the logic here, please change
2023 // doesDeclarationForceExternallyVisibleDefinition as well.
2024 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002025 // If it's not the case that both 'inline' and 'extern' are
2026 // specified on the definition, then this inline definition is
2027 // externally visible.
2028 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2029 return true;
2030
2031 // If any declaration is 'inline' but not 'extern', then this definition
2032 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002033 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2034 Redecl != RedeclEnd;
2035 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002036 if (Redecl->isInlineSpecified() &&
2037 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002038 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002039 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002040
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002041 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002042 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002043
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002044 // C99 6.7.4p6:
2045 // [...] If all of the file scope declarations for a function in a
2046 // translation unit include the inline function specifier without extern,
2047 // then the definition in that translation unit is an inline definition.
2048 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2049 Redecl != RedeclEnd;
2050 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002051 if (RedeclForcesDefC99(*Redecl))
2052 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002053 }
2054
2055 // C99 6.7.4p6:
2056 // An inline definition does not provide an external definition for the
2057 // function, and does not forbid an external definition in another
2058 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002059 return false;
2060}
2061
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002062/// getOverloadedOperator - Which C++ overloaded operator this
2063/// function represents, if any.
2064OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002065 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2066 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002067 else
2068 return OO_None;
2069}
2070
Sean Hunta6c058d2010-01-13 09:01:02 +00002071/// getLiteralIdentifier - The literal suffix identifier this function
2072/// represents, if any.
2073const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2074 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2075 return getDeclName().getCXXLiteralIdentifier();
2076 else
2077 return 0;
2078}
2079
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002080FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2081 if (TemplateOrSpecialization.isNull())
2082 return TK_NonTemplate;
2083 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2084 return TK_FunctionTemplate;
2085 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2086 return TK_MemberSpecialization;
2087 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2088 return TK_FunctionTemplateSpecialization;
2089 if (TemplateOrSpecialization.is
2090 <DependentFunctionTemplateSpecializationInfo*>())
2091 return TK_DependentFunctionTemplateSpecialization;
2092
David Blaikieb219cfc2011-09-23 05:06:16 +00002093 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002094}
2095
Douglas Gregor2db32322009-10-07 23:56:10 +00002096FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002097 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002098 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2099
2100 return 0;
2101}
2102
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002103MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2104 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2105}
2106
Douglas Gregor2db32322009-10-07 23:56:10 +00002107void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002108FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2109 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002110 TemplateSpecializationKind TSK) {
2111 assert(TemplateOrSpecialization.isNull() &&
2112 "Member function is already a specialization");
2113 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002114 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002115 TemplateOrSpecialization = Info;
2116}
2117
Douglas Gregor3b846b62009-10-27 20:53:28 +00002118bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002119 // If the function is invalid, it can't be implicitly instantiated.
2120 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002121 return false;
2122
2123 switch (getTemplateSpecializationKind()) {
2124 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002125 case TSK_ExplicitInstantiationDefinition:
2126 return false;
2127
2128 case TSK_ImplicitInstantiation:
2129 return true;
2130
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002131 // It is possible to instantiate TSK_ExplicitSpecialization kind
2132 // if the FunctionDecl has a class scope specialization pattern.
2133 case TSK_ExplicitSpecialization:
2134 return getClassScopeSpecializationPattern() != 0;
2135
Douglas Gregor3b846b62009-10-27 20:53:28 +00002136 case TSK_ExplicitInstantiationDeclaration:
2137 // Handled below.
2138 break;
2139 }
2140
2141 // Find the actual template from which we will instantiate.
2142 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002143 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002144 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002145 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002146
2147 // C++0x [temp.explicit]p9:
2148 // Except for inline functions, other explicit instantiation declarations
2149 // have the effect of suppressing the implicit instantiation of the entity
2150 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002151 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002152 return true;
2153
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002154 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002155}
2156
2157bool FunctionDecl::isTemplateInstantiation() const {
2158 switch (getTemplateSpecializationKind()) {
2159 case TSK_Undeclared:
2160 case TSK_ExplicitSpecialization:
2161 return false;
2162 case TSK_ImplicitInstantiation:
2163 case TSK_ExplicitInstantiationDeclaration:
2164 case TSK_ExplicitInstantiationDefinition:
2165 return true;
2166 }
2167 llvm_unreachable("All TSK values handled.");
2168}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002169
2170FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002171 // Handle class scope explicit specialization special case.
2172 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2173 return getClassScopeSpecializationPattern();
2174
Douglas Gregor3b846b62009-10-27 20:53:28 +00002175 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2176 while (Primary->getInstantiatedFromMemberTemplate()) {
2177 // If we have hit a point where the user provided a specialization of
2178 // this template, we're done looking.
2179 if (Primary->isMemberSpecialization())
2180 break;
2181
2182 Primary = Primary->getInstantiatedFromMemberTemplate();
2183 }
2184
2185 return Primary->getTemplatedDecl();
2186 }
2187
2188 return getInstantiatedFromMemberFunction();
2189}
2190
Douglas Gregor16e8be22009-06-29 17:30:29 +00002191FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002192 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002193 = TemplateOrSpecialization
2194 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002195 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002196 }
2197 return 0;
2198}
2199
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002200FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2201 return getASTContext().getClassScopeSpecializationPattern(this);
2202}
2203
Douglas Gregor16e8be22009-06-29 17:30:29 +00002204const TemplateArgumentList *
2205FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002206 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002207 = TemplateOrSpecialization
2208 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002209 return Info->TemplateArguments;
2210 }
2211 return 0;
2212}
2213
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002214const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002215FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2216 if (FunctionTemplateSpecializationInfo *Info
2217 = TemplateOrSpecialization
2218 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2219 return Info->TemplateArgumentsAsWritten;
2220 }
2221 return 0;
2222}
2223
Mike Stump1eb44332009-09-09 15:08:12 +00002224void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002225FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2226 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002227 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002228 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002229 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002230 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2231 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002232 assert(TSK != TSK_Undeclared &&
2233 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002234 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002235 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002236 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002237 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2238 TemplateArgs,
2239 TemplateArgsAsWritten,
2240 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002241 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002242 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002243}
2244
John McCallaf2094e2010-04-08 09:05:18 +00002245void
2246FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2247 const UnresolvedSetImpl &Templates,
2248 const TemplateArgumentListInfo &TemplateArgs) {
2249 assert(TemplateOrSpecialization.isNull());
2250 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2251 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002252 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002253 void *Buffer = Context.Allocate(Size);
2254 DependentFunctionTemplateSpecializationInfo *Info =
2255 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2256 TemplateArgs);
2257 TemplateOrSpecialization = Info;
2258}
2259
2260DependentFunctionTemplateSpecializationInfo::
2261DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2262 const TemplateArgumentListInfo &TArgs)
2263 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2264
2265 d.NumTemplates = Ts.size();
2266 d.NumArgs = TArgs.size();
2267
2268 FunctionTemplateDecl **TsArray =
2269 const_cast<FunctionTemplateDecl**>(getTemplates());
2270 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2271 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2272
2273 TemplateArgumentLoc *ArgsArray =
2274 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2275 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2276 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2277}
2278
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002279TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002280 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002281 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002282 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002283 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002284 if (FTSInfo)
2285 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Douglas Gregor2db32322009-10-07 23:56:10 +00002287 MemberSpecializationInfo *MSInfo
2288 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2289 if (MSInfo)
2290 return MSInfo->getTemplateSpecializationKind();
2291
2292 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002293}
2294
Mike Stump1eb44332009-09-09 15:08:12 +00002295void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002296FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2297 SourceLocation PointOfInstantiation) {
2298 if (FunctionTemplateSpecializationInfo *FTSInfo
2299 = TemplateOrSpecialization.dyn_cast<
2300 FunctionTemplateSpecializationInfo*>()) {
2301 FTSInfo->setTemplateSpecializationKind(TSK);
2302 if (TSK != TSK_ExplicitSpecialization &&
2303 PointOfInstantiation.isValid() &&
2304 FTSInfo->getPointOfInstantiation().isInvalid())
2305 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2306 } else if (MemberSpecializationInfo *MSInfo
2307 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2308 MSInfo->setTemplateSpecializationKind(TSK);
2309 if (TSK != TSK_ExplicitSpecialization &&
2310 PointOfInstantiation.isValid() &&
2311 MSInfo->getPointOfInstantiation().isInvalid())
2312 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2313 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002314 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002315}
2316
2317SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002318 if (FunctionTemplateSpecializationInfo *FTSInfo
2319 = TemplateOrSpecialization.dyn_cast<
2320 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002321 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002322 else if (MemberSpecializationInfo *MSInfo
2323 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002324 return MSInfo->getPointOfInstantiation();
2325
2326 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002327}
2328
Douglas Gregor9f185072009-09-11 20:15:17 +00002329bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002330 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002331 return true;
2332
2333 // If this function was instantiated from a member function of a
2334 // class template, check whether that member function was defined out-of-line.
2335 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2336 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002337 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002338 return Definition->isOutOfLine();
2339 }
2340
2341 // If this function was instantiated from a function template,
2342 // check whether that function template was defined out-of-line.
2343 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2344 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002345 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002346 return Definition->isOutOfLine();
2347 }
2348
2349 return false;
2350}
2351
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002352SourceRange FunctionDecl::getSourceRange() const {
2353 return SourceRange(getOuterLocStart(), EndRangeLoc);
2354}
2355
Anna Zaks9392d4e2012-01-18 02:45:01 +00002356unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002357 IdentifierInfo *FnInfo = getIdentifier();
2358
2359 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002360 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002361
2362 // Builtin handling.
2363 switch (getBuiltinID()) {
2364 case Builtin::BI__builtin_memset:
2365 case Builtin::BI__builtin___memset_chk:
2366 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002367 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002368
2369 case Builtin::BI__builtin_memcpy:
2370 case Builtin::BI__builtin___memcpy_chk:
2371 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002372 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002373
2374 case Builtin::BI__builtin_memmove:
2375 case Builtin::BI__builtin___memmove_chk:
2376 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002377 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002378
2379 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002380 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002381 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002382 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002383
2384 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002385 case Builtin::BImemcmp:
2386 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002387
2388 case Builtin::BI__builtin_strncpy:
2389 case Builtin::BI__builtin___strncpy_chk:
2390 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002391 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002392
2393 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002394 case Builtin::BIstrncmp:
2395 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002396
2397 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002398 case Builtin::BIstrncasecmp:
2399 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002400
2401 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002402 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002403 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002404 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002405
2406 case Builtin::BI__builtin_strndup:
2407 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002408 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002409
Anna Zaksc36bedc2012-02-01 19:08:57 +00002410 case Builtin::BI__builtin_strlen:
2411 case Builtin::BIstrlen:
2412 return Builtin::BIstrlen;
2413
Anna Zaksd9b859a2012-01-13 21:52:01 +00002414 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002415 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002416 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002417 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002418 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002419 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002420 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002421 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002422 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002423 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002424 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002425 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002426 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002427 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002428 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002429 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002430 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002431 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002432 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002433 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002434 else if (FnInfo->isStr("strlen"))
2435 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002436 }
2437 break;
2438 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002439 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002440}
2441
Chris Lattner8a934232008-03-31 00:36:02 +00002442//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002443// FieldDecl Implementation
2444//===----------------------------------------------------------------------===//
2445
Jay Foad4ba2a172011-01-12 09:06:06 +00002446FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002447 SourceLocation StartLoc, SourceLocation IdLoc,
2448 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002449 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2450 bool HasInit) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002451 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith7a614d82011-06-11 17:19:42 +00002452 BW, Mutable, HasInit);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002453}
2454
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002455FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2456 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2457 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2458 0, QualType(), 0, 0, false, false);
2459}
2460
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002461bool FieldDecl::isAnonymousStructOrUnion() const {
2462 if (!isImplicit() || getDeclName())
2463 return false;
2464
2465 if (const RecordType *Record = getType()->getAs<RecordType>())
2466 return Record->getDecl()->isAnonymousStructOrUnion();
2467
2468 return false;
2469}
2470
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002471unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2472 assert(isBitField() && "not a bitfield");
2473 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2474 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2475}
2476
John McCallba4f5d52011-01-20 07:57:12 +00002477unsigned FieldDecl::getFieldIndex() const {
2478 if (CachedFieldIndex) return CachedFieldIndex - 1;
2479
Richard Smith180f4792011-11-10 06:34:14 +00002480 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002481 const RecordDecl *RD = getParent();
2482 const FieldDecl *LastFD = 0;
2483 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smith180f4792011-11-10 06:34:14 +00002484
2485 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2486 I != E; ++I, ++Index) {
2487 (*I)->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002488
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002489 if (IsMsStruct) {
2490 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smith180f4792011-11-10 06:34:14 +00002491 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2492 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002493 continue;
2494 }
Richard Smith180f4792011-11-10 06:34:14 +00002495 LastFD = (*I);
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002496 }
John McCallba4f5d52011-01-20 07:57:12 +00002497 }
2498
Richard Smith180f4792011-11-10 06:34:14 +00002499 assert(CachedFieldIndex && "failed to find field in parent");
2500 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002501}
2502
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002503SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002504 if (const Expr *E = InitializerOrBitWidth.getPointer())
2505 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002506 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002507}
2508
Richard Smith7a614d82011-06-11 17:19:42 +00002509void FieldDecl::setInClassInitializer(Expr *Init) {
2510 assert(!InitializerOrBitWidth.getPointer() &&
2511 "bit width or initializer already set");
2512 InitializerOrBitWidth.setPointer(Init);
2513 InitializerOrBitWidth.setInt(0);
2514}
2515
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002516//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002517// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002518//===----------------------------------------------------------------------===//
2519
Douglas Gregor1693e152010-07-06 18:42:40 +00002520SourceLocation TagDecl::getOuterLocStart() const {
2521 return getTemplateOrInnerLocStart(this);
2522}
2523
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002524SourceRange TagDecl::getSourceRange() const {
2525 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002526 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002527}
2528
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002529TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002530 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002531}
2532
Richard Smith162e1c12011-04-15 14:24:37 +00002533void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2534 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002535 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002536 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002537 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002538}
2539
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002540void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002541 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002542
2543 if (isa<CXXRecordDecl>(this)) {
2544 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2545 struct CXXRecordDecl::DefinitionData *Data =
2546 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002547 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2548 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002549 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002550}
2551
2552void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002553 assert((!isa<CXXRecordDecl>(this) ||
2554 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2555 "definition completed but not started");
2556
John McCall5e1cdac2011-10-07 06:10:15 +00002557 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002558 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002559
2560 if (ASTMutationListener *L = getASTMutationListener())
2561 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002562}
2563
John McCall5e1cdac2011-10-07 06:10:15 +00002564TagDecl *TagDecl::getDefinition() const {
2565 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002566 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002567 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2568 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002569
2570 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002571 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002572 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002573 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002574
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002575 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002576}
2577
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002578void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2579 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002580 // Make sure the extended qualifier info is allocated.
2581 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002582 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002583 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002584 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002585 } else {
John McCallb6217662010-03-15 10:12:16 +00002586 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002587 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002588 if (getExtInfo()->NumTemplParamLists == 0) {
2589 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002590 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002591 }
2592 else
2593 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002594 }
2595 }
2596}
2597
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002598void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2599 unsigned NumTPLists,
2600 TemplateParameterList **TPLists) {
2601 assert(NumTPLists > 0);
2602 // Make sure the extended decl info is allocated.
2603 if (!hasExtInfo())
2604 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002605 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002606 // Set the template parameter lists info.
2607 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2608}
2609
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002610//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002611// EnumDecl Implementation
2612//===----------------------------------------------------------------------===//
2613
David Blaikie99ba9e32011-12-20 02:48:34 +00002614void EnumDecl::anchor() { }
2615
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002616EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2617 SourceLocation StartLoc, SourceLocation IdLoc,
2618 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002619 EnumDecl *PrevDecl, bool IsScoped,
2620 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002621 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002622 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002623 C.getTypeDeclType(Enum, PrevDecl);
2624 return Enum;
2625}
2626
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002627EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2628 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2629 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2630 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002631}
2632
Douglas Gregor838db382010-02-11 01:19:42 +00002633void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002634 QualType NewPromotionType,
2635 unsigned NumPositiveBits,
2636 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002637 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002638 if (!IntegerType)
2639 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002640 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002641 setNumPositiveBits(NumPositiveBits);
2642 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002643 TagDecl::completeDefinition();
2644}
2645
Richard Smith1af83c42012-03-23 03:33:32 +00002646TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2647 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2648 return MSI->getTemplateSpecializationKind();
2649
2650 return TSK_Undeclared;
2651}
2652
2653void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2654 SourceLocation PointOfInstantiation) {
2655 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2656 assert(MSI && "Not an instantiated member enumeration?");
2657 MSI->setTemplateSpecializationKind(TSK);
2658 if (TSK != TSK_ExplicitSpecialization &&
2659 PointOfInstantiation.isValid() &&
2660 MSI->getPointOfInstantiation().isInvalid())
2661 MSI->setPointOfInstantiation(PointOfInstantiation);
2662}
2663
Richard Smithf1c66b42012-03-14 23:13:10 +00002664EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2665 if (SpecializationInfo)
2666 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2667
2668 return 0;
2669}
2670
2671void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2672 TemplateSpecializationKind TSK) {
2673 assert(!SpecializationInfo && "Member enum is already a specialization");
2674 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2675}
2676
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002677//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002678// RecordDecl Implementation
2679//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002680
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002681RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2682 SourceLocation StartLoc, SourceLocation IdLoc,
2683 IdentifierInfo *Id, RecordDecl *PrevDecl)
2684 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002685 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002686 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002687 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002688 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002689 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002690}
2691
Jay Foad4ba2a172011-01-12 09:06:06 +00002692RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002693 SourceLocation StartLoc, SourceLocation IdLoc,
2694 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2695 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2696 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002697 C.getTypeDeclType(R, PrevDecl);
2698 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002699}
2700
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002701RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2702 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2703 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2704 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002705}
2706
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002707bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002708 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002709 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2710}
2711
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002712RecordDecl::field_iterator RecordDecl::field_begin() const {
2713 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2714 LoadFieldsFromExternalStorage();
2715
2716 return field_iterator(decl_iterator(FirstDecl));
2717}
2718
Douglas Gregorda2142f2011-02-19 18:51:44 +00002719/// completeDefinition - Notes that the definition of this type is now
2720/// complete.
2721void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002722 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002723 TagDecl::completeDefinition();
2724}
2725
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002726void RecordDecl::LoadFieldsFromExternalStorage() const {
2727 ExternalASTSource *Source = getASTContext().getExternalSource();
2728 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2729
2730 // Notify that we have a RecordDecl doing some initialization.
2731 ExternalASTSource::Deserializing TheFields(Source);
2732
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002734 LoadedFieldsFromExternalStorage = true;
2735 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2736 case ELR_Success:
2737 break;
2738
2739 case ELR_AlreadyLoaded:
2740 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002741 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002742 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002743
2744#ifndef NDEBUG
2745 // Check that all decls we got were FieldDecls.
2746 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2747 assert(isa<FieldDecl>(Decls[i]));
2748#endif
2749
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002750 if (Decls.empty())
2751 return;
2752
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002753 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2754 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002755}
2756
Steve Naroff56ee6892008-10-08 17:01:13 +00002757//===----------------------------------------------------------------------===//
2758// BlockDecl Implementation
2759//===----------------------------------------------------------------------===//
2760
David Blaikie4278c652011-09-21 18:16:56 +00002761void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002762 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002763
Steve Naroffe78b8092009-03-13 16:56:44 +00002764 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002765 if (!NewParamInfo.empty()) {
2766 NumParams = NewParamInfo.size();
2767 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2768 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002769 }
2770}
2771
John McCall6b5a61b2011-02-07 10:33:21 +00002772void BlockDecl::setCaptures(ASTContext &Context,
2773 const Capture *begin,
2774 const Capture *end,
2775 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002776 CapturesCXXThis = capturesCXXThis;
2777
2778 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002779 NumCaptures = 0;
2780 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002781 return;
2782 }
2783
John McCall6b5a61b2011-02-07 10:33:21 +00002784 NumCaptures = end - begin;
2785
2786 // Avoid new Capture[] because we don't want to provide a default
2787 // constructor.
2788 size_t allocationSize = NumCaptures * sizeof(Capture);
2789 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2790 memcpy(buffer, begin, allocationSize);
2791 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002792}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002793
John McCall204e1332011-06-15 22:51:16 +00002794bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2795 for (capture_const_iterator
2796 i = capture_begin(), e = capture_end(); i != e; ++i)
2797 // Only auto vars can be captured, so no redeclaration worries.
2798 if (i->getVariable() == variable)
2799 return true;
2800
2801 return false;
2802}
2803
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002804SourceRange BlockDecl::getSourceRange() const {
2805 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2806}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002807
2808//===----------------------------------------------------------------------===//
2809// Other Decl Allocation/Deallocation Method Implementations
2810//===----------------------------------------------------------------------===//
2811
David Blaikie99ba9e32011-12-20 02:48:34 +00002812void TranslationUnitDecl::anchor() { }
2813
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002814TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2815 return new (C) TranslationUnitDecl(C);
2816}
2817
David Blaikie99ba9e32011-12-20 02:48:34 +00002818void LabelDecl::anchor() { }
2819
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002820LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002821 SourceLocation IdentL, IdentifierInfo *II) {
2822 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2823}
2824
2825LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2826 SourceLocation IdentL, IdentifierInfo *II,
2827 SourceLocation GnuLabelL) {
2828 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2829 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002830}
2831
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002832LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2833 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2834 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002835}
2836
David Blaikie99ba9e32011-12-20 02:48:34 +00002837void ValueDecl::anchor() { }
2838
2839void ImplicitParamDecl::anchor() { }
2840
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002841ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002842 SourceLocation IdLoc,
2843 IdentifierInfo *Id,
2844 QualType Type) {
2845 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002846}
2847
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002848ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2849 unsigned ID) {
2850 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2851 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2852}
2853
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002854FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002855 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002856 const DeclarationNameInfo &NameInfo,
2857 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002858 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002859 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002860 bool hasWrittenPrototype,
2861 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002862 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2863 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002864 isInlineSpecified,
2865 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002866 New->HasWrittenPrototype = hasWrittenPrototype;
2867 return New;
2868}
2869
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002870FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2871 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2872 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2873 DeclarationNameInfo(), QualType(), 0,
2874 SC_None, SC_None, false, false);
2875}
2876
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002877BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2878 return new (C) BlockDecl(DC, L);
2879}
2880
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002881BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2882 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2883 return new (Mem) BlockDecl(0, SourceLocation());
2884}
2885
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002886EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2887 SourceLocation L,
2888 IdentifierInfo *Id, QualType T,
2889 Expr *E, const llvm::APSInt &V) {
2890 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2891}
2892
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002893EnumConstantDecl *
2894EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2895 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2896 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2897 llvm::APSInt());
2898}
2899
David Blaikie99ba9e32011-12-20 02:48:34 +00002900void IndirectFieldDecl::anchor() { }
2901
Benjamin Kramerd9811462010-11-21 14:11:41 +00002902IndirectFieldDecl *
2903IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2904 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2905 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002906 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2907}
2908
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002909IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2910 unsigned ID) {
2911 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2912 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2913 QualType(), 0, 0);
2914}
2915
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002916SourceRange EnumConstantDecl::getSourceRange() const {
2917 SourceLocation End = getLocation();
2918 if (Init)
2919 End = Init->getLocEnd();
2920 return SourceRange(getLocation(), End);
2921}
2922
David Blaikie99ba9e32011-12-20 02:48:34 +00002923void TypeDecl::anchor() { }
2924
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002925TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002926 SourceLocation StartLoc, SourceLocation IdLoc,
2927 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2928 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002929}
2930
David Blaikie99ba9e32011-12-20 02:48:34 +00002931void TypedefNameDecl::anchor() { }
2932
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002933TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2934 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2935 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2936}
2937
Richard Smith162e1c12011-04-15 14:24:37 +00002938TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2939 SourceLocation StartLoc,
2940 SourceLocation IdLoc, IdentifierInfo *Id,
2941 TypeSourceInfo *TInfo) {
2942 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2943}
2944
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002945TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2946 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2947 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2948}
2949
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002950SourceRange TypedefDecl::getSourceRange() const {
2951 SourceLocation RangeEnd = getLocation();
2952 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2953 if (typeIsPostfix(TInfo->getType()))
2954 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2955 }
2956 return SourceRange(getLocStart(), RangeEnd);
2957}
2958
Richard Smith162e1c12011-04-15 14:24:37 +00002959SourceRange TypeAliasDecl::getSourceRange() const {
2960 SourceLocation RangeEnd = getLocStart();
2961 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2962 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2963 return SourceRange(getLocStart(), RangeEnd);
2964}
2965
David Blaikie99ba9e32011-12-20 02:48:34 +00002966void FileScopeAsmDecl::anchor() { }
2967
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002968FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002969 StringLiteral *Str,
2970 SourceLocation AsmLoc,
2971 SourceLocation RParenLoc) {
2972 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002973}
Douglas Gregor15de72c2011-12-02 23:23:56 +00002974
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002975FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2976 unsigned ID) {
2977 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2978 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2979}
2980
Douglas Gregor15de72c2011-12-02 23:23:56 +00002981//===----------------------------------------------------------------------===//
2982// ImportDecl Implementation
2983//===----------------------------------------------------------------------===//
2984
2985/// \brief Retrieve the number of module identifiers needed to name the given
2986/// module.
2987static unsigned getNumModuleIdentifiers(Module *Mod) {
2988 unsigned Result = 1;
2989 while (Mod->Parent) {
2990 Mod = Mod->Parent;
2991 ++Result;
2992 }
2993 return Result;
2994}
2995
Douglas Gregor5948ae12012-01-03 18:04:46 +00002996ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00002997 Module *Imported,
2998 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00002999 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003000 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003001{
3002 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3003 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3004 memcpy(StoredLocs, IdentifierLocs.data(),
3005 IdentifierLocs.size() * sizeof(SourceLocation));
3006}
3007
Douglas Gregor5948ae12012-01-03 18:04:46 +00003008ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003009 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003010 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003011 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003012{
3013 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3014}
3015
3016ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003017 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003018 ArrayRef<SourceLocation> IdentifierLocs) {
3019 void *Mem = C.Allocate(sizeof(ImportDecl) +
3020 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003021 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003022}
3023
3024ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003025 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003026 Module *Imported,
3027 SourceLocation EndLoc) {
3028 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003029 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003030 Import->setImplicit();
3031 return Import;
3032}
3033
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003034ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3035 unsigned NumLocations) {
3036 void *Mem = AllocateDeserializedDecl(C, ID,
3037 (sizeof(ImportDecl) +
3038 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003039 return new (Mem) ImportDecl(EmptyShell());
3040}
3041
3042ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3043 if (!ImportedAndComplete.getInt())
3044 return ArrayRef<SourceLocation>();
3045
3046 const SourceLocation *StoredLocs
3047 = reinterpret_cast<const SourceLocation *>(this + 1);
3048 return ArrayRef<SourceLocation>(StoredLocs,
3049 getNumModuleIdentifiers(getImportedModule()));
3050}
3051
3052SourceRange ImportDecl::getSourceRange() const {
3053 if (!ImportedAndComplete.getInt())
3054 return SourceRange(getLocation(),
3055 *reinterpret_cast<const SourceLocation *>(this + 1));
3056
3057 return SourceRange(getLocation(), getIdentifierLocs().back());
3058}