blob: 2f42607eab6cb30f36aeaca6f8d11cec257aa822 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000027#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000028#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000029#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000031
David Blaikie9c70e042011-09-21 18:16:56 +000032#include <algorithm>
33
Chris Lattner6d9a6852006-10-25 05:11:20 +000034using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000035
Chris Lattner88f70d62008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor1baf38f2011-03-26 12:10:19 +000040static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
41 // If this declaration has an explicit visibility attribute, use it.
42 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
43 switch (A->getVisibility()) {
44 case VisibilityAttr::Default:
45 return DefaultVisibility;
46 case VisibilityAttr::Hidden:
47 return HiddenVisibility;
48 case VisibilityAttr::Protected:
49 return ProtectedVisibility;
50 }
John McCall457a04e2010-10-22 21:05:15 +000051 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +000052
53 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
54 // implies visibility(default).
Douglas Gregore8bbc122011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +000056 for (specific_attr_iterator<AvailabilityAttr>
57 A = D->specific_attr_begin<AvailabilityAttr>(),
58 AEnd = D->specific_attr_end<AvailabilityAttr>();
59 A != AEnd; ++A)
60 if ((*A)->getPlatform()->getName().equals("macosx"))
61 return DefaultVisibility;
62 }
63
64 return llvm::Optional<Visibility>();
John McCall457a04e2010-10-22 21:05:15 +000065}
66
John McCallc273f242010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallc273f242010-10-30 11:50:40 +000068
Benjamin Kramer396dcf32010-11-05 19:56:37 +000069namespace {
John McCall07072662010-11-02 01:45:15 +000070/// Flags controlling the computation of linkage and visibility.
71struct LVFlags {
72 bool ConsiderGlobalVisibility;
73 bool ConsiderVisibilityAttributes;
John McCall8bc6d5b2011-03-04 10:39:25 +000074 bool ConsiderTemplateParameterTypes;
John McCall07072662010-11-02 01:45:15 +000075
76 LVFlags() : ConsiderGlobalVisibility(true),
John McCall8bc6d5b2011-03-04 10:39:25 +000077 ConsiderVisibilityAttributes(true),
78 ConsiderTemplateParameterTypes(true) {
John McCall07072662010-11-02 01:45:15 +000079 }
80
Douglas Gregorbf62d642010-12-06 18:36:25 +000081 /// \brief Returns a set of flags that is only useful for computing the
82 /// linkage, not the visibility, of a declaration.
83 static LVFlags CreateOnlyDeclLinkage() {
84 LVFlags F;
85 F.ConsiderGlobalVisibility = false;
86 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +000087 F.ConsiderTemplateParameterTypes = false;
Douglas Gregorbf62d642010-12-06 18:36:25 +000088 return F;
89 }
90
John McCall07072662010-11-02 01:45:15 +000091 /// Returns a set of flags, otherwise based on these, which ignores
92 /// off all sources of visibility except template arguments.
93 LVFlags onlyTemplateVisibility() const {
94 LVFlags F = *this;
95 F.ConsiderGlobalVisibility = false;
96 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +000097 F.ConsiderTemplateParameterTypes = false;
John McCall07072662010-11-02 01:45:15 +000098 return F;
99 }
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000100};
Benjamin Kramer396dcf32010-11-05 19:56:37 +0000101} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +0000102
Rafael Espindola2f869a32012-01-14 00:30:36 +0000103static LinkageInfo getLVForType(QualType T) {
104 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
105 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
106}
107
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000108/// \brief Get the most restrictive linkage for the types in the given
109/// template parameter list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000110static LinkageInfo
John McCall457a04e2010-10-22 21:05:15 +0000111getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000112 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000113 for (TemplateParameterList::const_iterator P = Params->begin(),
114 PEnd = Params->end();
115 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000116 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
117 if (NTTP->isExpandedParameterPack()) {
118 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
119 QualType T = NTTP->getExpansionType(I);
120 if (!T->isDependentType())
Rafael Espindola2f869a32012-01-14 00:30:36 +0000121 LV.merge(getLVForType(T));
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000122 }
123 continue;
124 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000125
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000126 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000127 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000128 continue;
129 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000130 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000131
132 if (TemplateTemplateParmDecl *TTP
133 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000134 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 }
136 }
137
John McCall457a04e2010-10-22 21:05:15 +0000138 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000139}
140
Douglas Gregorbf62d642010-12-06 18:36:25 +0000141/// getLVForDecl - Get the linkage and visibility for the given declaration.
142static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
143
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000144/// \brief Get the most restrictive linkage for the types and
145/// declarations in the given template argument list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000146static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
147 unsigned NumArgs,
148 LVFlags &F) {
149 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000150
151 for (unsigned I = 0; I != NumArgs; ++I) {
152 switch (Args[I].getKind()) {
153 case TemplateArgument::Null:
154 case TemplateArgument::Integral:
155 case TemplateArgument::Expression:
156 break;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000157
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000158 case TemplateArgument::Type:
Rafael Espindola2f869a32012-01-14 00:30:36 +0000159 LV.merge(getLVForType(Args[I].getAsType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000160 break;
161
162 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000163 // The decl can validly be null as the representation of nullptr
164 // arguments, valid only in C++0x.
165 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000166 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
167 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000168 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000169 break;
170
171 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000172 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000173 if (TemplateDecl *Template
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000174 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola2f869a32012-01-14 00:30:36 +0000175 LV.merge(getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000176 break;
177
178 case TemplateArgument::Pack:
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000179 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
180 Args[I].pack_size(),
181 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000182 break;
183 }
184 }
185
John McCall457a04e2010-10-22 21:05:15 +0000186 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000187}
188
Rafael Espindola2f869a32012-01-14 00:30:36 +0000189static LinkageInfo
Douglas Gregorbf62d642010-12-06 18:36:25 +0000190getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
191 LVFlags &F) {
192 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000193}
194
John McCallb8c604a2011-06-27 23:06:04 +0000195static bool shouldConsiderTemplateLV(const FunctionDecl *fn,
196 const FunctionTemplateSpecializationInfo *spec) {
197 return !(spec->isExplicitSpecialization() &&
198 fn->hasAttr<VisibilityAttr>());
199}
200
201static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
202 return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
203}
204
John McCall07072662010-11-02 01:45:15 +0000205static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000206 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000207 "Not a name having namespace scope");
208 ASTContext &Context = D->getASTContext();
209
210 // C++ [basic.link]p3:
211 // A name having namespace scope (3.3.6) has internal linkage if it
212 // is the name of
213 // - an object, reference, function or function template that is
214 // explicitly declared static; or,
215 // (This bullet corresponds to C99 6.2.2p3.)
216 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
217 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000218 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000219 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000220
221 // - an object or reference that is explicitly declared const
222 // and neither explicitly declared extern nor previously
223 // declared to have external linkage; or
224 // (there is no equivalent in C99)
225 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000226 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000227 Var->getStorageClass() != SC_Extern &&
228 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000229 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000230 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000231 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000232 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000233 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000234 FoundExtern = true;
235
236 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000237 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000238 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000239 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000240 const VarDecl *PrevVar = Var->getPreviousDecl();
241 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000242 if (PrevVar->getStorageClass() == SC_PrivateExtern)
243 break;
244 if (PrevVar)
245 return PrevVar->getLinkageAndVisibility();
246 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000247 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000248 // C++ [temp]p4:
249 // A non-member function template can have internal linkage; any
250 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000251 const FunctionDecl *Function = 0;
252 if (const FunctionTemplateDecl *FunTmpl
253 = dyn_cast<FunctionTemplateDecl>(D))
254 Function = FunTmpl->getTemplatedDecl();
255 else
256 Function = cast<FunctionDecl>(D);
257
258 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000259 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000260 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000261 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
262 // - a data member of an anonymous union.
263 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000264 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000265 }
266
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000267 if (D->isInAnonymousNamespace()) {
268 const VarDecl *Var = dyn_cast<VarDecl>(D);
269 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000270 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
271 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000272 return LinkageInfo::uniqueExternal();
273 }
John McCallb7139c42010-10-28 04:18:25 +0000274
John McCall457a04e2010-10-22 21:05:15 +0000275 // Set up the defaults.
276
277 // C99 6.2.2p5:
278 // If the declaration of an identifier for an object has file
279 // scope and no storage-class specifier, its linkage is
280 // external.
John McCallc273f242010-10-30 11:50:40 +0000281 LinkageInfo LV;
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000282 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCallc273f242010-10-30 11:50:40 +0000283
John McCall07072662010-11-02 01:45:15 +0000284 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000285 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
286 LV.setVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000287 F.ConsiderGlobalVisibility = false;
John McCall2faf32c2010-12-10 02:59:44 +0000288 } else {
289 // If we're declared in a namespace with a visibility attribute,
290 // use that namespace's visibility, but don't call it explicit.
291 for (const DeclContext *DC = D->getDeclContext();
292 !isa<TranslationUnitDecl>(DC);
293 DC = DC->getParent()) {
Rafael Espindola09593a32012-01-01 17:48:19 +0000294 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
295 if (!ND) continue;
296 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola0f960a02012-01-01 18:06:40 +0000297 LV.setVisibility(*Vis, true);
John McCall2faf32c2010-12-10 02:59:44 +0000298 F.ConsiderGlobalVisibility = false;
299 break;
300 }
301 }
John McCall07072662010-11-02 01:45:15 +0000302 }
John McCallc273f242010-10-30 11:50:40 +0000303 }
John McCall457a04e2010-10-22 21:05:15 +0000304
Douglas Gregorf73b2822009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000306
Douglas Gregorf73b2822009-11-25 22:24:25 +0000307 // A name having namespace scope has external linkage if it is the
308 // name of
309 //
310 // - an object or reference, unless it has internal linkage; or
311 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall457a04e2010-10-22 21:05:15 +0000315 // Modify the variable's LV by the LV of its type unless this is
316 // C or extern "C". This follows from [basic.link]p9:
317 // A type without linkage shall not be used as the type of a
318 // variable or function with external linkage unless
319 // - the entity has C language linkage, or
320 // - the entity is declared within an unnamed namespace, or
321 // - the entity is not used or is defined in the same
322 // translation unit.
323 // and [basic.link]p10:
324 // ...the types specified by all declarations referring to a
325 // given variable or function shall be identical...
326 // C does not have an equivalent rule.
327 //
John McCall5fe84122010-10-26 04:59:26 +0000328 // Ignore this if we've got an explicit attribute; the user
329 // probably knows what they're doing.
330 //
John McCall457a04e2010-10-22 21:05:15 +0000331 // Note that we don't want to make the variable non-external
332 // because of this, but unique-external linkage suits us.
Eli Friedman839192f2012-01-15 01:23:58 +0000333 if (Context.getLangOptions().CPlusPlus &&
334 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000335 LinkageInfo TypeLV = getLVForType(Var->getType());
336 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000337 return LinkageInfo::uniqueExternal();
338 if (!LV.visibilityExplicit())
Rafael Espindola2f869a32012-01-14 00:30:36 +0000339 LV.mergeVisibility(TypeLV.visibility(), TypeLV.visibilityExplicit());
John McCall37bb6c92010-10-29 22:22:43 +0000340 }
341
John McCall23032652010-11-02 18:38:13 +0000342 if (Var->getStorageClass() == SC_PrivateExtern)
343 LV.setVisibility(HiddenVisibility, true);
344
Douglas Gregorf73b2822009-11-25 22:24:25 +0000345 if (!Context.getLangOptions().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()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000359 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
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)
373 LV.setVisibility(HiddenVisibility, true);
374
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.
380 if (!Context.getLangOptions().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()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000394 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
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.
Eli Friedman839192f2012-01-15 01:23:58 +0000404 if (Context.getLangOptions().CPlusPlus &&
405 !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()) {
John McCallb8c604a2011-06-27 23:06:04 +0000413 if (shouldConsiderTemplateLV(Function, specInfo)) {
414 LV.merge(getLVForDecl(specInfo->getTemplate(),
415 F.onlyTemplateVisibility()));
416 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000417 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F));
John McCallb8c604a2011-06-27 23:06:04 +0000418 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000419 }
420
Douglas Gregorf73b2822009-11-25 22:24:25 +0000421 // - a named class (Clause 9), or an unnamed class defined in a
422 // typedef declaration in which the class has the typedef name
423 // for linkage purposes (7.1.3); or
424 // - a named enumeration (7.2), or an unnamed enumeration
425 // defined in a typedef declaration in which the enumeration
426 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000427 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
428 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000429 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000430 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000431
John McCall457a04e2010-10-22 21:05:15 +0000432 // If this is a class template specialization, consider the
433 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000434 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000435 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCallb8c604a2011-06-27 23:06:04 +0000436 if (shouldConsiderTemplateLV(spec)) {
437 // From the template.
438 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
439 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000440
John McCallb8c604a2011-06-27 23:06:04 +0000441 // The arguments at which the template was instantiated.
442 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000443 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F));
John McCallb8c604a2011-06-27 23:06:04 +0000444 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000445 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000446
John McCall5fe84122010-10-26 04:59:26 +0000447 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000448 if (F.ConsiderGlobalVisibility)
449 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000450 (Context.getLangOptions().CPlusPlus &&
451 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000452
Douglas Gregorf73b2822009-11-25 22:24:25 +0000453 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000454 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000455 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000456 if (!isExternalLinkage(EnumLV.linkage()))
457 return LinkageInfo::none();
458 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000459
460 // - a template, unless it is a function template that has
461 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000462 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
463 if (F.ConsiderTemplateParameterTypes)
464 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000465
Douglas Gregorf73b2822009-11-25 22:24:25 +0000466 // - a namespace (7.3), unless it is declared within an unnamed
467 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000468 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
469 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000470
John McCall457a04e2010-10-22 21:05:15 +0000471 // By extension, we assign external linkage to Objective-C
472 // interfaces.
473 } else if (isa<ObjCInterfaceDecl>(D)) {
474 // fallout
475
476 // Everything not covered here has no linkage.
477 } else {
John McCallc273f242010-10-30 11:50:40 +0000478 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000479 }
480
481 // If we ended up with non-external linkage, visibility should
482 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000483 if (LV.linkage() != ExternalLinkage)
484 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000485
John McCall457a04e2010-10-22 21:05:15 +0000486 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000487}
488
John McCall07072662010-11-02 01:45:15 +0000489static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000490 // Only certain class members have linkage. Note that fields don't
491 // really have linkage, but it's convenient to say they do for the
492 // purposes of calculating linkage of pointer-to-data-member
493 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000494 if (!(isa<CXXMethodDecl>(D) ||
495 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000496 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000497 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000498 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000499 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000500
John McCall07072662010-11-02 01:45:15 +0000501 LinkageInfo LV;
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000502 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall07072662010-11-02 01:45:15 +0000503
504 // The flags we're going to use to compute the class's visibility.
505 LVFlags ClassF = F;
506
507 // If we have an explicit visibility attribute, merge that in.
508 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000509 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
510 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000511
512 // Ignore global visibility later, but not this attribute.
513 F.ConsiderGlobalVisibility = false;
514
515 // Ignore both global visibility and attributes when computing our
516 // parent's visibility.
517 ClassF = F.onlyTemplateVisibility();
518 }
519 }
John McCallc273f242010-10-30 11:50:40 +0000520
521 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000522 // linkage.
523 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
524 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000525 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000526
527 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000528 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000529 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000530
John McCall8823c652010-08-13 08:35:10 +0000531 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000532 // If the type of the function uses a type with unique-external
533 // linkage, it's not legally usable from outside this translation unit.
534 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
535 return LinkageInfo::uniqueExternal();
536
John McCall37bb6c92010-10-29 22:22:43 +0000537 TemplateSpecializationKind TSK = TSK_Undeclared;
538
John McCall457a04e2010-10-22 21:05:15 +0000539 // If this is a method template specialization, use the linkage for
540 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000541 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000542 = MD->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000543 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000544 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
545 F));
John McCallb8c604a2011-06-27 23:06:04 +0000546 if (F.ConsiderTemplateParameterTypes)
547 LV.merge(getLVForTemplateParameterList(
548 spec->getTemplate()->getTemplateParameters()));
549 }
John McCall37bb6c92010-10-29 22:22:43 +0000550
John McCallb8c604a2011-06-27 23:06:04 +0000551 TSK = spec->getTemplateSpecializationKind();
John McCall37bb6c92010-10-29 22:22:43 +0000552 } else if (MemberSpecializationInfo *MSI =
553 MD->getMemberSpecializationInfo()) {
554 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000555 }
556
John McCall37bb6c92010-10-29 22:22:43 +0000557 // If we're paying attention to global visibility, apply
558 // -finline-visibility-hidden if this is an inline method.
559 //
John McCallc273f242010-10-30 11:50:40 +0000560 // Note that ConsiderGlobalVisibility doesn't yet have information
561 // about whether containing classes have visibility attributes,
562 // and that's intentional.
563 if (TSK != TSK_ExplicitInstantiationDeclaration &&
Rafael Espindola0b790462011-12-27 21:15:28 +0000564 TSK != TSK_ExplicitInstantiationDefinition &&
John McCall07072662010-11-02 01:45:15 +0000565 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000566 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
567 // InlineVisibilityHidden only applies to definitions, and
568 // isInlined() only gives meaningful answers on definitions
569 // anyway.
570 const FunctionDecl *Def = 0;
571 if (MD->hasBody(Def) && Def->isInlined())
572 LV.setVisibility(HiddenVisibility);
573 }
John McCall457a04e2010-10-22 21:05:15 +0000574
John McCall37bb6c92010-10-29 22:22:43 +0000575 // Note that in contrast to basically every other situation, we
576 // *do* apply -fvisibility to method declarations.
577
578 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000579 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000580 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-06-27 23:06:04 +0000581 if (shouldConsiderTemplateLV(spec)) {
582 // Merge template argument/parameter information for member
583 // class template specializations.
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000584 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
585 F));
John McCall8bc6d5b2011-03-04 10:39:25 +0000586 if (F.ConsiderTemplateParameterTypes)
587 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000588 spec->getSpecializedTemplate()->getTemplateParameters()));
589 }
John McCall37bb6c92010-10-29 22:22:43 +0000590 }
591
John McCall37bb6c92010-10-29 22:22:43 +0000592 // Static data members.
593 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000594 // Modify the variable's linkage by its type, but ignore the
595 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000596 LinkageInfo TypeLV = getLVForType(VD->getType());
597 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000598 LV.mergeLinkage(UniqueExternalLinkage);
599 if (!LV.visibilityExplicit())
Rafael Espindola2f869a32012-01-14 00:30:36 +0000600 LV.mergeVisibility(TypeLV.visibility(), TypeLV.visibilityExplicit());
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) ==
656 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000657 return Linkage(CachedLinkage);
658 }
659
660 CachedLinkage = getLVForDecl(this,
661 LVFlags::CreateOnlyDeclLinkage()).linkage();
662 HasCachedLinkage = 1;
663 return Linkage(CachedLinkage);
664}
665
John McCallc273f242010-10-30 11:50:40 +0000666LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000667 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000668 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000669 HasCachedLinkage = 1;
670 CachedLinkage = LI.linkage();
671 return LI;
John McCall033caa52010-10-29 00:29:13 +0000672}
Ted Kremenek926d8602010-04-20 23:15:35 +0000673
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000674llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
675 // Use the most recent declaration of a variable.
676 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000677 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000678
679 // Use the most recent declaration of a function, and also handle
680 // function template specializations.
681 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
682 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000683 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000684 return V;
685
686 // If the function is a specialization of a template with an
687 // explicit visibility attribute, use that.
688 if (FunctionTemplateSpecializationInfo *templateInfo
689 = fn->getTemplateSpecializationInfo())
690 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
691
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000692 // If the function is a member of a specialization of a class template
693 // and the corresponding decl has explicit visibility, use that.
694 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
695 if (InstantiatedFrom)
696 return getVisibilityOf(InstantiatedFrom);
697
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000698 return llvm::Optional<Visibility>();
699 }
700
701 // Otherwise, just check the declaration itself first.
702 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
703 return V;
704
705 // If there wasn't explicit visibility there, and this is a
706 // specialization of a class template, check for visibility
707 // on the pattern.
708 if (const ClassTemplateSpecializationDecl *spec
709 = dyn_cast<ClassTemplateSpecializationDecl>(this))
710 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
711
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000712 // If this is a member class of a specialization of a class template
713 // and the corresponding decl has explicit visibility, use that.
714 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
715 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
716 if (InstantiatedFrom)
717 return getVisibilityOf(InstantiatedFrom);
718 }
719
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000720 return llvm::Optional<Visibility>();
721}
722
John McCall07072662010-11-02 01:45:15 +0000723static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000724 // Objective-C: treat all Objective-C declarations as having external
725 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000726 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000727 default:
728 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000729 case Decl::ParmVar:
730 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000731 case Decl::TemplateTemplateParm: // count these as external
732 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000733 case Decl::ObjCAtDefsField:
734 case Decl::ObjCCategory:
735 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000736 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000737 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000738 case Decl::ObjCMethod:
739 case Decl::ObjCProperty:
740 case Decl::ObjCPropertyImpl:
741 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000742 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000743
744 case Decl::CXXRecord: {
745 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
746 if (Record->isLambda()) {
747 if (!Record->getLambdaManglingNumber()) {
748 // This lambda has no mangling number, so it's internal.
749 return LinkageInfo::internal();
750 }
751
752 // This lambda has its linkage/visibility determined by its owner.
753 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
754 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
755 if (isa<ParmVarDecl>(ContextDecl))
756 DC = ContextDecl->getDeclContext()->getRedeclContext();
757 else
758 return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags);
759 }
760
761 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
762 return getLVForDecl(ND, Flags);
763
764 return LinkageInfo::external();
765 }
766
767 break;
768 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000769 }
770
Douglas Gregorf73b2822009-11-25 22:24:25 +0000771 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000772 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000773 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000774
775 // C++ [basic.link]p5:
776 // In addition, a member function, static data member, a named
777 // class or enumeration of class scope, or an unnamed class or
778 // enumeration defined in a class-scope typedef declaration such
779 // that the class or enumeration has the typedef name for linkage
780 // purposes (7.1.3), has external linkage if the name of the class
781 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000782 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000783 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000784
785 // C++ [basic.link]p6:
786 // The name of a function declared in block scope and the name of
787 // an object declared by a block scope extern declaration have
788 // linkage. If there is a visible declaration of an entity with
789 // linkage having the same name and type, ignoring entities
790 // declared outside the innermost enclosing namespace scope, the
791 // block scope declaration declares that same entity and receives
792 // the linkage of the previous declaration. If there is more than
793 // one such matching entity, the program is ill-formed. Otherwise,
794 // if no matching entity is found, the block scope entity receives
795 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000796 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
797 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000798 if (Function->isInAnonymousNamespace() &&
799 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000800 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000801
John McCallc273f242010-10-30 11:50:40 +0000802 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000803 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000804 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
805 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000806 }
807
Douglas Gregorec9fd132012-01-14 16:38:05 +0000808 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000809 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000810 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
811 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000812 }
813
814 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000815 }
816
John McCall033caa52010-10-29 00:29:13 +0000817 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000818 if (Var->getStorageClass() == SC_Extern ||
819 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000820 if (Var->isInAnonymousNamespace() &&
821 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000822 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000823
John McCallc273f242010-10-30 11:50:40 +0000824 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000825 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000826 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000827 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000828 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
829 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000830 }
831
Douglas Gregorec9fd132012-01-14 16:38:05 +0000832 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000833 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000834 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
835 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000836 }
837
838 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000839 }
840 }
841
842 // C++ [basic.link]p6:
843 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000844 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000845}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000846
Douglas Gregor2ada0482009-02-04 17:27:36 +0000847std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000848 return getQualifiedNameAsString(getASTContext().getLangOptions());
849}
850
851std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000852 const DeclContext *Ctx = getDeclContext();
853
854 if (Ctx->isFunctionOrMethod())
855 return getNameAsString();
856
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000857 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000858 ContextsTy Contexts;
859
860 // Collect contexts.
861 while (Ctx && isa<NamedDecl>(Ctx)) {
862 Contexts.push_back(Ctx);
863 Ctx = Ctx->getParent();
864 };
865
866 std::string QualName;
867 llvm::raw_string_ostream OS(QualName);
868
869 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
870 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000871 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000872 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000873 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
874 std::string TemplateArgsStr
875 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000876 TemplateArgs.data(),
877 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000878 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000879 OS << Spec->getName() << TemplateArgsStr;
880 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000881 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000882 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000883 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000884 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000885 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
886 if (!RD->getIdentifier())
887 OS << "<anonymous " << RD->getKindName() << '>';
888 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000889 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000890 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000891 const FunctionProtoType *FT = 0;
892 if (FD->hasWrittenPrototype())
893 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
894
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000895 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000896 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000897 unsigned NumParams = FD->getNumParams();
898 for (unsigned i = 0; i < NumParams; ++i) {
899 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000900 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000901 std::string Param;
902 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000903 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000904 }
905
906 if (FT->isVariadic()) {
907 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000908 OS << ", ";
909 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000910 }
911 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000912 OS << ')';
913 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000914 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000915 }
916 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000917 }
918
John McCalla2a3f7d2010-03-16 21:48:18 +0000919 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000920 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000921 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000922 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000923
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000924 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000925}
926
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000927bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000928 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
929
Douglas Gregor889ceb72009-02-03 19:21:40 +0000930 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
931 // We want to keep it, unless it nominates same namespace.
932 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000933 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
934 ->getOriginalNamespace() ==
935 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
936 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000937 }
Mike Stump11289f42009-09-09 15:08:12 +0000938
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000939 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
940 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000941 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000942
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000943 // For function templates, the underlying function declarations are linked.
944 if (const FunctionTemplateDecl *FunctionTemplate
945 = dyn_cast<FunctionTemplateDecl>(this))
946 if (const FunctionTemplateDecl *OldFunctionTemplate
947 = dyn_cast<FunctionTemplateDecl>(OldD))
948 return FunctionTemplate->getTemplatedDecl()
949 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000950
Steve Naroffc4173fa2009-02-22 19:35:57 +0000951 // For method declarations, we keep track of redeclarations.
952 if (isa<ObjCMethodDecl>(this))
953 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000954
John McCall9f3059a2009-10-09 21:13:30 +0000955 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
956 return true;
957
John McCall3f746822009-11-17 05:59:44 +0000958 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
959 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
960 cast<UsingShadowDecl>(OldD)->getTargetDecl();
961
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000962 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
963 ASTContext &Context = getASTContext();
964 return Context.getCanonicalNestedNameSpecifier(
965 cast<UsingDecl>(this)->getQualifier()) ==
966 Context.getCanonicalNestedNameSpecifier(
967 cast<UsingDecl>(OldD)->getQualifier());
968 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000969
Douglas Gregorb59643b2012-01-03 23:26:26 +0000970 // A typedef of an Objective-C class type can replace an Objective-C class
971 // declaration or definition, and vice versa.
972 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
973 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
974 return true;
975
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000976 // For non-function declarations, if the declarations are of the
977 // same kind then this must be a redeclaration, or semantic analysis
978 // would not have given us the new declaration.
979 return this->getKind() == OldD->getKind();
980}
981
Douglas Gregoreddf4332009-02-24 20:03:32 +0000982bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000983 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000984}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000985
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000986NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000987 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000988 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
989 ND = UD->getTargetDecl();
990
991 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
992 return AD->getClassInterface();
993
994 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000995}
996
John McCalla8ae2222010-04-06 21:38:20 +0000997bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000998 if (!isCXXClassMember())
999 return false;
1000
John McCalla8ae2222010-04-06 21:38:20 +00001001 const NamedDecl *D = this;
1002 if (isa<UsingShadowDecl>(D))
1003 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1004
Francois Pichet783dd6e2010-11-21 06:08:52 +00001005 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001006 return true;
1007 if (isa<CXXMethodDecl>(D))
1008 return cast<CXXMethodDecl>(D)->isInstance();
1009 if (isa<FunctionTemplateDecl>(D))
1010 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1011 ->getTemplatedDecl())->isInstance();
1012 return false;
1013}
1014
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001015//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001016// DeclaratorDecl Implementation
1017//===----------------------------------------------------------------------===//
1018
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001019template <typename DeclT>
1020static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1021 if (decl->getNumTemplateParameterLists() > 0)
1022 return decl->getTemplateParameterList(0)->getTemplateLoc();
1023 else
1024 return decl->getInnerLocStart();
1025}
1026
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001027SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001028 TypeSourceInfo *TSI = getTypeSourceInfo();
1029 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001030 return SourceLocation();
1031}
1032
Douglas Gregor14454802011-02-25 02:25:35 +00001033void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1034 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001035 // Make sure the extended decl info is allocated.
1036 if (!hasExtInfo()) {
1037 // Save (non-extended) type source info pointer.
1038 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1039 // Allocate external info struct.
1040 DeclInfo = new (getASTContext()) ExtInfo;
1041 // Restore savedTInfo into (extended) decl info.
1042 getExtInfo()->TInfo = savedTInfo;
1043 }
1044 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001045 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001046 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001047 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001048 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001049 if (getExtInfo()->NumTemplParamLists == 0) {
1050 // Save type source info pointer.
1051 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1052 // Deallocate the extended decl info.
1053 getASTContext().Deallocate(getExtInfo());
1054 // Restore savedTInfo into (non-extended) decl info.
1055 DeclInfo = savedTInfo;
1056 }
1057 else
1058 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001059 }
1060 }
1061}
1062
Abramo Bagnara60804e12011-03-18 15:16:37 +00001063void
1064DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1065 unsigned NumTPLists,
1066 TemplateParameterList **TPLists) {
1067 assert(NumTPLists > 0);
1068 // Make sure the extended decl info is allocated.
1069 if (!hasExtInfo()) {
1070 // Save (non-extended) type source info pointer.
1071 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1072 // Allocate external info struct.
1073 DeclInfo = new (getASTContext()) ExtInfo;
1074 // Restore savedTInfo into (extended) decl info.
1075 getExtInfo()->TInfo = savedTInfo;
1076 }
1077 // Set the template parameter lists info.
1078 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1079}
1080
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001081SourceLocation DeclaratorDecl::getOuterLocStart() const {
1082 return getTemplateOrInnerLocStart(this);
1083}
1084
Abramo Bagnaraea947882011-03-08 16:41:52 +00001085namespace {
1086
1087// Helper function: returns true if QT is or contains a type
1088// having a postfix component.
1089bool typeIsPostfix(clang::QualType QT) {
1090 while (true) {
1091 const Type* T = QT.getTypePtr();
1092 switch (T->getTypeClass()) {
1093 default:
1094 return false;
1095 case Type::Pointer:
1096 QT = cast<PointerType>(T)->getPointeeType();
1097 break;
1098 case Type::BlockPointer:
1099 QT = cast<BlockPointerType>(T)->getPointeeType();
1100 break;
1101 case Type::MemberPointer:
1102 QT = cast<MemberPointerType>(T)->getPointeeType();
1103 break;
1104 case Type::LValueReference:
1105 case Type::RValueReference:
1106 QT = cast<ReferenceType>(T)->getPointeeType();
1107 break;
1108 case Type::PackExpansion:
1109 QT = cast<PackExpansionType>(T)->getPattern();
1110 break;
1111 case Type::Paren:
1112 case Type::ConstantArray:
1113 case Type::DependentSizedArray:
1114 case Type::IncompleteArray:
1115 case Type::VariableArray:
1116 case Type::FunctionProto:
1117 case Type::FunctionNoProto:
1118 return true;
1119 }
1120 }
1121}
1122
1123} // namespace
1124
1125SourceRange DeclaratorDecl::getSourceRange() const {
1126 SourceLocation RangeEnd = getLocation();
1127 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1128 if (typeIsPostfix(TInfo->getType()))
1129 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1130 }
1131 return SourceRange(getOuterLocStart(), RangeEnd);
1132}
1133
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001134void
Douglas Gregor20527e22010-06-15 17:44:38 +00001135QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1136 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001137 TemplateParameterList **TPLists) {
1138 assert((NumTPLists == 0 || TPLists != 0) &&
1139 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001140
1141 // Free previous template parameters (if any).
1142 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001143 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001144 TemplParamLists = 0;
1145 NumTemplParamLists = 0;
1146 }
1147 // Set info on matched template parameter lists (if any).
1148 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001149 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001150 NumTemplParamLists = NumTPLists;
1151 for (unsigned i = NumTPLists; i-- > 0; )
1152 TemplParamLists[i] = TPLists[i];
1153 }
1154}
1155
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001156//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001157// VarDecl Implementation
1158//===----------------------------------------------------------------------===//
1159
Sebastian Redl833ef452010-01-26 22:01:41 +00001160const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1161 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001162 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001163 case SC_Auto: return "auto";
1164 case SC_Extern: return "extern";
1165 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1166 case SC_PrivateExtern: return "__private_extern__";
1167 case SC_Register: return "register";
1168 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001169 }
1170
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001171 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001172}
1173
Abramo Bagnaradff19302011-03-08 08:55:46 +00001174VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1175 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001176 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001177 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001178 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001179}
1180
Douglas Gregor72172e92012-01-05 21:55:30 +00001181VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1182 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1183 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1184 QualType(), 0, SC_None, SC_None);
1185}
1186
Douglas Gregorbf62d642010-12-06 18:36:25 +00001187void VarDecl::setStorageClass(StorageClass SC) {
1188 assert(isLegalForVariable(SC));
1189 if (getStorageClass() != SC)
1190 ClearLinkageCache();
1191
John McCallbeaa11c2011-05-01 02:13:58 +00001192 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001193}
1194
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001195SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001196 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001197 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001198 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001199}
1200
Sebastian Redl833ef452010-01-26 22:01:41 +00001201bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001202 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001203 return false;
1204
Eli Friedman839192f2012-01-15 01:23:58 +00001205 const DeclContext *DC = getDeclContext();
1206 if (DC->isRecord())
1207 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001208
Eli Friedman839192f2012-01-15 01:23:58 +00001209 ASTContext &Context = getASTContext();
1210 if (!Context.getLangOptions().CPlusPlus)
1211 return true;
1212 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001213}
1214
1215VarDecl *VarDecl::getCanonicalDecl() {
1216 return getFirstDeclaration();
1217}
1218
Sebastian Redl35351a92010-01-31 22:27:38 +00001219VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1220 // C++ [basic.def]p2:
1221 // A declaration is a definition unless [...] it contains the 'extern'
1222 // specifier or a linkage-specification and neither an initializer [...],
1223 // it declares a static data member in a class declaration [...].
1224 // C++ [temp.expl.spec]p15:
1225 // An explicit specialization of a static data member of a template is a
1226 // definition if the declaration includes an initializer; otherwise, it is
1227 // a declaration.
1228 if (isStaticDataMember()) {
1229 if (isOutOfLine() && (hasInit() ||
1230 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1231 return Definition;
1232 else
1233 return DeclarationOnly;
1234 }
1235 // C99 6.7p5:
1236 // A definition of an identifier is a declaration for that identifier that
1237 // [...] causes storage to be reserved for that object.
1238 // Note: that applies for all non-file-scope objects.
1239 // C99 6.9.2p1:
1240 // If the declaration of an identifier for an object has file scope and an
1241 // initializer, the declaration is an external definition for the identifier
1242 if (hasInit())
1243 return Definition;
1244 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1245 if (hasExternalStorage())
1246 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001247
John McCall8e7d6562010-08-26 03:08:43 +00001248 if (getStorageClassAsWritten() == SC_Extern ||
1249 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001250 for (const VarDecl *PrevVar = getPreviousDecl();
1251 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001252 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1253 return DeclarationOnly;
1254 }
1255 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001256 // C99 6.9.2p2:
1257 // A declaration of an object that has file scope without an initializer,
1258 // and without a storage class specifier or the scs 'static', constitutes
1259 // a tentative definition.
1260 // No such thing in C++.
1261 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1262 return TentativeDefinition;
1263
1264 // What's left is (in C, block-scope) declarations without initializers or
1265 // external storage. These are definitions.
1266 return Definition;
1267}
1268
Sebastian Redl35351a92010-01-31 22:27:38 +00001269VarDecl *VarDecl::getActingDefinition() {
1270 DefinitionKind Kind = isThisDeclarationADefinition();
1271 if (Kind != TentativeDefinition)
1272 return 0;
1273
Chris Lattner48eb14d2010-06-14 18:31:46 +00001274 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001275 VarDecl *First = getFirstDeclaration();
1276 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1277 I != E; ++I) {
1278 Kind = (*I)->isThisDeclarationADefinition();
1279 if (Kind == Definition)
1280 return 0;
1281 else if (Kind == TentativeDefinition)
1282 LastTentative = *I;
1283 }
1284 return LastTentative;
1285}
1286
1287bool VarDecl::isTentativeDefinitionNow() const {
1288 DefinitionKind Kind = isThisDeclarationADefinition();
1289 if (Kind != TentativeDefinition)
1290 return false;
1291
1292 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1293 if ((*I)->isThisDeclarationADefinition() == Definition)
1294 return false;
1295 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001296 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001297}
1298
Sebastian Redl5ca79842010-02-01 20:16:42 +00001299VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001300 VarDecl *First = getFirstDeclaration();
1301 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1302 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001303 if ((*I)->isThisDeclarationADefinition() == Definition)
1304 return *I;
1305 }
1306 return 0;
1307}
1308
John McCall37bb6c92010-10-29 22:22:43 +00001309VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1310 DefinitionKind Kind = DeclarationOnly;
1311
1312 const VarDecl *First = getFirstDeclaration();
1313 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001314 I != E; ++I) {
John McCall37bb6c92010-10-29 22:22:43 +00001315 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001316 if (Kind == Definition)
1317 break;
1318 }
John McCall37bb6c92010-10-29 22:22:43 +00001319
1320 return Kind;
1321}
1322
Sebastian Redl5ca79842010-02-01 20:16:42 +00001323const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001324 redecl_iterator I = redecls_begin(), E = redecls_end();
1325 while (I != E && !I->getInit())
1326 ++I;
1327
1328 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001329 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001330 return I->getInit();
1331 }
1332 return 0;
1333}
1334
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001335bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001336 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001337 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001338
1339 if (!isStaticDataMember())
1340 return false;
1341
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001342 // If this static data member was instantiated from a static data member of
1343 // a class template, check whether that static data member was defined
1344 // out-of-line.
1345 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1346 return VD->isOutOfLine();
1347
1348 return false;
1349}
1350
Douglas Gregor1d957a32009-10-27 18:42:08 +00001351VarDecl *VarDecl::getOutOfLineDefinition() {
1352 if (!isStaticDataMember())
1353 return 0;
1354
1355 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1356 RD != RDEnd; ++RD) {
1357 if (RD->getLexicalDeclContext()->isFileContext())
1358 return *RD;
1359 }
1360
1361 return 0;
1362}
1363
Douglas Gregord5058122010-02-11 01:19:42 +00001364void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001365 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1366 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001367 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001368 }
1369
1370 Init = I;
1371}
1372
Richard Smith242ad892011-12-21 02:55:12 +00001373bool VarDecl::isUsableInConstantExpressions() const {
1374 const LangOptions &Lang = getASTContext().getLangOptions();
1375
Richard Smith35ecb362012-03-02 04:14:40 +00001376 if (!Lang.CPlusPlus)
1377 return false;
1378
1379 // In C++11, any variable of reference type can be used in a constant
1380 // expression if it is initialized by a constant expression.
1381 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1382 return true;
1383
1384 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001385 // not require the variable to be non-volatile, but we consider this to be a
1386 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001387 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001388 return false;
1389
1390 // In C++, const, non-volatile variables of integral or enumeration types
1391 // can be used in constant expressions.
1392 if (getType()->isIntegralOrEnumerationType())
1393 return true;
1394
Richard Smith35ecb362012-03-02 04:14:40 +00001395 // Additionally, in C++11, non-volatile constexpr variables can be used in
1396 // constant expressions.
1397 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001398}
1399
Richard Smithd0b4dd62011-12-19 06:19:21 +00001400/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1401/// form, which contains extra information on the evaluated value of the
1402/// initializer.
1403EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1404 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1405 if (!Eval) {
1406 Stmt *S = Init.get<Stmt *>();
1407 Eval = new (getASTContext()) EvaluatedStmt;
1408 Eval->Value = S;
1409 Init = Eval;
1410 }
1411 return Eval;
1412}
1413
Richard Smithdafff942012-01-14 04:30:29 +00001414APValue *VarDecl::evaluateValue() const {
1415 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1416 return evaluateValue(Notes);
1417}
1418
1419APValue *VarDecl::evaluateValue(
1420 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001421 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1422
1423 // We only produce notes indicating why an initializer is non-constant the
1424 // first time it is evaluated. FIXME: The notes won't always be emitted the
1425 // first time we try evaluation, so might not be produced at all.
1426 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001427 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001428
1429 const Expr *Init = cast<Expr>(Eval->Value);
1430 assert(!Init->isValueDependent());
1431
1432 if (Eval->IsEvaluating) {
1433 // FIXME: Produce a diagnostic for self-initialization.
1434 Eval->CheckedICE = true;
1435 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001436 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001437 }
1438
1439 Eval->IsEvaluating = true;
1440
1441 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1442 this, Notes);
1443
1444 // Ensure the result is an uninitialized APValue if evaluation fails.
1445 if (!Result)
1446 Eval->Evaluated = APValue();
1447
1448 Eval->IsEvaluating = false;
1449 Eval->WasEvaluated = true;
1450
1451 // In C++11, we have determined whether the initializer was a constant
1452 // expression as a side-effect.
1453 if (getASTContext().getLangOptions().CPlusPlus0x && !Eval->CheckedICE) {
1454 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001455 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001456 }
1457
Richard Smithdafff942012-01-14 04:30:29 +00001458 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001459}
1460
1461bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001462 // Initializers of weak variables are never ICEs.
1463 if (isWeak())
1464 return false;
1465
Richard Smithd0b4dd62011-12-19 06:19:21 +00001466 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1467 if (Eval->CheckedICE)
1468 // We have already checked whether this subexpression is an
1469 // integral constant expression.
1470 return Eval->IsICE;
1471
1472 const Expr *Init = cast<Expr>(Eval->Value);
1473 assert(!Init->isValueDependent());
1474
1475 // In C++11, evaluate the initializer to check whether it's a constant
1476 // expression.
1477 if (getASTContext().getLangOptions().CPlusPlus0x) {
1478 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1479 evaluateValue(Notes);
1480 return Eval->IsICE;
1481 }
1482
1483 // It's an ICE whether or not the definition we found is
1484 // out-of-line. See DR 721 and the discussion in Clang PR
1485 // 6206 for details.
1486
1487 if (Eval->CheckingICE)
1488 return false;
1489 Eval->CheckingICE = true;
1490
1491 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1492 Eval->CheckingICE = false;
1493 Eval->CheckedICE = true;
1494 return Eval->IsICE;
1495}
1496
Douglas Gregorfe314812011-06-21 17:03:29 +00001497bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001498 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001499
1500 const Expr *E = getInit();
1501 if (!E)
1502 return false;
1503
1504 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1505 E = Cleanups->getSubExpr();
1506
1507 return isa<MaterializeTemporaryExpr>(E);
1508}
1509
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001510VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001511 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001512 return cast<VarDecl>(MSI->getInstantiatedFrom());
1513
1514 return 0;
1515}
1516
Douglas Gregor3c74d412009-10-14 20:14:33 +00001517TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001518 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001519 return MSI->getTemplateSpecializationKind();
1520
1521 return TSK_Undeclared;
1522}
1523
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001524MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001525 return getASTContext().getInstantiatedFromStaticDataMember(this);
1526}
1527
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001528void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1529 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001530 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001531 assert(MSI && "Not an instantiated static data member?");
1532 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001533 if (TSK != TSK_ExplicitSpecialization &&
1534 PointOfInstantiation.isValid() &&
1535 MSI->getPointOfInstantiation().isInvalid())
1536 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001537}
1538
Sebastian Redl833ef452010-01-26 22:01:41 +00001539//===----------------------------------------------------------------------===//
1540// ParmVarDecl Implementation
1541//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001542
Sebastian Redl833ef452010-01-26 22:01:41 +00001543ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001544 SourceLocation StartLoc,
1545 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001546 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001547 StorageClass S, StorageClass SCAsWritten,
1548 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001549 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001550 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001551}
1552
Douglas Gregor72172e92012-01-05 21:55:30 +00001553ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1554 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1555 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1556 0, QualType(), 0, SC_None, SC_None, 0);
1557}
1558
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001559SourceRange ParmVarDecl::getSourceRange() const {
1560 if (!hasInheritedDefaultArg()) {
1561 SourceRange ArgRange = getDefaultArgRange();
1562 if (ArgRange.isValid())
1563 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1564 }
1565
1566 return DeclaratorDecl::getSourceRange();
1567}
1568
Sebastian Redl833ef452010-01-26 22:01:41 +00001569Expr *ParmVarDecl::getDefaultArg() {
1570 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1571 assert(!hasUninstantiatedDefaultArg() &&
1572 "Default argument is not yet instantiated!");
1573
1574 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001575 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001576 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001577
Sebastian Redl833ef452010-01-26 22:01:41 +00001578 return Arg;
1579}
1580
Sebastian Redl833ef452010-01-26 22:01:41 +00001581SourceRange ParmVarDecl::getDefaultArgRange() const {
1582 if (const Expr *E = getInit())
1583 return E->getSourceRange();
1584
1585 if (hasUninstantiatedDefaultArg())
1586 return getUninstantiatedDefaultArg()->getSourceRange();
1587
1588 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001589}
1590
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001591bool ParmVarDecl::isParameterPack() const {
1592 return isa<PackExpansionType>(getType());
1593}
1594
Ted Kremenek540017e2011-10-06 05:00:56 +00001595void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1596 getASTContext().setParameterIndex(this, parameterIndex);
1597 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1598}
1599
1600unsigned ParmVarDecl::getParameterIndexLarge() const {
1601 return getASTContext().getParameterIndex(this);
1602}
1603
Nuno Lopes394ec982008-12-17 23:39:55 +00001604//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001605// FunctionDecl Implementation
1606//===----------------------------------------------------------------------===//
1607
Douglas Gregorb11aad82011-02-19 18:51:44 +00001608void FunctionDecl::getNameForDiagnostic(std::string &S,
1609 const PrintingPolicy &Policy,
1610 bool Qualified) const {
1611 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1612 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1613 if (TemplateArgs)
1614 S += TemplateSpecializationType::PrintTemplateArgumentList(
1615 TemplateArgs->data(),
1616 TemplateArgs->size(),
1617 Policy);
1618
1619}
1620
Ted Kremenek186a0742010-04-29 16:49:01 +00001621bool FunctionDecl::isVariadic() const {
1622 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1623 return FT->isVariadic();
1624 return false;
1625}
1626
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001627bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1628 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001629 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001630 Definition = *I;
1631 return true;
1632 }
1633 }
1634
1635 return false;
1636}
1637
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001638bool FunctionDecl::hasTrivialBody() const
1639{
1640 Stmt *S = getBody();
1641 if (!S) {
1642 // Since we don't have a body for this function, we don't know if it's
1643 // trivial or not.
1644 return false;
1645 }
1646
1647 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1648 return true;
1649 return false;
1650}
1651
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001652bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1653 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001654 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001655 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1656 return true;
1657 }
1658 }
1659
1660 return false;
1661}
1662
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001663Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001664 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1665 if (I->Body) {
1666 Definition = *I;
1667 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001668 } else if (I->IsLateTemplateParsed) {
1669 Definition = *I;
1670 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001671 }
1672 }
1673
1674 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001675}
1676
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001677void FunctionDecl::setBody(Stmt *B) {
1678 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001679 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001680 EndRangeLoc = B->getLocEnd();
1681}
1682
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001683void FunctionDecl::setPure(bool P) {
1684 IsPure = P;
1685 if (P)
1686 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1687 Parent->markedVirtualFunctionPure();
1688}
1689
Douglas Gregor16618f22009-09-12 00:17:51 +00001690bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001691 const TranslationUnitDecl *tunit =
1692 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1693 return tunit &&
Alexis Huntf9933e82011-05-15 20:59:31 +00001694 !tunit->getASTContext().getLangOptions().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001695 getIdentifier() &&
1696 getIdentifier()->isStr("main");
1697}
1698
1699bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1700 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1701 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1702 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1703 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1704 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1705
1706 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1707 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1708
1709 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1710 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1711
1712 ASTContext &Context =
1713 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1714 ->getASTContext();
1715
1716 // The result type and first argument type are constant across all
1717 // these operators. The second argument must be exactly void*.
1718 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001719}
1720
Douglas Gregor16618f22009-09-12 00:17:51 +00001721bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001722 if (getLinkage() != ExternalLinkage)
1723 return false;
1724
1725 if (getAttr<OverloadableAttr>())
1726 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001727
Chandler Carruth4322a282011-02-25 00:05:02 +00001728 const DeclContext *DC = getDeclContext();
1729 if (DC->isRecord())
1730 return false;
1731
Eli Friedman839192f2012-01-15 01:23:58 +00001732 ASTContext &Context = getASTContext();
1733 if (!Context.getLangOptions().CPlusPlus)
1734 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001735
Eli Friedman839192f2012-01-15 01:23:58 +00001736 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001737}
1738
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001739bool FunctionDecl::isGlobal() const {
1740 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1741 return Method->isStatic();
1742
John McCall8e7d6562010-08-26 03:08:43 +00001743 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001744 return false;
1745
Mike Stump11289f42009-09-09 15:08:12 +00001746 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001747 DC->isNamespace();
1748 DC = DC->getParent()) {
1749 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1750 if (!Namespace->getDeclName())
1751 return false;
1752 break;
1753 }
1754 }
1755
1756 return true;
1757}
1758
Sebastian Redl833ef452010-01-26 22:01:41 +00001759void
1760FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1761 redeclarable_base::setPreviousDeclaration(PrevDecl);
1762
1763 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1764 FunctionTemplateDecl *PrevFunTmpl
1765 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1766 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1767 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1768 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001769
Axel Naumannfbc7b982011-11-08 18:21:06 +00001770 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001771 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001772}
1773
1774const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1775 return getFirstDeclaration();
1776}
1777
1778FunctionDecl *FunctionDecl::getCanonicalDecl() {
1779 return getFirstDeclaration();
1780}
1781
Douglas Gregorbf62d642010-12-06 18:36:25 +00001782void FunctionDecl::setStorageClass(StorageClass SC) {
1783 assert(isLegalForFunction(SC));
1784 if (getStorageClass() != SC)
1785 ClearLinkageCache();
1786
1787 SClass = SC;
1788}
1789
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001790/// \brief Returns a value indicating whether this function
1791/// corresponds to a builtin function.
1792///
1793/// The function corresponds to a built-in function if it is
1794/// declared at translation scope or within an extern "C" block and
1795/// its name matches with the name of a builtin. The returned value
1796/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001797/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001798/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001799unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001800 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001801 return 0;
1802
1803 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001804 if (!BuiltinID)
1805 return 0;
1806
1807 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001808 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1809 return BuiltinID;
1810
1811 // This function has the name of a known C library
1812 // function. Determine whether it actually refers to the C library
1813 // function or whether it just has the same name.
1814
Douglas Gregora908e7f2009-02-17 03:23:10 +00001815 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001816 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001817 return 0;
1818
Douglas Gregore711f702009-02-14 18:57:46 +00001819 // If this function is at translation-unit scope and we're not in
1820 // C++, it refers to the C library function.
1821 if (!Context.getLangOptions().CPlusPlus &&
1822 getDeclContext()->isTranslationUnit())
1823 return BuiltinID;
1824
1825 // If the function is in an extern "C" linkage specification and is
1826 // not marked "overloadable", it's the real function.
1827 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001828 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001829 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001830 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001831 return BuiltinID;
1832
1833 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001834 return 0;
1835}
1836
1837
Chris Lattner47c0d002009-04-25 06:03:53 +00001838/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001839/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001840/// after it has been created.
1841unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001842 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001843 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001844 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001845 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001846
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001847}
1848
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001849void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001850 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001851 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001852 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001853
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001854 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001855 if (!NewParamInfo.empty()) {
1856 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1857 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001858 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001859}
Chris Lattner41943152007-01-25 04:52:46 +00001860
James Molloy6f8780b2012-02-29 10:24:19 +00001861void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1862 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1863
1864 if (!NewDecls.empty()) {
1865 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1866 std::copy(NewDecls.begin(), NewDecls.end(), A);
1867 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1868 }
1869}
1870
Chris Lattner58258242008-04-10 02:22:51 +00001871/// getMinRequiredArguments - Returns the minimum number of arguments
1872/// needed to call this function. This may be fewer than the number of
1873/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001874/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001875unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001876 if (!getASTContext().getLangOptions().CPlusPlus)
1877 return getNumParams();
1878
Douglas Gregor7825bf32011-01-06 22:09:01 +00001879 unsigned NumRequiredArgs = getNumParams();
1880
1881 // If the last parameter is a parameter pack, we don't need an argument for
1882 // it.
1883 if (NumRequiredArgs > 0 &&
1884 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1885 --NumRequiredArgs;
1886
1887 // If this parameter has a default argument, we don't need an argument for
1888 // it.
1889 while (NumRequiredArgs > 0 &&
1890 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001891 --NumRequiredArgs;
1892
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001893 // We might have parameter packs before the end. These can't be deduced,
1894 // but they can still handle multiple arguments.
1895 unsigned ArgIdx = NumRequiredArgs;
1896 while (ArgIdx > 0) {
1897 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1898 NumRequiredArgs = ArgIdx;
1899
1900 --ArgIdx;
1901 }
1902
Chris Lattner58258242008-04-10 02:22:51 +00001903 return NumRequiredArgs;
1904}
1905
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001906bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001907 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001908 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001909
1910 if (isa<CXXMethodDecl>(this)) {
1911 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1912 return true;
1913 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001914
1915 switch (getTemplateSpecializationKind()) {
1916 case TSK_Undeclared:
1917 case TSK_ExplicitSpecialization:
1918 return false;
1919
1920 case TSK_ImplicitInstantiation:
1921 case TSK_ExplicitInstantiationDeclaration:
1922 case TSK_ExplicitInstantiationDefinition:
1923 // Handle below.
1924 break;
1925 }
1926
1927 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001928 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001929 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001930 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001931
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001932 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001933 return PatternDecl->isInlined();
1934
1935 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001936}
1937
Eli Friedman1b125c32012-02-07 03:50:18 +00001938static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1939 // Only consider file-scope declarations in this test.
1940 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1941 return false;
1942
1943 // Only consider explicit declarations; the presence of a builtin for a
1944 // libcall shouldn't affect whether a definition is externally visible.
1945 if (Redecl->isImplicit())
1946 return false;
1947
1948 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1949 return true; // Not an inline definition
1950
1951 return false;
1952}
1953
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001954/// \brief For a function declaration in C or C++, determine whether this
1955/// declaration causes the definition to be externally visible.
1956///
Eli Friedman1b125c32012-02-07 03:50:18 +00001957/// Specifically, this determines if adding the current declaration to the set
1958/// of redeclarations of the given functions causes
1959/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001960bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1961 assert(!doesThisDeclarationHaveABody() &&
1962 "Must have a declaration without a body.");
1963
1964 ASTContext &Context = getASTContext();
1965
Eli Friedman1b125c32012-02-07 03:50:18 +00001966 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
1967 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1968 // an externally visible definition.
1969 //
1970 // FIXME: What happens if gnu_inline gets added on after the first
1971 // declaration?
1972 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1973 return false;
1974
1975 const FunctionDecl *Prev = this;
1976 bool FoundBody = false;
1977 while ((Prev = Prev->getPreviousDecl())) {
1978 FoundBody |= Prev->Body;
1979
1980 if (Prev->Body) {
1981 // If it's not the case that both 'inline' and 'extern' are
1982 // specified on the definition, then it is always externally visible.
1983 if (!Prev->isInlineSpecified() ||
1984 Prev->getStorageClassAsWritten() != SC_Extern)
1985 return false;
1986 } else if (Prev->isInlineSpecified() &&
1987 Prev->getStorageClassAsWritten() != SC_Extern) {
1988 return false;
1989 }
1990 }
1991 return FoundBody;
1992 }
1993
1994 if (Context.getLangOptions().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001995 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001996
1997 // C99 6.7.4p6:
1998 // [...] If all of the file scope declarations for a function in a
1999 // translation unit include the inline function specifier without extern,
2000 // then the definition in that translation unit is an inline definition.
2001 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002002 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002003 const FunctionDecl *Prev = this;
2004 bool FoundBody = false;
2005 while ((Prev = Prev->getPreviousDecl())) {
2006 FoundBody |= Prev->Body;
2007 if (RedeclForcesDefC99(Prev))
2008 return false;
2009 }
2010 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002011}
2012
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002013/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002014/// definition will be externally visible.
2015///
2016/// Inline function definitions are always available for inlining optimizations.
2017/// However, depending on the language dialect, declaration specifiers, and
2018/// attributes, the definition of an inline function may or may not be
2019/// "externally" visible to other translation units in the program.
2020///
2021/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002022/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002023/// inline definition becomes externally visible (C99 6.7.4p6).
2024///
2025/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2026/// definition, we use the GNU semantics for inline, which are nearly the
2027/// opposite of C99 semantics. In particular, "inline" by itself will create
2028/// an externally visible symbol, but "extern inline" will not create an
2029/// externally visible symbol.
2030bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002031 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002032 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002033 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002034
Rafael Espindolafb2af642011-06-02 16:13:27 +00002035 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002036 // Note: If you change the logic here, please change
2037 // doesDeclarationForceExternallyVisibleDefinition as well.
2038 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002039 // If it's not the case that both 'inline' and 'extern' are
2040 // specified on the definition, then this inline definition is
2041 // externally visible.
2042 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2043 return true;
2044
2045 // If any declaration is 'inline' but not 'extern', then this definition
2046 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002047 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2048 Redecl != RedeclEnd;
2049 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002050 if (Redecl->isInlineSpecified() &&
2051 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002052 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002053 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002054
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002055 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002056 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002057
Douglas Gregor299d76e2009-09-13 07:46:26 +00002058 // C99 6.7.4p6:
2059 // [...] If all of the file scope declarations for a function in a
2060 // translation unit include the inline function specifier without extern,
2061 // then the definition in that translation unit is an inline definition.
2062 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2063 Redecl != RedeclEnd;
2064 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002065 if (RedeclForcesDefC99(*Redecl))
2066 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002067 }
2068
2069 // C99 6.7.4p6:
2070 // An inline definition does not provide an external definition for the
2071 // function, and does not forbid an external definition in another
2072 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002073 return false;
2074}
2075
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002076/// getOverloadedOperator - Which C++ overloaded operator this
2077/// function represents, if any.
2078OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002079 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2080 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002081 else
2082 return OO_None;
2083}
2084
Alexis Huntc88db062010-01-13 09:01:02 +00002085/// getLiteralIdentifier - The literal suffix identifier this function
2086/// represents, if any.
2087const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2088 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2089 return getDeclName().getCXXLiteralIdentifier();
2090 else
2091 return 0;
2092}
2093
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002094FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2095 if (TemplateOrSpecialization.isNull())
2096 return TK_NonTemplate;
2097 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2098 return TK_FunctionTemplate;
2099 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2100 return TK_MemberSpecialization;
2101 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2102 return TK_FunctionTemplateSpecialization;
2103 if (TemplateOrSpecialization.is
2104 <DependentFunctionTemplateSpecializationInfo*>())
2105 return TK_DependentFunctionTemplateSpecialization;
2106
David Blaikie83d382b2011-09-23 05:06:16 +00002107 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002108}
2109
Douglas Gregord801b062009-10-07 23:56:10 +00002110FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002111 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002112 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2113
2114 return 0;
2115}
2116
Douglas Gregor06db9f52009-10-12 20:18:28 +00002117MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2118 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2119}
2120
Douglas Gregord801b062009-10-07 23:56:10 +00002121void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002122FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2123 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002124 TemplateSpecializationKind TSK) {
2125 assert(TemplateOrSpecialization.isNull() &&
2126 "Member function is already a specialization");
2127 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002128 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002129 TemplateOrSpecialization = Info;
2130}
2131
Douglas Gregorafca3b42009-10-27 20:53:28 +00002132bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002133 // If the function is invalid, it can't be implicitly instantiated.
2134 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002135 return false;
2136
2137 switch (getTemplateSpecializationKind()) {
2138 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002139 case TSK_ExplicitInstantiationDefinition:
2140 return false;
2141
2142 case TSK_ImplicitInstantiation:
2143 return true;
2144
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002145 // It is possible to instantiate TSK_ExplicitSpecialization kind
2146 // if the FunctionDecl has a class scope specialization pattern.
2147 case TSK_ExplicitSpecialization:
2148 return getClassScopeSpecializationPattern() != 0;
2149
Douglas Gregorafca3b42009-10-27 20:53:28 +00002150 case TSK_ExplicitInstantiationDeclaration:
2151 // Handled below.
2152 break;
2153 }
2154
2155 // Find the actual template from which we will instantiate.
2156 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002157 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002158 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002159 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002160
2161 // C++0x [temp.explicit]p9:
2162 // Except for inline functions, other explicit instantiation declarations
2163 // have the effect of suppressing the implicit instantiation of the entity
2164 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002165 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002166 return true;
2167
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002168 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002169}
2170
2171bool FunctionDecl::isTemplateInstantiation() const {
2172 switch (getTemplateSpecializationKind()) {
2173 case TSK_Undeclared:
2174 case TSK_ExplicitSpecialization:
2175 return false;
2176 case TSK_ImplicitInstantiation:
2177 case TSK_ExplicitInstantiationDeclaration:
2178 case TSK_ExplicitInstantiationDefinition:
2179 return true;
2180 }
2181 llvm_unreachable("All TSK values handled.");
2182}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002183
2184FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002185 // Handle class scope explicit specialization special case.
2186 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2187 return getClassScopeSpecializationPattern();
2188
Douglas Gregorafca3b42009-10-27 20:53:28 +00002189 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2190 while (Primary->getInstantiatedFromMemberTemplate()) {
2191 // If we have hit a point where the user provided a specialization of
2192 // this template, we're done looking.
2193 if (Primary->isMemberSpecialization())
2194 break;
2195
2196 Primary = Primary->getInstantiatedFromMemberTemplate();
2197 }
2198
2199 return Primary->getTemplatedDecl();
2200 }
2201
2202 return getInstantiatedFromMemberFunction();
2203}
2204
Douglas Gregor70d83e22009-06-29 17:30:29 +00002205FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002206 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002207 = TemplateOrSpecialization
2208 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002209 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002210 }
2211 return 0;
2212}
2213
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002214FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2215 return getASTContext().getClassScopeSpecializationPattern(this);
2216}
2217
Douglas Gregor70d83e22009-06-29 17:30:29 +00002218const TemplateArgumentList *
2219FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002220 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002221 = TemplateOrSpecialization
2222 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002223 return Info->TemplateArguments;
2224 }
2225 return 0;
2226}
2227
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002228const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002229FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2230 if (FunctionTemplateSpecializationInfo *Info
2231 = TemplateOrSpecialization
2232 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2233 return Info->TemplateArgumentsAsWritten;
2234 }
2235 return 0;
2236}
2237
Mike Stump11289f42009-09-09 15:08:12 +00002238void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002239FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2240 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002241 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002242 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002243 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002244 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2245 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002246 assert(TSK != TSK_Undeclared &&
2247 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002248 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002249 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002250 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002251 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2252 TemplateArgs,
2253 TemplateArgsAsWritten,
2254 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002255 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00002256
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002257 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002258 // function template specializations.
2259 if (InsertPos)
Sebastian Redl9ab988f2011-04-14 14:07:59 +00002260 Template->addSpecialization(Info, InsertPos);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002261 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00002262 // Try to insert the new node. If there is an existing node, leave it, the
2263 // set will contain the canonical decls while
2264 // FunctionTemplateDecl::findSpecialization will return
2265 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002266 FunctionTemplateSpecializationInfo *Existing
2267 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00002268 (void)Existing;
2269 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
2270 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002271 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002272}
2273
John McCallb9c78482010-04-08 09:05:18 +00002274void
2275FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2276 const UnresolvedSetImpl &Templates,
2277 const TemplateArgumentListInfo &TemplateArgs) {
2278 assert(TemplateOrSpecialization.isNull());
2279 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2280 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002281 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002282 void *Buffer = Context.Allocate(Size);
2283 DependentFunctionTemplateSpecializationInfo *Info =
2284 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2285 TemplateArgs);
2286 TemplateOrSpecialization = Info;
2287}
2288
2289DependentFunctionTemplateSpecializationInfo::
2290DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2291 const TemplateArgumentListInfo &TArgs)
2292 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2293
2294 d.NumTemplates = Ts.size();
2295 d.NumArgs = TArgs.size();
2296
2297 FunctionTemplateDecl **TsArray =
2298 const_cast<FunctionTemplateDecl**>(getTemplates());
2299 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2300 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2301
2302 TemplateArgumentLoc *ArgsArray =
2303 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2304 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2305 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2306}
2307
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002308TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002309 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002310 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002311 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002312 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002313 if (FTSInfo)
2314 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregord801b062009-10-07 23:56:10 +00002316 MemberSpecializationInfo *MSInfo
2317 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2318 if (MSInfo)
2319 return MSInfo->getTemplateSpecializationKind();
2320
2321 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002322}
2323
Mike Stump11289f42009-09-09 15:08:12 +00002324void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002325FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2326 SourceLocation PointOfInstantiation) {
2327 if (FunctionTemplateSpecializationInfo *FTSInfo
2328 = TemplateOrSpecialization.dyn_cast<
2329 FunctionTemplateSpecializationInfo*>()) {
2330 FTSInfo->setTemplateSpecializationKind(TSK);
2331 if (TSK != TSK_ExplicitSpecialization &&
2332 PointOfInstantiation.isValid() &&
2333 FTSInfo->getPointOfInstantiation().isInvalid())
2334 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2335 } else if (MemberSpecializationInfo *MSInfo
2336 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2337 MSInfo->setTemplateSpecializationKind(TSK);
2338 if (TSK != TSK_ExplicitSpecialization &&
2339 PointOfInstantiation.isValid() &&
2340 MSInfo->getPointOfInstantiation().isInvalid())
2341 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2342 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002343 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002344}
2345
2346SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002347 if (FunctionTemplateSpecializationInfo *FTSInfo
2348 = TemplateOrSpecialization.dyn_cast<
2349 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002350 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002351 else if (MemberSpecializationInfo *MSInfo
2352 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002353 return MSInfo->getPointOfInstantiation();
2354
2355 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002356}
2357
Douglas Gregor6411b922009-09-11 20:15:17 +00002358bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002359 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002360 return true;
2361
2362 // If this function was instantiated from a member function of a
2363 // class template, check whether that member function was defined out-of-line.
2364 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2365 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002366 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002367 return Definition->isOutOfLine();
2368 }
2369
2370 // If this function was instantiated from a function template,
2371 // check whether that function template was defined out-of-line.
2372 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2373 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002374 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002375 return Definition->isOutOfLine();
2376 }
2377
2378 return false;
2379}
2380
Abramo Bagnaraea947882011-03-08 16:41:52 +00002381SourceRange FunctionDecl::getSourceRange() const {
2382 return SourceRange(getOuterLocStart(), EndRangeLoc);
2383}
2384
Anna Zaks28db7ce2012-01-18 02:45:01 +00002385unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002386 IdentifierInfo *FnInfo = getIdentifier();
2387
2388 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002389 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002390
2391 // Builtin handling.
2392 switch (getBuiltinID()) {
2393 case Builtin::BI__builtin_memset:
2394 case Builtin::BI__builtin___memset_chk:
2395 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002396 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002397
2398 case Builtin::BI__builtin_memcpy:
2399 case Builtin::BI__builtin___memcpy_chk:
2400 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002401 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002402
2403 case Builtin::BI__builtin_memmove:
2404 case Builtin::BI__builtin___memmove_chk:
2405 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002406 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002407
2408 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002409 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002410 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002411 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002412
2413 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002414 case Builtin::BImemcmp:
2415 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002416
2417 case Builtin::BI__builtin_strncpy:
2418 case Builtin::BI__builtin___strncpy_chk:
2419 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002420 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002421
2422 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002423 case Builtin::BIstrncmp:
2424 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002425
2426 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002427 case Builtin::BIstrncasecmp:
2428 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002429
2430 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002431 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002432 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002433 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002434
2435 case Builtin::BI__builtin_strndup:
2436 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002437 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002438
Anna Zaks314cd092012-02-01 19:08:57 +00002439 case Builtin::BI__builtin_strlen:
2440 case Builtin::BIstrlen:
2441 return Builtin::BIstrlen;
2442
Anna Zaks201d4892012-01-13 21:52:01 +00002443 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002444 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002445 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002446 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002447 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002448 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002449 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002450 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002451 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002452 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002453 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002454 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002455 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002456 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002457 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002458 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002459 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002460 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002461 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002462 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002463 else if (FnInfo->isStr("strlen"))
2464 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002465 }
2466 break;
2467 }
Anna Zaks22122702012-01-17 00:37:07 +00002468 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002469}
2470
Chris Lattner59a25942008-03-31 00:36:02 +00002471//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002472// FieldDecl Implementation
2473//===----------------------------------------------------------------------===//
2474
Jay Foad39c79802011-01-12 09:06:06 +00002475FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002476 SourceLocation StartLoc, SourceLocation IdLoc,
2477 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002478 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2479 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002480 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002481 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002482}
2483
Douglas Gregor72172e92012-01-05 21:55:30 +00002484FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2485 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2486 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2487 0, QualType(), 0, 0, false, false);
2488}
2489
Sebastian Redl833ef452010-01-26 22:01:41 +00002490bool FieldDecl::isAnonymousStructOrUnion() const {
2491 if (!isImplicit() || getDeclName())
2492 return false;
2493
2494 if (const RecordType *Record = getType()->getAs<RecordType>())
2495 return Record->getDecl()->isAnonymousStructOrUnion();
2496
2497 return false;
2498}
2499
Richard Smithcaf33902011-10-10 18:28:20 +00002500unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2501 assert(isBitField() && "not a bitfield");
2502 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2503 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2504}
2505
John McCall4e819612011-01-20 07:57:12 +00002506unsigned FieldDecl::getFieldIndex() const {
2507 if (CachedFieldIndex) return CachedFieldIndex - 1;
2508
Richard Smithd62306a2011-11-10 06:34:14 +00002509 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002510 const RecordDecl *RD = getParent();
2511 const FieldDecl *LastFD = 0;
2512 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002513
2514 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2515 I != E; ++I, ++Index) {
2516 (*I)->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002517
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002518 if (IsMsStruct) {
2519 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smithd62306a2011-11-10 06:34:14 +00002520 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2521 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002522 continue;
2523 }
Richard Smithd62306a2011-11-10 06:34:14 +00002524 LastFD = (*I);
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002525 }
John McCall4e819612011-01-20 07:57:12 +00002526 }
2527
Richard Smithd62306a2011-11-10 06:34:14 +00002528 assert(CachedFieldIndex && "failed to find field in parent");
2529 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002530}
2531
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002532SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002533 if (const Expr *E = InitializerOrBitWidth.getPointer())
2534 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002535 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002536}
2537
Richard Smith938f40b2011-06-11 17:19:42 +00002538void FieldDecl::setInClassInitializer(Expr *Init) {
2539 assert(!InitializerOrBitWidth.getPointer() &&
2540 "bit width or initializer already set");
2541 InitializerOrBitWidth.setPointer(Init);
2542 InitializerOrBitWidth.setInt(0);
2543}
2544
Sebastian Redl833ef452010-01-26 22:01:41 +00002545//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002546// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002547//===----------------------------------------------------------------------===//
2548
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002549SourceLocation TagDecl::getOuterLocStart() const {
2550 return getTemplateOrInnerLocStart(this);
2551}
2552
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002553SourceRange TagDecl::getSourceRange() const {
2554 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002555 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002556}
2557
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002558TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002559 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002560}
2561
Richard Smithdda56e42011-04-15 14:24:37 +00002562void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2563 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002564 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002565 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002566 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002567}
2568
Douglas Gregordee1be82009-01-17 00:42:38 +00002569void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002570 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002571
2572 if (isa<CXXRecordDecl>(this)) {
2573 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2574 struct CXXRecordDecl::DefinitionData *Data =
2575 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002576 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2577 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002578 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002579}
2580
2581void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002582 assert((!isa<CXXRecordDecl>(this) ||
2583 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2584 "definition completed but not started");
2585
John McCallf937c022011-10-07 06:10:15 +00002586 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002587 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002588
2589 if (ASTMutationListener *L = getASTMutationListener())
2590 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002591}
2592
John McCallf937c022011-10-07 06:10:15 +00002593TagDecl *TagDecl::getDefinition() const {
2594 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002595 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002596 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2597 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002598
2599 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002600 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002601 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002602 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002603
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002604 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002605}
2606
Douglas Gregor14454802011-02-25 02:25:35 +00002607void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2608 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002609 // Make sure the extended qualifier info is allocated.
2610 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002611 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002612 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002613 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002614 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002615 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002616 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002617 if (getExtInfo()->NumTemplParamLists == 0) {
2618 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002619 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002620 }
2621 else
2622 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002623 }
2624 }
2625}
2626
Abramo Bagnara60804e12011-03-18 15:16:37 +00002627void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2628 unsigned NumTPLists,
2629 TemplateParameterList **TPLists) {
2630 assert(NumTPLists > 0);
2631 // Make sure the extended decl info is allocated.
2632 if (!hasExtInfo())
2633 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002634 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002635 // Set the template parameter lists info.
2636 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2637}
2638
Ted Kremenek21475702008-09-05 17:16:31 +00002639//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002640// EnumDecl Implementation
2641//===----------------------------------------------------------------------===//
2642
David Blaikie68e081d2011-12-20 02:48:34 +00002643void EnumDecl::anchor() { }
2644
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002645EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2646 SourceLocation StartLoc, SourceLocation IdLoc,
2647 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002648 EnumDecl *PrevDecl, bool IsScoped,
2649 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002650 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002651 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002652 C.getTypeDeclType(Enum, PrevDecl);
2653 return Enum;
2654}
2655
Douglas Gregor72172e92012-01-05 21:55:30 +00002656EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2657 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2658 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2659 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002660}
2661
Douglas Gregord5058122010-02-11 01:19:42 +00002662void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002663 QualType NewPromotionType,
2664 unsigned NumPositiveBits,
2665 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002666 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002667 if (!IntegerType)
2668 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002669 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002670 setNumPositiveBits(NumPositiveBits);
2671 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002672 TagDecl::completeDefinition();
2673}
2674
2675//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002676// RecordDecl Implementation
2677//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002678
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002679RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2680 SourceLocation StartLoc, SourceLocation IdLoc,
2681 IdentifierInfo *Id, RecordDecl *PrevDecl)
2682 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002683 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002684 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002685 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002686 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002687 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002688}
2689
Jay Foad39c79802011-01-12 09:06:06 +00002690RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002691 SourceLocation StartLoc, SourceLocation IdLoc,
2692 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2693 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2694 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002695 C.getTypeDeclType(R, PrevDecl);
2696 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002697}
2698
Douglas Gregor72172e92012-01-05 21:55:30 +00002699RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2700 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2701 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2702 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002703}
2704
Douglas Gregordfcad112009-03-25 15:59:44 +00002705bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002706 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002707 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2708}
2709
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002710RecordDecl::field_iterator RecordDecl::field_begin() const {
2711 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2712 LoadFieldsFromExternalStorage();
2713
2714 return field_iterator(decl_iterator(FirstDecl));
2715}
2716
Douglas Gregorb11aad82011-02-19 18:51:44 +00002717/// completeDefinition - Notes that the definition of this type is now
2718/// complete.
2719void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002720 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002721 TagDecl::completeDefinition();
2722}
2723
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002724void RecordDecl::LoadFieldsFromExternalStorage() const {
2725 ExternalASTSource *Source = getASTContext().getExternalSource();
2726 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2727
2728 // Notify that we have a RecordDecl doing some initialization.
2729 ExternalASTSource::Deserializing TheFields(Source);
2730
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002731 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002732 LoadedFieldsFromExternalStorage = true;
2733 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2734 case ELR_Success:
2735 break;
2736
2737 case ELR_AlreadyLoaded:
2738 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002739 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002740 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002741
2742#ifndef NDEBUG
2743 // Check that all decls we got were FieldDecls.
2744 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2745 assert(isa<FieldDecl>(Decls[i]));
2746#endif
2747
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002748 if (Decls.empty())
2749 return;
2750
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002751 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2752 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002753}
2754
Steve Naroff415d3d52008-10-08 17:01:13 +00002755//===----------------------------------------------------------------------===//
2756// BlockDecl Implementation
2757//===----------------------------------------------------------------------===//
2758
David Blaikie9c70e042011-09-21 18:16:56 +00002759void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002760 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002761
Steve Naroffc4b30e52009-03-13 16:56:44 +00002762 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002763 if (!NewParamInfo.empty()) {
2764 NumParams = NewParamInfo.size();
2765 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2766 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002767 }
2768}
2769
John McCall351762c2011-02-07 10:33:21 +00002770void BlockDecl::setCaptures(ASTContext &Context,
2771 const Capture *begin,
2772 const Capture *end,
2773 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002774 CapturesCXXThis = capturesCXXThis;
2775
2776 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002777 NumCaptures = 0;
2778 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002779 return;
2780 }
2781
John McCall351762c2011-02-07 10:33:21 +00002782 NumCaptures = end - begin;
2783
2784 // Avoid new Capture[] because we don't want to provide a default
2785 // constructor.
2786 size_t allocationSize = NumCaptures * sizeof(Capture);
2787 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2788 memcpy(buffer, begin, allocationSize);
2789 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002790}
Sebastian Redl833ef452010-01-26 22:01:41 +00002791
John McCallce45f882011-06-15 22:51:16 +00002792bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2793 for (capture_const_iterator
2794 i = capture_begin(), e = capture_end(); i != e; ++i)
2795 // Only auto vars can be captured, so no redeclaration worries.
2796 if (i->getVariable() == variable)
2797 return true;
2798
2799 return false;
2800}
2801
Douglas Gregor70226da2010-12-21 16:27:07 +00002802SourceRange BlockDecl::getSourceRange() const {
2803 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2804}
Sebastian Redl833ef452010-01-26 22:01:41 +00002805
2806//===----------------------------------------------------------------------===//
2807// Other Decl Allocation/Deallocation Method Implementations
2808//===----------------------------------------------------------------------===//
2809
David Blaikie68e081d2011-12-20 02:48:34 +00002810void TranslationUnitDecl::anchor() { }
2811
Sebastian Redl833ef452010-01-26 22:01:41 +00002812TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2813 return new (C) TranslationUnitDecl(C);
2814}
2815
David Blaikie68e081d2011-12-20 02:48:34 +00002816void LabelDecl::anchor() { }
2817
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002818LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002819 SourceLocation IdentL, IdentifierInfo *II) {
2820 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2821}
2822
2823LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2824 SourceLocation IdentL, IdentifierInfo *II,
2825 SourceLocation GnuLabelL) {
2826 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2827 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002828}
2829
Douglas Gregor72172e92012-01-05 21:55:30 +00002830LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2831 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2832 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002833}
2834
David Blaikie68e081d2011-12-20 02:48:34 +00002835void ValueDecl::anchor() { }
2836
2837void ImplicitParamDecl::anchor() { }
2838
Sebastian Redl833ef452010-01-26 22:01:41 +00002839ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002840 SourceLocation IdLoc,
2841 IdentifierInfo *Id,
2842 QualType Type) {
2843 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002844}
2845
Douglas Gregor72172e92012-01-05 21:55:30 +00002846ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2847 unsigned ID) {
2848 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2849 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2850}
2851
Sebastian Redl833ef452010-01-26 22:01:41 +00002852FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002853 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002854 const DeclarationNameInfo &NameInfo,
2855 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002856 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002857 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002858 bool hasWrittenPrototype,
2859 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002860 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2861 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002862 isInlineSpecified,
2863 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002864 New->HasWrittenPrototype = hasWrittenPrototype;
2865 return New;
2866}
2867
Douglas Gregor72172e92012-01-05 21:55:30 +00002868FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2869 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2870 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2871 DeclarationNameInfo(), QualType(), 0,
2872 SC_None, SC_None, false, false);
2873}
2874
Sebastian Redl833ef452010-01-26 22:01:41 +00002875BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2876 return new (C) BlockDecl(DC, L);
2877}
2878
Douglas Gregor72172e92012-01-05 21:55:30 +00002879BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2880 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2881 return new (Mem) BlockDecl(0, SourceLocation());
2882}
2883
Sebastian Redl833ef452010-01-26 22:01:41 +00002884EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2885 SourceLocation L,
2886 IdentifierInfo *Id, QualType T,
2887 Expr *E, const llvm::APSInt &V) {
2888 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2889}
2890
Douglas Gregor72172e92012-01-05 21:55:30 +00002891EnumConstantDecl *
2892EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2893 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2894 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2895 llvm::APSInt());
2896}
2897
David Blaikie68e081d2011-12-20 02:48:34 +00002898void IndirectFieldDecl::anchor() { }
2899
Benjamin Kramer39593702010-11-21 14:11:41 +00002900IndirectFieldDecl *
2901IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2902 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2903 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002904 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2905}
2906
Douglas Gregor72172e92012-01-05 21:55:30 +00002907IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2908 unsigned ID) {
2909 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2910 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2911 QualType(), 0, 0);
2912}
2913
Douglas Gregorbe996932010-09-01 20:41:53 +00002914SourceRange EnumConstantDecl::getSourceRange() const {
2915 SourceLocation End = getLocation();
2916 if (Init)
2917 End = Init->getLocEnd();
2918 return SourceRange(getLocation(), End);
2919}
2920
David Blaikie68e081d2011-12-20 02:48:34 +00002921void TypeDecl::anchor() { }
2922
Sebastian Redl833ef452010-01-26 22:01:41 +00002923TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002924 SourceLocation StartLoc, SourceLocation IdLoc,
2925 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2926 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002927}
2928
David Blaikie68e081d2011-12-20 02:48:34 +00002929void TypedefNameDecl::anchor() { }
2930
Douglas Gregor72172e92012-01-05 21:55:30 +00002931TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2932 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2933 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2934}
2935
Richard Smithdda56e42011-04-15 14:24:37 +00002936TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2937 SourceLocation StartLoc,
2938 SourceLocation IdLoc, IdentifierInfo *Id,
2939 TypeSourceInfo *TInfo) {
2940 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2941}
2942
Douglas Gregor72172e92012-01-05 21:55:30 +00002943TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2944 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2945 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2946}
2947
Abramo Bagnaraea947882011-03-08 16:41:52 +00002948SourceRange TypedefDecl::getSourceRange() const {
2949 SourceLocation RangeEnd = getLocation();
2950 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2951 if (typeIsPostfix(TInfo->getType()))
2952 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2953 }
2954 return SourceRange(getLocStart(), RangeEnd);
2955}
2956
Richard Smithdda56e42011-04-15 14:24:37 +00002957SourceRange TypeAliasDecl::getSourceRange() const {
2958 SourceLocation RangeEnd = getLocStart();
2959 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2960 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2961 return SourceRange(getLocStart(), RangeEnd);
2962}
2963
David Blaikie68e081d2011-12-20 02:48:34 +00002964void FileScopeAsmDecl::anchor() { }
2965
Sebastian Redl833ef452010-01-26 22:01:41 +00002966FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002967 StringLiteral *Str,
2968 SourceLocation AsmLoc,
2969 SourceLocation RParenLoc) {
2970 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002971}
Douglas Gregorba345522011-12-02 23:23:56 +00002972
Douglas Gregor72172e92012-01-05 21:55:30 +00002973FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2974 unsigned ID) {
2975 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2976 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2977}
2978
Douglas Gregorba345522011-12-02 23:23:56 +00002979//===----------------------------------------------------------------------===//
2980// ImportDecl Implementation
2981//===----------------------------------------------------------------------===//
2982
2983/// \brief Retrieve the number of module identifiers needed to name the given
2984/// module.
2985static unsigned getNumModuleIdentifiers(Module *Mod) {
2986 unsigned Result = 1;
2987 while (Mod->Parent) {
2988 Mod = Mod->Parent;
2989 ++Result;
2990 }
2991 return Result;
2992}
2993
Douglas Gregor22d09742012-01-03 18:04:46 +00002994ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002995 Module *Imported,
2996 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00002997 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002998 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002999{
3000 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3001 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3002 memcpy(StoredLocs, IdentifierLocs.data(),
3003 IdentifierLocs.size() * sizeof(SourceLocation));
3004}
3005
Douglas Gregor22d09742012-01-03 18:04:46 +00003006ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003007 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003008 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003009 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003010{
3011 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3012}
3013
3014ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003015 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003016 ArrayRef<SourceLocation> IdentifierLocs) {
3017 void *Mem = C.Allocate(sizeof(ImportDecl) +
3018 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003019 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003020}
3021
3022ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003023 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003024 Module *Imported,
3025 SourceLocation EndLoc) {
3026 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003027 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003028 Import->setImplicit();
3029 return Import;
3030}
3031
Douglas Gregor72172e92012-01-05 21:55:30 +00003032ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3033 unsigned NumLocations) {
3034 void *Mem = AllocateDeserializedDecl(C, ID,
3035 (sizeof(ImportDecl) +
3036 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003037 return new (Mem) ImportDecl(EmptyShell());
3038}
3039
3040ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3041 if (!ImportedAndComplete.getInt())
3042 return ArrayRef<SourceLocation>();
3043
3044 const SourceLocation *StoredLocs
3045 = reinterpret_cast<const SourceLocation *>(this + 1);
3046 return ArrayRef<SourceLocation>(StoredLocs,
3047 getNumModuleIdentifiers(getImportedModule()));
3048}
3049
3050SourceRange ImportDecl::getSourceRange() const {
3051 if (!ImportedAndComplete.getInt())
3052 return SourceRange(getLocation(),
3053 *reinterpret_cast<const SourceLocation *>(this + 1));
3054
3055 return SourceRange(getLocation(), getIdentifierLocs().back());
3056}