blob: 0e2793490d95147cb3efb11c0d10d88726193c05 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000027#include "clang/Basic/Module.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000028#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000029#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000031
David Blaikie4278c652011-09-21 18:16:56 +000032#include <algorithm>
33
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattnerd3b90652008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor4421d2b2011-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 McCall1fb0caa2010-10-22 21:05:15 +000051 }
Douglas Gregor4421d2b2011-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 Gregorbcfd1f52011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-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 McCall1fb0caa2010-10-22 21:05:15 +000065}
66
John McCallaf146032010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallaf146032010-10-30 11:50:40 +000068
Benjamin Kramer752c2e92010-11-05 19:56:37 +000069namespace {
John McCall36987482010-11-02 01:45:15 +000070/// Flags controlling the computation of linkage and visibility.
71struct LVFlags {
72 bool ConsiderGlobalVisibility;
73 bool ConsiderVisibilityAttributes;
John McCall1a0918a2011-03-04 10:39:25 +000074 bool ConsiderTemplateParameterTypes;
John McCall36987482010-11-02 01:45:15 +000075
76 LVFlags() : ConsiderGlobalVisibility(true),
John McCall1a0918a2011-03-04 10:39:25 +000077 ConsiderVisibilityAttributes(true),
78 ConsiderTemplateParameterTypes(true) {
John McCall36987482010-11-02 01:45:15 +000079 }
80
Douglas Gregor381d34e2010-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 McCall1a0918a2011-03-04 10:39:25 +000087 F.ConsiderTemplateParameterTypes = false;
Douglas Gregor381d34e2010-12-06 18:36:25 +000088 return F;
89 }
90
John McCall36987482010-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 McCall1a0918a2011-03-04 10:39:25 +000097 F.ConsiderTemplateParameterTypes = false;
John McCall36987482010-11-02 01:45:15 +000098 return F;
99 }
Douglas Gregor89d63e52010-12-06 18:50:56 +0000100};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000101} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000102
Rafael Espindola093ecc92012-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000108/// \brief Get the most restrictive linkage for the types in the given
109/// template parameter list.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000110static LinkageInfo
John McCall1fb0caa2010-10-22 21:05:15 +0000111getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000112 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000113 for (TemplateParameterList::const_iterator P = Params->begin(),
114 PEnd = Params->end();
115 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-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 Espindola093ecc92012-01-14 00:30:36 +0000121 LV.merge(getLVForType(T));
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000122 }
123 continue;
124 }
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000125
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000126 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000127 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000128 continue;
129 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000130 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000131
132 if (TemplateTemplateParmDecl *TTP
133 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000134 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000135 }
136 }
137
John McCall1fb0caa2010-10-22 21:05:15 +0000138 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000139}
140
Douglas Gregor381d34e2010-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 Gregor0b6bc8b2010-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 Espindola093ecc92012-01-14 00:30:36 +0000146static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
147 unsigned NumArgs,
148 LVFlags &F) {
149 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-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 Espindolab5d763d2012-01-02 06:26:22 +0000157
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000158 case TemplateArgument::Type:
Rafael Espindola093ecc92012-01-14 00:30:36 +0000159 LV.merge(getLVForType(Args[I].getAsType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000160 break;
161
162 case TemplateArgument::Declaration:
John McCall1fb0caa2010-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 Gregor89d63e52010-12-06 18:50:56 +0000166 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
167 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000168 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000169 break;
170
171 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000172 case TemplateArgument::TemplateExpansion:
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000173 if (TemplateDecl *Template
Douglas Gregora7fc9012011-01-05 18:58:31 +0000174 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola093ecc92012-01-14 00:30:36 +0000175 LV.merge(getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000176 break;
177
178 case TemplateArgument::Pack:
Rafael Espindola860097c2012-02-23 04:17:32 +0000179 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
180 Args[I].pack_size(),
181 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000182 break;
183 }
184 }
185
John McCall1fb0caa2010-10-22 21:05:15 +0000186 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000187}
188
Rafael Espindola093ecc92012-01-14 00:30:36 +0000189static LinkageInfo
Douglas Gregor381d34e2010-12-06 18:36:25 +0000190getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
191 LVFlags &F) {
192 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000193}
194
John McCall6ce51ee2011-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 McCall36987482010-11-02 01:45:15 +0000205static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000206 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-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 McCalld931b082010-08-26 03:08:43 +0000218 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000219 return LinkageInfo::internal();
Douglas Gregord85b5b92009-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 Friedmane9d65542009-11-26 03:04:01 +0000226 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000227 Var->getStorageClass() != SC_Extern &&
228 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000229 bool FoundExtern = false;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000230 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000231 PrevVar && !FoundExtern;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000232 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000233 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000234 FoundExtern = true;
235
236 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000237 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000238 }
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000239 if (Var->getStorageClass() == SC_None) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000240 const VarDecl *PrevVar = Var->getPreviousDecl();
241 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000242 if (PrevVar->getStorageClass() == SC_PrivateExtern)
243 break;
244 if (PrevVar)
245 return PrevVar->getLinkageAndVisibility();
246 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000247 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-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 Gregord85b5b92009-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 McCalld931b082010-08-26 03:08:43 +0000259 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000260 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-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 McCallaf146032010-10-30 11:50:40 +0000264 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000265 }
266
Chandler Carruth094b6432011-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 Friedman750dc2b2012-01-15 01:23:58 +0000270 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
271 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth094b6432011-02-24 19:03:39 +0000272 return LinkageInfo::uniqueExternal();
273 }
John McCalle7bc9722010-10-28 04:18:25 +0000274
John McCall1fb0caa2010-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 McCallaf146032010-10-30 11:50:40 +0000281 LinkageInfo LV;
Rafael Espindola860097c2012-02-23 04:17:32 +0000282 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCallaf146032010-10-30 11:50:40 +0000283
John McCall36987482010-11-02 01:45:15 +0000284 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000285 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
286 LV.setVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000287 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-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 Espindola6f26b5e2012-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 Espindola71cb8a22012-01-01 18:06:40 +0000297 LV.setVisibility(*Vis, true);
John McCall90f14502010-12-10 02:59:44 +0000298 F.ConsiderGlobalVisibility = false;
299 break;
300 }
301 }
John McCall36987482010-11-02 01:45:15 +0000302 }
John McCallaf146032010-10-30 11:50:40 +0000303 }
John McCall1fb0caa2010-10-22 21:05:15 +0000304
Douglas Gregord85b5b92009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000306
Douglas Gregord85b5b92009-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 McCall110e8e52010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall1fb0caa2010-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 McCallac65c622010-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 McCall1fb0caa2010-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 Friedman750dc2b2012-01-15 01:23:58 +0000333 if (Context.getLangOptions().CPlusPlus &&
334 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000335 LinkageInfo TypeLV = getLVForType(Var->getType());
336 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000337 return LinkageInfo::uniqueExternal();
Rafael Espindola2f47c362012-03-10 13:01:40 +0000338 LV.mergeVisibilityWithMin(TypeLV.visibility(),
339 TypeLV.visibilityExplicit());
John McCall110e8e52010-10-29 22:22:43 +0000340 }
341
John McCall35cebc32010-11-02 18:38:13 +0000342 if (Var->getStorageClass() == SC_PrivateExtern)
343 LV.setVisibility(HiddenVisibility, true);
344
Douglas Gregord85b5b92009-11-25 22:24:25 +0000345 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000346 (Var->getStorageClass() == SC_Extern ||
347 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000348
Douglas Gregord85b5b92009-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 Gregoref96ee02012-01-14 16:38:05 +0000358 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000359 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000360 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
361 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000362 }
363 }
364
Douglas Gregord85b5b92009-11-25 22:24:25 +0000365 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000366 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-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 McCall1fb0caa2010-10-22 21:05:15 +0000371
John McCall35cebc32010-11-02 18:38:13 +0000372 if (Function->getStorageClass() == SC_PrivateExtern)
373 LV.setVisibility(HiddenVisibility, true);
374
Douglas Gregord85b5b92009-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 McCalld931b082010-08-26 03:08:43 +0000381 (Function->getStorageClass() == SC_Extern ||
382 Function->getStorageClass() == SC_PrivateExtern ||
383 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-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 Gregoref96ee02012-01-14 16:38:05 +0000393 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000394 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000395 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
396 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000397 }
398 }
399
John McCallaf8ca372011-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 Friedman750dc2b2012-01-15 01:23:58 +0000404 if (Context.getLangOptions().CPlusPlus &&
405 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000406 Function->getType()->getLinkage() == UniqueExternalLinkage)
407 return LinkageInfo::uniqueExternal();
408
John McCall6ce51ee2011-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000412 = Function->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-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 Espindola860097c2012-02-23 04:17:32 +0000417 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000418 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000419 }
420
Douglas Gregord85b5b92009-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 McCall1fb0caa2010-10-22 21:05:15 +0000427 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
428 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000429 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000430 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000431
John McCall1fb0caa2010-10-22 21:05:15 +0000432 // If this is a class template specialization, consider the
433 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000434 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000435 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000436 if (shouldConsiderTemplateLV(spec)) {
437 // From the template.
438 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
439 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000440
John McCall6ce51ee2011-06-27 23:06:04 +0000441 // The arguments at which the template was instantiated.
442 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola860097c2012-02-23 04:17:32 +0000443 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F));
John McCall6ce51ee2011-06-27 23:06:04 +0000444 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000445 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000446
John McCallac65c622010-10-26 04:59:26 +0000447 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000448 if (F.ConsiderGlobalVisibility)
449 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000450 (Context.getLangOptions().CPlusPlus &&
451 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000452
Douglas Gregord85b5b92009-11-25 22:24:25 +0000453 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000454 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000455 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000456 if (!isExternalLinkage(EnumLV.linkage()))
457 return LinkageInfo::none();
458 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000459
460 // - a template, unless it is a function template that has
461 // internal linkage (Clause 14);
John McCall1a0918a2011-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000465
Douglas Gregord85b5b92009-11-25 22:24:25 +0000466 // - a namespace (7.3), unless it is declared within an unnamed
467 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000468 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
469 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000470
John McCall1fb0caa2010-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 McCallaf146032010-10-30 11:50:40 +0000478 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000479 }
480
481 // If we ended up with non-external linkage, visibility should
482 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000483 if (LV.linkage() != ExternalLinkage)
484 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000485
John McCall1fb0caa2010-10-22 21:05:15 +0000486 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000487}
488
John McCall36987482010-11-02 01:45:15 +0000489static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-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 McCall3cdfc4d2010-08-13 08:35:10 +0000494 if (!(isa<CXXMethodDecl>(D) ||
495 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000496 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000497 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000498 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000499 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000500
John McCall36987482010-11-02 01:45:15 +0000501 LinkageInfo LV;
Rafael Espindola860097c2012-02-23 04:17:32 +0000502 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall36987482010-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 Gregor4421d2b2011-03-26 12:10:19 +0000509 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
510 LV.mergeVisibility(*Vis, true);
John McCall36987482010-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 McCallaf146032010-10-30 11:50:40 +0000520
521 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000522 // linkage.
523 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
524 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000525 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000526
527 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000528 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000529 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000530
John McCall3cdfc4d2010-08-13 08:35:10 +0000531 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-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 McCall110e8e52010-10-29 22:22:43 +0000537 TemplateSpecializationKind TSK = TSK_Undeclared;
538
John McCall1fb0caa2010-10-22 21:05:15 +0000539 // If this is a method template specialization, use the linkage for
540 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000541 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000542 = MD->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000543 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola860097c2012-02-23 04:17:32 +0000544 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
545 F));
John McCall6ce51ee2011-06-27 23:06:04 +0000546 if (F.ConsiderTemplateParameterTypes)
547 LV.merge(getLVForTemplateParameterList(
548 spec->getTemplate()->getTemplateParameters()));
549 }
John McCall110e8e52010-10-29 22:22:43 +0000550
John McCall6ce51ee2011-06-27 23:06:04 +0000551 TSK = spec->getTemplateSpecializationKind();
John McCall110e8e52010-10-29 22:22:43 +0000552 } else if (MemberSpecializationInfo *MSI =
553 MD->getMemberSpecializationInfo()) {
554 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000555 }
556
John McCall110e8e52010-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 McCallaf146032010-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 Espindolafedb6ec2011-12-27 21:15:28 +0000564 TSK != TSK_ExplicitInstantiationDefinition &&
John McCall36987482010-11-02 01:45:15 +0000565 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-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 McCall1fb0caa2010-10-22 21:05:15 +0000574
John McCall110e8e52010-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 McCall6ce51ee2011-06-27 23:06:04 +0000579 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000580 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000581 if (shouldConsiderTemplateLV(spec)) {
582 // Merge template argument/parameter information for member
583 // class template specializations.
Rafael Espindola860097c2012-02-23 04:17:32 +0000584 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
585 F));
John McCall1a0918a2011-03-04 10:39:25 +0000586 if (F.ConsiderTemplateParameterTypes)
587 LV.merge(getLVForTemplateParameterList(
John McCall6ce51ee2011-06-27 23:06:04 +0000588 spec->getSpecializedTemplate()->getTemplateParameters()));
589 }
John McCall110e8e52010-10-29 22:22:43 +0000590 }
591
John McCall110e8e52010-10-29 22:22:43 +0000592 // Static data members.
593 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-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 Espindola093ecc92012-01-14 00:30:36 +0000596 LinkageInfo TypeLV = getLVForType(VD->getType());
597 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000598 LV.mergeLinkage(UniqueExternalLinkage);
599 if (!LV.visibilityExplicit())
Rafael Espindola093ecc92012-01-14 00:30:36 +0000600 LV.mergeVisibility(TypeLV.visibility(), TypeLV.visibilityExplicit());
John McCall110e8e52010-10-29 22:22:43 +0000601 }
602
John McCall1fb0caa2010-10-22 21:05:15 +0000603 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000604}
605
John McCallf76b0922011-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 Blaikie99ba9e32011-12-20 02:48:34 +0000615void NamedDecl::anchor() { }
616
John McCallf76b0922011-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 McCall15e310a2011-02-19 02:53:41 +0000629 if (ClassTemplateDecl *temp =
630 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-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 McCall15e310a2011-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 McCallf76b0922011-02-08 19:01:05 +0000640 }
John McCall15e310a2011-02-19 02:53:41 +0000641
642 // Clear cached linkage for function template decls, too.
643 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000644 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
645 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-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 McCall78951942011-03-22 06:58:49 +0000649 }
John McCall15e310a2011-02-19 02:53:41 +0000650
John McCallf76b0922011-02-08 19:01:05 +0000651}
652
Douglas Gregor381d34e2010-12-06 18:36:25 +0000653Linkage NamedDecl::getLinkage() const {
654 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000655 assert(Linkage(CachedLinkage) ==
656 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-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 McCallaf146032010-10-30 11:50:40 +0000666LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000667 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000668 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000669 HasCachedLinkage = 1;
670 CachedLinkage = LI.linkage();
671 return LI;
John McCall0df95872010-10-29 00:29:13 +0000672}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000673
Douglas Gregor4421d2b2011-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 Gregoref96ee02012-01-14 16:38:05 +0000677 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor4421d2b2011-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 Gregoref96ee02012-01-14 16:38:05 +0000683 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor4421d2b2011-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 Espindola860097c2012-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 Gregor4421d2b2011-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 Espindola860097c2012-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 Gregor4421d2b2011-03-26 12:10:19 +0000720 return llvm::Optional<Visibility>();
721}
722
John McCall36987482010-11-02 01:45:15 +0000723static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000724 // Objective-C: treat all Objective-C declarations as having external
725 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000726 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000727 default:
728 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000729 case Decl::ParmVar:
730 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000731 case Decl::TemplateTemplateParm: // count these as external
732 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000733 case Decl::ObjCAtDefsField:
734 case Decl::ObjCCategory:
735 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000736 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000737 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000738 case Decl::ObjCMethod:
739 case Decl::ObjCProperty:
740 case Decl::ObjCPropertyImpl:
741 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000742 return LinkageInfo::external();
Douglas Gregor5878cbc2012-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 Kremenekbecc3082010-04-20 23:15:35 +0000769 }
770
Douglas Gregord85b5b92009-11-25 22:24:25 +0000771 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000772 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000773 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-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 McCall0df95872010-10-29 00:29:13 +0000782 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000783 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-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 McCall0df95872010-10-29 00:29:13 +0000796 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
797 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000798 if (Function->isInAnonymousNamespace() &&
799 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000800 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000801
John McCallaf146032010-10-30 11:50:40 +0000802 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000803 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000804 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
805 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000806 }
807
Douglas Gregoref96ee02012-01-14 16:38:05 +0000808 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000809 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000810 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
811 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000812 }
813
814 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000815 }
816
John McCall0df95872010-10-29 00:29:13 +0000817 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000818 if (Var->getStorageClass() == SC_Extern ||
819 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000820 if (Var->isInAnonymousNamespace() &&
821 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000822 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000823
John McCallaf146032010-10-30 11:50:40 +0000824 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000825 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000826 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000827 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000828 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
829 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000830 }
831
Douglas Gregoref96ee02012-01-14 16:38:05 +0000832 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000833 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000834 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
835 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000836 }
837
838 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000839 }
840 }
841
842 // C++ [basic.link]p6:
843 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000844 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000845}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000846
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000847std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000848 return getQualifiedNameAsString(getASTContext().getLangOptions());
849}
850
851std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000852 const DeclContext *Ctx = getDeclContext();
853
854 if (Ctx->isFunctionOrMethod())
855 return getNameAsString();
856
Chris Lattner5f9e2722011-07-23 10:55:15 +0000857 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-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 Stump1eb44332009-09-09 15:08:12 +0000871 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000872 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000873 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
874 std::string TemplateArgsStr
875 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000876 TemplateArgs.data(),
877 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000878 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000879 OS << Spec->getName() << TemplateArgsStr;
880 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000881 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000882 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000883 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000884 OS << *ND;
Benjamin Kramer68eebbb2010-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 Kramerb8989f22011-10-14 18:45:37 +0000889 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000890 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-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 Kramerb8989f22011-10-14 18:45:37 +0000895 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000896 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000897 unsigned NumParams = FD->getNumParams();
898 for (unsigned i = 0; i < NumParams; ++i) {
899 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000900 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000901 std::string Param;
902 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000903 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000904 }
905
906 if (FT->isVariadic()) {
907 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000908 OS << ", ";
909 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000910 }
911 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000912 OS << ')';
913 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000914 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000915 }
916 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000917 }
918
John McCall8472af42010-03-16 21:48:18 +0000919 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000920 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000921 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000922 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000923
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000924 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000925}
926
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000927bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000928 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
929
Douglas Gregor2a3009a2009-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 Gregordb992412011-02-25 16:33:46 +0000933 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
934 ->getOriginalNamespace() ==
935 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
936 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000939 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
940 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000941 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000942
Douglas Gregore53060f2009-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 Stump1eb44332009-09-09 15:08:12 +0000950
Steve Naroff0de21fd2009-02-22 19:35:57 +0000951 // For method declarations, we keep track of redeclarations.
952 if (isa<ObjCMethodDecl>(this))
953 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
John McCallf36e02d2009-10-09 21:13:30 +0000955 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
956 return true;
957
John McCall9488ea12009-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 Gregordc355712011-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 Kyrtzidisc80117e2010-11-04 08:48:52 +0000969
Douglas Gregor7a537402012-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 Gregor6ed40e32008-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 Gregord6f7e9d2009-02-24 20:03:32 +0000982bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000983 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000984}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000985
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000986NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000987 NamedDecl *ND = this;
Benjamin Kramer56757e92012-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 Carlssone136e0e2009-06-26 06:29:23 +0000995}
996
John McCall161755a2010-04-06 21:38:20 +0000997bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000998 if (!isCXXClassMember())
999 return false;
1000
John McCall161755a2010-04-06 21:38:20 +00001001 const NamedDecl *D = this;
1002 if (isa<UsingShadowDecl>(D))
1003 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1004
Francois Pichet87c2e122010-11-21 06:08:52 +00001005 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-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 Kyrtzidis52393042008-11-09 23:41:00 +00001015//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001016// DeclaratorDecl Implementation
1017//===----------------------------------------------------------------------===//
1018
Douglas Gregor1693e152010-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 Kyrtzidisa5d82002009-08-21 00:31:54 +00001027SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001028 TypeSourceInfo *TSI = getTypeSourceInfo();
1029 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001030 return SourceLocation();
1031}
1032
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001033void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1034 if (QualifierLoc) {
John McCallb6217662010-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 Gregorc22b5ff2011-02-25 02:25:35 +00001045 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001046 } else {
John McCallb6217662010-03-15 10:12:16 +00001047 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001048 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-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 McCallb6217662010-03-15 10:12:16 +00001059 }
1060 }
1061}
1062
Abramo Bagnara7f0a9152011-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 Gregor1693e152010-07-06 18:42:40 +00001081SourceLocation DeclaratorDecl::getOuterLocStart() const {
1082 return getTemplateOrInnerLocStart(this);
1083}
1084
Abramo Bagnaraa2026c92011-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 Bagnara9b934882010-06-12 08:15:14 +00001134void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001135QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1136 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001137 TemplateParameterList **TPLists) {
1138 assert((NumTPLists == 0 || TPLists != 0) &&
1139 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001140
1141 // Free previous template parameters (if any).
1142 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001143 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-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 Gregorc722ea42010-06-15 17:44:38 +00001149 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001150 NumTemplParamLists = NumTPLists;
1151 for (unsigned i = NumTPLists; i-- > 0; )
1152 TemplParamLists[i] = TPLists[i];
1153 }
1154}
1155
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001156//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001157// VarDecl Implementation
1158//===----------------------------------------------------------------------===//
1159
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001160const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1161 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001162 case SC_None: break;
Peter Collingbourne8be0c742011-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 Redl7783bfc2010-01-26 22:01:41 +00001169 }
1170
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001171 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001172}
1173
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001174VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1175 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001176 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001177 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001178 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001179}
1180
Douglas Gregor1e68ecc2012-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 Gregor381d34e2010-12-06 18:36:25 +00001187void VarDecl::setStorageClass(StorageClass SC) {
1188 assert(isLegalForVariable(SC));
1189 if (getStorageClass() != SC)
1190 ClearLinkageCache();
1191
John McCallf1e4fbf2011-05-01 02:13:58 +00001192 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001193}
1194
Douglas Gregor1693e152010-07-06 18:42:40 +00001195SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001196 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001197 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001198 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001199}
1200
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001201bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001202 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001203 return false;
1204
Eli Friedman750dc2b2012-01-15 01:23:58 +00001205 const DeclContext *DC = getDeclContext();
1206 if (DC->isRecord())
1207 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001208
Eli Friedman750dc2b2012-01-15 01:23:58 +00001209 ASTContext &Context = getASTContext();
1210 if (!Context.getLangOptions().CPlusPlus)
1211 return true;
1212 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001213}
1214
1215VarDecl *VarDecl::getCanonicalDecl() {
1216 return getFirstDeclaration();
1217}
1218
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001219VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1220 ASTContext &C) const
1221{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001222 // C++ [basic.def]p2:
1223 // A declaration is a definition unless [...] it contains the 'extern'
1224 // specifier or a linkage-specification and neither an initializer [...],
1225 // it declares a static data member in a class declaration [...].
1226 // C++ [temp.expl.spec]p15:
1227 // An explicit specialization of a static data member of a template is a
1228 // definition if the declaration includes an initializer; otherwise, it is
1229 // a declaration.
1230 if (isStaticDataMember()) {
1231 if (isOutOfLine() && (hasInit() ||
1232 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1233 return Definition;
1234 else
1235 return DeclarationOnly;
1236 }
1237 // C99 6.7p5:
1238 // A definition of an identifier is a declaration for that identifier that
1239 // [...] causes storage to be reserved for that object.
1240 // Note: that applies for all non-file-scope objects.
1241 // C99 6.9.2p1:
1242 // If the declaration of an identifier for an object has file scope and an
1243 // initializer, the declaration is an external definition for the identifier
1244 if (hasInit())
1245 return Definition;
1246 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1247 if (hasExternalStorage())
1248 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001249
John McCalld931b082010-08-26 03:08:43 +00001250 if (getStorageClassAsWritten() == SC_Extern ||
1251 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001252 for (const VarDecl *PrevVar = getPreviousDecl();
1253 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001254 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1255 return DeclarationOnly;
1256 }
1257 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001258 // C99 6.9.2p2:
1259 // A declaration of an object that has file scope without an initializer,
1260 // and without a storage class specifier or the scs 'static', constitutes
1261 // a tentative definition.
1262 // No such thing in C++.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001263 if (!C.getLangOptions().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001264 return TentativeDefinition;
1265
1266 // What's left is (in C, block-scope) declarations without initializers or
1267 // external storage. These are definitions.
1268 return Definition;
1269}
1270
Sebastian Redle9d12b62010-01-31 22:27:38 +00001271VarDecl *VarDecl::getActingDefinition() {
1272 DefinitionKind Kind = isThisDeclarationADefinition();
1273 if (Kind != TentativeDefinition)
1274 return 0;
1275
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001276 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001277 VarDecl *First = getFirstDeclaration();
1278 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1279 I != E; ++I) {
1280 Kind = (*I)->isThisDeclarationADefinition();
1281 if (Kind == Definition)
1282 return 0;
1283 else if (Kind == TentativeDefinition)
1284 LastTentative = *I;
1285 }
1286 return LastTentative;
1287}
1288
1289bool VarDecl::isTentativeDefinitionNow() const {
1290 DefinitionKind Kind = isThisDeclarationADefinition();
1291 if (Kind != TentativeDefinition)
1292 return false;
1293
1294 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1295 if ((*I)->isThisDeclarationADefinition() == Definition)
1296 return false;
1297 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001298 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001299}
1300
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001301VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001302 VarDecl *First = getFirstDeclaration();
1303 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1304 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001305 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001306 return *I;
1307 }
1308 return 0;
1309}
1310
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001311VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001312 DefinitionKind Kind = DeclarationOnly;
1313
1314 const VarDecl *First = getFirstDeclaration();
1315 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001316 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001317 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001318 if (Kind == Definition)
1319 break;
1320 }
John McCall110e8e52010-10-29 22:22:43 +00001321
1322 return Kind;
1323}
1324
Sebastian Redl31310a22010-02-01 20:16:42 +00001325const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001326 redecl_iterator I = redecls_begin(), E = redecls_end();
1327 while (I != E && !I->getInit())
1328 ++I;
1329
1330 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001331 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001332 return I->getInit();
1333 }
1334 return 0;
1335}
1336
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001337bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001338 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001339 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001340
1341 if (!isStaticDataMember())
1342 return false;
1343
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001344 // If this static data member was instantiated from a static data member of
1345 // a class template, check whether that static data member was defined
1346 // out-of-line.
1347 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1348 return VD->isOutOfLine();
1349
1350 return false;
1351}
1352
Douglas Gregor0d035142009-10-27 18:42:08 +00001353VarDecl *VarDecl::getOutOfLineDefinition() {
1354 if (!isStaticDataMember())
1355 return 0;
1356
1357 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1358 RD != RDEnd; ++RD) {
1359 if (RD->getLexicalDeclContext()->isFileContext())
1360 return *RD;
1361 }
1362
1363 return 0;
1364}
1365
Douglas Gregor838db382010-02-11 01:19:42 +00001366void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001367 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1368 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001369 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001370 }
1371
1372 Init = I;
1373}
1374
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001375bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
1376 const LangOptions &Lang = C.getLangOptions();
Richard Smith1d238ea2011-12-21 02:55:12 +00001377
Richard Smith16581332012-03-02 04:14:40 +00001378 if (!Lang.CPlusPlus)
1379 return false;
1380
1381 // In C++11, any variable of reference type can be used in a constant
1382 // expression if it is initialized by a constant expression.
1383 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1384 return true;
1385
1386 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001387 // not require the variable to be non-volatile, but we consider this to be a
1388 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001389 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001390 return false;
1391
1392 // In C++, const, non-volatile variables of integral or enumeration types
1393 // can be used in constant expressions.
1394 if (getType()->isIntegralOrEnumerationType())
1395 return true;
1396
Richard Smith16581332012-03-02 04:14:40 +00001397 // Additionally, in C++11, non-volatile constexpr variables can be used in
1398 // constant expressions.
1399 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001400}
1401
Richard Smith099e7f62011-12-19 06:19:21 +00001402/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1403/// form, which contains extra information on the evaluated value of the
1404/// initializer.
1405EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1406 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1407 if (!Eval) {
1408 Stmt *S = Init.get<Stmt *>();
1409 Eval = new (getASTContext()) EvaluatedStmt;
1410 Eval->Value = S;
1411 Init = Eval;
1412 }
1413 return Eval;
1414}
1415
Richard Smith2d6a5672012-01-14 04:30:29 +00001416APValue *VarDecl::evaluateValue() const {
1417 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1418 return evaluateValue(Notes);
1419}
1420
1421APValue *VarDecl::evaluateValue(
1422 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001423 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1424
1425 // We only produce notes indicating why an initializer is non-constant the
1426 // first time it is evaluated. FIXME: The notes won't always be emitted the
1427 // first time we try evaluation, so might not be produced at all.
1428 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001429 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001430
1431 const Expr *Init = cast<Expr>(Eval->Value);
1432 assert(!Init->isValueDependent());
1433
1434 if (Eval->IsEvaluating) {
1435 // FIXME: Produce a diagnostic for self-initialization.
1436 Eval->CheckedICE = true;
1437 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001438 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001439 }
1440
1441 Eval->IsEvaluating = true;
1442
1443 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1444 this, Notes);
1445
1446 // Ensure the result is an uninitialized APValue if evaluation fails.
1447 if (!Result)
1448 Eval->Evaluated = APValue();
1449
1450 Eval->IsEvaluating = false;
1451 Eval->WasEvaluated = true;
1452
1453 // In C++11, we have determined whether the initializer was a constant
1454 // expression as a side-effect.
1455 if (getASTContext().getLangOptions().CPlusPlus0x && !Eval->CheckedICE) {
1456 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001457 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001458 }
1459
Richard Smith2d6a5672012-01-14 04:30:29 +00001460 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001461}
1462
1463bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001464 // Initializers of weak variables are never ICEs.
1465 if (isWeak())
1466 return false;
1467
Richard Smith099e7f62011-12-19 06:19:21 +00001468 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1469 if (Eval->CheckedICE)
1470 // We have already checked whether this subexpression is an
1471 // integral constant expression.
1472 return Eval->IsICE;
1473
1474 const Expr *Init = cast<Expr>(Eval->Value);
1475 assert(!Init->isValueDependent());
1476
1477 // In C++11, evaluate the initializer to check whether it's a constant
1478 // expression.
1479 if (getASTContext().getLangOptions().CPlusPlus0x) {
1480 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1481 evaluateValue(Notes);
1482 return Eval->IsICE;
1483 }
1484
1485 // It's an ICE whether or not the definition we found is
1486 // out-of-line. See DR 721 and the discussion in Clang PR
1487 // 6206 for details.
1488
1489 if (Eval->CheckingICE)
1490 return false;
1491 Eval->CheckingICE = true;
1492
1493 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1494 Eval->CheckingICE = false;
1495 Eval->CheckedICE = true;
1496 return Eval->IsICE;
1497}
1498
Douglas Gregor03e80032011-06-21 17:03:29 +00001499bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001500 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001501
1502 const Expr *E = getInit();
1503 if (!E)
1504 return false;
1505
1506 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1507 E = Cleanups->getSubExpr();
1508
1509 return isa<MaterializeTemporaryExpr>(E);
1510}
1511
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001512VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001513 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001514 return cast<VarDecl>(MSI->getInstantiatedFrom());
1515
1516 return 0;
1517}
1518
Douglas Gregor663b5a02009-10-14 20:14:33 +00001519TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001520 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001521 return MSI->getTemplateSpecializationKind();
1522
1523 return TSK_Undeclared;
1524}
1525
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001526MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001527 return getASTContext().getInstantiatedFromStaticDataMember(this);
1528}
1529
Douglas Gregor0a897e32009-10-15 17:21:20 +00001530void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1531 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001532 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001533 assert(MSI && "Not an instantiated static data member?");
1534 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001535 if (TSK != TSK_ExplicitSpecialization &&
1536 PointOfInstantiation.isValid() &&
1537 MSI->getPointOfInstantiation().isInvalid())
1538 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001539}
1540
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001541//===----------------------------------------------------------------------===//
1542// ParmVarDecl Implementation
1543//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001544
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001545ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001546 SourceLocation StartLoc,
1547 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001548 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001549 StorageClass S, StorageClass SCAsWritten,
1550 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001551 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001552 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001553}
1554
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001555ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1556 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1557 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1558 0, QualType(), 0, SC_None, SC_None, 0);
1559}
1560
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001561SourceRange ParmVarDecl::getSourceRange() const {
1562 if (!hasInheritedDefaultArg()) {
1563 SourceRange ArgRange = getDefaultArgRange();
1564 if (ArgRange.isValid())
1565 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1566 }
1567
1568 return DeclaratorDecl::getSourceRange();
1569}
1570
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001571Expr *ParmVarDecl::getDefaultArg() {
1572 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1573 assert(!hasUninstantiatedDefaultArg() &&
1574 "Default argument is not yet instantiated!");
1575
1576 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001577 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001578 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001579
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001580 return Arg;
1581}
1582
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001583SourceRange ParmVarDecl::getDefaultArgRange() const {
1584 if (const Expr *E = getInit())
1585 return E->getSourceRange();
1586
1587 if (hasUninstantiatedDefaultArg())
1588 return getUninstantiatedDefaultArg()->getSourceRange();
1589
1590 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001591}
1592
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001593bool ParmVarDecl::isParameterPack() const {
1594 return isa<PackExpansionType>(getType());
1595}
1596
Ted Kremenekd211cb72011-10-06 05:00:56 +00001597void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1598 getASTContext().setParameterIndex(this, parameterIndex);
1599 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1600}
1601
1602unsigned ParmVarDecl::getParameterIndexLarge() const {
1603 return getASTContext().getParameterIndex(this);
1604}
1605
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001606//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001607// FunctionDecl Implementation
1608//===----------------------------------------------------------------------===//
1609
Douglas Gregorda2142f2011-02-19 18:51:44 +00001610void FunctionDecl::getNameForDiagnostic(std::string &S,
1611 const PrintingPolicy &Policy,
1612 bool Qualified) const {
1613 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1614 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1615 if (TemplateArgs)
1616 S += TemplateSpecializationType::PrintTemplateArgumentList(
1617 TemplateArgs->data(),
1618 TemplateArgs->size(),
1619 Policy);
1620
1621}
1622
Ted Kremenek9498d382010-04-29 16:49:01 +00001623bool FunctionDecl::isVariadic() const {
1624 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1625 return FT->isVariadic();
1626 return false;
1627}
1628
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001629bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1630 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001631 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001632 Definition = *I;
1633 return true;
1634 }
1635 }
1636
1637 return false;
1638}
1639
Anders Carlssonffb945f2011-05-14 23:26:09 +00001640bool FunctionDecl::hasTrivialBody() const
1641{
1642 Stmt *S = getBody();
1643 if (!S) {
1644 // Since we don't have a body for this function, we don't know if it's
1645 // trivial or not.
1646 return false;
1647 }
1648
1649 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1650 return true;
1651 return false;
1652}
1653
Sean Hunt10620eb2011-05-06 20:44:56 +00001654bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1655 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001656 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001657 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1658 return true;
1659 }
1660 }
1661
1662 return false;
1663}
1664
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001665Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001666 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1667 if (I->Body) {
1668 Definition = *I;
1669 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001670 } else if (I->IsLateTemplateParsed) {
1671 Definition = *I;
1672 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001673 }
1674 }
1675
1676 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001677}
1678
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001679void FunctionDecl::setBody(Stmt *B) {
1680 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001681 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001682 EndRangeLoc = B->getLocEnd();
1683}
1684
Douglas Gregor21386642010-09-28 21:55:22 +00001685void FunctionDecl::setPure(bool P) {
1686 IsPure = P;
1687 if (P)
1688 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1689 Parent->markedVirtualFunctionPure();
1690}
1691
Douglas Gregor48a83b52009-09-12 00:17:51 +00001692bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001693 const TranslationUnitDecl *tunit =
1694 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1695 return tunit &&
Sean Hunt55878032011-05-15 20:59:31 +00001696 !tunit->getASTContext().getLangOptions().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001697 getIdentifier() &&
1698 getIdentifier()->isStr("main");
1699}
1700
1701bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1702 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1703 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1704 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1705 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1706 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1707
1708 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1709 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1710
1711 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1712 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1713
1714 ASTContext &Context =
1715 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1716 ->getASTContext();
1717
1718 // The result type and first argument type are constant across all
1719 // these operators. The second argument must be exactly void*.
1720 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001721}
1722
Douglas Gregor48a83b52009-09-12 00:17:51 +00001723bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001724 if (getLinkage() != ExternalLinkage)
1725 return false;
1726
1727 if (getAttr<OverloadableAttr>())
1728 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001729
Chandler Carruth10aad442011-02-25 00:05:02 +00001730 const DeclContext *DC = getDeclContext();
1731 if (DC->isRecord())
1732 return false;
1733
Eli Friedman750dc2b2012-01-15 01:23:58 +00001734 ASTContext &Context = getASTContext();
1735 if (!Context.getLangOptions().CPlusPlus)
1736 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001737
Eli Friedman750dc2b2012-01-15 01:23:58 +00001738 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001739}
1740
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001741bool FunctionDecl::isGlobal() const {
1742 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1743 return Method->isStatic();
1744
John McCalld931b082010-08-26 03:08:43 +00001745 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001746 return false;
1747
Mike Stump1eb44332009-09-09 15:08:12 +00001748 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001749 DC->isNamespace();
1750 DC = DC->getParent()) {
1751 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1752 if (!Namespace->getDeclName())
1753 return false;
1754 break;
1755 }
1756 }
1757
1758 return true;
1759}
1760
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001761void
1762FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1763 redeclarable_base::setPreviousDeclaration(PrevDecl);
1764
1765 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1766 FunctionTemplateDecl *PrevFunTmpl
1767 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1768 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1769 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1770 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001771
Axel Naumannd9d137e2011-11-08 18:21:06 +00001772 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001773 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001774}
1775
1776const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1777 return getFirstDeclaration();
1778}
1779
1780FunctionDecl *FunctionDecl::getCanonicalDecl() {
1781 return getFirstDeclaration();
1782}
1783
Douglas Gregor381d34e2010-12-06 18:36:25 +00001784void FunctionDecl::setStorageClass(StorageClass SC) {
1785 assert(isLegalForFunction(SC));
1786 if (getStorageClass() != SC)
1787 ClearLinkageCache();
1788
1789 SClass = SC;
1790}
1791
Douglas Gregor3e41d602009-02-13 23:20:09 +00001792/// \brief Returns a value indicating whether this function
1793/// corresponds to a builtin function.
1794///
1795/// The function corresponds to a built-in function if it is
1796/// declared at translation scope or within an extern "C" block and
1797/// its name matches with the name of a builtin. The returned value
1798/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001799/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001800/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001801unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001802 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001803 return 0;
1804
1805 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001806 if (!BuiltinID)
1807 return 0;
1808
1809 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001810 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1811 return BuiltinID;
1812
1813 // This function has the name of a known C library
1814 // function. Determine whether it actually refers to the C library
1815 // function or whether it just has the same name.
1816
Douglas Gregor9add3172009-02-17 03:23:10 +00001817 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001818 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001819 return 0;
1820
Douglas Gregor3c385e52009-02-14 18:57:46 +00001821 // If this function is at translation-unit scope and we're not in
1822 // C++, it refers to the C library function.
1823 if (!Context.getLangOptions().CPlusPlus &&
1824 getDeclContext()->isTranslationUnit())
1825 return BuiltinID;
1826
1827 // If the function is in an extern "C" linkage specification and is
1828 // not marked "overloadable", it's the real function.
1829 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001830 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001831 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001832 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001833 return BuiltinID;
1834
1835 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001836 return 0;
1837}
1838
1839
Chris Lattner1ad9b282009-04-25 06:03:53 +00001840/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001841/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001842/// after it has been created.
1843unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001844 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001845 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001846 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001847 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Reid Spencer5f016e22007-07-11 17:01:13 +00001849}
1850
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001851void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001852 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001854 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001857 if (!NewParamInfo.empty()) {
1858 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1859 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 }
1861}
1862
James Molloy16f1f712012-02-29 10:24:19 +00001863void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1864 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1865
1866 if (!NewDecls.empty()) {
1867 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1868 std::copy(NewDecls.begin(), NewDecls.end(), A);
1869 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1870 }
1871}
1872
Chris Lattner8123a952008-04-10 02:22:51 +00001873/// getMinRequiredArguments - Returns the minimum number of arguments
1874/// needed to call this function. This may be fewer than the number of
1875/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001876/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001877unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001878 if (!getASTContext().getLangOptions().CPlusPlus)
1879 return getNumParams();
1880
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001881 unsigned NumRequiredArgs = getNumParams();
1882
1883 // If the last parameter is a parameter pack, we don't need an argument for
1884 // it.
1885 if (NumRequiredArgs > 0 &&
1886 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1887 --NumRequiredArgs;
1888
1889 // If this parameter has a default argument, we don't need an argument for
1890 // it.
1891 while (NumRequiredArgs > 0 &&
1892 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001893 --NumRequiredArgs;
1894
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001895 // We might have parameter packs before the end. These can't be deduced,
1896 // but they can still handle multiple arguments.
1897 unsigned ArgIdx = NumRequiredArgs;
1898 while (ArgIdx > 0) {
1899 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1900 NumRequiredArgs = ArgIdx;
1901
1902 --ArgIdx;
1903 }
1904
Chris Lattner8123a952008-04-10 02:22:51 +00001905 return NumRequiredArgs;
1906}
1907
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001908bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001909 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001910 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001911
1912 if (isa<CXXMethodDecl>(this)) {
1913 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1914 return true;
1915 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001916
1917 switch (getTemplateSpecializationKind()) {
1918 case TSK_Undeclared:
1919 case TSK_ExplicitSpecialization:
1920 return false;
1921
1922 case TSK_ImplicitInstantiation:
1923 case TSK_ExplicitInstantiationDeclaration:
1924 case TSK_ExplicitInstantiationDefinition:
1925 // Handle below.
1926 break;
1927 }
1928
1929 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001930 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001931 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001932 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001933
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001934 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001935 return PatternDecl->isInlined();
1936
1937 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001938}
1939
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001940static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1941 // Only consider file-scope declarations in this test.
1942 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1943 return false;
1944
1945 // Only consider explicit declarations; the presence of a builtin for a
1946 // libcall shouldn't affect whether a definition is externally visible.
1947 if (Redecl->isImplicit())
1948 return false;
1949
1950 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1951 return true; // Not an inline definition
1952
1953 return false;
1954}
1955
Nick Lewyckydce67a72011-07-18 05:26:13 +00001956/// \brief For a function declaration in C or C++, determine whether this
1957/// declaration causes the definition to be externally visible.
1958///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001959/// Specifically, this determines if adding the current declaration to the set
1960/// of redeclarations of the given functions causes
1961/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001962bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1963 assert(!doesThisDeclarationHaveABody() &&
1964 "Must have a declaration without a body.");
1965
1966 ASTContext &Context = getASTContext();
1967
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001968 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
1969 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1970 // an externally visible definition.
1971 //
1972 // FIXME: What happens if gnu_inline gets added on after the first
1973 // declaration?
1974 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1975 return false;
1976
1977 const FunctionDecl *Prev = this;
1978 bool FoundBody = false;
1979 while ((Prev = Prev->getPreviousDecl())) {
1980 FoundBody |= Prev->Body;
1981
1982 if (Prev->Body) {
1983 // If it's not the case that both 'inline' and 'extern' are
1984 // specified on the definition, then it is always externally visible.
1985 if (!Prev->isInlineSpecified() ||
1986 Prev->getStorageClassAsWritten() != SC_Extern)
1987 return false;
1988 } else if (Prev->isInlineSpecified() &&
1989 Prev->getStorageClassAsWritten() != SC_Extern) {
1990 return false;
1991 }
1992 }
1993 return FoundBody;
1994 }
1995
1996 if (Context.getLangOptions().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001997 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001998
1999 // C99 6.7.4p6:
2000 // [...] If all of the file scope declarations for a function in a
2001 // translation unit include the inline function specifier without extern,
2002 // then the definition in that translation unit is an inline definition.
2003 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002004 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002005 const FunctionDecl *Prev = this;
2006 bool FoundBody = false;
2007 while ((Prev = Prev->getPreviousDecl())) {
2008 FoundBody |= Prev->Body;
2009 if (RedeclForcesDefC99(Prev))
2010 return false;
2011 }
2012 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00002013}
2014
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002015/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002016/// definition will be externally visible.
2017///
2018/// Inline function definitions are always available for inlining optimizations.
2019/// However, depending on the language dialect, declaration specifiers, and
2020/// attributes, the definition of an inline function may or may not be
2021/// "externally" visible to other translation units in the program.
2022///
2023/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002024/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002025/// inline definition becomes externally visible (C99 6.7.4p6).
2026///
2027/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2028/// definition, we use the GNU semantics for inline, which are nearly the
2029/// opposite of C99 semantics. In particular, "inline" by itself will create
2030/// an externally visible symbol, but "extern inline" will not create an
2031/// externally visible symbol.
2032bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002033 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002034 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002035 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002036
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00002037 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002038 // Note: If you change the logic here, please change
2039 // doesDeclarationForceExternallyVisibleDefinition as well.
2040 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002041 // If it's not the case that both 'inline' and 'extern' are
2042 // specified on the definition, then this inline definition is
2043 // externally visible.
2044 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2045 return true;
2046
2047 // If any declaration is 'inline' but not 'extern', then this definition
2048 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002049 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2050 Redecl != RedeclEnd;
2051 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002052 if (Redecl->isInlineSpecified() &&
2053 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002054 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002055 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002056
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002057 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002058 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002059
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002060 // C99 6.7.4p6:
2061 // [...] If all of the file scope declarations for a function in a
2062 // translation unit include the inline function specifier without extern,
2063 // then the definition in that translation unit is an inline definition.
2064 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2065 Redecl != RedeclEnd;
2066 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002067 if (RedeclForcesDefC99(*Redecl))
2068 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002069 }
2070
2071 // C99 6.7.4p6:
2072 // An inline definition does not provide an external definition for the
2073 // function, and does not forbid an external definition in another
2074 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002075 return false;
2076}
2077
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002078/// getOverloadedOperator - Which C++ overloaded operator this
2079/// function represents, if any.
2080OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002081 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2082 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002083 else
2084 return OO_None;
2085}
2086
Sean Hunta6c058d2010-01-13 09:01:02 +00002087/// getLiteralIdentifier - The literal suffix identifier this function
2088/// represents, if any.
2089const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2090 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2091 return getDeclName().getCXXLiteralIdentifier();
2092 else
2093 return 0;
2094}
2095
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002096FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2097 if (TemplateOrSpecialization.isNull())
2098 return TK_NonTemplate;
2099 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2100 return TK_FunctionTemplate;
2101 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2102 return TK_MemberSpecialization;
2103 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2104 return TK_FunctionTemplateSpecialization;
2105 if (TemplateOrSpecialization.is
2106 <DependentFunctionTemplateSpecializationInfo*>())
2107 return TK_DependentFunctionTemplateSpecialization;
2108
David Blaikieb219cfc2011-09-23 05:06:16 +00002109 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002110}
2111
Douglas Gregor2db32322009-10-07 23:56:10 +00002112FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002113 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002114 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2115
2116 return 0;
2117}
2118
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002119MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2120 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2121}
2122
Douglas Gregor2db32322009-10-07 23:56:10 +00002123void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002124FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2125 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002126 TemplateSpecializationKind TSK) {
2127 assert(TemplateOrSpecialization.isNull() &&
2128 "Member function is already a specialization");
2129 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002130 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002131 TemplateOrSpecialization = Info;
2132}
2133
Douglas Gregor3b846b62009-10-27 20:53:28 +00002134bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002135 // If the function is invalid, it can't be implicitly instantiated.
2136 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002137 return false;
2138
2139 switch (getTemplateSpecializationKind()) {
2140 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002141 case TSK_ExplicitInstantiationDefinition:
2142 return false;
2143
2144 case TSK_ImplicitInstantiation:
2145 return true;
2146
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002147 // It is possible to instantiate TSK_ExplicitSpecialization kind
2148 // if the FunctionDecl has a class scope specialization pattern.
2149 case TSK_ExplicitSpecialization:
2150 return getClassScopeSpecializationPattern() != 0;
2151
Douglas Gregor3b846b62009-10-27 20:53:28 +00002152 case TSK_ExplicitInstantiationDeclaration:
2153 // Handled below.
2154 break;
2155 }
2156
2157 // Find the actual template from which we will instantiate.
2158 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002159 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002160 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002161 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002162
2163 // C++0x [temp.explicit]p9:
2164 // Except for inline functions, other explicit instantiation declarations
2165 // have the effect of suppressing the implicit instantiation of the entity
2166 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002167 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002168 return true;
2169
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002170 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002171}
2172
2173bool FunctionDecl::isTemplateInstantiation() const {
2174 switch (getTemplateSpecializationKind()) {
2175 case TSK_Undeclared:
2176 case TSK_ExplicitSpecialization:
2177 return false;
2178 case TSK_ImplicitInstantiation:
2179 case TSK_ExplicitInstantiationDeclaration:
2180 case TSK_ExplicitInstantiationDefinition:
2181 return true;
2182 }
2183 llvm_unreachable("All TSK values handled.");
2184}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002185
2186FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002187 // Handle class scope explicit specialization special case.
2188 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2189 return getClassScopeSpecializationPattern();
2190
Douglas Gregor3b846b62009-10-27 20:53:28 +00002191 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2192 while (Primary->getInstantiatedFromMemberTemplate()) {
2193 // If we have hit a point where the user provided a specialization of
2194 // this template, we're done looking.
2195 if (Primary->isMemberSpecialization())
2196 break;
2197
2198 Primary = Primary->getInstantiatedFromMemberTemplate();
2199 }
2200
2201 return Primary->getTemplatedDecl();
2202 }
2203
2204 return getInstantiatedFromMemberFunction();
2205}
2206
Douglas Gregor16e8be22009-06-29 17:30:29 +00002207FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002208 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002209 = TemplateOrSpecialization
2210 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002211 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002212 }
2213 return 0;
2214}
2215
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002216FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2217 return getASTContext().getClassScopeSpecializationPattern(this);
2218}
2219
Douglas Gregor16e8be22009-06-29 17:30:29 +00002220const TemplateArgumentList *
2221FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002222 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002223 = TemplateOrSpecialization
2224 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002225 return Info->TemplateArguments;
2226 }
2227 return 0;
2228}
2229
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002230const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002231FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2232 if (FunctionTemplateSpecializationInfo *Info
2233 = TemplateOrSpecialization
2234 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2235 return Info->TemplateArgumentsAsWritten;
2236 }
2237 return 0;
2238}
2239
Mike Stump1eb44332009-09-09 15:08:12 +00002240void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002241FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2242 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002243 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002244 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002245 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002246 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2247 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002248 assert(TSK != TSK_Undeclared &&
2249 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002250 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002251 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002252 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002253 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2254 TemplateArgs,
2255 TemplateArgsAsWritten,
2256 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002257 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Douglas Gregor127102b2009-06-29 20:59:39 +00002259 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002260 // function template specializations.
2261 if (InsertPos)
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00002262 Template->addSpecialization(Info, InsertPos);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002263 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00002264 // Try to insert the new node. If there is an existing node, leave it, the
2265 // set will contain the canonical decls while
2266 // FunctionTemplateDecl::findSpecialization will return
2267 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002268 FunctionTemplateSpecializationInfo *Existing
2269 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00002270 (void)Existing;
2271 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
2272 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002273 }
Douglas Gregor1637be72009-06-26 00:10:03 +00002274}
2275
John McCallaf2094e2010-04-08 09:05:18 +00002276void
2277FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2278 const UnresolvedSetImpl &Templates,
2279 const TemplateArgumentListInfo &TemplateArgs) {
2280 assert(TemplateOrSpecialization.isNull());
2281 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2282 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002283 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002284 void *Buffer = Context.Allocate(Size);
2285 DependentFunctionTemplateSpecializationInfo *Info =
2286 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2287 TemplateArgs);
2288 TemplateOrSpecialization = Info;
2289}
2290
2291DependentFunctionTemplateSpecializationInfo::
2292DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2293 const TemplateArgumentListInfo &TArgs)
2294 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2295
2296 d.NumTemplates = Ts.size();
2297 d.NumArgs = TArgs.size();
2298
2299 FunctionTemplateDecl **TsArray =
2300 const_cast<FunctionTemplateDecl**>(getTemplates());
2301 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2302 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2303
2304 TemplateArgumentLoc *ArgsArray =
2305 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2306 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2307 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2308}
2309
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002310TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002311 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002312 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002313 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002314 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002315 if (FTSInfo)
2316 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Douglas Gregor2db32322009-10-07 23:56:10 +00002318 MemberSpecializationInfo *MSInfo
2319 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2320 if (MSInfo)
2321 return MSInfo->getTemplateSpecializationKind();
2322
2323 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002324}
2325
Mike Stump1eb44332009-09-09 15:08:12 +00002326void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002327FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2328 SourceLocation PointOfInstantiation) {
2329 if (FunctionTemplateSpecializationInfo *FTSInfo
2330 = TemplateOrSpecialization.dyn_cast<
2331 FunctionTemplateSpecializationInfo*>()) {
2332 FTSInfo->setTemplateSpecializationKind(TSK);
2333 if (TSK != TSK_ExplicitSpecialization &&
2334 PointOfInstantiation.isValid() &&
2335 FTSInfo->getPointOfInstantiation().isInvalid())
2336 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2337 } else if (MemberSpecializationInfo *MSInfo
2338 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2339 MSInfo->setTemplateSpecializationKind(TSK);
2340 if (TSK != TSK_ExplicitSpecialization &&
2341 PointOfInstantiation.isValid() &&
2342 MSInfo->getPointOfInstantiation().isInvalid())
2343 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2344 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002345 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002346}
2347
2348SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002349 if (FunctionTemplateSpecializationInfo *FTSInfo
2350 = TemplateOrSpecialization.dyn_cast<
2351 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002352 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002353 else if (MemberSpecializationInfo *MSInfo
2354 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002355 return MSInfo->getPointOfInstantiation();
2356
2357 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002358}
2359
Douglas Gregor9f185072009-09-11 20:15:17 +00002360bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002361 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002362 return true;
2363
2364 // If this function was instantiated from a member function of a
2365 // class template, check whether that member function was defined out-of-line.
2366 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2367 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002368 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002369 return Definition->isOutOfLine();
2370 }
2371
2372 // If this function was instantiated from a function template,
2373 // check whether that function template was defined out-of-line.
2374 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2375 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002376 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002377 return Definition->isOutOfLine();
2378 }
2379
2380 return false;
2381}
2382
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002383SourceRange FunctionDecl::getSourceRange() const {
2384 return SourceRange(getOuterLocStart(), EndRangeLoc);
2385}
2386
Anna Zaks9392d4e2012-01-18 02:45:01 +00002387unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002388 IdentifierInfo *FnInfo = getIdentifier();
2389
2390 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002391 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002392
2393 // Builtin handling.
2394 switch (getBuiltinID()) {
2395 case Builtin::BI__builtin_memset:
2396 case Builtin::BI__builtin___memset_chk:
2397 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002398 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002399
2400 case Builtin::BI__builtin_memcpy:
2401 case Builtin::BI__builtin___memcpy_chk:
2402 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002403 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002404
2405 case Builtin::BI__builtin_memmove:
2406 case Builtin::BI__builtin___memmove_chk:
2407 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002408 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002409
2410 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002411 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002412 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002413 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002414
2415 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002416 case Builtin::BImemcmp:
2417 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002418
2419 case Builtin::BI__builtin_strncpy:
2420 case Builtin::BI__builtin___strncpy_chk:
2421 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002422 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002423
2424 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002425 case Builtin::BIstrncmp:
2426 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002427
2428 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002429 case Builtin::BIstrncasecmp:
2430 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002431
2432 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002433 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002434 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002435 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002436
2437 case Builtin::BI__builtin_strndup:
2438 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002439 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002440
Anna Zaksc36bedc2012-02-01 19:08:57 +00002441 case Builtin::BI__builtin_strlen:
2442 case Builtin::BIstrlen:
2443 return Builtin::BIstrlen;
2444
Anna Zaksd9b859a2012-01-13 21:52:01 +00002445 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002446 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002447 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002448 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002449 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002450 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002451 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002452 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002453 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002454 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002455 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002456 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002457 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002458 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002459 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002460 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002461 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002462 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002463 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002464 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002465 else if (FnInfo->isStr("strlen"))
2466 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002467 }
2468 break;
2469 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002470 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002471}
2472
Chris Lattner8a934232008-03-31 00:36:02 +00002473//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002474// FieldDecl Implementation
2475//===----------------------------------------------------------------------===//
2476
Jay Foad4ba2a172011-01-12 09:06:06 +00002477FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002478 SourceLocation StartLoc, SourceLocation IdLoc,
2479 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002480 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2481 bool HasInit) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002482 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith7a614d82011-06-11 17:19:42 +00002483 BW, Mutable, HasInit);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002484}
2485
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002486FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2487 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2488 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2489 0, QualType(), 0, 0, false, false);
2490}
2491
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002492bool FieldDecl::isAnonymousStructOrUnion() const {
2493 if (!isImplicit() || getDeclName())
2494 return false;
2495
2496 if (const RecordType *Record = getType()->getAs<RecordType>())
2497 return Record->getDecl()->isAnonymousStructOrUnion();
2498
2499 return false;
2500}
2501
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002502unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2503 assert(isBitField() && "not a bitfield");
2504 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2505 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2506}
2507
John McCallba4f5d52011-01-20 07:57:12 +00002508unsigned FieldDecl::getFieldIndex() const {
2509 if (CachedFieldIndex) return CachedFieldIndex - 1;
2510
Richard Smith180f4792011-11-10 06:34:14 +00002511 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002512 const RecordDecl *RD = getParent();
2513 const FieldDecl *LastFD = 0;
2514 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smith180f4792011-11-10 06:34:14 +00002515
2516 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2517 I != E; ++I, ++Index) {
2518 (*I)->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002519
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002520 if (IsMsStruct) {
2521 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smith180f4792011-11-10 06:34:14 +00002522 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2523 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002524 continue;
2525 }
Richard Smith180f4792011-11-10 06:34:14 +00002526 LastFD = (*I);
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002527 }
John McCallba4f5d52011-01-20 07:57:12 +00002528 }
2529
Richard Smith180f4792011-11-10 06:34:14 +00002530 assert(CachedFieldIndex && "failed to find field in parent");
2531 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002532}
2533
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002534SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002535 if (const Expr *E = InitializerOrBitWidth.getPointer())
2536 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002537 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002538}
2539
Richard Smith7a614d82011-06-11 17:19:42 +00002540void FieldDecl::setInClassInitializer(Expr *Init) {
2541 assert(!InitializerOrBitWidth.getPointer() &&
2542 "bit width or initializer already set");
2543 InitializerOrBitWidth.setPointer(Init);
2544 InitializerOrBitWidth.setInt(0);
2545}
2546
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002547//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002548// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002549//===----------------------------------------------------------------------===//
2550
Douglas Gregor1693e152010-07-06 18:42:40 +00002551SourceLocation TagDecl::getOuterLocStart() const {
2552 return getTemplateOrInnerLocStart(this);
2553}
2554
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002555SourceRange TagDecl::getSourceRange() const {
2556 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002557 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002558}
2559
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002560TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002561 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002562}
2563
Richard Smith162e1c12011-04-15 14:24:37 +00002564void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2565 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002566 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002567 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002568 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002569}
2570
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002571void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002572 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002573
2574 if (isa<CXXRecordDecl>(this)) {
2575 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2576 struct CXXRecordDecl::DefinitionData *Data =
2577 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002578 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2579 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002580 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002581}
2582
2583void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002584 assert((!isa<CXXRecordDecl>(this) ||
2585 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2586 "definition completed but not started");
2587
John McCall5e1cdac2011-10-07 06:10:15 +00002588 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002589 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002590
2591 if (ASTMutationListener *L = getASTMutationListener())
2592 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002593}
2594
John McCall5e1cdac2011-10-07 06:10:15 +00002595TagDecl *TagDecl::getDefinition() const {
2596 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002597 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002598 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2599 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002600
2601 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002602 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002603 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002604 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002606 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002607}
2608
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002609void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2610 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002611 // Make sure the extended qualifier info is allocated.
2612 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002613 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002614 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002615 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002616 } else {
John McCallb6217662010-03-15 10:12:16 +00002617 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002618 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002619 if (getExtInfo()->NumTemplParamLists == 0) {
2620 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002621 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002622 }
2623 else
2624 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002625 }
2626 }
2627}
2628
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002629void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2630 unsigned NumTPLists,
2631 TemplateParameterList **TPLists) {
2632 assert(NumTPLists > 0);
2633 // Make sure the extended decl info is allocated.
2634 if (!hasExtInfo())
2635 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002636 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002637 // Set the template parameter lists info.
2638 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2639}
2640
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002641//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002642// EnumDecl Implementation
2643//===----------------------------------------------------------------------===//
2644
David Blaikie99ba9e32011-12-20 02:48:34 +00002645void EnumDecl::anchor() { }
2646
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002647EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2648 SourceLocation StartLoc, SourceLocation IdLoc,
2649 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002650 EnumDecl *PrevDecl, bool IsScoped,
2651 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002652 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002653 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002654 C.getTypeDeclType(Enum, PrevDecl);
2655 return Enum;
2656}
2657
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002658EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2659 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2660 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2661 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002662}
2663
Douglas Gregor838db382010-02-11 01:19:42 +00002664void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002665 QualType NewPromotionType,
2666 unsigned NumPositiveBits,
2667 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002668 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002669 if (!IntegerType)
2670 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002671 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002672 setNumPositiveBits(NumPositiveBits);
2673 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002674 TagDecl::completeDefinition();
2675}
2676
2677//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002678// RecordDecl Implementation
2679//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002680
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002681RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2682 SourceLocation StartLoc, SourceLocation IdLoc,
2683 IdentifierInfo *Id, RecordDecl *PrevDecl)
2684 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002685 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002686 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002687 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002688 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002689 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002690}
2691
Jay Foad4ba2a172011-01-12 09:06:06 +00002692RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002693 SourceLocation StartLoc, SourceLocation IdLoc,
2694 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2695 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2696 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002697 C.getTypeDeclType(R, PrevDecl);
2698 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002699}
2700
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002701RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2702 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2703 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2704 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002705}
2706
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002707bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002708 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002709 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2710}
2711
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002712RecordDecl::field_iterator RecordDecl::field_begin() const {
2713 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2714 LoadFieldsFromExternalStorage();
2715
2716 return field_iterator(decl_iterator(FirstDecl));
2717}
2718
Douglas Gregorda2142f2011-02-19 18:51:44 +00002719/// completeDefinition - Notes that the definition of this type is now
2720/// complete.
2721void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002722 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002723 TagDecl::completeDefinition();
2724}
2725
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002726void RecordDecl::LoadFieldsFromExternalStorage() const {
2727 ExternalASTSource *Source = getASTContext().getExternalSource();
2728 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2729
2730 // Notify that we have a RecordDecl doing some initialization.
2731 ExternalASTSource::Deserializing TheFields(Source);
2732
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002734 LoadedFieldsFromExternalStorage = true;
2735 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2736 case ELR_Success:
2737 break;
2738
2739 case ELR_AlreadyLoaded:
2740 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002741 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002742 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002743
2744#ifndef NDEBUG
2745 // Check that all decls we got were FieldDecls.
2746 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2747 assert(isa<FieldDecl>(Decls[i]));
2748#endif
2749
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002750 if (Decls.empty())
2751 return;
2752
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002753 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2754 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002755}
2756
Steve Naroff56ee6892008-10-08 17:01:13 +00002757//===----------------------------------------------------------------------===//
2758// BlockDecl Implementation
2759//===----------------------------------------------------------------------===//
2760
David Blaikie4278c652011-09-21 18:16:56 +00002761void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002762 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002763
Steve Naroffe78b8092009-03-13 16:56:44 +00002764 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002765 if (!NewParamInfo.empty()) {
2766 NumParams = NewParamInfo.size();
2767 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2768 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002769 }
2770}
2771
John McCall6b5a61b2011-02-07 10:33:21 +00002772void BlockDecl::setCaptures(ASTContext &Context,
2773 const Capture *begin,
2774 const Capture *end,
2775 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002776 CapturesCXXThis = capturesCXXThis;
2777
2778 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002779 NumCaptures = 0;
2780 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002781 return;
2782 }
2783
John McCall6b5a61b2011-02-07 10:33:21 +00002784 NumCaptures = end - begin;
2785
2786 // Avoid new Capture[] because we don't want to provide a default
2787 // constructor.
2788 size_t allocationSize = NumCaptures * sizeof(Capture);
2789 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2790 memcpy(buffer, begin, allocationSize);
2791 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002792}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002793
John McCall204e1332011-06-15 22:51:16 +00002794bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2795 for (capture_const_iterator
2796 i = capture_begin(), e = capture_end(); i != e; ++i)
2797 // Only auto vars can be captured, so no redeclaration worries.
2798 if (i->getVariable() == variable)
2799 return true;
2800
2801 return false;
2802}
2803
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002804SourceRange BlockDecl::getSourceRange() const {
2805 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2806}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002807
2808//===----------------------------------------------------------------------===//
2809// Other Decl Allocation/Deallocation Method Implementations
2810//===----------------------------------------------------------------------===//
2811
David Blaikie99ba9e32011-12-20 02:48:34 +00002812void TranslationUnitDecl::anchor() { }
2813
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002814TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2815 return new (C) TranslationUnitDecl(C);
2816}
2817
David Blaikie99ba9e32011-12-20 02:48:34 +00002818void LabelDecl::anchor() { }
2819
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002820LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002821 SourceLocation IdentL, IdentifierInfo *II) {
2822 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2823}
2824
2825LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2826 SourceLocation IdentL, IdentifierInfo *II,
2827 SourceLocation GnuLabelL) {
2828 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2829 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002830}
2831
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002832LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2833 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2834 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002835}
2836
David Blaikie99ba9e32011-12-20 02:48:34 +00002837void ValueDecl::anchor() { }
2838
2839void ImplicitParamDecl::anchor() { }
2840
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002841ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002842 SourceLocation IdLoc,
2843 IdentifierInfo *Id,
2844 QualType Type) {
2845 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002846}
2847
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002848ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2849 unsigned ID) {
2850 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2851 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2852}
2853
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002854FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002855 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002856 const DeclarationNameInfo &NameInfo,
2857 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002858 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002859 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002860 bool hasWrittenPrototype,
2861 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002862 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2863 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002864 isInlineSpecified,
2865 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002866 New->HasWrittenPrototype = hasWrittenPrototype;
2867 return New;
2868}
2869
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002870FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2871 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2872 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2873 DeclarationNameInfo(), QualType(), 0,
2874 SC_None, SC_None, false, false);
2875}
2876
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002877BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2878 return new (C) BlockDecl(DC, L);
2879}
2880
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002881BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2882 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2883 return new (Mem) BlockDecl(0, SourceLocation());
2884}
2885
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002886EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2887 SourceLocation L,
2888 IdentifierInfo *Id, QualType T,
2889 Expr *E, const llvm::APSInt &V) {
2890 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2891}
2892
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002893EnumConstantDecl *
2894EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2895 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2896 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2897 llvm::APSInt());
2898}
2899
David Blaikie99ba9e32011-12-20 02:48:34 +00002900void IndirectFieldDecl::anchor() { }
2901
Benjamin Kramerd9811462010-11-21 14:11:41 +00002902IndirectFieldDecl *
2903IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2904 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2905 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002906 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2907}
2908
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002909IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2910 unsigned ID) {
2911 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2912 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2913 QualType(), 0, 0);
2914}
2915
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002916SourceRange EnumConstantDecl::getSourceRange() const {
2917 SourceLocation End = getLocation();
2918 if (Init)
2919 End = Init->getLocEnd();
2920 return SourceRange(getLocation(), End);
2921}
2922
David Blaikie99ba9e32011-12-20 02:48:34 +00002923void TypeDecl::anchor() { }
2924
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002925TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002926 SourceLocation StartLoc, SourceLocation IdLoc,
2927 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2928 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002929}
2930
David Blaikie99ba9e32011-12-20 02:48:34 +00002931void TypedefNameDecl::anchor() { }
2932
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002933TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2934 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2935 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2936}
2937
Richard Smith162e1c12011-04-15 14:24:37 +00002938TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2939 SourceLocation StartLoc,
2940 SourceLocation IdLoc, IdentifierInfo *Id,
2941 TypeSourceInfo *TInfo) {
2942 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2943}
2944
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002945TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2946 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2947 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2948}
2949
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002950SourceRange TypedefDecl::getSourceRange() const {
2951 SourceLocation RangeEnd = getLocation();
2952 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2953 if (typeIsPostfix(TInfo->getType()))
2954 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2955 }
2956 return SourceRange(getLocStart(), RangeEnd);
2957}
2958
Richard Smith162e1c12011-04-15 14:24:37 +00002959SourceRange TypeAliasDecl::getSourceRange() const {
2960 SourceLocation RangeEnd = getLocStart();
2961 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2962 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2963 return SourceRange(getLocStart(), RangeEnd);
2964}
2965
David Blaikie99ba9e32011-12-20 02:48:34 +00002966void FileScopeAsmDecl::anchor() { }
2967
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002968FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002969 StringLiteral *Str,
2970 SourceLocation AsmLoc,
2971 SourceLocation RParenLoc) {
2972 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002973}
Douglas Gregor15de72c2011-12-02 23:23:56 +00002974
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002975FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2976 unsigned ID) {
2977 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2978 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2979}
2980
Douglas Gregor15de72c2011-12-02 23:23:56 +00002981//===----------------------------------------------------------------------===//
2982// ImportDecl Implementation
2983//===----------------------------------------------------------------------===//
2984
2985/// \brief Retrieve the number of module identifiers needed to name the given
2986/// module.
2987static unsigned getNumModuleIdentifiers(Module *Mod) {
2988 unsigned Result = 1;
2989 while (Mod->Parent) {
2990 Mod = Mod->Parent;
2991 ++Result;
2992 }
2993 return Result;
2994}
2995
Douglas Gregor5948ae12012-01-03 18:04:46 +00002996ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00002997 Module *Imported,
2998 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00002999 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003000 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003001{
3002 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3003 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3004 memcpy(StoredLocs, IdentifierLocs.data(),
3005 IdentifierLocs.size() * sizeof(SourceLocation));
3006}
3007
Douglas Gregor5948ae12012-01-03 18:04:46 +00003008ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003009 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003010 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003011 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003012{
3013 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3014}
3015
3016ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003017 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003018 ArrayRef<SourceLocation> IdentifierLocs) {
3019 void *Mem = C.Allocate(sizeof(ImportDecl) +
3020 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003021 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003022}
3023
3024ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003025 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003026 Module *Imported,
3027 SourceLocation EndLoc) {
3028 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003029 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003030 Import->setImplicit();
3031 return Import;
3032}
3033
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003034ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3035 unsigned NumLocations) {
3036 void *Mem = AllocateDeserializedDecl(C, ID,
3037 (sizeof(ImportDecl) +
3038 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003039 return new (Mem) ImportDecl(EmptyShell());
3040}
3041
3042ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3043 if (!ImportedAndComplete.getInt())
3044 return ArrayRef<SourceLocation>();
3045
3046 const SourceLocation *StoredLocs
3047 = reinterpret_cast<const SourceLocation *>(this + 1);
3048 return ArrayRef<SourceLocation>(StoredLocs,
3049 getNumModuleIdentifiers(getImportedModule()));
3050}
3051
3052SourceRange ImportDecl::getSourceRange() const {
3053 if (!ImportedAndComplete.getInt())
3054 return SourceRange(getLocation(),
3055 *reinterpret_cast<const SourceLocation *>(this + 1));
3056
3057 return SourceRange(getLocation(), getIdentifierLocs().back());
3058}