blob: 62cc62ae39e0b3f6ebe104f81e2c21dce0c1b290 [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 Espindola96dcb8d2012-05-21 20:31:27 +0000161static bool shouldConsiderTemplateLV(const FunctionDecl *fn,
162 const FunctionTemplateSpecializationInfo *spec) {
163 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000164}
165
166static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola93c289c2012-05-21 20:15:56 +0000167 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000168}
169
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000170static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
171 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000172 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000173 "Not a name having namespace scope");
174 ASTContext &Context = D->getASTContext();
175
176 // C++ [basic.link]p3:
177 // A name having namespace scope (3.3.6) has internal linkage if it
178 // is the name of
179 // - an object, reference, function or function template that is
180 // explicitly declared static; or,
181 // (This bullet corresponds to C99 6.2.2p3.)
182 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
183 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000184 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000185 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000186
187 // - an object or reference that is explicitly declared const
188 // and neither explicitly declared extern nor previously
189 // declared to have external linkage; or
190 // (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000191 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000192 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000193 Var->getStorageClass() != SC_Extern &&
194 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000195 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000196 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000197 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000198 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000199 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000200 FoundExtern = true;
201
202 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000203 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000204 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000205 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000206 const VarDecl *PrevVar = Var->getPreviousDecl();
207 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000208 if (PrevVar->getStorageClass() == SC_PrivateExtern)
209 break;
210 if (PrevVar)
211 return PrevVar->getLinkageAndVisibility();
212 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000213 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000214 // C++ [temp]p4:
215 // A non-member function template can have internal linkage; any
216 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000217 const FunctionDecl *Function = 0;
218 if (const FunctionTemplateDecl *FunTmpl
219 = dyn_cast<FunctionTemplateDecl>(D))
220 Function = FunTmpl->getTemplatedDecl();
221 else
222 Function = cast<FunctionDecl>(D);
223
224 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000225 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000226 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000227 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
228 // - a data member of an anonymous union.
229 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000230 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000231 }
232
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000233 if (D->isInAnonymousNamespace()) {
234 const VarDecl *Var = dyn_cast<VarDecl>(D);
235 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000236 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
237 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000238 return LinkageInfo::uniqueExternal();
239 }
John McCallb7139c42010-10-28 04:18:25 +0000240
John McCall457a04e2010-10-22 21:05:15 +0000241 // Set up the defaults.
242
243 // C99 6.2.2p5:
244 // If the declaration of an identifier for an object has file
245 // scope and no storage-class specifier, its linkage is
246 // external.
John McCallc273f242010-10-30 11:50:40 +0000247 LinkageInfo LV;
248
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000249 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000250 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000251 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000252 } else {
253 // If we're declared in a namespace with a visibility attribute,
254 // use that namespace's visibility, but don't call it explicit.
255 for (const DeclContext *DC = D->getDeclContext();
256 !isa<TranslationUnitDecl>(DC);
257 DC = DC->getParent()) {
258 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
259 if (!ND) continue;
260 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000261 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000262 break;
263 }
264 }
265 }
266 }
267
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000268 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000269 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000270
Douglas Gregorf73b2822009-11-25 22:24:25 +0000271 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000272
Douglas Gregorf73b2822009-11-25 22:24:25 +0000273 // A name having namespace scope has external linkage if it is the
274 // name of
275 //
276 // - an object or reference, unless it has internal linkage; or
277 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000278 // GCC applies the following optimization to variables and static
279 // data members, but not to functions:
280 //
John McCall457a04e2010-10-22 21:05:15 +0000281 // Modify the variable's LV by the LV of its type unless this is
282 // C or extern "C". This follows from [basic.link]p9:
283 // A type without linkage shall not be used as the type of a
284 // variable or function with external linkage unless
285 // - the entity has C language linkage, or
286 // - the entity is declared within an unnamed namespace, or
287 // - the entity is not used or is defined in the same
288 // translation unit.
289 // and [basic.link]p10:
290 // ...the types specified by all declarations referring to a
291 // given variable or function shall be identical...
292 // C does not have an equivalent rule.
293 //
John McCall5fe84122010-10-26 04:59:26 +0000294 // Ignore this if we've got an explicit attribute; the user
295 // probably knows what they're doing.
296 //
John McCall457a04e2010-10-22 21:05:15 +0000297 // Note that we don't want to make the variable non-external
298 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000299 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000300 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000301 LinkageInfo TypeLV = getLVForType(Var->getType());
302 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000303 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000304 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000305 }
306
John McCall23032652010-11-02 18:38:13 +0000307 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000308 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000309
David Blaikiebbafb8a2012-03-11 07:00:24 +0000310 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000311 (Var->getStorageClass() == SC_Extern ||
312 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000313
Douglas Gregorf73b2822009-11-25 22:24:25 +0000314 // C99 6.2.2p4:
315 // For an identifier declared with the storage-class specifier
316 // extern in a scope in which a prior declaration of that
317 // identifier is visible, if the prior declaration specifies
318 // internal or external linkage, the linkage of the identifier
319 // at the later declaration is the same as the linkage
320 // specified at the prior declaration. If no prior declaration
321 // is visible, or if the prior declaration specifies no
322 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000323 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000324 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000325 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
326 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000327 }
328 }
329
Douglas Gregorf73b2822009-11-25 22:24:25 +0000330 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000331 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000332 // In theory, we can modify the function's LV by the LV of its
333 // type unless it has C linkage (see comment above about variables
334 // for justification). In practice, GCC doesn't do this, so it's
335 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000336
John McCall23032652010-11-02 18:38:13 +0000337 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000338 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000339
Douglas Gregorf73b2822009-11-25 22:24:25 +0000340 // C99 6.2.2p5:
341 // If the declaration of an identifier for a function has no
342 // storage-class specifier, its linkage is determined exactly
343 // as if it were declared with the storage-class specifier
344 // extern.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000345 if (!Context.getLangOpts().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000346 (Function->getStorageClass() == SC_Extern ||
347 Function->getStorageClass() == SC_PrivateExtern ||
348 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349 // C99 6.2.2p4:
350 // For an identifier declared with the storage-class specifier
351 // extern in a scope in which a prior declaration of that
352 // identifier is visible, if the prior declaration specifies
353 // internal or external linkage, the linkage of the identifier
354 // at the later declaration is the same as the linkage
355 // specified at the prior declaration. If no prior declaration
356 // is visible, or if the prior declaration specifies no
357 // linkage, then the identifier has external linkage.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000358 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000359 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000360 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
361 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000362 }
363 }
364
John McCallf768aa72011-02-10 06:50:24 +0000365 // In C++, then if the type of the function uses a type with
366 // unique-external linkage, it's not legally usable from outside
367 // this translation unit. However, we should use the C linkage
368 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000369 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000370 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000371 Function->getType()->getLinkage() == UniqueExternalLinkage)
372 return LinkageInfo::uniqueExternal();
373
John McCallb8c604a2011-06-27 23:06:04 +0000374 // Consider LV from the template and the template arguments unless
375 // this is an explicit specialization with a visibility attribute.
376 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000377 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola96dcb8d2012-05-21 20:31:27 +0000378 if (shouldConsiderTemplateLV(Function, specInfo)) {
John McCallb8c604a2011-06-27 23:06:04 +0000379 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000380 true));
John McCallb8c604a2011-06-27 23:06:04 +0000381 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000382 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs,
383 OnlyTemplate));
John McCallb8c604a2011-06-27 23:06:04 +0000384 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000385 }
386
Douglas Gregorf73b2822009-11-25 22:24:25 +0000387 // - a named class (Clause 9), or an unnamed class defined in a
388 // typedef declaration in which the class has the typedef name
389 // for linkage purposes (7.1.3); or
390 // - a named enumeration (7.2), or an unnamed enumeration
391 // defined in a typedef declaration in which the enumeration
392 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000393 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
394 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000395 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000396 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000397
John McCall457a04e2010-10-22 21:05:15 +0000398 // If this is a class template specialization, consider the
399 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000400 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000401 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCallb8c604a2011-06-27 23:06:04 +0000402 if (shouldConsiderTemplateLV(spec)) {
403 // From the template.
404 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000405 true));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000406
John McCallb8c604a2011-06-27 23:06:04 +0000407 // The arguments at which the template was instantiated.
408 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindola7f90b7d2012-05-15 14:09:55 +0000409 LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs,
410 OnlyTemplate));
John McCallb8c604a2011-06-27 23:06:04 +0000411 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000412 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000413
414 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000415 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000416 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
417 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000418 if (!isExternalLinkage(EnumLV.linkage()))
419 return LinkageInfo::none();
420 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000421
422 // - a template, unless it is a function template that has
423 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000424 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000425 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000426 // - a namespace (7.3), unless it is declared within an unnamed
427 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000428 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
429 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000430
John McCall457a04e2010-10-22 21:05:15 +0000431 // By extension, we assign external linkage to Objective-C
432 // interfaces.
433 } else if (isa<ObjCInterfaceDecl>(D)) {
434 // fallout
435
436 // Everything not covered here has no linkage.
437 } else {
John McCallc273f242010-10-30 11:50:40 +0000438 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000439 }
440
441 // If we ended up with non-external linkage, visibility should
442 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000443 if (LV.linkage() != ExternalLinkage)
444 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000445
John McCall457a04e2010-10-22 21:05:15 +0000446 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000447}
448
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000449static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000450 // Only certain class members have linkage. Note that fields don't
451 // really have linkage, but it's convenient to say they do for the
452 // purposes of calculating linkage of pointer-to-data-member
453 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000454 if (!(isa<CXXMethodDecl>(D) ||
455 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000456 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000457 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000458 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000459 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000460
John McCall07072662010-11-02 01:45:15 +0000461 LinkageInfo LV;
462
John McCall07072662010-11-02 01:45:15 +0000463 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000464 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000465 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000466 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000467 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000468
469 // If this class member has an explicit visibility attribute, the only
470 // thing that can change its visibility is the template arguments, so
471 // only look for them when processing the the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000472 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000473
474 // If we're paying attention to global visibility, apply
475 // -finline-visibility-hidden if this is an inline method.
476 //
477 // Note that we do this before merging information about
478 // the class visibility.
479 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
480 TemplateSpecializationKind TSK = TSK_Undeclared;
481 if (FunctionTemplateSpecializationInfo *spec
482 = MD->getTemplateSpecializationInfo()) {
483 TSK = spec->getTemplateSpecializationKind();
484 } else if (MemberSpecializationInfo *MSI =
485 MD->getMemberSpecializationInfo()) {
486 TSK = MSI->getTemplateSpecializationKind();
487 }
488
489 const FunctionDecl *Def = 0;
490 // InlineVisibilityHidden only applies to definitions, and
491 // isInlined() only gives meaningful answers on definitions
492 // anyway.
493 if (TSK != TSK_ExplicitInstantiationDeclaration &&
494 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000495 !OnlyTemplate &&
Rafael Espindola505a7c82012-04-16 18:25:01 +0000496 !LV.visibilityExplicit() &&
497 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
498 MD->hasBody(Def) && Def->isInlined())
499 LV.mergeVisibility(HiddenVisibility, true);
500 }
John McCallc273f242010-10-30 11:50:40 +0000501
Rafael Espindola53cf2192012-04-19 05:50:08 +0000502 // If this member has an visibility attribute, ClassF will exclude
503 // attributes on the class or command line options, keeping only information
504 // about the template instantiation. If the member has no visibility
505 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
506 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000507 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
508 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000509 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000510 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000511
512 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000513 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000514 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000515
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000516 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000517 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000518
John McCall8823c652010-08-13 08:35:10 +0000519 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000520 // If the type of the function uses a type with unique-external
521 // linkage, it's not legally usable from outside this translation unit.
522 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
523 return LinkageInfo::uniqueExternal();
524
John McCall457a04e2010-10-22 21:05:15 +0000525 // If this is a method template specialization, use the linkage for
526 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000527 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000528 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola96dcb8d2012-05-21 20:31:27 +0000529 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000530 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000531 OnlyTemplate));
532 if (!OnlyTemplate)
John McCallb8c604a2011-06-27 23:06:04 +0000533 LV.merge(getLVForTemplateParameterList(
534 spec->getTemplate()->getTemplateParameters()));
535 }
John McCalle6e622e2010-11-01 01:29:57 +0000536 }
John McCall457a04e2010-10-22 21:05:15 +0000537
John McCall37bb6c92010-10-29 22:22:43 +0000538 // Note that in contrast to basically every other situation, we
539 // *do* apply -fvisibility to method declarations.
540
541 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000542 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000543 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCallb8c604a2011-06-27 23:06:04 +0000544 if (shouldConsiderTemplateLV(spec)) {
545 // Merge template argument/parameter information for member
546 // class template specializations.
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000547 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000548 OnlyTemplate));
549 if (!OnlyTemplate)
John McCall8bc6d5b2011-03-04 10:39:25 +0000550 LV.merge(getLVForTemplateParameterList(
John McCallb8c604a2011-06-27 23:06:04 +0000551 spec->getSpecializedTemplate()->getTemplateParameters()));
552 }
John McCall37bb6c92010-10-29 22:22:43 +0000553 }
554
John McCall37bb6c92010-10-29 22:22:43 +0000555 // Static data members.
556 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000557 // Modify the variable's linkage by its type, but ignore the
558 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000559 LinkageInfo TypeLV = getLVForType(VD->getType());
560 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000561 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000562 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000563 }
564
John McCall457a04e2010-10-22 21:05:15 +0000565 return LV;
John McCall8823c652010-08-13 08:35:10 +0000566}
567
John McCalld396b972011-02-08 19:01:05 +0000568static void clearLinkageForClass(const CXXRecordDecl *record) {
569 for (CXXRecordDecl::decl_iterator
570 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
571 Decl *child = *i;
572 if (isa<NamedDecl>(child))
573 cast<NamedDecl>(child)->ClearLinkageCache();
574 }
575}
576
David Blaikie68e081d2011-12-20 02:48:34 +0000577void NamedDecl::anchor() { }
578
John McCalld396b972011-02-08 19:01:05 +0000579void NamedDecl::ClearLinkageCache() {
580 // Note that we can't skip clearing the linkage of children just
581 // because the parent doesn't have cached linkage: we don't cache
582 // when computing linkage for parent contexts.
583
584 HasCachedLinkage = 0;
585
586 // If we're changing the linkage of a class, we need to reset the
587 // linkage of child declarations, too.
588 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
589 clearLinkageForClass(record);
590
John McCall83779672011-02-19 02:53:41 +0000591 if (ClassTemplateDecl *temp =
592 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000593 // Clear linkage for the template pattern.
594 CXXRecordDecl *record = temp->getTemplatedDecl();
595 record->HasCachedLinkage = 0;
596 clearLinkageForClass(record);
597
John McCall83779672011-02-19 02:53:41 +0000598 // We need to clear linkage for specializations, too.
599 for (ClassTemplateDecl::spec_iterator
600 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
601 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000602 }
John McCall83779672011-02-19 02:53:41 +0000603
604 // Clear cached linkage for function template decls, too.
605 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000606 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
607 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000608 for (FunctionTemplateDecl::spec_iterator
609 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
610 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000611 }
John McCall83779672011-02-19 02:53:41 +0000612
John McCalld396b972011-02-08 19:01:05 +0000613}
614
Douglas Gregorbf62d642010-12-06 18:36:25 +0000615Linkage NamedDecl::getLinkage() const {
616 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000617 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000618 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000619 return Linkage(CachedLinkage);
620 }
621
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000622 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000623 HasCachedLinkage = 1;
624 return Linkage(CachedLinkage);
625}
626
John McCallc273f242010-10-30 11:50:40 +0000627LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000628 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000629 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000630 HasCachedLinkage = 1;
631 CachedLinkage = LI.linkage();
632 return LI;
John McCall033caa52010-10-29 00:29:13 +0000633}
Ted Kremenek926d8602010-04-20 23:15:35 +0000634
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000635llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
636 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000637 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
638 if (llvm::Optional<Visibility> V =
639 getVisibilityOf(Var->getMostRecentDecl()))
640 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000641
Rafael Espindola96e68242012-05-16 02:10:38 +0000642 if (Var->isStaticDataMember()) {
643 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
644 if (InstantiatedFrom)
645 return getVisibilityOf(InstantiatedFrom);
646 }
647
648 return llvm::Optional<Visibility>();
649 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000650 // Use the most recent declaration of a function, and also handle
651 // function template specializations.
652 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
653 if (llvm::Optional<Visibility> V
Douglas Gregorec9fd132012-01-14 16:38:05 +0000654 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000655 return V;
656
657 // If the function is a specialization of a template with an
658 // explicit visibility attribute, use that.
659 if (FunctionTemplateSpecializationInfo *templateInfo
660 = fn->getTemplateSpecializationInfo())
661 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
662
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000663 // If the function is a member of a specialization of a class template
664 // and the corresponding decl has explicit visibility, use that.
665 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
666 if (InstantiatedFrom)
667 return getVisibilityOf(InstantiatedFrom);
668
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000669 return llvm::Optional<Visibility>();
670 }
671
672 // Otherwise, just check the declaration itself first.
673 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
674 return V;
675
676 // If there wasn't explicit visibility there, and this is a
677 // specialization of a class template, check for visibility
678 // on the pattern.
679 if (const ClassTemplateSpecializationDecl *spec
680 = dyn_cast<ClassTemplateSpecializationDecl>(this))
681 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
682
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000683 // If this is a member class of a specialization of a class template
684 // and the corresponding decl has explicit visibility, use that.
685 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
686 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
687 if (InstantiatedFrom)
688 return getVisibilityOf(InstantiatedFrom);
689 }
690
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000691 return llvm::Optional<Visibility>();
692}
693
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000694static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000695 // Objective-C: treat all Objective-C declarations as having external
696 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000697 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000698 default:
699 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000700 case Decl::ParmVar:
701 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000702 case Decl::TemplateTemplateParm: // count these as external
703 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000704 case Decl::ObjCAtDefsField:
705 case Decl::ObjCCategory:
706 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000707 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000708 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000709 case Decl::ObjCMethod:
710 case Decl::ObjCProperty:
711 case Decl::ObjCPropertyImpl:
712 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000713 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000714
715 case Decl::CXXRecord: {
716 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
717 if (Record->isLambda()) {
718 if (!Record->getLambdaManglingNumber()) {
719 // This lambda has no mangling number, so it's internal.
720 return LinkageInfo::internal();
721 }
722
723 // This lambda has its linkage/visibility determined by its owner.
724 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
725 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
726 if (isa<ParmVarDecl>(ContextDecl))
727 DC = ContextDecl->getDeclContext()->getRedeclContext();
728 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000729 return getLVForDecl(cast<NamedDecl>(ContextDecl),
730 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000731 }
732
733 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000734 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000735
736 return LinkageInfo::external();
737 }
738
739 break;
740 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000741 }
742
Douglas Gregorf73b2822009-11-25 22:24:25 +0000743 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000744 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000745 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000746
747 // C++ [basic.link]p5:
748 // In addition, a member function, static data member, a named
749 // class or enumeration of class scope, or an unnamed class or
750 // enumeration defined in a class-scope typedef declaration such
751 // that the class or enumeration has the typedef name for linkage
752 // purposes (7.1.3), has external linkage if the name of the class
753 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000754 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000755 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000756
757 // C++ [basic.link]p6:
758 // The name of a function declared in block scope and the name of
759 // an object declared by a block scope extern declaration have
760 // linkage. If there is a visible declaration of an entity with
761 // linkage having the same name and type, ignoring entities
762 // declared outside the innermost enclosing namespace scope, the
763 // block scope declaration declares that same entity and receives
764 // the linkage of the previous declaration. If there is more than
765 // one such matching entity, the program is ill-formed. Otherwise,
766 // if no matching entity is found, the block scope entity receives
767 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000768 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
769 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000770 if (Function->isInAnonymousNamespace() &&
771 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000772 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000773
John McCallc273f242010-10-30 11:50:40 +0000774 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000775 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000776 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000777 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000778 }
779
Douglas Gregorec9fd132012-01-14 16:38:05 +0000780 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000781 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000782 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
783 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000784 }
785
786 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000787 }
788
John McCall033caa52010-10-29 00:29:13 +0000789 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000790 if (Var->getStorageClass() == SC_Extern ||
791 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000792 if (Var->isInAnonymousNamespace() &&
793 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000794 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000795
John McCallc273f242010-10-30 11:50:40 +0000796 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000797 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000798 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000799 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000800 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000801 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000802 }
803
Douglas Gregorec9fd132012-01-14 16:38:05 +0000804 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000805 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000806 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
807 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000808 }
809
810 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000811 }
812 }
813
814 // C++ [basic.link]p6:
815 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000816 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000817}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000818
Douglas Gregor2ada0482009-02-04 17:27:36 +0000819std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000820 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000821}
822
823std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000824 const DeclContext *Ctx = getDeclContext();
825
826 if (Ctx->isFunctionOrMethod())
827 return getNameAsString();
828
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000829 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000830 ContextsTy Contexts;
831
832 // Collect contexts.
833 while (Ctx && isa<NamedDecl>(Ctx)) {
834 Contexts.push_back(Ctx);
835 Ctx = Ctx->getParent();
836 };
837
838 std::string QualName;
839 llvm::raw_string_ostream OS(QualName);
840
841 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
842 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000843 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000844 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000845 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
846 std::string TemplateArgsStr
847 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000848 TemplateArgs.data(),
849 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000850 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000851 OS << Spec->getName() << TemplateArgsStr;
852 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000853 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000854 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000855 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000856 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000857 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
858 if (!RD->getIdentifier())
859 OS << "<anonymous " << RD->getKindName() << '>';
860 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000861 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000862 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000863 const FunctionProtoType *FT = 0;
864 if (FD->hasWrittenPrototype())
865 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
866
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000867 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000868 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000869 unsigned NumParams = FD->getNumParams();
870 for (unsigned i = 0; i < NumParams; ++i) {
871 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000872 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000873 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000874 }
875
876 if (FT->isVariadic()) {
877 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000878 OS << ", ";
879 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000880 }
881 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000882 OS << ')';
883 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000884 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000885 }
886 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000887 }
888
John McCalla2a3f7d2010-03-16 21:48:18 +0000889 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000890 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000891 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000892 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000893
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000894 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000895}
896
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000897bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000898 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
899
Douglas Gregor889ceb72009-02-03 19:21:40 +0000900 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
901 // We want to keep it, unless it nominates same namespace.
902 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000903 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
904 ->getOriginalNamespace() ==
905 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
906 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000907 }
Mike Stump11289f42009-09-09 15:08:12 +0000908
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000909 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
910 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000911 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000912
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000913 // For function templates, the underlying function declarations are linked.
914 if (const FunctionTemplateDecl *FunctionTemplate
915 = dyn_cast<FunctionTemplateDecl>(this))
916 if (const FunctionTemplateDecl *OldFunctionTemplate
917 = dyn_cast<FunctionTemplateDecl>(OldD))
918 return FunctionTemplate->getTemplatedDecl()
919 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000920
Steve Naroffc4173fa2009-02-22 19:35:57 +0000921 // For method declarations, we keep track of redeclarations.
922 if (isa<ObjCMethodDecl>(this))
923 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000924
John McCall9f3059a2009-10-09 21:13:30 +0000925 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
926 return true;
927
John McCall3f746822009-11-17 05:59:44 +0000928 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
929 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
930 cast<UsingShadowDecl>(OldD)->getTargetDecl();
931
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000932 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
933 ASTContext &Context = getASTContext();
934 return Context.getCanonicalNestedNameSpecifier(
935 cast<UsingDecl>(this)->getQualifier()) ==
936 Context.getCanonicalNestedNameSpecifier(
937 cast<UsingDecl>(OldD)->getQualifier());
938 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000939
Douglas Gregorb59643b2012-01-03 23:26:26 +0000940 // A typedef of an Objective-C class type can replace an Objective-C class
941 // declaration or definition, and vice versa.
942 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
943 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
944 return true;
945
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000946 // For non-function declarations, if the declarations are of the
947 // same kind then this must be a redeclaration, or semantic analysis
948 // would not have given us the new declaration.
949 return this->getKind() == OldD->getKind();
950}
951
Douglas Gregoreddf4332009-02-24 20:03:32 +0000952bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000953 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000954}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000955
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000956NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000957 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000958 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
959 ND = UD->getTargetDecl();
960
961 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
962 return AD->getClassInterface();
963
964 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000965}
966
John McCalla8ae2222010-04-06 21:38:20 +0000967bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000968 if (!isCXXClassMember())
969 return false;
970
John McCalla8ae2222010-04-06 21:38:20 +0000971 const NamedDecl *D = this;
972 if (isa<UsingShadowDecl>(D))
973 D = cast<UsingShadowDecl>(D)->getTargetDecl();
974
Francois Pichet783dd6e2010-11-21 06:08:52 +0000975 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000976 return true;
977 if (isa<CXXMethodDecl>(D))
978 return cast<CXXMethodDecl>(D)->isInstance();
979 if (isa<FunctionTemplateDecl>(D))
980 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
981 ->getTemplatedDecl())->isInstance();
982 return false;
983}
984
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000985//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000986// DeclaratorDecl Implementation
987//===----------------------------------------------------------------------===//
988
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000989template <typename DeclT>
990static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
991 if (decl->getNumTemplateParameterLists() > 0)
992 return decl->getTemplateParameterList(0)->getTemplateLoc();
993 else
994 return decl->getInnerLocStart();
995}
996
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000997SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000998 TypeSourceInfo *TSI = getTypeSourceInfo();
999 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001000 return SourceLocation();
1001}
1002
Douglas Gregor14454802011-02-25 02:25:35 +00001003void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1004 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001005 // Make sure the extended decl info is allocated.
1006 if (!hasExtInfo()) {
1007 // Save (non-extended) type source info pointer.
1008 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1009 // Allocate external info struct.
1010 DeclInfo = new (getASTContext()) ExtInfo;
1011 // Restore savedTInfo into (extended) decl info.
1012 getExtInfo()->TInfo = savedTInfo;
1013 }
1014 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001015 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001016 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001017 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001018 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001019 if (getExtInfo()->NumTemplParamLists == 0) {
1020 // Save type source info pointer.
1021 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1022 // Deallocate the extended decl info.
1023 getASTContext().Deallocate(getExtInfo());
1024 // Restore savedTInfo into (non-extended) decl info.
1025 DeclInfo = savedTInfo;
1026 }
1027 else
1028 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001029 }
1030 }
1031}
1032
Abramo Bagnara60804e12011-03-18 15:16:37 +00001033void
1034DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1035 unsigned NumTPLists,
1036 TemplateParameterList **TPLists) {
1037 assert(NumTPLists > 0);
1038 // Make sure the extended decl info is allocated.
1039 if (!hasExtInfo()) {
1040 // Save (non-extended) type source info pointer.
1041 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1042 // Allocate external info struct.
1043 DeclInfo = new (getASTContext()) ExtInfo;
1044 // Restore savedTInfo into (extended) decl info.
1045 getExtInfo()->TInfo = savedTInfo;
1046 }
1047 // Set the template parameter lists info.
1048 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1049}
1050
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001051SourceLocation DeclaratorDecl::getOuterLocStart() const {
1052 return getTemplateOrInnerLocStart(this);
1053}
1054
Abramo Bagnaraea947882011-03-08 16:41:52 +00001055namespace {
1056
1057// Helper function: returns true if QT is or contains a type
1058// having a postfix component.
1059bool typeIsPostfix(clang::QualType QT) {
1060 while (true) {
1061 const Type* T = QT.getTypePtr();
1062 switch (T->getTypeClass()) {
1063 default:
1064 return false;
1065 case Type::Pointer:
1066 QT = cast<PointerType>(T)->getPointeeType();
1067 break;
1068 case Type::BlockPointer:
1069 QT = cast<BlockPointerType>(T)->getPointeeType();
1070 break;
1071 case Type::MemberPointer:
1072 QT = cast<MemberPointerType>(T)->getPointeeType();
1073 break;
1074 case Type::LValueReference:
1075 case Type::RValueReference:
1076 QT = cast<ReferenceType>(T)->getPointeeType();
1077 break;
1078 case Type::PackExpansion:
1079 QT = cast<PackExpansionType>(T)->getPattern();
1080 break;
1081 case Type::Paren:
1082 case Type::ConstantArray:
1083 case Type::DependentSizedArray:
1084 case Type::IncompleteArray:
1085 case Type::VariableArray:
1086 case Type::FunctionProto:
1087 case Type::FunctionNoProto:
1088 return true;
1089 }
1090 }
1091}
1092
1093} // namespace
1094
1095SourceRange DeclaratorDecl::getSourceRange() const {
1096 SourceLocation RangeEnd = getLocation();
1097 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1098 if (typeIsPostfix(TInfo->getType()))
1099 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1100 }
1101 return SourceRange(getOuterLocStart(), RangeEnd);
1102}
1103
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001104void
Douglas Gregor20527e22010-06-15 17:44:38 +00001105QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1106 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001107 TemplateParameterList **TPLists) {
1108 assert((NumTPLists == 0 || TPLists != 0) &&
1109 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001110
1111 // Free previous template parameters (if any).
1112 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001113 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001114 TemplParamLists = 0;
1115 NumTemplParamLists = 0;
1116 }
1117 // Set info on matched template parameter lists (if any).
1118 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001119 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001120 NumTemplParamLists = NumTPLists;
1121 for (unsigned i = NumTPLists; i-- > 0; )
1122 TemplParamLists[i] = TPLists[i];
1123 }
1124}
1125
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001126//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001127// VarDecl Implementation
1128//===----------------------------------------------------------------------===//
1129
Sebastian Redl833ef452010-01-26 22:01:41 +00001130const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1131 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001132 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001133 case SC_Auto: return "auto";
1134 case SC_Extern: return "extern";
1135 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1136 case SC_PrivateExtern: return "__private_extern__";
1137 case SC_Register: return "register";
1138 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001139 }
1140
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001141 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001142}
1143
Abramo Bagnaradff19302011-03-08 08:55:46 +00001144VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1145 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001146 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001147 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001148 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001149}
1150
Douglas Gregor72172e92012-01-05 21:55:30 +00001151VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1152 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1153 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1154 QualType(), 0, SC_None, SC_None);
1155}
1156
Douglas Gregorbf62d642010-12-06 18:36:25 +00001157void VarDecl::setStorageClass(StorageClass SC) {
1158 assert(isLegalForVariable(SC));
1159 if (getStorageClass() != SC)
1160 ClearLinkageCache();
1161
John McCallbeaa11c2011-05-01 02:13:58 +00001162 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001163}
1164
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001165SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001166 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001167 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001168 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001169}
1170
Sebastian Redl833ef452010-01-26 22:01:41 +00001171bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001172 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001173 return false;
1174
Eli Friedman839192f2012-01-15 01:23:58 +00001175 const DeclContext *DC = getDeclContext();
1176 if (DC->isRecord())
1177 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001178
Eli Friedman839192f2012-01-15 01:23:58 +00001179 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001180 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001181 return true;
1182 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001183}
1184
1185VarDecl *VarDecl::getCanonicalDecl() {
1186 return getFirstDeclaration();
1187}
1188
Daniel Dunbar9d355812012-03-09 01:51:51 +00001189VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1190 ASTContext &C) const
1191{
Sebastian Redl35351a92010-01-31 22:27:38 +00001192 // C++ [basic.def]p2:
1193 // A declaration is a definition unless [...] it contains the 'extern'
1194 // specifier or a linkage-specification and neither an initializer [...],
1195 // it declares a static data member in a class declaration [...].
1196 // C++ [temp.expl.spec]p15:
1197 // An explicit specialization of a static data member of a template is a
1198 // definition if the declaration includes an initializer; otherwise, it is
1199 // a declaration.
1200 if (isStaticDataMember()) {
1201 if (isOutOfLine() && (hasInit() ||
1202 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1203 return Definition;
1204 else
1205 return DeclarationOnly;
1206 }
1207 // C99 6.7p5:
1208 // A definition of an identifier is a declaration for that identifier that
1209 // [...] causes storage to be reserved for that object.
1210 // Note: that applies for all non-file-scope objects.
1211 // C99 6.9.2p1:
1212 // If the declaration of an identifier for an object has file scope and an
1213 // initializer, the declaration is an external definition for the identifier
1214 if (hasInit())
1215 return Definition;
1216 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1217 if (hasExternalStorage())
1218 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001219
John McCall8e7d6562010-08-26 03:08:43 +00001220 if (getStorageClassAsWritten() == SC_Extern ||
1221 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001222 for (const VarDecl *PrevVar = getPreviousDecl();
1223 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001224 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1225 return DeclarationOnly;
1226 }
1227 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001228 // C99 6.9.2p2:
1229 // A declaration of an object that has file scope without an initializer,
1230 // and without a storage class specifier or the scs 'static', constitutes
1231 // a tentative definition.
1232 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001233 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001234 return TentativeDefinition;
1235
1236 // What's left is (in C, block-scope) declarations without initializers or
1237 // external storage. These are definitions.
1238 return Definition;
1239}
1240
Sebastian Redl35351a92010-01-31 22:27:38 +00001241VarDecl *VarDecl::getActingDefinition() {
1242 DefinitionKind Kind = isThisDeclarationADefinition();
1243 if (Kind != TentativeDefinition)
1244 return 0;
1245
Chris Lattner48eb14d2010-06-14 18:31:46 +00001246 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001247 VarDecl *First = getFirstDeclaration();
1248 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1249 I != E; ++I) {
1250 Kind = (*I)->isThisDeclarationADefinition();
1251 if (Kind == Definition)
1252 return 0;
1253 else if (Kind == TentativeDefinition)
1254 LastTentative = *I;
1255 }
1256 return LastTentative;
1257}
1258
1259bool VarDecl::isTentativeDefinitionNow() const {
1260 DefinitionKind Kind = isThisDeclarationADefinition();
1261 if (Kind != TentativeDefinition)
1262 return false;
1263
1264 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1265 if ((*I)->isThisDeclarationADefinition() == Definition)
1266 return false;
1267 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001268 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001269}
1270
Daniel Dunbar9d355812012-03-09 01:51:51 +00001271VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001272 VarDecl *First = getFirstDeclaration();
1273 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1274 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001275 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001276 return *I;
1277 }
1278 return 0;
1279}
1280
Daniel Dunbar9d355812012-03-09 01:51:51 +00001281VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001282 DefinitionKind Kind = DeclarationOnly;
1283
1284 const VarDecl *First = getFirstDeclaration();
1285 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001286 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001287 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001288 if (Kind == Definition)
1289 break;
1290 }
John McCall37bb6c92010-10-29 22:22:43 +00001291
1292 return Kind;
1293}
1294
Sebastian Redl5ca79842010-02-01 20:16:42 +00001295const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001296 redecl_iterator I = redecls_begin(), E = redecls_end();
1297 while (I != E && !I->getInit())
1298 ++I;
1299
1300 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001301 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001302 return I->getInit();
1303 }
1304 return 0;
1305}
1306
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001307bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001308 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001309 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001310
1311 if (!isStaticDataMember())
1312 return false;
1313
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001314 // If this static data member was instantiated from a static data member of
1315 // a class template, check whether that static data member was defined
1316 // out-of-line.
1317 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1318 return VD->isOutOfLine();
1319
1320 return false;
1321}
1322
Douglas Gregor1d957a32009-10-27 18:42:08 +00001323VarDecl *VarDecl::getOutOfLineDefinition() {
1324 if (!isStaticDataMember())
1325 return 0;
1326
1327 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1328 RD != RDEnd; ++RD) {
1329 if (RD->getLexicalDeclContext()->isFileContext())
1330 return *RD;
1331 }
1332
1333 return 0;
1334}
1335
Douglas Gregord5058122010-02-11 01:19:42 +00001336void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001337 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1338 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001339 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001340 }
1341
1342 Init = I;
1343}
1344
Daniel Dunbar9d355812012-03-09 01:51:51 +00001345bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001346 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001347
Richard Smith35ecb362012-03-02 04:14:40 +00001348 if (!Lang.CPlusPlus)
1349 return false;
1350
1351 // In C++11, any variable of reference type can be used in a constant
1352 // expression if it is initialized by a constant expression.
1353 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1354 return true;
1355
1356 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001357 // not require the variable to be non-volatile, but we consider this to be a
1358 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001359 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001360 return false;
1361
1362 // In C++, const, non-volatile variables of integral or enumeration types
1363 // can be used in constant expressions.
1364 if (getType()->isIntegralOrEnumerationType())
1365 return true;
1366
Richard Smith35ecb362012-03-02 04:14:40 +00001367 // Additionally, in C++11, non-volatile constexpr variables can be used in
1368 // constant expressions.
1369 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001370}
1371
Richard Smithd0b4dd62011-12-19 06:19:21 +00001372/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1373/// form, which contains extra information on the evaluated value of the
1374/// initializer.
1375EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1376 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1377 if (!Eval) {
1378 Stmt *S = Init.get<Stmt *>();
1379 Eval = new (getASTContext()) EvaluatedStmt;
1380 Eval->Value = S;
1381 Init = Eval;
1382 }
1383 return Eval;
1384}
1385
Richard Smithdafff942012-01-14 04:30:29 +00001386APValue *VarDecl::evaluateValue() const {
1387 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1388 return evaluateValue(Notes);
1389}
1390
1391APValue *VarDecl::evaluateValue(
1392 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001393 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1394
1395 // We only produce notes indicating why an initializer is non-constant the
1396 // first time it is evaluated. FIXME: The notes won't always be emitted the
1397 // first time we try evaluation, so might not be produced at all.
1398 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001399 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001400
1401 const Expr *Init = cast<Expr>(Eval->Value);
1402 assert(!Init->isValueDependent());
1403
1404 if (Eval->IsEvaluating) {
1405 // FIXME: Produce a diagnostic for self-initialization.
1406 Eval->CheckedICE = true;
1407 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001408 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001409 }
1410
1411 Eval->IsEvaluating = true;
1412
1413 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1414 this, Notes);
1415
1416 // Ensure the result is an uninitialized APValue if evaluation fails.
1417 if (!Result)
1418 Eval->Evaluated = APValue();
1419
1420 Eval->IsEvaluating = false;
1421 Eval->WasEvaluated = true;
1422
1423 // In C++11, we have determined whether the initializer was a constant
1424 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001425 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001426 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001427 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001428 }
1429
Richard Smithdafff942012-01-14 04:30:29 +00001430 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001431}
1432
1433bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001434 // Initializers of weak variables are never ICEs.
1435 if (isWeak())
1436 return false;
1437
Richard Smithd0b4dd62011-12-19 06:19:21 +00001438 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1439 if (Eval->CheckedICE)
1440 // We have already checked whether this subexpression is an
1441 // integral constant expression.
1442 return Eval->IsICE;
1443
1444 const Expr *Init = cast<Expr>(Eval->Value);
1445 assert(!Init->isValueDependent());
1446
1447 // In C++11, evaluate the initializer to check whether it's a constant
1448 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001449 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001450 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1451 evaluateValue(Notes);
1452 return Eval->IsICE;
1453 }
1454
1455 // It's an ICE whether or not the definition we found is
1456 // out-of-line. See DR 721 and the discussion in Clang PR
1457 // 6206 for details.
1458
1459 if (Eval->CheckingICE)
1460 return false;
1461 Eval->CheckingICE = true;
1462
1463 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1464 Eval->CheckingICE = false;
1465 Eval->CheckedICE = true;
1466 return Eval->IsICE;
1467}
1468
Douglas Gregorfe314812011-06-21 17:03:29 +00001469bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001470 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001471
1472 const Expr *E = getInit();
1473 if (!E)
1474 return false;
1475
1476 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1477 E = Cleanups->getSubExpr();
1478
1479 return isa<MaterializeTemporaryExpr>(E);
1480}
1481
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001482VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001483 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001484 return cast<VarDecl>(MSI->getInstantiatedFrom());
1485
1486 return 0;
1487}
1488
Douglas Gregor3c74d412009-10-14 20:14:33 +00001489TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001490 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001491 return MSI->getTemplateSpecializationKind();
1492
1493 return TSK_Undeclared;
1494}
1495
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001496MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001497 return getASTContext().getInstantiatedFromStaticDataMember(this);
1498}
1499
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001500void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1501 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001502 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001503 assert(MSI && "Not an instantiated static data member?");
1504 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001505 if (TSK != TSK_ExplicitSpecialization &&
1506 PointOfInstantiation.isValid() &&
1507 MSI->getPointOfInstantiation().isInvalid())
1508 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001509}
1510
Sebastian Redl833ef452010-01-26 22:01:41 +00001511//===----------------------------------------------------------------------===//
1512// ParmVarDecl Implementation
1513//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001514
Sebastian Redl833ef452010-01-26 22:01:41 +00001515ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001516 SourceLocation StartLoc,
1517 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001518 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001519 StorageClass S, StorageClass SCAsWritten,
1520 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001521 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001522 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001523}
1524
Douglas Gregor72172e92012-01-05 21:55:30 +00001525ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1526 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1527 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1528 0, QualType(), 0, SC_None, SC_None, 0);
1529}
1530
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001531SourceRange ParmVarDecl::getSourceRange() const {
1532 if (!hasInheritedDefaultArg()) {
1533 SourceRange ArgRange = getDefaultArgRange();
1534 if (ArgRange.isValid())
1535 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1536 }
1537
1538 return DeclaratorDecl::getSourceRange();
1539}
1540
Sebastian Redl833ef452010-01-26 22:01:41 +00001541Expr *ParmVarDecl::getDefaultArg() {
1542 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1543 assert(!hasUninstantiatedDefaultArg() &&
1544 "Default argument is not yet instantiated!");
1545
1546 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001547 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001548 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001549
Sebastian Redl833ef452010-01-26 22:01:41 +00001550 return Arg;
1551}
1552
Sebastian Redl833ef452010-01-26 22:01:41 +00001553SourceRange ParmVarDecl::getDefaultArgRange() const {
1554 if (const Expr *E = getInit())
1555 return E->getSourceRange();
1556
1557 if (hasUninstantiatedDefaultArg())
1558 return getUninstantiatedDefaultArg()->getSourceRange();
1559
1560 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001561}
1562
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001563bool ParmVarDecl::isParameterPack() const {
1564 return isa<PackExpansionType>(getType());
1565}
1566
Ted Kremenek540017e2011-10-06 05:00:56 +00001567void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1568 getASTContext().setParameterIndex(this, parameterIndex);
1569 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1570}
1571
1572unsigned ParmVarDecl::getParameterIndexLarge() const {
1573 return getASTContext().getParameterIndex(this);
1574}
1575
Nuno Lopes394ec982008-12-17 23:39:55 +00001576//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001577// FunctionDecl Implementation
1578//===----------------------------------------------------------------------===//
1579
Douglas Gregorb11aad82011-02-19 18:51:44 +00001580void FunctionDecl::getNameForDiagnostic(std::string &S,
1581 const PrintingPolicy &Policy,
1582 bool Qualified) const {
1583 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1584 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1585 if (TemplateArgs)
1586 S += TemplateSpecializationType::PrintTemplateArgumentList(
1587 TemplateArgs->data(),
1588 TemplateArgs->size(),
1589 Policy);
1590
1591}
1592
Ted Kremenek186a0742010-04-29 16:49:01 +00001593bool FunctionDecl::isVariadic() const {
1594 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1595 return FT->isVariadic();
1596 return false;
1597}
1598
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001599bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1600 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001601 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001602 Definition = *I;
1603 return true;
1604 }
1605 }
1606
1607 return false;
1608}
1609
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001610bool FunctionDecl::hasTrivialBody() const
1611{
1612 Stmt *S = getBody();
1613 if (!S) {
1614 // Since we don't have a body for this function, we don't know if it's
1615 // trivial or not.
1616 return false;
1617 }
1618
1619 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1620 return true;
1621 return false;
1622}
1623
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001624bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1625 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001626 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001627 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1628 return true;
1629 }
1630 }
1631
1632 return false;
1633}
1634
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001635Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001636 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1637 if (I->Body) {
1638 Definition = *I;
1639 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001640 } else if (I->IsLateTemplateParsed) {
1641 Definition = *I;
1642 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001643 }
1644 }
1645
1646 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001647}
1648
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001649void FunctionDecl::setBody(Stmt *B) {
1650 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001651 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001652 EndRangeLoc = B->getLocEnd();
1653}
1654
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001655void FunctionDecl::setPure(bool P) {
1656 IsPure = P;
1657 if (P)
1658 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1659 Parent->markedVirtualFunctionPure();
1660}
1661
Douglas Gregor16618f22009-09-12 00:17:51 +00001662bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001663 const TranslationUnitDecl *tunit =
1664 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1665 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001666 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001667 getIdentifier() &&
1668 getIdentifier()->isStr("main");
1669}
1670
1671bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1672 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1673 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1674 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1675 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1676 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1677
1678 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1679 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1680
1681 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1682 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1683
1684 ASTContext &Context =
1685 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1686 ->getASTContext();
1687
1688 // The result type and first argument type are constant across all
1689 // these operators. The second argument must be exactly void*.
1690 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001691}
1692
Douglas Gregor16618f22009-09-12 00:17:51 +00001693bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001694 if (getLinkage() != ExternalLinkage)
1695 return false;
1696
1697 if (getAttr<OverloadableAttr>())
1698 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001699
Chandler Carruth4322a282011-02-25 00:05:02 +00001700 const DeclContext *DC = getDeclContext();
1701 if (DC->isRecord())
1702 return false;
1703
Eli Friedman839192f2012-01-15 01:23:58 +00001704 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001705 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001706 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001707
Eli Friedman839192f2012-01-15 01:23:58 +00001708 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001709}
1710
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001711bool FunctionDecl::isGlobal() const {
1712 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1713 return Method->isStatic();
1714
John McCall8e7d6562010-08-26 03:08:43 +00001715 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001716 return false;
1717
Mike Stump11289f42009-09-09 15:08:12 +00001718 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001719 DC->isNamespace();
1720 DC = DC->getParent()) {
1721 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1722 if (!Namespace->getDeclName())
1723 return false;
1724 break;
1725 }
1726 }
1727
1728 return true;
1729}
1730
Sebastian Redl833ef452010-01-26 22:01:41 +00001731void
1732FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1733 redeclarable_base::setPreviousDeclaration(PrevDecl);
1734
1735 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1736 FunctionTemplateDecl *PrevFunTmpl
1737 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1738 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1739 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1740 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001741
Axel Naumannfbc7b982011-11-08 18:21:06 +00001742 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001743 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001744}
1745
1746const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1747 return getFirstDeclaration();
1748}
1749
1750FunctionDecl *FunctionDecl::getCanonicalDecl() {
1751 return getFirstDeclaration();
1752}
1753
Douglas Gregorbf62d642010-12-06 18:36:25 +00001754void FunctionDecl::setStorageClass(StorageClass SC) {
1755 assert(isLegalForFunction(SC));
1756 if (getStorageClass() != SC)
1757 ClearLinkageCache();
1758
1759 SClass = SC;
1760}
1761
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001762/// \brief Returns a value indicating whether this function
1763/// corresponds to a builtin function.
1764///
1765/// The function corresponds to a built-in function if it is
1766/// declared at translation scope or within an extern "C" block and
1767/// its name matches with the name of a builtin. The returned value
1768/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001769/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001770/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001771unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001772 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001773 return 0;
1774
1775 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001776 if (!BuiltinID)
1777 return 0;
1778
1779 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001780 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1781 return BuiltinID;
1782
1783 // This function has the name of a known C library
1784 // function. Determine whether it actually refers to the C library
1785 // function or whether it just has the same name.
1786
Douglas Gregora908e7f2009-02-17 03:23:10 +00001787 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001788 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001789 return 0;
1790
Douglas Gregore711f702009-02-14 18:57:46 +00001791 // If this function is at translation-unit scope and we're not in
1792 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001793 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001794 getDeclContext()->isTranslationUnit())
1795 return BuiltinID;
1796
1797 // If the function is in an extern "C" linkage specification and is
1798 // not marked "overloadable", it's the real function.
1799 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001800 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001801 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001802 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001803 return BuiltinID;
1804
1805 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001806 return 0;
1807}
1808
1809
Chris Lattner47c0d002009-04-25 06:03:53 +00001810/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001811/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001812/// after it has been created.
1813unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001814 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001815 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001816 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001817 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001818
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001819}
1820
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001821void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001822 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001823 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001824 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001825
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001826 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001827 if (!NewParamInfo.empty()) {
1828 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1829 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001830 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001831}
Chris Lattner41943152007-01-25 04:52:46 +00001832
James Molloy6f8780b2012-02-29 10:24:19 +00001833void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1834 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1835
1836 if (!NewDecls.empty()) {
1837 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1838 std::copy(NewDecls.begin(), NewDecls.end(), A);
1839 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1840 }
1841}
1842
Chris Lattner58258242008-04-10 02:22:51 +00001843/// getMinRequiredArguments - Returns the minimum number of arguments
1844/// needed to call this function. This may be fewer than the number of
1845/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001846/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001847unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001848 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001849 return getNumParams();
1850
Douglas Gregor7825bf32011-01-06 22:09:01 +00001851 unsigned NumRequiredArgs = getNumParams();
1852
1853 // If the last parameter is a parameter pack, we don't need an argument for
1854 // it.
1855 if (NumRequiredArgs > 0 &&
1856 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1857 --NumRequiredArgs;
1858
1859 // If this parameter has a default argument, we don't need an argument for
1860 // it.
1861 while (NumRequiredArgs > 0 &&
1862 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001863 --NumRequiredArgs;
1864
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001865 // We might have parameter packs before the end. These can't be deduced,
1866 // but they can still handle multiple arguments.
1867 unsigned ArgIdx = NumRequiredArgs;
1868 while (ArgIdx > 0) {
1869 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1870 NumRequiredArgs = ArgIdx;
1871
1872 --ArgIdx;
1873 }
1874
Chris Lattner58258242008-04-10 02:22:51 +00001875 return NumRequiredArgs;
1876}
1877
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001878bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001879 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001880 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001881
1882 if (isa<CXXMethodDecl>(this)) {
1883 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1884 return true;
1885 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001886
1887 switch (getTemplateSpecializationKind()) {
1888 case TSK_Undeclared:
1889 case TSK_ExplicitSpecialization:
1890 return false;
1891
1892 case TSK_ImplicitInstantiation:
1893 case TSK_ExplicitInstantiationDeclaration:
1894 case TSK_ExplicitInstantiationDefinition:
1895 // Handle below.
1896 break;
1897 }
1898
1899 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001900 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001901 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001902 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001903
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001904 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001905 return PatternDecl->isInlined();
1906
1907 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001908}
1909
Eli Friedman1b125c32012-02-07 03:50:18 +00001910static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1911 // Only consider file-scope declarations in this test.
1912 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1913 return false;
1914
1915 // Only consider explicit declarations; the presence of a builtin for a
1916 // libcall shouldn't affect whether a definition is externally visible.
1917 if (Redecl->isImplicit())
1918 return false;
1919
1920 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1921 return true; // Not an inline definition
1922
1923 return false;
1924}
1925
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001926/// \brief For a function declaration in C or C++, determine whether this
1927/// declaration causes the definition to be externally visible.
1928///
Eli Friedman1b125c32012-02-07 03:50:18 +00001929/// Specifically, this determines if adding the current declaration to the set
1930/// of redeclarations of the given functions causes
1931/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001932bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1933 assert(!doesThisDeclarationHaveABody() &&
1934 "Must have a declaration without a body.");
1935
1936 ASTContext &Context = getASTContext();
1937
David Blaikiebbafb8a2012-03-11 07:00:24 +00001938 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001939 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1940 // an externally visible definition.
1941 //
1942 // FIXME: What happens if gnu_inline gets added on after the first
1943 // declaration?
1944 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1945 return false;
1946
1947 const FunctionDecl *Prev = this;
1948 bool FoundBody = false;
1949 while ((Prev = Prev->getPreviousDecl())) {
1950 FoundBody |= Prev->Body;
1951
1952 if (Prev->Body) {
1953 // If it's not the case that both 'inline' and 'extern' are
1954 // specified on the definition, then it is always externally visible.
1955 if (!Prev->isInlineSpecified() ||
1956 Prev->getStorageClassAsWritten() != SC_Extern)
1957 return false;
1958 } else if (Prev->isInlineSpecified() &&
1959 Prev->getStorageClassAsWritten() != SC_Extern) {
1960 return false;
1961 }
1962 }
1963 return FoundBody;
1964 }
1965
David Blaikiebbafb8a2012-03-11 07:00:24 +00001966 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001967 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001968
1969 // C99 6.7.4p6:
1970 // [...] If all of the file scope declarations for a function in a
1971 // translation unit include the inline function specifier without extern,
1972 // then the definition in that translation unit is an inline definition.
1973 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001974 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001975 const FunctionDecl *Prev = this;
1976 bool FoundBody = false;
1977 while ((Prev = Prev->getPreviousDecl())) {
1978 FoundBody |= Prev->Body;
1979 if (RedeclForcesDefC99(Prev))
1980 return false;
1981 }
1982 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001983}
1984
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001985/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001986/// definition will be externally visible.
1987///
1988/// Inline function definitions are always available for inlining optimizations.
1989/// However, depending on the language dialect, declaration specifiers, and
1990/// attributes, the definition of an inline function may or may not be
1991/// "externally" visible to other translation units in the program.
1992///
1993/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001994/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001995/// inline definition becomes externally visible (C99 6.7.4p6).
1996///
1997/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1998/// definition, we use the GNU semantics for inline, which are nearly the
1999/// opposite of C99 semantics. In particular, "inline" by itself will create
2000/// an externally visible symbol, but "extern inline" will not create an
2001/// externally visible symbol.
2002bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002003 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002004 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002005 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002006
David Blaikiebbafb8a2012-03-11 07:00:24 +00002007 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002008 // Note: If you change the logic here, please change
2009 // doesDeclarationForceExternallyVisibleDefinition as well.
2010 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002011 // If it's not the case that both 'inline' and 'extern' are
2012 // specified on the definition, then this inline definition is
2013 // externally visible.
2014 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2015 return true;
2016
2017 // If any declaration is 'inline' but not 'extern', then this definition
2018 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002019 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2020 Redecl != RedeclEnd;
2021 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002022 if (Redecl->isInlineSpecified() &&
2023 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002024 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002025 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002026
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002027 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002028 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002029
Douglas Gregor299d76e2009-09-13 07:46:26 +00002030 // C99 6.7.4p6:
2031 // [...] If all of the file scope declarations for a function in a
2032 // translation unit include the inline function specifier without extern,
2033 // then the definition in that translation unit is an inline definition.
2034 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2035 Redecl != RedeclEnd;
2036 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002037 if (RedeclForcesDefC99(*Redecl))
2038 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002039 }
2040
2041 // C99 6.7.4p6:
2042 // An inline definition does not provide an external definition for the
2043 // function, and does not forbid an external definition in another
2044 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002045 return false;
2046}
2047
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002048/// getOverloadedOperator - Which C++ overloaded operator this
2049/// function represents, if any.
2050OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002051 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2052 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002053 else
2054 return OO_None;
2055}
2056
Alexis Huntc88db062010-01-13 09:01:02 +00002057/// getLiteralIdentifier - The literal suffix identifier this function
2058/// represents, if any.
2059const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2060 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2061 return getDeclName().getCXXLiteralIdentifier();
2062 else
2063 return 0;
2064}
2065
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002066FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2067 if (TemplateOrSpecialization.isNull())
2068 return TK_NonTemplate;
2069 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2070 return TK_FunctionTemplate;
2071 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2072 return TK_MemberSpecialization;
2073 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2074 return TK_FunctionTemplateSpecialization;
2075 if (TemplateOrSpecialization.is
2076 <DependentFunctionTemplateSpecializationInfo*>())
2077 return TK_DependentFunctionTemplateSpecialization;
2078
David Blaikie83d382b2011-09-23 05:06:16 +00002079 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002080}
2081
Douglas Gregord801b062009-10-07 23:56:10 +00002082FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002083 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002084 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2085
2086 return 0;
2087}
2088
Douglas Gregor06db9f52009-10-12 20:18:28 +00002089MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2090 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2091}
2092
Douglas Gregord801b062009-10-07 23:56:10 +00002093void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002094FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2095 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002096 TemplateSpecializationKind TSK) {
2097 assert(TemplateOrSpecialization.isNull() &&
2098 "Member function is already a specialization");
2099 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002100 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002101 TemplateOrSpecialization = Info;
2102}
2103
Douglas Gregorafca3b42009-10-27 20:53:28 +00002104bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002105 // If the function is invalid, it can't be implicitly instantiated.
2106 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002107 return false;
2108
2109 switch (getTemplateSpecializationKind()) {
2110 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002111 case TSK_ExplicitInstantiationDefinition:
2112 return false;
2113
2114 case TSK_ImplicitInstantiation:
2115 return true;
2116
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002117 // It is possible to instantiate TSK_ExplicitSpecialization kind
2118 // if the FunctionDecl has a class scope specialization pattern.
2119 case TSK_ExplicitSpecialization:
2120 return getClassScopeSpecializationPattern() != 0;
2121
Douglas Gregorafca3b42009-10-27 20:53:28 +00002122 case TSK_ExplicitInstantiationDeclaration:
2123 // Handled below.
2124 break;
2125 }
2126
2127 // Find the actual template from which we will instantiate.
2128 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002129 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002130 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002131 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002132
2133 // C++0x [temp.explicit]p9:
2134 // Except for inline functions, other explicit instantiation declarations
2135 // have the effect of suppressing the implicit instantiation of the entity
2136 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002137 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002138 return true;
2139
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002140 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002141}
2142
2143bool FunctionDecl::isTemplateInstantiation() const {
2144 switch (getTemplateSpecializationKind()) {
2145 case TSK_Undeclared:
2146 case TSK_ExplicitSpecialization:
2147 return false;
2148 case TSK_ImplicitInstantiation:
2149 case TSK_ExplicitInstantiationDeclaration:
2150 case TSK_ExplicitInstantiationDefinition:
2151 return true;
2152 }
2153 llvm_unreachable("All TSK values handled.");
2154}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002155
2156FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002157 // Handle class scope explicit specialization special case.
2158 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2159 return getClassScopeSpecializationPattern();
2160
Douglas Gregorafca3b42009-10-27 20:53:28 +00002161 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2162 while (Primary->getInstantiatedFromMemberTemplate()) {
2163 // If we have hit a point where the user provided a specialization of
2164 // this template, we're done looking.
2165 if (Primary->isMemberSpecialization())
2166 break;
2167
2168 Primary = Primary->getInstantiatedFromMemberTemplate();
2169 }
2170
2171 return Primary->getTemplatedDecl();
2172 }
2173
2174 return getInstantiatedFromMemberFunction();
2175}
2176
Douglas Gregor70d83e22009-06-29 17:30:29 +00002177FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002178 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002179 = TemplateOrSpecialization
2180 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002181 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002182 }
2183 return 0;
2184}
2185
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002186FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2187 return getASTContext().getClassScopeSpecializationPattern(this);
2188}
2189
Douglas Gregor70d83e22009-06-29 17:30:29 +00002190const TemplateArgumentList *
2191FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002192 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002193 = TemplateOrSpecialization
2194 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002195 return Info->TemplateArguments;
2196 }
2197 return 0;
2198}
2199
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002200const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002201FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2202 if (FunctionTemplateSpecializationInfo *Info
2203 = TemplateOrSpecialization
2204 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2205 return Info->TemplateArgumentsAsWritten;
2206 }
2207 return 0;
2208}
2209
Mike Stump11289f42009-09-09 15:08:12 +00002210void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002211FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2212 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002213 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002214 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002215 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002216 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2217 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002218 assert(TSK != TSK_Undeclared &&
2219 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002220 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002221 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002222 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002223 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2224 TemplateArgs,
2225 TemplateArgsAsWritten,
2226 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002227 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002228 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002229}
2230
John McCallb9c78482010-04-08 09:05:18 +00002231void
2232FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2233 const UnresolvedSetImpl &Templates,
2234 const TemplateArgumentListInfo &TemplateArgs) {
2235 assert(TemplateOrSpecialization.isNull());
2236 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2237 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002238 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002239 void *Buffer = Context.Allocate(Size);
2240 DependentFunctionTemplateSpecializationInfo *Info =
2241 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2242 TemplateArgs);
2243 TemplateOrSpecialization = Info;
2244}
2245
2246DependentFunctionTemplateSpecializationInfo::
2247DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2248 const TemplateArgumentListInfo &TArgs)
2249 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2250
2251 d.NumTemplates = Ts.size();
2252 d.NumArgs = TArgs.size();
2253
2254 FunctionTemplateDecl **TsArray =
2255 const_cast<FunctionTemplateDecl**>(getTemplates());
2256 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2257 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2258
2259 TemplateArgumentLoc *ArgsArray =
2260 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2261 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2262 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2263}
2264
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002265TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002266 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002267 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002268 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002269 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002270 if (FTSInfo)
2271 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002272
Douglas Gregord801b062009-10-07 23:56:10 +00002273 MemberSpecializationInfo *MSInfo
2274 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2275 if (MSInfo)
2276 return MSInfo->getTemplateSpecializationKind();
2277
2278 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002279}
2280
Mike Stump11289f42009-09-09 15:08:12 +00002281void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002282FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2283 SourceLocation PointOfInstantiation) {
2284 if (FunctionTemplateSpecializationInfo *FTSInfo
2285 = TemplateOrSpecialization.dyn_cast<
2286 FunctionTemplateSpecializationInfo*>()) {
2287 FTSInfo->setTemplateSpecializationKind(TSK);
2288 if (TSK != TSK_ExplicitSpecialization &&
2289 PointOfInstantiation.isValid() &&
2290 FTSInfo->getPointOfInstantiation().isInvalid())
2291 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2292 } else if (MemberSpecializationInfo *MSInfo
2293 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2294 MSInfo->setTemplateSpecializationKind(TSK);
2295 if (TSK != TSK_ExplicitSpecialization &&
2296 PointOfInstantiation.isValid() &&
2297 MSInfo->getPointOfInstantiation().isInvalid())
2298 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2299 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002300 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002301}
2302
2303SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002304 if (FunctionTemplateSpecializationInfo *FTSInfo
2305 = TemplateOrSpecialization.dyn_cast<
2306 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002307 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002308 else if (MemberSpecializationInfo *MSInfo
2309 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002310 return MSInfo->getPointOfInstantiation();
2311
2312 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002313}
2314
Douglas Gregor6411b922009-09-11 20:15:17 +00002315bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002316 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002317 return true;
2318
2319 // If this function was instantiated from a member function of a
2320 // class template, check whether that member function was defined out-of-line.
2321 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2322 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002323 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002324 return Definition->isOutOfLine();
2325 }
2326
2327 // If this function was instantiated from a function template,
2328 // check whether that function template was defined out-of-line.
2329 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2330 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002331 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002332 return Definition->isOutOfLine();
2333 }
2334
2335 return false;
2336}
2337
Abramo Bagnaraea947882011-03-08 16:41:52 +00002338SourceRange FunctionDecl::getSourceRange() const {
2339 return SourceRange(getOuterLocStart(), EndRangeLoc);
2340}
2341
Anna Zaks28db7ce2012-01-18 02:45:01 +00002342unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002343 IdentifierInfo *FnInfo = getIdentifier();
2344
2345 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002346 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002347
2348 // Builtin handling.
2349 switch (getBuiltinID()) {
2350 case Builtin::BI__builtin_memset:
2351 case Builtin::BI__builtin___memset_chk:
2352 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002353 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002354
2355 case Builtin::BI__builtin_memcpy:
2356 case Builtin::BI__builtin___memcpy_chk:
2357 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002358 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002359
2360 case Builtin::BI__builtin_memmove:
2361 case Builtin::BI__builtin___memmove_chk:
2362 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002363 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002364
2365 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002366 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002367 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002368 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002369
2370 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002371 case Builtin::BImemcmp:
2372 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002373
2374 case Builtin::BI__builtin_strncpy:
2375 case Builtin::BI__builtin___strncpy_chk:
2376 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002377 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002378
2379 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002380 case Builtin::BIstrncmp:
2381 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002382
2383 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002384 case Builtin::BIstrncasecmp:
2385 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002386
2387 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002388 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002389 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002390 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002391
2392 case Builtin::BI__builtin_strndup:
2393 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002394 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002395
Anna Zaks314cd092012-02-01 19:08:57 +00002396 case Builtin::BI__builtin_strlen:
2397 case Builtin::BIstrlen:
2398 return Builtin::BIstrlen;
2399
Anna Zaks201d4892012-01-13 21:52:01 +00002400 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002401 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002402 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002403 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002404 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002405 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002406 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002407 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002408 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002409 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002410 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002411 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002412 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002413 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002414 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002415 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002416 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002417 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002418 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002419 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002420 else if (FnInfo->isStr("strlen"))
2421 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002422 }
2423 break;
2424 }
Anna Zaks22122702012-01-17 00:37:07 +00002425 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002426}
2427
Chris Lattner59a25942008-03-31 00:36:02 +00002428//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002429// FieldDecl Implementation
2430//===----------------------------------------------------------------------===//
2431
Jay Foad39c79802011-01-12 09:06:06 +00002432FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002433 SourceLocation StartLoc, SourceLocation IdLoc,
2434 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002435 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2436 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002437 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002438 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002439}
2440
Douglas Gregor72172e92012-01-05 21:55:30 +00002441FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2442 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2443 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2444 0, QualType(), 0, 0, false, false);
2445}
2446
Sebastian Redl833ef452010-01-26 22:01:41 +00002447bool FieldDecl::isAnonymousStructOrUnion() const {
2448 if (!isImplicit() || getDeclName())
2449 return false;
2450
2451 if (const RecordType *Record = getType()->getAs<RecordType>())
2452 return Record->getDecl()->isAnonymousStructOrUnion();
2453
2454 return false;
2455}
2456
Richard Smithcaf33902011-10-10 18:28:20 +00002457unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2458 assert(isBitField() && "not a bitfield");
2459 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2460 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2461}
2462
John McCall4e819612011-01-20 07:57:12 +00002463unsigned FieldDecl::getFieldIndex() const {
2464 if (CachedFieldIndex) return CachedFieldIndex - 1;
2465
Richard Smithd62306a2011-11-10 06:34:14 +00002466 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002467 const RecordDecl *RD = getParent();
2468 const FieldDecl *LastFD = 0;
2469 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smithd62306a2011-11-10 06:34:14 +00002470
2471 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2472 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002473 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002474
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002475 if (IsMsStruct) {
2476 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie2d7c57e2012-04-30 02:36:29 +00002477 if (getASTContext().ZeroBitfieldFollowsNonBitfield(&*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002478 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002479 continue;
2480 }
David Blaikie2d7c57e2012-04-30 02:36:29 +00002481 LastFD = &*I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002482 }
John McCall4e819612011-01-20 07:57:12 +00002483 }
2484
Richard Smithd62306a2011-11-10 06:34:14 +00002485 assert(CachedFieldIndex && "failed to find field in parent");
2486 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002487}
2488
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002489SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002490 if (const Expr *E = InitializerOrBitWidth.getPointer())
2491 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002492 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002493}
2494
Richard Smith938f40b2011-06-11 17:19:42 +00002495void FieldDecl::setInClassInitializer(Expr *Init) {
2496 assert(!InitializerOrBitWidth.getPointer() &&
2497 "bit width or initializer already set");
2498 InitializerOrBitWidth.setPointer(Init);
2499 InitializerOrBitWidth.setInt(0);
2500}
2501
Sebastian Redl833ef452010-01-26 22:01:41 +00002502//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002503// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002504//===----------------------------------------------------------------------===//
2505
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002506SourceLocation TagDecl::getOuterLocStart() const {
2507 return getTemplateOrInnerLocStart(this);
2508}
2509
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002510SourceRange TagDecl::getSourceRange() const {
2511 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002512 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002513}
2514
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002515TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002516 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002517}
2518
Richard Smithdda56e42011-04-15 14:24:37 +00002519void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2520 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002521 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002522 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002523 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002524}
2525
Douglas Gregordee1be82009-01-17 00:42:38 +00002526void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002527 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002528
2529 if (isa<CXXRecordDecl>(this)) {
2530 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2531 struct CXXRecordDecl::DefinitionData *Data =
2532 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002533 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2534 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002535 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002536}
2537
2538void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002539 assert((!isa<CXXRecordDecl>(this) ||
2540 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2541 "definition completed but not started");
2542
John McCallf937c022011-10-07 06:10:15 +00002543 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002544 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002545
2546 if (ASTMutationListener *L = getASTMutationListener())
2547 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002548}
2549
John McCallf937c022011-10-07 06:10:15 +00002550TagDecl *TagDecl::getDefinition() const {
2551 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002552 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002553 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2554 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002555
2556 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002557 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002558 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002559 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002560
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002561 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002562}
2563
Douglas Gregor14454802011-02-25 02:25:35 +00002564void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2565 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002566 // Make sure the extended qualifier info is allocated.
2567 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002568 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002569 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002570 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002571 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002572 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002573 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002574 if (getExtInfo()->NumTemplParamLists == 0) {
2575 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002576 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002577 }
2578 else
2579 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002580 }
2581 }
2582}
2583
Abramo Bagnara60804e12011-03-18 15:16:37 +00002584void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2585 unsigned NumTPLists,
2586 TemplateParameterList **TPLists) {
2587 assert(NumTPLists > 0);
2588 // Make sure the extended decl info is allocated.
2589 if (!hasExtInfo())
2590 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002591 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002592 // Set the template parameter lists info.
2593 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2594}
2595
Ted Kremenek21475702008-09-05 17:16:31 +00002596//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002597// EnumDecl Implementation
2598//===----------------------------------------------------------------------===//
2599
David Blaikie68e081d2011-12-20 02:48:34 +00002600void EnumDecl::anchor() { }
2601
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002602EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2603 SourceLocation StartLoc, SourceLocation IdLoc,
2604 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002605 EnumDecl *PrevDecl, bool IsScoped,
2606 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002607 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002608 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002609 C.getTypeDeclType(Enum, PrevDecl);
2610 return Enum;
2611}
2612
Douglas Gregor72172e92012-01-05 21:55:30 +00002613EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2614 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2615 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2616 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002617}
2618
Douglas Gregord5058122010-02-11 01:19:42 +00002619void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002620 QualType NewPromotionType,
2621 unsigned NumPositiveBits,
2622 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002623 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002624 if (!IntegerType)
2625 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002626 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002627 setNumPositiveBits(NumPositiveBits);
2628 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002629 TagDecl::completeDefinition();
2630}
2631
Richard Smith7d137e32012-03-23 03:33:32 +00002632TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2633 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2634 return MSI->getTemplateSpecializationKind();
2635
2636 return TSK_Undeclared;
2637}
2638
2639void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2640 SourceLocation PointOfInstantiation) {
2641 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2642 assert(MSI && "Not an instantiated member enumeration?");
2643 MSI->setTemplateSpecializationKind(TSK);
2644 if (TSK != TSK_ExplicitSpecialization &&
2645 PointOfInstantiation.isValid() &&
2646 MSI->getPointOfInstantiation().isInvalid())
2647 MSI->setPointOfInstantiation(PointOfInstantiation);
2648}
2649
Richard Smith4b38ded2012-03-14 23:13:10 +00002650EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2651 if (SpecializationInfo)
2652 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2653
2654 return 0;
2655}
2656
2657void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2658 TemplateSpecializationKind TSK) {
2659 assert(!SpecializationInfo && "Member enum is already a specialization");
2660 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2661}
2662
Sebastian Redl833ef452010-01-26 22:01:41 +00002663//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002664// RecordDecl Implementation
2665//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002666
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002667RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2668 SourceLocation StartLoc, SourceLocation IdLoc,
2669 IdentifierInfo *Id, RecordDecl *PrevDecl)
2670 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002671 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002672 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002673 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002674 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002675 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002676}
2677
Jay Foad39c79802011-01-12 09:06:06 +00002678RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002679 SourceLocation StartLoc, SourceLocation IdLoc,
2680 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2681 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2682 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002683 C.getTypeDeclType(R, PrevDecl);
2684 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002685}
2686
Douglas Gregor72172e92012-01-05 21:55:30 +00002687RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2688 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2689 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2690 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002691}
2692
Douglas Gregordfcad112009-03-25 15:59:44 +00002693bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002694 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002695 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2696}
2697
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002698RecordDecl::field_iterator RecordDecl::field_begin() const {
2699 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2700 LoadFieldsFromExternalStorage();
2701
2702 return field_iterator(decl_iterator(FirstDecl));
2703}
2704
Douglas Gregorb11aad82011-02-19 18:51:44 +00002705/// completeDefinition - Notes that the definition of this type is now
2706/// complete.
2707void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002708 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002709 TagDecl::completeDefinition();
2710}
2711
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002712void RecordDecl::LoadFieldsFromExternalStorage() const {
2713 ExternalASTSource *Source = getASTContext().getExternalSource();
2714 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2715
2716 // Notify that we have a RecordDecl doing some initialization.
2717 ExternalASTSource::Deserializing TheFields(Source);
2718
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002719 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002720 LoadedFieldsFromExternalStorage = true;
2721 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2722 case ELR_Success:
2723 break;
2724
2725 case ELR_AlreadyLoaded:
2726 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002727 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002728 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002729
2730#ifndef NDEBUG
2731 // Check that all decls we got were FieldDecls.
2732 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2733 assert(isa<FieldDecl>(Decls[i]));
2734#endif
2735
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002736 if (Decls.empty())
2737 return;
2738
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002739 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2740 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002741}
2742
Steve Naroff415d3d52008-10-08 17:01:13 +00002743//===----------------------------------------------------------------------===//
2744// BlockDecl Implementation
2745//===----------------------------------------------------------------------===//
2746
David Blaikie9c70e042011-09-21 18:16:56 +00002747void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002748 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002749
Steve Naroffc4b30e52009-03-13 16:56:44 +00002750 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002751 if (!NewParamInfo.empty()) {
2752 NumParams = NewParamInfo.size();
2753 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2754 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002755 }
2756}
2757
John McCall351762c2011-02-07 10:33:21 +00002758void BlockDecl::setCaptures(ASTContext &Context,
2759 const Capture *begin,
2760 const Capture *end,
2761 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002762 CapturesCXXThis = capturesCXXThis;
2763
2764 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002765 NumCaptures = 0;
2766 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002767 return;
2768 }
2769
John McCall351762c2011-02-07 10:33:21 +00002770 NumCaptures = end - begin;
2771
2772 // Avoid new Capture[] because we don't want to provide a default
2773 // constructor.
2774 size_t allocationSize = NumCaptures * sizeof(Capture);
2775 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2776 memcpy(buffer, begin, allocationSize);
2777 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002778}
Sebastian Redl833ef452010-01-26 22:01:41 +00002779
John McCallce45f882011-06-15 22:51:16 +00002780bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2781 for (capture_const_iterator
2782 i = capture_begin(), e = capture_end(); i != e; ++i)
2783 // Only auto vars can be captured, so no redeclaration worries.
2784 if (i->getVariable() == variable)
2785 return true;
2786
2787 return false;
2788}
2789
Douglas Gregor70226da2010-12-21 16:27:07 +00002790SourceRange BlockDecl::getSourceRange() const {
2791 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2792}
Sebastian Redl833ef452010-01-26 22:01:41 +00002793
2794//===----------------------------------------------------------------------===//
2795// Other Decl Allocation/Deallocation Method Implementations
2796//===----------------------------------------------------------------------===//
2797
David Blaikie68e081d2011-12-20 02:48:34 +00002798void TranslationUnitDecl::anchor() { }
2799
Sebastian Redl833ef452010-01-26 22:01:41 +00002800TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2801 return new (C) TranslationUnitDecl(C);
2802}
2803
David Blaikie68e081d2011-12-20 02:48:34 +00002804void LabelDecl::anchor() { }
2805
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002806LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002807 SourceLocation IdentL, IdentifierInfo *II) {
2808 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2809}
2810
2811LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2812 SourceLocation IdentL, IdentifierInfo *II,
2813 SourceLocation GnuLabelL) {
2814 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2815 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002816}
2817
Douglas Gregor72172e92012-01-05 21:55:30 +00002818LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2819 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2820 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002821}
2822
David Blaikie68e081d2011-12-20 02:48:34 +00002823void ValueDecl::anchor() { }
2824
2825void ImplicitParamDecl::anchor() { }
2826
Sebastian Redl833ef452010-01-26 22:01:41 +00002827ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002828 SourceLocation IdLoc,
2829 IdentifierInfo *Id,
2830 QualType Type) {
2831 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002832}
2833
Douglas Gregor72172e92012-01-05 21:55:30 +00002834ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2835 unsigned ID) {
2836 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2837 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2838}
2839
Sebastian Redl833ef452010-01-26 22:01:41 +00002840FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002841 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002842 const DeclarationNameInfo &NameInfo,
2843 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002844 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002845 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002846 bool hasWrittenPrototype,
2847 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002848 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2849 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002850 isInlineSpecified,
2851 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002852 New->HasWrittenPrototype = hasWrittenPrototype;
2853 return New;
2854}
2855
Douglas Gregor72172e92012-01-05 21:55:30 +00002856FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2857 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2858 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2859 DeclarationNameInfo(), QualType(), 0,
2860 SC_None, SC_None, false, false);
2861}
2862
Sebastian Redl833ef452010-01-26 22:01:41 +00002863BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2864 return new (C) BlockDecl(DC, L);
2865}
2866
Douglas Gregor72172e92012-01-05 21:55:30 +00002867BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2868 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2869 return new (Mem) BlockDecl(0, SourceLocation());
2870}
2871
Sebastian Redl833ef452010-01-26 22:01:41 +00002872EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2873 SourceLocation L,
2874 IdentifierInfo *Id, QualType T,
2875 Expr *E, const llvm::APSInt &V) {
2876 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2877}
2878
Douglas Gregor72172e92012-01-05 21:55:30 +00002879EnumConstantDecl *
2880EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2881 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2882 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2883 llvm::APSInt());
2884}
2885
David Blaikie68e081d2011-12-20 02:48:34 +00002886void IndirectFieldDecl::anchor() { }
2887
Benjamin Kramer39593702010-11-21 14:11:41 +00002888IndirectFieldDecl *
2889IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2890 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2891 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002892 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2893}
2894
Douglas Gregor72172e92012-01-05 21:55:30 +00002895IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2896 unsigned ID) {
2897 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2898 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2899 QualType(), 0, 0);
2900}
2901
Douglas Gregorbe996932010-09-01 20:41:53 +00002902SourceRange EnumConstantDecl::getSourceRange() const {
2903 SourceLocation End = getLocation();
2904 if (Init)
2905 End = Init->getLocEnd();
2906 return SourceRange(getLocation(), End);
2907}
2908
David Blaikie68e081d2011-12-20 02:48:34 +00002909void TypeDecl::anchor() { }
2910
Sebastian Redl833ef452010-01-26 22:01:41 +00002911TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002912 SourceLocation StartLoc, SourceLocation IdLoc,
2913 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2914 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002915}
2916
David Blaikie68e081d2011-12-20 02:48:34 +00002917void TypedefNameDecl::anchor() { }
2918
Douglas Gregor72172e92012-01-05 21:55:30 +00002919TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2920 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2921 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2922}
2923
Richard Smithdda56e42011-04-15 14:24:37 +00002924TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2925 SourceLocation StartLoc,
2926 SourceLocation IdLoc, IdentifierInfo *Id,
2927 TypeSourceInfo *TInfo) {
2928 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2929}
2930
Douglas Gregor72172e92012-01-05 21:55:30 +00002931TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2932 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2933 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2934}
2935
Abramo Bagnaraea947882011-03-08 16:41:52 +00002936SourceRange TypedefDecl::getSourceRange() const {
2937 SourceLocation RangeEnd = getLocation();
2938 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2939 if (typeIsPostfix(TInfo->getType()))
2940 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2941 }
2942 return SourceRange(getLocStart(), RangeEnd);
2943}
2944
Richard Smithdda56e42011-04-15 14:24:37 +00002945SourceRange TypeAliasDecl::getSourceRange() const {
2946 SourceLocation RangeEnd = getLocStart();
2947 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2948 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2949 return SourceRange(getLocStart(), RangeEnd);
2950}
2951
David Blaikie68e081d2011-12-20 02:48:34 +00002952void FileScopeAsmDecl::anchor() { }
2953
Sebastian Redl833ef452010-01-26 22:01:41 +00002954FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002955 StringLiteral *Str,
2956 SourceLocation AsmLoc,
2957 SourceLocation RParenLoc) {
2958 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002959}
Douglas Gregorba345522011-12-02 23:23:56 +00002960
Douglas Gregor72172e92012-01-05 21:55:30 +00002961FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2962 unsigned ID) {
2963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2964 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2965}
2966
Douglas Gregorba345522011-12-02 23:23:56 +00002967//===----------------------------------------------------------------------===//
2968// ImportDecl Implementation
2969//===----------------------------------------------------------------------===//
2970
2971/// \brief Retrieve the number of module identifiers needed to name the given
2972/// module.
2973static unsigned getNumModuleIdentifiers(Module *Mod) {
2974 unsigned Result = 1;
2975 while (Mod->Parent) {
2976 Mod = Mod->Parent;
2977 ++Result;
2978 }
2979 return Result;
2980}
2981
Douglas Gregor22d09742012-01-03 18:04:46 +00002982ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002983 Module *Imported,
2984 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00002985 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002986 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002987{
2988 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
2989 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
2990 memcpy(StoredLocs, IdentifierLocs.data(),
2991 IdentifierLocs.size() * sizeof(SourceLocation));
2992}
2993
Douglas Gregor22d09742012-01-03 18:04:46 +00002994ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00002995 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00002996 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00002997 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00002998{
2999 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3000}
3001
3002ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003003 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003004 ArrayRef<SourceLocation> IdentifierLocs) {
3005 void *Mem = C.Allocate(sizeof(ImportDecl) +
3006 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003007 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003008}
3009
3010ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003011 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003012 Module *Imported,
3013 SourceLocation EndLoc) {
3014 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003015 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003016 Import->setImplicit();
3017 return Import;
3018}
3019
Douglas Gregor72172e92012-01-05 21:55:30 +00003020ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3021 unsigned NumLocations) {
3022 void *Mem = AllocateDeserializedDecl(C, ID,
3023 (sizeof(ImportDecl) +
3024 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003025 return new (Mem) ImportDecl(EmptyShell());
3026}
3027
3028ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3029 if (!ImportedAndComplete.getInt())
3030 return ArrayRef<SourceLocation>();
3031
3032 const SourceLocation *StoredLocs
3033 = reinterpret_cast<const SourceLocation *>(this + 1);
3034 return ArrayRef<SourceLocation>(StoredLocs,
3035 getNumModuleIdentifiers(getImportedModule()));
3036}
3037
3038SourceRange ImportDecl::getSourceRange() const {
3039 if (!ImportedAndComplete.getInt())
3040 return SourceRange(getLocation(),
3041 *reinterpret_cast<const SourceLocation *>(this + 1));
3042
3043 return SourceRange(getLocation(), getIdentifierLocs().back());
3044}