blob: 15fa7902031220c5cda89293412b9c377779eb8e [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 Espindolab522a5f2012-04-23 17:51:55 +0000125 LV.mergeWithMin(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 Espindolab522a5f2012-04-23 17:51:55 +0000133 LV.mergeWithMin(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 Espindolab522a5f2012-04-23 17:51:55 +0000141 LV.mergeWithMin(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 Espindolabbc5cbc2012-04-22 15:31:59 +0000410 LV.merge(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 Espindola8add48e2012-04-22 00:43:48 +0000426 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000427 // - a namespace (7.3), unless it is declared within an unnamed
428 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000429 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
430 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000431
John McCall457a04e2010-10-22 21:05:15 +0000432 // By extension, we assign external linkage to Objective-C
433 // interfaces.
434 } else if (isa<ObjCInterfaceDecl>(D)) {
435 // fallout
436
437 // Everything not covered here has no linkage.
438 } else {
John McCallc273f242010-10-30 11:50:40 +0000439 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000440 }
441
442 // If we ended up with non-external linkage, visibility should
443 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000444 if (LV.linkage() != ExternalLinkage)
445 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000446
John McCall457a04e2010-10-22 21:05:15 +0000447 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000448}
449
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000450static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000451 // Only certain class members have linkage. Note that fields don't
452 // really have linkage, but it's convenient to say they do for the
453 // purposes of calculating linkage of pointer-to-data-member
454 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000455 if (!(isa<CXXMethodDecl>(D) ||
456 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000457 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000458 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000459 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000460 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000461
John McCall07072662010-11-02 01:45:15 +0000462 LinkageInfo LV;
463
John McCall07072662010-11-02 01:45:15 +0000464 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000465 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000466 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000467 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000468 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000469
470 // If this class member has an explicit visibility attribute, the only
471 // thing that can change its visibility is the template arguments, so
472 // only look for them when processing the the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000473 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000474
475 // If we're paying attention to global visibility, apply
476 // -finline-visibility-hidden if this is an inline method.
477 //
478 // Note that we do this before merging information about
479 // the class visibility.
480 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
481 TemplateSpecializationKind TSK = TSK_Undeclared;
482 if (FunctionTemplateSpecializationInfo *spec
483 = MD->getTemplateSpecializationInfo()) {
484 TSK = spec->getTemplateSpecializationKind();
485 } else if (MemberSpecializationInfo *MSI =
486 MD->getMemberSpecializationInfo()) {
487 TSK = MSI->getTemplateSpecializationKind();
488 }
489
490 const FunctionDecl *Def = 0;
491 // InlineVisibilityHidden only applies to definitions, and
492 // isInlined() only gives meaningful answers on definitions
493 // anyway.
494 if (TSK != TSK_ExplicitInstantiationDeclaration &&
495 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000496 !OnlyTemplate &&
Rafael Espindola505a7c82012-04-16 18:25:01 +0000497 !LV.visibilityExplicit() &&
498 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
499 MD->hasBody(Def) && Def->isInlined())
500 LV.mergeVisibility(HiddenVisibility, true);
501 }
John McCallc273f242010-10-30 11:50:40 +0000502
Rafael Espindola53cf2192012-04-19 05:50:08 +0000503 // If this member has an visibility attribute, ClassF will exclude
504 // attributes on the class or command line options, keeping only information
505 // about the template instantiation. If the member has no visibility
506 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
507 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000508 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
509 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000510 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000511 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000512
513 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000514 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000515 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000516
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000517 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000518 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000519
John McCall8823c652010-08-13 08:35:10 +0000520 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000521 // If the type of the function uses a type with unique-external
522 // linkage, it's not legally usable from outside this translation unit.
523 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
524 return LinkageInfo::uniqueExternal();
525
John McCall457a04e2010-10-22 21:05:15 +0000526 // If this is a method template specialization, use the linkage for
527 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000528 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000529 = MD->getTemplateSpecializationInfo()) {
John McCallb8c604a2011-06-27 23:06:04 +0000530 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000531 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000532 OnlyTemplate));
533 if (!OnlyTemplate)
John McCallb8c604a2011-06-27 23:06:04 +0000534 LV.merge(getLVForTemplateParameterList(
535 spec->getTemplate()->getTemplateParameters()));
536 }
John McCalle6e622e2010-11-01 01:29:57 +0000537 }
John McCall457a04e2010-10-22 21:05:15 +0000538
John McCall37bb6c92010-10-29 22:22:43 +0000539 // Note that in contrast to basically every other situation, we
540 // *do* apply -fvisibility to method declarations.
541
542 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000543 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000544 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-06-27 23:06:04 +0000545 if (shouldConsiderTemplateLV(spec)) {
546 // Merge template argument/parameter information for member
547 // class template specializations.
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000548 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000549 OnlyTemplate));
550 if (!OnlyTemplate)
John McCall8bc6d5b2011-03-04 10:39:25 +0000551 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000552 spec->getSpecializedTemplate()->getTemplateParameters()));
553 }
John McCall37bb6c92010-10-29 22:22:43 +0000554 }
555
John McCall37bb6c92010-10-29 22:22:43 +0000556 // Static data members.
557 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000558 // Modify the variable's linkage by its type, but ignore the
559 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000560 LinkageInfo TypeLV = getLVForType(VD->getType());
561 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000562 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000563 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000564 }
565
John McCall457a04e2010-10-22 21:05:15 +0000566 return LV;
John McCall8823c652010-08-13 08:35:10 +0000567}
568
John McCalld396b972011-02-08 19:01:05 +0000569static void clearLinkageForClass(const CXXRecordDecl *record) {
570 for (CXXRecordDecl::decl_iterator
571 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
572 Decl *child = *i;
573 if (isa<NamedDecl>(child))
574 cast<NamedDecl>(child)->ClearLinkageCache();
575 }
576}
577
David Blaikie68e081d2011-12-20 02:48:34 +0000578void NamedDecl::anchor() { }
579
John McCalld396b972011-02-08 19:01:05 +0000580void NamedDecl::ClearLinkageCache() {
581 // Note that we can't skip clearing the linkage of children just
582 // because the parent doesn't have cached linkage: we don't cache
583 // when computing linkage for parent contexts.
584
585 HasCachedLinkage = 0;
586
587 // If we're changing the linkage of a class, we need to reset the
588 // linkage of child declarations, too.
589 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
590 clearLinkageForClass(record);
591
John McCall83779672011-02-19 02:53:41 +0000592 if (ClassTemplateDecl *temp =
593 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000594 // Clear linkage for the template pattern.
595 CXXRecordDecl *record = temp->getTemplatedDecl();
596 record->HasCachedLinkage = 0;
597 clearLinkageForClass(record);
598
John McCall83779672011-02-19 02:53:41 +0000599 // We need to clear linkage for specializations, too.
600 for (ClassTemplateDecl::spec_iterator
601 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
602 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000603 }
John McCall83779672011-02-19 02:53:41 +0000604
605 // Clear cached linkage for function template decls, too.
606 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000607 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
608 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000609 for (FunctionTemplateDecl::spec_iterator
610 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
611 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000612 }
John McCall83779672011-02-19 02:53:41 +0000613
John McCalld396b972011-02-08 19:01:05 +0000614}
615
Douglas Gregorbf62d642010-12-06 18:36:25 +0000616Linkage NamedDecl::getLinkage() const {
617 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000618 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000619 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000620 return Linkage(CachedLinkage);
621 }
622
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000623 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000624 HasCachedLinkage = 1;
625 return Linkage(CachedLinkage);
626}
627
John McCallc273f242010-10-30 11:50:40 +0000628LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000629 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000630 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000631 HasCachedLinkage = 1;
632 CachedLinkage = LI.linkage();
633 return LI;
John McCall033caa52010-10-29 00:29:13 +0000634}
Ted Kremenek926d8602010-04-20 23:15:35 +0000635
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000636llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
637 // Use the most recent declaration of a variable.
638 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000639 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000640
641 // Use the most recent declaration of a function, and also handle
642 // function template specializations.
643 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
644 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000645 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000646 return V;
647
648 // If the function is a specialization of a template with an
649 // explicit visibility attribute, use that.
650 if (FunctionTemplateSpecializationInfo *templateInfo
651 = fn->getTemplateSpecializationInfo())
652 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
653
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000654 // If the function is a member of a specialization of a class template
655 // and the corresponding decl has explicit visibility, use that.
656 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
657 if (InstantiatedFrom)
658 return getVisibilityOf(InstantiatedFrom);
659
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000660 return llvm::Optional<Visibility>();
661 }
662
663 // Otherwise, just check the declaration itself first.
664 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
665 return V;
666
667 // If there wasn't explicit visibility there, and this is a
668 // specialization of a class template, check for visibility
669 // on the pattern.
670 if (const ClassTemplateSpecializationDecl *spec
671 = dyn_cast<ClassTemplateSpecializationDecl>(this))
672 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
673
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000674 // If this is a member class of a specialization of a class template
675 // and the corresponding decl has explicit visibility, use that.
676 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
677 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
678 if (InstantiatedFrom)
679 return getVisibilityOf(InstantiatedFrom);
680 }
681
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000682 return llvm::Optional<Visibility>();
683}
684
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000685static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000686 // Objective-C: treat all Objective-C declarations as having external
687 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000688 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000689 default:
690 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000691 case Decl::ParmVar:
692 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000693 case Decl::TemplateTemplateParm: // count these as external
694 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000695 case Decl::ObjCAtDefsField:
696 case Decl::ObjCCategory:
697 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000698 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000699 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000700 case Decl::ObjCMethod:
701 case Decl::ObjCProperty:
702 case Decl::ObjCPropertyImpl:
703 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000704 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000705
706 case Decl::CXXRecord: {
707 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
708 if (Record->isLambda()) {
709 if (!Record->getLambdaManglingNumber()) {
710 // This lambda has no mangling number, so it's internal.
711 return LinkageInfo::internal();
712 }
713
714 // This lambda has its linkage/visibility determined by its owner.
715 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
716 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
717 if (isa<ParmVarDecl>(ContextDecl))
718 DC = ContextDecl->getDeclContext()->getRedeclContext();
719 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000720 return getLVForDecl(cast<NamedDecl>(ContextDecl),
721 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000722 }
723
724 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000725 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000726
727 return LinkageInfo::external();
728 }
729
730 break;
731 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000732 }
733
Douglas Gregorf73b2822009-11-25 22:24:25 +0000734 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000735 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000736 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000737
738 // C++ [basic.link]p5:
739 // In addition, a member function, static data member, a named
740 // class or enumeration of class scope, or an unnamed class or
741 // enumeration defined in a class-scope typedef declaration such
742 // that the class or enumeration has the typedef name for linkage
743 // purposes (7.1.3), has external linkage if the name of the class
744 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000745 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000746 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000747
748 // C++ [basic.link]p6:
749 // The name of a function declared in block scope and the name of
750 // an object declared by a block scope extern declaration have
751 // linkage. If there is a visible declaration of an entity with
752 // linkage having the same name and type, ignoring entities
753 // declared outside the innermost enclosing namespace scope, the
754 // block scope declaration declares that same entity and receives
755 // the linkage of the previous declaration. If there is more than
756 // one such matching entity, the program is ill-formed. Otherwise,
757 // if no matching entity is found, the block scope entity receives
758 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000759 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
760 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000761 if (Function->isInAnonymousNamespace() &&
762 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000763 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000764
John McCallc273f242010-10-30 11:50:40 +0000765 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000766 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000767 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000768 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000769 }
770
Douglas Gregorec9fd132012-01-14 16:38:05 +0000771 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000772 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000773 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
774 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000775 }
776
777 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000778 }
779
John McCall033caa52010-10-29 00:29:13 +0000780 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000781 if (Var->getStorageClass() == SC_Extern ||
782 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000783 if (Var->isInAnonymousNamespace() &&
784 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000785 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000786
John McCallc273f242010-10-30 11:50:40 +0000787 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000788 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000789 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000790 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000791 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000792 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000793 }
794
Douglas Gregorec9fd132012-01-14 16:38:05 +0000795 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000796 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000797 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
798 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000799 }
800
801 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000802 }
803 }
804
805 // C++ [basic.link]p6:
806 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000807 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000808}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000809
Douglas Gregor2ada0482009-02-04 17:27:36 +0000810std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000811 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000812}
813
814std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000815 const DeclContext *Ctx = getDeclContext();
816
817 if (Ctx->isFunctionOrMethod())
818 return getNameAsString();
819
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000820 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000821 ContextsTy Contexts;
822
823 // Collect contexts.
824 while (Ctx && isa<NamedDecl>(Ctx)) {
825 Contexts.push_back(Ctx);
826 Ctx = Ctx->getParent();
827 };
828
829 std::string QualName;
830 llvm::raw_string_ostream OS(QualName);
831
832 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
833 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000834 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000835 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000836 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
837 std::string TemplateArgsStr
838 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000839 TemplateArgs.data(),
840 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000841 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000842 OS << Spec->getName() << TemplateArgsStr;
843 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000844 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000845 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000846 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000847 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000848 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
849 if (!RD->getIdentifier())
850 OS << "<anonymous " << RD->getKindName() << '>';
851 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000852 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000853 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000854 const FunctionProtoType *FT = 0;
855 if (FD->hasWrittenPrototype())
856 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
857
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000858 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000859 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000860 unsigned NumParams = FD->getNumParams();
861 for (unsigned i = 0; i < NumParams; ++i) {
862 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000863 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000864 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000865 }
866
867 if (FT->isVariadic()) {
868 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000869 OS << ", ";
870 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000871 }
872 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000873 OS << ')';
874 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000875 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000876 }
877 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000878 }
879
John McCalla2a3f7d2010-03-16 21:48:18 +0000880 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000881 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000882 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000883 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000884
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000885 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000886}
887
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000888bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000889 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
890
Douglas Gregor889ceb72009-02-03 19:21:40 +0000891 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
892 // We want to keep it, unless it nominates same namespace.
893 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000894 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
895 ->getOriginalNamespace() ==
896 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
897 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000898 }
Mike Stump11289f42009-09-09 15:08:12 +0000899
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000900 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
901 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000902 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000903
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000904 // For function templates, the underlying function declarations are linked.
905 if (const FunctionTemplateDecl *FunctionTemplate
906 = dyn_cast<FunctionTemplateDecl>(this))
907 if (const FunctionTemplateDecl *OldFunctionTemplate
908 = dyn_cast<FunctionTemplateDecl>(OldD))
909 return FunctionTemplate->getTemplatedDecl()
910 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000911
Steve Naroffc4173fa2009-02-22 19:35:57 +0000912 // For method declarations, we keep track of redeclarations.
913 if (isa<ObjCMethodDecl>(this))
914 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000915
John McCall9f3059a2009-10-09 21:13:30 +0000916 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
917 return true;
918
John McCall3f746822009-11-17 05:59:44 +0000919 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
920 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
921 cast<UsingShadowDecl>(OldD)->getTargetDecl();
922
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000923 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
924 ASTContext &Context = getASTContext();
925 return Context.getCanonicalNestedNameSpecifier(
926 cast<UsingDecl>(this)->getQualifier()) ==
927 Context.getCanonicalNestedNameSpecifier(
928 cast<UsingDecl>(OldD)->getQualifier());
929 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000930
Douglas Gregorb59643b2012-01-03 23:26:26 +0000931 // A typedef of an Objective-C class type can replace an Objective-C class
932 // declaration or definition, and vice versa.
933 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
934 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
935 return true;
936
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000937 // For non-function declarations, if the declarations are of the
938 // same kind then this must be a redeclaration, or semantic analysis
939 // would not have given us the new declaration.
940 return this->getKind() == OldD->getKind();
941}
942
Douglas Gregoreddf4332009-02-24 20:03:32 +0000943bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000944 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000945}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000946
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000947NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000948 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000949 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
950 ND = UD->getTargetDecl();
951
952 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
953 return AD->getClassInterface();
954
955 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000956}
957
John McCalla8ae2222010-04-06 21:38:20 +0000958bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000959 if (!isCXXClassMember())
960 return false;
961
John McCalla8ae2222010-04-06 21:38:20 +0000962 const NamedDecl *D = this;
963 if (isa<UsingShadowDecl>(D))
964 D = cast<UsingShadowDecl>(D)->getTargetDecl();
965
Francois Pichet783dd6e2010-11-21 06:08:52 +0000966 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000967 return true;
968 if (isa<CXXMethodDecl>(D))
969 return cast<CXXMethodDecl>(D)->isInstance();
970 if (isa<FunctionTemplateDecl>(D))
971 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
972 ->getTemplatedDecl())->isInstance();
973 return false;
974}
975
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000976//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000977// DeclaratorDecl Implementation
978//===----------------------------------------------------------------------===//
979
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000980template <typename DeclT>
981static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
982 if (decl->getNumTemplateParameterLists() > 0)
983 return decl->getTemplateParameterList(0)->getTemplateLoc();
984 else
985 return decl->getInnerLocStart();
986}
987
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000988SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000989 TypeSourceInfo *TSI = getTypeSourceInfo();
990 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000991 return SourceLocation();
992}
993
Douglas Gregor14454802011-02-25 02:25:35 +0000994void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
995 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +0000996 // Make sure the extended decl info is allocated.
997 if (!hasExtInfo()) {
998 // Save (non-extended) type source info pointer.
999 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1000 // Allocate external info struct.
1001 DeclInfo = new (getASTContext()) ExtInfo;
1002 // Restore savedTInfo into (extended) decl info.
1003 getExtInfo()->TInfo = savedTInfo;
1004 }
1005 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001006 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001007 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001008 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001009 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001010 if (getExtInfo()->NumTemplParamLists == 0) {
1011 // Save type source info pointer.
1012 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1013 // Deallocate the extended decl info.
1014 getASTContext().Deallocate(getExtInfo());
1015 // Restore savedTInfo into (non-extended) decl info.
1016 DeclInfo = savedTInfo;
1017 }
1018 else
1019 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001020 }
1021 }
1022}
1023
Abramo Bagnara60804e12011-03-18 15:16:37 +00001024void
1025DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1026 unsigned NumTPLists,
1027 TemplateParameterList **TPLists) {
1028 assert(NumTPLists > 0);
1029 // Make sure the extended decl info is allocated.
1030 if (!hasExtInfo()) {
1031 // Save (non-extended) type source info pointer.
1032 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1033 // Allocate external info struct.
1034 DeclInfo = new (getASTContext()) ExtInfo;
1035 // Restore savedTInfo into (extended) decl info.
1036 getExtInfo()->TInfo = savedTInfo;
1037 }
1038 // Set the template parameter lists info.
1039 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1040}
1041
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001042SourceLocation DeclaratorDecl::getOuterLocStart() const {
1043 return getTemplateOrInnerLocStart(this);
1044}
1045
Abramo Bagnaraea947882011-03-08 16:41:52 +00001046namespace {
1047
1048// Helper function: returns true if QT is or contains a type
1049// having a postfix component.
1050bool typeIsPostfix(clang::QualType QT) {
1051 while (true) {
1052 const Type* T = QT.getTypePtr();
1053 switch (T->getTypeClass()) {
1054 default:
1055 return false;
1056 case Type::Pointer:
1057 QT = cast<PointerType>(T)->getPointeeType();
1058 break;
1059 case Type::BlockPointer:
1060 QT = cast<BlockPointerType>(T)->getPointeeType();
1061 break;
1062 case Type::MemberPointer:
1063 QT = cast<MemberPointerType>(T)->getPointeeType();
1064 break;
1065 case Type::LValueReference:
1066 case Type::RValueReference:
1067 QT = cast<ReferenceType>(T)->getPointeeType();
1068 break;
1069 case Type::PackExpansion:
1070 QT = cast<PackExpansionType>(T)->getPattern();
1071 break;
1072 case Type::Paren:
1073 case Type::ConstantArray:
1074 case Type::DependentSizedArray:
1075 case Type::IncompleteArray:
1076 case Type::VariableArray:
1077 case Type::FunctionProto:
1078 case Type::FunctionNoProto:
1079 return true;
1080 }
1081 }
1082}
1083
1084} // namespace
1085
1086SourceRange DeclaratorDecl::getSourceRange() const {
1087 SourceLocation RangeEnd = getLocation();
1088 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1089 if (typeIsPostfix(TInfo->getType()))
1090 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1091 }
1092 return SourceRange(getOuterLocStart(), RangeEnd);
1093}
1094
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001095void
Douglas Gregor20527e22010-06-15 17:44:38 +00001096QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1097 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001098 TemplateParameterList **TPLists) {
1099 assert((NumTPLists == 0 || TPLists != 0) &&
1100 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001101
1102 // Free previous template parameters (if any).
1103 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001104 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001105 TemplParamLists = 0;
1106 NumTemplParamLists = 0;
1107 }
1108 // Set info on matched template parameter lists (if any).
1109 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001110 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001111 NumTemplParamLists = NumTPLists;
1112 for (unsigned i = NumTPLists; i-- > 0; )
1113 TemplParamLists[i] = TPLists[i];
1114 }
1115}
1116
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001117//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001118// VarDecl Implementation
1119//===----------------------------------------------------------------------===//
1120
Sebastian Redl833ef452010-01-26 22:01:41 +00001121const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1122 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001123 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001124 case SC_Auto: return "auto";
1125 case SC_Extern: return "extern";
1126 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1127 case SC_PrivateExtern: return "__private_extern__";
1128 case SC_Register: return "register";
1129 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001130 }
1131
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001132 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001133}
1134
Abramo Bagnaradff19302011-03-08 08:55:46 +00001135VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1136 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001137 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001138 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001139 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001140}
1141
Douglas Gregor72172e92012-01-05 21:55:30 +00001142VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1143 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1144 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1145 QualType(), 0, SC_None, SC_None);
1146}
1147
Douglas Gregorbf62d642010-12-06 18:36:25 +00001148void VarDecl::setStorageClass(StorageClass SC) {
1149 assert(isLegalForVariable(SC));
1150 if (getStorageClass() != SC)
1151 ClearLinkageCache();
1152
John McCallbeaa11c2011-05-01 02:13:58 +00001153 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001154}
1155
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001156SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001157 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001158 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001159 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001160}
1161
Sebastian Redl833ef452010-01-26 22:01:41 +00001162bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001163 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001164 return false;
1165
Eli Friedman839192f2012-01-15 01:23:58 +00001166 const DeclContext *DC = getDeclContext();
1167 if (DC->isRecord())
1168 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001169
Eli Friedman839192f2012-01-15 01:23:58 +00001170 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001171 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001172 return true;
1173 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001174}
1175
1176VarDecl *VarDecl::getCanonicalDecl() {
1177 return getFirstDeclaration();
1178}
1179
Daniel Dunbar9d355812012-03-09 01:51:51 +00001180VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1181 ASTContext &C) const
1182{
Sebastian Redl35351a92010-01-31 22:27:38 +00001183 // C++ [basic.def]p2:
1184 // A declaration is a definition unless [...] it contains the 'extern'
1185 // specifier or a linkage-specification and neither an initializer [...],
1186 // it declares a static data member in a class declaration [...].
1187 // C++ [temp.expl.spec]p15:
1188 // An explicit specialization of a static data member of a template is a
1189 // definition if the declaration includes an initializer; otherwise, it is
1190 // a declaration.
1191 if (isStaticDataMember()) {
1192 if (isOutOfLine() && (hasInit() ||
1193 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1194 return Definition;
1195 else
1196 return DeclarationOnly;
1197 }
1198 // C99 6.7p5:
1199 // A definition of an identifier is a declaration for that identifier that
1200 // [...] causes storage to be reserved for that object.
1201 // Note: that applies for all non-file-scope objects.
1202 // C99 6.9.2p1:
1203 // If the declaration of an identifier for an object has file scope and an
1204 // initializer, the declaration is an external definition for the identifier
1205 if (hasInit())
1206 return Definition;
1207 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1208 if (hasExternalStorage())
1209 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001210
John McCall8e7d6562010-08-26 03:08:43 +00001211 if (getStorageClassAsWritten() == SC_Extern ||
1212 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001213 for (const VarDecl *PrevVar = getPreviousDecl();
1214 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001215 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1216 return DeclarationOnly;
1217 }
1218 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001219 // C99 6.9.2p2:
1220 // A declaration of an object that has file scope without an initializer,
1221 // and without a storage class specifier or the scs 'static', constitutes
1222 // a tentative definition.
1223 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001224 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001225 return TentativeDefinition;
1226
1227 // What's left is (in C, block-scope) declarations without initializers or
1228 // external storage. These are definitions.
1229 return Definition;
1230}
1231
Sebastian Redl35351a92010-01-31 22:27:38 +00001232VarDecl *VarDecl::getActingDefinition() {
1233 DefinitionKind Kind = isThisDeclarationADefinition();
1234 if (Kind != TentativeDefinition)
1235 return 0;
1236
Chris Lattner48eb14d2010-06-14 18:31:46 +00001237 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001238 VarDecl *First = getFirstDeclaration();
1239 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1240 I != E; ++I) {
1241 Kind = (*I)->isThisDeclarationADefinition();
1242 if (Kind == Definition)
1243 return 0;
1244 else if (Kind == TentativeDefinition)
1245 LastTentative = *I;
1246 }
1247 return LastTentative;
1248}
1249
1250bool VarDecl::isTentativeDefinitionNow() const {
1251 DefinitionKind Kind = isThisDeclarationADefinition();
1252 if (Kind != TentativeDefinition)
1253 return false;
1254
1255 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1256 if ((*I)->isThisDeclarationADefinition() == Definition)
1257 return false;
1258 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001259 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001260}
1261
Daniel Dunbar9d355812012-03-09 01:51:51 +00001262VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001263 VarDecl *First = getFirstDeclaration();
1264 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1265 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001266 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001267 return *I;
1268 }
1269 return 0;
1270}
1271
Daniel Dunbar9d355812012-03-09 01:51:51 +00001272VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001273 DefinitionKind Kind = DeclarationOnly;
1274
1275 const VarDecl *First = getFirstDeclaration();
1276 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001277 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001278 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001279 if (Kind == Definition)
1280 break;
1281 }
John McCall37bb6c92010-10-29 22:22:43 +00001282
1283 return Kind;
1284}
1285
Sebastian Redl5ca79842010-02-01 20:16:42 +00001286const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001287 redecl_iterator I = redecls_begin(), E = redecls_end();
1288 while (I != E && !I->getInit())
1289 ++I;
1290
1291 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001292 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001293 return I->getInit();
1294 }
1295 return 0;
1296}
1297
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001298bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001299 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001300 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001301
1302 if (!isStaticDataMember())
1303 return false;
1304
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001305 // If this static data member was instantiated from a static data member of
1306 // a class template, check whether that static data member was defined
1307 // out-of-line.
1308 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1309 return VD->isOutOfLine();
1310
1311 return false;
1312}
1313
Douglas Gregor1d957a32009-10-27 18:42:08 +00001314VarDecl *VarDecl::getOutOfLineDefinition() {
1315 if (!isStaticDataMember())
1316 return 0;
1317
1318 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1319 RD != RDEnd; ++RD) {
1320 if (RD->getLexicalDeclContext()->isFileContext())
1321 return *RD;
1322 }
1323
1324 return 0;
1325}
1326
Douglas Gregord5058122010-02-11 01:19:42 +00001327void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001328 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1329 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001330 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001331 }
1332
1333 Init = I;
1334}
1335
Daniel Dunbar9d355812012-03-09 01:51:51 +00001336bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001337 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001338
Richard Smith35ecb362012-03-02 04:14:40 +00001339 if (!Lang.CPlusPlus)
1340 return false;
1341
1342 // In C++11, any variable of reference type can be used in a constant
1343 // expression if it is initialized by a constant expression.
1344 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1345 return true;
1346
1347 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001348 // not require the variable to be non-volatile, but we consider this to be a
1349 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001350 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001351 return false;
1352
1353 // In C++, const, non-volatile variables of integral or enumeration types
1354 // can be used in constant expressions.
1355 if (getType()->isIntegralOrEnumerationType())
1356 return true;
1357
Richard Smith35ecb362012-03-02 04:14:40 +00001358 // Additionally, in C++11, non-volatile constexpr variables can be used in
1359 // constant expressions.
1360 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001361}
1362
Richard Smithd0b4dd62011-12-19 06:19:21 +00001363/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1364/// form, which contains extra information on the evaluated value of the
1365/// initializer.
1366EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1367 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1368 if (!Eval) {
1369 Stmt *S = Init.get<Stmt *>();
1370 Eval = new (getASTContext()) EvaluatedStmt;
1371 Eval->Value = S;
1372 Init = Eval;
1373 }
1374 return Eval;
1375}
1376
Richard Smithdafff942012-01-14 04:30:29 +00001377APValue *VarDecl::evaluateValue() const {
1378 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1379 return evaluateValue(Notes);
1380}
1381
1382APValue *VarDecl::evaluateValue(
1383 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001384 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1385
1386 // We only produce notes indicating why an initializer is non-constant the
1387 // first time it is evaluated. FIXME: The notes won't always be emitted the
1388 // first time we try evaluation, so might not be produced at all.
1389 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001390 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001391
1392 const Expr *Init = cast<Expr>(Eval->Value);
1393 assert(!Init->isValueDependent());
1394
1395 if (Eval->IsEvaluating) {
1396 // FIXME: Produce a diagnostic for self-initialization.
1397 Eval->CheckedICE = true;
1398 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001399 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001400 }
1401
1402 Eval->IsEvaluating = true;
1403
1404 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1405 this, Notes);
1406
1407 // Ensure the result is an uninitialized APValue if evaluation fails.
1408 if (!Result)
1409 Eval->Evaluated = APValue();
1410
1411 Eval->IsEvaluating = false;
1412 Eval->WasEvaluated = true;
1413
1414 // In C++11, we have determined whether the initializer was a constant
1415 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001416 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001417 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001418 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001419 }
1420
Richard Smithdafff942012-01-14 04:30:29 +00001421 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001422}
1423
1424bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001425 // Initializers of weak variables are never ICEs.
1426 if (isWeak())
1427 return false;
1428
Richard Smithd0b4dd62011-12-19 06:19:21 +00001429 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1430 if (Eval->CheckedICE)
1431 // We have already checked whether this subexpression is an
1432 // integral constant expression.
1433 return Eval->IsICE;
1434
1435 const Expr *Init = cast<Expr>(Eval->Value);
1436 assert(!Init->isValueDependent());
1437
1438 // In C++11, evaluate the initializer to check whether it's a constant
1439 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001440 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001441 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1442 evaluateValue(Notes);
1443 return Eval->IsICE;
1444 }
1445
1446 // It's an ICE whether or not the definition we found is
1447 // out-of-line. See DR 721 and the discussion in Clang PR
1448 // 6206 for details.
1449
1450 if (Eval->CheckingICE)
1451 return false;
1452 Eval->CheckingICE = true;
1453
1454 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1455 Eval->CheckingICE = false;
1456 Eval->CheckedICE = true;
1457 return Eval->IsICE;
1458}
1459
Douglas Gregorfe314812011-06-21 17:03:29 +00001460bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001461 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001462
1463 const Expr *E = getInit();
1464 if (!E)
1465 return false;
1466
1467 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1468 E = Cleanups->getSubExpr();
1469
1470 return isa<MaterializeTemporaryExpr>(E);
1471}
1472
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001473VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001474 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001475 return cast<VarDecl>(MSI->getInstantiatedFrom());
1476
1477 return 0;
1478}
1479
Douglas Gregor3c74d412009-10-14 20:14:33 +00001480TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001481 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001482 return MSI->getTemplateSpecializationKind();
1483
1484 return TSK_Undeclared;
1485}
1486
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001487MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001488 return getASTContext().getInstantiatedFromStaticDataMember(this);
1489}
1490
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001491void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1492 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001493 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001494 assert(MSI && "Not an instantiated static data member?");
1495 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001496 if (TSK != TSK_ExplicitSpecialization &&
1497 PointOfInstantiation.isValid() &&
1498 MSI->getPointOfInstantiation().isInvalid())
1499 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001500}
1501
Sebastian Redl833ef452010-01-26 22:01:41 +00001502//===----------------------------------------------------------------------===//
1503// ParmVarDecl Implementation
1504//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001505
Sebastian Redl833ef452010-01-26 22:01:41 +00001506ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001507 SourceLocation StartLoc,
1508 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001509 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001510 StorageClass S, StorageClass SCAsWritten,
1511 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001512 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001513 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001514}
1515
Douglas Gregor72172e92012-01-05 21:55:30 +00001516ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1517 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1518 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1519 0, QualType(), 0, SC_None, SC_None, 0);
1520}
1521
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001522SourceRange ParmVarDecl::getSourceRange() const {
1523 if (!hasInheritedDefaultArg()) {
1524 SourceRange ArgRange = getDefaultArgRange();
1525 if (ArgRange.isValid())
1526 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1527 }
1528
1529 return DeclaratorDecl::getSourceRange();
1530}
1531
Sebastian Redl833ef452010-01-26 22:01:41 +00001532Expr *ParmVarDecl::getDefaultArg() {
1533 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1534 assert(!hasUninstantiatedDefaultArg() &&
1535 "Default argument is not yet instantiated!");
1536
1537 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001538 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001539 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001540
Sebastian Redl833ef452010-01-26 22:01:41 +00001541 return Arg;
1542}
1543
Sebastian Redl833ef452010-01-26 22:01:41 +00001544SourceRange ParmVarDecl::getDefaultArgRange() const {
1545 if (const Expr *E = getInit())
1546 return E->getSourceRange();
1547
1548 if (hasUninstantiatedDefaultArg())
1549 return getUninstantiatedDefaultArg()->getSourceRange();
1550
1551 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001552}
1553
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001554bool ParmVarDecl::isParameterPack() const {
1555 return isa<PackExpansionType>(getType());
1556}
1557
Ted Kremenek540017e2011-10-06 05:00:56 +00001558void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1559 getASTContext().setParameterIndex(this, parameterIndex);
1560 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1561}
1562
1563unsigned ParmVarDecl::getParameterIndexLarge() const {
1564 return getASTContext().getParameterIndex(this);
1565}
1566
Nuno Lopes394ec982008-12-17 23:39:55 +00001567//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001568// FunctionDecl Implementation
1569//===----------------------------------------------------------------------===//
1570
Douglas Gregorb11aad82011-02-19 18:51:44 +00001571void FunctionDecl::getNameForDiagnostic(std::string &S,
1572 const PrintingPolicy &Policy,
1573 bool Qualified) const {
1574 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1575 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1576 if (TemplateArgs)
1577 S += TemplateSpecializationType::PrintTemplateArgumentList(
1578 TemplateArgs->data(),
1579 TemplateArgs->size(),
1580 Policy);
1581
1582}
1583
Ted Kremenek186a0742010-04-29 16:49:01 +00001584bool FunctionDecl::isVariadic() const {
1585 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1586 return FT->isVariadic();
1587 return false;
1588}
1589
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001590bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1591 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001592 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001593 Definition = *I;
1594 return true;
1595 }
1596 }
1597
1598 return false;
1599}
1600
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001601bool FunctionDecl::hasTrivialBody() const
1602{
1603 Stmt *S = getBody();
1604 if (!S) {
1605 // Since we don't have a body for this function, we don't know if it's
1606 // trivial or not.
1607 return false;
1608 }
1609
1610 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1611 return true;
1612 return false;
1613}
1614
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001615bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1616 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001617 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001618 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1619 return true;
1620 }
1621 }
1622
1623 return false;
1624}
1625
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001626Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001627 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1628 if (I->Body) {
1629 Definition = *I;
1630 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001631 } else if (I->IsLateTemplateParsed) {
1632 Definition = *I;
1633 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001634 }
1635 }
1636
1637 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001638}
1639
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001640void FunctionDecl::setBody(Stmt *B) {
1641 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001642 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001643 EndRangeLoc = B->getLocEnd();
1644}
1645
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001646void FunctionDecl::setPure(bool P) {
1647 IsPure = P;
1648 if (P)
1649 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1650 Parent->markedVirtualFunctionPure();
1651}
1652
Douglas Gregor16618f22009-09-12 00:17:51 +00001653bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001654 const TranslationUnitDecl *tunit =
1655 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1656 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001657 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001658 getIdentifier() &&
1659 getIdentifier()->isStr("main");
1660}
1661
1662bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1663 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1664 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1665 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1666 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1667 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1668
1669 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1670 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1671
1672 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1673 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1674
1675 ASTContext &Context =
1676 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1677 ->getASTContext();
1678
1679 // The result type and first argument type are constant across all
1680 // these operators. The second argument must be exactly void*.
1681 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001682}
1683
Douglas Gregor16618f22009-09-12 00:17:51 +00001684bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001685 if (getLinkage() != ExternalLinkage)
1686 return false;
1687
1688 if (getAttr<OverloadableAttr>())
1689 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001690
Chandler Carruth4322a282011-02-25 00:05:02 +00001691 const DeclContext *DC = getDeclContext();
1692 if (DC->isRecord())
1693 return false;
1694
Eli Friedman839192f2012-01-15 01:23:58 +00001695 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001696 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001697 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001698
Eli Friedman839192f2012-01-15 01:23:58 +00001699 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001700}
1701
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001702bool FunctionDecl::isGlobal() const {
1703 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1704 return Method->isStatic();
1705
John McCall8e7d6562010-08-26 03:08:43 +00001706 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001707 return false;
1708
Mike Stump11289f42009-09-09 15:08:12 +00001709 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001710 DC->isNamespace();
1711 DC = DC->getParent()) {
1712 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1713 if (!Namespace->getDeclName())
1714 return false;
1715 break;
1716 }
1717 }
1718
1719 return true;
1720}
1721
Sebastian Redl833ef452010-01-26 22:01:41 +00001722void
1723FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1724 redeclarable_base::setPreviousDeclaration(PrevDecl);
1725
1726 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1727 FunctionTemplateDecl *PrevFunTmpl
1728 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1729 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1730 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1731 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001732
Axel Naumannfbc7b982011-11-08 18:21:06 +00001733 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001734 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001735}
1736
1737const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1738 return getFirstDeclaration();
1739}
1740
1741FunctionDecl *FunctionDecl::getCanonicalDecl() {
1742 return getFirstDeclaration();
1743}
1744
Douglas Gregorbf62d642010-12-06 18:36:25 +00001745void FunctionDecl::setStorageClass(StorageClass SC) {
1746 assert(isLegalForFunction(SC));
1747 if (getStorageClass() != SC)
1748 ClearLinkageCache();
1749
1750 SClass = SC;
1751}
1752
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001753/// \brief Returns a value indicating whether this function
1754/// corresponds to a builtin function.
1755///
1756/// The function corresponds to a built-in function if it is
1757/// declared at translation scope or within an extern "C" block and
1758/// its name matches with the name of a builtin. The returned value
1759/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001760/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001761/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001762unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001763 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001764 return 0;
1765
1766 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001767 if (!BuiltinID)
1768 return 0;
1769
1770 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001771 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1772 return BuiltinID;
1773
1774 // This function has the name of a known C library
1775 // function. Determine whether it actually refers to the C library
1776 // function or whether it just has the same name.
1777
Douglas Gregora908e7f2009-02-17 03:23:10 +00001778 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001779 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001780 return 0;
1781
Douglas Gregore711f702009-02-14 18:57:46 +00001782 // If this function is at translation-unit scope and we're not in
1783 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001784 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001785 getDeclContext()->isTranslationUnit())
1786 return BuiltinID;
1787
1788 // If the function is in an extern "C" linkage specification and is
1789 // not marked "overloadable", it's the real function.
1790 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001791 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001792 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001793 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001794 return BuiltinID;
1795
1796 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001797 return 0;
1798}
1799
1800
Chris Lattner47c0d002009-04-25 06:03:53 +00001801/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001802/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001803/// after it has been created.
1804unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001805 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001806 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001807 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001808 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001809
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001810}
1811
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001812void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001813 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001814 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001815 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001816
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001817 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001818 if (!NewParamInfo.empty()) {
1819 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1820 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001821 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001822}
Chris Lattner41943152007-01-25 04:52:46 +00001823
James Molloy6f8780b2012-02-29 10:24:19 +00001824void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1825 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1826
1827 if (!NewDecls.empty()) {
1828 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1829 std::copy(NewDecls.begin(), NewDecls.end(), A);
1830 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1831 }
1832}
1833
Chris Lattner58258242008-04-10 02:22:51 +00001834/// getMinRequiredArguments - Returns the minimum number of arguments
1835/// needed to call this function. This may be fewer than the number of
1836/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001837/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001838unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001839 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001840 return getNumParams();
1841
Douglas Gregor7825bf32011-01-06 22:09:01 +00001842 unsigned NumRequiredArgs = getNumParams();
1843
1844 // If the last parameter is a parameter pack, we don't need an argument for
1845 // it.
1846 if (NumRequiredArgs > 0 &&
1847 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1848 --NumRequiredArgs;
1849
1850 // If this parameter has a default argument, we don't need an argument for
1851 // it.
1852 while (NumRequiredArgs > 0 &&
1853 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001854 --NumRequiredArgs;
1855
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001856 // We might have parameter packs before the end. These can't be deduced,
1857 // but they can still handle multiple arguments.
1858 unsigned ArgIdx = NumRequiredArgs;
1859 while (ArgIdx > 0) {
1860 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1861 NumRequiredArgs = ArgIdx;
1862
1863 --ArgIdx;
1864 }
1865
Chris Lattner58258242008-04-10 02:22:51 +00001866 return NumRequiredArgs;
1867}
1868
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001869bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001870 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001871 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001872
1873 if (isa<CXXMethodDecl>(this)) {
1874 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1875 return true;
1876 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001877
1878 switch (getTemplateSpecializationKind()) {
1879 case TSK_Undeclared:
1880 case TSK_ExplicitSpecialization:
1881 return false;
1882
1883 case TSK_ImplicitInstantiation:
1884 case TSK_ExplicitInstantiationDeclaration:
1885 case TSK_ExplicitInstantiationDefinition:
1886 // Handle below.
1887 break;
1888 }
1889
1890 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001891 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001892 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001893 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001894
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001895 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001896 return PatternDecl->isInlined();
1897
1898 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001899}
1900
Eli Friedman1b125c32012-02-07 03:50:18 +00001901static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1902 // Only consider file-scope declarations in this test.
1903 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1904 return false;
1905
1906 // Only consider explicit declarations; the presence of a builtin for a
1907 // libcall shouldn't affect whether a definition is externally visible.
1908 if (Redecl->isImplicit())
1909 return false;
1910
1911 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1912 return true; // Not an inline definition
1913
1914 return false;
1915}
1916
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001917/// \brief For a function declaration in C or C++, determine whether this
1918/// declaration causes the definition to be externally visible.
1919///
Eli Friedman1b125c32012-02-07 03:50:18 +00001920/// Specifically, this determines if adding the current declaration to the set
1921/// of redeclarations of the given functions causes
1922/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001923bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1924 assert(!doesThisDeclarationHaveABody() &&
1925 "Must have a declaration without a body.");
1926
1927 ASTContext &Context = getASTContext();
1928
David Blaikiebbafb8a2012-03-11 07:00:24 +00001929 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001930 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1931 // an externally visible definition.
1932 //
1933 // FIXME: What happens if gnu_inline gets added on after the first
1934 // declaration?
1935 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1936 return false;
1937
1938 const FunctionDecl *Prev = this;
1939 bool FoundBody = false;
1940 while ((Prev = Prev->getPreviousDecl())) {
1941 FoundBody |= Prev->Body;
1942
1943 if (Prev->Body) {
1944 // If it's not the case that both 'inline' and 'extern' are
1945 // specified on the definition, then it is always externally visible.
1946 if (!Prev->isInlineSpecified() ||
1947 Prev->getStorageClassAsWritten() != SC_Extern)
1948 return false;
1949 } else if (Prev->isInlineSpecified() &&
1950 Prev->getStorageClassAsWritten() != SC_Extern) {
1951 return false;
1952 }
1953 }
1954 return FoundBody;
1955 }
1956
David Blaikiebbafb8a2012-03-11 07:00:24 +00001957 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001958 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001959
1960 // C99 6.7.4p6:
1961 // [...] If all of the file scope declarations for a function in a
1962 // translation unit include the inline function specifier without extern,
1963 // then the definition in that translation unit is an inline definition.
1964 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001965 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001966 const FunctionDecl *Prev = this;
1967 bool FoundBody = false;
1968 while ((Prev = Prev->getPreviousDecl())) {
1969 FoundBody |= Prev->Body;
1970 if (RedeclForcesDefC99(Prev))
1971 return false;
1972 }
1973 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001974}
1975
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001976/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001977/// definition will be externally visible.
1978///
1979/// Inline function definitions are always available for inlining optimizations.
1980/// However, depending on the language dialect, declaration specifiers, and
1981/// attributes, the definition of an inline function may or may not be
1982/// "externally" visible to other translation units in the program.
1983///
1984/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001985/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001986/// inline definition becomes externally visible (C99 6.7.4p6).
1987///
1988/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1989/// definition, we use the GNU semantics for inline, which are nearly the
1990/// opposite of C99 semantics. In particular, "inline" by itself will create
1991/// an externally visible symbol, but "extern inline" will not create an
1992/// externally visible symbol.
1993bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001994 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001995 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001996 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001997
David Blaikiebbafb8a2012-03-11 07:00:24 +00001998 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001999 // Note: If you change the logic here, please change
2000 // doesDeclarationForceExternallyVisibleDefinition as well.
2001 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002002 // If it's not the case that both 'inline' and 'extern' are
2003 // specified on the definition, then this inline definition is
2004 // externally visible.
2005 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2006 return true;
2007
2008 // If any declaration is 'inline' but not 'extern', then this definition
2009 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002010 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2011 Redecl != RedeclEnd;
2012 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002013 if (Redecl->isInlineSpecified() &&
2014 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002015 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002016 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002017
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002018 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002019 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002020
Douglas Gregor299d76e2009-09-13 07:46:26 +00002021 // C99 6.7.4p6:
2022 // [...] If all of the file scope declarations for a function in a
2023 // translation unit include the inline function specifier without extern,
2024 // then the definition in that translation unit is an inline definition.
2025 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2026 Redecl != RedeclEnd;
2027 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002028 if (RedeclForcesDefC99(*Redecl))
2029 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002030 }
2031
2032 // C99 6.7.4p6:
2033 // An inline definition does not provide an external definition for the
2034 // function, and does not forbid an external definition in another
2035 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002036 return false;
2037}
2038
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002039/// getOverloadedOperator - Which C++ overloaded operator this
2040/// function represents, if any.
2041OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002042 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2043 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002044 else
2045 return OO_None;
2046}
2047
Alexis Huntc88db062010-01-13 09:01:02 +00002048/// getLiteralIdentifier - The literal suffix identifier this function
2049/// represents, if any.
2050const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2051 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2052 return getDeclName().getCXXLiteralIdentifier();
2053 else
2054 return 0;
2055}
2056
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002057FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2058 if (TemplateOrSpecialization.isNull())
2059 return TK_NonTemplate;
2060 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2061 return TK_FunctionTemplate;
2062 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2063 return TK_MemberSpecialization;
2064 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2065 return TK_FunctionTemplateSpecialization;
2066 if (TemplateOrSpecialization.is
2067 <DependentFunctionTemplateSpecializationInfo*>())
2068 return TK_DependentFunctionTemplateSpecialization;
2069
David Blaikie83d382b2011-09-23 05:06:16 +00002070 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002071}
2072
Douglas Gregord801b062009-10-07 23:56:10 +00002073FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002074 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002075 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2076
2077 return 0;
2078}
2079
Douglas Gregor06db9f52009-10-12 20:18:28 +00002080MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2081 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2082}
2083
Douglas Gregord801b062009-10-07 23:56:10 +00002084void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002085FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2086 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002087 TemplateSpecializationKind TSK) {
2088 assert(TemplateOrSpecialization.isNull() &&
2089 "Member function is already a specialization");
2090 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002091 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002092 TemplateOrSpecialization = Info;
2093}
2094
Douglas Gregorafca3b42009-10-27 20:53:28 +00002095bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002096 // If the function is invalid, it can't be implicitly instantiated.
2097 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002098 return false;
2099
2100 switch (getTemplateSpecializationKind()) {
2101 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002102 case TSK_ExplicitInstantiationDefinition:
2103 return false;
2104
2105 case TSK_ImplicitInstantiation:
2106 return true;
2107
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002108 // It is possible to instantiate TSK_ExplicitSpecialization kind
2109 // if the FunctionDecl has a class scope specialization pattern.
2110 case TSK_ExplicitSpecialization:
2111 return getClassScopeSpecializationPattern() != 0;
2112
Douglas Gregorafca3b42009-10-27 20:53:28 +00002113 case TSK_ExplicitInstantiationDeclaration:
2114 // Handled below.
2115 break;
2116 }
2117
2118 // Find the actual template from which we will instantiate.
2119 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002120 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002121 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002122 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002123
2124 // C++0x [temp.explicit]p9:
2125 // Except for inline functions, other explicit instantiation declarations
2126 // have the effect of suppressing the implicit instantiation of the entity
2127 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002128 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002129 return true;
2130
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002131 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002132}
2133
2134bool FunctionDecl::isTemplateInstantiation() const {
2135 switch (getTemplateSpecializationKind()) {
2136 case TSK_Undeclared:
2137 case TSK_ExplicitSpecialization:
2138 return false;
2139 case TSK_ImplicitInstantiation:
2140 case TSK_ExplicitInstantiationDeclaration:
2141 case TSK_ExplicitInstantiationDefinition:
2142 return true;
2143 }
2144 llvm_unreachable("All TSK values handled.");
2145}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002146
2147FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002148 // Handle class scope explicit specialization special case.
2149 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2150 return getClassScopeSpecializationPattern();
2151
Douglas Gregorafca3b42009-10-27 20:53:28 +00002152 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2153 while (Primary->getInstantiatedFromMemberTemplate()) {
2154 // If we have hit a point where the user provided a specialization of
2155 // this template, we're done looking.
2156 if (Primary->isMemberSpecialization())
2157 break;
2158
2159 Primary = Primary->getInstantiatedFromMemberTemplate();
2160 }
2161
2162 return Primary->getTemplatedDecl();
2163 }
2164
2165 return getInstantiatedFromMemberFunction();
2166}
2167
Douglas Gregor70d83e22009-06-29 17:30:29 +00002168FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002169 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002170 = TemplateOrSpecialization
2171 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002172 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002173 }
2174 return 0;
2175}
2176
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002177FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2178 return getASTContext().getClassScopeSpecializationPattern(this);
2179}
2180
Douglas Gregor70d83e22009-06-29 17:30:29 +00002181const TemplateArgumentList *
2182FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002183 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002184 = TemplateOrSpecialization
2185 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002186 return Info->TemplateArguments;
2187 }
2188 return 0;
2189}
2190
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002191const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002192FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2193 if (FunctionTemplateSpecializationInfo *Info
2194 = TemplateOrSpecialization
2195 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2196 return Info->TemplateArgumentsAsWritten;
2197 }
2198 return 0;
2199}
2200
Mike Stump11289f42009-09-09 15:08:12 +00002201void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002202FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2203 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002204 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002205 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002206 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002207 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2208 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002209 assert(TSK != TSK_Undeclared &&
2210 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002211 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002212 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002213 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002214 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2215 TemplateArgs,
2216 TemplateArgsAsWritten,
2217 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002218 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002219 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002220}
2221
John McCallb9c78482010-04-08 09:05:18 +00002222void
2223FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2224 const UnresolvedSetImpl &Templates,
2225 const TemplateArgumentListInfo &TemplateArgs) {
2226 assert(TemplateOrSpecialization.isNull());
2227 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2228 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002229 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002230 void *Buffer = Context.Allocate(Size);
2231 DependentFunctionTemplateSpecializationInfo *Info =
2232 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2233 TemplateArgs);
2234 TemplateOrSpecialization = Info;
2235}
2236
2237DependentFunctionTemplateSpecializationInfo::
2238DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2239 const TemplateArgumentListInfo &TArgs)
2240 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2241
2242 d.NumTemplates = Ts.size();
2243 d.NumArgs = TArgs.size();
2244
2245 FunctionTemplateDecl **TsArray =
2246 const_cast<FunctionTemplateDecl**>(getTemplates());
2247 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2248 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2249
2250 TemplateArgumentLoc *ArgsArray =
2251 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2252 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2253 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2254}
2255
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002256TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002257 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002258 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002259 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002260 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002261 if (FTSInfo)
2262 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002263
Douglas Gregord801b062009-10-07 23:56:10 +00002264 MemberSpecializationInfo *MSInfo
2265 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2266 if (MSInfo)
2267 return MSInfo->getTemplateSpecializationKind();
2268
2269 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002270}
2271
Mike Stump11289f42009-09-09 15:08:12 +00002272void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002273FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2274 SourceLocation PointOfInstantiation) {
2275 if (FunctionTemplateSpecializationInfo *FTSInfo
2276 = TemplateOrSpecialization.dyn_cast<
2277 FunctionTemplateSpecializationInfo*>()) {
2278 FTSInfo->setTemplateSpecializationKind(TSK);
2279 if (TSK != TSK_ExplicitSpecialization &&
2280 PointOfInstantiation.isValid() &&
2281 FTSInfo->getPointOfInstantiation().isInvalid())
2282 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2283 } else if (MemberSpecializationInfo *MSInfo
2284 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2285 MSInfo->setTemplateSpecializationKind(TSK);
2286 if (TSK != TSK_ExplicitSpecialization &&
2287 PointOfInstantiation.isValid() &&
2288 MSInfo->getPointOfInstantiation().isInvalid())
2289 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2290 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002291 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002292}
2293
2294SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002295 if (FunctionTemplateSpecializationInfo *FTSInfo
2296 = TemplateOrSpecialization.dyn_cast<
2297 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002298 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002299 else if (MemberSpecializationInfo *MSInfo
2300 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002301 return MSInfo->getPointOfInstantiation();
2302
2303 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002304}
2305
Douglas Gregor6411b922009-09-11 20:15:17 +00002306bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002307 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002308 return true;
2309
2310 // If this function was instantiated from a member function of a
2311 // class template, check whether that member function was defined out-of-line.
2312 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2313 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002314 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002315 return Definition->isOutOfLine();
2316 }
2317
2318 // If this function was instantiated from a function template,
2319 // check whether that function template was defined out-of-line.
2320 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2321 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002322 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002323 return Definition->isOutOfLine();
2324 }
2325
2326 return false;
2327}
2328
Abramo Bagnaraea947882011-03-08 16:41:52 +00002329SourceRange FunctionDecl::getSourceRange() const {
2330 return SourceRange(getOuterLocStart(), EndRangeLoc);
2331}
2332
Anna Zaks28db7ce2012-01-18 02:45:01 +00002333unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002334 IdentifierInfo *FnInfo = getIdentifier();
2335
2336 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002337 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002338
2339 // Builtin handling.
2340 switch (getBuiltinID()) {
2341 case Builtin::BI__builtin_memset:
2342 case Builtin::BI__builtin___memset_chk:
2343 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002344 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002345
2346 case Builtin::BI__builtin_memcpy:
2347 case Builtin::BI__builtin___memcpy_chk:
2348 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002349 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002350
2351 case Builtin::BI__builtin_memmove:
2352 case Builtin::BI__builtin___memmove_chk:
2353 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002354 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002355
2356 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002357 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002358 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002359 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002360
2361 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002362 case Builtin::BImemcmp:
2363 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002364
2365 case Builtin::BI__builtin_strncpy:
2366 case Builtin::BI__builtin___strncpy_chk:
2367 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002368 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002369
2370 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002371 case Builtin::BIstrncmp:
2372 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002373
2374 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002375 case Builtin::BIstrncasecmp:
2376 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002377
2378 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002379 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002380 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002381 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002382
2383 case Builtin::BI__builtin_strndup:
2384 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002385 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002386
Anna Zaks314cd092012-02-01 19:08:57 +00002387 case Builtin::BI__builtin_strlen:
2388 case Builtin::BIstrlen:
2389 return Builtin::BIstrlen;
2390
Anna Zaks201d4892012-01-13 21:52:01 +00002391 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002392 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002393 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002394 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002395 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002396 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002397 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002398 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002399 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002400 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002401 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002402 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002403 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002404 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002405 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002406 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002407 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002408 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002409 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002410 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002411 else if (FnInfo->isStr("strlen"))
2412 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002413 }
2414 break;
2415 }
Anna Zaks22122702012-01-17 00:37:07 +00002416 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002417}
2418
Chris Lattner59a25942008-03-31 00:36:02 +00002419//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002420// FieldDecl Implementation
2421//===----------------------------------------------------------------------===//
2422
Jay Foad39c79802011-01-12 09:06:06 +00002423FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002424 SourceLocation StartLoc, SourceLocation IdLoc,
2425 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002426 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2427 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002428 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002429 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002430}
2431
Douglas Gregor72172e92012-01-05 21:55:30 +00002432FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2433 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2434 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2435 0, QualType(), 0, 0, false, false);
2436}
2437
Sebastian Redl833ef452010-01-26 22:01:41 +00002438bool FieldDecl::isAnonymousStructOrUnion() const {
2439 if (!isImplicit() || getDeclName())
2440 return false;
2441
2442 if (const RecordType *Record = getType()->getAs<RecordType>())
2443 return Record->getDecl()->isAnonymousStructOrUnion();
2444
2445 return false;
2446}
2447
Richard Smithcaf33902011-10-10 18:28:20 +00002448unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2449 assert(isBitField() && "not a bitfield");
2450 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2451 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2452}
2453
John McCall4e819612011-01-20 07:57:12 +00002454unsigned FieldDecl::getFieldIndex() const {
2455 if (CachedFieldIndex) return CachedFieldIndex - 1;
2456
Richard Smithd62306a2011-11-10 06:34:14 +00002457 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002458 const RecordDecl *RD = getParent();
2459 const FieldDecl *LastFD = 0;
2460 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002461
2462 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2463 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002464 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002465
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002466 if (IsMsStruct) {
2467 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie2d7c57e2012-04-30 02:36:29 +00002468 if (getASTContext().ZeroBitfieldFollowsNonBitfield(&*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002469 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002470 continue;
2471 }
David Blaikie2d7c57e2012-04-30 02:36:29 +00002472 LastFD = &*I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002473 }
John McCall4e819612011-01-20 07:57:12 +00002474 }
2475
Richard Smithd62306a2011-11-10 06:34:14 +00002476 assert(CachedFieldIndex && "failed to find field in parent");
2477 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002478}
2479
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002480SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002481 if (const Expr *E = InitializerOrBitWidth.getPointer())
2482 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002483 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002484}
2485
Richard Smith938f40b2011-06-11 17:19:42 +00002486void FieldDecl::setInClassInitializer(Expr *Init) {
2487 assert(!InitializerOrBitWidth.getPointer() &&
2488 "bit width or initializer already set");
2489 InitializerOrBitWidth.setPointer(Init);
2490 InitializerOrBitWidth.setInt(0);
2491}
2492
Sebastian Redl833ef452010-01-26 22:01:41 +00002493//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002494// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002495//===----------------------------------------------------------------------===//
2496
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002497SourceLocation TagDecl::getOuterLocStart() const {
2498 return getTemplateOrInnerLocStart(this);
2499}
2500
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002501SourceRange TagDecl::getSourceRange() const {
2502 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002503 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002504}
2505
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002506TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002507 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002508}
2509
Richard Smithdda56e42011-04-15 14:24:37 +00002510void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2511 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002512 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002513 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002514 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002515}
2516
Douglas Gregordee1be82009-01-17 00:42:38 +00002517void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002518 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002519
2520 if (isa<CXXRecordDecl>(this)) {
2521 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2522 struct CXXRecordDecl::DefinitionData *Data =
2523 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002524 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2525 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002526 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002527}
2528
2529void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002530 assert((!isa<CXXRecordDecl>(this) ||
2531 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2532 "definition completed but not started");
2533
John McCallf937c022011-10-07 06:10:15 +00002534 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002535 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002536
2537 if (ASTMutationListener *L = getASTMutationListener())
2538 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002539}
2540
John McCallf937c022011-10-07 06:10:15 +00002541TagDecl *TagDecl::getDefinition() const {
2542 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002543 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002544 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2545 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002546
2547 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002548 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002549 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002550 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002551
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002552 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002553}
2554
Douglas Gregor14454802011-02-25 02:25:35 +00002555void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2556 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002557 // Make sure the extended qualifier info is allocated.
2558 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002559 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002560 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002561 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002562 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002563 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002564 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002565 if (getExtInfo()->NumTemplParamLists == 0) {
2566 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002567 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002568 }
2569 else
2570 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002571 }
2572 }
2573}
2574
Abramo Bagnara60804e12011-03-18 15:16:37 +00002575void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2576 unsigned NumTPLists,
2577 TemplateParameterList **TPLists) {
2578 assert(NumTPLists > 0);
2579 // Make sure the extended decl info is allocated.
2580 if (!hasExtInfo())
2581 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002582 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002583 // Set the template parameter lists info.
2584 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2585}
2586
Ted Kremenek21475702008-09-05 17:16:31 +00002587//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002588// EnumDecl Implementation
2589//===----------------------------------------------------------------------===//
2590
David Blaikie68e081d2011-12-20 02:48:34 +00002591void EnumDecl::anchor() { }
2592
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002593EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2594 SourceLocation StartLoc, SourceLocation IdLoc,
2595 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002596 EnumDecl *PrevDecl, bool IsScoped,
2597 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002598 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002599 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002600 C.getTypeDeclType(Enum, PrevDecl);
2601 return Enum;
2602}
2603
Douglas Gregor72172e92012-01-05 21:55:30 +00002604EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2605 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2606 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2607 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002608}
2609
Douglas Gregord5058122010-02-11 01:19:42 +00002610void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002611 QualType NewPromotionType,
2612 unsigned NumPositiveBits,
2613 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002614 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002615 if (!IntegerType)
2616 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002617 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002618 setNumPositiveBits(NumPositiveBits);
2619 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002620 TagDecl::completeDefinition();
2621}
2622
Richard Smith7d137e32012-03-23 03:33:32 +00002623TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2624 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2625 return MSI->getTemplateSpecializationKind();
2626
2627 return TSK_Undeclared;
2628}
2629
2630void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2631 SourceLocation PointOfInstantiation) {
2632 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2633 assert(MSI && "Not an instantiated member enumeration?");
2634 MSI->setTemplateSpecializationKind(TSK);
2635 if (TSK != TSK_ExplicitSpecialization &&
2636 PointOfInstantiation.isValid() &&
2637 MSI->getPointOfInstantiation().isInvalid())
2638 MSI->setPointOfInstantiation(PointOfInstantiation);
2639}
2640
Richard Smith4b38ded2012-03-14 23:13:10 +00002641EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2642 if (SpecializationInfo)
2643 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2644
2645 return 0;
2646}
2647
2648void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2649 TemplateSpecializationKind TSK) {
2650 assert(!SpecializationInfo && "Member enum is already a specialization");
2651 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2652}
2653
Sebastian Redl833ef452010-01-26 22:01:41 +00002654//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002655// RecordDecl Implementation
2656//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002657
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002658RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2659 SourceLocation StartLoc, SourceLocation IdLoc,
2660 IdentifierInfo *Id, RecordDecl *PrevDecl)
2661 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002662 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002663 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002664 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002665 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002666 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002667}
2668
Jay Foad39c79802011-01-12 09:06:06 +00002669RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002670 SourceLocation StartLoc, SourceLocation IdLoc,
2671 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2672 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2673 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002674 C.getTypeDeclType(R, PrevDecl);
2675 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002676}
2677
Douglas Gregor72172e92012-01-05 21:55:30 +00002678RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2679 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2680 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2681 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002682}
2683
Douglas Gregordfcad112009-03-25 15:59:44 +00002684bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002685 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002686 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2687}
2688
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002689RecordDecl::field_iterator RecordDecl::field_begin() const {
2690 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2691 LoadFieldsFromExternalStorage();
2692
2693 return field_iterator(decl_iterator(FirstDecl));
2694}
2695
Douglas Gregorb11aad82011-02-19 18:51:44 +00002696/// completeDefinition - Notes that the definition of this type is now
2697/// complete.
2698void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002699 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002700 TagDecl::completeDefinition();
2701}
2702
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002703void RecordDecl::LoadFieldsFromExternalStorage() const {
2704 ExternalASTSource *Source = getASTContext().getExternalSource();
2705 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2706
2707 // Notify that we have a RecordDecl doing some initialization.
2708 ExternalASTSource::Deserializing TheFields(Source);
2709
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002710 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002711 LoadedFieldsFromExternalStorage = true;
2712 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2713 case ELR_Success:
2714 break;
2715
2716 case ELR_AlreadyLoaded:
2717 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002718 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002719 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002720
2721#ifndef NDEBUG
2722 // Check that all decls we got were FieldDecls.
2723 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2724 assert(isa<FieldDecl>(Decls[i]));
2725#endif
2726
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002727 if (Decls.empty())
2728 return;
2729
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002730 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2731 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002732}
2733
Steve Naroff415d3d52008-10-08 17:01:13 +00002734//===----------------------------------------------------------------------===//
2735// BlockDecl Implementation
2736//===----------------------------------------------------------------------===//
2737
David Blaikie9c70e042011-09-21 18:16:56 +00002738void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002739 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002740
Steve Naroffc4b30e52009-03-13 16:56:44 +00002741 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002742 if (!NewParamInfo.empty()) {
2743 NumParams = NewParamInfo.size();
2744 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2745 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002746 }
2747}
2748
John McCall351762c2011-02-07 10:33:21 +00002749void BlockDecl::setCaptures(ASTContext &Context,
2750 const Capture *begin,
2751 const Capture *end,
2752 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002753 CapturesCXXThis = capturesCXXThis;
2754
2755 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002756 NumCaptures = 0;
2757 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002758 return;
2759 }
2760
John McCall351762c2011-02-07 10:33:21 +00002761 NumCaptures = end - begin;
2762
2763 // Avoid new Capture[] because we don't want to provide a default
2764 // constructor.
2765 size_t allocationSize = NumCaptures * sizeof(Capture);
2766 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2767 memcpy(buffer, begin, allocationSize);
2768 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002769}
Sebastian Redl833ef452010-01-26 22:01:41 +00002770
John McCallce45f882011-06-15 22:51:16 +00002771bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2772 for (capture_const_iterator
2773 i = capture_begin(), e = capture_end(); i != e; ++i)
2774 // Only auto vars can be captured, so no redeclaration worries.
2775 if (i->getVariable() == variable)
2776 return true;
2777
2778 return false;
2779}
2780
Douglas Gregor70226da2010-12-21 16:27:07 +00002781SourceRange BlockDecl::getSourceRange() const {
2782 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2783}
Sebastian Redl833ef452010-01-26 22:01:41 +00002784
2785//===----------------------------------------------------------------------===//
2786// Other Decl Allocation/Deallocation Method Implementations
2787//===----------------------------------------------------------------------===//
2788
David Blaikie68e081d2011-12-20 02:48:34 +00002789void TranslationUnitDecl::anchor() { }
2790
Sebastian Redl833ef452010-01-26 22:01:41 +00002791TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2792 return new (C) TranslationUnitDecl(C);
2793}
2794
David Blaikie68e081d2011-12-20 02:48:34 +00002795void LabelDecl::anchor() { }
2796
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002797LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002798 SourceLocation IdentL, IdentifierInfo *II) {
2799 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2800}
2801
2802LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2803 SourceLocation IdentL, IdentifierInfo *II,
2804 SourceLocation GnuLabelL) {
2805 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2806 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002807}
2808
Douglas Gregor72172e92012-01-05 21:55:30 +00002809LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2810 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2811 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002812}
2813
David Blaikie68e081d2011-12-20 02:48:34 +00002814void ValueDecl::anchor() { }
2815
2816void ImplicitParamDecl::anchor() { }
2817
Sebastian Redl833ef452010-01-26 22:01:41 +00002818ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002819 SourceLocation IdLoc,
2820 IdentifierInfo *Id,
2821 QualType Type) {
2822 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002823}
2824
Douglas Gregor72172e92012-01-05 21:55:30 +00002825ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2826 unsigned ID) {
2827 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2828 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2829}
2830
Sebastian Redl833ef452010-01-26 22:01:41 +00002831FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002832 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002833 const DeclarationNameInfo &NameInfo,
2834 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002835 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002836 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002837 bool hasWrittenPrototype,
2838 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002839 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2840 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002841 isInlineSpecified,
2842 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002843 New->HasWrittenPrototype = hasWrittenPrototype;
2844 return New;
2845}
2846
Douglas Gregor72172e92012-01-05 21:55:30 +00002847FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2848 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2849 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2850 DeclarationNameInfo(), QualType(), 0,
2851 SC_None, SC_None, false, false);
2852}
2853
Sebastian Redl833ef452010-01-26 22:01:41 +00002854BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2855 return new (C) BlockDecl(DC, L);
2856}
2857
Douglas Gregor72172e92012-01-05 21:55:30 +00002858BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2859 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2860 return new (Mem) BlockDecl(0, SourceLocation());
2861}
2862
Sebastian Redl833ef452010-01-26 22:01:41 +00002863EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2864 SourceLocation L,
2865 IdentifierInfo *Id, QualType T,
2866 Expr *E, const llvm::APSInt &V) {
2867 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2868}
2869
Douglas Gregor72172e92012-01-05 21:55:30 +00002870EnumConstantDecl *
2871EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2872 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2873 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2874 llvm::APSInt());
2875}
2876
David Blaikie68e081d2011-12-20 02:48:34 +00002877void IndirectFieldDecl::anchor() { }
2878
Benjamin Kramer39593702010-11-21 14:11:41 +00002879IndirectFieldDecl *
2880IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2881 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2882 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002883 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2884}
2885
Douglas Gregor72172e92012-01-05 21:55:30 +00002886IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2887 unsigned ID) {
2888 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2889 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2890 QualType(), 0, 0);
2891}
2892
Douglas Gregorbe996932010-09-01 20:41:53 +00002893SourceRange EnumConstantDecl::getSourceRange() const {
2894 SourceLocation End = getLocation();
2895 if (Init)
2896 End = Init->getLocEnd();
2897 return SourceRange(getLocation(), End);
2898}
2899
David Blaikie68e081d2011-12-20 02:48:34 +00002900void TypeDecl::anchor() { }
2901
Sebastian Redl833ef452010-01-26 22:01:41 +00002902TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002903 SourceLocation StartLoc, SourceLocation IdLoc,
2904 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2905 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002906}
2907
David Blaikie68e081d2011-12-20 02:48:34 +00002908void TypedefNameDecl::anchor() { }
2909
Douglas Gregor72172e92012-01-05 21:55:30 +00002910TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2911 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2912 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2913}
2914
Richard Smithdda56e42011-04-15 14:24:37 +00002915TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2916 SourceLocation StartLoc,
2917 SourceLocation IdLoc, IdentifierInfo *Id,
2918 TypeSourceInfo *TInfo) {
2919 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2920}
2921
Douglas Gregor72172e92012-01-05 21:55:30 +00002922TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2923 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2924 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2925}
2926
Abramo Bagnaraea947882011-03-08 16:41:52 +00002927SourceRange TypedefDecl::getSourceRange() const {
2928 SourceLocation RangeEnd = getLocation();
2929 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2930 if (typeIsPostfix(TInfo->getType()))
2931 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2932 }
2933 return SourceRange(getLocStart(), RangeEnd);
2934}
2935
Richard Smithdda56e42011-04-15 14:24:37 +00002936SourceRange TypeAliasDecl::getSourceRange() const {
2937 SourceLocation RangeEnd = getLocStart();
2938 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2939 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2940 return SourceRange(getLocStart(), RangeEnd);
2941}
2942
David Blaikie68e081d2011-12-20 02:48:34 +00002943void FileScopeAsmDecl::anchor() { }
2944
Sebastian Redl833ef452010-01-26 22:01:41 +00002945FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002946 StringLiteral *Str,
2947 SourceLocation AsmLoc,
2948 SourceLocation RParenLoc) {
2949 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002950}
Douglas Gregorba345522011-12-02 23:23:56 +00002951
Douglas Gregor72172e92012-01-05 21:55:30 +00002952FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2953 unsigned ID) {
2954 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2955 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2956}
2957
Douglas Gregorba345522011-12-02 23:23:56 +00002958//===----------------------------------------------------------------------===//
2959// ImportDecl Implementation
2960//===----------------------------------------------------------------------===//
2961
2962/// \brief Retrieve the number of module identifiers needed to name the given
2963/// module.
2964static unsigned getNumModuleIdentifiers(Module *Mod) {
2965 unsigned Result = 1;
2966 while (Mod->Parent) {
2967 Mod = Mod->Parent;
2968 ++Result;
2969 }
2970 return Result;
2971}
2972
Douglas Gregor22d09742012-01-03 18:04:46 +00002973ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002974 Module *Imported,
2975 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00002976 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002977 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002978{
2979 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
2980 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
2981 memcpy(StoredLocs, IdentifierLocs.data(),
2982 IdentifierLocs.size() * sizeof(SourceLocation));
2983}
2984
Douglas Gregor22d09742012-01-03 18:04:46 +00002985ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002986 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00002987 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002988 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002989{
2990 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
2991}
2992
2993ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00002994 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00002995 ArrayRef<SourceLocation> IdentifierLocs) {
2996 void *Mem = C.Allocate(sizeof(ImportDecl) +
2997 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00002998 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00002999}
3000
3001ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003002 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003003 Module *Imported,
3004 SourceLocation EndLoc) {
3005 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003006 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003007 Import->setImplicit();
3008 return Import;
3009}
3010
Douglas Gregor72172e92012-01-05 21:55:30 +00003011ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3012 unsigned NumLocations) {
3013 void *Mem = AllocateDeserializedDecl(C, ID,
3014 (sizeof(ImportDecl) +
3015 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003016 return new (Mem) ImportDecl(EmptyShell());
3017}
3018
3019ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3020 if (!ImportedAndComplete.getInt())
3021 return ArrayRef<SourceLocation>();
3022
3023 const SourceLocation *StoredLocs
3024 = reinterpret_cast<const SourceLocation *>(this + 1);
3025 return ArrayRef<SourceLocation>(StoredLocs,
3026 getNumModuleIdentifiers(getImportedModule()));
3027}
3028
3029SourceRange ImportDecl::getSourceRange() const {
3030 if (!ImportedAndComplete.getInt())
3031 return SourceRange(getLocation(),
3032 *reinterpret_cast<const SourceLocation *>(this + 1));
3033
3034 return SourceRange(getLocation(), getIdentifierLocs().back());
3035}