blob: 405c2b328f9c5554eb0ddf965933a753ef5fa6b8 [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
Rafael Espindola2f869a32012-01-14 00:30:36 +000069static LinkageInfo getLVForType(QualType T) {
70 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
71 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
72}
73
Douglas Gregor7dc5c172010-02-03 09:33:45 +000074/// \brief Get the most restrictive linkage for the types in the given
75/// template parameter list.
Rafael Espindola2f869a32012-01-14 00:30:36 +000076static LinkageInfo
John McCall457a04e2010-10-22 21:05:15 +000077getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000078 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000079 for (TemplateParameterList::const_iterator P = Params->begin(),
80 PEnd = Params->end();
81 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000082 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
83 if (NTTP->isExpandedParameterPack()) {
84 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
85 QualType T = NTTP->getExpansionType(I);
86 if (!T->isDependentType())
Rafael Espindola2f869a32012-01-14 00:30:36 +000087 LV.merge(getLVForType(T));
Douglas Gregor0231d8d2011-01-19 20:10:05 +000088 }
89 continue;
90 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +000091
Douglas Gregor7dc5c172010-02-03 09:33:45 +000092 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000093 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000094 continue;
95 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +000096 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +000097
98 if (TemplateTemplateParmDecl *TTP
99 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000100 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000101 }
102 }
103
John McCall457a04e2010-10-22 21:05:15 +0000104 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000105}
106
Douglas Gregorbf62d642010-12-06 18:36:25 +0000107/// getLVForDecl - Get the linkage and visibility for the given declaration.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000108static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000109
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000110/// \brief Get the most restrictive linkage for the types and
111/// declarations in the given template argument list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000112static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
113 unsigned NumArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000114 bool OnlyTemplate) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000115 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000116
117 for (unsigned I = 0; I != NumArgs; ++I) {
118 switch (Args[I].getKind()) {
119 case TemplateArgument::Null:
120 case TemplateArgument::Integral:
121 case TemplateArgument::Expression:
122 break;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000123
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000124 case TemplateArgument::Type:
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000125 LV.mergeWithMin(getLVForType(Args[I].getAsType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000126 break;
127
128 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000129 // The decl can validly be null as the representation of nullptr
130 // arguments, valid only in C++0x.
131 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000132 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000133 LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate));
John McCall457a04e2010-10-22 21:05:15 +0000134 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 break;
136
137 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000138 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000139 if (TemplateDecl *Template
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000140 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000141 LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000142 break;
143
144 case TemplateArgument::Pack:
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000145 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
146 Args[I].pack_size(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000147 OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000148 break;
149 }
150 }
151
John McCall457a04e2010-10-22 21:05:15 +0000152 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000153}
154
Rafael Espindola2f869a32012-01-14 00:30:36 +0000155static LinkageInfo
Douglas Gregorbf62d642010-12-06 18:36:25 +0000156getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000157 bool OnlyTemplate) {
158 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall8823c652010-08-13 08:35:10 +0000159}
160
Rafael Espindola340941d2012-05-25 16:41:35 +0000161static bool shouldConsiderTemplateVis(const FunctionDecl *fn,
Rafael Espindola96dcb8d2012-05-21 20:31:27 +0000162 const FunctionTemplateSpecializationInfo *spec) {
163 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000164}
165
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000166static bool
167shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola93c289c2012-05-21 20:15:56 +0000168 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000169}
170
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000171static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
172 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000173 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000174 "Not a name having namespace scope");
175 ASTContext &Context = D->getASTContext();
176
177 // C++ [basic.link]p3:
178 // A name having namespace scope (3.3.6) has internal linkage if it
179 // is the name of
180 // - an object, reference, function or function template that is
181 // explicitly declared static; or,
182 // (This bullet corresponds to C99 6.2.2p3.)
183 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
184 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000185 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000186 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000187
188 // - an object or reference that is explicitly declared const
189 // and neither explicitly declared extern nor previously
190 // declared to have external linkage; or
191 // (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000192 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000193 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000194 Var->getStorageClass() != SC_Extern &&
195 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000196 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000197 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000198 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000199 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000200 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000201 FoundExtern = true;
202
203 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000204 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000205 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000206 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000207 const VarDecl *PrevVar = Var->getPreviousDecl();
208 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000209 if (PrevVar->getStorageClass() == SC_PrivateExtern)
210 break;
211 if (PrevVar)
212 return PrevVar->getLinkageAndVisibility();
213 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000214 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000215 // C++ [temp]p4:
216 // A non-member function template can have internal linkage; any
217 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000218 const FunctionDecl *Function = 0;
219 if (const FunctionTemplateDecl *FunTmpl
220 = dyn_cast<FunctionTemplateDecl>(D))
221 Function = FunTmpl->getTemplatedDecl();
222 else
223 Function = cast<FunctionDecl>(D);
224
225 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000226 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000227 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000228 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
229 // - a data member of an anonymous union.
230 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000231 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000232 }
233
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000234 if (D->isInAnonymousNamespace()) {
235 const VarDecl *Var = dyn_cast<VarDecl>(D);
236 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000237 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
238 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000239 return LinkageInfo::uniqueExternal();
240 }
John McCallb7139c42010-10-28 04:18:25 +0000241
John McCall457a04e2010-10-22 21:05:15 +0000242 // Set up the defaults.
243
244 // C99 6.2.2p5:
245 // If the declaration of an identifier for an object has file
246 // scope and no storage-class specifier, its linkage is
247 // external.
John McCallc273f242010-10-30 11:50:40 +0000248 LinkageInfo LV;
249
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000250 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000251 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000252 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000253 } else {
254 // If we're declared in a namespace with a visibility attribute,
255 // use that namespace's visibility, but don't call it explicit.
256 for (const DeclContext *DC = D->getDeclContext();
257 !isa<TranslationUnitDecl>(DC);
258 DC = DC->getParent()) {
259 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
260 if (!ND) continue;
261 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000262 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000263 break;
264 }
265 }
266 }
267 }
268
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000269 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000270 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000271
Douglas Gregorf73b2822009-11-25 22:24:25 +0000272 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000273
Douglas Gregorf73b2822009-11-25 22:24:25 +0000274 // A name having namespace scope has external linkage if it is the
275 // name of
276 //
277 // - an object or reference, unless it has internal linkage; or
278 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000279 // GCC applies the following optimization to variables and static
280 // data members, but not to functions:
281 //
John McCall457a04e2010-10-22 21:05:15 +0000282 // Modify the variable's LV by the LV of its type unless this is
283 // C or extern "C". This follows from [basic.link]p9:
284 // A type without linkage shall not be used as the type of a
285 // variable or function with external linkage unless
286 // - the entity has C language linkage, or
287 // - the entity is declared within an unnamed namespace, or
288 // - the entity is not used or is defined in the same
289 // translation unit.
290 // and [basic.link]p10:
291 // ...the types specified by all declarations referring to a
292 // given variable or function shall be identical...
293 // C does not have an equivalent rule.
294 //
John McCall5fe84122010-10-26 04:59:26 +0000295 // Ignore this if we've got an explicit attribute; the user
296 // probably knows what they're doing.
297 //
John McCall457a04e2010-10-22 21:05:15 +0000298 // Note that we don't want to make the variable non-external
299 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000300 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000301 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000302 LinkageInfo TypeLV = getLVForType(Var->getType());
303 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000304 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000305 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000306 }
307
John McCall23032652010-11-02 18:38:13 +0000308 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000309 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000310
David Blaikiebbafb8a2012-03-11 07:00:24 +0000311 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000312 (Var->getStorageClass() == SC_Extern ||
313 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000314
Douglas Gregorf73b2822009-11-25 22:24:25 +0000315 // C99 6.2.2p4:
316 // For an identifier declared with the storage-class specifier
317 // extern in a scope in which a prior declaration of that
318 // identifier is visible, if the prior declaration specifies
319 // internal or external linkage, the linkage of the identifier
320 // at the later declaration is the same as the linkage
321 // specified at the prior declaration. If no prior declaration
322 // is visible, or if the prior declaration specifies no
323 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000324 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000325 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000326 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
327 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000328 }
329 }
330
Douglas Gregorf73b2822009-11-25 22:24:25 +0000331 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000332 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000333 // In theory, we can modify the function's LV by the LV of its
334 // type unless it has C linkage (see comment above about variables
335 // for justification). In practice, GCC doesn't do this, so it's
336 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000337
John McCall23032652010-11-02 18:38:13 +0000338 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000339 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000340
Douglas Gregorf73b2822009-11-25 22:24:25 +0000341 // C99 6.2.2p5:
342 // If the declaration of an identifier for a function has no
343 // storage-class specifier, its linkage is determined exactly
344 // as if it were declared with the storage-class specifier
345 // extern.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000346 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000347 (Function->getStorageClass() == SC_Extern ||
348 Function->getStorageClass() == SC_PrivateExtern ||
349 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000350 // C99 6.2.2p4:
351 // For an identifier declared with the storage-class specifier
352 // extern in a scope in which a prior declaration of that
353 // identifier is visible, if the prior declaration specifies
354 // internal or external linkage, the linkage of the identifier
355 // at the later declaration is the same as the linkage
356 // specified at the prior declaration. If no prior declaration
357 // is visible, or if the prior declaration specifies no
358 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000359 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000360 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000361 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
362 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000363 }
364 }
365
John McCallf768aa72011-02-10 06:50:24 +0000366 // In C++, then if the type of the function uses a type with
367 // unique-external linkage, it's not legally usable from outside
368 // this translation unit. However, we should use the C linkage
369 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000370 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000371 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000372 Function->getType()->getLinkage() == UniqueExternalLinkage)
373 return LinkageInfo::uniqueExternal();
374
John McCallb8c604a2011-06-27 23:06:04 +0000375 // Consider LV from the template and the template arguments unless
376 // this is an explicit specialization with a visibility attribute.
377 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000378 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000379 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
380 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
381 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
382 OnlyTemplate);
383 if (shouldConsiderTemplateVis(Function, specInfo)) {
384 LV.merge(TempLV);
385 LV.mergeWithMin(ArgsLV);
386 } else {
387 LV.mergeLinkage(TempLV);
388 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000389 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000390 }
391
Douglas Gregorf73b2822009-11-25 22:24:25 +0000392 // - a named class (Clause 9), or an unnamed class defined in a
393 // typedef declaration in which the class has the typedef name
394 // for linkage purposes (7.1.3); or
395 // - a named enumeration (7.2), or an unnamed enumeration
396 // defined in a typedef declaration in which the enumeration
397 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000398 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
399 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000400 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000401 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000402
John McCall457a04e2010-10-22 21:05:15 +0000403 // If this is a class template specialization, consider the
404 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000405 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000406 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000407 // From the template.
408 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000409
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000410 // The arguments at which the template was instantiated.
411 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
412 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
413 OnlyTemplate);
414 if (shouldConsiderTemplateVis(spec)) {
415 LV.merge(TempLV);
416 LV.mergeWithMin(ArgsLV);
417 } else {
418 LV.mergeLinkage(TempLV);
419 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000420 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000421 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000422
423 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000424 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000425 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
426 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000427 if (!isExternalLinkage(EnumLV.linkage()))
428 return LinkageInfo::none();
429 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000430
431 // - a template, unless it is a function template that has
432 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000433 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000434 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000435 // - a namespace (7.3), unless it is declared within an unnamed
436 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000437 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
438 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000439
John McCall457a04e2010-10-22 21:05:15 +0000440 // By extension, we assign external linkage to Objective-C
441 // interfaces.
442 } else if (isa<ObjCInterfaceDecl>(D)) {
443 // fallout
444
445 // Everything not covered here has no linkage.
446 } else {
John McCallc273f242010-10-30 11:50:40 +0000447 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000448 }
449
450 // If we ended up with non-external linkage, visibility should
451 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000452 if (LV.linkage() != ExternalLinkage)
453 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000454
John McCall457a04e2010-10-22 21:05:15 +0000455 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000456}
457
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000458static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000459 // Only certain class members have linkage. Note that fields don't
460 // really have linkage, but it's convenient to say they do for the
461 // purposes of calculating linkage of pointer-to-data-member
462 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000463 if (!(isa<CXXMethodDecl>(D) ||
464 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000465 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000466 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000467 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000468 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000469
John McCall07072662010-11-02 01:45:15 +0000470 LinkageInfo LV;
471
John McCall07072662010-11-02 01:45:15 +0000472 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000473 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000474 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000475 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000476 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000477
478 // If this class member has an explicit visibility attribute, the only
479 // thing that can change its visibility is the template arguments, so
480 // only look for them when processing the the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000481 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000482
483 // If we're paying attention to global visibility, apply
484 // -finline-visibility-hidden if this is an inline method.
485 //
486 // Note that we do this before merging information about
487 // the class visibility.
488 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
489 TemplateSpecializationKind TSK = TSK_Undeclared;
490 if (FunctionTemplateSpecializationInfo *spec
491 = MD->getTemplateSpecializationInfo()) {
492 TSK = spec->getTemplateSpecializationKind();
493 } else if (MemberSpecializationInfo *MSI =
494 MD->getMemberSpecializationInfo()) {
495 TSK = MSI->getTemplateSpecializationKind();
496 }
497
498 const FunctionDecl *Def = 0;
499 // InlineVisibilityHidden only applies to definitions, and
500 // isInlined() only gives meaningful answers on definitions
501 // anyway.
502 if (TSK != TSK_ExplicitInstantiationDeclaration &&
503 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000504 !OnlyTemplate &&
Rafael Espindola505a7c82012-04-16 18:25:01 +0000505 !LV.visibilityExplicit() &&
506 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
507 MD->hasBody(Def) && Def->isInlined())
508 LV.mergeVisibility(HiddenVisibility, true);
509 }
John McCallc273f242010-10-30 11:50:40 +0000510
Rafael Espindola53cf2192012-04-19 05:50:08 +0000511 // If this member has an visibility attribute, ClassF will exclude
512 // attributes on the class or command line options, keeping only information
513 // about the template instantiation. If the member has no visibility
514 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
515 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000516 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
517 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000518 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000519 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000520
521 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000522 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000523 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000524
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000525 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000526 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000527
John McCall8823c652010-08-13 08:35:10 +0000528 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000529 // If the type of the function uses a type with unique-external
530 // linkage, it's not legally usable from outside this translation unit.
531 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
532 return LinkageInfo::uniqueExternal();
533
John McCall457a04e2010-10-22 21:05:15 +0000534 // If this is a method template specialization, use the linkage for
535 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000536 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000537 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000538 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000539 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000540 OnlyTemplate));
541 if (!OnlyTemplate)
John McCallb8c604a2011-06-27 23:06:04 +0000542 LV.merge(getLVForTemplateParameterList(
543 spec->getTemplate()->getTemplateParameters()));
544 }
John McCalle6e622e2010-11-01 01:29:57 +0000545 }
John McCall457a04e2010-10-22 21:05:15 +0000546
John McCall37bb6c92010-10-29 22:22:43 +0000547 // Note that in contrast to basically every other situation, we
548 // *do* apply -fvisibility to method declarations.
549
550 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000551 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000552 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000553 // Merge template argument/parameter information for member
554 // class template specializations.
555 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
556 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
557 OnlyTemplate);
558 TemplateParameterList *TemplateParams =
559 spec->getSpecializedTemplate()->getTemplateParameters();
560 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000561 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000562 LV.mergeWithMin(ArgsLV);
Rafael Espindola4d71d0f2012-05-25 14:17:45 +0000563 if (!OnlyTemplate)
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000564 LV.merge(ParamsLV);
565 } else {
566 LV.mergeLinkage(ArgsLV);
567 if (!OnlyTemplate)
568 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000569 }
John McCall37bb6c92010-10-29 22:22:43 +0000570 }
571
John McCall37bb6c92010-10-29 22:22:43 +0000572 // Static data members.
573 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000574 // Modify the variable's linkage by its type, but ignore the
575 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000576 LinkageInfo TypeLV = getLVForType(VD->getType());
577 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000578 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000579 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000580 }
581
John McCall457a04e2010-10-22 21:05:15 +0000582 return LV;
John McCall8823c652010-08-13 08:35:10 +0000583}
584
John McCalld396b972011-02-08 19:01:05 +0000585static void clearLinkageForClass(const CXXRecordDecl *record) {
586 for (CXXRecordDecl::decl_iterator
587 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
588 Decl *child = *i;
589 if (isa<NamedDecl>(child))
590 cast<NamedDecl>(child)->ClearLinkageCache();
591 }
592}
593
David Blaikie68e081d2011-12-20 02:48:34 +0000594void NamedDecl::anchor() { }
595
John McCalld396b972011-02-08 19:01:05 +0000596void NamedDecl::ClearLinkageCache() {
597 // Note that we can't skip clearing the linkage of children just
598 // because the parent doesn't have cached linkage: we don't cache
599 // when computing linkage for parent contexts.
600
601 HasCachedLinkage = 0;
602
603 // If we're changing the linkage of a class, we need to reset the
604 // linkage of child declarations, too.
605 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
606 clearLinkageForClass(record);
607
John McCall83779672011-02-19 02:53:41 +0000608 if (ClassTemplateDecl *temp =
609 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000610 // Clear linkage for the template pattern.
611 CXXRecordDecl *record = temp->getTemplatedDecl();
612 record->HasCachedLinkage = 0;
613 clearLinkageForClass(record);
614
John McCall83779672011-02-19 02:53:41 +0000615 // We need to clear linkage for specializations, too.
616 for (ClassTemplateDecl::spec_iterator
617 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
618 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000619 }
John McCall83779672011-02-19 02:53:41 +0000620
621 // Clear cached linkage for function template decls, too.
622 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000623 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
624 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000625 for (FunctionTemplateDecl::spec_iterator
626 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
627 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000628 }
John McCall83779672011-02-19 02:53:41 +0000629
John McCalld396b972011-02-08 19:01:05 +0000630}
631
Douglas Gregorbf62d642010-12-06 18:36:25 +0000632Linkage NamedDecl::getLinkage() const {
633 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000634 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000635 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000636 return Linkage(CachedLinkage);
637 }
638
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000639 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000640 HasCachedLinkage = 1;
641 return Linkage(CachedLinkage);
642}
643
John McCallc273f242010-10-30 11:50:40 +0000644LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000645 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000646 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000647 HasCachedLinkage = 1;
648 CachedLinkage = LI.linkage();
649 return LI;
John McCall033caa52010-10-29 00:29:13 +0000650}
Ted Kremenek926d8602010-04-20 23:15:35 +0000651
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000652llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
653 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000654 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
655 if (llvm::Optional<Visibility> V =
656 getVisibilityOf(Var->getMostRecentDecl()))
657 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000658
Rafael Espindola96e68242012-05-16 02:10:38 +0000659 if (Var->isStaticDataMember()) {
660 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
661 if (InstantiatedFrom)
662 return getVisibilityOf(InstantiatedFrom);
663 }
664
665 return llvm::Optional<Visibility>();
666 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000667 // Use the most recent declaration of a function, and also handle
668 // function template specializations.
669 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
670 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000671 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000672 return V;
673
674 // If the function is a specialization of a template with an
675 // explicit visibility attribute, use that.
676 if (FunctionTemplateSpecializationInfo *templateInfo
677 = fn->getTemplateSpecializationInfo())
678 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
679
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000680 // If the function is a member of a specialization of a class template
681 // and the corresponding decl has explicit visibility, use that.
682 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
683 if (InstantiatedFrom)
684 return getVisibilityOf(InstantiatedFrom);
685
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000686 return llvm::Optional<Visibility>();
687 }
688
689 // Otherwise, just check the declaration itself first.
690 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
691 return V;
692
693 // If there wasn't explicit visibility there, and this is a
694 // specialization of a class template, check for visibility
695 // on the pattern.
696 if (const ClassTemplateSpecializationDecl *spec
697 = dyn_cast<ClassTemplateSpecializationDecl>(this))
698 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
699
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000700 // If this is a member class of a specialization of a class template
701 // and the corresponding decl has explicit visibility, use that.
702 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
703 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
704 if (InstantiatedFrom)
705 return getVisibilityOf(InstantiatedFrom);
706 }
707
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000708 return llvm::Optional<Visibility>();
709}
710
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000711static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000712 // Objective-C: treat all Objective-C declarations as having external
713 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000714 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000715 default:
716 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000717 case Decl::ParmVar:
718 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000719 case Decl::TemplateTemplateParm: // count these as external
720 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000721 case Decl::ObjCAtDefsField:
722 case Decl::ObjCCategory:
723 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000724 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000725 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000726 case Decl::ObjCMethod:
727 case Decl::ObjCProperty:
728 case Decl::ObjCPropertyImpl:
729 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000730 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000731
732 case Decl::CXXRecord: {
733 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
734 if (Record->isLambda()) {
735 if (!Record->getLambdaManglingNumber()) {
736 // This lambda has no mangling number, so it's internal.
737 return LinkageInfo::internal();
738 }
739
740 // This lambda has its linkage/visibility determined by its owner.
741 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
742 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
743 if (isa<ParmVarDecl>(ContextDecl))
744 DC = ContextDecl->getDeclContext()->getRedeclContext();
745 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000746 return getLVForDecl(cast<NamedDecl>(ContextDecl),
747 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000748 }
749
750 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000751 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000752
753 return LinkageInfo::external();
754 }
755
756 break;
757 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000758 }
759
Douglas Gregorf73b2822009-11-25 22:24:25 +0000760 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000761 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000762 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000763
764 // C++ [basic.link]p5:
765 // In addition, a member function, static data member, a named
766 // class or enumeration of class scope, or an unnamed class or
767 // enumeration defined in a class-scope typedef declaration such
768 // that the class or enumeration has the typedef name for linkage
769 // purposes (7.1.3), has external linkage if the name of the class
770 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000771 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000772 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000773
774 // C++ [basic.link]p6:
775 // The name of a function declared in block scope and the name of
776 // an object declared by a block scope extern declaration have
777 // linkage. If there is a visible declaration of an entity with
778 // linkage having the same name and type, ignoring entities
779 // declared outside the innermost enclosing namespace scope, the
780 // block scope declaration declares that same entity and receives
781 // the linkage of the previous declaration. If there is more than
782 // one such matching entity, the program is ill-formed. Otherwise,
783 // if no matching entity is found, the block scope entity receives
784 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000785 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
786 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000787 if (Function->isInAnonymousNamespace() &&
788 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000789 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000790
John McCallc273f242010-10-30 11:50:40 +0000791 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000792 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000793 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000794 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000795 }
796
Douglas Gregorec9fd132012-01-14 16:38:05 +0000797 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000798 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000799 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
800 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000801 }
802
803 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000804 }
805
John McCall033caa52010-10-29 00:29:13 +0000806 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000807 if (Var->getStorageClass() == SC_Extern ||
808 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000809 if (Var->isInAnonymousNamespace() &&
810 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000811 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000812
John McCallc273f242010-10-30 11:50:40 +0000813 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000814 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000815 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000816 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000817 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000818 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000819 }
820
Douglas Gregorec9fd132012-01-14 16:38:05 +0000821 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000822 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000823 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
824 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000825 }
826
827 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000828 }
829 }
830
831 // C++ [basic.link]p6:
832 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000833 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000834}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000835
Douglas Gregor2ada0482009-02-04 17:27:36 +0000836std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000837 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000838}
839
840std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000841 const DeclContext *Ctx = getDeclContext();
842
843 if (Ctx->isFunctionOrMethod())
844 return getNameAsString();
845
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000846 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000847 ContextsTy Contexts;
848
849 // Collect contexts.
850 while (Ctx && isa<NamedDecl>(Ctx)) {
851 Contexts.push_back(Ctx);
852 Ctx = Ctx->getParent();
853 };
854
855 std::string QualName;
856 llvm::raw_string_ostream OS(QualName);
857
858 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
859 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000860 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000861 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000862 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
863 std::string TemplateArgsStr
864 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000865 TemplateArgs.data(),
866 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000867 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000868 OS << Spec->getName() << TemplateArgsStr;
869 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000870 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000871 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000872 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000873 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000874 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
875 if (!RD->getIdentifier())
876 OS << "<anonymous " << RD->getKindName() << '>';
877 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000878 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000879 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000880 const FunctionProtoType *FT = 0;
881 if (FD->hasWrittenPrototype())
882 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
883
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000884 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000885 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000886 unsigned NumParams = FD->getNumParams();
887 for (unsigned i = 0; i < NumParams; ++i) {
888 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000889 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000890 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000891 }
892
893 if (FT->isVariadic()) {
894 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000895 OS << ", ";
896 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000897 }
898 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000899 OS << ')';
900 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000901 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000902 }
903 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000904 }
905
John McCalla2a3f7d2010-03-16 21:48:18 +0000906 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000907 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000908 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000909 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000910
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000911 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000912}
913
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000914bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000915 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
916
Douglas Gregor889ceb72009-02-03 19:21:40 +0000917 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
918 // We want to keep it, unless it nominates same namespace.
919 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000920 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
921 ->getOriginalNamespace() ==
922 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
923 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000926 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
927 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000928 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000929
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000930 // For function templates, the underlying function declarations are linked.
931 if (const FunctionTemplateDecl *FunctionTemplate
932 = dyn_cast<FunctionTemplateDecl>(this))
933 if (const FunctionTemplateDecl *OldFunctionTemplate
934 = dyn_cast<FunctionTemplateDecl>(OldD))
935 return FunctionTemplate->getTemplatedDecl()
936 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000937
Steve Naroffc4173fa2009-02-22 19:35:57 +0000938 // For method declarations, we keep track of redeclarations.
939 if (isa<ObjCMethodDecl>(this))
940 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000941
John McCall9f3059a2009-10-09 21:13:30 +0000942 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
943 return true;
944
John McCall3f746822009-11-17 05:59:44 +0000945 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
946 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
947 cast<UsingShadowDecl>(OldD)->getTargetDecl();
948
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000949 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
950 ASTContext &Context = getASTContext();
951 return Context.getCanonicalNestedNameSpecifier(
952 cast<UsingDecl>(this)->getQualifier()) ==
953 Context.getCanonicalNestedNameSpecifier(
954 cast<UsingDecl>(OldD)->getQualifier());
955 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000956
Douglas Gregorb59643b2012-01-03 23:26:26 +0000957 // A typedef of an Objective-C class type can replace an Objective-C class
958 // declaration or definition, and vice versa.
959 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
960 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
961 return true;
962
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000963 // For non-function declarations, if the declarations are of the
964 // same kind then this must be a redeclaration, or semantic analysis
965 // would not have given us the new declaration.
966 return this->getKind() == OldD->getKind();
967}
968
Douglas Gregoreddf4332009-02-24 20:03:32 +0000969bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000970 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000971}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000972
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000973NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000974 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000975 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
976 ND = UD->getTargetDecl();
977
978 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
979 return AD->getClassInterface();
980
981 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000982}
983
John McCalla8ae2222010-04-06 21:38:20 +0000984bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000985 if (!isCXXClassMember())
986 return false;
987
John McCalla8ae2222010-04-06 21:38:20 +0000988 const NamedDecl *D = this;
989 if (isa<UsingShadowDecl>(D))
990 D = cast<UsingShadowDecl>(D)->getTargetDecl();
991
Francois Pichet783dd6e2010-11-21 06:08:52 +0000992 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000993 return true;
994 if (isa<CXXMethodDecl>(D))
995 return cast<CXXMethodDecl>(D)->isInstance();
996 if (isa<FunctionTemplateDecl>(D))
997 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
998 ->getTemplatedDecl())->isInstance();
999 return false;
1000}
1001
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001002//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001003// DeclaratorDecl Implementation
1004//===----------------------------------------------------------------------===//
1005
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001006template <typename DeclT>
1007static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1008 if (decl->getNumTemplateParameterLists() > 0)
1009 return decl->getTemplateParameterList(0)->getTemplateLoc();
1010 else
1011 return decl->getInnerLocStart();
1012}
1013
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001014SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001015 TypeSourceInfo *TSI = getTypeSourceInfo();
1016 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001017 return SourceLocation();
1018}
1019
Douglas Gregor14454802011-02-25 02:25:35 +00001020void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1021 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001022 // Make sure the extended decl info is allocated.
1023 if (!hasExtInfo()) {
1024 // Save (non-extended) type source info pointer.
1025 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1026 // Allocate external info struct.
1027 DeclInfo = new (getASTContext()) ExtInfo;
1028 // Restore savedTInfo into (extended) decl info.
1029 getExtInfo()->TInfo = savedTInfo;
1030 }
1031 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001032 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001033 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001034 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001035 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001036 if (getExtInfo()->NumTemplParamLists == 0) {
1037 // Save type source info pointer.
1038 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1039 // Deallocate the extended decl info.
1040 getASTContext().Deallocate(getExtInfo());
1041 // Restore savedTInfo into (non-extended) decl info.
1042 DeclInfo = savedTInfo;
1043 }
1044 else
1045 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001046 }
1047 }
1048}
1049
Abramo Bagnara60804e12011-03-18 15:16:37 +00001050void
1051DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1052 unsigned NumTPLists,
1053 TemplateParameterList **TPLists) {
1054 assert(NumTPLists > 0);
1055 // Make sure the extended decl info is allocated.
1056 if (!hasExtInfo()) {
1057 // Save (non-extended) type source info pointer.
1058 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1059 // Allocate external info struct.
1060 DeclInfo = new (getASTContext()) ExtInfo;
1061 // Restore savedTInfo into (extended) decl info.
1062 getExtInfo()->TInfo = savedTInfo;
1063 }
1064 // Set the template parameter lists info.
1065 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1066}
1067
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001068SourceLocation DeclaratorDecl::getOuterLocStart() const {
1069 return getTemplateOrInnerLocStart(this);
1070}
1071
Abramo Bagnaraea947882011-03-08 16:41:52 +00001072namespace {
1073
1074// Helper function: returns true if QT is or contains a type
1075// having a postfix component.
1076bool typeIsPostfix(clang::QualType QT) {
1077 while (true) {
1078 const Type* T = QT.getTypePtr();
1079 switch (T->getTypeClass()) {
1080 default:
1081 return false;
1082 case Type::Pointer:
1083 QT = cast<PointerType>(T)->getPointeeType();
1084 break;
1085 case Type::BlockPointer:
1086 QT = cast<BlockPointerType>(T)->getPointeeType();
1087 break;
1088 case Type::MemberPointer:
1089 QT = cast<MemberPointerType>(T)->getPointeeType();
1090 break;
1091 case Type::LValueReference:
1092 case Type::RValueReference:
1093 QT = cast<ReferenceType>(T)->getPointeeType();
1094 break;
1095 case Type::PackExpansion:
1096 QT = cast<PackExpansionType>(T)->getPattern();
1097 break;
1098 case Type::Paren:
1099 case Type::ConstantArray:
1100 case Type::DependentSizedArray:
1101 case Type::IncompleteArray:
1102 case Type::VariableArray:
1103 case Type::FunctionProto:
1104 case Type::FunctionNoProto:
1105 return true;
1106 }
1107 }
1108}
1109
1110} // namespace
1111
1112SourceRange DeclaratorDecl::getSourceRange() const {
1113 SourceLocation RangeEnd = getLocation();
1114 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1115 if (typeIsPostfix(TInfo->getType()))
1116 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1117 }
1118 return SourceRange(getOuterLocStart(), RangeEnd);
1119}
1120
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001121void
Douglas Gregor20527e22010-06-15 17:44:38 +00001122QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1123 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001124 TemplateParameterList **TPLists) {
1125 assert((NumTPLists == 0 || TPLists != 0) &&
1126 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001127
1128 // Free previous template parameters (if any).
1129 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001130 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001131 TemplParamLists = 0;
1132 NumTemplParamLists = 0;
1133 }
1134 // Set info on matched template parameter lists (if any).
1135 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001136 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001137 NumTemplParamLists = NumTPLists;
1138 for (unsigned i = NumTPLists; i-- > 0; )
1139 TemplParamLists[i] = TPLists[i];
1140 }
1141}
1142
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001143//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001144// VarDecl Implementation
1145//===----------------------------------------------------------------------===//
1146
Sebastian Redl833ef452010-01-26 22:01:41 +00001147const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1148 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001149 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001150 case SC_Auto: return "auto";
1151 case SC_Extern: return "extern";
1152 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1153 case SC_PrivateExtern: return "__private_extern__";
1154 case SC_Register: return "register";
1155 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001156 }
1157
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001158 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001159}
1160
Abramo Bagnaradff19302011-03-08 08:55:46 +00001161VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1162 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001163 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001164 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001165 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001166}
1167
Douglas Gregor72172e92012-01-05 21:55:30 +00001168VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1169 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1170 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1171 QualType(), 0, SC_None, SC_None);
1172}
1173
Douglas Gregorbf62d642010-12-06 18:36:25 +00001174void VarDecl::setStorageClass(StorageClass SC) {
1175 assert(isLegalForVariable(SC));
1176 if (getStorageClass() != SC)
1177 ClearLinkageCache();
1178
John McCallbeaa11c2011-05-01 02:13:58 +00001179 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001180}
1181
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001182SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001183 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001184 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001185 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001186}
1187
Sebastian Redl833ef452010-01-26 22:01:41 +00001188bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001189 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001190 return false;
1191
Eli Friedman839192f2012-01-15 01:23:58 +00001192 const DeclContext *DC = getDeclContext();
1193 if (DC->isRecord())
1194 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001195
Eli Friedman839192f2012-01-15 01:23:58 +00001196 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001197 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001198 return true;
1199 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001200}
1201
1202VarDecl *VarDecl::getCanonicalDecl() {
1203 return getFirstDeclaration();
1204}
1205
Daniel Dunbar9d355812012-03-09 01:51:51 +00001206VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1207 ASTContext &C) const
1208{
Sebastian Redl35351a92010-01-31 22:27:38 +00001209 // C++ [basic.def]p2:
1210 // A declaration is a definition unless [...] it contains the 'extern'
1211 // specifier or a linkage-specification and neither an initializer [...],
1212 // it declares a static data member in a class declaration [...].
1213 // C++ [temp.expl.spec]p15:
1214 // An explicit specialization of a static data member of a template is a
1215 // definition if the declaration includes an initializer; otherwise, it is
1216 // a declaration.
1217 if (isStaticDataMember()) {
1218 if (isOutOfLine() && (hasInit() ||
1219 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1220 return Definition;
1221 else
1222 return DeclarationOnly;
1223 }
1224 // C99 6.7p5:
1225 // A definition of an identifier is a declaration for that identifier that
1226 // [...] causes storage to be reserved for that object.
1227 // Note: that applies for all non-file-scope objects.
1228 // C99 6.9.2p1:
1229 // If the declaration of an identifier for an object has file scope and an
1230 // initializer, the declaration is an external definition for the identifier
1231 if (hasInit())
1232 return Definition;
1233 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1234 if (hasExternalStorage())
1235 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001236
John McCall8e7d6562010-08-26 03:08:43 +00001237 if (getStorageClassAsWritten() == SC_Extern ||
1238 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001239 for (const VarDecl *PrevVar = getPreviousDecl();
1240 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001241 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1242 return DeclarationOnly;
1243 }
1244 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001245 // C99 6.9.2p2:
1246 // A declaration of an object that has file scope without an initializer,
1247 // and without a storage class specifier or the scs 'static', constitutes
1248 // a tentative definition.
1249 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001250 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001251 return TentativeDefinition;
1252
1253 // What's left is (in C, block-scope) declarations without initializers or
1254 // external storage. These are definitions.
1255 return Definition;
1256}
1257
Sebastian Redl35351a92010-01-31 22:27:38 +00001258VarDecl *VarDecl::getActingDefinition() {
1259 DefinitionKind Kind = isThisDeclarationADefinition();
1260 if (Kind != TentativeDefinition)
1261 return 0;
1262
Chris Lattner48eb14d2010-06-14 18:31:46 +00001263 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001264 VarDecl *First = getFirstDeclaration();
1265 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1266 I != E; ++I) {
1267 Kind = (*I)->isThisDeclarationADefinition();
1268 if (Kind == Definition)
1269 return 0;
1270 else if (Kind == TentativeDefinition)
1271 LastTentative = *I;
1272 }
1273 return LastTentative;
1274}
1275
1276bool VarDecl::isTentativeDefinitionNow() const {
1277 DefinitionKind Kind = isThisDeclarationADefinition();
1278 if (Kind != TentativeDefinition)
1279 return false;
1280
1281 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1282 if ((*I)->isThisDeclarationADefinition() == Definition)
1283 return false;
1284 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001285 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001286}
1287
Daniel Dunbar9d355812012-03-09 01:51:51 +00001288VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001289 VarDecl *First = getFirstDeclaration();
1290 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1291 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001292 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001293 return *I;
1294 }
1295 return 0;
1296}
1297
Daniel Dunbar9d355812012-03-09 01:51:51 +00001298VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001299 DefinitionKind Kind = DeclarationOnly;
1300
1301 const VarDecl *First = getFirstDeclaration();
1302 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001303 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001304 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001305 if (Kind == Definition)
1306 break;
1307 }
John McCall37bb6c92010-10-29 22:22:43 +00001308
1309 return Kind;
1310}
1311
Sebastian Redl5ca79842010-02-01 20:16:42 +00001312const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001313 redecl_iterator I = redecls_begin(), E = redecls_end();
1314 while (I != E && !I->getInit())
1315 ++I;
1316
1317 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001318 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001319 return I->getInit();
1320 }
1321 return 0;
1322}
1323
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001324bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001325 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001326 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001327
1328 if (!isStaticDataMember())
1329 return false;
1330
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001331 // If this static data member was instantiated from a static data member of
1332 // a class template, check whether that static data member was defined
1333 // out-of-line.
1334 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1335 return VD->isOutOfLine();
1336
1337 return false;
1338}
1339
Douglas Gregor1d957a32009-10-27 18:42:08 +00001340VarDecl *VarDecl::getOutOfLineDefinition() {
1341 if (!isStaticDataMember())
1342 return 0;
1343
1344 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1345 RD != RDEnd; ++RD) {
1346 if (RD->getLexicalDeclContext()->isFileContext())
1347 return *RD;
1348 }
1349
1350 return 0;
1351}
1352
Douglas Gregord5058122010-02-11 01:19:42 +00001353void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001354 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1355 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001356 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001357 }
1358
1359 Init = I;
1360}
1361
Daniel Dunbar9d355812012-03-09 01:51:51 +00001362bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001363 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001364
Richard Smith35ecb362012-03-02 04:14:40 +00001365 if (!Lang.CPlusPlus)
1366 return false;
1367
1368 // In C++11, any variable of reference type can be used in a constant
1369 // expression if it is initialized by a constant expression.
1370 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1371 return true;
1372
1373 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001374 // not require the variable to be non-volatile, but we consider this to be a
1375 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001376 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001377 return false;
1378
1379 // In C++, const, non-volatile variables of integral or enumeration types
1380 // can be used in constant expressions.
1381 if (getType()->isIntegralOrEnumerationType())
1382 return true;
1383
Richard Smith35ecb362012-03-02 04:14:40 +00001384 // Additionally, in C++11, non-volatile constexpr variables can be used in
1385 // constant expressions.
1386 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001387}
1388
Richard Smithd0b4dd62011-12-19 06:19:21 +00001389/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1390/// form, which contains extra information on the evaluated value of the
1391/// initializer.
1392EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1393 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1394 if (!Eval) {
1395 Stmt *S = Init.get<Stmt *>();
1396 Eval = new (getASTContext()) EvaluatedStmt;
1397 Eval->Value = S;
1398 Init = Eval;
1399 }
1400 return Eval;
1401}
1402
Richard Smithdafff942012-01-14 04:30:29 +00001403APValue *VarDecl::evaluateValue() const {
1404 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1405 return evaluateValue(Notes);
1406}
1407
1408APValue *VarDecl::evaluateValue(
1409 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001410 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1411
1412 // We only produce notes indicating why an initializer is non-constant the
1413 // first time it is evaluated. FIXME: The notes won't always be emitted the
1414 // first time we try evaluation, so might not be produced at all.
1415 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001416 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001417
1418 const Expr *Init = cast<Expr>(Eval->Value);
1419 assert(!Init->isValueDependent());
1420
1421 if (Eval->IsEvaluating) {
1422 // FIXME: Produce a diagnostic for self-initialization.
1423 Eval->CheckedICE = true;
1424 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001425 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001426 }
1427
1428 Eval->IsEvaluating = true;
1429
1430 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1431 this, Notes);
1432
1433 // Ensure the result is an uninitialized APValue if evaluation fails.
1434 if (!Result)
1435 Eval->Evaluated = APValue();
1436
1437 Eval->IsEvaluating = false;
1438 Eval->WasEvaluated = true;
1439
1440 // In C++11, we have determined whether the initializer was a constant
1441 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001442 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001443 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001444 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001445 }
1446
Richard Smithdafff942012-01-14 04:30:29 +00001447 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001448}
1449
1450bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001451 // Initializers of weak variables are never ICEs.
1452 if (isWeak())
1453 return false;
1454
Richard Smithd0b4dd62011-12-19 06:19:21 +00001455 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1456 if (Eval->CheckedICE)
1457 // We have already checked whether this subexpression is an
1458 // integral constant expression.
1459 return Eval->IsICE;
1460
1461 const Expr *Init = cast<Expr>(Eval->Value);
1462 assert(!Init->isValueDependent());
1463
1464 // In C++11, evaluate the initializer to check whether it's a constant
1465 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001466 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001467 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1468 evaluateValue(Notes);
1469 return Eval->IsICE;
1470 }
1471
1472 // It's an ICE whether or not the definition we found is
1473 // out-of-line. See DR 721 and the discussion in Clang PR
1474 // 6206 for details.
1475
1476 if (Eval->CheckingICE)
1477 return false;
1478 Eval->CheckingICE = true;
1479
1480 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1481 Eval->CheckingICE = false;
1482 Eval->CheckedICE = true;
1483 return Eval->IsICE;
1484}
1485
Douglas Gregorfe314812011-06-21 17:03:29 +00001486bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001487 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001488
1489 const Expr *E = getInit();
1490 if (!E)
1491 return false;
1492
1493 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1494 E = Cleanups->getSubExpr();
1495
1496 return isa<MaterializeTemporaryExpr>(E);
1497}
1498
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001499VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001500 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001501 return cast<VarDecl>(MSI->getInstantiatedFrom());
1502
1503 return 0;
1504}
1505
Douglas Gregor3c74d412009-10-14 20:14:33 +00001506TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001507 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001508 return MSI->getTemplateSpecializationKind();
1509
1510 return TSK_Undeclared;
1511}
1512
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001513MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001514 return getASTContext().getInstantiatedFromStaticDataMember(this);
1515}
1516
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001517void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1518 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001519 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001520 assert(MSI && "Not an instantiated static data member?");
1521 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001522 if (TSK != TSK_ExplicitSpecialization &&
1523 PointOfInstantiation.isValid() &&
1524 MSI->getPointOfInstantiation().isInvalid())
1525 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001526}
1527
Sebastian Redl833ef452010-01-26 22:01:41 +00001528//===----------------------------------------------------------------------===//
1529// ParmVarDecl Implementation
1530//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001531
Sebastian Redl833ef452010-01-26 22:01:41 +00001532ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001533 SourceLocation StartLoc,
1534 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001535 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001536 StorageClass S, StorageClass SCAsWritten,
1537 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001538 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001539 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001540}
1541
Douglas Gregor72172e92012-01-05 21:55:30 +00001542ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1543 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1544 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1545 0, QualType(), 0, SC_None, SC_None, 0);
1546}
1547
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001548SourceRange ParmVarDecl::getSourceRange() const {
1549 if (!hasInheritedDefaultArg()) {
1550 SourceRange ArgRange = getDefaultArgRange();
1551 if (ArgRange.isValid())
1552 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1553 }
1554
1555 return DeclaratorDecl::getSourceRange();
1556}
1557
Sebastian Redl833ef452010-01-26 22:01:41 +00001558Expr *ParmVarDecl::getDefaultArg() {
1559 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1560 assert(!hasUninstantiatedDefaultArg() &&
1561 "Default argument is not yet instantiated!");
1562
1563 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001564 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001565 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001566
Sebastian Redl833ef452010-01-26 22:01:41 +00001567 return Arg;
1568}
1569
Sebastian Redl833ef452010-01-26 22:01:41 +00001570SourceRange ParmVarDecl::getDefaultArgRange() const {
1571 if (const Expr *E = getInit())
1572 return E->getSourceRange();
1573
1574 if (hasUninstantiatedDefaultArg())
1575 return getUninstantiatedDefaultArg()->getSourceRange();
1576
1577 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001578}
1579
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001580bool ParmVarDecl::isParameterPack() const {
1581 return isa<PackExpansionType>(getType());
1582}
1583
Ted Kremenek540017e2011-10-06 05:00:56 +00001584void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1585 getASTContext().setParameterIndex(this, parameterIndex);
1586 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1587}
1588
1589unsigned ParmVarDecl::getParameterIndexLarge() const {
1590 return getASTContext().getParameterIndex(this);
1591}
1592
Nuno Lopes394ec982008-12-17 23:39:55 +00001593//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001594// FunctionDecl Implementation
1595//===----------------------------------------------------------------------===//
1596
Douglas Gregorb11aad82011-02-19 18:51:44 +00001597void FunctionDecl::getNameForDiagnostic(std::string &S,
1598 const PrintingPolicy &Policy,
1599 bool Qualified) const {
1600 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1601 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1602 if (TemplateArgs)
1603 S += TemplateSpecializationType::PrintTemplateArgumentList(
1604 TemplateArgs->data(),
1605 TemplateArgs->size(),
1606 Policy);
1607
1608}
1609
Ted Kremenek186a0742010-04-29 16:49:01 +00001610bool FunctionDecl::isVariadic() const {
1611 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1612 return FT->isVariadic();
1613 return false;
1614}
1615
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001616bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1617 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001618 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001619 Definition = *I;
1620 return true;
1621 }
1622 }
1623
1624 return false;
1625}
1626
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001627bool FunctionDecl::hasTrivialBody() const
1628{
1629 Stmt *S = getBody();
1630 if (!S) {
1631 // Since we don't have a body for this function, we don't know if it's
1632 // trivial or not.
1633 return false;
1634 }
1635
1636 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1637 return true;
1638 return false;
1639}
1640
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001641bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1642 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001643 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001644 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1645 return true;
1646 }
1647 }
1648
1649 return false;
1650}
1651
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001652Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001653 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1654 if (I->Body) {
1655 Definition = *I;
1656 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001657 } else if (I->IsLateTemplateParsed) {
1658 Definition = *I;
1659 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001660 }
1661 }
1662
1663 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001664}
1665
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001666void FunctionDecl::setBody(Stmt *B) {
1667 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001668 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001669 EndRangeLoc = B->getLocEnd();
1670}
1671
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001672void FunctionDecl::setPure(bool P) {
1673 IsPure = P;
1674 if (P)
1675 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1676 Parent->markedVirtualFunctionPure();
1677}
1678
Douglas Gregor16618f22009-09-12 00:17:51 +00001679bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001680 const TranslationUnitDecl *tunit =
1681 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1682 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001683 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001684 getIdentifier() &&
1685 getIdentifier()->isStr("main");
1686}
1687
1688bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1689 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1690 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1691 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1692 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1693 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1694
1695 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1696 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1697
1698 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1699 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1700
1701 ASTContext &Context =
1702 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1703 ->getASTContext();
1704
1705 // The result type and first argument type are constant across all
1706 // these operators. The second argument must be exactly void*.
1707 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001708}
1709
Douglas Gregor16618f22009-09-12 00:17:51 +00001710bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001711 if (getLinkage() != ExternalLinkage)
1712 return false;
1713
1714 if (getAttr<OverloadableAttr>())
1715 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001716
Chandler Carruth4322a282011-02-25 00:05:02 +00001717 const DeclContext *DC = getDeclContext();
1718 if (DC->isRecord())
1719 return false;
1720
Eli Friedman839192f2012-01-15 01:23:58 +00001721 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001722 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001723 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001724
Eli Friedman839192f2012-01-15 01:23:58 +00001725 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001726}
1727
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001728bool FunctionDecl::isGlobal() const {
1729 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1730 return Method->isStatic();
1731
John McCall8e7d6562010-08-26 03:08:43 +00001732 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001733 return false;
1734
Mike Stump11289f42009-09-09 15:08:12 +00001735 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001736 DC->isNamespace();
1737 DC = DC->getParent()) {
1738 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1739 if (!Namespace->getDeclName())
1740 return false;
1741 break;
1742 }
1743 }
1744
1745 return true;
1746}
1747
Sebastian Redl833ef452010-01-26 22:01:41 +00001748void
1749FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1750 redeclarable_base::setPreviousDeclaration(PrevDecl);
1751
1752 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1753 FunctionTemplateDecl *PrevFunTmpl
1754 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1755 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1756 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1757 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001758
Axel Naumannfbc7b982011-11-08 18:21:06 +00001759 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001760 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001761}
1762
1763const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1764 return getFirstDeclaration();
1765}
1766
1767FunctionDecl *FunctionDecl::getCanonicalDecl() {
1768 return getFirstDeclaration();
1769}
1770
Douglas Gregorbf62d642010-12-06 18:36:25 +00001771void FunctionDecl::setStorageClass(StorageClass SC) {
1772 assert(isLegalForFunction(SC));
1773 if (getStorageClass() != SC)
1774 ClearLinkageCache();
1775
1776 SClass = SC;
1777}
1778
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001779/// \brief Returns a value indicating whether this function
1780/// corresponds to a builtin function.
1781///
1782/// The function corresponds to a built-in function if it is
1783/// declared at translation scope or within an extern "C" block and
1784/// its name matches with the name of a builtin. The returned value
1785/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001786/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001787/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001788unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001789 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001790 return 0;
1791
1792 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001793 if (!BuiltinID)
1794 return 0;
1795
1796 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001797 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1798 return BuiltinID;
1799
1800 // This function has the name of a known C library
1801 // function. Determine whether it actually refers to the C library
1802 // function or whether it just has the same name.
1803
Douglas Gregora908e7f2009-02-17 03:23:10 +00001804 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001805 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001806 return 0;
1807
Douglas Gregore711f702009-02-14 18:57:46 +00001808 // If this function is at translation-unit scope and we're not in
1809 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001810 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001811 getDeclContext()->isTranslationUnit())
1812 return BuiltinID;
1813
1814 // If the function is in an extern "C" linkage specification and is
1815 // not marked "overloadable", it's the real function.
1816 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001817 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001818 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001819 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001820 return BuiltinID;
1821
1822 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001823 return 0;
1824}
1825
1826
Chris Lattner47c0d002009-04-25 06:03:53 +00001827/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001828/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001829/// after it has been created.
1830unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001831 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001832 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001833 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001834 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001835
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001836}
1837
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001838void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001839 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001840 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001841 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001842
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001843 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001844 if (!NewParamInfo.empty()) {
1845 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1846 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001847 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001848}
Chris Lattner41943152007-01-25 04:52:46 +00001849
James Molloy6f8780b2012-02-29 10:24:19 +00001850void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1851 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1852
1853 if (!NewDecls.empty()) {
1854 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1855 std::copy(NewDecls.begin(), NewDecls.end(), A);
1856 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1857 }
1858}
1859
Chris Lattner58258242008-04-10 02:22:51 +00001860/// getMinRequiredArguments - Returns the minimum number of arguments
1861/// needed to call this function. This may be fewer than the number of
1862/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001863/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001864unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001865 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001866 return getNumParams();
1867
Douglas Gregor7825bf32011-01-06 22:09:01 +00001868 unsigned NumRequiredArgs = getNumParams();
1869
1870 // If the last parameter is a parameter pack, we don't need an argument for
1871 // it.
1872 if (NumRequiredArgs > 0 &&
1873 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1874 --NumRequiredArgs;
1875
1876 // If this parameter has a default argument, we don't need an argument for
1877 // it.
1878 while (NumRequiredArgs > 0 &&
1879 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001880 --NumRequiredArgs;
1881
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001882 // We might have parameter packs before the end. These can't be deduced,
1883 // but they can still handle multiple arguments.
1884 unsigned ArgIdx = NumRequiredArgs;
1885 while (ArgIdx > 0) {
1886 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1887 NumRequiredArgs = ArgIdx;
1888
1889 --ArgIdx;
1890 }
1891
Chris Lattner58258242008-04-10 02:22:51 +00001892 return NumRequiredArgs;
1893}
1894
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001895bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001896 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001897 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001898
1899 if (isa<CXXMethodDecl>(this)) {
1900 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1901 return true;
1902 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001903
1904 switch (getTemplateSpecializationKind()) {
1905 case TSK_Undeclared:
1906 case TSK_ExplicitSpecialization:
1907 return false;
1908
1909 case TSK_ImplicitInstantiation:
1910 case TSK_ExplicitInstantiationDeclaration:
1911 case TSK_ExplicitInstantiationDefinition:
1912 // Handle below.
1913 break;
1914 }
1915
1916 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001917 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001918 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001919 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001920
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001921 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001922 return PatternDecl->isInlined();
1923
1924 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001925}
1926
Eli Friedman1b125c32012-02-07 03:50:18 +00001927static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1928 // Only consider file-scope declarations in this test.
1929 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1930 return false;
1931
1932 // Only consider explicit declarations; the presence of a builtin for a
1933 // libcall shouldn't affect whether a definition is externally visible.
1934 if (Redecl->isImplicit())
1935 return false;
1936
1937 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1938 return true; // Not an inline definition
1939
1940 return false;
1941}
1942
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001943/// \brief For a function declaration in C or C++, determine whether this
1944/// declaration causes the definition to be externally visible.
1945///
Eli Friedman1b125c32012-02-07 03:50:18 +00001946/// Specifically, this determines if adding the current declaration to the set
1947/// of redeclarations of the given functions causes
1948/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001949bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1950 assert(!doesThisDeclarationHaveABody() &&
1951 "Must have a declaration without a body.");
1952
1953 ASTContext &Context = getASTContext();
1954
David Blaikiebbafb8a2012-03-11 07:00:24 +00001955 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001956 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1957 // an externally visible definition.
1958 //
1959 // FIXME: What happens if gnu_inline gets added on after the first
1960 // declaration?
1961 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1962 return false;
1963
1964 const FunctionDecl *Prev = this;
1965 bool FoundBody = false;
1966 while ((Prev = Prev->getPreviousDecl())) {
1967 FoundBody |= Prev->Body;
1968
1969 if (Prev->Body) {
1970 // If it's not the case that both 'inline' and 'extern' are
1971 // specified on the definition, then it is always externally visible.
1972 if (!Prev->isInlineSpecified() ||
1973 Prev->getStorageClassAsWritten() != SC_Extern)
1974 return false;
1975 } else if (Prev->isInlineSpecified() &&
1976 Prev->getStorageClassAsWritten() != SC_Extern) {
1977 return false;
1978 }
1979 }
1980 return FoundBody;
1981 }
1982
David Blaikiebbafb8a2012-03-11 07:00:24 +00001983 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001984 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001985
1986 // C99 6.7.4p6:
1987 // [...] If all of the file scope declarations for a function in a
1988 // translation unit include the inline function specifier without extern,
1989 // then the definition in that translation unit is an inline definition.
1990 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001991 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001992 const FunctionDecl *Prev = this;
1993 bool FoundBody = false;
1994 while ((Prev = Prev->getPreviousDecl())) {
1995 FoundBody |= Prev->Body;
1996 if (RedeclForcesDefC99(Prev))
1997 return false;
1998 }
1999 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002000}
2001
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002002/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002003/// definition will be externally visible.
2004///
2005/// Inline function definitions are always available for inlining optimizations.
2006/// However, depending on the language dialect, declaration specifiers, and
2007/// attributes, the definition of an inline function may or may not be
2008/// "externally" visible to other translation units in the program.
2009///
2010/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002011/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002012/// inline definition becomes externally visible (C99 6.7.4p6).
2013///
2014/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2015/// definition, we use the GNU semantics for inline, which are nearly the
2016/// opposite of C99 semantics. In particular, "inline" by itself will create
2017/// an externally visible symbol, but "extern inline" will not create an
2018/// externally visible symbol.
2019bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002020 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002021 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002022 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002023
David Blaikiebbafb8a2012-03-11 07:00:24 +00002024 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002025 // Note: If you change the logic here, please change
2026 // doesDeclarationForceExternallyVisibleDefinition as well.
2027 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002028 // If it's not the case that both 'inline' and 'extern' are
2029 // specified on the definition, then this inline definition is
2030 // externally visible.
2031 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2032 return true;
2033
2034 // If any declaration is 'inline' but not 'extern', then this definition
2035 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002036 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2037 Redecl != RedeclEnd;
2038 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002039 if (Redecl->isInlineSpecified() &&
2040 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002041 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002042 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002043
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002044 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002045 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002046
Douglas Gregor299d76e2009-09-13 07:46:26 +00002047 // C99 6.7.4p6:
2048 // [...] If all of the file scope declarations for a function in a
2049 // translation unit include the inline function specifier without extern,
2050 // then the definition in that translation unit is an inline definition.
2051 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2052 Redecl != RedeclEnd;
2053 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002054 if (RedeclForcesDefC99(*Redecl))
2055 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002056 }
2057
2058 // C99 6.7.4p6:
2059 // An inline definition does not provide an external definition for the
2060 // function, and does not forbid an external definition in another
2061 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002062 return false;
2063}
2064
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002065/// getOverloadedOperator - Which C++ overloaded operator this
2066/// function represents, if any.
2067OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002068 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2069 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002070 else
2071 return OO_None;
2072}
2073
Alexis Huntc88db062010-01-13 09:01:02 +00002074/// getLiteralIdentifier - The literal suffix identifier this function
2075/// represents, if any.
2076const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2077 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2078 return getDeclName().getCXXLiteralIdentifier();
2079 else
2080 return 0;
2081}
2082
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002083FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2084 if (TemplateOrSpecialization.isNull())
2085 return TK_NonTemplate;
2086 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2087 return TK_FunctionTemplate;
2088 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2089 return TK_MemberSpecialization;
2090 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2091 return TK_FunctionTemplateSpecialization;
2092 if (TemplateOrSpecialization.is
2093 <DependentFunctionTemplateSpecializationInfo*>())
2094 return TK_DependentFunctionTemplateSpecialization;
2095
David Blaikie83d382b2011-09-23 05:06:16 +00002096 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002097}
2098
Douglas Gregord801b062009-10-07 23:56:10 +00002099FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002100 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002101 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2102
2103 return 0;
2104}
2105
Douglas Gregor06db9f52009-10-12 20:18:28 +00002106MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2107 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2108}
2109
Douglas Gregord801b062009-10-07 23:56:10 +00002110void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002111FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2112 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002113 TemplateSpecializationKind TSK) {
2114 assert(TemplateOrSpecialization.isNull() &&
2115 "Member function is already a specialization");
2116 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002117 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002118 TemplateOrSpecialization = Info;
2119}
2120
Douglas Gregorafca3b42009-10-27 20:53:28 +00002121bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002122 // If the function is invalid, it can't be implicitly instantiated.
2123 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002124 return false;
2125
2126 switch (getTemplateSpecializationKind()) {
2127 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002128 case TSK_ExplicitInstantiationDefinition:
2129 return false;
2130
2131 case TSK_ImplicitInstantiation:
2132 return true;
2133
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002134 // It is possible to instantiate TSK_ExplicitSpecialization kind
2135 // if the FunctionDecl has a class scope specialization pattern.
2136 case TSK_ExplicitSpecialization:
2137 return getClassScopeSpecializationPattern() != 0;
2138
Douglas Gregorafca3b42009-10-27 20:53:28 +00002139 case TSK_ExplicitInstantiationDeclaration:
2140 // Handled below.
2141 break;
2142 }
2143
2144 // Find the actual template from which we will instantiate.
2145 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002146 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002147 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002148 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002149
2150 // C++0x [temp.explicit]p9:
2151 // Except for inline functions, other explicit instantiation declarations
2152 // have the effect of suppressing the implicit instantiation of the entity
2153 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002154 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002155 return true;
2156
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002157 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002158}
2159
2160bool FunctionDecl::isTemplateInstantiation() const {
2161 switch (getTemplateSpecializationKind()) {
2162 case TSK_Undeclared:
2163 case TSK_ExplicitSpecialization:
2164 return false;
2165 case TSK_ImplicitInstantiation:
2166 case TSK_ExplicitInstantiationDeclaration:
2167 case TSK_ExplicitInstantiationDefinition:
2168 return true;
2169 }
2170 llvm_unreachable("All TSK values handled.");
2171}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002172
2173FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002174 // Handle class scope explicit specialization special case.
2175 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2176 return getClassScopeSpecializationPattern();
2177
Douglas Gregorafca3b42009-10-27 20:53:28 +00002178 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2179 while (Primary->getInstantiatedFromMemberTemplate()) {
2180 // If we have hit a point where the user provided a specialization of
2181 // this template, we're done looking.
2182 if (Primary->isMemberSpecialization())
2183 break;
2184
2185 Primary = Primary->getInstantiatedFromMemberTemplate();
2186 }
2187
2188 return Primary->getTemplatedDecl();
2189 }
2190
2191 return getInstantiatedFromMemberFunction();
2192}
2193
Douglas Gregor70d83e22009-06-29 17:30:29 +00002194FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002195 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002196 = TemplateOrSpecialization
2197 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002198 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002199 }
2200 return 0;
2201}
2202
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002203FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2204 return getASTContext().getClassScopeSpecializationPattern(this);
2205}
2206
Douglas Gregor70d83e22009-06-29 17:30:29 +00002207const TemplateArgumentList *
2208FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002209 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002210 = TemplateOrSpecialization
2211 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002212 return Info->TemplateArguments;
2213 }
2214 return 0;
2215}
2216
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002217const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002218FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2219 if (FunctionTemplateSpecializationInfo *Info
2220 = TemplateOrSpecialization
2221 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2222 return Info->TemplateArgumentsAsWritten;
2223 }
2224 return 0;
2225}
2226
Mike Stump11289f42009-09-09 15:08:12 +00002227void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002228FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2229 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002230 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002231 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002232 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002233 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2234 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002235 assert(TSK != TSK_Undeclared &&
2236 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002237 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002238 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002239 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002240 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2241 TemplateArgs,
2242 TemplateArgsAsWritten,
2243 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002244 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002245 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002246}
2247
John McCallb9c78482010-04-08 09:05:18 +00002248void
2249FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2250 const UnresolvedSetImpl &Templates,
2251 const TemplateArgumentListInfo &TemplateArgs) {
2252 assert(TemplateOrSpecialization.isNull());
2253 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2254 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002255 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002256 void *Buffer = Context.Allocate(Size);
2257 DependentFunctionTemplateSpecializationInfo *Info =
2258 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2259 TemplateArgs);
2260 TemplateOrSpecialization = Info;
2261}
2262
2263DependentFunctionTemplateSpecializationInfo::
2264DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2265 const TemplateArgumentListInfo &TArgs)
2266 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2267
2268 d.NumTemplates = Ts.size();
2269 d.NumArgs = TArgs.size();
2270
2271 FunctionTemplateDecl **TsArray =
2272 const_cast<FunctionTemplateDecl**>(getTemplates());
2273 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2274 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2275
2276 TemplateArgumentLoc *ArgsArray =
2277 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2278 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2279 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2280}
2281
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002282TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002283 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002284 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002285 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002286 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002287 if (FTSInfo)
2288 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002289
Douglas Gregord801b062009-10-07 23:56:10 +00002290 MemberSpecializationInfo *MSInfo
2291 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2292 if (MSInfo)
2293 return MSInfo->getTemplateSpecializationKind();
2294
2295 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002296}
2297
Mike Stump11289f42009-09-09 15:08:12 +00002298void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002299FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2300 SourceLocation PointOfInstantiation) {
2301 if (FunctionTemplateSpecializationInfo *FTSInfo
2302 = TemplateOrSpecialization.dyn_cast<
2303 FunctionTemplateSpecializationInfo*>()) {
2304 FTSInfo->setTemplateSpecializationKind(TSK);
2305 if (TSK != TSK_ExplicitSpecialization &&
2306 PointOfInstantiation.isValid() &&
2307 FTSInfo->getPointOfInstantiation().isInvalid())
2308 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2309 } else if (MemberSpecializationInfo *MSInfo
2310 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2311 MSInfo->setTemplateSpecializationKind(TSK);
2312 if (TSK != TSK_ExplicitSpecialization &&
2313 PointOfInstantiation.isValid() &&
2314 MSInfo->getPointOfInstantiation().isInvalid())
2315 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2316 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002317 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002318}
2319
2320SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002321 if (FunctionTemplateSpecializationInfo *FTSInfo
2322 = TemplateOrSpecialization.dyn_cast<
2323 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002324 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002325 else if (MemberSpecializationInfo *MSInfo
2326 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002327 return MSInfo->getPointOfInstantiation();
2328
2329 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002330}
2331
Douglas Gregor6411b922009-09-11 20:15:17 +00002332bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002333 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002334 return true;
2335
2336 // If this function was instantiated from a member function of a
2337 // class template, check whether that member function was defined out-of-line.
2338 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2339 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002340 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002341 return Definition->isOutOfLine();
2342 }
2343
2344 // If this function was instantiated from a function template,
2345 // check whether that function template was defined out-of-line.
2346 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2347 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002348 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002349 return Definition->isOutOfLine();
2350 }
2351
2352 return false;
2353}
2354
Abramo Bagnaraea947882011-03-08 16:41:52 +00002355SourceRange FunctionDecl::getSourceRange() const {
2356 return SourceRange(getOuterLocStart(), EndRangeLoc);
2357}
2358
Anna Zaks28db7ce2012-01-18 02:45:01 +00002359unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002360 IdentifierInfo *FnInfo = getIdentifier();
2361
2362 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002363 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002364
2365 // Builtin handling.
2366 switch (getBuiltinID()) {
2367 case Builtin::BI__builtin_memset:
2368 case Builtin::BI__builtin___memset_chk:
2369 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002370 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002371
2372 case Builtin::BI__builtin_memcpy:
2373 case Builtin::BI__builtin___memcpy_chk:
2374 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002375 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002376
2377 case Builtin::BI__builtin_memmove:
2378 case Builtin::BI__builtin___memmove_chk:
2379 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002380 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002381
2382 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002383 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002384 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002385 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002386
2387 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002388 case Builtin::BImemcmp:
2389 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002390
2391 case Builtin::BI__builtin_strncpy:
2392 case Builtin::BI__builtin___strncpy_chk:
2393 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002394 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002395
2396 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002397 case Builtin::BIstrncmp:
2398 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002399
2400 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002401 case Builtin::BIstrncasecmp:
2402 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002403
2404 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002405 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002406 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002407 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002408
2409 case Builtin::BI__builtin_strndup:
2410 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002411 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002412
Anna Zaks314cd092012-02-01 19:08:57 +00002413 case Builtin::BI__builtin_strlen:
2414 case Builtin::BIstrlen:
2415 return Builtin::BIstrlen;
2416
Anna Zaks201d4892012-01-13 21:52:01 +00002417 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002418 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002419 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002420 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002421 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002422 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002423 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002424 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002425 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002426 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002427 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002428 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002429 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002430 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002431 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002432 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002433 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002434 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002435 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002436 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002437 else if (FnInfo->isStr("strlen"))
2438 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002439 }
2440 break;
2441 }
Anna Zaks22122702012-01-17 00:37:07 +00002442 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002443}
2444
Chris Lattner59a25942008-03-31 00:36:02 +00002445//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002446// FieldDecl Implementation
2447//===----------------------------------------------------------------------===//
2448
Jay Foad39c79802011-01-12 09:06:06 +00002449FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002450 SourceLocation StartLoc, SourceLocation IdLoc,
2451 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002452 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2453 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002454 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002455 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002456}
2457
Douglas Gregor72172e92012-01-05 21:55:30 +00002458FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2459 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2460 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2461 0, QualType(), 0, 0, false, false);
2462}
2463
Sebastian Redl833ef452010-01-26 22:01:41 +00002464bool FieldDecl::isAnonymousStructOrUnion() const {
2465 if (!isImplicit() || getDeclName())
2466 return false;
2467
2468 if (const RecordType *Record = getType()->getAs<RecordType>())
2469 return Record->getDecl()->isAnonymousStructOrUnion();
2470
2471 return false;
2472}
2473
Richard Smithcaf33902011-10-10 18:28:20 +00002474unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2475 assert(isBitField() && "not a bitfield");
2476 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2477 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2478}
2479
John McCall4e819612011-01-20 07:57:12 +00002480unsigned FieldDecl::getFieldIndex() const {
2481 if (CachedFieldIndex) return CachedFieldIndex - 1;
2482
Richard Smithd62306a2011-11-10 06:34:14 +00002483 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002484 const RecordDecl *RD = getParent();
2485 const FieldDecl *LastFD = 0;
2486 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002487
2488 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2489 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002490 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002491
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002492 if (IsMsStruct) {
2493 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie2d7c57e2012-04-30 02:36:29 +00002494 if (getASTContext().ZeroBitfieldFollowsNonBitfield(&*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002495 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002496 continue;
2497 }
David Blaikie2d7c57e2012-04-30 02:36:29 +00002498 LastFD = &*I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002499 }
John McCall4e819612011-01-20 07:57:12 +00002500 }
2501
Richard Smithd62306a2011-11-10 06:34:14 +00002502 assert(CachedFieldIndex && "failed to find field in parent");
2503 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002504}
2505
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002506SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002507 if (const Expr *E = InitializerOrBitWidth.getPointer())
2508 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002509 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002510}
2511
Richard Smith938f40b2011-06-11 17:19:42 +00002512void FieldDecl::setInClassInitializer(Expr *Init) {
2513 assert(!InitializerOrBitWidth.getPointer() &&
2514 "bit width or initializer already set");
2515 InitializerOrBitWidth.setPointer(Init);
2516 InitializerOrBitWidth.setInt(0);
2517}
2518
Sebastian Redl833ef452010-01-26 22:01:41 +00002519//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002520// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002521//===----------------------------------------------------------------------===//
2522
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002523SourceLocation TagDecl::getOuterLocStart() const {
2524 return getTemplateOrInnerLocStart(this);
2525}
2526
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002527SourceRange TagDecl::getSourceRange() const {
2528 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002529 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002530}
2531
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002532TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002533 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002534}
2535
Richard Smithdda56e42011-04-15 14:24:37 +00002536void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2537 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002538 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002539 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002540 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002541}
2542
Douglas Gregordee1be82009-01-17 00:42:38 +00002543void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002544 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002545
2546 if (isa<CXXRecordDecl>(this)) {
2547 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2548 struct CXXRecordDecl::DefinitionData *Data =
2549 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002550 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2551 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002552 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002553}
2554
2555void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002556 assert((!isa<CXXRecordDecl>(this) ||
2557 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2558 "definition completed but not started");
2559
John McCallf937c022011-10-07 06:10:15 +00002560 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002561 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002562
2563 if (ASTMutationListener *L = getASTMutationListener())
2564 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002565}
2566
John McCallf937c022011-10-07 06:10:15 +00002567TagDecl *TagDecl::getDefinition() const {
2568 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002569 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002570 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2571 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002572
2573 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002574 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002575 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002576 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002577
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002578 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002579}
2580
Douglas Gregor14454802011-02-25 02:25:35 +00002581void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2582 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002583 // Make sure the extended qualifier info is allocated.
2584 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002585 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002586 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002587 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002588 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002589 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002590 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002591 if (getExtInfo()->NumTemplParamLists == 0) {
2592 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002593 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002594 }
2595 else
2596 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002597 }
2598 }
2599}
2600
Abramo Bagnara60804e12011-03-18 15:16:37 +00002601void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2602 unsigned NumTPLists,
2603 TemplateParameterList **TPLists) {
2604 assert(NumTPLists > 0);
2605 // Make sure the extended decl info is allocated.
2606 if (!hasExtInfo())
2607 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002608 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002609 // Set the template parameter lists info.
2610 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2611}
2612
Ted Kremenek21475702008-09-05 17:16:31 +00002613//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002614// EnumDecl Implementation
2615//===----------------------------------------------------------------------===//
2616
David Blaikie68e081d2011-12-20 02:48:34 +00002617void EnumDecl::anchor() { }
2618
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002619EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2620 SourceLocation StartLoc, SourceLocation IdLoc,
2621 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002622 EnumDecl *PrevDecl, bool IsScoped,
2623 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002624 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002625 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002626 C.getTypeDeclType(Enum, PrevDecl);
2627 return Enum;
2628}
2629
Douglas Gregor72172e92012-01-05 21:55:30 +00002630EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2631 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2632 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2633 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002634}
2635
Douglas Gregord5058122010-02-11 01:19:42 +00002636void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002637 QualType NewPromotionType,
2638 unsigned NumPositiveBits,
2639 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002640 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002641 if (!IntegerType)
2642 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002643 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002644 setNumPositiveBits(NumPositiveBits);
2645 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002646 TagDecl::completeDefinition();
2647}
2648
Richard Smith7d137e32012-03-23 03:33:32 +00002649TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2650 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2651 return MSI->getTemplateSpecializationKind();
2652
2653 return TSK_Undeclared;
2654}
2655
2656void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2657 SourceLocation PointOfInstantiation) {
2658 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2659 assert(MSI && "Not an instantiated member enumeration?");
2660 MSI->setTemplateSpecializationKind(TSK);
2661 if (TSK != TSK_ExplicitSpecialization &&
2662 PointOfInstantiation.isValid() &&
2663 MSI->getPointOfInstantiation().isInvalid())
2664 MSI->setPointOfInstantiation(PointOfInstantiation);
2665}
2666
Richard Smith4b38ded2012-03-14 23:13:10 +00002667EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2668 if (SpecializationInfo)
2669 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2670
2671 return 0;
2672}
2673
2674void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2675 TemplateSpecializationKind TSK) {
2676 assert(!SpecializationInfo && "Member enum is already a specialization");
2677 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2678}
2679
Sebastian Redl833ef452010-01-26 22:01:41 +00002680//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002681// RecordDecl Implementation
2682//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002683
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002684RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2685 SourceLocation StartLoc, SourceLocation IdLoc,
2686 IdentifierInfo *Id, RecordDecl *PrevDecl)
2687 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002688 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002689 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002690 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002691 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002692 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002693}
2694
Jay Foad39c79802011-01-12 09:06:06 +00002695RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002696 SourceLocation StartLoc, SourceLocation IdLoc,
2697 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2698 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2699 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002700 C.getTypeDeclType(R, PrevDecl);
2701 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002702}
2703
Douglas Gregor72172e92012-01-05 21:55:30 +00002704RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2705 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2706 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2707 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002708}
2709
Douglas Gregordfcad112009-03-25 15:59:44 +00002710bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002711 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002712 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2713}
2714
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002715RecordDecl::field_iterator RecordDecl::field_begin() const {
2716 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2717 LoadFieldsFromExternalStorage();
2718
2719 return field_iterator(decl_iterator(FirstDecl));
2720}
2721
Douglas Gregorb11aad82011-02-19 18:51:44 +00002722/// completeDefinition - Notes that the definition of this type is now
2723/// complete.
2724void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002725 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002726 TagDecl::completeDefinition();
2727}
2728
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002729void RecordDecl::LoadFieldsFromExternalStorage() const {
2730 ExternalASTSource *Source = getASTContext().getExternalSource();
2731 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2732
2733 // Notify that we have a RecordDecl doing some initialization.
2734 ExternalASTSource::Deserializing TheFields(Source);
2735
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002736 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002737 LoadedFieldsFromExternalStorage = true;
2738 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2739 case ELR_Success:
2740 break;
2741
2742 case ELR_AlreadyLoaded:
2743 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002744 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002745 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002746
2747#ifndef NDEBUG
2748 // Check that all decls we got were FieldDecls.
2749 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2750 assert(isa<FieldDecl>(Decls[i]));
2751#endif
2752
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002753 if (Decls.empty())
2754 return;
2755
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002756 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2757 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002758}
2759
Steve Naroff415d3d52008-10-08 17:01:13 +00002760//===----------------------------------------------------------------------===//
2761// BlockDecl Implementation
2762//===----------------------------------------------------------------------===//
2763
David Blaikie9c70e042011-09-21 18:16:56 +00002764void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002765 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002766
Steve Naroffc4b30e52009-03-13 16:56:44 +00002767 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002768 if (!NewParamInfo.empty()) {
2769 NumParams = NewParamInfo.size();
2770 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2771 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002772 }
2773}
2774
John McCall351762c2011-02-07 10:33:21 +00002775void BlockDecl::setCaptures(ASTContext &Context,
2776 const Capture *begin,
2777 const Capture *end,
2778 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002779 CapturesCXXThis = capturesCXXThis;
2780
2781 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002782 NumCaptures = 0;
2783 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002784 return;
2785 }
2786
John McCall351762c2011-02-07 10:33:21 +00002787 NumCaptures = end - begin;
2788
2789 // Avoid new Capture[] because we don't want to provide a default
2790 // constructor.
2791 size_t allocationSize = NumCaptures * sizeof(Capture);
2792 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2793 memcpy(buffer, begin, allocationSize);
2794 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002795}
Sebastian Redl833ef452010-01-26 22:01:41 +00002796
John McCallce45f882011-06-15 22:51:16 +00002797bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2798 for (capture_const_iterator
2799 i = capture_begin(), e = capture_end(); i != e; ++i)
2800 // Only auto vars can be captured, so no redeclaration worries.
2801 if (i->getVariable() == variable)
2802 return true;
2803
2804 return false;
2805}
2806
Douglas Gregor70226da2010-12-21 16:27:07 +00002807SourceRange BlockDecl::getSourceRange() const {
2808 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2809}
Sebastian Redl833ef452010-01-26 22:01:41 +00002810
2811//===----------------------------------------------------------------------===//
2812// Other Decl Allocation/Deallocation Method Implementations
2813//===----------------------------------------------------------------------===//
2814
David Blaikie68e081d2011-12-20 02:48:34 +00002815void TranslationUnitDecl::anchor() { }
2816
Sebastian Redl833ef452010-01-26 22:01:41 +00002817TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2818 return new (C) TranslationUnitDecl(C);
2819}
2820
David Blaikie68e081d2011-12-20 02:48:34 +00002821void LabelDecl::anchor() { }
2822
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002823LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002824 SourceLocation IdentL, IdentifierInfo *II) {
2825 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2826}
2827
2828LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2829 SourceLocation IdentL, IdentifierInfo *II,
2830 SourceLocation GnuLabelL) {
2831 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2832 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002833}
2834
Douglas Gregor72172e92012-01-05 21:55:30 +00002835LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2836 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2837 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002838}
2839
David Blaikie68e081d2011-12-20 02:48:34 +00002840void ValueDecl::anchor() { }
2841
2842void ImplicitParamDecl::anchor() { }
2843
Sebastian Redl833ef452010-01-26 22:01:41 +00002844ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002845 SourceLocation IdLoc,
2846 IdentifierInfo *Id,
2847 QualType Type) {
2848 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002849}
2850
Douglas Gregor72172e92012-01-05 21:55:30 +00002851ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2852 unsigned ID) {
2853 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2854 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2855}
2856
Sebastian Redl833ef452010-01-26 22:01:41 +00002857FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002858 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002859 const DeclarationNameInfo &NameInfo,
2860 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002861 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002862 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002863 bool hasWrittenPrototype,
2864 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002865 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2866 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002867 isInlineSpecified,
2868 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002869 New->HasWrittenPrototype = hasWrittenPrototype;
2870 return New;
2871}
2872
Douglas Gregor72172e92012-01-05 21:55:30 +00002873FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2874 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2875 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2876 DeclarationNameInfo(), QualType(), 0,
2877 SC_None, SC_None, false, false);
2878}
2879
Sebastian Redl833ef452010-01-26 22:01:41 +00002880BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2881 return new (C) BlockDecl(DC, L);
2882}
2883
Douglas Gregor72172e92012-01-05 21:55:30 +00002884BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2885 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2886 return new (Mem) BlockDecl(0, SourceLocation());
2887}
2888
Sebastian Redl833ef452010-01-26 22:01:41 +00002889EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2890 SourceLocation L,
2891 IdentifierInfo *Id, QualType T,
2892 Expr *E, const llvm::APSInt &V) {
2893 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2894}
2895
Douglas Gregor72172e92012-01-05 21:55:30 +00002896EnumConstantDecl *
2897EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2898 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2899 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2900 llvm::APSInt());
2901}
2902
David Blaikie68e081d2011-12-20 02:48:34 +00002903void IndirectFieldDecl::anchor() { }
2904
Benjamin Kramer39593702010-11-21 14:11:41 +00002905IndirectFieldDecl *
2906IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2907 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2908 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002909 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2910}
2911
Douglas Gregor72172e92012-01-05 21:55:30 +00002912IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2913 unsigned ID) {
2914 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2915 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2916 QualType(), 0, 0);
2917}
2918
Douglas Gregorbe996932010-09-01 20:41:53 +00002919SourceRange EnumConstantDecl::getSourceRange() const {
2920 SourceLocation End = getLocation();
2921 if (Init)
2922 End = Init->getLocEnd();
2923 return SourceRange(getLocation(), End);
2924}
2925
David Blaikie68e081d2011-12-20 02:48:34 +00002926void TypeDecl::anchor() { }
2927
Sebastian Redl833ef452010-01-26 22:01:41 +00002928TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002929 SourceLocation StartLoc, SourceLocation IdLoc,
2930 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2931 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002932}
2933
David Blaikie68e081d2011-12-20 02:48:34 +00002934void TypedefNameDecl::anchor() { }
2935
Douglas Gregor72172e92012-01-05 21:55:30 +00002936TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2937 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2938 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2939}
2940
Richard Smithdda56e42011-04-15 14:24:37 +00002941TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2942 SourceLocation StartLoc,
2943 SourceLocation IdLoc, IdentifierInfo *Id,
2944 TypeSourceInfo *TInfo) {
2945 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2946}
2947
Douglas Gregor72172e92012-01-05 21:55:30 +00002948TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2949 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2950 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2951}
2952
Abramo Bagnaraea947882011-03-08 16:41:52 +00002953SourceRange TypedefDecl::getSourceRange() const {
2954 SourceLocation RangeEnd = getLocation();
2955 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2956 if (typeIsPostfix(TInfo->getType()))
2957 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2958 }
2959 return SourceRange(getLocStart(), RangeEnd);
2960}
2961
Richard Smithdda56e42011-04-15 14:24:37 +00002962SourceRange TypeAliasDecl::getSourceRange() const {
2963 SourceLocation RangeEnd = getLocStart();
2964 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2965 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2966 return SourceRange(getLocStart(), RangeEnd);
2967}
2968
David Blaikie68e081d2011-12-20 02:48:34 +00002969void FileScopeAsmDecl::anchor() { }
2970
Sebastian Redl833ef452010-01-26 22:01:41 +00002971FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002972 StringLiteral *Str,
2973 SourceLocation AsmLoc,
2974 SourceLocation RParenLoc) {
2975 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002976}
Douglas Gregorba345522011-12-02 23:23:56 +00002977
Douglas Gregor72172e92012-01-05 21:55:30 +00002978FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2979 unsigned ID) {
2980 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2981 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2982}
2983
Douglas Gregorba345522011-12-02 23:23:56 +00002984//===----------------------------------------------------------------------===//
2985// ImportDecl Implementation
2986//===----------------------------------------------------------------------===//
2987
2988/// \brief Retrieve the number of module identifiers needed to name the given
2989/// module.
2990static unsigned getNumModuleIdentifiers(Module *Mod) {
2991 unsigned Result = 1;
2992 while (Mod->Parent) {
2993 Mod = Mod->Parent;
2994 ++Result;
2995 }
2996 return Result;
2997}
2998
Douglas Gregor22d09742012-01-03 18:04:46 +00002999ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003000 Module *Imported,
3001 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003002 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003003 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003004{
3005 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3006 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3007 memcpy(StoredLocs, IdentifierLocs.data(),
3008 IdentifierLocs.size() * sizeof(SourceLocation));
3009}
3010
Douglas Gregor22d09742012-01-03 18:04:46 +00003011ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003012 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003013 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003014 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003015{
3016 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3017}
3018
3019ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003020 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003021 ArrayRef<SourceLocation> IdentifierLocs) {
3022 void *Mem = C.Allocate(sizeof(ImportDecl) +
3023 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003024 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003025}
3026
3027ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003028 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003029 Module *Imported,
3030 SourceLocation EndLoc) {
3031 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003032 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003033 Import->setImplicit();
3034 return Import;
3035}
3036
Douglas Gregor72172e92012-01-05 21:55:30 +00003037ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3038 unsigned NumLocations) {
3039 void *Mem = AllocateDeserializedDecl(C, ID,
3040 (sizeof(ImportDecl) +
3041 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003042 return new (Mem) ImportDecl(EmptyShell());
3043}
3044
3045ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3046 if (!ImportedAndComplete.getInt())
3047 return ArrayRef<SourceLocation>();
3048
3049 const SourceLocation *StoredLocs
3050 = reinterpret_cast<const SourceLocation *>(this + 1);
3051 return ArrayRef<SourceLocation>(StoredLocs,
3052 getNumModuleIdentifiers(getImportedModule()));
3053}
3054
3055SourceRange ImportDecl::getSourceRange() const {
3056 if (!ImportedAndComplete.getInt())
3057 return SourceRange(getLocation(),
3058 *reinterpret_cast<const SourceLocation *>(this + 1));
3059
3060 return SourceRange(getLocation(), getIdentifierLocs().back());
3061}