blob: 12f8f7e0b17c3f37fedbaa52dd8557e3e77d3fb1 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000027#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000028#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000029#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000031
David Blaikie9c70e042011-09-21 18:16:56 +000032#include <algorithm>
33
Chris Lattner6d9a6852006-10-25 05:11:20 +000034using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000035
Chris Lattner88f70d62008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor1baf38f2011-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 McCall457a04e2010-10-22 21:05:15 +000051 }
Douglas Gregor1baf38f2011-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 Gregore8bbc122011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-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 McCall457a04e2010-10-22 21:05:15 +000065}
66
John McCallc273f242010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallc273f242010-10-30 11:50:40 +000068
Benjamin Kramer396dcf32010-11-05 19:56:37 +000069namespace {
John McCall07072662010-11-02 01:45:15 +000070/// Flags controlling the computation of linkage and visibility.
71struct LVFlags {
Rafael Espindola505a7c82012-04-16 18:25:01 +000072 const bool ConsiderGlobalVisibility;
73 const bool ConsiderVisibilityAttributes;
74 const bool ConsiderTemplateParameterTypes;
John McCall07072662010-11-02 01:45:15 +000075
76 LVFlags() : ConsiderGlobalVisibility(true),
John McCall8bc6d5b2011-03-04 10:39:25 +000077 ConsiderVisibilityAttributes(true),
78 ConsiderTemplateParameterTypes(true) {
John McCall07072662010-11-02 01:45:15 +000079 }
80
Rafael Espindola9d287402012-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 Gregorbf62d642010-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 Espindola9d287402012-04-16 13:44:41 +000090 return LVFlags(false, false, false);
John McCall07072662010-11-02 01:45:15 +000091 }
Douglas Gregor91df6cf2010-12-06 18:50:56 +000092};
Benjamin Kramer396dcf32010-11-05 19:56:37 +000093} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +000094
Rafael Espindola2f869a32012-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 Gregor7dc5c172010-02-03 09:33:45 +0000100/// \brief Get the most restrictive linkage for the types in the given
101/// template parameter list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000102static LinkageInfo
John McCall457a04e2010-10-22 21:05:15 +0000103getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000104 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000105 for (TemplateParameterList::const_iterator P = Params->begin(),
106 PEnd = Params->end();
107 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-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 Espindola2f869a32012-01-14 00:30:36 +0000113 LV.merge(getLVForType(T));
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000114 }
115 continue;
116 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000117
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000118 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000119 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000120 continue;
121 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000122 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000123
124 if (TemplateTemplateParmDecl *TTP
125 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000126 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000127 }
128 }
129
John McCall457a04e2010-10-22 21:05:15 +0000130 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000131}
132
Douglas Gregorbf62d642010-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 Gregor7dc5c172010-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 Espindola2f869a32012-01-14 00:30:36 +0000138static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
139 unsigned NumArgs,
140 LVFlags &F) {
141 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-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 Espindolaeeb9d9f2012-01-02 06:26:22 +0000149
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000150 case TemplateArgument::Type:
Rafael Espindola2f869a32012-01-14 00:30:36 +0000151 LV.merge(getLVForType(Args[I].getAsType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000152 break;
153
154 case TemplateArgument::Declaration:
John McCall457a04e2010-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 Gregor91df6cf2010-12-06 18:50:56 +0000158 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
159 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000160 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000161 break;
162
163 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000164 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000165 if (TemplateDecl *Template
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000166 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola2f869a32012-01-14 00:30:36 +0000167 LV.merge(getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000168 break;
169
170 case TemplateArgument::Pack:
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000171 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
172 Args[I].pack_size(),
173 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000174 break;
175 }
176 }
177
John McCall457a04e2010-10-22 21:05:15 +0000178 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000179}
180
Rafael Espindola2f869a32012-01-14 00:30:36 +0000181static LinkageInfo
Douglas Gregorbf62d642010-12-06 18:36:25 +0000182getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
183 LVFlags &F) {
184 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000185}
186
John McCallb8c604a2011-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 McCall07072662010-11-02 01:45:15 +0000197static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000198 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-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 McCall8e7d6562010-08-26 03:08:43 +0000210 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000211 return LinkageInfo::internal();
Douglas Gregorf73b2822009-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000217 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000218 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000219 Var->getStorageClass() != SC_Extern &&
220 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000221 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000222 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000223 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000224 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000225 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000226 FoundExtern = true;
227
228 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000229 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000231 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000232 const VarDecl *PrevVar = Var->getPreviousDecl();
233 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000234 if (PrevVar->getStorageClass() == SC_PrivateExtern)
235 break;
236 if (PrevVar)
237 return PrevVar->getLinkageAndVisibility();
238 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000239 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-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 McCall8e7d6562010-08-26 03:08:43 +0000251 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000252 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-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 McCallc273f242010-10-30 11:50:40 +0000256 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000257 }
258
Chandler Carruth9682a2fd2011-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 Friedman839192f2012-01-15 01:23:58 +0000262 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
263 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000264 return LinkageInfo::uniqueExternal();
265 }
John McCallb7139c42010-10-28 04:18:25 +0000266
John McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +0000273 LinkageInfo LV;
274
Rafael Espindola78158af2012-04-16 18:46:26 +0000275 if (F.ConsiderVisibilityAttributes) {
276 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000277 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000278 } else {
279 // If we're declared in a namespace with a visibility attribute,
280 // use that namespace's visibility, but don't call it explicit.
281 for (const DeclContext *DC = D->getDeclContext();
282 !isa<TranslationUnitDecl>(DC);
283 DC = DC->getParent()) {
284 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
285 if (!ND) continue;
286 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000287 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000288 break;
289 }
290 }
291 }
292 }
293
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000294 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
295
Douglas Gregorf73b2822009-11-25 22:24:25 +0000296 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000297
Douglas Gregorf73b2822009-11-25 22:24:25 +0000298 // A name having namespace scope has external linkage if it is the
299 // name of
300 //
301 // - an object or reference, unless it has internal linkage; or
302 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000303 // GCC applies the following optimization to variables and static
304 // data members, but not to functions:
305 //
John McCall457a04e2010-10-22 21:05:15 +0000306 // Modify the variable's LV by the LV of its type unless this is
307 // C or extern "C". This follows from [basic.link]p9:
308 // A type without linkage shall not be used as the type of a
309 // variable or function with external linkage unless
310 // - the entity has C language linkage, or
311 // - the entity is declared within an unnamed namespace, or
312 // - the entity is not used or is defined in the same
313 // translation unit.
314 // and [basic.link]p10:
315 // ...the types specified by all declarations referring to a
316 // given variable or function shall be identical...
317 // C does not have an equivalent rule.
318 //
John McCall5fe84122010-10-26 04:59:26 +0000319 // Ignore this if we've got an explicit attribute; the user
320 // probably knows what they're doing.
321 //
John McCall457a04e2010-10-22 21:05:15 +0000322 // Note that we don't want to make the variable non-external
323 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000324 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000325 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000326 LinkageInfo TypeLV = getLVForType(Var->getType());
327 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000328 return LinkageInfo::uniqueExternal();
Rafael Espindola2dd5ed52012-04-17 18:47:20 +0000329 LV.mergeVisibilityWithMin(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000330 }
331
John McCall23032652010-11-02 18:38:13 +0000332 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000333 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000334
David Blaikiebbafb8a2012-03-11 07:00:24 +0000335 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000336 (Var->getStorageClass() == SC_Extern ||
337 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000338
Douglas Gregorf73b2822009-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 Gregorec9fd132012-01-14 16:38:05 +0000348 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000349 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000350 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
351 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000352 }
353 }
354
Douglas Gregorf73b2822009-11-25 22:24:25 +0000355 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000356 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-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 McCall457a04e2010-10-22 21:05:15 +0000361
John McCall23032652010-11-02 18:38:13 +0000362 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000363 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000364
Douglas Gregorf73b2822009-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000370 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000371 (Function->getStorageClass() == SC_Extern ||
372 Function->getStorageClass() == SC_PrivateExtern ||
373 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-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 Gregorec9fd132012-01-14 16:38:05 +0000383 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000384 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000385 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
386 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000387 }
388 }
389
John McCallf768aa72011-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000394 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000395 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000396 Function->getType()->getLinkage() == UniqueExternalLinkage)
397 return LinkageInfo::uniqueExternal();
398
John McCallb8c604a2011-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 Gregor7dc5c172010-02-03 09:33:45 +0000402 = Function->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000403 if (shouldConsiderTemplateLV(Function, specInfo)) {
404 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola9d287402012-04-16 13:44:41 +0000405 LVFlags::CreateOnlyDeclLinkage()));
John McCallb8c604a2011-06-27 23:06:04 +0000406 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000407 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F));
John McCallb8c604a2011-06-27 23:06:04 +0000408 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000409 }
410
Douglas Gregorf73b2822009-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 McCall457a04e2010-10-22 21:05:15 +0000417 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
418 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000419 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000420 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000421
John McCall457a04e2010-10-22 21:05:15 +0000422 // If this is a class template specialization, consider the
423 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000424 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000425 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCallb8c604a2011-06-27 23:06:04 +0000426 if (shouldConsiderTemplateLV(spec)) {
427 // From the template.
428 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola9d287402012-04-16 13:44:41 +0000429 LVFlags::CreateOnlyDeclLinkage()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000430
John McCallb8c604a2011-06-27 23:06:04 +0000431 // The arguments at which the template was instantiated.
432 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000433 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F));
John McCallb8c604a2011-06-27 23:06:04 +0000434 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000435 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000436
437 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000438 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000439 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000440 if (!isExternalLinkage(EnumLV.linkage()))
441 return LinkageInfo::none();
442 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000443
444 // - a template, unless it is a function template that has
445 // internal linkage (Clause 14);
John McCall8bc6d5b2011-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 Gregor7dc5c172010-02-03 09:33:45 +0000449
Douglas Gregorf73b2822009-11-25 22:24:25 +0000450 // - a namespace (7.3), unless it is declared within an unnamed
451 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000452 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
453 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000454
John McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +0000462 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000463 }
464
465 // If we ended up with non-external linkage, visibility should
466 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000467 if (LV.linkage() != ExternalLinkage)
468 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000469
John McCall457a04e2010-10-22 21:05:15 +0000470 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000471}
472
John McCall07072662010-11-02 01:45:15 +0000473static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-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 McCall8823c652010-08-13 08:35:10 +0000478 if (!(isa<CXXMethodDecl>(D) ||
479 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000480 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000481 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000482 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000483 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000484
John McCall07072662010-11-02 01:45:15 +0000485 LinkageInfo LV;
486
Rafael Espindola505a7c82012-04-16 18:25:01 +0000487 bool DHasExplicitVisibility = false;
John McCall07072662010-11-02 01:45:15 +0000488 // If we have an explicit visibility attribute, merge that in.
489 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000490 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
491 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000492
Rafael Espindola505a7c82012-04-16 18:25:01 +0000493 DHasExplicitVisibility = true;
John McCall07072662010-11-02 01:45:15 +0000494 }
495 }
Rafael Espindola505a7c82012-04-16 18:25:01 +0000496 // Ignore both global visibility and attributes when computing our
497 // parent's visibility if we already have an explicit one.
498 LVFlags ClassF = DHasExplicitVisibility ?
499 LVFlags::CreateOnlyDeclLinkage() : F;
500
501 // If we're paying attention to global visibility, apply
502 // -finline-visibility-hidden if this is an inline method.
503 //
504 // Note that we do this before merging information about
505 // the class visibility.
506 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
507 TemplateSpecializationKind TSK = TSK_Undeclared;
508 if (FunctionTemplateSpecializationInfo *spec
509 = MD->getTemplateSpecializationInfo()) {
510 TSK = spec->getTemplateSpecializationKind();
511 } else if (MemberSpecializationInfo *MSI =
512 MD->getMemberSpecializationInfo()) {
513 TSK = MSI->getTemplateSpecializationKind();
514 }
515
516 const FunctionDecl *Def = 0;
517 // InlineVisibilityHidden only applies to definitions, and
518 // isInlined() only gives meaningful answers on definitions
519 // anyway.
520 if (TSK != TSK_ExplicitInstantiationDeclaration &&
521 TSK != TSK_ExplicitInstantiationDefinition &&
522 F.ConsiderGlobalVisibility &&
523 !LV.visibilityExplicit() &&
524 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
525 MD->hasBody(Def) && Def->isInlined())
526 LV.mergeVisibility(HiddenVisibility, true);
527 }
John McCallc273f242010-10-30 11:50:40 +0000528
529 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000530 // linkage.
531 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
532 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000533 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000534
535 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000536 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000537 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000538
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000539 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
540
John McCall8823c652010-08-13 08:35:10 +0000541 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000542 // If the type of the function uses a type with unique-external
543 // linkage, it's not legally usable from outside this translation unit.
544 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
545 return LinkageInfo::uniqueExternal();
546
John McCall457a04e2010-10-22 21:05:15 +0000547 // If this is a method template specialization, use the linkage for
548 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000549 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000550 = MD->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000551 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000552 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
553 F));
John McCallb8c604a2011-06-27 23:06:04 +0000554 if (F.ConsiderTemplateParameterTypes)
555 LV.merge(getLVForTemplateParameterList(
556 spec->getTemplate()->getTemplateParameters()));
557 }
John McCalle6e622e2010-11-01 01:29:57 +0000558 }
John McCall457a04e2010-10-22 21:05:15 +0000559
John McCall37bb6c92010-10-29 22:22:43 +0000560 // Note that in contrast to basically every other situation, we
561 // *do* apply -fvisibility to method declarations.
562
563 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000564 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000565 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-06-27 23:06:04 +0000566 if (shouldConsiderTemplateLV(spec)) {
567 // Merge template argument/parameter information for member
568 // class template specializations.
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000569 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
570 F));
John McCall8bc6d5b2011-03-04 10:39:25 +0000571 if (F.ConsiderTemplateParameterTypes)
572 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000573 spec->getSpecializedTemplate()->getTemplateParameters()));
574 }
John McCall37bb6c92010-10-29 22:22:43 +0000575 }
576
John McCall37bb6c92010-10-29 22:22:43 +0000577 // Static data members.
578 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000579 // Modify the variable's linkage by its type, but ignore the
580 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000581 LinkageInfo TypeLV = getLVForType(VD->getType());
582 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000583 LV.mergeLinkage(UniqueExternalLinkage);
584 if (!LV.visibilityExplicit())
Rafael Espindola2dd5ed52012-04-17 18:47:20 +0000585 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000586 }
587
John McCall457a04e2010-10-22 21:05:15 +0000588 return LV;
John McCall8823c652010-08-13 08:35:10 +0000589}
590
John McCalld396b972011-02-08 19:01:05 +0000591static void clearLinkageForClass(const CXXRecordDecl *record) {
592 for (CXXRecordDecl::decl_iterator
593 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
594 Decl *child = *i;
595 if (isa<NamedDecl>(child))
596 cast<NamedDecl>(child)->ClearLinkageCache();
597 }
598}
599
David Blaikie68e081d2011-12-20 02:48:34 +0000600void NamedDecl::anchor() { }
601
John McCalld396b972011-02-08 19:01:05 +0000602void NamedDecl::ClearLinkageCache() {
603 // Note that we can't skip clearing the linkage of children just
604 // because the parent doesn't have cached linkage: we don't cache
605 // when computing linkage for parent contexts.
606
607 HasCachedLinkage = 0;
608
609 // If we're changing the linkage of a class, we need to reset the
610 // linkage of child declarations, too.
611 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
612 clearLinkageForClass(record);
613
John McCall83779672011-02-19 02:53:41 +0000614 if (ClassTemplateDecl *temp =
615 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000616 // Clear linkage for the template pattern.
617 CXXRecordDecl *record = temp->getTemplatedDecl();
618 record->HasCachedLinkage = 0;
619 clearLinkageForClass(record);
620
John McCall83779672011-02-19 02:53:41 +0000621 // We need to clear linkage for specializations, too.
622 for (ClassTemplateDecl::spec_iterator
623 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
624 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000625 }
John McCall83779672011-02-19 02:53:41 +0000626
627 // Clear cached linkage for function template decls, too.
628 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000629 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
630 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000631 for (FunctionTemplateDecl::spec_iterator
632 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
633 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000634 }
John McCall83779672011-02-19 02:53:41 +0000635
John McCalld396b972011-02-08 19:01:05 +0000636}
637
Douglas Gregorbf62d642010-12-06 18:36:25 +0000638Linkage NamedDecl::getLinkage() const {
639 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000640 assert(Linkage(CachedLinkage) ==
641 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000642 return Linkage(CachedLinkage);
643 }
644
645 CachedLinkage = getLVForDecl(this,
646 LVFlags::CreateOnlyDeclLinkage()).linkage();
647 HasCachedLinkage = 1;
648 return Linkage(CachedLinkage);
649}
650
John McCallc273f242010-10-30 11:50:40 +0000651LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000652 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000653 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000654 HasCachedLinkage = 1;
655 CachedLinkage = LI.linkage();
656 return LI;
John McCall033caa52010-10-29 00:29:13 +0000657}
Ted Kremenek926d8602010-04-20 23:15:35 +0000658
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000659llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
660 // Use the most recent declaration of a variable.
661 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000662 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000663
664 // Use the most recent declaration of a function, and also handle
665 // function template specializations.
666 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
667 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000668 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000669 return V;
670
671 // If the function is a specialization of a template with an
672 // explicit visibility attribute, use that.
673 if (FunctionTemplateSpecializationInfo *templateInfo
674 = fn->getTemplateSpecializationInfo())
675 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
676
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000677 // If the function is a member of a specialization of a class template
678 // and the corresponding decl has explicit visibility, use that.
679 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
680 if (InstantiatedFrom)
681 return getVisibilityOf(InstantiatedFrom);
682
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000683 return llvm::Optional<Visibility>();
684 }
685
686 // Otherwise, just check the declaration itself first.
687 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
688 return V;
689
690 // If there wasn't explicit visibility there, and this is a
691 // specialization of a class template, check for visibility
692 // on the pattern.
693 if (const ClassTemplateSpecializationDecl *spec
694 = dyn_cast<ClassTemplateSpecializationDecl>(this))
695 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
696
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000697 // If this is a member class of a specialization of a class template
698 // and the corresponding decl has explicit visibility, use that.
699 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
700 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
701 if (InstantiatedFrom)
702 return getVisibilityOf(InstantiatedFrom);
703 }
704
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000705 return llvm::Optional<Visibility>();
706}
707
John McCall07072662010-11-02 01:45:15 +0000708static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000709 // Objective-C: treat all Objective-C declarations as having external
710 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000711 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000712 default:
713 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000714 case Decl::ParmVar:
715 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000716 case Decl::TemplateTemplateParm: // count these as external
717 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000718 case Decl::ObjCAtDefsField:
719 case Decl::ObjCCategory:
720 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000721 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000722 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000723 case Decl::ObjCMethod:
724 case Decl::ObjCProperty:
725 case Decl::ObjCPropertyImpl:
726 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000727 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000728
729 case Decl::CXXRecord: {
730 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
731 if (Record->isLambda()) {
732 if (!Record->getLambdaManglingNumber()) {
733 // This lambda has no mangling number, so it's internal.
734 return LinkageInfo::internal();
735 }
736
737 // This lambda has its linkage/visibility determined by its owner.
738 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
739 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
740 if (isa<ParmVarDecl>(ContextDecl))
741 DC = ContextDecl->getDeclContext()->getRedeclContext();
742 else
743 return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags);
744 }
745
746 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
747 return getLVForDecl(ND, Flags);
748
749 return LinkageInfo::external();
750 }
751
752 break;
753 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000754 }
755
Douglas Gregorf73b2822009-11-25 22:24:25 +0000756 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000757 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000758 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000759
760 // C++ [basic.link]p5:
761 // In addition, a member function, static data member, a named
762 // class or enumeration of class scope, or an unnamed class or
763 // enumeration defined in a class-scope typedef declaration such
764 // that the class or enumeration has the typedef name for linkage
765 // purposes (7.1.3), has external linkage if the name of the class
766 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000767 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000768 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000769
770 // C++ [basic.link]p6:
771 // The name of a function declared in block scope and the name of
772 // an object declared by a block scope extern declaration have
773 // linkage. If there is a visible declaration of an entity with
774 // linkage having the same name and type, ignoring entities
775 // declared outside the innermost enclosing namespace scope, the
776 // block scope declaration declares that same entity and receives
777 // the linkage of the previous declaration. If there is more than
778 // one such matching entity, the program is ill-formed. Otherwise,
779 // if no matching entity is found, the block scope entity receives
780 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000781 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
782 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000783 if (Function->isInAnonymousNamespace() &&
784 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000785 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000786
John McCallc273f242010-10-30 11:50:40 +0000787 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000788 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000789 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000790 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000791 }
792
Douglas Gregorec9fd132012-01-14 16:38:05 +0000793 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000794 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000795 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
796 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000797 }
798
799 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000800 }
801
John McCall033caa52010-10-29 00:29:13 +0000802 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000803 if (Var->getStorageClass() == SC_Extern ||
804 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000805 if (Var->isInAnonymousNamespace() &&
806 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000807 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000808
John McCallc273f242010-10-30 11:50:40 +0000809 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000810 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000811 LV.mergeVisibility(HiddenVisibility, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000812 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000813 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000814 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000815 }
816
Douglas Gregorec9fd132012-01-14 16:38:05 +0000817 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000818 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000819 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
820 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000821 }
822
823 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000824 }
825 }
826
827 // C++ [basic.link]p6:
828 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000829 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000830}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000831
Douglas Gregor2ada0482009-02-04 17:27:36 +0000832std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000833 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000834}
835
836std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000837 const DeclContext *Ctx = getDeclContext();
838
839 if (Ctx->isFunctionOrMethod())
840 return getNameAsString();
841
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000842 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000843 ContextsTy Contexts;
844
845 // Collect contexts.
846 while (Ctx && isa<NamedDecl>(Ctx)) {
847 Contexts.push_back(Ctx);
848 Ctx = Ctx->getParent();
849 };
850
851 std::string QualName;
852 llvm::raw_string_ostream OS(QualName);
853
854 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
855 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000856 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000857 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000858 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
859 std::string TemplateArgsStr
860 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000861 TemplateArgs.data(),
862 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000863 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000864 OS << Spec->getName() << TemplateArgsStr;
865 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000866 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000867 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000868 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000869 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000870 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
871 if (!RD->getIdentifier())
872 OS << "<anonymous " << RD->getKindName() << '>';
873 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000874 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000875 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000876 const FunctionProtoType *FT = 0;
877 if (FD->hasWrittenPrototype())
878 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
879
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000880 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000881 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000882 unsigned NumParams = FD->getNumParams();
883 for (unsigned i = 0; i < NumParams; ++i) {
884 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000885 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000886 std::string Param;
887 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000888 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000889 }
890
891 if (FT->isVariadic()) {
892 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000893 OS << ", ";
894 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000895 }
896 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000897 OS << ')';
898 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000899 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000900 }
901 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000902 }
903
John McCalla2a3f7d2010-03-16 21:48:18 +0000904 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000905 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000906 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000907 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000908
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000909 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000910}
911
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000912bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000913 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
914
Douglas Gregor889ceb72009-02-03 19:21:40 +0000915 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
916 // We want to keep it, unless it nominates same namespace.
917 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000918 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
919 ->getOriginalNamespace() ==
920 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
921 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000922 }
Mike Stump11289f42009-09-09 15:08:12 +0000923
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000924 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
925 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000926 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000927
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000928 // For function templates, the underlying function declarations are linked.
929 if (const FunctionTemplateDecl *FunctionTemplate
930 = dyn_cast<FunctionTemplateDecl>(this))
931 if (const FunctionTemplateDecl *OldFunctionTemplate
932 = dyn_cast<FunctionTemplateDecl>(OldD))
933 return FunctionTemplate->getTemplatedDecl()
934 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000935
Steve Naroffc4173fa2009-02-22 19:35:57 +0000936 // For method declarations, we keep track of redeclarations.
937 if (isa<ObjCMethodDecl>(this))
938 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000939
John McCall9f3059a2009-10-09 21:13:30 +0000940 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
941 return true;
942
John McCall3f746822009-11-17 05:59:44 +0000943 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
944 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
945 cast<UsingShadowDecl>(OldD)->getTargetDecl();
946
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000947 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
948 ASTContext &Context = getASTContext();
949 return Context.getCanonicalNestedNameSpecifier(
950 cast<UsingDecl>(this)->getQualifier()) ==
951 Context.getCanonicalNestedNameSpecifier(
952 cast<UsingDecl>(OldD)->getQualifier());
953 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000954
Douglas Gregorb59643b2012-01-03 23:26:26 +0000955 // A typedef of an Objective-C class type can replace an Objective-C class
956 // declaration or definition, and vice versa.
957 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
958 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
959 return true;
960
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000961 // For non-function declarations, if the declarations are of the
962 // same kind then this must be a redeclaration, or semantic analysis
963 // would not have given us the new declaration.
964 return this->getKind() == OldD->getKind();
965}
966
Douglas Gregoreddf4332009-02-24 20:03:32 +0000967bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000968 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000969}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000970
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000971NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000972 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000973 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
974 ND = UD->getTargetDecl();
975
976 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
977 return AD->getClassInterface();
978
979 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000980}
981
John McCalla8ae2222010-04-06 21:38:20 +0000982bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000983 if (!isCXXClassMember())
984 return false;
985
John McCalla8ae2222010-04-06 21:38:20 +0000986 const NamedDecl *D = this;
987 if (isa<UsingShadowDecl>(D))
988 D = cast<UsingShadowDecl>(D)->getTargetDecl();
989
Francois Pichet783dd6e2010-11-21 06:08:52 +0000990 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000991 return true;
992 if (isa<CXXMethodDecl>(D))
993 return cast<CXXMethodDecl>(D)->isInstance();
994 if (isa<FunctionTemplateDecl>(D))
995 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
996 ->getTemplatedDecl())->isInstance();
997 return false;
998}
999
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001000//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001001// DeclaratorDecl Implementation
1002//===----------------------------------------------------------------------===//
1003
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001004template <typename DeclT>
1005static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1006 if (decl->getNumTemplateParameterLists() > 0)
1007 return decl->getTemplateParameterList(0)->getTemplateLoc();
1008 else
1009 return decl->getInnerLocStart();
1010}
1011
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001012SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001013 TypeSourceInfo *TSI = getTypeSourceInfo();
1014 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001015 return SourceLocation();
1016}
1017
Douglas Gregor14454802011-02-25 02:25:35 +00001018void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1019 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001020 // Make sure the extended decl info is allocated.
1021 if (!hasExtInfo()) {
1022 // Save (non-extended) type source info pointer.
1023 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1024 // Allocate external info struct.
1025 DeclInfo = new (getASTContext()) ExtInfo;
1026 // Restore savedTInfo into (extended) decl info.
1027 getExtInfo()->TInfo = savedTInfo;
1028 }
1029 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001030 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001031 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001032 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001033 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001034 if (getExtInfo()->NumTemplParamLists == 0) {
1035 // Save type source info pointer.
1036 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1037 // Deallocate the extended decl info.
1038 getASTContext().Deallocate(getExtInfo());
1039 // Restore savedTInfo into (non-extended) decl info.
1040 DeclInfo = savedTInfo;
1041 }
1042 else
1043 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001044 }
1045 }
1046}
1047
Abramo Bagnara60804e12011-03-18 15:16:37 +00001048void
1049DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1050 unsigned NumTPLists,
1051 TemplateParameterList **TPLists) {
1052 assert(NumTPLists > 0);
1053 // Make sure the extended decl info is allocated.
1054 if (!hasExtInfo()) {
1055 // Save (non-extended) type source info pointer.
1056 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1057 // Allocate external info struct.
1058 DeclInfo = new (getASTContext()) ExtInfo;
1059 // Restore savedTInfo into (extended) decl info.
1060 getExtInfo()->TInfo = savedTInfo;
1061 }
1062 // Set the template parameter lists info.
1063 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1064}
1065
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001066SourceLocation DeclaratorDecl::getOuterLocStart() const {
1067 return getTemplateOrInnerLocStart(this);
1068}
1069
Abramo Bagnaraea947882011-03-08 16:41:52 +00001070namespace {
1071
1072// Helper function: returns true if QT is or contains a type
1073// having a postfix component.
1074bool typeIsPostfix(clang::QualType QT) {
1075 while (true) {
1076 const Type* T = QT.getTypePtr();
1077 switch (T->getTypeClass()) {
1078 default:
1079 return false;
1080 case Type::Pointer:
1081 QT = cast<PointerType>(T)->getPointeeType();
1082 break;
1083 case Type::BlockPointer:
1084 QT = cast<BlockPointerType>(T)->getPointeeType();
1085 break;
1086 case Type::MemberPointer:
1087 QT = cast<MemberPointerType>(T)->getPointeeType();
1088 break;
1089 case Type::LValueReference:
1090 case Type::RValueReference:
1091 QT = cast<ReferenceType>(T)->getPointeeType();
1092 break;
1093 case Type::PackExpansion:
1094 QT = cast<PackExpansionType>(T)->getPattern();
1095 break;
1096 case Type::Paren:
1097 case Type::ConstantArray:
1098 case Type::DependentSizedArray:
1099 case Type::IncompleteArray:
1100 case Type::VariableArray:
1101 case Type::FunctionProto:
1102 case Type::FunctionNoProto:
1103 return true;
1104 }
1105 }
1106}
1107
1108} // namespace
1109
1110SourceRange DeclaratorDecl::getSourceRange() const {
1111 SourceLocation RangeEnd = getLocation();
1112 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1113 if (typeIsPostfix(TInfo->getType()))
1114 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1115 }
1116 return SourceRange(getOuterLocStart(), RangeEnd);
1117}
1118
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001119void
Douglas Gregor20527e22010-06-15 17:44:38 +00001120QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1121 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001122 TemplateParameterList **TPLists) {
1123 assert((NumTPLists == 0 || TPLists != 0) &&
1124 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001125
1126 // Free previous template parameters (if any).
1127 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001128 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001129 TemplParamLists = 0;
1130 NumTemplParamLists = 0;
1131 }
1132 // Set info on matched template parameter lists (if any).
1133 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001134 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001135 NumTemplParamLists = NumTPLists;
1136 for (unsigned i = NumTPLists; i-- > 0; )
1137 TemplParamLists[i] = TPLists[i];
1138 }
1139}
1140
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001141//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001142// VarDecl Implementation
1143//===----------------------------------------------------------------------===//
1144
Sebastian Redl833ef452010-01-26 22:01:41 +00001145const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1146 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001147 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001148 case SC_Auto: return "auto";
1149 case SC_Extern: return "extern";
1150 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1151 case SC_PrivateExtern: return "__private_extern__";
1152 case SC_Register: return "register";
1153 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001154 }
1155
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001156 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001157}
1158
Abramo Bagnaradff19302011-03-08 08:55:46 +00001159VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1160 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001161 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001162 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001163 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001164}
1165
Douglas Gregor72172e92012-01-05 21:55:30 +00001166VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1167 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1168 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1169 QualType(), 0, SC_None, SC_None);
1170}
1171
Douglas Gregorbf62d642010-12-06 18:36:25 +00001172void VarDecl::setStorageClass(StorageClass SC) {
1173 assert(isLegalForVariable(SC));
1174 if (getStorageClass() != SC)
1175 ClearLinkageCache();
1176
John McCallbeaa11c2011-05-01 02:13:58 +00001177 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001178}
1179
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001180SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001181 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001182 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001183 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001184}
1185
Sebastian Redl833ef452010-01-26 22:01:41 +00001186bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001187 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001188 return false;
1189
Eli Friedman839192f2012-01-15 01:23:58 +00001190 const DeclContext *DC = getDeclContext();
1191 if (DC->isRecord())
1192 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001193
Eli Friedman839192f2012-01-15 01:23:58 +00001194 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001195 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001196 return true;
1197 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001198}
1199
1200VarDecl *VarDecl::getCanonicalDecl() {
1201 return getFirstDeclaration();
1202}
1203
Daniel Dunbar9d355812012-03-09 01:51:51 +00001204VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1205 ASTContext &C) const
1206{
Sebastian Redl35351a92010-01-31 22:27:38 +00001207 // C++ [basic.def]p2:
1208 // A declaration is a definition unless [...] it contains the 'extern'
1209 // specifier or a linkage-specification and neither an initializer [...],
1210 // it declares a static data member in a class declaration [...].
1211 // C++ [temp.expl.spec]p15:
1212 // An explicit specialization of a static data member of a template is a
1213 // definition if the declaration includes an initializer; otherwise, it is
1214 // a declaration.
1215 if (isStaticDataMember()) {
1216 if (isOutOfLine() && (hasInit() ||
1217 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1218 return Definition;
1219 else
1220 return DeclarationOnly;
1221 }
1222 // C99 6.7p5:
1223 // A definition of an identifier is a declaration for that identifier that
1224 // [...] causes storage to be reserved for that object.
1225 // Note: that applies for all non-file-scope objects.
1226 // C99 6.9.2p1:
1227 // If the declaration of an identifier for an object has file scope and an
1228 // initializer, the declaration is an external definition for the identifier
1229 if (hasInit())
1230 return Definition;
1231 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1232 if (hasExternalStorage())
1233 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001234
John McCall8e7d6562010-08-26 03:08:43 +00001235 if (getStorageClassAsWritten() == SC_Extern ||
1236 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001237 for (const VarDecl *PrevVar = getPreviousDecl();
1238 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001239 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1240 return DeclarationOnly;
1241 }
1242 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001243 // C99 6.9.2p2:
1244 // A declaration of an object that has file scope without an initializer,
1245 // and without a storage class specifier or the scs 'static', constitutes
1246 // a tentative definition.
1247 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001248 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001249 return TentativeDefinition;
1250
1251 // What's left is (in C, block-scope) declarations without initializers or
1252 // external storage. These are definitions.
1253 return Definition;
1254}
1255
Sebastian Redl35351a92010-01-31 22:27:38 +00001256VarDecl *VarDecl::getActingDefinition() {
1257 DefinitionKind Kind = isThisDeclarationADefinition();
1258 if (Kind != TentativeDefinition)
1259 return 0;
1260
Chris Lattner48eb14d2010-06-14 18:31:46 +00001261 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001262 VarDecl *First = getFirstDeclaration();
1263 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1264 I != E; ++I) {
1265 Kind = (*I)->isThisDeclarationADefinition();
1266 if (Kind == Definition)
1267 return 0;
1268 else if (Kind == TentativeDefinition)
1269 LastTentative = *I;
1270 }
1271 return LastTentative;
1272}
1273
1274bool VarDecl::isTentativeDefinitionNow() const {
1275 DefinitionKind Kind = isThisDeclarationADefinition();
1276 if (Kind != TentativeDefinition)
1277 return false;
1278
1279 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1280 if ((*I)->isThisDeclarationADefinition() == Definition)
1281 return false;
1282 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001283 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001284}
1285
Daniel Dunbar9d355812012-03-09 01:51:51 +00001286VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001287 VarDecl *First = getFirstDeclaration();
1288 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1289 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001290 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001291 return *I;
1292 }
1293 return 0;
1294}
1295
Daniel Dunbar9d355812012-03-09 01:51:51 +00001296VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001297 DefinitionKind Kind = DeclarationOnly;
1298
1299 const VarDecl *First = getFirstDeclaration();
1300 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001301 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001302 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001303 if (Kind == Definition)
1304 break;
1305 }
John McCall37bb6c92010-10-29 22:22:43 +00001306
1307 return Kind;
1308}
1309
Sebastian Redl5ca79842010-02-01 20:16:42 +00001310const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001311 redecl_iterator I = redecls_begin(), E = redecls_end();
1312 while (I != E && !I->getInit())
1313 ++I;
1314
1315 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001316 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001317 return I->getInit();
1318 }
1319 return 0;
1320}
1321
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001322bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001323 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001324 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001325
1326 if (!isStaticDataMember())
1327 return false;
1328
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001329 // If this static data member was instantiated from a static data member of
1330 // a class template, check whether that static data member was defined
1331 // out-of-line.
1332 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1333 return VD->isOutOfLine();
1334
1335 return false;
1336}
1337
Douglas Gregor1d957a32009-10-27 18:42:08 +00001338VarDecl *VarDecl::getOutOfLineDefinition() {
1339 if (!isStaticDataMember())
1340 return 0;
1341
1342 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1343 RD != RDEnd; ++RD) {
1344 if (RD->getLexicalDeclContext()->isFileContext())
1345 return *RD;
1346 }
1347
1348 return 0;
1349}
1350
Douglas Gregord5058122010-02-11 01:19:42 +00001351void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001352 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1353 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001354 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001355 }
1356
1357 Init = I;
1358}
1359
Daniel Dunbar9d355812012-03-09 01:51:51 +00001360bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001361 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001362
Richard Smith35ecb362012-03-02 04:14:40 +00001363 if (!Lang.CPlusPlus)
1364 return false;
1365
1366 // In C++11, any variable of reference type can be used in a constant
1367 // expression if it is initialized by a constant expression.
1368 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1369 return true;
1370
1371 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001372 // not require the variable to be non-volatile, but we consider this to be a
1373 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001374 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001375 return false;
1376
1377 // In C++, const, non-volatile variables of integral or enumeration types
1378 // can be used in constant expressions.
1379 if (getType()->isIntegralOrEnumerationType())
1380 return true;
1381
Richard Smith35ecb362012-03-02 04:14:40 +00001382 // Additionally, in C++11, non-volatile constexpr variables can be used in
1383 // constant expressions.
1384 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001385}
1386
Richard Smithd0b4dd62011-12-19 06:19:21 +00001387/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1388/// form, which contains extra information on the evaluated value of the
1389/// initializer.
1390EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1391 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1392 if (!Eval) {
1393 Stmt *S = Init.get<Stmt *>();
1394 Eval = new (getASTContext()) EvaluatedStmt;
1395 Eval->Value = S;
1396 Init = Eval;
1397 }
1398 return Eval;
1399}
1400
Richard Smithdafff942012-01-14 04:30:29 +00001401APValue *VarDecl::evaluateValue() const {
1402 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1403 return evaluateValue(Notes);
1404}
1405
1406APValue *VarDecl::evaluateValue(
1407 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001408 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1409
1410 // We only produce notes indicating why an initializer is non-constant the
1411 // first time it is evaluated. FIXME: The notes won't always be emitted the
1412 // first time we try evaluation, so might not be produced at all.
1413 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001414 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001415
1416 const Expr *Init = cast<Expr>(Eval->Value);
1417 assert(!Init->isValueDependent());
1418
1419 if (Eval->IsEvaluating) {
1420 // FIXME: Produce a diagnostic for self-initialization.
1421 Eval->CheckedICE = true;
1422 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001423 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001424 }
1425
1426 Eval->IsEvaluating = true;
1427
1428 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1429 this, Notes);
1430
1431 // Ensure the result is an uninitialized APValue if evaluation fails.
1432 if (!Result)
1433 Eval->Evaluated = APValue();
1434
1435 Eval->IsEvaluating = false;
1436 Eval->WasEvaluated = true;
1437
1438 // In C++11, we have determined whether the initializer was a constant
1439 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001440 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001441 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001442 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001443 }
1444
Richard Smithdafff942012-01-14 04:30:29 +00001445 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001446}
1447
1448bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001449 // Initializers of weak variables are never ICEs.
1450 if (isWeak())
1451 return false;
1452
Richard Smithd0b4dd62011-12-19 06:19:21 +00001453 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1454 if (Eval->CheckedICE)
1455 // We have already checked whether this subexpression is an
1456 // integral constant expression.
1457 return Eval->IsICE;
1458
1459 const Expr *Init = cast<Expr>(Eval->Value);
1460 assert(!Init->isValueDependent());
1461
1462 // In C++11, evaluate the initializer to check whether it's a constant
1463 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001464 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001465 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1466 evaluateValue(Notes);
1467 return Eval->IsICE;
1468 }
1469
1470 // It's an ICE whether or not the definition we found is
1471 // out-of-line. See DR 721 and the discussion in Clang PR
1472 // 6206 for details.
1473
1474 if (Eval->CheckingICE)
1475 return false;
1476 Eval->CheckingICE = true;
1477
1478 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1479 Eval->CheckingICE = false;
1480 Eval->CheckedICE = true;
1481 return Eval->IsICE;
1482}
1483
Douglas Gregorfe314812011-06-21 17:03:29 +00001484bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001485 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001486
1487 const Expr *E = getInit();
1488 if (!E)
1489 return false;
1490
1491 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1492 E = Cleanups->getSubExpr();
1493
1494 return isa<MaterializeTemporaryExpr>(E);
1495}
1496
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001497VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001498 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001499 return cast<VarDecl>(MSI->getInstantiatedFrom());
1500
1501 return 0;
1502}
1503
Douglas Gregor3c74d412009-10-14 20:14:33 +00001504TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001505 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001506 return MSI->getTemplateSpecializationKind();
1507
1508 return TSK_Undeclared;
1509}
1510
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001511MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001512 return getASTContext().getInstantiatedFromStaticDataMember(this);
1513}
1514
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001515void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1516 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001517 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001518 assert(MSI && "Not an instantiated static data member?");
1519 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001520 if (TSK != TSK_ExplicitSpecialization &&
1521 PointOfInstantiation.isValid() &&
1522 MSI->getPointOfInstantiation().isInvalid())
1523 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001524}
1525
Sebastian Redl833ef452010-01-26 22:01:41 +00001526//===----------------------------------------------------------------------===//
1527// ParmVarDecl Implementation
1528//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001529
Sebastian Redl833ef452010-01-26 22:01:41 +00001530ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001531 SourceLocation StartLoc,
1532 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001533 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001534 StorageClass S, StorageClass SCAsWritten,
1535 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001536 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001537 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001538}
1539
Douglas Gregor72172e92012-01-05 21:55:30 +00001540ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1541 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1542 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1543 0, QualType(), 0, SC_None, SC_None, 0);
1544}
1545
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001546SourceRange ParmVarDecl::getSourceRange() const {
1547 if (!hasInheritedDefaultArg()) {
1548 SourceRange ArgRange = getDefaultArgRange();
1549 if (ArgRange.isValid())
1550 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1551 }
1552
1553 return DeclaratorDecl::getSourceRange();
1554}
1555
Sebastian Redl833ef452010-01-26 22:01:41 +00001556Expr *ParmVarDecl::getDefaultArg() {
1557 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1558 assert(!hasUninstantiatedDefaultArg() &&
1559 "Default argument is not yet instantiated!");
1560
1561 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001562 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001563 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001564
Sebastian Redl833ef452010-01-26 22:01:41 +00001565 return Arg;
1566}
1567
Sebastian Redl833ef452010-01-26 22:01:41 +00001568SourceRange ParmVarDecl::getDefaultArgRange() const {
1569 if (const Expr *E = getInit())
1570 return E->getSourceRange();
1571
1572 if (hasUninstantiatedDefaultArg())
1573 return getUninstantiatedDefaultArg()->getSourceRange();
1574
1575 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001576}
1577
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001578bool ParmVarDecl::isParameterPack() const {
1579 return isa<PackExpansionType>(getType());
1580}
1581
Ted Kremenek540017e2011-10-06 05:00:56 +00001582void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1583 getASTContext().setParameterIndex(this, parameterIndex);
1584 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1585}
1586
1587unsigned ParmVarDecl::getParameterIndexLarge() const {
1588 return getASTContext().getParameterIndex(this);
1589}
1590
Nuno Lopes394ec982008-12-17 23:39:55 +00001591//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001592// FunctionDecl Implementation
1593//===----------------------------------------------------------------------===//
1594
Douglas Gregorb11aad82011-02-19 18:51:44 +00001595void FunctionDecl::getNameForDiagnostic(std::string &S,
1596 const PrintingPolicy &Policy,
1597 bool Qualified) const {
1598 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1599 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1600 if (TemplateArgs)
1601 S += TemplateSpecializationType::PrintTemplateArgumentList(
1602 TemplateArgs->data(),
1603 TemplateArgs->size(),
1604 Policy);
1605
1606}
1607
Ted Kremenek186a0742010-04-29 16:49:01 +00001608bool FunctionDecl::isVariadic() const {
1609 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1610 return FT->isVariadic();
1611 return false;
1612}
1613
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001614bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1615 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001616 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001617 Definition = *I;
1618 return true;
1619 }
1620 }
1621
1622 return false;
1623}
1624
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001625bool FunctionDecl::hasTrivialBody() const
1626{
1627 Stmt *S = getBody();
1628 if (!S) {
1629 // Since we don't have a body for this function, we don't know if it's
1630 // trivial or not.
1631 return false;
1632 }
1633
1634 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1635 return true;
1636 return false;
1637}
1638
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001639bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1640 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001641 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001642 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1643 return true;
1644 }
1645 }
1646
1647 return false;
1648}
1649
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001650Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001651 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1652 if (I->Body) {
1653 Definition = *I;
1654 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001655 } else if (I->IsLateTemplateParsed) {
1656 Definition = *I;
1657 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001658 }
1659 }
1660
1661 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001662}
1663
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001664void FunctionDecl::setBody(Stmt *B) {
1665 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001666 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001667 EndRangeLoc = B->getLocEnd();
1668}
1669
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001670void FunctionDecl::setPure(bool P) {
1671 IsPure = P;
1672 if (P)
1673 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1674 Parent->markedVirtualFunctionPure();
1675}
1676
Douglas Gregor16618f22009-09-12 00:17:51 +00001677bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001678 const TranslationUnitDecl *tunit =
1679 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1680 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001681 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001682 getIdentifier() &&
1683 getIdentifier()->isStr("main");
1684}
1685
1686bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1687 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1688 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1689 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1690 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1691 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1692
1693 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1694 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1695
1696 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1697 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1698
1699 ASTContext &Context =
1700 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1701 ->getASTContext();
1702
1703 // The result type and first argument type are constant across all
1704 // these operators. The second argument must be exactly void*.
1705 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001706}
1707
Douglas Gregor16618f22009-09-12 00:17:51 +00001708bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001709 if (getLinkage() != ExternalLinkage)
1710 return false;
1711
1712 if (getAttr<OverloadableAttr>())
1713 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001714
Chandler Carruth4322a282011-02-25 00:05:02 +00001715 const DeclContext *DC = getDeclContext();
1716 if (DC->isRecord())
1717 return false;
1718
Eli Friedman839192f2012-01-15 01:23:58 +00001719 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001720 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001721 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001722
Eli Friedman839192f2012-01-15 01:23:58 +00001723 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001724}
1725
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001726bool FunctionDecl::isGlobal() const {
1727 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1728 return Method->isStatic();
1729
John McCall8e7d6562010-08-26 03:08:43 +00001730 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001731 return false;
1732
Mike Stump11289f42009-09-09 15:08:12 +00001733 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001734 DC->isNamespace();
1735 DC = DC->getParent()) {
1736 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1737 if (!Namespace->getDeclName())
1738 return false;
1739 break;
1740 }
1741 }
1742
1743 return true;
1744}
1745
Sebastian Redl833ef452010-01-26 22:01:41 +00001746void
1747FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1748 redeclarable_base::setPreviousDeclaration(PrevDecl);
1749
1750 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1751 FunctionTemplateDecl *PrevFunTmpl
1752 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1753 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1754 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1755 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001756
Axel Naumannfbc7b982011-11-08 18:21:06 +00001757 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001758 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001759}
1760
1761const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1762 return getFirstDeclaration();
1763}
1764
1765FunctionDecl *FunctionDecl::getCanonicalDecl() {
1766 return getFirstDeclaration();
1767}
1768
Douglas Gregorbf62d642010-12-06 18:36:25 +00001769void FunctionDecl::setStorageClass(StorageClass SC) {
1770 assert(isLegalForFunction(SC));
1771 if (getStorageClass() != SC)
1772 ClearLinkageCache();
1773
1774 SClass = SC;
1775}
1776
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001777/// \brief Returns a value indicating whether this function
1778/// corresponds to a builtin function.
1779///
1780/// The function corresponds to a built-in function if it is
1781/// declared at translation scope or within an extern "C" block and
1782/// its name matches with the name of a builtin. The returned value
1783/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001784/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001785/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001786unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001787 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001788 return 0;
1789
1790 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001791 if (!BuiltinID)
1792 return 0;
1793
1794 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001795 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1796 return BuiltinID;
1797
1798 // This function has the name of a known C library
1799 // function. Determine whether it actually refers to the C library
1800 // function or whether it just has the same name.
1801
Douglas Gregora908e7f2009-02-17 03:23:10 +00001802 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001803 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001804 return 0;
1805
Douglas Gregore711f702009-02-14 18:57:46 +00001806 // If this function is at translation-unit scope and we're not in
1807 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001808 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001809 getDeclContext()->isTranslationUnit())
1810 return BuiltinID;
1811
1812 // If the function is in an extern "C" linkage specification and is
1813 // not marked "overloadable", it's the real function.
1814 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001815 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001816 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001817 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001818 return BuiltinID;
1819
1820 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001821 return 0;
1822}
1823
1824
Chris Lattner47c0d002009-04-25 06:03:53 +00001825/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001826/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001827/// after it has been created.
1828unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001829 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001830 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001831 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001832 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001833
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001834}
1835
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001836void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001837 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001838 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001839 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001840
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001841 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001842 if (!NewParamInfo.empty()) {
1843 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1844 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001845 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001846}
Chris Lattner41943152007-01-25 04:52:46 +00001847
James Molloy6f8780b2012-02-29 10:24:19 +00001848void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1849 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1850
1851 if (!NewDecls.empty()) {
1852 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1853 std::copy(NewDecls.begin(), NewDecls.end(), A);
1854 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1855 }
1856}
1857
Chris Lattner58258242008-04-10 02:22:51 +00001858/// getMinRequiredArguments - Returns the minimum number of arguments
1859/// needed to call this function. This may be fewer than the number of
1860/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001861/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001862unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001863 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001864 return getNumParams();
1865
Douglas Gregor7825bf32011-01-06 22:09:01 +00001866 unsigned NumRequiredArgs = getNumParams();
1867
1868 // If the last parameter is a parameter pack, we don't need an argument for
1869 // it.
1870 if (NumRequiredArgs > 0 &&
1871 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1872 --NumRequiredArgs;
1873
1874 // If this parameter has a default argument, we don't need an argument for
1875 // it.
1876 while (NumRequiredArgs > 0 &&
1877 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001878 --NumRequiredArgs;
1879
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001880 // We might have parameter packs before the end. These can't be deduced,
1881 // but they can still handle multiple arguments.
1882 unsigned ArgIdx = NumRequiredArgs;
1883 while (ArgIdx > 0) {
1884 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1885 NumRequiredArgs = ArgIdx;
1886
1887 --ArgIdx;
1888 }
1889
Chris Lattner58258242008-04-10 02:22:51 +00001890 return NumRequiredArgs;
1891}
1892
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001893bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001894 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001895 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001896
1897 if (isa<CXXMethodDecl>(this)) {
1898 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1899 return true;
1900 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001901
1902 switch (getTemplateSpecializationKind()) {
1903 case TSK_Undeclared:
1904 case TSK_ExplicitSpecialization:
1905 return false;
1906
1907 case TSK_ImplicitInstantiation:
1908 case TSK_ExplicitInstantiationDeclaration:
1909 case TSK_ExplicitInstantiationDefinition:
1910 // Handle below.
1911 break;
1912 }
1913
1914 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001915 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001916 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001917 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001918
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001919 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001920 return PatternDecl->isInlined();
1921
1922 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001923}
1924
Eli Friedman1b125c32012-02-07 03:50:18 +00001925static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1926 // Only consider file-scope declarations in this test.
1927 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1928 return false;
1929
1930 // Only consider explicit declarations; the presence of a builtin for a
1931 // libcall shouldn't affect whether a definition is externally visible.
1932 if (Redecl->isImplicit())
1933 return false;
1934
1935 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1936 return true; // Not an inline definition
1937
1938 return false;
1939}
1940
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001941/// \brief For a function declaration in C or C++, determine whether this
1942/// declaration causes the definition to be externally visible.
1943///
Eli Friedman1b125c32012-02-07 03:50:18 +00001944/// Specifically, this determines if adding the current declaration to the set
1945/// of redeclarations of the given functions causes
1946/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001947bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1948 assert(!doesThisDeclarationHaveABody() &&
1949 "Must have a declaration without a body.");
1950
1951 ASTContext &Context = getASTContext();
1952
David Blaikiebbafb8a2012-03-11 07:00:24 +00001953 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001954 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1955 // an externally visible definition.
1956 //
1957 // FIXME: What happens if gnu_inline gets added on after the first
1958 // declaration?
1959 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1960 return false;
1961
1962 const FunctionDecl *Prev = this;
1963 bool FoundBody = false;
1964 while ((Prev = Prev->getPreviousDecl())) {
1965 FoundBody |= Prev->Body;
1966
1967 if (Prev->Body) {
1968 // If it's not the case that both 'inline' and 'extern' are
1969 // specified on the definition, then it is always externally visible.
1970 if (!Prev->isInlineSpecified() ||
1971 Prev->getStorageClassAsWritten() != SC_Extern)
1972 return false;
1973 } else if (Prev->isInlineSpecified() &&
1974 Prev->getStorageClassAsWritten() != SC_Extern) {
1975 return false;
1976 }
1977 }
1978 return FoundBody;
1979 }
1980
David Blaikiebbafb8a2012-03-11 07:00:24 +00001981 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001982 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001983
1984 // C99 6.7.4p6:
1985 // [...] If all of the file scope declarations for a function in a
1986 // translation unit include the inline function specifier without extern,
1987 // then the definition in that translation unit is an inline definition.
1988 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001989 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001990 const FunctionDecl *Prev = this;
1991 bool FoundBody = false;
1992 while ((Prev = Prev->getPreviousDecl())) {
1993 FoundBody |= Prev->Body;
1994 if (RedeclForcesDefC99(Prev))
1995 return false;
1996 }
1997 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001998}
1999
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002000/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002001/// definition will be externally visible.
2002///
2003/// Inline function definitions are always available for inlining optimizations.
2004/// However, depending on the language dialect, declaration specifiers, and
2005/// attributes, the definition of an inline function may or may not be
2006/// "externally" visible to other translation units in the program.
2007///
2008/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002009/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002010/// inline definition becomes externally visible (C99 6.7.4p6).
2011///
2012/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2013/// definition, we use the GNU semantics for inline, which are nearly the
2014/// opposite of C99 semantics. In particular, "inline" by itself will create
2015/// an externally visible symbol, but "extern inline" will not create an
2016/// externally visible symbol.
2017bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002018 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002019 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002020 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002021
David Blaikiebbafb8a2012-03-11 07:00:24 +00002022 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002023 // Note: If you change the logic here, please change
2024 // doesDeclarationForceExternallyVisibleDefinition as well.
2025 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002026 // If it's not the case that both 'inline' and 'extern' are
2027 // specified on the definition, then this inline definition is
2028 // externally visible.
2029 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2030 return true;
2031
2032 // If any declaration is 'inline' but not 'extern', then this definition
2033 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002034 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2035 Redecl != RedeclEnd;
2036 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002037 if (Redecl->isInlineSpecified() &&
2038 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002039 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002040 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002041
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002042 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002043 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002044
Douglas Gregor299d76e2009-09-13 07:46:26 +00002045 // C99 6.7.4p6:
2046 // [...] If all of the file scope declarations for a function in a
2047 // translation unit include the inline function specifier without extern,
2048 // then the definition in that translation unit is an inline definition.
2049 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2050 Redecl != RedeclEnd;
2051 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002052 if (RedeclForcesDefC99(*Redecl))
2053 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002054 }
2055
2056 // C99 6.7.4p6:
2057 // An inline definition does not provide an external definition for the
2058 // function, and does not forbid an external definition in another
2059 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002060 return false;
2061}
2062
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002063/// getOverloadedOperator - Which C++ overloaded operator this
2064/// function represents, if any.
2065OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002066 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2067 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002068 else
2069 return OO_None;
2070}
2071
Alexis Huntc88db062010-01-13 09:01:02 +00002072/// getLiteralIdentifier - The literal suffix identifier this function
2073/// represents, if any.
2074const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2075 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2076 return getDeclName().getCXXLiteralIdentifier();
2077 else
2078 return 0;
2079}
2080
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002081FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2082 if (TemplateOrSpecialization.isNull())
2083 return TK_NonTemplate;
2084 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2085 return TK_FunctionTemplate;
2086 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2087 return TK_MemberSpecialization;
2088 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2089 return TK_FunctionTemplateSpecialization;
2090 if (TemplateOrSpecialization.is
2091 <DependentFunctionTemplateSpecializationInfo*>())
2092 return TK_DependentFunctionTemplateSpecialization;
2093
David Blaikie83d382b2011-09-23 05:06:16 +00002094 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002095}
2096
Douglas Gregord801b062009-10-07 23:56:10 +00002097FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002098 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002099 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2100
2101 return 0;
2102}
2103
Douglas Gregor06db9f52009-10-12 20:18:28 +00002104MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2105 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2106}
2107
Douglas Gregord801b062009-10-07 23:56:10 +00002108void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002109FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2110 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002111 TemplateSpecializationKind TSK) {
2112 assert(TemplateOrSpecialization.isNull() &&
2113 "Member function is already a specialization");
2114 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002115 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002116 TemplateOrSpecialization = Info;
2117}
2118
Douglas Gregorafca3b42009-10-27 20:53:28 +00002119bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002120 // If the function is invalid, it can't be implicitly instantiated.
2121 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002122 return false;
2123
2124 switch (getTemplateSpecializationKind()) {
2125 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002126 case TSK_ExplicitInstantiationDefinition:
2127 return false;
2128
2129 case TSK_ImplicitInstantiation:
2130 return true;
2131
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002132 // It is possible to instantiate TSK_ExplicitSpecialization kind
2133 // if the FunctionDecl has a class scope specialization pattern.
2134 case TSK_ExplicitSpecialization:
2135 return getClassScopeSpecializationPattern() != 0;
2136
Douglas Gregorafca3b42009-10-27 20:53:28 +00002137 case TSK_ExplicitInstantiationDeclaration:
2138 // Handled below.
2139 break;
2140 }
2141
2142 // Find the actual template from which we will instantiate.
2143 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002144 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002145 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002146 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002147
2148 // C++0x [temp.explicit]p9:
2149 // Except for inline functions, other explicit instantiation declarations
2150 // have the effect of suppressing the implicit instantiation of the entity
2151 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002152 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002153 return true;
2154
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002155 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002156}
2157
2158bool FunctionDecl::isTemplateInstantiation() const {
2159 switch (getTemplateSpecializationKind()) {
2160 case TSK_Undeclared:
2161 case TSK_ExplicitSpecialization:
2162 return false;
2163 case TSK_ImplicitInstantiation:
2164 case TSK_ExplicitInstantiationDeclaration:
2165 case TSK_ExplicitInstantiationDefinition:
2166 return true;
2167 }
2168 llvm_unreachable("All TSK values handled.");
2169}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002170
2171FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002172 // Handle class scope explicit specialization special case.
2173 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2174 return getClassScopeSpecializationPattern();
2175
Douglas Gregorafca3b42009-10-27 20:53:28 +00002176 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2177 while (Primary->getInstantiatedFromMemberTemplate()) {
2178 // If we have hit a point where the user provided a specialization of
2179 // this template, we're done looking.
2180 if (Primary->isMemberSpecialization())
2181 break;
2182
2183 Primary = Primary->getInstantiatedFromMemberTemplate();
2184 }
2185
2186 return Primary->getTemplatedDecl();
2187 }
2188
2189 return getInstantiatedFromMemberFunction();
2190}
2191
Douglas Gregor70d83e22009-06-29 17:30:29 +00002192FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002193 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002194 = TemplateOrSpecialization
2195 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002196 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002197 }
2198 return 0;
2199}
2200
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002201FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2202 return getASTContext().getClassScopeSpecializationPattern(this);
2203}
2204
Douglas Gregor70d83e22009-06-29 17:30:29 +00002205const TemplateArgumentList *
2206FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002207 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002208 = TemplateOrSpecialization
2209 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002210 return Info->TemplateArguments;
2211 }
2212 return 0;
2213}
2214
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002215const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002216FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2217 if (FunctionTemplateSpecializationInfo *Info
2218 = TemplateOrSpecialization
2219 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2220 return Info->TemplateArgumentsAsWritten;
2221 }
2222 return 0;
2223}
2224
Mike Stump11289f42009-09-09 15:08:12 +00002225void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002226FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2227 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002228 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002229 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002230 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002231 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2232 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002233 assert(TSK != TSK_Undeclared &&
2234 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002235 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002236 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002237 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002238 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2239 TemplateArgs,
2240 TemplateArgsAsWritten,
2241 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002242 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002243 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002244}
2245
John McCallb9c78482010-04-08 09:05:18 +00002246void
2247FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2248 const UnresolvedSetImpl &Templates,
2249 const TemplateArgumentListInfo &TemplateArgs) {
2250 assert(TemplateOrSpecialization.isNull());
2251 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2252 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002253 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002254 void *Buffer = Context.Allocate(Size);
2255 DependentFunctionTemplateSpecializationInfo *Info =
2256 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2257 TemplateArgs);
2258 TemplateOrSpecialization = Info;
2259}
2260
2261DependentFunctionTemplateSpecializationInfo::
2262DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2263 const TemplateArgumentListInfo &TArgs)
2264 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2265
2266 d.NumTemplates = Ts.size();
2267 d.NumArgs = TArgs.size();
2268
2269 FunctionTemplateDecl **TsArray =
2270 const_cast<FunctionTemplateDecl**>(getTemplates());
2271 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2272 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2273
2274 TemplateArgumentLoc *ArgsArray =
2275 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2276 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2277 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2278}
2279
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002280TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002281 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002282 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002283 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002284 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002285 if (FTSInfo)
2286 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002287
Douglas Gregord801b062009-10-07 23:56:10 +00002288 MemberSpecializationInfo *MSInfo
2289 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2290 if (MSInfo)
2291 return MSInfo->getTemplateSpecializationKind();
2292
2293 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002294}
2295
Mike Stump11289f42009-09-09 15:08:12 +00002296void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002297FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2298 SourceLocation PointOfInstantiation) {
2299 if (FunctionTemplateSpecializationInfo *FTSInfo
2300 = TemplateOrSpecialization.dyn_cast<
2301 FunctionTemplateSpecializationInfo*>()) {
2302 FTSInfo->setTemplateSpecializationKind(TSK);
2303 if (TSK != TSK_ExplicitSpecialization &&
2304 PointOfInstantiation.isValid() &&
2305 FTSInfo->getPointOfInstantiation().isInvalid())
2306 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2307 } else if (MemberSpecializationInfo *MSInfo
2308 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2309 MSInfo->setTemplateSpecializationKind(TSK);
2310 if (TSK != TSK_ExplicitSpecialization &&
2311 PointOfInstantiation.isValid() &&
2312 MSInfo->getPointOfInstantiation().isInvalid())
2313 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2314 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002315 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002316}
2317
2318SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002319 if (FunctionTemplateSpecializationInfo *FTSInfo
2320 = TemplateOrSpecialization.dyn_cast<
2321 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002322 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002323 else if (MemberSpecializationInfo *MSInfo
2324 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002325 return MSInfo->getPointOfInstantiation();
2326
2327 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002328}
2329
Douglas Gregor6411b922009-09-11 20:15:17 +00002330bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002331 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002332 return true;
2333
2334 // If this function was instantiated from a member function of a
2335 // class template, check whether that member function was defined out-of-line.
2336 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2337 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002338 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002339 return Definition->isOutOfLine();
2340 }
2341
2342 // If this function was instantiated from a function template,
2343 // check whether that function template was defined out-of-line.
2344 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2345 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002346 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002347 return Definition->isOutOfLine();
2348 }
2349
2350 return false;
2351}
2352
Abramo Bagnaraea947882011-03-08 16:41:52 +00002353SourceRange FunctionDecl::getSourceRange() const {
2354 return SourceRange(getOuterLocStart(), EndRangeLoc);
2355}
2356
Anna Zaks28db7ce2012-01-18 02:45:01 +00002357unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002358 IdentifierInfo *FnInfo = getIdentifier();
2359
2360 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002361 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002362
2363 // Builtin handling.
2364 switch (getBuiltinID()) {
2365 case Builtin::BI__builtin_memset:
2366 case Builtin::BI__builtin___memset_chk:
2367 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002368 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002369
2370 case Builtin::BI__builtin_memcpy:
2371 case Builtin::BI__builtin___memcpy_chk:
2372 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002373 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002374
2375 case Builtin::BI__builtin_memmove:
2376 case Builtin::BI__builtin___memmove_chk:
2377 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002378 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002379
2380 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002381 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002382 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002383 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002384
2385 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002386 case Builtin::BImemcmp:
2387 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002388
2389 case Builtin::BI__builtin_strncpy:
2390 case Builtin::BI__builtin___strncpy_chk:
2391 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002392 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002393
2394 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002395 case Builtin::BIstrncmp:
2396 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002397
2398 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002399 case Builtin::BIstrncasecmp:
2400 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002401
2402 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002403 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002404 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002405 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002406
2407 case Builtin::BI__builtin_strndup:
2408 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002409 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002410
Anna Zaks314cd092012-02-01 19:08:57 +00002411 case Builtin::BI__builtin_strlen:
2412 case Builtin::BIstrlen:
2413 return Builtin::BIstrlen;
2414
Anna Zaks201d4892012-01-13 21:52:01 +00002415 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002416 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002417 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002418 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002419 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002420 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002421 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002422 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002423 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002424 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002425 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002426 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002427 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002428 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002429 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002430 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002431 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002432 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002433 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002434 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002435 else if (FnInfo->isStr("strlen"))
2436 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002437 }
2438 break;
2439 }
Anna Zaks22122702012-01-17 00:37:07 +00002440 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002441}
2442
Chris Lattner59a25942008-03-31 00:36:02 +00002443//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002444// FieldDecl Implementation
2445//===----------------------------------------------------------------------===//
2446
Jay Foad39c79802011-01-12 09:06:06 +00002447FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002448 SourceLocation StartLoc, SourceLocation IdLoc,
2449 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002450 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2451 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002452 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002453 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002454}
2455
Douglas Gregor72172e92012-01-05 21:55:30 +00002456FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2457 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2458 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2459 0, QualType(), 0, 0, false, false);
2460}
2461
Sebastian Redl833ef452010-01-26 22:01:41 +00002462bool FieldDecl::isAnonymousStructOrUnion() const {
2463 if (!isImplicit() || getDeclName())
2464 return false;
2465
2466 if (const RecordType *Record = getType()->getAs<RecordType>())
2467 return Record->getDecl()->isAnonymousStructOrUnion();
2468
2469 return false;
2470}
2471
Richard Smithcaf33902011-10-10 18:28:20 +00002472unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2473 assert(isBitField() && "not a bitfield");
2474 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2475 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2476}
2477
John McCall4e819612011-01-20 07:57:12 +00002478unsigned FieldDecl::getFieldIndex() const {
2479 if (CachedFieldIndex) return CachedFieldIndex - 1;
2480
Richard Smithd62306a2011-11-10 06:34:14 +00002481 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002482 const RecordDecl *RD = getParent();
2483 const FieldDecl *LastFD = 0;
2484 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002485
2486 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2487 I != E; ++I, ++Index) {
2488 (*I)->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002489
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002490 if (IsMsStruct) {
2491 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smithd62306a2011-11-10 06:34:14 +00002492 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2493 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002494 continue;
2495 }
Richard Smithd62306a2011-11-10 06:34:14 +00002496 LastFD = (*I);
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002497 }
John McCall4e819612011-01-20 07:57:12 +00002498 }
2499
Richard Smithd62306a2011-11-10 06:34:14 +00002500 assert(CachedFieldIndex && "failed to find field in parent");
2501 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002502}
2503
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002504SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002505 if (const Expr *E = InitializerOrBitWidth.getPointer())
2506 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002507 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002508}
2509
Richard Smith938f40b2011-06-11 17:19:42 +00002510void FieldDecl::setInClassInitializer(Expr *Init) {
2511 assert(!InitializerOrBitWidth.getPointer() &&
2512 "bit width or initializer already set");
2513 InitializerOrBitWidth.setPointer(Init);
2514 InitializerOrBitWidth.setInt(0);
2515}
2516
Sebastian Redl833ef452010-01-26 22:01:41 +00002517//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002518// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002519//===----------------------------------------------------------------------===//
2520
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002521SourceLocation TagDecl::getOuterLocStart() const {
2522 return getTemplateOrInnerLocStart(this);
2523}
2524
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002525SourceRange TagDecl::getSourceRange() const {
2526 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002527 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002528}
2529
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002530TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002531 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002532}
2533
Richard Smithdda56e42011-04-15 14:24:37 +00002534void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2535 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002536 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002537 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002538 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002539}
2540
Douglas Gregordee1be82009-01-17 00:42:38 +00002541void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002542 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002543
2544 if (isa<CXXRecordDecl>(this)) {
2545 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2546 struct CXXRecordDecl::DefinitionData *Data =
2547 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002548 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2549 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002550 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002551}
2552
2553void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002554 assert((!isa<CXXRecordDecl>(this) ||
2555 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2556 "definition completed but not started");
2557
John McCallf937c022011-10-07 06:10:15 +00002558 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002559 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002560
2561 if (ASTMutationListener *L = getASTMutationListener())
2562 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002563}
2564
John McCallf937c022011-10-07 06:10:15 +00002565TagDecl *TagDecl::getDefinition() const {
2566 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002567 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002568 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2569 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002570
2571 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002572 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002573 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002574 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002575
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002576 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002577}
2578
Douglas Gregor14454802011-02-25 02:25:35 +00002579void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2580 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002581 // Make sure the extended qualifier info is allocated.
2582 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002583 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002584 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002585 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002586 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002587 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002588 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002589 if (getExtInfo()->NumTemplParamLists == 0) {
2590 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002591 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002592 }
2593 else
2594 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002595 }
2596 }
2597}
2598
Abramo Bagnara60804e12011-03-18 15:16:37 +00002599void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2600 unsigned NumTPLists,
2601 TemplateParameterList **TPLists) {
2602 assert(NumTPLists > 0);
2603 // Make sure the extended decl info is allocated.
2604 if (!hasExtInfo())
2605 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002606 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002607 // Set the template parameter lists info.
2608 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2609}
2610
Ted Kremenek21475702008-09-05 17:16:31 +00002611//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002612// EnumDecl Implementation
2613//===----------------------------------------------------------------------===//
2614
David Blaikie68e081d2011-12-20 02:48:34 +00002615void EnumDecl::anchor() { }
2616
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002617EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2618 SourceLocation StartLoc, SourceLocation IdLoc,
2619 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002620 EnumDecl *PrevDecl, bool IsScoped,
2621 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002622 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002623 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002624 C.getTypeDeclType(Enum, PrevDecl);
2625 return Enum;
2626}
2627
Douglas Gregor72172e92012-01-05 21:55:30 +00002628EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2629 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2630 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2631 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002632}
2633
Douglas Gregord5058122010-02-11 01:19:42 +00002634void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002635 QualType NewPromotionType,
2636 unsigned NumPositiveBits,
2637 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002638 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002639 if (!IntegerType)
2640 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002641 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002642 setNumPositiveBits(NumPositiveBits);
2643 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002644 TagDecl::completeDefinition();
2645}
2646
Richard Smith7d137e32012-03-23 03:33:32 +00002647TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2648 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2649 return MSI->getTemplateSpecializationKind();
2650
2651 return TSK_Undeclared;
2652}
2653
2654void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2655 SourceLocation PointOfInstantiation) {
2656 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2657 assert(MSI && "Not an instantiated member enumeration?");
2658 MSI->setTemplateSpecializationKind(TSK);
2659 if (TSK != TSK_ExplicitSpecialization &&
2660 PointOfInstantiation.isValid() &&
2661 MSI->getPointOfInstantiation().isInvalid())
2662 MSI->setPointOfInstantiation(PointOfInstantiation);
2663}
2664
Richard Smith4b38ded2012-03-14 23:13:10 +00002665EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2666 if (SpecializationInfo)
2667 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2668
2669 return 0;
2670}
2671
2672void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2673 TemplateSpecializationKind TSK) {
2674 assert(!SpecializationInfo && "Member enum is already a specialization");
2675 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2676}
2677
Sebastian Redl833ef452010-01-26 22:01:41 +00002678//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002679// RecordDecl Implementation
2680//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002681
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002682RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2683 SourceLocation StartLoc, SourceLocation IdLoc,
2684 IdentifierInfo *Id, RecordDecl *PrevDecl)
2685 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002686 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002687 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002688 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002689 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002690 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002691}
2692
Jay Foad39c79802011-01-12 09:06:06 +00002693RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002694 SourceLocation StartLoc, SourceLocation IdLoc,
2695 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2696 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2697 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002698 C.getTypeDeclType(R, PrevDecl);
2699 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002700}
2701
Douglas Gregor72172e92012-01-05 21:55:30 +00002702RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2703 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2704 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2705 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002706}
2707
Douglas Gregordfcad112009-03-25 15:59:44 +00002708bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002709 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002710 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2711}
2712
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002713RecordDecl::field_iterator RecordDecl::field_begin() const {
2714 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2715 LoadFieldsFromExternalStorage();
2716
2717 return field_iterator(decl_iterator(FirstDecl));
2718}
2719
Douglas Gregorb11aad82011-02-19 18:51:44 +00002720/// completeDefinition - Notes that the definition of this type is now
2721/// complete.
2722void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002723 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002724 TagDecl::completeDefinition();
2725}
2726
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002727void RecordDecl::LoadFieldsFromExternalStorage() const {
2728 ExternalASTSource *Source = getASTContext().getExternalSource();
2729 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2730
2731 // Notify that we have a RecordDecl doing some initialization.
2732 ExternalASTSource::Deserializing TheFields(Source);
2733
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002734 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002735 LoadedFieldsFromExternalStorage = true;
2736 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2737 case ELR_Success:
2738 break;
2739
2740 case ELR_AlreadyLoaded:
2741 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002742 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002743 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002744
2745#ifndef NDEBUG
2746 // Check that all decls we got were FieldDecls.
2747 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2748 assert(isa<FieldDecl>(Decls[i]));
2749#endif
2750
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002751 if (Decls.empty())
2752 return;
2753
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002754 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2755 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002756}
2757
Steve Naroff415d3d52008-10-08 17:01:13 +00002758//===----------------------------------------------------------------------===//
2759// BlockDecl Implementation
2760//===----------------------------------------------------------------------===//
2761
David Blaikie9c70e042011-09-21 18:16:56 +00002762void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002763 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002764
Steve Naroffc4b30e52009-03-13 16:56:44 +00002765 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002766 if (!NewParamInfo.empty()) {
2767 NumParams = NewParamInfo.size();
2768 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2769 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002770 }
2771}
2772
John McCall351762c2011-02-07 10:33:21 +00002773void BlockDecl::setCaptures(ASTContext &Context,
2774 const Capture *begin,
2775 const Capture *end,
2776 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002777 CapturesCXXThis = capturesCXXThis;
2778
2779 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002780 NumCaptures = 0;
2781 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002782 return;
2783 }
2784
John McCall351762c2011-02-07 10:33:21 +00002785 NumCaptures = end - begin;
2786
2787 // Avoid new Capture[] because we don't want to provide a default
2788 // constructor.
2789 size_t allocationSize = NumCaptures * sizeof(Capture);
2790 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2791 memcpy(buffer, begin, allocationSize);
2792 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002793}
Sebastian Redl833ef452010-01-26 22:01:41 +00002794
John McCallce45f882011-06-15 22:51:16 +00002795bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2796 for (capture_const_iterator
2797 i = capture_begin(), e = capture_end(); i != e; ++i)
2798 // Only auto vars can be captured, so no redeclaration worries.
2799 if (i->getVariable() == variable)
2800 return true;
2801
2802 return false;
2803}
2804
Douglas Gregor70226da2010-12-21 16:27:07 +00002805SourceRange BlockDecl::getSourceRange() const {
2806 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2807}
Sebastian Redl833ef452010-01-26 22:01:41 +00002808
2809//===----------------------------------------------------------------------===//
2810// Other Decl Allocation/Deallocation Method Implementations
2811//===----------------------------------------------------------------------===//
2812
David Blaikie68e081d2011-12-20 02:48:34 +00002813void TranslationUnitDecl::anchor() { }
2814
Sebastian Redl833ef452010-01-26 22:01:41 +00002815TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2816 return new (C) TranslationUnitDecl(C);
2817}
2818
David Blaikie68e081d2011-12-20 02:48:34 +00002819void LabelDecl::anchor() { }
2820
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002821LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002822 SourceLocation IdentL, IdentifierInfo *II) {
2823 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2824}
2825
2826LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2827 SourceLocation IdentL, IdentifierInfo *II,
2828 SourceLocation GnuLabelL) {
2829 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2830 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002831}
2832
Douglas Gregor72172e92012-01-05 21:55:30 +00002833LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2834 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2835 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002836}
2837
David Blaikie68e081d2011-12-20 02:48:34 +00002838void ValueDecl::anchor() { }
2839
2840void ImplicitParamDecl::anchor() { }
2841
Sebastian Redl833ef452010-01-26 22:01:41 +00002842ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002843 SourceLocation IdLoc,
2844 IdentifierInfo *Id,
2845 QualType Type) {
2846 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002847}
2848
Douglas Gregor72172e92012-01-05 21:55:30 +00002849ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2850 unsigned ID) {
2851 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2852 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2853}
2854
Sebastian Redl833ef452010-01-26 22:01:41 +00002855FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002856 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002857 const DeclarationNameInfo &NameInfo,
2858 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002859 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002860 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002861 bool hasWrittenPrototype,
2862 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002863 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2864 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002865 isInlineSpecified,
2866 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002867 New->HasWrittenPrototype = hasWrittenPrototype;
2868 return New;
2869}
2870
Douglas Gregor72172e92012-01-05 21:55:30 +00002871FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2872 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2873 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2874 DeclarationNameInfo(), QualType(), 0,
2875 SC_None, SC_None, false, false);
2876}
2877
Sebastian Redl833ef452010-01-26 22:01:41 +00002878BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2879 return new (C) BlockDecl(DC, L);
2880}
2881
Douglas Gregor72172e92012-01-05 21:55:30 +00002882BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2884 return new (Mem) BlockDecl(0, SourceLocation());
2885}
2886
Sebastian Redl833ef452010-01-26 22:01:41 +00002887EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2888 SourceLocation L,
2889 IdentifierInfo *Id, QualType T,
2890 Expr *E, const llvm::APSInt &V) {
2891 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2892}
2893
Douglas Gregor72172e92012-01-05 21:55:30 +00002894EnumConstantDecl *
2895EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2896 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2897 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2898 llvm::APSInt());
2899}
2900
David Blaikie68e081d2011-12-20 02:48:34 +00002901void IndirectFieldDecl::anchor() { }
2902
Benjamin Kramer39593702010-11-21 14:11:41 +00002903IndirectFieldDecl *
2904IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2905 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2906 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002907 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2908}
2909
Douglas Gregor72172e92012-01-05 21:55:30 +00002910IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2911 unsigned ID) {
2912 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2913 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2914 QualType(), 0, 0);
2915}
2916
Douglas Gregorbe996932010-09-01 20:41:53 +00002917SourceRange EnumConstantDecl::getSourceRange() const {
2918 SourceLocation End = getLocation();
2919 if (Init)
2920 End = Init->getLocEnd();
2921 return SourceRange(getLocation(), End);
2922}
2923
David Blaikie68e081d2011-12-20 02:48:34 +00002924void TypeDecl::anchor() { }
2925
Sebastian Redl833ef452010-01-26 22:01:41 +00002926TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002927 SourceLocation StartLoc, SourceLocation IdLoc,
2928 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2929 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002930}
2931
David Blaikie68e081d2011-12-20 02:48:34 +00002932void TypedefNameDecl::anchor() { }
2933
Douglas Gregor72172e92012-01-05 21:55:30 +00002934TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2935 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2936 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2937}
2938
Richard Smithdda56e42011-04-15 14:24:37 +00002939TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2940 SourceLocation StartLoc,
2941 SourceLocation IdLoc, IdentifierInfo *Id,
2942 TypeSourceInfo *TInfo) {
2943 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2944}
2945
Douglas Gregor72172e92012-01-05 21:55:30 +00002946TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2947 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2948 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2949}
2950
Abramo Bagnaraea947882011-03-08 16:41:52 +00002951SourceRange TypedefDecl::getSourceRange() const {
2952 SourceLocation RangeEnd = getLocation();
2953 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2954 if (typeIsPostfix(TInfo->getType()))
2955 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2956 }
2957 return SourceRange(getLocStart(), RangeEnd);
2958}
2959
Richard Smithdda56e42011-04-15 14:24:37 +00002960SourceRange TypeAliasDecl::getSourceRange() const {
2961 SourceLocation RangeEnd = getLocStart();
2962 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2963 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2964 return SourceRange(getLocStart(), RangeEnd);
2965}
2966
David Blaikie68e081d2011-12-20 02:48:34 +00002967void FileScopeAsmDecl::anchor() { }
2968
Sebastian Redl833ef452010-01-26 22:01:41 +00002969FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002970 StringLiteral *Str,
2971 SourceLocation AsmLoc,
2972 SourceLocation RParenLoc) {
2973 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002974}
Douglas Gregorba345522011-12-02 23:23:56 +00002975
Douglas Gregor72172e92012-01-05 21:55:30 +00002976FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2977 unsigned ID) {
2978 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2979 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2980}
2981
Douglas Gregorba345522011-12-02 23:23:56 +00002982//===----------------------------------------------------------------------===//
2983// ImportDecl Implementation
2984//===----------------------------------------------------------------------===//
2985
2986/// \brief Retrieve the number of module identifiers needed to name the given
2987/// module.
2988static unsigned getNumModuleIdentifiers(Module *Mod) {
2989 unsigned Result = 1;
2990 while (Mod->Parent) {
2991 Mod = Mod->Parent;
2992 ++Result;
2993 }
2994 return Result;
2995}
2996
Douglas Gregor22d09742012-01-03 18:04:46 +00002997ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002998 Module *Imported,
2999 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003000 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003001 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003002{
3003 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3004 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3005 memcpy(StoredLocs, IdentifierLocs.data(),
3006 IdentifierLocs.size() * sizeof(SourceLocation));
3007}
3008
Douglas Gregor22d09742012-01-03 18:04:46 +00003009ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003010 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003011 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003012 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003013{
3014 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3015}
3016
3017ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003018 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003019 ArrayRef<SourceLocation> IdentifierLocs) {
3020 void *Mem = C.Allocate(sizeof(ImportDecl) +
3021 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003022 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003023}
3024
3025ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003026 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003027 Module *Imported,
3028 SourceLocation EndLoc) {
3029 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003030 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003031 Import->setImplicit();
3032 return Import;
3033}
3034
Douglas Gregor72172e92012-01-05 21:55:30 +00003035ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3036 unsigned NumLocations) {
3037 void *Mem = AllocateDeserializedDecl(C, ID,
3038 (sizeof(ImportDecl) +
3039 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003040 return new (Mem) ImportDecl(EmptyShell());
3041}
3042
3043ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3044 if (!ImportedAndComplete.getInt())
3045 return ArrayRef<SourceLocation>();
3046
3047 const SourceLocation *StoredLocs
3048 = reinterpret_cast<const SourceLocation *>(this + 1);
3049 return ArrayRef<SourceLocation>(StoredLocs,
3050 getNumModuleIdentifiers(getImportedModule()));
3051}
3052
3053SourceRange ImportDecl::getSourceRange() const {
3054 if (!ImportedAndComplete.getInt())
3055 return SourceRange(getLocation(),
3056 *reinterpret_cast<const SourceLocation *>(this + 1));
3057
3058 return SourceRange(getLocation(), getIdentifierLocs().back());
3059}