blob: a98e8dddbb825e6d05ca2875a322b4bc536e2d23 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000027#include "clang/Basic/Module.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000028#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000029#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000031
David Blaikie4278c652011-09-21 18:16:56 +000032#include <algorithm>
33
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattnerd3b90652008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor4421d2b2011-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 McCall1fb0caa2010-10-22 21:05:15 +000051 }
Douglas Gregor4421d2b2011-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 Gregorbcfd1f52011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-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 McCall1fb0caa2010-10-22 21:05:15 +000065}
66
John McCallaf146032010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallaf146032010-10-30 11:50:40 +000068
Rafael Espindola093ecc92012-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 Gregor0b6bc8b2010-02-03 09:33:45 +000074/// \brief Get the most restrictive linkage for the types in the given
75/// template parameter list.
Rafael Espindola093ecc92012-01-14 00:30:36 +000076static LinkageInfo
John McCall1fb0caa2010-10-22 21:05:15 +000077getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola093ecc92012-01-14 00:30:36 +000078 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000079 for (TemplateParameterList::const_iterator P = Params->begin(),
80 PEnd = Params->end();
81 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-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 Espindola093ecc92012-01-14 00:30:36 +000087 LV.merge(getLVForType(T));
Douglas Gregor6952f1e2011-01-19 20:10:05 +000088 }
89 continue;
90 }
Rafael Espindolab5d763d2012-01-02 06:26:22 +000091
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000092 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +000093 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000094 continue;
95 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +000096 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000097
98 if (TemplateTemplateParmDecl *TTP
99 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000100 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000101 }
102 }
103
John McCall1fb0caa2010-10-22 21:05:15 +0000104 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000105}
106
Douglas Gregor381d34e2010-12-06 18:36:25 +0000107/// getLVForDecl - Get the linkage and visibility for the given declaration.
Rafael Espindola1266b612012-04-21 23:28:21 +0000108static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000109
Douglas Gregor0b6bc8b2010-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 Espindola093ecc92012-01-14 00:30:36 +0000112static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
113 unsigned NumArgs,
Rafael Espindola1266b612012-04-21 23:28:21 +0000114 bool OnlyTemplate) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000115 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-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 Espindolab5d763d2012-01-02 06:26:22 +0000123
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000124 case TemplateArgument::Type:
Rafael Espindola923b0c92012-04-23 17:51:55 +0000125 LV.mergeWithMin(getLVForType(Args[I].getAsType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000126 break;
127
128 case TemplateArgument::Declaration:
John McCall1fb0caa2010-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 Gregor89d63e52010-12-06 18:50:56 +0000132 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Rafael Espindola923b0c92012-04-23 17:51:55 +0000133 LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate));
John McCall1fb0caa2010-10-22 21:05:15 +0000134 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000135 break;
136
137 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000138 case TemplateArgument::TemplateExpansion:
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000139 if (TemplateDecl *Template
Douglas Gregora7fc9012011-01-05 18:58:31 +0000140 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola923b0c92012-04-23 17:51:55 +0000141 LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000142 break;
143
144 case TemplateArgument::Pack:
Rafael Espindola860097c2012-02-23 04:17:32 +0000145 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
146 Args[I].pack_size(),
Rafael Espindola1266b612012-04-21 23:28:21 +0000147 OnlyTemplate));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000148 break;
149 }
150 }
151
John McCall1fb0caa2010-10-22 21:05:15 +0000152 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000153}
154
Rafael Espindola093ecc92012-01-14 00:30:36 +0000155static LinkageInfo
Douglas Gregor381d34e2010-12-06 18:36:25 +0000156getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola1266b612012-04-21 23:28:21 +0000157 bool OnlyTemplate) {
158 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall3cdfc4d2010-08-13 08:35:10 +0000159}
160
John McCall6ce51ee2011-06-27 23:06:04 +0000161static bool shouldConsiderTemplateLV(const FunctionDecl *fn,
162 const FunctionTemplateSpecializationInfo *spec) {
163 return !(spec->isExplicitSpecialization() &&
164 fn->hasAttr<VisibilityAttr>());
165}
166
167static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
168 return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
169}
170
Rafael Espindola1266b612012-04-21 23:28:21 +0000171static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
172 bool OnlyTemplate) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000173 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000174 "Not a name having namespace scope");
175 ASTContext &Context = D->getASTContext();
176
177 // C++ [basic.link]p3:
178 // A name having namespace scope (3.3.6) has internal linkage if it
179 // is the name of
180 // - an object, reference, function or function template that is
181 // explicitly declared static; or,
182 // (This bullet corresponds to C99 6.2.2p3.)
183 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
184 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000185 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000186 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000187
188 // - an object or reference that is explicitly declared const
189 // and neither explicitly declared extern nor previously
190 // declared to have external linkage; or
191 // (there is no equivalent in C99)
David Blaikie4e4d0842012-03-11 07:00:24 +0000192 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000193 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000194 Var->getStorageClass() != SC_Extern &&
195 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000196 bool FoundExtern = false;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000197 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000198 PrevVar && !FoundExtern;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000199 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000200 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000201 FoundExtern = true;
202
203 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000204 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000205 }
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000206 if (Var->getStorageClass() == SC_None) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000207 const VarDecl *PrevVar = Var->getPreviousDecl();
208 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000209 if (PrevVar->getStorageClass() == SC_PrivateExtern)
210 break;
211 if (PrevVar)
212 return PrevVar->getLinkageAndVisibility();
213 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000214 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000215 // C++ [temp]p4:
216 // A non-member function template can have internal linkage; any
217 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000218 const FunctionDecl *Function = 0;
219 if (const FunctionTemplateDecl *FunTmpl
220 = dyn_cast<FunctionTemplateDecl>(D))
221 Function = FunTmpl->getTemplatedDecl();
222 else
223 Function = cast<FunctionDecl>(D);
224
225 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000226 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000227 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000228 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
229 // - a data member of an anonymous union.
230 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000231 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000232 }
233
Chandler Carruth094b6432011-02-24 19:03:39 +0000234 if (D->isInAnonymousNamespace()) {
235 const VarDecl *Var = dyn_cast<VarDecl>(D);
236 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman750dc2b2012-01-15 01:23:58 +0000237 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
238 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth094b6432011-02-24 19:03:39 +0000239 return LinkageInfo::uniqueExternal();
240 }
John McCalle7bc9722010-10-28 04:18:25 +0000241
John McCall1fb0caa2010-10-22 21:05:15 +0000242 // Set up the defaults.
243
244 // C99 6.2.2p5:
245 // If the declaration of an identifier for an object has file
246 // scope and no storage-class specifier, its linkage is
247 // external.
John McCallaf146032010-10-30 11:50:40 +0000248 LinkageInfo LV;
249
Rafael Espindola1266b612012-04-21 23:28:21 +0000250 if (!OnlyTemplate) {
Rafael Espindolae9836a22012-04-16 18:46:26 +0000251 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000252 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000253 } else {
254 // If we're declared in a namespace with a visibility attribute,
255 // use that namespace's visibility, but don't call it explicit.
256 for (const DeclContext *DC = D->getDeclContext();
257 !isa<TranslationUnitDecl>(DC);
258 DC = DC->getParent()) {
259 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
260 if (!ND) continue;
261 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000262 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000263 break;
264 }
265 }
266 }
267 }
268
Rafael Espindola1266b612012-04-21 23:28:21 +0000269 if (!OnlyTemplate)
Rafael Espindola4fc14902012-04-19 04:37:16 +0000270 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolaff257982012-04-19 02:55:01 +0000271
Douglas Gregord85b5b92009-11-25 22:24:25 +0000272 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000273
Douglas Gregord85b5b92009-11-25 22:24:25 +0000274 // A name having namespace scope has external linkage if it is the
275 // name of
276 //
277 // - an object or reference, unless it has internal linkage; or
278 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000279 // GCC applies the following optimization to variables and static
280 // data members, but not to functions:
281 //
John McCall1fb0caa2010-10-22 21:05:15 +0000282 // Modify the variable's LV by the LV of its type unless this is
283 // C or extern "C". This follows from [basic.link]p9:
284 // A type without linkage shall not be used as the type of a
285 // variable or function with external linkage unless
286 // - the entity has C language linkage, or
287 // - the entity is declared within an unnamed namespace, or
288 // - the entity is not used or is defined in the same
289 // translation unit.
290 // and [basic.link]p10:
291 // ...the types specified by all declarations referring to a
292 // given variable or function shall be identical...
293 // C does not have an equivalent rule.
294 //
John McCallac65c622010-10-26 04:59:26 +0000295 // Ignore this if we've got an explicit attribute; the user
296 // probably knows what they're doing.
297 //
John McCall1fb0caa2010-10-22 21:05:15 +0000298 // Note that we don't want to make the variable non-external
299 // because of this, but unique-external linkage suits us.
David Blaikie4e4d0842012-03-11 07:00:24 +0000300 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000301 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000302 LinkageInfo TypeLV = getLVForType(Var->getType());
303 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000304 return LinkageInfo::uniqueExternal();
Rafael Espindolad70d20a2012-04-19 05:24:05 +0000305 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000306 }
307
John McCall35cebc32010-11-02 18:38:13 +0000308 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000309 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000310
David Blaikie4e4d0842012-03-11 07:00:24 +0000311 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000312 (Var->getStorageClass() == SC_Extern ||
313 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000314
Douglas Gregord85b5b92009-11-25 22:24:25 +0000315 // C99 6.2.2p4:
316 // For an identifier declared with the storage-class specifier
317 // extern in a scope in which a prior declaration of that
318 // identifier is visible, if the prior declaration specifies
319 // internal or external linkage, the linkage of the identifier
320 // at the later declaration is the same as the linkage
321 // specified at the prior declaration. If no prior declaration
322 // is visible, or if the prior declaration specifies no
323 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000324 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000325 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000326 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
327 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000328 }
329 }
330
Douglas Gregord85b5b92009-11-25 22:24:25 +0000331 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000332 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000333 // In theory, we can modify the function's LV by the LV of its
334 // type unless it has C linkage (see comment above about variables
335 // for justification). In practice, GCC doesn't do this, so it's
336 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000337
John McCall35cebc32010-11-02 18:38:13 +0000338 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000339 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000340
Douglas Gregord85b5b92009-11-25 22:24:25 +0000341 // C99 6.2.2p5:
342 // If the declaration of an identifier for a function has no
343 // storage-class specifier, its linkage is determined exactly
344 // as if it were declared with the storage-class specifier
345 // extern.
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000347 (Function->getStorageClass() == SC_Extern ||
348 Function->getStorageClass() == SC_PrivateExtern ||
349 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000350 // C99 6.2.2p4:
351 // For an identifier declared with the storage-class specifier
352 // extern in a scope in which a prior declaration of that
353 // identifier is visible, if the prior declaration specifies
354 // internal or external linkage, the linkage of the identifier
355 // at the later declaration is the same as the linkage
356 // specified at the prior declaration. If no prior declaration
357 // is visible, or if the prior declaration specifies no
358 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000359 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000360 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000361 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
362 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000363 }
364 }
365
John McCallaf8ca372011-02-10 06:50:24 +0000366 // In C++, then if the type of the function uses a type with
367 // unique-external linkage, it's not legally usable from outside
368 // this translation unit. However, we should use the C linkage
369 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000370 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000371 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000372 Function->getType()->getLinkage() == UniqueExternalLinkage)
373 return LinkageInfo::uniqueExternal();
374
John McCall6ce51ee2011-06-27 23:06:04 +0000375 // Consider LV from the template and the template arguments unless
376 // this is an explicit specialization with a visibility attribute.
377 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000378 = Function->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000379 if (shouldConsiderTemplateLV(Function, specInfo)) {
380 LV.merge(getLVForDecl(specInfo->getTemplate(),
Rafael Espindola1266b612012-04-21 23:28:21 +0000381 true));
John McCall6ce51ee2011-06-27 23:06:04 +0000382 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola1266b612012-04-21 23:28:21 +0000383 LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs,
384 OnlyTemplate));
John McCall6ce51ee2011-06-27 23:06:04 +0000385 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000386 }
387
Douglas Gregord85b5b92009-11-25 22:24:25 +0000388 // - a named class (Clause 9), or an unnamed class defined in a
389 // typedef declaration in which the class has the typedef name
390 // for linkage purposes (7.1.3); or
391 // - a named enumeration (7.2), or an unnamed enumeration
392 // defined in a typedef declaration in which the enumeration
393 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000394 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
395 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000396 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000397 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000398
John McCall1fb0caa2010-10-22 21:05:15 +0000399 // If this is a class template specialization, consider the
400 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000401 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000402 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000403 if (shouldConsiderTemplateLV(spec)) {
404 // From the template.
405 LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
Rafael Espindola1266b612012-04-21 23:28:21 +0000406 true));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000407
John McCall6ce51ee2011-06-27 23:06:04 +0000408 // The arguments at which the template was instantiated.
409 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
Rafael Espindolaf6a8b9c2012-04-22 15:31:59 +0000410 LV.merge(getLVForTemplateArgumentList(TemplateArgs,
411 OnlyTemplate));
John McCall6ce51ee2011-06-27 23:06:04 +0000412 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000413 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000414
415 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000416 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000417 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
418 OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000419 if (!isExternalLinkage(EnumLV.linkage()))
420 return LinkageInfo::none();
421 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000422
423 // - a template, unless it is a function template that has
424 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000425 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola60115a02012-04-22 00:43:48 +0000426 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregord85b5b92009-11-25 22:24:25 +0000427 // - a namespace (7.3), unless it is declared within an unnamed
428 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000429 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
430 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000431
John McCall1fb0caa2010-10-22 21:05:15 +0000432 // By extension, we assign external linkage to Objective-C
433 // interfaces.
434 } else if (isa<ObjCInterfaceDecl>(D)) {
435 // fallout
436
437 // Everything not covered here has no linkage.
438 } else {
John McCallaf146032010-10-30 11:50:40 +0000439 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000440 }
441
442 // If we ended up with non-external linkage, visibility should
443 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000444 if (LV.linkage() != ExternalLinkage)
445 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000446
John McCall1fb0caa2010-10-22 21:05:15 +0000447 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000448}
449
Rafael Espindola1266b612012-04-21 23:28:21 +0000450static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall1fb0caa2010-10-22 21:05:15 +0000451 // Only certain class members have linkage. Note that fields don't
452 // really have linkage, but it's convenient to say they do for the
453 // purposes of calculating linkage of pointer-to-data-member
454 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000455 if (!(isa<CXXMethodDecl>(D) ||
456 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000457 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000458 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000459 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000460 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000461
John McCall36987482010-11-02 01:45:15 +0000462 LinkageInfo LV;
463
John McCall36987482010-11-02 01:45:15 +0000464 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola1266b612012-04-21 23:28:21 +0000465 if (!OnlyTemplate) {
Rafael Espindola41574542012-04-19 04:27:47 +0000466 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000467 LV.mergeVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000468 }
Rafael Espindolac7e60602012-04-19 05:50:08 +0000469
470 // If this class member has an explicit visibility attribute, the only
471 // thing that can change its visibility is the template arguments, so
472 // only look for them when processing the the class.
Rafael Espindola1266b612012-04-21 23:28:21 +0000473 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola0f905902012-04-16 18:25:01 +0000474
475 // If we're paying attention to global visibility, apply
476 // -finline-visibility-hidden if this is an inline method.
477 //
478 // Note that we do this before merging information about
479 // the class visibility.
480 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
481 TemplateSpecializationKind TSK = TSK_Undeclared;
482 if (FunctionTemplateSpecializationInfo *spec
483 = MD->getTemplateSpecializationInfo()) {
484 TSK = spec->getTemplateSpecializationKind();
485 } else if (MemberSpecializationInfo *MSI =
486 MD->getMemberSpecializationInfo()) {
487 TSK = MSI->getTemplateSpecializationKind();
488 }
489
490 const FunctionDecl *Def = 0;
491 // InlineVisibilityHidden only applies to definitions, and
492 // isInlined() only gives meaningful answers on definitions
493 // anyway.
494 if (TSK != TSK_ExplicitInstantiationDeclaration &&
495 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola1266b612012-04-21 23:28:21 +0000496 !OnlyTemplate &&
Rafael Espindola0f905902012-04-16 18:25:01 +0000497 !LV.visibilityExplicit() &&
498 MD->getASTContext().getLangOpts().InlineVisibilityHidden &&
499 MD->hasBody(Def) && Def->isInlined())
500 LV.mergeVisibility(HiddenVisibility, true);
501 }
John McCallaf146032010-10-30 11:50:40 +0000502
Rafael Espindolac7e60602012-04-19 05:50:08 +0000503 // If this member has an visibility attribute, ClassF will exclude
504 // attributes on the class or command line options, keeping only information
505 // about the template instantiation. If the member has no visibility
506 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
507 // produces the desired result.
Rafael Espindola1266b612012-04-21 23:28:21 +0000508 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
509 ClassOnlyTemplate));
John McCall36987482010-11-02 01:45:15 +0000510 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000511 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000512
513 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000514 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000515 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000516
Rafael Espindola1266b612012-04-21 23:28:21 +0000517 if (!OnlyTemplate)
Rafael Espindola4fc14902012-04-19 04:37:16 +0000518 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaff257982012-04-19 02:55:01 +0000519
John McCall3cdfc4d2010-08-13 08:35:10 +0000520 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000521 // If the type of the function uses a type with unique-external
522 // linkage, it's not legally usable from outside this translation unit.
523 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
524 return LinkageInfo::uniqueExternal();
525
John McCall1fb0caa2010-10-22 21:05:15 +0000526 // If this is a method template specialization, use the linkage for
527 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000528 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000529 = MD->getTemplateSpecializationInfo()) {
John McCall6ce51ee2011-06-27 23:06:04 +0000530 if (shouldConsiderTemplateLV(MD, spec)) {
Rafael Espindola860097c2012-02-23 04:17:32 +0000531 LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
Rafael Espindola1266b612012-04-21 23:28:21 +0000532 OnlyTemplate));
533 if (!OnlyTemplate)
John McCall6ce51ee2011-06-27 23:06:04 +0000534 LV.merge(getLVForTemplateParameterList(
535 spec->getTemplate()->getTemplateParameters()));
536 }
John McCall66cbcf32010-11-01 01:29:57 +0000537 }
John McCall1fb0caa2010-10-22 21:05:15 +0000538
John McCall110e8e52010-10-29 22:22:43 +0000539 // Note that in contrast to basically every other situation, we
540 // *do* apply -fvisibility to method declarations.
541
542 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000543 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000544 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000545 if (shouldConsiderTemplateLV(spec)) {
546 // Merge template argument/parameter information for member
547 // class template specializations.
Rafael Espindola860097c2012-02-23 04:17:32 +0000548 LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
Rafael Espindola1266b612012-04-21 23:28:21 +0000549 OnlyTemplate));
550 if (!OnlyTemplate)
John McCall1a0918a2011-03-04 10:39:25 +0000551 LV.merge(getLVForTemplateParameterList(
John McCall6ce51ee2011-06-27 23:06:04 +0000552 spec->getSpecializedTemplate()->getTemplateParameters()));
553 }
John McCall110e8e52010-10-29 22:22:43 +0000554 }
555
John McCall110e8e52010-10-29 22:22:43 +0000556 // Static data members.
557 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000558 // Modify the variable's linkage by its type, but ignore the
559 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000560 LinkageInfo TypeLV = getLVForType(VD->getType());
561 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000562 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindolac7e60602012-04-19 05:50:08 +0000563 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000564 }
565
John McCall1fb0caa2010-10-22 21:05:15 +0000566 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000567}
568
John McCallf76b0922011-02-08 19:01:05 +0000569static void clearLinkageForClass(const CXXRecordDecl *record) {
570 for (CXXRecordDecl::decl_iterator
571 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
572 Decl *child = *i;
573 if (isa<NamedDecl>(child))
574 cast<NamedDecl>(child)->ClearLinkageCache();
575 }
576}
577
David Blaikie99ba9e32011-12-20 02:48:34 +0000578void NamedDecl::anchor() { }
579
John McCallf76b0922011-02-08 19:01:05 +0000580void NamedDecl::ClearLinkageCache() {
581 // Note that we can't skip clearing the linkage of children just
582 // because the parent doesn't have cached linkage: we don't cache
583 // when computing linkage for parent contexts.
584
585 HasCachedLinkage = 0;
586
587 // If we're changing the linkage of a class, we need to reset the
588 // linkage of child declarations, too.
589 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
590 clearLinkageForClass(record);
591
John McCall15e310a2011-02-19 02:53:41 +0000592 if (ClassTemplateDecl *temp =
593 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000594 // Clear linkage for the template pattern.
595 CXXRecordDecl *record = temp->getTemplatedDecl();
596 record->HasCachedLinkage = 0;
597 clearLinkageForClass(record);
598
John McCall15e310a2011-02-19 02:53:41 +0000599 // We need to clear linkage for specializations, too.
600 for (ClassTemplateDecl::spec_iterator
601 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
602 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000603 }
John McCall15e310a2011-02-19 02:53:41 +0000604
605 // Clear cached linkage for function template decls, too.
606 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000607 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
608 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000609 for (FunctionTemplateDecl::spec_iterator
610 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
611 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000612 }
John McCall15e310a2011-02-19 02:53:41 +0000613
John McCallf76b0922011-02-08 19:01:05 +0000614}
615
Douglas Gregor381d34e2010-12-06 18:36:25 +0000616Linkage NamedDecl::getLinkage() const {
617 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000618 assert(Linkage(CachedLinkage) ==
Rafael Espindola1266b612012-04-21 23:28:21 +0000619 getLVForDecl(this, true).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000620 return Linkage(CachedLinkage);
621 }
622
Rafael Espindola1266b612012-04-21 23:28:21 +0000623 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000624 HasCachedLinkage = 1;
625 return Linkage(CachedLinkage);
626}
627
John McCallaf146032010-10-30 11:50:40 +0000628LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola1266b612012-04-21 23:28:21 +0000629 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000630 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000631 HasCachedLinkage = 1;
632 CachedLinkage = LI.linkage();
633 return LI;
John McCall0df95872010-10-29 00:29:13 +0000634}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000635
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000636llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
637 // Use the most recent declaration of a variable.
638 if (const VarDecl *var = dyn_cast<VarDecl>(this))
Douglas Gregoref96ee02012-01-14 16:38:05 +0000639 return getVisibilityOf(var->getMostRecentDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000640
641 // Use the most recent declaration of a function, and also handle
642 // function template specializations.
643 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
644 if (llvm::Optional<Visibility> V
Douglas Gregoref96ee02012-01-14 16:38:05 +0000645 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000646 return V;
647
648 // If the function is a specialization of a template with an
649 // explicit visibility attribute, use that.
650 if (FunctionTemplateSpecializationInfo *templateInfo
651 = fn->getTemplateSpecializationInfo())
652 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
653
Rafael Espindola860097c2012-02-23 04:17:32 +0000654 // If the function is a member of a specialization of a class template
655 // and the corresponding decl has explicit visibility, use that.
656 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
657 if (InstantiatedFrom)
658 return getVisibilityOf(InstantiatedFrom);
659
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000660 return llvm::Optional<Visibility>();
661 }
662
663 // Otherwise, just check the declaration itself first.
664 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
665 return V;
666
667 // If there wasn't explicit visibility there, and this is a
668 // specialization of a class template, check for visibility
669 // on the pattern.
670 if (const ClassTemplateSpecializationDecl *spec
671 = dyn_cast<ClassTemplateSpecializationDecl>(this))
672 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
673
Rafael Espindola860097c2012-02-23 04:17:32 +0000674 // If this is a member class of a specialization of a class template
675 // and the corresponding decl has explicit visibility, use that.
676 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
677 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
678 if (InstantiatedFrom)
679 return getVisibilityOf(InstantiatedFrom);
680 }
681
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000682 return llvm::Optional<Visibility>();
683}
684
Rafael Espindola1266b612012-04-21 23:28:21 +0000685static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000686 // Objective-C: treat all Objective-C declarations as having external
687 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000688 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000689 default:
690 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000691 case Decl::ParmVar:
692 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000693 case Decl::TemplateTemplateParm: // count these as external
694 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000695 case Decl::ObjCAtDefsField:
696 case Decl::ObjCCategory:
697 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000698 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000699 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000700 case Decl::ObjCMethod:
701 case Decl::ObjCProperty:
702 case Decl::ObjCPropertyImpl:
703 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000704 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000705
706 case Decl::CXXRecord: {
707 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
708 if (Record->isLambda()) {
709 if (!Record->getLambdaManglingNumber()) {
710 // This lambda has no mangling number, so it's internal.
711 return LinkageInfo::internal();
712 }
713
714 // This lambda has its linkage/visibility determined by its owner.
715 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
716 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
717 if (isa<ParmVarDecl>(ContextDecl))
718 DC = ContextDecl->getDeclContext()->getRedeclContext();
719 else
Rafael Espindola1266b612012-04-21 23:28:21 +0000720 return getLVForDecl(cast<NamedDecl>(ContextDecl),
721 OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000722 }
723
724 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola1266b612012-04-21 23:28:21 +0000725 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000726
727 return LinkageInfo::external();
728 }
729
730 break;
731 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000732 }
733
Douglas Gregord85b5b92009-11-25 22:24:25 +0000734 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000735 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola1266b612012-04-21 23:28:21 +0000736 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000737
738 // C++ [basic.link]p5:
739 // In addition, a member function, static data member, a named
740 // class or enumeration of class scope, or an unnamed class or
741 // enumeration defined in a class-scope typedef declaration such
742 // that the class or enumeration has the typedef name for linkage
743 // purposes (7.1.3), has external linkage if the name of the class
744 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000745 if (D->getDeclContext()->isRecord())
Rafael Espindola1266b612012-04-21 23:28:21 +0000746 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000747
748 // C++ [basic.link]p6:
749 // The name of a function declared in block scope and the name of
750 // an object declared by a block scope extern declaration have
751 // linkage. If there is a visible declaration of an entity with
752 // linkage having the same name and type, ignoring entities
753 // declared outside the innermost enclosing namespace scope, the
754 // block scope declaration declares that same entity and receives
755 // the linkage of the previous declaration. If there is more than
756 // one such matching entity, the program is ill-formed. Otherwise,
757 // if no matching entity is found, the block scope entity receives
758 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000759 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
760 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000761 if (Function->isInAnonymousNamespace() &&
762 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000763 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000764
John McCallaf146032010-10-30 11:50:40 +0000765 LinkageInfo LV;
Rafael Espindola1266b612012-04-21 23:28:21 +0000766 if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000767 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000768 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000769 }
770
Douglas Gregoref96ee02012-01-14 16:38:05 +0000771 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000772 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000773 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
774 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000775 }
776
777 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000778 }
779
John McCall0df95872010-10-29 00:29:13 +0000780 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000781 if (Var->getStorageClass() == SC_Extern ||
782 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000783 if (Var->isInAnonymousNamespace() &&
784 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000785 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000786
John McCallaf146032010-10-30 11:50:40 +0000787 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000788 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000789 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola1266b612012-04-21 23:28:21 +0000790 else if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000791 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000792 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000793 }
794
Douglas Gregoref96ee02012-01-14 16:38:05 +0000795 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000796 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000797 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
798 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000799 }
800
801 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000802 }
803 }
804
805 // C++ [basic.link]p6:
806 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000807 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000808}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000809
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000810std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000811 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000812}
813
814std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000815 const DeclContext *Ctx = getDeclContext();
816
817 if (Ctx->isFunctionOrMethod())
818 return getNameAsString();
819
Chris Lattner5f9e2722011-07-23 10:55:15 +0000820 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000821 ContextsTy Contexts;
822
823 // Collect contexts.
824 while (Ctx && isa<NamedDecl>(Ctx)) {
825 Contexts.push_back(Ctx);
826 Ctx = Ctx->getParent();
827 };
828
829 std::string QualName;
830 llvm::raw_string_ostream OS(QualName);
831
832 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
833 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000834 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000835 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000836 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
837 std::string TemplateArgsStr
838 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000839 TemplateArgs.data(),
840 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000841 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000842 OS << Spec->getName() << TemplateArgsStr;
843 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000844 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000845 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000846 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000847 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000848 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
849 if (!RD->getIdentifier())
850 OS << "<anonymous " << RD->getKindName() << '>';
851 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000852 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000853 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000854 const FunctionProtoType *FT = 0;
855 if (FD->hasWrittenPrototype())
856 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
857
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000858 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000859 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000860 unsigned NumParams = FD->getNumParams();
861 for (unsigned i = 0; i < NumParams; ++i) {
862 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000863 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000864 std::string Param;
865 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000866 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000867 }
868
869 if (FT->isVariadic()) {
870 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000871 OS << ", ";
872 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000873 }
874 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000875 OS << ')';
876 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000877 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000878 }
879 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000880 }
881
John McCall8472af42010-03-16 21:48:18 +0000882 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000883 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000884 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000885 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000886
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000887 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000888}
889
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000890bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000891 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
892
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000893 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
894 // We want to keep it, unless it nominates same namespace.
895 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000896 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
897 ->getOriginalNamespace() ==
898 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
899 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000902 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
903 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000904 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000905
Douglas Gregore53060f2009-06-25 22:08:12 +0000906 // For function templates, the underlying function declarations are linked.
907 if (const FunctionTemplateDecl *FunctionTemplate
908 = dyn_cast<FunctionTemplateDecl>(this))
909 if (const FunctionTemplateDecl *OldFunctionTemplate
910 = dyn_cast<FunctionTemplateDecl>(OldD))
911 return FunctionTemplate->getTemplatedDecl()
912 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Steve Naroff0de21fd2009-02-22 19:35:57 +0000914 // For method declarations, we keep track of redeclarations.
915 if (isa<ObjCMethodDecl>(this))
916 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000917
John McCallf36e02d2009-10-09 21:13:30 +0000918 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
919 return true;
920
John McCall9488ea12009-11-17 05:59:44 +0000921 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
922 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
923 cast<UsingShadowDecl>(OldD)->getTargetDecl();
924
Douglas Gregordc355712011-02-25 00:36:19 +0000925 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
926 ASTContext &Context = getASTContext();
927 return Context.getCanonicalNestedNameSpecifier(
928 cast<UsingDecl>(this)->getQualifier()) ==
929 Context.getCanonicalNestedNameSpecifier(
930 cast<UsingDecl>(OldD)->getQualifier());
931 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000932
Douglas Gregor7a537402012-01-03 23:26:26 +0000933 // A typedef of an Objective-C class type can replace an Objective-C class
934 // declaration or definition, and vice versa.
935 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
936 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
937 return true;
938
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000939 // For non-function declarations, if the declarations are of the
940 // same kind then this must be a redeclaration, or semantic analysis
941 // would not have given us the new declaration.
942 return this->getKind() == OldD->getKind();
943}
944
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000945bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000946 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000947}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000948
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000949NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000950 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000951 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
952 ND = UD->getTargetDecl();
953
954 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
955 return AD->getClassInterface();
956
957 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +0000958}
959
John McCall161755a2010-04-06 21:38:20 +0000960bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000961 if (!isCXXClassMember())
962 return false;
963
John McCall161755a2010-04-06 21:38:20 +0000964 const NamedDecl *D = this;
965 if (isa<UsingShadowDecl>(D))
966 D = cast<UsingShadowDecl>(D)->getTargetDecl();
967
Francois Pichet87c2e122010-11-21 06:08:52 +0000968 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000969 return true;
970 if (isa<CXXMethodDecl>(D))
971 return cast<CXXMethodDecl>(D)->isInstance();
972 if (isa<FunctionTemplateDecl>(D))
973 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
974 ->getTemplatedDecl())->isInstance();
975 return false;
976}
977
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000978//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000979// DeclaratorDecl Implementation
980//===----------------------------------------------------------------------===//
981
Douglas Gregor1693e152010-07-06 18:42:40 +0000982template <typename DeclT>
983static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
984 if (decl->getNumTemplateParameterLists() > 0)
985 return decl->getTemplateParameterList(0)->getTemplateLoc();
986 else
987 return decl->getInnerLocStart();
988}
989
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000990SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000991 TypeSourceInfo *TSI = getTypeSourceInfo();
992 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000993 return SourceLocation();
994}
995
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000996void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
997 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +0000998 // Make sure the extended decl info is allocated.
999 if (!hasExtInfo()) {
1000 // Save (non-extended) type source info pointer.
1001 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1002 // Allocate external info struct.
1003 DeclInfo = new (getASTContext()) ExtInfo;
1004 // Restore savedTInfo into (extended) decl info.
1005 getExtInfo()->TInfo = savedTInfo;
1006 }
1007 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001008 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001009 } else {
John McCallb6217662010-03-15 10:12:16 +00001010 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001011 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001012 if (getExtInfo()->NumTemplParamLists == 0) {
1013 // Save type source info pointer.
1014 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1015 // Deallocate the extended decl info.
1016 getASTContext().Deallocate(getExtInfo());
1017 // Restore savedTInfo into (non-extended) decl info.
1018 DeclInfo = savedTInfo;
1019 }
1020 else
1021 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001022 }
1023 }
1024}
1025
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001026void
1027DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1028 unsigned NumTPLists,
1029 TemplateParameterList **TPLists) {
1030 assert(NumTPLists > 0);
1031 // Make sure the extended decl info is allocated.
1032 if (!hasExtInfo()) {
1033 // Save (non-extended) type source info pointer.
1034 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1035 // Allocate external info struct.
1036 DeclInfo = new (getASTContext()) ExtInfo;
1037 // Restore savedTInfo into (extended) decl info.
1038 getExtInfo()->TInfo = savedTInfo;
1039 }
1040 // Set the template parameter lists info.
1041 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1042}
1043
Douglas Gregor1693e152010-07-06 18:42:40 +00001044SourceLocation DeclaratorDecl::getOuterLocStart() const {
1045 return getTemplateOrInnerLocStart(this);
1046}
1047
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001048namespace {
1049
1050// Helper function: returns true if QT is or contains a type
1051// having a postfix component.
1052bool typeIsPostfix(clang::QualType QT) {
1053 while (true) {
1054 const Type* T = QT.getTypePtr();
1055 switch (T->getTypeClass()) {
1056 default:
1057 return false;
1058 case Type::Pointer:
1059 QT = cast<PointerType>(T)->getPointeeType();
1060 break;
1061 case Type::BlockPointer:
1062 QT = cast<BlockPointerType>(T)->getPointeeType();
1063 break;
1064 case Type::MemberPointer:
1065 QT = cast<MemberPointerType>(T)->getPointeeType();
1066 break;
1067 case Type::LValueReference:
1068 case Type::RValueReference:
1069 QT = cast<ReferenceType>(T)->getPointeeType();
1070 break;
1071 case Type::PackExpansion:
1072 QT = cast<PackExpansionType>(T)->getPattern();
1073 break;
1074 case Type::Paren:
1075 case Type::ConstantArray:
1076 case Type::DependentSizedArray:
1077 case Type::IncompleteArray:
1078 case Type::VariableArray:
1079 case Type::FunctionProto:
1080 case Type::FunctionNoProto:
1081 return true;
1082 }
1083 }
1084}
1085
1086} // namespace
1087
1088SourceRange DeclaratorDecl::getSourceRange() const {
1089 SourceLocation RangeEnd = getLocation();
1090 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1091 if (typeIsPostfix(TInfo->getType()))
1092 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1093 }
1094 return SourceRange(getOuterLocStart(), RangeEnd);
1095}
1096
Abramo Bagnara9b934882010-06-12 08:15:14 +00001097void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001098QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1099 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001100 TemplateParameterList **TPLists) {
1101 assert((NumTPLists == 0 || TPLists != 0) &&
1102 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001103
1104 // Free previous template parameters (if any).
1105 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001106 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001107 TemplParamLists = 0;
1108 NumTemplParamLists = 0;
1109 }
1110 // Set info on matched template parameter lists (if any).
1111 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001112 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001113 NumTemplParamLists = NumTPLists;
1114 for (unsigned i = NumTPLists; i-- > 0; )
1115 TemplParamLists[i] = TPLists[i];
1116 }
1117}
1118
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001119//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001120// VarDecl Implementation
1121//===----------------------------------------------------------------------===//
1122
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001123const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1124 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001125 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001126 case SC_Auto: return "auto";
1127 case SC_Extern: return "extern";
1128 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1129 case SC_PrivateExtern: return "__private_extern__";
1130 case SC_Register: return "register";
1131 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001132 }
1133
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001134 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001135}
1136
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001137VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1138 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001139 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001140 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001141 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001142}
1143
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001144VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1145 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1146 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1147 QualType(), 0, SC_None, SC_None);
1148}
1149
Douglas Gregor381d34e2010-12-06 18:36:25 +00001150void VarDecl::setStorageClass(StorageClass SC) {
1151 assert(isLegalForVariable(SC));
1152 if (getStorageClass() != SC)
1153 ClearLinkageCache();
1154
John McCallf1e4fbf2011-05-01 02:13:58 +00001155 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001156}
1157
Douglas Gregor1693e152010-07-06 18:42:40 +00001158SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001159 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001160 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001161 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001162}
1163
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001164bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001165 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001166 return false;
1167
Eli Friedman750dc2b2012-01-15 01:23:58 +00001168 const DeclContext *DC = getDeclContext();
1169 if (DC->isRecord())
1170 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001171
Eli Friedman750dc2b2012-01-15 01:23:58 +00001172 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001173 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001174 return true;
1175 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001176}
1177
1178VarDecl *VarDecl::getCanonicalDecl() {
1179 return getFirstDeclaration();
1180}
1181
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001182VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1183 ASTContext &C) const
1184{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001185 // C++ [basic.def]p2:
1186 // A declaration is a definition unless [...] it contains the 'extern'
1187 // specifier or a linkage-specification and neither an initializer [...],
1188 // it declares a static data member in a class declaration [...].
1189 // C++ [temp.expl.spec]p15:
1190 // An explicit specialization of a static data member of a template is a
1191 // definition if the declaration includes an initializer; otherwise, it is
1192 // a declaration.
1193 if (isStaticDataMember()) {
1194 if (isOutOfLine() && (hasInit() ||
1195 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1196 return Definition;
1197 else
1198 return DeclarationOnly;
1199 }
1200 // C99 6.7p5:
1201 // A definition of an identifier is a declaration for that identifier that
1202 // [...] causes storage to be reserved for that object.
1203 // Note: that applies for all non-file-scope objects.
1204 // C99 6.9.2p1:
1205 // If the declaration of an identifier for an object has file scope and an
1206 // initializer, the declaration is an external definition for the identifier
1207 if (hasInit())
1208 return Definition;
1209 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1210 if (hasExternalStorage())
1211 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001212
John McCalld931b082010-08-26 03:08:43 +00001213 if (getStorageClassAsWritten() == SC_Extern ||
1214 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001215 for (const VarDecl *PrevVar = getPreviousDecl();
1216 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001217 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1218 return DeclarationOnly;
1219 }
1220 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001221 // C99 6.9.2p2:
1222 // A declaration of an object that has file scope without an initializer,
1223 // and without a storage class specifier or the scs 'static', constitutes
1224 // a tentative definition.
1225 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001226 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001227 return TentativeDefinition;
1228
1229 // What's left is (in C, block-scope) declarations without initializers or
1230 // external storage. These are definitions.
1231 return Definition;
1232}
1233
Sebastian Redle9d12b62010-01-31 22:27:38 +00001234VarDecl *VarDecl::getActingDefinition() {
1235 DefinitionKind Kind = isThisDeclarationADefinition();
1236 if (Kind != TentativeDefinition)
1237 return 0;
1238
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001239 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001240 VarDecl *First = getFirstDeclaration();
1241 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1242 I != E; ++I) {
1243 Kind = (*I)->isThisDeclarationADefinition();
1244 if (Kind == Definition)
1245 return 0;
1246 else if (Kind == TentativeDefinition)
1247 LastTentative = *I;
1248 }
1249 return LastTentative;
1250}
1251
1252bool VarDecl::isTentativeDefinitionNow() const {
1253 DefinitionKind Kind = isThisDeclarationADefinition();
1254 if (Kind != TentativeDefinition)
1255 return false;
1256
1257 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1258 if ((*I)->isThisDeclarationADefinition() == Definition)
1259 return false;
1260 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001261 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001262}
1263
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001264VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001265 VarDecl *First = getFirstDeclaration();
1266 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1267 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001268 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001269 return *I;
1270 }
1271 return 0;
1272}
1273
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001274VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001275 DefinitionKind Kind = DeclarationOnly;
1276
1277 const VarDecl *First = getFirstDeclaration();
1278 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001279 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001280 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001281 if (Kind == Definition)
1282 break;
1283 }
John McCall110e8e52010-10-29 22:22:43 +00001284
1285 return Kind;
1286}
1287
Sebastian Redl31310a22010-02-01 20:16:42 +00001288const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001289 redecl_iterator I = redecls_begin(), E = redecls_end();
1290 while (I != E && !I->getInit())
1291 ++I;
1292
1293 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001294 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001295 return I->getInit();
1296 }
1297 return 0;
1298}
1299
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001300bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001301 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001302 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001303
1304 if (!isStaticDataMember())
1305 return false;
1306
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001307 // If this static data member was instantiated from a static data member of
1308 // a class template, check whether that static data member was defined
1309 // out-of-line.
1310 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1311 return VD->isOutOfLine();
1312
1313 return false;
1314}
1315
Douglas Gregor0d035142009-10-27 18:42:08 +00001316VarDecl *VarDecl::getOutOfLineDefinition() {
1317 if (!isStaticDataMember())
1318 return 0;
1319
1320 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1321 RD != RDEnd; ++RD) {
1322 if (RD->getLexicalDeclContext()->isFileContext())
1323 return *RD;
1324 }
1325
1326 return 0;
1327}
1328
Douglas Gregor838db382010-02-11 01:19:42 +00001329void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001330 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1331 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001332 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001333 }
1334
1335 Init = I;
1336}
1337
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001338bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001339 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001340
Richard Smith16581332012-03-02 04:14:40 +00001341 if (!Lang.CPlusPlus)
1342 return false;
1343
1344 // In C++11, any variable of reference type can be used in a constant
1345 // expression if it is initialized by a constant expression.
1346 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1347 return true;
1348
1349 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001350 // not require the variable to be non-volatile, but we consider this to be a
1351 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001352 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001353 return false;
1354
1355 // In C++, const, non-volatile variables of integral or enumeration types
1356 // can be used in constant expressions.
1357 if (getType()->isIntegralOrEnumerationType())
1358 return true;
1359
Richard Smith16581332012-03-02 04:14:40 +00001360 // Additionally, in C++11, non-volatile constexpr variables can be used in
1361 // constant expressions.
1362 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001363}
1364
Richard Smith099e7f62011-12-19 06:19:21 +00001365/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1366/// form, which contains extra information on the evaluated value of the
1367/// initializer.
1368EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1369 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1370 if (!Eval) {
1371 Stmt *S = Init.get<Stmt *>();
1372 Eval = new (getASTContext()) EvaluatedStmt;
1373 Eval->Value = S;
1374 Init = Eval;
1375 }
1376 return Eval;
1377}
1378
Richard Smith2d6a5672012-01-14 04:30:29 +00001379APValue *VarDecl::evaluateValue() const {
1380 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1381 return evaluateValue(Notes);
1382}
1383
1384APValue *VarDecl::evaluateValue(
1385 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001386 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1387
1388 // We only produce notes indicating why an initializer is non-constant the
1389 // first time it is evaluated. FIXME: The notes won't always be emitted the
1390 // first time we try evaluation, so might not be produced at all.
1391 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001392 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001393
1394 const Expr *Init = cast<Expr>(Eval->Value);
1395 assert(!Init->isValueDependent());
1396
1397 if (Eval->IsEvaluating) {
1398 // FIXME: Produce a diagnostic for self-initialization.
1399 Eval->CheckedICE = true;
1400 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001401 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001402 }
1403
1404 Eval->IsEvaluating = true;
1405
1406 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1407 this, Notes);
1408
1409 // Ensure the result is an uninitialized APValue if evaluation fails.
1410 if (!Result)
1411 Eval->Evaluated = APValue();
1412
1413 Eval->IsEvaluating = false;
1414 Eval->WasEvaluated = true;
1415
1416 // In C++11, we have determined whether the initializer was a constant
1417 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001418 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001419 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001420 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001421 }
1422
Richard Smith2d6a5672012-01-14 04:30:29 +00001423 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001424}
1425
1426bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001427 // Initializers of weak variables are never ICEs.
1428 if (isWeak())
1429 return false;
1430
Richard Smith099e7f62011-12-19 06:19:21 +00001431 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1432 if (Eval->CheckedICE)
1433 // We have already checked whether this subexpression is an
1434 // integral constant expression.
1435 return Eval->IsICE;
1436
1437 const Expr *Init = cast<Expr>(Eval->Value);
1438 assert(!Init->isValueDependent());
1439
1440 // In C++11, evaluate the initializer to check whether it's a constant
1441 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001442 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001443 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1444 evaluateValue(Notes);
1445 return Eval->IsICE;
1446 }
1447
1448 // It's an ICE whether or not the definition we found is
1449 // out-of-line. See DR 721 and the discussion in Clang PR
1450 // 6206 for details.
1451
1452 if (Eval->CheckingICE)
1453 return false;
1454 Eval->CheckingICE = true;
1455
1456 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1457 Eval->CheckingICE = false;
1458 Eval->CheckedICE = true;
1459 return Eval->IsICE;
1460}
1461
Douglas Gregor03e80032011-06-21 17:03:29 +00001462bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001463 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001464
1465 const Expr *E = getInit();
1466 if (!E)
1467 return false;
1468
1469 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1470 E = Cleanups->getSubExpr();
1471
1472 return isa<MaterializeTemporaryExpr>(E);
1473}
1474
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001475VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001476 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001477 return cast<VarDecl>(MSI->getInstantiatedFrom());
1478
1479 return 0;
1480}
1481
Douglas Gregor663b5a02009-10-14 20:14:33 +00001482TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001483 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001484 return MSI->getTemplateSpecializationKind();
1485
1486 return TSK_Undeclared;
1487}
1488
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001489MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001490 return getASTContext().getInstantiatedFromStaticDataMember(this);
1491}
1492
Douglas Gregor0a897e32009-10-15 17:21:20 +00001493void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1494 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001495 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001496 assert(MSI && "Not an instantiated static data member?");
1497 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001498 if (TSK != TSK_ExplicitSpecialization &&
1499 PointOfInstantiation.isValid() &&
1500 MSI->getPointOfInstantiation().isInvalid())
1501 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001502}
1503
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001504//===----------------------------------------------------------------------===//
1505// ParmVarDecl Implementation
1506//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001507
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001508ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001509 SourceLocation StartLoc,
1510 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001511 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001512 StorageClass S, StorageClass SCAsWritten,
1513 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001514 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001515 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001516}
1517
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001518ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1519 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1520 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1521 0, QualType(), 0, SC_None, SC_None, 0);
1522}
1523
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001524SourceRange ParmVarDecl::getSourceRange() const {
1525 if (!hasInheritedDefaultArg()) {
1526 SourceRange ArgRange = getDefaultArgRange();
1527 if (ArgRange.isValid())
1528 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1529 }
1530
1531 return DeclaratorDecl::getSourceRange();
1532}
1533
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001534Expr *ParmVarDecl::getDefaultArg() {
1535 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1536 assert(!hasUninstantiatedDefaultArg() &&
1537 "Default argument is not yet instantiated!");
1538
1539 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001540 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001541 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001542
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001543 return Arg;
1544}
1545
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001546SourceRange ParmVarDecl::getDefaultArgRange() const {
1547 if (const Expr *E = getInit())
1548 return E->getSourceRange();
1549
1550 if (hasUninstantiatedDefaultArg())
1551 return getUninstantiatedDefaultArg()->getSourceRange();
1552
1553 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001554}
1555
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001556bool ParmVarDecl::isParameterPack() const {
1557 return isa<PackExpansionType>(getType());
1558}
1559
Ted Kremenekd211cb72011-10-06 05:00:56 +00001560void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1561 getASTContext().setParameterIndex(this, parameterIndex);
1562 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1563}
1564
1565unsigned ParmVarDecl::getParameterIndexLarge() const {
1566 return getASTContext().getParameterIndex(this);
1567}
1568
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001569//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001570// FunctionDecl Implementation
1571//===----------------------------------------------------------------------===//
1572
Douglas Gregorda2142f2011-02-19 18:51:44 +00001573void FunctionDecl::getNameForDiagnostic(std::string &S,
1574 const PrintingPolicy &Policy,
1575 bool Qualified) const {
1576 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1577 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1578 if (TemplateArgs)
1579 S += TemplateSpecializationType::PrintTemplateArgumentList(
1580 TemplateArgs->data(),
1581 TemplateArgs->size(),
1582 Policy);
1583
1584}
1585
Ted Kremenek9498d382010-04-29 16:49:01 +00001586bool FunctionDecl::isVariadic() const {
1587 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1588 return FT->isVariadic();
1589 return false;
1590}
1591
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001592bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1593 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001594 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001595 Definition = *I;
1596 return true;
1597 }
1598 }
1599
1600 return false;
1601}
1602
Anders Carlssonffb945f2011-05-14 23:26:09 +00001603bool FunctionDecl::hasTrivialBody() const
1604{
1605 Stmt *S = getBody();
1606 if (!S) {
1607 // Since we don't have a body for this function, we don't know if it's
1608 // trivial or not.
1609 return false;
1610 }
1611
1612 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1613 return true;
1614 return false;
1615}
1616
Sean Hunt10620eb2011-05-06 20:44:56 +00001617bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1618 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001619 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001620 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1621 return true;
1622 }
1623 }
1624
1625 return false;
1626}
1627
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001628Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001629 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1630 if (I->Body) {
1631 Definition = *I;
1632 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001633 } else if (I->IsLateTemplateParsed) {
1634 Definition = *I;
1635 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001636 }
1637 }
1638
1639 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001640}
1641
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001642void FunctionDecl::setBody(Stmt *B) {
1643 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001644 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001645 EndRangeLoc = B->getLocEnd();
1646}
1647
Douglas Gregor21386642010-09-28 21:55:22 +00001648void FunctionDecl::setPure(bool P) {
1649 IsPure = P;
1650 if (P)
1651 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1652 Parent->markedVirtualFunctionPure();
1653}
1654
Douglas Gregor48a83b52009-09-12 00:17:51 +00001655bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001656 const TranslationUnitDecl *tunit =
1657 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1658 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001659 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001660 getIdentifier() &&
1661 getIdentifier()->isStr("main");
1662}
1663
1664bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1665 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1666 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1667 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1668 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1669 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1670
1671 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1672 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1673
1674 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1675 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1676
1677 ASTContext &Context =
1678 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1679 ->getASTContext();
1680
1681 // The result type and first argument type are constant across all
1682 // these operators. The second argument must be exactly void*.
1683 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001684}
1685
Douglas Gregor48a83b52009-09-12 00:17:51 +00001686bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001687 if (getLinkage() != ExternalLinkage)
1688 return false;
1689
1690 if (getAttr<OverloadableAttr>())
1691 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001692
Chandler Carruth10aad442011-02-25 00:05:02 +00001693 const DeclContext *DC = getDeclContext();
1694 if (DC->isRecord())
1695 return false;
1696
Eli Friedman750dc2b2012-01-15 01:23:58 +00001697 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001698 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001699 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001700
Eli Friedman750dc2b2012-01-15 01:23:58 +00001701 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001702}
1703
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001704bool FunctionDecl::isGlobal() const {
1705 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1706 return Method->isStatic();
1707
John McCalld931b082010-08-26 03:08:43 +00001708 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001709 return false;
1710
Mike Stump1eb44332009-09-09 15:08:12 +00001711 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001712 DC->isNamespace();
1713 DC = DC->getParent()) {
1714 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1715 if (!Namespace->getDeclName())
1716 return false;
1717 break;
1718 }
1719 }
1720
1721 return true;
1722}
1723
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001724void
1725FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1726 redeclarable_base::setPreviousDeclaration(PrevDecl);
1727
1728 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1729 FunctionTemplateDecl *PrevFunTmpl
1730 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1731 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1732 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1733 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001734
Axel Naumannd9d137e2011-11-08 18:21:06 +00001735 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001736 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001737}
1738
1739const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1740 return getFirstDeclaration();
1741}
1742
1743FunctionDecl *FunctionDecl::getCanonicalDecl() {
1744 return getFirstDeclaration();
1745}
1746
Douglas Gregor381d34e2010-12-06 18:36:25 +00001747void FunctionDecl::setStorageClass(StorageClass SC) {
1748 assert(isLegalForFunction(SC));
1749 if (getStorageClass() != SC)
1750 ClearLinkageCache();
1751
1752 SClass = SC;
1753}
1754
Douglas Gregor3e41d602009-02-13 23:20:09 +00001755/// \brief Returns a value indicating whether this function
1756/// corresponds to a builtin function.
1757///
1758/// The function corresponds to a built-in function if it is
1759/// declared at translation scope or within an extern "C" block and
1760/// its name matches with the name of a builtin. The returned value
1761/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001762/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001763/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001764unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001765 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001766 return 0;
1767
1768 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001769 if (!BuiltinID)
1770 return 0;
1771
1772 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001773 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1774 return BuiltinID;
1775
1776 // This function has the name of a known C library
1777 // function. Determine whether it actually refers to the C library
1778 // function or whether it just has the same name.
1779
Douglas Gregor9add3172009-02-17 03:23:10 +00001780 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001781 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001782 return 0;
1783
Douglas Gregor3c385e52009-02-14 18:57:46 +00001784 // If this function is at translation-unit scope and we're not in
1785 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001786 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001787 getDeclContext()->isTranslationUnit())
1788 return BuiltinID;
1789
1790 // If the function is in an extern "C" linkage specification and is
1791 // not marked "overloadable", it's the real function.
1792 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001793 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001794 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001795 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001796 return BuiltinID;
1797
1798 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001799 return 0;
1800}
1801
1802
Chris Lattner1ad9b282009-04-25 06:03:53 +00001803/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001804/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001805/// after it has been created.
1806unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001807 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001808 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001809 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001810 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Reid Spencer5f016e22007-07-11 17:01:13 +00001812}
1813
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001814void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001815 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001817 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Reid Spencer5f016e22007-07-11 17:01:13 +00001819 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001820 if (!NewParamInfo.empty()) {
1821 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1822 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001823 }
1824}
1825
James Molloy16f1f712012-02-29 10:24:19 +00001826void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1827 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1828
1829 if (!NewDecls.empty()) {
1830 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1831 std::copy(NewDecls.begin(), NewDecls.end(), A);
1832 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1833 }
1834}
1835
Chris Lattner8123a952008-04-10 02:22:51 +00001836/// getMinRequiredArguments - Returns the minimum number of arguments
1837/// needed to call this function. This may be fewer than the number of
1838/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001839/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001840unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001841 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001842 return getNumParams();
1843
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001844 unsigned NumRequiredArgs = getNumParams();
1845
1846 // If the last parameter is a parameter pack, we don't need an argument for
1847 // it.
1848 if (NumRequiredArgs > 0 &&
1849 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1850 --NumRequiredArgs;
1851
1852 // If this parameter has a default argument, we don't need an argument for
1853 // it.
1854 while (NumRequiredArgs > 0 &&
1855 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001856 --NumRequiredArgs;
1857
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001858 // We might have parameter packs before the end. These can't be deduced,
1859 // but they can still handle multiple arguments.
1860 unsigned ArgIdx = NumRequiredArgs;
1861 while (ArgIdx > 0) {
1862 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1863 NumRequiredArgs = ArgIdx;
1864
1865 --ArgIdx;
1866 }
1867
Chris Lattner8123a952008-04-10 02:22:51 +00001868 return NumRequiredArgs;
1869}
1870
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001871bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001872 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001873 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001874
1875 if (isa<CXXMethodDecl>(this)) {
1876 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1877 return true;
1878 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001879
1880 switch (getTemplateSpecializationKind()) {
1881 case TSK_Undeclared:
1882 case TSK_ExplicitSpecialization:
1883 return false;
1884
1885 case TSK_ImplicitInstantiation:
1886 case TSK_ExplicitInstantiationDeclaration:
1887 case TSK_ExplicitInstantiationDefinition:
1888 // Handle below.
1889 break;
1890 }
1891
1892 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001893 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001894 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001895 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001896
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001897 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001898 return PatternDecl->isInlined();
1899
1900 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001901}
1902
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001903static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1904 // Only consider file-scope declarations in this test.
1905 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1906 return false;
1907
1908 // Only consider explicit declarations; the presence of a builtin for a
1909 // libcall shouldn't affect whether a definition is externally visible.
1910 if (Redecl->isImplicit())
1911 return false;
1912
1913 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1914 return true; // Not an inline definition
1915
1916 return false;
1917}
1918
Nick Lewyckydce67a72011-07-18 05:26:13 +00001919/// \brief For a function declaration in C or C++, determine whether this
1920/// declaration causes the definition to be externally visible.
1921///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001922/// Specifically, this determines if adding the current declaration to the set
1923/// of redeclarations of the given functions causes
1924/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001925bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1926 assert(!doesThisDeclarationHaveABody() &&
1927 "Must have a declaration without a body.");
1928
1929 ASTContext &Context = getASTContext();
1930
David Blaikie4e4d0842012-03-11 07:00:24 +00001931 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001932 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1933 // an externally visible definition.
1934 //
1935 // FIXME: What happens if gnu_inline gets added on after the first
1936 // declaration?
1937 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1938 return false;
1939
1940 const FunctionDecl *Prev = this;
1941 bool FoundBody = false;
1942 while ((Prev = Prev->getPreviousDecl())) {
1943 FoundBody |= Prev->Body;
1944
1945 if (Prev->Body) {
1946 // If it's not the case that both 'inline' and 'extern' are
1947 // specified on the definition, then it is always externally visible.
1948 if (!Prev->isInlineSpecified() ||
1949 Prev->getStorageClassAsWritten() != SC_Extern)
1950 return false;
1951 } else if (Prev->isInlineSpecified() &&
1952 Prev->getStorageClassAsWritten() != SC_Extern) {
1953 return false;
1954 }
1955 }
1956 return FoundBody;
1957 }
1958
David Blaikie4e4d0842012-03-11 07:00:24 +00001959 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001960 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001961
1962 // C99 6.7.4p6:
1963 // [...] If all of the file scope declarations for a function in a
1964 // translation unit include the inline function specifier without extern,
1965 // then the definition in that translation unit is an inline definition.
1966 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001967 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001968 const FunctionDecl *Prev = this;
1969 bool FoundBody = false;
1970 while ((Prev = Prev->getPreviousDecl())) {
1971 FoundBody |= Prev->Body;
1972 if (RedeclForcesDefC99(Prev))
1973 return false;
1974 }
1975 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00001976}
1977
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001978/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001979/// definition will be externally visible.
1980///
1981/// Inline function definitions are always available for inlining optimizations.
1982/// However, depending on the language dialect, declaration specifiers, and
1983/// attributes, the definition of an inline function may or may not be
1984/// "externally" visible to other translation units in the program.
1985///
1986/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001987/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001988/// inline definition becomes externally visible (C99 6.7.4p6).
1989///
1990/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1991/// definition, we use the GNU semantics for inline, which are nearly the
1992/// opposite of C99 semantics. In particular, "inline" by itself will create
1993/// an externally visible symbol, but "extern inline" will not create an
1994/// externally visible symbol.
1995bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00001996 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001997 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001998 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001999
David Blaikie4e4d0842012-03-11 07:00:24 +00002000 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002001 // Note: If you change the logic here, please change
2002 // doesDeclarationForceExternallyVisibleDefinition as well.
2003 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002004 // If it's not the case that both 'inline' and 'extern' are
2005 // specified on the definition, then this inline definition is
2006 // externally visible.
2007 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2008 return true;
2009
2010 // If any declaration is 'inline' but not 'extern', then this definition
2011 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002012 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2013 Redecl != RedeclEnd;
2014 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002015 if (Redecl->isInlineSpecified() &&
2016 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002017 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002018 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002019
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002020 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002021 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002022
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002023 // C99 6.7.4p6:
2024 // [...] If all of the file scope declarations for a function in a
2025 // translation unit include the inline function specifier without extern,
2026 // then the definition in that translation unit is an inline definition.
2027 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2028 Redecl != RedeclEnd;
2029 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002030 if (RedeclForcesDefC99(*Redecl))
2031 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002032 }
2033
2034 // C99 6.7.4p6:
2035 // An inline definition does not provide an external definition for the
2036 // function, and does not forbid an external definition in another
2037 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002038 return false;
2039}
2040
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002041/// getOverloadedOperator - Which C++ overloaded operator this
2042/// function represents, if any.
2043OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002044 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2045 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002046 else
2047 return OO_None;
2048}
2049
Sean Hunta6c058d2010-01-13 09:01:02 +00002050/// getLiteralIdentifier - The literal suffix identifier this function
2051/// represents, if any.
2052const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2053 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2054 return getDeclName().getCXXLiteralIdentifier();
2055 else
2056 return 0;
2057}
2058
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002059FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2060 if (TemplateOrSpecialization.isNull())
2061 return TK_NonTemplate;
2062 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2063 return TK_FunctionTemplate;
2064 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2065 return TK_MemberSpecialization;
2066 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2067 return TK_FunctionTemplateSpecialization;
2068 if (TemplateOrSpecialization.is
2069 <DependentFunctionTemplateSpecializationInfo*>())
2070 return TK_DependentFunctionTemplateSpecialization;
2071
David Blaikieb219cfc2011-09-23 05:06:16 +00002072 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002073}
2074
Douglas Gregor2db32322009-10-07 23:56:10 +00002075FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002076 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002077 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2078
2079 return 0;
2080}
2081
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002082MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2083 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2084}
2085
Douglas Gregor2db32322009-10-07 23:56:10 +00002086void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002087FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2088 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002089 TemplateSpecializationKind TSK) {
2090 assert(TemplateOrSpecialization.isNull() &&
2091 "Member function is already a specialization");
2092 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002093 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002094 TemplateOrSpecialization = Info;
2095}
2096
Douglas Gregor3b846b62009-10-27 20:53:28 +00002097bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002098 // If the function is invalid, it can't be implicitly instantiated.
2099 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002100 return false;
2101
2102 switch (getTemplateSpecializationKind()) {
2103 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002104 case TSK_ExplicitInstantiationDefinition:
2105 return false;
2106
2107 case TSK_ImplicitInstantiation:
2108 return true;
2109
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002110 // It is possible to instantiate TSK_ExplicitSpecialization kind
2111 // if the FunctionDecl has a class scope specialization pattern.
2112 case TSK_ExplicitSpecialization:
2113 return getClassScopeSpecializationPattern() != 0;
2114
Douglas Gregor3b846b62009-10-27 20:53:28 +00002115 case TSK_ExplicitInstantiationDeclaration:
2116 // Handled below.
2117 break;
2118 }
2119
2120 // Find the actual template from which we will instantiate.
2121 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002122 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002123 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002124 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002125
2126 // C++0x [temp.explicit]p9:
2127 // Except for inline functions, other explicit instantiation declarations
2128 // have the effect of suppressing the implicit instantiation of the entity
2129 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002130 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002131 return true;
2132
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002133 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002134}
2135
2136bool FunctionDecl::isTemplateInstantiation() const {
2137 switch (getTemplateSpecializationKind()) {
2138 case TSK_Undeclared:
2139 case TSK_ExplicitSpecialization:
2140 return false;
2141 case TSK_ImplicitInstantiation:
2142 case TSK_ExplicitInstantiationDeclaration:
2143 case TSK_ExplicitInstantiationDefinition:
2144 return true;
2145 }
2146 llvm_unreachable("All TSK values handled.");
2147}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002148
2149FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002150 // Handle class scope explicit specialization special case.
2151 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2152 return getClassScopeSpecializationPattern();
2153
Douglas Gregor3b846b62009-10-27 20:53:28 +00002154 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2155 while (Primary->getInstantiatedFromMemberTemplate()) {
2156 // If we have hit a point where the user provided a specialization of
2157 // this template, we're done looking.
2158 if (Primary->isMemberSpecialization())
2159 break;
2160
2161 Primary = Primary->getInstantiatedFromMemberTemplate();
2162 }
2163
2164 return Primary->getTemplatedDecl();
2165 }
2166
2167 return getInstantiatedFromMemberFunction();
2168}
2169
Douglas Gregor16e8be22009-06-29 17:30:29 +00002170FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002171 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002172 = TemplateOrSpecialization
2173 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002174 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002175 }
2176 return 0;
2177}
2178
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002179FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2180 return getASTContext().getClassScopeSpecializationPattern(this);
2181}
2182
Douglas Gregor16e8be22009-06-29 17:30:29 +00002183const TemplateArgumentList *
2184FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002185 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002186 = TemplateOrSpecialization
2187 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002188 return Info->TemplateArguments;
2189 }
2190 return 0;
2191}
2192
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002193const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002194FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2195 if (FunctionTemplateSpecializationInfo *Info
2196 = TemplateOrSpecialization
2197 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2198 return Info->TemplateArgumentsAsWritten;
2199 }
2200 return 0;
2201}
2202
Mike Stump1eb44332009-09-09 15:08:12 +00002203void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002204FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2205 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002206 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002207 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002208 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002209 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2210 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002211 assert(TSK != TSK_Undeclared &&
2212 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002213 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002214 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002215 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002216 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2217 TemplateArgs,
2218 TemplateArgsAsWritten,
2219 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002220 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002221 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002222}
2223
John McCallaf2094e2010-04-08 09:05:18 +00002224void
2225FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2226 const UnresolvedSetImpl &Templates,
2227 const TemplateArgumentListInfo &TemplateArgs) {
2228 assert(TemplateOrSpecialization.isNull());
2229 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2230 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002231 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002232 void *Buffer = Context.Allocate(Size);
2233 DependentFunctionTemplateSpecializationInfo *Info =
2234 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2235 TemplateArgs);
2236 TemplateOrSpecialization = Info;
2237}
2238
2239DependentFunctionTemplateSpecializationInfo::
2240DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2241 const TemplateArgumentListInfo &TArgs)
2242 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2243
2244 d.NumTemplates = Ts.size();
2245 d.NumArgs = TArgs.size();
2246
2247 FunctionTemplateDecl **TsArray =
2248 const_cast<FunctionTemplateDecl**>(getTemplates());
2249 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2250 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2251
2252 TemplateArgumentLoc *ArgsArray =
2253 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2254 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2255 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2256}
2257
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002258TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002259 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002260 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002261 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002262 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002263 if (FTSInfo)
2264 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Douglas Gregor2db32322009-10-07 23:56:10 +00002266 MemberSpecializationInfo *MSInfo
2267 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2268 if (MSInfo)
2269 return MSInfo->getTemplateSpecializationKind();
2270
2271 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002272}
2273
Mike Stump1eb44332009-09-09 15:08:12 +00002274void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002275FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2276 SourceLocation PointOfInstantiation) {
2277 if (FunctionTemplateSpecializationInfo *FTSInfo
2278 = TemplateOrSpecialization.dyn_cast<
2279 FunctionTemplateSpecializationInfo*>()) {
2280 FTSInfo->setTemplateSpecializationKind(TSK);
2281 if (TSK != TSK_ExplicitSpecialization &&
2282 PointOfInstantiation.isValid() &&
2283 FTSInfo->getPointOfInstantiation().isInvalid())
2284 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2285 } else if (MemberSpecializationInfo *MSInfo
2286 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2287 MSInfo->setTemplateSpecializationKind(TSK);
2288 if (TSK != TSK_ExplicitSpecialization &&
2289 PointOfInstantiation.isValid() &&
2290 MSInfo->getPointOfInstantiation().isInvalid())
2291 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2292 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002293 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002294}
2295
2296SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002297 if (FunctionTemplateSpecializationInfo *FTSInfo
2298 = TemplateOrSpecialization.dyn_cast<
2299 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002300 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002301 else if (MemberSpecializationInfo *MSInfo
2302 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002303 return MSInfo->getPointOfInstantiation();
2304
2305 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002306}
2307
Douglas Gregor9f185072009-09-11 20:15:17 +00002308bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002309 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002310 return true;
2311
2312 // If this function was instantiated from a member function of a
2313 // class template, check whether that member function was defined out-of-line.
2314 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2315 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002316 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002317 return Definition->isOutOfLine();
2318 }
2319
2320 // If this function was instantiated from a function template,
2321 // check whether that function template was defined out-of-line.
2322 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2323 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002324 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002325 return Definition->isOutOfLine();
2326 }
2327
2328 return false;
2329}
2330
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002331SourceRange FunctionDecl::getSourceRange() const {
2332 return SourceRange(getOuterLocStart(), EndRangeLoc);
2333}
2334
Anna Zaks9392d4e2012-01-18 02:45:01 +00002335unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002336 IdentifierInfo *FnInfo = getIdentifier();
2337
2338 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002339 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002340
2341 // Builtin handling.
2342 switch (getBuiltinID()) {
2343 case Builtin::BI__builtin_memset:
2344 case Builtin::BI__builtin___memset_chk:
2345 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002346 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002347
2348 case Builtin::BI__builtin_memcpy:
2349 case Builtin::BI__builtin___memcpy_chk:
2350 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002351 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002352
2353 case Builtin::BI__builtin_memmove:
2354 case Builtin::BI__builtin___memmove_chk:
2355 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002356 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002357
2358 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002359 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002360 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002361 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002362
2363 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002364 case Builtin::BImemcmp:
2365 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002366
2367 case Builtin::BI__builtin_strncpy:
2368 case Builtin::BI__builtin___strncpy_chk:
2369 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002370 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002371
2372 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002373 case Builtin::BIstrncmp:
2374 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002375
2376 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002377 case Builtin::BIstrncasecmp:
2378 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002379
2380 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002381 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002382 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002383 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002384
2385 case Builtin::BI__builtin_strndup:
2386 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002387 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002388
Anna Zaksc36bedc2012-02-01 19:08:57 +00002389 case Builtin::BI__builtin_strlen:
2390 case Builtin::BIstrlen:
2391 return Builtin::BIstrlen;
2392
Anna Zaksd9b859a2012-01-13 21:52:01 +00002393 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002394 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002395 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002396 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002397 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002398 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002399 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002400 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002401 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002402 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002403 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002404 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002405 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002406 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002407 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002408 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002409 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002410 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002411 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002412 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002413 else if (FnInfo->isStr("strlen"))
2414 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002415 }
2416 break;
2417 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002418 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002419}
2420
Chris Lattner8a934232008-03-31 00:36:02 +00002421//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002422// FieldDecl Implementation
2423//===----------------------------------------------------------------------===//
2424
Jay Foad4ba2a172011-01-12 09:06:06 +00002425FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002426 SourceLocation StartLoc, SourceLocation IdLoc,
2427 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002428 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2429 bool HasInit) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002430 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith7a614d82011-06-11 17:19:42 +00002431 BW, Mutable, HasInit);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002432}
2433
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002434FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2435 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2436 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
2437 0, QualType(), 0, 0, false, false);
2438}
2439
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002440bool FieldDecl::isAnonymousStructOrUnion() const {
2441 if (!isImplicit() || getDeclName())
2442 return false;
2443
2444 if (const RecordType *Record = getType()->getAs<RecordType>())
2445 return Record->getDecl()->isAnonymousStructOrUnion();
2446
2447 return false;
2448}
2449
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002450unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2451 assert(isBitField() && "not a bitfield");
2452 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2453 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2454}
2455
John McCallba4f5d52011-01-20 07:57:12 +00002456unsigned FieldDecl::getFieldIndex() const {
2457 if (CachedFieldIndex) return CachedFieldIndex - 1;
2458
Richard Smith180f4792011-11-10 06:34:14 +00002459 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002460 const RecordDecl *RD = getParent();
2461 const FieldDecl *LastFD = 0;
2462 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smith180f4792011-11-10 06:34:14 +00002463
2464 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2465 I != E; ++I, ++Index) {
2466 (*I)->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002467
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002468 if (IsMsStruct) {
2469 // Zero-length bitfields following non-bitfield members are ignored.
Richard Smith180f4792011-11-10 06:34:14 +00002470 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2471 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002472 continue;
2473 }
Richard Smith180f4792011-11-10 06:34:14 +00002474 LastFD = (*I);
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002475 }
John McCallba4f5d52011-01-20 07:57:12 +00002476 }
2477
Richard Smith180f4792011-11-10 06:34:14 +00002478 assert(CachedFieldIndex && "failed to find field in parent");
2479 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002480}
2481
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002482SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002483 if (const Expr *E = InitializerOrBitWidth.getPointer())
2484 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002485 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002486}
2487
Richard Smith7a614d82011-06-11 17:19:42 +00002488void FieldDecl::setInClassInitializer(Expr *Init) {
2489 assert(!InitializerOrBitWidth.getPointer() &&
2490 "bit width or initializer already set");
2491 InitializerOrBitWidth.setPointer(Init);
2492 InitializerOrBitWidth.setInt(0);
2493}
2494
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002495//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002496// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002497//===----------------------------------------------------------------------===//
2498
Douglas Gregor1693e152010-07-06 18:42:40 +00002499SourceLocation TagDecl::getOuterLocStart() const {
2500 return getTemplateOrInnerLocStart(this);
2501}
2502
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002503SourceRange TagDecl::getSourceRange() const {
2504 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002505 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002506}
2507
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002508TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002509 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002510}
2511
Richard Smith162e1c12011-04-15 14:24:37 +00002512void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2513 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002514 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002515 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002516 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002517}
2518
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002519void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002520 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002521
2522 if (isa<CXXRecordDecl>(this)) {
2523 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2524 struct CXXRecordDecl::DefinitionData *Data =
2525 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002526 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2527 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002528 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002529}
2530
2531void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002532 assert((!isa<CXXRecordDecl>(this) ||
2533 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2534 "definition completed but not started");
2535
John McCall5e1cdac2011-10-07 06:10:15 +00002536 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002537 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002538
2539 if (ASTMutationListener *L = getASTMutationListener())
2540 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002541}
2542
John McCall5e1cdac2011-10-07 06:10:15 +00002543TagDecl *TagDecl::getDefinition() const {
2544 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002545 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002546 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2547 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002548
2549 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002550 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002551 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002552 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002554 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002555}
2556
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002557void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2558 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002559 // Make sure the extended qualifier info is allocated.
2560 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002561 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002562 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002563 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002564 } else {
John McCallb6217662010-03-15 10:12:16 +00002565 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002566 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002567 if (getExtInfo()->NumTemplParamLists == 0) {
2568 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002569 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002570 }
2571 else
2572 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002573 }
2574 }
2575}
2576
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002577void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2578 unsigned NumTPLists,
2579 TemplateParameterList **TPLists) {
2580 assert(NumTPLists > 0);
2581 // Make sure the extended decl info is allocated.
2582 if (!hasExtInfo())
2583 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002584 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002585 // Set the template parameter lists info.
2586 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2587}
2588
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002589//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002590// EnumDecl Implementation
2591//===----------------------------------------------------------------------===//
2592
David Blaikie99ba9e32011-12-20 02:48:34 +00002593void EnumDecl::anchor() { }
2594
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002595EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2596 SourceLocation StartLoc, SourceLocation IdLoc,
2597 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002598 EnumDecl *PrevDecl, bool IsScoped,
2599 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002600 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002601 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002602 C.getTypeDeclType(Enum, PrevDecl);
2603 return Enum;
2604}
2605
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002606EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2607 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2608 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2609 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002610}
2611
Douglas Gregor838db382010-02-11 01:19:42 +00002612void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002613 QualType NewPromotionType,
2614 unsigned NumPositiveBits,
2615 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002616 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002617 if (!IntegerType)
2618 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002619 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002620 setNumPositiveBits(NumPositiveBits);
2621 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002622 TagDecl::completeDefinition();
2623}
2624
Richard Smith1af83c42012-03-23 03:33:32 +00002625TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2626 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2627 return MSI->getTemplateSpecializationKind();
2628
2629 return TSK_Undeclared;
2630}
2631
2632void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2633 SourceLocation PointOfInstantiation) {
2634 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2635 assert(MSI && "Not an instantiated member enumeration?");
2636 MSI->setTemplateSpecializationKind(TSK);
2637 if (TSK != TSK_ExplicitSpecialization &&
2638 PointOfInstantiation.isValid() &&
2639 MSI->getPointOfInstantiation().isInvalid())
2640 MSI->setPointOfInstantiation(PointOfInstantiation);
2641}
2642
Richard Smithf1c66b42012-03-14 23:13:10 +00002643EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2644 if (SpecializationInfo)
2645 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2646
2647 return 0;
2648}
2649
2650void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2651 TemplateSpecializationKind TSK) {
2652 assert(!SpecializationInfo && "Member enum is already a specialization");
2653 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2654}
2655
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002656//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002657// RecordDecl Implementation
2658//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002659
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002660RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2661 SourceLocation StartLoc, SourceLocation IdLoc,
2662 IdentifierInfo *Id, RecordDecl *PrevDecl)
2663 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002664 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002665 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002666 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002667 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002668 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002669}
2670
Jay Foad4ba2a172011-01-12 09:06:06 +00002671RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002672 SourceLocation StartLoc, SourceLocation IdLoc,
2673 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2674 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2675 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002676 C.getTypeDeclType(R, PrevDecl);
2677 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002678}
2679
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002680RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2681 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2682 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2683 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002684}
2685
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002686bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002687 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002688 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2689}
2690
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002691RecordDecl::field_iterator RecordDecl::field_begin() const {
2692 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2693 LoadFieldsFromExternalStorage();
2694
2695 return field_iterator(decl_iterator(FirstDecl));
2696}
2697
Douglas Gregorda2142f2011-02-19 18:51:44 +00002698/// completeDefinition - Notes that the definition of this type is now
2699/// complete.
2700void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002701 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002702 TagDecl::completeDefinition();
2703}
2704
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002705void RecordDecl::LoadFieldsFromExternalStorage() const {
2706 ExternalASTSource *Source = getASTContext().getExternalSource();
2707 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2708
2709 // Notify that we have a RecordDecl doing some initialization.
2710 ExternalASTSource::Deserializing TheFields(Source);
2711
Chris Lattner5f9e2722011-07-23 10:55:15 +00002712 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002713 LoadedFieldsFromExternalStorage = true;
2714 switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2715 case ELR_Success:
2716 break;
2717
2718 case ELR_AlreadyLoaded:
2719 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002720 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002721 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002722
2723#ifndef NDEBUG
2724 // Check that all decls we got were FieldDecls.
2725 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2726 assert(isa<FieldDecl>(Decls[i]));
2727#endif
2728
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002729 if (Decls.empty())
2730 return;
2731
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002732 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2733 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002734}
2735
Steve Naroff56ee6892008-10-08 17:01:13 +00002736//===----------------------------------------------------------------------===//
2737// BlockDecl Implementation
2738//===----------------------------------------------------------------------===//
2739
David Blaikie4278c652011-09-21 18:16:56 +00002740void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002741 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Steve Naroffe78b8092009-03-13 16:56:44 +00002743 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002744 if (!NewParamInfo.empty()) {
2745 NumParams = NewParamInfo.size();
2746 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2747 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002748 }
2749}
2750
John McCall6b5a61b2011-02-07 10:33:21 +00002751void BlockDecl::setCaptures(ASTContext &Context,
2752 const Capture *begin,
2753 const Capture *end,
2754 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002755 CapturesCXXThis = capturesCXXThis;
2756
2757 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002758 NumCaptures = 0;
2759 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002760 return;
2761 }
2762
John McCall6b5a61b2011-02-07 10:33:21 +00002763 NumCaptures = end - begin;
2764
2765 // Avoid new Capture[] because we don't want to provide a default
2766 // constructor.
2767 size_t allocationSize = NumCaptures * sizeof(Capture);
2768 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2769 memcpy(buffer, begin, allocationSize);
2770 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002771}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002772
John McCall204e1332011-06-15 22:51:16 +00002773bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2774 for (capture_const_iterator
2775 i = capture_begin(), e = capture_end(); i != e; ++i)
2776 // Only auto vars can be captured, so no redeclaration worries.
2777 if (i->getVariable() == variable)
2778 return true;
2779
2780 return false;
2781}
2782
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002783SourceRange BlockDecl::getSourceRange() const {
2784 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2785}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002786
2787//===----------------------------------------------------------------------===//
2788// Other Decl Allocation/Deallocation Method Implementations
2789//===----------------------------------------------------------------------===//
2790
David Blaikie99ba9e32011-12-20 02:48:34 +00002791void TranslationUnitDecl::anchor() { }
2792
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002793TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2794 return new (C) TranslationUnitDecl(C);
2795}
2796
David Blaikie99ba9e32011-12-20 02:48:34 +00002797void LabelDecl::anchor() { }
2798
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002799LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002800 SourceLocation IdentL, IdentifierInfo *II) {
2801 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2802}
2803
2804LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2805 SourceLocation IdentL, IdentifierInfo *II,
2806 SourceLocation GnuLabelL) {
2807 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2808 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002809}
2810
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002811LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2812 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2813 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002814}
2815
David Blaikie99ba9e32011-12-20 02:48:34 +00002816void ValueDecl::anchor() { }
2817
2818void ImplicitParamDecl::anchor() { }
2819
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002820ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002821 SourceLocation IdLoc,
2822 IdentifierInfo *Id,
2823 QualType Type) {
2824 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002825}
2826
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002827ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2828 unsigned ID) {
2829 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2830 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2831}
2832
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002833FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002834 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002835 const DeclarationNameInfo &NameInfo,
2836 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002837 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002838 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002839 bool hasWrittenPrototype,
2840 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002841 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2842 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002843 isInlineSpecified,
2844 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002845 New->HasWrittenPrototype = hasWrittenPrototype;
2846 return New;
2847}
2848
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002849FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2850 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2851 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2852 DeclarationNameInfo(), QualType(), 0,
2853 SC_None, SC_None, false, false);
2854}
2855
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002856BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2857 return new (C) BlockDecl(DC, L);
2858}
2859
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002860BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2861 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2862 return new (Mem) BlockDecl(0, SourceLocation());
2863}
2864
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002865EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2866 SourceLocation L,
2867 IdentifierInfo *Id, QualType T,
2868 Expr *E, const llvm::APSInt &V) {
2869 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2870}
2871
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002872EnumConstantDecl *
2873EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2874 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2875 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2876 llvm::APSInt());
2877}
2878
David Blaikie99ba9e32011-12-20 02:48:34 +00002879void IndirectFieldDecl::anchor() { }
2880
Benjamin Kramerd9811462010-11-21 14:11:41 +00002881IndirectFieldDecl *
2882IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2883 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2884 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002885 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2886}
2887
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002888IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2889 unsigned ID) {
2890 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2891 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2892 QualType(), 0, 0);
2893}
2894
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002895SourceRange EnumConstantDecl::getSourceRange() const {
2896 SourceLocation End = getLocation();
2897 if (Init)
2898 End = Init->getLocEnd();
2899 return SourceRange(getLocation(), End);
2900}
2901
David Blaikie99ba9e32011-12-20 02:48:34 +00002902void TypeDecl::anchor() { }
2903
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002904TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002905 SourceLocation StartLoc, SourceLocation IdLoc,
2906 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2907 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002908}
2909
David Blaikie99ba9e32011-12-20 02:48:34 +00002910void TypedefNameDecl::anchor() { }
2911
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002912TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2913 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2914 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2915}
2916
Richard Smith162e1c12011-04-15 14:24:37 +00002917TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2918 SourceLocation StartLoc,
2919 SourceLocation IdLoc, IdentifierInfo *Id,
2920 TypeSourceInfo *TInfo) {
2921 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2922}
2923
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002924TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2925 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2926 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2927}
2928
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002929SourceRange TypedefDecl::getSourceRange() const {
2930 SourceLocation RangeEnd = getLocation();
2931 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2932 if (typeIsPostfix(TInfo->getType()))
2933 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2934 }
2935 return SourceRange(getLocStart(), RangeEnd);
2936}
2937
Richard Smith162e1c12011-04-15 14:24:37 +00002938SourceRange TypeAliasDecl::getSourceRange() const {
2939 SourceLocation RangeEnd = getLocStart();
2940 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2941 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2942 return SourceRange(getLocStart(), RangeEnd);
2943}
2944
David Blaikie99ba9e32011-12-20 02:48:34 +00002945void FileScopeAsmDecl::anchor() { }
2946
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002947FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002948 StringLiteral *Str,
2949 SourceLocation AsmLoc,
2950 SourceLocation RParenLoc) {
2951 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002952}
Douglas Gregor15de72c2011-12-02 23:23:56 +00002953
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002954FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2955 unsigned ID) {
2956 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2957 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2958}
2959
Douglas Gregor15de72c2011-12-02 23:23:56 +00002960//===----------------------------------------------------------------------===//
2961// ImportDecl Implementation
2962//===----------------------------------------------------------------------===//
2963
2964/// \brief Retrieve the number of module identifiers needed to name the given
2965/// module.
2966static unsigned getNumModuleIdentifiers(Module *Mod) {
2967 unsigned Result = 1;
2968 while (Mod->Parent) {
2969 Mod = Mod->Parent;
2970 ++Result;
2971 }
2972 return Result;
2973}
2974
Douglas Gregor5948ae12012-01-03 18:04:46 +00002975ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00002976 Module *Imported,
2977 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00002978 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00002979 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00002980{
2981 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
2982 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
2983 memcpy(StoredLocs, IdentifierLocs.data(),
2984 IdentifierLocs.size() * sizeof(SourceLocation));
2985}
2986
Douglas Gregor5948ae12012-01-03 18:04:46 +00002987ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00002988 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00002989 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00002990 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00002991{
2992 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
2993}
2994
2995ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00002996 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00002997 ArrayRef<SourceLocation> IdentifierLocs) {
2998 void *Mem = C.Allocate(sizeof(ImportDecl) +
2999 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003000 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003001}
3002
3003ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003004 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003005 Module *Imported,
3006 SourceLocation EndLoc) {
3007 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003008 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003009 Import->setImplicit();
3010 return Import;
3011}
3012
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003013ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3014 unsigned NumLocations) {
3015 void *Mem = AllocateDeserializedDecl(C, ID,
3016 (sizeof(ImportDecl) +
3017 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003018 return new (Mem) ImportDecl(EmptyShell());
3019}
3020
3021ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3022 if (!ImportedAndComplete.getInt())
3023 return ArrayRef<SourceLocation>();
3024
3025 const SourceLocation *StoredLocs
3026 = reinterpret_cast<const SourceLocation *>(this + 1);
3027 return ArrayRef<SourceLocation>(StoredLocs,
3028 getNumModuleIdentifiers(getImportedModule()));
3029}
3030
3031SourceRange ImportDecl::getSourceRange() const {
3032 if (!ImportedAndComplete.getInt())
3033 return SourceRange(getLocation(),
3034 *reinterpret_cast<const SourceLocation *>(this + 1));
3035
3036 return SourceRange(getLocation(), getIdentifierLocs().back());
3037}