blob: 3a2d532f183200358b9e962eda8c7f78edfd15d3 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000028#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000029#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000030
David Blaikie9c70e042011-09-21 18:16:56 +000031#include <algorithm>
32
Chris Lattner6d9a6852006-10-25 05:11:20 +000033using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000034
Chris Lattner88f70d62008-03-15 05:43:15 +000035//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000036// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000037//===----------------------------------------------------------------------===//
38
Douglas Gregor1baf38f2011-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 McCall659a3372010-12-18 03:30:47 +000050
John McCall457a04e2010-10-22 21:05:15 +000051 return DefaultVisibility;
John McCall457a04e2010-10-22 21:05:15 +000052 }
Douglas Gregor1baf38f2011-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 Gregore8bbc122011-09-02 00:18:52 +000056 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-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 McCall457a04e2010-10-22 21:05:15 +000066}
67
John McCallc273f242010-10-30 11:50:40 +000068typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall457a04e2010-10-22 21:05:15 +000069typedef std::pair<Linkage,Visibility> LVPair;
John McCallc273f242010-10-30 11:50:40 +000070
John McCall457a04e2010-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 McCallc273f242010-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 Kramer396dcf32010-11-05 19:56:37 +000081namespace {
John McCall07072662010-11-02 01:45:15 +000082/// Flags controlling the computation of linkage and visibility.
83struct LVFlags {
84 bool ConsiderGlobalVisibility;
85 bool ConsiderVisibilityAttributes;
John McCall8bc6d5b2011-03-04 10:39:25 +000086 bool ConsiderTemplateParameterTypes;
John McCall07072662010-11-02 01:45:15 +000087
88 LVFlags() : ConsiderGlobalVisibility(true),
John McCall8bc6d5b2011-03-04 10:39:25 +000089 ConsiderVisibilityAttributes(true),
90 ConsiderTemplateParameterTypes(true) {
John McCall07072662010-11-02 01:45:15 +000091 }
92
Douglas Gregorbf62d642010-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 McCall8bc6d5b2011-03-04 10:39:25 +000099 F.ConsiderTemplateParameterTypes = false;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000100 return F;
101 }
102
John McCall07072662010-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 McCall8bc6d5b2011-03-04 10:39:25 +0000109 F.ConsiderTemplateParameterTypes = false;
John McCall07072662010-11-02 01:45:15 +0000110 return F;
111 }
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000112};
Benjamin Kramer396dcf32010-11-05 19:56:37 +0000113} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +0000114
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000115/// \brief Get the most restrictive linkage for the types in the given
116/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +0000117static LVPair
118getLVForTemplateParameterList(const TemplateParameterList *Params) {
119 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000120 for (TemplateParameterList::const_iterator P = Params->begin(),
121 PEnd = Params->end();
122 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-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 Gregor7dc5c172010-02-03 09:33:45 +0000133 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000134 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 continue;
136 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000137 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000138
139 if (TemplateTemplateParmDecl *TTP
140 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000141 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000142 }
143 }
144
John McCall457a04e2010-10-22 21:05:15 +0000145 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000146}
147
Douglas Gregorbf62d642010-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 Gregor7dc5c172010-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 McCall457a04e2010-10-22 21:05:15 +0000153static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregorbf62d642010-12-06 18:36:25 +0000154 unsigned NumArgs,
155 LVFlags &F) {
John McCall457a04e2010-10-22 21:05:15 +0000156 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-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 McCall457a04e2010-10-22 21:05:15 +0000166 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000167 break;
168
169 case TemplateArgument::Declaration:
John McCall457a04e2010-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 Gregor91df6cf2010-12-06 18:50:56 +0000173 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
174 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000175 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000176 break;
177
178 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000179 case TemplateArgument::TemplateExpansion:
180 if (TemplateDecl *Template
181 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000182 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000183 break;
184
185 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000186 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregorbf62d642010-12-06 18:36:25 +0000187 Args[I].pack_size(),
188 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000189 break;
190 }
191 }
192
John McCall457a04e2010-10-22 21:05:15 +0000193 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000194}
195
John McCallc273f242010-10-30 11:50:40 +0000196static LVPair
Douglas Gregorbf62d642010-12-06 18:36:25 +0000197getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
198 LVFlags &F) {
199 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000200}
201
John McCallb8c604a2011-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 McCall07072662010-11-02 01:45:15 +0000212static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000213 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-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 McCall8e7d6562010-08-26 03:08:43 +0000225 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000226 return LinkageInfo::internal();
Douglas Gregorf73b2822009-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 Friedmanf873c2f2009-11-26 03:04:01 +0000233 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000234 Var->getStorageClass() != SC_Extern &&
235 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000236 bool FoundExtern = false;
237 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
238 PrevVar && !FoundExtern;
239 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000240 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000241 FoundExtern = true;
242
243 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000244 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000245 }
Fariborz Jahanian8feee2d2011-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 Gregorf73b2822009-11-25 22:24:25 +0000254 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-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 McCall8e7d6562010-08-26 03:08:43 +0000266 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000267 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-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 McCallc273f242010-10-30 11:50:40 +0000271 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000272 }
273
Chandler Carruth9682a2fd2011-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 McCallb7139c42010-10-28 04:18:25 +0000280
John McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +0000287 LinkageInfo LV;
288
John McCall07072662010-11-02 01:45:15 +0000289 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000290 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
291 LV.setVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000292 F.ConsiderGlobalVisibility = false;
John McCall2faf32c2010-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 Gregor1baf38f2011-03-26 12:10:19 +0000300 if (llvm::Optional<Visibility> Vis
301 = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
302 LV.setVisibility(*Vis, false);
John McCall2faf32c2010-12-10 02:59:44 +0000303 F.ConsiderGlobalVisibility = false;
304 break;
305 }
306 }
John McCall07072662010-11-02 01:45:15 +0000307 }
John McCallc273f242010-10-30 11:50:40 +0000308 }
John McCall457a04e2010-10-22 21:05:15 +0000309
Douglas Gregorf73b2822009-11-25 22:24:25 +0000310 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000311
Douglas Gregorf73b2822009-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 McCall37bb6c92010-10-29 22:22:43 +0000317 // GCC applies the following optimization to variables and static
318 // data members, but not to functions:
319 //
John McCall457a04e2010-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 McCall5fe84122010-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 McCall457a04e2010-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 McCall36cd5cc2010-10-30 09:18:49 +0000338 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000339 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
340 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000341 return LinkageInfo::uniqueExternal();
342 if (!LV.visibilityExplicit())
343 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000344 }
345
John McCall23032652010-11-02 18:38:13 +0000346 if (Var->getStorageClass() == SC_PrivateExtern)
347 LV.setVisibility(HiddenVisibility, true);
348
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000350 (Var->getStorageClass() == SC_Extern ||
351 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000352
Douglas Gregorf73b2822009-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 Gregorbf62d642010-12-06 18:36:25 +0000363 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000364 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
365 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000366 }
367 }
368
Douglas Gregorf73b2822009-11-25 22:24:25 +0000369 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000370 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-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 McCall457a04e2010-10-22 21:05:15 +0000375
John McCall23032652010-11-02 18:38:13 +0000376 if (Function->getStorageClass() == SC_PrivateExtern)
377 LV.setVisibility(HiddenVisibility, true);
378
Douglas Gregorf73b2822009-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 McCall8e7d6562010-08-26 03:08:43 +0000385 (Function->getStorageClass() == SC_Extern ||
386 Function->getStorageClass() == SC_PrivateExtern ||
387 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-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 Gregorbf62d642010-12-06 18:36:25 +0000398 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000399 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
400 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000401 }
402 }
403
John McCallf768aa72011-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 McCallb8c604a2011-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 Gregor7dc5c172010-02-03 09:33:45 +0000415 = Function->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-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 Gregor7dc5c172010-02-03 09:33:45 +0000422 }
423
Douglas Gregorf73b2822009-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 McCall457a04e2010-10-22 21:05:15 +0000430 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
431 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000432 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000433 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000434
John McCall457a04e2010-10-22 21:05:15 +0000435 // If this is a class template specialization, consider the
436 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000437 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000438 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCallb8c604a2011-06-27 23:06:04 +0000439 if (shouldConsiderTemplateLV(spec)) {
440 // From the template.
441 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
442 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000443
John McCallb8c604a2011-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 Gregor7dc5c172010-02-03 09:33:45 +0000448 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000449
John McCall5fe84122010-10-26 04:59:26 +0000450 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000451 if (F.ConsiderGlobalVisibility)
452 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000453 (Context.getLangOptions().CPlusPlus &&
454 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000455
Douglas Gregorf73b2822009-11-25 22:24:25 +0000456 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000457 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000458 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000459 if (!isExternalLinkage(EnumLV.linkage()))
460 return LinkageInfo::none();
461 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000462
463 // - a template, unless it is a function template that has
464 // internal linkage (Clause 14);
John McCall8bc6d5b2011-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 Gregor7dc5c172010-02-03 09:33:45 +0000468
Douglas Gregorf73b2822009-11-25 22:24:25 +0000469 // - a namespace (7.3), unless it is declared within an unnamed
470 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000471 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
472 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000473
John McCall457a04e2010-10-22 21:05:15 +0000474 // By extension, we assign external linkage to Objective-C
475 // interfaces.
476 } else if (isa<ObjCInterfaceDecl>(D)) {
477 // fallout
478
479 // Everything not covered here has no linkage.
480 } else {
John McCallc273f242010-10-30 11:50:40 +0000481 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000482 }
483
484 // If we ended up with non-external linkage, visibility should
485 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000486 if (LV.linkage() != ExternalLinkage)
487 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000488
489 // If we didn't end up with hidden visibility, consider attributes
490 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000491 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000492 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000493
494 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000495}
496
John McCall07072662010-11-02 01:45:15 +0000497static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-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 McCall8823c652010-08-13 08:35:10 +0000502 if (!(isa<CXXMethodDecl>(D) ||
503 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000504 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000505 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000506 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000507 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000508
John McCall07072662010-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 Gregor1baf38f2011-03-26 12:10:19 +0000516 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
517 LV.mergeVisibility(*Vis, true);
John McCall07072662010-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 McCallc273f242010-10-30 11:50:40 +0000527
528 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000529 // linkage.
530 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
531 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000532 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000533
534 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000535 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000536 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000537
John McCall8823c652010-08-13 08:35:10 +0000538 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-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 McCall37bb6c92010-10-29 22:22:43 +0000544 TemplateSpecializationKind TSK = TSK_Undeclared;
545
John McCall457a04e2010-10-22 21:05:15 +0000546 // If this is a method template specialization, use the linkage for
547 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000548 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000549 = MD->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-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 McCall37bb6c92010-10-29 22:22:43 +0000556
John McCallb8c604a2011-06-27 23:06:04 +0000557 TSK = spec->getTemplateSpecializationKind();
John McCall37bb6c92010-10-29 22:22:43 +0000558 } else if (MemberSpecializationInfo *MSI =
559 MD->getMemberSpecializationInfo()) {
560 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000561 }
562
John McCall37bb6c92010-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 McCallc273f242010-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 McCall07072662010-11-02 01:45:15 +0000570 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-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 McCall457a04e2010-10-22 21:05:15 +0000579
John McCall37bb6c92010-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 McCallb8c604a2011-06-27 23:06:04 +0000584 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000585 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-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 McCall8bc6d5b2011-03-04 10:39:25 +0000590 if (F.ConsiderTemplateParameterTypes)
591 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000592 spec->getSpecializedTemplate()->getTemplateParameters()));
593 }
John McCall37bb6c92010-10-29 22:22:43 +0000594 }
595
John McCall37bb6c92010-10-29 22:22:43 +0000596 // Static data members.
597 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-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 McCallc273f242010-10-30 11:50:40 +0000602 LV.mergeLinkage(UniqueExternalLinkage);
603 if (!LV.visibilityExplicit())
604 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000605 }
606
John McCall07072662010-11-02 01:45:15 +0000607 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000608
609 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000610 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000611 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000612 }
613
John McCall457a04e2010-10-22 21:05:15 +0000614 return LV;
John McCall8823c652010-08-13 08:35:10 +0000615}
616
John McCalld396b972011-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 McCall83779672011-02-19 02:53:41 +0000638 if (ClassTemplateDecl *temp =
639 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-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 McCall83779672011-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 McCalld396b972011-02-08 19:01:05 +0000649 }
John McCall83779672011-02-19 02:53:41 +0000650
651 // Clear cached linkage for function template decls, too.
652 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000653 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
654 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-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 McCall8f9a4292011-03-22 06:58:49 +0000658 }
John McCall83779672011-02-19 02:53:41 +0000659
John McCalld396b972011-02-08 19:01:05 +0000660}
661
Douglas Gregorbf62d642010-12-06 18:36:25 +0000662Linkage NamedDecl::getLinkage() const {
663 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000664 assert(Linkage(CachedLinkage) ==
665 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-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 McCallc273f242010-10-30 11:50:40 +0000675LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000676 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000677 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000678 HasCachedLinkage = 1;
679 CachedLinkage = LI.linkage();
680 return LI;
John McCall033caa52010-10-29 00:29:13 +0000681}
Ted Kremenek926d8602010-04-20 23:15:35 +0000682
Douglas Gregor1baf38f2011-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 McCall07072662010-11-02 01:45:15 +0000718static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000719 // Objective-C: treat all Objective-C declarations as having external
720 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000721 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000722 default:
723 break;
John McCall457a04e2010-10-22 21:05:15 +0000724 case Decl::TemplateTemplateParm: // count these as external
725 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000726 case Decl::ObjCAtDefsField:
727 case Decl::ObjCCategory:
728 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000729 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000730 case Decl::ObjCForwardProtocol:
731 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000732 case Decl::ObjCMethod:
733 case Decl::ObjCProperty:
734 case Decl::ObjCPropertyImpl:
735 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000736 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000737 }
738
Douglas Gregorf73b2822009-11-25 22:24:25 +0000739 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000740 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000741 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000742
743 // C++ [basic.link]p5:
744 // In addition, a member function, static data member, a named
745 // class or enumeration of class scope, or an unnamed class or
746 // enumeration defined in a class-scope typedef declaration such
747 // that the class or enumeration has the typedef name for linkage
748 // purposes (7.1.3), has external linkage if the name of the class
749 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000750 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000751 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000752
753 // C++ [basic.link]p6:
754 // The name of a function declared in block scope and the name of
755 // an object declared by a block scope extern declaration have
756 // linkage. If there is a visible declaration of an entity with
757 // linkage having the same name and type, ignoring entities
758 // declared outside the innermost enclosing namespace scope, the
759 // block scope declaration declares that same entity and receives
760 // the linkage of the previous declaration. If there is more than
761 // one such matching entity, the program is ill-formed. Otherwise,
762 // if no matching entity is found, the block scope entity receives
763 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000764 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
765 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000766 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000767 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000768
John McCallc273f242010-10-30 11:50:40 +0000769 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000770 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000771 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
772 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000773 }
774
John McCall457a04e2010-10-22 21:05:15 +0000775 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000776 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000777 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
778 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000779 }
780
781 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000782 }
783
John McCall033caa52010-10-29 00:29:13 +0000784 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000785 if (Var->getStorageClass() == SC_Extern ||
786 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000787 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000788 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000789
John McCallc273f242010-10-30 11:50:40 +0000790 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000791 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000792 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000793 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000794 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
795 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000796 }
797
John McCall457a04e2010-10-22 21:05:15 +0000798 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000799 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000800 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
801 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000802 }
803
804 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000805 }
806 }
807
808 // C++ [basic.link]p6:
809 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000810 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000811}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000812
Douglas Gregor2ada0482009-02-04 17:27:36 +0000813std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000814 return getQualifiedNameAsString(getASTContext().getLangOptions());
815}
816
817std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000818 const DeclContext *Ctx = getDeclContext();
819
820 if (Ctx->isFunctionOrMethod())
821 return getNameAsString();
822
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000823 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000824 ContextsTy Contexts;
825
826 // Collect contexts.
827 while (Ctx && isa<NamedDecl>(Ctx)) {
828 Contexts.push_back(Ctx);
829 Ctx = Ctx->getParent();
830 };
831
832 std::string QualName;
833 llvm::raw_string_ostream OS(QualName);
834
835 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
836 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000837 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000838 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000839 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
840 std::string TemplateArgsStr
841 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000842 TemplateArgs.data(),
843 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000844 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000845 OS << Spec->getName() << TemplateArgsStr;
846 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000847 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000848 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000849 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000850 OS << ND;
851 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
852 if (!RD->getIdentifier())
853 OS << "<anonymous " << RD->getKindName() << '>';
854 else
855 OS << RD;
856 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000857 const FunctionProtoType *FT = 0;
858 if (FD->hasWrittenPrototype())
859 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
860
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000861 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000862 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000863 unsigned NumParams = FD->getNumParams();
864 for (unsigned i = 0; i < NumParams; ++i) {
865 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000866 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000867 std::string Param;
868 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000869 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000870 }
871
872 if (FT->isVariadic()) {
873 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000874 OS << ", ";
875 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000876 }
877 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000878 OS << ')';
879 } else {
880 OS << cast<NamedDecl>(*I);
881 }
882 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000883 }
884
John McCalla2a3f7d2010-03-16 21:48:18 +0000885 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000886 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000887 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000888 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000889
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000890 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000891}
892
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000893bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000894 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
895
Douglas Gregor889ceb72009-02-03 19:21:40 +0000896 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
897 // We want to keep it, unless it nominates same namespace.
898 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000899 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
900 ->getOriginalNamespace() ==
901 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
902 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000903 }
Mike Stump11289f42009-09-09 15:08:12 +0000904
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000905 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
906 // For function declarations, we keep track of redeclarations.
907 return FD->getPreviousDeclaration() == OldD;
908
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000909 // For function templates, the underlying function declarations are linked.
910 if (const FunctionTemplateDecl *FunctionTemplate
911 = dyn_cast<FunctionTemplateDecl>(this))
912 if (const FunctionTemplateDecl *OldFunctionTemplate
913 = dyn_cast<FunctionTemplateDecl>(OldD))
914 return FunctionTemplate->getTemplatedDecl()
915 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000916
Steve Naroffc4173fa2009-02-22 19:35:57 +0000917 // For method declarations, we keep track of redeclarations.
918 if (isa<ObjCMethodDecl>(this))
919 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000920
John McCall9f3059a2009-10-09 21:13:30 +0000921 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
922 return true;
923
John McCall3f746822009-11-17 05:59:44 +0000924 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
925 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
926 cast<UsingShadowDecl>(OldD)->getTargetDecl();
927
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000928 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
929 ASTContext &Context = getASTContext();
930 return Context.getCanonicalNestedNameSpecifier(
931 cast<UsingDecl>(this)->getQualifier()) ==
932 Context.getCanonicalNestedNameSpecifier(
933 cast<UsingDecl>(OldD)->getQualifier());
934 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000935
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000936 // For non-function declarations, if the declarations are of the
937 // same kind then this must be a redeclaration, or semantic analysis
938 // would not have given us the new declaration.
939 return this->getKind() == OldD->getKind();
940}
941
Douglas Gregoreddf4332009-02-24 20:03:32 +0000942bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000943 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000944}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000945
Anders Carlsson6915bf62009-06-26 06:29:23 +0000946NamedDecl *NamedDecl::getUnderlyingDecl() {
947 NamedDecl *ND = this;
948 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000949 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000950 ND = UD->getTargetDecl();
951 else if (ObjCCompatibleAliasDecl *AD
952 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
953 return AD->getClassInterface();
954 else
955 return ND;
956 }
957}
958
John McCalla8ae2222010-04-06 21:38:20 +0000959bool NamedDecl::isCXXInstanceMember() const {
960 assert(isCXXClassMember() &&
961 "checking whether non-member is instance member");
962
963 const NamedDecl *D = this;
964 if (isa<UsingShadowDecl>(D))
965 D = cast<UsingShadowDecl>(D)->getTargetDecl();
966
Francois Pichet783dd6e2010-11-21 06:08:52 +0000967 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000968 return true;
969 if (isa<CXXMethodDecl>(D))
970 return cast<CXXMethodDecl>(D)->isInstance();
971 if (isa<FunctionTemplateDecl>(D))
972 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
973 ->getTemplatedDecl())->isInstance();
974 return false;
975}
976
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000977//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000978// DeclaratorDecl Implementation
979//===----------------------------------------------------------------------===//
980
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000981template <typename DeclT>
982static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
983 if (decl->getNumTemplateParameterLists() > 0)
984 return decl->getTemplateParameterList(0)->getTemplateLoc();
985 else
986 return decl->getInnerLocStart();
987}
988
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000989SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000990 TypeSourceInfo *TSI = getTypeSourceInfo();
991 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000992 return SourceLocation();
993}
994
Douglas Gregor14454802011-02-25 02:25:35 +0000995void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
996 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +0000997 // Make sure the extended decl info is allocated.
998 if (!hasExtInfo()) {
999 // Save (non-extended) type source info pointer.
1000 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1001 // Allocate external info struct.
1002 DeclInfo = new (getASTContext()) ExtInfo;
1003 // Restore savedTInfo into (extended) decl info.
1004 getExtInfo()->TInfo = savedTInfo;
1005 }
1006 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001007 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001008 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001009 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001010 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001011 if (getExtInfo()->NumTemplParamLists == 0) {
1012 // Save type source info pointer.
1013 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1014 // Deallocate the extended decl info.
1015 getASTContext().Deallocate(getExtInfo());
1016 // Restore savedTInfo into (non-extended) decl info.
1017 DeclInfo = savedTInfo;
1018 }
1019 else
1020 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001021 }
1022 }
1023}
1024
Abramo Bagnara60804e12011-03-18 15:16:37 +00001025void
1026DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1027 unsigned NumTPLists,
1028 TemplateParameterList **TPLists) {
1029 assert(NumTPLists > 0);
1030 // Make sure the extended decl info is allocated.
1031 if (!hasExtInfo()) {
1032 // Save (non-extended) type source info pointer.
1033 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1034 // Allocate external info struct.
1035 DeclInfo = new (getASTContext()) ExtInfo;
1036 // Restore savedTInfo into (extended) decl info.
1037 getExtInfo()->TInfo = savedTInfo;
1038 }
1039 // Set the template parameter lists info.
1040 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1041}
1042
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001043SourceLocation DeclaratorDecl::getOuterLocStart() const {
1044 return getTemplateOrInnerLocStart(this);
1045}
1046
Abramo Bagnaraea947882011-03-08 16:41:52 +00001047namespace {
1048
1049// Helper function: returns true if QT is or contains a type
1050// having a postfix component.
1051bool typeIsPostfix(clang::QualType QT) {
1052 while (true) {
1053 const Type* T = QT.getTypePtr();
1054 switch (T->getTypeClass()) {
1055 default:
1056 return false;
1057 case Type::Pointer:
1058 QT = cast<PointerType>(T)->getPointeeType();
1059 break;
1060 case Type::BlockPointer:
1061 QT = cast<BlockPointerType>(T)->getPointeeType();
1062 break;
1063 case Type::MemberPointer:
1064 QT = cast<MemberPointerType>(T)->getPointeeType();
1065 break;
1066 case Type::LValueReference:
1067 case Type::RValueReference:
1068 QT = cast<ReferenceType>(T)->getPointeeType();
1069 break;
1070 case Type::PackExpansion:
1071 QT = cast<PackExpansionType>(T)->getPattern();
1072 break;
1073 case Type::Paren:
1074 case Type::ConstantArray:
1075 case Type::DependentSizedArray:
1076 case Type::IncompleteArray:
1077 case Type::VariableArray:
1078 case Type::FunctionProto:
1079 case Type::FunctionNoProto:
1080 return true;
1081 }
1082 }
1083}
1084
1085} // namespace
1086
1087SourceRange DeclaratorDecl::getSourceRange() const {
1088 SourceLocation RangeEnd = getLocation();
1089 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1090 if (typeIsPostfix(TInfo->getType()))
1091 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1092 }
1093 return SourceRange(getOuterLocStart(), RangeEnd);
1094}
1095
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001096void
Douglas Gregor20527e22010-06-15 17:44:38 +00001097QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1098 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001099 TemplateParameterList **TPLists) {
1100 assert((NumTPLists == 0 || TPLists != 0) &&
1101 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001102
1103 // Free previous template parameters (if any).
1104 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001105 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001106 TemplParamLists = 0;
1107 NumTemplParamLists = 0;
1108 }
1109 // Set info on matched template parameter lists (if any).
1110 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001111 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001112 NumTemplParamLists = NumTPLists;
1113 for (unsigned i = NumTPLists; i-- > 0; )
1114 TemplParamLists[i] = TPLists[i];
1115 }
1116}
1117
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001118//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001119// VarDecl Implementation
1120//===----------------------------------------------------------------------===//
1121
Sebastian Redl833ef452010-01-26 22:01:41 +00001122const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1123 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001124 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001125 case SC_Auto: return "auto";
1126 case SC_Extern: return "extern";
1127 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1128 case SC_PrivateExtern: return "__private_extern__";
1129 case SC_Register: return "register";
1130 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001131 }
1132
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001133 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001134 return 0;
1135}
1136
Abramo Bagnaradff19302011-03-08 08:55:46 +00001137VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1138 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001139 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001140 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001141 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001142}
1143
Douglas Gregorbf62d642010-12-06 18:36:25 +00001144void VarDecl::setStorageClass(StorageClass SC) {
1145 assert(isLegalForVariable(SC));
1146 if (getStorageClass() != SC)
1147 ClearLinkageCache();
1148
John McCallbeaa11c2011-05-01 02:13:58 +00001149 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001150}
1151
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001152SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001153 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001154 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001155 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001156}
1157
Sebastian Redl833ef452010-01-26 22:01:41 +00001158bool VarDecl::isExternC() const {
1159 ASTContext &Context = getASTContext();
1160 if (!Context.getLangOptions().CPlusPlus)
1161 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001162 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001163 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1164
Chandler Carruth4322a282011-02-25 00:05:02 +00001165 const DeclContext *DC = getDeclContext();
1166 if (DC->isFunctionOrMethod())
1167 return false;
1168
1169 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001170 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1171 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001172 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001173
1174 break;
1175 }
1176
Sebastian Redl833ef452010-01-26 22:01:41 +00001177 }
1178
1179 return false;
1180}
1181
1182VarDecl *VarDecl::getCanonicalDecl() {
1183 return getFirstDeclaration();
1184}
1185
Sebastian Redl35351a92010-01-31 22:27:38 +00001186VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1187 // C++ [basic.def]p2:
1188 // A declaration is a definition unless [...] it contains the 'extern'
1189 // specifier or a linkage-specification and neither an initializer [...],
1190 // it declares a static data member in a class declaration [...].
1191 // C++ [temp.expl.spec]p15:
1192 // An explicit specialization of a static data member of a template is a
1193 // definition if the declaration includes an initializer; otherwise, it is
1194 // a declaration.
1195 if (isStaticDataMember()) {
1196 if (isOutOfLine() && (hasInit() ||
1197 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1198 return Definition;
1199 else
1200 return DeclarationOnly;
1201 }
1202 // C99 6.7p5:
1203 // A definition of an identifier is a declaration for that identifier that
1204 // [...] causes storage to be reserved for that object.
1205 // Note: that applies for all non-file-scope objects.
1206 // C99 6.9.2p1:
1207 // If the declaration of an identifier for an object has file scope and an
1208 // initializer, the declaration is an external definition for the identifier
1209 if (hasInit())
1210 return Definition;
1211 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1212 if (hasExternalStorage())
1213 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001214
John McCall8e7d6562010-08-26 03:08:43 +00001215 if (getStorageClassAsWritten() == SC_Extern ||
1216 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001217 for (const VarDecl *PrevVar = getPreviousDeclaration();
1218 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1219 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1220 return DeclarationOnly;
1221 }
1222 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001223 // C99 6.9.2p2:
1224 // A declaration of an object that has file scope without an initializer,
1225 // and without a storage class specifier or the scs 'static', constitutes
1226 // a tentative definition.
1227 // No such thing in C++.
1228 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1229 return TentativeDefinition;
1230
1231 // What's left is (in C, block-scope) declarations without initializers or
1232 // external storage. These are definitions.
1233 return Definition;
1234}
1235
Sebastian Redl35351a92010-01-31 22:27:38 +00001236VarDecl *VarDecl::getActingDefinition() {
1237 DefinitionKind Kind = isThisDeclarationADefinition();
1238 if (Kind != TentativeDefinition)
1239 return 0;
1240
Chris Lattner48eb14d2010-06-14 18:31:46 +00001241 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001242 VarDecl *First = getFirstDeclaration();
1243 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1244 I != E; ++I) {
1245 Kind = (*I)->isThisDeclarationADefinition();
1246 if (Kind == Definition)
1247 return 0;
1248 else if (Kind == TentativeDefinition)
1249 LastTentative = *I;
1250 }
1251 return LastTentative;
1252}
1253
1254bool VarDecl::isTentativeDefinitionNow() const {
1255 DefinitionKind Kind = isThisDeclarationADefinition();
1256 if (Kind != TentativeDefinition)
1257 return false;
1258
1259 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1260 if ((*I)->isThisDeclarationADefinition() == Definition)
1261 return false;
1262 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001263 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001264}
1265
Sebastian Redl5ca79842010-02-01 20:16:42 +00001266VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001267 VarDecl *First = getFirstDeclaration();
1268 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1269 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001270 if ((*I)->isThisDeclarationADefinition() == Definition)
1271 return *I;
1272 }
1273 return 0;
1274}
1275
John McCall37bb6c92010-10-29 22:22:43 +00001276VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1277 DefinitionKind Kind = DeclarationOnly;
1278
1279 const VarDecl *First = getFirstDeclaration();
1280 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1281 I != E; ++I)
1282 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1283
1284 return Kind;
1285}
1286
Sebastian Redl5ca79842010-02-01 20:16:42 +00001287const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001288 redecl_iterator I = redecls_begin(), E = redecls_end();
1289 while (I != E && !I->getInit())
1290 ++I;
1291
1292 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001293 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001294 return I->getInit();
1295 }
1296 return 0;
1297}
1298
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001299bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001300 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001301 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001302
1303 if (!isStaticDataMember())
1304 return false;
1305
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001306 // If this static data member was instantiated from a static data member of
1307 // a class template, check whether that static data member was defined
1308 // out-of-line.
1309 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1310 return VD->isOutOfLine();
1311
1312 return false;
1313}
1314
Douglas Gregor1d957a32009-10-27 18:42:08 +00001315VarDecl *VarDecl::getOutOfLineDefinition() {
1316 if (!isStaticDataMember())
1317 return 0;
1318
1319 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1320 RD != RDEnd; ++RD) {
1321 if (RD->getLexicalDeclContext()->isFileContext())
1322 return *RD;
1323 }
1324
1325 return 0;
1326}
1327
Douglas Gregord5058122010-02-11 01:19:42 +00001328void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001329 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1330 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001331 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001332 }
1333
1334 Init = I;
1335}
1336
Douglas Gregorfe314812011-06-21 17:03:29 +00001337bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001338 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001339
1340 const Expr *E = getInit();
1341 if (!E)
1342 return false;
1343
1344 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1345 E = Cleanups->getSubExpr();
1346
1347 return isa<MaterializeTemporaryExpr>(E);
1348}
1349
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001350VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001351 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001352 return cast<VarDecl>(MSI->getInstantiatedFrom());
1353
1354 return 0;
1355}
1356
Douglas Gregor3c74d412009-10-14 20:14:33 +00001357TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001358 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001359 return MSI->getTemplateSpecializationKind();
1360
1361 return TSK_Undeclared;
1362}
1363
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001364MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001365 return getASTContext().getInstantiatedFromStaticDataMember(this);
1366}
1367
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001368void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1369 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001370 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001371 assert(MSI && "Not an instantiated static data member?");
1372 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001373 if (TSK != TSK_ExplicitSpecialization &&
1374 PointOfInstantiation.isValid() &&
1375 MSI->getPointOfInstantiation().isInvalid())
1376 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001377}
1378
Sebastian Redl833ef452010-01-26 22:01:41 +00001379//===----------------------------------------------------------------------===//
1380// ParmVarDecl Implementation
1381//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001382
Sebastian Redl833ef452010-01-26 22:01:41 +00001383ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001384 SourceLocation StartLoc,
1385 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001386 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001387 StorageClass S, StorageClass SCAsWritten,
1388 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001389 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001390 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001391}
1392
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001393SourceRange ParmVarDecl::getSourceRange() const {
1394 if (!hasInheritedDefaultArg()) {
1395 SourceRange ArgRange = getDefaultArgRange();
1396 if (ArgRange.isValid())
1397 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1398 }
1399
1400 return DeclaratorDecl::getSourceRange();
1401}
1402
Sebastian Redl833ef452010-01-26 22:01:41 +00001403Expr *ParmVarDecl::getDefaultArg() {
1404 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1405 assert(!hasUninstantiatedDefaultArg() &&
1406 "Default argument is not yet instantiated!");
1407
1408 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001409 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001410 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001411
Sebastian Redl833ef452010-01-26 22:01:41 +00001412 return Arg;
1413}
1414
1415unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001416 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001417 return E->getNumTemporaries();
1418
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001419 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001420}
1421
Sebastian Redl833ef452010-01-26 22:01:41 +00001422CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1423 assert(getNumDefaultArgTemporaries() &&
1424 "Default arguments does not have any temporaries!");
1425
John McCall5d413782010-12-06 08:20:24 +00001426 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001427 return E->getTemporary(i);
1428}
1429
1430SourceRange ParmVarDecl::getDefaultArgRange() const {
1431 if (const Expr *E = getInit())
1432 return E->getSourceRange();
1433
1434 if (hasUninstantiatedDefaultArg())
1435 return getUninstantiatedDefaultArg()->getSourceRange();
1436
1437 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001438}
1439
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001440bool ParmVarDecl::isParameterPack() const {
1441 return isa<PackExpansionType>(getType());
1442}
1443
Nuno Lopes394ec982008-12-17 23:39:55 +00001444//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001445// FunctionDecl Implementation
1446//===----------------------------------------------------------------------===//
1447
Douglas Gregorb11aad82011-02-19 18:51:44 +00001448void FunctionDecl::getNameForDiagnostic(std::string &S,
1449 const PrintingPolicy &Policy,
1450 bool Qualified) const {
1451 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1452 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1453 if (TemplateArgs)
1454 S += TemplateSpecializationType::PrintTemplateArgumentList(
1455 TemplateArgs->data(),
1456 TemplateArgs->size(),
1457 Policy);
1458
1459}
1460
Ted Kremenek186a0742010-04-29 16:49:01 +00001461bool FunctionDecl::isVariadic() const {
1462 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1463 return FT->isVariadic();
1464 return false;
1465}
1466
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001467bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1468 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001469 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001470 Definition = *I;
1471 return true;
1472 }
1473 }
1474
1475 return false;
1476}
1477
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001478bool FunctionDecl::hasTrivialBody() const
1479{
1480 Stmt *S = getBody();
1481 if (!S) {
1482 // Since we don't have a body for this function, we don't know if it's
1483 // trivial or not.
1484 return false;
1485 }
1486
1487 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1488 return true;
1489 return false;
1490}
1491
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001492bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1493 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001494 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001495 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1496 return true;
1497 }
1498 }
1499
1500 return false;
1501}
1502
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001503Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001504 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1505 if (I->Body) {
1506 Definition = *I;
1507 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001508 } else if (I->IsLateTemplateParsed) {
1509 Definition = *I;
1510 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001511 }
1512 }
1513
1514 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001515}
1516
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001517void FunctionDecl::setBody(Stmt *B) {
1518 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001519 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001520 EndRangeLoc = B->getLocEnd();
1521}
1522
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001523void FunctionDecl::setPure(bool P) {
1524 IsPure = P;
1525 if (P)
1526 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1527 Parent->markedVirtualFunctionPure();
1528}
1529
Douglas Gregor16618f22009-09-12 00:17:51 +00001530bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001531 const TranslationUnitDecl *tunit =
1532 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1533 return tunit &&
Alexis Huntf9933e82011-05-15 20:59:31 +00001534 !tunit->getASTContext().getLangOptions().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001535 getIdentifier() &&
1536 getIdentifier()->isStr("main");
1537}
1538
1539bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1540 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1541 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1542 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1543 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1544 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1545
1546 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1547 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1548
1549 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1550 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1551
1552 ASTContext &Context =
1553 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1554 ->getASTContext();
1555
1556 // The result type and first argument type are constant across all
1557 // these operators. The second argument must be exactly void*.
1558 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001559}
1560
Douglas Gregor16618f22009-09-12 00:17:51 +00001561bool FunctionDecl::isExternC() const {
1562 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001563 // In C, any non-static, non-overloadable function has external
1564 // linkage.
1565 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001566 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001567
Chandler Carruth4322a282011-02-25 00:05:02 +00001568 const DeclContext *DC = getDeclContext();
1569 if (DC->isRecord())
1570 return false;
1571
1572 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001573 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1574 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001575 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001576 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001577
1578 break;
1579 }
1580 }
1581
Douglas Gregorbff62032010-10-21 16:57:46 +00001582 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001583}
1584
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001585bool FunctionDecl::isGlobal() const {
1586 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1587 return Method->isStatic();
1588
John McCall8e7d6562010-08-26 03:08:43 +00001589 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001590 return false;
1591
Mike Stump11289f42009-09-09 15:08:12 +00001592 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001593 DC->isNamespace();
1594 DC = DC->getParent()) {
1595 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1596 if (!Namespace->getDeclName())
1597 return false;
1598 break;
1599 }
1600 }
1601
1602 return true;
1603}
1604
Sebastian Redl833ef452010-01-26 22:01:41 +00001605void
1606FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1607 redeclarable_base::setPreviousDeclaration(PrevDecl);
1608
1609 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1610 FunctionTemplateDecl *PrevFunTmpl
1611 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1612 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1613 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1614 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001615
1616 if (PrevDecl->IsInline)
1617 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001618}
1619
1620const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1621 return getFirstDeclaration();
1622}
1623
1624FunctionDecl *FunctionDecl::getCanonicalDecl() {
1625 return getFirstDeclaration();
1626}
1627
Douglas Gregorbf62d642010-12-06 18:36:25 +00001628void FunctionDecl::setStorageClass(StorageClass SC) {
1629 assert(isLegalForFunction(SC));
1630 if (getStorageClass() != SC)
1631 ClearLinkageCache();
1632
1633 SClass = SC;
1634}
1635
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001636/// \brief Returns a value indicating whether this function
1637/// corresponds to a builtin function.
1638///
1639/// The function corresponds to a built-in function if it is
1640/// declared at translation scope or within an extern "C" block and
1641/// its name matches with the name of a builtin. The returned value
1642/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001643/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001644/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001645unsigned FunctionDecl::getBuiltinID() const {
1646 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001647 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1648 return 0;
1649
1650 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1651 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1652 return BuiltinID;
1653
1654 // This function has the name of a known C library
1655 // function. Determine whether it actually refers to the C library
1656 // function or whether it just has the same name.
1657
Douglas Gregora908e7f2009-02-17 03:23:10 +00001658 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001659 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001660 return 0;
1661
Douglas Gregore711f702009-02-14 18:57:46 +00001662 // If this function is at translation-unit scope and we're not in
1663 // C++, it refers to the C library function.
1664 if (!Context.getLangOptions().CPlusPlus &&
1665 getDeclContext()->isTranslationUnit())
1666 return BuiltinID;
1667
1668 // If the function is in an extern "C" linkage specification and is
1669 // not marked "overloadable", it's the real function.
1670 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001671 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001672 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001673 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001674 return BuiltinID;
1675
1676 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001677 return 0;
1678}
1679
1680
Chris Lattner47c0d002009-04-25 06:03:53 +00001681/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001682/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001683/// after it has been created.
1684unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001685 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001686 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001687 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001688 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001689
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001690}
1691
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001692void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001693 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001694 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001695 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001696
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001697 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001698 if (!NewParamInfo.empty()) {
1699 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1700 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001701 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001702}
Chris Lattner41943152007-01-25 04:52:46 +00001703
Chris Lattner58258242008-04-10 02:22:51 +00001704/// getMinRequiredArguments - Returns the minimum number of arguments
1705/// needed to call this function. This may be fewer than the number of
1706/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001707/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001708unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001709 if (!getASTContext().getLangOptions().CPlusPlus)
1710 return getNumParams();
1711
Douglas Gregor7825bf32011-01-06 22:09:01 +00001712 unsigned NumRequiredArgs = getNumParams();
1713
1714 // If the last parameter is a parameter pack, we don't need an argument for
1715 // it.
1716 if (NumRequiredArgs > 0 &&
1717 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1718 --NumRequiredArgs;
1719
1720 // If this parameter has a default argument, we don't need an argument for
1721 // it.
1722 while (NumRequiredArgs > 0 &&
1723 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001724 --NumRequiredArgs;
1725
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001726 // We might have parameter packs before the end. These can't be deduced,
1727 // but they can still handle multiple arguments.
1728 unsigned ArgIdx = NumRequiredArgs;
1729 while (ArgIdx > 0) {
1730 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1731 NumRequiredArgs = ArgIdx;
1732
1733 --ArgIdx;
1734 }
1735
Chris Lattner58258242008-04-10 02:22:51 +00001736 return NumRequiredArgs;
1737}
1738
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001739bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001740 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001741 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001742
1743 if (isa<CXXMethodDecl>(this)) {
1744 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1745 return true;
1746 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001747
1748 switch (getTemplateSpecializationKind()) {
1749 case TSK_Undeclared:
1750 case TSK_ExplicitSpecialization:
1751 return false;
1752
1753 case TSK_ImplicitInstantiation:
1754 case TSK_ExplicitInstantiationDeclaration:
1755 case TSK_ExplicitInstantiationDefinition:
1756 // Handle below.
1757 break;
1758 }
1759
1760 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001761 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001762 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001763 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001764
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001765 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001766 return PatternDecl->isInlined();
1767
1768 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001769}
1770
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001771/// \brief For a function declaration in C or C++, determine whether this
1772/// declaration causes the definition to be externally visible.
1773///
1774/// Determines whether this is the first non-inline redeclaration of an inline
1775/// function in a language where "inline" does not normally require an
1776/// externally visible definition.
1777bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1778 assert(!doesThisDeclarationHaveABody() &&
1779 "Must have a declaration without a body.");
1780
1781 ASTContext &Context = getASTContext();
1782
1783 // In C99 mode, a function may have an inline definition (causing it to
1784 // be deferred) then redeclared later. As a special case, "extern inline"
1785 // is not required to produce an external symbol.
1786 if (Context.getLangOptions().GNUInline || !Context.getLangOptions().C99 ||
1787 Context.getLangOptions().CPlusPlus)
1788 return false;
1789 if (getLinkage() != ExternalLinkage || isInlineSpecified())
1790 return false;
Nick Lewyckyba4cc012011-07-18 07:11:55 +00001791 const FunctionDecl *Definition = 0;
1792 if (hasBody(Definition))
1793 return Definition->isInlined() &&
1794 Definition->isInlineDefinitionExternallyVisible();
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001795 return false;
1796}
1797
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001798/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001799/// definition will be externally visible.
1800///
1801/// Inline function definitions are always available for inlining optimizations.
1802/// However, depending on the language dialect, declaration specifiers, and
1803/// attributes, the definition of an inline function may or may not be
1804/// "externally" visible to other translation units in the program.
1805///
1806/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001807/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001808/// inline definition becomes externally visible (C99 6.7.4p6).
1809///
1810/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1811/// definition, we use the GNU semantics for inline, which are nearly the
1812/// opposite of C99 semantics. In particular, "inline" by itself will create
1813/// an externally visible symbol, but "extern inline" will not create an
1814/// externally visible symbol.
1815bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001816 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001817 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001818 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001819
Rafael Espindolafb2af642011-06-02 16:13:27 +00001820 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001821 // If it's not the case that both 'inline' and 'extern' are
1822 // specified on the definition, then this inline definition is
1823 // externally visible.
1824 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1825 return true;
1826
1827 // If any declaration is 'inline' but not 'extern', then this definition
1828 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001829 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1830 Redecl != RedeclEnd;
1831 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001832 if (Redecl->isInlineSpecified() &&
1833 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001834 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001835 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001836
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001837 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001838 }
1839
1840 // C99 6.7.4p6:
1841 // [...] If all of the file scope declarations for a function in a
1842 // translation unit include the inline function specifier without extern,
1843 // then the definition in that translation unit is an inline definition.
1844 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1845 Redecl != RedeclEnd;
1846 ++Redecl) {
1847 // Only consider file-scope declarations in this test.
1848 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1849 continue;
1850
John McCall8e7d6562010-08-26 03:08:43 +00001851 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001852 return true; // Not an inline definition
1853 }
1854
1855 // C99 6.7.4p6:
1856 // An inline definition does not provide an external definition for the
1857 // function, and does not forbid an external definition in another
1858 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001859 return false;
1860}
1861
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001862/// getOverloadedOperator - Which C++ overloaded operator this
1863/// function represents, if any.
1864OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001865 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1866 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001867 else
1868 return OO_None;
1869}
1870
Alexis Huntc88db062010-01-13 09:01:02 +00001871/// getLiteralIdentifier - The literal suffix identifier this function
1872/// represents, if any.
1873const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1874 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1875 return getDeclName().getCXXLiteralIdentifier();
1876 else
1877 return 0;
1878}
1879
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001880FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1881 if (TemplateOrSpecialization.isNull())
1882 return TK_NonTemplate;
1883 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1884 return TK_FunctionTemplate;
1885 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1886 return TK_MemberSpecialization;
1887 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1888 return TK_FunctionTemplateSpecialization;
1889 if (TemplateOrSpecialization.is
1890 <DependentFunctionTemplateSpecializationInfo*>())
1891 return TK_DependentFunctionTemplateSpecialization;
1892
1893 assert(false && "Did we miss a TemplateOrSpecialization type?");
1894 return TK_NonTemplate;
1895}
1896
Douglas Gregord801b062009-10-07 23:56:10 +00001897FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001898 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001899 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1900
1901 return 0;
1902}
1903
Douglas Gregor06db9f52009-10-12 20:18:28 +00001904MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1905 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1906}
1907
Douglas Gregord801b062009-10-07 23:56:10 +00001908void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001909FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1910 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001911 TemplateSpecializationKind TSK) {
1912 assert(TemplateOrSpecialization.isNull() &&
1913 "Member function is already a specialization");
1914 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001915 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001916 TemplateOrSpecialization = Info;
1917}
1918
Douglas Gregorafca3b42009-10-27 20:53:28 +00001919bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001920 // If the function is invalid, it can't be implicitly instantiated.
1921 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001922 return false;
1923
1924 switch (getTemplateSpecializationKind()) {
1925 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00001926 case TSK_ExplicitInstantiationDefinition:
1927 return false;
1928
1929 case TSK_ImplicitInstantiation:
1930 return true;
1931
Francois Pichet00c7e6c2011-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 Gregorafca3b42009-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 Kyrtzidis36ea3222010-07-07 11:31:19 +00001944 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001945 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001946 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-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 Kyrtzidis36ea3222010-07-07 11:31:19 +00001952 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001953 return true;
1954
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001955 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001956}
1957
1958FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00001959 // Handle class scope explicit specialization special case.
1960 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1961 return getClassScopeSpecializationPattern();
1962
Douglas Gregorafca3b42009-10-27 20:53:28 +00001963 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1964 while (Primary->getInstantiatedFromMemberTemplate()) {
1965 // If we have hit a point where the user provided a specialization of
1966 // this template, we're done looking.
1967 if (Primary->isMemberSpecialization())
1968 break;
1969
1970 Primary = Primary->getInstantiatedFromMemberTemplate();
1971 }
1972
1973 return Primary->getTemplatedDecl();
1974 }
1975
1976 return getInstantiatedFromMemberFunction();
1977}
1978
Douglas Gregor70d83e22009-06-29 17:30:29 +00001979FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001980 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001981 = TemplateOrSpecialization
1982 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001983 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001984 }
1985 return 0;
1986}
1987
Francois Pichet00c7e6c2011-08-14 03:52:19 +00001988FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
1989 return getASTContext().getClassScopeSpecializationPattern(this);
1990}
1991
Douglas Gregor70d83e22009-06-29 17:30:29 +00001992const TemplateArgumentList *
1993FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001994 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001995 = TemplateOrSpecialization
1996 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001997 return Info->TemplateArguments;
1998 }
1999 return 0;
2000}
2001
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002002const TemplateArgumentListInfo *
2003FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2004 if (FunctionTemplateSpecializationInfo *Info
2005 = TemplateOrSpecialization
2006 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2007 return Info->TemplateArgumentsAsWritten;
2008 }
2009 return 0;
2010}
2011
Mike Stump11289f42009-09-09 15:08:12 +00002012void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002013FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2014 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002015 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002016 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002017 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002018 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2019 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002020 assert(TSK != TSK_Undeclared &&
2021 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002022 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002023 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002024 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002025 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2026 TemplateArgs,
2027 TemplateArgsAsWritten,
2028 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002029 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00002030
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002031 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002032 // function template specializations.
2033 if (InsertPos)
Sebastian Redl9ab988f2011-04-14 14:07:59 +00002034 Template->addSpecialization(Info, InsertPos);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002035 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00002036 // Try to insert the new node. If there is an existing node, leave it, the
2037 // set will contain the canonical decls while
2038 // FunctionTemplateDecl::findSpecialization will return
2039 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002040 FunctionTemplateSpecializationInfo *Existing
2041 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00002042 (void)Existing;
2043 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
2044 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002045 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002046}
2047
John McCallb9c78482010-04-08 09:05:18 +00002048void
2049FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2050 const UnresolvedSetImpl &Templates,
2051 const TemplateArgumentListInfo &TemplateArgs) {
2052 assert(TemplateOrSpecialization.isNull());
2053 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2054 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002055 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002056 void *Buffer = Context.Allocate(Size);
2057 DependentFunctionTemplateSpecializationInfo *Info =
2058 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2059 TemplateArgs);
2060 TemplateOrSpecialization = Info;
2061}
2062
2063DependentFunctionTemplateSpecializationInfo::
2064DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2065 const TemplateArgumentListInfo &TArgs)
2066 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2067
2068 d.NumTemplates = Ts.size();
2069 d.NumArgs = TArgs.size();
2070
2071 FunctionTemplateDecl **TsArray =
2072 const_cast<FunctionTemplateDecl**>(getTemplates());
2073 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2074 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2075
2076 TemplateArgumentLoc *ArgsArray =
2077 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2078 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2079 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2080}
2081
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002082TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002083 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002084 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002085 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002086 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002087 if (FTSInfo)
2088 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002089
Douglas Gregord801b062009-10-07 23:56:10 +00002090 MemberSpecializationInfo *MSInfo
2091 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2092 if (MSInfo)
2093 return MSInfo->getTemplateSpecializationKind();
2094
2095 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002096}
2097
Mike Stump11289f42009-09-09 15:08:12 +00002098void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002099FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2100 SourceLocation PointOfInstantiation) {
2101 if (FunctionTemplateSpecializationInfo *FTSInfo
2102 = TemplateOrSpecialization.dyn_cast<
2103 FunctionTemplateSpecializationInfo*>()) {
2104 FTSInfo->setTemplateSpecializationKind(TSK);
2105 if (TSK != TSK_ExplicitSpecialization &&
2106 PointOfInstantiation.isValid() &&
2107 FTSInfo->getPointOfInstantiation().isInvalid())
2108 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2109 } else if (MemberSpecializationInfo *MSInfo
2110 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2111 MSInfo->setTemplateSpecializationKind(TSK);
2112 if (TSK != TSK_ExplicitSpecialization &&
2113 PointOfInstantiation.isValid() &&
2114 MSInfo->getPointOfInstantiation().isInvalid())
2115 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2116 } else
2117 assert(false && "Function cannot have a template specialization kind");
2118}
2119
2120SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002121 if (FunctionTemplateSpecializationInfo *FTSInfo
2122 = TemplateOrSpecialization.dyn_cast<
2123 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002124 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002125 else if (MemberSpecializationInfo *MSInfo
2126 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002127 return MSInfo->getPointOfInstantiation();
2128
2129 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002130}
2131
Douglas Gregor6411b922009-09-11 20:15:17 +00002132bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002133 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002134 return true;
2135
2136 // If this function was instantiated from a member function of a
2137 // class template, check whether that member function was defined out-of-line.
2138 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2139 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002140 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002141 return Definition->isOutOfLine();
2142 }
2143
2144 // If this function was instantiated from a function template,
2145 // check whether that function template was defined out-of-line.
2146 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2147 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002148 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002149 return Definition->isOutOfLine();
2150 }
2151
2152 return false;
2153}
2154
Abramo Bagnaraea947882011-03-08 16:41:52 +00002155SourceRange FunctionDecl::getSourceRange() const {
2156 return SourceRange(getOuterLocStart(), EndRangeLoc);
2157}
2158
Chris Lattner59a25942008-03-31 00:36:02 +00002159//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002160// FieldDecl Implementation
2161//===----------------------------------------------------------------------===//
2162
Jay Foad39c79802011-01-12 09:06:06 +00002163FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002164 SourceLocation StartLoc, SourceLocation IdLoc,
2165 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002166 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2167 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002168 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002169 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002170}
2171
2172bool FieldDecl::isAnonymousStructOrUnion() const {
2173 if (!isImplicit() || getDeclName())
2174 return false;
2175
2176 if (const RecordType *Record = getType()->getAs<RecordType>())
2177 return Record->getDecl()->isAnonymousStructOrUnion();
2178
2179 return false;
2180}
2181
John McCall4e819612011-01-20 07:57:12 +00002182unsigned FieldDecl::getFieldIndex() const {
2183 if (CachedFieldIndex) return CachedFieldIndex - 1;
2184
2185 unsigned index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002186 const RecordDecl *RD = getParent();
2187 const FieldDecl *LastFD = 0;
2188 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
2189
2190 RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
John McCall4e819612011-01-20 07:57:12 +00002191 while (true) {
2192 assert(i != e && "failed to find field in parent!");
2193 if (*i == this)
2194 break;
2195
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002196 if (IsMsStruct) {
2197 // Zero-length bitfields following non-bitfield members are ignored.
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00002198 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD)) {
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002199 ++i;
2200 continue;
2201 }
2202 LastFD = (*i);
2203 }
John McCall4e819612011-01-20 07:57:12 +00002204 ++i;
2205 ++index;
2206 }
2207
2208 CachedFieldIndex = index + 1;
2209 return index;
2210}
2211
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002212SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002213 if (const Expr *E = InitializerOrBitWidth.getPointer())
2214 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002215 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002216}
2217
Richard Smith938f40b2011-06-11 17:19:42 +00002218void FieldDecl::setInClassInitializer(Expr *Init) {
2219 assert(!InitializerOrBitWidth.getPointer() &&
2220 "bit width or initializer already set");
2221 InitializerOrBitWidth.setPointer(Init);
2222 InitializerOrBitWidth.setInt(0);
2223}
2224
Sebastian Redl833ef452010-01-26 22:01:41 +00002225//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002226// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002227//===----------------------------------------------------------------------===//
2228
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002229SourceLocation TagDecl::getOuterLocStart() const {
2230 return getTemplateOrInnerLocStart(this);
2231}
2232
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002233SourceRange TagDecl::getSourceRange() const {
2234 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002235 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002236}
2237
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002238TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002239 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002240}
2241
Richard Smithdda56e42011-04-15 14:24:37 +00002242void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2243 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002244 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002245 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002246 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002247}
2248
Douglas Gregordee1be82009-01-17 00:42:38 +00002249void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002250 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002251
2252 if (isa<CXXRecordDecl>(this)) {
2253 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2254 struct CXXRecordDecl::DefinitionData *Data =
2255 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002256 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2257 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002258 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002259}
2260
2261void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002262 assert((!isa<CXXRecordDecl>(this) ||
2263 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2264 "definition completed but not started");
2265
Douglas Gregordee1be82009-01-17 00:42:38 +00002266 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002267 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002268
2269 if (ASTMutationListener *L = getASTMutationListener())
2270 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002271}
2272
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002273TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002274 if (isDefinition())
2275 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002276 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2277 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002278
2279 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002280 R != REnd; ++R)
2281 if (R->isDefinition())
2282 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002283
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002284 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002285}
2286
Douglas Gregor14454802011-02-25 02:25:35 +00002287void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2288 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002289 // Make sure the extended qualifier info is allocated.
2290 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002291 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002292 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002293 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002294 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002295 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002296 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002297 if (getExtInfo()->NumTemplParamLists == 0) {
2298 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002299 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002300 }
2301 else
2302 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002303 }
2304 }
2305}
2306
Abramo Bagnara60804e12011-03-18 15:16:37 +00002307void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2308 unsigned NumTPLists,
2309 TemplateParameterList **TPLists) {
2310 assert(NumTPLists > 0);
2311 // Make sure the extended decl info is allocated.
2312 if (!hasExtInfo())
2313 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002314 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002315 // Set the template parameter lists info.
2316 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2317}
2318
Ted Kremenek21475702008-09-05 17:16:31 +00002319//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002320// EnumDecl Implementation
2321//===----------------------------------------------------------------------===//
2322
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002323EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2324 SourceLocation StartLoc, SourceLocation IdLoc,
2325 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002326 EnumDecl *PrevDecl, bool IsScoped,
2327 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002328 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002329 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002330 C.getTypeDeclType(Enum, PrevDecl);
2331 return Enum;
2332}
2333
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002334EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002335 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002336 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002337}
2338
Douglas Gregord5058122010-02-11 01:19:42 +00002339void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002340 QualType NewPromotionType,
2341 unsigned NumPositiveBits,
2342 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002343 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002344 if (!IntegerType)
2345 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002346 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002347 setNumPositiveBits(NumPositiveBits);
2348 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002349 TagDecl::completeDefinition();
2350}
2351
2352//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002353// RecordDecl Implementation
2354//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002355
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002356RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2357 SourceLocation StartLoc, SourceLocation IdLoc,
2358 IdentifierInfo *Id, RecordDecl *PrevDecl)
2359 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002360 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002361 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002362 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002363 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002364 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002365}
2366
Jay Foad39c79802011-01-12 09:06:06 +00002367RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002368 SourceLocation StartLoc, SourceLocation IdLoc,
2369 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2370 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2371 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002372 C.getTypeDeclType(R, PrevDecl);
2373 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002374}
2375
Jay Foad39c79802011-01-12 09:06:06 +00002376RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002377 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2378 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002379}
2380
Douglas Gregordfcad112009-03-25 15:59:44 +00002381bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002382 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002383 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2384}
2385
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002386RecordDecl::field_iterator RecordDecl::field_begin() const {
2387 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2388 LoadFieldsFromExternalStorage();
2389
2390 return field_iterator(decl_iterator(FirstDecl));
2391}
2392
Douglas Gregorb11aad82011-02-19 18:51:44 +00002393/// completeDefinition - Notes that the definition of this type is now
2394/// complete.
2395void RecordDecl::completeDefinition() {
2396 assert(!isDefinition() && "Cannot redefine record!");
2397 TagDecl::completeDefinition();
2398}
2399
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002400void RecordDecl::LoadFieldsFromExternalStorage() const {
2401 ExternalASTSource *Source = getASTContext().getExternalSource();
2402 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2403
2404 // Notify that we have a RecordDecl doing some initialization.
2405 ExternalASTSource::Deserializing TheFields(Source);
2406
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002407 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002408 LoadedFieldsFromExternalStorage = true;
2409 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2410 case ELR_Success:
2411 break;
2412
2413 case ELR_AlreadyLoaded:
2414 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002415 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002416 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002417
2418#ifndef NDEBUG
2419 // Check that all decls we got were FieldDecls.
2420 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2421 assert(isa<FieldDecl>(Decls[i]));
2422#endif
2423
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002424 if (Decls.empty())
2425 return;
2426
2427 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2428}
2429
Steve Naroff415d3d52008-10-08 17:01:13 +00002430//===----------------------------------------------------------------------===//
2431// BlockDecl Implementation
2432//===----------------------------------------------------------------------===//
2433
David Blaikie9c70e042011-09-21 18:16:56 +00002434void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002435 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002436
Steve Naroffc4b30e52009-03-13 16:56:44 +00002437 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002438 if (!NewParamInfo.empty()) {
2439 NumParams = NewParamInfo.size();
2440 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2441 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002442 }
2443}
2444
John McCall351762c2011-02-07 10:33:21 +00002445void BlockDecl::setCaptures(ASTContext &Context,
2446 const Capture *begin,
2447 const Capture *end,
2448 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002449 CapturesCXXThis = capturesCXXThis;
2450
2451 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002452 NumCaptures = 0;
2453 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002454 return;
2455 }
2456
John McCall351762c2011-02-07 10:33:21 +00002457 NumCaptures = end - begin;
2458
2459 // Avoid new Capture[] because we don't want to provide a default
2460 // constructor.
2461 size_t allocationSize = NumCaptures * sizeof(Capture);
2462 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2463 memcpy(buffer, begin, allocationSize);
2464 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002465}
Sebastian Redl833ef452010-01-26 22:01:41 +00002466
John McCallce45f882011-06-15 22:51:16 +00002467bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2468 for (capture_const_iterator
2469 i = capture_begin(), e = capture_end(); i != e; ++i)
2470 // Only auto vars can be captured, so no redeclaration worries.
2471 if (i->getVariable() == variable)
2472 return true;
2473
2474 return false;
2475}
2476
Douglas Gregor70226da2010-12-21 16:27:07 +00002477SourceRange BlockDecl::getSourceRange() const {
2478 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2479}
Sebastian Redl833ef452010-01-26 22:01:41 +00002480
2481//===----------------------------------------------------------------------===//
2482// Other Decl Allocation/Deallocation Method Implementations
2483//===----------------------------------------------------------------------===//
2484
2485TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2486 return new (C) TranslationUnitDecl(C);
2487}
2488
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002489LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002490 SourceLocation IdentL, IdentifierInfo *II) {
2491 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2492}
2493
2494LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2495 SourceLocation IdentL, IdentifierInfo *II,
2496 SourceLocation GnuLabelL) {
2497 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2498 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002499}
2500
2501
Sebastian Redl833ef452010-01-26 22:01:41 +00002502NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002503 SourceLocation StartLoc,
2504 SourceLocation IdLoc, IdentifierInfo *Id) {
2505 return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
Sebastian Redl833ef452010-01-26 22:01:41 +00002506}
2507
Douglas Gregor417e87c2010-10-27 19:49:05 +00002508NamespaceDecl *NamespaceDecl::getNextNamespace() {
2509 return dyn_cast_or_null<NamespaceDecl>(
2510 NextNamespace.get(getASTContext().getExternalSource()));
2511}
2512
Sebastian Redl833ef452010-01-26 22:01:41 +00002513ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002514 SourceLocation IdLoc,
2515 IdentifierInfo *Id,
2516 QualType Type) {
2517 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002518}
2519
2520FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002521 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002522 const DeclarationNameInfo &NameInfo,
2523 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002524 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002525 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002526 bool hasWrittenPrototype,
2527 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002528 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2529 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002530 isInlineSpecified,
2531 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002532 New->HasWrittenPrototype = hasWrittenPrototype;
2533 return New;
2534}
2535
2536BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2537 return new (C) BlockDecl(DC, L);
2538}
2539
2540EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2541 SourceLocation L,
2542 IdentifierInfo *Id, QualType T,
2543 Expr *E, const llvm::APSInt &V) {
2544 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2545}
2546
Benjamin Kramer39593702010-11-21 14:11:41 +00002547IndirectFieldDecl *
2548IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2549 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2550 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002551 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2552}
2553
Douglas Gregorbe996932010-09-01 20:41:53 +00002554SourceRange EnumConstantDecl::getSourceRange() const {
2555 SourceLocation End = getLocation();
2556 if (Init)
2557 End = Init->getLocEnd();
2558 return SourceRange(getLocation(), End);
2559}
2560
Sebastian Redl833ef452010-01-26 22:01:41 +00002561TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002562 SourceLocation StartLoc, SourceLocation IdLoc,
2563 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2564 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002565}
2566
Richard Smithdda56e42011-04-15 14:24:37 +00002567TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2568 SourceLocation StartLoc,
2569 SourceLocation IdLoc, IdentifierInfo *Id,
2570 TypeSourceInfo *TInfo) {
2571 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2572}
2573
Abramo Bagnaraea947882011-03-08 16:41:52 +00002574SourceRange TypedefDecl::getSourceRange() const {
2575 SourceLocation RangeEnd = getLocation();
2576 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2577 if (typeIsPostfix(TInfo->getType()))
2578 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2579 }
2580 return SourceRange(getLocStart(), RangeEnd);
2581}
2582
Richard Smithdda56e42011-04-15 14:24:37 +00002583SourceRange TypeAliasDecl::getSourceRange() const {
2584 SourceLocation RangeEnd = getLocStart();
2585 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2586 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2587 return SourceRange(getLocStart(), RangeEnd);
2588}
2589
Sebastian Redl833ef452010-01-26 22:01:41 +00002590FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002591 StringLiteral *Str,
2592 SourceLocation AsmLoc,
2593 SourceLocation RParenLoc) {
2594 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002595}