blob: 8b8e1ee752093a671bcd704b4b044ae4f9c8c5e8 [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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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 Espindola140aadf2012-12-25 07:31:49 +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
Rafael Espindola7ac928b2013-01-04 21:18:45 +00001180template<typename T>
1181static bool hasCLanguageLinkageTemplate(const T &D) {
Rafael Espindola78eeba82012-12-28 14:21:58 +00001182 // Language linkage is a C++ concept, but saying that everything in C has
Rafael Espindola2b721f52013-01-04 20:41:40 +00001183 // C language linkage fits the implementation nicely.
Rafael Espindola78eeba82012-12-28 14:21:58 +00001184 ASTContext &Context = D.getASTContext();
1185 if (!Context.getLangOpts().CPlusPlus)
1186 return true;
1187
1188 // dcl.link 4: A C language linkage is ignored in determining the language
1189 // linkage of the names of class members and the function type of class member
1190 // functions.
1191 const DeclContext *DC = D.getDeclContext();
1192 if (DC->isRecord())
1193 return false;
1194
1195 // If the first decl is in an extern "C" context, any other redeclaration
1196 // will have C language linkage. If the first one is not in an extern "C"
1197 // context, we would have reported an error for any other decl being in one.
Rafael Espindola7ac928b2013-01-04 21:18:45 +00001198 const T *First = D.getFirstDeclaration();
Rafael Espindola78eeba82012-12-28 14:21:58 +00001199 return First->getDeclContext()->isExternCContext();
1200}
1201
1202bool VarDecl::hasCLanguageLinkage() const {
1203 return hasCLanguageLinkageTemplate(*this);
1204}
1205
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001206bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001207 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001208 return false;
1209
Eli Friedman750dc2b2012-01-15 01:23:58 +00001210 const DeclContext *DC = getDeclContext();
1211 if (DC->isRecord())
1212 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001213
Eli Friedman750dc2b2012-01-15 01:23:58 +00001214 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001215 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001216 return true;
1217 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001218}
1219
1220VarDecl *VarDecl::getCanonicalDecl() {
1221 return getFirstDeclaration();
1222}
1223
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001224VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1225 ASTContext &C) const
1226{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001227 // C++ [basic.def]p2:
1228 // A declaration is a definition unless [...] it contains the 'extern'
1229 // specifier or a linkage-specification and neither an initializer [...],
1230 // it declares a static data member in a class declaration [...].
1231 // C++ [temp.expl.spec]p15:
1232 // An explicit specialization of a static data member of a template is a
1233 // definition if the declaration includes an initializer; otherwise, it is
1234 // a declaration.
1235 if (isStaticDataMember()) {
1236 if (isOutOfLine() && (hasInit() ||
1237 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1238 return Definition;
1239 else
1240 return DeclarationOnly;
1241 }
1242 // C99 6.7p5:
1243 // A definition of an identifier is a declaration for that identifier that
1244 // [...] causes storage to be reserved for that object.
1245 // Note: that applies for all non-file-scope objects.
1246 // C99 6.9.2p1:
1247 // If the declaration of an identifier for an object has file scope and an
1248 // initializer, the declaration is an external definition for the identifier
1249 if (hasInit())
1250 return Definition;
1251 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1252 if (hasExternalStorage())
1253 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001254
John McCalld931b082010-08-26 03:08:43 +00001255 if (getStorageClassAsWritten() == SC_Extern ||
1256 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001257 for (const VarDecl *PrevVar = getPreviousDecl();
1258 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Rafael Espindola372df452012-12-17 22:23:47 +00001259 if (PrevVar->getLinkage() == InternalLinkage)
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001260 return DeclarationOnly;
1261 }
1262 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001263 // C99 6.9.2p2:
1264 // A declaration of an object that has file scope without an initializer,
1265 // and without a storage class specifier or the scs 'static', constitutes
1266 // a tentative definition.
1267 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001268 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001269 return TentativeDefinition;
1270
1271 // What's left is (in C, block-scope) declarations without initializers or
1272 // external storage. These are definitions.
1273 return Definition;
1274}
1275
Sebastian Redle9d12b62010-01-31 22:27:38 +00001276VarDecl *VarDecl::getActingDefinition() {
1277 DefinitionKind Kind = isThisDeclarationADefinition();
1278 if (Kind != TentativeDefinition)
1279 return 0;
1280
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001281 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001282 VarDecl *First = getFirstDeclaration();
1283 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1284 I != E; ++I) {
1285 Kind = (*I)->isThisDeclarationADefinition();
1286 if (Kind == Definition)
1287 return 0;
1288 else if (Kind == TentativeDefinition)
1289 LastTentative = *I;
1290 }
1291 return LastTentative;
1292}
1293
1294bool VarDecl::isTentativeDefinitionNow() const {
1295 DefinitionKind Kind = isThisDeclarationADefinition();
1296 if (Kind != TentativeDefinition)
1297 return false;
1298
1299 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1300 if ((*I)->isThisDeclarationADefinition() == Definition)
1301 return false;
1302 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001303 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001304}
1305
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001306VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001307 VarDecl *First = getFirstDeclaration();
1308 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1309 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001310 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001311 return *I;
1312 }
1313 return 0;
1314}
1315
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001316VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001317 DefinitionKind Kind = DeclarationOnly;
1318
1319 const VarDecl *First = getFirstDeclaration();
1320 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001321 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001322 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001323 if (Kind == Definition)
1324 break;
1325 }
John McCall110e8e52010-10-29 22:22:43 +00001326
1327 return Kind;
1328}
1329
Sebastian Redl31310a22010-02-01 20:16:42 +00001330const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001331 redecl_iterator I = redecls_begin(), E = redecls_end();
1332 while (I != E && !I->getInit())
1333 ++I;
1334
1335 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001336 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001337 return I->getInit();
1338 }
1339 return 0;
1340}
1341
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001342bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001343 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001344 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001345
1346 if (!isStaticDataMember())
1347 return false;
1348
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001349 // If this static data member was instantiated from a static data member of
1350 // a class template, check whether that static data member was defined
1351 // out-of-line.
1352 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1353 return VD->isOutOfLine();
1354
1355 return false;
1356}
1357
Douglas Gregor0d035142009-10-27 18:42:08 +00001358VarDecl *VarDecl::getOutOfLineDefinition() {
1359 if (!isStaticDataMember())
1360 return 0;
1361
1362 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1363 RD != RDEnd; ++RD) {
1364 if (RD->getLexicalDeclContext()->isFileContext())
1365 return *RD;
1366 }
1367
1368 return 0;
1369}
1370
Douglas Gregor838db382010-02-11 01:19:42 +00001371void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001372 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1373 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001374 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001375 }
1376
1377 Init = I;
1378}
1379
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001380bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001381 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001382
Richard Smith16581332012-03-02 04:14:40 +00001383 if (!Lang.CPlusPlus)
1384 return false;
1385
1386 // In C++11, any variable of reference type can be used in a constant
1387 // expression if it is initialized by a constant expression.
Richard Smith80ad52f2013-01-02 11:42:31 +00001388 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith16581332012-03-02 04:14:40 +00001389 return true;
1390
1391 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001392 // not require the variable to be non-volatile, but we consider this to be a
1393 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001394 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001395 return false;
1396
1397 // In C++, const, non-volatile variables of integral or enumeration types
1398 // can be used in constant expressions.
1399 if (getType()->isIntegralOrEnumerationType())
1400 return true;
1401
Richard Smith16581332012-03-02 04:14:40 +00001402 // Additionally, in C++11, non-volatile constexpr variables can be used in
1403 // constant expressions.
Richard Smith80ad52f2013-01-02 11:42:31 +00001404 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001405}
1406
Richard Smith099e7f62011-12-19 06:19:21 +00001407/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1408/// form, which contains extra information on the evaluated value of the
1409/// initializer.
1410EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1411 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1412 if (!Eval) {
1413 Stmt *S = Init.get<Stmt *>();
1414 Eval = new (getASTContext()) EvaluatedStmt;
1415 Eval->Value = S;
1416 Init = Eval;
1417 }
1418 return Eval;
1419}
1420
Richard Smith2d6a5672012-01-14 04:30:29 +00001421APValue *VarDecl::evaluateValue() const {
1422 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1423 return evaluateValue(Notes);
1424}
1425
1426APValue *VarDecl::evaluateValue(
1427 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001428 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1429
1430 // We only produce notes indicating why an initializer is non-constant the
1431 // first time it is evaluated. FIXME: The notes won't always be emitted the
1432 // first time we try evaluation, so might not be produced at all.
1433 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001434 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001435
1436 const Expr *Init = cast<Expr>(Eval->Value);
1437 assert(!Init->isValueDependent());
1438
1439 if (Eval->IsEvaluating) {
1440 // FIXME: Produce a diagnostic for self-initialization.
1441 Eval->CheckedICE = true;
1442 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001443 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001444 }
1445
1446 Eval->IsEvaluating = true;
1447
1448 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1449 this, Notes);
1450
1451 // Ensure the result is an uninitialized APValue if evaluation fails.
1452 if (!Result)
1453 Eval->Evaluated = APValue();
1454
1455 Eval->IsEvaluating = false;
1456 Eval->WasEvaluated = true;
1457
1458 // In C++11, we have determined whether the initializer was a constant
1459 // expression as a side-effect.
Richard Smith80ad52f2013-01-02 11:42:31 +00001460 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001461 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001462 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001463 }
1464
Richard Smith2d6a5672012-01-14 04:30:29 +00001465 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001466}
1467
1468bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001469 // Initializers of weak variables are never ICEs.
1470 if (isWeak())
1471 return false;
1472
Richard Smith099e7f62011-12-19 06:19:21 +00001473 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1474 if (Eval->CheckedICE)
1475 // We have already checked whether this subexpression is an
1476 // integral constant expression.
1477 return Eval->IsICE;
1478
1479 const Expr *Init = cast<Expr>(Eval->Value);
1480 assert(!Init->isValueDependent());
1481
1482 // In C++11, evaluate the initializer to check whether it's a constant
1483 // expression.
Richard Smith80ad52f2013-01-02 11:42:31 +00001484 if (getASTContext().getLangOpts().CPlusPlus11) {
Richard Smith099e7f62011-12-19 06:19:21 +00001485 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1486 evaluateValue(Notes);
1487 return Eval->IsICE;
1488 }
1489
1490 // It's an ICE whether or not the definition we found is
1491 // out-of-line. See DR 721 and the discussion in Clang PR
1492 // 6206 for details.
1493
1494 if (Eval->CheckingICE)
1495 return false;
1496 Eval->CheckingICE = true;
1497
1498 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1499 Eval->CheckingICE = false;
1500 Eval->CheckedICE = true;
1501 return Eval->IsICE;
1502}
1503
Douglas Gregor03e80032011-06-21 17:03:29 +00001504bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001505 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001506
1507 const Expr *E = getInit();
1508 if (!E)
1509 return false;
1510
1511 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1512 E = Cleanups->getSubExpr();
1513
1514 return isa<MaterializeTemporaryExpr>(E);
1515}
1516
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001517VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001518 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001519 return cast<VarDecl>(MSI->getInstantiatedFrom());
1520
1521 return 0;
1522}
1523
Douglas Gregor663b5a02009-10-14 20:14:33 +00001524TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001525 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001526 return MSI->getTemplateSpecializationKind();
1527
1528 return TSK_Undeclared;
1529}
1530
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001531MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001532 return getASTContext().getInstantiatedFromStaticDataMember(this);
1533}
1534
Douglas Gregor0a897e32009-10-15 17:21:20 +00001535void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1536 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001537 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001538 assert(MSI && "Not an instantiated static data member?");
1539 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001540 if (TSK != TSK_ExplicitSpecialization &&
1541 PointOfInstantiation.isValid() &&
1542 MSI->getPointOfInstantiation().isInvalid())
1543 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001544}
1545
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001546//===----------------------------------------------------------------------===//
1547// ParmVarDecl Implementation
1548//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001549
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001550ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001551 SourceLocation StartLoc,
1552 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001553 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001554 StorageClass S, StorageClass SCAsWritten,
1555 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001556 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001557 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001558}
1559
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001560ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1561 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1562 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1563 0, QualType(), 0, SC_None, SC_None, 0);
1564}
1565
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001566SourceRange ParmVarDecl::getSourceRange() const {
1567 if (!hasInheritedDefaultArg()) {
1568 SourceRange ArgRange = getDefaultArgRange();
1569 if (ArgRange.isValid())
1570 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1571 }
1572
1573 return DeclaratorDecl::getSourceRange();
1574}
1575
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001576Expr *ParmVarDecl::getDefaultArg() {
1577 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1578 assert(!hasUninstantiatedDefaultArg() &&
1579 "Default argument is not yet instantiated!");
1580
1581 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001582 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001583 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001584
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001585 return Arg;
1586}
1587
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001588SourceRange ParmVarDecl::getDefaultArgRange() const {
1589 if (const Expr *E = getInit())
1590 return E->getSourceRange();
1591
1592 if (hasUninstantiatedDefaultArg())
1593 return getUninstantiatedDefaultArg()->getSourceRange();
1594
1595 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001596}
1597
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001598bool ParmVarDecl::isParameterPack() const {
1599 return isa<PackExpansionType>(getType());
1600}
1601
Ted Kremenekd211cb72011-10-06 05:00:56 +00001602void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1603 getASTContext().setParameterIndex(this, parameterIndex);
1604 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1605}
1606
1607unsigned ParmVarDecl::getParameterIndexLarge() const {
1608 return getASTContext().getParameterIndex(this);
1609}
1610
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001611//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001612// FunctionDecl Implementation
1613//===----------------------------------------------------------------------===//
1614
Douglas Gregorda2142f2011-02-19 18:51:44 +00001615void FunctionDecl::getNameForDiagnostic(std::string &S,
1616 const PrintingPolicy &Policy,
1617 bool Qualified) const {
1618 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1619 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1620 if (TemplateArgs)
1621 S += TemplateSpecializationType::PrintTemplateArgumentList(
1622 TemplateArgs->data(),
1623 TemplateArgs->size(),
1624 Policy);
1625
1626}
1627
Ted Kremenek9498d382010-04-29 16:49:01 +00001628bool FunctionDecl::isVariadic() const {
1629 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1630 return FT->isVariadic();
1631 return false;
1632}
1633
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001634bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1635 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001636 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001637 Definition = *I;
1638 return true;
1639 }
1640 }
1641
1642 return false;
1643}
1644
Anders Carlssonffb945f2011-05-14 23:26:09 +00001645bool FunctionDecl::hasTrivialBody() const
1646{
1647 Stmt *S = getBody();
1648 if (!S) {
1649 // Since we don't have a body for this function, we don't know if it's
1650 // trivial or not.
1651 return false;
1652 }
1653
1654 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1655 return true;
1656 return false;
1657}
1658
Sean Hunt10620eb2011-05-06 20:44:56 +00001659bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1660 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001661 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001662 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1663 return true;
1664 }
1665 }
1666
1667 return false;
1668}
1669
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001670Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001671 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1672 if (I->Body) {
1673 Definition = *I;
1674 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001675 } else if (I->IsLateTemplateParsed) {
1676 Definition = *I;
1677 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001678 }
1679 }
1680
1681 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001682}
1683
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001684void FunctionDecl::setBody(Stmt *B) {
1685 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001686 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001687 EndRangeLoc = B->getLocEnd();
Rafael Espindola140aadf2012-12-25 07:31:49 +00001688 for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); R != REnd;
1689 ++R)
1690 R->ClearLVCache();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001691}
1692
Douglas Gregor21386642010-09-28 21:55:22 +00001693void FunctionDecl::setPure(bool P) {
1694 IsPure = P;
1695 if (P)
1696 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1697 Parent->markedVirtualFunctionPure();
1698}
1699
Douglas Gregor48a83b52009-09-12 00:17:51 +00001700bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001701 const TranslationUnitDecl *tunit =
1702 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1703 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001704 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001705 getIdentifier() &&
1706 getIdentifier()->isStr("main");
1707}
1708
1709bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1710 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1711 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1712 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1713 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1714 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1715
1716 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1717 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1718
1719 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1720 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1721
1722 ASTContext &Context =
1723 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1724 ->getASTContext();
1725
1726 // The result type and first argument type are constant across all
1727 // these operators. The second argument must be exactly void*.
1728 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001729}
1730
Rafael Espindola78eeba82012-12-28 14:21:58 +00001731bool FunctionDecl::hasCLanguageLinkage() const {
1732 return hasCLanguageLinkageTemplate(*this);
1733}
1734
Douglas Gregor48a83b52009-09-12 00:17:51 +00001735bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001736 if (getLinkage() != ExternalLinkage)
1737 return false;
1738
1739 if (getAttr<OverloadableAttr>())
1740 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001741
Chandler Carruth10aad442011-02-25 00:05:02 +00001742 const DeclContext *DC = getDeclContext();
1743 if (DC->isRecord())
1744 return false;
1745
Eli Friedman750dc2b2012-01-15 01:23:58 +00001746 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001747 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001748 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001749
Eli Friedman750dc2b2012-01-15 01:23:58 +00001750 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001751}
1752
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001753bool FunctionDecl::isGlobal() const {
1754 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1755 return Method->isStatic();
1756
John McCalld931b082010-08-26 03:08:43 +00001757 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001758 return false;
1759
Mike Stump1eb44332009-09-09 15:08:12 +00001760 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001761 DC->isNamespace();
1762 DC = DC->getParent()) {
1763 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1764 if (!Namespace->getDeclName())
1765 return false;
1766 break;
1767 }
1768 }
1769
1770 return true;
1771}
1772
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001773void
1774FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1775 redeclarable_base::setPreviousDeclaration(PrevDecl);
1776
1777 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1778 FunctionTemplateDecl *PrevFunTmpl
1779 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1780 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1781 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1782 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001783
Axel Naumannd9d137e2011-11-08 18:21:06 +00001784 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001785 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001786}
1787
1788const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1789 return getFirstDeclaration();
1790}
1791
1792FunctionDecl *FunctionDecl::getCanonicalDecl() {
1793 return getFirstDeclaration();
1794}
1795
Douglas Gregor381d34e2010-12-06 18:36:25 +00001796void FunctionDecl::setStorageClass(StorageClass SC) {
1797 assert(isLegalForFunction(SC));
1798 if (getStorageClass() != SC)
Rafael Espindola140aadf2012-12-25 07:31:49 +00001799 ClearLVCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001800
1801 SClass = SC;
1802}
1803
Douglas Gregor3e41d602009-02-13 23:20:09 +00001804/// \brief Returns a value indicating whether this function
1805/// corresponds to a builtin function.
1806///
1807/// The function corresponds to a built-in function if it is
1808/// declared at translation scope or within an extern "C" block and
1809/// its name matches with the name of a builtin. The returned value
1810/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001811/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001812/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001813unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001814 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001815 return 0;
1816
1817 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001818 if (!BuiltinID)
1819 return 0;
1820
1821 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001822 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1823 return BuiltinID;
1824
1825 // This function has the name of a known C library
1826 // function. Determine whether it actually refers to the C library
1827 // function or whether it just has the same name.
1828
Douglas Gregor9add3172009-02-17 03:23:10 +00001829 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001830 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001831 return 0;
1832
Douglas Gregor3c385e52009-02-14 18:57:46 +00001833 // If this function is at translation-unit scope and we're not in
1834 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001835 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001836 getDeclContext()->isTranslationUnit())
1837 return BuiltinID;
1838
1839 // If the function is in an extern "C" linkage specification and is
1840 // not marked "overloadable", it's the real function.
1841 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001842 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001843 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001844 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001845 return BuiltinID;
1846
1847 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001848 return 0;
1849}
1850
1851
Chris Lattner1ad9b282009-04-25 06:03:53 +00001852/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001853/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001854/// after it has been created.
1855unsigned FunctionDecl::getNumParams() const {
Eli Friedman482466b2012-08-30 22:22:09 +00001856 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001857 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001858 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001859 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Reid Spencer5f016e22007-07-11 17:01:13 +00001861}
1862
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001863void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001864 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001865 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001866 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001869 if (!NewParamInfo.empty()) {
1870 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1871 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001872 }
1873}
1874
James Molloy16f1f712012-02-29 10:24:19 +00001875void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1876 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1877
1878 if (!NewDecls.empty()) {
1879 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1880 std::copy(NewDecls.begin(), NewDecls.end(), A);
1881 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1882 }
1883}
1884
Chris Lattner8123a952008-04-10 02:22:51 +00001885/// getMinRequiredArguments - Returns the minimum number of arguments
1886/// needed to call this function. This may be fewer than the number of
1887/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001888/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001889unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001890 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001891 return getNumParams();
1892
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001893 unsigned NumRequiredArgs = getNumParams();
1894
1895 // If the last parameter is a parameter pack, we don't need an argument for
1896 // it.
1897 if (NumRequiredArgs > 0 &&
1898 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1899 --NumRequiredArgs;
1900
1901 // If this parameter has a default argument, we don't need an argument for
1902 // it.
1903 while (NumRequiredArgs > 0 &&
1904 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001905 --NumRequiredArgs;
1906
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001907 // We might have parameter packs before the end. These can't be deduced,
1908 // but they can still handle multiple arguments.
1909 unsigned ArgIdx = NumRequiredArgs;
1910 while (ArgIdx > 0) {
1911 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1912 NumRequiredArgs = ArgIdx;
1913
1914 --ArgIdx;
1915 }
1916
Chris Lattner8123a952008-04-10 02:22:51 +00001917 return NumRequiredArgs;
1918}
1919
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001920bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001921 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001922 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001923
1924 if (isa<CXXMethodDecl>(this)) {
1925 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1926 return true;
1927 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001928
1929 switch (getTemplateSpecializationKind()) {
1930 case TSK_Undeclared:
1931 case TSK_ExplicitSpecialization:
1932 return false;
1933
1934 case TSK_ImplicitInstantiation:
1935 case TSK_ExplicitInstantiationDeclaration:
1936 case TSK_ExplicitInstantiationDefinition:
1937 // Handle below.
1938 break;
1939 }
1940
1941 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001942 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001943 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001944 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001945
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001946 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001947 return PatternDecl->isInlined();
1948
1949 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001950}
1951
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001952static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1953 // Only consider file-scope declarations in this test.
1954 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1955 return false;
1956
1957 // Only consider explicit declarations; the presence of a builtin for a
1958 // libcall shouldn't affect whether a definition is externally visible.
1959 if (Redecl->isImplicit())
1960 return false;
1961
1962 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1963 return true; // Not an inline definition
1964
1965 return false;
1966}
1967
Nick Lewyckydce67a72011-07-18 05:26:13 +00001968/// \brief For a function declaration in C or C++, determine whether this
1969/// declaration causes the definition to be externally visible.
1970///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001971/// Specifically, this determines if adding the current declaration to the set
1972/// of redeclarations of the given functions causes
1973/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001974bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1975 assert(!doesThisDeclarationHaveABody() &&
1976 "Must have a declaration without a body.");
1977
1978 ASTContext &Context = getASTContext();
1979
David Blaikie4e4d0842012-03-11 07:00:24 +00001980 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001981 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1982 // an externally visible definition.
1983 //
1984 // FIXME: What happens if gnu_inline gets added on after the first
1985 // declaration?
1986 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1987 return false;
1988
1989 const FunctionDecl *Prev = this;
1990 bool FoundBody = false;
1991 while ((Prev = Prev->getPreviousDecl())) {
1992 FoundBody |= Prev->Body;
1993
1994 if (Prev->Body) {
1995 // If it's not the case that both 'inline' and 'extern' are
1996 // specified on the definition, then it is always externally visible.
1997 if (!Prev->isInlineSpecified() ||
1998 Prev->getStorageClassAsWritten() != SC_Extern)
1999 return false;
2000 } else if (Prev->isInlineSpecified() &&
2001 Prev->getStorageClassAsWritten() != SC_Extern) {
2002 return false;
2003 }
2004 }
2005 return FoundBody;
2006 }
2007
David Blaikie4e4d0842012-03-11 07:00:24 +00002008 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002009 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002010
2011 // C99 6.7.4p6:
2012 // [...] If all of the file scope declarations for a function in a
2013 // translation unit include the inline function specifier without extern,
2014 // then the definition in that translation unit is an inline definition.
2015 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002016 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002017 const FunctionDecl *Prev = this;
2018 bool FoundBody = false;
2019 while ((Prev = Prev->getPreviousDecl())) {
2020 FoundBody |= Prev->Body;
2021 if (RedeclForcesDefC99(Prev))
2022 return false;
2023 }
2024 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00002025}
2026
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002027/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002028/// definition will be externally visible.
2029///
2030/// Inline function definitions are always available for inlining optimizations.
2031/// However, depending on the language dialect, declaration specifiers, and
2032/// attributes, the definition of an inline function may or may not be
2033/// "externally" visible to other translation units in the program.
2034///
2035/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002036/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002037/// inline definition becomes externally visible (C99 6.7.4p6).
2038///
2039/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2040/// definition, we use the GNU semantics for inline, which are nearly the
2041/// opposite of C99 semantics. In particular, "inline" by itself will create
2042/// an externally visible symbol, but "extern inline" will not create an
2043/// externally visible symbol.
2044bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002045 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002046 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002047 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002048
David Blaikie4e4d0842012-03-11 07:00:24 +00002049 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002050 // Note: If you change the logic here, please change
2051 // doesDeclarationForceExternallyVisibleDefinition as well.
2052 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002053 // If it's not the case that both 'inline' and 'extern' are
2054 // specified on the definition, then this inline definition is
2055 // externally visible.
2056 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2057 return true;
2058
2059 // If any declaration is 'inline' but not 'extern', then this definition
2060 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002061 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2062 Redecl != RedeclEnd;
2063 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002064 if (Redecl->isInlineSpecified() &&
2065 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002066 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002067 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002068
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002069 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002070 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002071
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002072 // C99 6.7.4p6:
2073 // [...] If all of the file scope declarations for a function in a
2074 // translation unit include the inline function specifier without extern,
2075 // then the definition in that translation unit is an inline definition.
2076 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2077 Redecl != RedeclEnd;
2078 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002079 if (RedeclForcesDefC99(*Redecl))
2080 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002081 }
2082
2083 // C99 6.7.4p6:
2084 // An inline definition does not provide an external definition for the
2085 // function, and does not forbid an external definition in another
2086 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002087 return false;
2088}
2089
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002090/// getOverloadedOperator - Which C++ overloaded operator this
2091/// function represents, if any.
2092OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002093 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2094 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002095 else
2096 return OO_None;
2097}
2098
Sean Hunta6c058d2010-01-13 09:01:02 +00002099/// getLiteralIdentifier - The literal suffix identifier this function
2100/// represents, if any.
2101const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2102 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2103 return getDeclName().getCXXLiteralIdentifier();
2104 else
2105 return 0;
2106}
2107
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002108FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2109 if (TemplateOrSpecialization.isNull())
2110 return TK_NonTemplate;
2111 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2112 return TK_FunctionTemplate;
2113 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2114 return TK_MemberSpecialization;
2115 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2116 return TK_FunctionTemplateSpecialization;
2117 if (TemplateOrSpecialization.is
2118 <DependentFunctionTemplateSpecializationInfo*>())
2119 return TK_DependentFunctionTemplateSpecialization;
2120
David Blaikieb219cfc2011-09-23 05:06:16 +00002121 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002122}
2123
Douglas Gregor2db32322009-10-07 23:56:10 +00002124FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002125 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002126 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2127
2128 return 0;
2129}
2130
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002131MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2132 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2133}
2134
Douglas Gregor2db32322009-10-07 23:56:10 +00002135void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002136FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2137 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002138 TemplateSpecializationKind TSK) {
2139 assert(TemplateOrSpecialization.isNull() &&
2140 "Member function is already a specialization");
2141 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002142 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002143 TemplateOrSpecialization = Info;
2144}
2145
Douglas Gregor3b846b62009-10-27 20:53:28 +00002146bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002147 // If the function is invalid, it can't be implicitly instantiated.
2148 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002149 return false;
2150
2151 switch (getTemplateSpecializationKind()) {
2152 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002153 case TSK_ExplicitInstantiationDefinition:
2154 return false;
2155
2156 case TSK_ImplicitInstantiation:
2157 return true;
2158
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002159 // It is possible to instantiate TSK_ExplicitSpecialization kind
2160 // if the FunctionDecl has a class scope specialization pattern.
2161 case TSK_ExplicitSpecialization:
2162 return getClassScopeSpecializationPattern() != 0;
2163
Douglas Gregor3b846b62009-10-27 20:53:28 +00002164 case TSK_ExplicitInstantiationDeclaration:
2165 // Handled below.
2166 break;
2167 }
2168
2169 // Find the actual template from which we will instantiate.
2170 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002171 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002172 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002173 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002174
2175 // C++0x [temp.explicit]p9:
2176 // Except for inline functions, other explicit instantiation declarations
2177 // have the effect of suppressing the implicit instantiation of the entity
2178 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002179 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002180 return true;
2181
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002182 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002183}
2184
2185bool FunctionDecl::isTemplateInstantiation() const {
2186 switch (getTemplateSpecializationKind()) {
2187 case TSK_Undeclared:
2188 case TSK_ExplicitSpecialization:
2189 return false;
2190 case TSK_ImplicitInstantiation:
2191 case TSK_ExplicitInstantiationDeclaration:
2192 case TSK_ExplicitInstantiationDefinition:
2193 return true;
2194 }
2195 llvm_unreachable("All TSK values handled.");
2196}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002197
2198FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002199 // Handle class scope explicit specialization special case.
2200 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2201 return getClassScopeSpecializationPattern();
2202
Douglas Gregor3b846b62009-10-27 20:53:28 +00002203 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2204 while (Primary->getInstantiatedFromMemberTemplate()) {
2205 // If we have hit a point where the user provided a specialization of
2206 // this template, we're done looking.
2207 if (Primary->isMemberSpecialization())
2208 break;
2209
2210 Primary = Primary->getInstantiatedFromMemberTemplate();
2211 }
2212
2213 return Primary->getTemplatedDecl();
2214 }
2215
2216 return getInstantiatedFromMemberFunction();
2217}
2218
Douglas Gregor16e8be22009-06-29 17:30:29 +00002219FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002220 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002221 = TemplateOrSpecialization
2222 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002223 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002224 }
2225 return 0;
2226}
2227
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002228FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2229 return getASTContext().getClassScopeSpecializationPattern(this);
2230}
2231
Douglas Gregor16e8be22009-06-29 17:30:29 +00002232const TemplateArgumentList *
2233FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002234 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002235 = TemplateOrSpecialization
2236 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002237 return Info->TemplateArguments;
2238 }
2239 return 0;
2240}
2241
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002242const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002243FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2244 if (FunctionTemplateSpecializationInfo *Info
2245 = TemplateOrSpecialization
2246 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2247 return Info->TemplateArgumentsAsWritten;
2248 }
2249 return 0;
2250}
2251
Mike Stump1eb44332009-09-09 15:08:12 +00002252void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002253FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2254 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002255 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002256 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002257 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002258 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2259 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002260 assert(TSK != TSK_Undeclared &&
2261 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002262 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002263 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002264 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002265 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2266 TemplateArgs,
2267 TemplateArgsAsWritten,
2268 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002269 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002270 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002271}
2272
John McCallaf2094e2010-04-08 09:05:18 +00002273void
2274FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2275 const UnresolvedSetImpl &Templates,
2276 const TemplateArgumentListInfo &TemplateArgs) {
2277 assert(TemplateOrSpecialization.isNull());
2278 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2279 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002280 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002281 void *Buffer = Context.Allocate(Size);
2282 DependentFunctionTemplateSpecializationInfo *Info =
2283 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2284 TemplateArgs);
2285 TemplateOrSpecialization = Info;
2286}
2287
2288DependentFunctionTemplateSpecializationInfo::
2289DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2290 const TemplateArgumentListInfo &TArgs)
2291 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2292
2293 d.NumTemplates = Ts.size();
2294 d.NumArgs = TArgs.size();
2295
2296 FunctionTemplateDecl **TsArray =
2297 const_cast<FunctionTemplateDecl**>(getTemplates());
2298 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2299 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2300
2301 TemplateArgumentLoc *ArgsArray =
2302 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2303 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2304 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2305}
2306
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002307TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002308 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002309 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002310 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002311 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002312 if (FTSInfo)
2313 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Douglas Gregor2db32322009-10-07 23:56:10 +00002315 MemberSpecializationInfo *MSInfo
2316 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2317 if (MSInfo)
2318 return MSInfo->getTemplateSpecializationKind();
2319
2320 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002321}
2322
Mike Stump1eb44332009-09-09 15:08:12 +00002323void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002324FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2325 SourceLocation PointOfInstantiation) {
2326 if (FunctionTemplateSpecializationInfo *FTSInfo
2327 = TemplateOrSpecialization.dyn_cast<
2328 FunctionTemplateSpecializationInfo*>()) {
2329 FTSInfo->setTemplateSpecializationKind(TSK);
2330 if (TSK != TSK_ExplicitSpecialization &&
2331 PointOfInstantiation.isValid() &&
2332 FTSInfo->getPointOfInstantiation().isInvalid())
2333 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2334 } else if (MemberSpecializationInfo *MSInfo
2335 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2336 MSInfo->setTemplateSpecializationKind(TSK);
2337 if (TSK != TSK_ExplicitSpecialization &&
2338 PointOfInstantiation.isValid() &&
2339 MSInfo->getPointOfInstantiation().isInvalid())
2340 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2341 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002342 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002343}
2344
2345SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002346 if (FunctionTemplateSpecializationInfo *FTSInfo
2347 = TemplateOrSpecialization.dyn_cast<
2348 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002349 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002350 else if (MemberSpecializationInfo *MSInfo
2351 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002352 return MSInfo->getPointOfInstantiation();
2353
2354 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002355}
2356
Douglas Gregor9f185072009-09-11 20:15:17 +00002357bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002358 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002359 return true;
2360
2361 // If this function was instantiated from a member function of a
2362 // class template, check whether that member function was defined out-of-line.
2363 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2364 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002365 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002366 return Definition->isOutOfLine();
2367 }
2368
2369 // If this function was instantiated from a function template,
2370 // check whether that function template was defined out-of-line.
2371 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2372 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002373 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002374 return Definition->isOutOfLine();
2375 }
2376
2377 return false;
2378}
2379
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002380SourceRange FunctionDecl::getSourceRange() const {
2381 return SourceRange(getOuterLocStart(), EndRangeLoc);
2382}
2383
Anna Zaks9392d4e2012-01-18 02:45:01 +00002384unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002385 IdentifierInfo *FnInfo = getIdentifier();
2386
2387 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002388 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002389
2390 // Builtin handling.
2391 switch (getBuiltinID()) {
2392 case Builtin::BI__builtin_memset:
2393 case Builtin::BI__builtin___memset_chk:
2394 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002395 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002396
2397 case Builtin::BI__builtin_memcpy:
2398 case Builtin::BI__builtin___memcpy_chk:
2399 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002400 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002401
2402 case Builtin::BI__builtin_memmove:
2403 case Builtin::BI__builtin___memmove_chk:
2404 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002405 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002406
2407 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002408 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002409 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002410 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002411
2412 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002413 case Builtin::BImemcmp:
2414 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002415
2416 case Builtin::BI__builtin_strncpy:
2417 case Builtin::BI__builtin___strncpy_chk:
2418 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002419 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002420
2421 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002422 case Builtin::BIstrncmp:
2423 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002424
2425 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002426 case Builtin::BIstrncasecmp:
2427 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002428
2429 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002430 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002431 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002432 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002433
2434 case Builtin::BI__builtin_strndup:
2435 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002436 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002437
Anna Zaksc36bedc2012-02-01 19:08:57 +00002438 case Builtin::BI__builtin_strlen:
2439 case Builtin::BIstrlen:
2440 return Builtin::BIstrlen;
2441
Anna Zaksd9b859a2012-01-13 21:52:01 +00002442 default:
Rafael Espindola9f0c6922012-12-30 17:23:09 +00002443 if (hasCLanguageLinkage()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002444 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002445 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002446 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002447 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002448 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002449 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002450 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002451 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002452 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002453 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002454 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002455 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002456 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002457 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002458 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002459 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002460 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002461 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002462 else if (FnInfo->isStr("strlen"))
2463 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002464 }
2465 break;
2466 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002467 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002468}
2469
Chris Lattner8a934232008-03-31 00:36:02 +00002470//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002471// FieldDecl Implementation
2472//===----------------------------------------------------------------------===//
2473
Jay Foad4ba2a172011-01-12 09:06:06 +00002474FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002475 SourceLocation StartLoc, SourceLocation IdLoc,
2476 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002477 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smithca523302012-06-10 03:12:00 +00002478 InClassInitStyle InitStyle) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002479 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithca523302012-06-10 03:12:00 +00002480 BW, Mutable, InitStyle);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002481}
2482
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002483FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2484 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2485 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smithca523302012-06-10 03:12:00 +00002486 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002487}
2488
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002489bool FieldDecl::isAnonymousStructOrUnion() const {
2490 if (!isImplicit() || getDeclName())
2491 return false;
2492
2493 if (const RecordType *Record = getType()->getAs<RecordType>())
2494 return Record->getDecl()->isAnonymousStructOrUnion();
2495
2496 return false;
2497}
2498
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002499unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2500 assert(isBitField() && "not a bitfield");
2501 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2502 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2503}
2504
John McCallba4f5d52011-01-20 07:57:12 +00002505unsigned FieldDecl::getFieldIndex() const {
2506 if (CachedFieldIndex) return CachedFieldIndex - 1;
2507
Richard Smith180f4792011-11-10 06:34:14 +00002508 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002509 const RecordDecl *RD = getParent();
2510 const FieldDecl *LastFD = 0;
Eli Friedman5f608ae2012-10-12 23:29:20 +00002511 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smith180f4792011-11-10 06:34:14 +00002512
2513 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2514 I != E; ++I, ++Index) {
David Blaikie262bc182012-04-30 02:36:29 +00002515 I->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002516
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002517 if (IsMsStruct) {
2518 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie581deb32012-06-06 20:45:41 +00002519 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smith180f4792011-11-10 06:34:14 +00002520 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002521 continue;
2522 }
David Blaikie581deb32012-06-06 20:45:41 +00002523 LastFD = *I;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002524 }
John McCallba4f5d52011-01-20 07:57:12 +00002525 }
2526
Richard Smith180f4792011-11-10 06:34:14 +00002527 assert(CachedFieldIndex && "failed to find field in parent");
2528 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002529}
2530
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002531SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002532 if (const Expr *E = InitializerOrBitWidth.getPointer())
2533 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002534 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002535}
2536
Abramo Bagnaraa5335762012-07-02 20:35:48 +00002537void FieldDecl::setBitWidth(Expr *Width) {
2538 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2539 "bit width or initializer already set");
2540 InitializerOrBitWidth.setPointer(Width);
2541}
2542
Richard Smith7a614d82011-06-11 17:19:42 +00002543void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smithca523302012-06-10 03:12:00 +00002544 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith7a614d82011-06-11 17:19:42 +00002545 "bit width or initializer already set");
2546 InitializerOrBitWidth.setPointer(Init);
Richard Smith7a614d82011-06-11 17:19:42 +00002547}
2548
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002549//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002550// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002551//===----------------------------------------------------------------------===//
2552
Douglas Gregor1693e152010-07-06 18:42:40 +00002553SourceLocation TagDecl::getOuterLocStart() const {
2554 return getTemplateOrInnerLocStart(this);
2555}
2556
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002557SourceRange TagDecl::getSourceRange() const {
2558 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002559 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002560}
2561
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002562TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002563 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002564}
2565
Richard Smith162e1c12011-04-15 14:24:37 +00002566void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2567 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002568 if (TypeForDecl)
Rafael Espindola140aadf2012-12-25 07:31:49 +00002569 const_cast<Type*>(TypeForDecl)->ClearLVCache();
2570 ClearLVCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002571}
2572
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002573void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002574 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002575
David Blaikie66cff722012-11-14 01:52:05 +00002576 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall86ff3082010-02-04 22:26:26 +00002577 struct CXXRecordDecl::DefinitionData *Data =
2578 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002579 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2580 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002581 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002582}
2583
2584void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002585 assert((!isa<CXXRecordDecl>(this) ||
2586 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2587 "definition completed but not started");
2588
John McCall5e1cdac2011-10-07 06:10:15 +00002589 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002590 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002591
2592 if (ASTMutationListener *L = getASTMutationListener())
2593 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002594}
2595
John McCall5e1cdac2011-10-07 06:10:15 +00002596TagDecl *TagDecl::getDefinition() const {
2597 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002598 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002599 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2600 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002601
2602 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002603 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002604 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002605 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002607 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002608}
2609
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002610void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2611 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002612 // Make sure the extended qualifier info is allocated.
2613 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002614 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002615 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002616 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002617 } else {
John McCallb6217662010-03-15 10:12:16 +00002618 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002619 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002620 if (getExtInfo()->NumTemplParamLists == 0) {
2621 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002622 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002623 }
2624 else
2625 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002626 }
2627 }
2628}
2629
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002630void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2631 unsigned NumTPLists,
2632 TemplateParameterList **TPLists) {
2633 assert(NumTPLists > 0);
2634 // Make sure the extended decl info is allocated.
2635 if (!hasExtInfo())
2636 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002637 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002638 // Set the template parameter lists info.
2639 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2640}
2641
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002642//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002643// EnumDecl Implementation
2644//===----------------------------------------------------------------------===//
2645
David Blaikie99ba9e32011-12-20 02:48:34 +00002646void EnumDecl::anchor() { }
2647
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002648EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2649 SourceLocation StartLoc, SourceLocation IdLoc,
2650 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002651 EnumDecl *PrevDecl, bool IsScoped,
2652 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002653 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002654 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002655 C.getTypeDeclType(Enum, PrevDecl);
2656 return Enum;
2657}
2658
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002659EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2660 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2661 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2662 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002663}
2664
Douglas Gregor838db382010-02-11 01:19:42 +00002665void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002666 QualType NewPromotionType,
2667 unsigned NumPositiveBits,
2668 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002669 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002670 if (!IntegerType)
2671 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002672 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002673 setNumPositiveBits(NumPositiveBits);
2674 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002675 TagDecl::completeDefinition();
2676}
2677
Richard Smith1af83c42012-03-23 03:33:32 +00002678TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2679 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2680 return MSI->getTemplateSpecializationKind();
2681
2682 return TSK_Undeclared;
2683}
2684
2685void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2686 SourceLocation PointOfInstantiation) {
2687 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2688 assert(MSI && "Not an instantiated member enumeration?");
2689 MSI->setTemplateSpecializationKind(TSK);
2690 if (TSK != TSK_ExplicitSpecialization &&
2691 PointOfInstantiation.isValid() &&
2692 MSI->getPointOfInstantiation().isInvalid())
2693 MSI->setPointOfInstantiation(PointOfInstantiation);
2694}
2695
Richard Smithf1c66b42012-03-14 23:13:10 +00002696EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2697 if (SpecializationInfo)
2698 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2699
2700 return 0;
2701}
2702
2703void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2704 TemplateSpecializationKind TSK) {
2705 assert(!SpecializationInfo && "Member enum is already a specialization");
2706 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2707}
2708
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002709//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002710// RecordDecl Implementation
2711//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002712
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002713RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2714 SourceLocation StartLoc, SourceLocation IdLoc,
2715 IdentifierInfo *Id, RecordDecl *PrevDecl)
2716 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002717 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002718 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002719 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002720 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002721 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002722}
2723
Jay Foad4ba2a172011-01-12 09:06:06 +00002724RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002725 SourceLocation StartLoc, SourceLocation IdLoc,
2726 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2727 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2728 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002729 C.getTypeDeclType(R, PrevDecl);
2730 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002731}
2732
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002733RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2734 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2735 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2736 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002737}
2738
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002739bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002740 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002741 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2742}
2743
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002744RecordDecl::field_iterator RecordDecl::field_begin() const {
2745 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2746 LoadFieldsFromExternalStorage();
2747
2748 return field_iterator(decl_iterator(FirstDecl));
2749}
2750
Douglas Gregorda2142f2011-02-19 18:51:44 +00002751/// completeDefinition - Notes that the definition of this type is now
2752/// complete.
2753void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002754 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002755 TagDecl::completeDefinition();
2756}
2757
Eli Friedman5f608ae2012-10-12 23:29:20 +00002758/// isMsStruct - Get whether or not this record uses ms_struct layout.
2759/// This which can be turned on with an attribute, pragma, or the
2760/// -mms-bitfields command-line option.
2761bool RecordDecl::isMsStruct(const ASTContext &C) const {
2762 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2763}
2764
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002765static bool isFieldOrIndirectField(Decl::Kind K) {
2766 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2767}
2768
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002769void RecordDecl::LoadFieldsFromExternalStorage() const {
2770 ExternalASTSource *Source = getASTContext().getExternalSource();
2771 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2772
2773 // Notify that we have a RecordDecl doing some initialization.
2774 ExternalASTSource::Deserializing TheFields(Source);
2775
Chris Lattner5f9e2722011-07-23 10:55:15 +00002776 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002777 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002778 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2779 Decls)) {
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002780 case ELR_Success:
2781 break;
2782
2783 case ELR_AlreadyLoaded:
2784 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002785 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002786 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002787
2788#ifndef NDEBUG
2789 // Check that all decls we got were FieldDecls.
2790 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002791 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002792#endif
2793
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002794 if (Decls.empty())
2795 return;
2796
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002797 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2798 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002799}
2800
Steve Naroff56ee6892008-10-08 17:01:13 +00002801//===----------------------------------------------------------------------===//
2802// BlockDecl Implementation
2803//===----------------------------------------------------------------------===//
2804
David Blaikie4278c652011-09-21 18:16:56 +00002805void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002806 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002807
Steve Naroffe78b8092009-03-13 16:56:44 +00002808 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002809 if (!NewParamInfo.empty()) {
2810 NumParams = NewParamInfo.size();
2811 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2812 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002813 }
2814}
2815
John McCall6b5a61b2011-02-07 10:33:21 +00002816void BlockDecl::setCaptures(ASTContext &Context,
2817 const Capture *begin,
2818 const Capture *end,
2819 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002820 CapturesCXXThis = capturesCXXThis;
2821
2822 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002823 NumCaptures = 0;
2824 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002825 return;
2826 }
2827
John McCall6b5a61b2011-02-07 10:33:21 +00002828 NumCaptures = end - begin;
2829
2830 // Avoid new Capture[] because we don't want to provide a default
2831 // constructor.
2832 size_t allocationSize = NumCaptures * sizeof(Capture);
2833 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2834 memcpy(buffer, begin, allocationSize);
2835 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002836}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002837
John McCall204e1332011-06-15 22:51:16 +00002838bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2839 for (capture_const_iterator
2840 i = capture_begin(), e = capture_end(); i != e; ++i)
2841 // Only auto vars can be captured, so no redeclaration worries.
2842 if (i->getVariable() == variable)
2843 return true;
2844
2845 return false;
2846}
2847
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002848SourceRange BlockDecl::getSourceRange() const {
2849 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2850}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002851
2852//===----------------------------------------------------------------------===//
2853// Other Decl Allocation/Deallocation Method Implementations
2854//===----------------------------------------------------------------------===//
2855
David Blaikie99ba9e32011-12-20 02:48:34 +00002856void TranslationUnitDecl::anchor() { }
2857
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002858TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2859 return new (C) TranslationUnitDecl(C);
2860}
2861
David Blaikie99ba9e32011-12-20 02:48:34 +00002862void LabelDecl::anchor() { }
2863
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002864LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002865 SourceLocation IdentL, IdentifierInfo *II) {
2866 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2867}
2868
2869LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2870 SourceLocation IdentL, IdentifierInfo *II,
2871 SourceLocation GnuLabelL) {
2872 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2873 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002874}
2875
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002876LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2877 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2878 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002879}
2880
David Blaikie99ba9e32011-12-20 02:48:34 +00002881void ValueDecl::anchor() { }
2882
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +00002883bool ValueDecl::isWeak() const {
2884 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
2885 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
2886 return true;
2887
2888 return isWeakImported();
2889}
2890
David Blaikie99ba9e32011-12-20 02:48:34 +00002891void ImplicitParamDecl::anchor() { }
2892
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002893ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002894 SourceLocation IdLoc,
2895 IdentifierInfo *Id,
2896 QualType Type) {
2897 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002898}
2899
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002900ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2901 unsigned ID) {
2902 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2903 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2904}
2905
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002906FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002907 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002908 const DeclarationNameInfo &NameInfo,
2909 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002910 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002911 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002912 bool hasWrittenPrototype,
2913 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002914 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2915 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002916 isInlineSpecified,
2917 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002918 New->HasWrittenPrototype = hasWrittenPrototype;
2919 return New;
2920}
2921
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002922FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2923 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2924 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2925 DeclarationNameInfo(), QualType(), 0,
2926 SC_None, SC_None, false, false);
2927}
2928
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002929BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2930 return new (C) BlockDecl(DC, L);
2931}
2932
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002933BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2934 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2935 return new (Mem) BlockDecl(0, SourceLocation());
2936}
2937
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002938EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2939 SourceLocation L,
2940 IdentifierInfo *Id, QualType T,
2941 Expr *E, const llvm::APSInt &V) {
2942 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2943}
2944
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002945EnumConstantDecl *
2946EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2947 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2948 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2949 llvm::APSInt());
2950}
2951
David Blaikie99ba9e32011-12-20 02:48:34 +00002952void IndirectFieldDecl::anchor() { }
2953
Benjamin Kramerd9811462010-11-21 14:11:41 +00002954IndirectFieldDecl *
2955IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2956 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2957 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002958 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2959}
2960
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002961IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2962 unsigned ID) {
2963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2964 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2965 QualType(), 0, 0);
2966}
2967
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002968SourceRange EnumConstantDecl::getSourceRange() const {
2969 SourceLocation End = getLocation();
2970 if (Init)
2971 End = Init->getLocEnd();
2972 return SourceRange(getLocation(), End);
2973}
2974
David Blaikie99ba9e32011-12-20 02:48:34 +00002975void TypeDecl::anchor() { }
2976
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002977TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002978 SourceLocation StartLoc, SourceLocation IdLoc,
2979 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2980 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002981}
2982
David Blaikie99ba9e32011-12-20 02:48:34 +00002983void TypedefNameDecl::anchor() { }
2984
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002985TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2986 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2987 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2988}
2989
Richard Smith162e1c12011-04-15 14:24:37 +00002990TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2991 SourceLocation StartLoc,
2992 SourceLocation IdLoc, IdentifierInfo *Id,
2993 TypeSourceInfo *TInfo) {
2994 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2995}
2996
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002997TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2998 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2999 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3000}
3001
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00003002SourceRange TypedefDecl::getSourceRange() const {
3003 SourceLocation RangeEnd = getLocation();
3004 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3005 if (typeIsPostfix(TInfo->getType()))
3006 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3007 }
3008 return SourceRange(getLocStart(), RangeEnd);
3009}
3010
Richard Smith162e1c12011-04-15 14:24:37 +00003011SourceRange TypeAliasDecl::getSourceRange() const {
3012 SourceLocation RangeEnd = getLocStart();
3013 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3014 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3015 return SourceRange(getLocStart(), RangeEnd);
3016}
3017
David Blaikie99ba9e32011-12-20 02:48:34 +00003018void FileScopeAsmDecl::anchor() { }
3019
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003020FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00003021 StringLiteral *Str,
3022 SourceLocation AsmLoc,
3023 SourceLocation RParenLoc) {
3024 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003025}
Douglas Gregor15de72c2011-12-02 23:23:56 +00003026
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003027FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3028 unsigned ID) {
3029 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3030 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3031}
3032
Douglas Gregor15de72c2011-12-02 23:23:56 +00003033//===----------------------------------------------------------------------===//
3034// ImportDecl Implementation
3035//===----------------------------------------------------------------------===//
3036
3037/// \brief Retrieve the number of module identifiers needed to name the given
3038/// module.
3039static unsigned getNumModuleIdentifiers(Module *Mod) {
3040 unsigned Result = 1;
3041 while (Mod->Parent) {
3042 Mod = Mod->Parent;
3043 ++Result;
3044 }
3045 return Result;
3046}
3047
Douglas Gregor5948ae12012-01-03 18:04:46 +00003048ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003049 Module *Imported,
3050 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003051 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003052 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003053{
3054 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3055 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3056 memcpy(StoredLocs, IdentifierLocs.data(),
3057 IdentifierLocs.size() * sizeof(SourceLocation));
3058}
3059
Douglas Gregor5948ae12012-01-03 18:04:46 +00003060ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003061 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003062 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003063 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003064{
3065 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3066}
3067
3068ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003069 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003070 ArrayRef<SourceLocation> IdentifierLocs) {
3071 void *Mem = C.Allocate(sizeof(ImportDecl) +
3072 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003073 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003074}
3075
3076ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003077 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003078 Module *Imported,
3079 SourceLocation EndLoc) {
3080 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003081 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003082 Import->setImplicit();
3083 return Import;
3084}
3085
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003086ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3087 unsigned NumLocations) {
3088 void *Mem = AllocateDeserializedDecl(C, ID,
3089 (sizeof(ImportDecl) +
3090 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003091 return new (Mem) ImportDecl(EmptyShell());
3092}
3093
3094ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3095 if (!ImportedAndComplete.getInt())
3096 return ArrayRef<SourceLocation>();
3097
3098 const SourceLocation *StoredLocs
3099 = reinterpret_cast<const SourceLocation *>(this + 1);
3100 return ArrayRef<SourceLocation>(StoredLocs,
3101 getNumModuleIdentifiers(getImportedModule()));
3102}
3103
3104SourceRange ImportDecl::getSourceRange() const {
3105 if (!ImportedAndComplete.getInt())
3106 return SourceRange(getLocation(),
3107 *reinterpret_cast<const SourceLocation *>(this + 1));
3108
3109 return SourceRange(getLocation(), getIdentifierLocs().back());
3110}