blob: c98853adec57422c4748cd3f9b7bdc680db40b68 [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 Espindolac7c7ad52012-07-13 14:25:36 +0000171static bool useInlineVisibilityHidden(const NamedDecl *D) {
172 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000173 const LangOptions &Opts = D->getASTContext().getLangOpts();
174 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000175 return false;
176
177 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
178 if (!FD)
179 return false;
180
181 TemplateSpecializationKind TSK = TSK_Undeclared;
182 if (FunctionTemplateSpecializationInfo *spec
183 = FD->getTemplateSpecializationInfo()) {
184 TSK = spec->getTemplateSpecializationKind();
185 } else if (MemberSpecializationInfo *MSI =
186 FD->getMemberSpecializationInfo()) {
187 TSK = MSI->getTemplateSpecializationKind();
188 }
189
190 const FunctionDecl *Def = 0;
191 // InlineVisibilityHidden only applies to definitions, and
192 // isInlined() only gives meaningful answers on definitions
193 // anyway.
194 return TSK != TSK_ExplicitInstantiationDeclaration &&
195 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000196 FD->hasBody(Def) && Def->isInlined();
197}
198
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000199static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
200 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000201 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000202 "Not a name having namespace scope");
203 ASTContext &Context = D->getASTContext();
204
205 // C++ [basic.link]p3:
206 // A name having namespace scope (3.3.6) has internal linkage if it
207 // is the name of
208 // - an object, reference, function or function template that is
209 // explicitly declared static; or,
210 // (This bullet corresponds to C99 6.2.2p3.)
211 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
212 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000213 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000214 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000215
216 // - an object or reference that is explicitly declared const
217 // and neither explicitly declared extern nor previously
218 // declared to have external linkage; or
219 // (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000220 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000221 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000222 Var->getStorageClass() != SC_Extern &&
223 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000224 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000225 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000226 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000227 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000228 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000229 FoundExtern = true;
230
231 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000232 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000233 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000234 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000235 const VarDecl *PrevVar = Var->getPreviousDecl();
236 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000237 if (PrevVar->getStorageClass() == SC_PrivateExtern)
238 break;
239 if (PrevVar)
240 return PrevVar->getLinkageAndVisibility();
241 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000242 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000243 // C++ [temp]p4:
244 // A non-member function template can have internal linkage; any
245 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000246 const FunctionDecl *Function = 0;
247 if (const FunctionTemplateDecl *FunTmpl
248 = dyn_cast<FunctionTemplateDecl>(D))
249 Function = FunTmpl->getTemplatedDecl();
250 else
251 Function = cast<FunctionDecl>(D);
252
253 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000254 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000255 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000256 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
257 // - a data member of an anonymous union.
258 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000259 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000260 }
261
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000262 if (D->isInAnonymousNamespace()) {
263 const VarDecl *Var = dyn_cast<VarDecl>(D);
264 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000265 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
266 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000267 return LinkageInfo::uniqueExternal();
268 }
John McCallb7139c42010-10-28 04:18:25 +0000269
John McCall457a04e2010-10-22 21:05:15 +0000270 // Set up the defaults.
271
272 // C99 6.2.2p5:
273 // If the declaration of an identifier for an object has file
274 // scope and no storage-class specifier, its linkage is
275 // external.
John McCallc273f242010-10-30 11:50:40 +0000276 LinkageInfo LV;
277
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000278 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000279 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000280 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000281 } else {
282 // If we're declared in a namespace with a visibility attribute,
283 // use that namespace's visibility, but don't call it explicit.
284 for (const DeclContext *DC = D->getDeclContext();
285 !isa<TranslationUnitDecl>(DC);
286 DC = DC->getParent()) {
287 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
288 if (!ND) continue;
289 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000290 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000291 break;
292 }
293 }
294 }
295 }
296
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000297 if (!OnlyTemplate) {
Rafael Espindolab660efd2012-04-19 04:37:16 +0000298 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000299 // If we're paying attention to global visibility, apply
300 // -finline-visibility-hidden if this is an inline method.
301 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
302 LV.mergeVisibility(HiddenVisibility, true);
303 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000304
Douglas Gregorf73b2822009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000306
Douglas Gregorf73b2822009-11-25 22:24:25 +0000307 // A name having namespace scope has external linkage if it is the
308 // name of
309 //
310 // - an object or reference, unless it has internal linkage; or
311 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall457a04e2010-10-22 21:05:15 +0000315 // Modify the variable's LV by the LV of its type unless this is
316 // C or extern "C". This follows from [basic.link]p9:
317 // A type without linkage shall not be used as the type of a
318 // variable or function with external linkage unless
319 // - the entity has C language linkage, or
320 // - the entity is declared within an unnamed namespace, or
321 // - the entity is not used or is defined in the same
322 // translation unit.
323 // and [basic.link]p10:
324 // ...the types specified by all declarations referring to a
325 // given variable or function shall be identical...
326 // C does not have an equivalent rule.
327 //
John McCall5fe84122010-10-26 04:59:26 +0000328 // Ignore this if we've got an explicit attribute; the user
329 // probably knows what they're doing.
330 //
John McCall457a04e2010-10-22 21:05:15 +0000331 // Note that we don't want to make the variable non-external
332 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000333 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000334 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000335 LinkageInfo TypeLV = getLVForType(Var->getType());
336 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000337 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000338 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000339 }
340
John McCall23032652010-11-02 18:38:13 +0000341 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000342 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000343
David Blaikiebbafb8a2012-03-11 07:00:24 +0000344 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000345 (Var->getStorageClass() == SC_Extern ||
346 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000347
Douglas Gregorf73b2822009-11-25 22:24:25 +0000348 // C99 6.2.2p4:
349 // For an identifier declared with the storage-class specifier
350 // extern in a scope in which a prior declaration of that
351 // identifier is visible, if the prior declaration specifies
352 // internal or external linkage, the linkage of the identifier
353 // at the later declaration is the same as the linkage
354 // specified at the prior declaration. If no prior declaration
355 // is visible, or if the prior declaration specifies no
356 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000357 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000358 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000359 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
360 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000361 }
362 }
363
Douglas Gregorf73b2822009-11-25 22:24:25 +0000364 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000365 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000366 // In theory, we can modify the function's LV by the LV of its
367 // type unless it has C linkage (see comment above about variables
368 // for justification). In practice, GCC doesn't do this, so it's
369 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000370
John McCall23032652010-11-02 18:38:13 +0000371 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000372 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000373
Douglas Gregorf73b2822009-11-25 22:24:25 +0000374 // C99 6.2.2p5:
375 // If the declaration of an identifier for a function has no
376 // storage-class specifier, its linkage is determined exactly
377 // as if it were declared with the storage-class specifier
378 // extern.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000379 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000380 (Function->getStorageClass() == SC_Extern ||
381 Function->getStorageClass() == SC_PrivateExtern ||
382 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000383 // C99 6.2.2p4:
384 // For an identifier declared with the storage-class specifier
385 // extern in a scope in which a prior declaration of that
386 // identifier is visible, if the prior declaration specifies
387 // internal or external linkage, the linkage of the identifier
388 // at the later declaration is the same as the linkage
389 // specified at the prior declaration. If no prior declaration
390 // is visible, or if the prior declaration specifies no
391 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000392 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000393 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000394 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
395 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000396 }
397 }
398
John McCallf768aa72011-02-10 06:50:24 +0000399 // In C++, then if the type of the function uses a type with
400 // unique-external linkage, it's not legally usable from outside
401 // this translation unit. However, we should use the C linkage
402 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000403 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000404 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000405 Function->getType()->getLinkage() == UniqueExternalLinkage)
406 return LinkageInfo::uniqueExternal();
407
John McCallb8c604a2011-06-27 23:06:04 +0000408 // Consider LV from the template and the template arguments unless
409 // this is an explicit specialization with a visibility attribute.
410 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000411 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000412 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
413 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
414 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
415 OnlyTemplate);
416 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000417 LV.mergeWithMin(TempLV);
Rafael Espindola340941d2012-05-25 16:41:35 +0000418 LV.mergeWithMin(ArgsLV);
419 } else {
420 LV.mergeLinkage(TempLV);
421 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000422 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000423 }
424
Douglas Gregorf73b2822009-11-25 22:24:25 +0000425 // - a named class (Clause 9), or an unnamed class defined in a
426 // typedef declaration in which the class has the typedef name
427 // for linkage purposes (7.1.3); or
428 // - a named enumeration (7.2), or an unnamed enumeration
429 // defined in a typedef declaration in which the enumeration
430 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000431 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
432 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000433 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000434 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000435
John McCall457a04e2010-10-22 21:05:15 +0000436 // If this is a class template specialization, consider the
437 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000438 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000439 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000440 // From the template.
441 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000442
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000443 // The arguments at which the template was instantiated.
444 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
445 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
446 OnlyTemplate);
447 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000448 LV.mergeWithMin(TempLV);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000449 LV.mergeWithMin(ArgsLV);
450 } else {
451 LV.mergeLinkage(TempLV);
452 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000453 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000454 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000455
456 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000457 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000458 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
459 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000460 if (!isExternalLinkage(EnumLV.linkage()))
461 return LinkageInfo::none();
462 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000463
464 // - a template, unless it is a function template that has
465 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000466 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000467 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000468 // - a namespace (7.3), unless it is declared within an unnamed
469 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000470 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
471 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000472
John McCall457a04e2010-10-22 21:05:15 +0000473 // By extension, we assign external linkage to Objective-C
474 // interfaces.
475 } else if (isa<ObjCInterfaceDecl>(D)) {
476 // fallout
477
478 // Everything not covered here has no linkage.
479 } else {
John McCallc273f242010-10-30 11:50:40 +0000480 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000481 }
482
483 // If we ended up with non-external linkage, visibility should
484 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000485 if (LV.linkage() != ExternalLinkage)
486 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000487
John McCall457a04e2010-10-22 21:05:15 +0000488 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000489}
490
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000491static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000492 // Only certain class members have linkage. Note that fields don't
493 // really have linkage, but it's convenient to say they do for the
494 // purposes of calculating linkage of pointer-to-data-member
495 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000496 if (!(isa<CXXMethodDecl>(D) ||
497 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000498 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000499 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000500 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000501 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000502
John McCall07072662010-11-02 01:45:15 +0000503 LinkageInfo LV;
504
John McCall07072662010-11-02 01:45:15 +0000505 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000506 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000507 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000508 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000509 // If we're paying attention to global visibility, apply
510 // -finline-visibility-hidden if this is an inline method.
511 //
512 // Note that we do this before merging information about
513 // the class visibility.
514 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
515 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000516 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000517
518 // If this class member has an explicit visibility attribute, the only
519 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000520 // only look for them when processing the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000521 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000522
Rafael Espindola53cf2192012-04-19 05:50:08 +0000523 // If this member has an visibility attribute, ClassF will exclude
524 // attributes on the class or command line options, keeping only information
525 // about the template instantiation. If the member has no visibility
526 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
527 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000528 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
529 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000530 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000531 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000532
533 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000534 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000535 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000536
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000537 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000538 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000539
John McCall8823c652010-08-13 08:35:10 +0000540 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000541 // If the type of the function uses a type with unique-external
542 // linkage, it's not legally usable from outside this translation unit.
543 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
544 return LinkageInfo::uniqueExternal();
545
John McCall457a04e2010-10-22 21:05:15 +0000546 // If this is a method template specialization, use the linkage for
547 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000548 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000549 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000550 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
551 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
552 OnlyTemplate);
553 TemplateParameterList *TemplateParams =
554 spec->getTemplate()->getTemplateParameters();
555 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola340941d2012-05-25 16:41:35 +0000556 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000557 LV.mergeWithMin(ArgsLV);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000558 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000559 LV.mergeWithMin(ParamsLV);
Rafael Espindola67a498c2012-05-25 17:22:33 +0000560 } else {
561 LV.mergeLinkage(ArgsLV);
562 if (!OnlyTemplate)
563 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000564 }
John McCalle6e622e2010-11-01 01:29:57 +0000565 }
John McCall457a04e2010-10-22 21:05:15 +0000566
John McCall37bb6c92010-10-29 22:22:43 +0000567 // Note that in contrast to basically every other situation, we
568 // *do* apply -fvisibility to method declarations.
569
570 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000571 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000572 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000573 // Merge template argument/parameter information for member
574 // class template specializations.
575 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
576 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
577 OnlyTemplate);
578 TemplateParameterList *TemplateParams =
579 spec->getSpecializedTemplate()->getTemplateParameters();
580 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000581 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000582 LV.mergeWithMin(ArgsLV);
Rafael Espindola4d71d0f2012-05-25 14:17:45 +0000583 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000584 LV.mergeWithMin(ParamsLV);
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000585 } else {
586 LV.mergeLinkage(ArgsLV);
587 if (!OnlyTemplate)
588 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000589 }
John McCall37bb6c92010-10-29 22:22:43 +0000590 }
591
John McCall37bb6c92010-10-29 22:22:43 +0000592 // Static data members.
593 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000594 // Modify the variable's linkage by its type, but ignore the
595 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000596 LinkageInfo TypeLV = getLVForType(VD->getType());
597 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000598 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000599 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000600 }
601
John McCall457a04e2010-10-22 21:05:15 +0000602 return LV;
John McCall8823c652010-08-13 08:35:10 +0000603}
604
John McCalld396b972011-02-08 19:01:05 +0000605static void clearLinkageForClass(const CXXRecordDecl *record) {
606 for (CXXRecordDecl::decl_iterator
607 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
608 Decl *child = *i;
609 if (isa<NamedDecl>(child))
610 cast<NamedDecl>(child)->ClearLinkageCache();
611 }
612}
613
David Blaikie68e081d2011-12-20 02:48:34 +0000614void NamedDecl::anchor() { }
615
John McCalld396b972011-02-08 19:01:05 +0000616void NamedDecl::ClearLinkageCache() {
617 // Note that we can't skip clearing the linkage of children just
618 // because the parent doesn't have cached linkage: we don't cache
619 // when computing linkage for parent contexts.
620
621 HasCachedLinkage = 0;
622
623 // If we're changing the linkage of a class, we need to reset the
624 // linkage of child declarations, too.
625 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
626 clearLinkageForClass(record);
627
John McCall83779672011-02-19 02:53:41 +0000628 if (ClassTemplateDecl *temp =
629 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000630 // Clear linkage for the template pattern.
631 CXXRecordDecl *record = temp->getTemplatedDecl();
632 record->HasCachedLinkage = 0;
633 clearLinkageForClass(record);
634
John McCall83779672011-02-19 02:53:41 +0000635 // We need to clear linkage for specializations, too.
636 for (ClassTemplateDecl::spec_iterator
637 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
638 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000639 }
John McCall83779672011-02-19 02:53:41 +0000640
641 // Clear cached linkage for function template decls, too.
642 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000643 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
644 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000645 for (FunctionTemplateDecl::spec_iterator
646 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
647 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000648 }
John McCall83779672011-02-19 02:53:41 +0000649
John McCalld396b972011-02-08 19:01:05 +0000650}
651
Douglas Gregorbf62d642010-12-06 18:36:25 +0000652Linkage NamedDecl::getLinkage() const {
653 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000654 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000655 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000656 return Linkage(CachedLinkage);
657 }
658
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000659 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000660 HasCachedLinkage = 1;
661 return Linkage(CachedLinkage);
662}
663
John McCallc273f242010-10-30 11:50:40 +0000664LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000665 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000666 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000667 HasCachedLinkage = 1;
668 CachedLinkage = LI.linkage();
669 return LI;
John McCall033caa52010-10-29 00:29:13 +0000670}
Ted Kremenek926d8602010-04-20 23:15:35 +0000671
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000672llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
673 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000674 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
675 if (llvm::Optional<Visibility> V =
676 getVisibilityOf(Var->getMostRecentDecl()))
677 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000678
Rafael Espindola96e68242012-05-16 02:10:38 +0000679 if (Var->isStaticDataMember()) {
680 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
681 if (InstantiatedFrom)
682 return getVisibilityOf(InstantiatedFrom);
683 }
684
685 return llvm::Optional<Visibility>();
686 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000687 // Use the most recent declaration of a function, and also handle
688 // function template specializations.
689 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
690 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000691 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000692 return V;
693
694 // If the function is a specialization of a template with an
695 // explicit visibility attribute, use that.
696 if (FunctionTemplateSpecializationInfo *templateInfo
697 = fn->getTemplateSpecializationInfo())
698 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
699
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000700 // If the function is a member of a specialization of a class template
701 // and the corresponding decl has explicit visibility, use that.
702 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
703 if (InstantiatedFrom)
704 return getVisibilityOf(InstantiatedFrom);
705
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000706 return llvm::Optional<Visibility>();
707 }
708
709 // Otherwise, just check the declaration itself first.
710 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
711 return V;
712
713 // If there wasn't explicit visibility there, and this is a
714 // specialization of a class template, check for visibility
715 // on the pattern.
716 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolaeca5cd22012-07-13 01:19:08 +0000717 = dyn_cast<ClassTemplateSpecializationDecl>(this))
718 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000719
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000720 // If this is a member class of a specialization of a class template
721 // and the corresponding decl has explicit visibility, use that.
722 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
723 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
724 if (InstantiatedFrom)
725 return getVisibilityOf(InstantiatedFrom);
726 }
727
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000728 return llvm::Optional<Visibility>();
729}
730
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000731static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000732 // Objective-C: treat all Objective-C declarations as having external
733 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000734 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000735 default:
736 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000737 case Decl::ParmVar:
738 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000739 case Decl::TemplateTemplateParm: // count these as external
740 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000741 case Decl::ObjCAtDefsField:
742 case Decl::ObjCCategory:
743 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000744 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000745 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000746 case Decl::ObjCMethod:
747 case Decl::ObjCProperty:
748 case Decl::ObjCPropertyImpl:
749 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000750 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000751
752 case Decl::CXXRecord: {
753 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
754 if (Record->isLambda()) {
755 if (!Record->getLambdaManglingNumber()) {
756 // This lambda has no mangling number, so it's internal.
757 return LinkageInfo::internal();
758 }
759
760 // This lambda has its linkage/visibility determined by its owner.
761 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
762 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
763 if (isa<ParmVarDecl>(ContextDecl))
764 DC = ContextDecl->getDeclContext()->getRedeclContext();
765 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000766 return getLVForDecl(cast<NamedDecl>(ContextDecl),
767 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000768 }
769
770 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000771 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000772
773 return LinkageInfo::external();
774 }
775
776 break;
777 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000778 }
779
Douglas Gregorf73b2822009-11-25 22:24:25 +0000780 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000781 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000782 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000783
784 // C++ [basic.link]p5:
785 // In addition, a member function, static data member, a named
786 // class or enumeration of class scope, or an unnamed class or
787 // enumeration defined in a class-scope typedef declaration such
788 // that the class or enumeration has the typedef name for linkage
789 // purposes (7.1.3), has external linkage if the name of the class
790 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000791 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000792 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000793
794 // C++ [basic.link]p6:
795 // The name of a function declared in block scope and the name of
796 // an object declared by a block scope extern declaration have
797 // linkage. If there is a visible declaration of an entity with
798 // linkage having the same name and type, ignoring entities
799 // declared outside the innermost enclosing namespace scope, the
800 // block scope declaration declares that same entity and receives
801 // the linkage of the previous declaration. If there is more than
802 // one such matching entity, the program is ill-formed. Otherwise,
803 // if no matching entity is found, the block scope entity receives
804 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000805 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
806 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000807 if (Function->isInAnonymousNamespace() &&
808 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000809 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000810
John McCallc273f242010-10-30 11:50:40 +0000811 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000812 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000813 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000814 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000815 }
816
Douglas Gregorec9fd132012-01-14 16:38:05 +0000817 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000818 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000819 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
820 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000821 }
822
823 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000824 }
825
John McCall033caa52010-10-29 00:29:13 +0000826 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000827 if (Var->getStorageClass() == SC_Extern ||
828 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000829 if (Var->isInAnonymousNamespace() &&
830 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000831 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000832
John McCallc273f242010-10-30 11:50:40 +0000833 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000834 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000835 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000836 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000837 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000838 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000839 }
840
Douglas Gregorec9fd132012-01-14 16:38:05 +0000841 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000842 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000843 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
844 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000845 }
846
847 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000848 }
849 }
850
851 // C++ [basic.link]p6:
852 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000853 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000854}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000855
Douglas Gregor2ada0482009-02-04 17:27:36 +0000856std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000857 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000858}
859
860std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000861 const DeclContext *Ctx = getDeclContext();
862
863 if (Ctx->isFunctionOrMethod())
864 return getNameAsString();
865
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000866 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000867 ContextsTy Contexts;
868
869 // Collect contexts.
870 while (Ctx && isa<NamedDecl>(Ctx)) {
871 Contexts.push_back(Ctx);
872 Ctx = Ctx->getParent();
873 };
874
875 std::string QualName;
876 llvm::raw_string_ostream OS(QualName);
877
878 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
879 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000880 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000881 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000882 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
883 std::string TemplateArgsStr
884 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000885 TemplateArgs.data(),
886 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000887 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000888 OS << Spec->getName() << TemplateArgsStr;
889 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000890 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000891 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000892 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000893 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000894 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
895 if (!RD->getIdentifier())
896 OS << "<anonymous " << RD->getKindName() << '>';
897 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000898 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000899 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000900 const FunctionProtoType *FT = 0;
901 if (FD->hasWrittenPrototype())
902 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
903
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000904 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000905 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000906 unsigned NumParams = FD->getNumParams();
907 for (unsigned i = 0; i < NumParams; ++i) {
908 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000909 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000910 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000911 }
912
913 if (FT->isVariadic()) {
914 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000915 OS << ", ";
916 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000917 }
918 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000919 OS << ')';
920 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000921 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000922 }
923 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000924 }
925
John McCalla2a3f7d2010-03-16 21:48:18 +0000926 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000927 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000928 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000929 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000930
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000931 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000932}
933
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000934bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000935 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
936
Douglas Gregor889ceb72009-02-03 19:21:40 +0000937 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
938 // We want to keep it, unless it nominates same namespace.
939 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000940 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
941 ->getOriginalNamespace() ==
942 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
943 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000944 }
Mike Stump11289f42009-09-09 15:08:12 +0000945
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000946 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
947 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000948 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000949
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000950 // For function templates, the underlying function declarations are linked.
951 if (const FunctionTemplateDecl *FunctionTemplate
952 = dyn_cast<FunctionTemplateDecl>(this))
953 if (const FunctionTemplateDecl *OldFunctionTemplate
954 = dyn_cast<FunctionTemplateDecl>(OldD))
955 return FunctionTemplate->getTemplatedDecl()
956 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000957
Steve Naroffc4173fa2009-02-22 19:35:57 +0000958 // For method declarations, we keep track of redeclarations.
959 if (isa<ObjCMethodDecl>(this))
960 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000961
John McCall9f3059a2009-10-09 21:13:30 +0000962 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
963 return true;
964
John McCall3f746822009-11-17 05:59:44 +0000965 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
966 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
967 cast<UsingShadowDecl>(OldD)->getTargetDecl();
968
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000969 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
970 ASTContext &Context = getASTContext();
971 return Context.getCanonicalNestedNameSpecifier(
972 cast<UsingDecl>(this)->getQualifier()) ==
973 Context.getCanonicalNestedNameSpecifier(
974 cast<UsingDecl>(OldD)->getQualifier());
975 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000976
Douglas Gregorb59643b2012-01-03 23:26:26 +0000977 // A typedef of an Objective-C class type can replace an Objective-C class
978 // declaration or definition, and vice versa.
979 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
980 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
981 return true;
982
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000983 // For non-function declarations, if the declarations are of the
984 // same kind then this must be a redeclaration, or semantic analysis
985 // would not have given us the new declaration.
986 return this->getKind() == OldD->getKind();
987}
988
Douglas Gregoreddf4332009-02-24 20:03:32 +0000989bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000990 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000991}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000992
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000993NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000994 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000995 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
996 ND = UD->getTargetDecl();
997
998 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
999 return AD->getClassInterface();
1000
1001 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001002}
1003
John McCalla8ae2222010-04-06 21:38:20 +00001004bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001005 if (!isCXXClassMember())
1006 return false;
1007
John McCalla8ae2222010-04-06 21:38:20 +00001008 const NamedDecl *D = this;
1009 if (isa<UsingShadowDecl>(D))
1010 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1011
Francois Pichet783dd6e2010-11-21 06:08:52 +00001012 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001013 return true;
1014 if (isa<CXXMethodDecl>(D))
1015 return cast<CXXMethodDecl>(D)->isInstance();
1016 if (isa<FunctionTemplateDecl>(D))
1017 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1018 ->getTemplatedDecl())->isInstance();
1019 return false;
1020}
1021
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001022//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001023// DeclaratorDecl Implementation
1024//===----------------------------------------------------------------------===//
1025
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001026template <typename DeclT>
1027static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1028 if (decl->getNumTemplateParameterLists() > 0)
1029 return decl->getTemplateParameterList(0)->getTemplateLoc();
1030 else
1031 return decl->getInnerLocStart();
1032}
1033
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001034SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001035 TypeSourceInfo *TSI = getTypeSourceInfo();
1036 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001037 return SourceLocation();
1038}
1039
Douglas Gregor14454802011-02-25 02:25:35 +00001040void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1041 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001042 // Make sure the extended decl info is allocated.
1043 if (!hasExtInfo()) {
1044 // Save (non-extended) type source info pointer.
1045 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1046 // Allocate external info struct.
1047 DeclInfo = new (getASTContext()) ExtInfo;
1048 // Restore savedTInfo into (extended) decl info.
1049 getExtInfo()->TInfo = savedTInfo;
1050 }
1051 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001052 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001053 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001054 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001055 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001056 if (getExtInfo()->NumTemplParamLists == 0) {
1057 // Save type source info pointer.
1058 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1059 // Deallocate the extended decl info.
1060 getASTContext().Deallocate(getExtInfo());
1061 // Restore savedTInfo into (non-extended) decl info.
1062 DeclInfo = savedTInfo;
1063 }
1064 else
1065 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001066 }
1067 }
1068}
1069
Abramo Bagnara60804e12011-03-18 15:16:37 +00001070void
1071DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1072 unsigned NumTPLists,
1073 TemplateParameterList **TPLists) {
1074 assert(NumTPLists > 0);
1075 // Make sure the extended decl info is allocated.
1076 if (!hasExtInfo()) {
1077 // Save (non-extended) type source info pointer.
1078 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1079 // Allocate external info struct.
1080 DeclInfo = new (getASTContext()) ExtInfo;
1081 // Restore savedTInfo into (extended) decl info.
1082 getExtInfo()->TInfo = savedTInfo;
1083 }
1084 // Set the template parameter lists info.
1085 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1086}
1087
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001088SourceLocation DeclaratorDecl::getOuterLocStart() const {
1089 return getTemplateOrInnerLocStart(this);
1090}
1091
Abramo Bagnaraea947882011-03-08 16:41:52 +00001092namespace {
1093
1094// Helper function: returns true if QT is or contains a type
1095// having a postfix component.
1096bool typeIsPostfix(clang::QualType QT) {
1097 while (true) {
1098 const Type* T = QT.getTypePtr();
1099 switch (T->getTypeClass()) {
1100 default:
1101 return false;
1102 case Type::Pointer:
1103 QT = cast<PointerType>(T)->getPointeeType();
1104 break;
1105 case Type::BlockPointer:
1106 QT = cast<BlockPointerType>(T)->getPointeeType();
1107 break;
1108 case Type::MemberPointer:
1109 QT = cast<MemberPointerType>(T)->getPointeeType();
1110 break;
1111 case Type::LValueReference:
1112 case Type::RValueReference:
1113 QT = cast<ReferenceType>(T)->getPointeeType();
1114 break;
1115 case Type::PackExpansion:
1116 QT = cast<PackExpansionType>(T)->getPattern();
1117 break;
1118 case Type::Paren:
1119 case Type::ConstantArray:
1120 case Type::DependentSizedArray:
1121 case Type::IncompleteArray:
1122 case Type::VariableArray:
1123 case Type::FunctionProto:
1124 case Type::FunctionNoProto:
1125 return true;
1126 }
1127 }
1128}
1129
1130} // namespace
1131
1132SourceRange DeclaratorDecl::getSourceRange() const {
1133 SourceLocation RangeEnd = getLocation();
1134 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1135 if (typeIsPostfix(TInfo->getType()))
1136 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1137 }
1138 return SourceRange(getOuterLocStart(), RangeEnd);
1139}
1140
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001141void
Douglas Gregor20527e22010-06-15 17:44:38 +00001142QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1143 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001144 TemplateParameterList **TPLists) {
1145 assert((NumTPLists == 0 || TPLists != 0) &&
1146 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001147
1148 // Free previous template parameters (if any).
1149 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001150 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001151 TemplParamLists = 0;
1152 NumTemplParamLists = 0;
1153 }
1154 // Set info on matched template parameter lists (if any).
1155 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001156 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001157 NumTemplParamLists = NumTPLists;
1158 for (unsigned i = NumTPLists; i-- > 0; )
1159 TemplParamLists[i] = TPLists[i];
1160 }
1161}
1162
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001163//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001164// VarDecl Implementation
1165//===----------------------------------------------------------------------===//
1166
Sebastian Redl833ef452010-01-26 22:01:41 +00001167const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1168 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001169 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001170 case SC_Auto: return "auto";
1171 case SC_Extern: return "extern";
1172 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1173 case SC_PrivateExtern: return "__private_extern__";
1174 case SC_Register: return "register";
1175 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001176 }
1177
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001178 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001179}
1180
Abramo Bagnaradff19302011-03-08 08:55:46 +00001181VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1182 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001183 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001184 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001185 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001186}
1187
Douglas Gregor72172e92012-01-05 21:55:30 +00001188VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1189 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1190 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1191 QualType(), 0, SC_None, SC_None);
1192}
1193
Douglas Gregorbf62d642010-12-06 18:36:25 +00001194void VarDecl::setStorageClass(StorageClass SC) {
1195 assert(isLegalForVariable(SC));
1196 if (getStorageClass() != SC)
1197 ClearLinkageCache();
1198
John McCallbeaa11c2011-05-01 02:13:58 +00001199 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001200}
1201
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001202SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001203 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001204 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001205 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001206}
1207
Sebastian Redl833ef452010-01-26 22:01:41 +00001208bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001209 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001210 return false;
1211
Eli Friedman839192f2012-01-15 01:23:58 +00001212 const DeclContext *DC = getDeclContext();
1213 if (DC->isRecord())
1214 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001215
Eli Friedman839192f2012-01-15 01:23:58 +00001216 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001217 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001218 return true;
1219 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001220}
1221
1222VarDecl *VarDecl::getCanonicalDecl() {
1223 return getFirstDeclaration();
1224}
1225
Daniel Dunbar9d355812012-03-09 01:51:51 +00001226VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1227 ASTContext &C) const
1228{
Sebastian Redl35351a92010-01-31 22:27:38 +00001229 // C++ [basic.def]p2:
1230 // A declaration is a definition unless [...] it contains the 'extern'
1231 // specifier or a linkage-specification and neither an initializer [...],
1232 // it declares a static data member in a class declaration [...].
1233 // C++ [temp.expl.spec]p15:
1234 // An explicit specialization of a static data member of a template is a
1235 // definition if the declaration includes an initializer; otherwise, it is
1236 // a declaration.
1237 if (isStaticDataMember()) {
1238 if (isOutOfLine() && (hasInit() ||
1239 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1240 return Definition;
1241 else
1242 return DeclarationOnly;
1243 }
1244 // C99 6.7p5:
1245 // A definition of an identifier is a declaration for that identifier that
1246 // [...] causes storage to be reserved for that object.
1247 // Note: that applies for all non-file-scope objects.
1248 // C99 6.9.2p1:
1249 // If the declaration of an identifier for an object has file scope and an
1250 // initializer, the declaration is an external definition for the identifier
1251 if (hasInit())
1252 return Definition;
1253 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1254 if (hasExternalStorage())
1255 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001256
John McCall8e7d6562010-08-26 03:08:43 +00001257 if (getStorageClassAsWritten() == SC_Extern ||
1258 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001259 for (const VarDecl *PrevVar = getPreviousDecl();
1260 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001261 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1262 return DeclarationOnly;
1263 }
1264 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001265 // C99 6.9.2p2:
1266 // A declaration of an object that has file scope without an initializer,
1267 // and without a storage class specifier or the scs 'static', constitutes
1268 // a tentative definition.
1269 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001270 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001271 return TentativeDefinition;
1272
1273 // What's left is (in C, block-scope) declarations without initializers or
1274 // external storage. These are definitions.
1275 return Definition;
1276}
1277
Sebastian Redl35351a92010-01-31 22:27:38 +00001278VarDecl *VarDecl::getActingDefinition() {
1279 DefinitionKind Kind = isThisDeclarationADefinition();
1280 if (Kind != TentativeDefinition)
1281 return 0;
1282
Chris Lattner48eb14d2010-06-14 18:31:46 +00001283 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001284 VarDecl *First = getFirstDeclaration();
1285 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1286 I != E; ++I) {
1287 Kind = (*I)->isThisDeclarationADefinition();
1288 if (Kind == Definition)
1289 return 0;
1290 else if (Kind == TentativeDefinition)
1291 LastTentative = *I;
1292 }
1293 return LastTentative;
1294}
1295
1296bool VarDecl::isTentativeDefinitionNow() const {
1297 DefinitionKind Kind = isThisDeclarationADefinition();
1298 if (Kind != TentativeDefinition)
1299 return false;
1300
1301 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1302 if ((*I)->isThisDeclarationADefinition() == Definition)
1303 return false;
1304 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001305 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001306}
1307
Daniel Dunbar9d355812012-03-09 01:51:51 +00001308VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001309 VarDecl *First = getFirstDeclaration();
1310 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1311 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001312 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001313 return *I;
1314 }
1315 return 0;
1316}
1317
Daniel Dunbar9d355812012-03-09 01:51:51 +00001318VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001319 DefinitionKind Kind = DeclarationOnly;
1320
1321 const VarDecl *First = getFirstDeclaration();
1322 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001323 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001324 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001325 if (Kind == Definition)
1326 break;
1327 }
John McCall37bb6c92010-10-29 22:22:43 +00001328
1329 return Kind;
1330}
1331
Sebastian Redl5ca79842010-02-01 20:16:42 +00001332const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001333 redecl_iterator I = redecls_begin(), E = redecls_end();
1334 while (I != E && !I->getInit())
1335 ++I;
1336
1337 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001338 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001339 return I->getInit();
1340 }
1341 return 0;
1342}
1343
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001344bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001345 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001346 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001347
1348 if (!isStaticDataMember())
1349 return false;
1350
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001351 // If this static data member was instantiated from a static data member of
1352 // a class template, check whether that static data member was defined
1353 // out-of-line.
1354 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1355 return VD->isOutOfLine();
1356
1357 return false;
1358}
1359
Douglas Gregor1d957a32009-10-27 18:42:08 +00001360VarDecl *VarDecl::getOutOfLineDefinition() {
1361 if (!isStaticDataMember())
1362 return 0;
1363
1364 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1365 RD != RDEnd; ++RD) {
1366 if (RD->getLexicalDeclContext()->isFileContext())
1367 return *RD;
1368 }
1369
1370 return 0;
1371}
1372
Douglas Gregord5058122010-02-11 01:19:42 +00001373void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001374 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1375 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001376 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001377 }
1378
1379 Init = I;
1380}
1381
Daniel Dunbar9d355812012-03-09 01:51:51 +00001382bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001383 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001384
Richard Smith35ecb362012-03-02 04:14:40 +00001385 if (!Lang.CPlusPlus)
1386 return false;
1387
1388 // In C++11, any variable of reference type can be used in a constant
1389 // expression if it is initialized by a constant expression.
1390 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1391 return true;
1392
1393 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001394 // not require the variable to be non-volatile, but we consider this to be a
1395 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001396 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001397 return false;
1398
1399 // In C++, const, non-volatile variables of integral or enumeration types
1400 // can be used in constant expressions.
1401 if (getType()->isIntegralOrEnumerationType())
1402 return true;
1403
Richard Smith35ecb362012-03-02 04:14:40 +00001404 // Additionally, in C++11, non-volatile constexpr variables can be used in
1405 // constant expressions.
1406 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001407}
1408
Richard Smithd0b4dd62011-12-19 06:19:21 +00001409/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1410/// form, which contains extra information on the evaluated value of the
1411/// initializer.
1412EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1413 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1414 if (!Eval) {
1415 Stmt *S = Init.get<Stmt *>();
1416 Eval = new (getASTContext()) EvaluatedStmt;
1417 Eval->Value = S;
1418 Init = Eval;
1419 }
1420 return Eval;
1421}
1422
Richard Smithdafff942012-01-14 04:30:29 +00001423APValue *VarDecl::evaluateValue() const {
1424 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1425 return evaluateValue(Notes);
1426}
1427
1428APValue *VarDecl::evaluateValue(
1429 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001430 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1431
1432 // We only produce notes indicating why an initializer is non-constant the
1433 // first time it is evaluated. FIXME: The notes won't always be emitted the
1434 // first time we try evaluation, so might not be produced at all.
1435 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001436 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001437
1438 const Expr *Init = cast<Expr>(Eval->Value);
1439 assert(!Init->isValueDependent());
1440
1441 if (Eval->IsEvaluating) {
1442 // FIXME: Produce a diagnostic for self-initialization.
1443 Eval->CheckedICE = true;
1444 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001445 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001446 }
1447
1448 Eval->IsEvaluating = true;
1449
1450 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1451 this, Notes);
1452
1453 // Ensure the result is an uninitialized APValue if evaluation fails.
1454 if (!Result)
1455 Eval->Evaluated = APValue();
1456
1457 Eval->IsEvaluating = false;
1458 Eval->WasEvaluated = true;
1459
1460 // In C++11, we have determined whether the initializer was a constant
1461 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001462 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001463 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001464 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001465 }
1466
Richard Smithdafff942012-01-14 04:30:29 +00001467 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001468}
1469
1470bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001471 // Initializers of weak variables are never ICEs.
1472 if (isWeak())
1473 return false;
1474
Richard Smithd0b4dd62011-12-19 06:19:21 +00001475 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1476 if (Eval->CheckedICE)
1477 // We have already checked whether this subexpression is an
1478 // integral constant expression.
1479 return Eval->IsICE;
1480
1481 const Expr *Init = cast<Expr>(Eval->Value);
1482 assert(!Init->isValueDependent());
1483
1484 // In C++11, evaluate the initializer to check whether it's a constant
1485 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001486 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001487 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1488 evaluateValue(Notes);
1489 return Eval->IsICE;
1490 }
1491
1492 // It's an ICE whether or not the definition we found is
1493 // out-of-line. See DR 721 and the discussion in Clang PR
1494 // 6206 for details.
1495
1496 if (Eval->CheckingICE)
1497 return false;
1498 Eval->CheckingICE = true;
1499
1500 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1501 Eval->CheckingICE = false;
1502 Eval->CheckedICE = true;
1503 return Eval->IsICE;
1504}
1505
Douglas Gregorfe314812011-06-21 17:03:29 +00001506bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001507 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001508
1509 const Expr *E = getInit();
1510 if (!E)
1511 return false;
1512
1513 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1514 E = Cleanups->getSubExpr();
1515
1516 return isa<MaterializeTemporaryExpr>(E);
1517}
1518
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001519VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001520 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001521 return cast<VarDecl>(MSI->getInstantiatedFrom());
1522
1523 return 0;
1524}
1525
Douglas Gregor3c74d412009-10-14 20:14:33 +00001526TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001527 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001528 return MSI->getTemplateSpecializationKind();
1529
1530 return TSK_Undeclared;
1531}
1532
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001533MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001534 return getASTContext().getInstantiatedFromStaticDataMember(this);
1535}
1536
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001537void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1538 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001539 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001540 assert(MSI && "Not an instantiated static data member?");
1541 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001542 if (TSK != TSK_ExplicitSpecialization &&
1543 PointOfInstantiation.isValid() &&
1544 MSI->getPointOfInstantiation().isInvalid())
1545 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001546}
1547
Sebastian Redl833ef452010-01-26 22:01:41 +00001548//===----------------------------------------------------------------------===//
1549// ParmVarDecl Implementation
1550//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001551
Sebastian Redl833ef452010-01-26 22:01:41 +00001552ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001553 SourceLocation StartLoc,
1554 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001555 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001556 StorageClass S, StorageClass SCAsWritten,
1557 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001558 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001559 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001560}
1561
Douglas Gregor72172e92012-01-05 21:55:30 +00001562ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1563 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1564 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1565 0, QualType(), 0, SC_None, SC_None, 0);
1566}
1567
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001568SourceRange ParmVarDecl::getSourceRange() const {
1569 if (!hasInheritedDefaultArg()) {
1570 SourceRange ArgRange = getDefaultArgRange();
1571 if (ArgRange.isValid())
1572 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1573 }
1574
1575 return DeclaratorDecl::getSourceRange();
1576}
1577
Sebastian Redl833ef452010-01-26 22:01:41 +00001578Expr *ParmVarDecl::getDefaultArg() {
1579 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1580 assert(!hasUninstantiatedDefaultArg() &&
1581 "Default argument is not yet instantiated!");
1582
1583 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001584 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001585 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001586
Sebastian Redl833ef452010-01-26 22:01:41 +00001587 return Arg;
1588}
1589
Sebastian Redl833ef452010-01-26 22:01:41 +00001590SourceRange ParmVarDecl::getDefaultArgRange() const {
1591 if (const Expr *E = getInit())
1592 return E->getSourceRange();
1593
1594 if (hasUninstantiatedDefaultArg())
1595 return getUninstantiatedDefaultArg()->getSourceRange();
1596
1597 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001598}
1599
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001600bool ParmVarDecl::isParameterPack() const {
1601 return isa<PackExpansionType>(getType());
1602}
1603
Ted Kremenek540017e2011-10-06 05:00:56 +00001604void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1605 getASTContext().setParameterIndex(this, parameterIndex);
1606 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1607}
1608
1609unsigned ParmVarDecl::getParameterIndexLarge() const {
1610 return getASTContext().getParameterIndex(this);
1611}
1612
Nuno Lopes394ec982008-12-17 23:39:55 +00001613//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001614// FunctionDecl Implementation
1615//===----------------------------------------------------------------------===//
1616
Douglas Gregorb11aad82011-02-19 18:51:44 +00001617void FunctionDecl::getNameForDiagnostic(std::string &S,
1618 const PrintingPolicy &Policy,
1619 bool Qualified) const {
1620 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1621 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1622 if (TemplateArgs)
1623 S += TemplateSpecializationType::PrintTemplateArgumentList(
1624 TemplateArgs->data(),
1625 TemplateArgs->size(),
1626 Policy);
1627
1628}
1629
Ted Kremenek186a0742010-04-29 16:49:01 +00001630bool FunctionDecl::isVariadic() const {
1631 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1632 return FT->isVariadic();
1633 return false;
1634}
1635
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001636bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1637 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001638 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001639 Definition = *I;
1640 return true;
1641 }
1642 }
1643
1644 return false;
1645}
1646
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001647bool FunctionDecl::hasTrivialBody() const
1648{
1649 Stmt *S = getBody();
1650 if (!S) {
1651 // Since we don't have a body for this function, we don't know if it's
1652 // trivial or not.
1653 return false;
1654 }
1655
1656 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1657 return true;
1658 return false;
1659}
1660
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001661bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1662 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001663 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001664 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1665 return true;
1666 }
1667 }
1668
1669 return false;
1670}
1671
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001672Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001673 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1674 if (I->Body) {
1675 Definition = *I;
1676 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001677 } else if (I->IsLateTemplateParsed) {
1678 Definition = *I;
1679 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001680 }
1681 }
1682
1683 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001684}
1685
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001686void FunctionDecl::setBody(Stmt *B) {
1687 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001688 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001689 EndRangeLoc = B->getLocEnd();
1690}
1691
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001692void FunctionDecl::setPure(bool P) {
1693 IsPure = P;
1694 if (P)
1695 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1696 Parent->markedVirtualFunctionPure();
1697}
1698
Richard Smith12f247f2012-06-08 21:09:22 +00001699void FunctionDecl::setConstexpr(bool IC) {
1700 IsConstexpr = IC;
1701 CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(this);
1702 if (IC && CD)
1703 CD->getParent()->markedConstructorConstexpr(CD);
1704}
1705
Douglas Gregor16618f22009-09-12 00:17:51 +00001706bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001707 const TranslationUnitDecl *tunit =
1708 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1709 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001710 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001711 getIdentifier() &&
1712 getIdentifier()->isStr("main");
1713}
1714
1715bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1716 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1717 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1718 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1719 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1720 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1721
1722 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1723 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1724
1725 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1726 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1727
1728 ASTContext &Context =
1729 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1730 ->getASTContext();
1731
1732 // The result type and first argument type are constant across all
1733 // these operators. The second argument must be exactly void*.
1734 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001735}
1736
Douglas Gregor16618f22009-09-12 00:17:51 +00001737bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001738 if (getLinkage() != ExternalLinkage)
1739 return false;
1740
1741 if (getAttr<OverloadableAttr>())
1742 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001743
Chandler Carruth4322a282011-02-25 00:05:02 +00001744 const DeclContext *DC = getDeclContext();
1745 if (DC->isRecord())
1746 return false;
1747
Eli Friedman839192f2012-01-15 01:23:58 +00001748 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001749 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001750 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001751
Eli Friedman839192f2012-01-15 01:23:58 +00001752 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001753}
1754
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001755bool FunctionDecl::isGlobal() const {
1756 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1757 return Method->isStatic();
1758
John McCall8e7d6562010-08-26 03:08:43 +00001759 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001760 return false;
1761
Mike Stump11289f42009-09-09 15:08:12 +00001762 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001763 DC->isNamespace();
1764 DC = DC->getParent()) {
1765 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1766 if (!Namespace->getDeclName())
1767 return false;
1768 break;
1769 }
1770 }
1771
1772 return true;
1773}
1774
Sebastian Redl833ef452010-01-26 22:01:41 +00001775void
1776FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1777 redeclarable_base::setPreviousDeclaration(PrevDecl);
1778
1779 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1780 FunctionTemplateDecl *PrevFunTmpl
1781 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1782 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1783 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1784 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001785
Axel Naumannfbc7b982011-11-08 18:21:06 +00001786 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001787 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001788}
1789
1790const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1791 return getFirstDeclaration();
1792}
1793
1794FunctionDecl *FunctionDecl::getCanonicalDecl() {
1795 return getFirstDeclaration();
1796}
1797
Douglas Gregorbf62d642010-12-06 18:36:25 +00001798void FunctionDecl::setStorageClass(StorageClass SC) {
1799 assert(isLegalForFunction(SC));
1800 if (getStorageClass() != SC)
1801 ClearLinkageCache();
1802
1803 SClass = SC;
1804}
1805
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001806/// \brief Returns a value indicating whether this function
1807/// corresponds to a builtin function.
1808///
1809/// The function corresponds to a built-in function if it is
1810/// declared at translation scope or within an extern "C" block and
1811/// its name matches with the name of a builtin. The returned value
1812/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001813/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001814/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001815unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001816 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001817 return 0;
1818
1819 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001820 if (!BuiltinID)
1821 return 0;
1822
1823 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001824 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1825 return BuiltinID;
1826
1827 // This function has the name of a known C library
1828 // function. Determine whether it actually refers to the C library
1829 // function or whether it just has the same name.
1830
Douglas Gregora908e7f2009-02-17 03:23:10 +00001831 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001832 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001833 return 0;
1834
Douglas Gregore711f702009-02-14 18:57:46 +00001835 // If this function is at translation-unit scope and we're not in
1836 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001837 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001838 getDeclContext()->isTranslationUnit())
1839 return BuiltinID;
1840
1841 // If the function is in an extern "C" linkage specification and is
1842 // not marked "overloadable", it's the real function.
1843 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001844 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001845 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001846 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001847 return BuiltinID;
1848
1849 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001850 return 0;
1851}
1852
1853
Chris Lattner47c0d002009-04-25 06:03:53 +00001854/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001855/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001856/// after it has been created.
1857unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001858 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001859 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001860 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001861 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001862
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001863}
1864
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001865void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001866 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001867 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001868 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001869
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001870 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001871 if (!NewParamInfo.empty()) {
1872 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1873 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001874 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001875}
Chris Lattner41943152007-01-25 04:52:46 +00001876
James Molloy6f8780b2012-02-29 10:24:19 +00001877void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1878 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1879
1880 if (!NewDecls.empty()) {
1881 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1882 std::copy(NewDecls.begin(), NewDecls.end(), A);
1883 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1884 }
1885}
1886
Chris Lattner58258242008-04-10 02:22:51 +00001887/// getMinRequiredArguments - Returns the minimum number of arguments
1888/// needed to call this function. This may be fewer than the number of
1889/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001890/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001891unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001892 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001893 return getNumParams();
1894
Douglas Gregor7825bf32011-01-06 22:09:01 +00001895 unsigned NumRequiredArgs = getNumParams();
1896
1897 // If the last parameter is a parameter pack, we don't need an argument for
1898 // it.
1899 if (NumRequiredArgs > 0 &&
1900 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1901 --NumRequiredArgs;
1902
1903 // If this parameter has a default argument, we don't need an argument for
1904 // it.
1905 while (NumRequiredArgs > 0 &&
1906 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001907 --NumRequiredArgs;
1908
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001909 // We might have parameter packs before the end. These can't be deduced,
1910 // but they can still handle multiple arguments.
1911 unsigned ArgIdx = NumRequiredArgs;
1912 while (ArgIdx > 0) {
1913 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1914 NumRequiredArgs = ArgIdx;
1915
1916 --ArgIdx;
1917 }
1918
Chris Lattner58258242008-04-10 02:22:51 +00001919 return NumRequiredArgs;
1920}
1921
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001922bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001923 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001924 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001925
1926 if (isa<CXXMethodDecl>(this)) {
1927 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1928 return true;
1929 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001930
1931 switch (getTemplateSpecializationKind()) {
1932 case TSK_Undeclared:
1933 case TSK_ExplicitSpecialization:
1934 return false;
1935
1936 case TSK_ImplicitInstantiation:
1937 case TSK_ExplicitInstantiationDeclaration:
1938 case TSK_ExplicitInstantiationDefinition:
1939 // Handle below.
1940 break;
1941 }
1942
1943 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001944 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001945 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001946 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001947
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001948 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001949 return PatternDecl->isInlined();
1950
1951 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001952}
1953
Eli Friedman1b125c32012-02-07 03:50:18 +00001954static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1955 // Only consider file-scope declarations in this test.
1956 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1957 return false;
1958
1959 // Only consider explicit declarations; the presence of a builtin for a
1960 // libcall shouldn't affect whether a definition is externally visible.
1961 if (Redecl->isImplicit())
1962 return false;
1963
1964 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1965 return true; // Not an inline definition
1966
1967 return false;
1968}
1969
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001970/// \brief For a function declaration in C or C++, determine whether this
1971/// declaration causes the definition to be externally visible.
1972///
Eli Friedman1b125c32012-02-07 03:50:18 +00001973/// Specifically, this determines if adding the current declaration to the set
1974/// of redeclarations of the given functions causes
1975/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001976bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1977 assert(!doesThisDeclarationHaveABody() &&
1978 "Must have a declaration without a body.");
1979
1980 ASTContext &Context = getASTContext();
1981
David Blaikiebbafb8a2012-03-11 07:00:24 +00001982 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001983 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1984 // an externally visible definition.
1985 //
1986 // FIXME: What happens if gnu_inline gets added on after the first
1987 // declaration?
1988 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1989 return false;
1990
1991 const FunctionDecl *Prev = this;
1992 bool FoundBody = false;
1993 while ((Prev = Prev->getPreviousDecl())) {
1994 FoundBody |= Prev->Body;
1995
1996 if (Prev->Body) {
1997 // If it's not the case that both 'inline' and 'extern' are
1998 // specified on the definition, then it is always externally visible.
1999 if (!Prev->isInlineSpecified() ||
2000 Prev->getStorageClassAsWritten() != SC_Extern)
2001 return false;
2002 } else if (Prev->isInlineSpecified() &&
2003 Prev->getStorageClassAsWritten() != SC_Extern) {
2004 return false;
2005 }
2006 }
2007 return FoundBody;
2008 }
2009
David Blaikiebbafb8a2012-03-11 07:00:24 +00002010 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002011 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002012
2013 // C99 6.7.4p6:
2014 // [...] If all of the file scope declarations for a function in a
2015 // translation unit include the inline function specifier without extern,
2016 // then the definition in that translation unit is an inline definition.
2017 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002018 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002019 const FunctionDecl *Prev = this;
2020 bool FoundBody = false;
2021 while ((Prev = Prev->getPreviousDecl())) {
2022 FoundBody |= Prev->Body;
2023 if (RedeclForcesDefC99(Prev))
2024 return false;
2025 }
2026 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002027}
2028
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002029/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002030/// definition will be externally visible.
2031///
2032/// Inline function definitions are always available for inlining optimizations.
2033/// However, depending on the language dialect, declaration specifiers, and
2034/// attributes, the definition of an inline function may or may not be
2035/// "externally" visible to other translation units in the program.
2036///
2037/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002038/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002039/// inline definition becomes externally visible (C99 6.7.4p6).
2040///
2041/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2042/// definition, we use the GNU semantics for inline, which are nearly the
2043/// opposite of C99 semantics. In particular, "inline" by itself will create
2044/// an externally visible symbol, but "extern inline" will not create an
2045/// externally visible symbol.
2046bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002047 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002048 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002049 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002050
David Blaikiebbafb8a2012-03-11 07:00:24 +00002051 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002052 // Note: If you change the logic here, please change
2053 // doesDeclarationForceExternallyVisibleDefinition as well.
2054 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002055 // If it's not the case that both 'inline' and 'extern' are
2056 // specified on the definition, then this inline definition is
2057 // externally visible.
2058 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2059 return true;
2060
2061 // If any declaration is 'inline' but not 'extern', then this definition
2062 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002063 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2064 Redecl != RedeclEnd;
2065 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002066 if (Redecl->isInlineSpecified() &&
2067 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002068 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002069 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002070
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002071 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002072 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002073
Douglas Gregor299d76e2009-09-13 07:46:26 +00002074 // C99 6.7.4p6:
2075 // [...] If all of the file scope declarations for a function in a
2076 // translation unit include the inline function specifier without extern,
2077 // then the definition in that translation unit is an inline definition.
2078 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2079 Redecl != RedeclEnd;
2080 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002081 if (RedeclForcesDefC99(*Redecl))
2082 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002083 }
2084
2085 // C99 6.7.4p6:
2086 // An inline definition does not provide an external definition for the
2087 // function, and does not forbid an external definition in another
2088 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002089 return false;
2090}
2091
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002092/// getOverloadedOperator - Which C++ overloaded operator this
2093/// function represents, if any.
2094OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002095 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2096 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002097 else
2098 return OO_None;
2099}
2100
Alexis Huntc88db062010-01-13 09:01:02 +00002101/// getLiteralIdentifier - The literal suffix identifier this function
2102/// represents, if any.
2103const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2104 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2105 return getDeclName().getCXXLiteralIdentifier();
2106 else
2107 return 0;
2108}
2109
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002110FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2111 if (TemplateOrSpecialization.isNull())
2112 return TK_NonTemplate;
2113 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2114 return TK_FunctionTemplate;
2115 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2116 return TK_MemberSpecialization;
2117 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2118 return TK_FunctionTemplateSpecialization;
2119 if (TemplateOrSpecialization.is
2120 <DependentFunctionTemplateSpecializationInfo*>())
2121 return TK_DependentFunctionTemplateSpecialization;
2122
David Blaikie83d382b2011-09-23 05:06:16 +00002123 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002124}
2125
Douglas Gregord801b062009-10-07 23:56:10 +00002126FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002127 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002128 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2129
2130 return 0;
2131}
2132
Douglas Gregor06db9f52009-10-12 20:18:28 +00002133MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2134 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2135}
2136
Douglas Gregord801b062009-10-07 23:56:10 +00002137void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002138FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2139 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002140 TemplateSpecializationKind TSK) {
2141 assert(TemplateOrSpecialization.isNull() &&
2142 "Member function is already a specialization");
2143 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002144 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002145 TemplateOrSpecialization = Info;
2146}
2147
Douglas Gregorafca3b42009-10-27 20:53:28 +00002148bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002149 // If the function is invalid, it can't be implicitly instantiated.
2150 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002151 return false;
2152
2153 switch (getTemplateSpecializationKind()) {
2154 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002155 case TSK_ExplicitInstantiationDefinition:
2156 return false;
2157
2158 case TSK_ImplicitInstantiation:
2159 return true;
2160
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002161 // It is possible to instantiate TSK_ExplicitSpecialization kind
2162 // if the FunctionDecl has a class scope specialization pattern.
2163 case TSK_ExplicitSpecialization:
2164 return getClassScopeSpecializationPattern() != 0;
2165
Douglas Gregorafca3b42009-10-27 20:53:28 +00002166 case TSK_ExplicitInstantiationDeclaration:
2167 // Handled below.
2168 break;
2169 }
2170
2171 // Find the actual template from which we will instantiate.
2172 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002173 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002174 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002175 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002176
2177 // C++0x [temp.explicit]p9:
2178 // Except for inline functions, other explicit instantiation declarations
2179 // have the effect of suppressing the implicit instantiation of the entity
2180 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002181 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002182 return true;
2183
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002184 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002185}
2186
2187bool FunctionDecl::isTemplateInstantiation() const {
2188 switch (getTemplateSpecializationKind()) {
2189 case TSK_Undeclared:
2190 case TSK_ExplicitSpecialization:
2191 return false;
2192 case TSK_ImplicitInstantiation:
2193 case TSK_ExplicitInstantiationDeclaration:
2194 case TSK_ExplicitInstantiationDefinition:
2195 return true;
2196 }
2197 llvm_unreachable("All TSK values handled.");
2198}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002199
2200FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002201 // Handle class scope explicit specialization special case.
2202 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2203 return getClassScopeSpecializationPattern();
2204
Douglas Gregorafca3b42009-10-27 20:53:28 +00002205 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2206 while (Primary->getInstantiatedFromMemberTemplate()) {
2207 // If we have hit a point where the user provided a specialization of
2208 // this template, we're done looking.
2209 if (Primary->isMemberSpecialization())
2210 break;
2211
2212 Primary = Primary->getInstantiatedFromMemberTemplate();
2213 }
2214
2215 return Primary->getTemplatedDecl();
2216 }
2217
2218 return getInstantiatedFromMemberFunction();
2219}
2220
Douglas Gregor70d83e22009-06-29 17:30:29 +00002221FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002222 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002223 = TemplateOrSpecialization
2224 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002225 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002226 }
2227 return 0;
2228}
2229
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002230FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2231 return getASTContext().getClassScopeSpecializationPattern(this);
2232}
2233
Douglas Gregor70d83e22009-06-29 17:30:29 +00002234const TemplateArgumentList *
2235FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002236 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002237 = TemplateOrSpecialization
2238 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002239 return Info->TemplateArguments;
2240 }
2241 return 0;
2242}
2243
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002244const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002245FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2246 if (FunctionTemplateSpecializationInfo *Info
2247 = TemplateOrSpecialization
2248 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2249 return Info->TemplateArgumentsAsWritten;
2250 }
2251 return 0;
2252}
2253
Mike Stump11289f42009-09-09 15:08:12 +00002254void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002255FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2256 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002257 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002258 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002259 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002260 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2261 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002262 assert(TSK != TSK_Undeclared &&
2263 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002264 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002265 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002266 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002267 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2268 TemplateArgs,
2269 TemplateArgsAsWritten,
2270 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002271 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002272 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002273}
2274
John McCallb9c78482010-04-08 09:05:18 +00002275void
2276FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2277 const UnresolvedSetImpl &Templates,
2278 const TemplateArgumentListInfo &TemplateArgs) {
2279 assert(TemplateOrSpecialization.isNull());
2280 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2281 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002282 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002283 void *Buffer = Context.Allocate(Size);
2284 DependentFunctionTemplateSpecializationInfo *Info =
2285 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2286 TemplateArgs);
2287 TemplateOrSpecialization = Info;
2288}
2289
2290DependentFunctionTemplateSpecializationInfo::
2291DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2292 const TemplateArgumentListInfo &TArgs)
2293 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2294
2295 d.NumTemplates = Ts.size();
2296 d.NumArgs = TArgs.size();
2297
2298 FunctionTemplateDecl **TsArray =
2299 const_cast<FunctionTemplateDecl**>(getTemplates());
2300 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2301 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2302
2303 TemplateArgumentLoc *ArgsArray =
2304 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2305 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2306 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2307}
2308
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002309TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002310 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002311 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002312 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002313 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002314 if (FTSInfo)
2315 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002316
Douglas Gregord801b062009-10-07 23:56:10 +00002317 MemberSpecializationInfo *MSInfo
2318 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2319 if (MSInfo)
2320 return MSInfo->getTemplateSpecializationKind();
2321
2322 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002323}
2324
Mike Stump11289f42009-09-09 15:08:12 +00002325void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002326FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2327 SourceLocation PointOfInstantiation) {
2328 if (FunctionTemplateSpecializationInfo *FTSInfo
2329 = TemplateOrSpecialization.dyn_cast<
2330 FunctionTemplateSpecializationInfo*>()) {
2331 FTSInfo->setTemplateSpecializationKind(TSK);
2332 if (TSK != TSK_ExplicitSpecialization &&
2333 PointOfInstantiation.isValid() &&
2334 FTSInfo->getPointOfInstantiation().isInvalid())
2335 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2336 } else if (MemberSpecializationInfo *MSInfo
2337 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2338 MSInfo->setTemplateSpecializationKind(TSK);
2339 if (TSK != TSK_ExplicitSpecialization &&
2340 PointOfInstantiation.isValid() &&
2341 MSInfo->getPointOfInstantiation().isInvalid())
2342 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2343 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002344 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002345}
2346
2347SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002348 if (FunctionTemplateSpecializationInfo *FTSInfo
2349 = TemplateOrSpecialization.dyn_cast<
2350 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002351 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002352 else if (MemberSpecializationInfo *MSInfo
2353 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002354 return MSInfo->getPointOfInstantiation();
2355
2356 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002357}
2358
Douglas Gregor6411b922009-09-11 20:15:17 +00002359bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002360 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002361 return true;
2362
2363 // If this function was instantiated from a member function of a
2364 // class template, check whether that member function was defined out-of-line.
2365 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2366 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002367 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002368 return Definition->isOutOfLine();
2369 }
2370
2371 // If this function was instantiated from a function template,
2372 // check whether that function template was defined out-of-line.
2373 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2374 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002375 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002376 return Definition->isOutOfLine();
2377 }
2378
2379 return false;
2380}
2381
Abramo Bagnaraea947882011-03-08 16:41:52 +00002382SourceRange FunctionDecl::getSourceRange() const {
2383 return SourceRange(getOuterLocStart(), EndRangeLoc);
2384}
2385
Anna Zaks28db7ce2012-01-18 02:45:01 +00002386unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002387 IdentifierInfo *FnInfo = getIdentifier();
2388
2389 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002390 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002391
2392 // Builtin handling.
2393 switch (getBuiltinID()) {
2394 case Builtin::BI__builtin_memset:
2395 case Builtin::BI__builtin___memset_chk:
2396 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002397 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002398
2399 case Builtin::BI__builtin_memcpy:
2400 case Builtin::BI__builtin___memcpy_chk:
2401 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002402 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002403
2404 case Builtin::BI__builtin_memmove:
2405 case Builtin::BI__builtin___memmove_chk:
2406 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002407 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002408
2409 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002410 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002411 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002412 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002413
2414 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002415 case Builtin::BImemcmp:
2416 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002417
2418 case Builtin::BI__builtin_strncpy:
2419 case Builtin::BI__builtin___strncpy_chk:
2420 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002421 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002422
2423 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002424 case Builtin::BIstrncmp:
2425 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002426
2427 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002428 case Builtin::BIstrncasecmp:
2429 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002430
2431 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002432 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002433 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002434 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002435
2436 case Builtin::BI__builtin_strndup:
2437 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002438 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002439
Anna Zaks314cd092012-02-01 19:08:57 +00002440 case Builtin::BI__builtin_strlen:
2441 case Builtin::BIstrlen:
2442 return Builtin::BIstrlen;
2443
Anna Zaks201d4892012-01-13 21:52:01 +00002444 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002445 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002446 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002447 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002448 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002449 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002450 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002451 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002452 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002453 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002454 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002455 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002456 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002457 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002458 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002459 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002460 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002461 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002462 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002463 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002464 else if (FnInfo->isStr("strlen"))
2465 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002466 }
2467 break;
2468 }
Anna Zaks22122702012-01-17 00:37:07 +00002469 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002470}
2471
Chris Lattner59a25942008-03-31 00:36:02 +00002472//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002473// FieldDecl Implementation
2474//===----------------------------------------------------------------------===//
2475
Jay Foad39c79802011-01-12 09:06:06 +00002476FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002477 SourceLocation StartLoc, SourceLocation IdLoc,
2478 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002479 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002480 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002481 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002482 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002483}
2484
Douglas Gregor72172e92012-01-05 21:55:30 +00002485FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2486 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2487 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002488 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002489}
2490
Sebastian Redl833ef452010-01-26 22:01:41 +00002491bool FieldDecl::isAnonymousStructOrUnion() const {
2492 if (!isImplicit() || getDeclName())
2493 return false;
2494
2495 if (const RecordType *Record = getType()->getAs<RecordType>())
2496 return Record->getDecl()->isAnonymousStructOrUnion();
2497
2498 return false;
2499}
2500
Richard Smithcaf33902011-10-10 18:28:20 +00002501unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2502 assert(isBitField() && "not a bitfield");
2503 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2504 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2505}
2506
John McCall4e819612011-01-20 07:57:12 +00002507unsigned FieldDecl::getFieldIndex() const {
2508 if (CachedFieldIndex) return CachedFieldIndex - 1;
2509
Richard Smithd62306a2011-11-10 06:34:14 +00002510 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002511 const RecordDecl *RD = getParent();
2512 const FieldDecl *LastFD = 0;
2513 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002514
2515 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2516 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002517 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002518
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002519 if (IsMsStruct) {
2520 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002521 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002522 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002523 continue;
2524 }
David Blaikie40ed2972012-06-06 20:45:41 +00002525 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002526 }
John McCall4e819612011-01-20 07:57:12 +00002527 }
2528
Richard Smithd62306a2011-11-10 06:34:14 +00002529 assert(CachedFieldIndex && "failed to find field in parent");
2530 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002531}
2532
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002533SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002534 if (const Expr *E = InitializerOrBitWidth.getPointer())
2535 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002536 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002537}
2538
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002539void FieldDecl::setBitWidth(Expr *Width) {
2540 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2541 "bit width or initializer already set");
2542 InitializerOrBitWidth.setPointer(Width);
2543}
2544
Richard Smith938f40b2011-06-11 17:19:42 +00002545void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002546 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002547 "bit width or initializer already set");
2548 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002549}
2550
Sebastian Redl833ef452010-01-26 22:01:41 +00002551//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002552// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002553//===----------------------------------------------------------------------===//
2554
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002555SourceLocation TagDecl::getOuterLocStart() const {
2556 return getTemplateOrInnerLocStart(this);
2557}
2558
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002559SourceRange TagDecl::getSourceRange() const {
2560 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002561 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002562}
2563
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002564TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002565 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002566}
2567
Richard Smithdda56e42011-04-15 14:24:37 +00002568void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2569 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002570 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002571 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002572 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002573}
2574
Douglas Gregordee1be82009-01-17 00:42:38 +00002575void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002576 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002577
2578 if (isa<CXXRecordDecl>(this)) {
2579 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2580 struct CXXRecordDecl::DefinitionData *Data =
2581 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002582 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2583 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002584 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002585}
2586
2587void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002588 assert((!isa<CXXRecordDecl>(this) ||
2589 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2590 "definition completed but not started");
2591
John McCallf937c022011-10-07 06:10:15 +00002592 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002593 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002594
2595 if (ASTMutationListener *L = getASTMutationListener())
2596 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002597}
2598
John McCallf937c022011-10-07 06:10:15 +00002599TagDecl *TagDecl::getDefinition() const {
2600 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002601 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002602 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2603 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002604
2605 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002606 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002607 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002608 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002609
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002610 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002611}
2612
Douglas Gregor14454802011-02-25 02:25:35 +00002613void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2614 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002615 // Make sure the extended qualifier info is allocated.
2616 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002617 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002618 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002619 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002620 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002621 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002622 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002623 if (getExtInfo()->NumTemplParamLists == 0) {
2624 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002625 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002626 }
2627 else
2628 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002629 }
2630 }
2631}
2632
Abramo Bagnara60804e12011-03-18 15:16:37 +00002633void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2634 unsigned NumTPLists,
2635 TemplateParameterList **TPLists) {
2636 assert(NumTPLists > 0);
2637 // Make sure the extended decl info is allocated.
2638 if (!hasExtInfo())
2639 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002640 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002641 // Set the template parameter lists info.
2642 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2643}
2644
Ted Kremenek21475702008-09-05 17:16:31 +00002645//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002646// EnumDecl Implementation
2647//===----------------------------------------------------------------------===//
2648
David Blaikie68e081d2011-12-20 02:48:34 +00002649void EnumDecl::anchor() { }
2650
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002651EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2652 SourceLocation StartLoc, SourceLocation IdLoc,
2653 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002654 EnumDecl *PrevDecl, bool IsScoped,
2655 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002656 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002657 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002658 C.getTypeDeclType(Enum, PrevDecl);
2659 return Enum;
2660}
2661
Douglas Gregor72172e92012-01-05 21:55:30 +00002662EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2663 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2664 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2665 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002666}
2667
Douglas Gregord5058122010-02-11 01:19:42 +00002668void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002669 QualType NewPromotionType,
2670 unsigned NumPositiveBits,
2671 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002672 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002673 if (!IntegerType)
2674 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002675 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002676 setNumPositiveBits(NumPositiveBits);
2677 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002678 TagDecl::completeDefinition();
2679}
2680
Richard Smith7d137e32012-03-23 03:33:32 +00002681TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2682 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2683 return MSI->getTemplateSpecializationKind();
2684
2685 return TSK_Undeclared;
2686}
2687
2688void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2689 SourceLocation PointOfInstantiation) {
2690 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2691 assert(MSI && "Not an instantiated member enumeration?");
2692 MSI->setTemplateSpecializationKind(TSK);
2693 if (TSK != TSK_ExplicitSpecialization &&
2694 PointOfInstantiation.isValid() &&
2695 MSI->getPointOfInstantiation().isInvalid())
2696 MSI->setPointOfInstantiation(PointOfInstantiation);
2697}
2698
Richard Smith4b38ded2012-03-14 23:13:10 +00002699EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2700 if (SpecializationInfo)
2701 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2702
2703 return 0;
2704}
2705
2706void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2707 TemplateSpecializationKind TSK) {
2708 assert(!SpecializationInfo && "Member enum is already a specialization");
2709 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2710}
2711
Sebastian Redl833ef452010-01-26 22:01:41 +00002712//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002713// RecordDecl Implementation
2714//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002715
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002716RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2717 SourceLocation StartLoc, SourceLocation IdLoc,
2718 IdentifierInfo *Id, RecordDecl *PrevDecl)
2719 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002720 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002721 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002722 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002723 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002724 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002725}
2726
Jay Foad39c79802011-01-12 09:06:06 +00002727RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002728 SourceLocation StartLoc, SourceLocation IdLoc,
2729 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2730 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2731 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002732 C.getTypeDeclType(R, PrevDecl);
2733 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002734}
2735
Douglas Gregor72172e92012-01-05 21:55:30 +00002736RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2737 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2738 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2739 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002740}
2741
Douglas Gregordfcad112009-03-25 15:59:44 +00002742bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002743 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002744 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2745}
2746
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002747RecordDecl::field_iterator RecordDecl::field_begin() const {
2748 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2749 LoadFieldsFromExternalStorage();
2750
2751 return field_iterator(decl_iterator(FirstDecl));
2752}
2753
Douglas Gregorb11aad82011-02-19 18:51:44 +00002754/// completeDefinition - Notes that the definition of this type is now
2755/// complete.
2756void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002757 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002758 TagDecl::completeDefinition();
2759}
2760
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002761void RecordDecl::LoadFieldsFromExternalStorage() const {
2762 ExternalASTSource *Source = getASTContext().getExternalSource();
2763 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2764
2765 // Notify that we have a RecordDecl doing some initialization.
2766 ExternalASTSource::Deserializing TheFields(Source);
2767
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002768 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002769 LoadedFieldsFromExternalStorage = true;
2770 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2771 case ELR_Success:
2772 break;
2773
2774 case ELR_AlreadyLoaded:
2775 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002776 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002777 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002778
2779#ifndef NDEBUG
2780 // Check that all decls we got were FieldDecls.
2781 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2782 assert(isa<FieldDecl>(Decls[i]));
2783#endif
2784
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002785 if (Decls.empty())
2786 return;
2787
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002788 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2789 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002790}
2791
Steve Naroff415d3d52008-10-08 17:01:13 +00002792//===----------------------------------------------------------------------===//
2793// BlockDecl Implementation
2794//===----------------------------------------------------------------------===//
2795
David Blaikie9c70e042011-09-21 18:16:56 +00002796void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002797 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002798
Steve Naroffc4b30e52009-03-13 16:56:44 +00002799 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002800 if (!NewParamInfo.empty()) {
2801 NumParams = NewParamInfo.size();
2802 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2803 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002804 }
2805}
2806
John McCall351762c2011-02-07 10:33:21 +00002807void BlockDecl::setCaptures(ASTContext &Context,
2808 const Capture *begin,
2809 const Capture *end,
2810 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002811 CapturesCXXThis = capturesCXXThis;
2812
2813 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002814 NumCaptures = 0;
2815 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002816 return;
2817 }
2818
John McCall351762c2011-02-07 10:33:21 +00002819 NumCaptures = end - begin;
2820
2821 // Avoid new Capture[] because we don't want to provide a default
2822 // constructor.
2823 size_t allocationSize = NumCaptures * sizeof(Capture);
2824 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2825 memcpy(buffer, begin, allocationSize);
2826 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002827}
Sebastian Redl833ef452010-01-26 22:01:41 +00002828
John McCallce45f882011-06-15 22:51:16 +00002829bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2830 for (capture_const_iterator
2831 i = capture_begin(), e = capture_end(); i != e; ++i)
2832 // Only auto vars can be captured, so no redeclaration worries.
2833 if (i->getVariable() == variable)
2834 return true;
2835
2836 return false;
2837}
2838
Douglas Gregor70226da2010-12-21 16:27:07 +00002839SourceRange BlockDecl::getSourceRange() const {
2840 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2841}
Sebastian Redl833ef452010-01-26 22:01:41 +00002842
2843//===----------------------------------------------------------------------===//
2844// Other Decl Allocation/Deallocation Method Implementations
2845//===----------------------------------------------------------------------===//
2846
David Blaikie68e081d2011-12-20 02:48:34 +00002847void TranslationUnitDecl::anchor() { }
2848
Sebastian Redl833ef452010-01-26 22:01:41 +00002849TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2850 return new (C) TranslationUnitDecl(C);
2851}
2852
David Blaikie68e081d2011-12-20 02:48:34 +00002853void LabelDecl::anchor() { }
2854
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002855LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002856 SourceLocation IdentL, IdentifierInfo *II) {
2857 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2858}
2859
2860LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2861 SourceLocation IdentL, IdentifierInfo *II,
2862 SourceLocation GnuLabelL) {
2863 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2864 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002865}
2866
Douglas Gregor72172e92012-01-05 21:55:30 +00002867LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2868 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2869 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002870}
2871
David Blaikie68e081d2011-12-20 02:48:34 +00002872void ValueDecl::anchor() { }
2873
2874void ImplicitParamDecl::anchor() { }
2875
Sebastian Redl833ef452010-01-26 22:01:41 +00002876ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002877 SourceLocation IdLoc,
2878 IdentifierInfo *Id,
2879 QualType Type) {
2880 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002881}
2882
Douglas Gregor72172e92012-01-05 21:55:30 +00002883ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2884 unsigned ID) {
2885 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2886 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2887}
2888
Sebastian Redl833ef452010-01-26 22:01:41 +00002889FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002890 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002891 const DeclarationNameInfo &NameInfo,
2892 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002893 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002894 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002895 bool hasWrittenPrototype,
2896 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002897 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2898 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002899 isInlineSpecified,
2900 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002901 New->HasWrittenPrototype = hasWrittenPrototype;
2902 return New;
2903}
2904
Douglas Gregor72172e92012-01-05 21:55:30 +00002905FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2906 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2907 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2908 DeclarationNameInfo(), QualType(), 0,
2909 SC_None, SC_None, false, false);
2910}
2911
Sebastian Redl833ef452010-01-26 22:01:41 +00002912BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2913 return new (C) BlockDecl(DC, L);
2914}
2915
Douglas Gregor72172e92012-01-05 21:55:30 +00002916BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2917 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2918 return new (Mem) BlockDecl(0, SourceLocation());
2919}
2920
Sebastian Redl833ef452010-01-26 22:01:41 +00002921EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2922 SourceLocation L,
2923 IdentifierInfo *Id, QualType T,
2924 Expr *E, const llvm::APSInt &V) {
2925 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2926}
2927
Douglas Gregor72172e92012-01-05 21:55:30 +00002928EnumConstantDecl *
2929EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2930 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2931 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2932 llvm::APSInt());
2933}
2934
David Blaikie68e081d2011-12-20 02:48:34 +00002935void IndirectFieldDecl::anchor() { }
2936
Benjamin Kramer39593702010-11-21 14:11:41 +00002937IndirectFieldDecl *
2938IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2939 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2940 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002941 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2942}
2943
Douglas Gregor72172e92012-01-05 21:55:30 +00002944IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2945 unsigned ID) {
2946 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2947 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2948 QualType(), 0, 0);
2949}
2950
Douglas Gregorbe996932010-09-01 20:41:53 +00002951SourceRange EnumConstantDecl::getSourceRange() const {
2952 SourceLocation End = getLocation();
2953 if (Init)
2954 End = Init->getLocEnd();
2955 return SourceRange(getLocation(), End);
2956}
2957
David Blaikie68e081d2011-12-20 02:48:34 +00002958void TypeDecl::anchor() { }
2959
Sebastian Redl833ef452010-01-26 22:01:41 +00002960TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002961 SourceLocation StartLoc, SourceLocation IdLoc,
2962 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2963 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002964}
2965
David Blaikie68e081d2011-12-20 02:48:34 +00002966void TypedefNameDecl::anchor() { }
2967
Douglas Gregor72172e92012-01-05 21:55:30 +00002968TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2969 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2970 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2971}
2972
Richard Smithdda56e42011-04-15 14:24:37 +00002973TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2974 SourceLocation StartLoc,
2975 SourceLocation IdLoc, IdentifierInfo *Id,
2976 TypeSourceInfo *TInfo) {
2977 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2978}
2979
Douglas Gregor72172e92012-01-05 21:55:30 +00002980TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2981 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2982 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2983}
2984
Abramo Bagnaraea947882011-03-08 16:41:52 +00002985SourceRange TypedefDecl::getSourceRange() const {
2986 SourceLocation RangeEnd = getLocation();
2987 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2988 if (typeIsPostfix(TInfo->getType()))
2989 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2990 }
2991 return SourceRange(getLocStart(), RangeEnd);
2992}
2993
Richard Smithdda56e42011-04-15 14:24:37 +00002994SourceRange TypeAliasDecl::getSourceRange() const {
2995 SourceLocation RangeEnd = getLocStart();
2996 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2997 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2998 return SourceRange(getLocStart(), RangeEnd);
2999}
3000
David Blaikie68e081d2011-12-20 02:48:34 +00003001void FileScopeAsmDecl::anchor() { }
3002
Sebastian Redl833ef452010-01-26 22:01:41 +00003003FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003004 StringLiteral *Str,
3005 SourceLocation AsmLoc,
3006 SourceLocation RParenLoc) {
3007 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003008}
Douglas Gregorba345522011-12-02 23:23:56 +00003009
Douglas Gregor72172e92012-01-05 21:55:30 +00003010FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3011 unsigned ID) {
3012 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3013 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3014}
3015
Douglas Gregorba345522011-12-02 23:23:56 +00003016//===----------------------------------------------------------------------===//
3017// ImportDecl Implementation
3018//===----------------------------------------------------------------------===//
3019
3020/// \brief Retrieve the number of module identifiers needed to name the given
3021/// module.
3022static unsigned getNumModuleIdentifiers(Module *Mod) {
3023 unsigned Result = 1;
3024 while (Mod->Parent) {
3025 Mod = Mod->Parent;
3026 ++Result;
3027 }
3028 return Result;
3029}
3030
Douglas Gregor22d09742012-01-03 18:04:46 +00003031ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003032 Module *Imported,
3033 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003034 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003035 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003036{
3037 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3038 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3039 memcpy(StoredLocs, IdentifierLocs.data(),
3040 IdentifierLocs.size() * sizeof(SourceLocation));
3041}
3042
Douglas Gregor22d09742012-01-03 18:04:46 +00003043ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003044 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003045 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003046 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003047{
3048 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3049}
3050
3051ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003052 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003053 ArrayRef<SourceLocation> IdentifierLocs) {
3054 void *Mem = C.Allocate(sizeof(ImportDecl) +
3055 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003056 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003057}
3058
3059ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003060 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003061 Module *Imported,
3062 SourceLocation EndLoc) {
3063 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003064 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003065 Import->setImplicit();
3066 return Import;
3067}
3068
Douglas Gregor72172e92012-01-05 21:55:30 +00003069ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3070 unsigned NumLocations) {
3071 void *Mem = AllocateDeserializedDecl(C, ID,
3072 (sizeof(ImportDecl) +
3073 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003074 return new (Mem) ImportDecl(EmptyShell());
3075}
3076
3077ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3078 if (!ImportedAndComplete.getInt())
3079 return ArrayRef<SourceLocation>();
3080
3081 const SourceLocation *StoredLocs
3082 = reinterpret_cast<const SourceLocation *>(this + 1);
3083 return ArrayRef<SourceLocation>(StoredLocs,
3084 getNumModuleIdentifiers(getImportedModule()));
3085}
3086
3087SourceRange ImportDecl::getSourceRange() const {
3088 if (!ImportedAndComplete.getInt())
3089 return SourceRange(getLocation(),
3090 *reinterpret_cast<const SourceLocation *>(this + 1));
3091
3092 return SourceRange(getLocation(), getIdentifierLocs().back());
3093}