blob: b76d67bd81bed9f1e15f843c06bf3e1ce42b3777 [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
Chris Lattner6d9a6852006-10-25 05:11:20 +000031using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000032
Chris Lattner88f70d62008-03-15 05:43:15 +000033//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000034// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000035//===----------------------------------------------------------------------===//
36
Douglas Gregor1baf38f2011-03-26 12:10:19 +000037static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
38 // If this declaration has an explicit visibility attribute, use it.
39 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
40 switch (A->getVisibility()) {
41 case VisibilityAttr::Default:
42 return DefaultVisibility;
43 case VisibilityAttr::Hidden:
44 return HiddenVisibility;
45 case VisibilityAttr::Protected:
46 return ProtectedVisibility;
47 }
John McCall659a3372010-12-18 03:30:47 +000048
John McCall457a04e2010-10-22 21:05:15 +000049 return DefaultVisibility;
John McCall457a04e2010-10-22 21:05:15 +000050 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +000051
52 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
53 // implies visibility(default).
Daniel Dunbar14ad22f2011-04-19 21:43:27 +000054 if (D->getASTContext().Target.getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +000055 for (specific_attr_iterator<AvailabilityAttr>
56 A = D->specific_attr_begin<AvailabilityAttr>(),
57 AEnd = D->specific_attr_end<AvailabilityAttr>();
58 A != AEnd; ++A)
59 if ((*A)->getPlatform()->getName().equals("macosx"))
60 return DefaultVisibility;
61 }
62
63 return llvm::Optional<Visibility>();
John McCall457a04e2010-10-22 21:05:15 +000064}
65
John McCallc273f242010-10-30 11:50:40 +000066typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall457a04e2010-10-22 21:05:15 +000067typedef std::pair<Linkage,Visibility> LVPair;
John McCallc273f242010-10-30 11:50:40 +000068
John McCall457a04e2010-10-22 21:05:15 +000069static LVPair merge(LVPair L, LVPair R) {
70 return LVPair(minLinkage(L.first, R.first),
71 minVisibility(L.second, R.second));
72}
73
John McCallc273f242010-10-30 11:50:40 +000074static LVPair merge(LVPair L, LinkageInfo R) {
75 return LVPair(minLinkage(L.first, R.linkage()),
76 minVisibility(L.second, R.visibility()));
77}
78
Benjamin Kramer396dcf32010-11-05 19:56:37 +000079namespace {
John McCall07072662010-11-02 01:45:15 +000080/// Flags controlling the computation of linkage and visibility.
81struct LVFlags {
82 bool ConsiderGlobalVisibility;
83 bool ConsiderVisibilityAttributes;
John McCall8bc6d5b2011-03-04 10:39:25 +000084 bool ConsiderTemplateParameterTypes;
John McCall07072662010-11-02 01:45:15 +000085
86 LVFlags() : ConsiderGlobalVisibility(true),
John McCall8bc6d5b2011-03-04 10:39:25 +000087 ConsiderVisibilityAttributes(true),
88 ConsiderTemplateParameterTypes(true) {
John McCall07072662010-11-02 01:45:15 +000089 }
90
Douglas Gregorbf62d642010-12-06 18:36:25 +000091 /// \brief Returns a set of flags that is only useful for computing the
92 /// linkage, not the visibility, of a declaration.
93 static LVFlags CreateOnlyDeclLinkage() {
94 LVFlags F;
95 F.ConsiderGlobalVisibility = false;
96 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +000097 F.ConsiderTemplateParameterTypes = false;
Douglas Gregorbf62d642010-12-06 18:36:25 +000098 return F;
99 }
100
John McCall07072662010-11-02 01:45:15 +0000101 /// Returns a set of flags, otherwise based on these, which ignores
102 /// off all sources of visibility except template arguments.
103 LVFlags onlyTemplateVisibility() const {
104 LVFlags F = *this;
105 F.ConsiderGlobalVisibility = false;
106 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +0000107 F.ConsiderTemplateParameterTypes = false;
John McCall07072662010-11-02 01:45:15 +0000108 return F;
109 }
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000110};
Benjamin Kramer396dcf32010-11-05 19:56:37 +0000111} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +0000112
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000113/// \brief Get the most restrictive linkage for the types in the given
114/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +0000115static LVPair
116getLVForTemplateParameterList(const TemplateParameterList *Params) {
117 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000118 for (TemplateParameterList::const_iterator P = Params->begin(),
119 PEnd = Params->end();
120 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000121 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
122 if (NTTP->isExpandedParameterPack()) {
123 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
124 QualType T = NTTP->getExpansionType(I);
125 if (!T->isDependentType())
126 LV = merge(LV, T->getLinkageAndVisibility());
127 }
128 continue;
129 }
130
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000131 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000132 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000133 continue;
134 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000135 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000136
137 if (TemplateTemplateParmDecl *TTP
138 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000139 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000140 }
141 }
142
John McCall457a04e2010-10-22 21:05:15 +0000143 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000144}
145
Douglas Gregorbf62d642010-12-06 18:36:25 +0000146/// getLVForDecl - Get the linkage and visibility for the given declaration.
147static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
148
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000149/// \brief Get the most restrictive linkage for the types and
150/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000151static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregorbf62d642010-12-06 18:36:25 +0000152 unsigned NumArgs,
153 LVFlags &F) {
John McCall457a04e2010-10-22 21:05:15 +0000154 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000155
156 for (unsigned I = 0; I != NumArgs; ++I) {
157 switch (Args[I].getKind()) {
158 case TemplateArgument::Null:
159 case TemplateArgument::Integral:
160 case TemplateArgument::Expression:
161 break;
162
163 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000164 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000165 break;
166
167 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000168 // The decl can validly be null as the representation of nullptr
169 // arguments, valid only in C++0x.
170 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000171 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
172 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000173 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000174 break;
175
176 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000177 case TemplateArgument::TemplateExpansion:
178 if (TemplateDecl *Template
179 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000180 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000181 break;
182
183 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000184 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregorbf62d642010-12-06 18:36:25 +0000185 Args[I].pack_size(),
186 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000187 break;
188 }
189 }
190
John McCall457a04e2010-10-22 21:05:15 +0000191 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000192}
193
John McCallc273f242010-10-30 11:50:40 +0000194static LVPair
Douglas Gregorbf62d642010-12-06 18:36:25 +0000195getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
196 LVFlags &F) {
197 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000198}
199
John McCallb8c604a2011-06-27 23:06:04 +0000200static bool shouldConsiderTemplateLV(const FunctionDecl *fn,
201 const FunctionTemplateSpecializationInfo *spec) {
202 return !(spec->isExplicitSpecialization() &&
203 fn->hasAttr<VisibilityAttr>());
204}
205
206static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
207 return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
208}
209
John McCall07072662010-11-02 01:45:15 +0000210static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000211 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000212 "Not a name having namespace scope");
213 ASTContext &Context = D->getASTContext();
214
215 // C++ [basic.link]p3:
216 // A name having namespace scope (3.3.6) has internal linkage if it
217 // is the name of
218 // - an object, reference, function or function template that is
219 // explicitly declared static; or,
220 // (This bullet corresponds to C99 6.2.2p3.)
221 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
222 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000223 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000224 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000225
226 // - an object or reference that is explicitly declared const
227 // and neither explicitly declared extern nor previously
228 // declared to have external linkage; or
229 // (there is no equivalent in C99)
230 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000231 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000232 Var->getStorageClass() != SC_Extern &&
233 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000234 bool FoundExtern = false;
235 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
236 PrevVar && !FoundExtern;
237 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000238 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000239 FoundExtern = true;
240
241 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000242 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000243 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000244 if (Var->getStorageClass() == SC_None) {
245 const VarDecl *PrevVar = Var->getPreviousDeclaration();
246 for (; PrevVar; PrevVar = PrevVar->getPreviousDeclaration())
247 if (PrevVar->getStorageClass() == SC_PrivateExtern)
248 break;
249 if (PrevVar)
250 return PrevVar->getLinkageAndVisibility();
251 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000252 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000253 // C++ [temp]p4:
254 // A non-member function template can have internal linkage; any
255 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000256 const FunctionDecl *Function = 0;
257 if (const FunctionTemplateDecl *FunTmpl
258 = dyn_cast<FunctionTemplateDecl>(D))
259 Function = FunTmpl->getTemplatedDecl();
260 else
261 Function = cast<FunctionDecl>(D);
262
263 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000264 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000265 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000266 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
267 // - a data member of an anonymous union.
268 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000269 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000270 }
271
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000272 if (D->isInAnonymousNamespace()) {
273 const VarDecl *Var = dyn_cast<VarDecl>(D);
274 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
275 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
276 return LinkageInfo::uniqueExternal();
277 }
John McCallb7139c42010-10-28 04:18:25 +0000278
John McCall457a04e2010-10-22 21:05:15 +0000279 // Set up the defaults.
280
281 // C99 6.2.2p5:
282 // If the declaration of an identifier for an object has file
283 // scope and no storage-class specifier, its linkage is
284 // external.
John McCallc273f242010-10-30 11:50:40 +0000285 LinkageInfo LV;
286
John McCall07072662010-11-02 01:45:15 +0000287 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000288 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
289 LV.setVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000290 F.ConsiderGlobalVisibility = false;
John McCall2faf32c2010-12-10 02:59:44 +0000291 } else {
292 // If we're declared in a namespace with a visibility attribute,
293 // use that namespace's visibility, but don't call it explicit.
294 for (const DeclContext *DC = D->getDeclContext();
295 !isa<TranslationUnitDecl>(DC);
296 DC = DC->getParent()) {
297 if (!isa<NamespaceDecl>(DC)) continue;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000298 if (llvm::Optional<Visibility> Vis
299 = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
300 LV.setVisibility(*Vis, false);
John McCall2faf32c2010-12-10 02:59:44 +0000301 F.ConsiderGlobalVisibility = false;
302 break;
303 }
304 }
John McCall07072662010-11-02 01:45:15 +0000305 }
John McCallc273f242010-10-30 11:50:40 +0000306 }
John McCall457a04e2010-10-22 21:05:15 +0000307
Douglas Gregorf73b2822009-11-25 22:24:25 +0000308 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000309
Douglas Gregorf73b2822009-11-25 22:24:25 +0000310 // A name having namespace scope has external linkage if it is the
311 // name of
312 //
313 // - an object or reference, unless it has internal linkage; or
314 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000315 // GCC applies the following optimization to variables and static
316 // data members, but not to functions:
317 //
John McCall457a04e2010-10-22 21:05:15 +0000318 // Modify the variable's LV by the LV of its type unless this is
319 // C or extern "C". This follows from [basic.link]p9:
320 // A type without linkage shall not be used as the type of a
321 // variable or function with external linkage unless
322 // - the entity has C language linkage, or
323 // - the entity is declared within an unnamed namespace, or
324 // - the entity is not used or is defined in the same
325 // translation unit.
326 // and [basic.link]p10:
327 // ...the types specified by all declarations referring to a
328 // given variable or function shall be identical...
329 // C does not have an equivalent rule.
330 //
John McCall5fe84122010-10-26 04:59:26 +0000331 // Ignore this if we've got an explicit attribute; the user
332 // probably knows what they're doing.
333 //
John McCall457a04e2010-10-22 21:05:15 +0000334 // Note that we don't want to make the variable non-external
335 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000336 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000337 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
338 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000339 return LinkageInfo::uniqueExternal();
340 if (!LV.visibilityExplicit())
341 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000342 }
343
John McCall23032652010-11-02 18:38:13 +0000344 if (Var->getStorageClass() == SC_PrivateExtern)
345 LV.setVisibility(HiddenVisibility, true);
346
Douglas Gregorf73b2822009-11-25 22:24:25 +0000347 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000348 (Var->getStorageClass() == SC_Extern ||
349 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000350
Douglas Gregorf73b2822009-11-25 22:24:25 +0000351 // C99 6.2.2p4:
352 // For an identifier declared with the storage-class specifier
353 // extern in a scope in which a prior declaration of that
354 // identifier is visible, if the prior declaration specifies
355 // internal or external linkage, the linkage of the identifier
356 // at the later declaration is the same as the linkage
357 // specified at the prior declaration. If no prior declaration
358 // is visible, or if the prior declaration specifies no
359 // linkage, then the identifier has external linkage.
360 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000361 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000362 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
363 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000364 }
365 }
366
Douglas Gregorf73b2822009-11-25 22:24:25 +0000367 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000368 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000369 // In theory, we can modify the function's LV by the LV of its
370 // type unless it has C linkage (see comment above about variables
371 // for justification). In practice, GCC doesn't do this, so it's
372 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000373
John McCall23032652010-11-02 18:38:13 +0000374 if (Function->getStorageClass() == SC_PrivateExtern)
375 LV.setVisibility(HiddenVisibility, true);
376
Douglas Gregorf73b2822009-11-25 22:24:25 +0000377 // C99 6.2.2p5:
378 // If the declaration of an identifier for a function has no
379 // storage-class specifier, its linkage is determined exactly
380 // as if it were declared with the storage-class specifier
381 // extern.
382 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000383 (Function->getStorageClass() == SC_Extern ||
384 Function->getStorageClass() == SC_PrivateExtern ||
385 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000386 // C99 6.2.2p4:
387 // For an identifier declared with the storage-class specifier
388 // extern in a scope in which a prior declaration of that
389 // identifier is visible, if the prior declaration specifies
390 // internal or external linkage, the linkage of the identifier
391 // at the later declaration is the same as the linkage
392 // specified at the prior declaration. If no prior declaration
393 // is visible, or if the prior declaration specifies no
394 // linkage, then the identifier has external linkage.
395 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000396 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000397 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
398 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000399 }
400 }
401
John McCallf768aa72011-02-10 06:50:24 +0000402 // In C++, then if the type of the function uses a type with
403 // unique-external linkage, it's not legally usable from outside
404 // this translation unit. However, we should use the C linkage
405 // rules instead for extern "C" declarations.
406 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
407 Function->getType()->getLinkage() == UniqueExternalLinkage)
408 return LinkageInfo::uniqueExternal();
409
John McCallb8c604a2011-06-27 23:06:04 +0000410 // Consider LV from the template and the template arguments unless
411 // this is an explicit specialization with a visibility attribute.
412 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000413 = Function->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000414 if (shouldConsiderTemplateLV(Function, specInfo)) {
415 LV.merge(getLVForDecl(specInfo->getTemplate(),
416 F.onlyTemplateVisibility()));
417 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
418 LV.merge(getLVForTemplateArgumentList(templateArgs, F));
419 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000420 }
421
Douglas Gregorf73b2822009-11-25 22:24:25 +0000422 // - a named class (Clause 9), or an unnamed class defined in a
423 // typedef declaration in which the class has the typedef name
424 // for linkage purposes (7.1.3); or
425 // - a named enumeration (7.2), or an unnamed enumeration
426 // defined in a typedef declaration in which the enumeration
427 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000428 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
429 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000430 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000431 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000432
John McCall457a04e2010-10-22 21:05:15 +0000433 // If this is a class template specialization, consider the
434 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000435 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000436 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCallb8c604a2011-06-27 23:06:04 +0000437 if (shouldConsiderTemplateLV(spec)) {
438 // From the template.
439 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
440 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000441
John McCallb8c604a2011-06-27 23:06:04 +0000442 // The arguments at which the template was instantiated.
443 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
444 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
445 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000446 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000447
John McCall5fe84122010-10-26 04:59:26 +0000448 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000449 if (F.ConsiderGlobalVisibility)
450 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000451 (Context.getLangOptions().CPlusPlus &&
452 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000453
Douglas Gregorf73b2822009-11-25 22:24:25 +0000454 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000455 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000456 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000457 if (!isExternalLinkage(EnumLV.linkage()))
458 return LinkageInfo::none();
459 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000460
461 // - a template, unless it is a function template that has
462 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000463 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
464 if (F.ConsiderTemplateParameterTypes)
465 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000466
Douglas Gregorf73b2822009-11-25 22:24:25 +0000467 // - a namespace (7.3), unless it is declared within an unnamed
468 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000469 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
470 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000471
John McCall457a04e2010-10-22 21:05:15 +0000472 // By extension, we assign external linkage to Objective-C
473 // interfaces.
474 } else if (isa<ObjCInterfaceDecl>(D)) {
475 // fallout
476
477 // Everything not covered here has no linkage.
478 } else {
John McCallc273f242010-10-30 11:50:40 +0000479 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000480 }
481
482 // If we ended up with non-external linkage, visibility should
483 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000484 if (LV.linkage() != ExternalLinkage)
485 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000486
487 // If we didn't end up with hidden visibility, consider attributes
488 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000489 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000490 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000491
492 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000493}
494
John McCall07072662010-11-02 01:45:15 +0000495static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000496 // Only certain class members have linkage. Note that fields don't
497 // really have linkage, but it's convenient to say they do for the
498 // purposes of calculating linkage of pointer-to-data-member
499 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000500 if (!(isa<CXXMethodDecl>(D) ||
501 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000502 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000503 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000504 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000505 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000506
John McCall07072662010-11-02 01:45:15 +0000507 LinkageInfo LV;
508
509 // The flags we're going to use to compute the class's visibility.
510 LVFlags ClassF = F;
511
512 // If we have an explicit visibility attribute, merge that in.
513 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000514 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
515 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000516
517 // Ignore global visibility later, but not this attribute.
518 F.ConsiderGlobalVisibility = false;
519
520 // Ignore both global visibility and attributes when computing our
521 // parent's visibility.
522 ClassF = F.onlyTemplateVisibility();
523 }
524 }
John McCallc273f242010-10-30 11:50:40 +0000525
526 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000527 // linkage.
528 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
529 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000530 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000531
532 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000533 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000534 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000535
John McCall8823c652010-08-13 08:35:10 +0000536 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000537 // If the type of the function uses a type with unique-external
538 // linkage, it's not legally usable from outside this translation unit.
539 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
540 return LinkageInfo::uniqueExternal();
541
John McCall37bb6c92010-10-29 22:22:43 +0000542 TemplateSpecializationKind TSK = TSK_Undeclared;
543
John McCall457a04e2010-10-22 21:05:15 +0000544 // If this is a method template specialization, use the linkage for
545 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000546 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000547 = MD->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000548 if (shouldConsiderTemplateLV(MD, spec)) {
549 LV.merge(getLVForTemplateArgumentList(*spec->TemplateArguments, F));
550 if (F.ConsiderTemplateParameterTypes)
551 LV.merge(getLVForTemplateParameterList(
552 spec->getTemplate()->getTemplateParameters()));
553 }
John McCall37bb6c92010-10-29 22:22:43 +0000554
John McCallb8c604a2011-06-27 23:06:04 +0000555 TSK = spec->getTemplateSpecializationKind();
John McCall37bb6c92010-10-29 22:22:43 +0000556 } else if (MemberSpecializationInfo *MSI =
557 MD->getMemberSpecializationInfo()) {
558 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000559 }
560
John McCall37bb6c92010-10-29 22:22:43 +0000561 // If we're paying attention to global visibility, apply
562 // -finline-visibility-hidden if this is an inline method.
563 //
John McCallc273f242010-10-30 11:50:40 +0000564 // Note that ConsiderGlobalVisibility doesn't yet have information
565 // about whether containing classes have visibility attributes,
566 // and that's intentional.
567 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000568 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000569 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
570 // InlineVisibilityHidden only applies to definitions, and
571 // isInlined() only gives meaningful answers on definitions
572 // anyway.
573 const FunctionDecl *Def = 0;
574 if (MD->hasBody(Def) && Def->isInlined())
575 LV.setVisibility(HiddenVisibility);
576 }
John McCall457a04e2010-10-22 21:05:15 +0000577
John McCall37bb6c92010-10-29 22:22:43 +0000578 // Note that in contrast to basically every other situation, we
579 // *do* apply -fvisibility to method declarations.
580
581 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000582 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000583 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-06-27 23:06:04 +0000584 if (shouldConsiderTemplateLV(spec)) {
585 // Merge template argument/parameter information for member
586 // class template specializations.
587 LV.merge(getLVForTemplateArgumentList(spec->getTemplateArgs(), F));
John McCall8bc6d5b2011-03-04 10:39:25 +0000588 if (F.ConsiderTemplateParameterTypes)
589 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000590 spec->getSpecializedTemplate()->getTemplateParameters()));
591 }
John McCall37bb6c92010-10-29 22:22:43 +0000592 }
593
John McCall37bb6c92010-10-29 22:22:43 +0000594 // Static data members.
595 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000596 // Modify the variable's linkage by its type, but ignore the
597 // type's visibility unless it's a definition.
598 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
599 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000600 LV.mergeLinkage(UniqueExternalLinkage);
601 if (!LV.visibilityExplicit())
602 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000603 }
604
John McCall07072662010-11-02 01:45:15 +0000605 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000606
607 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000608 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000609 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000610 }
611
John McCall457a04e2010-10-22 21:05:15 +0000612 return LV;
John McCall8823c652010-08-13 08:35:10 +0000613}
614
John McCalld396b972011-02-08 19:01:05 +0000615static void clearLinkageForClass(const CXXRecordDecl *record) {
616 for (CXXRecordDecl::decl_iterator
617 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
618 Decl *child = *i;
619 if (isa<NamedDecl>(child))
620 cast<NamedDecl>(child)->ClearLinkageCache();
621 }
622}
623
624void NamedDecl::ClearLinkageCache() {
625 // Note that we can't skip clearing the linkage of children just
626 // because the parent doesn't have cached linkage: we don't cache
627 // when computing linkage for parent contexts.
628
629 HasCachedLinkage = 0;
630
631 // If we're changing the linkage of a class, we need to reset the
632 // linkage of child declarations, too.
633 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
634 clearLinkageForClass(record);
635
John McCall83779672011-02-19 02:53:41 +0000636 if (ClassTemplateDecl *temp =
637 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000638 // Clear linkage for the template pattern.
639 CXXRecordDecl *record = temp->getTemplatedDecl();
640 record->HasCachedLinkage = 0;
641 clearLinkageForClass(record);
642
John McCall83779672011-02-19 02:53:41 +0000643 // We need to clear linkage for specializations, too.
644 for (ClassTemplateDecl::spec_iterator
645 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
646 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000647 }
John McCall83779672011-02-19 02:53:41 +0000648
649 // Clear cached linkage for function template decls, too.
650 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000651 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
652 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000653 for (FunctionTemplateDecl::spec_iterator
654 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
655 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000656 }
John McCall83779672011-02-19 02:53:41 +0000657
John McCalld396b972011-02-08 19:01:05 +0000658}
659
Douglas Gregorbf62d642010-12-06 18:36:25 +0000660Linkage NamedDecl::getLinkage() const {
661 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000662 assert(Linkage(CachedLinkage) ==
663 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000664 return Linkage(CachedLinkage);
665 }
666
667 CachedLinkage = getLVForDecl(this,
668 LVFlags::CreateOnlyDeclLinkage()).linkage();
669 HasCachedLinkage = 1;
670 return Linkage(CachedLinkage);
671}
672
John McCallc273f242010-10-30 11:50:40 +0000673LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000674 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000675 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000676 HasCachedLinkage = 1;
677 CachedLinkage = LI.linkage();
678 return LI;
John McCall033caa52010-10-29 00:29:13 +0000679}
Ted Kremenek926d8602010-04-20 23:15:35 +0000680
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000681llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
682 // Use the most recent declaration of a variable.
683 if (const VarDecl *var = dyn_cast<VarDecl>(this))
684 return getVisibilityOf(var->getMostRecentDeclaration());
685
686 // Use the most recent declaration of a function, and also handle
687 // function template specializations.
688 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
689 if (llvm::Optional<Visibility> V
690 = getVisibilityOf(fn->getMostRecentDeclaration()))
691 return V;
692
693 // If the function is a specialization of a template with an
694 // explicit visibility attribute, use that.
695 if (FunctionTemplateSpecializationInfo *templateInfo
696 = fn->getTemplateSpecializationInfo())
697 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
698
699 return llvm::Optional<Visibility>();
700 }
701
702 // Otherwise, just check the declaration itself first.
703 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
704 return V;
705
706 // If there wasn't explicit visibility there, and this is a
707 // specialization of a class template, check for visibility
708 // on the pattern.
709 if (const ClassTemplateSpecializationDecl *spec
710 = dyn_cast<ClassTemplateSpecializationDecl>(this))
711 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
712
713 return llvm::Optional<Visibility>();
714}
715
John McCall07072662010-11-02 01:45:15 +0000716static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000717 // Objective-C: treat all Objective-C declarations as having external
718 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000719 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000720 default:
721 break;
John McCall457a04e2010-10-22 21:05:15 +0000722 case Decl::TemplateTemplateParm: // count these as external
723 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000724 case Decl::ObjCAtDefsField:
725 case Decl::ObjCCategory:
726 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000727 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000728 case Decl::ObjCForwardProtocol:
729 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000730 case Decl::ObjCMethod:
731 case Decl::ObjCProperty:
732 case Decl::ObjCPropertyImpl:
733 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000734 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000735 }
736
Douglas Gregorf73b2822009-11-25 22:24:25 +0000737 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000738 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000739 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000740
741 // C++ [basic.link]p5:
742 // In addition, a member function, static data member, a named
743 // class or enumeration of class scope, or an unnamed class or
744 // enumeration defined in a class-scope typedef declaration such
745 // that the class or enumeration has the typedef name for linkage
746 // purposes (7.1.3), has external linkage if the name of the class
747 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000748 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000749 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000750
751 // C++ [basic.link]p6:
752 // The name of a function declared in block scope and the name of
753 // an object declared by a block scope extern declaration have
754 // linkage. If there is a visible declaration of an entity with
755 // linkage having the same name and type, ignoring entities
756 // declared outside the innermost enclosing namespace scope, the
757 // block scope declaration declares that same entity and receives
758 // the linkage of the previous declaration. If there is more than
759 // one such matching entity, the program is ill-formed. Otherwise,
760 // if no matching entity is found, the block scope entity receives
761 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000762 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
763 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000764 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000765 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000766
John McCallc273f242010-10-30 11:50:40 +0000767 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000768 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000769 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
770 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000771 }
772
John McCall457a04e2010-10-22 21:05:15 +0000773 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000774 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000775 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
776 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000777 }
778
779 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000780 }
781
John McCall033caa52010-10-29 00:29:13 +0000782 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000783 if (Var->getStorageClass() == SC_Extern ||
784 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000785 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000786 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000787
John McCallc273f242010-10-30 11:50:40 +0000788 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000789 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000790 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000791 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000792 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
793 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000794 }
795
John McCall457a04e2010-10-22 21:05:15 +0000796 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000797 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000798 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
799 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000800 }
801
802 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000803 }
804 }
805
806 // C++ [basic.link]p6:
807 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000808 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000809}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000810
Douglas Gregor2ada0482009-02-04 17:27:36 +0000811std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000812 return getQualifiedNameAsString(getASTContext().getLangOptions());
813}
814
815std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000816 const DeclContext *Ctx = getDeclContext();
817
818 if (Ctx->isFunctionOrMethod())
819 return getNameAsString();
820
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000821 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000822 ContextsTy Contexts;
823
824 // Collect contexts.
825 while (Ctx && isa<NamedDecl>(Ctx)) {
826 Contexts.push_back(Ctx);
827 Ctx = Ctx->getParent();
828 };
829
830 std::string QualName;
831 llvm::raw_string_ostream OS(QualName);
832
833 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
834 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000835 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000836 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000837 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
838 std::string TemplateArgsStr
839 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000840 TemplateArgs.data(),
841 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000842 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000843 OS << Spec->getName() << TemplateArgsStr;
844 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000845 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000846 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000847 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000848 OS << ND;
849 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
850 if (!RD->getIdentifier())
851 OS << "<anonymous " << RD->getKindName() << '>';
852 else
853 OS << RD;
854 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000855 const FunctionProtoType *FT = 0;
856 if (FD->hasWrittenPrototype())
857 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
858
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000859 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000860 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000861 unsigned NumParams = FD->getNumParams();
862 for (unsigned i = 0; i < NumParams; ++i) {
863 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000864 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000865 std::string Param;
866 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000867 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000868 }
869
870 if (FT->isVariadic()) {
871 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000872 OS << ", ";
873 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000874 }
875 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000876 OS << ')';
877 } else {
878 OS << cast<NamedDecl>(*I);
879 }
880 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000881 }
882
John McCalla2a3f7d2010-03-16 21:48:18 +0000883 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000884 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000885 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000886 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000887
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000888 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000889}
890
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000891bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000892 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
893
Douglas Gregor889ceb72009-02-03 19:21:40 +0000894 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
895 // We want to keep it, unless it nominates same namespace.
896 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000897 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
898 ->getOriginalNamespace() ==
899 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
900 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000903 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
904 // For function declarations, we keep track of redeclarations.
905 return FD->getPreviousDeclaration() == OldD;
906
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000907 // For function templates, the underlying function declarations are linked.
908 if (const FunctionTemplateDecl *FunctionTemplate
909 = dyn_cast<FunctionTemplateDecl>(this))
910 if (const FunctionTemplateDecl *OldFunctionTemplate
911 = dyn_cast<FunctionTemplateDecl>(OldD))
912 return FunctionTemplate->getTemplatedDecl()
913 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000914
Steve Naroffc4173fa2009-02-22 19:35:57 +0000915 // For method declarations, we keep track of redeclarations.
916 if (isa<ObjCMethodDecl>(this))
917 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000918
John McCall9f3059a2009-10-09 21:13:30 +0000919 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
920 return true;
921
John McCall3f746822009-11-17 05:59:44 +0000922 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
923 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
924 cast<UsingShadowDecl>(OldD)->getTargetDecl();
925
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000926 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
927 ASTContext &Context = getASTContext();
928 return Context.getCanonicalNestedNameSpecifier(
929 cast<UsingDecl>(this)->getQualifier()) ==
930 Context.getCanonicalNestedNameSpecifier(
931 cast<UsingDecl>(OldD)->getQualifier());
932 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000933
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000934 // For non-function declarations, if the declarations are of the
935 // same kind then this must be a redeclaration, or semantic analysis
936 // would not have given us the new declaration.
937 return this->getKind() == OldD->getKind();
938}
939
Douglas Gregoreddf4332009-02-24 20:03:32 +0000940bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000941 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000942}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000943
Anders Carlsson6915bf62009-06-26 06:29:23 +0000944NamedDecl *NamedDecl::getUnderlyingDecl() {
945 NamedDecl *ND = this;
946 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000947 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000948 ND = UD->getTargetDecl();
949 else if (ObjCCompatibleAliasDecl *AD
950 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
951 return AD->getClassInterface();
952 else
953 return ND;
954 }
955}
956
John McCalla8ae2222010-04-06 21:38:20 +0000957bool NamedDecl::isCXXInstanceMember() const {
958 assert(isCXXClassMember() &&
959 "checking whether non-member is instance member");
960
961 const NamedDecl *D = this;
962 if (isa<UsingShadowDecl>(D))
963 D = cast<UsingShadowDecl>(D)->getTargetDecl();
964
Francois Pichet783dd6e2010-11-21 06:08:52 +0000965 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000966 return true;
967 if (isa<CXXMethodDecl>(D))
968 return cast<CXXMethodDecl>(D)->isInstance();
969 if (isa<FunctionTemplateDecl>(D))
970 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
971 ->getTemplatedDecl())->isInstance();
972 return false;
973}
974
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000975//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000976// DeclaratorDecl Implementation
977//===----------------------------------------------------------------------===//
978
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000979template <typename DeclT>
980static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
981 if (decl->getNumTemplateParameterLists() > 0)
982 return decl->getTemplateParameterList(0)->getTemplateLoc();
983 else
984 return decl->getInnerLocStart();
985}
986
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000987SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000988 TypeSourceInfo *TSI = getTypeSourceInfo();
989 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000990 return SourceLocation();
991}
992
Douglas Gregor14454802011-02-25 02:25:35 +0000993void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
994 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +0000995 // Make sure the extended decl info is allocated.
996 if (!hasExtInfo()) {
997 // Save (non-extended) type source info pointer.
998 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
999 // Allocate external info struct.
1000 DeclInfo = new (getASTContext()) ExtInfo;
1001 // Restore savedTInfo into (extended) decl info.
1002 getExtInfo()->TInfo = savedTInfo;
1003 }
1004 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001005 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001006 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001007 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001008 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001009 if (getExtInfo()->NumTemplParamLists == 0) {
1010 // Save type source info pointer.
1011 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1012 // Deallocate the extended decl info.
1013 getASTContext().Deallocate(getExtInfo());
1014 // Restore savedTInfo into (non-extended) decl info.
1015 DeclInfo = savedTInfo;
1016 }
1017 else
1018 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001019 }
1020 }
1021}
1022
Abramo Bagnara60804e12011-03-18 15:16:37 +00001023void
1024DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1025 unsigned NumTPLists,
1026 TemplateParameterList **TPLists) {
1027 assert(NumTPLists > 0);
1028 // Make sure the extended decl info is allocated.
1029 if (!hasExtInfo()) {
1030 // Save (non-extended) type source info pointer.
1031 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1032 // Allocate external info struct.
1033 DeclInfo = new (getASTContext()) ExtInfo;
1034 // Restore savedTInfo into (extended) decl info.
1035 getExtInfo()->TInfo = savedTInfo;
1036 }
1037 // Set the template parameter lists info.
1038 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1039}
1040
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001041SourceLocation DeclaratorDecl::getOuterLocStart() const {
1042 return getTemplateOrInnerLocStart(this);
1043}
1044
Abramo Bagnaraea947882011-03-08 16:41:52 +00001045namespace {
1046
1047// Helper function: returns true if QT is or contains a type
1048// having a postfix component.
1049bool typeIsPostfix(clang::QualType QT) {
1050 while (true) {
1051 const Type* T = QT.getTypePtr();
1052 switch (T->getTypeClass()) {
1053 default:
1054 return false;
1055 case Type::Pointer:
1056 QT = cast<PointerType>(T)->getPointeeType();
1057 break;
1058 case Type::BlockPointer:
1059 QT = cast<BlockPointerType>(T)->getPointeeType();
1060 break;
1061 case Type::MemberPointer:
1062 QT = cast<MemberPointerType>(T)->getPointeeType();
1063 break;
1064 case Type::LValueReference:
1065 case Type::RValueReference:
1066 QT = cast<ReferenceType>(T)->getPointeeType();
1067 break;
1068 case Type::PackExpansion:
1069 QT = cast<PackExpansionType>(T)->getPattern();
1070 break;
1071 case Type::Paren:
1072 case Type::ConstantArray:
1073 case Type::DependentSizedArray:
1074 case Type::IncompleteArray:
1075 case Type::VariableArray:
1076 case Type::FunctionProto:
1077 case Type::FunctionNoProto:
1078 return true;
1079 }
1080 }
1081}
1082
1083} // namespace
1084
1085SourceRange DeclaratorDecl::getSourceRange() const {
1086 SourceLocation RangeEnd = getLocation();
1087 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1088 if (typeIsPostfix(TInfo->getType()))
1089 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1090 }
1091 return SourceRange(getOuterLocStart(), RangeEnd);
1092}
1093
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001094void
Douglas Gregor20527e22010-06-15 17:44:38 +00001095QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1096 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001097 TemplateParameterList **TPLists) {
1098 assert((NumTPLists == 0 || TPLists != 0) &&
1099 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001100
1101 // Free previous template parameters (if any).
1102 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001103 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001104 TemplParamLists = 0;
1105 NumTemplParamLists = 0;
1106 }
1107 // Set info on matched template parameter lists (if any).
1108 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001109 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001110 NumTemplParamLists = NumTPLists;
1111 for (unsigned i = NumTPLists; i-- > 0; )
1112 TemplParamLists[i] = TPLists[i];
1113 }
1114}
1115
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001116//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001117// VarDecl Implementation
1118//===----------------------------------------------------------------------===//
1119
Sebastian Redl833ef452010-01-26 22:01:41 +00001120const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1121 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +00001122 case SC_None: break;
1123 case SC_Auto: return "auto"; break;
1124 case SC_Extern: return "extern"; break;
1125 case SC_PrivateExtern: return "__private_extern__"; break;
1126 case SC_Register: return "register"; break;
1127 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +00001128 }
1129
1130 assert(0 && "Invalid storage class");
1131 return 0;
1132}
1133
Abramo Bagnaradff19302011-03-08 08:55:46 +00001134VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1135 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001136 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001137 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001138 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001139}
1140
Douglas Gregorbf62d642010-12-06 18:36:25 +00001141void VarDecl::setStorageClass(StorageClass SC) {
1142 assert(isLegalForVariable(SC));
1143 if (getStorageClass() != SC)
1144 ClearLinkageCache();
1145
John McCallbeaa11c2011-05-01 02:13:58 +00001146 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001147}
1148
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001149SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001150 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001151 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001152 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001153}
1154
Sebastian Redl833ef452010-01-26 22:01:41 +00001155bool VarDecl::isExternC() const {
1156 ASTContext &Context = getASTContext();
1157 if (!Context.getLangOptions().CPlusPlus)
1158 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001159 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001160 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1161
Chandler Carruth4322a282011-02-25 00:05:02 +00001162 const DeclContext *DC = getDeclContext();
1163 if (DC->isFunctionOrMethod())
1164 return false;
1165
1166 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001167 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1168 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001169 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001170
1171 break;
1172 }
1173
Sebastian Redl833ef452010-01-26 22:01:41 +00001174 }
1175
1176 return false;
1177}
1178
1179VarDecl *VarDecl::getCanonicalDecl() {
1180 return getFirstDeclaration();
1181}
1182
Sebastian Redl35351a92010-01-31 22:27:38 +00001183VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1184 // C++ [basic.def]p2:
1185 // A declaration is a definition unless [...] it contains the 'extern'
1186 // specifier or a linkage-specification and neither an initializer [...],
1187 // it declares a static data member in a class declaration [...].
1188 // C++ [temp.expl.spec]p15:
1189 // An explicit specialization of a static data member of a template is a
1190 // definition if the declaration includes an initializer; otherwise, it is
1191 // a declaration.
1192 if (isStaticDataMember()) {
1193 if (isOutOfLine() && (hasInit() ||
1194 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1195 return Definition;
1196 else
1197 return DeclarationOnly;
1198 }
1199 // C99 6.7p5:
1200 // A definition of an identifier is a declaration for that identifier that
1201 // [...] causes storage to be reserved for that object.
1202 // Note: that applies for all non-file-scope objects.
1203 // C99 6.9.2p1:
1204 // If the declaration of an identifier for an object has file scope and an
1205 // initializer, the declaration is an external definition for the identifier
1206 if (hasInit())
1207 return Definition;
1208 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1209 if (hasExternalStorage())
1210 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001211
John McCall8e7d6562010-08-26 03:08:43 +00001212 if (getStorageClassAsWritten() == SC_Extern ||
1213 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001214 for (const VarDecl *PrevVar = getPreviousDeclaration();
1215 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1216 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1217 return DeclarationOnly;
1218 }
1219 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001220 // C99 6.9.2p2:
1221 // A declaration of an object that has file scope without an initializer,
1222 // and without a storage class specifier or the scs 'static', constitutes
1223 // a tentative definition.
1224 // No such thing in C++.
1225 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1226 return TentativeDefinition;
1227
1228 // What's left is (in C, block-scope) declarations without initializers or
1229 // external storage. These are definitions.
1230 return Definition;
1231}
1232
Sebastian Redl35351a92010-01-31 22:27:38 +00001233VarDecl *VarDecl::getActingDefinition() {
1234 DefinitionKind Kind = isThisDeclarationADefinition();
1235 if (Kind != TentativeDefinition)
1236 return 0;
1237
Chris Lattner48eb14d2010-06-14 18:31:46 +00001238 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001239 VarDecl *First = getFirstDeclaration();
1240 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1241 I != E; ++I) {
1242 Kind = (*I)->isThisDeclarationADefinition();
1243 if (Kind == Definition)
1244 return 0;
1245 else if (Kind == TentativeDefinition)
1246 LastTentative = *I;
1247 }
1248 return LastTentative;
1249}
1250
1251bool VarDecl::isTentativeDefinitionNow() const {
1252 DefinitionKind Kind = isThisDeclarationADefinition();
1253 if (Kind != TentativeDefinition)
1254 return false;
1255
1256 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1257 if ((*I)->isThisDeclarationADefinition() == Definition)
1258 return false;
1259 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001260 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001261}
1262
Sebastian Redl5ca79842010-02-01 20:16:42 +00001263VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001264 VarDecl *First = getFirstDeclaration();
1265 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1266 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001267 if ((*I)->isThisDeclarationADefinition() == Definition)
1268 return *I;
1269 }
1270 return 0;
1271}
1272
John McCall37bb6c92010-10-29 22:22:43 +00001273VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1274 DefinitionKind Kind = DeclarationOnly;
1275
1276 const VarDecl *First = getFirstDeclaration();
1277 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1278 I != E; ++I)
1279 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1280
1281 return Kind;
1282}
1283
Sebastian Redl5ca79842010-02-01 20:16:42 +00001284const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001285 redecl_iterator I = redecls_begin(), E = redecls_end();
1286 while (I != E && !I->getInit())
1287 ++I;
1288
1289 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001290 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001291 return I->getInit();
1292 }
1293 return 0;
1294}
1295
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001296bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001297 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001298 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001299
1300 if (!isStaticDataMember())
1301 return false;
1302
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001303 // If this static data member was instantiated from a static data member of
1304 // a class template, check whether that static data member was defined
1305 // out-of-line.
1306 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1307 return VD->isOutOfLine();
1308
1309 return false;
1310}
1311
Douglas Gregor1d957a32009-10-27 18:42:08 +00001312VarDecl *VarDecl::getOutOfLineDefinition() {
1313 if (!isStaticDataMember())
1314 return 0;
1315
1316 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1317 RD != RDEnd; ++RD) {
1318 if (RD->getLexicalDeclContext()->isFileContext())
1319 return *RD;
1320 }
1321
1322 return 0;
1323}
1324
Douglas Gregord5058122010-02-11 01:19:42 +00001325void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001326 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1327 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001328 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001329 }
1330
1331 Init = I;
1332}
1333
Douglas Gregorfe314812011-06-21 17:03:29 +00001334bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001335 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001336
1337 const Expr *E = getInit();
1338 if (!E)
1339 return false;
1340
1341 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1342 E = Cleanups->getSubExpr();
1343
1344 return isa<MaterializeTemporaryExpr>(E);
1345}
1346
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001347VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001348 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001349 return cast<VarDecl>(MSI->getInstantiatedFrom());
1350
1351 return 0;
1352}
1353
Douglas Gregor3c74d412009-10-14 20:14:33 +00001354TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001355 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001356 return MSI->getTemplateSpecializationKind();
1357
1358 return TSK_Undeclared;
1359}
1360
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001361MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001362 return getASTContext().getInstantiatedFromStaticDataMember(this);
1363}
1364
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001365void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1366 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001367 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001368 assert(MSI && "Not an instantiated static data member?");
1369 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001370 if (TSK != TSK_ExplicitSpecialization &&
1371 PointOfInstantiation.isValid() &&
1372 MSI->getPointOfInstantiation().isInvalid())
1373 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001374}
1375
Sebastian Redl833ef452010-01-26 22:01:41 +00001376//===----------------------------------------------------------------------===//
1377// ParmVarDecl Implementation
1378//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001379
Sebastian Redl833ef452010-01-26 22:01:41 +00001380ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001381 SourceLocation StartLoc,
1382 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001383 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001384 StorageClass S, StorageClass SCAsWritten,
1385 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001386 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001387 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001388}
1389
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001390SourceRange ParmVarDecl::getSourceRange() const {
1391 if (!hasInheritedDefaultArg()) {
1392 SourceRange ArgRange = getDefaultArgRange();
1393 if (ArgRange.isValid())
1394 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1395 }
1396
1397 return DeclaratorDecl::getSourceRange();
1398}
1399
Sebastian Redl833ef452010-01-26 22:01:41 +00001400Expr *ParmVarDecl::getDefaultArg() {
1401 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1402 assert(!hasUninstantiatedDefaultArg() &&
1403 "Default argument is not yet instantiated!");
1404
1405 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001406 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001407 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001408
Sebastian Redl833ef452010-01-26 22:01:41 +00001409 return Arg;
1410}
1411
1412unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001413 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001414 return E->getNumTemporaries();
1415
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001416 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001417}
1418
Sebastian Redl833ef452010-01-26 22:01:41 +00001419CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1420 assert(getNumDefaultArgTemporaries() &&
1421 "Default arguments does not have any temporaries!");
1422
John McCall5d413782010-12-06 08:20:24 +00001423 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001424 return E->getTemporary(i);
1425}
1426
1427SourceRange ParmVarDecl::getDefaultArgRange() const {
1428 if (const Expr *E = getInit())
1429 return E->getSourceRange();
1430
1431 if (hasUninstantiatedDefaultArg())
1432 return getUninstantiatedDefaultArg()->getSourceRange();
1433
1434 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001435}
1436
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001437bool ParmVarDecl::isParameterPack() const {
1438 return isa<PackExpansionType>(getType());
1439}
1440
Nuno Lopes394ec982008-12-17 23:39:55 +00001441//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001442// FunctionDecl Implementation
1443//===----------------------------------------------------------------------===//
1444
Douglas Gregorb11aad82011-02-19 18:51:44 +00001445void FunctionDecl::getNameForDiagnostic(std::string &S,
1446 const PrintingPolicy &Policy,
1447 bool Qualified) const {
1448 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1449 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1450 if (TemplateArgs)
1451 S += TemplateSpecializationType::PrintTemplateArgumentList(
1452 TemplateArgs->data(),
1453 TemplateArgs->size(),
1454 Policy);
1455
1456}
1457
Ted Kremenek186a0742010-04-29 16:49:01 +00001458bool FunctionDecl::isVariadic() const {
1459 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1460 return FT->isVariadic();
1461 return false;
1462}
1463
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001464bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1465 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001466 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001467 Definition = *I;
1468 return true;
1469 }
1470 }
1471
1472 return false;
1473}
1474
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001475bool FunctionDecl::hasTrivialBody() const
1476{
1477 Stmt *S = getBody();
1478 if (!S) {
1479 // Since we don't have a body for this function, we don't know if it's
1480 // trivial or not.
1481 return false;
1482 }
1483
1484 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1485 return true;
1486 return false;
1487}
1488
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001489bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1490 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001491 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001492 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1493 return true;
1494 }
1495 }
1496
1497 return false;
1498}
1499
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001500Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001501 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1502 if (I->Body) {
1503 Definition = *I;
1504 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001505 } else if (I->IsLateTemplateParsed) {
1506 Definition = *I;
1507 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001508 }
1509 }
1510
1511 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001512}
1513
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001514void FunctionDecl::setBody(Stmt *B) {
1515 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001516 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001517 EndRangeLoc = B->getLocEnd();
1518}
1519
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001520void FunctionDecl::setPure(bool P) {
1521 IsPure = P;
1522 if (P)
1523 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1524 Parent->markedVirtualFunctionPure();
1525}
1526
Douglas Gregor16618f22009-09-12 00:17:51 +00001527bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001528 const TranslationUnitDecl *tunit =
1529 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1530 return tunit &&
Alexis Huntf9933e82011-05-15 20:59:31 +00001531 !tunit->getASTContext().getLangOptions().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001532 getIdentifier() &&
1533 getIdentifier()->isStr("main");
1534}
1535
1536bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1537 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1538 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1539 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1540 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1541 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1542
1543 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1544 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1545
1546 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1547 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1548
1549 ASTContext &Context =
1550 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1551 ->getASTContext();
1552
1553 // The result type and first argument type are constant across all
1554 // these operators. The second argument must be exactly void*.
1555 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001556}
1557
Douglas Gregor16618f22009-09-12 00:17:51 +00001558bool FunctionDecl::isExternC() const {
1559 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001560 // In C, any non-static, non-overloadable function has external
1561 // linkage.
1562 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001563 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001564
Chandler Carruth4322a282011-02-25 00:05:02 +00001565 const DeclContext *DC = getDeclContext();
1566 if (DC->isRecord())
1567 return false;
1568
1569 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001570 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1571 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001572 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001573 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001574
1575 break;
1576 }
1577 }
1578
Douglas Gregorbff62032010-10-21 16:57:46 +00001579 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001580}
1581
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001582bool FunctionDecl::isGlobal() const {
1583 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1584 return Method->isStatic();
1585
John McCall8e7d6562010-08-26 03:08:43 +00001586 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001587 return false;
1588
Mike Stump11289f42009-09-09 15:08:12 +00001589 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001590 DC->isNamespace();
1591 DC = DC->getParent()) {
1592 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1593 if (!Namespace->getDeclName())
1594 return false;
1595 break;
1596 }
1597 }
1598
1599 return true;
1600}
1601
Sebastian Redl833ef452010-01-26 22:01:41 +00001602void
1603FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1604 redeclarable_base::setPreviousDeclaration(PrevDecl);
1605
1606 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1607 FunctionTemplateDecl *PrevFunTmpl
1608 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1609 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1610 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1611 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001612
1613 if (PrevDecl->IsInline)
1614 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001615}
1616
1617const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1618 return getFirstDeclaration();
1619}
1620
1621FunctionDecl *FunctionDecl::getCanonicalDecl() {
1622 return getFirstDeclaration();
1623}
1624
Douglas Gregorbf62d642010-12-06 18:36:25 +00001625void FunctionDecl::setStorageClass(StorageClass SC) {
1626 assert(isLegalForFunction(SC));
1627 if (getStorageClass() != SC)
1628 ClearLinkageCache();
1629
1630 SClass = SC;
1631}
1632
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001633/// \brief Returns a value indicating whether this function
1634/// corresponds to a builtin function.
1635///
1636/// The function corresponds to a built-in function if it is
1637/// declared at translation scope or within an extern "C" block and
1638/// its name matches with the name of a builtin. The returned value
1639/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001640/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001641/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001642unsigned FunctionDecl::getBuiltinID() const {
1643 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001644 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1645 return 0;
1646
1647 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1648 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1649 return BuiltinID;
1650
1651 // This function has the name of a known C library
1652 // function. Determine whether it actually refers to the C library
1653 // function or whether it just has the same name.
1654
Douglas Gregora908e7f2009-02-17 03:23:10 +00001655 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001656 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001657 return 0;
1658
Douglas Gregore711f702009-02-14 18:57:46 +00001659 // If this function is at translation-unit scope and we're not in
1660 // C++, it refers to the C library function.
1661 if (!Context.getLangOptions().CPlusPlus &&
1662 getDeclContext()->isTranslationUnit())
1663 return BuiltinID;
1664
1665 // If the function is in an extern "C" linkage specification and is
1666 // not marked "overloadable", it's the real function.
1667 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001668 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001669 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001670 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001671 return BuiltinID;
1672
1673 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001674 return 0;
1675}
1676
1677
Chris Lattner47c0d002009-04-25 06:03:53 +00001678/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001679/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001680/// after it has been created.
1681unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001682 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001683 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001684 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001685 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001686
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001687}
1688
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001689void FunctionDecl::setParams(ASTContext &C,
1690 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001691 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001692 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001693
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001694 // Zero params -> null pointer.
1695 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001696 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001697 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001698 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001699 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001700}
Chris Lattner41943152007-01-25 04:52:46 +00001701
Chris Lattner58258242008-04-10 02:22:51 +00001702/// getMinRequiredArguments - Returns the minimum number of arguments
1703/// needed to call this function. This may be fewer than the number of
1704/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001705/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001706unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001707 if (!getASTContext().getLangOptions().CPlusPlus)
1708 return getNumParams();
1709
Douglas Gregor7825bf32011-01-06 22:09:01 +00001710 unsigned NumRequiredArgs = getNumParams();
1711
1712 // If the last parameter is a parameter pack, we don't need an argument for
1713 // it.
1714 if (NumRequiredArgs > 0 &&
1715 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1716 --NumRequiredArgs;
1717
1718 // If this parameter has a default argument, we don't need an argument for
1719 // it.
1720 while (NumRequiredArgs > 0 &&
1721 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001722 --NumRequiredArgs;
1723
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001724 // We might have parameter packs before the end. These can't be deduced,
1725 // but they can still handle multiple arguments.
1726 unsigned ArgIdx = NumRequiredArgs;
1727 while (ArgIdx > 0) {
1728 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1729 NumRequiredArgs = ArgIdx;
1730
1731 --ArgIdx;
1732 }
1733
Chris Lattner58258242008-04-10 02:22:51 +00001734 return NumRequiredArgs;
1735}
1736
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001737bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001738 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001739 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001740
1741 if (isa<CXXMethodDecl>(this)) {
1742 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1743 return true;
1744 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001745
1746 switch (getTemplateSpecializationKind()) {
1747 case TSK_Undeclared:
1748 case TSK_ExplicitSpecialization:
1749 return false;
1750
1751 case TSK_ImplicitInstantiation:
1752 case TSK_ExplicitInstantiationDeclaration:
1753 case TSK_ExplicitInstantiationDefinition:
1754 // Handle below.
1755 break;
1756 }
1757
1758 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001759 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001760 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001761 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001762
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001763 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001764 return PatternDecl->isInlined();
1765
1766 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001767}
1768
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001769/// \brief For a function declaration in C or C++, determine whether this
1770/// declaration causes the definition to be externally visible.
1771///
1772/// Determines whether this is the first non-inline redeclaration of an inline
1773/// function in a language where "inline" does not normally require an
1774/// externally visible definition.
1775bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1776 assert(!doesThisDeclarationHaveABody() &&
1777 "Must have a declaration without a body.");
1778
1779 ASTContext &Context = getASTContext();
1780
1781 // In C99 mode, a function may have an inline definition (causing it to
1782 // be deferred) then redeclared later. As a special case, "extern inline"
1783 // is not required to produce an external symbol.
1784 if (Context.getLangOptions().GNUInline || !Context.getLangOptions().C99 ||
1785 Context.getLangOptions().CPlusPlus)
1786 return false;
1787 if (getLinkage() != ExternalLinkage || isInlineSpecified())
1788 return false;
Nick Lewyckyba4cc012011-07-18 07:11:55 +00001789 const FunctionDecl *Definition = 0;
1790 if (hasBody(Definition))
1791 return Definition->isInlined() &&
1792 Definition->isInlineDefinitionExternallyVisible();
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001793 return false;
1794}
1795
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001796/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001797/// definition will be externally visible.
1798///
1799/// Inline function definitions are always available for inlining optimizations.
1800/// However, depending on the language dialect, declaration specifiers, and
1801/// attributes, the definition of an inline function may or may not be
1802/// "externally" visible to other translation units in the program.
1803///
1804/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001805/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001806/// inline definition becomes externally visible (C99 6.7.4p6).
1807///
1808/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1809/// definition, we use the GNU semantics for inline, which are nearly the
1810/// opposite of C99 semantics. In particular, "inline" by itself will create
1811/// an externally visible symbol, but "extern inline" will not create an
1812/// externally visible symbol.
1813bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001814 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001815 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001816 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001817
Rafael Espindolafb2af642011-06-02 16:13:27 +00001818 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001819 // If it's not the case that both 'inline' and 'extern' are
1820 // specified on the definition, then this inline definition is
1821 // externally visible.
1822 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1823 return true;
1824
1825 // If any declaration is 'inline' but not 'extern', then this definition
1826 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001827 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1828 Redecl != RedeclEnd;
1829 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001830 if (Redecl->isInlineSpecified() &&
1831 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001832 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001833 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001834
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001835 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001836 }
1837
1838 // C99 6.7.4p6:
1839 // [...] If all of the file scope declarations for a function in a
1840 // translation unit include the inline function specifier without extern,
1841 // then the definition in that translation unit is an inline definition.
1842 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1843 Redecl != RedeclEnd;
1844 ++Redecl) {
1845 // Only consider file-scope declarations in this test.
1846 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1847 continue;
1848
John McCall8e7d6562010-08-26 03:08:43 +00001849 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001850 return true; // Not an inline definition
1851 }
1852
1853 // C99 6.7.4p6:
1854 // An inline definition does not provide an external definition for the
1855 // function, and does not forbid an external definition in another
1856 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001857 return false;
1858}
1859
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001860/// getOverloadedOperator - Which C++ overloaded operator this
1861/// function represents, if any.
1862OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001863 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1864 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001865 else
1866 return OO_None;
1867}
1868
Alexis Huntc88db062010-01-13 09:01:02 +00001869/// getLiteralIdentifier - The literal suffix identifier this function
1870/// represents, if any.
1871const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1872 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1873 return getDeclName().getCXXLiteralIdentifier();
1874 else
1875 return 0;
1876}
1877
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001878FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1879 if (TemplateOrSpecialization.isNull())
1880 return TK_NonTemplate;
1881 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1882 return TK_FunctionTemplate;
1883 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1884 return TK_MemberSpecialization;
1885 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1886 return TK_FunctionTemplateSpecialization;
1887 if (TemplateOrSpecialization.is
1888 <DependentFunctionTemplateSpecializationInfo*>())
1889 return TK_DependentFunctionTemplateSpecialization;
1890
1891 assert(false && "Did we miss a TemplateOrSpecialization type?");
1892 return TK_NonTemplate;
1893}
1894
Douglas Gregord801b062009-10-07 23:56:10 +00001895FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001896 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001897 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1898
1899 return 0;
1900}
1901
Douglas Gregor06db9f52009-10-12 20:18:28 +00001902MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1903 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1904}
1905
Douglas Gregord801b062009-10-07 23:56:10 +00001906void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001907FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1908 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001909 TemplateSpecializationKind TSK) {
1910 assert(TemplateOrSpecialization.isNull() &&
1911 "Member function is already a specialization");
1912 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001913 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001914 TemplateOrSpecialization = Info;
1915}
1916
Douglas Gregorafca3b42009-10-27 20:53:28 +00001917bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001918 // If the function is invalid, it can't be implicitly instantiated.
1919 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001920 return false;
1921
1922 switch (getTemplateSpecializationKind()) {
1923 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00001924 case TSK_ExplicitInstantiationDefinition:
1925 return false;
1926
1927 case TSK_ImplicitInstantiation:
1928 return true;
1929
Francois Pichet00c7e6c2011-08-14 03:52:19 +00001930 // It is possible to instantiate TSK_ExplicitSpecialization kind
1931 // if the FunctionDecl has a class scope specialization pattern.
1932 case TSK_ExplicitSpecialization:
1933 return getClassScopeSpecializationPattern() != 0;
1934
Douglas Gregorafca3b42009-10-27 20:53:28 +00001935 case TSK_ExplicitInstantiationDeclaration:
1936 // Handled below.
1937 break;
1938 }
1939
1940 // Find the actual template from which we will instantiate.
1941 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001942 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001943 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001944 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001945
1946 // C++0x [temp.explicit]p9:
1947 // Except for inline functions, other explicit instantiation declarations
1948 // have the effect of suppressing the implicit instantiation of the entity
1949 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001950 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001951 return true;
1952
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001953 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001954}
1955
1956FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00001957 // Handle class scope explicit specialization special case.
1958 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1959 return getClassScopeSpecializationPattern();
1960
Douglas Gregorafca3b42009-10-27 20:53:28 +00001961 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1962 while (Primary->getInstantiatedFromMemberTemplate()) {
1963 // If we have hit a point where the user provided a specialization of
1964 // this template, we're done looking.
1965 if (Primary->isMemberSpecialization())
1966 break;
1967
1968 Primary = Primary->getInstantiatedFromMemberTemplate();
1969 }
1970
1971 return Primary->getTemplatedDecl();
1972 }
1973
1974 return getInstantiatedFromMemberFunction();
1975}
1976
Douglas Gregor70d83e22009-06-29 17:30:29 +00001977FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001978 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001979 = TemplateOrSpecialization
1980 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001981 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001982 }
1983 return 0;
1984}
1985
Francois Pichet00c7e6c2011-08-14 03:52:19 +00001986FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
1987 return getASTContext().getClassScopeSpecializationPattern(this);
1988}
1989
Douglas Gregor70d83e22009-06-29 17:30:29 +00001990const TemplateArgumentList *
1991FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001992 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001993 = TemplateOrSpecialization
1994 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001995 return Info->TemplateArguments;
1996 }
1997 return 0;
1998}
1999
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002000const TemplateArgumentListInfo *
2001FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2002 if (FunctionTemplateSpecializationInfo *Info
2003 = TemplateOrSpecialization
2004 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2005 return Info->TemplateArgumentsAsWritten;
2006 }
2007 return 0;
2008}
2009
Mike Stump11289f42009-09-09 15:08:12 +00002010void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002011FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2012 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002013 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002014 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002015 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002016 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2017 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002018 assert(TSK != TSK_Undeclared &&
2019 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002020 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002021 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002022 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002023 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2024 TemplateArgs,
2025 TemplateArgsAsWritten,
2026 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002027 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00002028
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002029 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002030 // function template specializations.
2031 if (InsertPos)
Sebastian Redl9ab988f2011-04-14 14:07:59 +00002032 Template->addSpecialization(Info, InsertPos);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002033 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00002034 // Try to insert the new node. If there is an existing node, leave it, the
2035 // set will contain the canonical decls while
2036 // FunctionTemplateDecl::findSpecialization will return
2037 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002038 FunctionTemplateSpecializationInfo *Existing
2039 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00002040 (void)Existing;
2041 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
2042 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002043 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002044}
2045
John McCallb9c78482010-04-08 09:05:18 +00002046void
2047FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2048 const UnresolvedSetImpl &Templates,
2049 const TemplateArgumentListInfo &TemplateArgs) {
2050 assert(TemplateOrSpecialization.isNull());
2051 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2052 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002053 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002054 void *Buffer = Context.Allocate(Size);
2055 DependentFunctionTemplateSpecializationInfo *Info =
2056 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2057 TemplateArgs);
2058 TemplateOrSpecialization = Info;
2059}
2060
2061DependentFunctionTemplateSpecializationInfo::
2062DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2063 const TemplateArgumentListInfo &TArgs)
2064 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2065
2066 d.NumTemplates = Ts.size();
2067 d.NumArgs = TArgs.size();
2068
2069 FunctionTemplateDecl **TsArray =
2070 const_cast<FunctionTemplateDecl**>(getTemplates());
2071 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2072 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2073
2074 TemplateArgumentLoc *ArgsArray =
2075 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2076 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2077 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2078}
2079
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002080TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002081 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002082 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002083 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002084 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002085 if (FTSInfo)
2086 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002087
Douglas Gregord801b062009-10-07 23:56:10 +00002088 MemberSpecializationInfo *MSInfo
2089 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2090 if (MSInfo)
2091 return MSInfo->getTemplateSpecializationKind();
2092
2093 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002094}
2095
Mike Stump11289f42009-09-09 15:08:12 +00002096void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002097FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2098 SourceLocation PointOfInstantiation) {
2099 if (FunctionTemplateSpecializationInfo *FTSInfo
2100 = TemplateOrSpecialization.dyn_cast<
2101 FunctionTemplateSpecializationInfo*>()) {
2102 FTSInfo->setTemplateSpecializationKind(TSK);
2103 if (TSK != TSK_ExplicitSpecialization &&
2104 PointOfInstantiation.isValid() &&
2105 FTSInfo->getPointOfInstantiation().isInvalid())
2106 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2107 } else if (MemberSpecializationInfo *MSInfo
2108 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2109 MSInfo->setTemplateSpecializationKind(TSK);
2110 if (TSK != TSK_ExplicitSpecialization &&
2111 PointOfInstantiation.isValid() &&
2112 MSInfo->getPointOfInstantiation().isInvalid())
2113 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2114 } else
2115 assert(false && "Function cannot have a template specialization kind");
2116}
2117
2118SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002119 if (FunctionTemplateSpecializationInfo *FTSInfo
2120 = TemplateOrSpecialization.dyn_cast<
2121 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002122 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002123 else if (MemberSpecializationInfo *MSInfo
2124 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002125 return MSInfo->getPointOfInstantiation();
2126
2127 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002128}
2129
Douglas Gregor6411b922009-09-11 20:15:17 +00002130bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002131 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002132 return true;
2133
2134 // If this function was instantiated from a member function of a
2135 // class template, check whether that member function was defined out-of-line.
2136 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2137 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002138 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002139 return Definition->isOutOfLine();
2140 }
2141
2142 // If this function was instantiated from a function template,
2143 // check whether that function template was defined out-of-line.
2144 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2145 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002146 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002147 return Definition->isOutOfLine();
2148 }
2149
2150 return false;
2151}
2152
Abramo Bagnaraea947882011-03-08 16:41:52 +00002153SourceRange FunctionDecl::getSourceRange() const {
2154 return SourceRange(getOuterLocStart(), EndRangeLoc);
2155}
2156
Chris Lattner59a25942008-03-31 00:36:02 +00002157//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002158// FieldDecl Implementation
2159//===----------------------------------------------------------------------===//
2160
Jay Foad39c79802011-01-12 09:06:06 +00002161FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002162 SourceLocation StartLoc, SourceLocation IdLoc,
2163 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002164 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2165 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002166 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002167 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002168}
2169
2170bool FieldDecl::isAnonymousStructOrUnion() const {
2171 if (!isImplicit() || getDeclName())
2172 return false;
2173
2174 if (const RecordType *Record = getType()->getAs<RecordType>())
2175 return Record->getDecl()->isAnonymousStructOrUnion();
2176
2177 return false;
2178}
2179
John McCall4e819612011-01-20 07:57:12 +00002180unsigned FieldDecl::getFieldIndex() const {
2181 if (CachedFieldIndex) return CachedFieldIndex - 1;
2182
2183 unsigned index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002184 const RecordDecl *RD = getParent();
2185 const FieldDecl *LastFD = 0;
2186 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
2187
2188 RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
John McCall4e819612011-01-20 07:57:12 +00002189 while (true) {
2190 assert(i != e && "failed to find field in parent!");
2191 if (*i == this)
2192 break;
2193
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002194 if (IsMsStruct) {
2195 // Zero-length bitfields following non-bitfield members are ignored.
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00002196 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD)) {
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002197 ++i;
2198 continue;
2199 }
2200 LastFD = (*i);
2201 }
John McCall4e819612011-01-20 07:57:12 +00002202 ++i;
2203 ++index;
2204 }
2205
2206 CachedFieldIndex = index + 1;
2207 return index;
2208}
2209
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002210SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002211 if (const Expr *E = InitializerOrBitWidth.getPointer())
2212 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002213 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002214}
2215
Richard Smith938f40b2011-06-11 17:19:42 +00002216void FieldDecl::setInClassInitializer(Expr *Init) {
2217 assert(!InitializerOrBitWidth.getPointer() &&
2218 "bit width or initializer already set");
2219 InitializerOrBitWidth.setPointer(Init);
2220 InitializerOrBitWidth.setInt(0);
2221}
2222
Sebastian Redl833ef452010-01-26 22:01:41 +00002223//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002224// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002225//===----------------------------------------------------------------------===//
2226
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002227SourceLocation TagDecl::getOuterLocStart() const {
2228 return getTemplateOrInnerLocStart(this);
2229}
2230
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002231SourceRange TagDecl::getSourceRange() const {
2232 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002233 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002234}
2235
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002236TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002237 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002238}
2239
Richard Smithdda56e42011-04-15 14:24:37 +00002240void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2241 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002242 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002243 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002244 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002245}
2246
Douglas Gregordee1be82009-01-17 00:42:38 +00002247void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002248 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002249
2250 if (isa<CXXRecordDecl>(this)) {
2251 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2252 struct CXXRecordDecl::DefinitionData *Data =
2253 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002254 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2255 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002256 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002257}
2258
2259void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002260 assert((!isa<CXXRecordDecl>(this) ||
2261 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2262 "definition completed but not started");
2263
Douglas Gregordee1be82009-01-17 00:42:38 +00002264 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002265 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002266
2267 if (ASTMutationListener *L = getASTMutationListener())
2268 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002269}
2270
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002271TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002272 if (isDefinition())
2273 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002274 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2275 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002276
2277 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002278 R != REnd; ++R)
2279 if (R->isDefinition())
2280 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002281
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002282 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002283}
2284
Douglas Gregor14454802011-02-25 02:25:35 +00002285void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2286 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002287 // Make sure the extended qualifier info is allocated.
2288 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002289 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002290 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002291 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002292 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002293 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002294 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002295 if (getExtInfo()->NumTemplParamLists == 0) {
2296 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002297 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002298 }
2299 else
2300 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002301 }
2302 }
2303}
2304
Abramo Bagnara60804e12011-03-18 15:16:37 +00002305void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2306 unsigned NumTPLists,
2307 TemplateParameterList **TPLists) {
2308 assert(NumTPLists > 0);
2309 // Make sure the extended decl info is allocated.
2310 if (!hasExtInfo())
2311 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002312 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002313 // Set the template parameter lists info.
2314 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2315}
2316
Ted Kremenek21475702008-09-05 17:16:31 +00002317//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002318// EnumDecl Implementation
2319//===----------------------------------------------------------------------===//
2320
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002321EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2322 SourceLocation StartLoc, SourceLocation IdLoc,
2323 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002324 EnumDecl *PrevDecl, bool IsScoped,
2325 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002326 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002327 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002328 C.getTypeDeclType(Enum, PrevDecl);
2329 return Enum;
2330}
2331
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002332EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002333 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002334 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002335}
2336
Douglas Gregord5058122010-02-11 01:19:42 +00002337void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002338 QualType NewPromotionType,
2339 unsigned NumPositiveBits,
2340 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002341 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002342 if (!IntegerType)
2343 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002344 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002345 setNumPositiveBits(NumPositiveBits);
2346 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002347 TagDecl::completeDefinition();
2348}
2349
2350//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002351// RecordDecl Implementation
2352//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002353
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002354RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2355 SourceLocation StartLoc, SourceLocation IdLoc,
2356 IdentifierInfo *Id, RecordDecl *PrevDecl)
2357 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002358 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002359 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002360 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002361 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002362 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002363}
2364
Jay Foad39c79802011-01-12 09:06:06 +00002365RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002366 SourceLocation StartLoc, SourceLocation IdLoc,
2367 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2368 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2369 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002370 C.getTypeDeclType(R, PrevDecl);
2371 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002372}
2373
Jay Foad39c79802011-01-12 09:06:06 +00002374RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002375 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2376 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002377}
2378
Douglas Gregordfcad112009-03-25 15:59:44 +00002379bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002380 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002381 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2382}
2383
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002384RecordDecl::field_iterator RecordDecl::field_begin() const {
2385 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2386 LoadFieldsFromExternalStorage();
2387
2388 return field_iterator(decl_iterator(FirstDecl));
2389}
2390
Douglas Gregorb11aad82011-02-19 18:51:44 +00002391/// completeDefinition - Notes that the definition of this type is now
2392/// complete.
2393void RecordDecl::completeDefinition() {
2394 assert(!isDefinition() && "Cannot redefine record!");
2395 TagDecl::completeDefinition();
2396}
2397
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002398void RecordDecl::LoadFieldsFromExternalStorage() const {
2399 ExternalASTSource *Source = getASTContext().getExternalSource();
2400 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2401
2402 // Notify that we have a RecordDecl doing some initialization.
2403 ExternalASTSource::Deserializing TheFields(Source);
2404
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002405 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002406 LoadedFieldsFromExternalStorage = true;
2407 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2408 case ELR_Success:
2409 break;
2410
2411 case ELR_AlreadyLoaded:
2412 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002413 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002414 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002415
2416#ifndef NDEBUG
2417 // Check that all decls we got were FieldDecls.
2418 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2419 assert(isa<FieldDecl>(Decls[i]));
2420#endif
2421
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002422 if (Decls.empty())
2423 return;
2424
2425 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2426}
2427
Steve Naroff415d3d52008-10-08 17:01:13 +00002428//===----------------------------------------------------------------------===//
2429// BlockDecl Implementation
2430//===----------------------------------------------------------------------===//
2431
Douglas Gregord5058122010-02-11 01:19:42 +00002432void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002433 unsigned NParms) {
2434 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002435
Steve Naroffc4b30e52009-03-13 16:56:44 +00002436 // Zero params -> null pointer.
2437 if (NParms) {
2438 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002439 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002440 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2441 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2442 }
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}