blob: 7ca8d397f1fb6aeb8f02fb5af9028e06503213f9 [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.
173 ASTContext &Context = D->getASTContext();
174 if (!Context.getLangOpts().CPlusPlus)
175 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 &&
196 FD->getASTContext().getLangOpts().InlineVisibilityHidden &&
197 FD->hasBody(Def) && Def->isInlined();
198}
199
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000200static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
201 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000202 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000203 "Not a name having namespace scope");
204 ASTContext &Context = D->getASTContext();
205
206 // C++ [basic.link]p3:
207 // A name having namespace scope (3.3.6) has internal linkage if it
208 // is the name of
209 // - an object, reference, function or function template that is
210 // explicitly declared static; or,
211 // (This bullet corresponds to C99 6.2.2p3.)
212 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
213 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000214 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000215 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000216
217 // - an object or reference that is explicitly declared const
218 // and neither explicitly declared extern nor previously
219 // declared to have external linkage; or
220 // (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000221 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000222 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000223 Var->getStorageClass() != SC_Extern &&
224 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000225 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000226 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000227 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000228 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000229 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 FoundExtern = true;
231
232 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000233 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000234 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000235 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000236 const VarDecl *PrevVar = Var->getPreviousDecl();
237 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000238 if (PrevVar->getStorageClass() == SC_PrivateExtern)
239 break;
240 if (PrevVar)
241 return PrevVar->getLinkageAndVisibility();
242 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000243 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000244 // C++ [temp]p4:
245 // A non-member function template can have internal linkage; any
246 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000247 const FunctionDecl *Function = 0;
248 if (const FunctionTemplateDecl *FunTmpl
249 = dyn_cast<FunctionTemplateDecl>(D))
250 Function = FunTmpl->getTemplatedDecl();
251 else
252 Function = cast<FunctionDecl>(D);
253
254 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000255 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000256 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000257 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
258 // - a data member of an anonymous union.
259 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000260 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000261 }
262
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000263 if (D->isInAnonymousNamespace()) {
264 const VarDecl *Var = dyn_cast<VarDecl>(D);
265 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000266 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
267 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000268 return LinkageInfo::uniqueExternal();
269 }
John McCallb7139c42010-10-28 04:18:25 +0000270
John McCall457a04e2010-10-22 21:05:15 +0000271 // Set up the defaults.
272
273 // C99 6.2.2p5:
274 // If the declaration of an identifier for an object has file
275 // scope and no storage-class specifier, its linkage is
276 // external.
John McCallc273f242010-10-30 11:50:40 +0000277 LinkageInfo LV;
278
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000279 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000280 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000281 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000282 } else {
283 // If we're declared in a namespace with a visibility attribute,
284 // use that namespace's visibility, but don't call it explicit.
285 for (const DeclContext *DC = D->getDeclContext();
286 !isa<TranslationUnitDecl>(DC);
287 DC = DC->getParent()) {
288 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
289 if (!ND) continue;
290 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000291 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000292 break;
293 }
294 }
295 }
296 }
297
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000298 if (!OnlyTemplate) {
Rafael Espindolab660efd2012-04-19 04:37:16 +0000299 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000300 // If we're paying attention to global visibility, apply
301 // -finline-visibility-hidden if this is an inline method.
302 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
303 LV.mergeVisibility(HiddenVisibility, true);
304 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000305
Douglas Gregorf73b2822009-11-25 22:24:25 +0000306 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000307
Douglas Gregorf73b2822009-11-25 22:24:25 +0000308 // A name having namespace scope has external linkage if it is the
309 // name of
310 //
311 // - an object or reference, unless it has internal linkage; or
312 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000313 // GCC applies the following optimization to variables and static
314 // data members, but not to functions:
315 //
John McCall457a04e2010-10-22 21:05:15 +0000316 // Modify the variable's LV by the LV of its type unless this is
317 // C or extern "C". This follows from [basic.link]p9:
318 // A type without linkage shall not be used as the type of a
319 // variable or function with external linkage unless
320 // - the entity has C language linkage, or
321 // - the entity is declared within an unnamed namespace, or
322 // - the entity is not used or is defined in the same
323 // translation unit.
324 // and [basic.link]p10:
325 // ...the types specified by all declarations referring to a
326 // given variable or function shall be identical...
327 // C does not have an equivalent rule.
328 //
John McCall5fe84122010-10-26 04:59:26 +0000329 // Ignore this if we've got an explicit attribute; the user
330 // probably knows what they're doing.
331 //
John McCall457a04e2010-10-22 21:05:15 +0000332 // Note that we don't want to make the variable non-external
333 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000334 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000335 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000336 LinkageInfo TypeLV = getLVForType(Var->getType());
337 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000338 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000339 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000340 }
341
John McCall23032652010-11-02 18:38:13 +0000342 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000343 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000344
David Blaikiebbafb8a2012-03-11 07:00:24 +0000345 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000346 (Var->getStorageClass() == SC_Extern ||
347 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000348
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349 // C99 6.2.2p4:
350 // For an identifier declared with the storage-class specifier
351 // extern in a scope in which a prior declaration of that
352 // identifier is visible, if the prior declaration specifies
353 // internal or external linkage, the linkage of the identifier
354 // at the later declaration is the same as the linkage
355 // specified at the prior declaration. If no prior declaration
356 // is visible, or if the prior declaration specifies no
357 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000358 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000359 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000360 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
361 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000362 }
363 }
364
Douglas Gregorf73b2822009-11-25 22:24:25 +0000365 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000366 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000367 // In theory, we can modify the function's LV by the LV of its
368 // type unless it has C linkage (see comment above about variables
369 // for justification). In practice, GCC doesn't do this, so it's
370 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000371
John McCall23032652010-11-02 18:38:13 +0000372 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000373 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000374
Douglas Gregorf73b2822009-11-25 22:24:25 +0000375 // C99 6.2.2p5:
376 // If the declaration of an identifier for a function has no
377 // storage-class specifier, its linkage is determined exactly
378 // as if it were declared with the storage-class specifier
379 // extern.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000380 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000381 (Function->getStorageClass() == SC_Extern ||
382 Function->getStorageClass() == SC_PrivateExtern ||
383 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000384 // C99 6.2.2p4:
385 // For an identifier declared with the storage-class specifier
386 // extern in a scope in which a prior declaration of that
387 // identifier is visible, if the prior declaration specifies
388 // internal or external linkage, the linkage of the identifier
389 // at the later declaration is the same as the linkage
390 // specified at the prior declaration. If no prior declaration
391 // is visible, or if the prior declaration specifies no
392 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000393 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000394 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000395 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
396 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000397 }
398 }
399
John McCallf768aa72011-02-10 06:50:24 +0000400 // In C++, then if the type of the function uses a type with
401 // unique-external linkage, it's not legally usable from outside
402 // this translation unit. However, we should use the C linkage
403 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000404 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000405 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000406 Function->getType()->getLinkage() == UniqueExternalLinkage)
407 return LinkageInfo::uniqueExternal();
408
John McCallb8c604a2011-06-27 23:06:04 +0000409 // Consider LV from the template and the template arguments unless
410 // this is an explicit specialization with a visibility attribute.
411 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000412 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000413 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
414 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
415 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
416 OnlyTemplate);
417 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000418 LV.mergeWithMin(TempLV);
Rafael Espindola340941d2012-05-25 16:41:35 +0000419 LV.mergeWithMin(ArgsLV);
420 } else {
421 LV.mergeLinkage(TempLV);
422 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000423 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000424 }
425
Douglas Gregorf73b2822009-11-25 22:24:25 +0000426 // - a named class (Clause 9), or an unnamed class defined in a
427 // typedef declaration in which the class has the typedef name
428 // for linkage purposes (7.1.3); or
429 // - a named enumeration (7.2), or an unnamed enumeration
430 // defined in a typedef declaration in which the enumeration
431 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000432 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
433 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000434 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000435 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000436
John McCall457a04e2010-10-22 21:05:15 +0000437 // If this is a class template specialization, consider the
438 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000439 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000440 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000441 // From the template.
442 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000443
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000444 // The arguments at which the template was instantiated.
445 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
446 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
447 OnlyTemplate);
448 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000449 LV.mergeWithMin(TempLV);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000450 LV.mergeWithMin(ArgsLV);
451 } else {
452 LV.mergeLinkage(TempLV);
453 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000454 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000455 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000456
457 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000458 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000459 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
460 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000461 if (!isExternalLinkage(EnumLV.linkage()))
462 return LinkageInfo::none();
463 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000464
465 // - a template, unless it is a function template that has
466 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000467 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000468 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000469 // - a namespace (7.3), unless it is declared within an unnamed
470 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000471 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
472 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000473
John McCall457a04e2010-10-22 21:05:15 +0000474 // By extension, we assign external linkage to Objective-C
475 // interfaces.
476 } else if (isa<ObjCInterfaceDecl>(D)) {
477 // fallout
478
479 // Everything not covered here has no linkage.
480 } else {
John McCallc273f242010-10-30 11:50:40 +0000481 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000482 }
483
484 // If we ended up with non-external linkage, visibility should
485 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000486 if (LV.linkage() != ExternalLinkage)
487 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000488
John McCall457a04e2010-10-22 21:05:15 +0000489 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000490}
491
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000492static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000493 // Only certain class members have linkage. Note that fields don't
494 // really have linkage, but it's convenient to say they do for the
495 // purposes of calculating linkage of pointer-to-data-member
496 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000497 if (!(isa<CXXMethodDecl>(D) ||
498 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000499 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000500 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000501 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000502 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000503
John McCall07072662010-11-02 01:45:15 +0000504 LinkageInfo LV;
505
John McCall07072662010-11-02 01:45:15 +0000506 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000507 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000508 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000509 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000510 // If we're paying attention to global visibility, apply
511 // -finline-visibility-hidden if this is an inline method.
512 //
513 // Note that we do this before merging information about
514 // the class visibility.
515 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
516 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000517 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000518
519 // If this class member has an explicit visibility attribute, the only
520 // thing that can change its visibility is the template arguments, so
521 // only look for them when processing the the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000522 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000523
Rafael Espindola53cf2192012-04-19 05:50:08 +0000524 // If this member has an visibility attribute, ClassF will exclude
525 // attributes on the class or command line options, keeping only information
526 // about the template instantiation. If the member has no visibility
527 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
528 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000529 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
530 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000531 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000532 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000533
534 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000535 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000536 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000537
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000538 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000539 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000540
John McCall8823c652010-08-13 08:35:10 +0000541 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000542 // If the type of the function uses a type with unique-external
543 // linkage, it's not legally usable from outside this translation unit.
544 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
545 return LinkageInfo::uniqueExternal();
546
John McCall457a04e2010-10-22 21:05:15 +0000547 // If this is a method template specialization, use the linkage for
548 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000549 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000550 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000551 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
552 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
553 OnlyTemplate);
554 TemplateParameterList *TemplateParams =
555 spec->getTemplate()->getTemplateParameters();
556 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola340941d2012-05-25 16:41:35 +0000557 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000558 LV.mergeWithMin(ArgsLV);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000559 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000560 LV.mergeWithMin(ParamsLV);
Rafael Espindola67a498c2012-05-25 17:22:33 +0000561 } else {
562 LV.mergeLinkage(ArgsLV);
563 if (!OnlyTemplate)
564 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000565 }
John McCalle6e622e2010-11-01 01:29:57 +0000566 }
John McCall457a04e2010-10-22 21:05:15 +0000567
John McCall37bb6c92010-10-29 22:22:43 +0000568 // Note that in contrast to basically every other situation, we
569 // *do* apply -fvisibility to method declarations.
570
571 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000572 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000573 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000574 // Merge template argument/parameter information for member
575 // class template specializations.
576 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
577 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
578 OnlyTemplate);
579 TemplateParameterList *TemplateParams =
580 spec->getSpecializedTemplate()->getTemplateParameters();
581 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000582 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000583 LV.mergeWithMin(ArgsLV);
Rafael Espindola4d71d0f2012-05-25 14:17:45 +0000584 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000585 LV.mergeWithMin(ParamsLV);
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000586 } else {
587 LV.mergeLinkage(ArgsLV);
588 if (!OnlyTemplate)
589 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000590 }
John McCall37bb6c92010-10-29 22:22:43 +0000591 }
592
John McCall37bb6c92010-10-29 22:22:43 +0000593 // Static data members.
594 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000595 // Modify the variable's linkage by its type, but ignore the
596 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000597 LinkageInfo TypeLV = getLVForType(VD->getType());
598 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000599 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000600 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000601 }
602
John McCall457a04e2010-10-22 21:05:15 +0000603 return LV;
John McCall8823c652010-08-13 08:35:10 +0000604}
605
John McCalld396b972011-02-08 19:01:05 +0000606static void clearLinkageForClass(const CXXRecordDecl *record) {
607 for (CXXRecordDecl::decl_iterator
608 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
609 Decl *child = *i;
610 if (isa<NamedDecl>(child))
611 cast<NamedDecl>(child)->ClearLinkageCache();
612 }
613}
614
David Blaikie68e081d2011-12-20 02:48:34 +0000615void NamedDecl::anchor() { }
616
John McCalld396b972011-02-08 19:01:05 +0000617void NamedDecl::ClearLinkageCache() {
618 // Note that we can't skip clearing the linkage of children just
619 // because the parent doesn't have cached linkage: we don't cache
620 // when computing linkage for parent contexts.
621
622 HasCachedLinkage = 0;
623
624 // If we're changing the linkage of a class, we need to reset the
625 // linkage of child declarations, too.
626 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
627 clearLinkageForClass(record);
628
John McCall83779672011-02-19 02:53:41 +0000629 if (ClassTemplateDecl *temp =
630 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000631 // Clear linkage for the template pattern.
632 CXXRecordDecl *record = temp->getTemplatedDecl();
633 record->HasCachedLinkage = 0;
634 clearLinkageForClass(record);
635
John McCall83779672011-02-19 02:53:41 +0000636 // We need to clear linkage for specializations, too.
637 for (ClassTemplateDecl::spec_iterator
638 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
639 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000640 }
John McCall83779672011-02-19 02:53:41 +0000641
642 // Clear cached linkage for function template decls, too.
643 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000644 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
645 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000646 for (FunctionTemplateDecl::spec_iterator
647 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
648 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000649 }
John McCall83779672011-02-19 02:53:41 +0000650
John McCalld396b972011-02-08 19:01:05 +0000651}
652
Douglas Gregorbf62d642010-12-06 18:36:25 +0000653Linkage NamedDecl::getLinkage() const {
654 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000655 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000656 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000657 return Linkage(CachedLinkage);
658 }
659
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000660 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000661 HasCachedLinkage = 1;
662 return Linkage(CachedLinkage);
663}
664
John McCallc273f242010-10-30 11:50:40 +0000665LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000666 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000667 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000668 HasCachedLinkage = 1;
669 CachedLinkage = LI.linkage();
670 return LI;
John McCall033caa52010-10-29 00:29:13 +0000671}
Ted Kremenek926d8602010-04-20 23:15:35 +0000672
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000673llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
674 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000675 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
676 if (llvm::Optional<Visibility> V =
677 getVisibilityOf(Var->getMostRecentDecl()))
678 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000679
Rafael Espindola96e68242012-05-16 02:10:38 +0000680 if (Var->isStaticDataMember()) {
681 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
682 if (InstantiatedFrom)
683 return getVisibilityOf(InstantiatedFrom);
684 }
685
686 return llvm::Optional<Visibility>();
687 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000688 // Use the most recent declaration of a function, and also handle
689 // function template specializations.
690 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
691 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000692 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000693 return V;
694
695 // If the function is a specialization of a template with an
696 // explicit visibility attribute, use that.
697 if (FunctionTemplateSpecializationInfo *templateInfo
698 = fn->getTemplateSpecializationInfo())
699 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
700
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000701 // If the function is a member of a specialization of a class template
702 // and the corresponding decl has explicit visibility, use that.
703 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
704 if (InstantiatedFrom)
705 return getVisibilityOf(InstantiatedFrom);
706
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000707 return llvm::Optional<Visibility>();
708 }
709
710 // Otherwise, just check the declaration itself first.
711 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
712 return V;
713
714 // If there wasn't explicit visibility there, and this is a
715 // specialization of a class template, check for visibility
716 // on the pattern.
717 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolaeca5cd22012-07-13 01:19:08 +0000718 = dyn_cast<ClassTemplateSpecializationDecl>(this))
719 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000720
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000721 // If this is a member class of a specialization of a class template
722 // and the corresponding decl has explicit visibility, use that.
723 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
724 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
725 if (InstantiatedFrom)
726 return getVisibilityOf(InstantiatedFrom);
727 }
728
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000729 return llvm::Optional<Visibility>();
730}
731
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000732static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000733 // Objective-C: treat all Objective-C declarations as having external
734 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000735 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000736 default:
737 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000738 case Decl::ParmVar:
739 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000740 case Decl::TemplateTemplateParm: // count these as external
741 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000742 case Decl::ObjCAtDefsField:
743 case Decl::ObjCCategory:
744 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000745 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000746 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000747 case Decl::ObjCMethod:
748 case Decl::ObjCProperty:
749 case Decl::ObjCPropertyImpl:
750 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000751 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000752
753 case Decl::CXXRecord: {
754 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
755 if (Record->isLambda()) {
756 if (!Record->getLambdaManglingNumber()) {
757 // This lambda has no mangling number, so it's internal.
758 return LinkageInfo::internal();
759 }
760
761 // This lambda has its linkage/visibility determined by its owner.
762 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
763 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
764 if (isa<ParmVarDecl>(ContextDecl))
765 DC = ContextDecl->getDeclContext()->getRedeclContext();
766 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000767 return getLVForDecl(cast<NamedDecl>(ContextDecl),
768 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000769 }
770
771 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000772 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000773
774 return LinkageInfo::external();
775 }
776
777 break;
778 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000779 }
780
Douglas Gregorf73b2822009-11-25 22:24:25 +0000781 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000782 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000783 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000784
785 // C++ [basic.link]p5:
786 // In addition, a member function, static data member, a named
787 // class or enumeration of class scope, or an unnamed class or
788 // enumeration defined in a class-scope typedef declaration such
789 // that the class or enumeration has the typedef name for linkage
790 // purposes (7.1.3), has external linkage if the name of the class
791 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000792 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000793 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000794
795 // C++ [basic.link]p6:
796 // The name of a function declared in block scope and the name of
797 // an object declared by a block scope extern declaration have
798 // linkage. If there is a visible declaration of an entity with
799 // linkage having the same name and type, ignoring entities
800 // declared outside the innermost enclosing namespace scope, the
801 // block scope declaration declares that same entity and receives
802 // the linkage of the previous declaration. If there is more than
803 // one such matching entity, the program is ill-formed. Otherwise,
804 // if no matching entity is found, the block scope entity receives
805 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000806 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
807 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000808 if (Function->isInAnonymousNamespace() &&
809 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000810 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000811
John McCallc273f242010-10-30 11:50:40 +0000812 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000813 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000814 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000815 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000816 }
817
Douglas Gregorec9fd132012-01-14 16:38:05 +0000818 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000819 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000820 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
821 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000822 }
823
824 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000825 }
826
John McCall033caa52010-10-29 00:29:13 +0000827 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000828 if (Var->getStorageClass() == SC_Extern ||
829 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000830 if (Var->isInAnonymousNamespace() &&
831 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000832 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000833
John McCallc273f242010-10-30 11:50:40 +0000834 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000835 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000836 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000837 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000838 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000839 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000840 }
841
Douglas Gregorec9fd132012-01-14 16:38:05 +0000842 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000843 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000844 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
845 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000846 }
847
848 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000849 }
850 }
851
852 // C++ [basic.link]p6:
853 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000854 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000855}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000856
Douglas Gregor2ada0482009-02-04 17:27:36 +0000857std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000858 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000859}
860
861std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000862 const DeclContext *Ctx = getDeclContext();
863
864 if (Ctx->isFunctionOrMethod())
865 return getNameAsString();
866
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000867 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000868 ContextsTy Contexts;
869
870 // Collect contexts.
871 while (Ctx && isa<NamedDecl>(Ctx)) {
872 Contexts.push_back(Ctx);
873 Ctx = Ctx->getParent();
874 };
875
876 std::string QualName;
877 llvm::raw_string_ostream OS(QualName);
878
879 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
880 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000881 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000882 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000883 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
884 std::string TemplateArgsStr
885 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000886 TemplateArgs.data(),
887 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000888 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000889 OS << Spec->getName() << TemplateArgsStr;
890 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000891 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000892 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000893 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000894 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000895 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
896 if (!RD->getIdentifier())
897 OS << "<anonymous " << RD->getKindName() << '>';
898 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000899 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000900 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000901 const FunctionProtoType *FT = 0;
902 if (FD->hasWrittenPrototype())
903 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
904
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000905 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000906 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000907 unsigned NumParams = FD->getNumParams();
908 for (unsigned i = 0; i < NumParams; ++i) {
909 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000910 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000911 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000912 }
913
914 if (FT->isVariadic()) {
915 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000916 OS << ", ";
917 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000918 }
919 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000920 OS << ')';
921 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000922 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000923 }
924 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000925 }
926
John McCalla2a3f7d2010-03-16 21:48:18 +0000927 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000928 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000929 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000930 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000931
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000932 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000933}
934
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000935bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000936 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
937
Douglas Gregor889ceb72009-02-03 19:21:40 +0000938 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
939 // We want to keep it, unless it nominates same namespace.
940 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000941 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
942 ->getOriginalNamespace() ==
943 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
944 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000945 }
Mike Stump11289f42009-09-09 15:08:12 +0000946
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000947 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
948 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000949 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000950
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000951 // For function templates, the underlying function declarations are linked.
952 if (const FunctionTemplateDecl *FunctionTemplate
953 = dyn_cast<FunctionTemplateDecl>(this))
954 if (const FunctionTemplateDecl *OldFunctionTemplate
955 = dyn_cast<FunctionTemplateDecl>(OldD))
956 return FunctionTemplate->getTemplatedDecl()
957 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000958
Steve Naroffc4173fa2009-02-22 19:35:57 +0000959 // For method declarations, we keep track of redeclarations.
960 if (isa<ObjCMethodDecl>(this))
961 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000962
John McCall9f3059a2009-10-09 21:13:30 +0000963 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
964 return true;
965
John McCall3f746822009-11-17 05:59:44 +0000966 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
967 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
968 cast<UsingShadowDecl>(OldD)->getTargetDecl();
969
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000970 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
971 ASTContext &Context = getASTContext();
972 return Context.getCanonicalNestedNameSpecifier(
973 cast<UsingDecl>(this)->getQualifier()) ==
974 Context.getCanonicalNestedNameSpecifier(
975 cast<UsingDecl>(OldD)->getQualifier());
976 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000977
Douglas Gregorb59643b2012-01-03 23:26:26 +0000978 // A typedef of an Objective-C class type can replace an Objective-C class
979 // declaration or definition, and vice versa.
980 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
981 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
982 return true;
983
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000984 // For non-function declarations, if the declarations are of the
985 // same kind then this must be a redeclaration, or semantic analysis
986 // would not have given us the new declaration.
987 return this->getKind() == OldD->getKind();
988}
989
Douglas Gregoreddf4332009-02-24 20:03:32 +0000990bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000991 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000992}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000993
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000994NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000995 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000996 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
997 ND = UD->getTargetDecl();
998
999 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1000 return AD->getClassInterface();
1001
1002 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001003}
1004
John McCalla8ae2222010-04-06 21:38:20 +00001005bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001006 if (!isCXXClassMember())
1007 return false;
1008
John McCalla8ae2222010-04-06 21:38:20 +00001009 const NamedDecl *D = this;
1010 if (isa<UsingShadowDecl>(D))
1011 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1012
Francois Pichet783dd6e2010-11-21 06:08:52 +00001013 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001014 return true;
1015 if (isa<CXXMethodDecl>(D))
1016 return cast<CXXMethodDecl>(D)->isInstance();
1017 if (isa<FunctionTemplateDecl>(D))
1018 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1019 ->getTemplatedDecl())->isInstance();
1020 return false;
1021}
1022
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001023//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001024// DeclaratorDecl Implementation
1025//===----------------------------------------------------------------------===//
1026
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001027template <typename DeclT>
1028static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1029 if (decl->getNumTemplateParameterLists() > 0)
1030 return decl->getTemplateParameterList(0)->getTemplateLoc();
1031 else
1032 return decl->getInnerLocStart();
1033}
1034
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001035SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001036 TypeSourceInfo *TSI = getTypeSourceInfo();
1037 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001038 return SourceLocation();
1039}
1040
Douglas Gregor14454802011-02-25 02:25:35 +00001041void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1042 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001043 // Make sure the extended decl info is allocated.
1044 if (!hasExtInfo()) {
1045 // Save (non-extended) type source info pointer.
1046 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1047 // Allocate external info struct.
1048 DeclInfo = new (getASTContext()) ExtInfo;
1049 // Restore savedTInfo into (extended) decl info.
1050 getExtInfo()->TInfo = savedTInfo;
1051 }
1052 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001053 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001054 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001055 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001056 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001057 if (getExtInfo()->NumTemplParamLists == 0) {
1058 // Save type source info pointer.
1059 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1060 // Deallocate the extended decl info.
1061 getASTContext().Deallocate(getExtInfo());
1062 // Restore savedTInfo into (non-extended) decl info.
1063 DeclInfo = savedTInfo;
1064 }
1065 else
1066 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001067 }
1068 }
1069}
1070
Abramo Bagnara60804e12011-03-18 15:16:37 +00001071void
1072DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1073 unsigned NumTPLists,
1074 TemplateParameterList **TPLists) {
1075 assert(NumTPLists > 0);
1076 // Make sure the extended decl info is allocated.
1077 if (!hasExtInfo()) {
1078 // Save (non-extended) type source info pointer.
1079 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1080 // Allocate external info struct.
1081 DeclInfo = new (getASTContext()) ExtInfo;
1082 // Restore savedTInfo into (extended) decl info.
1083 getExtInfo()->TInfo = savedTInfo;
1084 }
1085 // Set the template parameter lists info.
1086 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1087}
1088
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001089SourceLocation DeclaratorDecl::getOuterLocStart() const {
1090 return getTemplateOrInnerLocStart(this);
1091}
1092
Abramo Bagnaraea947882011-03-08 16:41:52 +00001093namespace {
1094
1095// Helper function: returns true if QT is or contains a type
1096// having a postfix component.
1097bool typeIsPostfix(clang::QualType QT) {
1098 while (true) {
1099 const Type* T = QT.getTypePtr();
1100 switch (T->getTypeClass()) {
1101 default:
1102 return false;
1103 case Type::Pointer:
1104 QT = cast<PointerType>(T)->getPointeeType();
1105 break;
1106 case Type::BlockPointer:
1107 QT = cast<BlockPointerType>(T)->getPointeeType();
1108 break;
1109 case Type::MemberPointer:
1110 QT = cast<MemberPointerType>(T)->getPointeeType();
1111 break;
1112 case Type::LValueReference:
1113 case Type::RValueReference:
1114 QT = cast<ReferenceType>(T)->getPointeeType();
1115 break;
1116 case Type::PackExpansion:
1117 QT = cast<PackExpansionType>(T)->getPattern();
1118 break;
1119 case Type::Paren:
1120 case Type::ConstantArray:
1121 case Type::DependentSizedArray:
1122 case Type::IncompleteArray:
1123 case Type::VariableArray:
1124 case Type::FunctionProto:
1125 case Type::FunctionNoProto:
1126 return true;
1127 }
1128 }
1129}
1130
1131} // namespace
1132
1133SourceRange DeclaratorDecl::getSourceRange() const {
1134 SourceLocation RangeEnd = getLocation();
1135 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1136 if (typeIsPostfix(TInfo->getType()))
1137 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1138 }
1139 return SourceRange(getOuterLocStart(), RangeEnd);
1140}
1141
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001142void
Douglas Gregor20527e22010-06-15 17:44:38 +00001143QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1144 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001145 TemplateParameterList **TPLists) {
1146 assert((NumTPLists == 0 || TPLists != 0) &&
1147 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001148
1149 // Free previous template parameters (if any).
1150 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001151 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001152 TemplParamLists = 0;
1153 NumTemplParamLists = 0;
1154 }
1155 // Set info on matched template parameter lists (if any).
1156 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001157 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001158 NumTemplParamLists = NumTPLists;
1159 for (unsigned i = NumTPLists; i-- > 0; )
1160 TemplParamLists[i] = TPLists[i];
1161 }
1162}
1163
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001164//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001165// VarDecl Implementation
1166//===----------------------------------------------------------------------===//
1167
Sebastian Redl833ef452010-01-26 22:01:41 +00001168const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1169 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001170 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001171 case SC_Auto: return "auto";
1172 case SC_Extern: return "extern";
1173 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1174 case SC_PrivateExtern: return "__private_extern__";
1175 case SC_Register: return "register";
1176 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001177 }
1178
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001179 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001180}
1181
Abramo Bagnaradff19302011-03-08 08:55:46 +00001182VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1183 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001184 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001185 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001186 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001187}
1188
Douglas Gregor72172e92012-01-05 21:55:30 +00001189VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1190 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1191 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1192 QualType(), 0, SC_None, SC_None);
1193}
1194
Douglas Gregorbf62d642010-12-06 18:36:25 +00001195void VarDecl::setStorageClass(StorageClass SC) {
1196 assert(isLegalForVariable(SC));
1197 if (getStorageClass() != SC)
1198 ClearLinkageCache();
1199
John McCallbeaa11c2011-05-01 02:13:58 +00001200 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001201}
1202
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001203SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001204 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001205 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001206 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001207}
1208
Sebastian Redl833ef452010-01-26 22:01:41 +00001209bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001210 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001211 return false;
1212
Eli Friedman839192f2012-01-15 01:23:58 +00001213 const DeclContext *DC = getDeclContext();
1214 if (DC->isRecord())
1215 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001216
Eli Friedman839192f2012-01-15 01:23:58 +00001217 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001218 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001219 return true;
1220 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001221}
1222
1223VarDecl *VarDecl::getCanonicalDecl() {
1224 return getFirstDeclaration();
1225}
1226
Daniel Dunbar9d355812012-03-09 01:51:51 +00001227VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1228 ASTContext &C) const
1229{
Sebastian Redl35351a92010-01-31 22:27:38 +00001230 // C++ [basic.def]p2:
1231 // A declaration is a definition unless [...] it contains the 'extern'
1232 // specifier or a linkage-specification and neither an initializer [...],
1233 // it declares a static data member in a class declaration [...].
1234 // C++ [temp.expl.spec]p15:
1235 // An explicit specialization of a static data member of a template is a
1236 // definition if the declaration includes an initializer; otherwise, it is
1237 // a declaration.
1238 if (isStaticDataMember()) {
1239 if (isOutOfLine() && (hasInit() ||
1240 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1241 return Definition;
1242 else
1243 return DeclarationOnly;
1244 }
1245 // C99 6.7p5:
1246 // A definition of an identifier is a declaration for that identifier that
1247 // [...] causes storage to be reserved for that object.
1248 // Note: that applies for all non-file-scope objects.
1249 // C99 6.9.2p1:
1250 // If the declaration of an identifier for an object has file scope and an
1251 // initializer, the declaration is an external definition for the identifier
1252 if (hasInit())
1253 return Definition;
1254 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1255 if (hasExternalStorage())
1256 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001257
John McCall8e7d6562010-08-26 03:08:43 +00001258 if (getStorageClassAsWritten() == SC_Extern ||
1259 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001260 for (const VarDecl *PrevVar = getPreviousDecl();
1261 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001262 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1263 return DeclarationOnly;
1264 }
1265 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001266 // C99 6.9.2p2:
1267 // A declaration of an object that has file scope without an initializer,
1268 // and without a storage class specifier or the scs 'static', constitutes
1269 // a tentative definition.
1270 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001271 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001272 return TentativeDefinition;
1273
1274 // What's left is (in C, block-scope) declarations without initializers or
1275 // external storage. These are definitions.
1276 return Definition;
1277}
1278
Sebastian Redl35351a92010-01-31 22:27:38 +00001279VarDecl *VarDecl::getActingDefinition() {
1280 DefinitionKind Kind = isThisDeclarationADefinition();
1281 if (Kind != TentativeDefinition)
1282 return 0;
1283
Chris Lattner48eb14d2010-06-14 18:31:46 +00001284 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001285 VarDecl *First = getFirstDeclaration();
1286 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1287 I != E; ++I) {
1288 Kind = (*I)->isThisDeclarationADefinition();
1289 if (Kind == Definition)
1290 return 0;
1291 else if (Kind == TentativeDefinition)
1292 LastTentative = *I;
1293 }
1294 return LastTentative;
1295}
1296
1297bool VarDecl::isTentativeDefinitionNow() const {
1298 DefinitionKind Kind = isThisDeclarationADefinition();
1299 if (Kind != TentativeDefinition)
1300 return false;
1301
1302 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1303 if ((*I)->isThisDeclarationADefinition() == Definition)
1304 return false;
1305 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001306 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001307}
1308
Daniel Dunbar9d355812012-03-09 01:51:51 +00001309VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001310 VarDecl *First = getFirstDeclaration();
1311 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1312 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001313 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001314 return *I;
1315 }
1316 return 0;
1317}
1318
Daniel Dunbar9d355812012-03-09 01:51:51 +00001319VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001320 DefinitionKind Kind = DeclarationOnly;
1321
1322 const VarDecl *First = getFirstDeclaration();
1323 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001324 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001325 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001326 if (Kind == Definition)
1327 break;
1328 }
John McCall37bb6c92010-10-29 22:22:43 +00001329
1330 return Kind;
1331}
1332
Sebastian Redl5ca79842010-02-01 20:16:42 +00001333const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001334 redecl_iterator I = redecls_begin(), E = redecls_end();
1335 while (I != E && !I->getInit())
1336 ++I;
1337
1338 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001339 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001340 return I->getInit();
1341 }
1342 return 0;
1343}
1344
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001345bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001346 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001347 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001348
1349 if (!isStaticDataMember())
1350 return false;
1351
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001352 // If this static data member was instantiated from a static data member of
1353 // a class template, check whether that static data member was defined
1354 // out-of-line.
1355 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1356 return VD->isOutOfLine();
1357
1358 return false;
1359}
1360
Douglas Gregor1d957a32009-10-27 18:42:08 +00001361VarDecl *VarDecl::getOutOfLineDefinition() {
1362 if (!isStaticDataMember())
1363 return 0;
1364
1365 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1366 RD != RDEnd; ++RD) {
1367 if (RD->getLexicalDeclContext()->isFileContext())
1368 return *RD;
1369 }
1370
1371 return 0;
1372}
1373
Douglas Gregord5058122010-02-11 01:19:42 +00001374void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001375 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1376 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001377 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001378 }
1379
1380 Init = I;
1381}
1382
Daniel Dunbar9d355812012-03-09 01:51:51 +00001383bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001384 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001385
Richard Smith35ecb362012-03-02 04:14:40 +00001386 if (!Lang.CPlusPlus)
1387 return false;
1388
1389 // In C++11, any variable of reference type can be used in a constant
1390 // expression if it is initialized by a constant expression.
1391 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1392 return true;
1393
1394 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001395 // not require the variable to be non-volatile, but we consider this to be a
1396 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001397 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001398 return false;
1399
1400 // In C++, const, non-volatile variables of integral or enumeration types
1401 // can be used in constant expressions.
1402 if (getType()->isIntegralOrEnumerationType())
1403 return true;
1404
Richard Smith35ecb362012-03-02 04:14:40 +00001405 // Additionally, in C++11, non-volatile constexpr variables can be used in
1406 // constant expressions.
1407 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001408}
1409
Richard Smithd0b4dd62011-12-19 06:19:21 +00001410/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1411/// form, which contains extra information on the evaluated value of the
1412/// initializer.
1413EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1414 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1415 if (!Eval) {
1416 Stmt *S = Init.get<Stmt *>();
1417 Eval = new (getASTContext()) EvaluatedStmt;
1418 Eval->Value = S;
1419 Init = Eval;
1420 }
1421 return Eval;
1422}
1423
Richard Smithdafff942012-01-14 04:30:29 +00001424APValue *VarDecl::evaluateValue() const {
1425 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1426 return evaluateValue(Notes);
1427}
1428
1429APValue *VarDecl::evaluateValue(
1430 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001431 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1432
1433 // We only produce notes indicating why an initializer is non-constant the
1434 // first time it is evaluated. FIXME: The notes won't always be emitted the
1435 // first time we try evaluation, so might not be produced at all.
1436 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001437 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001438
1439 const Expr *Init = cast<Expr>(Eval->Value);
1440 assert(!Init->isValueDependent());
1441
1442 if (Eval->IsEvaluating) {
1443 // FIXME: Produce a diagnostic for self-initialization.
1444 Eval->CheckedICE = true;
1445 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001446 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001447 }
1448
1449 Eval->IsEvaluating = true;
1450
1451 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1452 this, Notes);
1453
1454 // Ensure the result is an uninitialized APValue if evaluation fails.
1455 if (!Result)
1456 Eval->Evaluated = APValue();
1457
1458 Eval->IsEvaluating = false;
1459 Eval->WasEvaluated = true;
1460
1461 // In C++11, we have determined whether the initializer was a constant
1462 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001463 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001464 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001465 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001466 }
1467
Richard Smithdafff942012-01-14 04:30:29 +00001468 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001469}
1470
1471bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001472 // Initializers of weak variables are never ICEs.
1473 if (isWeak())
1474 return false;
1475
Richard Smithd0b4dd62011-12-19 06:19:21 +00001476 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1477 if (Eval->CheckedICE)
1478 // We have already checked whether this subexpression is an
1479 // integral constant expression.
1480 return Eval->IsICE;
1481
1482 const Expr *Init = cast<Expr>(Eval->Value);
1483 assert(!Init->isValueDependent());
1484
1485 // In C++11, evaluate the initializer to check whether it's a constant
1486 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001487 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001488 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1489 evaluateValue(Notes);
1490 return Eval->IsICE;
1491 }
1492
1493 // It's an ICE whether or not the definition we found is
1494 // out-of-line. See DR 721 and the discussion in Clang PR
1495 // 6206 for details.
1496
1497 if (Eval->CheckingICE)
1498 return false;
1499 Eval->CheckingICE = true;
1500
1501 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1502 Eval->CheckingICE = false;
1503 Eval->CheckedICE = true;
1504 return Eval->IsICE;
1505}
1506
Douglas Gregorfe314812011-06-21 17:03:29 +00001507bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001508 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001509
1510 const Expr *E = getInit();
1511 if (!E)
1512 return false;
1513
1514 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1515 E = Cleanups->getSubExpr();
1516
1517 return isa<MaterializeTemporaryExpr>(E);
1518}
1519
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001520VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001521 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001522 return cast<VarDecl>(MSI->getInstantiatedFrom());
1523
1524 return 0;
1525}
1526
Douglas Gregor3c74d412009-10-14 20:14:33 +00001527TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001528 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001529 return MSI->getTemplateSpecializationKind();
1530
1531 return TSK_Undeclared;
1532}
1533
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001534MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001535 return getASTContext().getInstantiatedFromStaticDataMember(this);
1536}
1537
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001538void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1539 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001540 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001541 assert(MSI && "Not an instantiated static data member?");
1542 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001543 if (TSK != TSK_ExplicitSpecialization &&
1544 PointOfInstantiation.isValid() &&
1545 MSI->getPointOfInstantiation().isInvalid())
1546 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001547}
1548
Sebastian Redl833ef452010-01-26 22:01:41 +00001549//===----------------------------------------------------------------------===//
1550// ParmVarDecl Implementation
1551//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001552
Sebastian Redl833ef452010-01-26 22:01:41 +00001553ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001554 SourceLocation StartLoc,
1555 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001556 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001557 StorageClass S, StorageClass SCAsWritten,
1558 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001559 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001560 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001561}
1562
Douglas Gregor72172e92012-01-05 21:55:30 +00001563ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1564 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1565 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1566 0, QualType(), 0, SC_None, SC_None, 0);
1567}
1568
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001569SourceRange ParmVarDecl::getSourceRange() const {
1570 if (!hasInheritedDefaultArg()) {
1571 SourceRange ArgRange = getDefaultArgRange();
1572 if (ArgRange.isValid())
1573 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1574 }
1575
1576 return DeclaratorDecl::getSourceRange();
1577}
1578
Sebastian Redl833ef452010-01-26 22:01:41 +00001579Expr *ParmVarDecl::getDefaultArg() {
1580 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1581 assert(!hasUninstantiatedDefaultArg() &&
1582 "Default argument is not yet instantiated!");
1583
1584 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001585 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001586 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001587
Sebastian Redl833ef452010-01-26 22:01:41 +00001588 return Arg;
1589}
1590
Sebastian Redl833ef452010-01-26 22:01:41 +00001591SourceRange ParmVarDecl::getDefaultArgRange() const {
1592 if (const Expr *E = getInit())
1593 return E->getSourceRange();
1594
1595 if (hasUninstantiatedDefaultArg())
1596 return getUninstantiatedDefaultArg()->getSourceRange();
1597
1598 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001599}
1600
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001601bool ParmVarDecl::isParameterPack() const {
1602 return isa<PackExpansionType>(getType());
1603}
1604
Ted Kremenek540017e2011-10-06 05:00:56 +00001605void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1606 getASTContext().setParameterIndex(this, parameterIndex);
1607 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1608}
1609
1610unsigned ParmVarDecl::getParameterIndexLarge() const {
1611 return getASTContext().getParameterIndex(this);
1612}
1613
Nuno Lopes394ec982008-12-17 23:39:55 +00001614//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001615// FunctionDecl Implementation
1616//===----------------------------------------------------------------------===//
1617
Douglas Gregorb11aad82011-02-19 18:51:44 +00001618void FunctionDecl::getNameForDiagnostic(std::string &S,
1619 const PrintingPolicy &Policy,
1620 bool Qualified) const {
1621 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1622 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1623 if (TemplateArgs)
1624 S += TemplateSpecializationType::PrintTemplateArgumentList(
1625 TemplateArgs->data(),
1626 TemplateArgs->size(),
1627 Policy);
1628
1629}
1630
Ted Kremenek186a0742010-04-29 16:49:01 +00001631bool FunctionDecl::isVariadic() const {
1632 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1633 return FT->isVariadic();
1634 return false;
1635}
1636
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001637bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1638 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001639 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001640 Definition = *I;
1641 return true;
1642 }
1643 }
1644
1645 return false;
1646}
1647
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001648bool FunctionDecl::hasTrivialBody() const
1649{
1650 Stmt *S = getBody();
1651 if (!S) {
1652 // Since we don't have a body for this function, we don't know if it's
1653 // trivial or not.
1654 return false;
1655 }
1656
1657 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1658 return true;
1659 return false;
1660}
1661
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001662bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1663 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001664 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001665 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1666 return true;
1667 }
1668 }
1669
1670 return false;
1671}
1672
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001673Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001674 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1675 if (I->Body) {
1676 Definition = *I;
1677 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001678 } else if (I->IsLateTemplateParsed) {
1679 Definition = *I;
1680 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001681 }
1682 }
1683
1684 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001685}
1686
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001687void FunctionDecl::setBody(Stmt *B) {
1688 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001689 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001690 EndRangeLoc = B->getLocEnd();
1691}
1692
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001693void FunctionDecl::setPure(bool P) {
1694 IsPure = P;
1695 if (P)
1696 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1697 Parent->markedVirtualFunctionPure();
1698}
1699
Richard Smith12f247f2012-06-08 21:09:22 +00001700void FunctionDecl::setConstexpr(bool IC) {
1701 IsConstexpr = IC;
1702 CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(this);
1703 if (IC && CD)
1704 CD->getParent()->markedConstructorConstexpr(CD);
1705}
1706
Douglas Gregor16618f22009-09-12 00:17:51 +00001707bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001708 const TranslationUnitDecl *tunit =
1709 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1710 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001711 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001712 getIdentifier() &&
1713 getIdentifier()->isStr("main");
1714}
1715
1716bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1717 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1718 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1719 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1720 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1721 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1722
1723 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1724 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1725
1726 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1727 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1728
1729 ASTContext &Context =
1730 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1731 ->getASTContext();
1732
1733 // The result type and first argument type are constant across all
1734 // these operators. The second argument must be exactly void*.
1735 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001736}
1737
Douglas Gregor16618f22009-09-12 00:17:51 +00001738bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001739 if (getLinkage() != ExternalLinkage)
1740 return false;
1741
1742 if (getAttr<OverloadableAttr>())
1743 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001744
Chandler Carruth4322a282011-02-25 00:05:02 +00001745 const DeclContext *DC = getDeclContext();
1746 if (DC->isRecord())
1747 return false;
1748
Eli Friedman839192f2012-01-15 01:23:58 +00001749 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001750 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001751 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001752
Eli Friedman839192f2012-01-15 01:23:58 +00001753 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001754}
1755
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001756bool FunctionDecl::isGlobal() const {
1757 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1758 return Method->isStatic();
1759
John McCall8e7d6562010-08-26 03:08:43 +00001760 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001761 return false;
1762
Mike Stump11289f42009-09-09 15:08:12 +00001763 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001764 DC->isNamespace();
1765 DC = DC->getParent()) {
1766 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1767 if (!Namespace->getDeclName())
1768 return false;
1769 break;
1770 }
1771 }
1772
1773 return true;
1774}
1775
Sebastian Redl833ef452010-01-26 22:01:41 +00001776void
1777FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1778 redeclarable_base::setPreviousDeclaration(PrevDecl);
1779
1780 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1781 FunctionTemplateDecl *PrevFunTmpl
1782 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1783 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1784 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1785 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001786
Axel Naumannfbc7b982011-11-08 18:21:06 +00001787 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001788 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001789}
1790
1791const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1792 return getFirstDeclaration();
1793}
1794
1795FunctionDecl *FunctionDecl::getCanonicalDecl() {
1796 return getFirstDeclaration();
1797}
1798
Douglas Gregorbf62d642010-12-06 18:36:25 +00001799void FunctionDecl::setStorageClass(StorageClass SC) {
1800 assert(isLegalForFunction(SC));
1801 if (getStorageClass() != SC)
1802 ClearLinkageCache();
1803
1804 SClass = SC;
1805}
1806
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001807/// \brief Returns a value indicating whether this function
1808/// corresponds to a builtin function.
1809///
1810/// The function corresponds to a built-in function if it is
1811/// declared at translation scope or within an extern "C" block and
1812/// its name matches with the name of a builtin. The returned value
1813/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001814/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001815/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001816unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001817 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001818 return 0;
1819
1820 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001821 if (!BuiltinID)
1822 return 0;
1823
1824 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001825 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1826 return BuiltinID;
1827
1828 // This function has the name of a known C library
1829 // function. Determine whether it actually refers to the C library
1830 // function or whether it just has the same name.
1831
Douglas Gregora908e7f2009-02-17 03:23:10 +00001832 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001833 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001834 return 0;
1835
Douglas Gregore711f702009-02-14 18:57:46 +00001836 // If this function is at translation-unit scope and we're not in
1837 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001838 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001839 getDeclContext()->isTranslationUnit())
1840 return BuiltinID;
1841
1842 // If the function is in an extern "C" linkage specification and is
1843 // not marked "overloadable", it's the real function.
1844 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001845 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001846 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001847 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001848 return BuiltinID;
1849
1850 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001851 return 0;
1852}
1853
1854
Chris Lattner47c0d002009-04-25 06:03:53 +00001855/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001856/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001857/// after it has been created.
1858unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001859 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001860 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001861 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001862 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001863
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001864}
1865
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001866void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001867 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001868 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001869 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001870
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001871 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001872 if (!NewParamInfo.empty()) {
1873 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1874 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001875 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001876}
Chris Lattner41943152007-01-25 04:52:46 +00001877
James Molloy6f8780b2012-02-29 10:24:19 +00001878void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1879 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1880
1881 if (!NewDecls.empty()) {
1882 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1883 std::copy(NewDecls.begin(), NewDecls.end(), A);
1884 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1885 }
1886}
1887
Chris Lattner58258242008-04-10 02:22:51 +00001888/// getMinRequiredArguments - Returns the minimum number of arguments
1889/// needed to call this function. This may be fewer than the number of
1890/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001891/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001892unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001893 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001894 return getNumParams();
1895
Douglas Gregor7825bf32011-01-06 22:09:01 +00001896 unsigned NumRequiredArgs = getNumParams();
1897
1898 // If the last parameter is a parameter pack, we don't need an argument for
1899 // it.
1900 if (NumRequiredArgs > 0 &&
1901 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1902 --NumRequiredArgs;
1903
1904 // If this parameter has a default argument, we don't need an argument for
1905 // it.
1906 while (NumRequiredArgs > 0 &&
1907 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001908 --NumRequiredArgs;
1909
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001910 // We might have parameter packs before the end. These can't be deduced,
1911 // but they can still handle multiple arguments.
1912 unsigned ArgIdx = NumRequiredArgs;
1913 while (ArgIdx > 0) {
1914 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1915 NumRequiredArgs = ArgIdx;
1916
1917 --ArgIdx;
1918 }
1919
Chris Lattner58258242008-04-10 02:22:51 +00001920 return NumRequiredArgs;
1921}
1922
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001923bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001924 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001925 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001926
1927 if (isa<CXXMethodDecl>(this)) {
1928 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1929 return true;
1930 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001931
1932 switch (getTemplateSpecializationKind()) {
1933 case TSK_Undeclared:
1934 case TSK_ExplicitSpecialization:
1935 return false;
1936
1937 case TSK_ImplicitInstantiation:
1938 case TSK_ExplicitInstantiationDeclaration:
1939 case TSK_ExplicitInstantiationDefinition:
1940 // Handle below.
1941 break;
1942 }
1943
1944 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001945 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001946 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001947 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001948
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001949 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001950 return PatternDecl->isInlined();
1951
1952 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001953}
1954
Eli Friedman1b125c32012-02-07 03:50:18 +00001955static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1956 // Only consider file-scope declarations in this test.
1957 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1958 return false;
1959
1960 // Only consider explicit declarations; the presence of a builtin for a
1961 // libcall shouldn't affect whether a definition is externally visible.
1962 if (Redecl->isImplicit())
1963 return false;
1964
1965 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1966 return true; // Not an inline definition
1967
1968 return false;
1969}
1970
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001971/// \brief For a function declaration in C or C++, determine whether this
1972/// declaration causes the definition to be externally visible.
1973///
Eli Friedman1b125c32012-02-07 03:50:18 +00001974/// Specifically, this determines if adding the current declaration to the set
1975/// of redeclarations of the given functions causes
1976/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001977bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1978 assert(!doesThisDeclarationHaveABody() &&
1979 "Must have a declaration without a body.");
1980
1981 ASTContext &Context = getASTContext();
1982
David Blaikiebbafb8a2012-03-11 07:00:24 +00001983 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001984 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1985 // an externally visible definition.
1986 //
1987 // FIXME: What happens if gnu_inline gets added on after the first
1988 // declaration?
1989 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1990 return false;
1991
1992 const FunctionDecl *Prev = this;
1993 bool FoundBody = false;
1994 while ((Prev = Prev->getPreviousDecl())) {
1995 FoundBody |= Prev->Body;
1996
1997 if (Prev->Body) {
1998 // If it's not the case that both 'inline' and 'extern' are
1999 // specified on the definition, then it is always externally visible.
2000 if (!Prev->isInlineSpecified() ||
2001 Prev->getStorageClassAsWritten() != SC_Extern)
2002 return false;
2003 } else if (Prev->isInlineSpecified() &&
2004 Prev->getStorageClassAsWritten() != SC_Extern) {
2005 return false;
2006 }
2007 }
2008 return FoundBody;
2009 }
2010
David Blaikiebbafb8a2012-03-11 07:00:24 +00002011 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002012 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002013
2014 // C99 6.7.4p6:
2015 // [...] If all of the file scope declarations for a function in a
2016 // translation unit include the inline function specifier without extern,
2017 // then the definition in that translation unit is an inline definition.
2018 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002019 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002020 const FunctionDecl *Prev = this;
2021 bool FoundBody = false;
2022 while ((Prev = Prev->getPreviousDecl())) {
2023 FoundBody |= Prev->Body;
2024 if (RedeclForcesDefC99(Prev))
2025 return false;
2026 }
2027 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002028}
2029
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002030/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002031/// definition will be externally visible.
2032///
2033/// Inline function definitions are always available for inlining optimizations.
2034/// However, depending on the language dialect, declaration specifiers, and
2035/// attributes, the definition of an inline function may or may not be
2036/// "externally" visible to other translation units in the program.
2037///
2038/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002039/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002040/// inline definition becomes externally visible (C99 6.7.4p6).
2041///
2042/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2043/// definition, we use the GNU semantics for inline, which are nearly the
2044/// opposite of C99 semantics. In particular, "inline" by itself will create
2045/// an externally visible symbol, but "extern inline" will not create an
2046/// externally visible symbol.
2047bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002048 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002049 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002050 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002051
David Blaikiebbafb8a2012-03-11 07:00:24 +00002052 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002053 // Note: If you change the logic here, please change
2054 // doesDeclarationForceExternallyVisibleDefinition as well.
2055 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002056 // If it's not the case that both 'inline' and 'extern' are
2057 // specified on the definition, then this inline definition is
2058 // externally visible.
2059 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2060 return true;
2061
2062 // If any declaration is 'inline' but not 'extern', then this definition
2063 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002064 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2065 Redecl != RedeclEnd;
2066 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002067 if (Redecl->isInlineSpecified() &&
2068 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002069 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002070 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002071
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002072 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002073 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002074
Douglas Gregor299d76e2009-09-13 07:46:26 +00002075 // C99 6.7.4p6:
2076 // [...] If all of the file scope declarations for a function in a
2077 // translation unit include the inline function specifier without extern,
2078 // then the definition in that translation unit is an inline definition.
2079 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2080 Redecl != RedeclEnd;
2081 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002082 if (RedeclForcesDefC99(*Redecl))
2083 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002084 }
2085
2086 // C99 6.7.4p6:
2087 // An inline definition does not provide an external definition for the
2088 // function, and does not forbid an external definition in another
2089 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002090 return false;
2091}
2092
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002093/// getOverloadedOperator - Which C++ overloaded operator this
2094/// function represents, if any.
2095OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002096 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2097 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002098 else
2099 return OO_None;
2100}
2101
Alexis Huntc88db062010-01-13 09:01:02 +00002102/// getLiteralIdentifier - The literal suffix identifier this function
2103/// represents, if any.
2104const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2105 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2106 return getDeclName().getCXXLiteralIdentifier();
2107 else
2108 return 0;
2109}
2110
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002111FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2112 if (TemplateOrSpecialization.isNull())
2113 return TK_NonTemplate;
2114 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2115 return TK_FunctionTemplate;
2116 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2117 return TK_MemberSpecialization;
2118 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2119 return TK_FunctionTemplateSpecialization;
2120 if (TemplateOrSpecialization.is
2121 <DependentFunctionTemplateSpecializationInfo*>())
2122 return TK_DependentFunctionTemplateSpecialization;
2123
David Blaikie83d382b2011-09-23 05:06:16 +00002124 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002125}
2126
Douglas Gregord801b062009-10-07 23:56:10 +00002127FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002128 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002129 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2130
2131 return 0;
2132}
2133
Douglas Gregor06db9f52009-10-12 20:18:28 +00002134MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2135 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2136}
2137
Douglas Gregord801b062009-10-07 23:56:10 +00002138void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002139FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2140 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002141 TemplateSpecializationKind TSK) {
2142 assert(TemplateOrSpecialization.isNull() &&
2143 "Member function is already a specialization");
2144 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002145 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002146 TemplateOrSpecialization = Info;
2147}
2148
Douglas Gregorafca3b42009-10-27 20:53:28 +00002149bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002150 // If the function is invalid, it can't be implicitly instantiated.
2151 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002152 return false;
2153
2154 switch (getTemplateSpecializationKind()) {
2155 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002156 case TSK_ExplicitInstantiationDefinition:
2157 return false;
2158
2159 case TSK_ImplicitInstantiation:
2160 return true;
2161
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002162 // It is possible to instantiate TSK_ExplicitSpecialization kind
2163 // if the FunctionDecl has a class scope specialization pattern.
2164 case TSK_ExplicitSpecialization:
2165 return getClassScopeSpecializationPattern() != 0;
2166
Douglas Gregorafca3b42009-10-27 20:53:28 +00002167 case TSK_ExplicitInstantiationDeclaration:
2168 // Handled below.
2169 break;
2170 }
2171
2172 // Find the actual template from which we will instantiate.
2173 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002174 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002175 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002176 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002177
2178 // C++0x [temp.explicit]p9:
2179 // Except for inline functions, other explicit instantiation declarations
2180 // have the effect of suppressing the implicit instantiation of the entity
2181 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002182 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002183 return true;
2184
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002185 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002186}
2187
2188bool FunctionDecl::isTemplateInstantiation() const {
2189 switch (getTemplateSpecializationKind()) {
2190 case TSK_Undeclared:
2191 case TSK_ExplicitSpecialization:
2192 return false;
2193 case TSK_ImplicitInstantiation:
2194 case TSK_ExplicitInstantiationDeclaration:
2195 case TSK_ExplicitInstantiationDefinition:
2196 return true;
2197 }
2198 llvm_unreachable("All TSK values handled.");
2199}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002200
2201FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002202 // Handle class scope explicit specialization special case.
2203 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2204 return getClassScopeSpecializationPattern();
2205
Douglas Gregorafca3b42009-10-27 20:53:28 +00002206 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2207 while (Primary->getInstantiatedFromMemberTemplate()) {
2208 // If we have hit a point where the user provided a specialization of
2209 // this template, we're done looking.
2210 if (Primary->isMemberSpecialization())
2211 break;
2212
2213 Primary = Primary->getInstantiatedFromMemberTemplate();
2214 }
2215
2216 return Primary->getTemplatedDecl();
2217 }
2218
2219 return getInstantiatedFromMemberFunction();
2220}
2221
Douglas Gregor70d83e22009-06-29 17:30:29 +00002222FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002223 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002224 = TemplateOrSpecialization
2225 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002226 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002227 }
2228 return 0;
2229}
2230
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002231FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2232 return getASTContext().getClassScopeSpecializationPattern(this);
2233}
2234
Douglas Gregor70d83e22009-06-29 17:30:29 +00002235const TemplateArgumentList *
2236FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002237 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002238 = TemplateOrSpecialization
2239 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002240 return Info->TemplateArguments;
2241 }
2242 return 0;
2243}
2244
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002245const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002246FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2247 if (FunctionTemplateSpecializationInfo *Info
2248 = TemplateOrSpecialization
2249 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2250 return Info->TemplateArgumentsAsWritten;
2251 }
2252 return 0;
2253}
2254
Mike Stump11289f42009-09-09 15:08:12 +00002255void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002256FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2257 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002258 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002259 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002260 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002261 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2262 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002263 assert(TSK != TSK_Undeclared &&
2264 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002265 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002266 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002267 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002268 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2269 TemplateArgs,
2270 TemplateArgsAsWritten,
2271 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002272 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002273 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002274}
2275
John McCallb9c78482010-04-08 09:05:18 +00002276void
2277FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2278 const UnresolvedSetImpl &Templates,
2279 const TemplateArgumentListInfo &TemplateArgs) {
2280 assert(TemplateOrSpecialization.isNull());
2281 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2282 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002283 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002284 void *Buffer = Context.Allocate(Size);
2285 DependentFunctionTemplateSpecializationInfo *Info =
2286 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2287 TemplateArgs);
2288 TemplateOrSpecialization = Info;
2289}
2290
2291DependentFunctionTemplateSpecializationInfo::
2292DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2293 const TemplateArgumentListInfo &TArgs)
2294 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2295
2296 d.NumTemplates = Ts.size();
2297 d.NumArgs = TArgs.size();
2298
2299 FunctionTemplateDecl **TsArray =
2300 const_cast<FunctionTemplateDecl**>(getTemplates());
2301 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2302 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2303
2304 TemplateArgumentLoc *ArgsArray =
2305 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2306 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2307 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2308}
2309
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002310TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002311 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002312 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002313 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002314 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002315 if (FTSInfo)
2316 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002317
Douglas Gregord801b062009-10-07 23:56:10 +00002318 MemberSpecializationInfo *MSInfo
2319 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2320 if (MSInfo)
2321 return MSInfo->getTemplateSpecializationKind();
2322
2323 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002324}
2325
Mike Stump11289f42009-09-09 15:08:12 +00002326void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002327FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2328 SourceLocation PointOfInstantiation) {
2329 if (FunctionTemplateSpecializationInfo *FTSInfo
2330 = TemplateOrSpecialization.dyn_cast<
2331 FunctionTemplateSpecializationInfo*>()) {
2332 FTSInfo->setTemplateSpecializationKind(TSK);
2333 if (TSK != TSK_ExplicitSpecialization &&
2334 PointOfInstantiation.isValid() &&
2335 FTSInfo->getPointOfInstantiation().isInvalid())
2336 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2337 } else if (MemberSpecializationInfo *MSInfo
2338 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2339 MSInfo->setTemplateSpecializationKind(TSK);
2340 if (TSK != TSK_ExplicitSpecialization &&
2341 PointOfInstantiation.isValid() &&
2342 MSInfo->getPointOfInstantiation().isInvalid())
2343 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2344 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002345 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002346}
2347
2348SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002349 if (FunctionTemplateSpecializationInfo *FTSInfo
2350 = TemplateOrSpecialization.dyn_cast<
2351 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002352 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002353 else if (MemberSpecializationInfo *MSInfo
2354 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002355 return MSInfo->getPointOfInstantiation();
2356
2357 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002358}
2359
Douglas Gregor6411b922009-09-11 20:15:17 +00002360bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002361 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002362 return true;
2363
2364 // If this function was instantiated from a member function of a
2365 // class template, check whether that member function was defined out-of-line.
2366 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2367 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002368 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002369 return Definition->isOutOfLine();
2370 }
2371
2372 // If this function was instantiated from a function template,
2373 // check whether that function template was defined out-of-line.
2374 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2375 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002376 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002377 return Definition->isOutOfLine();
2378 }
2379
2380 return false;
2381}
2382
Abramo Bagnaraea947882011-03-08 16:41:52 +00002383SourceRange FunctionDecl::getSourceRange() const {
2384 return SourceRange(getOuterLocStart(), EndRangeLoc);
2385}
2386
Anna Zaks28db7ce2012-01-18 02:45:01 +00002387unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002388 IdentifierInfo *FnInfo = getIdentifier();
2389
2390 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002391 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002392
2393 // Builtin handling.
2394 switch (getBuiltinID()) {
2395 case Builtin::BI__builtin_memset:
2396 case Builtin::BI__builtin___memset_chk:
2397 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002398 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002399
2400 case Builtin::BI__builtin_memcpy:
2401 case Builtin::BI__builtin___memcpy_chk:
2402 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002403 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002404
2405 case Builtin::BI__builtin_memmove:
2406 case Builtin::BI__builtin___memmove_chk:
2407 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002408 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002409
2410 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002411 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002412 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002413 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002414
2415 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002416 case Builtin::BImemcmp:
2417 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002418
2419 case Builtin::BI__builtin_strncpy:
2420 case Builtin::BI__builtin___strncpy_chk:
2421 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002422 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002423
2424 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002425 case Builtin::BIstrncmp:
2426 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002427
2428 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002429 case Builtin::BIstrncasecmp:
2430 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002431
2432 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002433 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002434 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002435 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002436
2437 case Builtin::BI__builtin_strndup:
2438 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002439 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002440
Anna Zaks314cd092012-02-01 19:08:57 +00002441 case Builtin::BI__builtin_strlen:
2442 case Builtin::BIstrlen:
2443 return Builtin::BIstrlen;
2444
Anna Zaks201d4892012-01-13 21:52:01 +00002445 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002446 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002447 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002448 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002449 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002450 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002451 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002452 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002453 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002454 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002455 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002456 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002457 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002458 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002459 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002460 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002461 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002462 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002463 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002464 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002465 else if (FnInfo->isStr("strlen"))
2466 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002467 }
2468 break;
2469 }
Anna Zaks22122702012-01-17 00:37:07 +00002470 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002471}
2472
Chris Lattner59a25942008-03-31 00:36:02 +00002473//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002474// FieldDecl Implementation
2475//===----------------------------------------------------------------------===//
2476
Jay Foad39c79802011-01-12 09:06:06 +00002477FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002478 SourceLocation StartLoc, SourceLocation IdLoc,
2479 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002480 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002481 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002482 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002483 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002484}
2485
Douglas Gregor72172e92012-01-05 21:55:30 +00002486FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2487 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2488 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002489 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002490}
2491
Sebastian Redl833ef452010-01-26 22:01:41 +00002492bool FieldDecl::isAnonymousStructOrUnion() const {
2493 if (!isImplicit() || getDeclName())
2494 return false;
2495
2496 if (const RecordType *Record = getType()->getAs<RecordType>())
2497 return Record->getDecl()->isAnonymousStructOrUnion();
2498
2499 return false;
2500}
2501
Richard Smithcaf33902011-10-10 18:28:20 +00002502unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2503 assert(isBitField() && "not a bitfield");
2504 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2505 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2506}
2507
John McCall4e819612011-01-20 07:57:12 +00002508unsigned FieldDecl::getFieldIndex() const {
2509 if (CachedFieldIndex) return CachedFieldIndex - 1;
2510
Richard Smithd62306a2011-11-10 06:34:14 +00002511 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002512 const RecordDecl *RD = getParent();
2513 const FieldDecl *LastFD = 0;
2514 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002515
2516 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2517 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002518 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002519
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002520 if (IsMsStruct) {
2521 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002522 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002523 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002524 continue;
2525 }
David Blaikie40ed2972012-06-06 20:45:41 +00002526 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002527 }
John McCall4e819612011-01-20 07:57:12 +00002528 }
2529
Richard Smithd62306a2011-11-10 06:34:14 +00002530 assert(CachedFieldIndex && "failed to find field in parent");
2531 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002532}
2533
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002534SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002535 if (const Expr *E = InitializerOrBitWidth.getPointer())
2536 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002537 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002538}
2539
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002540void FieldDecl::setBitWidth(Expr *Width) {
2541 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2542 "bit width or initializer already set");
2543 InitializerOrBitWidth.setPointer(Width);
2544}
2545
Richard Smith938f40b2011-06-11 17:19:42 +00002546void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002547 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002548 "bit width or initializer already set");
2549 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002550}
2551
Sebastian Redl833ef452010-01-26 22:01:41 +00002552//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002553// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002554//===----------------------------------------------------------------------===//
2555
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002556SourceLocation TagDecl::getOuterLocStart() const {
2557 return getTemplateOrInnerLocStart(this);
2558}
2559
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002560SourceRange TagDecl::getSourceRange() const {
2561 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002562 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002563}
2564
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002565TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002566 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002567}
2568
Richard Smithdda56e42011-04-15 14:24:37 +00002569void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2570 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002571 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002572 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002573 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002574}
2575
Douglas Gregordee1be82009-01-17 00:42:38 +00002576void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002577 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002578
2579 if (isa<CXXRecordDecl>(this)) {
2580 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2581 struct CXXRecordDecl::DefinitionData *Data =
2582 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002583 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2584 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002585 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002586}
2587
2588void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002589 assert((!isa<CXXRecordDecl>(this) ||
2590 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2591 "definition completed but not started");
2592
John McCallf937c022011-10-07 06:10:15 +00002593 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002594 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002595
2596 if (ASTMutationListener *L = getASTMutationListener())
2597 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002598}
2599
John McCallf937c022011-10-07 06:10:15 +00002600TagDecl *TagDecl::getDefinition() const {
2601 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002602 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002603 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2604 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002605
2606 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002607 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002608 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002609 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002610
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002611 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002612}
2613
Douglas Gregor14454802011-02-25 02:25:35 +00002614void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2615 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002616 // Make sure the extended qualifier info is allocated.
2617 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002618 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002619 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002620 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002621 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002622 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002623 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002624 if (getExtInfo()->NumTemplParamLists == 0) {
2625 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002626 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002627 }
2628 else
2629 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002630 }
2631 }
2632}
2633
Abramo Bagnara60804e12011-03-18 15:16:37 +00002634void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2635 unsigned NumTPLists,
2636 TemplateParameterList **TPLists) {
2637 assert(NumTPLists > 0);
2638 // Make sure the extended decl info is allocated.
2639 if (!hasExtInfo())
2640 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002641 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002642 // Set the template parameter lists info.
2643 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2644}
2645
Ted Kremenek21475702008-09-05 17:16:31 +00002646//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002647// EnumDecl Implementation
2648//===----------------------------------------------------------------------===//
2649
David Blaikie68e081d2011-12-20 02:48:34 +00002650void EnumDecl::anchor() { }
2651
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002652EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2653 SourceLocation StartLoc, SourceLocation IdLoc,
2654 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002655 EnumDecl *PrevDecl, bool IsScoped,
2656 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002657 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002658 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002659 C.getTypeDeclType(Enum, PrevDecl);
2660 return Enum;
2661}
2662
Douglas Gregor72172e92012-01-05 21:55:30 +00002663EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2664 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2665 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2666 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002667}
2668
Douglas Gregord5058122010-02-11 01:19:42 +00002669void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002670 QualType NewPromotionType,
2671 unsigned NumPositiveBits,
2672 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002673 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002674 if (!IntegerType)
2675 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002676 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002677 setNumPositiveBits(NumPositiveBits);
2678 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002679 TagDecl::completeDefinition();
2680}
2681
Richard Smith7d137e32012-03-23 03:33:32 +00002682TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2683 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2684 return MSI->getTemplateSpecializationKind();
2685
2686 return TSK_Undeclared;
2687}
2688
2689void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2690 SourceLocation PointOfInstantiation) {
2691 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2692 assert(MSI && "Not an instantiated member enumeration?");
2693 MSI->setTemplateSpecializationKind(TSK);
2694 if (TSK != TSK_ExplicitSpecialization &&
2695 PointOfInstantiation.isValid() &&
2696 MSI->getPointOfInstantiation().isInvalid())
2697 MSI->setPointOfInstantiation(PointOfInstantiation);
2698}
2699
Richard Smith4b38ded2012-03-14 23:13:10 +00002700EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2701 if (SpecializationInfo)
2702 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2703
2704 return 0;
2705}
2706
2707void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2708 TemplateSpecializationKind TSK) {
2709 assert(!SpecializationInfo && "Member enum is already a specialization");
2710 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2711}
2712
Sebastian Redl833ef452010-01-26 22:01:41 +00002713//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002714// RecordDecl Implementation
2715//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002716
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002717RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2718 SourceLocation StartLoc, SourceLocation IdLoc,
2719 IdentifierInfo *Id, RecordDecl *PrevDecl)
2720 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002721 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002722 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002723 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002724 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002725 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002726}
2727
Jay Foad39c79802011-01-12 09:06:06 +00002728RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002729 SourceLocation StartLoc, SourceLocation IdLoc,
2730 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2731 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2732 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002733 C.getTypeDeclType(R, PrevDecl);
2734 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002735}
2736
Douglas Gregor72172e92012-01-05 21:55:30 +00002737RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2738 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2739 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2740 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002741}
2742
Douglas Gregordfcad112009-03-25 15:59:44 +00002743bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002744 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002745 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2746}
2747
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002748RecordDecl::field_iterator RecordDecl::field_begin() const {
2749 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2750 LoadFieldsFromExternalStorage();
2751
2752 return field_iterator(decl_iterator(FirstDecl));
2753}
2754
Douglas Gregorb11aad82011-02-19 18:51:44 +00002755/// completeDefinition - Notes that the definition of this type is now
2756/// complete.
2757void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002758 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002759 TagDecl::completeDefinition();
2760}
2761
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002762void RecordDecl::LoadFieldsFromExternalStorage() const {
2763 ExternalASTSource *Source = getASTContext().getExternalSource();
2764 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2765
2766 // Notify that we have a RecordDecl doing some initialization.
2767 ExternalASTSource::Deserializing TheFields(Source);
2768
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002769 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002770 LoadedFieldsFromExternalStorage = true;
2771 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2772 case ELR_Success:
2773 break;
2774
2775 case ELR_AlreadyLoaded:
2776 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002777 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002778 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002779
2780#ifndef NDEBUG
2781 // Check that all decls we got were FieldDecls.
2782 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2783 assert(isa<FieldDecl>(Decls[i]));
2784#endif
2785
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002786 if (Decls.empty())
2787 return;
2788
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002789 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2790 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002791}
2792
Steve Naroff415d3d52008-10-08 17:01:13 +00002793//===----------------------------------------------------------------------===//
2794// BlockDecl Implementation
2795//===----------------------------------------------------------------------===//
2796
David Blaikie9c70e042011-09-21 18:16:56 +00002797void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002798 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002799
Steve Naroffc4b30e52009-03-13 16:56:44 +00002800 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002801 if (!NewParamInfo.empty()) {
2802 NumParams = NewParamInfo.size();
2803 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2804 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002805 }
2806}
2807
John McCall351762c2011-02-07 10:33:21 +00002808void BlockDecl::setCaptures(ASTContext &Context,
2809 const Capture *begin,
2810 const Capture *end,
2811 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002812 CapturesCXXThis = capturesCXXThis;
2813
2814 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002815 NumCaptures = 0;
2816 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002817 return;
2818 }
2819
John McCall351762c2011-02-07 10:33:21 +00002820 NumCaptures = end - begin;
2821
2822 // Avoid new Capture[] because we don't want to provide a default
2823 // constructor.
2824 size_t allocationSize = NumCaptures * sizeof(Capture);
2825 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2826 memcpy(buffer, begin, allocationSize);
2827 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002828}
Sebastian Redl833ef452010-01-26 22:01:41 +00002829
John McCallce45f882011-06-15 22:51:16 +00002830bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2831 for (capture_const_iterator
2832 i = capture_begin(), e = capture_end(); i != e; ++i)
2833 // Only auto vars can be captured, so no redeclaration worries.
2834 if (i->getVariable() == variable)
2835 return true;
2836
2837 return false;
2838}
2839
Douglas Gregor70226da2010-12-21 16:27:07 +00002840SourceRange BlockDecl::getSourceRange() const {
2841 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2842}
Sebastian Redl833ef452010-01-26 22:01:41 +00002843
2844//===----------------------------------------------------------------------===//
2845// Other Decl Allocation/Deallocation Method Implementations
2846//===----------------------------------------------------------------------===//
2847
David Blaikie68e081d2011-12-20 02:48:34 +00002848void TranslationUnitDecl::anchor() { }
2849
Sebastian Redl833ef452010-01-26 22:01:41 +00002850TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2851 return new (C) TranslationUnitDecl(C);
2852}
2853
David Blaikie68e081d2011-12-20 02:48:34 +00002854void LabelDecl::anchor() { }
2855
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002856LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002857 SourceLocation IdentL, IdentifierInfo *II) {
2858 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2859}
2860
2861LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2862 SourceLocation IdentL, IdentifierInfo *II,
2863 SourceLocation GnuLabelL) {
2864 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2865 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002866}
2867
Douglas Gregor72172e92012-01-05 21:55:30 +00002868LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2869 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2870 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002871}
2872
David Blaikie68e081d2011-12-20 02:48:34 +00002873void ValueDecl::anchor() { }
2874
2875void ImplicitParamDecl::anchor() { }
2876
Sebastian Redl833ef452010-01-26 22:01:41 +00002877ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002878 SourceLocation IdLoc,
2879 IdentifierInfo *Id,
2880 QualType Type) {
2881 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002882}
2883
Douglas Gregor72172e92012-01-05 21:55:30 +00002884ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2885 unsigned ID) {
2886 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2887 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2888}
2889
Sebastian Redl833ef452010-01-26 22:01:41 +00002890FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002891 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002892 const DeclarationNameInfo &NameInfo,
2893 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002894 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002895 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002896 bool hasWrittenPrototype,
2897 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002898 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2899 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002900 isInlineSpecified,
2901 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002902 New->HasWrittenPrototype = hasWrittenPrototype;
2903 return New;
2904}
2905
Douglas Gregor72172e92012-01-05 21:55:30 +00002906FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2907 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2908 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2909 DeclarationNameInfo(), QualType(), 0,
2910 SC_None, SC_None, false, false);
2911}
2912
Sebastian Redl833ef452010-01-26 22:01:41 +00002913BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2914 return new (C) BlockDecl(DC, L);
2915}
2916
Douglas Gregor72172e92012-01-05 21:55:30 +00002917BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2918 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2919 return new (Mem) BlockDecl(0, SourceLocation());
2920}
2921
Sebastian Redl833ef452010-01-26 22:01:41 +00002922EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2923 SourceLocation L,
2924 IdentifierInfo *Id, QualType T,
2925 Expr *E, const llvm::APSInt &V) {
2926 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2927}
2928
Douglas Gregor72172e92012-01-05 21:55:30 +00002929EnumConstantDecl *
2930EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2931 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2932 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2933 llvm::APSInt());
2934}
2935
David Blaikie68e081d2011-12-20 02:48:34 +00002936void IndirectFieldDecl::anchor() { }
2937
Benjamin Kramer39593702010-11-21 14:11:41 +00002938IndirectFieldDecl *
2939IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2940 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2941 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002942 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2943}
2944
Douglas Gregor72172e92012-01-05 21:55:30 +00002945IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2946 unsigned ID) {
2947 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2948 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2949 QualType(), 0, 0);
2950}
2951
Douglas Gregorbe996932010-09-01 20:41:53 +00002952SourceRange EnumConstantDecl::getSourceRange() const {
2953 SourceLocation End = getLocation();
2954 if (Init)
2955 End = Init->getLocEnd();
2956 return SourceRange(getLocation(), End);
2957}
2958
David Blaikie68e081d2011-12-20 02:48:34 +00002959void TypeDecl::anchor() { }
2960
Sebastian Redl833ef452010-01-26 22:01:41 +00002961TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002962 SourceLocation StartLoc, SourceLocation IdLoc,
2963 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2964 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002965}
2966
David Blaikie68e081d2011-12-20 02:48:34 +00002967void TypedefNameDecl::anchor() { }
2968
Douglas Gregor72172e92012-01-05 21:55:30 +00002969TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2970 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2971 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2972}
2973
Richard Smithdda56e42011-04-15 14:24:37 +00002974TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2975 SourceLocation StartLoc,
2976 SourceLocation IdLoc, IdentifierInfo *Id,
2977 TypeSourceInfo *TInfo) {
2978 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2979}
2980
Douglas Gregor72172e92012-01-05 21:55:30 +00002981TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2982 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2983 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2984}
2985
Abramo Bagnaraea947882011-03-08 16:41:52 +00002986SourceRange TypedefDecl::getSourceRange() const {
2987 SourceLocation RangeEnd = getLocation();
2988 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2989 if (typeIsPostfix(TInfo->getType()))
2990 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2991 }
2992 return SourceRange(getLocStart(), RangeEnd);
2993}
2994
Richard Smithdda56e42011-04-15 14:24:37 +00002995SourceRange TypeAliasDecl::getSourceRange() const {
2996 SourceLocation RangeEnd = getLocStart();
2997 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2998 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2999 return SourceRange(getLocStart(), RangeEnd);
3000}
3001
David Blaikie68e081d2011-12-20 02:48:34 +00003002void FileScopeAsmDecl::anchor() { }
3003
Sebastian Redl833ef452010-01-26 22:01:41 +00003004FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003005 StringLiteral *Str,
3006 SourceLocation AsmLoc,
3007 SourceLocation RParenLoc) {
3008 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003009}
Douglas Gregorba345522011-12-02 23:23:56 +00003010
Douglas Gregor72172e92012-01-05 21:55:30 +00003011FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3012 unsigned ID) {
3013 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3014 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3015}
3016
Douglas Gregorba345522011-12-02 23:23:56 +00003017//===----------------------------------------------------------------------===//
3018// ImportDecl Implementation
3019//===----------------------------------------------------------------------===//
3020
3021/// \brief Retrieve the number of module identifiers needed to name the given
3022/// module.
3023static unsigned getNumModuleIdentifiers(Module *Mod) {
3024 unsigned Result = 1;
3025 while (Mod->Parent) {
3026 Mod = Mod->Parent;
3027 ++Result;
3028 }
3029 return Result;
3030}
3031
Douglas Gregor22d09742012-01-03 18:04:46 +00003032ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003033 Module *Imported,
3034 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003035 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003036 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003037{
3038 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3039 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3040 memcpy(StoredLocs, IdentifierLocs.data(),
3041 IdentifierLocs.size() * sizeof(SourceLocation));
3042}
3043
Douglas Gregor22d09742012-01-03 18:04:46 +00003044ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003045 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003046 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003047 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003048{
3049 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3050}
3051
3052ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003053 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003054 ArrayRef<SourceLocation> IdentifierLocs) {
3055 void *Mem = C.Allocate(sizeof(ImportDecl) +
3056 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003057 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003058}
3059
3060ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003061 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003062 Module *Imported,
3063 SourceLocation EndLoc) {
3064 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003065 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003066 Import->setImplicit();
3067 return Import;
3068}
3069
Douglas Gregor72172e92012-01-05 21:55:30 +00003070ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3071 unsigned NumLocations) {
3072 void *Mem = AllocateDeserializedDecl(C, ID,
3073 (sizeof(ImportDecl) +
3074 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003075 return new (Mem) ImportDecl(EmptyShell());
3076}
3077
3078ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3079 if (!ImportedAndComplete.getInt())
3080 return ArrayRef<SourceLocation>();
3081
3082 const SourceLocation *StoredLocs
3083 = reinterpret_cast<const SourceLocation *>(this + 1);
3084 return ArrayRef<SourceLocation>(StoredLocs,
3085 getNumModuleIdentifiers(getImportedModule()));
3086}
3087
3088SourceRange ImportDecl::getSourceRange() const {
3089 if (!ImportedAndComplete.getInt())
3090 return SourceRange(getLocation(),
3091 *reinterpret_cast<const SourceLocation *>(this + 1));
3092
3093 return SourceRange(getLocation(), getIdentifierLocs().back());
3094}