blob: 21405d223d156c32a66bda2414a0abbfbbc798af [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
Rafael Espindola7f90b7d2012-05-15 14:09:55 +0000161static bool shouldConsiderTemplateLV(const FunctionDecl *fn) {
162 return !fn->hasAttr<VisibilityAttr>();
John McCallb8c604a2011-06-27 23:06:04 +0000163}
164
165static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola7f90b7d2012-05-15 14:09:55 +0000166 return !d->hasAttr<VisibilityAttr>();
John McCallb8c604a2011-06-27 23:06:04 +0000167}
168
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000169static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
170 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000171 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000172 "Not a name having namespace scope");
173 ASTContext &Context = D->getASTContext();
174
175 // C++ [basic.link]p3:
176 // A name having namespace scope (3.3.6) has internal linkage if it
177 // is the name of
178 // - an object, reference, function or function template that is
179 // explicitly declared static; or,
180 // (This bullet corresponds to C99 6.2.2p3.)
181 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
182 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000183 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000184 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000185
186 // - an object or reference that is explicitly declared const
187 // and neither explicitly declared extern nor previously
188 // declared to have external linkage; or
189 // (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000190 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000191 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000192 Var->getStorageClass() != SC_Extern &&
193 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000194 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000195 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000196 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000197 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000198 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000199 FoundExtern = true;
200
201 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000202 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000203 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000204 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000205 const VarDecl *PrevVar = Var->getPreviousDecl();
206 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000207 if (PrevVar->getStorageClass() == SC_PrivateExtern)
208 break;
209 if (PrevVar)
210 return PrevVar->getLinkageAndVisibility();
211 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000212 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000213 // C++ [temp]p4:
214 // A non-member function template can have internal linkage; any
215 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000216 const FunctionDecl *Function = 0;
217 if (const FunctionTemplateDecl *FunTmpl
218 = dyn_cast<FunctionTemplateDecl>(D))
219 Function = FunTmpl->getTemplatedDecl();
220 else
221 Function = cast<FunctionDecl>(D);
222
223 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000224 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000225 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000226 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
227 // - a data member of an anonymous union.
228 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000229 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 }
231
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000232 if (D->isInAnonymousNamespace()) {
233 const VarDecl *Var = dyn_cast<VarDecl>(D);
234 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000235 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
236 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000237 return LinkageInfo::uniqueExternal();
238 }
John McCallb7139c42010-10-28 04:18:25 +0000239
John McCall457a04e2010-10-22 21:05:15 +0000240 // Set up the defaults.
241
242 // C99 6.2.2p5:
243 // If the declaration of an identifier for an object has file
244 // scope and no storage-class specifier, its linkage is
245 // external.
John McCallc273f242010-10-30 11:50:40 +0000246 LinkageInfo LV;
247
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000248 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000249 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000250 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000251 } else {
252 // If we're declared in a namespace with a visibility attribute,
253 // use that namespace's visibility, but don't call it explicit.
254 for (const DeclContext *DC = D->getDeclContext();
255 !isa<TranslationUnitDecl>(DC);
256 DC = DC->getParent()) {
257 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
258 if (!ND) continue;
259 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000260 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000261 break;
262 }
263 }
264 }
265 }
266
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000267 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000268 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000269
Douglas Gregorf73b2822009-11-25 22:24:25 +0000270 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000271
Douglas Gregorf73b2822009-11-25 22:24:25 +0000272 // A name having namespace scope has external linkage if it is the
273 // name of
274 //
275 // - an object or reference, unless it has internal linkage; or
276 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000277 // GCC applies the following optimization to variables and static
278 // data members, but not to functions:
279 //
John McCall457a04e2010-10-22 21:05:15 +0000280 // Modify the variable's LV by the LV of its type unless this is
281 // C or extern "C". This follows from [basic.link]p9:
282 // A type without linkage shall not be used as the type of a
283 // variable or function with external linkage unless
284 // - the entity has C language linkage, or
285 // - the entity is declared within an unnamed namespace, or
286 // - the entity is not used or is defined in the same
287 // translation unit.
288 // and [basic.link]p10:
289 // ...the types specified by all declarations referring to a
290 // given variable or function shall be identical...
291 // C does not have an equivalent rule.
292 //
John McCall5fe84122010-10-26 04:59:26 +0000293 // Ignore this if we've got an explicit attribute; the user
294 // probably knows what they're doing.
295 //
John McCall457a04e2010-10-22 21:05:15 +0000296 // Note that we don't want to make the variable non-external
297 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000298 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000299 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000300 LinkageInfo TypeLV = getLVForType(Var->getType());
301 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000302 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000303 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000304 }
305
John McCall23032652010-11-02 18:38:13 +0000306 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000307 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000308
David Blaikiebbafb8a2012-03-11 07:00:24 +0000309 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000310 (Var->getStorageClass() == SC_Extern ||
311 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000312
Douglas Gregorf73b2822009-11-25 22:24:25 +0000313 // C99 6.2.2p4:
314 // For an identifier declared with the storage-class specifier
315 // extern in a scope in which a prior declaration of that
316 // identifier is visible, if the prior declaration specifies
317 // internal or external linkage, the linkage of the identifier
318 // at the later declaration is the same as the linkage
319 // specified at the prior declaration. If no prior declaration
320 // is visible, or if the prior declaration specifies no
321 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000322 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000323 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000324 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
325 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000326 }
327 }
328
Douglas Gregorf73b2822009-11-25 22:24:25 +0000329 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000330 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000331 // In theory, we can modify the function's LV by the LV of its
332 // type unless it has C linkage (see comment above about variables
333 // for justification). In practice, GCC doesn't do this, so it's
334 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000335
John McCall23032652010-11-02 18:38:13 +0000336 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000337 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000338
Douglas Gregorf73b2822009-11-25 22:24:25 +0000339 // C99 6.2.2p5:
340 // If the declaration of an identifier for a function has no
341 // storage-class specifier, its linkage is determined exactly
342 // as if it were declared with the storage-class specifier
343 // extern.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000344 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000345 (Function->getStorageClass() == SC_Extern ||
346 Function->getStorageClass() == SC_PrivateExtern ||
347 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000348 // C99 6.2.2p4:
349 // For an identifier declared with the storage-class specifier
350 // extern in a scope in which a prior declaration of that
351 // identifier is visible, if the prior declaration specifies
352 // internal or external linkage, the linkage of the identifier
353 // at the later declaration is the same as the linkage
354 // specified at the prior declaration. If no prior declaration
355 // is visible, or if the prior declaration specifies no
356 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000357 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000358 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000359 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
360 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000361 }
362 }
363
John McCallf768aa72011-02-10 06:50:24 +0000364 // In C++, then if the type of the function uses a type with
365 // unique-external linkage, it's not legally usable from outside
366 // this translation unit. However, we should use the C linkage
367 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000368 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000369 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000370 Function->getType()->getLinkage() == UniqueExternalLinkage)
371 return LinkageInfo::uniqueExternal();
372
John McCallb8c604a2011-06-27 23:06:04 +0000373 // Consider LV from the template and the template arguments unless
374 // this is an explicit specialization with a visibility attribute.
375 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000376 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola7f90b7d2012-05-15 14:09:55 +0000377 if (shouldConsiderTemplateLV(Function)) {
John McCallb8c604a2011-06-27 23:06:04 +0000378 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000379 true));
John McCallb8c604a2011-06-27 23:06:04 +0000380 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000381 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs,
382 OnlyTemplate));
John McCallb8c604a2011-06-27 23:06:04 +0000383 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000384 }
385
Douglas Gregorf73b2822009-11-25 22:24:25 +0000386 // - a named class (Clause 9), or an unnamed class defined in a
387 // typedef declaration in which the class has the typedef name
388 // for linkage purposes (7.1.3); or
389 // - a named enumeration (7.2), or an unnamed enumeration
390 // defined in a typedef declaration in which the enumeration
391 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000392 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
393 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000394 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000395 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000396
John McCall457a04e2010-10-22 21:05:15 +0000397 // If this is a class template specialization, consider the
398 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000399 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000400 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCallb8c604a2011-06-27 23:06:04 +0000401 if (shouldConsiderTemplateLV(spec)) {
402 // From the template.
403 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000404 true));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000405
John McCallb8c604a2011-06-27 23:06:04 +0000406 // The arguments at which the template was instantiated.
407 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola7f90b7d2012-05-15 14:09:55 +0000408 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs,
409 OnlyTemplate));
John McCallb8c604a2011-06-27 23:06:04 +0000410 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000411 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000412
413 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000414 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000415 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
416 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000417 if (!isExternalLinkage(EnumLV.linkage()))
418 return LinkageInfo::none();
419 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000420
421 // - a template, unless it is a function template that has
422 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000423 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000424 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000425 // - a namespace (7.3), unless it is declared within an unnamed
426 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000427 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
428 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000429
John McCall457a04e2010-10-22 21:05:15 +0000430 // By extension, we assign external linkage to Objective-C
431 // interfaces.
432 } else if (isa<ObjCInterfaceDecl>(D)) {
433 // fallout
434
435 // Everything not covered here has no linkage.
436 } else {
John McCallc273f242010-10-30 11:50:40 +0000437 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000438 }
439
440 // If we ended up with non-external linkage, visibility should
441 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000442 if (LV.linkage() != ExternalLinkage)
443 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000444
John McCall457a04e2010-10-22 21:05:15 +0000445 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000446}
447
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000448static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000449 // Only certain class members have linkage. Note that fields don't
450 // really have linkage, but it's convenient to say they do for the
451 // purposes of calculating linkage of pointer-to-data-member
452 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000453 if (!(isa<CXXMethodDecl>(D) ||
454 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000455 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000456 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000457 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000458 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000459
John McCall07072662010-11-02 01:45:15 +0000460 LinkageInfo LV;
461
John McCall07072662010-11-02 01:45:15 +0000462 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000463 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000464 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000465 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000466 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000467
468 // If this class member has an explicit visibility attribute, the only
469 // thing that can change its visibility is the template arguments, so
470 // only look for them when processing the the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000471 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000472
473 // If we're paying attention to global visibility, apply
474 // -finline-visibility-hidden if this is an inline method.
475 //
476 // Note that we do this before merging information about
477 // the class visibility.
478 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
479 TemplateSpecializationKind TSK = TSK_Undeclared;
480 if (FunctionTemplateSpecializationInfo *spec
481 = MD->getTemplateSpecializationInfo()) {
482 TSK = spec->getTemplateSpecializationKind();
483 } else if (MemberSpecializationInfo *MSI =
484 MD->getMemberSpecializationInfo()) {
485 TSK = MSI->getTemplateSpecializationKind();
486 }
487
488 const FunctionDecl *Def = 0;
489 // InlineVisibilityHidden only applies to definitions, and
490 // isInlined() only gives meaningful answers on definitions
491 // anyway.
492 if (TSK != TSK_ExplicitInstantiationDeclaration &&
493 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000494 !OnlyTemplate &&
Rafael Espindola505a7c82012-04-16 18:25:01 +0000495 !LV.visibilityExplicit() &&
496 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
497 MD->hasBody(Def) && Def->isInlined())
498 LV.mergeVisibility(HiddenVisibility, true);
499 }
John McCallc273f242010-10-30 11:50:40 +0000500
Rafael Espindola53cf2192012-04-19 05:50:08 +0000501 // If this member has an visibility attribute, ClassF will exclude
502 // attributes on the class or command line options, keeping only information
503 // about the template instantiation. If the member has no visibility
504 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
505 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000506 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
507 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000508 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000509 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000510
511 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000512 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000513 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000514
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000515 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000516 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000517
John McCall8823c652010-08-13 08:35:10 +0000518 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000519 // If the type of the function uses a type with unique-external
520 // linkage, it's not legally usable from outside this translation unit.
521 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
522 return LinkageInfo::uniqueExternal();
523
John McCall457a04e2010-10-22 21:05:15 +0000524 // If this is a method template specialization, use the linkage for
525 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000526 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000527 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola7f90b7d2012-05-15 14:09:55 +0000528 if (shouldConsiderTemplateLV(MD)) {
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000529 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000530 OnlyTemplate));
531 if (!OnlyTemplate)
John McCallb8c604a2011-06-27 23:06:04 +0000532 LV.merge(getLVForTemplateParameterList(
533 spec->getTemplate()->getTemplateParameters()));
534 }
John McCalle6e622e2010-11-01 01:29:57 +0000535 }
John McCall457a04e2010-10-22 21:05:15 +0000536
John McCall37bb6c92010-10-29 22:22:43 +0000537 // Note that in contrast to basically every other situation, we
538 // *do* apply -fvisibility to method declarations.
539
540 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000541 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000542 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-06-27 23:06:04 +0000543 if (shouldConsiderTemplateLV(spec)) {
544 // Merge template argument/parameter information for member
545 // class template specializations.
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000546 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000547 OnlyTemplate));
548 if (!OnlyTemplate)
John McCall8bc6d5b2011-03-04 10:39:25 +0000549 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000550 spec->getSpecializedTemplate()->getTemplateParameters()));
551 }
John McCall37bb6c92010-10-29 22:22:43 +0000552 }
553
John McCall37bb6c92010-10-29 22:22:43 +0000554 // Static data members.
555 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000556 // Modify the variable's linkage by its type, but ignore the
557 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000558 LinkageInfo TypeLV = getLVForType(VD->getType());
559 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000560 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000561 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000562 }
563
John McCall457a04e2010-10-22 21:05:15 +0000564 return LV;
John McCall8823c652010-08-13 08:35:10 +0000565}
566
John McCalld396b972011-02-08 19:01:05 +0000567static void clearLinkageForClass(const CXXRecordDecl *record) {
568 for (CXXRecordDecl::decl_iterator
569 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
570 Decl *child = *i;
571 if (isa<NamedDecl>(child))
572 cast<NamedDecl>(child)->ClearLinkageCache();
573 }
574}
575
David Blaikie68e081d2011-12-20 02:48:34 +0000576void NamedDecl::anchor() { }
577
John McCalld396b972011-02-08 19:01:05 +0000578void NamedDecl::ClearLinkageCache() {
579 // Note that we can't skip clearing the linkage of children just
580 // because the parent doesn't have cached linkage: we don't cache
581 // when computing linkage for parent contexts.
582
583 HasCachedLinkage = 0;
584
585 // If we're changing the linkage of a class, we need to reset the
586 // linkage of child declarations, too.
587 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
588 clearLinkageForClass(record);
589
John McCall83779672011-02-19 02:53:41 +0000590 if (ClassTemplateDecl *temp =
591 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000592 // Clear linkage for the template pattern.
593 CXXRecordDecl *record = temp->getTemplatedDecl();
594 record->HasCachedLinkage = 0;
595 clearLinkageForClass(record);
596
John McCall83779672011-02-19 02:53:41 +0000597 // We need to clear linkage for specializations, too.
598 for (ClassTemplateDecl::spec_iterator
599 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
600 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000601 }
John McCall83779672011-02-19 02:53:41 +0000602
603 // Clear cached linkage for function template decls, too.
604 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000605 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
606 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000607 for (FunctionTemplateDecl::spec_iterator
608 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
609 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000610 }
John McCall83779672011-02-19 02:53:41 +0000611
John McCalld396b972011-02-08 19:01:05 +0000612}
613
Douglas Gregorbf62d642010-12-06 18:36:25 +0000614Linkage NamedDecl::getLinkage() const {
615 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000616 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000617 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000618 return Linkage(CachedLinkage);
619 }
620
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000621 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000622 HasCachedLinkage = 1;
623 return Linkage(CachedLinkage);
624}
625
John McCallc273f242010-10-30 11:50:40 +0000626LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000627 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000628 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000629 HasCachedLinkage = 1;
630 CachedLinkage = LI.linkage();
631 return LI;
John McCall033caa52010-10-29 00:29:13 +0000632}
Ted Kremenek926d8602010-04-20 23:15:35 +0000633
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000634llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
635 // Use the most recent declaration of a variable.
636 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000637 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000638
639 // Use the most recent declaration of a function, and also handle
640 // function template specializations.
641 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
642 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000643 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000644 return V;
645
646 // If the function is a specialization of a template with an
647 // explicit visibility attribute, use that.
648 if (FunctionTemplateSpecializationInfo *templateInfo
649 = fn->getTemplateSpecializationInfo())
650 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
651
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000652 // If the function is a member of a specialization of a class template
653 // and the corresponding decl has explicit visibility, use that.
654 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
655 if (InstantiatedFrom)
656 return getVisibilityOf(InstantiatedFrom);
657
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000658 return llvm::Optional<Visibility>();
659 }
660
661 // Otherwise, just check the declaration itself first.
662 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
663 return V;
664
665 // If there wasn't explicit visibility there, and this is a
666 // specialization of a class template, check for visibility
667 // on the pattern.
668 if (const ClassTemplateSpecializationDecl *spec
669 = dyn_cast<ClassTemplateSpecializationDecl>(this))
670 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
671
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000672 // If this is a member class of a specialization of a class template
673 // and the corresponding decl has explicit visibility, use that.
674 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
675 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
676 if (InstantiatedFrom)
677 return getVisibilityOf(InstantiatedFrom);
678 }
679
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000680 return llvm::Optional<Visibility>();
681}
682
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000683static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000684 // Objective-C: treat all Objective-C declarations as having external
685 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000686 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000687 default:
688 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000689 case Decl::ParmVar:
690 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000691 case Decl::TemplateTemplateParm: // count these as external
692 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000693 case Decl::ObjCAtDefsField:
694 case Decl::ObjCCategory:
695 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000696 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000697 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000698 case Decl::ObjCMethod:
699 case Decl::ObjCProperty:
700 case Decl::ObjCPropertyImpl:
701 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000702 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000703
704 case Decl::CXXRecord: {
705 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
706 if (Record->isLambda()) {
707 if (!Record->getLambdaManglingNumber()) {
708 // This lambda has no mangling number, so it's internal.
709 return LinkageInfo::internal();
710 }
711
712 // This lambda has its linkage/visibility determined by its owner.
713 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
714 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
715 if (isa<ParmVarDecl>(ContextDecl))
716 DC = ContextDecl->getDeclContext()->getRedeclContext();
717 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000718 return getLVForDecl(cast<NamedDecl>(ContextDecl),
719 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000720 }
721
722 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000723 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000724
725 return LinkageInfo::external();
726 }
727
728 break;
729 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000730 }
731
Douglas Gregorf73b2822009-11-25 22:24:25 +0000732 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000733 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000734 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000735
736 // C++ [basic.link]p5:
737 // In addition, a member function, static data member, a named
738 // class or enumeration of class scope, or an unnamed class or
739 // enumeration defined in a class-scope typedef declaration such
740 // that the class or enumeration has the typedef name for linkage
741 // purposes (7.1.3), has external linkage if the name of the class
742 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000743 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000744 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000745
746 // C++ [basic.link]p6:
747 // The name of a function declared in block scope and the name of
748 // an object declared by a block scope extern declaration have
749 // linkage. If there is a visible declaration of an entity with
750 // linkage having the same name and type, ignoring entities
751 // declared outside the innermost enclosing namespace scope, the
752 // block scope declaration declares that same entity and receives
753 // the linkage of the previous declaration. If there is more than
754 // one such matching entity, the program is ill-formed. Otherwise,
755 // if no matching entity is found, the block scope entity receives
756 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000757 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
758 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000759 if (Function->isInAnonymousNamespace() &&
760 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000761 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000762
John McCallc273f242010-10-30 11:50:40 +0000763 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000764 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000765 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000766 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000767 }
768
Douglas Gregorec9fd132012-01-14 16:38:05 +0000769 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000770 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000771 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
772 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000773 }
774
775 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000776 }
777
John McCall033caa52010-10-29 00:29:13 +0000778 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000779 if (Var->getStorageClass() == SC_Extern ||
780 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000781 if (Var->isInAnonymousNamespace() &&
782 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000783 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000784
John McCallc273f242010-10-30 11:50:40 +0000785 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000786 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000787 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000788 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000789 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000790 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000791 }
792
Douglas Gregorec9fd132012-01-14 16:38:05 +0000793 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000794 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000795 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
796 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000797 }
798
799 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000800 }
801 }
802
803 // C++ [basic.link]p6:
804 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000805 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000806}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000807
Douglas Gregor2ada0482009-02-04 17:27:36 +0000808std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000809 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000810}
811
812std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000813 const DeclContext *Ctx = getDeclContext();
814
815 if (Ctx->isFunctionOrMethod())
816 return getNameAsString();
817
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000818 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000819 ContextsTy Contexts;
820
821 // Collect contexts.
822 while (Ctx && isa<NamedDecl>(Ctx)) {
823 Contexts.push_back(Ctx);
824 Ctx = Ctx->getParent();
825 };
826
827 std::string QualName;
828 llvm::raw_string_ostream OS(QualName);
829
830 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
831 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000832 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000833 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000834 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
835 std::string TemplateArgsStr
836 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000837 TemplateArgs.data(),
838 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000839 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000840 OS << Spec->getName() << TemplateArgsStr;
841 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000842 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000843 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000844 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000845 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000846 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
847 if (!RD->getIdentifier())
848 OS << "<anonymous " << RD->getKindName() << '>';
849 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000850 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000851 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000852 const FunctionProtoType *FT = 0;
853 if (FD->hasWrittenPrototype())
854 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
855
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000856 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000857 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000858 unsigned NumParams = FD->getNumParams();
859 for (unsigned i = 0; i < NumParams; ++i) {
860 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000861 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000862 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000863 }
864
865 if (FT->isVariadic()) {
866 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000867 OS << ", ";
868 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000869 }
870 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000871 OS << ')';
872 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000873 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000874 }
875 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000876 }
877
John McCalla2a3f7d2010-03-16 21:48:18 +0000878 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000879 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000880 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000881 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000882
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000883 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000884}
885
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000886bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000887 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
888
Douglas Gregor889ceb72009-02-03 19:21:40 +0000889 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
890 // We want to keep it, unless it nominates same namespace.
891 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000892 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
893 ->getOriginalNamespace() ==
894 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
895 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000896 }
Mike Stump11289f42009-09-09 15:08:12 +0000897
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000898 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
899 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000900 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000901
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000902 // For function templates, the underlying function declarations are linked.
903 if (const FunctionTemplateDecl *FunctionTemplate
904 = dyn_cast<FunctionTemplateDecl>(this))
905 if (const FunctionTemplateDecl *OldFunctionTemplate
906 = dyn_cast<FunctionTemplateDecl>(OldD))
907 return FunctionTemplate->getTemplatedDecl()
908 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000909
Steve Naroffc4173fa2009-02-22 19:35:57 +0000910 // For method declarations, we keep track of redeclarations.
911 if (isa<ObjCMethodDecl>(this))
912 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000913
John McCall9f3059a2009-10-09 21:13:30 +0000914 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
915 return true;
916
John McCall3f746822009-11-17 05:59:44 +0000917 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
918 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
919 cast<UsingShadowDecl>(OldD)->getTargetDecl();
920
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000921 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
922 ASTContext &Context = getASTContext();
923 return Context.getCanonicalNestedNameSpecifier(
924 cast<UsingDecl>(this)->getQualifier()) ==
925 Context.getCanonicalNestedNameSpecifier(
926 cast<UsingDecl>(OldD)->getQualifier());
927 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000928
Douglas Gregorb59643b2012-01-03 23:26:26 +0000929 // A typedef of an Objective-C class type can replace an Objective-C class
930 // declaration or definition, and vice versa.
931 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
932 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
933 return true;
934
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000935 // For non-function declarations, if the declarations are of the
936 // same kind then this must be a redeclaration, or semantic analysis
937 // would not have given us the new declaration.
938 return this->getKind() == OldD->getKind();
939}
940
Douglas Gregoreddf4332009-02-24 20:03:32 +0000941bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000942 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000943}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000944
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000945NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000946 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000947 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
948 ND = UD->getTargetDecl();
949
950 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
951 return AD->getClassInterface();
952
953 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000954}
955
John McCalla8ae2222010-04-06 21:38:20 +0000956bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000957 if (!isCXXClassMember())
958 return false;
959
John McCalla8ae2222010-04-06 21:38:20 +0000960 const NamedDecl *D = this;
961 if (isa<UsingShadowDecl>(D))
962 D = cast<UsingShadowDecl>(D)->getTargetDecl();
963
Francois Pichet783dd6e2010-11-21 06:08:52 +0000964 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000965 return true;
966 if (isa<CXXMethodDecl>(D))
967 return cast<CXXMethodDecl>(D)->isInstance();
968 if (isa<FunctionTemplateDecl>(D))
969 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
970 ->getTemplatedDecl())->isInstance();
971 return false;
972}
973
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000974//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000975// DeclaratorDecl Implementation
976//===----------------------------------------------------------------------===//
977
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000978template <typename DeclT>
979static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
980 if (decl->getNumTemplateParameterLists() > 0)
981 return decl->getTemplateParameterList(0)->getTemplateLoc();
982 else
983 return decl->getInnerLocStart();
984}
985
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000986SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000987 TypeSourceInfo *TSI = getTypeSourceInfo();
988 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000989 return SourceLocation();
990}
991
Douglas Gregor14454802011-02-25 02:25:35 +0000992void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
993 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +0000994 // Make sure the extended decl info is allocated.
995 if (!hasExtInfo()) {
996 // Save (non-extended) type source info pointer.
997 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
998 // Allocate external info struct.
999 DeclInfo = new (getASTContext()) ExtInfo;
1000 // Restore savedTInfo into (extended) decl info.
1001 getExtInfo()->TInfo = savedTInfo;
1002 }
1003 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001004 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001005 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001006 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001007 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001008 if (getExtInfo()->NumTemplParamLists == 0) {
1009 // Save type source info pointer.
1010 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1011 // Deallocate the extended decl info.
1012 getASTContext().Deallocate(getExtInfo());
1013 // Restore savedTInfo into (non-extended) decl info.
1014 DeclInfo = savedTInfo;
1015 }
1016 else
1017 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001018 }
1019 }
1020}
1021
Abramo Bagnara60804e12011-03-18 15:16:37 +00001022void
1023DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1024 unsigned NumTPLists,
1025 TemplateParameterList **TPLists) {
1026 assert(NumTPLists > 0);
1027 // Make sure the extended decl info is allocated.
1028 if (!hasExtInfo()) {
1029 // Save (non-extended) type source info pointer.
1030 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1031 // Allocate external info struct.
1032 DeclInfo = new (getASTContext()) ExtInfo;
1033 // Restore savedTInfo into (extended) decl info.
1034 getExtInfo()->TInfo = savedTInfo;
1035 }
1036 // Set the template parameter lists info.
1037 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1038}
1039
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001040SourceLocation DeclaratorDecl::getOuterLocStart() const {
1041 return getTemplateOrInnerLocStart(this);
1042}
1043
Abramo Bagnaraea947882011-03-08 16:41:52 +00001044namespace {
1045
1046// Helper function: returns true if QT is or contains a type
1047// having a postfix component.
1048bool typeIsPostfix(clang::QualType QT) {
1049 while (true) {
1050 const Type* T = QT.getTypePtr();
1051 switch (T->getTypeClass()) {
1052 default:
1053 return false;
1054 case Type::Pointer:
1055 QT = cast<PointerType>(T)->getPointeeType();
1056 break;
1057 case Type::BlockPointer:
1058 QT = cast<BlockPointerType>(T)->getPointeeType();
1059 break;
1060 case Type::MemberPointer:
1061 QT = cast<MemberPointerType>(T)->getPointeeType();
1062 break;
1063 case Type::LValueReference:
1064 case Type::RValueReference:
1065 QT = cast<ReferenceType>(T)->getPointeeType();
1066 break;
1067 case Type::PackExpansion:
1068 QT = cast<PackExpansionType>(T)->getPattern();
1069 break;
1070 case Type::Paren:
1071 case Type::ConstantArray:
1072 case Type::DependentSizedArray:
1073 case Type::IncompleteArray:
1074 case Type::VariableArray:
1075 case Type::FunctionProto:
1076 case Type::FunctionNoProto:
1077 return true;
1078 }
1079 }
1080}
1081
1082} // namespace
1083
1084SourceRange DeclaratorDecl::getSourceRange() const {
1085 SourceLocation RangeEnd = getLocation();
1086 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1087 if (typeIsPostfix(TInfo->getType()))
1088 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1089 }
1090 return SourceRange(getOuterLocStart(), RangeEnd);
1091}
1092
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001093void
Douglas Gregor20527e22010-06-15 17:44:38 +00001094QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1095 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001096 TemplateParameterList **TPLists) {
1097 assert((NumTPLists == 0 || TPLists != 0) &&
1098 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001099
1100 // Free previous template parameters (if any).
1101 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001102 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001103 TemplParamLists = 0;
1104 NumTemplParamLists = 0;
1105 }
1106 // Set info on matched template parameter lists (if any).
1107 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001108 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001109 NumTemplParamLists = NumTPLists;
1110 for (unsigned i = NumTPLists; i-- > 0; )
1111 TemplParamLists[i] = TPLists[i];
1112 }
1113}
1114
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001115//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001116// VarDecl Implementation
1117//===----------------------------------------------------------------------===//
1118
Sebastian Redl833ef452010-01-26 22:01:41 +00001119const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1120 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001121 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001122 case SC_Auto: return "auto";
1123 case SC_Extern: return "extern";
1124 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1125 case SC_PrivateExtern: return "__private_extern__";
1126 case SC_Register: return "register";
1127 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001128 }
1129
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001130 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001131}
1132
Abramo Bagnaradff19302011-03-08 08:55:46 +00001133VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1134 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001135 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001136 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001137 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001138}
1139
Douglas Gregor72172e92012-01-05 21:55:30 +00001140VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1141 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1142 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1143 QualType(), 0, SC_None, SC_None);
1144}
1145
Douglas Gregorbf62d642010-12-06 18:36:25 +00001146void VarDecl::setStorageClass(StorageClass SC) {
1147 assert(isLegalForVariable(SC));
1148 if (getStorageClass() != SC)
1149 ClearLinkageCache();
1150
John McCallbeaa11c2011-05-01 02:13:58 +00001151 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001152}
1153
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001154SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001155 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001156 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001157 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001158}
1159
Sebastian Redl833ef452010-01-26 22:01:41 +00001160bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001161 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001162 return false;
1163
Eli Friedman839192f2012-01-15 01:23:58 +00001164 const DeclContext *DC = getDeclContext();
1165 if (DC->isRecord())
1166 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001167
Eli Friedman839192f2012-01-15 01:23:58 +00001168 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001169 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001170 return true;
1171 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001172}
1173
1174VarDecl *VarDecl::getCanonicalDecl() {
1175 return getFirstDeclaration();
1176}
1177
Daniel Dunbar9d355812012-03-09 01:51:51 +00001178VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1179 ASTContext &C) const
1180{
Sebastian Redl35351a92010-01-31 22:27:38 +00001181 // C++ [basic.def]p2:
1182 // A declaration is a definition unless [...] it contains the 'extern'
1183 // specifier or a linkage-specification and neither an initializer [...],
1184 // it declares a static data member in a class declaration [...].
1185 // C++ [temp.expl.spec]p15:
1186 // An explicit specialization of a static data member of a template is a
1187 // definition if the declaration includes an initializer; otherwise, it is
1188 // a declaration.
1189 if (isStaticDataMember()) {
1190 if (isOutOfLine() && (hasInit() ||
1191 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1192 return Definition;
1193 else
1194 return DeclarationOnly;
1195 }
1196 // C99 6.7p5:
1197 // A definition of an identifier is a declaration for that identifier that
1198 // [...] causes storage to be reserved for that object.
1199 // Note: that applies for all non-file-scope objects.
1200 // C99 6.9.2p1:
1201 // If the declaration of an identifier for an object has file scope and an
1202 // initializer, the declaration is an external definition for the identifier
1203 if (hasInit())
1204 return Definition;
1205 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1206 if (hasExternalStorage())
1207 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001208
John McCall8e7d6562010-08-26 03:08:43 +00001209 if (getStorageClassAsWritten() == SC_Extern ||
1210 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001211 for (const VarDecl *PrevVar = getPreviousDecl();
1212 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001213 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1214 return DeclarationOnly;
1215 }
1216 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001217 // C99 6.9.2p2:
1218 // A declaration of an object that has file scope without an initializer,
1219 // and without a storage class specifier or the scs 'static', constitutes
1220 // a tentative definition.
1221 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001222 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001223 return TentativeDefinition;
1224
1225 // What's left is (in C, block-scope) declarations without initializers or
1226 // external storage. These are definitions.
1227 return Definition;
1228}
1229
Sebastian Redl35351a92010-01-31 22:27:38 +00001230VarDecl *VarDecl::getActingDefinition() {
1231 DefinitionKind Kind = isThisDeclarationADefinition();
1232 if (Kind != TentativeDefinition)
1233 return 0;
1234
Chris Lattner48eb14d2010-06-14 18:31:46 +00001235 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001236 VarDecl *First = getFirstDeclaration();
1237 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1238 I != E; ++I) {
1239 Kind = (*I)->isThisDeclarationADefinition();
1240 if (Kind == Definition)
1241 return 0;
1242 else if (Kind == TentativeDefinition)
1243 LastTentative = *I;
1244 }
1245 return LastTentative;
1246}
1247
1248bool VarDecl::isTentativeDefinitionNow() const {
1249 DefinitionKind Kind = isThisDeclarationADefinition();
1250 if (Kind != TentativeDefinition)
1251 return false;
1252
1253 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1254 if ((*I)->isThisDeclarationADefinition() == Definition)
1255 return false;
1256 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001257 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001258}
1259
Daniel Dunbar9d355812012-03-09 01:51:51 +00001260VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001261 VarDecl *First = getFirstDeclaration();
1262 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1263 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001264 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001265 return *I;
1266 }
1267 return 0;
1268}
1269
Daniel Dunbar9d355812012-03-09 01:51:51 +00001270VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001271 DefinitionKind Kind = DeclarationOnly;
1272
1273 const VarDecl *First = getFirstDeclaration();
1274 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001275 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001276 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001277 if (Kind == Definition)
1278 break;
1279 }
John McCall37bb6c92010-10-29 22:22:43 +00001280
1281 return Kind;
1282}
1283
Sebastian Redl5ca79842010-02-01 20:16:42 +00001284const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001285 redecl_iterator I = redecls_begin(), E = redecls_end();
1286 while (I != E && !I->getInit())
1287 ++I;
1288
1289 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001290 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001291 return I->getInit();
1292 }
1293 return 0;
1294}
1295
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001296bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001297 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001298 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001299
1300 if (!isStaticDataMember())
1301 return false;
1302
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001303 // If this static data member was instantiated from a static data member of
1304 // a class template, check whether that static data member was defined
1305 // out-of-line.
1306 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1307 return VD->isOutOfLine();
1308
1309 return false;
1310}
1311
Douglas Gregor1d957a32009-10-27 18:42:08 +00001312VarDecl *VarDecl::getOutOfLineDefinition() {
1313 if (!isStaticDataMember())
1314 return 0;
1315
1316 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1317 RD != RDEnd; ++RD) {
1318 if (RD->getLexicalDeclContext()->isFileContext())
1319 return *RD;
1320 }
1321
1322 return 0;
1323}
1324
Douglas Gregord5058122010-02-11 01:19:42 +00001325void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001326 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1327 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001328 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001329 }
1330
1331 Init = I;
1332}
1333
Daniel Dunbar9d355812012-03-09 01:51:51 +00001334bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001335 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001336
Richard Smith35ecb362012-03-02 04:14:40 +00001337 if (!Lang.CPlusPlus)
1338 return false;
1339
1340 // In C++11, any variable of reference type can be used in a constant
1341 // expression if it is initialized by a constant expression.
1342 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1343 return true;
1344
1345 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001346 // not require the variable to be non-volatile, but we consider this to be a
1347 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001348 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001349 return false;
1350
1351 // In C++, const, non-volatile variables of integral or enumeration types
1352 // can be used in constant expressions.
1353 if (getType()->isIntegralOrEnumerationType())
1354 return true;
1355
Richard Smith35ecb362012-03-02 04:14:40 +00001356 // Additionally, in C++11, non-volatile constexpr variables can be used in
1357 // constant expressions.
1358 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001359}
1360
Richard Smithd0b4dd62011-12-19 06:19:21 +00001361/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1362/// form, which contains extra information on the evaluated value of the
1363/// initializer.
1364EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1365 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1366 if (!Eval) {
1367 Stmt *S = Init.get<Stmt *>();
1368 Eval = new (getASTContext()) EvaluatedStmt;
1369 Eval->Value = S;
1370 Init = Eval;
1371 }
1372 return Eval;
1373}
1374
Richard Smithdafff942012-01-14 04:30:29 +00001375APValue *VarDecl::evaluateValue() const {
1376 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1377 return evaluateValue(Notes);
1378}
1379
1380APValue *VarDecl::evaluateValue(
1381 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001382 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1383
1384 // We only produce notes indicating why an initializer is non-constant the
1385 // first time it is evaluated. FIXME: The notes won't always be emitted the
1386 // first time we try evaluation, so might not be produced at all.
1387 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001388 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001389
1390 const Expr *Init = cast<Expr>(Eval->Value);
1391 assert(!Init->isValueDependent());
1392
1393 if (Eval->IsEvaluating) {
1394 // FIXME: Produce a diagnostic for self-initialization.
1395 Eval->CheckedICE = true;
1396 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001397 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001398 }
1399
1400 Eval->IsEvaluating = true;
1401
1402 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1403 this, Notes);
1404
1405 // Ensure the result is an uninitialized APValue if evaluation fails.
1406 if (!Result)
1407 Eval->Evaluated = APValue();
1408
1409 Eval->IsEvaluating = false;
1410 Eval->WasEvaluated = true;
1411
1412 // In C++11, we have determined whether the initializer was a constant
1413 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001414 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001415 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001416 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001417 }
1418
Richard Smithdafff942012-01-14 04:30:29 +00001419 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001420}
1421
1422bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001423 // Initializers of weak variables are never ICEs.
1424 if (isWeak())
1425 return false;
1426
Richard Smithd0b4dd62011-12-19 06:19:21 +00001427 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1428 if (Eval->CheckedICE)
1429 // We have already checked whether this subexpression is an
1430 // integral constant expression.
1431 return Eval->IsICE;
1432
1433 const Expr *Init = cast<Expr>(Eval->Value);
1434 assert(!Init->isValueDependent());
1435
1436 // In C++11, evaluate the initializer to check whether it's a constant
1437 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001438 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001439 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1440 evaluateValue(Notes);
1441 return Eval->IsICE;
1442 }
1443
1444 // It's an ICE whether or not the definition we found is
1445 // out-of-line. See DR 721 and the discussion in Clang PR
1446 // 6206 for details.
1447
1448 if (Eval->CheckingICE)
1449 return false;
1450 Eval->CheckingICE = true;
1451
1452 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1453 Eval->CheckingICE = false;
1454 Eval->CheckedICE = true;
1455 return Eval->IsICE;
1456}
1457
Douglas Gregorfe314812011-06-21 17:03:29 +00001458bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001459 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001460
1461 const Expr *E = getInit();
1462 if (!E)
1463 return false;
1464
1465 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1466 E = Cleanups->getSubExpr();
1467
1468 return isa<MaterializeTemporaryExpr>(E);
1469}
1470
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001471VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001472 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001473 return cast<VarDecl>(MSI->getInstantiatedFrom());
1474
1475 return 0;
1476}
1477
Douglas Gregor3c74d412009-10-14 20:14:33 +00001478TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001479 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001480 return MSI->getTemplateSpecializationKind();
1481
1482 return TSK_Undeclared;
1483}
1484
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001485MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001486 return getASTContext().getInstantiatedFromStaticDataMember(this);
1487}
1488
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001489void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1490 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001491 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001492 assert(MSI && "Not an instantiated static data member?");
1493 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001494 if (TSK != TSK_ExplicitSpecialization &&
1495 PointOfInstantiation.isValid() &&
1496 MSI->getPointOfInstantiation().isInvalid())
1497 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001498}
1499
Sebastian Redl833ef452010-01-26 22:01:41 +00001500//===----------------------------------------------------------------------===//
1501// ParmVarDecl Implementation
1502//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001503
Sebastian Redl833ef452010-01-26 22:01:41 +00001504ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001505 SourceLocation StartLoc,
1506 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001507 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001508 StorageClass S, StorageClass SCAsWritten,
1509 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001510 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001511 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001512}
1513
Douglas Gregor72172e92012-01-05 21:55:30 +00001514ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1515 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1516 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1517 0, QualType(), 0, SC_None, SC_None, 0);
1518}
1519
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001520SourceRange ParmVarDecl::getSourceRange() const {
1521 if (!hasInheritedDefaultArg()) {
1522 SourceRange ArgRange = getDefaultArgRange();
1523 if (ArgRange.isValid())
1524 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1525 }
1526
1527 return DeclaratorDecl::getSourceRange();
1528}
1529
Sebastian Redl833ef452010-01-26 22:01:41 +00001530Expr *ParmVarDecl::getDefaultArg() {
1531 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1532 assert(!hasUninstantiatedDefaultArg() &&
1533 "Default argument is not yet instantiated!");
1534
1535 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001536 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001537 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001538
Sebastian Redl833ef452010-01-26 22:01:41 +00001539 return Arg;
1540}
1541
Sebastian Redl833ef452010-01-26 22:01:41 +00001542SourceRange ParmVarDecl::getDefaultArgRange() const {
1543 if (const Expr *E = getInit())
1544 return E->getSourceRange();
1545
1546 if (hasUninstantiatedDefaultArg())
1547 return getUninstantiatedDefaultArg()->getSourceRange();
1548
1549 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001550}
1551
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001552bool ParmVarDecl::isParameterPack() const {
1553 return isa<PackExpansionType>(getType());
1554}
1555
Ted Kremenek540017e2011-10-06 05:00:56 +00001556void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1557 getASTContext().setParameterIndex(this, parameterIndex);
1558 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1559}
1560
1561unsigned ParmVarDecl::getParameterIndexLarge() const {
1562 return getASTContext().getParameterIndex(this);
1563}
1564
Nuno Lopes394ec982008-12-17 23:39:55 +00001565//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001566// FunctionDecl Implementation
1567//===----------------------------------------------------------------------===//
1568
Douglas Gregorb11aad82011-02-19 18:51:44 +00001569void FunctionDecl::getNameForDiagnostic(std::string &S,
1570 const PrintingPolicy &Policy,
1571 bool Qualified) const {
1572 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1573 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1574 if (TemplateArgs)
1575 S += TemplateSpecializationType::PrintTemplateArgumentList(
1576 TemplateArgs->data(),
1577 TemplateArgs->size(),
1578 Policy);
1579
1580}
1581
Ted Kremenek186a0742010-04-29 16:49:01 +00001582bool FunctionDecl::isVariadic() const {
1583 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1584 return FT->isVariadic();
1585 return false;
1586}
1587
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001588bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1589 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001590 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001591 Definition = *I;
1592 return true;
1593 }
1594 }
1595
1596 return false;
1597}
1598
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001599bool FunctionDecl::hasTrivialBody() const
1600{
1601 Stmt *S = getBody();
1602 if (!S) {
1603 // Since we don't have a body for this function, we don't know if it's
1604 // trivial or not.
1605 return false;
1606 }
1607
1608 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1609 return true;
1610 return false;
1611}
1612
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001613bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1614 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001615 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001616 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1617 return true;
1618 }
1619 }
1620
1621 return false;
1622}
1623
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001624Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001625 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1626 if (I->Body) {
1627 Definition = *I;
1628 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001629 } else if (I->IsLateTemplateParsed) {
1630 Definition = *I;
1631 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001632 }
1633 }
1634
1635 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001636}
1637
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001638void FunctionDecl::setBody(Stmt *B) {
1639 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001640 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001641 EndRangeLoc = B->getLocEnd();
1642}
1643
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001644void FunctionDecl::setPure(bool P) {
1645 IsPure = P;
1646 if (P)
1647 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1648 Parent->markedVirtualFunctionPure();
1649}
1650
Douglas Gregor16618f22009-09-12 00:17:51 +00001651bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001652 const TranslationUnitDecl *tunit =
1653 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1654 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001655 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001656 getIdentifier() &&
1657 getIdentifier()->isStr("main");
1658}
1659
1660bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1661 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1662 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1663 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1664 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1665 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1666
1667 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1668 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1669
1670 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1671 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1672
1673 ASTContext &Context =
1674 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1675 ->getASTContext();
1676
1677 // The result type and first argument type are constant across all
1678 // these operators. The second argument must be exactly void*.
1679 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001680}
1681
Douglas Gregor16618f22009-09-12 00:17:51 +00001682bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001683 if (getLinkage() != ExternalLinkage)
1684 return false;
1685
1686 if (getAttr<OverloadableAttr>())
1687 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001688
Chandler Carruth4322a282011-02-25 00:05:02 +00001689 const DeclContext *DC = getDeclContext();
1690 if (DC->isRecord())
1691 return false;
1692
Eli Friedman839192f2012-01-15 01:23:58 +00001693 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001694 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001695 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001696
Eli Friedman839192f2012-01-15 01:23:58 +00001697 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001698}
1699
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001700bool FunctionDecl::isGlobal() const {
1701 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1702 return Method->isStatic();
1703
John McCall8e7d6562010-08-26 03:08:43 +00001704 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001705 return false;
1706
Mike Stump11289f42009-09-09 15:08:12 +00001707 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001708 DC->isNamespace();
1709 DC = DC->getParent()) {
1710 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1711 if (!Namespace->getDeclName())
1712 return false;
1713 break;
1714 }
1715 }
1716
1717 return true;
1718}
1719
Sebastian Redl833ef452010-01-26 22:01:41 +00001720void
1721FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1722 redeclarable_base::setPreviousDeclaration(PrevDecl);
1723
1724 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1725 FunctionTemplateDecl *PrevFunTmpl
1726 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1727 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1728 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1729 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001730
Axel Naumannfbc7b982011-11-08 18:21:06 +00001731 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001732 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001733}
1734
1735const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1736 return getFirstDeclaration();
1737}
1738
1739FunctionDecl *FunctionDecl::getCanonicalDecl() {
1740 return getFirstDeclaration();
1741}
1742
Douglas Gregorbf62d642010-12-06 18:36:25 +00001743void FunctionDecl::setStorageClass(StorageClass SC) {
1744 assert(isLegalForFunction(SC));
1745 if (getStorageClass() != SC)
1746 ClearLinkageCache();
1747
1748 SClass = SC;
1749}
1750
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001751/// \brief Returns a value indicating whether this function
1752/// corresponds to a builtin function.
1753///
1754/// The function corresponds to a built-in function if it is
1755/// declared at translation scope or within an extern "C" block and
1756/// its name matches with the name of a builtin. The returned value
1757/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001758/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001759/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001760unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001761 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001762 return 0;
1763
1764 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001765 if (!BuiltinID)
1766 return 0;
1767
1768 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001769 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1770 return BuiltinID;
1771
1772 // This function has the name of a known C library
1773 // function. Determine whether it actually refers to the C library
1774 // function or whether it just has the same name.
1775
Douglas Gregora908e7f2009-02-17 03:23:10 +00001776 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001777 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001778 return 0;
1779
Douglas Gregore711f702009-02-14 18:57:46 +00001780 // If this function is at translation-unit scope and we're not in
1781 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001782 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001783 getDeclContext()->isTranslationUnit())
1784 return BuiltinID;
1785
1786 // If the function is in an extern "C" linkage specification and is
1787 // not marked "overloadable", it's the real function.
1788 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001789 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001790 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001791 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001792 return BuiltinID;
1793
1794 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001795 return 0;
1796}
1797
1798
Chris Lattner47c0d002009-04-25 06:03:53 +00001799/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001800/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001801/// after it has been created.
1802unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001803 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001804 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001805 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001806 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001807
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001808}
1809
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001810void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001811 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001812 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001813 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001814
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001815 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001816 if (!NewParamInfo.empty()) {
1817 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1818 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001819 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001820}
Chris Lattner41943152007-01-25 04:52:46 +00001821
James Molloy6f8780b2012-02-29 10:24:19 +00001822void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1823 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1824
1825 if (!NewDecls.empty()) {
1826 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1827 std::copy(NewDecls.begin(), NewDecls.end(), A);
1828 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1829 }
1830}
1831
Chris Lattner58258242008-04-10 02:22:51 +00001832/// getMinRequiredArguments - Returns the minimum number of arguments
1833/// needed to call this function. This may be fewer than the number of
1834/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001835/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001836unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001837 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001838 return getNumParams();
1839
Douglas Gregor7825bf32011-01-06 22:09:01 +00001840 unsigned NumRequiredArgs = getNumParams();
1841
1842 // If the last parameter is a parameter pack, we don't need an argument for
1843 // it.
1844 if (NumRequiredArgs > 0 &&
1845 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1846 --NumRequiredArgs;
1847
1848 // If this parameter has a default argument, we don't need an argument for
1849 // it.
1850 while (NumRequiredArgs > 0 &&
1851 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001852 --NumRequiredArgs;
1853
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001854 // We might have parameter packs before the end. These can't be deduced,
1855 // but they can still handle multiple arguments.
1856 unsigned ArgIdx = NumRequiredArgs;
1857 while (ArgIdx > 0) {
1858 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1859 NumRequiredArgs = ArgIdx;
1860
1861 --ArgIdx;
1862 }
1863
Chris Lattner58258242008-04-10 02:22:51 +00001864 return NumRequiredArgs;
1865}
1866
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001867bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001868 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001869 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001870
1871 if (isa<CXXMethodDecl>(this)) {
1872 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1873 return true;
1874 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001875
1876 switch (getTemplateSpecializationKind()) {
1877 case TSK_Undeclared:
1878 case TSK_ExplicitSpecialization:
1879 return false;
1880
1881 case TSK_ImplicitInstantiation:
1882 case TSK_ExplicitInstantiationDeclaration:
1883 case TSK_ExplicitInstantiationDefinition:
1884 // Handle below.
1885 break;
1886 }
1887
1888 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001889 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001890 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001891 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001892
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001893 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001894 return PatternDecl->isInlined();
1895
1896 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001897}
1898
Eli Friedman1b125c32012-02-07 03:50:18 +00001899static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1900 // Only consider file-scope declarations in this test.
1901 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1902 return false;
1903
1904 // Only consider explicit declarations; the presence of a builtin for a
1905 // libcall shouldn't affect whether a definition is externally visible.
1906 if (Redecl->isImplicit())
1907 return false;
1908
1909 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1910 return true; // Not an inline definition
1911
1912 return false;
1913}
1914
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001915/// \brief For a function declaration in C or C++, determine whether this
1916/// declaration causes the definition to be externally visible.
1917///
Eli Friedman1b125c32012-02-07 03:50:18 +00001918/// Specifically, this determines if adding the current declaration to the set
1919/// of redeclarations of the given functions causes
1920/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001921bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1922 assert(!doesThisDeclarationHaveABody() &&
1923 "Must have a declaration without a body.");
1924
1925 ASTContext &Context = getASTContext();
1926
David Blaikiebbafb8a2012-03-11 07:00:24 +00001927 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001928 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1929 // an externally visible definition.
1930 //
1931 // FIXME: What happens if gnu_inline gets added on after the first
1932 // declaration?
1933 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1934 return false;
1935
1936 const FunctionDecl *Prev = this;
1937 bool FoundBody = false;
1938 while ((Prev = Prev->getPreviousDecl())) {
1939 FoundBody |= Prev->Body;
1940
1941 if (Prev->Body) {
1942 // If it's not the case that both 'inline' and 'extern' are
1943 // specified on the definition, then it is always externally visible.
1944 if (!Prev->isInlineSpecified() ||
1945 Prev->getStorageClassAsWritten() != SC_Extern)
1946 return false;
1947 } else if (Prev->isInlineSpecified() &&
1948 Prev->getStorageClassAsWritten() != SC_Extern) {
1949 return false;
1950 }
1951 }
1952 return FoundBody;
1953 }
1954
David Blaikiebbafb8a2012-03-11 07:00:24 +00001955 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001956 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001957
1958 // C99 6.7.4p6:
1959 // [...] If all of the file scope declarations for a function in a
1960 // translation unit include the inline function specifier without extern,
1961 // then the definition in that translation unit is an inline definition.
1962 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001963 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001964 const FunctionDecl *Prev = this;
1965 bool FoundBody = false;
1966 while ((Prev = Prev->getPreviousDecl())) {
1967 FoundBody |= Prev->Body;
1968 if (RedeclForcesDefC99(Prev))
1969 return false;
1970 }
1971 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001972}
1973
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001974/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001975/// definition will be externally visible.
1976///
1977/// Inline function definitions are always available for inlining optimizations.
1978/// However, depending on the language dialect, declaration specifiers, and
1979/// attributes, the definition of an inline function may or may not be
1980/// "externally" visible to other translation units in the program.
1981///
1982/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001983/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001984/// inline definition becomes externally visible (C99 6.7.4p6).
1985///
1986/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1987/// definition, we use the GNU semantics for inline, which are nearly the
1988/// opposite of C99 semantics. In particular, "inline" by itself will create
1989/// an externally visible symbol, but "extern inline" will not create an
1990/// externally visible symbol.
1991bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001992 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001993 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001994 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001995
David Blaikiebbafb8a2012-03-11 07:00:24 +00001996 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001997 // Note: If you change the logic here, please change
1998 // doesDeclarationForceExternallyVisibleDefinition as well.
1999 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002000 // If it's not the case that both 'inline' and 'extern' are
2001 // specified on the definition, then this inline definition is
2002 // externally visible.
2003 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2004 return true;
2005
2006 // If any declaration is 'inline' but not 'extern', then this definition
2007 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002008 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2009 Redecl != RedeclEnd;
2010 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002011 if (Redecl->isInlineSpecified() &&
2012 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002013 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002014 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002015
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002016 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002017 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002018
Douglas Gregor299d76e2009-09-13 07:46:26 +00002019 // C99 6.7.4p6:
2020 // [...] If all of the file scope declarations for a function in a
2021 // translation unit include the inline function specifier without extern,
2022 // then the definition in that translation unit is an inline definition.
2023 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2024 Redecl != RedeclEnd;
2025 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002026 if (RedeclForcesDefC99(*Redecl))
2027 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002028 }
2029
2030 // C99 6.7.4p6:
2031 // An inline definition does not provide an external definition for the
2032 // function, and does not forbid an external definition in another
2033 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002034 return false;
2035}
2036
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002037/// getOverloadedOperator - Which C++ overloaded operator this
2038/// function represents, if any.
2039OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002040 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2041 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002042 else
2043 return OO_None;
2044}
2045
Alexis Huntc88db062010-01-13 09:01:02 +00002046/// getLiteralIdentifier - The literal suffix identifier this function
2047/// represents, if any.
2048const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2049 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2050 return getDeclName().getCXXLiteralIdentifier();
2051 else
2052 return 0;
2053}
2054
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002055FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2056 if (TemplateOrSpecialization.isNull())
2057 return TK_NonTemplate;
2058 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2059 return TK_FunctionTemplate;
2060 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2061 return TK_MemberSpecialization;
2062 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2063 return TK_FunctionTemplateSpecialization;
2064 if (TemplateOrSpecialization.is
2065 <DependentFunctionTemplateSpecializationInfo*>())
2066 return TK_DependentFunctionTemplateSpecialization;
2067
David Blaikie83d382b2011-09-23 05:06:16 +00002068 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002069}
2070
Douglas Gregord801b062009-10-07 23:56:10 +00002071FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002072 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002073 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2074
2075 return 0;
2076}
2077
Douglas Gregor06db9f52009-10-12 20:18:28 +00002078MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2079 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2080}
2081
Douglas Gregord801b062009-10-07 23:56:10 +00002082void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002083FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2084 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002085 TemplateSpecializationKind TSK) {
2086 assert(TemplateOrSpecialization.isNull() &&
2087 "Member function is already a specialization");
2088 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002089 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002090 TemplateOrSpecialization = Info;
2091}
2092
Douglas Gregorafca3b42009-10-27 20:53:28 +00002093bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002094 // If the function is invalid, it can't be implicitly instantiated.
2095 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002096 return false;
2097
2098 switch (getTemplateSpecializationKind()) {
2099 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002100 case TSK_ExplicitInstantiationDefinition:
2101 return false;
2102
2103 case TSK_ImplicitInstantiation:
2104 return true;
2105
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002106 // It is possible to instantiate TSK_ExplicitSpecialization kind
2107 // if the FunctionDecl has a class scope specialization pattern.
2108 case TSK_ExplicitSpecialization:
2109 return getClassScopeSpecializationPattern() != 0;
2110
Douglas Gregorafca3b42009-10-27 20:53:28 +00002111 case TSK_ExplicitInstantiationDeclaration:
2112 // Handled below.
2113 break;
2114 }
2115
2116 // Find the actual template from which we will instantiate.
2117 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002118 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002119 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002120 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002121
2122 // C++0x [temp.explicit]p9:
2123 // Except for inline functions, other explicit instantiation declarations
2124 // have the effect of suppressing the implicit instantiation of the entity
2125 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002126 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002127 return true;
2128
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002129 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002130}
2131
2132bool FunctionDecl::isTemplateInstantiation() const {
2133 switch (getTemplateSpecializationKind()) {
2134 case TSK_Undeclared:
2135 case TSK_ExplicitSpecialization:
2136 return false;
2137 case TSK_ImplicitInstantiation:
2138 case TSK_ExplicitInstantiationDeclaration:
2139 case TSK_ExplicitInstantiationDefinition:
2140 return true;
2141 }
2142 llvm_unreachable("All TSK values handled.");
2143}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002144
2145FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002146 // Handle class scope explicit specialization special case.
2147 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2148 return getClassScopeSpecializationPattern();
2149
Douglas Gregorafca3b42009-10-27 20:53:28 +00002150 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2151 while (Primary->getInstantiatedFromMemberTemplate()) {
2152 // If we have hit a point where the user provided a specialization of
2153 // this template, we're done looking.
2154 if (Primary->isMemberSpecialization())
2155 break;
2156
2157 Primary = Primary->getInstantiatedFromMemberTemplate();
2158 }
2159
2160 return Primary->getTemplatedDecl();
2161 }
2162
2163 return getInstantiatedFromMemberFunction();
2164}
2165
Douglas Gregor70d83e22009-06-29 17:30:29 +00002166FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002167 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002168 = TemplateOrSpecialization
2169 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002170 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002171 }
2172 return 0;
2173}
2174
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002175FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2176 return getASTContext().getClassScopeSpecializationPattern(this);
2177}
2178
Douglas Gregor70d83e22009-06-29 17:30:29 +00002179const TemplateArgumentList *
2180FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002181 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002182 = TemplateOrSpecialization
2183 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002184 return Info->TemplateArguments;
2185 }
2186 return 0;
2187}
2188
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002189const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002190FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2191 if (FunctionTemplateSpecializationInfo *Info
2192 = TemplateOrSpecialization
2193 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2194 return Info->TemplateArgumentsAsWritten;
2195 }
2196 return 0;
2197}
2198
Mike Stump11289f42009-09-09 15:08:12 +00002199void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002200FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2201 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002202 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002203 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002204 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002205 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2206 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002207 assert(TSK != TSK_Undeclared &&
2208 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002209 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002210 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002211 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002212 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2213 TemplateArgs,
2214 TemplateArgsAsWritten,
2215 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002216 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002217 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002218}
2219
John McCallb9c78482010-04-08 09:05:18 +00002220void
2221FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2222 const UnresolvedSetImpl &Templates,
2223 const TemplateArgumentListInfo &TemplateArgs) {
2224 assert(TemplateOrSpecialization.isNull());
2225 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2226 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002227 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002228 void *Buffer = Context.Allocate(Size);
2229 DependentFunctionTemplateSpecializationInfo *Info =
2230 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2231 TemplateArgs);
2232 TemplateOrSpecialization = Info;
2233}
2234
2235DependentFunctionTemplateSpecializationInfo::
2236DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2237 const TemplateArgumentListInfo &TArgs)
2238 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2239
2240 d.NumTemplates = Ts.size();
2241 d.NumArgs = TArgs.size();
2242
2243 FunctionTemplateDecl **TsArray =
2244 const_cast<FunctionTemplateDecl**>(getTemplates());
2245 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2246 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2247
2248 TemplateArgumentLoc *ArgsArray =
2249 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2250 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2251 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2252}
2253
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002254TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002255 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002256 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002257 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002258 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002259 if (FTSInfo)
2260 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002261
Douglas Gregord801b062009-10-07 23:56:10 +00002262 MemberSpecializationInfo *MSInfo
2263 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2264 if (MSInfo)
2265 return MSInfo->getTemplateSpecializationKind();
2266
2267 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002268}
2269
Mike Stump11289f42009-09-09 15:08:12 +00002270void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002271FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2272 SourceLocation PointOfInstantiation) {
2273 if (FunctionTemplateSpecializationInfo *FTSInfo
2274 = TemplateOrSpecialization.dyn_cast<
2275 FunctionTemplateSpecializationInfo*>()) {
2276 FTSInfo->setTemplateSpecializationKind(TSK);
2277 if (TSK != TSK_ExplicitSpecialization &&
2278 PointOfInstantiation.isValid() &&
2279 FTSInfo->getPointOfInstantiation().isInvalid())
2280 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2281 } else if (MemberSpecializationInfo *MSInfo
2282 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2283 MSInfo->setTemplateSpecializationKind(TSK);
2284 if (TSK != TSK_ExplicitSpecialization &&
2285 PointOfInstantiation.isValid() &&
2286 MSInfo->getPointOfInstantiation().isInvalid())
2287 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2288 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002289 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002290}
2291
2292SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002293 if (FunctionTemplateSpecializationInfo *FTSInfo
2294 = TemplateOrSpecialization.dyn_cast<
2295 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002296 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002297 else if (MemberSpecializationInfo *MSInfo
2298 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002299 return MSInfo->getPointOfInstantiation();
2300
2301 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002302}
2303
Douglas Gregor6411b922009-09-11 20:15:17 +00002304bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002305 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002306 return true;
2307
2308 // If this function was instantiated from a member function of a
2309 // class template, check whether that member function was defined out-of-line.
2310 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2311 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002312 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002313 return Definition->isOutOfLine();
2314 }
2315
2316 // If this function was instantiated from a function template,
2317 // check whether that function template was defined out-of-line.
2318 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2319 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002320 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002321 return Definition->isOutOfLine();
2322 }
2323
2324 return false;
2325}
2326
Abramo Bagnaraea947882011-03-08 16:41:52 +00002327SourceRange FunctionDecl::getSourceRange() const {
2328 return SourceRange(getOuterLocStart(), EndRangeLoc);
2329}
2330
Anna Zaks28db7ce2012-01-18 02:45:01 +00002331unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002332 IdentifierInfo *FnInfo = getIdentifier();
2333
2334 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002335 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002336
2337 // Builtin handling.
2338 switch (getBuiltinID()) {
2339 case Builtin::BI__builtin_memset:
2340 case Builtin::BI__builtin___memset_chk:
2341 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002342 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002343
2344 case Builtin::BI__builtin_memcpy:
2345 case Builtin::BI__builtin___memcpy_chk:
2346 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002347 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002348
2349 case Builtin::BI__builtin_memmove:
2350 case Builtin::BI__builtin___memmove_chk:
2351 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002352 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002353
2354 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002355 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002356 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002357 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002358
2359 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002360 case Builtin::BImemcmp:
2361 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002362
2363 case Builtin::BI__builtin_strncpy:
2364 case Builtin::BI__builtin___strncpy_chk:
2365 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002366 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002367
2368 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002369 case Builtin::BIstrncmp:
2370 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002371
2372 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002373 case Builtin::BIstrncasecmp:
2374 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002375
2376 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002377 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002378 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002379 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002380
2381 case Builtin::BI__builtin_strndup:
2382 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002383 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002384
Anna Zaks314cd092012-02-01 19:08:57 +00002385 case Builtin::BI__builtin_strlen:
2386 case Builtin::BIstrlen:
2387 return Builtin::BIstrlen;
2388
Anna Zaks201d4892012-01-13 21:52:01 +00002389 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002390 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002391 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002392 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002393 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002394 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002395 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002396 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002397 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002398 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002399 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002400 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002401 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002402 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002403 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002404 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002405 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002406 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002407 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002408 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002409 else if (FnInfo->isStr("strlen"))
2410 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002411 }
2412 break;
2413 }
Anna Zaks22122702012-01-17 00:37:07 +00002414 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002415}
2416
Chris Lattner59a25942008-03-31 00:36:02 +00002417//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002418// FieldDecl Implementation
2419//===----------------------------------------------------------------------===//
2420
Jay Foad39c79802011-01-12 09:06:06 +00002421FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002422 SourceLocation StartLoc, SourceLocation IdLoc,
2423 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002424 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2425 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002426 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002427 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002428}
2429
Douglas Gregor72172e92012-01-05 21:55:30 +00002430FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2431 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2432 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2433 0, QualType(), 0, 0, false, false);
2434}
2435
Sebastian Redl833ef452010-01-26 22:01:41 +00002436bool FieldDecl::isAnonymousStructOrUnion() const {
2437 if (!isImplicit() || getDeclName())
2438 return false;
2439
2440 if (const RecordType *Record = getType()->getAs<RecordType>())
2441 return Record->getDecl()->isAnonymousStructOrUnion();
2442
2443 return false;
2444}
2445
Richard Smithcaf33902011-10-10 18:28:20 +00002446unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2447 assert(isBitField() && "not a bitfield");
2448 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2449 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2450}
2451
John McCall4e819612011-01-20 07:57:12 +00002452unsigned FieldDecl::getFieldIndex() const {
2453 if (CachedFieldIndex) return CachedFieldIndex - 1;
2454
Richard Smithd62306a2011-11-10 06:34:14 +00002455 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002456 const RecordDecl *RD = getParent();
2457 const FieldDecl *LastFD = 0;
2458 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002459
2460 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2461 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002462 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002463
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002464 if (IsMsStruct) {
2465 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie2d7c57e2012-04-30 02:36:29 +00002466 if (getASTContext().ZeroBitfieldFollowsNonBitfield(&*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002467 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002468 continue;
2469 }
David Blaikie2d7c57e2012-04-30 02:36:29 +00002470 LastFD = &*I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002471 }
John McCall4e819612011-01-20 07:57:12 +00002472 }
2473
Richard Smithd62306a2011-11-10 06:34:14 +00002474 assert(CachedFieldIndex && "failed to find field in parent");
2475 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002476}
2477
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002478SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002479 if (const Expr *E = InitializerOrBitWidth.getPointer())
2480 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002481 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002482}
2483
Richard Smith938f40b2011-06-11 17:19:42 +00002484void FieldDecl::setInClassInitializer(Expr *Init) {
2485 assert(!InitializerOrBitWidth.getPointer() &&
2486 "bit width or initializer already set");
2487 InitializerOrBitWidth.setPointer(Init);
2488 InitializerOrBitWidth.setInt(0);
2489}
2490
Sebastian Redl833ef452010-01-26 22:01:41 +00002491//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002492// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002493//===----------------------------------------------------------------------===//
2494
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002495SourceLocation TagDecl::getOuterLocStart() const {
2496 return getTemplateOrInnerLocStart(this);
2497}
2498
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002499SourceRange TagDecl::getSourceRange() const {
2500 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002501 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002502}
2503
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002504TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002505 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002506}
2507
Richard Smithdda56e42011-04-15 14:24:37 +00002508void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2509 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002510 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002511 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002512 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002513}
2514
Douglas Gregordee1be82009-01-17 00:42:38 +00002515void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002516 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002517
2518 if (isa<CXXRecordDecl>(this)) {
2519 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2520 struct CXXRecordDecl::DefinitionData *Data =
2521 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002522 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2523 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002524 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002525}
2526
2527void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002528 assert((!isa<CXXRecordDecl>(this) ||
2529 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2530 "definition completed but not started");
2531
John McCallf937c022011-10-07 06:10:15 +00002532 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002533 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002534
2535 if (ASTMutationListener *L = getASTMutationListener())
2536 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002537}
2538
John McCallf937c022011-10-07 06:10:15 +00002539TagDecl *TagDecl::getDefinition() const {
2540 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002541 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002542 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2543 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002544
2545 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002546 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002547 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002548 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002549
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002550 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002551}
2552
Douglas Gregor14454802011-02-25 02:25:35 +00002553void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2554 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002555 // Make sure the extended qualifier info is allocated.
2556 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002557 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002558 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002559 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002560 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002561 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002562 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002563 if (getExtInfo()->NumTemplParamLists == 0) {
2564 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002565 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002566 }
2567 else
2568 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002569 }
2570 }
2571}
2572
Abramo Bagnara60804e12011-03-18 15:16:37 +00002573void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2574 unsigned NumTPLists,
2575 TemplateParameterList **TPLists) {
2576 assert(NumTPLists > 0);
2577 // Make sure the extended decl info is allocated.
2578 if (!hasExtInfo())
2579 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002580 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002581 // Set the template parameter lists info.
2582 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2583}
2584
Ted Kremenek21475702008-09-05 17:16:31 +00002585//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002586// EnumDecl Implementation
2587//===----------------------------------------------------------------------===//
2588
David Blaikie68e081d2011-12-20 02:48:34 +00002589void EnumDecl::anchor() { }
2590
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002591EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2592 SourceLocation StartLoc, SourceLocation IdLoc,
2593 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002594 EnumDecl *PrevDecl, bool IsScoped,
2595 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002596 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002597 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002598 C.getTypeDeclType(Enum, PrevDecl);
2599 return Enum;
2600}
2601
Douglas Gregor72172e92012-01-05 21:55:30 +00002602EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2603 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2604 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2605 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002606}
2607
Douglas Gregord5058122010-02-11 01:19:42 +00002608void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002609 QualType NewPromotionType,
2610 unsigned NumPositiveBits,
2611 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002612 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002613 if (!IntegerType)
2614 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002615 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002616 setNumPositiveBits(NumPositiveBits);
2617 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002618 TagDecl::completeDefinition();
2619}
2620
Richard Smith7d137e32012-03-23 03:33:32 +00002621TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2622 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2623 return MSI->getTemplateSpecializationKind();
2624
2625 return TSK_Undeclared;
2626}
2627
2628void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2629 SourceLocation PointOfInstantiation) {
2630 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2631 assert(MSI && "Not an instantiated member enumeration?");
2632 MSI->setTemplateSpecializationKind(TSK);
2633 if (TSK != TSK_ExplicitSpecialization &&
2634 PointOfInstantiation.isValid() &&
2635 MSI->getPointOfInstantiation().isInvalid())
2636 MSI->setPointOfInstantiation(PointOfInstantiation);
2637}
2638
Richard Smith4b38ded2012-03-14 23:13:10 +00002639EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2640 if (SpecializationInfo)
2641 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2642
2643 return 0;
2644}
2645
2646void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2647 TemplateSpecializationKind TSK) {
2648 assert(!SpecializationInfo && "Member enum is already a specialization");
2649 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2650}
2651
Sebastian Redl833ef452010-01-26 22:01:41 +00002652//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002653// RecordDecl Implementation
2654//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002655
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002656RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2657 SourceLocation StartLoc, SourceLocation IdLoc,
2658 IdentifierInfo *Id, RecordDecl *PrevDecl)
2659 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002660 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002661 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002662 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002663 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002664 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002665}
2666
Jay Foad39c79802011-01-12 09:06:06 +00002667RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002668 SourceLocation StartLoc, SourceLocation IdLoc,
2669 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2670 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2671 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002672 C.getTypeDeclType(R, PrevDecl);
2673 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002674}
2675
Douglas Gregor72172e92012-01-05 21:55:30 +00002676RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2677 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2678 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2679 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002680}
2681
Douglas Gregordfcad112009-03-25 15:59:44 +00002682bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002683 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002684 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2685}
2686
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002687RecordDecl::field_iterator RecordDecl::field_begin() const {
2688 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2689 LoadFieldsFromExternalStorage();
2690
2691 return field_iterator(decl_iterator(FirstDecl));
2692}
2693
Douglas Gregorb11aad82011-02-19 18:51:44 +00002694/// completeDefinition - Notes that the definition of this type is now
2695/// complete.
2696void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002697 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002698 TagDecl::completeDefinition();
2699}
2700
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002701void RecordDecl::LoadFieldsFromExternalStorage() const {
2702 ExternalASTSource *Source = getASTContext().getExternalSource();
2703 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2704
2705 // Notify that we have a RecordDecl doing some initialization.
2706 ExternalASTSource::Deserializing TheFields(Source);
2707
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002708 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002709 LoadedFieldsFromExternalStorage = true;
2710 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2711 case ELR_Success:
2712 break;
2713
2714 case ELR_AlreadyLoaded:
2715 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002716 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002717 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002718
2719#ifndef NDEBUG
2720 // Check that all decls we got were FieldDecls.
2721 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2722 assert(isa<FieldDecl>(Decls[i]));
2723#endif
2724
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002725 if (Decls.empty())
2726 return;
2727
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002728 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2729 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002730}
2731
Steve Naroff415d3d52008-10-08 17:01:13 +00002732//===----------------------------------------------------------------------===//
2733// BlockDecl Implementation
2734//===----------------------------------------------------------------------===//
2735
David Blaikie9c70e042011-09-21 18:16:56 +00002736void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002737 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002738
Steve Naroffc4b30e52009-03-13 16:56:44 +00002739 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002740 if (!NewParamInfo.empty()) {
2741 NumParams = NewParamInfo.size();
2742 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2743 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002744 }
2745}
2746
John McCall351762c2011-02-07 10:33:21 +00002747void BlockDecl::setCaptures(ASTContext &Context,
2748 const Capture *begin,
2749 const Capture *end,
2750 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002751 CapturesCXXThis = capturesCXXThis;
2752
2753 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002754 NumCaptures = 0;
2755 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002756 return;
2757 }
2758
John McCall351762c2011-02-07 10:33:21 +00002759 NumCaptures = end - begin;
2760
2761 // Avoid new Capture[] because we don't want to provide a default
2762 // constructor.
2763 size_t allocationSize = NumCaptures * sizeof(Capture);
2764 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2765 memcpy(buffer, begin, allocationSize);
2766 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002767}
Sebastian Redl833ef452010-01-26 22:01:41 +00002768
John McCallce45f882011-06-15 22:51:16 +00002769bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2770 for (capture_const_iterator
2771 i = capture_begin(), e = capture_end(); i != e; ++i)
2772 // Only auto vars can be captured, so no redeclaration worries.
2773 if (i->getVariable() == variable)
2774 return true;
2775
2776 return false;
2777}
2778
Douglas Gregor70226da2010-12-21 16:27:07 +00002779SourceRange BlockDecl::getSourceRange() const {
2780 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2781}
Sebastian Redl833ef452010-01-26 22:01:41 +00002782
2783//===----------------------------------------------------------------------===//
2784// Other Decl Allocation/Deallocation Method Implementations
2785//===----------------------------------------------------------------------===//
2786
David Blaikie68e081d2011-12-20 02:48:34 +00002787void TranslationUnitDecl::anchor() { }
2788
Sebastian Redl833ef452010-01-26 22:01:41 +00002789TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2790 return new (C) TranslationUnitDecl(C);
2791}
2792
David Blaikie68e081d2011-12-20 02:48:34 +00002793void LabelDecl::anchor() { }
2794
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002795LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002796 SourceLocation IdentL, IdentifierInfo *II) {
2797 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2798}
2799
2800LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2801 SourceLocation IdentL, IdentifierInfo *II,
2802 SourceLocation GnuLabelL) {
2803 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2804 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002805}
2806
Douglas Gregor72172e92012-01-05 21:55:30 +00002807LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2808 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2809 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002810}
2811
David Blaikie68e081d2011-12-20 02:48:34 +00002812void ValueDecl::anchor() { }
2813
2814void ImplicitParamDecl::anchor() { }
2815
Sebastian Redl833ef452010-01-26 22:01:41 +00002816ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002817 SourceLocation IdLoc,
2818 IdentifierInfo *Id,
2819 QualType Type) {
2820 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002821}
2822
Douglas Gregor72172e92012-01-05 21:55:30 +00002823ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2824 unsigned ID) {
2825 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2826 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2827}
2828
Sebastian Redl833ef452010-01-26 22:01:41 +00002829FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002830 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002831 const DeclarationNameInfo &NameInfo,
2832 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002833 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002834 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002835 bool hasWrittenPrototype,
2836 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002837 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2838 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002839 isInlineSpecified,
2840 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002841 New->HasWrittenPrototype = hasWrittenPrototype;
2842 return New;
2843}
2844
Douglas Gregor72172e92012-01-05 21:55:30 +00002845FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2846 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2847 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2848 DeclarationNameInfo(), QualType(), 0,
2849 SC_None, SC_None, false, false);
2850}
2851
Sebastian Redl833ef452010-01-26 22:01:41 +00002852BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2853 return new (C) BlockDecl(DC, L);
2854}
2855
Douglas Gregor72172e92012-01-05 21:55:30 +00002856BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2857 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2858 return new (Mem) BlockDecl(0, SourceLocation());
2859}
2860
Sebastian Redl833ef452010-01-26 22:01:41 +00002861EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2862 SourceLocation L,
2863 IdentifierInfo *Id, QualType T,
2864 Expr *E, const llvm::APSInt &V) {
2865 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2866}
2867
Douglas Gregor72172e92012-01-05 21:55:30 +00002868EnumConstantDecl *
2869EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2870 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2871 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2872 llvm::APSInt());
2873}
2874
David Blaikie68e081d2011-12-20 02:48:34 +00002875void IndirectFieldDecl::anchor() { }
2876
Benjamin Kramer39593702010-11-21 14:11:41 +00002877IndirectFieldDecl *
2878IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2879 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2880 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002881 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2882}
2883
Douglas Gregor72172e92012-01-05 21:55:30 +00002884IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2885 unsigned ID) {
2886 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2887 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2888 QualType(), 0, 0);
2889}
2890
Douglas Gregorbe996932010-09-01 20:41:53 +00002891SourceRange EnumConstantDecl::getSourceRange() const {
2892 SourceLocation End = getLocation();
2893 if (Init)
2894 End = Init->getLocEnd();
2895 return SourceRange(getLocation(), End);
2896}
2897
David Blaikie68e081d2011-12-20 02:48:34 +00002898void TypeDecl::anchor() { }
2899
Sebastian Redl833ef452010-01-26 22:01:41 +00002900TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002901 SourceLocation StartLoc, SourceLocation IdLoc,
2902 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2903 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002904}
2905
David Blaikie68e081d2011-12-20 02:48:34 +00002906void TypedefNameDecl::anchor() { }
2907
Douglas Gregor72172e92012-01-05 21:55:30 +00002908TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2909 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2910 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2911}
2912
Richard Smithdda56e42011-04-15 14:24:37 +00002913TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2914 SourceLocation StartLoc,
2915 SourceLocation IdLoc, IdentifierInfo *Id,
2916 TypeSourceInfo *TInfo) {
2917 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2918}
2919
Douglas Gregor72172e92012-01-05 21:55:30 +00002920TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2921 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2922 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2923}
2924
Abramo Bagnaraea947882011-03-08 16:41:52 +00002925SourceRange TypedefDecl::getSourceRange() const {
2926 SourceLocation RangeEnd = getLocation();
2927 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2928 if (typeIsPostfix(TInfo->getType()))
2929 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2930 }
2931 return SourceRange(getLocStart(), RangeEnd);
2932}
2933
Richard Smithdda56e42011-04-15 14:24:37 +00002934SourceRange TypeAliasDecl::getSourceRange() const {
2935 SourceLocation RangeEnd = getLocStart();
2936 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2937 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2938 return SourceRange(getLocStart(), RangeEnd);
2939}
2940
David Blaikie68e081d2011-12-20 02:48:34 +00002941void FileScopeAsmDecl::anchor() { }
2942
Sebastian Redl833ef452010-01-26 22:01:41 +00002943FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002944 StringLiteral *Str,
2945 SourceLocation AsmLoc,
2946 SourceLocation RParenLoc) {
2947 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002948}
Douglas Gregorba345522011-12-02 23:23:56 +00002949
Douglas Gregor72172e92012-01-05 21:55:30 +00002950FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2951 unsigned ID) {
2952 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2953 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2954}
2955
Douglas Gregorba345522011-12-02 23:23:56 +00002956//===----------------------------------------------------------------------===//
2957// ImportDecl Implementation
2958//===----------------------------------------------------------------------===//
2959
2960/// \brief Retrieve the number of module identifiers needed to name the given
2961/// module.
2962static unsigned getNumModuleIdentifiers(Module *Mod) {
2963 unsigned Result = 1;
2964 while (Mod->Parent) {
2965 Mod = Mod->Parent;
2966 ++Result;
2967 }
2968 return Result;
2969}
2970
Douglas Gregor22d09742012-01-03 18:04:46 +00002971ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002972 Module *Imported,
2973 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00002974 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002975 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002976{
2977 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
2978 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
2979 memcpy(StoredLocs, IdentifierLocs.data(),
2980 IdentifierLocs.size() * sizeof(SourceLocation));
2981}
2982
Douglas Gregor22d09742012-01-03 18:04:46 +00002983ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002984 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00002985 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002986 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002987{
2988 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
2989}
2990
2991ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00002992 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00002993 ArrayRef<SourceLocation> IdentifierLocs) {
2994 void *Mem = C.Allocate(sizeof(ImportDecl) +
2995 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00002996 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00002997}
2998
2999ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003000 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003001 Module *Imported,
3002 SourceLocation EndLoc) {
3003 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003004 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003005 Import->setImplicit();
3006 return Import;
3007}
3008
Douglas Gregor72172e92012-01-05 21:55:30 +00003009ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3010 unsigned NumLocations) {
3011 void *Mem = AllocateDeserializedDecl(C, ID,
3012 (sizeof(ImportDecl) +
3013 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003014 return new (Mem) ImportDecl(EmptyShell());
3015}
3016
3017ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3018 if (!ImportedAndComplete.getInt())
3019 return ArrayRef<SourceLocation>();
3020
3021 const SourceLocation *StoredLocs
3022 = reinterpret_cast<const SourceLocation *>(this + 1);
3023 return ArrayRef<SourceLocation>(StoredLocs,
3024 getNumModuleIdentifiers(getImportedModule()));
3025}
3026
3027SourceRange ImportDecl::getSourceRange() const {
3028 if (!ImportedAndComplete.getInt())
3029 return SourceRange(getLocation(),
3030 *reinterpret_cast<const SourceLocation *>(this + 1));
3031
3032 return SourceRange(getLocation(), getIdentifierLocs().back());
3033}