blob: ade6e2bc1b9e36d09375447f18c4d61d444c84e1 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000027#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000028#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000029#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000031
David Blaikie9c70e042011-09-21 18:16:56 +000032#include <algorithm>
33
Chris Lattner6d9a6852006-10-25 05:11:20 +000034using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000035
Chris Lattner88f70d62008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor1baf38f2011-03-26 12:10:19 +000040static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
41 // If this declaration has an explicit visibility attribute, use it.
42 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
43 switch (A->getVisibility()) {
44 case VisibilityAttr::Default:
45 return DefaultVisibility;
46 case VisibilityAttr::Hidden:
47 return HiddenVisibility;
48 case VisibilityAttr::Protected:
49 return ProtectedVisibility;
50 }
John McCall457a04e2010-10-22 21:05:15 +000051 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +000052
53 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
54 // implies visibility(default).
Douglas Gregore8bbc122011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +000056 for (specific_attr_iterator<AvailabilityAttr>
57 A = D->specific_attr_begin<AvailabilityAttr>(),
58 AEnd = D->specific_attr_end<AvailabilityAttr>();
59 A != AEnd; ++A)
60 if ((*A)->getPlatform()->getName().equals("macosx"))
61 return DefaultVisibility;
62 }
63
64 return llvm::Optional<Visibility>();
John McCall457a04e2010-10-22 21:05:15 +000065}
66
John McCallc273f242010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallc273f242010-10-30 11:50:40 +000068
Rafael Espindola2f869a32012-01-14 00:30:36 +000069static LinkageInfo getLVForType(QualType T) {
70 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
71 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
72}
73
Douglas Gregor7dc5c172010-02-03 09:33:45 +000074/// \brief Get the most restrictive linkage for the types in the given
75/// template parameter list.
Rafael Espindola2f869a32012-01-14 00:30:36 +000076static LinkageInfo
John McCall457a04e2010-10-22 21:05:15 +000077getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000078 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000079 for (TemplateParameterList::const_iterator P = Params->begin(),
80 PEnd = Params->end();
81 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000082 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
83 if (NTTP->isExpandedParameterPack()) {
84 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
85 QualType T = NTTP->getExpansionType(I);
86 if (!T->isDependentType())
Rafael Espindola2f869a32012-01-14 00:30:36 +000087 LV.merge(getLVForType(T));
Douglas Gregor0231d8d2011-01-19 20:10:05 +000088 }
89 continue;
90 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +000091
Douglas Gregor7dc5c172010-02-03 09:33:45 +000092 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000093 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000094 continue;
95 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +000096 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +000097
98 if (TemplateTemplateParmDecl *TTP
99 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000100 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000101 }
102 }
103
John McCall457a04e2010-10-22 21:05:15 +0000104 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000105}
106
Douglas Gregorbf62d642010-12-06 18:36:25 +0000107/// getLVForDecl - Get the linkage and visibility for the given declaration.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000108static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000109
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000110/// \brief Get the most restrictive linkage for the types and
111/// declarations in the given template argument list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000112static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
113 unsigned NumArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000114 bool OnlyTemplate) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000115 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000116
117 for (unsigned I = 0; I != NumArgs; ++I) {
118 switch (Args[I].getKind()) {
119 case TemplateArgument::Null:
120 case TemplateArgument::Integral:
121 case TemplateArgument::Expression:
122 break;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000123
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000124 case TemplateArgument::Type:
Rafael Espindola2f869a32012-01-14 00:30:36 +0000125 LV.merge(getLVForType(Args[I].getAsType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000126 break;
127
128 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000129 // The decl can validly be null as the representation of nullptr
130 // arguments, valid only in C++0x.
131 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000132 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000133 LV = merge(LV, getLVForDecl(ND, OnlyTemplate));
John McCall457a04e2010-10-22 21:05:15 +0000134 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 break;
136
137 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000138 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000139 if (TemplateDecl *Template
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000140 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000141 LV.merge(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000142 break;
143
144 case TemplateArgument::Pack:
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000145 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
146 Args[I].pack_size(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000147 OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000148 break;
149 }
150 }
151
John McCall457a04e2010-10-22 21:05:15 +0000152 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000153}
154
Rafael Espindola2f869a32012-01-14 00:30:36 +0000155static LinkageInfo
Douglas Gregorbf62d642010-12-06 18:36:25 +0000156getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000157 bool OnlyTemplate) {
158 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall8823c652010-08-13 08:35:10 +0000159}
160
John McCallb8c604a2011-06-27 23:06:04 +0000161static bool shouldConsiderTemplateLV(const FunctionDecl *fn,
162 const FunctionTemplateSpecializationInfo *spec) {
163 return !(spec->isExplicitSpecialization() &&
164 fn->hasAttr<VisibilityAttr>());
165}
166
167static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
168 return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
169}
170
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000171static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
172 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000173 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000174 "Not a name having namespace scope");
175 ASTContext &Context = D->getASTContext();
176
177 // C++ [basic.link]p3:
178 // A name having namespace scope (3.3.6) has internal linkage if it
179 // is the name of
180 // - an object, reference, function or function template that is
181 // explicitly declared static; or,
182 // (This bullet corresponds to C99 6.2.2p3.)
183 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
184 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000185 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000186 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000187
188 // - an object or reference that is explicitly declared const
189 // and neither explicitly declared extern nor previously
190 // declared to have external linkage; or
191 // (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000192 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000193 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000194 Var->getStorageClass() != SC_Extern &&
195 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000196 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000197 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000198 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000199 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000200 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000201 FoundExtern = true;
202
203 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000204 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000205 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000206 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000207 const VarDecl *PrevVar = Var->getPreviousDecl();
208 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000209 if (PrevVar->getStorageClass() == SC_PrivateExtern)
210 break;
211 if (PrevVar)
212 return PrevVar->getLinkageAndVisibility();
213 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000214 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000215 // C++ [temp]p4:
216 // A non-member function template can have internal linkage; any
217 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000218 const FunctionDecl *Function = 0;
219 if (const FunctionTemplateDecl *FunTmpl
220 = dyn_cast<FunctionTemplateDecl>(D))
221 Function = FunTmpl->getTemplatedDecl();
222 else
223 Function = cast<FunctionDecl>(D);
224
225 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000226 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000227 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000228 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
229 // - a data member of an anonymous union.
230 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000231 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000232 }
233
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000234 if (D->isInAnonymousNamespace()) {
235 const VarDecl *Var = dyn_cast<VarDecl>(D);
236 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000237 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
238 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000239 return LinkageInfo::uniqueExternal();
240 }
John McCallb7139c42010-10-28 04:18:25 +0000241
John McCall457a04e2010-10-22 21:05:15 +0000242 // Set up the defaults.
243
244 // C99 6.2.2p5:
245 // If the declaration of an identifier for an object has file
246 // scope and no storage-class specifier, its linkage is
247 // external.
John McCallc273f242010-10-30 11:50:40 +0000248 LinkageInfo LV;
249
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000250 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000251 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000252 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000253 } else {
254 // If we're declared in a namespace with a visibility attribute,
255 // use that namespace's visibility, but don't call it explicit.
256 for (const DeclContext *DC = D->getDeclContext();
257 !isa<TranslationUnitDecl>(DC);
258 DC = DC->getParent()) {
259 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
260 if (!ND) continue;
261 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000262 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000263 break;
264 }
265 }
266 }
267 }
268
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000269 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000270 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000271
Douglas Gregorf73b2822009-11-25 22:24:25 +0000272 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000273
Douglas Gregorf73b2822009-11-25 22:24:25 +0000274 // A name having namespace scope has external linkage if it is the
275 // name of
276 //
277 // - an object or reference, unless it has internal linkage; or
278 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000279 // GCC applies the following optimization to variables and static
280 // data members, but not to functions:
281 //
John McCall457a04e2010-10-22 21:05:15 +0000282 // Modify the variable's LV by the LV of its type unless this is
283 // C or extern "C". This follows from [basic.link]p9:
284 // A type without linkage shall not be used as the type of a
285 // variable or function with external linkage unless
286 // - the entity has C language linkage, or
287 // - the entity is declared within an unnamed namespace, or
288 // - the entity is not used or is defined in the same
289 // translation unit.
290 // and [basic.link]p10:
291 // ...the types specified by all declarations referring to a
292 // given variable or function shall be identical...
293 // C does not have an equivalent rule.
294 //
John McCall5fe84122010-10-26 04:59:26 +0000295 // Ignore this if we've got an explicit attribute; the user
296 // probably knows what they're doing.
297 //
John McCall457a04e2010-10-22 21:05:15 +0000298 // Note that we don't want to make the variable non-external
299 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000300 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000301 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000302 LinkageInfo TypeLV = getLVForType(Var->getType());
303 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000304 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000305 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000306 }
307
John McCall23032652010-11-02 18:38:13 +0000308 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000309 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000310
David Blaikiebbafb8a2012-03-11 07:00:24 +0000311 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000312 (Var->getStorageClass() == SC_Extern ||
313 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000314
Douglas Gregorf73b2822009-11-25 22:24:25 +0000315 // C99 6.2.2p4:
316 // For an identifier declared with the storage-class specifier
317 // extern in a scope in which a prior declaration of that
318 // identifier is visible, if the prior declaration specifies
319 // internal or external linkage, the linkage of the identifier
320 // at the later declaration is the same as the linkage
321 // specified at the prior declaration. If no prior declaration
322 // is visible, or if the prior declaration specifies no
323 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000324 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000325 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000326 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
327 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000328 }
329 }
330
Douglas Gregorf73b2822009-11-25 22:24:25 +0000331 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000332 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000333 // In theory, we can modify the function's LV by the LV of its
334 // type unless it has C linkage (see comment above about variables
335 // for justification). In practice, GCC doesn't do this, so it's
336 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000337
John McCall23032652010-11-02 18:38:13 +0000338 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000339 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000340
Douglas Gregorf73b2822009-11-25 22:24:25 +0000341 // C99 6.2.2p5:
342 // If the declaration of an identifier for a function has no
343 // storage-class specifier, its linkage is determined exactly
344 // as if it were declared with the storage-class specifier
345 // extern.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000346 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000347 (Function->getStorageClass() == SC_Extern ||
348 Function->getStorageClass() == SC_PrivateExtern ||
349 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000350 // C99 6.2.2p4:
351 // For an identifier declared with the storage-class specifier
352 // extern in a scope in which a prior declaration of that
353 // identifier is visible, if the prior declaration specifies
354 // internal or external linkage, the linkage of the identifier
355 // at the later declaration is the same as the linkage
356 // specified at the prior declaration. If no prior declaration
357 // is visible, or if the prior declaration specifies no
358 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000359 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000360 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000361 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
362 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000363 }
364 }
365
John McCallf768aa72011-02-10 06:50:24 +0000366 // In C++, then if the type of the function uses a type with
367 // unique-external linkage, it's not legally usable from outside
368 // this translation unit. However, we should use the C linkage
369 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000370 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000371 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000372 Function->getType()->getLinkage() == UniqueExternalLinkage)
373 return LinkageInfo::uniqueExternal();
374
John McCallb8c604a2011-06-27 23:06:04 +0000375 // Consider LV from the template and the template arguments unless
376 // this is an explicit specialization with a visibility attribute.
377 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000378 = Function->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000379 if (shouldConsiderTemplateLV(Function, specInfo)) {
380 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000381 true));
John McCallb8c604a2011-06-27 23:06:04 +0000382 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000383 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs,
384 OnlyTemplate));
John McCallb8c604a2011-06-27 23:06:04 +0000385 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000386 }
387
Douglas Gregorf73b2822009-11-25 22:24:25 +0000388 // - a named class (Clause 9), or an unnamed class defined in a
389 // typedef declaration in which the class has the typedef name
390 // for linkage purposes (7.1.3); or
391 // - a named enumeration (7.2), or an unnamed enumeration
392 // defined in a typedef declaration in which the enumeration
393 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000394 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
395 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000396 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000397 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000398
John McCall457a04e2010-10-22 21:05:15 +0000399 // If this is a class template specialization, consider the
400 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000401 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000402 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCallb8c604a2011-06-27 23:06:04 +0000403 if (shouldConsiderTemplateLV(spec)) {
404 // From the template.
405 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000406 true));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000407
John McCallb8c604a2011-06-27 23:06:04 +0000408 // The arguments at which the template was instantiated.
409 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000410 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs,
411 OnlyTemplate));
John McCallb8c604a2011-06-27 23:06:04 +0000412 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000413 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000414
415 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000416 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000417 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
418 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000419 if (!isExternalLinkage(EnumLV.linkage()))
420 return LinkageInfo::none();
421 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000422
423 // - a template, unless it is a function template that has
424 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000425 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000426 if (!OnlyTemplate)
John McCall8bc6d5b2011-03-04 10:39:25 +0000427 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000428
Douglas Gregorf73b2822009-11-25 22:24:25 +0000429 // - a namespace (7.3), unless it is declared within an unnamed
430 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000431 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
432 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000433
John McCall457a04e2010-10-22 21:05:15 +0000434 // By extension, we assign external linkage to Objective-C
435 // interfaces.
436 } else if (isa<ObjCInterfaceDecl>(D)) {
437 // fallout
438
439 // Everything not covered here has no linkage.
440 } else {
John McCallc273f242010-10-30 11:50:40 +0000441 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000442 }
443
444 // If we ended up with non-external linkage, visibility should
445 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000446 if (LV.linkage() != ExternalLinkage)
447 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000448
John McCall457a04e2010-10-22 21:05:15 +0000449 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000450}
451
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000452static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000453 // Only certain class members have linkage. Note that fields don't
454 // really have linkage, but it's convenient to say they do for the
455 // purposes of calculating linkage of pointer-to-data-member
456 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000457 if (!(isa<CXXMethodDecl>(D) ||
458 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000459 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000460 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000461 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000462 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000463
John McCall07072662010-11-02 01:45:15 +0000464 LinkageInfo LV;
465
John McCall07072662010-11-02 01:45:15 +0000466 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000467 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000468 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000469 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000470 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000471
472 // If this class member has an explicit visibility attribute, the only
473 // thing that can change its visibility is the template arguments, so
474 // only look for them when processing the the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000475 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000476
477 // If we're paying attention to global visibility, apply
478 // -finline-visibility-hidden if this is an inline method.
479 //
480 // Note that we do this before merging information about
481 // the class visibility.
482 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
483 TemplateSpecializationKind TSK = TSK_Undeclared;
484 if (FunctionTemplateSpecializationInfo *spec
485 = MD->getTemplateSpecializationInfo()) {
486 TSK = spec->getTemplateSpecializationKind();
487 } else if (MemberSpecializationInfo *MSI =
488 MD->getMemberSpecializationInfo()) {
489 TSK = MSI->getTemplateSpecializationKind();
490 }
491
492 const FunctionDecl *Def = 0;
493 // InlineVisibilityHidden only applies to definitions, and
494 // isInlined() only gives meaningful answers on definitions
495 // anyway.
496 if (TSK != TSK_ExplicitInstantiationDeclaration &&
497 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000498 !OnlyTemplate &&
Rafael Espindola505a7c82012-04-16 18:25:01 +0000499 !LV.visibilityExplicit() &&
500 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
501 MD->hasBody(Def) && Def->isInlined())
502 LV.mergeVisibility(HiddenVisibility, true);
503 }
John McCallc273f242010-10-30 11:50:40 +0000504
Rafael Espindola53cf2192012-04-19 05:50:08 +0000505 // If this member has an visibility attribute, ClassF will exclude
506 // attributes on the class or command line options, keeping only information
507 // about the template instantiation. If the member has no visibility
508 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
509 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000510 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
511 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000512 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000513 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000514
515 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000516 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000517 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000518
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000519 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000520 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000521
John McCall8823c652010-08-13 08:35:10 +0000522 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000523 // If the type of the function uses a type with unique-external
524 // linkage, it's not legally usable from outside this translation unit.
525 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
526 return LinkageInfo::uniqueExternal();
527
John McCall457a04e2010-10-22 21:05:15 +0000528 // If this is a method template specialization, use the linkage for
529 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000530 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000531 = MD->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000532 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000533 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000534 OnlyTemplate));
535 if (!OnlyTemplate)
John McCallb8c604a2011-06-27 23:06:04 +0000536 LV.merge(getLVForTemplateParameterList(
537 spec->getTemplate()->getTemplateParameters()));
538 }
John McCalle6e622e2010-11-01 01:29:57 +0000539 }
John McCall457a04e2010-10-22 21:05:15 +0000540
John McCall37bb6c92010-10-29 22:22:43 +0000541 // Note that in contrast to basically every other situation, we
542 // *do* apply -fvisibility to method declarations.
543
544 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000545 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000546 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-06-27 23:06:04 +0000547 if (shouldConsiderTemplateLV(spec)) {
548 // Merge template argument/parameter information for member
549 // class template specializations.
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000550 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000551 OnlyTemplate));
552 if (!OnlyTemplate)
John McCall8bc6d5b2011-03-04 10:39:25 +0000553 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000554 spec->getSpecializedTemplate()->getTemplateParameters()));
555 }
John McCall37bb6c92010-10-29 22:22:43 +0000556 }
557
John McCall37bb6c92010-10-29 22:22:43 +0000558 // Static data members.
559 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000560 // Modify the variable's linkage by its type, but ignore the
561 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000562 LinkageInfo TypeLV = getLVForType(VD->getType());
563 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000564 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000565 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000566 }
567
John McCall457a04e2010-10-22 21:05:15 +0000568 return LV;
John McCall8823c652010-08-13 08:35:10 +0000569}
570
John McCalld396b972011-02-08 19:01:05 +0000571static void clearLinkageForClass(const CXXRecordDecl *record) {
572 for (CXXRecordDecl::decl_iterator
573 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
574 Decl *child = *i;
575 if (isa<NamedDecl>(child))
576 cast<NamedDecl>(child)->ClearLinkageCache();
577 }
578}
579
David Blaikie68e081d2011-12-20 02:48:34 +0000580void NamedDecl::anchor() { }
581
John McCalld396b972011-02-08 19:01:05 +0000582void NamedDecl::ClearLinkageCache() {
583 // Note that we can't skip clearing the linkage of children just
584 // because the parent doesn't have cached linkage: we don't cache
585 // when computing linkage for parent contexts.
586
587 HasCachedLinkage = 0;
588
589 // If we're changing the linkage of a class, we need to reset the
590 // linkage of child declarations, too.
591 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
592 clearLinkageForClass(record);
593
John McCall83779672011-02-19 02:53:41 +0000594 if (ClassTemplateDecl *temp =
595 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000596 // Clear linkage for the template pattern.
597 CXXRecordDecl *record = temp->getTemplatedDecl();
598 record->HasCachedLinkage = 0;
599 clearLinkageForClass(record);
600
John McCall83779672011-02-19 02:53:41 +0000601 // We need to clear linkage for specializations, too.
602 for (ClassTemplateDecl::spec_iterator
603 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
604 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000605 }
John McCall83779672011-02-19 02:53:41 +0000606
607 // Clear cached linkage for function template decls, too.
608 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000609 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
610 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000611 for (FunctionTemplateDecl::spec_iterator
612 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
613 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000614 }
John McCall83779672011-02-19 02:53:41 +0000615
John McCalld396b972011-02-08 19:01:05 +0000616}
617
Douglas Gregorbf62d642010-12-06 18:36:25 +0000618Linkage NamedDecl::getLinkage() const {
619 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000620 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000621 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000622 return Linkage(CachedLinkage);
623 }
624
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000625 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000626 HasCachedLinkage = 1;
627 return Linkage(CachedLinkage);
628}
629
John McCallc273f242010-10-30 11:50:40 +0000630LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000631 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000632 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000633 HasCachedLinkage = 1;
634 CachedLinkage = LI.linkage();
635 return LI;
John McCall033caa52010-10-29 00:29:13 +0000636}
Ted Kremenek926d8602010-04-20 23:15:35 +0000637
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000638llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
639 // Use the most recent declaration of a variable.
640 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000641 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000642
643 // Use the most recent declaration of a function, and also handle
644 // function template specializations.
645 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
646 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000647 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000648 return V;
649
650 // If the function is a specialization of a template with an
651 // explicit visibility attribute, use that.
652 if (FunctionTemplateSpecializationInfo *templateInfo
653 = fn->getTemplateSpecializationInfo())
654 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
655
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000656 // If the function is a member of a specialization of a class template
657 // and the corresponding decl has explicit visibility, use that.
658 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
659 if (InstantiatedFrom)
660 return getVisibilityOf(InstantiatedFrom);
661
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000662 return llvm::Optional<Visibility>();
663 }
664
665 // Otherwise, just check the declaration itself first.
666 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
667 return V;
668
669 // If there wasn't explicit visibility there, and this is a
670 // specialization of a class template, check for visibility
671 // on the pattern.
672 if (const ClassTemplateSpecializationDecl *spec
673 = dyn_cast<ClassTemplateSpecializationDecl>(this))
674 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
675
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000676 // If this is a member class of a specialization of a class template
677 // and the corresponding decl has explicit visibility, use that.
678 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
679 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
680 if (InstantiatedFrom)
681 return getVisibilityOf(InstantiatedFrom);
682 }
683
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000684 return llvm::Optional<Visibility>();
685}
686
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000687static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000688 // Objective-C: treat all Objective-C declarations as having external
689 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000690 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000691 default:
692 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000693 case Decl::ParmVar:
694 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000695 case Decl::TemplateTemplateParm: // count these as external
696 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000697 case Decl::ObjCAtDefsField:
698 case Decl::ObjCCategory:
699 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000700 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000701 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000702 case Decl::ObjCMethod:
703 case Decl::ObjCProperty:
704 case Decl::ObjCPropertyImpl:
705 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000706 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000707
708 case Decl::CXXRecord: {
709 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
710 if (Record->isLambda()) {
711 if (!Record->getLambdaManglingNumber()) {
712 // This lambda has no mangling number, so it's internal.
713 return LinkageInfo::internal();
714 }
715
716 // This lambda has its linkage/visibility determined by its owner.
717 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
718 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
719 if (isa<ParmVarDecl>(ContextDecl))
720 DC = ContextDecl->getDeclContext()->getRedeclContext();
721 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000722 return getLVForDecl(cast<NamedDecl>(ContextDecl),
723 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000724 }
725
726 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000727 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000728
729 return LinkageInfo::external();
730 }
731
732 break;
733 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000734 }
735
Douglas Gregorf73b2822009-11-25 22:24:25 +0000736 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000737 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000738 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000739
740 // C++ [basic.link]p5:
741 // In addition, a member function, static data member, a named
742 // class or enumeration of class scope, or an unnamed class or
743 // enumeration defined in a class-scope typedef declaration such
744 // that the class or enumeration has the typedef name for linkage
745 // purposes (7.1.3), has external linkage if the name of the class
746 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000747 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000748 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000749
750 // C++ [basic.link]p6:
751 // The name of a function declared in block scope and the name of
752 // an object declared by a block scope extern declaration have
753 // linkage. If there is a visible declaration of an entity with
754 // linkage having the same name and type, ignoring entities
755 // declared outside the innermost enclosing namespace scope, the
756 // block scope declaration declares that same entity and receives
757 // the linkage of the previous declaration. If there is more than
758 // one such matching entity, the program is ill-formed. Otherwise,
759 // if no matching entity is found, the block scope entity receives
760 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000761 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
762 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000763 if (Function->isInAnonymousNamespace() &&
764 !Function->getDeclContext()->isExternCContext())
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;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000768 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000769 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000770 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000771 }
772
Douglas Gregorec9fd132012-01-14 16:38:05 +0000773 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000774 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
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) {
Eli Friedman839192f2012-01-15 01:23:58 +0000785 if (Var->isInAnonymousNamespace() &&
786 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000787 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000788
John McCallc273f242010-10-30 11:50:40 +0000789 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000790 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000791 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000792 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000793 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000794 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000795 }
796
Douglas Gregorec9fd132012-01-14 16:38:05 +0000797 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000798 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000799 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
800 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000801 }
802
803 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000804 }
805 }
806
807 // C++ [basic.link]p6:
808 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000809 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000810}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000811
Douglas Gregor2ada0482009-02-04 17:27:36 +0000812std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000813 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000814}
815
816std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000817 const DeclContext *Ctx = getDeclContext();
818
819 if (Ctx->isFunctionOrMethod())
820 return getNameAsString();
821
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000822 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000823 ContextsTy Contexts;
824
825 // Collect contexts.
826 while (Ctx && isa<NamedDecl>(Ctx)) {
827 Contexts.push_back(Ctx);
828 Ctx = Ctx->getParent();
829 };
830
831 std::string QualName;
832 llvm::raw_string_ostream OS(QualName);
833
834 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
835 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000836 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000837 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000838 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
839 std::string TemplateArgsStr
840 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000841 TemplateArgs.data(),
842 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000843 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000844 OS << Spec->getName() << TemplateArgsStr;
845 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000846 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000847 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000848 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000849 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000850 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
851 if (!RD->getIdentifier())
852 OS << "<anonymous " << RD->getKindName() << '>';
853 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000854 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000855 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000856 const FunctionProtoType *FT = 0;
857 if (FD->hasWrittenPrototype())
858 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
859
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000860 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000861 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000862 unsigned NumParams = FD->getNumParams();
863 for (unsigned i = 0; i < NumParams; ++i) {
864 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000865 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000866 std::string Param;
867 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000868 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000869 }
870
871 if (FT->isVariadic()) {
872 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000873 OS << ", ";
874 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000875 }
876 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000877 OS << ')';
878 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000879 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000880 }
881 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000882 }
883
John McCalla2a3f7d2010-03-16 21:48:18 +0000884 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000885 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000886 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000887 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000888
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000889 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000890}
891
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000892bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000893 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
894
Douglas Gregor889ceb72009-02-03 19:21:40 +0000895 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
896 // We want to keep it, unless it nominates same namespace.
897 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000898 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
899 ->getOriginalNamespace() ==
900 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
901 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000904 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
905 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000906 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000907
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000908 // For function templates, the underlying function declarations are linked.
909 if (const FunctionTemplateDecl *FunctionTemplate
910 = dyn_cast<FunctionTemplateDecl>(this))
911 if (const FunctionTemplateDecl *OldFunctionTemplate
912 = dyn_cast<FunctionTemplateDecl>(OldD))
913 return FunctionTemplate->getTemplatedDecl()
914 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000915
Steve Naroffc4173fa2009-02-22 19:35:57 +0000916 // For method declarations, we keep track of redeclarations.
917 if (isa<ObjCMethodDecl>(this))
918 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000919
John McCall9f3059a2009-10-09 21:13:30 +0000920 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
921 return true;
922
John McCall3f746822009-11-17 05:59:44 +0000923 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
924 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
925 cast<UsingShadowDecl>(OldD)->getTargetDecl();
926
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000927 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
928 ASTContext &Context = getASTContext();
929 return Context.getCanonicalNestedNameSpecifier(
930 cast<UsingDecl>(this)->getQualifier()) ==
931 Context.getCanonicalNestedNameSpecifier(
932 cast<UsingDecl>(OldD)->getQualifier());
933 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000934
Douglas Gregorb59643b2012-01-03 23:26:26 +0000935 // A typedef of an Objective-C class type can replace an Objective-C class
936 // declaration or definition, and vice versa.
937 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
938 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
939 return true;
940
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000941 // For non-function declarations, if the declarations are of the
942 // same kind then this must be a redeclaration, or semantic analysis
943 // would not have given us the new declaration.
944 return this->getKind() == OldD->getKind();
945}
946
Douglas Gregoreddf4332009-02-24 20:03:32 +0000947bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000948 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000949}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000950
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000951NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000952 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000953 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
954 ND = UD->getTargetDecl();
955
956 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
957 return AD->getClassInterface();
958
959 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000960}
961
John McCalla8ae2222010-04-06 21:38:20 +0000962bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000963 if (!isCXXClassMember())
964 return false;
965
John McCalla8ae2222010-04-06 21:38:20 +0000966 const NamedDecl *D = this;
967 if (isa<UsingShadowDecl>(D))
968 D = cast<UsingShadowDecl>(D)->getTargetDecl();
969
Francois Pichet783dd6e2010-11-21 06:08:52 +0000970 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000971 return true;
972 if (isa<CXXMethodDecl>(D))
973 return cast<CXXMethodDecl>(D)->isInstance();
974 if (isa<FunctionTemplateDecl>(D))
975 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
976 ->getTemplatedDecl())->isInstance();
977 return false;
978}
979
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000980//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000981// DeclaratorDecl Implementation
982//===----------------------------------------------------------------------===//
983
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000984template <typename DeclT>
985static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
986 if (decl->getNumTemplateParameterLists() > 0)
987 return decl->getTemplateParameterList(0)->getTemplateLoc();
988 else
989 return decl->getInnerLocStart();
990}
991
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000992SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000993 TypeSourceInfo *TSI = getTypeSourceInfo();
994 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000995 return SourceLocation();
996}
997
Douglas Gregor14454802011-02-25 02:25:35 +0000998void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
999 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001000 // Make sure the extended decl info is allocated.
1001 if (!hasExtInfo()) {
1002 // Save (non-extended) type source info pointer.
1003 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1004 // Allocate external info struct.
1005 DeclInfo = new (getASTContext()) ExtInfo;
1006 // Restore savedTInfo into (extended) decl info.
1007 getExtInfo()->TInfo = savedTInfo;
1008 }
1009 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001010 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001011 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001012 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001013 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001014 if (getExtInfo()->NumTemplParamLists == 0) {
1015 // Save type source info pointer.
1016 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1017 // Deallocate the extended decl info.
1018 getASTContext().Deallocate(getExtInfo());
1019 // Restore savedTInfo into (non-extended) decl info.
1020 DeclInfo = savedTInfo;
1021 }
1022 else
1023 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001024 }
1025 }
1026}
1027
Abramo Bagnara60804e12011-03-18 15:16:37 +00001028void
1029DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1030 unsigned NumTPLists,
1031 TemplateParameterList **TPLists) {
1032 assert(NumTPLists > 0);
1033 // Make sure the extended decl info is allocated.
1034 if (!hasExtInfo()) {
1035 // Save (non-extended) type source info pointer.
1036 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1037 // Allocate external info struct.
1038 DeclInfo = new (getASTContext()) ExtInfo;
1039 // Restore savedTInfo into (extended) decl info.
1040 getExtInfo()->TInfo = savedTInfo;
1041 }
1042 // Set the template parameter lists info.
1043 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1044}
1045
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001046SourceLocation DeclaratorDecl::getOuterLocStart() const {
1047 return getTemplateOrInnerLocStart(this);
1048}
1049
Abramo Bagnaraea947882011-03-08 16:41:52 +00001050namespace {
1051
1052// Helper function: returns true if QT is or contains a type
1053// having a postfix component.
1054bool typeIsPostfix(clang::QualType QT) {
1055 while (true) {
1056 const Type* T = QT.getTypePtr();
1057 switch (T->getTypeClass()) {
1058 default:
1059 return false;
1060 case Type::Pointer:
1061 QT = cast<PointerType>(T)->getPointeeType();
1062 break;
1063 case Type::BlockPointer:
1064 QT = cast<BlockPointerType>(T)->getPointeeType();
1065 break;
1066 case Type::MemberPointer:
1067 QT = cast<MemberPointerType>(T)->getPointeeType();
1068 break;
1069 case Type::LValueReference:
1070 case Type::RValueReference:
1071 QT = cast<ReferenceType>(T)->getPointeeType();
1072 break;
1073 case Type::PackExpansion:
1074 QT = cast<PackExpansionType>(T)->getPattern();
1075 break;
1076 case Type::Paren:
1077 case Type::ConstantArray:
1078 case Type::DependentSizedArray:
1079 case Type::IncompleteArray:
1080 case Type::VariableArray:
1081 case Type::FunctionProto:
1082 case Type::FunctionNoProto:
1083 return true;
1084 }
1085 }
1086}
1087
1088} // namespace
1089
1090SourceRange DeclaratorDecl::getSourceRange() const {
1091 SourceLocation RangeEnd = getLocation();
1092 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1093 if (typeIsPostfix(TInfo->getType()))
1094 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1095 }
1096 return SourceRange(getOuterLocStart(), RangeEnd);
1097}
1098
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001099void
Douglas Gregor20527e22010-06-15 17:44:38 +00001100QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1101 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001102 TemplateParameterList **TPLists) {
1103 assert((NumTPLists == 0 || TPLists != 0) &&
1104 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001105
1106 // Free previous template parameters (if any).
1107 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001108 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001109 TemplParamLists = 0;
1110 NumTemplParamLists = 0;
1111 }
1112 // Set info on matched template parameter lists (if any).
1113 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001114 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001115 NumTemplParamLists = NumTPLists;
1116 for (unsigned i = NumTPLists; i-- > 0; )
1117 TemplParamLists[i] = TPLists[i];
1118 }
1119}
1120
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001121//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001122// VarDecl Implementation
1123//===----------------------------------------------------------------------===//
1124
Sebastian Redl833ef452010-01-26 22:01:41 +00001125const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1126 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001127 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001128 case SC_Auto: return "auto";
1129 case SC_Extern: return "extern";
1130 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1131 case SC_PrivateExtern: return "__private_extern__";
1132 case SC_Register: return "register";
1133 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001134 }
1135
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001136 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001137}
1138
Abramo Bagnaradff19302011-03-08 08:55:46 +00001139VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1140 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001141 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001142 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001143 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001144}
1145
Douglas Gregor72172e92012-01-05 21:55:30 +00001146VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1147 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1148 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1149 QualType(), 0, SC_None, SC_None);
1150}
1151
Douglas Gregorbf62d642010-12-06 18:36:25 +00001152void VarDecl::setStorageClass(StorageClass SC) {
1153 assert(isLegalForVariable(SC));
1154 if (getStorageClass() != SC)
1155 ClearLinkageCache();
1156
John McCallbeaa11c2011-05-01 02:13:58 +00001157 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001158}
1159
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001160SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001161 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001162 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001163 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001164}
1165
Sebastian Redl833ef452010-01-26 22:01:41 +00001166bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001167 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001168 return false;
1169
Eli Friedman839192f2012-01-15 01:23:58 +00001170 const DeclContext *DC = getDeclContext();
1171 if (DC->isRecord())
1172 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001173
Eli Friedman839192f2012-01-15 01:23:58 +00001174 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001175 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001176 return true;
1177 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001178}
1179
1180VarDecl *VarDecl::getCanonicalDecl() {
1181 return getFirstDeclaration();
1182}
1183
Daniel Dunbar9d355812012-03-09 01:51:51 +00001184VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1185 ASTContext &C) const
1186{
Sebastian Redl35351a92010-01-31 22:27:38 +00001187 // C++ [basic.def]p2:
1188 // A declaration is a definition unless [...] it contains the 'extern'
1189 // specifier or a linkage-specification and neither an initializer [...],
1190 // it declares a static data member in a class declaration [...].
1191 // C++ [temp.expl.spec]p15:
1192 // An explicit specialization of a static data member of a template is a
1193 // definition if the declaration includes an initializer; otherwise, it is
1194 // a declaration.
1195 if (isStaticDataMember()) {
1196 if (isOutOfLine() && (hasInit() ||
1197 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1198 return Definition;
1199 else
1200 return DeclarationOnly;
1201 }
1202 // C99 6.7p5:
1203 // A definition of an identifier is a declaration for that identifier that
1204 // [...] causes storage to be reserved for that object.
1205 // Note: that applies for all non-file-scope objects.
1206 // C99 6.9.2p1:
1207 // If the declaration of an identifier for an object has file scope and an
1208 // initializer, the declaration is an external definition for the identifier
1209 if (hasInit())
1210 return Definition;
1211 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1212 if (hasExternalStorage())
1213 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001214
John McCall8e7d6562010-08-26 03:08:43 +00001215 if (getStorageClassAsWritten() == SC_Extern ||
1216 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001217 for (const VarDecl *PrevVar = getPreviousDecl();
1218 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001219 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1220 return DeclarationOnly;
1221 }
1222 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001223 // C99 6.9.2p2:
1224 // A declaration of an object that has file scope without an initializer,
1225 // and without a storage class specifier or the scs 'static', constitutes
1226 // a tentative definition.
1227 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001228 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001229 return TentativeDefinition;
1230
1231 // What's left is (in C, block-scope) declarations without initializers or
1232 // external storage. These are definitions.
1233 return Definition;
1234}
1235
Sebastian Redl35351a92010-01-31 22:27:38 +00001236VarDecl *VarDecl::getActingDefinition() {
1237 DefinitionKind Kind = isThisDeclarationADefinition();
1238 if (Kind != TentativeDefinition)
1239 return 0;
1240
Chris Lattner48eb14d2010-06-14 18:31:46 +00001241 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001242 VarDecl *First = getFirstDeclaration();
1243 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1244 I != E; ++I) {
1245 Kind = (*I)->isThisDeclarationADefinition();
1246 if (Kind == Definition)
1247 return 0;
1248 else if (Kind == TentativeDefinition)
1249 LastTentative = *I;
1250 }
1251 return LastTentative;
1252}
1253
1254bool VarDecl::isTentativeDefinitionNow() const {
1255 DefinitionKind Kind = isThisDeclarationADefinition();
1256 if (Kind != TentativeDefinition)
1257 return false;
1258
1259 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1260 if ((*I)->isThisDeclarationADefinition() == Definition)
1261 return false;
1262 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001263 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001264}
1265
Daniel Dunbar9d355812012-03-09 01:51:51 +00001266VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001267 VarDecl *First = getFirstDeclaration();
1268 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1269 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001270 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001271 return *I;
1272 }
1273 return 0;
1274}
1275
Daniel Dunbar9d355812012-03-09 01:51:51 +00001276VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001277 DefinitionKind Kind = DeclarationOnly;
1278
1279 const VarDecl *First = getFirstDeclaration();
1280 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001281 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001282 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001283 if (Kind == Definition)
1284 break;
1285 }
John McCall37bb6c92010-10-29 22:22:43 +00001286
1287 return Kind;
1288}
1289
Sebastian Redl5ca79842010-02-01 20:16:42 +00001290const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001291 redecl_iterator I = redecls_begin(), E = redecls_end();
1292 while (I != E && !I->getInit())
1293 ++I;
1294
1295 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001296 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001297 return I->getInit();
1298 }
1299 return 0;
1300}
1301
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001302bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001303 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001304 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001305
1306 if (!isStaticDataMember())
1307 return false;
1308
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001309 // If this static data member was instantiated from a static data member of
1310 // a class template, check whether that static data member was defined
1311 // out-of-line.
1312 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1313 return VD->isOutOfLine();
1314
1315 return false;
1316}
1317
Douglas Gregor1d957a32009-10-27 18:42:08 +00001318VarDecl *VarDecl::getOutOfLineDefinition() {
1319 if (!isStaticDataMember())
1320 return 0;
1321
1322 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1323 RD != RDEnd; ++RD) {
1324 if (RD->getLexicalDeclContext()->isFileContext())
1325 return *RD;
1326 }
1327
1328 return 0;
1329}
1330
Douglas Gregord5058122010-02-11 01:19:42 +00001331void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001332 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1333 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001334 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001335 }
1336
1337 Init = I;
1338}
1339
Daniel Dunbar9d355812012-03-09 01:51:51 +00001340bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001341 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001342
Richard Smith35ecb362012-03-02 04:14:40 +00001343 if (!Lang.CPlusPlus)
1344 return false;
1345
1346 // In C++11, any variable of reference type can be used in a constant
1347 // expression if it is initialized by a constant expression.
1348 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1349 return true;
1350
1351 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001352 // not require the variable to be non-volatile, but we consider this to be a
1353 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001354 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001355 return false;
1356
1357 // In C++, const, non-volatile variables of integral or enumeration types
1358 // can be used in constant expressions.
1359 if (getType()->isIntegralOrEnumerationType())
1360 return true;
1361
Richard Smith35ecb362012-03-02 04:14:40 +00001362 // Additionally, in C++11, non-volatile constexpr variables can be used in
1363 // constant expressions.
1364 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001365}
1366
Richard Smithd0b4dd62011-12-19 06:19:21 +00001367/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1368/// form, which contains extra information on the evaluated value of the
1369/// initializer.
1370EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1371 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1372 if (!Eval) {
1373 Stmt *S = Init.get<Stmt *>();
1374 Eval = new (getASTContext()) EvaluatedStmt;
1375 Eval->Value = S;
1376 Init = Eval;
1377 }
1378 return Eval;
1379}
1380
Richard Smithdafff942012-01-14 04:30:29 +00001381APValue *VarDecl::evaluateValue() const {
1382 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1383 return evaluateValue(Notes);
1384}
1385
1386APValue *VarDecl::evaluateValue(
1387 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001388 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1389
1390 // We only produce notes indicating why an initializer is non-constant the
1391 // first time it is evaluated. FIXME: The notes won't always be emitted the
1392 // first time we try evaluation, so might not be produced at all.
1393 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001394 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001395
1396 const Expr *Init = cast<Expr>(Eval->Value);
1397 assert(!Init->isValueDependent());
1398
1399 if (Eval->IsEvaluating) {
1400 // FIXME: Produce a diagnostic for self-initialization.
1401 Eval->CheckedICE = true;
1402 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001403 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001404 }
1405
1406 Eval->IsEvaluating = true;
1407
1408 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1409 this, Notes);
1410
1411 // Ensure the result is an uninitialized APValue if evaluation fails.
1412 if (!Result)
1413 Eval->Evaluated = APValue();
1414
1415 Eval->IsEvaluating = false;
1416 Eval->WasEvaluated = true;
1417
1418 // In C++11, we have determined whether the initializer was a constant
1419 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001420 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001421 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001422 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001423 }
1424
Richard Smithdafff942012-01-14 04:30:29 +00001425 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001426}
1427
1428bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001429 // Initializers of weak variables are never ICEs.
1430 if (isWeak())
1431 return false;
1432
Richard Smithd0b4dd62011-12-19 06:19:21 +00001433 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1434 if (Eval->CheckedICE)
1435 // We have already checked whether this subexpression is an
1436 // integral constant expression.
1437 return Eval->IsICE;
1438
1439 const Expr *Init = cast<Expr>(Eval->Value);
1440 assert(!Init->isValueDependent());
1441
1442 // In C++11, evaluate the initializer to check whether it's a constant
1443 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001444 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001445 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1446 evaluateValue(Notes);
1447 return Eval->IsICE;
1448 }
1449
1450 // It's an ICE whether or not the definition we found is
1451 // out-of-line. See DR 721 and the discussion in Clang PR
1452 // 6206 for details.
1453
1454 if (Eval->CheckingICE)
1455 return false;
1456 Eval->CheckingICE = true;
1457
1458 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1459 Eval->CheckingICE = false;
1460 Eval->CheckedICE = true;
1461 return Eval->IsICE;
1462}
1463
Douglas Gregorfe314812011-06-21 17:03:29 +00001464bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001465 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001466
1467 const Expr *E = getInit();
1468 if (!E)
1469 return false;
1470
1471 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1472 E = Cleanups->getSubExpr();
1473
1474 return isa<MaterializeTemporaryExpr>(E);
1475}
1476
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001477VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001478 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001479 return cast<VarDecl>(MSI->getInstantiatedFrom());
1480
1481 return 0;
1482}
1483
Douglas Gregor3c74d412009-10-14 20:14:33 +00001484TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001485 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001486 return MSI->getTemplateSpecializationKind();
1487
1488 return TSK_Undeclared;
1489}
1490
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001491MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001492 return getASTContext().getInstantiatedFromStaticDataMember(this);
1493}
1494
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001495void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1496 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001497 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001498 assert(MSI && "Not an instantiated static data member?");
1499 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001500 if (TSK != TSK_ExplicitSpecialization &&
1501 PointOfInstantiation.isValid() &&
1502 MSI->getPointOfInstantiation().isInvalid())
1503 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001504}
1505
Sebastian Redl833ef452010-01-26 22:01:41 +00001506//===----------------------------------------------------------------------===//
1507// ParmVarDecl Implementation
1508//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001509
Sebastian Redl833ef452010-01-26 22:01:41 +00001510ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001511 SourceLocation StartLoc,
1512 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001513 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001514 StorageClass S, StorageClass SCAsWritten,
1515 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001516 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001517 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001518}
1519
Douglas Gregor72172e92012-01-05 21:55:30 +00001520ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1521 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1522 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1523 0, QualType(), 0, SC_None, SC_None, 0);
1524}
1525
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001526SourceRange ParmVarDecl::getSourceRange() const {
1527 if (!hasInheritedDefaultArg()) {
1528 SourceRange ArgRange = getDefaultArgRange();
1529 if (ArgRange.isValid())
1530 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1531 }
1532
1533 return DeclaratorDecl::getSourceRange();
1534}
1535
Sebastian Redl833ef452010-01-26 22:01:41 +00001536Expr *ParmVarDecl::getDefaultArg() {
1537 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1538 assert(!hasUninstantiatedDefaultArg() &&
1539 "Default argument is not yet instantiated!");
1540
1541 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001542 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001543 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001544
Sebastian Redl833ef452010-01-26 22:01:41 +00001545 return Arg;
1546}
1547
Sebastian Redl833ef452010-01-26 22:01:41 +00001548SourceRange ParmVarDecl::getDefaultArgRange() const {
1549 if (const Expr *E = getInit())
1550 return E->getSourceRange();
1551
1552 if (hasUninstantiatedDefaultArg())
1553 return getUninstantiatedDefaultArg()->getSourceRange();
1554
1555 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001556}
1557
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001558bool ParmVarDecl::isParameterPack() const {
1559 return isa<PackExpansionType>(getType());
1560}
1561
Ted Kremenek540017e2011-10-06 05:00:56 +00001562void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1563 getASTContext().setParameterIndex(this, parameterIndex);
1564 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1565}
1566
1567unsigned ParmVarDecl::getParameterIndexLarge() const {
1568 return getASTContext().getParameterIndex(this);
1569}
1570
Nuno Lopes394ec982008-12-17 23:39:55 +00001571//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001572// FunctionDecl Implementation
1573//===----------------------------------------------------------------------===//
1574
Douglas Gregorb11aad82011-02-19 18:51:44 +00001575void FunctionDecl::getNameForDiagnostic(std::string &S,
1576 const PrintingPolicy &Policy,
1577 bool Qualified) const {
1578 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1579 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1580 if (TemplateArgs)
1581 S += TemplateSpecializationType::PrintTemplateArgumentList(
1582 TemplateArgs->data(),
1583 TemplateArgs->size(),
1584 Policy);
1585
1586}
1587
Ted Kremenek186a0742010-04-29 16:49:01 +00001588bool FunctionDecl::isVariadic() const {
1589 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1590 return FT->isVariadic();
1591 return false;
1592}
1593
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001594bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1595 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001596 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001597 Definition = *I;
1598 return true;
1599 }
1600 }
1601
1602 return false;
1603}
1604
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001605bool FunctionDecl::hasTrivialBody() const
1606{
1607 Stmt *S = getBody();
1608 if (!S) {
1609 // Since we don't have a body for this function, we don't know if it's
1610 // trivial or not.
1611 return false;
1612 }
1613
1614 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1615 return true;
1616 return false;
1617}
1618
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001619bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1620 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001621 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001622 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1623 return true;
1624 }
1625 }
1626
1627 return false;
1628}
1629
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001630Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001631 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1632 if (I->Body) {
1633 Definition = *I;
1634 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001635 } else if (I->IsLateTemplateParsed) {
1636 Definition = *I;
1637 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001638 }
1639 }
1640
1641 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001642}
1643
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001644void FunctionDecl::setBody(Stmt *B) {
1645 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001646 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001647 EndRangeLoc = B->getLocEnd();
1648}
1649
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001650void FunctionDecl::setPure(bool P) {
1651 IsPure = P;
1652 if (P)
1653 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1654 Parent->markedVirtualFunctionPure();
1655}
1656
Douglas Gregor16618f22009-09-12 00:17:51 +00001657bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001658 const TranslationUnitDecl *tunit =
1659 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1660 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001661 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001662 getIdentifier() &&
1663 getIdentifier()->isStr("main");
1664}
1665
1666bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1667 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1668 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1669 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1670 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1671 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1672
1673 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1674 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1675
1676 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1677 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1678
1679 ASTContext &Context =
1680 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1681 ->getASTContext();
1682
1683 // The result type and first argument type are constant across all
1684 // these operators. The second argument must be exactly void*.
1685 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001686}
1687
Douglas Gregor16618f22009-09-12 00:17:51 +00001688bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001689 if (getLinkage() != ExternalLinkage)
1690 return false;
1691
1692 if (getAttr<OverloadableAttr>())
1693 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001694
Chandler Carruth4322a282011-02-25 00:05:02 +00001695 const DeclContext *DC = getDeclContext();
1696 if (DC->isRecord())
1697 return false;
1698
Eli Friedman839192f2012-01-15 01:23:58 +00001699 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001700 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001701 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001702
Eli Friedman839192f2012-01-15 01:23:58 +00001703 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001704}
1705
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001706bool FunctionDecl::isGlobal() const {
1707 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1708 return Method->isStatic();
1709
John McCall8e7d6562010-08-26 03:08:43 +00001710 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001711 return false;
1712
Mike Stump11289f42009-09-09 15:08:12 +00001713 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001714 DC->isNamespace();
1715 DC = DC->getParent()) {
1716 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1717 if (!Namespace->getDeclName())
1718 return false;
1719 break;
1720 }
1721 }
1722
1723 return true;
1724}
1725
Sebastian Redl833ef452010-01-26 22:01:41 +00001726void
1727FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1728 redeclarable_base::setPreviousDeclaration(PrevDecl);
1729
1730 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1731 FunctionTemplateDecl *PrevFunTmpl
1732 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1733 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1734 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1735 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001736
Axel Naumannfbc7b982011-11-08 18:21:06 +00001737 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001738 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001739}
1740
1741const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1742 return getFirstDeclaration();
1743}
1744
1745FunctionDecl *FunctionDecl::getCanonicalDecl() {
1746 return getFirstDeclaration();
1747}
1748
Douglas Gregorbf62d642010-12-06 18:36:25 +00001749void FunctionDecl::setStorageClass(StorageClass SC) {
1750 assert(isLegalForFunction(SC));
1751 if (getStorageClass() != SC)
1752 ClearLinkageCache();
1753
1754 SClass = SC;
1755}
1756
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001757/// \brief Returns a value indicating whether this function
1758/// corresponds to a builtin function.
1759///
1760/// The function corresponds to a built-in function if it is
1761/// declared at translation scope or within an extern "C" block and
1762/// its name matches with the name of a builtin. The returned value
1763/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001764/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001765/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001766unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001767 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001768 return 0;
1769
1770 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001771 if (!BuiltinID)
1772 return 0;
1773
1774 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001775 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1776 return BuiltinID;
1777
1778 // This function has the name of a known C library
1779 // function. Determine whether it actually refers to the C library
1780 // function or whether it just has the same name.
1781
Douglas Gregora908e7f2009-02-17 03:23:10 +00001782 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001783 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001784 return 0;
1785
Douglas Gregore711f702009-02-14 18:57:46 +00001786 // If this function is at translation-unit scope and we're not in
1787 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001788 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001789 getDeclContext()->isTranslationUnit())
1790 return BuiltinID;
1791
1792 // If the function is in an extern "C" linkage specification and is
1793 // not marked "overloadable", it's the real function.
1794 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001795 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001796 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001797 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001798 return BuiltinID;
1799
1800 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001801 return 0;
1802}
1803
1804
Chris Lattner47c0d002009-04-25 06:03:53 +00001805/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001806/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001807/// after it has been created.
1808unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001809 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001810 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001811 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001812 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001813
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001814}
1815
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001816void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001817 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001818 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001819 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001820
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001821 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001822 if (!NewParamInfo.empty()) {
1823 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1824 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001825 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001826}
Chris Lattner41943152007-01-25 04:52:46 +00001827
James Molloy6f8780b2012-02-29 10:24:19 +00001828void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1829 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1830
1831 if (!NewDecls.empty()) {
1832 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1833 std::copy(NewDecls.begin(), NewDecls.end(), A);
1834 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1835 }
1836}
1837
Chris Lattner58258242008-04-10 02:22:51 +00001838/// getMinRequiredArguments - Returns the minimum number of arguments
1839/// needed to call this function. This may be fewer than the number of
1840/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001841/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001842unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001843 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001844 return getNumParams();
1845
Douglas Gregor7825bf32011-01-06 22:09:01 +00001846 unsigned NumRequiredArgs = getNumParams();
1847
1848 // If the last parameter is a parameter pack, we don't need an argument for
1849 // it.
1850 if (NumRequiredArgs > 0 &&
1851 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1852 --NumRequiredArgs;
1853
1854 // If this parameter has a default argument, we don't need an argument for
1855 // it.
1856 while (NumRequiredArgs > 0 &&
1857 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001858 --NumRequiredArgs;
1859
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001860 // We might have parameter packs before the end. These can't be deduced,
1861 // but they can still handle multiple arguments.
1862 unsigned ArgIdx = NumRequiredArgs;
1863 while (ArgIdx > 0) {
1864 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1865 NumRequiredArgs = ArgIdx;
1866
1867 --ArgIdx;
1868 }
1869
Chris Lattner58258242008-04-10 02:22:51 +00001870 return NumRequiredArgs;
1871}
1872
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001873bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001874 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001875 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001876
1877 if (isa<CXXMethodDecl>(this)) {
1878 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1879 return true;
1880 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001881
1882 switch (getTemplateSpecializationKind()) {
1883 case TSK_Undeclared:
1884 case TSK_ExplicitSpecialization:
1885 return false;
1886
1887 case TSK_ImplicitInstantiation:
1888 case TSK_ExplicitInstantiationDeclaration:
1889 case TSK_ExplicitInstantiationDefinition:
1890 // Handle below.
1891 break;
1892 }
1893
1894 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001895 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001896 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001897 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001898
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001899 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001900 return PatternDecl->isInlined();
1901
1902 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001903}
1904
Eli Friedman1b125c32012-02-07 03:50:18 +00001905static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1906 // Only consider file-scope declarations in this test.
1907 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1908 return false;
1909
1910 // Only consider explicit declarations; the presence of a builtin for a
1911 // libcall shouldn't affect whether a definition is externally visible.
1912 if (Redecl->isImplicit())
1913 return false;
1914
1915 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1916 return true; // Not an inline definition
1917
1918 return false;
1919}
1920
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001921/// \brief For a function declaration in C or C++, determine whether this
1922/// declaration causes the definition to be externally visible.
1923///
Eli Friedman1b125c32012-02-07 03:50:18 +00001924/// Specifically, this determines if adding the current declaration to the set
1925/// of redeclarations of the given functions causes
1926/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001927bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1928 assert(!doesThisDeclarationHaveABody() &&
1929 "Must have a declaration without a body.");
1930
1931 ASTContext &Context = getASTContext();
1932
David Blaikiebbafb8a2012-03-11 07:00:24 +00001933 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001934 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1935 // an externally visible definition.
1936 //
1937 // FIXME: What happens if gnu_inline gets added on after the first
1938 // declaration?
1939 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1940 return false;
1941
1942 const FunctionDecl *Prev = this;
1943 bool FoundBody = false;
1944 while ((Prev = Prev->getPreviousDecl())) {
1945 FoundBody |= Prev->Body;
1946
1947 if (Prev->Body) {
1948 // If it's not the case that both 'inline' and 'extern' are
1949 // specified on the definition, then it is always externally visible.
1950 if (!Prev->isInlineSpecified() ||
1951 Prev->getStorageClassAsWritten() != SC_Extern)
1952 return false;
1953 } else if (Prev->isInlineSpecified() &&
1954 Prev->getStorageClassAsWritten() != SC_Extern) {
1955 return false;
1956 }
1957 }
1958 return FoundBody;
1959 }
1960
David Blaikiebbafb8a2012-03-11 07:00:24 +00001961 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001962 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001963
1964 // C99 6.7.4p6:
1965 // [...] If all of the file scope declarations for a function in a
1966 // translation unit include the inline function specifier without extern,
1967 // then the definition in that translation unit is an inline definition.
1968 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001969 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001970 const FunctionDecl *Prev = this;
1971 bool FoundBody = false;
1972 while ((Prev = Prev->getPreviousDecl())) {
1973 FoundBody |= Prev->Body;
1974 if (RedeclForcesDefC99(Prev))
1975 return false;
1976 }
1977 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001978}
1979
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001980/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001981/// definition will be externally visible.
1982///
1983/// Inline function definitions are always available for inlining optimizations.
1984/// However, depending on the language dialect, declaration specifiers, and
1985/// attributes, the definition of an inline function may or may not be
1986/// "externally" visible to other translation units in the program.
1987///
1988/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001989/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001990/// inline definition becomes externally visible (C99 6.7.4p6).
1991///
1992/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1993/// definition, we use the GNU semantics for inline, which are nearly the
1994/// opposite of C99 semantics. In particular, "inline" by itself will create
1995/// an externally visible symbol, but "extern inline" will not create an
1996/// externally visible symbol.
1997bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001998 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001999 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002000 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002001
David Blaikiebbafb8a2012-03-11 07:00:24 +00002002 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002003 // Note: If you change the logic here, please change
2004 // doesDeclarationForceExternallyVisibleDefinition as well.
2005 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002006 // If it's not the case that both 'inline' and 'extern' are
2007 // specified on the definition, then this inline definition is
2008 // externally visible.
2009 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2010 return true;
2011
2012 // If any declaration is 'inline' but not 'extern', then this definition
2013 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002014 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2015 Redecl != RedeclEnd;
2016 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002017 if (Redecl->isInlineSpecified() &&
2018 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002019 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002020 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002021
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002022 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002023 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002024
Douglas Gregor299d76e2009-09-13 07:46:26 +00002025 // C99 6.7.4p6:
2026 // [...] If all of the file scope declarations for a function in a
2027 // translation unit include the inline function specifier without extern,
2028 // then the definition in that translation unit is an inline definition.
2029 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2030 Redecl != RedeclEnd;
2031 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002032 if (RedeclForcesDefC99(*Redecl))
2033 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002034 }
2035
2036 // C99 6.7.4p6:
2037 // An inline definition does not provide an external definition for the
2038 // function, and does not forbid an external definition in another
2039 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002040 return false;
2041}
2042
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002043/// getOverloadedOperator - Which C++ overloaded operator this
2044/// function represents, if any.
2045OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002046 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2047 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002048 else
2049 return OO_None;
2050}
2051
Alexis Huntc88db062010-01-13 09:01:02 +00002052/// getLiteralIdentifier - The literal suffix identifier this function
2053/// represents, if any.
2054const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2055 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2056 return getDeclName().getCXXLiteralIdentifier();
2057 else
2058 return 0;
2059}
2060
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002061FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2062 if (TemplateOrSpecialization.isNull())
2063 return TK_NonTemplate;
2064 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2065 return TK_FunctionTemplate;
2066 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2067 return TK_MemberSpecialization;
2068 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2069 return TK_FunctionTemplateSpecialization;
2070 if (TemplateOrSpecialization.is
2071 <DependentFunctionTemplateSpecializationInfo*>())
2072 return TK_DependentFunctionTemplateSpecialization;
2073
David Blaikie83d382b2011-09-23 05:06:16 +00002074 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002075}
2076
Douglas Gregord801b062009-10-07 23:56:10 +00002077FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002078 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002079 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2080
2081 return 0;
2082}
2083
Douglas Gregor06db9f52009-10-12 20:18:28 +00002084MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2085 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2086}
2087
Douglas Gregord801b062009-10-07 23:56:10 +00002088void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002089FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2090 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002091 TemplateSpecializationKind TSK) {
2092 assert(TemplateOrSpecialization.isNull() &&
2093 "Member function is already a specialization");
2094 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002095 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002096 TemplateOrSpecialization = Info;
2097}
2098
Douglas Gregorafca3b42009-10-27 20:53:28 +00002099bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002100 // If the function is invalid, it can't be implicitly instantiated.
2101 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002102 return false;
2103
2104 switch (getTemplateSpecializationKind()) {
2105 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002106 case TSK_ExplicitInstantiationDefinition:
2107 return false;
2108
2109 case TSK_ImplicitInstantiation:
2110 return true;
2111
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002112 // It is possible to instantiate TSK_ExplicitSpecialization kind
2113 // if the FunctionDecl has a class scope specialization pattern.
2114 case TSK_ExplicitSpecialization:
2115 return getClassScopeSpecializationPattern() != 0;
2116
Douglas Gregorafca3b42009-10-27 20:53:28 +00002117 case TSK_ExplicitInstantiationDeclaration:
2118 // Handled below.
2119 break;
2120 }
2121
2122 // Find the actual template from which we will instantiate.
2123 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002124 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002125 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002126 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002127
2128 // C++0x [temp.explicit]p9:
2129 // Except for inline functions, other explicit instantiation declarations
2130 // have the effect of suppressing the implicit instantiation of the entity
2131 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002132 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002133 return true;
2134
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002135 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002136}
2137
2138bool FunctionDecl::isTemplateInstantiation() const {
2139 switch (getTemplateSpecializationKind()) {
2140 case TSK_Undeclared:
2141 case TSK_ExplicitSpecialization:
2142 return false;
2143 case TSK_ImplicitInstantiation:
2144 case TSK_ExplicitInstantiationDeclaration:
2145 case TSK_ExplicitInstantiationDefinition:
2146 return true;
2147 }
2148 llvm_unreachable("All TSK values handled.");
2149}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002150
2151FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002152 // Handle class scope explicit specialization special case.
2153 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2154 return getClassScopeSpecializationPattern();
2155
Douglas Gregorafca3b42009-10-27 20:53:28 +00002156 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2157 while (Primary->getInstantiatedFromMemberTemplate()) {
2158 // If we have hit a point where the user provided a specialization of
2159 // this template, we're done looking.
2160 if (Primary->isMemberSpecialization())
2161 break;
2162
2163 Primary = Primary->getInstantiatedFromMemberTemplate();
2164 }
2165
2166 return Primary->getTemplatedDecl();
2167 }
2168
2169 return getInstantiatedFromMemberFunction();
2170}
2171
Douglas Gregor70d83e22009-06-29 17:30:29 +00002172FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002173 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002174 = TemplateOrSpecialization
2175 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002176 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002177 }
2178 return 0;
2179}
2180
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002181FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2182 return getASTContext().getClassScopeSpecializationPattern(this);
2183}
2184
Douglas Gregor70d83e22009-06-29 17:30:29 +00002185const TemplateArgumentList *
2186FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002187 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002188 = TemplateOrSpecialization
2189 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002190 return Info->TemplateArguments;
2191 }
2192 return 0;
2193}
2194
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002195const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002196FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2197 if (FunctionTemplateSpecializationInfo *Info
2198 = TemplateOrSpecialization
2199 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2200 return Info->TemplateArgumentsAsWritten;
2201 }
2202 return 0;
2203}
2204
Mike Stump11289f42009-09-09 15:08:12 +00002205void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002206FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2207 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002208 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002209 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002210 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002211 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2212 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002213 assert(TSK != TSK_Undeclared &&
2214 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002215 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002216 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002217 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002218 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2219 TemplateArgs,
2220 TemplateArgsAsWritten,
2221 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002222 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002223 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002224}
2225
John McCallb9c78482010-04-08 09:05:18 +00002226void
2227FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2228 const UnresolvedSetImpl &Templates,
2229 const TemplateArgumentListInfo &TemplateArgs) {
2230 assert(TemplateOrSpecialization.isNull());
2231 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2232 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002233 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002234 void *Buffer = Context.Allocate(Size);
2235 DependentFunctionTemplateSpecializationInfo *Info =
2236 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2237 TemplateArgs);
2238 TemplateOrSpecialization = Info;
2239}
2240
2241DependentFunctionTemplateSpecializationInfo::
2242DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2243 const TemplateArgumentListInfo &TArgs)
2244 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2245
2246 d.NumTemplates = Ts.size();
2247 d.NumArgs = TArgs.size();
2248
2249 FunctionTemplateDecl **TsArray =
2250 const_cast<FunctionTemplateDecl**>(getTemplates());
2251 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2252 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2253
2254 TemplateArgumentLoc *ArgsArray =
2255 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2256 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2257 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2258}
2259
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002260TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002261 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002262 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002263 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002264 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002265 if (FTSInfo)
2266 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002267
Douglas Gregord801b062009-10-07 23:56:10 +00002268 MemberSpecializationInfo *MSInfo
2269 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2270 if (MSInfo)
2271 return MSInfo->getTemplateSpecializationKind();
2272
2273 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002274}
2275
Mike Stump11289f42009-09-09 15:08:12 +00002276void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002277FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2278 SourceLocation PointOfInstantiation) {
2279 if (FunctionTemplateSpecializationInfo *FTSInfo
2280 = TemplateOrSpecialization.dyn_cast<
2281 FunctionTemplateSpecializationInfo*>()) {
2282 FTSInfo->setTemplateSpecializationKind(TSK);
2283 if (TSK != TSK_ExplicitSpecialization &&
2284 PointOfInstantiation.isValid() &&
2285 FTSInfo->getPointOfInstantiation().isInvalid())
2286 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2287 } else if (MemberSpecializationInfo *MSInfo
2288 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2289 MSInfo->setTemplateSpecializationKind(TSK);
2290 if (TSK != TSK_ExplicitSpecialization &&
2291 PointOfInstantiation.isValid() &&
2292 MSInfo->getPointOfInstantiation().isInvalid())
2293 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2294 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002295 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002296}
2297
2298SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002299 if (FunctionTemplateSpecializationInfo *FTSInfo
2300 = TemplateOrSpecialization.dyn_cast<
2301 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002302 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002303 else if (MemberSpecializationInfo *MSInfo
2304 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002305 return MSInfo->getPointOfInstantiation();
2306
2307 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002308}
2309
Douglas Gregor6411b922009-09-11 20:15:17 +00002310bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002311 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002312 return true;
2313
2314 // If this function was instantiated from a member function of a
2315 // class template, check whether that member function was defined out-of-line.
2316 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2317 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002318 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002319 return Definition->isOutOfLine();
2320 }
2321
2322 // If this function was instantiated from a function template,
2323 // check whether that function template was defined out-of-line.
2324 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2325 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002326 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002327 return Definition->isOutOfLine();
2328 }
2329
2330 return false;
2331}
2332
Abramo Bagnaraea947882011-03-08 16:41:52 +00002333SourceRange FunctionDecl::getSourceRange() const {
2334 return SourceRange(getOuterLocStart(), EndRangeLoc);
2335}
2336
Anna Zaks28db7ce2012-01-18 02:45:01 +00002337unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002338 IdentifierInfo *FnInfo = getIdentifier();
2339
2340 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002341 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002342
2343 // Builtin handling.
2344 switch (getBuiltinID()) {
2345 case Builtin::BI__builtin_memset:
2346 case Builtin::BI__builtin___memset_chk:
2347 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002348 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002349
2350 case Builtin::BI__builtin_memcpy:
2351 case Builtin::BI__builtin___memcpy_chk:
2352 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002353 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002354
2355 case Builtin::BI__builtin_memmove:
2356 case Builtin::BI__builtin___memmove_chk:
2357 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002358 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002359
2360 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002361 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002362 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002363 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002364
2365 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002366 case Builtin::BImemcmp:
2367 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002368
2369 case Builtin::BI__builtin_strncpy:
2370 case Builtin::BI__builtin___strncpy_chk:
2371 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002372 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002373
2374 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002375 case Builtin::BIstrncmp:
2376 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002377
2378 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002379 case Builtin::BIstrncasecmp:
2380 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002381
2382 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002383 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002384 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002385 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002386
2387 case Builtin::BI__builtin_strndup:
2388 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002389 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002390
Anna Zaks314cd092012-02-01 19:08:57 +00002391 case Builtin::BI__builtin_strlen:
2392 case Builtin::BIstrlen:
2393 return Builtin::BIstrlen;
2394
Anna Zaks201d4892012-01-13 21:52:01 +00002395 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002396 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002397 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002398 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002399 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002400 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002401 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002402 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002403 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002404 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002405 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002406 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002407 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002408 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002409 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002410 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002411 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002412 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002413 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002414 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002415 else if (FnInfo->isStr("strlen"))
2416 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002417 }
2418 break;
2419 }
Anna Zaks22122702012-01-17 00:37:07 +00002420 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002421}
2422
Chris Lattner59a25942008-03-31 00:36:02 +00002423//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002424// FieldDecl Implementation
2425//===----------------------------------------------------------------------===//
2426
Jay Foad39c79802011-01-12 09:06:06 +00002427FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002428 SourceLocation StartLoc, SourceLocation IdLoc,
2429 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002430 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2431 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002432 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002433 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002434}
2435
Douglas Gregor72172e92012-01-05 21:55:30 +00002436FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2437 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2438 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2439 0, QualType(), 0, 0, false, false);
2440}
2441
Sebastian Redl833ef452010-01-26 22:01:41 +00002442bool FieldDecl::isAnonymousStructOrUnion() const {
2443 if (!isImplicit() || getDeclName())
2444 return false;
2445
2446 if (const RecordType *Record = getType()->getAs<RecordType>())
2447 return Record->getDecl()->isAnonymousStructOrUnion();
2448
2449 return false;
2450}
2451
Richard Smithcaf33902011-10-10 18:28:20 +00002452unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2453 assert(isBitField() && "not a bitfield");
2454 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2455 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2456}
2457
John McCall4e819612011-01-20 07:57:12 +00002458unsigned FieldDecl::getFieldIndex() const {
2459 if (CachedFieldIndex) return CachedFieldIndex - 1;
2460
Richard Smithd62306a2011-11-10 06:34:14 +00002461 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002462 const RecordDecl *RD = getParent();
2463 const FieldDecl *LastFD = 0;
2464 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002465
2466 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2467 I != E; ++I, ++Index) {
2468 (*I)->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002469
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002470 if (IsMsStruct) {
2471 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smithd62306a2011-11-10 06:34:14 +00002472 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2473 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002474 continue;
2475 }
Richard Smithd62306a2011-11-10 06:34:14 +00002476 LastFD = (*I);
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002477 }
John McCall4e819612011-01-20 07:57:12 +00002478 }
2479
Richard Smithd62306a2011-11-10 06:34:14 +00002480 assert(CachedFieldIndex && "failed to find field in parent");
2481 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002482}
2483
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002484SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002485 if (const Expr *E = InitializerOrBitWidth.getPointer())
2486 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002487 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002488}
2489
Richard Smith938f40b2011-06-11 17:19:42 +00002490void FieldDecl::setInClassInitializer(Expr *Init) {
2491 assert(!InitializerOrBitWidth.getPointer() &&
2492 "bit width or initializer already set");
2493 InitializerOrBitWidth.setPointer(Init);
2494 InitializerOrBitWidth.setInt(0);
2495}
2496
Sebastian Redl833ef452010-01-26 22:01:41 +00002497//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002498// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002499//===----------------------------------------------------------------------===//
2500
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002501SourceLocation TagDecl::getOuterLocStart() const {
2502 return getTemplateOrInnerLocStart(this);
2503}
2504
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002505SourceRange TagDecl::getSourceRange() const {
2506 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002507 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002508}
2509
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002510TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002511 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002512}
2513
Richard Smithdda56e42011-04-15 14:24:37 +00002514void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2515 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002516 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002517 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002518 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002519}
2520
Douglas Gregordee1be82009-01-17 00:42:38 +00002521void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002522 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002523
2524 if (isa<CXXRecordDecl>(this)) {
2525 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2526 struct CXXRecordDecl::DefinitionData *Data =
2527 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002528 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2529 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002530 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002531}
2532
2533void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002534 assert((!isa<CXXRecordDecl>(this) ||
2535 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2536 "definition completed but not started");
2537
John McCallf937c022011-10-07 06:10:15 +00002538 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002539 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002540
2541 if (ASTMutationListener *L = getASTMutationListener())
2542 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002543}
2544
John McCallf937c022011-10-07 06:10:15 +00002545TagDecl *TagDecl::getDefinition() const {
2546 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002547 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002548 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2549 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002550
2551 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002552 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002553 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002554 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002555
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002556 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002557}
2558
Douglas Gregor14454802011-02-25 02:25:35 +00002559void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2560 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002561 // Make sure the extended qualifier info is allocated.
2562 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002563 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002564 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002565 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002566 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002567 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002568 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002569 if (getExtInfo()->NumTemplParamLists == 0) {
2570 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002571 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002572 }
2573 else
2574 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002575 }
2576 }
2577}
2578
Abramo Bagnara60804e12011-03-18 15:16:37 +00002579void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2580 unsigned NumTPLists,
2581 TemplateParameterList **TPLists) {
2582 assert(NumTPLists > 0);
2583 // Make sure the extended decl info is allocated.
2584 if (!hasExtInfo())
2585 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002586 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002587 // Set the template parameter lists info.
2588 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2589}
2590
Ted Kremenek21475702008-09-05 17:16:31 +00002591//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002592// EnumDecl Implementation
2593//===----------------------------------------------------------------------===//
2594
David Blaikie68e081d2011-12-20 02:48:34 +00002595void EnumDecl::anchor() { }
2596
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002597EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2598 SourceLocation StartLoc, SourceLocation IdLoc,
2599 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002600 EnumDecl *PrevDecl, bool IsScoped,
2601 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002602 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002603 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002604 C.getTypeDeclType(Enum, PrevDecl);
2605 return Enum;
2606}
2607
Douglas Gregor72172e92012-01-05 21:55:30 +00002608EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2609 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2610 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2611 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002612}
2613
Douglas Gregord5058122010-02-11 01:19:42 +00002614void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002615 QualType NewPromotionType,
2616 unsigned NumPositiveBits,
2617 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002618 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002619 if (!IntegerType)
2620 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002621 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002622 setNumPositiveBits(NumPositiveBits);
2623 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002624 TagDecl::completeDefinition();
2625}
2626
Richard Smith7d137e32012-03-23 03:33:32 +00002627TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2628 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2629 return MSI->getTemplateSpecializationKind();
2630
2631 return TSK_Undeclared;
2632}
2633
2634void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2635 SourceLocation PointOfInstantiation) {
2636 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2637 assert(MSI && "Not an instantiated member enumeration?");
2638 MSI->setTemplateSpecializationKind(TSK);
2639 if (TSK != TSK_ExplicitSpecialization &&
2640 PointOfInstantiation.isValid() &&
2641 MSI->getPointOfInstantiation().isInvalid())
2642 MSI->setPointOfInstantiation(PointOfInstantiation);
2643}
2644
Richard Smith4b38ded2012-03-14 23:13:10 +00002645EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2646 if (SpecializationInfo)
2647 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2648
2649 return 0;
2650}
2651
2652void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2653 TemplateSpecializationKind TSK) {
2654 assert(!SpecializationInfo && "Member enum is already a specialization");
2655 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2656}
2657
Sebastian Redl833ef452010-01-26 22:01:41 +00002658//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002659// RecordDecl Implementation
2660//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002661
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002662RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2663 SourceLocation StartLoc, SourceLocation IdLoc,
2664 IdentifierInfo *Id, RecordDecl *PrevDecl)
2665 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002666 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002667 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002668 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002669 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002670 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002671}
2672
Jay Foad39c79802011-01-12 09:06:06 +00002673RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002674 SourceLocation StartLoc, SourceLocation IdLoc,
2675 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2676 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2677 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002678 C.getTypeDeclType(R, PrevDecl);
2679 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002680}
2681
Douglas Gregor72172e92012-01-05 21:55:30 +00002682RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2683 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2684 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2685 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002686}
2687
Douglas Gregordfcad112009-03-25 15:59:44 +00002688bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002689 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002690 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2691}
2692
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002693RecordDecl::field_iterator RecordDecl::field_begin() const {
2694 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2695 LoadFieldsFromExternalStorage();
2696
2697 return field_iterator(decl_iterator(FirstDecl));
2698}
2699
Douglas Gregorb11aad82011-02-19 18:51:44 +00002700/// completeDefinition - Notes that the definition of this type is now
2701/// complete.
2702void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002703 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002704 TagDecl::completeDefinition();
2705}
2706
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002707void RecordDecl::LoadFieldsFromExternalStorage() const {
2708 ExternalASTSource *Source = getASTContext().getExternalSource();
2709 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2710
2711 // Notify that we have a RecordDecl doing some initialization.
2712 ExternalASTSource::Deserializing TheFields(Source);
2713
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002714 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002715 LoadedFieldsFromExternalStorage = true;
2716 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2717 case ELR_Success:
2718 break;
2719
2720 case ELR_AlreadyLoaded:
2721 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002722 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002723 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002724
2725#ifndef NDEBUG
2726 // Check that all decls we got were FieldDecls.
2727 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2728 assert(isa<FieldDecl>(Decls[i]));
2729#endif
2730
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002731 if (Decls.empty())
2732 return;
2733
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002734 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2735 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002736}
2737
Steve Naroff415d3d52008-10-08 17:01:13 +00002738//===----------------------------------------------------------------------===//
2739// BlockDecl Implementation
2740//===----------------------------------------------------------------------===//
2741
David Blaikie9c70e042011-09-21 18:16:56 +00002742void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002743 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002744
Steve Naroffc4b30e52009-03-13 16:56:44 +00002745 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002746 if (!NewParamInfo.empty()) {
2747 NumParams = NewParamInfo.size();
2748 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2749 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002750 }
2751}
2752
John McCall351762c2011-02-07 10:33:21 +00002753void BlockDecl::setCaptures(ASTContext &Context,
2754 const Capture *begin,
2755 const Capture *end,
2756 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002757 CapturesCXXThis = capturesCXXThis;
2758
2759 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002760 NumCaptures = 0;
2761 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002762 return;
2763 }
2764
John McCall351762c2011-02-07 10:33:21 +00002765 NumCaptures = end - begin;
2766
2767 // Avoid new Capture[] because we don't want to provide a default
2768 // constructor.
2769 size_t allocationSize = NumCaptures * sizeof(Capture);
2770 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2771 memcpy(buffer, begin, allocationSize);
2772 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002773}
Sebastian Redl833ef452010-01-26 22:01:41 +00002774
John McCallce45f882011-06-15 22:51:16 +00002775bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2776 for (capture_const_iterator
2777 i = capture_begin(), e = capture_end(); i != e; ++i)
2778 // Only auto vars can be captured, so no redeclaration worries.
2779 if (i->getVariable() == variable)
2780 return true;
2781
2782 return false;
2783}
2784
Douglas Gregor70226da2010-12-21 16:27:07 +00002785SourceRange BlockDecl::getSourceRange() const {
2786 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2787}
Sebastian Redl833ef452010-01-26 22:01:41 +00002788
2789//===----------------------------------------------------------------------===//
2790// Other Decl Allocation/Deallocation Method Implementations
2791//===----------------------------------------------------------------------===//
2792
David Blaikie68e081d2011-12-20 02:48:34 +00002793void TranslationUnitDecl::anchor() { }
2794
Sebastian Redl833ef452010-01-26 22:01:41 +00002795TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2796 return new (C) TranslationUnitDecl(C);
2797}
2798
David Blaikie68e081d2011-12-20 02:48:34 +00002799void LabelDecl::anchor() { }
2800
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002801LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002802 SourceLocation IdentL, IdentifierInfo *II) {
2803 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2804}
2805
2806LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2807 SourceLocation IdentL, IdentifierInfo *II,
2808 SourceLocation GnuLabelL) {
2809 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2810 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002811}
2812
Douglas Gregor72172e92012-01-05 21:55:30 +00002813LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2814 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2815 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002816}
2817
David Blaikie68e081d2011-12-20 02:48:34 +00002818void ValueDecl::anchor() { }
2819
2820void ImplicitParamDecl::anchor() { }
2821
Sebastian Redl833ef452010-01-26 22:01:41 +00002822ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002823 SourceLocation IdLoc,
2824 IdentifierInfo *Id,
2825 QualType Type) {
2826 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002827}
2828
Douglas Gregor72172e92012-01-05 21:55:30 +00002829ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2830 unsigned ID) {
2831 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2832 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2833}
2834
Sebastian Redl833ef452010-01-26 22:01:41 +00002835FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002836 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002837 const DeclarationNameInfo &NameInfo,
2838 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002839 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002840 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002841 bool hasWrittenPrototype,
2842 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002843 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2844 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002845 isInlineSpecified,
2846 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002847 New->HasWrittenPrototype = hasWrittenPrototype;
2848 return New;
2849}
2850
Douglas Gregor72172e92012-01-05 21:55:30 +00002851FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2852 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2853 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2854 DeclarationNameInfo(), QualType(), 0,
2855 SC_None, SC_None, false, false);
2856}
2857
Sebastian Redl833ef452010-01-26 22:01:41 +00002858BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2859 return new (C) BlockDecl(DC, L);
2860}
2861
Douglas Gregor72172e92012-01-05 21:55:30 +00002862BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2863 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2864 return new (Mem) BlockDecl(0, SourceLocation());
2865}
2866
Sebastian Redl833ef452010-01-26 22:01:41 +00002867EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2868 SourceLocation L,
2869 IdentifierInfo *Id, QualType T,
2870 Expr *E, const llvm::APSInt &V) {
2871 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2872}
2873
Douglas Gregor72172e92012-01-05 21:55:30 +00002874EnumConstantDecl *
2875EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2876 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2877 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2878 llvm::APSInt());
2879}
2880
David Blaikie68e081d2011-12-20 02:48:34 +00002881void IndirectFieldDecl::anchor() { }
2882
Benjamin Kramer39593702010-11-21 14:11:41 +00002883IndirectFieldDecl *
2884IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2885 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2886 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002887 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2888}
2889
Douglas Gregor72172e92012-01-05 21:55:30 +00002890IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2891 unsigned ID) {
2892 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2893 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2894 QualType(), 0, 0);
2895}
2896
Douglas Gregorbe996932010-09-01 20:41:53 +00002897SourceRange EnumConstantDecl::getSourceRange() const {
2898 SourceLocation End = getLocation();
2899 if (Init)
2900 End = Init->getLocEnd();
2901 return SourceRange(getLocation(), End);
2902}
2903
David Blaikie68e081d2011-12-20 02:48:34 +00002904void TypeDecl::anchor() { }
2905
Sebastian Redl833ef452010-01-26 22:01:41 +00002906TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002907 SourceLocation StartLoc, SourceLocation IdLoc,
2908 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2909 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002910}
2911
David Blaikie68e081d2011-12-20 02:48:34 +00002912void TypedefNameDecl::anchor() { }
2913
Douglas Gregor72172e92012-01-05 21:55:30 +00002914TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2915 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2916 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2917}
2918
Richard Smithdda56e42011-04-15 14:24:37 +00002919TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2920 SourceLocation StartLoc,
2921 SourceLocation IdLoc, IdentifierInfo *Id,
2922 TypeSourceInfo *TInfo) {
2923 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2924}
2925
Douglas Gregor72172e92012-01-05 21:55:30 +00002926TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2927 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2928 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2929}
2930
Abramo Bagnaraea947882011-03-08 16:41:52 +00002931SourceRange TypedefDecl::getSourceRange() const {
2932 SourceLocation RangeEnd = getLocation();
2933 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2934 if (typeIsPostfix(TInfo->getType()))
2935 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2936 }
2937 return SourceRange(getLocStart(), RangeEnd);
2938}
2939
Richard Smithdda56e42011-04-15 14:24:37 +00002940SourceRange TypeAliasDecl::getSourceRange() const {
2941 SourceLocation RangeEnd = getLocStart();
2942 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2943 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2944 return SourceRange(getLocStart(), RangeEnd);
2945}
2946
David Blaikie68e081d2011-12-20 02:48:34 +00002947void FileScopeAsmDecl::anchor() { }
2948
Sebastian Redl833ef452010-01-26 22:01:41 +00002949FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002950 StringLiteral *Str,
2951 SourceLocation AsmLoc,
2952 SourceLocation RParenLoc) {
2953 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002954}
Douglas Gregorba345522011-12-02 23:23:56 +00002955
Douglas Gregor72172e92012-01-05 21:55:30 +00002956FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2957 unsigned ID) {
2958 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2959 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2960}
2961
Douglas Gregorba345522011-12-02 23:23:56 +00002962//===----------------------------------------------------------------------===//
2963// ImportDecl Implementation
2964//===----------------------------------------------------------------------===//
2965
2966/// \brief Retrieve the number of module identifiers needed to name the given
2967/// module.
2968static unsigned getNumModuleIdentifiers(Module *Mod) {
2969 unsigned Result = 1;
2970 while (Mod->Parent) {
2971 Mod = Mod->Parent;
2972 ++Result;
2973 }
2974 return Result;
2975}
2976
Douglas Gregor22d09742012-01-03 18:04:46 +00002977ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002978 Module *Imported,
2979 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00002980 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002981 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002982{
2983 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
2984 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
2985 memcpy(StoredLocs, IdentifierLocs.data(),
2986 IdentifierLocs.size() * sizeof(SourceLocation));
2987}
2988
Douglas Gregor22d09742012-01-03 18:04:46 +00002989ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002990 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00002991 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002992 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002993{
2994 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
2995}
2996
2997ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00002998 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00002999 ArrayRef<SourceLocation> IdentifierLocs) {
3000 void *Mem = C.Allocate(sizeof(ImportDecl) +
3001 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003002 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003003}
3004
3005ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003006 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003007 Module *Imported,
3008 SourceLocation EndLoc) {
3009 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003010 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003011 Import->setImplicit();
3012 return Import;
3013}
3014
Douglas Gregor72172e92012-01-05 21:55:30 +00003015ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3016 unsigned NumLocations) {
3017 void *Mem = AllocateDeserializedDecl(C, ID,
3018 (sizeof(ImportDecl) +
3019 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003020 return new (Mem) ImportDecl(EmptyShell());
3021}
3022
3023ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3024 if (!ImportedAndComplete.getInt())
3025 return ArrayRef<SourceLocation>();
3026
3027 const SourceLocation *StoredLocs
3028 = reinterpret_cast<const SourceLocation *>(this + 1);
3029 return ArrayRef<SourceLocation>(StoredLocs,
3030 getNumModuleIdentifiers(getImportedModule()));
3031}
3032
3033SourceRange ImportDecl::getSourceRange() const {
3034 if (!ImportedAndComplete.getInt())
3035 return SourceRange(getLocation(),
3036 *reinterpret_cast<const SourceLocation *>(this + 1));
3037
3038 return SourceRange(getLocation(), getIdentifierLocs().back());
3039}