blob: a3cdb401ea262b4496c24787e5ff76438c1fb247 [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"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.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"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000024#include "clang/AST/Stmt.h"
25#include "clang/AST/TypeLoc.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000027#include "clang/Basic/IdentifierTable.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000028#include "clang/Basic/Module.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000029#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000030#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000031#include "llvm/Support/ErrorHandling.h"
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
Rafael Espindoladfb31662012-12-25 00:39:58 +0000107/// Compute the linkage and visibility for the given declaration.
108static LinkageInfo computeLVForDecl(const NamedDecl *D, bool OnlyTemplate);
109
110static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
111 if (!OnlyTemplate)
112 return D->getLinkageAndVisibility();
113 return computeLVForDecl(D, OnlyTemplate);
114}
Douglas Gregor381d34e2010-12-06 18:36:25 +0000115
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000116/// \brief Get the most restrictive linkage for the types and
117/// declarations in the given template argument list.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000118static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
119 unsigned NumArgs,
Rafael Espindola1266b612012-04-21 23:28:21 +0000120 bool OnlyTemplate) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000121 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000122
123 for (unsigned I = 0; I != NumArgs; ++I) {
124 switch (Args[I].getKind()) {
125 case TemplateArgument::Null:
126 case TemplateArgument::Integral:
127 case TemplateArgument::Expression:
128 break;
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000129
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000130 case TemplateArgument::Type:
Rafael Espindola923b0c92012-04-23 17:51:55 +0000131 LV.mergeWithMin(getLVForType(Args[I].getAsType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000132 break;
133
134 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +0000135 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
136 LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate));
137 break;
138
139 case TemplateArgument::NullPtr:
140 LV.mergeWithMin(getLVForType(Args[I].getNullPtrType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000141 break;
142
143 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000144 case TemplateArgument::TemplateExpansion:
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000145 if (TemplateDecl *Template
Douglas Gregora7fc9012011-01-05 18:58:31 +0000146 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola923b0c92012-04-23 17:51:55 +0000147 LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000148 break;
149
150 case TemplateArgument::Pack:
Rafael Espindola860097c2012-02-23 04:17:32 +0000151 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
152 Args[I].pack_size(),
Rafael Espindola1266b612012-04-21 23:28:21 +0000153 OnlyTemplate));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000154 break;
155 }
156 }
157
John McCall1fb0caa2010-10-22 21:05:15 +0000158 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000159}
160
Rafael Espindola093ecc92012-01-14 00:30:36 +0000161static LinkageInfo
Douglas Gregor381d34e2010-12-06 18:36:25 +0000162getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola1266b612012-04-21 23:28:21 +0000163 bool OnlyTemplate) {
164 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall3cdfc4d2010-08-13 08:35:10 +0000165}
166
Rafael Espindola9db614f2012-05-25 16:41:35 +0000167static bool shouldConsiderTemplateVis(const FunctionDecl *fn,
Rafael Espindolacae1c622012-05-21 20:31:27 +0000168 const FunctionTemplateSpecializationInfo *spec) {
169 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCall6ce51ee2011-06-27 23:06:04 +0000170}
171
Rafael Espindolaad359be2012-05-25 14:47:05 +0000172static bool
173shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola0b0ad0a2012-05-21 20:15:56 +0000174 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCall6ce51ee2011-06-27 23:06:04 +0000175}
176
Rafael Espindolab04b7312012-07-13 14:25:36 +0000177static bool useInlineVisibilityHidden(const NamedDecl *D) {
178 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola0bab9da2012-07-13 23:26:43 +0000179 const LangOptions &Opts = D->getASTContext().getLangOpts();
180 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolab04b7312012-07-13 14:25:36 +0000181 return false;
182
183 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
184 if (!FD)
185 return false;
186
187 TemplateSpecializationKind TSK = TSK_Undeclared;
188 if (FunctionTemplateSpecializationInfo *spec
189 = FD->getTemplateSpecializationInfo()) {
190 TSK = spec->getTemplateSpecializationKind();
191 } else if (MemberSpecializationInfo *MSI =
192 FD->getMemberSpecializationInfo()) {
193 TSK = MSI->getTemplateSpecializationKind();
194 }
195
196 const FunctionDecl *Def = 0;
197 // InlineVisibilityHidden only applies to definitions, and
198 // isInlined() only gives meaningful answers on definitions
199 // anyway.
200 return TSK != TSK_ExplicitInstantiationDeclaration &&
201 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola0142f0c2012-10-11 16:32:25 +0000202 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolab04b7312012-07-13 14:25:36 +0000203}
204
Rafael Espindola1266b612012-04-21 23:28:21 +0000205static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
206 bool OnlyTemplate) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000207 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000208 "Not a name having namespace scope");
209 ASTContext &Context = D->getASTContext();
210
211 // C++ [basic.link]p3:
212 // A name having namespace scope (3.3.6) has internal linkage if it
213 // is the name of
214 // - an object, reference, function or function template that is
215 // explicitly declared static; or,
216 // (This bullet corresponds to C99 6.2.2p3.)
217 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
218 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000219 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000220 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000221
Richard Smith820e9a72012-10-19 06:37:48 +0000222 // - a non-volatile object or reference that is explicitly declared const
223 // or constexpr and neither explicitly declared extern nor previously
224 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikie4e4d0842012-03-11 07:00:24 +0000225 if (Context.getLangOpts().CPlusPlus &&
Richard Smith820e9a72012-10-19 06:37:48 +0000226 Var->getType().isConstQualified() &&
227 !Var->getType().isVolatileQualified() &&
John McCalld931b082010-08-26 03:08:43 +0000228 Var->getStorageClass() != SC_Extern &&
229 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000230 bool FoundExtern = false;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000231 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000232 PrevVar && !FoundExtern;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000233 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000234 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000235 FoundExtern = true;
236
237 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000238 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000239 }
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000240 if (Var->getStorageClass() == SC_None) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000241 const VarDecl *PrevVar = Var->getPreviousDecl();
242 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000243 if (PrevVar->getStorageClass() == SC_PrivateExtern)
244 break;
Eli Friedman8c7a1852012-10-26 23:05:34 +0000245 if (PrevVar)
246 return PrevVar->getLinkageAndVisibility();
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000247 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000248 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000249 // C++ [temp]p4:
250 // A non-member function template can have internal linkage; any
251 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000252 const FunctionDecl *Function = 0;
253 if (const FunctionTemplateDecl *FunTmpl
254 = dyn_cast<FunctionTemplateDecl>(D))
255 Function = FunTmpl->getTemplatedDecl();
256 else
257 Function = cast<FunctionDecl>(D);
258
259 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000260 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000261 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000262 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
263 // - a data member of an anonymous union.
264 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000265 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000266 }
267
Chandler Carruth094b6432011-02-24 19:03:39 +0000268 if (D->isInAnonymousNamespace()) {
269 const VarDecl *Var = dyn_cast<VarDecl>(D);
270 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman750dc2b2012-01-15 01:23:58 +0000271 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
272 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth094b6432011-02-24 19:03:39 +0000273 return LinkageInfo::uniqueExternal();
274 }
John McCalle7bc9722010-10-28 04:18:25 +0000275
John McCall1fb0caa2010-10-22 21:05:15 +0000276 // Set up the defaults.
277
278 // C99 6.2.2p5:
279 // If the declaration of an identifier for an object has file
280 // scope and no storage-class specifier, its linkage is
281 // external.
John McCallaf146032010-10-30 11:50:40 +0000282 LinkageInfo LV;
283
Rafael Espindola1266b612012-04-21 23:28:21 +0000284 if (!OnlyTemplate) {
Rafael Espindolae9836a22012-04-16 18:46:26 +0000285 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000286 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000287 } else {
288 // If we're declared in a namespace with a visibility attribute,
289 // use that namespace's visibility, but don't call it explicit.
290 for (const DeclContext *DC = D->getDeclContext();
291 !isa<TranslationUnitDecl>(DC);
292 DC = DC->getParent()) {
293 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
294 if (!ND) continue;
295 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000296 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000297 break;
298 }
299 }
300 }
301 }
302
Rafael Espindolab04b7312012-07-13 14:25:36 +0000303 if (!OnlyTemplate) {
Rafael Espindola4fc14902012-04-19 04:37:16 +0000304 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolab04b7312012-07-13 14:25:36 +0000305 // If we're paying attention to global visibility, apply
306 // -finline-visibility-hidden if this is an inline method.
307 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
308 LV.mergeVisibility(HiddenVisibility, true);
309 }
Rafael Espindolaff257982012-04-19 02:55:01 +0000310
Douglas Gregord85b5b92009-11-25 22:24:25 +0000311 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000312
Douglas Gregord85b5b92009-11-25 22:24:25 +0000313 // A name having namespace scope has external linkage if it is the
314 // name of
315 //
316 // - an object or reference, unless it has internal linkage; or
317 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000318 // GCC applies the following optimization to variables and static
319 // data members, but not to functions:
320 //
John McCall1fb0caa2010-10-22 21:05:15 +0000321 // Modify the variable's LV by the LV of its type unless this is
322 // C or extern "C". This follows from [basic.link]p9:
323 // A type without linkage shall not be used as the type of a
324 // variable or function with external linkage unless
325 // - the entity has C language linkage, or
326 // - the entity is declared within an unnamed namespace, or
327 // - the entity is not used or is defined in the same
328 // translation unit.
329 // and [basic.link]p10:
330 // ...the types specified by all declarations referring to a
331 // given variable or function shall be identical...
332 // C does not have an equivalent rule.
333 //
John McCallac65c622010-10-26 04:59:26 +0000334 // Ignore this if we've got an explicit attribute; the user
335 // probably knows what they're doing.
336 //
John McCall1fb0caa2010-10-22 21:05:15 +0000337 // Note that we don't want to make the variable non-external
338 // because of this, but unique-external linkage suits us.
David Blaikie4e4d0842012-03-11 07:00:24 +0000339 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000340 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000341 LinkageInfo TypeLV = getLVForType(Var->getType());
342 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000343 return LinkageInfo::uniqueExternal();
Rafael Espindolad70d20a2012-04-19 05:24:05 +0000344 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000345 }
346
John McCall35cebc32010-11-02 18:38:13 +0000347 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000348 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000349
Rafael Espindola538fb982012-11-12 04:10:23 +0000350 // Note that Sema::MergeVarDecl already takes care of implementing
351 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
352 // to do it here.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000353
Douglas Gregord85b5b92009-11-25 22:24:25 +0000354 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000355 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000356 // In theory, we can modify the function's LV by the LV of its
357 // type unless it has C linkage (see comment above about variables
358 // for justification). In practice, GCC doesn't do this, so it's
359 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000360
John McCall35cebc32010-11-02 18:38:13 +0000361 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000362 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000363
Rafael Espindola51758612012-11-21 02:47:19 +0000364 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
365 // merging storage classes and visibility attributes, so we don't have to
366 // look at previous decls in here.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000367
John McCallaf8ca372011-02-10 06:50:24 +0000368 // In C++, then if the type of the function uses a type with
369 // unique-external linkage, it's not legally usable from outside
370 // this translation unit. However, we should use the C linkage
371 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000372 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000373 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000374 Function->getType()->getLinkage() == UniqueExternalLinkage)
375 return LinkageInfo::uniqueExternal();
376
John McCall6ce51ee2011-06-27 23:06:04 +0000377 // Consider LV from the template and the template arguments unless
378 // this is an explicit specialization with a visibility attribute.
379 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000380 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola9db614f2012-05-25 16:41:35 +0000381 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
382 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
383 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
384 OnlyTemplate);
385 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000386 LV.mergeWithMin(TempLV);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000387 LV.mergeWithMin(ArgsLV);
388 } else {
389 LV.mergeLinkage(TempLV);
390 LV.mergeLinkage(ArgsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000391 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000392 }
393
Douglas Gregord85b5b92009-11-25 22:24:25 +0000394 // - a named class (Clause 9), or an unnamed class defined in a
395 // typedef declaration in which the class has the typedef name
396 // for linkage purposes (7.1.3); or
397 // - a named enumeration (7.2), or an unnamed enumeration
398 // defined in a typedef declaration in which the enumeration
399 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000400 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
401 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000402 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000403 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000404
John McCall1fb0caa2010-10-22 21:05:15 +0000405 // If this is a class template specialization, consider the
406 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000407 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000408 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindolaad359be2012-05-25 14:47:05 +0000409 // From the template.
410 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000411
Rafael Espindolaad359be2012-05-25 14:47:05 +0000412 // The arguments at which the template was instantiated.
413 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
414 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
415 OnlyTemplate);
416 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000417 LV.mergeWithMin(TempLV);
Rafael Espindolaad359be2012-05-25 14:47:05 +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 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000424
425 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000426 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000427 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
428 OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000429 if (!isExternalLinkage(EnumLV.linkage()))
430 return LinkageInfo::none();
431 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000432
433 // - a template, unless it is a function template that has
434 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000435 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola60115a02012-04-22 00:43:48 +0000436 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregord85b5b92009-11-25 22:24:25 +0000437 // - a namespace (7.3), unless it is declared within an unnamed
438 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000439 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
440 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000441
John McCall1fb0caa2010-10-22 21:05:15 +0000442 // By extension, we assign external linkage to Objective-C
443 // interfaces.
444 } else if (isa<ObjCInterfaceDecl>(D)) {
445 // fallout
446
447 // Everything not covered here has no linkage.
448 } else {
John McCallaf146032010-10-30 11:50:40 +0000449 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000450 }
451
452 // If we ended up with non-external linkage, visibility should
453 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000454 if (LV.linkage() != ExternalLinkage)
455 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000456
John McCall1fb0caa2010-10-22 21:05:15 +0000457 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000458}
459
Rafael Espindola1266b612012-04-21 23:28:21 +0000460static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall1fb0caa2010-10-22 21:05:15 +0000461 // Only certain class members have linkage. Note that fields don't
462 // really have linkage, but it's convenient to say they do for the
463 // purposes of calculating linkage of pointer-to-data-member
464 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000465 if (!(isa<CXXMethodDecl>(D) ||
466 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000467 isa<FieldDecl>(D) ||
David Blaikie66cff722012-11-14 01:52:05 +0000468 isa<TagDecl>(D)))
John McCallaf146032010-10-30 11:50:40 +0000469 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000470
John McCall36987482010-11-02 01:45:15 +0000471 LinkageInfo LV;
472
John McCall36987482010-11-02 01:45:15 +0000473 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola1266b612012-04-21 23:28:21 +0000474 if (!OnlyTemplate) {
Rafael Espindola41574542012-04-19 04:27:47 +0000475 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000476 LV.mergeVisibility(*Vis, true);
Rafael Espindolab04b7312012-07-13 14:25:36 +0000477 // If we're paying attention to global visibility, apply
478 // -finline-visibility-hidden if this is an inline method.
479 //
480 // Note that we do this before merging information about
481 // the class visibility.
482 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
483 LV.mergeVisibility(HiddenVisibility, true);
John McCall36987482010-11-02 01:45:15 +0000484 }
Rafael Espindolac7e60602012-04-19 05:50:08 +0000485
486 // If this class member has an explicit visibility attribute, the only
487 // thing that can change its visibility is the template arguments, so
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +0000488 // only look for them when processing the class.
Rafael Espindola1266b612012-04-21 23:28:21 +0000489 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola0f905902012-04-16 18:25:01 +0000490
Rafael Espindolac7e60602012-04-19 05:50:08 +0000491 // If this member has an visibility attribute, ClassF will exclude
492 // attributes on the class or command line options, keeping only information
493 // about the template instantiation. If the member has no visibility
494 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
495 // produces the desired result.
Rafael Espindola1266b612012-04-21 23:28:21 +0000496 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
497 ClassOnlyTemplate));
John McCall36987482010-11-02 01:45:15 +0000498 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000499 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000500
501 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000502 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000503 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000504
Rafael Espindola1266b612012-04-21 23:28:21 +0000505 if (!OnlyTemplate)
Rafael Espindola4fc14902012-04-19 04:37:16 +0000506 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaff257982012-04-19 02:55:01 +0000507
John McCall3cdfc4d2010-08-13 08:35:10 +0000508 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000509 // If the type of the function uses a type with unique-external
510 // linkage, it's not legally usable from outside this translation unit.
511 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
512 return LinkageInfo::uniqueExternal();
513
John McCall1fb0caa2010-10-22 21:05:15 +0000514 // If this is a method template specialization, use the linkage for
515 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000516 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000517 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000518 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
519 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
520 OnlyTemplate);
521 TemplateParameterList *TemplateParams =
522 spec->getTemplate()->getTemplateParameters();
523 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000524 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000525 LV.mergeWithMin(ArgsLV);
Rafael Espindola1266b612012-04-21 23:28:21 +0000526 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000527 LV.mergeWithMin(ParamsLV);
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000528 } else {
529 LV.mergeLinkage(ArgsLV);
530 if (!OnlyTemplate)
531 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000532 }
John McCall66cbcf32010-11-01 01:29:57 +0000533 }
John McCall1fb0caa2010-10-22 21:05:15 +0000534
John McCall110e8e52010-10-29 22:22:43 +0000535 // Note that in contrast to basically every other situation, we
536 // *do* apply -fvisibility to method declarations.
537
538 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000539 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000540 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000541 // Merge template argument/parameter information for member
542 // class template specializations.
543 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
544 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
545 OnlyTemplate);
546 TemplateParameterList *TemplateParams =
547 spec->getSpecializedTemplate()->getTemplateParameters();
548 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindolaad359be2012-05-25 14:47:05 +0000549 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000550 LV.mergeWithMin(ArgsLV);
Rafael Espindola59073bb2012-05-25 14:17:45 +0000551 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000552 LV.mergeWithMin(ParamsLV);
Rafael Espindola20831e22012-05-25 15:51:26 +0000553 } else {
554 LV.mergeLinkage(ArgsLV);
555 if (!OnlyTemplate)
556 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000557 }
John McCall110e8e52010-10-29 22:22:43 +0000558 }
559
John McCall110e8e52010-10-29 22:22:43 +0000560 // Static data members.
561 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000562 // Modify the variable's linkage by its type, but ignore the
563 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000564 LinkageInfo TypeLV = getLVForType(VD->getType());
565 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000566 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindolac7e60602012-04-19 05:50:08 +0000567 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000568 }
569
John McCall1fb0caa2010-10-22 21:05:15 +0000570 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000571}
572
John McCallf76b0922011-02-08 19:01:05 +0000573static void clearLinkageForClass(const CXXRecordDecl *record) {
574 for (CXXRecordDecl::decl_iterator
575 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
576 Decl *child = *i;
577 if (isa<NamedDecl>(child))
Rafael Espindoladfb31662012-12-25 00:39:58 +0000578 cast<NamedDecl>(child)->ClearLVCache();
John McCallf76b0922011-02-08 19:01:05 +0000579 }
580}
581
David Blaikie99ba9e32011-12-20 02:48:34 +0000582void NamedDecl::anchor() { }
583
Rafael Espindoladfb31662012-12-25 00:39:58 +0000584void NamedDecl::ClearLVCache() {
John McCallf76b0922011-02-08 19:01:05 +0000585 // Note that we can't skip clearing the linkage of children just
586 // because the parent doesn't have cached linkage: we don't cache
587 // when computing linkage for parent contexts.
588
Rafael Espindoladfb31662012-12-25 00:39:58 +0000589 CacheValidAndVisibility = 0;
John McCallf76b0922011-02-08 19:01:05 +0000590
591 // If we're changing the linkage of a class, we need to reset the
592 // linkage of child declarations, too.
593 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
594 clearLinkageForClass(record);
595
John McCall15e310a2011-02-19 02:53:41 +0000596 if (ClassTemplateDecl *temp =
597 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000598 // Clear linkage for the template pattern.
599 CXXRecordDecl *record = temp->getTemplatedDecl();
Rafael Espindoladfb31662012-12-25 00:39:58 +0000600 record->CacheValidAndVisibility = 0;
John McCallf76b0922011-02-08 19:01:05 +0000601 clearLinkageForClass(record);
602
John McCall15e310a2011-02-19 02:53:41 +0000603 // We need to clear linkage for specializations, too.
604 for (ClassTemplateDecl::spec_iterator
605 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindoladfb31662012-12-25 00:39:58 +0000606 i->ClearLVCache();
John McCallf76b0922011-02-08 19:01:05 +0000607 }
John McCall15e310a2011-02-19 02:53:41 +0000608
609 // Clear cached linkage for function template decls, too.
610 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000611 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
Rafael Espindoladfb31662012-12-25 00:39:58 +0000612 temp->getTemplatedDecl()->ClearLVCache();
John McCall15e310a2011-02-19 02:53:41 +0000613 for (FunctionTemplateDecl::spec_iterator
614 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindoladfb31662012-12-25 00:39:58 +0000615 i->ClearLVCache();
John McCall78951942011-03-22 06:58:49 +0000616 }
John McCall15e310a2011-02-19 02:53:41 +0000617
John McCallf76b0922011-02-08 19:01:05 +0000618}
619
Douglas Gregor381d34e2010-12-06 18:36:25 +0000620Linkage NamedDecl::getLinkage() const {
Rafael Espindoladfb31662012-12-25 00:39:58 +0000621 return getLinkageAndVisibility().linkage();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000622}
623
John McCallaf146032010-10-30 11:50:40 +0000624LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindoladfb31662012-12-25 00:39:58 +0000625 if (CacheValidAndVisibility) {
626 Linkage L = static_cast<Linkage>(CachedLinkage);
627 Visibility V = static_cast<Visibility>(CacheValidAndVisibility - 1);
628 bool Explicit = CachedVisibilityExplicit;
629 LinkageInfo LV(L, V, Explicit);
630 assert(LV == computeLVForDecl(this, false));
631 return LV;
632 }
633 LinkageInfo LV = computeLVForDecl(this, false);
634 CachedLinkage = LV.linkage();
635 CacheValidAndVisibility = LV.visibility() + 1;
636 CachedVisibilityExplicit = LV.visibilityExplicit();
637 return LV;
John McCall0df95872010-10-29 00:29:13 +0000638}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000639
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000640llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
641 // Use the most recent declaration of a variable.
Rafael Espindola797105a2012-05-16 02:10:38 +0000642 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolac6b82c32012-11-12 04:32:23 +0000643 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola797105a2012-05-16 02:10:38 +0000644 return V;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000645
Rafael Espindola797105a2012-05-16 02:10:38 +0000646 if (Var->isStaticDataMember()) {
647 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
648 if (InstantiatedFrom)
649 return getVisibilityOf(InstantiatedFrom);
650 }
651
652 return llvm::Optional<Visibility>();
653 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000654 // Use the most recent declaration of a function, and also handle
655 // function template specializations.
656 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolac6b82c32012-11-12 04:32:23 +0000657 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000658 return V;
659
660 // If the function is a specialization of a template with an
661 // explicit visibility attribute, use that.
662 if (FunctionTemplateSpecializationInfo *templateInfo
663 = fn->getTemplateSpecializationInfo())
664 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
665
Rafael Espindola860097c2012-02-23 04:17:32 +0000666 // If the function is a member of a specialization of a class template
667 // and the corresponding decl has explicit visibility, use that.
668 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
669 if (InstantiatedFrom)
670 return getVisibilityOf(InstantiatedFrom);
671
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000672 return llvm::Optional<Visibility>();
673 }
674
675 // Otherwise, just check the declaration itself first.
676 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
677 return V;
678
Rafael Espindola98499012012-07-31 19:02:02 +0000679 // The visibility of a template is stored in the templated decl.
680 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
681 return getVisibilityOf(TD->getTemplatedDecl());
682
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000683 // If there wasn't explicit visibility there, and this is a
684 // specialization of a class template, check for visibility
685 // on the pattern.
686 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolad3d02dd2012-07-13 01:19:08 +0000687 = dyn_cast<ClassTemplateSpecializationDecl>(this))
688 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000689
Rafael Espindola860097c2012-02-23 04:17:32 +0000690 // If this is a member class of a specialization of a class template
691 // and the corresponding decl has explicit visibility, use that.
692 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
693 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
694 if (InstantiatedFrom)
695 return getVisibilityOf(InstantiatedFrom);
696 }
697
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000698 return llvm::Optional<Visibility>();
699}
700
Rafael Espindoladfb31662012-12-25 00:39:58 +0000701static LinkageInfo computeLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000702 // Objective-C: treat all Objective-C declarations as having external
703 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000704 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000705 default:
706 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000707 case Decl::ParmVar:
708 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000709 case Decl::TemplateTemplateParm: // count these as external
710 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000711 case Decl::ObjCAtDefsField:
712 case Decl::ObjCCategory:
713 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000714 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000715 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000716 case Decl::ObjCMethod:
717 case Decl::ObjCProperty:
718 case Decl::ObjCPropertyImpl:
719 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000720 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000721
722 case Decl::CXXRecord: {
723 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
724 if (Record->isLambda()) {
725 if (!Record->getLambdaManglingNumber()) {
726 // This lambda has no mangling number, so it's internal.
727 return LinkageInfo::internal();
728 }
729
730 // This lambda has its linkage/visibility determined by its owner.
731 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
732 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
733 if (isa<ParmVarDecl>(ContextDecl))
734 DC = ContextDecl->getDeclContext()->getRedeclContext();
735 else
Rafael Espindola1266b612012-04-21 23:28:21 +0000736 return getLVForDecl(cast<NamedDecl>(ContextDecl),
737 OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000738 }
739
740 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola1266b612012-04-21 23:28:21 +0000741 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000742
743 return LinkageInfo::external();
744 }
745
746 break;
747 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000748 }
749
Douglas Gregord85b5b92009-11-25 22:24:25 +0000750 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000751 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola1266b612012-04-21 23:28:21 +0000752 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000753
754 // C++ [basic.link]p5:
755 // In addition, a member function, static data member, a named
756 // class or enumeration of class scope, or an unnamed class or
757 // enumeration defined in a class-scope typedef declaration such
758 // that the class or enumeration has the typedef name for linkage
759 // purposes (7.1.3), has external linkage if the name of the class
760 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000761 if (D->getDeclContext()->isRecord())
Rafael Espindola1266b612012-04-21 23:28:21 +0000762 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000763
764 // C++ [basic.link]p6:
765 // The name of a function declared in block scope and the name of
766 // an object declared by a block scope extern declaration have
767 // linkage. If there is a visible declaration of an entity with
768 // linkage having the same name and type, ignoring entities
769 // declared outside the innermost enclosing namespace scope, the
770 // block scope declaration declares that same entity and receives
771 // the linkage of the previous declaration. If there is more than
772 // one such matching entity, the program is ill-formed. Otherwise,
773 // if no matching entity is found, the block scope entity receives
774 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000775 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
776 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000777 if (Function->isInAnonymousNamespace() &&
778 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000779 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000780
John McCallaf146032010-10-30 11:50:40 +0000781 LinkageInfo LV;
Rafael Espindola1266b612012-04-21 23:28:21 +0000782 if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000783 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000784 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000785 }
Rafael Espindolab2829202012-11-29 16:38:22 +0000786
787 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
788 // merging storage classes and visibility attributes, so we don't have to
789 // look at previous decls in here.
John McCall1fb0caa2010-10-22 21:05:15 +0000790
791 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000792 }
793
John McCall0df95872010-10-29 00:29:13 +0000794 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
Rafael Espindolaa7a2f2a2012-12-18 04:18:55 +0000795 if (Var->getStorageClassAsWritten() == SC_Extern ||
796 Var->getStorageClassAsWritten() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000797 if (Var->isInAnonymousNamespace() &&
798 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000799 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000800
Rafael Espindolaa7a2f2a2012-12-18 04:18:55 +0000801 // This is an "extern int foo;" which got merged with a file static.
802 if (Var->getStorageClass() == SC_Static)
803 return LinkageInfo::internal();
804
John McCallaf146032010-10-30 11:50:40 +0000805 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000806 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000807 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola1266b612012-04-21 23:28:21 +0000808 else if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000809 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000810 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000811 }
John McCall1fb0caa2010-10-22 21:05:15 +0000812
Rafael Espindola538fb982012-11-12 04:10:23 +0000813 // Note that Sema::MergeVarDecl already takes care of implementing
814 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
815 // have to do it here.
John McCall1fb0caa2010-10-22 21:05:15 +0000816 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000817 }
818 }
819
820 // C++ [basic.link]p6:
821 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000822 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000823}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000824
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000825std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000826 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000827}
828
829std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000830 const DeclContext *Ctx = getDeclContext();
831
832 if (Ctx->isFunctionOrMethod())
833 return getNameAsString();
834
Chris Lattner5f9e2722011-07-23 10:55:15 +0000835 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000836 ContextsTy Contexts;
837
838 // Collect contexts.
839 while (Ctx && isa<NamedDecl>(Ctx)) {
840 Contexts.push_back(Ctx);
841 Ctx = Ctx->getParent();
842 };
843
844 std::string QualName;
845 llvm::raw_string_ostream OS(QualName);
846
847 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
848 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000849 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000850 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000851 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
852 std::string TemplateArgsStr
853 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000854 TemplateArgs.data(),
855 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000856 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000857 OS << Spec->getName() << TemplateArgsStr;
858 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000859 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000860 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000861 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000862 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000863 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
864 if (!RD->getIdentifier())
865 OS << "<anonymous " << RD->getKindName() << '>';
866 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000867 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000868 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000869 const FunctionProtoType *FT = 0;
870 if (FD->hasWrittenPrototype())
Eli Friedman482466b2012-08-30 22:22:09 +0000871 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinig3521d012009-12-28 03:19:38 +0000872
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000873 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000874 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000875 unsigned NumParams = FD->getNumParams();
876 for (unsigned i = 0; i < NumParams; ++i) {
877 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000878 OS << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000879 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinig3521d012009-12-28 03:19:38 +0000880 }
881
882 if (FT->isVariadic()) {
883 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000884 OS << ", ";
885 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000886 }
887 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000888 OS << ')';
889 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000890 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000891 }
892 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000893 }
894
John McCall8472af42010-03-16 21:48:18 +0000895 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000896 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000897 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000898 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000899
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000900 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000901}
902
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000903bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000904 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
905
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000906 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
907 // We want to keep it, unless it nominates same namespace.
908 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000909 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
910 ->getOriginalNamespace() ==
911 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
912 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000915 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
916 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000917 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000918
Douglas Gregore53060f2009-06-25 22:08:12 +0000919 // For function templates, the underlying function declarations are linked.
920 if (const FunctionTemplateDecl *FunctionTemplate
921 = dyn_cast<FunctionTemplateDecl>(this))
922 if (const FunctionTemplateDecl *OldFunctionTemplate
923 = dyn_cast<FunctionTemplateDecl>(OldD))
924 return FunctionTemplate->getTemplatedDecl()
925 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Steve Naroff0de21fd2009-02-22 19:35:57 +0000927 // For method declarations, we keep track of redeclarations.
928 if (isa<ObjCMethodDecl>(this))
929 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
John McCallf36e02d2009-10-09 21:13:30 +0000931 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
932 return true;
933
John McCall9488ea12009-11-17 05:59:44 +0000934 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
935 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
936 cast<UsingShadowDecl>(OldD)->getTargetDecl();
937
Douglas Gregordc355712011-02-25 00:36:19 +0000938 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
939 ASTContext &Context = getASTContext();
940 return Context.getCanonicalNestedNameSpecifier(
941 cast<UsingDecl>(this)->getQualifier()) ==
942 Context.getCanonicalNestedNameSpecifier(
943 cast<UsingDecl>(OldD)->getQualifier());
944 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000945
Douglas Gregor7a537402012-01-03 23:26:26 +0000946 // A typedef of an Objective-C class type can replace an Objective-C class
947 // declaration or definition, and vice versa.
948 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
949 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
950 return true;
951
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000952 // For non-function declarations, if the declarations are of the
953 // same kind then this must be a redeclaration, or semantic analysis
954 // would not have given us the new declaration.
955 return this->getKind() == OldD->getKind();
956}
957
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000958bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000959 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000960}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000961
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000962NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000963 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000964 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
965 ND = UD->getTargetDecl();
966
967 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
968 return AD->getClassInterface();
969
970 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +0000971}
972
John McCall161755a2010-04-06 21:38:20 +0000973bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000974 if (!isCXXClassMember())
975 return false;
976
John McCall161755a2010-04-06 21:38:20 +0000977 const NamedDecl *D = this;
978 if (isa<UsingShadowDecl>(D))
979 D = cast<UsingShadowDecl>(D)->getTargetDecl();
980
Francois Pichet87c2e122010-11-21 06:08:52 +0000981 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000982 return true;
983 if (isa<CXXMethodDecl>(D))
984 return cast<CXXMethodDecl>(D)->isInstance();
985 if (isa<FunctionTemplateDecl>(D))
986 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
987 ->getTemplatedDecl())->isInstance();
988 return false;
989}
990
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000991//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000992// DeclaratorDecl Implementation
993//===----------------------------------------------------------------------===//
994
Douglas Gregor1693e152010-07-06 18:42:40 +0000995template <typename DeclT>
996static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
997 if (decl->getNumTemplateParameterLists() > 0)
998 return decl->getTemplateParameterList(0)->getTemplateLoc();
999 else
1000 return decl->getInnerLocStart();
1001}
1002
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001003SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001004 TypeSourceInfo *TSI = getTypeSourceInfo();
1005 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001006 return SourceLocation();
1007}
1008
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001009void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1010 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001011 // Make sure the extended decl info is allocated.
1012 if (!hasExtInfo()) {
1013 // Save (non-extended) type source info pointer.
1014 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1015 // Allocate external info struct.
1016 DeclInfo = new (getASTContext()) ExtInfo;
1017 // Restore savedTInfo into (extended) decl info.
1018 getExtInfo()->TInfo = savedTInfo;
1019 }
1020 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001021 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001022 } else {
John McCallb6217662010-03-15 10:12:16 +00001023 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001024 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001025 if (getExtInfo()->NumTemplParamLists == 0) {
1026 // Save type source info pointer.
1027 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1028 // Deallocate the extended decl info.
1029 getASTContext().Deallocate(getExtInfo());
1030 // Restore savedTInfo into (non-extended) decl info.
1031 DeclInfo = savedTInfo;
1032 }
1033 else
1034 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001035 }
1036 }
1037}
1038
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001039void
1040DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1041 unsigned NumTPLists,
1042 TemplateParameterList **TPLists) {
1043 assert(NumTPLists > 0);
1044 // Make sure the extended decl info is allocated.
1045 if (!hasExtInfo()) {
1046 // Save (non-extended) type source info pointer.
1047 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1048 // Allocate external info struct.
1049 DeclInfo = new (getASTContext()) ExtInfo;
1050 // Restore savedTInfo into (extended) decl info.
1051 getExtInfo()->TInfo = savedTInfo;
1052 }
1053 // Set the template parameter lists info.
1054 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1055}
1056
Douglas Gregor1693e152010-07-06 18:42:40 +00001057SourceLocation DeclaratorDecl::getOuterLocStart() const {
1058 return getTemplateOrInnerLocStart(this);
1059}
1060
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001061namespace {
1062
1063// Helper function: returns true if QT is or contains a type
1064// having a postfix component.
1065bool typeIsPostfix(clang::QualType QT) {
1066 while (true) {
1067 const Type* T = QT.getTypePtr();
1068 switch (T->getTypeClass()) {
1069 default:
1070 return false;
1071 case Type::Pointer:
1072 QT = cast<PointerType>(T)->getPointeeType();
1073 break;
1074 case Type::BlockPointer:
1075 QT = cast<BlockPointerType>(T)->getPointeeType();
1076 break;
1077 case Type::MemberPointer:
1078 QT = cast<MemberPointerType>(T)->getPointeeType();
1079 break;
1080 case Type::LValueReference:
1081 case Type::RValueReference:
1082 QT = cast<ReferenceType>(T)->getPointeeType();
1083 break;
1084 case Type::PackExpansion:
1085 QT = cast<PackExpansionType>(T)->getPattern();
1086 break;
1087 case Type::Paren:
1088 case Type::ConstantArray:
1089 case Type::DependentSizedArray:
1090 case Type::IncompleteArray:
1091 case Type::VariableArray:
1092 case Type::FunctionProto:
1093 case Type::FunctionNoProto:
1094 return true;
1095 }
1096 }
1097}
1098
1099} // namespace
1100
1101SourceRange DeclaratorDecl::getSourceRange() const {
1102 SourceLocation RangeEnd = getLocation();
1103 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1104 if (typeIsPostfix(TInfo->getType()))
1105 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1106 }
1107 return SourceRange(getOuterLocStart(), RangeEnd);
1108}
1109
Abramo Bagnara9b934882010-06-12 08:15:14 +00001110void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001111QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1112 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001113 TemplateParameterList **TPLists) {
1114 assert((NumTPLists == 0 || TPLists != 0) &&
1115 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001116
1117 // Free previous template parameters (if any).
1118 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001119 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001120 TemplParamLists = 0;
1121 NumTemplParamLists = 0;
1122 }
1123 // Set info on matched template parameter lists (if any).
1124 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001125 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001126 NumTemplParamLists = NumTPLists;
1127 for (unsigned i = NumTPLists; i-- > 0; )
1128 TemplParamLists[i] = TPLists[i];
1129 }
1130}
1131
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001132//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001133// VarDecl Implementation
1134//===----------------------------------------------------------------------===//
1135
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001136const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1137 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001138 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001139 case SC_Auto: return "auto";
1140 case SC_Extern: return "extern";
1141 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1142 case SC_PrivateExtern: return "__private_extern__";
1143 case SC_Register: return "register";
1144 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001145 }
1146
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001147 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001148}
1149
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001150VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1151 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001152 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001153 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001154 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001155}
1156
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001157VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1158 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1159 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1160 QualType(), 0, SC_None, SC_None);
1161}
1162
Douglas Gregor381d34e2010-12-06 18:36:25 +00001163void VarDecl::setStorageClass(StorageClass SC) {
1164 assert(isLegalForVariable(SC));
1165 if (getStorageClass() != SC)
Rafael Espindoladfb31662012-12-25 00:39:58 +00001166 ClearLVCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001167
John McCallf1e4fbf2011-05-01 02:13:58 +00001168 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001169}
1170
Douglas Gregor1693e152010-07-06 18:42:40 +00001171SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisd69f31c2012-10-08 23:08:41 +00001172 if (const Expr *Init = getInit()) {
1173 SourceLocation InitEnd = Init->getLocEnd();
1174 if (InitEnd.isValid())
1175 return SourceRange(getOuterLocStart(), InitEnd);
1176 }
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001177 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001178}
1179
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001180bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001181 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001182 return false;
1183
Eli Friedman750dc2b2012-01-15 01:23:58 +00001184 const DeclContext *DC = getDeclContext();
1185 if (DC->isRecord())
1186 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001187
Eli Friedman750dc2b2012-01-15 01:23:58 +00001188 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001189 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001190 return true;
1191 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001192}
1193
1194VarDecl *VarDecl::getCanonicalDecl() {
1195 return getFirstDeclaration();
1196}
1197
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001198VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1199 ASTContext &C) const
1200{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001201 // C++ [basic.def]p2:
1202 // A declaration is a definition unless [...] it contains the 'extern'
1203 // specifier or a linkage-specification and neither an initializer [...],
1204 // it declares a static data member in a class declaration [...].
1205 // C++ [temp.expl.spec]p15:
1206 // An explicit specialization of a static data member of a template is a
1207 // definition if the declaration includes an initializer; otherwise, it is
1208 // a declaration.
1209 if (isStaticDataMember()) {
1210 if (isOutOfLine() && (hasInit() ||
1211 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1212 return Definition;
1213 else
1214 return DeclarationOnly;
1215 }
1216 // C99 6.7p5:
1217 // A definition of an identifier is a declaration for that identifier that
1218 // [...] causes storage to be reserved for that object.
1219 // Note: that applies for all non-file-scope objects.
1220 // C99 6.9.2p1:
1221 // If the declaration of an identifier for an object has file scope and an
1222 // initializer, the declaration is an external definition for the identifier
1223 if (hasInit())
1224 return Definition;
1225 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1226 if (hasExternalStorage())
1227 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001228
John McCalld931b082010-08-26 03:08:43 +00001229 if (getStorageClassAsWritten() == SC_Extern ||
1230 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001231 for (const VarDecl *PrevVar = getPreviousDecl();
1232 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Rafael Espindola372df452012-12-17 22:23:47 +00001233 if (PrevVar->getLinkage() == InternalLinkage)
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001234 return DeclarationOnly;
1235 }
1236 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001237 // C99 6.9.2p2:
1238 // A declaration of an object that has file scope without an initializer,
1239 // and without a storage class specifier or the scs 'static', constitutes
1240 // a tentative definition.
1241 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001242 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001243 return TentativeDefinition;
1244
1245 // What's left is (in C, block-scope) declarations without initializers or
1246 // external storage. These are definitions.
1247 return Definition;
1248}
1249
Sebastian Redle9d12b62010-01-31 22:27:38 +00001250VarDecl *VarDecl::getActingDefinition() {
1251 DefinitionKind Kind = isThisDeclarationADefinition();
1252 if (Kind != TentativeDefinition)
1253 return 0;
1254
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001255 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001256 VarDecl *First = getFirstDeclaration();
1257 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1258 I != E; ++I) {
1259 Kind = (*I)->isThisDeclarationADefinition();
1260 if (Kind == Definition)
1261 return 0;
1262 else if (Kind == TentativeDefinition)
1263 LastTentative = *I;
1264 }
1265 return LastTentative;
1266}
1267
1268bool VarDecl::isTentativeDefinitionNow() const {
1269 DefinitionKind Kind = isThisDeclarationADefinition();
1270 if (Kind != TentativeDefinition)
1271 return false;
1272
1273 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1274 if ((*I)->isThisDeclarationADefinition() == Definition)
1275 return false;
1276 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001277 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001278}
1279
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001280VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001281 VarDecl *First = getFirstDeclaration();
1282 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1283 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001284 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001285 return *I;
1286 }
1287 return 0;
1288}
1289
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001290VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001291 DefinitionKind Kind = DeclarationOnly;
1292
1293 const VarDecl *First = getFirstDeclaration();
1294 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001295 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001296 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001297 if (Kind == Definition)
1298 break;
1299 }
John McCall110e8e52010-10-29 22:22:43 +00001300
1301 return Kind;
1302}
1303
Sebastian Redl31310a22010-02-01 20:16:42 +00001304const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001305 redecl_iterator I = redecls_begin(), E = redecls_end();
1306 while (I != E && !I->getInit())
1307 ++I;
1308
1309 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001310 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001311 return I->getInit();
1312 }
1313 return 0;
1314}
1315
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001316bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001317 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001318 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001319
1320 if (!isStaticDataMember())
1321 return false;
1322
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001323 // If this static data member was instantiated from a static data member of
1324 // a class template, check whether that static data member was defined
1325 // out-of-line.
1326 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1327 return VD->isOutOfLine();
1328
1329 return false;
1330}
1331
Douglas Gregor0d035142009-10-27 18:42:08 +00001332VarDecl *VarDecl::getOutOfLineDefinition() {
1333 if (!isStaticDataMember())
1334 return 0;
1335
1336 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1337 RD != RDEnd; ++RD) {
1338 if (RD->getLexicalDeclContext()->isFileContext())
1339 return *RD;
1340 }
1341
1342 return 0;
1343}
1344
Douglas Gregor838db382010-02-11 01:19:42 +00001345void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001346 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1347 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001348 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001349 }
1350
1351 Init = I;
1352}
1353
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001354bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001355 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001356
Richard Smith16581332012-03-02 04:14:40 +00001357 if (!Lang.CPlusPlus)
1358 return false;
1359
1360 // In C++11, any variable of reference type can be used in a constant
1361 // expression if it is initialized by a constant expression.
1362 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1363 return true;
1364
1365 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001366 // not require the variable to be non-volatile, but we consider this to be a
1367 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001368 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001369 return false;
1370
1371 // In C++, const, non-volatile variables of integral or enumeration types
1372 // can be used in constant expressions.
1373 if (getType()->isIntegralOrEnumerationType())
1374 return true;
1375
Richard Smith16581332012-03-02 04:14:40 +00001376 // Additionally, in C++11, non-volatile constexpr variables can be used in
1377 // constant expressions.
1378 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001379}
1380
Richard Smith099e7f62011-12-19 06:19:21 +00001381/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1382/// form, which contains extra information on the evaluated value of the
1383/// initializer.
1384EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1385 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1386 if (!Eval) {
1387 Stmt *S = Init.get<Stmt *>();
1388 Eval = new (getASTContext()) EvaluatedStmt;
1389 Eval->Value = S;
1390 Init = Eval;
1391 }
1392 return Eval;
1393}
1394
Richard Smith2d6a5672012-01-14 04:30:29 +00001395APValue *VarDecl::evaluateValue() const {
1396 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1397 return evaluateValue(Notes);
1398}
1399
1400APValue *VarDecl::evaluateValue(
1401 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001402 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1403
1404 // We only produce notes indicating why an initializer is non-constant the
1405 // first time it is evaluated. FIXME: The notes won't always be emitted the
1406 // first time we try evaluation, so might not be produced at all.
1407 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001408 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001409
1410 const Expr *Init = cast<Expr>(Eval->Value);
1411 assert(!Init->isValueDependent());
1412
1413 if (Eval->IsEvaluating) {
1414 // FIXME: Produce a diagnostic for self-initialization.
1415 Eval->CheckedICE = true;
1416 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001417 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001418 }
1419
1420 Eval->IsEvaluating = true;
1421
1422 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1423 this, Notes);
1424
1425 // Ensure the result is an uninitialized APValue if evaluation fails.
1426 if (!Result)
1427 Eval->Evaluated = APValue();
1428
1429 Eval->IsEvaluating = false;
1430 Eval->WasEvaluated = true;
1431
1432 // In C++11, we have determined whether the initializer was a constant
1433 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001434 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001435 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001436 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001437 }
1438
Richard Smith2d6a5672012-01-14 04:30:29 +00001439 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001440}
1441
1442bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001443 // Initializers of weak variables are never ICEs.
1444 if (isWeak())
1445 return false;
1446
Richard Smith099e7f62011-12-19 06:19:21 +00001447 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1448 if (Eval->CheckedICE)
1449 // We have already checked whether this subexpression is an
1450 // integral constant expression.
1451 return Eval->IsICE;
1452
1453 const Expr *Init = cast<Expr>(Eval->Value);
1454 assert(!Init->isValueDependent());
1455
1456 // In C++11, evaluate the initializer to check whether it's a constant
1457 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001458 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001459 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1460 evaluateValue(Notes);
1461 return Eval->IsICE;
1462 }
1463
1464 // It's an ICE whether or not the definition we found is
1465 // out-of-line. See DR 721 and the discussion in Clang PR
1466 // 6206 for details.
1467
1468 if (Eval->CheckingICE)
1469 return false;
1470 Eval->CheckingICE = true;
1471
1472 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1473 Eval->CheckingICE = false;
1474 Eval->CheckedICE = true;
1475 return Eval->IsICE;
1476}
1477
Douglas Gregor03e80032011-06-21 17:03:29 +00001478bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001479 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001480
1481 const Expr *E = getInit();
1482 if (!E)
1483 return false;
1484
1485 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1486 E = Cleanups->getSubExpr();
1487
1488 return isa<MaterializeTemporaryExpr>(E);
1489}
1490
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001491VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001492 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001493 return cast<VarDecl>(MSI->getInstantiatedFrom());
1494
1495 return 0;
1496}
1497
Douglas Gregor663b5a02009-10-14 20:14:33 +00001498TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001499 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001500 return MSI->getTemplateSpecializationKind();
1501
1502 return TSK_Undeclared;
1503}
1504
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001505MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001506 return getASTContext().getInstantiatedFromStaticDataMember(this);
1507}
1508
Douglas Gregor0a897e32009-10-15 17:21:20 +00001509void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1510 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001511 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001512 assert(MSI && "Not an instantiated static data member?");
1513 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001514 if (TSK != TSK_ExplicitSpecialization &&
1515 PointOfInstantiation.isValid() &&
1516 MSI->getPointOfInstantiation().isInvalid())
1517 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001518}
1519
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001520//===----------------------------------------------------------------------===//
1521// ParmVarDecl Implementation
1522//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001523
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001524ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001525 SourceLocation StartLoc,
1526 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001527 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001528 StorageClass S, StorageClass SCAsWritten,
1529 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001530 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001531 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001532}
1533
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001534ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1535 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1536 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1537 0, QualType(), 0, SC_None, SC_None, 0);
1538}
1539
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001540SourceRange ParmVarDecl::getSourceRange() const {
1541 if (!hasInheritedDefaultArg()) {
1542 SourceRange ArgRange = getDefaultArgRange();
1543 if (ArgRange.isValid())
1544 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1545 }
1546
1547 return DeclaratorDecl::getSourceRange();
1548}
1549
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001550Expr *ParmVarDecl::getDefaultArg() {
1551 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1552 assert(!hasUninstantiatedDefaultArg() &&
1553 "Default argument is not yet instantiated!");
1554
1555 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001556 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001557 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001558
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001559 return Arg;
1560}
1561
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001562SourceRange ParmVarDecl::getDefaultArgRange() const {
1563 if (const Expr *E = getInit())
1564 return E->getSourceRange();
1565
1566 if (hasUninstantiatedDefaultArg())
1567 return getUninstantiatedDefaultArg()->getSourceRange();
1568
1569 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001570}
1571
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001572bool ParmVarDecl::isParameterPack() const {
1573 return isa<PackExpansionType>(getType());
1574}
1575
Ted Kremenekd211cb72011-10-06 05:00:56 +00001576void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1577 getASTContext().setParameterIndex(this, parameterIndex);
1578 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1579}
1580
1581unsigned ParmVarDecl::getParameterIndexLarge() const {
1582 return getASTContext().getParameterIndex(this);
1583}
1584
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001585//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001586// FunctionDecl Implementation
1587//===----------------------------------------------------------------------===//
1588
Douglas Gregorda2142f2011-02-19 18:51:44 +00001589void FunctionDecl::getNameForDiagnostic(std::string &S,
1590 const PrintingPolicy &Policy,
1591 bool Qualified) const {
1592 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1593 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1594 if (TemplateArgs)
1595 S += TemplateSpecializationType::PrintTemplateArgumentList(
1596 TemplateArgs->data(),
1597 TemplateArgs->size(),
1598 Policy);
1599
1600}
1601
Ted Kremenek9498d382010-04-29 16:49:01 +00001602bool FunctionDecl::isVariadic() const {
1603 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1604 return FT->isVariadic();
1605 return false;
1606}
1607
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001608bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1609 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001610 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001611 Definition = *I;
1612 return true;
1613 }
1614 }
1615
1616 return false;
1617}
1618
Anders Carlssonffb945f2011-05-14 23:26:09 +00001619bool FunctionDecl::hasTrivialBody() const
1620{
1621 Stmt *S = getBody();
1622 if (!S) {
1623 // Since we don't have a body for this function, we don't know if it's
1624 // trivial or not.
1625 return false;
1626 }
1627
1628 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1629 return true;
1630 return false;
1631}
1632
Sean Hunt10620eb2011-05-06 20:44:56 +00001633bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1634 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001635 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001636 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1637 return true;
1638 }
1639 }
1640
1641 return false;
1642}
1643
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001644Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001645 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1646 if (I->Body) {
1647 Definition = *I;
1648 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001649 } else if (I->IsLateTemplateParsed) {
1650 Definition = *I;
1651 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001652 }
1653 }
1654
1655 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001656}
1657
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001658void FunctionDecl::setBody(Stmt *B) {
1659 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001660 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001661 EndRangeLoc = B->getLocEnd();
Rafael Espindoladfb31662012-12-25 00:39:58 +00001662 ClearLVCache();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001663}
1664
Douglas Gregor21386642010-09-28 21:55:22 +00001665void FunctionDecl::setPure(bool P) {
1666 IsPure = P;
1667 if (P)
1668 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1669 Parent->markedVirtualFunctionPure();
1670}
1671
Douglas Gregor48a83b52009-09-12 00:17:51 +00001672bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001673 const TranslationUnitDecl *tunit =
1674 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1675 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001676 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001677 getIdentifier() &&
1678 getIdentifier()->isStr("main");
1679}
1680
1681bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1682 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1683 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1684 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1685 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1686 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1687
1688 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1689 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1690
1691 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1692 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1693
1694 ASTContext &Context =
1695 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1696 ->getASTContext();
1697
1698 // The result type and first argument type are constant across all
1699 // these operators. The second argument must be exactly void*.
1700 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001701}
1702
Douglas Gregor48a83b52009-09-12 00:17:51 +00001703bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001704 if (getLinkage() != ExternalLinkage)
1705 return false;
1706
1707 if (getAttr<OverloadableAttr>())
1708 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001709
Chandler Carruth10aad442011-02-25 00:05:02 +00001710 const DeclContext *DC = getDeclContext();
1711 if (DC->isRecord())
1712 return false;
1713
Eli Friedman750dc2b2012-01-15 01:23:58 +00001714 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001715 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001716 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001717
Eli Friedman750dc2b2012-01-15 01:23:58 +00001718 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001719}
1720
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001721bool FunctionDecl::isGlobal() const {
1722 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1723 return Method->isStatic();
1724
John McCalld931b082010-08-26 03:08:43 +00001725 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001726 return false;
1727
Mike Stump1eb44332009-09-09 15:08:12 +00001728 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001729 DC->isNamespace();
1730 DC = DC->getParent()) {
1731 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1732 if (!Namespace->getDeclName())
1733 return false;
1734 break;
1735 }
1736 }
1737
1738 return true;
1739}
1740
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001741void
1742FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1743 redeclarable_base::setPreviousDeclaration(PrevDecl);
1744
1745 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1746 FunctionTemplateDecl *PrevFunTmpl
1747 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1748 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1749 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1750 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001751
Axel Naumannd9d137e2011-11-08 18:21:06 +00001752 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001753 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001754}
1755
1756const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1757 return getFirstDeclaration();
1758}
1759
1760FunctionDecl *FunctionDecl::getCanonicalDecl() {
1761 return getFirstDeclaration();
1762}
1763
Douglas Gregor381d34e2010-12-06 18:36:25 +00001764void FunctionDecl::setStorageClass(StorageClass SC) {
1765 assert(isLegalForFunction(SC));
1766 if (getStorageClass() != SC)
Rafael Espindoladfb31662012-12-25 00:39:58 +00001767 ClearLVCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001768
1769 SClass = SC;
1770}
1771
Douglas Gregor3e41d602009-02-13 23:20:09 +00001772/// \brief Returns a value indicating whether this function
1773/// corresponds to a builtin function.
1774///
1775/// The function corresponds to a built-in function if it is
1776/// declared at translation scope or within an extern "C" block and
1777/// its name matches with the name of a builtin. The returned value
1778/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001779/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001780/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001781unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001782 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001783 return 0;
1784
1785 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001786 if (!BuiltinID)
1787 return 0;
1788
1789 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001790 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1791 return BuiltinID;
1792
1793 // This function has the name of a known C library
1794 // function. Determine whether it actually refers to the C library
1795 // function or whether it just has the same name.
1796
Douglas Gregor9add3172009-02-17 03:23:10 +00001797 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001798 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001799 return 0;
1800
Douglas Gregor3c385e52009-02-14 18:57:46 +00001801 // If this function is at translation-unit scope and we're not in
1802 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001803 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001804 getDeclContext()->isTranslationUnit())
1805 return BuiltinID;
1806
1807 // If the function is in an extern "C" linkage specification and is
1808 // not marked "overloadable", it's the real function.
1809 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001810 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001811 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001812 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001813 return BuiltinID;
1814
1815 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001816 return 0;
1817}
1818
1819
Chris Lattner1ad9b282009-04-25 06:03:53 +00001820/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001821/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001822/// after it has been created.
1823unsigned FunctionDecl::getNumParams() const {
Eli Friedman482466b2012-08-30 22:22:09 +00001824 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001825 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001826 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001827 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Reid Spencer5f016e22007-07-11 17:01:13 +00001829}
1830
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001831void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001832 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001834 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001837 if (!NewParamInfo.empty()) {
1838 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1839 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 }
1841}
1842
James Molloy16f1f712012-02-29 10:24:19 +00001843void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1844 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1845
1846 if (!NewDecls.empty()) {
1847 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1848 std::copy(NewDecls.begin(), NewDecls.end(), A);
1849 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1850 }
1851}
1852
Chris Lattner8123a952008-04-10 02:22:51 +00001853/// getMinRequiredArguments - Returns the minimum number of arguments
1854/// needed to call this function. This may be fewer than the number of
1855/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001856/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001857unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001858 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001859 return getNumParams();
1860
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001861 unsigned NumRequiredArgs = getNumParams();
1862
1863 // If the last parameter is a parameter pack, we don't need an argument for
1864 // it.
1865 if (NumRequiredArgs > 0 &&
1866 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1867 --NumRequiredArgs;
1868
1869 // If this parameter has a default argument, we don't need an argument for
1870 // it.
1871 while (NumRequiredArgs > 0 &&
1872 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001873 --NumRequiredArgs;
1874
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001875 // We might have parameter packs before the end. These can't be deduced,
1876 // but they can still handle multiple arguments.
1877 unsigned ArgIdx = NumRequiredArgs;
1878 while (ArgIdx > 0) {
1879 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1880 NumRequiredArgs = ArgIdx;
1881
1882 --ArgIdx;
1883 }
1884
Chris Lattner8123a952008-04-10 02:22:51 +00001885 return NumRequiredArgs;
1886}
1887
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001888bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001889 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001890 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001891
1892 if (isa<CXXMethodDecl>(this)) {
1893 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1894 return true;
1895 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001896
1897 switch (getTemplateSpecializationKind()) {
1898 case TSK_Undeclared:
1899 case TSK_ExplicitSpecialization:
1900 return false;
1901
1902 case TSK_ImplicitInstantiation:
1903 case TSK_ExplicitInstantiationDeclaration:
1904 case TSK_ExplicitInstantiationDefinition:
1905 // Handle below.
1906 break;
1907 }
1908
1909 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001910 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001911 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001912 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001913
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001914 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001915 return PatternDecl->isInlined();
1916
1917 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001918}
1919
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001920static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1921 // Only consider file-scope declarations in this test.
1922 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1923 return false;
1924
1925 // Only consider explicit declarations; the presence of a builtin for a
1926 // libcall shouldn't affect whether a definition is externally visible.
1927 if (Redecl->isImplicit())
1928 return false;
1929
1930 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1931 return true; // Not an inline definition
1932
1933 return false;
1934}
1935
Nick Lewyckydce67a72011-07-18 05:26:13 +00001936/// \brief For a function declaration in C or C++, determine whether this
1937/// declaration causes the definition to be externally visible.
1938///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001939/// Specifically, this determines if adding the current declaration to the set
1940/// of redeclarations of the given functions causes
1941/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001942bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1943 assert(!doesThisDeclarationHaveABody() &&
1944 "Must have a declaration without a body.");
1945
1946 ASTContext &Context = getASTContext();
1947
David Blaikie4e4d0842012-03-11 07:00:24 +00001948 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001949 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1950 // an externally visible definition.
1951 //
1952 // FIXME: What happens if gnu_inline gets added on after the first
1953 // declaration?
1954 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1955 return false;
1956
1957 const FunctionDecl *Prev = this;
1958 bool FoundBody = false;
1959 while ((Prev = Prev->getPreviousDecl())) {
1960 FoundBody |= Prev->Body;
1961
1962 if (Prev->Body) {
1963 // If it's not the case that both 'inline' and 'extern' are
1964 // specified on the definition, then it is always externally visible.
1965 if (!Prev->isInlineSpecified() ||
1966 Prev->getStorageClassAsWritten() != SC_Extern)
1967 return false;
1968 } else if (Prev->isInlineSpecified() &&
1969 Prev->getStorageClassAsWritten() != SC_Extern) {
1970 return false;
1971 }
1972 }
1973 return FoundBody;
1974 }
1975
David Blaikie4e4d0842012-03-11 07:00:24 +00001976 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001977 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001978
1979 // C99 6.7.4p6:
1980 // [...] If all of the file scope declarations for a function in a
1981 // translation unit include the inline function specifier without extern,
1982 // then the definition in that translation unit is an inline definition.
1983 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001984 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001985 const FunctionDecl *Prev = this;
1986 bool FoundBody = false;
1987 while ((Prev = Prev->getPreviousDecl())) {
1988 FoundBody |= Prev->Body;
1989 if (RedeclForcesDefC99(Prev))
1990 return false;
1991 }
1992 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00001993}
1994
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001995/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001996/// definition will be externally visible.
1997///
1998/// Inline function definitions are always available for inlining optimizations.
1999/// However, depending on the language dialect, declaration specifiers, and
2000/// attributes, the definition of an inline function may or may not be
2001/// "externally" visible to other translation units in the program.
2002///
2003/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002004/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002005/// inline definition becomes externally visible (C99 6.7.4p6).
2006///
2007/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2008/// definition, we use the GNU semantics for inline, which are nearly the
2009/// opposite of C99 semantics. In particular, "inline" by itself will create
2010/// an externally visible symbol, but "extern inline" will not create an
2011/// externally visible symbol.
2012bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002013 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002014 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002015 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002016
David Blaikie4e4d0842012-03-11 07:00:24 +00002017 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002018 // Note: If you change the logic here, please change
2019 // doesDeclarationForceExternallyVisibleDefinition as well.
2020 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002021 // If it's not the case that both 'inline' and 'extern' are
2022 // specified on the definition, then this inline definition is
2023 // externally visible.
2024 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2025 return true;
2026
2027 // If any declaration is 'inline' but not 'extern', then this definition
2028 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002029 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2030 Redecl != RedeclEnd;
2031 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002032 if (Redecl->isInlineSpecified() &&
2033 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002034 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002035 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002036
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002037 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002038 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002039
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002040 // C99 6.7.4p6:
2041 // [...] If all of the file scope declarations for a function in a
2042 // translation unit include the inline function specifier without extern,
2043 // then the definition in that translation unit is an inline definition.
2044 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2045 Redecl != RedeclEnd;
2046 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002047 if (RedeclForcesDefC99(*Redecl))
2048 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002049 }
2050
2051 // C99 6.7.4p6:
2052 // An inline definition does not provide an external definition for the
2053 // function, and does not forbid an external definition in another
2054 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002055 return false;
2056}
2057
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002058/// getOverloadedOperator - Which C++ overloaded operator this
2059/// function represents, if any.
2060OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002061 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2062 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002063 else
2064 return OO_None;
2065}
2066
Sean Hunta6c058d2010-01-13 09:01:02 +00002067/// getLiteralIdentifier - The literal suffix identifier this function
2068/// represents, if any.
2069const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2070 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2071 return getDeclName().getCXXLiteralIdentifier();
2072 else
2073 return 0;
2074}
2075
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002076FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2077 if (TemplateOrSpecialization.isNull())
2078 return TK_NonTemplate;
2079 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2080 return TK_FunctionTemplate;
2081 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2082 return TK_MemberSpecialization;
2083 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2084 return TK_FunctionTemplateSpecialization;
2085 if (TemplateOrSpecialization.is
2086 <DependentFunctionTemplateSpecializationInfo*>())
2087 return TK_DependentFunctionTemplateSpecialization;
2088
David Blaikieb219cfc2011-09-23 05:06:16 +00002089 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002090}
2091
Douglas Gregor2db32322009-10-07 23:56:10 +00002092FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002093 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002094 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2095
2096 return 0;
2097}
2098
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002099MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2100 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2101}
2102
Douglas Gregor2db32322009-10-07 23:56:10 +00002103void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002104FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2105 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002106 TemplateSpecializationKind TSK) {
2107 assert(TemplateOrSpecialization.isNull() &&
2108 "Member function is already a specialization");
2109 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002110 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002111 TemplateOrSpecialization = Info;
2112}
2113
Douglas Gregor3b846b62009-10-27 20:53:28 +00002114bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002115 // If the function is invalid, it can't be implicitly instantiated.
2116 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002117 return false;
2118
2119 switch (getTemplateSpecializationKind()) {
2120 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002121 case TSK_ExplicitInstantiationDefinition:
2122 return false;
2123
2124 case TSK_ImplicitInstantiation:
2125 return true;
2126
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002127 // It is possible to instantiate TSK_ExplicitSpecialization kind
2128 // if the FunctionDecl has a class scope specialization pattern.
2129 case TSK_ExplicitSpecialization:
2130 return getClassScopeSpecializationPattern() != 0;
2131
Douglas Gregor3b846b62009-10-27 20:53:28 +00002132 case TSK_ExplicitInstantiationDeclaration:
2133 // Handled below.
2134 break;
2135 }
2136
2137 // Find the actual template from which we will instantiate.
2138 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002139 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002140 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002141 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002142
2143 // C++0x [temp.explicit]p9:
2144 // Except for inline functions, other explicit instantiation declarations
2145 // have the effect of suppressing the implicit instantiation of the entity
2146 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002147 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002148 return true;
2149
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002150 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002151}
2152
2153bool FunctionDecl::isTemplateInstantiation() const {
2154 switch (getTemplateSpecializationKind()) {
2155 case TSK_Undeclared:
2156 case TSK_ExplicitSpecialization:
2157 return false;
2158 case TSK_ImplicitInstantiation:
2159 case TSK_ExplicitInstantiationDeclaration:
2160 case TSK_ExplicitInstantiationDefinition:
2161 return true;
2162 }
2163 llvm_unreachable("All TSK values handled.");
2164}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002165
2166FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002167 // Handle class scope explicit specialization special case.
2168 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2169 return getClassScopeSpecializationPattern();
2170
Douglas Gregor3b846b62009-10-27 20:53:28 +00002171 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2172 while (Primary->getInstantiatedFromMemberTemplate()) {
2173 // If we have hit a point where the user provided a specialization of
2174 // this template, we're done looking.
2175 if (Primary->isMemberSpecialization())
2176 break;
2177
2178 Primary = Primary->getInstantiatedFromMemberTemplate();
2179 }
2180
2181 return Primary->getTemplatedDecl();
2182 }
2183
2184 return getInstantiatedFromMemberFunction();
2185}
2186
Douglas Gregor16e8be22009-06-29 17:30:29 +00002187FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002188 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002189 = TemplateOrSpecialization
2190 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002191 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002192 }
2193 return 0;
2194}
2195
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002196FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2197 return getASTContext().getClassScopeSpecializationPattern(this);
2198}
2199
Douglas Gregor16e8be22009-06-29 17:30:29 +00002200const TemplateArgumentList *
2201FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002202 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002203 = TemplateOrSpecialization
2204 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002205 return Info->TemplateArguments;
2206 }
2207 return 0;
2208}
2209
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002210const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002211FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2212 if (FunctionTemplateSpecializationInfo *Info
2213 = TemplateOrSpecialization
2214 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2215 return Info->TemplateArgumentsAsWritten;
2216 }
2217 return 0;
2218}
2219
Mike Stump1eb44332009-09-09 15:08:12 +00002220void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002221FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2222 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002223 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002224 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002225 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002226 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2227 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002228 assert(TSK != TSK_Undeclared &&
2229 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002230 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002231 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002232 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002233 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2234 TemplateArgs,
2235 TemplateArgsAsWritten,
2236 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002237 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002238 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002239}
2240
John McCallaf2094e2010-04-08 09:05:18 +00002241void
2242FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2243 const UnresolvedSetImpl &Templates,
2244 const TemplateArgumentListInfo &TemplateArgs) {
2245 assert(TemplateOrSpecialization.isNull());
2246 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2247 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002248 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002249 void *Buffer = Context.Allocate(Size);
2250 DependentFunctionTemplateSpecializationInfo *Info =
2251 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2252 TemplateArgs);
2253 TemplateOrSpecialization = Info;
2254}
2255
2256DependentFunctionTemplateSpecializationInfo::
2257DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2258 const TemplateArgumentListInfo &TArgs)
2259 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2260
2261 d.NumTemplates = Ts.size();
2262 d.NumArgs = TArgs.size();
2263
2264 FunctionTemplateDecl **TsArray =
2265 const_cast<FunctionTemplateDecl**>(getTemplates());
2266 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2267 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2268
2269 TemplateArgumentLoc *ArgsArray =
2270 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2271 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2272 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2273}
2274
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002275TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002276 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002277 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002278 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002279 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002280 if (FTSInfo)
2281 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Douglas Gregor2db32322009-10-07 23:56:10 +00002283 MemberSpecializationInfo *MSInfo
2284 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2285 if (MSInfo)
2286 return MSInfo->getTemplateSpecializationKind();
2287
2288 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002289}
2290
Mike Stump1eb44332009-09-09 15:08:12 +00002291void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002292FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2293 SourceLocation PointOfInstantiation) {
2294 if (FunctionTemplateSpecializationInfo *FTSInfo
2295 = TemplateOrSpecialization.dyn_cast<
2296 FunctionTemplateSpecializationInfo*>()) {
2297 FTSInfo->setTemplateSpecializationKind(TSK);
2298 if (TSK != TSK_ExplicitSpecialization &&
2299 PointOfInstantiation.isValid() &&
2300 FTSInfo->getPointOfInstantiation().isInvalid())
2301 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2302 } else if (MemberSpecializationInfo *MSInfo
2303 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2304 MSInfo->setTemplateSpecializationKind(TSK);
2305 if (TSK != TSK_ExplicitSpecialization &&
2306 PointOfInstantiation.isValid() &&
2307 MSInfo->getPointOfInstantiation().isInvalid())
2308 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2309 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002310 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002311}
2312
2313SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002314 if (FunctionTemplateSpecializationInfo *FTSInfo
2315 = TemplateOrSpecialization.dyn_cast<
2316 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002317 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002318 else if (MemberSpecializationInfo *MSInfo
2319 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002320 return MSInfo->getPointOfInstantiation();
2321
2322 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002323}
2324
Douglas Gregor9f185072009-09-11 20:15:17 +00002325bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002326 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002327 return true;
2328
2329 // If this function was instantiated from a member function of a
2330 // class template, check whether that member function was defined out-of-line.
2331 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2332 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002333 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002334 return Definition->isOutOfLine();
2335 }
2336
2337 // If this function was instantiated from a function template,
2338 // check whether that function template was defined out-of-line.
2339 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2340 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002341 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002342 return Definition->isOutOfLine();
2343 }
2344
2345 return false;
2346}
2347
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002348SourceRange FunctionDecl::getSourceRange() const {
2349 return SourceRange(getOuterLocStart(), EndRangeLoc);
2350}
2351
Anna Zaks9392d4e2012-01-18 02:45:01 +00002352unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002353 IdentifierInfo *FnInfo = getIdentifier();
2354
2355 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002356 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002357
2358 // Builtin handling.
2359 switch (getBuiltinID()) {
2360 case Builtin::BI__builtin_memset:
2361 case Builtin::BI__builtin___memset_chk:
2362 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002363 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002364
2365 case Builtin::BI__builtin_memcpy:
2366 case Builtin::BI__builtin___memcpy_chk:
2367 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002368 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002369
2370 case Builtin::BI__builtin_memmove:
2371 case Builtin::BI__builtin___memmove_chk:
2372 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002373 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002374
2375 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002376 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002377 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002378 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002379
2380 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002381 case Builtin::BImemcmp:
2382 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002383
2384 case Builtin::BI__builtin_strncpy:
2385 case Builtin::BI__builtin___strncpy_chk:
2386 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002387 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002388
2389 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002390 case Builtin::BIstrncmp:
2391 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002392
2393 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002394 case Builtin::BIstrncasecmp:
2395 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002396
2397 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002398 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002399 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002400 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002401
2402 case Builtin::BI__builtin_strndup:
2403 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002404 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002405
Anna Zaksc36bedc2012-02-01 19:08:57 +00002406 case Builtin::BI__builtin_strlen:
2407 case Builtin::BIstrlen:
2408 return Builtin::BIstrlen;
2409
Anna Zaksd9b859a2012-01-13 21:52:01 +00002410 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002411 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002412 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002413 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002414 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002415 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002416 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002417 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002418 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002419 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002420 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002421 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002422 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002423 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002424 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002425 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002426 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002427 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002428 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002429 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002430 else if (FnInfo->isStr("strlen"))
2431 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002432 }
2433 break;
2434 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002435 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002436}
2437
Chris Lattner8a934232008-03-31 00:36:02 +00002438//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002439// FieldDecl Implementation
2440//===----------------------------------------------------------------------===//
2441
Jay Foad4ba2a172011-01-12 09:06:06 +00002442FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002443 SourceLocation StartLoc, SourceLocation IdLoc,
2444 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002445 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smithca523302012-06-10 03:12:00 +00002446 InClassInitStyle InitStyle) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002447 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithca523302012-06-10 03:12:00 +00002448 BW, Mutable, InitStyle);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002449}
2450
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002451FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2452 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2453 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smithca523302012-06-10 03:12:00 +00002454 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002455}
2456
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002457bool FieldDecl::isAnonymousStructOrUnion() const {
2458 if (!isImplicit() || getDeclName())
2459 return false;
2460
2461 if (const RecordType *Record = getType()->getAs<RecordType>())
2462 return Record->getDecl()->isAnonymousStructOrUnion();
2463
2464 return false;
2465}
2466
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002467unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2468 assert(isBitField() && "not a bitfield");
2469 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2470 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2471}
2472
John McCallba4f5d52011-01-20 07:57:12 +00002473unsigned FieldDecl::getFieldIndex() const {
2474 if (CachedFieldIndex) return CachedFieldIndex - 1;
2475
Richard Smith180f4792011-11-10 06:34:14 +00002476 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002477 const RecordDecl *RD = getParent();
2478 const FieldDecl *LastFD = 0;
Eli Friedman5f608ae2012-10-12 23:29:20 +00002479 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smith180f4792011-11-10 06:34:14 +00002480
2481 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2482 I != E; ++I, ++Index) {
David Blaikie262bc182012-04-30 02:36:29 +00002483 I->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002484
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002485 if (IsMsStruct) {
2486 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie581deb32012-06-06 20:45:41 +00002487 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smith180f4792011-11-10 06:34:14 +00002488 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002489 continue;
2490 }
David Blaikie581deb32012-06-06 20:45:41 +00002491 LastFD = *I;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002492 }
John McCallba4f5d52011-01-20 07:57:12 +00002493 }
2494
Richard Smith180f4792011-11-10 06:34:14 +00002495 assert(CachedFieldIndex && "failed to find field in parent");
2496 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002497}
2498
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002499SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002500 if (const Expr *E = InitializerOrBitWidth.getPointer())
2501 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002502 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002503}
2504
Abramo Bagnaraa5335762012-07-02 20:35:48 +00002505void FieldDecl::setBitWidth(Expr *Width) {
2506 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2507 "bit width or initializer already set");
2508 InitializerOrBitWidth.setPointer(Width);
2509}
2510
Richard Smith7a614d82011-06-11 17:19:42 +00002511void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smithca523302012-06-10 03:12:00 +00002512 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith7a614d82011-06-11 17:19:42 +00002513 "bit width or initializer already set");
2514 InitializerOrBitWidth.setPointer(Init);
Richard Smith7a614d82011-06-11 17:19:42 +00002515}
2516
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002517//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002518// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002519//===----------------------------------------------------------------------===//
2520
Douglas Gregor1693e152010-07-06 18:42:40 +00002521SourceLocation TagDecl::getOuterLocStart() const {
2522 return getTemplateOrInnerLocStart(this);
2523}
2524
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002525SourceRange TagDecl::getSourceRange() const {
2526 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002527 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002528}
2529
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002530TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002531 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002532}
2533
Richard Smith162e1c12011-04-15 14:24:37 +00002534void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2535 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002536 if (TypeForDecl)
Rafael Espindoladfb31662012-12-25 00:39:58 +00002537 const_cast<Type*>(TypeForDecl)->ClearLVCache();
2538 ClearLVCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002539}
2540
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002541void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002542 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002543
David Blaikie66cff722012-11-14 01:52:05 +00002544 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall86ff3082010-02-04 22:26:26 +00002545 struct CXXRecordDecl::DefinitionData *Data =
2546 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002547 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2548 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002549 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002550}
2551
2552void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002553 assert((!isa<CXXRecordDecl>(this) ||
2554 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2555 "definition completed but not started");
2556
John McCall5e1cdac2011-10-07 06:10:15 +00002557 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002558 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002559
2560 if (ASTMutationListener *L = getASTMutationListener())
2561 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002562}
2563
John McCall5e1cdac2011-10-07 06:10:15 +00002564TagDecl *TagDecl::getDefinition() const {
2565 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002566 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002567 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2568 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002569
2570 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002571 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002572 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002573 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002574
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002575 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002576}
2577
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002578void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2579 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002580 // Make sure the extended qualifier info is allocated.
2581 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002582 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002583 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002584 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002585 } else {
John McCallb6217662010-03-15 10:12:16 +00002586 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002587 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002588 if (getExtInfo()->NumTemplParamLists == 0) {
2589 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002590 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002591 }
2592 else
2593 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002594 }
2595 }
2596}
2597
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002598void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2599 unsigned NumTPLists,
2600 TemplateParameterList **TPLists) {
2601 assert(NumTPLists > 0);
2602 // Make sure the extended decl info is allocated.
2603 if (!hasExtInfo())
2604 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002605 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002606 // Set the template parameter lists info.
2607 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2608}
2609
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002610//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002611// EnumDecl Implementation
2612//===----------------------------------------------------------------------===//
2613
David Blaikie99ba9e32011-12-20 02:48:34 +00002614void EnumDecl::anchor() { }
2615
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002616EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2617 SourceLocation StartLoc, SourceLocation IdLoc,
2618 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002619 EnumDecl *PrevDecl, bool IsScoped,
2620 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002621 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002622 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002623 C.getTypeDeclType(Enum, PrevDecl);
2624 return Enum;
2625}
2626
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002627EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2628 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2629 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2630 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002631}
2632
Douglas Gregor838db382010-02-11 01:19:42 +00002633void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002634 QualType NewPromotionType,
2635 unsigned NumPositiveBits,
2636 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002637 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002638 if (!IntegerType)
2639 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002640 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002641 setNumPositiveBits(NumPositiveBits);
2642 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002643 TagDecl::completeDefinition();
2644}
2645
Richard Smith1af83c42012-03-23 03:33:32 +00002646TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2647 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2648 return MSI->getTemplateSpecializationKind();
2649
2650 return TSK_Undeclared;
2651}
2652
2653void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2654 SourceLocation PointOfInstantiation) {
2655 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2656 assert(MSI && "Not an instantiated member enumeration?");
2657 MSI->setTemplateSpecializationKind(TSK);
2658 if (TSK != TSK_ExplicitSpecialization &&
2659 PointOfInstantiation.isValid() &&
2660 MSI->getPointOfInstantiation().isInvalid())
2661 MSI->setPointOfInstantiation(PointOfInstantiation);
2662}
2663
Richard Smithf1c66b42012-03-14 23:13:10 +00002664EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2665 if (SpecializationInfo)
2666 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2667
2668 return 0;
2669}
2670
2671void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2672 TemplateSpecializationKind TSK) {
2673 assert(!SpecializationInfo && "Member enum is already a specialization");
2674 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2675}
2676
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002677//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002678// RecordDecl Implementation
2679//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002680
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002681RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2682 SourceLocation StartLoc, SourceLocation IdLoc,
2683 IdentifierInfo *Id, RecordDecl *PrevDecl)
2684 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002685 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002686 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002687 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002688 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002689 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002690}
2691
Jay Foad4ba2a172011-01-12 09:06:06 +00002692RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002693 SourceLocation StartLoc, SourceLocation IdLoc,
2694 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2695 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2696 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002697 C.getTypeDeclType(R, PrevDecl);
2698 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002699}
2700
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002701RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2702 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2703 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2704 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002705}
2706
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002707bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002708 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002709 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2710}
2711
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002712RecordDecl::field_iterator RecordDecl::field_begin() const {
2713 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2714 LoadFieldsFromExternalStorage();
2715
2716 return field_iterator(decl_iterator(FirstDecl));
2717}
2718
Douglas Gregorda2142f2011-02-19 18:51:44 +00002719/// completeDefinition - Notes that the definition of this type is now
2720/// complete.
2721void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002722 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002723 TagDecl::completeDefinition();
2724}
2725
Eli Friedman5f608ae2012-10-12 23:29:20 +00002726/// isMsStruct - Get whether or not this record uses ms_struct layout.
2727/// This which can be turned on with an attribute, pragma, or the
2728/// -mms-bitfields command-line option.
2729bool RecordDecl::isMsStruct(const ASTContext &C) const {
2730 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2731}
2732
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002733static bool isFieldOrIndirectField(Decl::Kind K) {
2734 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2735}
2736
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002737void RecordDecl::LoadFieldsFromExternalStorage() const {
2738 ExternalASTSource *Source = getASTContext().getExternalSource();
2739 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2740
2741 // Notify that we have a RecordDecl doing some initialization.
2742 ExternalASTSource::Deserializing TheFields(Source);
2743
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002745 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002746 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2747 Decls)) {
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002748 case ELR_Success:
2749 break;
2750
2751 case ELR_AlreadyLoaded:
2752 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002753 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002754 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002755
2756#ifndef NDEBUG
2757 // Check that all decls we got were FieldDecls.
2758 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002759 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002760#endif
2761
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002762 if (Decls.empty())
2763 return;
2764
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002765 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2766 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002767}
2768
Steve Naroff56ee6892008-10-08 17:01:13 +00002769//===----------------------------------------------------------------------===//
2770// BlockDecl Implementation
2771//===----------------------------------------------------------------------===//
2772
David Blaikie4278c652011-09-21 18:16:56 +00002773void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002774 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002775
Steve Naroffe78b8092009-03-13 16:56:44 +00002776 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002777 if (!NewParamInfo.empty()) {
2778 NumParams = NewParamInfo.size();
2779 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2780 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002781 }
2782}
2783
John McCall6b5a61b2011-02-07 10:33:21 +00002784void BlockDecl::setCaptures(ASTContext &Context,
2785 const Capture *begin,
2786 const Capture *end,
2787 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002788 CapturesCXXThis = capturesCXXThis;
2789
2790 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002791 NumCaptures = 0;
2792 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002793 return;
2794 }
2795
John McCall6b5a61b2011-02-07 10:33:21 +00002796 NumCaptures = end - begin;
2797
2798 // Avoid new Capture[] because we don't want to provide a default
2799 // constructor.
2800 size_t allocationSize = NumCaptures * sizeof(Capture);
2801 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2802 memcpy(buffer, begin, allocationSize);
2803 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002804}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002805
John McCall204e1332011-06-15 22:51:16 +00002806bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2807 for (capture_const_iterator
2808 i = capture_begin(), e = capture_end(); i != e; ++i)
2809 // Only auto vars can be captured, so no redeclaration worries.
2810 if (i->getVariable() == variable)
2811 return true;
2812
2813 return false;
2814}
2815
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002816SourceRange BlockDecl::getSourceRange() const {
2817 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2818}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002819
2820//===----------------------------------------------------------------------===//
2821// Other Decl Allocation/Deallocation Method Implementations
2822//===----------------------------------------------------------------------===//
2823
David Blaikie99ba9e32011-12-20 02:48:34 +00002824void TranslationUnitDecl::anchor() { }
2825
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002826TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2827 return new (C) TranslationUnitDecl(C);
2828}
2829
David Blaikie99ba9e32011-12-20 02:48:34 +00002830void LabelDecl::anchor() { }
2831
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002832LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002833 SourceLocation IdentL, IdentifierInfo *II) {
2834 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2835}
2836
2837LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2838 SourceLocation IdentL, IdentifierInfo *II,
2839 SourceLocation GnuLabelL) {
2840 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2841 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002842}
2843
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002844LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2845 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2846 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002847}
2848
David Blaikie99ba9e32011-12-20 02:48:34 +00002849void ValueDecl::anchor() { }
2850
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +00002851bool ValueDecl::isWeak() const {
2852 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
2853 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
2854 return true;
2855
2856 return isWeakImported();
2857}
2858
David Blaikie99ba9e32011-12-20 02:48:34 +00002859void ImplicitParamDecl::anchor() { }
2860
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002861ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002862 SourceLocation IdLoc,
2863 IdentifierInfo *Id,
2864 QualType Type) {
2865 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002866}
2867
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002868ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2869 unsigned ID) {
2870 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2871 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2872}
2873
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002874FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002875 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002876 const DeclarationNameInfo &NameInfo,
2877 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002878 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002879 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002880 bool hasWrittenPrototype,
2881 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002882 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2883 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002884 isInlineSpecified,
2885 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002886 New->HasWrittenPrototype = hasWrittenPrototype;
2887 return New;
2888}
2889
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002890FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2891 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2892 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2893 DeclarationNameInfo(), QualType(), 0,
2894 SC_None, SC_None, false, false);
2895}
2896
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002897BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2898 return new (C) BlockDecl(DC, L);
2899}
2900
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002901BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2902 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2903 return new (Mem) BlockDecl(0, SourceLocation());
2904}
2905
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002906EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2907 SourceLocation L,
2908 IdentifierInfo *Id, QualType T,
2909 Expr *E, const llvm::APSInt &V) {
2910 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2911}
2912
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002913EnumConstantDecl *
2914EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2915 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2916 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2917 llvm::APSInt());
2918}
2919
David Blaikie99ba9e32011-12-20 02:48:34 +00002920void IndirectFieldDecl::anchor() { }
2921
Benjamin Kramerd9811462010-11-21 14:11:41 +00002922IndirectFieldDecl *
2923IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2924 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2925 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002926 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2927}
2928
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002929IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2930 unsigned ID) {
2931 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2932 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2933 QualType(), 0, 0);
2934}
2935
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002936SourceRange EnumConstantDecl::getSourceRange() const {
2937 SourceLocation End = getLocation();
2938 if (Init)
2939 End = Init->getLocEnd();
2940 return SourceRange(getLocation(), End);
2941}
2942
David Blaikie99ba9e32011-12-20 02:48:34 +00002943void TypeDecl::anchor() { }
2944
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002945TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002946 SourceLocation StartLoc, SourceLocation IdLoc,
2947 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2948 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002949}
2950
David Blaikie99ba9e32011-12-20 02:48:34 +00002951void TypedefNameDecl::anchor() { }
2952
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002953TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2954 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2955 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2956}
2957
Richard Smith162e1c12011-04-15 14:24:37 +00002958TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2959 SourceLocation StartLoc,
2960 SourceLocation IdLoc, IdentifierInfo *Id,
2961 TypeSourceInfo *TInfo) {
2962 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2963}
2964
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002965TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2966 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2967 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2968}
2969
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002970SourceRange TypedefDecl::getSourceRange() const {
2971 SourceLocation RangeEnd = getLocation();
2972 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2973 if (typeIsPostfix(TInfo->getType()))
2974 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2975 }
2976 return SourceRange(getLocStart(), RangeEnd);
2977}
2978
Richard Smith162e1c12011-04-15 14:24:37 +00002979SourceRange TypeAliasDecl::getSourceRange() const {
2980 SourceLocation RangeEnd = getLocStart();
2981 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2982 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2983 return SourceRange(getLocStart(), RangeEnd);
2984}
2985
David Blaikie99ba9e32011-12-20 02:48:34 +00002986void FileScopeAsmDecl::anchor() { }
2987
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002988FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002989 StringLiteral *Str,
2990 SourceLocation AsmLoc,
2991 SourceLocation RParenLoc) {
2992 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002993}
Douglas Gregor15de72c2011-12-02 23:23:56 +00002994
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002995FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2996 unsigned ID) {
2997 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2998 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2999}
3000
Douglas Gregor15de72c2011-12-02 23:23:56 +00003001//===----------------------------------------------------------------------===//
3002// ImportDecl Implementation
3003//===----------------------------------------------------------------------===//
3004
3005/// \brief Retrieve the number of module identifiers needed to name the given
3006/// module.
3007static unsigned getNumModuleIdentifiers(Module *Mod) {
3008 unsigned Result = 1;
3009 while (Mod->Parent) {
3010 Mod = Mod->Parent;
3011 ++Result;
3012 }
3013 return Result;
3014}
3015
Douglas Gregor5948ae12012-01-03 18:04:46 +00003016ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003017 Module *Imported,
3018 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003019 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003020 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003021{
3022 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3023 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3024 memcpy(StoredLocs, IdentifierLocs.data(),
3025 IdentifierLocs.size() * sizeof(SourceLocation));
3026}
3027
Douglas Gregor5948ae12012-01-03 18:04:46 +00003028ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003029 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003030 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003031 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003032{
3033 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3034}
3035
3036ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003037 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003038 ArrayRef<SourceLocation> IdentifierLocs) {
3039 void *Mem = C.Allocate(sizeof(ImportDecl) +
3040 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003041 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003042}
3043
3044ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003045 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003046 Module *Imported,
3047 SourceLocation EndLoc) {
3048 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003049 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003050 Import->setImplicit();
3051 return Import;
3052}
3053
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003054ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3055 unsigned NumLocations) {
3056 void *Mem = AllocateDeserializedDecl(C, ID,
3057 (sizeof(ImportDecl) +
3058 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003059 return new (Mem) ImportDecl(EmptyShell());
3060}
3061
3062ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3063 if (!ImportedAndComplete.getInt())
3064 return ArrayRef<SourceLocation>();
3065
3066 const SourceLocation *StoredLocs
3067 = reinterpret_cast<const SourceLocation *>(this + 1);
3068 return ArrayRef<SourceLocation>(StoredLocs,
3069 getNumModuleIdentifiers(getImportedModule()));
3070}
3071
3072SourceRange ImportDecl::getSourceRange() const {
3073 if (!ImportedAndComplete.getInt())
3074 return SourceRange(getLocation(),
3075 *reinterpret_cast<const SourceLocation *>(this + 1));
3076
3077 return SourceRange(getLocation(), getIdentifierLocs().back());
3078}