blob: 5c7c0694a04b1ba8495259ce0941db0d66128b16 [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"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000028#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000029#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000030
David Blaikie4278c652011-09-21 18:16:56 +000031#include <algorithm>
32
Reid Spencer5f016e22007-07-11 17:01:13 +000033using namespace clang;
34
Chris Lattnerd3b90652008-03-15 05:43:15 +000035//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000036// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000037//===----------------------------------------------------------------------===//
38
Douglas Gregor4421d2b2011-03-26 12:10:19 +000039static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
40 // If this declaration has an explicit visibility attribute, use it.
41 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
42 switch (A->getVisibility()) {
43 case VisibilityAttr::Default:
44 return DefaultVisibility;
45 case VisibilityAttr::Hidden:
46 return HiddenVisibility;
47 case VisibilityAttr::Protected:
48 return ProtectedVisibility;
49 }
John McCall7f1b9872010-12-18 03:30:47 +000050
John McCall1fb0caa2010-10-22 21:05:15 +000051 return DefaultVisibility;
John McCall1fb0caa2010-10-22 21:05:15 +000052 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +000053
54 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
55 // implies visibility(default).
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000056 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +000057 for (specific_attr_iterator<AvailabilityAttr>
58 A = D->specific_attr_begin<AvailabilityAttr>(),
59 AEnd = D->specific_attr_end<AvailabilityAttr>();
60 A != AEnd; ++A)
61 if ((*A)->getPlatform()->getName().equals("macosx"))
62 return DefaultVisibility;
63 }
64
65 return llvm::Optional<Visibility>();
John McCall1fb0caa2010-10-22 21:05:15 +000066}
67
John McCallaf146032010-10-30 11:50:40 +000068typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall1fb0caa2010-10-22 21:05:15 +000069typedef std::pair<Linkage,Visibility> LVPair;
John McCallaf146032010-10-30 11:50:40 +000070
John McCall1fb0caa2010-10-22 21:05:15 +000071static LVPair merge(LVPair L, LVPair R) {
72 return LVPair(minLinkage(L.first, R.first),
73 minVisibility(L.second, R.second));
74}
75
John McCallaf146032010-10-30 11:50:40 +000076static LVPair merge(LVPair L, LinkageInfo R) {
77 return LVPair(minLinkage(L.first, R.linkage()),
78 minVisibility(L.second, R.visibility()));
79}
80
Benjamin Kramer752c2e92010-11-05 19:56:37 +000081namespace {
John McCall36987482010-11-02 01:45:15 +000082/// Flags controlling the computation of linkage and visibility.
83struct LVFlags {
84 bool ConsiderGlobalVisibility;
85 bool ConsiderVisibilityAttributes;
John McCall1a0918a2011-03-04 10:39:25 +000086 bool ConsiderTemplateParameterTypes;
John McCall36987482010-11-02 01:45:15 +000087
88 LVFlags() : ConsiderGlobalVisibility(true),
John McCall1a0918a2011-03-04 10:39:25 +000089 ConsiderVisibilityAttributes(true),
90 ConsiderTemplateParameterTypes(true) {
John McCall36987482010-11-02 01:45:15 +000091 }
92
Douglas Gregor381d34e2010-12-06 18:36:25 +000093 /// \brief Returns a set of flags that is only useful for computing the
94 /// linkage, not the visibility, of a declaration.
95 static LVFlags CreateOnlyDeclLinkage() {
96 LVFlags F;
97 F.ConsiderGlobalVisibility = false;
98 F.ConsiderVisibilityAttributes = false;
John McCall1a0918a2011-03-04 10:39:25 +000099 F.ConsiderTemplateParameterTypes = false;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000100 return F;
101 }
102
John McCall36987482010-11-02 01:45:15 +0000103 /// Returns a set of flags, otherwise based on these, which ignores
104 /// off all sources of visibility except template arguments.
105 LVFlags onlyTemplateVisibility() const {
106 LVFlags F = *this;
107 F.ConsiderGlobalVisibility = false;
108 F.ConsiderVisibilityAttributes = false;
John McCall1a0918a2011-03-04 10:39:25 +0000109 F.ConsiderTemplateParameterTypes = false;
John McCall36987482010-11-02 01:45:15 +0000110 return F;
111 }
Douglas Gregor89d63e52010-12-06 18:50:56 +0000112};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000113} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000114
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000115/// \brief Get the most restrictive linkage for the types in the given
116/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +0000117static LVPair
118getLVForTemplateParameterList(const TemplateParameterList *Params) {
119 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000120 for (TemplateParameterList::const_iterator P = Params->begin(),
121 PEnd = Params->end();
122 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000123 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
124 if (NTTP->isExpandedParameterPack()) {
125 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
126 QualType T = NTTP->getExpansionType(I);
127 if (!T->isDependentType())
128 LV = merge(LV, T->getLinkageAndVisibility());
129 }
130 continue;
131 }
132
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000133 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000134 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000135 continue;
136 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000137 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000138
139 if (TemplateTemplateParmDecl *TTP
140 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000141 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000142 }
143 }
144
John McCall1fb0caa2010-10-22 21:05:15 +0000145 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000146}
147
Douglas Gregor381d34e2010-12-06 18:36:25 +0000148/// getLVForDecl - Get the linkage and visibility for the given declaration.
149static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
150
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000151/// \brief Get the most restrictive linkage for the types and
152/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +0000153static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000154 unsigned NumArgs,
155 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000156 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000157
158 for (unsigned I = 0; I != NumArgs; ++I) {
159 switch (Args[I].getKind()) {
160 case TemplateArgument::Null:
161 case TemplateArgument::Integral:
162 case TemplateArgument::Expression:
163 break;
164
165 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000166 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000167 break;
168
169 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000170 // The decl can validly be null as the representation of nullptr
171 // arguments, valid only in C++0x.
172 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor89d63e52010-12-06 18:50:56 +0000173 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
174 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000175 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000176 break;
177
178 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000179 case TemplateArgument::TemplateExpansion:
180 if (TemplateDecl *Template
181 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor89d63e52010-12-06 18:50:56 +0000182 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000183 break;
184
185 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000186 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000187 Args[I].pack_size(),
188 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000189 break;
190 }
191 }
192
John McCall1fb0caa2010-10-22 21:05:15 +0000193 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000194}
195
John McCallaf146032010-10-30 11:50:40 +0000196static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000197getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
198 LVFlags &F) {
199 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000200}
201
John McCall6ce51ee2011-06-27 23:06:04 +0000202static bool shouldConsiderTemplateLV(const FunctionDecl *fn,
203 const FunctionTemplateSpecializationInfo *spec) {
204 return !(spec->isExplicitSpecialization() &&
205 fn->hasAttr<VisibilityAttr>());
206}
207
208static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
209 return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
210}
211
John McCall36987482010-11-02 01:45:15 +0000212static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000213 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000214 "Not a name having namespace scope");
215 ASTContext &Context = D->getASTContext();
216
217 // C++ [basic.link]p3:
218 // A name having namespace scope (3.3.6) has internal linkage if it
219 // is the name of
220 // - an object, reference, function or function template that is
221 // explicitly declared static; or,
222 // (This bullet corresponds to C99 6.2.2p3.)
223 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
224 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000225 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000226 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000227
228 // - an object or reference that is explicitly declared const
229 // and neither explicitly declared extern nor previously
230 // declared to have external linkage; or
231 // (there is no equivalent in C99)
232 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000233 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000234 Var->getStorageClass() != SC_Extern &&
235 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000236 bool FoundExtern = false;
237 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
238 PrevVar && !FoundExtern;
239 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000240 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000241 FoundExtern = true;
242
243 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000244 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000245 }
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000246 if (Var->getStorageClass() == SC_None) {
247 const VarDecl *PrevVar = Var->getPreviousDeclaration();
248 for (; PrevVar; PrevVar = PrevVar->getPreviousDeclaration())
249 if (PrevVar->getStorageClass() == SC_PrivateExtern)
250 break;
251 if (PrevVar)
252 return PrevVar->getLinkageAndVisibility();
253 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000254 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000255 // C++ [temp]p4:
256 // A non-member function template can have internal linkage; any
257 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000258 const FunctionDecl *Function = 0;
259 if (const FunctionTemplateDecl *FunTmpl
260 = dyn_cast<FunctionTemplateDecl>(D))
261 Function = FunTmpl->getTemplatedDecl();
262 else
263 Function = cast<FunctionDecl>(D);
264
265 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000266 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000267 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000268 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
269 // - a data member of an anonymous union.
270 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000271 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000272 }
273
Chandler Carruth094b6432011-02-24 19:03:39 +0000274 if (D->isInAnonymousNamespace()) {
275 const VarDecl *Var = dyn_cast<VarDecl>(D);
276 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
277 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
278 return LinkageInfo::uniqueExternal();
279 }
John McCalle7bc9722010-10-28 04:18:25 +0000280
John McCall1fb0caa2010-10-22 21:05:15 +0000281 // Set up the defaults.
282
283 // C99 6.2.2p5:
284 // If the declaration of an identifier for an object has file
285 // scope and no storage-class specifier, its linkage is
286 // external.
John McCallaf146032010-10-30 11:50:40 +0000287 LinkageInfo LV;
288
John McCall36987482010-11-02 01:45:15 +0000289 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000290 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
291 LV.setVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000292 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000293 } else {
294 // If we're declared in a namespace with a visibility attribute,
295 // use that namespace's visibility, but don't call it explicit.
296 for (const DeclContext *DC = D->getDeclContext();
297 !isa<TranslationUnitDecl>(DC);
298 DC = DC->getParent()) {
299 if (!isa<NamespaceDecl>(DC)) continue;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000300 if (llvm::Optional<Visibility> Vis
301 = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
302 LV.setVisibility(*Vis, false);
John McCall90f14502010-12-10 02:59:44 +0000303 F.ConsiderGlobalVisibility = false;
304 break;
305 }
306 }
John McCall36987482010-11-02 01:45:15 +0000307 }
John McCallaf146032010-10-30 11:50:40 +0000308 }
John McCall1fb0caa2010-10-22 21:05:15 +0000309
Douglas Gregord85b5b92009-11-25 22:24:25 +0000310 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000311
Douglas Gregord85b5b92009-11-25 22:24:25 +0000312 // A name having namespace scope has external linkage if it is the
313 // name of
314 //
315 // - an object or reference, unless it has internal linkage; or
316 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000317 // GCC applies the following optimization to variables and static
318 // data members, but not to functions:
319 //
John McCall1fb0caa2010-10-22 21:05:15 +0000320 // Modify the variable's LV by the LV of its type unless this is
321 // C or extern "C". This follows from [basic.link]p9:
322 // A type without linkage shall not be used as the type of a
323 // variable or function with external linkage unless
324 // - the entity has C language linkage, or
325 // - the entity is declared within an unnamed namespace, or
326 // - the entity is not used or is defined in the same
327 // translation unit.
328 // and [basic.link]p10:
329 // ...the types specified by all declarations referring to a
330 // given variable or function shall be identical...
331 // C does not have an equivalent rule.
332 //
John McCallac65c622010-10-26 04:59:26 +0000333 // Ignore this if we've got an explicit attribute; the user
334 // probably knows what they're doing.
335 //
John McCall1fb0caa2010-10-22 21:05:15 +0000336 // Note that we don't want to make the variable non-external
337 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000338 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000339 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
340 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000341 return LinkageInfo::uniqueExternal();
342 if (!LV.visibilityExplicit())
343 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000344 }
345
John McCall35cebc32010-11-02 18:38:13 +0000346 if (Var->getStorageClass() == SC_PrivateExtern)
347 LV.setVisibility(HiddenVisibility, true);
348
Douglas Gregord85b5b92009-11-25 22:24:25 +0000349 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000350 (Var->getStorageClass() == SC_Extern ||
351 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000352
Douglas Gregord85b5b92009-11-25 22:24:25 +0000353 // C99 6.2.2p4:
354 // For an identifier declared with the storage-class specifier
355 // extern in a scope in which a prior declaration of that
356 // identifier is visible, if the prior declaration specifies
357 // internal or external linkage, the linkage of the identifier
358 // at the later declaration is the same as the linkage
359 // specified at the prior declaration. If no prior declaration
360 // is visible, or if the prior declaration specifies no
361 // linkage, then the identifier has external linkage.
362 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000363 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000364 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
365 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000366 }
367 }
368
Douglas Gregord85b5b92009-11-25 22:24:25 +0000369 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000370 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000371 // In theory, we can modify the function's LV by the LV of its
372 // type unless it has C linkage (see comment above about variables
373 // for justification). In practice, GCC doesn't do this, so it's
374 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000375
John McCall35cebc32010-11-02 18:38:13 +0000376 if (Function->getStorageClass() == SC_PrivateExtern)
377 LV.setVisibility(HiddenVisibility, true);
378
Douglas Gregord85b5b92009-11-25 22:24:25 +0000379 // C99 6.2.2p5:
380 // If the declaration of an identifier for a function has no
381 // storage-class specifier, its linkage is determined exactly
382 // as if it were declared with the storage-class specifier
383 // extern.
384 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000385 (Function->getStorageClass() == SC_Extern ||
386 Function->getStorageClass() == SC_PrivateExtern ||
387 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000388 // C99 6.2.2p4:
389 // For an identifier declared with the storage-class specifier
390 // extern in a scope in which a prior declaration of that
391 // identifier is visible, if the prior declaration specifies
392 // internal or external linkage, the linkage of the identifier
393 // at the later declaration is the same as the linkage
394 // specified at the prior declaration. If no prior declaration
395 // is visible, or if the prior declaration specifies no
396 // linkage, then the identifier has external linkage.
397 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000398 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000399 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
400 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000401 }
402 }
403
John McCallaf8ca372011-02-10 06:50:24 +0000404 // In C++, then if the type of the function uses a type with
405 // unique-external linkage, it's not legally usable from outside
406 // this translation unit. However, we should use the C linkage
407 // rules instead for extern "C" declarations.
408 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
409 Function->getType()->getLinkage() == UniqueExternalLinkage)
410 return LinkageInfo::uniqueExternal();
411
John McCall6ce51ee2011-06-27 23:06:04 +0000412 // Consider LV from the template and the template arguments unless
413 // this is an explicit specialization with a visibility attribute.
414 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000415 = Function->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000416 if (shouldConsiderTemplateLV(Function, specInfo)) {
417 LV.merge(getLVForDecl(specInfo->getTemplate(),
418 F.onlyTemplateVisibility()));
419 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
420 LV.merge(getLVForTemplateArgumentList(templateArgs, F));
421 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000422 }
423
Douglas Gregord85b5b92009-11-25 22:24:25 +0000424 // - a named class (Clause 9), or an unnamed class defined in a
425 // typedef declaration in which the class has the typedef name
426 // for linkage purposes (7.1.3); or
427 // - a named enumeration (7.2), or an unnamed enumeration
428 // defined in a typedef declaration in which the enumeration
429 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000430 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
431 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000432 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000433 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000434
John McCall1fb0caa2010-10-22 21:05:15 +0000435 // If this is a class template specialization, consider the
436 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000437 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000438 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000439 if (shouldConsiderTemplateLV(spec)) {
440 // From the template.
441 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
442 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000443
John McCall6ce51ee2011-06-27 23:06:04 +0000444 // The arguments at which the template was instantiated.
445 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
446 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
447 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000448 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000449
John McCallac65c622010-10-26 04:59:26 +0000450 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000451 if (F.ConsiderGlobalVisibility)
452 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000453 (Context.getLangOptions().CPlusPlus &&
454 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000455
Douglas Gregord85b5b92009-11-25 22:24:25 +0000456 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000457 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000458 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000459 if (!isExternalLinkage(EnumLV.linkage()))
460 return LinkageInfo::none();
461 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000462
463 // - a template, unless it is a function template that has
464 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000465 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
466 if (F.ConsiderTemplateParameterTypes)
467 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000468
Douglas Gregord85b5b92009-11-25 22:24:25 +0000469 // - a namespace (7.3), unless it is declared within an unnamed
470 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000471 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
472 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000473
John McCall1fb0caa2010-10-22 21:05:15 +0000474 // By extension, we assign external linkage to Objective-C
475 // interfaces.
476 } else if (isa<ObjCInterfaceDecl>(D)) {
477 // fallout
478
479 // Everything not covered here has no linkage.
480 } else {
John McCallaf146032010-10-30 11:50:40 +0000481 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000482 }
483
484 // If we ended up with non-external linkage, visibility should
485 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000486 if (LV.linkage() != ExternalLinkage)
487 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000488
489 // If we didn't end up with hidden visibility, consider attributes
490 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000491 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000492 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000493
494 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000495}
496
John McCall36987482010-11-02 01:45:15 +0000497static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000498 // Only certain class members have linkage. Note that fields don't
499 // really have linkage, but it's convenient to say they do for the
500 // purposes of calculating linkage of pointer-to-data-member
501 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000502 if (!(isa<CXXMethodDecl>(D) ||
503 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000504 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000505 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000506 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000507 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000508
John McCall36987482010-11-02 01:45:15 +0000509 LinkageInfo LV;
510
511 // The flags we're going to use to compute the class's visibility.
512 LVFlags ClassF = F;
513
514 // If we have an explicit visibility attribute, merge that in.
515 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000516 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
517 LV.mergeVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000518
519 // Ignore global visibility later, but not this attribute.
520 F.ConsiderGlobalVisibility = false;
521
522 // Ignore both global visibility and attributes when computing our
523 // parent's visibility.
524 ClassF = F.onlyTemplateVisibility();
525 }
526 }
John McCallaf146032010-10-30 11:50:40 +0000527
528 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000529 // linkage.
530 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
531 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000532 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000533
534 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000535 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000536 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000537
John McCall3cdfc4d2010-08-13 08:35:10 +0000538 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000539 // If the type of the function uses a type with unique-external
540 // linkage, it's not legally usable from outside this translation unit.
541 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
542 return LinkageInfo::uniqueExternal();
543
John McCall110e8e52010-10-29 22:22:43 +0000544 TemplateSpecializationKind TSK = TSK_Undeclared;
545
John McCall1fb0caa2010-10-22 21:05:15 +0000546 // If this is a method template specialization, use the linkage for
547 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000548 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000549 = MD->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000550 if (shouldConsiderTemplateLV(MD, spec)) {
551 LV.merge(getLVForTemplateArgumentList(*spec->TemplateArguments, F));
552 if (F.ConsiderTemplateParameterTypes)
553 LV.merge(getLVForTemplateParameterList(
554 spec->getTemplate()->getTemplateParameters()));
555 }
John McCall110e8e52010-10-29 22:22:43 +0000556
John McCall6ce51ee2011-06-27 23:06:04 +0000557 TSK = spec->getTemplateSpecializationKind();
John McCall110e8e52010-10-29 22:22:43 +0000558 } else if (MemberSpecializationInfo *MSI =
559 MD->getMemberSpecializationInfo()) {
560 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000561 }
562
John McCall110e8e52010-10-29 22:22:43 +0000563 // If we're paying attention to global visibility, apply
564 // -finline-visibility-hidden if this is an inline method.
565 //
John McCallaf146032010-10-30 11:50:40 +0000566 // Note that ConsiderGlobalVisibility doesn't yet have information
567 // about whether containing classes have visibility attributes,
568 // and that's intentional.
569 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000570 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000571 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
572 // InlineVisibilityHidden only applies to definitions, and
573 // isInlined() only gives meaningful answers on definitions
574 // anyway.
575 const FunctionDecl *Def = 0;
576 if (MD->hasBody(Def) && Def->isInlined())
577 LV.setVisibility(HiddenVisibility);
578 }
John McCall1fb0caa2010-10-22 21:05:15 +0000579
John McCall110e8e52010-10-29 22:22:43 +0000580 // Note that in contrast to basically every other situation, we
581 // *do* apply -fvisibility to method declarations.
582
583 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000584 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000585 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000586 if (shouldConsiderTemplateLV(spec)) {
587 // Merge template argument/parameter information for member
588 // class template specializations.
589 LV.merge(getLVForTemplateArgumentList(spec->getTemplateArgs(), F));
John McCall1a0918a2011-03-04 10:39:25 +0000590 if (F.ConsiderTemplateParameterTypes)
591 LV.merge(getLVForTemplateParameterList(
John McCall6ce51ee2011-06-27 23:06:04 +0000592 spec->getSpecializedTemplate()->getTemplateParameters()));
593 }
John McCall110e8e52010-10-29 22:22:43 +0000594 }
595
John McCall110e8e52010-10-29 22:22:43 +0000596 // Static data members.
597 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000598 // Modify the variable's linkage by its type, but ignore the
599 // type's visibility unless it's a definition.
600 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
601 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000602 LV.mergeLinkage(UniqueExternalLinkage);
603 if (!LV.visibilityExplicit())
604 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000605 }
606
John McCall36987482010-11-02 01:45:15 +0000607 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000608
609 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000610 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000611 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000612 }
613
John McCall1fb0caa2010-10-22 21:05:15 +0000614 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000615}
616
John McCallf76b0922011-02-08 19:01:05 +0000617static void clearLinkageForClass(const CXXRecordDecl *record) {
618 for (CXXRecordDecl::decl_iterator
619 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
620 Decl *child = *i;
621 if (isa<NamedDecl>(child))
622 cast<NamedDecl>(child)->ClearLinkageCache();
623 }
624}
625
626void NamedDecl::ClearLinkageCache() {
627 // Note that we can't skip clearing the linkage of children just
628 // because the parent doesn't have cached linkage: we don't cache
629 // when computing linkage for parent contexts.
630
631 HasCachedLinkage = 0;
632
633 // If we're changing the linkage of a class, we need to reset the
634 // linkage of child declarations, too.
635 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
636 clearLinkageForClass(record);
637
John McCall15e310a2011-02-19 02:53:41 +0000638 if (ClassTemplateDecl *temp =
639 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000640 // Clear linkage for the template pattern.
641 CXXRecordDecl *record = temp->getTemplatedDecl();
642 record->HasCachedLinkage = 0;
643 clearLinkageForClass(record);
644
John McCall15e310a2011-02-19 02:53:41 +0000645 // We need to clear linkage for specializations, too.
646 for (ClassTemplateDecl::spec_iterator
647 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
648 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000649 }
John McCall15e310a2011-02-19 02:53:41 +0000650
651 // Clear cached linkage for function template decls, too.
652 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000653 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
654 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000655 for (FunctionTemplateDecl::spec_iterator
656 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
657 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000658 }
John McCall15e310a2011-02-19 02:53:41 +0000659
John McCallf76b0922011-02-08 19:01:05 +0000660}
661
Douglas Gregor381d34e2010-12-06 18:36:25 +0000662Linkage NamedDecl::getLinkage() const {
663 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000664 assert(Linkage(CachedLinkage) ==
665 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000666 return Linkage(CachedLinkage);
667 }
668
669 CachedLinkage = getLVForDecl(this,
670 LVFlags::CreateOnlyDeclLinkage()).linkage();
671 HasCachedLinkage = 1;
672 return Linkage(CachedLinkage);
673}
674
John McCallaf146032010-10-30 11:50:40 +0000675LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000676 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000677 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000678 HasCachedLinkage = 1;
679 CachedLinkage = LI.linkage();
680 return LI;
John McCall0df95872010-10-29 00:29:13 +0000681}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000682
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000683llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
684 // Use the most recent declaration of a variable.
685 if (const VarDecl *var = dyn_cast<VarDecl>(this))
686 return getVisibilityOf(var->getMostRecentDeclaration());
687
688 // Use the most recent declaration of a function, and also handle
689 // function template specializations.
690 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
691 if (llvm::Optional<Visibility> V
692 = getVisibilityOf(fn->getMostRecentDeclaration()))
693 return V;
694
695 // If the function is a specialization of a template with an
696 // explicit visibility attribute, use that.
697 if (FunctionTemplateSpecializationInfo *templateInfo
698 = fn->getTemplateSpecializationInfo())
699 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
700
701 return llvm::Optional<Visibility>();
702 }
703
704 // Otherwise, just check the declaration itself first.
705 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
706 return V;
707
708 // If there wasn't explicit visibility there, and this is a
709 // specialization of a class template, check for visibility
710 // on the pattern.
711 if (const ClassTemplateSpecializationDecl *spec
712 = dyn_cast<ClassTemplateSpecializationDecl>(this))
713 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
714
715 return llvm::Optional<Visibility>();
716}
717
John McCall36987482010-11-02 01:45:15 +0000718static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000719 // Objective-C: treat all Objective-C declarations as having external
720 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000721 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000722 default:
723 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000724 case Decl::ParmVar:
725 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000726 case Decl::TemplateTemplateParm: // count these as external
727 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000728 case Decl::ObjCAtDefsField:
729 case Decl::ObjCCategory:
730 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000731 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000732 case Decl::ObjCForwardProtocol:
733 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000734 case Decl::ObjCMethod:
735 case Decl::ObjCProperty:
736 case Decl::ObjCPropertyImpl:
737 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000738 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000739 }
740
Douglas Gregord85b5b92009-11-25 22:24:25 +0000741 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000742 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000743 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000744
745 // C++ [basic.link]p5:
746 // In addition, a member function, static data member, a named
747 // class or enumeration of class scope, or an unnamed class or
748 // enumeration defined in a class-scope typedef declaration such
749 // that the class or enumeration has the typedef name for linkage
750 // purposes (7.1.3), has external linkage if the name of the class
751 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000752 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000753 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000754
755 // C++ [basic.link]p6:
756 // The name of a function declared in block scope and the name of
757 // an object declared by a block scope extern declaration have
758 // linkage. If there is a visible declaration of an entity with
759 // linkage having the same name and type, ignoring entities
760 // declared outside the innermost enclosing namespace scope, the
761 // block scope declaration declares that same entity and receives
762 // the linkage of the previous declaration. If there is more than
763 // one such matching entity, the program is ill-formed. Otherwise,
764 // if no matching entity is found, the block scope entity receives
765 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000766 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
767 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000768 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000769 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000770
John McCallaf146032010-10-30 11:50:40 +0000771 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000772 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000773 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
774 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000775 }
776
John McCall1fb0caa2010-10-22 21:05:15 +0000777 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000778 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000779 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
780 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000781 }
782
783 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000784 }
785
John McCall0df95872010-10-29 00:29:13 +0000786 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000787 if (Var->getStorageClass() == SC_Extern ||
788 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000789 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000790 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000791
John McCallaf146032010-10-30 11:50:40 +0000792 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000793 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000794 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000795 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000796 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
797 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000798 }
799
John McCall1fb0caa2010-10-22 21:05:15 +0000800 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000801 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000802 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
803 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000804 }
805
806 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000807 }
808 }
809
810 // C++ [basic.link]p6:
811 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000812 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000813}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000814
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000815std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000816 return getQualifiedNameAsString(getASTContext().getLangOptions());
817}
818
819std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000820 const DeclContext *Ctx = getDeclContext();
821
822 if (Ctx->isFunctionOrMethod())
823 return getNameAsString();
824
Chris Lattner5f9e2722011-07-23 10:55:15 +0000825 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000826 ContextsTy Contexts;
827
828 // Collect contexts.
829 while (Ctx && isa<NamedDecl>(Ctx)) {
830 Contexts.push_back(Ctx);
831 Ctx = Ctx->getParent();
832 };
833
834 std::string QualName;
835 llvm::raw_string_ostream OS(QualName);
836
837 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
838 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000839 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000840 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000841 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
842 std::string TemplateArgsStr
843 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000844 TemplateArgs.data(),
845 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000846 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000847 OS << Spec->getName() << TemplateArgsStr;
848 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000849 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000850 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000851 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000852 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000853 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
854 if (!RD->getIdentifier())
855 OS << "<anonymous " << RD->getKindName() << '>';
856 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000857 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000858 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000859 const FunctionProtoType *FT = 0;
860 if (FD->hasWrittenPrototype())
861 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
862
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000863 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000864 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000865 unsigned NumParams = FD->getNumParams();
866 for (unsigned i = 0; i < NumParams; ++i) {
867 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000868 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000869 std::string Param;
870 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000871 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000872 }
873
874 if (FT->isVariadic()) {
875 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000876 OS << ", ";
877 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000878 }
879 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000880 OS << ')';
881 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000882 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000883 }
884 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000885 }
886
John McCall8472af42010-03-16 21:48:18 +0000887 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000888 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000889 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000890 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000891
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000892 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000893}
894
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000895bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000896 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
897
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000898 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
899 // We want to keep it, unless it nominates same namespace.
900 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000901 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
902 ->getOriginalNamespace() ==
903 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
904 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000907 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
908 // For function declarations, we keep track of redeclarations.
909 return FD->getPreviousDeclaration() == OldD;
910
Douglas Gregore53060f2009-06-25 22:08:12 +0000911 // For function templates, the underlying function declarations are linked.
912 if (const FunctionTemplateDecl *FunctionTemplate
913 = dyn_cast<FunctionTemplateDecl>(this))
914 if (const FunctionTemplateDecl *OldFunctionTemplate
915 = dyn_cast<FunctionTemplateDecl>(OldD))
916 return FunctionTemplate->getTemplatedDecl()
917 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Steve Naroff0de21fd2009-02-22 19:35:57 +0000919 // For method declarations, we keep track of redeclarations.
920 if (isa<ObjCMethodDecl>(this))
921 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
John McCallf36e02d2009-10-09 21:13:30 +0000923 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
924 return true;
925
John McCall9488ea12009-11-17 05:59:44 +0000926 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
927 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
928 cast<UsingShadowDecl>(OldD)->getTargetDecl();
929
Douglas Gregordc355712011-02-25 00:36:19 +0000930 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
931 ASTContext &Context = getASTContext();
932 return Context.getCanonicalNestedNameSpecifier(
933 cast<UsingDecl>(this)->getQualifier()) ==
934 Context.getCanonicalNestedNameSpecifier(
935 cast<UsingDecl>(OldD)->getQualifier());
936 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000937
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000938 // For non-function declarations, if the declarations are of the
939 // same kind then this must be a redeclaration, or semantic analysis
940 // would not have given us the new declaration.
941 return this->getKind() == OldD->getKind();
942}
943
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000944bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000945 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000946}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000947
Anders Carlssone136e0e2009-06-26 06:29:23 +0000948NamedDecl *NamedDecl::getUnderlyingDecl() {
949 NamedDecl *ND = this;
950 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000951 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000952 ND = UD->getTargetDecl();
953 else if (ObjCCompatibleAliasDecl *AD
954 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
955 return AD->getClassInterface();
956 else
957 return ND;
958 }
959}
960
John McCall161755a2010-04-06 21:38:20 +0000961bool NamedDecl::isCXXInstanceMember() const {
962 assert(isCXXClassMember() &&
963 "checking whether non-member is instance member");
964
965 const NamedDecl *D = this;
966 if (isa<UsingShadowDecl>(D))
967 D = cast<UsingShadowDecl>(D)->getTargetDecl();
968
Francois Pichet87c2e122010-11-21 06:08:52 +0000969 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000970 return true;
971 if (isa<CXXMethodDecl>(D))
972 return cast<CXXMethodDecl>(D)->isInstance();
973 if (isa<FunctionTemplateDecl>(D))
974 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
975 ->getTemplatedDecl())->isInstance();
976 return false;
977}
978
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000979//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000980// DeclaratorDecl Implementation
981//===----------------------------------------------------------------------===//
982
Douglas Gregor1693e152010-07-06 18:42:40 +0000983template <typename DeclT>
984static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
985 if (decl->getNumTemplateParameterLists() > 0)
986 return decl->getTemplateParameterList(0)->getTemplateLoc();
987 else
988 return decl->getInnerLocStart();
989}
990
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000991SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000992 TypeSourceInfo *TSI = getTypeSourceInfo();
993 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000994 return SourceLocation();
995}
996
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000997void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
998 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +0000999 // Make sure the extended decl info is allocated.
1000 if (!hasExtInfo()) {
1001 // Save (non-extended) type source info pointer.
1002 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1003 // Allocate external info struct.
1004 DeclInfo = new (getASTContext()) ExtInfo;
1005 // Restore savedTInfo into (extended) decl info.
1006 getExtInfo()->TInfo = savedTInfo;
1007 }
1008 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001009 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001010 } else {
John McCallb6217662010-03-15 10:12:16 +00001011 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001012 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001013 if (getExtInfo()->NumTemplParamLists == 0) {
1014 // Save type source info pointer.
1015 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1016 // Deallocate the extended decl info.
1017 getASTContext().Deallocate(getExtInfo());
1018 // Restore savedTInfo into (non-extended) decl info.
1019 DeclInfo = savedTInfo;
1020 }
1021 else
1022 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001023 }
1024 }
1025}
1026
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001027void
1028DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1029 unsigned NumTPLists,
1030 TemplateParameterList **TPLists) {
1031 assert(NumTPLists > 0);
1032 // Make sure the extended decl info is allocated.
1033 if (!hasExtInfo()) {
1034 // Save (non-extended) type source info pointer.
1035 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1036 // Allocate external info struct.
1037 DeclInfo = new (getASTContext()) ExtInfo;
1038 // Restore savedTInfo into (extended) decl info.
1039 getExtInfo()->TInfo = savedTInfo;
1040 }
1041 // Set the template parameter lists info.
1042 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1043}
1044
Douglas Gregor1693e152010-07-06 18:42:40 +00001045SourceLocation DeclaratorDecl::getOuterLocStart() const {
1046 return getTemplateOrInnerLocStart(this);
1047}
1048
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001049namespace {
1050
1051// Helper function: returns true if QT is or contains a type
1052// having a postfix component.
1053bool typeIsPostfix(clang::QualType QT) {
1054 while (true) {
1055 const Type* T = QT.getTypePtr();
1056 switch (T->getTypeClass()) {
1057 default:
1058 return false;
1059 case Type::Pointer:
1060 QT = cast<PointerType>(T)->getPointeeType();
1061 break;
1062 case Type::BlockPointer:
1063 QT = cast<BlockPointerType>(T)->getPointeeType();
1064 break;
1065 case Type::MemberPointer:
1066 QT = cast<MemberPointerType>(T)->getPointeeType();
1067 break;
1068 case Type::LValueReference:
1069 case Type::RValueReference:
1070 QT = cast<ReferenceType>(T)->getPointeeType();
1071 break;
1072 case Type::PackExpansion:
1073 QT = cast<PackExpansionType>(T)->getPattern();
1074 break;
1075 case Type::Paren:
1076 case Type::ConstantArray:
1077 case Type::DependentSizedArray:
1078 case Type::IncompleteArray:
1079 case Type::VariableArray:
1080 case Type::FunctionProto:
1081 case Type::FunctionNoProto:
1082 return true;
1083 }
1084 }
1085}
1086
1087} // namespace
1088
1089SourceRange DeclaratorDecl::getSourceRange() const {
1090 SourceLocation RangeEnd = getLocation();
1091 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1092 if (typeIsPostfix(TInfo->getType()))
1093 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1094 }
1095 return SourceRange(getOuterLocStart(), RangeEnd);
1096}
1097
Abramo Bagnara9b934882010-06-12 08:15:14 +00001098void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001099QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1100 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001101 TemplateParameterList **TPLists) {
1102 assert((NumTPLists == 0 || TPLists != 0) &&
1103 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001104
1105 // Free previous template parameters (if any).
1106 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001107 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001108 TemplParamLists = 0;
1109 NumTemplParamLists = 0;
1110 }
1111 // Set info on matched template parameter lists (if any).
1112 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001113 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001114 NumTemplParamLists = NumTPLists;
1115 for (unsigned i = NumTPLists; i-- > 0; )
1116 TemplParamLists[i] = TPLists[i];
1117 }
1118}
1119
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001120//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001121// VarDecl Implementation
1122//===----------------------------------------------------------------------===//
1123
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001124const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1125 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001126 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001127 case SC_Auto: return "auto";
1128 case SC_Extern: return "extern";
1129 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1130 case SC_PrivateExtern: return "__private_extern__";
1131 case SC_Register: return "register";
1132 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001133 }
1134
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001135 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001136 return 0;
1137}
1138
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001139VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1140 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001141 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001142 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001143 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001144}
1145
Douglas Gregor381d34e2010-12-06 18:36:25 +00001146void VarDecl::setStorageClass(StorageClass SC) {
1147 assert(isLegalForVariable(SC));
1148 if (getStorageClass() != SC)
1149 ClearLinkageCache();
1150
John McCallf1e4fbf2011-05-01 02:13:58 +00001151 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001152}
1153
Douglas Gregor1693e152010-07-06 18:42:40 +00001154SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001155 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001156 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001157 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001158}
1159
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001160bool VarDecl::isExternC() const {
1161 ASTContext &Context = getASTContext();
1162 if (!Context.getLangOptions().CPlusPlus)
1163 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +00001164 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001165 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1166
Chandler Carruth10aad442011-02-25 00:05:02 +00001167 const DeclContext *DC = getDeclContext();
1168 if (DC->isFunctionOrMethod())
1169 return false;
1170
1171 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001172 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1173 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001174 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001175
1176 break;
1177 }
1178
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001179 }
1180
1181 return false;
1182}
1183
1184VarDecl *VarDecl::getCanonicalDecl() {
1185 return getFirstDeclaration();
1186}
1187
Sebastian Redle9d12b62010-01-31 22:27:38 +00001188VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1189 // C++ [basic.def]p2:
1190 // A declaration is a definition unless [...] it contains the 'extern'
1191 // specifier or a linkage-specification and neither an initializer [...],
1192 // it declares a static data member in a class declaration [...].
1193 // C++ [temp.expl.spec]p15:
1194 // An explicit specialization of a static data member of a template is a
1195 // definition if the declaration includes an initializer; otherwise, it is
1196 // a declaration.
1197 if (isStaticDataMember()) {
1198 if (isOutOfLine() && (hasInit() ||
1199 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1200 return Definition;
1201 else
1202 return DeclarationOnly;
1203 }
1204 // C99 6.7p5:
1205 // A definition of an identifier is a declaration for that identifier that
1206 // [...] causes storage to be reserved for that object.
1207 // Note: that applies for all non-file-scope objects.
1208 // C99 6.9.2p1:
1209 // If the declaration of an identifier for an object has file scope and an
1210 // initializer, the declaration is an external definition for the identifier
1211 if (hasInit())
1212 return Definition;
1213 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1214 if (hasExternalStorage())
1215 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001216
John McCalld931b082010-08-26 03:08:43 +00001217 if (getStorageClassAsWritten() == SC_Extern ||
1218 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001219 for (const VarDecl *PrevVar = getPreviousDeclaration();
1220 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1221 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1222 return DeclarationOnly;
1223 }
1224 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001225 // C99 6.9.2p2:
1226 // A declaration of an object that has file scope without an initializer,
1227 // and without a storage class specifier or the scs 'static', constitutes
1228 // a tentative definition.
1229 // No such thing in C++.
1230 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1231 return TentativeDefinition;
1232
1233 // What's left is (in C, block-scope) declarations without initializers or
1234 // external storage. These are definitions.
1235 return Definition;
1236}
1237
Sebastian Redle9d12b62010-01-31 22:27:38 +00001238VarDecl *VarDecl::getActingDefinition() {
1239 DefinitionKind Kind = isThisDeclarationADefinition();
1240 if (Kind != TentativeDefinition)
1241 return 0;
1242
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001243 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001244 VarDecl *First = getFirstDeclaration();
1245 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1246 I != E; ++I) {
1247 Kind = (*I)->isThisDeclarationADefinition();
1248 if (Kind == Definition)
1249 return 0;
1250 else if (Kind == TentativeDefinition)
1251 LastTentative = *I;
1252 }
1253 return LastTentative;
1254}
1255
1256bool VarDecl::isTentativeDefinitionNow() const {
1257 DefinitionKind Kind = isThisDeclarationADefinition();
1258 if (Kind != TentativeDefinition)
1259 return false;
1260
1261 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1262 if ((*I)->isThisDeclarationADefinition() == Definition)
1263 return false;
1264 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001265 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001266}
1267
Sebastian Redl31310a22010-02-01 20:16:42 +00001268VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001269 VarDecl *First = getFirstDeclaration();
1270 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1271 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001272 if ((*I)->isThisDeclarationADefinition() == Definition)
1273 return *I;
1274 }
1275 return 0;
1276}
1277
John McCall110e8e52010-10-29 22:22:43 +00001278VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1279 DefinitionKind Kind = DeclarationOnly;
1280
1281 const VarDecl *First = getFirstDeclaration();
1282 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1283 I != E; ++I)
1284 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1285
1286 return Kind;
1287}
1288
Sebastian Redl31310a22010-02-01 20:16:42 +00001289const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001290 redecl_iterator I = redecls_begin(), E = redecls_end();
1291 while (I != E && !I->getInit())
1292 ++I;
1293
1294 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001295 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001296 return I->getInit();
1297 }
1298 return 0;
1299}
1300
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001301bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001302 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001303 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001304
1305 if (!isStaticDataMember())
1306 return false;
1307
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001308 // If this static data member was instantiated from a static data member of
1309 // a class template, check whether that static data member was defined
1310 // out-of-line.
1311 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1312 return VD->isOutOfLine();
1313
1314 return false;
1315}
1316
Douglas Gregor0d035142009-10-27 18:42:08 +00001317VarDecl *VarDecl::getOutOfLineDefinition() {
1318 if (!isStaticDataMember())
1319 return 0;
1320
1321 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1322 RD != RDEnd; ++RD) {
1323 if (RD->getLexicalDeclContext()->isFileContext())
1324 return *RD;
1325 }
1326
1327 return 0;
1328}
1329
Douglas Gregor838db382010-02-11 01:19:42 +00001330void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001331 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1332 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001333 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001334 }
1335
1336 Init = I;
1337}
1338
Douglas Gregor03e80032011-06-21 17:03:29 +00001339bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001340 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001341
1342 const Expr *E = getInit();
1343 if (!E)
1344 return false;
1345
1346 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1347 E = Cleanups->getSubExpr();
1348
1349 return isa<MaterializeTemporaryExpr>(E);
1350}
1351
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001352VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001353 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001354 return cast<VarDecl>(MSI->getInstantiatedFrom());
1355
1356 return 0;
1357}
1358
Douglas Gregor663b5a02009-10-14 20:14:33 +00001359TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001360 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001361 return MSI->getTemplateSpecializationKind();
1362
1363 return TSK_Undeclared;
1364}
1365
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001366MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001367 return getASTContext().getInstantiatedFromStaticDataMember(this);
1368}
1369
Douglas Gregor0a897e32009-10-15 17:21:20 +00001370void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1371 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001372 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001373 assert(MSI && "Not an instantiated static data member?");
1374 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001375 if (TSK != TSK_ExplicitSpecialization &&
1376 PointOfInstantiation.isValid() &&
1377 MSI->getPointOfInstantiation().isInvalid())
1378 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001379}
1380
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001381//===----------------------------------------------------------------------===//
1382// ParmVarDecl Implementation
1383//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001384
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001385ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001386 SourceLocation StartLoc,
1387 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001388 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001389 StorageClass S, StorageClass SCAsWritten,
1390 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001391 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001392 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001393}
1394
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001395SourceRange ParmVarDecl::getSourceRange() const {
1396 if (!hasInheritedDefaultArg()) {
1397 SourceRange ArgRange = getDefaultArgRange();
1398 if (ArgRange.isValid())
1399 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1400 }
1401
1402 return DeclaratorDecl::getSourceRange();
1403}
1404
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001405Expr *ParmVarDecl::getDefaultArg() {
1406 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1407 assert(!hasUninstantiatedDefaultArg() &&
1408 "Default argument is not yet instantiated!");
1409
1410 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001411 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001412 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001413
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001414 return Arg;
1415}
1416
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001417SourceRange ParmVarDecl::getDefaultArgRange() const {
1418 if (const Expr *E = getInit())
1419 return E->getSourceRange();
1420
1421 if (hasUninstantiatedDefaultArg())
1422 return getUninstantiatedDefaultArg()->getSourceRange();
1423
1424 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001425}
1426
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001427bool ParmVarDecl::isParameterPack() const {
1428 return isa<PackExpansionType>(getType());
1429}
1430
Ted Kremenekd211cb72011-10-06 05:00:56 +00001431void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1432 getASTContext().setParameterIndex(this, parameterIndex);
1433 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1434}
1435
1436unsigned ParmVarDecl::getParameterIndexLarge() const {
1437 return getASTContext().getParameterIndex(this);
1438}
1439
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001440//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001441// FunctionDecl Implementation
1442//===----------------------------------------------------------------------===//
1443
Douglas Gregorda2142f2011-02-19 18:51:44 +00001444void FunctionDecl::getNameForDiagnostic(std::string &S,
1445 const PrintingPolicy &Policy,
1446 bool Qualified) const {
1447 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1448 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1449 if (TemplateArgs)
1450 S += TemplateSpecializationType::PrintTemplateArgumentList(
1451 TemplateArgs->data(),
1452 TemplateArgs->size(),
1453 Policy);
1454
1455}
1456
Ted Kremenek9498d382010-04-29 16:49:01 +00001457bool FunctionDecl::isVariadic() const {
1458 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1459 return FT->isVariadic();
1460 return false;
1461}
1462
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001463bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1464 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001465 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001466 Definition = *I;
1467 return true;
1468 }
1469 }
1470
1471 return false;
1472}
1473
Anders Carlssonffb945f2011-05-14 23:26:09 +00001474bool FunctionDecl::hasTrivialBody() const
1475{
1476 Stmt *S = getBody();
1477 if (!S) {
1478 // Since we don't have a body for this function, we don't know if it's
1479 // trivial or not.
1480 return false;
1481 }
1482
1483 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1484 return true;
1485 return false;
1486}
1487
Sean Hunt10620eb2011-05-06 20:44:56 +00001488bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1489 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001490 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001491 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1492 return true;
1493 }
1494 }
1495
1496 return false;
1497}
1498
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001499Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001500 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1501 if (I->Body) {
1502 Definition = *I;
1503 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001504 } else if (I->IsLateTemplateParsed) {
1505 Definition = *I;
1506 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001507 }
1508 }
1509
1510 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001511}
1512
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001513void FunctionDecl::setBody(Stmt *B) {
1514 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001515 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001516 EndRangeLoc = B->getLocEnd();
1517}
1518
Douglas Gregor21386642010-09-28 21:55:22 +00001519void FunctionDecl::setPure(bool P) {
1520 IsPure = P;
1521 if (P)
1522 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1523 Parent->markedVirtualFunctionPure();
1524}
1525
Douglas Gregor48a83b52009-09-12 00:17:51 +00001526bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001527 const TranslationUnitDecl *tunit =
1528 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1529 return tunit &&
Sean Hunt55878032011-05-15 20:59:31 +00001530 !tunit->getASTContext().getLangOptions().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001531 getIdentifier() &&
1532 getIdentifier()->isStr("main");
1533}
1534
1535bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1536 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1537 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1538 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1539 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1540 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1541
1542 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1543 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1544
1545 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1546 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1547
1548 ASTContext &Context =
1549 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1550 ->getASTContext();
1551
1552 // The result type and first argument type are constant across all
1553 // these operators. The second argument must be exactly void*.
1554 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001555}
1556
Douglas Gregor48a83b52009-09-12 00:17:51 +00001557bool FunctionDecl::isExternC() const {
1558 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001559 // In C, any non-static, non-overloadable function has external
1560 // linkage.
1561 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001562 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001563
Chandler Carruth10aad442011-02-25 00:05:02 +00001564 const DeclContext *DC = getDeclContext();
1565 if (DC->isRecord())
1566 return false;
1567
1568 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor63935192009-03-02 00:19:53 +00001569 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1570 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001571 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001572 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001573
1574 break;
1575 }
1576 }
1577
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001578 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001579}
1580
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001581bool FunctionDecl::isGlobal() const {
1582 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1583 return Method->isStatic();
1584
John McCalld931b082010-08-26 03:08:43 +00001585 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001586 return false;
1587
Mike Stump1eb44332009-09-09 15:08:12 +00001588 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001589 DC->isNamespace();
1590 DC = DC->getParent()) {
1591 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1592 if (!Namespace->getDeclName())
1593 return false;
1594 break;
1595 }
1596 }
1597
1598 return true;
1599}
1600
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001601void
1602FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1603 redeclarable_base::setPreviousDeclaration(PrevDecl);
1604
1605 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1606 FunctionTemplateDecl *PrevFunTmpl
1607 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1608 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1609 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1610 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001611
Axel Naumannd9d137e2011-11-08 18:21:06 +00001612 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001613 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001614}
1615
1616const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1617 return getFirstDeclaration();
1618}
1619
1620FunctionDecl *FunctionDecl::getCanonicalDecl() {
1621 return getFirstDeclaration();
1622}
1623
Douglas Gregor381d34e2010-12-06 18:36:25 +00001624void FunctionDecl::setStorageClass(StorageClass SC) {
1625 assert(isLegalForFunction(SC));
1626 if (getStorageClass() != SC)
1627 ClearLinkageCache();
1628
1629 SClass = SC;
1630}
1631
Douglas Gregor3e41d602009-02-13 23:20:09 +00001632/// \brief Returns a value indicating whether this function
1633/// corresponds to a builtin function.
1634///
1635/// The function corresponds to a built-in function if it is
1636/// declared at translation scope or within an extern "C" block and
1637/// its name matches with the name of a builtin. The returned value
1638/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001639/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001640/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001641unsigned FunctionDecl::getBuiltinID() const {
1642 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001643 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1644 return 0;
1645
1646 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1647 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1648 return BuiltinID;
1649
1650 // This function has the name of a known C library
1651 // function. Determine whether it actually refers to the C library
1652 // function or whether it just has the same name.
1653
Douglas Gregor9add3172009-02-17 03:23:10 +00001654 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001655 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001656 return 0;
1657
Douglas Gregor3c385e52009-02-14 18:57:46 +00001658 // If this function is at translation-unit scope and we're not in
1659 // C++, it refers to the C library function.
1660 if (!Context.getLangOptions().CPlusPlus &&
1661 getDeclContext()->isTranslationUnit())
1662 return BuiltinID;
1663
1664 // If the function is in an extern "C" linkage specification and is
1665 // not marked "overloadable", it's the real function.
1666 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001667 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001668 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001669 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001670 return BuiltinID;
1671
1672 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001673 return 0;
1674}
1675
1676
Chris Lattner1ad9b282009-04-25 06:03:53 +00001677/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001678/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001679/// after it has been created.
1680unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001681 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001682 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001683 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001684 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Reid Spencer5f016e22007-07-11 17:01:13 +00001686}
1687
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001688void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001689 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001690 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001691 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001694 if (!NewParamInfo.empty()) {
1695 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1696 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 }
1698}
1699
Chris Lattner8123a952008-04-10 02:22:51 +00001700/// getMinRequiredArguments - Returns the minimum number of arguments
1701/// needed to call this function. This may be fewer than the number of
1702/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001703/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001704unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001705 if (!getASTContext().getLangOptions().CPlusPlus)
1706 return getNumParams();
1707
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001708 unsigned NumRequiredArgs = getNumParams();
1709
1710 // If the last parameter is a parameter pack, we don't need an argument for
1711 // it.
1712 if (NumRequiredArgs > 0 &&
1713 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1714 --NumRequiredArgs;
1715
1716 // If this parameter has a default argument, we don't need an argument for
1717 // it.
1718 while (NumRequiredArgs > 0 &&
1719 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001720 --NumRequiredArgs;
1721
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001722 // We might have parameter packs before the end. These can't be deduced,
1723 // but they can still handle multiple arguments.
1724 unsigned ArgIdx = NumRequiredArgs;
1725 while (ArgIdx > 0) {
1726 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1727 NumRequiredArgs = ArgIdx;
1728
1729 --ArgIdx;
1730 }
1731
Chris Lattner8123a952008-04-10 02:22:51 +00001732 return NumRequiredArgs;
1733}
1734
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001735bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001736 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001737 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001738
1739 if (isa<CXXMethodDecl>(this)) {
1740 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1741 return true;
1742 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001743
1744 switch (getTemplateSpecializationKind()) {
1745 case TSK_Undeclared:
1746 case TSK_ExplicitSpecialization:
1747 return false;
1748
1749 case TSK_ImplicitInstantiation:
1750 case TSK_ExplicitInstantiationDeclaration:
1751 case TSK_ExplicitInstantiationDefinition:
1752 // Handle below.
1753 break;
1754 }
1755
1756 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001757 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001758 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001759 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001760
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001761 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001762 return PatternDecl->isInlined();
1763
1764 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001765}
1766
Nick Lewyckydce67a72011-07-18 05:26:13 +00001767/// \brief For a function declaration in C or C++, determine whether this
1768/// declaration causes the definition to be externally visible.
1769///
1770/// Determines whether this is the first non-inline redeclaration of an inline
1771/// function in a language where "inline" does not normally require an
1772/// externally visible definition.
1773bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1774 assert(!doesThisDeclarationHaveABody() &&
1775 "Must have a declaration without a body.");
1776
1777 ASTContext &Context = getASTContext();
1778
1779 // In C99 mode, a function may have an inline definition (causing it to
1780 // be deferred) then redeclared later. As a special case, "extern inline"
1781 // is not required to produce an external symbol.
1782 if (Context.getLangOptions().GNUInline || !Context.getLangOptions().C99 ||
1783 Context.getLangOptions().CPlusPlus)
1784 return false;
1785 if (getLinkage() != ExternalLinkage || isInlineSpecified())
1786 return false;
Nick Lewyckyf57ef052011-07-18 07:11:55 +00001787 const FunctionDecl *Definition = 0;
1788 if (hasBody(Definition))
1789 return Definition->isInlined() &&
1790 Definition->isInlineDefinitionExternallyVisible();
Nick Lewyckydce67a72011-07-18 05:26:13 +00001791 return false;
1792}
1793
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001794/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001795/// definition will be externally visible.
1796///
1797/// Inline function definitions are always available for inlining optimizations.
1798/// However, depending on the language dialect, declaration specifiers, and
1799/// attributes, the definition of an inline function may or may not be
1800/// "externally" visible to other translation units in the program.
1801///
1802/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001803/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001804/// inline definition becomes externally visible (C99 6.7.4p6).
1805///
1806/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1807/// definition, we use the GNU semantics for inline, which are nearly the
1808/// opposite of C99 semantics. In particular, "inline" by itself will create
1809/// an externally visible symbol, but "extern inline" will not create an
1810/// externally visible symbol.
1811bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00001812 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001813 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001814 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001815
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00001816 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001817 // If it's not the case that both 'inline' and 'extern' are
1818 // specified on the definition, then this inline definition is
1819 // externally visible.
1820 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1821 return true;
1822
1823 // If any declaration is 'inline' but not 'extern', then this definition
1824 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001825 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1826 Redecl != RedeclEnd;
1827 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001828 if (Redecl->isInlineSpecified() &&
1829 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001830 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001831 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001832
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001833 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001834 }
1835
1836 // C99 6.7.4p6:
1837 // [...] If all of the file scope declarations for a function in a
1838 // translation unit include the inline function specifier without extern,
1839 // then the definition in that translation unit is an inline definition.
1840 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1841 Redecl != RedeclEnd;
1842 ++Redecl) {
1843 // Only consider file-scope declarations in this test.
1844 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1845 continue;
Eli Friedman8a1d6a52011-10-11 22:09:24 +00001846
1847 // Only consider explicit declarations; the presence of a builtin for a
1848 // libcall shouldn't affect whether a definition is externally visible.
1849 if (Redecl->isImplicit())
1850 continue;
1851
John McCalld931b082010-08-26 03:08:43 +00001852 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001853 return true; // Not an inline definition
1854 }
1855
1856 // C99 6.7.4p6:
1857 // An inline definition does not provide an external definition for the
1858 // function, and does not forbid an external definition in another
1859 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001860 return false;
1861}
1862
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001863/// getOverloadedOperator - Which C++ overloaded operator this
1864/// function represents, if any.
1865OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001866 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1867 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001868 else
1869 return OO_None;
1870}
1871
Sean Hunta6c058d2010-01-13 09:01:02 +00001872/// getLiteralIdentifier - The literal suffix identifier this function
1873/// represents, if any.
1874const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1875 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1876 return getDeclName().getCXXLiteralIdentifier();
1877 else
1878 return 0;
1879}
1880
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001881FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1882 if (TemplateOrSpecialization.isNull())
1883 return TK_NonTemplate;
1884 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1885 return TK_FunctionTemplate;
1886 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1887 return TK_MemberSpecialization;
1888 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1889 return TK_FunctionTemplateSpecialization;
1890 if (TemplateOrSpecialization.is
1891 <DependentFunctionTemplateSpecializationInfo*>())
1892 return TK_DependentFunctionTemplateSpecialization;
1893
David Blaikieb219cfc2011-09-23 05:06:16 +00001894 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001895}
1896
Douglas Gregor2db32322009-10-07 23:56:10 +00001897FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001898 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001899 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1900
1901 return 0;
1902}
1903
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001904MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1905 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1906}
1907
Douglas Gregor2db32322009-10-07 23:56:10 +00001908void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001909FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1910 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001911 TemplateSpecializationKind TSK) {
1912 assert(TemplateOrSpecialization.isNull() &&
1913 "Member function is already a specialization");
1914 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001915 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001916 TemplateOrSpecialization = Info;
1917}
1918
Douglas Gregor3b846b62009-10-27 20:53:28 +00001919bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001920 // If the function is invalid, it can't be implicitly instantiated.
1921 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001922 return false;
1923
1924 switch (getTemplateSpecializationKind()) {
1925 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00001926 case TSK_ExplicitInstantiationDefinition:
1927 return false;
1928
1929 case TSK_ImplicitInstantiation:
1930 return true;
1931
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001932 // It is possible to instantiate TSK_ExplicitSpecialization kind
1933 // if the FunctionDecl has a class scope specialization pattern.
1934 case TSK_ExplicitSpecialization:
1935 return getClassScopeSpecializationPattern() != 0;
1936
Douglas Gregor3b846b62009-10-27 20:53:28 +00001937 case TSK_ExplicitInstantiationDeclaration:
1938 // Handled below.
1939 break;
1940 }
1941
1942 // Find the actual template from which we will instantiate.
1943 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001944 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001945 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001946 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001947
1948 // C++0x [temp.explicit]p9:
1949 // Except for inline functions, other explicit instantiation declarations
1950 // have the effect of suppressing the implicit instantiation of the entity
1951 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001952 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001953 return true;
1954
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001955 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00001956}
1957
1958bool FunctionDecl::isTemplateInstantiation() const {
1959 switch (getTemplateSpecializationKind()) {
1960 case TSK_Undeclared:
1961 case TSK_ExplicitSpecialization:
1962 return false;
1963 case TSK_ImplicitInstantiation:
1964 case TSK_ExplicitInstantiationDeclaration:
1965 case TSK_ExplicitInstantiationDefinition:
1966 return true;
1967 }
1968 llvm_unreachable("All TSK values handled.");
1969}
Douglas Gregor3b846b62009-10-27 20:53:28 +00001970
1971FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00001972 // Handle class scope explicit specialization special case.
1973 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1974 return getClassScopeSpecializationPattern();
1975
Douglas Gregor3b846b62009-10-27 20:53:28 +00001976 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1977 while (Primary->getInstantiatedFromMemberTemplate()) {
1978 // If we have hit a point where the user provided a specialization of
1979 // this template, we're done looking.
1980 if (Primary->isMemberSpecialization())
1981 break;
1982
1983 Primary = Primary->getInstantiatedFromMemberTemplate();
1984 }
1985
1986 return Primary->getTemplatedDecl();
1987 }
1988
1989 return getInstantiatedFromMemberFunction();
1990}
1991
Douglas Gregor16e8be22009-06-29 17:30:29 +00001992FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001993 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001994 = TemplateOrSpecialization
1995 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001996 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001997 }
1998 return 0;
1999}
2000
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002001FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2002 return getASTContext().getClassScopeSpecializationPattern(this);
2003}
2004
Douglas Gregor16e8be22009-06-29 17:30:29 +00002005const TemplateArgumentList *
2006FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002007 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002008 = TemplateOrSpecialization
2009 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002010 return Info->TemplateArguments;
2011 }
2012 return 0;
2013}
2014
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002015const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002016FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2017 if (FunctionTemplateSpecializationInfo *Info
2018 = TemplateOrSpecialization
2019 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2020 return Info->TemplateArgumentsAsWritten;
2021 }
2022 return 0;
2023}
2024
Mike Stump1eb44332009-09-09 15:08:12 +00002025void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002026FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2027 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002028 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002029 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002030 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002031 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2032 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002033 assert(TSK != TSK_Undeclared &&
2034 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002035 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002036 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002037 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002038 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2039 TemplateArgs,
2040 TemplateArgsAsWritten,
2041 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002042 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Douglas Gregor127102b2009-06-29 20:59:39 +00002044 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002045 // function template specializations.
2046 if (InsertPos)
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00002047 Template->addSpecialization(Info, InsertPos);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002048 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00002049 // Try to insert the new node. If there is an existing node, leave it, the
2050 // set will contain the canonical decls while
2051 // FunctionTemplateDecl::findSpecialization will return
2052 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002053 FunctionTemplateSpecializationInfo *Existing
2054 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00002055 (void)Existing;
2056 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
2057 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002058 }
Douglas Gregor1637be72009-06-26 00:10:03 +00002059}
2060
John McCallaf2094e2010-04-08 09:05:18 +00002061void
2062FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2063 const UnresolvedSetImpl &Templates,
2064 const TemplateArgumentListInfo &TemplateArgs) {
2065 assert(TemplateOrSpecialization.isNull());
2066 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2067 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002068 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002069 void *Buffer = Context.Allocate(Size);
2070 DependentFunctionTemplateSpecializationInfo *Info =
2071 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2072 TemplateArgs);
2073 TemplateOrSpecialization = Info;
2074}
2075
2076DependentFunctionTemplateSpecializationInfo::
2077DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2078 const TemplateArgumentListInfo &TArgs)
2079 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2080
2081 d.NumTemplates = Ts.size();
2082 d.NumArgs = TArgs.size();
2083
2084 FunctionTemplateDecl **TsArray =
2085 const_cast<FunctionTemplateDecl**>(getTemplates());
2086 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2087 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2088
2089 TemplateArgumentLoc *ArgsArray =
2090 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2091 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2092 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2093}
2094
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002095TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002096 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002097 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002098 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002099 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002100 if (FTSInfo)
2101 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Douglas Gregor2db32322009-10-07 23:56:10 +00002103 MemberSpecializationInfo *MSInfo
2104 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2105 if (MSInfo)
2106 return MSInfo->getTemplateSpecializationKind();
2107
2108 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002109}
2110
Mike Stump1eb44332009-09-09 15:08:12 +00002111void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002112FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2113 SourceLocation PointOfInstantiation) {
2114 if (FunctionTemplateSpecializationInfo *FTSInfo
2115 = TemplateOrSpecialization.dyn_cast<
2116 FunctionTemplateSpecializationInfo*>()) {
2117 FTSInfo->setTemplateSpecializationKind(TSK);
2118 if (TSK != TSK_ExplicitSpecialization &&
2119 PointOfInstantiation.isValid() &&
2120 FTSInfo->getPointOfInstantiation().isInvalid())
2121 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2122 } else if (MemberSpecializationInfo *MSInfo
2123 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2124 MSInfo->setTemplateSpecializationKind(TSK);
2125 if (TSK != TSK_ExplicitSpecialization &&
2126 PointOfInstantiation.isValid() &&
2127 MSInfo->getPointOfInstantiation().isInvalid())
2128 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2129 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002130 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002131}
2132
2133SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002134 if (FunctionTemplateSpecializationInfo *FTSInfo
2135 = TemplateOrSpecialization.dyn_cast<
2136 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002137 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002138 else if (MemberSpecializationInfo *MSInfo
2139 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002140 return MSInfo->getPointOfInstantiation();
2141
2142 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002143}
2144
Douglas Gregor9f185072009-09-11 20:15:17 +00002145bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002146 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002147 return true;
2148
2149 // If this function was instantiated from a member function of a
2150 // class template, check whether that member function was defined out-of-line.
2151 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2152 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002153 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002154 return Definition->isOutOfLine();
2155 }
2156
2157 // If this function was instantiated from a function template,
2158 // check whether that function template was defined out-of-line.
2159 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2160 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002161 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002162 return Definition->isOutOfLine();
2163 }
2164
2165 return false;
2166}
2167
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002168SourceRange FunctionDecl::getSourceRange() const {
2169 return SourceRange(getOuterLocStart(), EndRangeLoc);
2170}
2171
Chris Lattner8a934232008-03-31 00:36:02 +00002172//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002173// FieldDecl Implementation
2174//===----------------------------------------------------------------------===//
2175
Jay Foad4ba2a172011-01-12 09:06:06 +00002176FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002177 SourceLocation StartLoc, SourceLocation IdLoc,
2178 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002179 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2180 bool HasInit) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002181 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith7a614d82011-06-11 17:19:42 +00002182 BW, Mutable, HasInit);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002183}
2184
2185bool FieldDecl::isAnonymousStructOrUnion() const {
2186 if (!isImplicit() || getDeclName())
2187 return false;
2188
2189 if (const RecordType *Record = getType()->getAs<RecordType>())
2190 return Record->getDecl()->isAnonymousStructOrUnion();
2191
2192 return false;
2193}
2194
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002195unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2196 assert(isBitField() && "not a bitfield");
2197 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2198 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2199}
2200
John McCallba4f5d52011-01-20 07:57:12 +00002201unsigned FieldDecl::getFieldIndex() const {
2202 if (CachedFieldIndex) return CachedFieldIndex - 1;
2203
Richard Smith180f4792011-11-10 06:34:14 +00002204 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002205 const RecordDecl *RD = getParent();
2206 const FieldDecl *LastFD = 0;
2207 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smith180f4792011-11-10 06:34:14 +00002208
2209 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2210 I != E; ++I, ++Index) {
2211 (*I)->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002212
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002213 if (IsMsStruct) {
2214 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smith180f4792011-11-10 06:34:14 +00002215 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2216 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002217 continue;
2218 }
Richard Smith180f4792011-11-10 06:34:14 +00002219 LastFD = (*I);
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002220 }
John McCallba4f5d52011-01-20 07:57:12 +00002221 }
2222
Richard Smith180f4792011-11-10 06:34:14 +00002223 assert(CachedFieldIndex && "failed to find field in parent");
2224 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002225}
2226
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002227SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002228 if (const Expr *E = InitializerOrBitWidth.getPointer())
2229 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002230 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002231}
2232
Richard Smith7a614d82011-06-11 17:19:42 +00002233void FieldDecl::setInClassInitializer(Expr *Init) {
2234 assert(!InitializerOrBitWidth.getPointer() &&
2235 "bit width or initializer already set");
2236 InitializerOrBitWidth.setPointer(Init);
2237 InitializerOrBitWidth.setInt(0);
2238}
2239
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002240//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002241// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002242//===----------------------------------------------------------------------===//
2243
Douglas Gregor1693e152010-07-06 18:42:40 +00002244SourceLocation TagDecl::getOuterLocStart() const {
2245 return getTemplateOrInnerLocStart(this);
2246}
2247
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002248SourceRange TagDecl::getSourceRange() const {
2249 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002250 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002251}
2252
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002253TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002254 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002255}
2256
Richard Smith162e1c12011-04-15 14:24:37 +00002257void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2258 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002259 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002260 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002261 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002262}
2263
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002264void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002265 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002266
2267 if (isa<CXXRecordDecl>(this)) {
2268 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2269 struct CXXRecordDecl::DefinitionData *Data =
2270 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002271 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2272 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002273 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002274}
2275
2276void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002277 assert((!isa<CXXRecordDecl>(this) ||
2278 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2279 "definition completed but not started");
2280
John McCall5e1cdac2011-10-07 06:10:15 +00002281 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002282 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002283
2284 if (ASTMutationListener *L = getASTMutationListener())
2285 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002286}
2287
John McCall5e1cdac2011-10-07 06:10:15 +00002288TagDecl *TagDecl::getDefinition() const {
2289 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002290 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002291 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2292 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002293
2294 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002295 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002296 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002297 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002299 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002300}
2301
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002302void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2303 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002304 // Make sure the extended qualifier info is allocated.
2305 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002306 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002307 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002308 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002309 } else {
John McCallb6217662010-03-15 10:12:16 +00002310 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002311 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002312 if (getExtInfo()->NumTemplParamLists == 0) {
2313 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002314 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002315 }
2316 else
2317 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002318 }
2319 }
2320}
2321
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002322void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2323 unsigned NumTPLists,
2324 TemplateParameterList **TPLists) {
2325 assert(NumTPLists > 0);
2326 // Make sure the extended decl info is allocated.
2327 if (!hasExtInfo())
2328 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002329 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002330 // Set the template parameter lists info.
2331 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2332}
2333
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002334//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002335// EnumDecl Implementation
2336//===----------------------------------------------------------------------===//
2337
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002338EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2339 SourceLocation StartLoc, SourceLocation IdLoc,
2340 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002341 EnumDecl *PrevDecl, bool IsScoped,
2342 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002343 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002344 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002345 C.getTypeDeclType(Enum, PrevDecl);
2346 return Enum;
2347}
2348
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002349EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002350 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002351 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002352}
2353
Douglas Gregor838db382010-02-11 01:19:42 +00002354void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002355 QualType NewPromotionType,
2356 unsigned NumPositiveBits,
2357 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002358 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002359 if (!IntegerType)
2360 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002361 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002362 setNumPositiveBits(NumPositiveBits);
2363 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002364 TagDecl::completeDefinition();
2365}
2366
2367//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002368// RecordDecl Implementation
2369//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002370
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002371RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2372 SourceLocation StartLoc, SourceLocation IdLoc,
2373 IdentifierInfo *Id, RecordDecl *PrevDecl)
2374 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002375 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002376 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002377 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002378 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002379 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002380}
2381
Jay Foad4ba2a172011-01-12 09:06:06 +00002382RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002383 SourceLocation StartLoc, SourceLocation IdLoc,
2384 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2385 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2386 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002387 C.getTypeDeclType(R, PrevDecl);
2388 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002389}
2390
Jay Foad4ba2a172011-01-12 09:06:06 +00002391RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002392 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2393 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002394}
2395
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002396bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002397 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002398 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2399}
2400
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002401RecordDecl::field_iterator RecordDecl::field_begin() const {
2402 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2403 LoadFieldsFromExternalStorage();
2404
2405 return field_iterator(decl_iterator(FirstDecl));
2406}
2407
Douglas Gregorda2142f2011-02-19 18:51:44 +00002408/// completeDefinition - Notes that the definition of this type is now
2409/// complete.
2410void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002411 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002412 TagDecl::completeDefinition();
2413}
2414
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002415void RecordDecl::LoadFieldsFromExternalStorage() const {
2416 ExternalASTSource *Source = getASTContext().getExternalSource();
2417 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2418
2419 // Notify that we have a RecordDecl doing some initialization.
2420 ExternalASTSource::Deserializing TheFields(Source);
2421
Chris Lattner5f9e2722011-07-23 10:55:15 +00002422 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002423 LoadedFieldsFromExternalStorage = true;
2424 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2425 case ELR_Success:
2426 break;
2427
2428 case ELR_AlreadyLoaded:
2429 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002430 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002431 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002432
2433#ifndef NDEBUG
2434 // Check that all decls we got were FieldDecls.
2435 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2436 assert(isa<FieldDecl>(Decls[i]));
2437#endif
2438
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002439 if (Decls.empty())
2440 return;
2441
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002442 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2443 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002444}
2445
Steve Naroff56ee6892008-10-08 17:01:13 +00002446//===----------------------------------------------------------------------===//
2447// BlockDecl Implementation
2448//===----------------------------------------------------------------------===//
2449
David Blaikie4278c652011-09-21 18:16:56 +00002450void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002451 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Steve Naroffe78b8092009-03-13 16:56:44 +00002453 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002454 if (!NewParamInfo.empty()) {
2455 NumParams = NewParamInfo.size();
2456 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2457 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002458 }
2459}
2460
John McCall6b5a61b2011-02-07 10:33:21 +00002461void BlockDecl::setCaptures(ASTContext &Context,
2462 const Capture *begin,
2463 const Capture *end,
2464 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002465 CapturesCXXThis = capturesCXXThis;
2466
2467 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002468 NumCaptures = 0;
2469 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002470 return;
2471 }
2472
John McCall6b5a61b2011-02-07 10:33:21 +00002473 NumCaptures = end - begin;
2474
2475 // Avoid new Capture[] because we don't want to provide a default
2476 // constructor.
2477 size_t allocationSize = NumCaptures * sizeof(Capture);
2478 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2479 memcpy(buffer, begin, allocationSize);
2480 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002481}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002482
John McCall204e1332011-06-15 22:51:16 +00002483bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2484 for (capture_const_iterator
2485 i = capture_begin(), e = capture_end(); i != e; ++i)
2486 // Only auto vars can be captured, so no redeclaration worries.
2487 if (i->getVariable() == variable)
2488 return true;
2489
2490 return false;
2491}
2492
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002493SourceRange BlockDecl::getSourceRange() const {
2494 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2495}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002496
2497//===----------------------------------------------------------------------===//
2498// Other Decl Allocation/Deallocation Method Implementations
2499//===----------------------------------------------------------------------===//
2500
2501TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2502 return new (C) TranslationUnitDecl(C);
2503}
2504
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002505LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002506 SourceLocation IdentL, IdentifierInfo *II) {
2507 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2508}
2509
2510LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2511 SourceLocation IdentL, IdentifierInfo *II,
2512 SourceLocation GnuLabelL) {
2513 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2514 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002515}
2516
2517
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002518NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002519 SourceLocation StartLoc,
2520 SourceLocation IdLoc, IdentifierInfo *Id) {
2521 return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002522}
2523
Douglas Gregor06c91932010-10-27 19:49:05 +00002524NamespaceDecl *NamespaceDecl::getNextNamespace() {
2525 return dyn_cast_or_null<NamespaceDecl>(
2526 NextNamespace.get(getASTContext().getExternalSource()));
2527}
2528
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002529ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002530 SourceLocation IdLoc,
2531 IdentifierInfo *Id,
2532 QualType Type) {
2533 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002534}
2535
2536FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002537 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002538 const DeclarationNameInfo &NameInfo,
2539 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002540 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002541 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002542 bool hasWrittenPrototype,
2543 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002544 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2545 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002546 isInlineSpecified,
2547 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002548 New->HasWrittenPrototype = hasWrittenPrototype;
2549 return New;
2550}
2551
2552BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2553 return new (C) BlockDecl(DC, L);
2554}
2555
2556EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2557 SourceLocation L,
2558 IdentifierInfo *Id, QualType T,
2559 Expr *E, const llvm::APSInt &V) {
2560 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2561}
2562
Benjamin Kramerd9811462010-11-21 14:11:41 +00002563IndirectFieldDecl *
2564IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2565 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2566 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002567 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2568}
2569
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002570SourceRange EnumConstantDecl::getSourceRange() const {
2571 SourceLocation End = getLocation();
2572 if (Init)
2573 End = Init->getLocEnd();
2574 return SourceRange(getLocation(), End);
2575}
2576
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002577TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002578 SourceLocation StartLoc, SourceLocation IdLoc,
2579 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2580 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002581}
2582
Richard Smith162e1c12011-04-15 14:24:37 +00002583TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2584 SourceLocation StartLoc,
2585 SourceLocation IdLoc, IdentifierInfo *Id,
2586 TypeSourceInfo *TInfo) {
2587 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2588}
2589
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002590SourceRange TypedefDecl::getSourceRange() const {
2591 SourceLocation RangeEnd = getLocation();
2592 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2593 if (typeIsPostfix(TInfo->getType()))
2594 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2595 }
2596 return SourceRange(getLocStart(), RangeEnd);
2597}
2598
Richard Smith162e1c12011-04-15 14:24:37 +00002599SourceRange TypeAliasDecl::getSourceRange() const {
2600 SourceLocation RangeEnd = getLocStart();
2601 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2602 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2603 return SourceRange(getLocStart(), RangeEnd);
2604}
2605
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002606FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002607 StringLiteral *Str,
2608 SourceLocation AsmLoc,
2609 SourceLocation RParenLoc) {
2610 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002611}