blob: 13f931ae0b180bc7b7c67a01823b6d3eb4f100ef [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
Rafael Espindola9db614f2012-05-25 16:41:35 +0000161static bool shouldConsiderTemplateVis(const FunctionDecl *fn,
Rafael Espindolacae1c622012-05-21 20:31:27 +0000162 const FunctionTemplateSpecializationInfo *spec) {
163 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCall6ce51ee2011-06-27 23:06:04 +0000164}
165
Rafael Espindolaad359be2012-05-25 14:47:05 +0000166static bool
167shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola0b0ad0a2012-05-21 20:15:56 +0000168 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCall6ce51ee2011-06-27 23:06:04 +0000169}
170
Rafael Espindolab04b7312012-07-13 14:25:36 +0000171static bool useInlineVisibilityHidden(const NamedDecl *D) {
172 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola0bab9da2012-07-13 23:26:43 +0000173 const LangOptions &Opts = D->getASTContext().getLangOpts();
174 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolab04b7312012-07-13 14:25:36 +0000175 return false;
176
177 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
178 if (!FD)
179 return false;
180
181 TemplateSpecializationKind TSK = TSK_Undeclared;
182 if (FunctionTemplateSpecializationInfo *spec
183 = FD->getTemplateSpecializationInfo()) {
184 TSK = spec->getTemplateSpecializationKind();
185 } else if (MemberSpecializationInfo *MSI =
186 FD->getMemberSpecializationInfo()) {
187 TSK = MSI->getTemplateSpecializationKind();
188 }
189
190 const FunctionDecl *Def = 0;
191 // InlineVisibilityHidden only applies to definitions, and
192 // isInlined() only gives meaningful answers on definitions
193 // anyway.
194 return TSK != TSK_ExplicitInstantiationDeclaration &&
195 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolab04b7312012-07-13 14:25:36 +0000196 FD->hasBody(Def) && Def->isInlined();
197}
198
Rafael Espindola1266b612012-04-21 23:28:21 +0000199static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
200 bool OnlyTemplate) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000201 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000202 "Not a name having namespace scope");
203 ASTContext &Context = D->getASTContext();
204
205 // C++ [basic.link]p3:
206 // A name having namespace scope (3.3.6) has internal linkage if it
207 // is the name of
208 // - an object, reference, function or function template that is
209 // explicitly declared static; or,
210 // (This bullet corresponds to C99 6.2.2p3.)
211 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
212 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000213 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000214 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000215
216 // - an object or reference that is explicitly declared const
217 // and neither explicitly declared extern nor previously
218 // declared to have external linkage; or
219 // (there is no equivalent in C99)
David Blaikie4e4d0842012-03-11 07:00:24 +0000220 if (Context.getLangOpts().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000221 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000222 Var->getStorageClass() != SC_Extern &&
223 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000224 bool FoundExtern = false;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000225 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000226 PrevVar && !FoundExtern;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000227 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000228 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000229 FoundExtern = true;
230
231 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000232 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000233 }
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000234 if (Var->getStorageClass() == SC_None) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000235 const VarDecl *PrevVar = Var->getPreviousDecl();
236 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000237 if (PrevVar->getStorageClass() == SC_PrivateExtern)
238 break;
239 if (PrevVar)
240 return PrevVar->getLinkageAndVisibility();
241 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000242 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000243 // C++ [temp]p4:
244 // A non-member function template can have internal linkage; any
245 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000246 const FunctionDecl *Function = 0;
247 if (const FunctionTemplateDecl *FunTmpl
248 = dyn_cast<FunctionTemplateDecl>(D))
249 Function = FunTmpl->getTemplatedDecl();
250 else
251 Function = cast<FunctionDecl>(D);
252
253 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000254 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000255 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000256 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
257 // - a data member of an anonymous union.
258 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000259 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000260 }
261
Chandler Carruth094b6432011-02-24 19:03:39 +0000262 if (D->isInAnonymousNamespace()) {
263 const VarDecl *Var = dyn_cast<VarDecl>(D);
264 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman750dc2b2012-01-15 01:23:58 +0000265 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
266 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth094b6432011-02-24 19:03:39 +0000267 return LinkageInfo::uniqueExternal();
268 }
John McCalle7bc9722010-10-28 04:18:25 +0000269
John McCall1fb0caa2010-10-22 21:05:15 +0000270 // Set up the defaults.
271
272 // C99 6.2.2p5:
273 // If the declaration of an identifier for an object has file
274 // scope and no storage-class specifier, its linkage is
275 // external.
John McCallaf146032010-10-30 11:50:40 +0000276 LinkageInfo LV;
277
Rafael Espindola1266b612012-04-21 23:28:21 +0000278 if (!OnlyTemplate) {
Rafael Espindolae9836a22012-04-16 18:46:26 +0000279 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000280 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000281 } else {
282 // If we're declared in a namespace with a visibility attribute,
283 // use that namespace's visibility, but don't call it explicit.
284 for (const DeclContext *DC = D->getDeclContext();
285 !isa<TranslationUnitDecl>(DC);
286 DC = DC->getParent()) {
287 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
288 if (!ND) continue;
289 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000290 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000291 break;
292 }
293 }
294 }
295 }
296
Rafael Espindolab04b7312012-07-13 14:25:36 +0000297 if (!OnlyTemplate) {
Rafael Espindola4fc14902012-04-19 04:37:16 +0000298 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolab04b7312012-07-13 14:25:36 +0000299 // If we're paying attention to global visibility, apply
300 // -finline-visibility-hidden if this is an inline method.
301 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
302 LV.mergeVisibility(HiddenVisibility, true);
303 }
Rafael Espindolaff257982012-04-19 02:55:01 +0000304
Douglas Gregord85b5b92009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000306
Douglas Gregord85b5b92009-11-25 22:24:25 +0000307 // A name having namespace scope has external linkage if it is the
308 // name of
309 //
310 // - an object or reference, unless it has internal linkage; or
311 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall1fb0caa2010-10-22 21:05:15 +0000315 // Modify the variable's LV by the LV of its type unless this is
316 // C or extern "C". This follows from [basic.link]p9:
317 // A type without linkage shall not be used as the type of a
318 // variable or function with external linkage unless
319 // - the entity has C language linkage, or
320 // - the entity is declared within an unnamed namespace, or
321 // - the entity is not used or is defined in the same
322 // translation unit.
323 // and [basic.link]p10:
324 // ...the types specified by all declarations referring to a
325 // given variable or function shall be identical...
326 // C does not have an equivalent rule.
327 //
John McCallac65c622010-10-26 04:59:26 +0000328 // Ignore this if we've got an explicit attribute; the user
329 // probably knows what they're doing.
330 //
John McCall1fb0caa2010-10-22 21:05:15 +0000331 // Note that we don't want to make the variable non-external
332 // because of this, but unique-external linkage suits us.
David Blaikie4e4d0842012-03-11 07:00:24 +0000333 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000334 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000335 LinkageInfo TypeLV = getLVForType(Var->getType());
336 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000337 return LinkageInfo::uniqueExternal();
Rafael Espindolad70d20a2012-04-19 05:24:05 +0000338 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000339 }
340
John McCall35cebc32010-11-02 18:38:13 +0000341 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000342 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000343
David Blaikie4e4d0842012-03-11 07:00:24 +0000344 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000345 (Var->getStorageClass() == SC_Extern ||
346 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000347
Douglas Gregord85b5b92009-11-25 22:24:25 +0000348 // C99 6.2.2p4:
349 // For an identifier declared with the storage-class specifier
350 // extern in a scope in which a prior declaration of that
351 // identifier is visible, if the prior declaration specifies
352 // internal or external linkage, the linkage of the identifier
353 // at the later declaration is the same as the linkage
354 // specified at the prior declaration. If no prior declaration
355 // is visible, or if the prior declaration specifies no
356 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000357 if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000358 LinkageInfo PrevLV = getLVForDecl(PrevVar, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000359 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
360 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000361 }
362 }
363
Douglas Gregord85b5b92009-11-25 22:24:25 +0000364 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000365 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000366 // In theory, we can modify the function's LV by the LV of its
367 // type unless it has C linkage (see comment above about variables
368 // for justification). In practice, GCC doesn't do this, so it's
369 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000370
John McCall35cebc32010-11-02 18:38:13 +0000371 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000372 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000373
Douglas Gregord85b5b92009-11-25 22:24:25 +0000374 // C99 6.2.2p5:
375 // If the declaration of an identifier for a function has no
376 // storage-class specifier, its linkage is determined exactly
377 // as if it were declared with the storage-class specifier
378 // extern.
David Blaikie4e4d0842012-03-11 07:00:24 +0000379 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000380 (Function->getStorageClass() == SC_Extern ||
381 Function->getStorageClass() == SC_PrivateExtern ||
382 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000383 // C99 6.2.2p4:
384 // For an identifier declared with the storage-class specifier
385 // extern in a scope in which a prior declaration of that
386 // identifier is visible, if the prior declaration specifies
387 // internal or external linkage, the linkage of the identifier
388 // at the later declaration is the same as the linkage
389 // specified at the prior declaration. If no prior declaration
390 // is visible, or if the prior declaration specifies no
391 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000392 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000393 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000394 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
395 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000396 }
397 }
398
John McCallaf8ca372011-02-10 06:50:24 +0000399 // In C++, then if the type of the function uses a type with
400 // unique-external linkage, it's not legally usable from outside
401 // this translation unit. However, we should use the C linkage
402 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000403 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000404 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000405 Function->getType()->getLinkage() == UniqueExternalLinkage)
406 return LinkageInfo::uniqueExternal();
407
John McCall6ce51ee2011-06-27 23:06:04 +0000408 // Consider LV from the template and the template arguments unless
409 // this is an explicit specialization with a visibility attribute.
410 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000411 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola9db614f2012-05-25 16:41:35 +0000412 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
413 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
414 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
415 OnlyTemplate);
416 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000417 LV.mergeWithMin(TempLV);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000418 LV.mergeWithMin(ArgsLV);
419 } else {
420 LV.mergeLinkage(TempLV);
421 LV.mergeLinkage(ArgsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000422 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000423 }
424
Douglas Gregord85b5b92009-11-25 22:24:25 +0000425 // - a named class (Clause 9), or an unnamed class defined in a
426 // typedef declaration in which the class has the typedef name
427 // for linkage purposes (7.1.3); or
428 // - a named enumeration (7.2), or an unnamed enumeration
429 // defined in a typedef declaration in which the enumeration
430 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000431 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
432 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000433 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000434 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000435
John McCall1fb0caa2010-10-22 21:05:15 +0000436 // If this is a class template specialization, consider the
437 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000438 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000439 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindolaad359be2012-05-25 14:47:05 +0000440 // From the template.
441 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000442
Rafael Espindolaad359be2012-05-25 14:47:05 +0000443 // The arguments at which the template was instantiated.
444 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
445 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
446 OnlyTemplate);
447 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000448 LV.mergeWithMin(TempLV);
Rafael Espindolaad359be2012-05-25 14:47:05 +0000449 LV.mergeWithMin(ArgsLV);
450 } else {
451 LV.mergeLinkage(TempLV);
452 LV.mergeLinkage(ArgsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000453 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000454 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000455
456 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000457 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000458 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
459 OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000460 if (!isExternalLinkage(EnumLV.linkage()))
461 return LinkageInfo::none();
462 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000463
464 // - a template, unless it is a function template that has
465 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000466 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola60115a02012-04-22 00:43:48 +0000467 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregord85b5b92009-11-25 22:24:25 +0000468 // - a namespace (7.3), unless it is declared within an unnamed
469 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000470 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
471 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000472
John McCall1fb0caa2010-10-22 21:05:15 +0000473 // By extension, we assign external linkage to Objective-C
474 // interfaces.
475 } else if (isa<ObjCInterfaceDecl>(D)) {
476 // fallout
477
478 // Everything not covered here has no linkage.
479 } else {
John McCallaf146032010-10-30 11:50:40 +0000480 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000481 }
482
483 // If we ended up with non-external linkage, visibility should
484 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000485 if (LV.linkage() != ExternalLinkage)
486 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000487
John McCall1fb0caa2010-10-22 21:05:15 +0000488 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000489}
490
Rafael Espindola1266b612012-04-21 23:28:21 +0000491static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall1fb0caa2010-10-22 21:05:15 +0000492 // Only certain class members have linkage. Note that fields don't
493 // really have linkage, but it's convenient to say they do for the
494 // purposes of calculating linkage of pointer-to-data-member
495 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000496 if (!(isa<CXXMethodDecl>(D) ||
497 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000498 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000499 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000500 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000501 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000502
John McCall36987482010-11-02 01:45:15 +0000503 LinkageInfo LV;
504
John McCall36987482010-11-02 01:45:15 +0000505 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola1266b612012-04-21 23:28:21 +0000506 if (!OnlyTemplate) {
Rafael Espindola41574542012-04-19 04:27:47 +0000507 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000508 LV.mergeVisibility(*Vis, true);
Rafael Espindolab04b7312012-07-13 14:25:36 +0000509 // If we're paying attention to global visibility, apply
510 // -finline-visibility-hidden if this is an inline method.
511 //
512 // Note that we do this before merging information about
513 // the class visibility.
514 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
515 LV.mergeVisibility(HiddenVisibility, true);
John McCall36987482010-11-02 01:45:15 +0000516 }
Rafael Espindolac7e60602012-04-19 05:50:08 +0000517
518 // If this class member has an explicit visibility attribute, the only
519 // thing that can change its visibility is the template arguments, so
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +0000520 // only look for them when processing the class.
Rafael Espindola1266b612012-04-21 23:28:21 +0000521 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola0f905902012-04-16 18:25:01 +0000522
Rafael Espindolac7e60602012-04-19 05:50:08 +0000523 // If this member has an visibility attribute, ClassF will exclude
524 // attributes on the class or command line options, keeping only information
525 // about the template instantiation. If the member has no visibility
526 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
527 // produces the desired result.
Rafael Espindola1266b612012-04-21 23:28:21 +0000528 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
529 ClassOnlyTemplate));
John McCall36987482010-11-02 01:45:15 +0000530 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000531 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000532
533 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000534 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000535 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000536
Rafael Espindola1266b612012-04-21 23:28:21 +0000537 if (!OnlyTemplate)
Rafael Espindola4fc14902012-04-19 04:37:16 +0000538 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaff257982012-04-19 02:55:01 +0000539
John McCall3cdfc4d2010-08-13 08:35:10 +0000540 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000541 // If the type of the function uses a type with unique-external
542 // linkage, it's not legally usable from outside this translation unit.
543 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
544 return LinkageInfo::uniqueExternal();
545
John McCall1fb0caa2010-10-22 21:05:15 +0000546 // If this is a method template specialization, use the linkage for
547 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000548 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000549 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000550 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
551 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
552 OnlyTemplate);
553 TemplateParameterList *TemplateParams =
554 spec->getTemplate()->getTemplateParameters();
555 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000556 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000557 LV.mergeWithMin(ArgsLV);
Rafael Espindola1266b612012-04-21 23:28:21 +0000558 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000559 LV.mergeWithMin(ParamsLV);
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000560 } else {
561 LV.mergeLinkage(ArgsLV);
562 if (!OnlyTemplate)
563 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000564 }
John McCall66cbcf32010-11-01 01:29:57 +0000565 }
John McCall1fb0caa2010-10-22 21:05:15 +0000566
John McCall110e8e52010-10-29 22:22:43 +0000567 // Note that in contrast to basically every other situation, we
568 // *do* apply -fvisibility to method declarations.
569
570 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000571 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000572 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000573 // Merge template argument/parameter information for member
574 // class template specializations.
575 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
576 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
577 OnlyTemplate);
578 TemplateParameterList *TemplateParams =
579 spec->getSpecializedTemplate()->getTemplateParameters();
580 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindolaad359be2012-05-25 14:47:05 +0000581 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000582 LV.mergeWithMin(ArgsLV);
Rafael Espindola59073bb2012-05-25 14:17:45 +0000583 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000584 LV.mergeWithMin(ParamsLV);
Rafael Espindola20831e22012-05-25 15:51:26 +0000585 } else {
586 LV.mergeLinkage(ArgsLV);
587 if (!OnlyTemplate)
588 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000589 }
John McCall110e8e52010-10-29 22:22:43 +0000590 }
591
John McCall110e8e52010-10-29 22:22:43 +0000592 // Static data members.
593 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000594 // Modify the variable's linkage by its type, but ignore the
595 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000596 LinkageInfo TypeLV = getLVForType(VD->getType());
597 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000598 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindolac7e60602012-04-19 05:50:08 +0000599 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000600 }
601
John McCall1fb0caa2010-10-22 21:05:15 +0000602 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000603}
604
John McCallf76b0922011-02-08 19:01:05 +0000605static void clearLinkageForClass(const CXXRecordDecl *record) {
606 for (CXXRecordDecl::decl_iterator
607 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
608 Decl *child = *i;
609 if (isa<NamedDecl>(child))
610 cast<NamedDecl>(child)->ClearLinkageCache();
611 }
612}
613
David Blaikie99ba9e32011-12-20 02:48:34 +0000614void NamedDecl::anchor() { }
615
John McCallf76b0922011-02-08 19:01:05 +0000616void NamedDecl::ClearLinkageCache() {
617 // Note that we can't skip clearing the linkage of children just
618 // because the parent doesn't have cached linkage: we don't cache
619 // when computing linkage for parent contexts.
620
621 HasCachedLinkage = 0;
622
623 // If we're changing the linkage of a class, we need to reset the
624 // linkage of child declarations, too.
625 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
626 clearLinkageForClass(record);
627
John McCall15e310a2011-02-19 02:53:41 +0000628 if (ClassTemplateDecl *temp =
629 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000630 // Clear linkage for the template pattern.
631 CXXRecordDecl *record = temp->getTemplatedDecl();
632 record->HasCachedLinkage = 0;
633 clearLinkageForClass(record);
634
John McCall15e310a2011-02-19 02:53:41 +0000635 // We need to clear linkage for specializations, too.
636 for (ClassTemplateDecl::spec_iterator
637 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
638 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000639 }
John McCall15e310a2011-02-19 02:53:41 +0000640
641 // Clear cached linkage for function template decls, too.
642 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000643 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
644 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000645 for (FunctionTemplateDecl::spec_iterator
646 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
647 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000648 }
John McCall15e310a2011-02-19 02:53:41 +0000649
John McCallf76b0922011-02-08 19:01:05 +0000650}
651
Douglas Gregor381d34e2010-12-06 18:36:25 +0000652Linkage NamedDecl::getLinkage() const {
653 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000654 assert(Linkage(CachedLinkage) ==
Rafael Espindola1266b612012-04-21 23:28:21 +0000655 getLVForDecl(this, true).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000656 return Linkage(CachedLinkage);
657 }
658
Rafael Espindola1266b612012-04-21 23:28:21 +0000659 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000660 HasCachedLinkage = 1;
661 return Linkage(CachedLinkage);
662}
663
John McCallaf146032010-10-30 11:50:40 +0000664LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola1266b612012-04-21 23:28:21 +0000665 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000666 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000667 HasCachedLinkage = 1;
668 CachedLinkage = LI.linkage();
669 return LI;
John McCall0df95872010-10-29 00:29:13 +0000670}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000671
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000672llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
673 // Use the most recent declaration of a variable.
Rafael Espindola797105a2012-05-16 02:10:38 +0000674 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
675 if (llvm::Optional<Visibility> V =
676 getVisibilityOf(Var->getMostRecentDecl()))
677 return V;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000678
Rafael Espindola797105a2012-05-16 02:10:38 +0000679 if (Var->isStaticDataMember()) {
680 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
681 if (InstantiatedFrom)
682 return getVisibilityOf(InstantiatedFrom);
683 }
684
685 return llvm::Optional<Visibility>();
686 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000687 // Use the most recent declaration of a function, and also handle
688 // function template specializations.
689 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
690 if (llvm::Optional<Visibility> V
Douglas Gregoref96ee02012-01-14 16:38:05 +0000691 = getVisibilityOf(fn->getMostRecentDecl()))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000692 return V;
693
694 // If the function is a specialization of a template with an
695 // explicit visibility attribute, use that.
696 if (FunctionTemplateSpecializationInfo *templateInfo
697 = fn->getTemplateSpecializationInfo())
698 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
699
Rafael Espindola860097c2012-02-23 04:17:32 +0000700 // If the function is a member of a specialization of a class template
701 // and the corresponding decl has explicit visibility, use that.
702 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
703 if (InstantiatedFrom)
704 return getVisibilityOf(InstantiatedFrom);
705
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000706 return llvm::Optional<Visibility>();
707 }
708
709 // Otherwise, just check the declaration itself first.
710 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
711 return V;
712
Rafael Espindola98499012012-07-31 19:02:02 +0000713 // The visibility of a template is stored in the templated decl.
714 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
715 return getVisibilityOf(TD->getTemplatedDecl());
716
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000717 // If there wasn't explicit visibility there, and this is a
718 // specialization of a class template, check for visibility
719 // on the pattern.
720 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolad3d02dd2012-07-13 01:19:08 +0000721 = dyn_cast<ClassTemplateSpecializationDecl>(this))
722 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000723
Rafael Espindola860097c2012-02-23 04:17:32 +0000724 // If this is a member class of a specialization of a class template
725 // and the corresponding decl has explicit visibility, use that.
726 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
727 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
728 if (InstantiatedFrom)
729 return getVisibilityOf(InstantiatedFrom);
730 }
731
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000732 return llvm::Optional<Visibility>();
733}
734
Rafael Espindola1266b612012-04-21 23:28:21 +0000735static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000736 // Objective-C: treat all Objective-C declarations as having external
737 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000738 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000739 default:
740 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000741 case Decl::ParmVar:
742 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000743 case Decl::TemplateTemplateParm: // count these as external
744 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000745 case Decl::ObjCAtDefsField:
746 case Decl::ObjCCategory:
747 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000748 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000749 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000750 case Decl::ObjCMethod:
751 case Decl::ObjCProperty:
752 case Decl::ObjCPropertyImpl:
753 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000754 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000755
756 case Decl::CXXRecord: {
757 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
758 if (Record->isLambda()) {
759 if (!Record->getLambdaManglingNumber()) {
760 // This lambda has no mangling number, so it's internal.
761 return LinkageInfo::internal();
762 }
763
764 // This lambda has its linkage/visibility determined by its owner.
765 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
766 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
767 if (isa<ParmVarDecl>(ContextDecl))
768 DC = ContextDecl->getDeclContext()->getRedeclContext();
769 else
Rafael Espindola1266b612012-04-21 23:28:21 +0000770 return getLVForDecl(cast<NamedDecl>(ContextDecl),
771 OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000772 }
773
774 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola1266b612012-04-21 23:28:21 +0000775 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000776
777 return LinkageInfo::external();
778 }
779
780 break;
781 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000782 }
783
Douglas Gregord85b5b92009-11-25 22:24:25 +0000784 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000785 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola1266b612012-04-21 23:28:21 +0000786 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000787
788 // C++ [basic.link]p5:
789 // In addition, a member function, static data member, a named
790 // class or enumeration of class scope, or an unnamed class or
791 // enumeration defined in a class-scope typedef declaration such
792 // that the class or enumeration has the typedef name for linkage
793 // purposes (7.1.3), has external linkage if the name of the class
794 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000795 if (D->getDeclContext()->isRecord())
Rafael Espindola1266b612012-04-21 23:28:21 +0000796 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000797
798 // C++ [basic.link]p6:
799 // The name of a function declared in block scope and the name of
800 // an object declared by a block scope extern declaration have
801 // linkage. If there is a visible declaration of an entity with
802 // linkage having the same name and type, ignoring entities
803 // declared outside the innermost enclosing namespace scope, the
804 // block scope declaration declares that same entity and receives
805 // the linkage of the previous declaration. If there is more than
806 // one such matching entity, the program is ill-formed. Otherwise,
807 // if no matching entity is found, the block scope entity receives
808 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000809 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
810 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000811 if (Function->isInAnonymousNamespace() &&
812 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000813 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000814
John McCallaf146032010-10-30 11:50:40 +0000815 LinkageInfo LV;
Rafael Espindola1266b612012-04-21 23:28:21 +0000816 if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000817 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000818 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000819 }
820
Douglas Gregoref96ee02012-01-14 16:38:05 +0000821 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000822 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000823 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
824 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000825 }
826
827 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000828 }
829
John McCall0df95872010-10-29 00:29:13 +0000830 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000831 if (Var->getStorageClass() == SC_Extern ||
832 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000833 if (Var->isInAnonymousNamespace() &&
834 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000835 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000836
John McCallaf146032010-10-30 11:50:40 +0000837 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000838 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000839 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola1266b612012-04-21 23:28:21 +0000840 else if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000841 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000842 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000843 }
844
Douglas Gregoref96ee02012-01-14 16:38:05 +0000845 if (const VarDecl *Prev = Var->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000846 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000847 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
848 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000849 }
850
851 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000852 }
853 }
854
855 // C++ [basic.link]p6:
856 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000857 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000858}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000859
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000860std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000861 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000862}
863
864std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000865 const DeclContext *Ctx = getDeclContext();
866
867 if (Ctx->isFunctionOrMethod())
868 return getNameAsString();
869
Chris Lattner5f9e2722011-07-23 10:55:15 +0000870 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000871 ContextsTy Contexts;
872
873 // Collect contexts.
874 while (Ctx && isa<NamedDecl>(Ctx)) {
875 Contexts.push_back(Ctx);
876 Ctx = Ctx->getParent();
877 };
878
879 std::string QualName;
880 llvm::raw_string_ostream OS(QualName);
881
882 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
883 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000884 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000885 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000886 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
887 std::string TemplateArgsStr
888 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000889 TemplateArgs.data(),
890 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000891 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000892 OS << Spec->getName() << TemplateArgsStr;
893 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000894 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000895 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000896 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000897 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000898 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
899 if (!RD->getIdentifier())
900 OS << "<anonymous " << RD->getKindName() << '>';
901 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000902 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000903 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000904 const FunctionProtoType *FT = 0;
905 if (FD->hasWrittenPrototype())
Eli Friedman482466b2012-08-30 22:22:09 +0000906 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinig3521d012009-12-28 03:19:38 +0000907
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000908 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000909 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000910 unsigned NumParams = FD->getNumParams();
911 for (unsigned i = 0; i < NumParams; ++i) {
912 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000913 OS << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000914 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinig3521d012009-12-28 03:19:38 +0000915 }
916
917 if (FT->isVariadic()) {
918 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000919 OS << ", ";
920 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000921 }
922 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000923 OS << ')';
924 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000925 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000926 }
927 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000928 }
929
John McCall8472af42010-03-16 21:48:18 +0000930 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000931 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000932 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000933 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000934
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000935 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000936}
937
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000938bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000939 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
940
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000941 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
942 // We want to keep it, unless it nominates same namespace.
943 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000944 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
945 ->getOriginalNamespace() ==
946 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
947 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000948 }
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000950 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
951 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000952 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000953
Douglas Gregore53060f2009-06-25 22:08:12 +0000954 // For function templates, the underlying function declarations are linked.
955 if (const FunctionTemplateDecl *FunctionTemplate
956 = dyn_cast<FunctionTemplateDecl>(this))
957 if (const FunctionTemplateDecl *OldFunctionTemplate
958 = dyn_cast<FunctionTemplateDecl>(OldD))
959 return FunctionTemplate->getTemplatedDecl()
960 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Steve Naroff0de21fd2009-02-22 19:35:57 +0000962 // For method declarations, we keep track of redeclarations.
963 if (isa<ObjCMethodDecl>(this))
964 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000965
John McCallf36e02d2009-10-09 21:13:30 +0000966 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
967 return true;
968
John McCall9488ea12009-11-17 05:59:44 +0000969 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
970 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
971 cast<UsingShadowDecl>(OldD)->getTargetDecl();
972
Douglas Gregordc355712011-02-25 00:36:19 +0000973 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
974 ASTContext &Context = getASTContext();
975 return Context.getCanonicalNestedNameSpecifier(
976 cast<UsingDecl>(this)->getQualifier()) ==
977 Context.getCanonicalNestedNameSpecifier(
978 cast<UsingDecl>(OldD)->getQualifier());
979 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000980
Douglas Gregor7a537402012-01-03 23:26:26 +0000981 // A typedef of an Objective-C class type can replace an Objective-C class
982 // declaration or definition, and vice versa.
983 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
984 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
985 return true;
986
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000987 // For non-function declarations, if the declarations are of the
988 // same kind then this must be a redeclaration, or semantic analysis
989 // would not have given us the new declaration.
990 return this->getKind() == OldD->getKind();
991}
992
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000993bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000994 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000995}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000996
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000997NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000998 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000999 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1000 ND = UD->getTargetDecl();
1001
1002 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1003 return AD->getClassInterface();
1004
1005 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +00001006}
1007
John McCall161755a2010-04-06 21:38:20 +00001008bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +00001009 if (!isCXXClassMember())
1010 return false;
1011
John McCall161755a2010-04-06 21:38:20 +00001012 const NamedDecl *D = this;
1013 if (isa<UsingShadowDecl>(D))
1014 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1015
Francois Pichet87c2e122010-11-21 06:08:52 +00001016 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +00001017 return true;
1018 if (isa<CXXMethodDecl>(D))
1019 return cast<CXXMethodDecl>(D)->isInstance();
1020 if (isa<FunctionTemplateDecl>(D))
1021 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1022 ->getTemplatedDecl())->isInstance();
1023 return false;
1024}
1025
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +00001026//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001027// DeclaratorDecl Implementation
1028//===----------------------------------------------------------------------===//
1029
Douglas Gregor1693e152010-07-06 18:42:40 +00001030template <typename DeclT>
1031static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1032 if (decl->getNumTemplateParameterLists() > 0)
1033 return decl->getTemplateParameterList(0)->getTemplateLoc();
1034 else
1035 return decl->getInnerLocStart();
1036}
1037
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001038SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001039 TypeSourceInfo *TSI = getTypeSourceInfo();
1040 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001041 return SourceLocation();
1042}
1043
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001044void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1045 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001046 // Make sure the extended decl info is allocated.
1047 if (!hasExtInfo()) {
1048 // Save (non-extended) type source info pointer.
1049 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1050 // Allocate external info struct.
1051 DeclInfo = new (getASTContext()) ExtInfo;
1052 // Restore savedTInfo into (extended) decl info.
1053 getExtInfo()->TInfo = savedTInfo;
1054 }
1055 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001056 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001057 } else {
John McCallb6217662010-03-15 10:12:16 +00001058 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001059 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001060 if (getExtInfo()->NumTemplParamLists == 0) {
1061 // Save type source info pointer.
1062 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1063 // Deallocate the extended decl info.
1064 getASTContext().Deallocate(getExtInfo());
1065 // Restore savedTInfo into (non-extended) decl info.
1066 DeclInfo = savedTInfo;
1067 }
1068 else
1069 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001070 }
1071 }
1072}
1073
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001074void
1075DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1076 unsigned NumTPLists,
1077 TemplateParameterList **TPLists) {
1078 assert(NumTPLists > 0);
1079 // Make sure the extended decl info is allocated.
1080 if (!hasExtInfo()) {
1081 // Save (non-extended) type source info pointer.
1082 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1083 // Allocate external info struct.
1084 DeclInfo = new (getASTContext()) ExtInfo;
1085 // Restore savedTInfo into (extended) decl info.
1086 getExtInfo()->TInfo = savedTInfo;
1087 }
1088 // Set the template parameter lists info.
1089 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1090}
1091
Douglas Gregor1693e152010-07-06 18:42:40 +00001092SourceLocation DeclaratorDecl::getOuterLocStart() const {
1093 return getTemplateOrInnerLocStart(this);
1094}
1095
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001096namespace {
1097
1098// Helper function: returns true if QT is or contains a type
1099// having a postfix component.
1100bool typeIsPostfix(clang::QualType QT) {
1101 while (true) {
1102 const Type* T = QT.getTypePtr();
1103 switch (T->getTypeClass()) {
1104 default:
1105 return false;
1106 case Type::Pointer:
1107 QT = cast<PointerType>(T)->getPointeeType();
1108 break;
1109 case Type::BlockPointer:
1110 QT = cast<BlockPointerType>(T)->getPointeeType();
1111 break;
1112 case Type::MemberPointer:
1113 QT = cast<MemberPointerType>(T)->getPointeeType();
1114 break;
1115 case Type::LValueReference:
1116 case Type::RValueReference:
1117 QT = cast<ReferenceType>(T)->getPointeeType();
1118 break;
1119 case Type::PackExpansion:
1120 QT = cast<PackExpansionType>(T)->getPattern();
1121 break;
1122 case Type::Paren:
1123 case Type::ConstantArray:
1124 case Type::DependentSizedArray:
1125 case Type::IncompleteArray:
1126 case Type::VariableArray:
1127 case Type::FunctionProto:
1128 case Type::FunctionNoProto:
1129 return true;
1130 }
1131 }
1132}
1133
1134} // namespace
1135
1136SourceRange DeclaratorDecl::getSourceRange() const {
1137 SourceLocation RangeEnd = getLocation();
1138 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1139 if (typeIsPostfix(TInfo->getType()))
1140 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1141 }
1142 return SourceRange(getOuterLocStart(), RangeEnd);
1143}
1144
Abramo Bagnara9b934882010-06-12 08:15:14 +00001145void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001146QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1147 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001148 TemplateParameterList **TPLists) {
1149 assert((NumTPLists == 0 || TPLists != 0) &&
1150 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001151
1152 // Free previous template parameters (if any).
1153 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001154 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001155 TemplParamLists = 0;
1156 NumTemplParamLists = 0;
1157 }
1158 // Set info on matched template parameter lists (if any).
1159 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001160 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001161 NumTemplParamLists = NumTPLists;
1162 for (unsigned i = NumTPLists; i-- > 0; )
1163 TemplParamLists[i] = TPLists[i];
1164 }
1165}
1166
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001167//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001168// VarDecl Implementation
1169//===----------------------------------------------------------------------===//
1170
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001171const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1172 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001173 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001174 case SC_Auto: return "auto";
1175 case SC_Extern: return "extern";
1176 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1177 case SC_PrivateExtern: return "__private_extern__";
1178 case SC_Register: return "register";
1179 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001180 }
1181
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001182 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001183}
1184
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001185VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1186 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001187 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001188 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001189 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001190}
1191
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001192VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1193 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1194 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1195 QualType(), 0, SC_None, SC_None);
1196}
1197
Douglas Gregor381d34e2010-12-06 18:36:25 +00001198void VarDecl::setStorageClass(StorageClass SC) {
1199 assert(isLegalForVariable(SC));
1200 if (getStorageClass() != SC)
1201 ClearLinkageCache();
1202
John McCallf1e4fbf2011-05-01 02:13:58 +00001203 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001204}
1205
Douglas Gregor1693e152010-07-06 18:42:40 +00001206SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001207 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001208 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001209 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001210}
1211
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001212bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001213 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001214 return false;
1215
Eli Friedman750dc2b2012-01-15 01:23:58 +00001216 const DeclContext *DC = getDeclContext();
1217 if (DC->isRecord())
1218 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001219
Eli Friedman750dc2b2012-01-15 01:23:58 +00001220 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001221 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001222 return true;
1223 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001224}
1225
1226VarDecl *VarDecl::getCanonicalDecl() {
1227 return getFirstDeclaration();
1228}
1229
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001230VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1231 ASTContext &C) const
1232{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001233 // C++ [basic.def]p2:
1234 // A declaration is a definition unless [...] it contains the 'extern'
1235 // specifier or a linkage-specification and neither an initializer [...],
1236 // it declares a static data member in a class declaration [...].
1237 // C++ [temp.expl.spec]p15:
1238 // An explicit specialization of a static data member of a template is a
1239 // definition if the declaration includes an initializer; otherwise, it is
1240 // a declaration.
1241 if (isStaticDataMember()) {
1242 if (isOutOfLine() && (hasInit() ||
1243 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1244 return Definition;
1245 else
1246 return DeclarationOnly;
1247 }
1248 // C99 6.7p5:
1249 // A definition of an identifier is a declaration for that identifier that
1250 // [...] causes storage to be reserved for that object.
1251 // Note: that applies for all non-file-scope objects.
1252 // C99 6.9.2p1:
1253 // If the declaration of an identifier for an object has file scope and an
1254 // initializer, the declaration is an external definition for the identifier
1255 if (hasInit())
1256 return Definition;
1257 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1258 if (hasExternalStorage())
1259 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001260
John McCalld931b082010-08-26 03:08:43 +00001261 if (getStorageClassAsWritten() == SC_Extern ||
1262 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001263 for (const VarDecl *PrevVar = getPreviousDecl();
1264 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001265 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1266 return DeclarationOnly;
1267 }
1268 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001269 // C99 6.9.2p2:
1270 // A declaration of an object that has file scope without an initializer,
1271 // and without a storage class specifier or the scs 'static', constitutes
1272 // a tentative definition.
1273 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001274 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001275 return TentativeDefinition;
1276
1277 // What's left is (in C, block-scope) declarations without initializers or
1278 // external storage. These are definitions.
1279 return Definition;
1280}
1281
Sebastian Redle9d12b62010-01-31 22:27:38 +00001282VarDecl *VarDecl::getActingDefinition() {
1283 DefinitionKind Kind = isThisDeclarationADefinition();
1284 if (Kind != TentativeDefinition)
1285 return 0;
1286
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001287 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001288 VarDecl *First = getFirstDeclaration();
1289 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1290 I != E; ++I) {
1291 Kind = (*I)->isThisDeclarationADefinition();
1292 if (Kind == Definition)
1293 return 0;
1294 else if (Kind == TentativeDefinition)
1295 LastTentative = *I;
1296 }
1297 return LastTentative;
1298}
1299
1300bool VarDecl::isTentativeDefinitionNow() const {
1301 DefinitionKind Kind = isThisDeclarationADefinition();
1302 if (Kind != TentativeDefinition)
1303 return false;
1304
1305 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1306 if ((*I)->isThisDeclarationADefinition() == Definition)
1307 return false;
1308 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001309 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001310}
1311
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001312VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001313 VarDecl *First = getFirstDeclaration();
1314 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1315 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001316 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001317 return *I;
1318 }
1319 return 0;
1320}
1321
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001322VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001323 DefinitionKind Kind = DeclarationOnly;
1324
1325 const VarDecl *First = getFirstDeclaration();
1326 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001327 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001328 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001329 if (Kind == Definition)
1330 break;
1331 }
John McCall110e8e52010-10-29 22:22:43 +00001332
1333 return Kind;
1334}
1335
Sebastian Redl31310a22010-02-01 20:16:42 +00001336const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001337 redecl_iterator I = redecls_begin(), E = redecls_end();
1338 while (I != E && !I->getInit())
1339 ++I;
1340
1341 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001342 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001343 return I->getInit();
1344 }
1345 return 0;
1346}
1347
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001348bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001349 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001350 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001351
1352 if (!isStaticDataMember())
1353 return false;
1354
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001355 // If this static data member was instantiated from a static data member of
1356 // a class template, check whether that static data member was defined
1357 // out-of-line.
1358 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1359 return VD->isOutOfLine();
1360
1361 return false;
1362}
1363
Douglas Gregor0d035142009-10-27 18:42:08 +00001364VarDecl *VarDecl::getOutOfLineDefinition() {
1365 if (!isStaticDataMember())
1366 return 0;
1367
1368 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1369 RD != RDEnd; ++RD) {
1370 if (RD->getLexicalDeclContext()->isFileContext())
1371 return *RD;
1372 }
1373
1374 return 0;
1375}
1376
Douglas Gregor838db382010-02-11 01:19:42 +00001377void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001378 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1379 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001380 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001381 }
1382
1383 Init = I;
1384}
1385
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001386bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001387 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001388
Richard Smith16581332012-03-02 04:14:40 +00001389 if (!Lang.CPlusPlus)
1390 return false;
1391
1392 // In C++11, any variable of reference type can be used in a constant
1393 // expression if it is initialized by a constant expression.
1394 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1395 return true;
1396
1397 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001398 // not require the variable to be non-volatile, but we consider this to be a
1399 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001400 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001401 return false;
1402
1403 // In C++, const, non-volatile variables of integral or enumeration types
1404 // can be used in constant expressions.
1405 if (getType()->isIntegralOrEnumerationType())
1406 return true;
1407
Richard Smith16581332012-03-02 04:14:40 +00001408 // Additionally, in C++11, non-volatile constexpr variables can be used in
1409 // constant expressions.
1410 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001411}
1412
Richard Smith099e7f62011-12-19 06:19:21 +00001413/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1414/// form, which contains extra information on the evaluated value of the
1415/// initializer.
1416EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1417 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1418 if (!Eval) {
1419 Stmt *S = Init.get<Stmt *>();
1420 Eval = new (getASTContext()) EvaluatedStmt;
1421 Eval->Value = S;
1422 Init = Eval;
1423 }
1424 return Eval;
1425}
1426
Richard Smith2d6a5672012-01-14 04:30:29 +00001427APValue *VarDecl::evaluateValue() const {
1428 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1429 return evaluateValue(Notes);
1430}
1431
1432APValue *VarDecl::evaluateValue(
1433 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001434 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1435
1436 // We only produce notes indicating why an initializer is non-constant the
1437 // first time it is evaluated. FIXME: The notes won't always be emitted the
1438 // first time we try evaluation, so might not be produced at all.
1439 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001440 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001441
1442 const Expr *Init = cast<Expr>(Eval->Value);
1443 assert(!Init->isValueDependent());
1444
1445 if (Eval->IsEvaluating) {
1446 // FIXME: Produce a diagnostic for self-initialization.
1447 Eval->CheckedICE = true;
1448 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001449 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001450 }
1451
1452 Eval->IsEvaluating = true;
1453
1454 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1455 this, Notes);
1456
1457 // Ensure the result is an uninitialized APValue if evaluation fails.
1458 if (!Result)
1459 Eval->Evaluated = APValue();
1460
1461 Eval->IsEvaluating = false;
1462 Eval->WasEvaluated = true;
1463
1464 // In C++11, we have determined whether the initializer was a constant
1465 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001466 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001467 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001468 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001469 }
1470
Richard Smith2d6a5672012-01-14 04:30:29 +00001471 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001472}
1473
1474bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001475 // Initializers of weak variables are never ICEs.
1476 if (isWeak())
1477 return false;
1478
Richard Smith099e7f62011-12-19 06:19:21 +00001479 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1480 if (Eval->CheckedICE)
1481 // We have already checked whether this subexpression is an
1482 // integral constant expression.
1483 return Eval->IsICE;
1484
1485 const Expr *Init = cast<Expr>(Eval->Value);
1486 assert(!Init->isValueDependent());
1487
1488 // In C++11, evaluate the initializer to check whether it's a constant
1489 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001490 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001491 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1492 evaluateValue(Notes);
1493 return Eval->IsICE;
1494 }
1495
1496 // It's an ICE whether or not the definition we found is
1497 // out-of-line. See DR 721 and the discussion in Clang PR
1498 // 6206 for details.
1499
1500 if (Eval->CheckingICE)
1501 return false;
1502 Eval->CheckingICE = true;
1503
1504 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1505 Eval->CheckingICE = false;
1506 Eval->CheckedICE = true;
1507 return Eval->IsICE;
1508}
1509
Douglas Gregor03e80032011-06-21 17:03:29 +00001510bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001511 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001512
1513 const Expr *E = getInit();
1514 if (!E)
1515 return false;
1516
1517 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1518 E = Cleanups->getSubExpr();
1519
1520 return isa<MaterializeTemporaryExpr>(E);
1521}
1522
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001523VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001524 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001525 return cast<VarDecl>(MSI->getInstantiatedFrom());
1526
1527 return 0;
1528}
1529
Douglas Gregor663b5a02009-10-14 20:14:33 +00001530TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001531 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001532 return MSI->getTemplateSpecializationKind();
1533
1534 return TSK_Undeclared;
1535}
1536
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001537MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001538 return getASTContext().getInstantiatedFromStaticDataMember(this);
1539}
1540
Douglas Gregor0a897e32009-10-15 17:21:20 +00001541void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1542 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001543 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001544 assert(MSI && "Not an instantiated static data member?");
1545 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001546 if (TSK != TSK_ExplicitSpecialization &&
1547 PointOfInstantiation.isValid() &&
1548 MSI->getPointOfInstantiation().isInvalid())
1549 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001550}
1551
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001552//===----------------------------------------------------------------------===//
1553// ParmVarDecl Implementation
1554//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001555
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001556ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001557 SourceLocation StartLoc,
1558 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001559 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001560 StorageClass S, StorageClass SCAsWritten,
1561 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001562 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001563 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001564}
1565
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001566ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1567 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1568 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1569 0, QualType(), 0, SC_None, SC_None, 0);
1570}
1571
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001572SourceRange ParmVarDecl::getSourceRange() const {
1573 if (!hasInheritedDefaultArg()) {
1574 SourceRange ArgRange = getDefaultArgRange();
1575 if (ArgRange.isValid())
1576 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1577 }
1578
1579 return DeclaratorDecl::getSourceRange();
1580}
1581
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001582Expr *ParmVarDecl::getDefaultArg() {
1583 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1584 assert(!hasUninstantiatedDefaultArg() &&
1585 "Default argument is not yet instantiated!");
1586
1587 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001588 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001589 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001590
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001591 return Arg;
1592}
1593
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001594SourceRange ParmVarDecl::getDefaultArgRange() const {
1595 if (const Expr *E = getInit())
1596 return E->getSourceRange();
1597
1598 if (hasUninstantiatedDefaultArg())
1599 return getUninstantiatedDefaultArg()->getSourceRange();
1600
1601 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001602}
1603
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001604bool ParmVarDecl::isParameterPack() const {
1605 return isa<PackExpansionType>(getType());
1606}
1607
Ted Kremenekd211cb72011-10-06 05:00:56 +00001608void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1609 getASTContext().setParameterIndex(this, parameterIndex);
1610 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1611}
1612
1613unsigned ParmVarDecl::getParameterIndexLarge() const {
1614 return getASTContext().getParameterIndex(this);
1615}
1616
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001617//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001618// FunctionDecl Implementation
1619//===----------------------------------------------------------------------===//
1620
Douglas Gregorda2142f2011-02-19 18:51:44 +00001621void FunctionDecl::getNameForDiagnostic(std::string &S,
1622 const PrintingPolicy &Policy,
1623 bool Qualified) const {
1624 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1625 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1626 if (TemplateArgs)
1627 S += TemplateSpecializationType::PrintTemplateArgumentList(
1628 TemplateArgs->data(),
1629 TemplateArgs->size(),
1630 Policy);
1631
1632}
1633
Ted Kremenek9498d382010-04-29 16:49:01 +00001634bool FunctionDecl::isVariadic() const {
1635 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1636 return FT->isVariadic();
1637 return false;
1638}
1639
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001640bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1641 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001642 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001643 Definition = *I;
1644 return true;
1645 }
1646 }
1647
1648 return false;
1649}
1650
Anders Carlssonffb945f2011-05-14 23:26:09 +00001651bool FunctionDecl::hasTrivialBody() const
1652{
1653 Stmt *S = getBody();
1654 if (!S) {
1655 // Since we don't have a body for this function, we don't know if it's
1656 // trivial or not.
1657 return false;
1658 }
1659
1660 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1661 return true;
1662 return false;
1663}
1664
Sean Hunt10620eb2011-05-06 20:44:56 +00001665bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1666 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001667 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001668 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1669 return true;
1670 }
1671 }
1672
1673 return false;
1674}
1675
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001676Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001677 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1678 if (I->Body) {
1679 Definition = *I;
1680 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001681 } else if (I->IsLateTemplateParsed) {
1682 Definition = *I;
1683 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001684 }
1685 }
1686
1687 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001688}
1689
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001690void FunctionDecl::setBody(Stmt *B) {
1691 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001692 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001693 EndRangeLoc = B->getLocEnd();
1694}
1695
Douglas Gregor21386642010-09-28 21:55:22 +00001696void FunctionDecl::setPure(bool P) {
1697 IsPure = P;
1698 if (P)
1699 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1700 Parent->markedVirtualFunctionPure();
1701}
1702
Richard Smith3f5f5582012-06-08 21:09:22 +00001703void FunctionDecl::setConstexpr(bool IC) {
1704 IsConstexpr = IC;
1705 CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(this);
1706 if (IC && CD)
1707 CD->getParent()->markedConstructorConstexpr(CD);
1708}
1709
Douglas Gregor48a83b52009-09-12 00:17:51 +00001710bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001711 const TranslationUnitDecl *tunit =
1712 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1713 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001714 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001715 getIdentifier() &&
1716 getIdentifier()->isStr("main");
1717}
1718
1719bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1720 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1721 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1722 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1723 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1724 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1725
1726 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1727 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1728
1729 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1730 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1731
1732 ASTContext &Context =
1733 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1734 ->getASTContext();
1735
1736 // The result type and first argument type are constant across all
1737 // these operators. The second argument must be exactly void*.
1738 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001739}
1740
Douglas Gregor48a83b52009-09-12 00:17:51 +00001741bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001742 if (getLinkage() != ExternalLinkage)
1743 return false;
1744
1745 if (getAttr<OverloadableAttr>())
1746 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001747
Chandler Carruth10aad442011-02-25 00:05:02 +00001748 const DeclContext *DC = getDeclContext();
1749 if (DC->isRecord())
1750 return false;
1751
Eli Friedman750dc2b2012-01-15 01:23:58 +00001752 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001753 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001754 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001755
Eli Friedman750dc2b2012-01-15 01:23:58 +00001756 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001757}
1758
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001759bool FunctionDecl::isGlobal() const {
1760 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1761 return Method->isStatic();
1762
John McCalld931b082010-08-26 03:08:43 +00001763 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001764 return false;
1765
Mike Stump1eb44332009-09-09 15:08:12 +00001766 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001767 DC->isNamespace();
1768 DC = DC->getParent()) {
1769 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1770 if (!Namespace->getDeclName())
1771 return false;
1772 break;
1773 }
1774 }
1775
1776 return true;
1777}
1778
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001779void
1780FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1781 redeclarable_base::setPreviousDeclaration(PrevDecl);
1782
1783 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1784 FunctionTemplateDecl *PrevFunTmpl
1785 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1786 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1787 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1788 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001789
Axel Naumannd9d137e2011-11-08 18:21:06 +00001790 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001791 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001792}
1793
1794const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1795 return getFirstDeclaration();
1796}
1797
1798FunctionDecl *FunctionDecl::getCanonicalDecl() {
1799 return getFirstDeclaration();
1800}
1801
Douglas Gregor381d34e2010-12-06 18:36:25 +00001802void FunctionDecl::setStorageClass(StorageClass SC) {
1803 assert(isLegalForFunction(SC));
1804 if (getStorageClass() != SC)
1805 ClearLinkageCache();
1806
1807 SClass = SC;
1808}
1809
Douglas Gregor3e41d602009-02-13 23:20:09 +00001810/// \brief Returns a value indicating whether this function
1811/// corresponds to a builtin function.
1812///
1813/// The function corresponds to a built-in function if it is
1814/// declared at translation scope or within an extern "C" block and
1815/// its name matches with the name of a builtin. The returned value
1816/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001817/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001818/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001819unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001820 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001821 return 0;
1822
1823 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001824 if (!BuiltinID)
1825 return 0;
1826
1827 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001828 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1829 return BuiltinID;
1830
1831 // This function has the name of a known C library
1832 // function. Determine whether it actually refers to the C library
1833 // function or whether it just has the same name.
1834
Douglas Gregor9add3172009-02-17 03:23:10 +00001835 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001836 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001837 return 0;
1838
Douglas Gregor3c385e52009-02-14 18:57:46 +00001839 // If this function is at translation-unit scope and we're not in
1840 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001841 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001842 getDeclContext()->isTranslationUnit())
1843 return BuiltinID;
1844
1845 // If the function is in an extern "C" linkage specification and is
1846 // not marked "overloadable", it's the real function.
1847 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001848 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001849 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001850 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001851 return BuiltinID;
1852
1853 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001854 return 0;
1855}
1856
1857
Chris Lattner1ad9b282009-04-25 06:03:53 +00001858/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001859/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001860/// after it has been created.
1861unsigned FunctionDecl::getNumParams() const {
Eli Friedman482466b2012-08-30 22:22:09 +00001862 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001863 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001864 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001865 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Reid Spencer5f016e22007-07-11 17:01:13 +00001867}
1868
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001869void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001870 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001872 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001875 if (!NewParamInfo.empty()) {
1876 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1877 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 }
1879}
1880
James Molloy16f1f712012-02-29 10:24:19 +00001881void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1882 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1883
1884 if (!NewDecls.empty()) {
1885 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1886 std::copy(NewDecls.begin(), NewDecls.end(), A);
1887 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1888 }
1889}
1890
Chris Lattner8123a952008-04-10 02:22:51 +00001891/// getMinRequiredArguments - Returns the minimum number of arguments
1892/// needed to call this function. This may be fewer than the number of
1893/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001894/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001895unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001896 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001897 return getNumParams();
1898
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001899 unsigned NumRequiredArgs = getNumParams();
1900
1901 // If the last parameter is a parameter pack, we don't need an argument for
1902 // it.
1903 if (NumRequiredArgs > 0 &&
1904 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1905 --NumRequiredArgs;
1906
1907 // If this parameter has a default argument, we don't need an argument for
1908 // it.
1909 while (NumRequiredArgs > 0 &&
1910 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001911 --NumRequiredArgs;
1912
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001913 // We might have parameter packs before the end. These can't be deduced,
1914 // but they can still handle multiple arguments.
1915 unsigned ArgIdx = NumRequiredArgs;
1916 while (ArgIdx > 0) {
1917 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1918 NumRequiredArgs = ArgIdx;
1919
1920 --ArgIdx;
1921 }
1922
Chris Lattner8123a952008-04-10 02:22:51 +00001923 return NumRequiredArgs;
1924}
1925
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001926bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001927 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001928 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001929
1930 if (isa<CXXMethodDecl>(this)) {
1931 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1932 return true;
1933 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001934
1935 switch (getTemplateSpecializationKind()) {
1936 case TSK_Undeclared:
1937 case TSK_ExplicitSpecialization:
1938 return false;
1939
1940 case TSK_ImplicitInstantiation:
1941 case TSK_ExplicitInstantiationDeclaration:
1942 case TSK_ExplicitInstantiationDefinition:
1943 // Handle below.
1944 break;
1945 }
1946
1947 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001948 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001949 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001950 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001951
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001952 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001953 return PatternDecl->isInlined();
1954
1955 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001956}
1957
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001958static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1959 // Only consider file-scope declarations in this test.
1960 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1961 return false;
1962
1963 // Only consider explicit declarations; the presence of a builtin for a
1964 // libcall shouldn't affect whether a definition is externally visible.
1965 if (Redecl->isImplicit())
1966 return false;
1967
1968 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1969 return true; // Not an inline definition
1970
1971 return false;
1972}
1973
Nick Lewyckydce67a72011-07-18 05:26:13 +00001974/// \brief For a function declaration in C or C++, determine whether this
1975/// declaration causes the definition to be externally visible.
1976///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001977/// Specifically, this determines if adding the current declaration to the set
1978/// of redeclarations of the given functions causes
1979/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001980bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1981 assert(!doesThisDeclarationHaveABody() &&
1982 "Must have a declaration without a body.");
1983
1984 ASTContext &Context = getASTContext();
1985
David Blaikie4e4d0842012-03-11 07:00:24 +00001986 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001987 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1988 // an externally visible definition.
1989 //
1990 // FIXME: What happens if gnu_inline gets added on after the first
1991 // declaration?
1992 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1993 return false;
1994
1995 const FunctionDecl *Prev = this;
1996 bool FoundBody = false;
1997 while ((Prev = Prev->getPreviousDecl())) {
1998 FoundBody |= Prev->Body;
1999
2000 if (Prev->Body) {
2001 // If it's not the case that both 'inline' and 'extern' are
2002 // specified on the definition, then it is always externally visible.
2003 if (!Prev->isInlineSpecified() ||
2004 Prev->getStorageClassAsWritten() != SC_Extern)
2005 return false;
2006 } else if (Prev->isInlineSpecified() &&
2007 Prev->getStorageClassAsWritten() != SC_Extern) {
2008 return false;
2009 }
2010 }
2011 return FoundBody;
2012 }
2013
David Blaikie4e4d0842012-03-11 07:00:24 +00002014 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002015 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002016
2017 // C99 6.7.4p6:
2018 // [...] If all of the file scope declarations for a function in a
2019 // translation unit include the inline function specifier without extern,
2020 // then the definition in that translation unit is an inline definition.
2021 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002022 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002023 const FunctionDecl *Prev = this;
2024 bool FoundBody = false;
2025 while ((Prev = Prev->getPreviousDecl())) {
2026 FoundBody |= Prev->Body;
2027 if (RedeclForcesDefC99(Prev))
2028 return false;
2029 }
2030 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00002031}
2032
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002033/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002034/// definition will be externally visible.
2035///
2036/// Inline function definitions are always available for inlining optimizations.
2037/// However, depending on the language dialect, declaration specifiers, and
2038/// attributes, the definition of an inline function may or may not be
2039/// "externally" visible to other translation units in the program.
2040///
2041/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002042/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002043/// inline definition becomes externally visible (C99 6.7.4p6).
2044///
2045/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2046/// definition, we use the GNU semantics for inline, which are nearly the
2047/// opposite of C99 semantics. In particular, "inline" by itself will create
2048/// an externally visible symbol, but "extern inline" will not create an
2049/// externally visible symbol.
2050bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002051 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002052 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002053 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002054
David Blaikie4e4d0842012-03-11 07:00:24 +00002055 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002056 // Note: If you change the logic here, please change
2057 // doesDeclarationForceExternallyVisibleDefinition as well.
2058 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002059 // If it's not the case that both 'inline' and 'extern' are
2060 // specified on the definition, then this inline definition is
2061 // externally visible.
2062 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2063 return true;
2064
2065 // If any declaration is 'inline' but not 'extern', then this definition
2066 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002067 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2068 Redecl != RedeclEnd;
2069 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002070 if (Redecl->isInlineSpecified() &&
2071 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002072 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002073 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002074
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002075 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002076 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002077
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002078 // C99 6.7.4p6:
2079 // [...] If all of the file scope declarations for a function in a
2080 // translation unit include the inline function specifier without extern,
2081 // then the definition in that translation unit is an inline definition.
2082 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2083 Redecl != RedeclEnd;
2084 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002085 if (RedeclForcesDefC99(*Redecl))
2086 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002087 }
2088
2089 // C99 6.7.4p6:
2090 // An inline definition does not provide an external definition for the
2091 // function, and does not forbid an external definition in another
2092 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002093 return false;
2094}
2095
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002096/// getOverloadedOperator - Which C++ overloaded operator this
2097/// function represents, if any.
2098OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002099 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2100 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002101 else
2102 return OO_None;
2103}
2104
Sean Hunta6c058d2010-01-13 09:01:02 +00002105/// getLiteralIdentifier - The literal suffix identifier this function
2106/// represents, if any.
2107const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2108 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2109 return getDeclName().getCXXLiteralIdentifier();
2110 else
2111 return 0;
2112}
2113
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002114FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2115 if (TemplateOrSpecialization.isNull())
2116 return TK_NonTemplate;
2117 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2118 return TK_FunctionTemplate;
2119 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2120 return TK_MemberSpecialization;
2121 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2122 return TK_FunctionTemplateSpecialization;
2123 if (TemplateOrSpecialization.is
2124 <DependentFunctionTemplateSpecializationInfo*>())
2125 return TK_DependentFunctionTemplateSpecialization;
2126
David Blaikieb219cfc2011-09-23 05:06:16 +00002127 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002128}
2129
Douglas Gregor2db32322009-10-07 23:56:10 +00002130FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002131 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002132 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2133
2134 return 0;
2135}
2136
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002137MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2138 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2139}
2140
Douglas Gregor2db32322009-10-07 23:56:10 +00002141void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002142FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2143 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002144 TemplateSpecializationKind TSK) {
2145 assert(TemplateOrSpecialization.isNull() &&
2146 "Member function is already a specialization");
2147 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002148 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002149 TemplateOrSpecialization = Info;
2150}
2151
Douglas Gregor3b846b62009-10-27 20:53:28 +00002152bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002153 // If the function is invalid, it can't be implicitly instantiated.
2154 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002155 return false;
2156
2157 switch (getTemplateSpecializationKind()) {
2158 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002159 case TSK_ExplicitInstantiationDefinition:
2160 return false;
2161
2162 case TSK_ImplicitInstantiation:
2163 return true;
2164
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002165 // It is possible to instantiate TSK_ExplicitSpecialization kind
2166 // if the FunctionDecl has a class scope specialization pattern.
2167 case TSK_ExplicitSpecialization:
2168 return getClassScopeSpecializationPattern() != 0;
2169
Douglas Gregor3b846b62009-10-27 20:53:28 +00002170 case TSK_ExplicitInstantiationDeclaration:
2171 // Handled below.
2172 break;
2173 }
2174
2175 // Find the actual template from which we will instantiate.
2176 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002177 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002178 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002179 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002180
2181 // C++0x [temp.explicit]p9:
2182 // Except for inline functions, other explicit instantiation declarations
2183 // have the effect of suppressing the implicit instantiation of the entity
2184 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002185 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002186 return true;
2187
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002188 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002189}
2190
2191bool FunctionDecl::isTemplateInstantiation() const {
2192 switch (getTemplateSpecializationKind()) {
2193 case TSK_Undeclared:
2194 case TSK_ExplicitSpecialization:
2195 return false;
2196 case TSK_ImplicitInstantiation:
2197 case TSK_ExplicitInstantiationDeclaration:
2198 case TSK_ExplicitInstantiationDefinition:
2199 return true;
2200 }
2201 llvm_unreachable("All TSK values handled.");
2202}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002203
2204FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002205 // Handle class scope explicit specialization special case.
2206 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2207 return getClassScopeSpecializationPattern();
2208
Douglas Gregor3b846b62009-10-27 20:53:28 +00002209 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2210 while (Primary->getInstantiatedFromMemberTemplate()) {
2211 // If we have hit a point where the user provided a specialization of
2212 // this template, we're done looking.
2213 if (Primary->isMemberSpecialization())
2214 break;
2215
2216 Primary = Primary->getInstantiatedFromMemberTemplate();
2217 }
2218
2219 return Primary->getTemplatedDecl();
2220 }
2221
2222 return getInstantiatedFromMemberFunction();
2223}
2224
Douglas Gregor16e8be22009-06-29 17:30:29 +00002225FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002226 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002227 = TemplateOrSpecialization
2228 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002229 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002230 }
2231 return 0;
2232}
2233
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002234FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2235 return getASTContext().getClassScopeSpecializationPattern(this);
2236}
2237
Douglas Gregor16e8be22009-06-29 17:30:29 +00002238const TemplateArgumentList *
2239FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002240 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002241 = TemplateOrSpecialization
2242 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002243 return Info->TemplateArguments;
2244 }
2245 return 0;
2246}
2247
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002248const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002249FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2250 if (FunctionTemplateSpecializationInfo *Info
2251 = TemplateOrSpecialization
2252 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2253 return Info->TemplateArgumentsAsWritten;
2254 }
2255 return 0;
2256}
2257
Mike Stump1eb44332009-09-09 15:08:12 +00002258void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002259FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2260 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002261 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002262 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002263 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002264 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2265 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002266 assert(TSK != TSK_Undeclared &&
2267 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002268 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002269 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002270 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002271 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2272 TemplateArgs,
2273 TemplateArgsAsWritten,
2274 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002275 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002276 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002277}
2278
John McCallaf2094e2010-04-08 09:05:18 +00002279void
2280FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2281 const UnresolvedSetImpl &Templates,
2282 const TemplateArgumentListInfo &TemplateArgs) {
2283 assert(TemplateOrSpecialization.isNull());
2284 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2285 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002286 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002287 void *Buffer = Context.Allocate(Size);
2288 DependentFunctionTemplateSpecializationInfo *Info =
2289 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2290 TemplateArgs);
2291 TemplateOrSpecialization = Info;
2292}
2293
2294DependentFunctionTemplateSpecializationInfo::
2295DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2296 const TemplateArgumentListInfo &TArgs)
2297 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2298
2299 d.NumTemplates = Ts.size();
2300 d.NumArgs = TArgs.size();
2301
2302 FunctionTemplateDecl **TsArray =
2303 const_cast<FunctionTemplateDecl**>(getTemplates());
2304 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2305 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2306
2307 TemplateArgumentLoc *ArgsArray =
2308 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2309 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2310 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2311}
2312
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002313TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002314 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002315 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002316 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002317 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002318 if (FTSInfo)
2319 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Douglas Gregor2db32322009-10-07 23:56:10 +00002321 MemberSpecializationInfo *MSInfo
2322 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2323 if (MSInfo)
2324 return MSInfo->getTemplateSpecializationKind();
2325
2326 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002327}
2328
Mike Stump1eb44332009-09-09 15:08:12 +00002329void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002330FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2331 SourceLocation PointOfInstantiation) {
2332 if (FunctionTemplateSpecializationInfo *FTSInfo
2333 = TemplateOrSpecialization.dyn_cast<
2334 FunctionTemplateSpecializationInfo*>()) {
2335 FTSInfo->setTemplateSpecializationKind(TSK);
2336 if (TSK != TSK_ExplicitSpecialization &&
2337 PointOfInstantiation.isValid() &&
2338 FTSInfo->getPointOfInstantiation().isInvalid())
2339 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2340 } else if (MemberSpecializationInfo *MSInfo
2341 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2342 MSInfo->setTemplateSpecializationKind(TSK);
2343 if (TSK != TSK_ExplicitSpecialization &&
2344 PointOfInstantiation.isValid() &&
2345 MSInfo->getPointOfInstantiation().isInvalid())
2346 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2347 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002348 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002349}
2350
2351SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002352 if (FunctionTemplateSpecializationInfo *FTSInfo
2353 = TemplateOrSpecialization.dyn_cast<
2354 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002355 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002356 else if (MemberSpecializationInfo *MSInfo
2357 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002358 return MSInfo->getPointOfInstantiation();
2359
2360 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002361}
2362
Douglas Gregor9f185072009-09-11 20:15:17 +00002363bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002364 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002365 return true;
2366
2367 // If this function was instantiated from a member function of a
2368 // class template, check whether that member function was defined out-of-line.
2369 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2370 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002371 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002372 return Definition->isOutOfLine();
2373 }
2374
2375 // If this function was instantiated from a function template,
2376 // check whether that function template was defined out-of-line.
2377 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2378 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002379 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002380 return Definition->isOutOfLine();
2381 }
2382
2383 return false;
2384}
2385
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002386SourceRange FunctionDecl::getSourceRange() const {
2387 return SourceRange(getOuterLocStart(), EndRangeLoc);
2388}
2389
Anna Zaks9392d4e2012-01-18 02:45:01 +00002390unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002391 IdentifierInfo *FnInfo = getIdentifier();
2392
2393 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002394 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002395
2396 // Builtin handling.
2397 switch (getBuiltinID()) {
2398 case Builtin::BI__builtin_memset:
2399 case Builtin::BI__builtin___memset_chk:
2400 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002401 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002402
2403 case Builtin::BI__builtin_memcpy:
2404 case Builtin::BI__builtin___memcpy_chk:
2405 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002406 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002407
2408 case Builtin::BI__builtin_memmove:
2409 case Builtin::BI__builtin___memmove_chk:
2410 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002411 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002412
2413 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002414 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002415 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002416 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002417
2418 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002419 case Builtin::BImemcmp:
2420 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002421
2422 case Builtin::BI__builtin_strncpy:
2423 case Builtin::BI__builtin___strncpy_chk:
2424 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002425 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002426
2427 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002428 case Builtin::BIstrncmp:
2429 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002430
2431 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002432 case Builtin::BIstrncasecmp:
2433 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002434
2435 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002436 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002437 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002438 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002439
2440 case Builtin::BI__builtin_strndup:
2441 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002442 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002443
Anna Zaksc36bedc2012-02-01 19:08:57 +00002444 case Builtin::BI__builtin_strlen:
2445 case Builtin::BIstrlen:
2446 return Builtin::BIstrlen;
2447
Anna Zaksd9b859a2012-01-13 21:52:01 +00002448 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002449 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002450 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002451 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002452 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002453 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002454 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002455 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002456 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002457 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002458 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002459 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002460 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002461 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002462 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002463 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002464 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002465 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002466 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002467 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002468 else if (FnInfo->isStr("strlen"))
2469 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002470 }
2471 break;
2472 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002473 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002474}
2475
Chris Lattner8a934232008-03-31 00:36:02 +00002476//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002477// FieldDecl Implementation
2478//===----------------------------------------------------------------------===//
2479
Jay Foad4ba2a172011-01-12 09:06:06 +00002480FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002481 SourceLocation StartLoc, SourceLocation IdLoc,
2482 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002483 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smithca523302012-06-10 03:12:00 +00002484 InClassInitStyle InitStyle) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002485 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithca523302012-06-10 03:12:00 +00002486 BW, Mutable, InitStyle);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002487}
2488
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002489FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2490 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2491 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smithca523302012-06-10 03:12:00 +00002492 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002493}
2494
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002495bool FieldDecl::isAnonymousStructOrUnion() const {
2496 if (!isImplicit() || getDeclName())
2497 return false;
2498
2499 if (const RecordType *Record = getType()->getAs<RecordType>())
2500 return Record->getDecl()->isAnonymousStructOrUnion();
2501
2502 return false;
2503}
2504
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002505unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2506 assert(isBitField() && "not a bitfield");
2507 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2508 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2509}
2510
John McCallba4f5d52011-01-20 07:57:12 +00002511unsigned FieldDecl::getFieldIndex() const {
2512 if (CachedFieldIndex) return CachedFieldIndex - 1;
2513
Richard Smith180f4792011-11-10 06:34:14 +00002514 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002515 const RecordDecl *RD = getParent();
2516 const FieldDecl *LastFD = 0;
2517 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
Richard Smith180f4792011-11-10 06:34:14 +00002518
2519 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2520 I != E; ++I, ++Index) {
David Blaikie262bc182012-04-30 02:36:29 +00002521 I->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002522
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002523 if (IsMsStruct) {
2524 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie581deb32012-06-06 20:45:41 +00002525 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smith180f4792011-11-10 06:34:14 +00002526 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002527 continue;
2528 }
David Blaikie581deb32012-06-06 20:45:41 +00002529 LastFD = *I;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002530 }
John McCallba4f5d52011-01-20 07:57:12 +00002531 }
2532
Richard Smith180f4792011-11-10 06:34:14 +00002533 assert(CachedFieldIndex && "failed to find field in parent");
2534 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002535}
2536
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002537SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002538 if (const Expr *E = InitializerOrBitWidth.getPointer())
2539 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002540 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002541}
2542
Abramo Bagnaraa5335762012-07-02 20:35:48 +00002543void FieldDecl::setBitWidth(Expr *Width) {
2544 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2545 "bit width or initializer already set");
2546 InitializerOrBitWidth.setPointer(Width);
2547}
2548
Richard Smith7a614d82011-06-11 17:19:42 +00002549void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smithca523302012-06-10 03:12:00 +00002550 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith7a614d82011-06-11 17:19:42 +00002551 "bit width or initializer already set");
2552 InitializerOrBitWidth.setPointer(Init);
Richard Smith7a614d82011-06-11 17:19:42 +00002553}
2554
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002555//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002556// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002557//===----------------------------------------------------------------------===//
2558
Douglas Gregor1693e152010-07-06 18:42:40 +00002559SourceLocation TagDecl::getOuterLocStart() const {
2560 return getTemplateOrInnerLocStart(this);
2561}
2562
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002563SourceRange TagDecl::getSourceRange() const {
2564 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002565 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002566}
2567
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002568TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002569 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002570}
2571
Richard Smith162e1c12011-04-15 14:24:37 +00002572void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2573 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002574 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002575 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002576 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002577}
2578
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002579void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002580 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002581
2582 if (isa<CXXRecordDecl>(this)) {
2583 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2584 struct CXXRecordDecl::DefinitionData *Data =
2585 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002586 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2587 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002588 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002589}
2590
2591void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002592 assert((!isa<CXXRecordDecl>(this) ||
2593 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2594 "definition completed but not started");
2595
John McCall5e1cdac2011-10-07 06:10:15 +00002596 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002597 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002598
2599 if (ASTMutationListener *L = getASTMutationListener())
2600 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002601}
2602
John McCall5e1cdac2011-10-07 06:10:15 +00002603TagDecl *TagDecl::getDefinition() const {
2604 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002605 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002606 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2607 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002608
2609 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002610 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002611 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002612 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002613
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002614 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002615}
2616
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002617void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2618 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002619 // Make sure the extended qualifier info is allocated.
2620 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002621 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002622 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002623 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002624 } else {
John McCallb6217662010-03-15 10:12:16 +00002625 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002626 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002627 if (getExtInfo()->NumTemplParamLists == 0) {
2628 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002629 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002630 }
2631 else
2632 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002633 }
2634 }
2635}
2636
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002637void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2638 unsigned NumTPLists,
2639 TemplateParameterList **TPLists) {
2640 assert(NumTPLists > 0);
2641 // Make sure the extended decl info is allocated.
2642 if (!hasExtInfo())
2643 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002644 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002645 // Set the template parameter lists info.
2646 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2647}
2648
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002649//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002650// EnumDecl Implementation
2651//===----------------------------------------------------------------------===//
2652
David Blaikie99ba9e32011-12-20 02:48:34 +00002653void EnumDecl::anchor() { }
2654
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002655EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2656 SourceLocation StartLoc, SourceLocation IdLoc,
2657 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002658 EnumDecl *PrevDecl, bool IsScoped,
2659 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002660 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002661 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002662 C.getTypeDeclType(Enum, PrevDecl);
2663 return Enum;
2664}
2665
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002666EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2667 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2668 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2669 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002670}
2671
Douglas Gregor838db382010-02-11 01:19:42 +00002672void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002673 QualType NewPromotionType,
2674 unsigned NumPositiveBits,
2675 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002676 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002677 if (!IntegerType)
2678 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002679 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002680 setNumPositiveBits(NumPositiveBits);
2681 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002682 TagDecl::completeDefinition();
2683}
2684
Richard Smith1af83c42012-03-23 03:33:32 +00002685TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2686 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2687 return MSI->getTemplateSpecializationKind();
2688
2689 return TSK_Undeclared;
2690}
2691
2692void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2693 SourceLocation PointOfInstantiation) {
2694 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2695 assert(MSI && "Not an instantiated member enumeration?");
2696 MSI->setTemplateSpecializationKind(TSK);
2697 if (TSK != TSK_ExplicitSpecialization &&
2698 PointOfInstantiation.isValid() &&
2699 MSI->getPointOfInstantiation().isInvalid())
2700 MSI->setPointOfInstantiation(PointOfInstantiation);
2701}
2702
Richard Smithf1c66b42012-03-14 23:13:10 +00002703EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2704 if (SpecializationInfo)
2705 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2706
2707 return 0;
2708}
2709
2710void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2711 TemplateSpecializationKind TSK) {
2712 assert(!SpecializationInfo && "Member enum is already a specialization");
2713 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2714}
2715
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002716//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002717// RecordDecl Implementation
2718//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002719
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002720RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2721 SourceLocation StartLoc, SourceLocation IdLoc,
2722 IdentifierInfo *Id, RecordDecl *PrevDecl)
2723 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002724 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002725 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002726 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002727 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002728 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002729}
2730
Jay Foad4ba2a172011-01-12 09:06:06 +00002731RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002732 SourceLocation StartLoc, SourceLocation IdLoc,
2733 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2734 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2735 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002736 C.getTypeDeclType(R, PrevDecl);
2737 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002738}
2739
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002740RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2741 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2742 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2743 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002744}
2745
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002746bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002747 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002748 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2749}
2750
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002751RecordDecl::field_iterator RecordDecl::field_begin() const {
2752 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2753 LoadFieldsFromExternalStorage();
2754
2755 return field_iterator(decl_iterator(FirstDecl));
2756}
2757
Douglas Gregorda2142f2011-02-19 18:51:44 +00002758/// completeDefinition - Notes that the definition of this type is now
2759/// complete.
2760void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002761 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002762 TagDecl::completeDefinition();
2763}
2764
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002765static bool isFieldOrIndirectField(Decl::Kind K) {
2766 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2767}
2768
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002769void RecordDecl::LoadFieldsFromExternalStorage() const {
2770 ExternalASTSource *Source = getASTContext().getExternalSource();
2771 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2772
2773 // Notify that we have a RecordDecl doing some initialization.
2774 ExternalASTSource::Deserializing TheFields(Source);
2775
Chris Lattner5f9e2722011-07-23 10:55:15 +00002776 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002777 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002778 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2779 Decls)) {
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002780 case ELR_Success:
2781 break;
2782
2783 case ELR_AlreadyLoaded:
2784 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002785 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002786 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002787
2788#ifndef NDEBUG
2789 // Check that all decls we got were FieldDecls.
2790 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002791 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002792#endif
2793
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002794 if (Decls.empty())
2795 return;
2796
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002797 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2798 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002799}
2800
Steve Naroff56ee6892008-10-08 17:01:13 +00002801//===----------------------------------------------------------------------===//
2802// BlockDecl Implementation
2803//===----------------------------------------------------------------------===//
2804
David Blaikie4278c652011-09-21 18:16:56 +00002805void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002806 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002807
Steve Naroffe78b8092009-03-13 16:56:44 +00002808 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002809 if (!NewParamInfo.empty()) {
2810 NumParams = NewParamInfo.size();
2811 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2812 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002813 }
2814}
2815
John McCall6b5a61b2011-02-07 10:33:21 +00002816void BlockDecl::setCaptures(ASTContext &Context,
2817 const Capture *begin,
2818 const Capture *end,
2819 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002820 CapturesCXXThis = capturesCXXThis;
2821
2822 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002823 NumCaptures = 0;
2824 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002825 return;
2826 }
2827
John McCall6b5a61b2011-02-07 10:33:21 +00002828 NumCaptures = end - begin;
2829
2830 // Avoid new Capture[] because we don't want to provide a default
2831 // constructor.
2832 size_t allocationSize = NumCaptures * sizeof(Capture);
2833 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2834 memcpy(buffer, begin, allocationSize);
2835 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002836}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002837
John McCall204e1332011-06-15 22:51:16 +00002838bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2839 for (capture_const_iterator
2840 i = capture_begin(), e = capture_end(); i != e; ++i)
2841 // Only auto vars can be captured, so no redeclaration worries.
2842 if (i->getVariable() == variable)
2843 return true;
2844
2845 return false;
2846}
2847
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002848SourceRange BlockDecl::getSourceRange() const {
2849 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2850}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002851
2852//===----------------------------------------------------------------------===//
2853// Other Decl Allocation/Deallocation Method Implementations
2854//===----------------------------------------------------------------------===//
2855
David Blaikie99ba9e32011-12-20 02:48:34 +00002856void TranslationUnitDecl::anchor() { }
2857
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002858TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2859 return new (C) TranslationUnitDecl(C);
2860}
2861
David Blaikie99ba9e32011-12-20 02:48:34 +00002862void LabelDecl::anchor() { }
2863
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002864LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002865 SourceLocation IdentL, IdentifierInfo *II) {
2866 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2867}
2868
2869LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2870 SourceLocation IdentL, IdentifierInfo *II,
2871 SourceLocation GnuLabelL) {
2872 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2873 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002874}
2875
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002876LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2877 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2878 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002879}
2880
David Blaikie99ba9e32011-12-20 02:48:34 +00002881void ValueDecl::anchor() { }
2882
2883void ImplicitParamDecl::anchor() { }
2884
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002885ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002886 SourceLocation IdLoc,
2887 IdentifierInfo *Id,
2888 QualType Type) {
2889 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002890}
2891
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002892ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2893 unsigned ID) {
2894 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2895 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2896}
2897
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002898FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002899 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002900 const DeclarationNameInfo &NameInfo,
2901 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002902 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002903 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002904 bool hasWrittenPrototype,
2905 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002906 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2907 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002908 isInlineSpecified,
2909 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002910 New->HasWrittenPrototype = hasWrittenPrototype;
2911 return New;
2912}
2913
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002914FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2915 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2916 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2917 DeclarationNameInfo(), QualType(), 0,
2918 SC_None, SC_None, false, false);
2919}
2920
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002921BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2922 return new (C) BlockDecl(DC, L);
2923}
2924
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002925BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2926 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2927 return new (Mem) BlockDecl(0, SourceLocation());
2928}
2929
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002930EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2931 SourceLocation L,
2932 IdentifierInfo *Id, QualType T,
2933 Expr *E, const llvm::APSInt &V) {
2934 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2935}
2936
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002937EnumConstantDecl *
2938EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2939 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2940 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2941 llvm::APSInt());
2942}
2943
David Blaikie99ba9e32011-12-20 02:48:34 +00002944void IndirectFieldDecl::anchor() { }
2945
Benjamin Kramerd9811462010-11-21 14:11:41 +00002946IndirectFieldDecl *
2947IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2948 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2949 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002950 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2951}
2952
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002953IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2954 unsigned ID) {
2955 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2956 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2957 QualType(), 0, 0);
2958}
2959
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002960SourceRange EnumConstantDecl::getSourceRange() const {
2961 SourceLocation End = getLocation();
2962 if (Init)
2963 End = Init->getLocEnd();
2964 return SourceRange(getLocation(), End);
2965}
2966
David Blaikie99ba9e32011-12-20 02:48:34 +00002967void TypeDecl::anchor() { }
2968
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002969TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002970 SourceLocation StartLoc, SourceLocation IdLoc,
2971 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2972 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002973}
2974
David Blaikie99ba9e32011-12-20 02:48:34 +00002975void TypedefNameDecl::anchor() { }
2976
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002977TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2978 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2979 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2980}
2981
Richard Smith162e1c12011-04-15 14:24:37 +00002982TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2983 SourceLocation StartLoc,
2984 SourceLocation IdLoc, IdentifierInfo *Id,
2985 TypeSourceInfo *TInfo) {
2986 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2987}
2988
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002989TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2990 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2991 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2992}
2993
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002994SourceRange TypedefDecl::getSourceRange() const {
2995 SourceLocation RangeEnd = getLocation();
2996 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2997 if (typeIsPostfix(TInfo->getType()))
2998 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2999 }
3000 return SourceRange(getLocStart(), RangeEnd);
3001}
3002
Richard Smith162e1c12011-04-15 14:24:37 +00003003SourceRange TypeAliasDecl::getSourceRange() const {
3004 SourceLocation RangeEnd = getLocStart();
3005 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3006 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3007 return SourceRange(getLocStart(), RangeEnd);
3008}
3009
David Blaikie99ba9e32011-12-20 02:48:34 +00003010void FileScopeAsmDecl::anchor() { }
3011
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003012FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00003013 StringLiteral *Str,
3014 SourceLocation AsmLoc,
3015 SourceLocation RParenLoc) {
3016 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003017}
Douglas Gregor15de72c2011-12-02 23:23:56 +00003018
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003019FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3020 unsigned ID) {
3021 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3022 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3023}
3024
Douglas Gregor15de72c2011-12-02 23:23:56 +00003025//===----------------------------------------------------------------------===//
3026// ImportDecl Implementation
3027//===----------------------------------------------------------------------===//
3028
3029/// \brief Retrieve the number of module identifiers needed to name the given
3030/// module.
3031static unsigned getNumModuleIdentifiers(Module *Mod) {
3032 unsigned Result = 1;
3033 while (Mod->Parent) {
3034 Mod = Mod->Parent;
3035 ++Result;
3036 }
3037 return Result;
3038}
3039
Douglas Gregor5948ae12012-01-03 18:04:46 +00003040ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003041 Module *Imported,
3042 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003043 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003044 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003045{
3046 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3047 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3048 memcpy(StoredLocs, IdentifierLocs.data(),
3049 IdentifierLocs.size() * sizeof(SourceLocation));
3050}
3051
Douglas Gregor5948ae12012-01-03 18:04:46 +00003052ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003053 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003054 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003055 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003056{
3057 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3058}
3059
3060ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003061 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003062 ArrayRef<SourceLocation> IdentifierLocs) {
3063 void *Mem = C.Allocate(sizeof(ImportDecl) +
3064 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003065 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003066}
3067
3068ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003069 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003070 Module *Imported,
3071 SourceLocation EndLoc) {
3072 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003073 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003074 Import->setImplicit();
3075 return Import;
3076}
3077
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003078ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3079 unsigned NumLocations) {
3080 void *Mem = AllocateDeserializedDecl(C, ID,
3081 (sizeof(ImportDecl) +
3082 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003083 return new (Mem) ImportDecl(EmptyShell());
3084}
3085
3086ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3087 if (!ImportedAndComplete.getInt())
3088 return ArrayRef<SourceLocation>();
3089
3090 const SourceLocation *StoredLocs
3091 = reinterpret_cast<const SourceLocation *>(this + 1);
3092 return ArrayRef<SourceLocation>(StoredLocs,
3093 getNumModuleIdentifiers(getImportedModule()));
3094}
3095
3096SourceRange ImportDecl::getSourceRange() const {
3097 if (!ImportedAndComplete.getInt())
3098 return SourceRange(getLocation(),
3099 *reinterpret_cast<const SourceLocation *>(this + 1));
3100
3101 return SourceRange(getLocation(), getIdentifierLocs().back());
3102}