blob: 74abbaa492de659efd50ba58700f76c74e88bc74 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000027#include "clang/Basic/Module.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000028#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000029#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000031
David Blaikie4278c652011-09-21 18:16:56 +000032#include <algorithm>
33
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattnerd3b90652008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor4421d2b2011-03-26 12:10:19 +000040static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
41 // If this declaration has an explicit visibility attribute, use it.
42 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
43 switch (A->getVisibility()) {
44 case VisibilityAttr::Default:
45 return DefaultVisibility;
46 case VisibilityAttr::Hidden:
47 return HiddenVisibility;
48 case VisibilityAttr::Protected:
49 return ProtectedVisibility;
50 }
John McCall1fb0caa2010-10-22 21:05:15 +000051 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +000052
53 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
54 // implies visibility(default).
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +000056 for (specific_attr_iterator<AvailabilityAttr>
57 A = D->specific_attr_begin<AvailabilityAttr>(),
58 AEnd = D->specific_attr_end<AvailabilityAttr>();
59 A != AEnd; ++A)
60 if ((*A)->getPlatform()->getName().equals("macosx"))
61 return DefaultVisibility;
62 }
63
64 return llvm::Optional<Visibility>();
John McCall1fb0caa2010-10-22 21:05:15 +000065}
66
John McCallaf146032010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallaf146032010-10-30 11:50:40 +000068
Rafael Espindola093ecc92012-01-14 00:30:36 +000069static LinkageInfo getLVForType(QualType T) {
70 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
71 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
72}
73
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000074/// \brief Get the most restrictive linkage for the types in the given
75/// template parameter list.
Rafael Espindola093ecc92012-01-14 00:30:36 +000076static LinkageInfo
John McCall1fb0caa2010-10-22 21:05:15 +000077getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola093ecc92012-01-14 00:30:36 +000078 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000079 for (TemplateParameterList::const_iterator P = Params->begin(),
80 PEnd = Params->end();
81 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +000082 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
83 if (NTTP->isExpandedParameterPack()) {
84 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
85 QualType T = NTTP->getExpansionType(I);
86 if (!T->isDependentType())
Rafael Espindola093ecc92012-01-14 00:30:36 +000087 LV.merge(getLVForType(T));
Douglas Gregor6952f1e2011-01-19 20:10:05 +000088 }
89 continue;
90 }
Rafael Espindolab5d763d2012-01-02 06:26:22 +000091
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000092 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +000093 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000094 continue;
95 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +000096 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000097
98 if (TemplateTemplateParmDecl *TTP
99 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000100 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000101 }
102 }
103
John McCall1fb0caa2010-10-22 21:05:15 +0000104 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000105}
106
Douglas Gregor381d34e2010-12-06 18:36:25 +0000107/// getLVForDecl - Get the linkage and visibility for the given declaration.
Rafael Espindola1266b612012-04-21 23:28:21 +0000108static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000109
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000110/// \brief Get the most restrictive linkage for the types and
111/// declarations in the given template argument list.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000112static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
113 unsigned NumArgs,
Rafael Espindola1266b612012-04-21 23:28:21 +0000114 bool OnlyTemplate) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000115 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000116
117 for (unsigned I = 0; I != NumArgs; ++I) {
118 switch (Args[I].getKind()) {
119 case TemplateArgument::Null:
120 case TemplateArgument::Integral:
121 case TemplateArgument::Expression:
122 break;
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000123
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000124 case TemplateArgument::Type:
Rafael Espindola923b0c92012-04-23 17:51:55 +0000125 LV.mergeWithMin(getLVForType(Args[I].getAsType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000126 break;
127
128 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +0000129 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
130 LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate));
131 break;
132
133 case TemplateArgument::NullPtr:
134 LV.mergeWithMin(getLVForType(Args[I].getNullPtrType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000135 break;
136
137 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000138 case TemplateArgument::TemplateExpansion:
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000139 if (TemplateDecl *Template
Douglas Gregora7fc9012011-01-05 18:58:31 +0000140 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola923b0c92012-04-23 17:51:55 +0000141 LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000142 break;
143
144 case TemplateArgument::Pack:
Rafael Espindola860097c2012-02-23 04:17:32 +0000145 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
146 Args[I].pack_size(),
Rafael Espindola1266b612012-04-21 23:28:21 +0000147 OnlyTemplate));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000148 break;
149 }
150 }
151
John McCall1fb0caa2010-10-22 21:05:15 +0000152 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000153}
154
Rafael Espindola093ecc92012-01-14 00:30:36 +0000155static LinkageInfo
Douglas Gregor381d34e2010-12-06 18:36:25 +0000156getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola1266b612012-04-21 23:28:21 +0000157 bool OnlyTemplate) {
158 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall3cdfc4d2010-08-13 08:35:10 +0000159}
160
Rafael Espindola9db614f2012-05-25 16:41:35 +0000161static bool shouldConsiderTemplateVis(const FunctionDecl *fn,
Rafael Espindolacae1c622012-05-21 20:31:27 +0000162 const FunctionTemplateSpecializationInfo *spec) {
163 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCall6ce51ee2011-06-27 23:06:04 +0000164}
165
Rafael Espindolaad359be2012-05-25 14:47:05 +0000166static bool
167shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola0b0ad0a2012-05-21 20:15:56 +0000168 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCall6ce51ee2011-06-27 23:06:04 +0000169}
170
Rafael Espindolab04b7312012-07-13 14:25:36 +0000171static bool useInlineVisibilityHidden(const NamedDecl *D) {
172 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola0bab9da2012-07-13 23:26:43 +0000173 const LangOptions &Opts = D->getASTContext().getLangOpts();
174 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolab04b7312012-07-13 14:25:36 +0000175 return false;
176
177 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
178 if (!FD)
179 return false;
180
181 TemplateSpecializationKind TSK = TSK_Undeclared;
182 if (FunctionTemplateSpecializationInfo *spec
183 = FD->getTemplateSpecializationInfo()) {
184 TSK = spec->getTemplateSpecializationKind();
185 } else if (MemberSpecializationInfo *MSI =
186 FD->getMemberSpecializationInfo()) {
187 TSK = MSI->getTemplateSpecializationKind();
188 }
189
190 const FunctionDecl *Def = 0;
191 // InlineVisibilityHidden only applies to definitions, and
192 // isInlined() only gives meaningful answers on definitions
193 // anyway.
194 return TSK != TSK_ExplicitInstantiationDeclaration &&
195 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola0142f0c2012-10-11 16:32:25 +0000196 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolab04b7312012-07-13 14:25:36 +0000197}
198
Rafael Espindola1266b612012-04-21 23:28:21 +0000199static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
200 bool OnlyTemplate) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000201 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000202 "Not a name having namespace scope");
203 ASTContext &Context = D->getASTContext();
204
205 // C++ [basic.link]p3:
206 // A name having namespace scope (3.3.6) has internal linkage if it
207 // is the name of
208 // - an object, reference, function or function template that is
209 // explicitly declared static; or,
210 // (This bullet corresponds to C99 6.2.2p3.)
211 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
212 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000213 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000214 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000215
Richard Smith820e9a72012-10-19 06:37:48 +0000216 // - a non-volatile object or reference that is explicitly declared const
217 // or constexpr and neither explicitly declared extern nor previously
218 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikie4e4d0842012-03-11 07:00:24 +0000219 if (Context.getLangOpts().CPlusPlus &&
Richard Smith820e9a72012-10-19 06:37:48 +0000220 Var->getType().isConstQualified() &&
221 !Var->getType().isVolatileQualified() &&
John McCalld931b082010-08-26 03:08:43 +0000222 Var->getStorageClass() != SC_Extern &&
223 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000224 bool FoundExtern = false;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000225 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000226 PrevVar && !FoundExtern;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000227 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000228 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000229 FoundExtern = true;
230
231 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000232 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000233 }
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000234 if (Var->getStorageClass() == SC_None) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000235 const VarDecl *PrevVar = Var->getPreviousDecl();
236 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000237 if (PrevVar->getStorageClass() == SC_PrivateExtern)
238 break;
Eli Friedman8c7a1852012-10-26 23:05:34 +0000239 if (PrevVar)
240 return PrevVar->getLinkageAndVisibility();
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000241 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000242 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000243 // C++ [temp]p4:
244 // A non-member function template can have internal linkage; any
245 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000246 const FunctionDecl *Function = 0;
247 if (const FunctionTemplateDecl *FunTmpl
248 = dyn_cast<FunctionTemplateDecl>(D))
249 Function = FunTmpl->getTemplatedDecl();
250 else
251 Function = cast<FunctionDecl>(D);
252
253 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000254 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000255 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000256 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
257 // - a data member of an anonymous union.
258 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000259 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000260 }
261
Chandler Carruth094b6432011-02-24 19:03:39 +0000262 if (D->isInAnonymousNamespace()) {
263 const VarDecl *Var = dyn_cast<VarDecl>(D);
264 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman750dc2b2012-01-15 01:23:58 +0000265 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
266 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth094b6432011-02-24 19:03:39 +0000267 return LinkageInfo::uniqueExternal();
268 }
John McCalle7bc9722010-10-28 04:18:25 +0000269
John McCall1fb0caa2010-10-22 21:05:15 +0000270 // Set up the defaults.
271
272 // C99 6.2.2p5:
273 // If the declaration of an identifier for an object has file
274 // scope and no storage-class specifier, its linkage is
275 // external.
John McCallaf146032010-10-30 11:50:40 +0000276 LinkageInfo LV;
277
Rafael Espindola1266b612012-04-21 23:28:21 +0000278 if (!OnlyTemplate) {
Rafael Espindolae9836a22012-04-16 18:46:26 +0000279 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000280 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000281 } else {
282 // If we're declared in a namespace with a visibility attribute,
283 // use that namespace's visibility, but don't call it explicit.
284 for (const DeclContext *DC = D->getDeclContext();
285 !isa<TranslationUnitDecl>(DC);
286 DC = DC->getParent()) {
287 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
288 if (!ND) continue;
289 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000290 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000291 break;
292 }
293 }
294 }
295 }
296
Rafael Espindolab04b7312012-07-13 14:25:36 +0000297 if (!OnlyTemplate) {
Rafael Espindola4fc14902012-04-19 04:37:16 +0000298 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolab04b7312012-07-13 14:25:36 +0000299 // If we're paying attention to global visibility, apply
300 // -finline-visibility-hidden if this is an inline method.
301 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
302 LV.mergeVisibility(HiddenVisibility, true);
303 }
Rafael Espindolaff257982012-04-19 02:55:01 +0000304
Douglas Gregord85b5b92009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000306
Douglas Gregord85b5b92009-11-25 22:24:25 +0000307 // A name having namespace scope has external linkage if it is the
308 // name of
309 //
310 // - an object or reference, unless it has internal linkage; or
311 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall1fb0caa2010-10-22 21:05:15 +0000315 // Modify the variable's LV by the LV of its type unless this is
316 // C or extern "C". This follows from [basic.link]p9:
317 // A type without linkage shall not be used as the type of a
318 // variable or function with external linkage unless
319 // - the entity has C language linkage, or
320 // - the entity is declared within an unnamed namespace, or
321 // - the entity is not used or is defined in the same
322 // translation unit.
323 // and [basic.link]p10:
324 // ...the types specified by all declarations referring to a
325 // given variable or function shall be identical...
326 // C does not have an equivalent rule.
327 //
John McCallac65c622010-10-26 04:59:26 +0000328 // Ignore this if we've got an explicit attribute; the user
329 // probably knows what they're doing.
330 //
John McCall1fb0caa2010-10-22 21:05:15 +0000331 // Note that we don't want to make the variable non-external
332 // because of this, but unique-external linkage suits us.
David Blaikie4e4d0842012-03-11 07:00:24 +0000333 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000334 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000335 LinkageInfo TypeLV = getLVForType(Var->getType());
336 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000337 return LinkageInfo::uniqueExternal();
Rafael Espindolad70d20a2012-04-19 05:24:05 +0000338 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000339 }
340
John McCall35cebc32010-11-02 18:38:13 +0000341 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000342 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000343
Rafael Espindola538fb982012-11-12 04:10:23 +0000344 // Note that Sema::MergeVarDecl already takes care of implementing
345 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
346 // to do it here.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000347
Douglas Gregord85b5b92009-11-25 22:24:25 +0000348 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000349 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000350 // In theory, we can modify the function's LV by the LV of its
351 // type unless it has C linkage (see comment above about variables
352 // for justification). In practice, GCC doesn't do this, so it's
353 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000354
John McCall35cebc32010-11-02 18:38:13 +0000355 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000356 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000357
Douglas Gregord85b5b92009-11-25 22:24:25 +0000358 // C99 6.2.2p5:
359 // If the declaration of an identifier for a function has no
360 // storage-class specifier, its linkage is determined exactly
361 // as if it were declared with the storage-class specifier
362 // extern.
David Blaikie4e4d0842012-03-11 07:00:24 +0000363 if (!Context.getLangOpts().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000364 (Function->getStorageClass() == SC_Extern ||
365 Function->getStorageClass() == SC_PrivateExtern ||
366 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000367 // C99 6.2.2p4:
368 // For an identifier declared with the storage-class specifier
369 // extern in a scope in which a prior declaration of that
370 // identifier is visible, if the prior declaration specifies
371 // internal or external linkage, the linkage of the identifier
372 // at the later declaration is the same as the linkage
373 // specified at the prior declaration. If no prior declaration
374 // is visible, or if the prior declaration specifies no
375 // linkage, then the identifier has external linkage.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000376 if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000377 LinkageInfo PrevLV = getLVForDecl(PrevFunc, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000378 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
379 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000380 }
381 }
382
John McCallaf8ca372011-02-10 06:50:24 +0000383 // In C++, then if the type of the function uses a type with
384 // unique-external linkage, it's not legally usable from outside
385 // this translation unit. However, we should use the C linkage
386 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000387 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000388 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000389 Function->getType()->getLinkage() == UniqueExternalLinkage)
390 return LinkageInfo::uniqueExternal();
391
John McCall6ce51ee2011-06-27 23:06:04 +0000392 // Consider LV from the template and the template arguments unless
393 // this is an explicit specialization with a visibility attribute.
394 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000395 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola9db614f2012-05-25 16:41:35 +0000396 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
397 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
398 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
399 OnlyTemplate);
400 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000401 LV.mergeWithMin(TempLV);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000402 LV.mergeWithMin(ArgsLV);
403 } else {
404 LV.mergeLinkage(TempLV);
405 LV.mergeLinkage(ArgsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000406 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000407 }
408
Douglas Gregord85b5b92009-11-25 22:24:25 +0000409 // - a named class (Clause 9), or an unnamed class defined in a
410 // typedef declaration in which the class has the typedef name
411 // for linkage purposes (7.1.3); or
412 // - a named enumeration (7.2), or an unnamed enumeration
413 // defined in a typedef declaration in which the enumeration
414 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000415 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
416 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000417 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000418 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000419
John McCall1fb0caa2010-10-22 21:05:15 +0000420 // If this is a class template specialization, consider the
421 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000422 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000423 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindolaad359be2012-05-25 14:47:05 +0000424 // From the template.
425 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000426
Rafael Espindolaad359be2012-05-25 14:47:05 +0000427 // The arguments at which the template was instantiated.
428 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
429 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
430 OnlyTemplate);
431 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000432 LV.mergeWithMin(TempLV);
Rafael Espindolaad359be2012-05-25 14:47:05 +0000433 LV.mergeWithMin(ArgsLV);
434 } else {
435 LV.mergeLinkage(TempLV);
436 LV.mergeLinkage(ArgsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000437 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000438 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000439
440 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000441 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000442 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
443 OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000444 if (!isExternalLinkage(EnumLV.linkage()))
445 return LinkageInfo::none();
446 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000447
448 // - a template, unless it is a function template that has
449 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000450 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola60115a02012-04-22 00:43:48 +0000451 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregord85b5b92009-11-25 22:24:25 +0000452 // - a namespace (7.3), unless it is declared within an unnamed
453 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000454 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
455 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000456
John McCall1fb0caa2010-10-22 21:05:15 +0000457 // By extension, we assign external linkage to Objective-C
458 // interfaces.
459 } else if (isa<ObjCInterfaceDecl>(D)) {
460 // fallout
461
462 // Everything not covered here has no linkage.
463 } else {
John McCallaf146032010-10-30 11:50:40 +0000464 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000465 }
466
467 // If we ended up with non-external linkage, visibility should
468 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000469 if (LV.linkage() != ExternalLinkage)
470 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000471
John McCall1fb0caa2010-10-22 21:05:15 +0000472 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000473}
474
Rafael Espindola1266b612012-04-21 23:28:21 +0000475static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall1fb0caa2010-10-22 21:05:15 +0000476 // Only certain class members have linkage. Note that fields don't
477 // really have linkage, but it's convenient to say they do for the
478 // purposes of calculating linkage of pointer-to-data-member
479 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000480 if (!(isa<CXXMethodDecl>(D) ||
481 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000482 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000483 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000484 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000485 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000486
John McCall36987482010-11-02 01:45:15 +0000487 LinkageInfo LV;
488
John McCall36987482010-11-02 01:45:15 +0000489 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola1266b612012-04-21 23:28:21 +0000490 if (!OnlyTemplate) {
Rafael Espindola41574542012-04-19 04:27:47 +0000491 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000492 LV.mergeVisibility(*Vis, true);
Rafael Espindolab04b7312012-07-13 14:25:36 +0000493 // If we're paying attention to global visibility, apply
494 // -finline-visibility-hidden if this is an inline method.
495 //
496 // Note that we do this before merging information about
497 // the class visibility.
498 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
499 LV.mergeVisibility(HiddenVisibility, true);
John McCall36987482010-11-02 01:45:15 +0000500 }
Rafael Espindolac7e60602012-04-19 05:50:08 +0000501
502 // If this class member has an explicit visibility attribute, the only
503 // thing that can change its visibility is the template arguments, so
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +0000504 // only look for them when processing the class.
Rafael Espindola1266b612012-04-21 23:28:21 +0000505 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola0f905902012-04-16 18:25:01 +0000506
Rafael Espindolac7e60602012-04-19 05:50:08 +0000507 // If this member has an visibility attribute, ClassF will exclude
508 // attributes on the class or command line options, keeping only information
509 // about the template instantiation. If the member has no visibility
510 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
511 // produces the desired result.
Rafael Espindola1266b612012-04-21 23:28:21 +0000512 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
513 ClassOnlyTemplate));
John McCall36987482010-11-02 01:45:15 +0000514 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000515 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000516
517 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000518 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000519 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000520
Rafael Espindola1266b612012-04-21 23:28:21 +0000521 if (!OnlyTemplate)
Rafael Espindola4fc14902012-04-19 04:37:16 +0000522 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaff257982012-04-19 02:55:01 +0000523
John McCall3cdfc4d2010-08-13 08:35:10 +0000524 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000525 // If the type of the function uses a type with unique-external
526 // linkage, it's not legally usable from outside this translation unit.
527 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
528 return LinkageInfo::uniqueExternal();
529
John McCall1fb0caa2010-10-22 21:05:15 +0000530 // If this is a method template specialization, use the linkage for
531 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000532 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000533 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000534 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
535 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
536 OnlyTemplate);
537 TemplateParameterList *TemplateParams =
538 spec->getTemplate()->getTemplateParameters();
539 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000540 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000541 LV.mergeWithMin(ArgsLV);
Rafael Espindola1266b612012-04-21 23:28:21 +0000542 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000543 LV.mergeWithMin(ParamsLV);
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000544 } else {
545 LV.mergeLinkage(ArgsLV);
546 if (!OnlyTemplate)
547 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000548 }
John McCall66cbcf32010-11-01 01:29:57 +0000549 }
John McCall1fb0caa2010-10-22 21:05:15 +0000550
John McCall110e8e52010-10-29 22:22:43 +0000551 // Note that in contrast to basically every other situation, we
552 // *do* apply -fvisibility to method declarations.
553
554 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000555 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000556 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000557 // Merge template argument/parameter information for member
558 // class template specializations.
559 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
560 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
561 OnlyTemplate);
562 TemplateParameterList *TemplateParams =
563 spec->getSpecializedTemplate()->getTemplateParameters();
564 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindolaad359be2012-05-25 14:47:05 +0000565 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000566 LV.mergeWithMin(ArgsLV);
Rafael Espindola59073bb2012-05-25 14:17:45 +0000567 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000568 LV.mergeWithMin(ParamsLV);
Rafael Espindola20831e22012-05-25 15:51:26 +0000569 } else {
570 LV.mergeLinkage(ArgsLV);
571 if (!OnlyTemplate)
572 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000573 }
John McCall110e8e52010-10-29 22:22:43 +0000574 }
575
John McCall110e8e52010-10-29 22:22:43 +0000576 // Static data members.
577 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000578 // Modify the variable's linkage by its type, but ignore the
579 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000580 LinkageInfo TypeLV = getLVForType(VD->getType());
581 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000582 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindolac7e60602012-04-19 05:50:08 +0000583 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000584 }
585
John McCall1fb0caa2010-10-22 21:05:15 +0000586 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000587}
588
John McCallf76b0922011-02-08 19:01:05 +0000589static void clearLinkageForClass(const CXXRecordDecl *record) {
590 for (CXXRecordDecl::decl_iterator
591 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
592 Decl *child = *i;
593 if (isa<NamedDecl>(child))
594 cast<NamedDecl>(child)->ClearLinkageCache();
595 }
596}
597
David Blaikie99ba9e32011-12-20 02:48:34 +0000598void NamedDecl::anchor() { }
599
John McCallf76b0922011-02-08 19:01:05 +0000600void NamedDecl::ClearLinkageCache() {
601 // Note that we can't skip clearing the linkage of children just
602 // because the parent doesn't have cached linkage: we don't cache
603 // when computing linkage for parent contexts.
604
605 HasCachedLinkage = 0;
606
607 // If we're changing the linkage of a class, we need to reset the
608 // linkage of child declarations, too.
609 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
610 clearLinkageForClass(record);
611
John McCall15e310a2011-02-19 02:53:41 +0000612 if (ClassTemplateDecl *temp =
613 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000614 // Clear linkage for the template pattern.
615 CXXRecordDecl *record = temp->getTemplatedDecl();
616 record->HasCachedLinkage = 0;
617 clearLinkageForClass(record);
618
John McCall15e310a2011-02-19 02:53:41 +0000619 // We need to clear linkage for specializations, too.
620 for (ClassTemplateDecl::spec_iterator
621 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
622 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000623 }
John McCall15e310a2011-02-19 02:53:41 +0000624
625 // Clear cached linkage for function template decls, too.
626 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000627 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
628 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000629 for (FunctionTemplateDecl::spec_iterator
630 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
631 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000632 }
John McCall15e310a2011-02-19 02:53:41 +0000633
John McCallf76b0922011-02-08 19:01:05 +0000634}
635
Douglas Gregor381d34e2010-12-06 18:36:25 +0000636Linkage NamedDecl::getLinkage() const {
637 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000638 assert(Linkage(CachedLinkage) ==
Rafael Espindola1266b612012-04-21 23:28:21 +0000639 getLVForDecl(this, true).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000640 return Linkage(CachedLinkage);
641 }
642
Rafael Espindola1266b612012-04-21 23:28:21 +0000643 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000644 HasCachedLinkage = 1;
645 return Linkage(CachedLinkage);
646}
647
John McCallaf146032010-10-30 11:50:40 +0000648LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola1266b612012-04-21 23:28:21 +0000649 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000650 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000651 HasCachedLinkage = 1;
652 CachedLinkage = LI.linkage();
653 return LI;
John McCall0df95872010-10-29 00:29:13 +0000654}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000655
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000656llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
657 // Use the most recent declaration of a variable.
Rafael Espindola797105a2012-05-16 02:10:38 +0000658 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolac6b82c32012-11-12 04:32:23 +0000659 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola797105a2012-05-16 02:10:38 +0000660 return V;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000661
Rafael Espindola797105a2012-05-16 02:10:38 +0000662 if (Var->isStaticDataMember()) {
663 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
664 if (InstantiatedFrom)
665 return getVisibilityOf(InstantiatedFrom);
666 }
667
668 return llvm::Optional<Visibility>();
669 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000670 // Use the most recent declaration of a function, and also handle
671 // function template specializations.
672 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolac6b82c32012-11-12 04:32:23 +0000673 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000674 return V;
675
676 // If the function is a specialization of a template with an
677 // explicit visibility attribute, use that.
678 if (FunctionTemplateSpecializationInfo *templateInfo
679 = fn->getTemplateSpecializationInfo())
680 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
681
Rafael Espindola860097c2012-02-23 04:17:32 +0000682 // If the function is a member of a specialization of a class template
683 // and the corresponding decl has explicit visibility, use that.
684 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
685 if (InstantiatedFrom)
686 return getVisibilityOf(InstantiatedFrom);
687
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000688 return llvm::Optional<Visibility>();
689 }
690
691 // Otherwise, just check the declaration itself first.
692 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
693 return V;
694
Rafael Espindola98499012012-07-31 19:02:02 +0000695 // The visibility of a template is stored in the templated decl.
696 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
697 return getVisibilityOf(TD->getTemplatedDecl());
698
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000699 // If there wasn't explicit visibility there, and this is a
700 // specialization of a class template, check for visibility
701 // on the pattern.
702 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolad3d02dd2012-07-13 01:19:08 +0000703 = dyn_cast<ClassTemplateSpecializationDecl>(this))
704 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000705
Rafael Espindola860097c2012-02-23 04:17:32 +0000706 // If this is a member class of a specialization of a class template
707 // and the corresponding decl has explicit visibility, use that.
708 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
709 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
710 if (InstantiatedFrom)
711 return getVisibilityOf(InstantiatedFrom);
712 }
713
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000714 return llvm::Optional<Visibility>();
715}
716
Rafael Espindola1266b612012-04-21 23:28:21 +0000717static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000718 // Objective-C: treat all Objective-C declarations as having external
719 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000720 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000721 default:
722 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000723 case Decl::ParmVar:
724 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000725 case Decl::TemplateTemplateParm: // count these as external
726 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000727 case Decl::ObjCAtDefsField:
728 case Decl::ObjCCategory:
729 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000730 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000731 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000732 case Decl::ObjCMethod:
733 case Decl::ObjCProperty:
734 case Decl::ObjCPropertyImpl:
735 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000736 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000737
738 case Decl::CXXRecord: {
739 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
740 if (Record->isLambda()) {
741 if (!Record->getLambdaManglingNumber()) {
742 // This lambda has no mangling number, so it's internal.
743 return LinkageInfo::internal();
744 }
745
746 // This lambda has its linkage/visibility determined by its owner.
747 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
748 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
749 if (isa<ParmVarDecl>(ContextDecl))
750 DC = ContextDecl->getDeclContext()->getRedeclContext();
751 else
Rafael Espindola1266b612012-04-21 23:28:21 +0000752 return getLVForDecl(cast<NamedDecl>(ContextDecl),
753 OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000754 }
755
756 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola1266b612012-04-21 23:28:21 +0000757 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000758
759 return LinkageInfo::external();
760 }
761
762 break;
763 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000764 }
765
Douglas Gregord85b5b92009-11-25 22:24:25 +0000766 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000767 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola1266b612012-04-21 23:28:21 +0000768 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000769
770 // C++ [basic.link]p5:
771 // In addition, a member function, static data member, a named
772 // class or enumeration of class scope, or an unnamed class or
773 // enumeration defined in a class-scope typedef declaration such
774 // that the class or enumeration has the typedef name for linkage
775 // purposes (7.1.3), has external linkage if the name of the class
776 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000777 if (D->getDeclContext()->isRecord())
Rafael Espindola1266b612012-04-21 23:28:21 +0000778 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000779
780 // C++ [basic.link]p6:
781 // The name of a function declared in block scope and the name of
782 // an object declared by a block scope extern declaration have
783 // linkage. If there is a visible declaration of an entity with
784 // linkage having the same name and type, ignoring entities
785 // declared outside the innermost enclosing namespace scope, the
786 // block scope declaration declares that same entity and receives
787 // the linkage of the previous declaration. If there is more than
788 // one such matching entity, the program is ill-formed. Otherwise,
789 // if no matching entity is found, the block scope entity receives
790 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000791 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
792 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000793 if (Function->isInAnonymousNamespace() &&
794 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000795 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000796
John McCallaf146032010-10-30 11:50:40 +0000797 LinkageInfo LV;
Rafael Espindola1266b612012-04-21 23:28:21 +0000798 if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000799 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000800 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000801 }
802
Douglas Gregoref96ee02012-01-14 16:38:05 +0000803 if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000804 LinkageInfo PrevLV = getLVForDecl(Prev, OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000805 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
806 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000807 }
808
809 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000810 }
811
John McCall0df95872010-10-29 00:29:13 +0000812 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000813 if (Var->getStorageClass() == SC_Extern ||
814 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000815 if (Var->isInAnonymousNamespace() &&
816 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000817 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000818
John McCallaf146032010-10-30 11:50:40 +0000819 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000820 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000821 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola1266b612012-04-21 23:28:21 +0000822 else if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000823 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000824 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000825 }
John McCall1fb0caa2010-10-22 21:05:15 +0000826
Rafael Espindola538fb982012-11-12 04:10:23 +0000827 // Note that Sema::MergeVarDecl already takes care of implementing
828 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
829 // have to do it here.
John McCall1fb0caa2010-10-22 21:05:15 +0000830 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000831 }
832 }
833
834 // C++ [basic.link]p6:
835 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000836 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000837}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000838
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000839std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000840 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000841}
842
843std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000844 const DeclContext *Ctx = getDeclContext();
845
846 if (Ctx->isFunctionOrMethod())
847 return getNameAsString();
848
Chris Lattner5f9e2722011-07-23 10:55:15 +0000849 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000850 ContextsTy Contexts;
851
852 // Collect contexts.
853 while (Ctx && isa<NamedDecl>(Ctx)) {
854 Contexts.push_back(Ctx);
855 Ctx = Ctx->getParent();
856 };
857
858 std::string QualName;
859 llvm::raw_string_ostream OS(QualName);
860
861 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
862 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000863 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000864 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000865 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
866 std::string TemplateArgsStr
867 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000868 TemplateArgs.data(),
869 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000870 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000871 OS << Spec->getName() << TemplateArgsStr;
872 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000873 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000874 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000875 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000876 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000877 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
878 if (!RD->getIdentifier())
879 OS << "<anonymous " << RD->getKindName() << '>';
880 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000881 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000882 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000883 const FunctionProtoType *FT = 0;
884 if (FD->hasWrittenPrototype())
Eli Friedman482466b2012-08-30 22:22:09 +0000885 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinig3521d012009-12-28 03:19:38 +0000886
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000887 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000888 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000889 unsigned NumParams = FD->getNumParams();
890 for (unsigned i = 0; i < NumParams; ++i) {
891 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000892 OS << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000893 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinig3521d012009-12-28 03:19:38 +0000894 }
895
896 if (FT->isVariadic()) {
897 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000898 OS << ", ";
899 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000900 }
901 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000902 OS << ')';
903 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000904 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000905 }
906 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000907 }
908
John McCall8472af42010-03-16 21:48:18 +0000909 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000910 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000911 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000912 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000913
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000914 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000915}
916
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000917bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000918 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
919
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000920 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
921 // We want to keep it, unless it nominates same namespace.
922 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000923 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
924 ->getOriginalNamespace() ==
925 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
926 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000929 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
930 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000931 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000932
Douglas Gregore53060f2009-06-25 22:08:12 +0000933 // For function templates, the underlying function declarations are linked.
934 if (const FunctionTemplateDecl *FunctionTemplate
935 = dyn_cast<FunctionTemplateDecl>(this))
936 if (const FunctionTemplateDecl *OldFunctionTemplate
937 = dyn_cast<FunctionTemplateDecl>(OldD))
938 return FunctionTemplate->getTemplatedDecl()
939 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Steve Naroff0de21fd2009-02-22 19:35:57 +0000941 // For method declarations, we keep track of redeclarations.
942 if (isa<ObjCMethodDecl>(this))
943 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000944
John McCallf36e02d2009-10-09 21:13:30 +0000945 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
946 return true;
947
John McCall9488ea12009-11-17 05:59:44 +0000948 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
949 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
950 cast<UsingShadowDecl>(OldD)->getTargetDecl();
951
Douglas Gregordc355712011-02-25 00:36:19 +0000952 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
953 ASTContext &Context = getASTContext();
954 return Context.getCanonicalNestedNameSpecifier(
955 cast<UsingDecl>(this)->getQualifier()) ==
956 Context.getCanonicalNestedNameSpecifier(
957 cast<UsingDecl>(OldD)->getQualifier());
958 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000959
Douglas Gregor7a537402012-01-03 23:26:26 +0000960 // A typedef of an Objective-C class type can replace an Objective-C class
961 // declaration or definition, and vice versa.
962 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
963 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
964 return true;
965
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000966 // For non-function declarations, if the declarations are of the
967 // same kind then this must be a redeclaration, or semantic analysis
968 // would not have given us the new declaration.
969 return this->getKind() == OldD->getKind();
970}
971
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000972bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000973 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000974}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000975
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000976NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000977 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000978 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
979 ND = UD->getTargetDecl();
980
981 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
982 return AD->getClassInterface();
983
984 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +0000985}
986
John McCall161755a2010-04-06 21:38:20 +0000987bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000988 if (!isCXXClassMember())
989 return false;
990
John McCall161755a2010-04-06 21:38:20 +0000991 const NamedDecl *D = this;
992 if (isa<UsingShadowDecl>(D))
993 D = cast<UsingShadowDecl>(D)->getTargetDecl();
994
Francois Pichet87c2e122010-11-21 06:08:52 +0000995 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000996 return true;
997 if (isa<CXXMethodDecl>(D))
998 return cast<CXXMethodDecl>(D)->isInstance();
999 if (isa<FunctionTemplateDecl>(D))
1000 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1001 ->getTemplatedDecl())->isInstance();
1002 return false;
1003}
1004
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +00001005//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001006// DeclaratorDecl Implementation
1007//===----------------------------------------------------------------------===//
1008
Douglas Gregor1693e152010-07-06 18:42:40 +00001009template <typename DeclT>
1010static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1011 if (decl->getNumTemplateParameterLists() > 0)
1012 return decl->getTemplateParameterList(0)->getTemplateLoc();
1013 else
1014 return decl->getInnerLocStart();
1015}
1016
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001017SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001018 TypeSourceInfo *TSI = getTypeSourceInfo();
1019 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001020 return SourceLocation();
1021}
1022
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001023void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1024 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001025 // Make sure the extended decl info is allocated.
1026 if (!hasExtInfo()) {
1027 // Save (non-extended) type source info pointer.
1028 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1029 // Allocate external info struct.
1030 DeclInfo = new (getASTContext()) ExtInfo;
1031 // Restore savedTInfo into (extended) decl info.
1032 getExtInfo()->TInfo = savedTInfo;
1033 }
1034 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001035 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001036 } else {
John McCallb6217662010-03-15 10:12:16 +00001037 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001038 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001039 if (getExtInfo()->NumTemplParamLists == 0) {
1040 // Save type source info pointer.
1041 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1042 // Deallocate the extended decl info.
1043 getASTContext().Deallocate(getExtInfo());
1044 // Restore savedTInfo into (non-extended) decl info.
1045 DeclInfo = savedTInfo;
1046 }
1047 else
1048 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001049 }
1050 }
1051}
1052
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001053void
1054DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1055 unsigned NumTPLists,
1056 TemplateParameterList **TPLists) {
1057 assert(NumTPLists > 0);
1058 // Make sure the extended decl info is allocated.
1059 if (!hasExtInfo()) {
1060 // Save (non-extended) type source info pointer.
1061 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1062 // Allocate external info struct.
1063 DeclInfo = new (getASTContext()) ExtInfo;
1064 // Restore savedTInfo into (extended) decl info.
1065 getExtInfo()->TInfo = savedTInfo;
1066 }
1067 // Set the template parameter lists info.
1068 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1069}
1070
Douglas Gregor1693e152010-07-06 18:42:40 +00001071SourceLocation DeclaratorDecl::getOuterLocStart() const {
1072 return getTemplateOrInnerLocStart(this);
1073}
1074
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001075namespace {
1076
1077// Helper function: returns true if QT is or contains a type
1078// having a postfix component.
1079bool typeIsPostfix(clang::QualType QT) {
1080 while (true) {
1081 const Type* T = QT.getTypePtr();
1082 switch (T->getTypeClass()) {
1083 default:
1084 return false;
1085 case Type::Pointer:
1086 QT = cast<PointerType>(T)->getPointeeType();
1087 break;
1088 case Type::BlockPointer:
1089 QT = cast<BlockPointerType>(T)->getPointeeType();
1090 break;
1091 case Type::MemberPointer:
1092 QT = cast<MemberPointerType>(T)->getPointeeType();
1093 break;
1094 case Type::LValueReference:
1095 case Type::RValueReference:
1096 QT = cast<ReferenceType>(T)->getPointeeType();
1097 break;
1098 case Type::PackExpansion:
1099 QT = cast<PackExpansionType>(T)->getPattern();
1100 break;
1101 case Type::Paren:
1102 case Type::ConstantArray:
1103 case Type::DependentSizedArray:
1104 case Type::IncompleteArray:
1105 case Type::VariableArray:
1106 case Type::FunctionProto:
1107 case Type::FunctionNoProto:
1108 return true;
1109 }
1110 }
1111}
1112
1113} // namespace
1114
1115SourceRange DeclaratorDecl::getSourceRange() const {
1116 SourceLocation RangeEnd = getLocation();
1117 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1118 if (typeIsPostfix(TInfo->getType()))
1119 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1120 }
1121 return SourceRange(getOuterLocStart(), RangeEnd);
1122}
1123
Abramo Bagnara9b934882010-06-12 08:15:14 +00001124void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001125QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1126 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001127 TemplateParameterList **TPLists) {
1128 assert((NumTPLists == 0 || TPLists != 0) &&
1129 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001130
1131 // Free previous template parameters (if any).
1132 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001133 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001134 TemplParamLists = 0;
1135 NumTemplParamLists = 0;
1136 }
1137 // Set info on matched template parameter lists (if any).
1138 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001139 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001140 NumTemplParamLists = NumTPLists;
1141 for (unsigned i = NumTPLists; i-- > 0; )
1142 TemplParamLists[i] = TPLists[i];
1143 }
1144}
1145
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001146//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001147// VarDecl Implementation
1148//===----------------------------------------------------------------------===//
1149
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001150const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1151 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001152 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001153 case SC_Auto: return "auto";
1154 case SC_Extern: return "extern";
1155 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1156 case SC_PrivateExtern: return "__private_extern__";
1157 case SC_Register: return "register";
1158 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001159 }
1160
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001161 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001162}
1163
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001164VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1165 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001166 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001167 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001168 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001169}
1170
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001171VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1172 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1173 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1174 QualType(), 0, SC_None, SC_None);
1175}
1176
Douglas Gregor381d34e2010-12-06 18:36:25 +00001177void VarDecl::setStorageClass(StorageClass SC) {
1178 assert(isLegalForVariable(SC));
1179 if (getStorageClass() != SC)
1180 ClearLinkageCache();
1181
John McCallf1e4fbf2011-05-01 02:13:58 +00001182 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001183}
1184
Douglas Gregor1693e152010-07-06 18:42:40 +00001185SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisd69f31c2012-10-08 23:08:41 +00001186 if (const Expr *Init = getInit()) {
1187 SourceLocation InitEnd = Init->getLocEnd();
1188 if (InitEnd.isValid())
1189 return SourceRange(getOuterLocStart(), InitEnd);
1190 }
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001191 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001192}
1193
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001194bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001195 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001196 return false;
1197
Eli Friedman750dc2b2012-01-15 01:23:58 +00001198 const DeclContext *DC = getDeclContext();
1199 if (DC->isRecord())
1200 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001201
Eli Friedman750dc2b2012-01-15 01:23:58 +00001202 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001203 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001204 return true;
1205 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001206}
1207
1208VarDecl *VarDecl::getCanonicalDecl() {
1209 return getFirstDeclaration();
1210}
1211
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001212VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1213 ASTContext &C) const
1214{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001215 // C++ [basic.def]p2:
1216 // A declaration is a definition unless [...] it contains the 'extern'
1217 // specifier or a linkage-specification and neither an initializer [...],
1218 // it declares a static data member in a class declaration [...].
1219 // C++ [temp.expl.spec]p15:
1220 // An explicit specialization of a static data member of a template is a
1221 // definition if the declaration includes an initializer; otherwise, it is
1222 // a declaration.
1223 if (isStaticDataMember()) {
1224 if (isOutOfLine() && (hasInit() ||
1225 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1226 return Definition;
1227 else
1228 return DeclarationOnly;
1229 }
1230 // C99 6.7p5:
1231 // A definition of an identifier is a declaration for that identifier that
1232 // [...] causes storage to be reserved for that object.
1233 // Note: that applies for all non-file-scope objects.
1234 // C99 6.9.2p1:
1235 // If the declaration of an identifier for an object has file scope and an
1236 // initializer, the declaration is an external definition for the identifier
1237 if (hasInit())
1238 return Definition;
1239 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1240 if (hasExternalStorage())
1241 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001242
John McCalld931b082010-08-26 03:08:43 +00001243 if (getStorageClassAsWritten() == SC_Extern ||
1244 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001245 for (const VarDecl *PrevVar = getPreviousDecl();
1246 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001247 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1248 return DeclarationOnly;
1249 }
1250 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001251 // C99 6.9.2p2:
1252 // A declaration of an object that has file scope without an initializer,
1253 // and without a storage class specifier or the scs 'static', constitutes
1254 // a tentative definition.
1255 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001256 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001257 return TentativeDefinition;
1258
1259 // What's left is (in C, block-scope) declarations without initializers or
1260 // external storage. These are definitions.
1261 return Definition;
1262}
1263
Sebastian Redle9d12b62010-01-31 22:27:38 +00001264VarDecl *VarDecl::getActingDefinition() {
1265 DefinitionKind Kind = isThisDeclarationADefinition();
1266 if (Kind != TentativeDefinition)
1267 return 0;
1268
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001269 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001270 VarDecl *First = getFirstDeclaration();
1271 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1272 I != E; ++I) {
1273 Kind = (*I)->isThisDeclarationADefinition();
1274 if (Kind == Definition)
1275 return 0;
1276 else if (Kind == TentativeDefinition)
1277 LastTentative = *I;
1278 }
1279 return LastTentative;
1280}
1281
1282bool VarDecl::isTentativeDefinitionNow() const {
1283 DefinitionKind Kind = isThisDeclarationADefinition();
1284 if (Kind != TentativeDefinition)
1285 return false;
1286
1287 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1288 if ((*I)->isThisDeclarationADefinition() == Definition)
1289 return false;
1290 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001291 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001292}
1293
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001294VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001295 VarDecl *First = getFirstDeclaration();
1296 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1297 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001298 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001299 return *I;
1300 }
1301 return 0;
1302}
1303
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001304VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001305 DefinitionKind Kind = DeclarationOnly;
1306
1307 const VarDecl *First = getFirstDeclaration();
1308 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001309 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001310 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001311 if (Kind == Definition)
1312 break;
1313 }
John McCall110e8e52010-10-29 22:22:43 +00001314
1315 return Kind;
1316}
1317
Sebastian Redl31310a22010-02-01 20:16:42 +00001318const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001319 redecl_iterator I = redecls_begin(), E = redecls_end();
1320 while (I != E && !I->getInit())
1321 ++I;
1322
1323 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001324 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001325 return I->getInit();
1326 }
1327 return 0;
1328}
1329
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001330bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001331 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001332 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001333
1334 if (!isStaticDataMember())
1335 return false;
1336
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001337 // If this static data member was instantiated from a static data member of
1338 // a class template, check whether that static data member was defined
1339 // out-of-line.
1340 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1341 return VD->isOutOfLine();
1342
1343 return false;
1344}
1345
Douglas Gregor0d035142009-10-27 18:42:08 +00001346VarDecl *VarDecl::getOutOfLineDefinition() {
1347 if (!isStaticDataMember())
1348 return 0;
1349
1350 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1351 RD != RDEnd; ++RD) {
1352 if (RD->getLexicalDeclContext()->isFileContext())
1353 return *RD;
1354 }
1355
1356 return 0;
1357}
1358
Douglas Gregor838db382010-02-11 01:19:42 +00001359void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001360 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1361 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001362 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001363 }
1364
1365 Init = I;
1366}
1367
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001368bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001369 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001370
Richard Smith16581332012-03-02 04:14:40 +00001371 if (!Lang.CPlusPlus)
1372 return false;
1373
1374 // In C++11, any variable of reference type can be used in a constant
1375 // expression if it is initialized by a constant expression.
1376 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1377 return true;
1378
1379 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001380 // not require the variable to be non-volatile, but we consider this to be a
1381 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001382 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001383 return false;
1384
1385 // In C++, const, non-volatile variables of integral or enumeration types
1386 // can be used in constant expressions.
1387 if (getType()->isIntegralOrEnumerationType())
1388 return true;
1389
Richard Smith16581332012-03-02 04:14:40 +00001390 // Additionally, in C++11, non-volatile constexpr variables can be used in
1391 // constant expressions.
1392 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001393}
1394
Richard Smith099e7f62011-12-19 06:19:21 +00001395/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1396/// form, which contains extra information on the evaluated value of the
1397/// initializer.
1398EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1399 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1400 if (!Eval) {
1401 Stmt *S = Init.get<Stmt *>();
1402 Eval = new (getASTContext()) EvaluatedStmt;
1403 Eval->Value = S;
1404 Init = Eval;
1405 }
1406 return Eval;
1407}
1408
Richard Smith2d6a5672012-01-14 04:30:29 +00001409APValue *VarDecl::evaluateValue() const {
1410 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1411 return evaluateValue(Notes);
1412}
1413
1414APValue *VarDecl::evaluateValue(
1415 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001416 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1417
1418 // We only produce notes indicating why an initializer is non-constant the
1419 // first time it is evaluated. FIXME: The notes won't always be emitted the
1420 // first time we try evaluation, so might not be produced at all.
1421 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001422 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001423
1424 const Expr *Init = cast<Expr>(Eval->Value);
1425 assert(!Init->isValueDependent());
1426
1427 if (Eval->IsEvaluating) {
1428 // FIXME: Produce a diagnostic for self-initialization.
1429 Eval->CheckedICE = true;
1430 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001431 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001432 }
1433
1434 Eval->IsEvaluating = true;
1435
1436 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1437 this, Notes);
1438
1439 // Ensure the result is an uninitialized APValue if evaluation fails.
1440 if (!Result)
1441 Eval->Evaluated = APValue();
1442
1443 Eval->IsEvaluating = false;
1444 Eval->WasEvaluated = true;
1445
1446 // In C++11, we have determined whether the initializer was a constant
1447 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001448 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001449 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001450 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001451 }
1452
Richard Smith2d6a5672012-01-14 04:30:29 +00001453 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001454}
1455
1456bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001457 // Initializers of weak variables are never ICEs.
1458 if (isWeak())
1459 return false;
1460
Richard Smith099e7f62011-12-19 06:19:21 +00001461 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1462 if (Eval->CheckedICE)
1463 // We have already checked whether this subexpression is an
1464 // integral constant expression.
1465 return Eval->IsICE;
1466
1467 const Expr *Init = cast<Expr>(Eval->Value);
1468 assert(!Init->isValueDependent());
1469
1470 // In C++11, evaluate the initializer to check whether it's a constant
1471 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001472 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001473 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1474 evaluateValue(Notes);
1475 return Eval->IsICE;
1476 }
1477
1478 // It's an ICE whether or not the definition we found is
1479 // out-of-line. See DR 721 and the discussion in Clang PR
1480 // 6206 for details.
1481
1482 if (Eval->CheckingICE)
1483 return false;
1484 Eval->CheckingICE = true;
1485
1486 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1487 Eval->CheckingICE = false;
1488 Eval->CheckedICE = true;
1489 return Eval->IsICE;
1490}
1491
Douglas Gregor03e80032011-06-21 17:03:29 +00001492bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001493 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001494
1495 const Expr *E = getInit();
1496 if (!E)
1497 return false;
1498
1499 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1500 E = Cleanups->getSubExpr();
1501
1502 return isa<MaterializeTemporaryExpr>(E);
1503}
1504
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001505VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001506 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001507 return cast<VarDecl>(MSI->getInstantiatedFrom());
1508
1509 return 0;
1510}
1511
Douglas Gregor663b5a02009-10-14 20:14:33 +00001512TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001513 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001514 return MSI->getTemplateSpecializationKind();
1515
1516 return TSK_Undeclared;
1517}
1518
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001519MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001520 return getASTContext().getInstantiatedFromStaticDataMember(this);
1521}
1522
Douglas Gregor0a897e32009-10-15 17:21:20 +00001523void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1524 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001525 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001526 assert(MSI && "Not an instantiated static data member?");
1527 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001528 if (TSK != TSK_ExplicitSpecialization &&
1529 PointOfInstantiation.isValid() &&
1530 MSI->getPointOfInstantiation().isInvalid())
1531 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001532}
1533
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001534//===----------------------------------------------------------------------===//
1535// ParmVarDecl Implementation
1536//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001537
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001538ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001539 SourceLocation StartLoc,
1540 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001541 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001542 StorageClass S, StorageClass SCAsWritten,
1543 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001544 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001545 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001546}
1547
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001548ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1549 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1550 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1551 0, QualType(), 0, SC_None, SC_None, 0);
1552}
1553
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001554SourceRange ParmVarDecl::getSourceRange() const {
1555 if (!hasInheritedDefaultArg()) {
1556 SourceRange ArgRange = getDefaultArgRange();
1557 if (ArgRange.isValid())
1558 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1559 }
1560
1561 return DeclaratorDecl::getSourceRange();
1562}
1563
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001564Expr *ParmVarDecl::getDefaultArg() {
1565 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1566 assert(!hasUninstantiatedDefaultArg() &&
1567 "Default argument is not yet instantiated!");
1568
1569 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001570 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001571 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001572
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001573 return Arg;
1574}
1575
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001576SourceRange ParmVarDecl::getDefaultArgRange() const {
1577 if (const Expr *E = getInit())
1578 return E->getSourceRange();
1579
1580 if (hasUninstantiatedDefaultArg())
1581 return getUninstantiatedDefaultArg()->getSourceRange();
1582
1583 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001584}
1585
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001586bool ParmVarDecl::isParameterPack() const {
1587 return isa<PackExpansionType>(getType());
1588}
1589
Ted Kremenekd211cb72011-10-06 05:00:56 +00001590void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1591 getASTContext().setParameterIndex(this, parameterIndex);
1592 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1593}
1594
1595unsigned ParmVarDecl::getParameterIndexLarge() const {
1596 return getASTContext().getParameterIndex(this);
1597}
1598
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001599//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001600// FunctionDecl Implementation
1601//===----------------------------------------------------------------------===//
1602
Douglas Gregorda2142f2011-02-19 18:51:44 +00001603void FunctionDecl::getNameForDiagnostic(std::string &S,
1604 const PrintingPolicy &Policy,
1605 bool Qualified) const {
1606 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1607 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1608 if (TemplateArgs)
1609 S += TemplateSpecializationType::PrintTemplateArgumentList(
1610 TemplateArgs->data(),
1611 TemplateArgs->size(),
1612 Policy);
1613
1614}
1615
Ted Kremenek9498d382010-04-29 16:49:01 +00001616bool FunctionDecl::isVariadic() const {
1617 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1618 return FT->isVariadic();
1619 return false;
1620}
1621
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001622bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1623 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001624 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001625 Definition = *I;
1626 return true;
1627 }
1628 }
1629
1630 return false;
1631}
1632
Anders Carlssonffb945f2011-05-14 23:26:09 +00001633bool FunctionDecl::hasTrivialBody() const
1634{
1635 Stmt *S = getBody();
1636 if (!S) {
1637 // Since we don't have a body for this function, we don't know if it's
1638 // trivial or not.
1639 return false;
1640 }
1641
1642 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1643 return true;
1644 return false;
1645}
1646
Sean Hunt10620eb2011-05-06 20:44:56 +00001647bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1648 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001649 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001650 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1651 return true;
1652 }
1653 }
1654
1655 return false;
1656}
1657
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001658Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001659 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1660 if (I->Body) {
1661 Definition = *I;
1662 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001663 } else if (I->IsLateTemplateParsed) {
1664 Definition = *I;
1665 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001666 }
1667 }
1668
1669 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001670}
1671
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001672void FunctionDecl::setBody(Stmt *B) {
1673 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001674 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001675 EndRangeLoc = B->getLocEnd();
1676}
1677
Douglas Gregor21386642010-09-28 21:55:22 +00001678void FunctionDecl::setPure(bool P) {
1679 IsPure = P;
1680 if (P)
1681 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1682 Parent->markedVirtualFunctionPure();
1683}
1684
Richard Smith3f5f5582012-06-08 21:09:22 +00001685void FunctionDecl::setConstexpr(bool IC) {
1686 IsConstexpr = IC;
1687 CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(this);
1688 if (IC && CD)
1689 CD->getParent()->markedConstructorConstexpr(CD);
1690}
1691
Douglas Gregor48a83b52009-09-12 00:17:51 +00001692bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001693 const TranslationUnitDecl *tunit =
1694 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1695 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001696 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001697 getIdentifier() &&
1698 getIdentifier()->isStr("main");
1699}
1700
1701bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1702 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1703 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1704 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1705 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1706 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1707
1708 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1709 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1710
1711 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1712 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1713
1714 ASTContext &Context =
1715 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1716 ->getASTContext();
1717
1718 // The result type and first argument type are constant across all
1719 // these operators. The second argument must be exactly void*.
1720 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001721}
1722
Douglas Gregor48a83b52009-09-12 00:17:51 +00001723bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001724 if (getLinkage() != ExternalLinkage)
1725 return false;
1726
1727 if (getAttr<OverloadableAttr>())
1728 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001729
Chandler Carruth10aad442011-02-25 00:05:02 +00001730 const DeclContext *DC = getDeclContext();
1731 if (DC->isRecord())
1732 return false;
1733
Eli Friedman750dc2b2012-01-15 01:23:58 +00001734 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001735 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001736 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001737
Eli Friedman750dc2b2012-01-15 01:23:58 +00001738 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001739}
1740
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001741bool FunctionDecl::isGlobal() const {
1742 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1743 return Method->isStatic();
1744
John McCalld931b082010-08-26 03:08:43 +00001745 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001746 return false;
1747
Mike Stump1eb44332009-09-09 15:08:12 +00001748 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001749 DC->isNamespace();
1750 DC = DC->getParent()) {
1751 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1752 if (!Namespace->getDeclName())
1753 return false;
1754 break;
1755 }
1756 }
1757
1758 return true;
1759}
1760
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001761void
1762FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1763 redeclarable_base::setPreviousDeclaration(PrevDecl);
1764
1765 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1766 FunctionTemplateDecl *PrevFunTmpl
1767 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1768 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1769 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1770 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001771
Axel Naumannd9d137e2011-11-08 18:21:06 +00001772 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001773 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001774}
1775
1776const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1777 return getFirstDeclaration();
1778}
1779
1780FunctionDecl *FunctionDecl::getCanonicalDecl() {
1781 return getFirstDeclaration();
1782}
1783
Douglas Gregor381d34e2010-12-06 18:36:25 +00001784void FunctionDecl::setStorageClass(StorageClass SC) {
1785 assert(isLegalForFunction(SC));
1786 if (getStorageClass() != SC)
1787 ClearLinkageCache();
1788
1789 SClass = SC;
1790}
1791
Douglas Gregor3e41d602009-02-13 23:20:09 +00001792/// \brief Returns a value indicating whether this function
1793/// corresponds to a builtin function.
1794///
1795/// The function corresponds to a built-in function if it is
1796/// declared at translation scope or within an extern "C" block and
1797/// its name matches with the name of a builtin. The returned value
1798/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001799/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001800/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001801unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001802 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001803 return 0;
1804
1805 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001806 if (!BuiltinID)
1807 return 0;
1808
1809 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001810 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1811 return BuiltinID;
1812
1813 // This function has the name of a known C library
1814 // function. Determine whether it actually refers to the C library
1815 // function or whether it just has the same name.
1816
Douglas Gregor9add3172009-02-17 03:23:10 +00001817 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001818 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001819 return 0;
1820
Douglas Gregor3c385e52009-02-14 18:57:46 +00001821 // If this function is at translation-unit scope and we're not in
1822 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001823 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001824 getDeclContext()->isTranslationUnit())
1825 return BuiltinID;
1826
1827 // If the function is in an extern "C" linkage specification and is
1828 // not marked "overloadable", it's the real function.
1829 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001830 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001831 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001832 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001833 return BuiltinID;
1834
1835 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001836 return 0;
1837}
1838
1839
Chris Lattner1ad9b282009-04-25 06:03:53 +00001840/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001841/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001842/// after it has been created.
1843unsigned FunctionDecl::getNumParams() const {
Eli Friedman482466b2012-08-30 22:22:09 +00001844 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001845 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001846 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001847 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Reid Spencer5f016e22007-07-11 17:01:13 +00001849}
1850
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001851void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001852 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001854 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001857 if (!NewParamInfo.empty()) {
1858 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1859 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 }
1861}
1862
James Molloy16f1f712012-02-29 10:24:19 +00001863void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1864 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1865
1866 if (!NewDecls.empty()) {
1867 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1868 std::copy(NewDecls.begin(), NewDecls.end(), A);
1869 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1870 }
1871}
1872
Chris Lattner8123a952008-04-10 02:22:51 +00001873/// getMinRequiredArguments - Returns the minimum number of arguments
1874/// needed to call this function. This may be fewer than the number of
1875/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001876/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001877unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001878 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001879 return getNumParams();
1880
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001881 unsigned NumRequiredArgs = getNumParams();
1882
1883 // If the last parameter is a parameter pack, we don't need an argument for
1884 // it.
1885 if (NumRequiredArgs > 0 &&
1886 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1887 --NumRequiredArgs;
1888
1889 // If this parameter has a default argument, we don't need an argument for
1890 // it.
1891 while (NumRequiredArgs > 0 &&
1892 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001893 --NumRequiredArgs;
1894
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001895 // We might have parameter packs before the end. These can't be deduced,
1896 // but they can still handle multiple arguments.
1897 unsigned ArgIdx = NumRequiredArgs;
1898 while (ArgIdx > 0) {
1899 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1900 NumRequiredArgs = ArgIdx;
1901
1902 --ArgIdx;
1903 }
1904
Chris Lattner8123a952008-04-10 02:22:51 +00001905 return NumRequiredArgs;
1906}
1907
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001908bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001909 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001910 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001911
1912 if (isa<CXXMethodDecl>(this)) {
1913 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1914 return true;
1915 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001916
1917 switch (getTemplateSpecializationKind()) {
1918 case TSK_Undeclared:
1919 case TSK_ExplicitSpecialization:
1920 return false;
1921
1922 case TSK_ImplicitInstantiation:
1923 case TSK_ExplicitInstantiationDeclaration:
1924 case TSK_ExplicitInstantiationDefinition:
1925 // Handle below.
1926 break;
1927 }
1928
1929 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001930 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001931 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001932 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001933
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001934 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001935 return PatternDecl->isInlined();
1936
1937 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001938}
1939
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001940static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1941 // Only consider file-scope declarations in this test.
1942 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1943 return false;
1944
1945 // Only consider explicit declarations; the presence of a builtin for a
1946 // libcall shouldn't affect whether a definition is externally visible.
1947 if (Redecl->isImplicit())
1948 return false;
1949
1950 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1951 return true; // Not an inline definition
1952
1953 return false;
1954}
1955
Nick Lewyckydce67a72011-07-18 05:26:13 +00001956/// \brief For a function declaration in C or C++, determine whether this
1957/// declaration causes the definition to be externally visible.
1958///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001959/// Specifically, this determines if adding the current declaration to the set
1960/// of redeclarations of the given functions causes
1961/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001962bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1963 assert(!doesThisDeclarationHaveABody() &&
1964 "Must have a declaration without a body.");
1965
1966 ASTContext &Context = getASTContext();
1967
David Blaikie4e4d0842012-03-11 07:00:24 +00001968 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001969 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1970 // an externally visible definition.
1971 //
1972 // FIXME: What happens if gnu_inline gets added on after the first
1973 // declaration?
1974 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1975 return false;
1976
1977 const FunctionDecl *Prev = this;
1978 bool FoundBody = false;
1979 while ((Prev = Prev->getPreviousDecl())) {
1980 FoundBody |= Prev->Body;
1981
1982 if (Prev->Body) {
1983 // If it's not the case that both 'inline' and 'extern' are
1984 // specified on the definition, then it is always externally visible.
1985 if (!Prev->isInlineSpecified() ||
1986 Prev->getStorageClassAsWritten() != SC_Extern)
1987 return false;
1988 } else if (Prev->isInlineSpecified() &&
1989 Prev->getStorageClassAsWritten() != SC_Extern) {
1990 return false;
1991 }
1992 }
1993 return FoundBody;
1994 }
1995
David Blaikie4e4d0842012-03-11 07:00:24 +00001996 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001997 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001998
1999 // C99 6.7.4p6:
2000 // [...] If all of the file scope declarations for a function in a
2001 // translation unit include the inline function specifier without extern,
2002 // then the definition in that translation unit is an inline definition.
2003 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002004 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002005 const FunctionDecl *Prev = this;
2006 bool FoundBody = false;
2007 while ((Prev = Prev->getPreviousDecl())) {
2008 FoundBody |= Prev->Body;
2009 if (RedeclForcesDefC99(Prev))
2010 return false;
2011 }
2012 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00002013}
2014
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002015/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002016/// definition will be externally visible.
2017///
2018/// Inline function definitions are always available for inlining optimizations.
2019/// However, depending on the language dialect, declaration specifiers, and
2020/// attributes, the definition of an inline function may or may not be
2021/// "externally" visible to other translation units in the program.
2022///
2023/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002024/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002025/// inline definition becomes externally visible (C99 6.7.4p6).
2026///
2027/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2028/// definition, we use the GNU semantics for inline, which are nearly the
2029/// opposite of C99 semantics. In particular, "inline" by itself will create
2030/// an externally visible symbol, but "extern inline" will not create an
2031/// externally visible symbol.
2032bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002033 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002034 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002035 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002036
David Blaikie4e4d0842012-03-11 07:00:24 +00002037 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002038 // Note: If you change the logic here, please change
2039 // doesDeclarationForceExternallyVisibleDefinition as well.
2040 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002041 // If it's not the case that both 'inline' and 'extern' are
2042 // specified on the definition, then this inline definition is
2043 // externally visible.
2044 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2045 return true;
2046
2047 // If any declaration is 'inline' but not 'extern', then this definition
2048 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002049 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2050 Redecl != RedeclEnd;
2051 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002052 if (Redecl->isInlineSpecified() &&
2053 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002054 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002055 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002056
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002057 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002058 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002059
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002060 // C99 6.7.4p6:
2061 // [...] If all of the file scope declarations for a function in a
2062 // translation unit include the inline function specifier without extern,
2063 // then the definition in that translation unit is an inline definition.
2064 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2065 Redecl != RedeclEnd;
2066 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002067 if (RedeclForcesDefC99(*Redecl))
2068 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002069 }
2070
2071 // C99 6.7.4p6:
2072 // An inline definition does not provide an external definition for the
2073 // function, and does not forbid an external definition in another
2074 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002075 return false;
2076}
2077
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002078/// getOverloadedOperator - Which C++ overloaded operator this
2079/// function represents, if any.
2080OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002081 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2082 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002083 else
2084 return OO_None;
2085}
2086
Sean Hunta6c058d2010-01-13 09:01:02 +00002087/// getLiteralIdentifier - The literal suffix identifier this function
2088/// represents, if any.
2089const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2090 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2091 return getDeclName().getCXXLiteralIdentifier();
2092 else
2093 return 0;
2094}
2095
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002096FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2097 if (TemplateOrSpecialization.isNull())
2098 return TK_NonTemplate;
2099 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2100 return TK_FunctionTemplate;
2101 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2102 return TK_MemberSpecialization;
2103 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2104 return TK_FunctionTemplateSpecialization;
2105 if (TemplateOrSpecialization.is
2106 <DependentFunctionTemplateSpecializationInfo*>())
2107 return TK_DependentFunctionTemplateSpecialization;
2108
David Blaikieb219cfc2011-09-23 05:06:16 +00002109 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002110}
2111
Douglas Gregor2db32322009-10-07 23:56:10 +00002112FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002113 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002114 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2115
2116 return 0;
2117}
2118
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002119MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2120 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2121}
2122
Douglas Gregor2db32322009-10-07 23:56:10 +00002123void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002124FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2125 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002126 TemplateSpecializationKind TSK) {
2127 assert(TemplateOrSpecialization.isNull() &&
2128 "Member function is already a specialization");
2129 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002130 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002131 TemplateOrSpecialization = Info;
2132}
2133
Douglas Gregor3b846b62009-10-27 20:53:28 +00002134bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002135 // If the function is invalid, it can't be implicitly instantiated.
2136 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002137 return false;
2138
2139 switch (getTemplateSpecializationKind()) {
2140 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002141 case TSK_ExplicitInstantiationDefinition:
2142 return false;
2143
2144 case TSK_ImplicitInstantiation:
2145 return true;
2146
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002147 // It is possible to instantiate TSK_ExplicitSpecialization kind
2148 // if the FunctionDecl has a class scope specialization pattern.
2149 case TSK_ExplicitSpecialization:
2150 return getClassScopeSpecializationPattern() != 0;
2151
Douglas Gregor3b846b62009-10-27 20:53:28 +00002152 case TSK_ExplicitInstantiationDeclaration:
2153 // Handled below.
2154 break;
2155 }
2156
2157 // Find the actual template from which we will instantiate.
2158 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002159 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002160 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002161 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002162
2163 // C++0x [temp.explicit]p9:
2164 // Except for inline functions, other explicit instantiation declarations
2165 // have the effect of suppressing the implicit instantiation of the entity
2166 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002167 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002168 return true;
2169
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002170 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002171}
2172
2173bool FunctionDecl::isTemplateInstantiation() const {
2174 switch (getTemplateSpecializationKind()) {
2175 case TSK_Undeclared:
2176 case TSK_ExplicitSpecialization:
2177 return false;
2178 case TSK_ImplicitInstantiation:
2179 case TSK_ExplicitInstantiationDeclaration:
2180 case TSK_ExplicitInstantiationDefinition:
2181 return true;
2182 }
2183 llvm_unreachable("All TSK values handled.");
2184}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002185
2186FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002187 // Handle class scope explicit specialization special case.
2188 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2189 return getClassScopeSpecializationPattern();
2190
Douglas Gregor3b846b62009-10-27 20:53:28 +00002191 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2192 while (Primary->getInstantiatedFromMemberTemplate()) {
2193 // If we have hit a point where the user provided a specialization of
2194 // this template, we're done looking.
2195 if (Primary->isMemberSpecialization())
2196 break;
2197
2198 Primary = Primary->getInstantiatedFromMemberTemplate();
2199 }
2200
2201 return Primary->getTemplatedDecl();
2202 }
2203
2204 return getInstantiatedFromMemberFunction();
2205}
2206
Douglas Gregor16e8be22009-06-29 17:30:29 +00002207FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002208 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002209 = TemplateOrSpecialization
2210 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002211 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002212 }
2213 return 0;
2214}
2215
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002216FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2217 return getASTContext().getClassScopeSpecializationPattern(this);
2218}
2219
Douglas Gregor16e8be22009-06-29 17:30:29 +00002220const TemplateArgumentList *
2221FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002222 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002223 = TemplateOrSpecialization
2224 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002225 return Info->TemplateArguments;
2226 }
2227 return 0;
2228}
2229
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002230const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002231FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2232 if (FunctionTemplateSpecializationInfo *Info
2233 = TemplateOrSpecialization
2234 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2235 return Info->TemplateArgumentsAsWritten;
2236 }
2237 return 0;
2238}
2239
Mike Stump1eb44332009-09-09 15:08:12 +00002240void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002241FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2242 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002243 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002244 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002245 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002246 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2247 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002248 assert(TSK != TSK_Undeclared &&
2249 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002250 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002251 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002252 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002253 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2254 TemplateArgs,
2255 TemplateArgsAsWritten,
2256 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002257 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002258 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002259}
2260
John McCallaf2094e2010-04-08 09:05:18 +00002261void
2262FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2263 const UnresolvedSetImpl &Templates,
2264 const TemplateArgumentListInfo &TemplateArgs) {
2265 assert(TemplateOrSpecialization.isNull());
2266 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2267 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002268 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002269 void *Buffer = Context.Allocate(Size);
2270 DependentFunctionTemplateSpecializationInfo *Info =
2271 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2272 TemplateArgs);
2273 TemplateOrSpecialization = Info;
2274}
2275
2276DependentFunctionTemplateSpecializationInfo::
2277DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2278 const TemplateArgumentListInfo &TArgs)
2279 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2280
2281 d.NumTemplates = Ts.size();
2282 d.NumArgs = TArgs.size();
2283
2284 FunctionTemplateDecl **TsArray =
2285 const_cast<FunctionTemplateDecl**>(getTemplates());
2286 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2287 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2288
2289 TemplateArgumentLoc *ArgsArray =
2290 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2291 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2292 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2293}
2294
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002295TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002296 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002297 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002298 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002299 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002300 if (FTSInfo)
2301 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Douglas Gregor2db32322009-10-07 23:56:10 +00002303 MemberSpecializationInfo *MSInfo
2304 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2305 if (MSInfo)
2306 return MSInfo->getTemplateSpecializationKind();
2307
2308 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002309}
2310
Mike Stump1eb44332009-09-09 15:08:12 +00002311void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002312FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2313 SourceLocation PointOfInstantiation) {
2314 if (FunctionTemplateSpecializationInfo *FTSInfo
2315 = TemplateOrSpecialization.dyn_cast<
2316 FunctionTemplateSpecializationInfo*>()) {
2317 FTSInfo->setTemplateSpecializationKind(TSK);
2318 if (TSK != TSK_ExplicitSpecialization &&
2319 PointOfInstantiation.isValid() &&
2320 FTSInfo->getPointOfInstantiation().isInvalid())
2321 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2322 } else if (MemberSpecializationInfo *MSInfo
2323 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2324 MSInfo->setTemplateSpecializationKind(TSK);
2325 if (TSK != TSK_ExplicitSpecialization &&
2326 PointOfInstantiation.isValid() &&
2327 MSInfo->getPointOfInstantiation().isInvalid())
2328 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2329 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002330 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002331}
2332
2333SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002334 if (FunctionTemplateSpecializationInfo *FTSInfo
2335 = TemplateOrSpecialization.dyn_cast<
2336 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002337 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002338 else if (MemberSpecializationInfo *MSInfo
2339 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002340 return MSInfo->getPointOfInstantiation();
2341
2342 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002343}
2344
Douglas Gregor9f185072009-09-11 20:15:17 +00002345bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002346 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002347 return true;
2348
2349 // If this function was instantiated from a member function of a
2350 // class template, check whether that member function was defined out-of-line.
2351 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2352 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002353 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002354 return Definition->isOutOfLine();
2355 }
2356
2357 // If this function was instantiated from a function template,
2358 // check whether that function template was defined out-of-line.
2359 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2360 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002361 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002362 return Definition->isOutOfLine();
2363 }
2364
2365 return false;
2366}
2367
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002368SourceRange FunctionDecl::getSourceRange() const {
2369 return SourceRange(getOuterLocStart(), EndRangeLoc);
2370}
2371
Anna Zaks9392d4e2012-01-18 02:45:01 +00002372unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002373 IdentifierInfo *FnInfo = getIdentifier();
2374
2375 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002376 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002377
2378 // Builtin handling.
2379 switch (getBuiltinID()) {
2380 case Builtin::BI__builtin_memset:
2381 case Builtin::BI__builtin___memset_chk:
2382 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002383 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002384
2385 case Builtin::BI__builtin_memcpy:
2386 case Builtin::BI__builtin___memcpy_chk:
2387 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002388 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002389
2390 case Builtin::BI__builtin_memmove:
2391 case Builtin::BI__builtin___memmove_chk:
2392 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002393 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002394
2395 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002396 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002397 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002398 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002399
2400 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002401 case Builtin::BImemcmp:
2402 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002403
2404 case Builtin::BI__builtin_strncpy:
2405 case Builtin::BI__builtin___strncpy_chk:
2406 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002407 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002408
2409 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002410 case Builtin::BIstrncmp:
2411 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002412
2413 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002414 case Builtin::BIstrncasecmp:
2415 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002416
2417 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002418 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002419 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002420 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002421
2422 case Builtin::BI__builtin_strndup:
2423 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002424 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002425
Anna Zaksc36bedc2012-02-01 19:08:57 +00002426 case Builtin::BI__builtin_strlen:
2427 case Builtin::BIstrlen:
2428 return Builtin::BIstrlen;
2429
Anna Zaksd9b859a2012-01-13 21:52:01 +00002430 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002431 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002432 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002433 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002434 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002435 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002436 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002437 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002438 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002439 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002440 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002441 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002442 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002443 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002444 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002445 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002446 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002447 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002448 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002449 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002450 else if (FnInfo->isStr("strlen"))
2451 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002452 }
2453 break;
2454 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002455 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002456}
2457
Chris Lattner8a934232008-03-31 00:36:02 +00002458//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002459// FieldDecl Implementation
2460//===----------------------------------------------------------------------===//
2461
Jay Foad4ba2a172011-01-12 09:06:06 +00002462FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002463 SourceLocation StartLoc, SourceLocation IdLoc,
2464 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002465 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smithca523302012-06-10 03:12:00 +00002466 InClassInitStyle InitStyle) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002467 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithca523302012-06-10 03:12:00 +00002468 BW, Mutable, InitStyle);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002469}
2470
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002471FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2472 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2473 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smithca523302012-06-10 03:12:00 +00002474 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002475}
2476
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002477bool FieldDecl::isAnonymousStructOrUnion() const {
2478 if (!isImplicit() || getDeclName())
2479 return false;
2480
2481 if (const RecordType *Record = getType()->getAs<RecordType>())
2482 return Record->getDecl()->isAnonymousStructOrUnion();
2483
2484 return false;
2485}
2486
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002487unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2488 assert(isBitField() && "not a bitfield");
2489 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2490 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2491}
2492
John McCallba4f5d52011-01-20 07:57:12 +00002493unsigned FieldDecl::getFieldIndex() const {
2494 if (CachedFieldIndex) return CachedFieldIndex - 1;
2495
Richard Smith180f4792011-11-10 06:34:14 +00002496 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002497 const RecordDecl *RD = getParent();
2498 const FieldDecl *LastFD = 0;
Eli Friedman5f608ae2012-10-12 23:29:20 +00002499 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smith180f4792011-11-10 06:34:14 +00002500
2501 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2502 I != E; ++I, ++Index) {
David Blaikie262bc182012-04-30 02:36:29 +00002503 I->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002504
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002505 if (IsMsStruct) {
2506 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie581deb32012-06-06 20:45:41 +00002507 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smith180f4792011-11-10 06:34:14 +00002508 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002509 continue;
2510 }
David Blaikie581deb32012-06-06 20:45:41 +00002511 LastFD = *I;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002512 }
John McCallba4f5d52011-01-20 07:57:12 +00002513 }
2514
Richard Smith180f4792011-11-10 06:34:14 +00002515 assert(CachedFieldIndex && "failed to find field in parent");
2516 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002517}
2518
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002519SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002520 if (const Expr *E = InitializerOrBitWidth.getPointer())
2521 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002522 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002523}
2524
Abramo Bagnaraa5335762012-07-02 20:35:48 +00002525void FieldDecl::setBitWidth(Expr *Width) {
2526 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2527 "bit width or initializer already set");
2528 InitializerOrBitWidth.setPointer(Width);
2529}
2530
Richard Smith7a614d82011-06-11 17:19:42 +00002531void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smithca523302012-06-10 03:12:00 +00002532 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith7a614d82011-06-11 17:19:42 +00002533 "bit width or initializer already set");
2534 InitializerOrBitWidth.setPointer(Init);
Richard Smith7a614d82011-06-11 17:19:42 +00002535}
2536
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002537//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002538// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002539//===----------------------------------------------------------------------===//
2540
Douglas Gregor1693e152010-07-06 18:42:40 +00002541SourceLocation TagDecl::getOuterLocStart() const {
2542 return getTemplateOrInnerLocStart(this);
2543}
2544
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002545SourceRange TagDecl::getSourceRange() const {
2546 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002547 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002548}
2549
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002550TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002551 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002552}
2553
Richard Smith162e1c12011-04-15 14:24:37 +00002554void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2555 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002556 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002557 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002558 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002559}
2560
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002561void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002562 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002563
2564 if (isa<CXXRecordDecl>(this)) {
2565 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2566 struct CXXRecordDecl::DefinitionData *Data =
2567 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002568 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2569 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002570 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002571}
2572
2573void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002574 assert((!isa<CXXRecordDecl>(this) ||
2575 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2576 "definition completed but not started");
2577
John McCall5e1cdac2011-10-07 06:10:15 +00002578 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002579 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002580
2581 if (ASTMutationListener *L = getASTMutationListener())
2582 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002583}
2584
John McCall5e1cdac2011-10-07 06:10:15 +00002585TagDecl *TagDecl::getDefinition() const {
2586 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002587 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002588 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2589 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002590
2591 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002592 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002593 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002594 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002596 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002597}
2598
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002599void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2600 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002601 // Make sure the extended qualifier info is allocated.
2602 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002603 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002604 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002605 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002606 } else {
John McCallb6217662010-03-15 10:12:16 +00002607 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002608 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002609 if (getExtInfo()->NumTemplParamLists == 0) {
2610 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002611 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002612 }
2613 else
2614 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002615 }
2616 }
2617}
2618
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002619void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2620 unsigned NumTPLists,
2621 TemplateParameterList **TPLists) {
2622 assert(NumTPLists > 0);
2623 // Make sure the extended decl info is allocated.
2624 if (!hasExtInfo())
2625 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002626 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002627 // Set the template parameter lists info.
2628 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2629}
2630
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002631//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002632// EnumDecl Implementation
2633//===----------------------------------------------------------------------===//
2634
David Blaikie99ba9e32011-12-20 02:48:34 +00002635void EnumDecl::anchor() { }
2636
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002637EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2638 SourceLocation StartLoc, SourceLocation IdLoc,
2639 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002640 EnumDecl *PrevDecl, bool IsScoped,
2641 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002642 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002643 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002644 C.getTypeDeclType(Enum, PrevDecl);
2645 return Enum;
2646}
2647
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002648EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2649 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2650 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2651 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002652}
2653
Douglas Gregor838db382010-02-11 01:19:42 +00002654void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002655 QualType NewPromotionType,
2656 unsigned NumPositiveBits,
2657 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002658 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002659 if (!IntegerType)
2660 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002661 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002662 setNumPositiveBits(NumPositiveBits);
2663 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002664 TagDecl::completeDefinition();
2665}
2666
Richard Smith1af83c42012-03-23 03:33:32 +00002667TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2668 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2669 return MSI->getTemplateSpecializationKind();
2670
2671 return TSK_Undeclared;
2672}
2673
2674void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2675 SourceLocation PointOfInstantiation) {
2676 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2677 assert(MSI && "Not an instantiated member enumeration?");
2678 MSI->setTemplateSpecializationKind(TSK);
2679 if (TSK != TSK_ExplicitSpecialization &&
2680 PointOfInstantiation.isValid() &&
2681 MSI->getPointOfInstantiation().isInvalid())
2682 MSI->setPointOfInstantiation(PointOfInstantiation);
2683}
2684
Richard Smithf1c66b42012-03-14 23:13:10 +00002685EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2686 if (SpecializationInfo)
2687 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2688
2689 return 0;
2690}
2691
2692void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2693 TemplateSpecializationKind TSK) {
2694 assert(!SpecializationInfo && "Member enum is already a specialization");
2695 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2696}
2697
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002698//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002699// RecordDecl Implementation
2700//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002701
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002702RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2703 SourceLocation StartLoc, SourceLocation IdLoc,
2704 IdentifierInfo *Id, RecordDecl *PrevDecl)
2705 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002706 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002707 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002708 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002709 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002710 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002711}
2712
Jay Foad4ba2a172011-01-12 09:06:06 +00002713RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002714 SourceLocation StartLoc, SourceLocation IdLoc,
2715 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2716 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2717 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002718 C.getTypeDeclType(R, PrevDecl);
2719 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002720}
2721
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002722RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2723 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2724 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2725 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002726}
2727
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002728bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002729 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002730 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2731}
2732
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002733RecordDecl::field_iterator RecordDecl::field_begin() const {
2734 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2735 LoadFieldsFromExternalStorage();
2736
2737 return field_iterator(decl_iterator(FirstDecl));
2738}
2739
Douglas Gregorda2142f2011-02-19 18:51:44 +00002740/// completeDefinition - Notes that the definition of this type is now
2741/// complete.
2742void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002743 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002744 TagDecl::completeDefinition();
2745}
2746
Eli Friedman5f608ae2012-10-12 23:29:20 +00002747/// isMsStruct - Get whether or not this record uses ms_struct layout.
2748/// This which can be turned on with an attribute, pragma, or the
2749/// -mms-bitfields command-line option.
2750bool RecordDecl::isMsStruct(const ASTContext &C) const {
2751 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2752}
2753
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002754static bool isFieldOrIndirectField(Decl::Kind K) {
2755 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2756}
2757
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002758void RecordDecl::LoadFieldsFromExternalStorage() const {
2759 ExternalASTSource *Source = getASTContext().getExternalSource();
2760 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2761
2762 // Notify that we have a RecordDecl doing some initialization.
2763 ExternalASTSource::Deserializing TheFields(Source);
2764
Chris Lattner5f9e2722011-07-23 10:55:15 +00002765 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002766 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002767 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2768 Decls)) {
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002769 case ELR_Success:
2770 break;
2771
2772 case ELR_AlreadyLoaded:
2773 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002774 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002775 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002776
2777#ifndef NDEBUG
2778 // Check that all decls we got were FieldDecls.
2779 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002780 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002781#endif
2782
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002783 if (Decls.empty())
2784 return;
2785
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002786 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2787 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002788}
2789
Steve Naroff56ee6892008-10-08 17:01:13 +00002790//===----------------------------------------------------------------------===//
2791// BlockDecl Implementation
2792//===----------------------------------------------------------------------===//
2793
David Blaikie4278c652011-09-21 18:16:56 +00002794void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002795 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002796
Steve Naroffe78b8092009-03-13 16:56:44 +00002797 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002798 if (!NewParamInfo.empty()) {
2799 NumParams = NewParamInfo.size();
2800 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2801 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002802 }
2803}
2804
John McCall6b5a61b2011-02-07 10:33:21 +00002805void BlockDecl::setCaptures(ASTContext &Context,
2806 const Capture *begin,
2807 const Capture *end,
2808 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002809 CapturesCXXThis = capturesCXXThis;
2810
2811 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002812 NumCaptures = 0;
2813 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002814 return;
2815 }
2816
John McCall6b5a61b2011-02-07 10:33:21 +00002817 NumCaptures = end - begin;
2818
2819 // Avoid new Capture[] because we don't want to provide a default
2820 // constructor.
2821 size_t allocationSize = NumCaptures * sizeof(Capture);
2822 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2823 memcpy(buffer, begin, allocationSize);
2824 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002825}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002826
John McCall204e1332011-06-15 22:51:16 +00002827bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2828 for (capture_const_iterator
2829 i = capture_begin(), e = capture_end(); i != e; ++i)
2830 // Only auto vars can be captured, so no redeclaration worries.
2831 if (i->getVariable() == variable)
2832 return true;
2833
2834 return false;
2835}
2836
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002837SourceRange BlockDecl::getSourceRange() const {
2838 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2839}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002840
2841//===----------------------------------------------------------------------===//
2842// Other Decl Allocation/Deallocation Method Implementations
2843//===----------------------------------------------------------------------===//
2844
David Blaikie99ba9e32011-12-20 02:48:34 +00002845void TranslationUnitDecl::anchor() { }
2846
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002847TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2848 return new (C) TranslationUnitDecl(C);
2849}
2850
David Blaikie99ba9e32011-12-20 02:48:34 +00002851void LabelDecl::anchor() { }
2852
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002853LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002854 SourceLocation IdentL, IdentifierInfo *II) {
2855 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2856}
2857
2858LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2859 SourceLocation IdentL, IdentifierInfo *II,
2860 SourceLocation GnuLabelL) {
2861 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2862 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002863}
2864
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002865LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2866 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2867 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002868}
2869
David Blaikie99ba9e32011-12-20 02:48:34 +00002870void ValueDecl::anchor() { }
2871
2872void ImplicitParamDecl::anchor() { }
2873
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002874ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002875 SourceLocation IdLoc,
2876 IdentifierInfo *Id,
2877 QualType Type) {
2878 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002879}
2880
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002881ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2882 unsigned ID) {
2883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2884 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2885}
2886
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002887FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002888 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002889 const DeclarationNameInfo &NameInfo,
2890 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002891 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002892 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002893 bool hasWrittenPrototype,
2894 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002895 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2896 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002897 isInlineSpecified,
2898 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002899 New->HasWrittenPrototype = hasWrittenPrototype;
2900 return New;
2901}
2902
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002903FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2904 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2905 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2906 DeclarationNameInfo(), QualType(), 0,
2907 SC_None, SC_None, false, false);
2908}
2909
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002910BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2911 return new (C) BlockDecl(DC, L);
2912}
2913
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002914BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2915 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2916 return new (Mem) BlockDecl(0, SourceLocation());
2917}
2918
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002919EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2920 SourceLocation L,
2921 IdentifierInfo *Id, QualType T,
2922 Expr *E, const llvm::APSInt &V) {
2923 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2924}
2925
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002926EnumConstantDecl *
2927EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2928 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2929 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2930 llvm::APSInt());
2931}
2932
David Blaikie99ba9e32011-12-20 02:48:34 +00002933void IndirectFieldDecl::anchor() { }
2934
Benjamin Kramerd9811462010-11-21 14:11:41 +00002935IndirectFieldDecl *
2936IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2937 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2938 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002939 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2940}
2941
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002942IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2943 unsigned ID) {
2944 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2945 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2946 QualType(), 0, 0);
2947}
2948
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002949SourceRange EnumConstantDecl::getSourceRange() const {
2950 SourceLocation End = getLocation();
2951 if (Init)
2952 End = Init->getLocEnd();
2953 return SourceRange(getLocation(), End);
2954}
2955
David Blaikie99ba9e32011-12-20 02:48:34 +00002956void TypeDecl::anchor() { }
2957
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002958TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002959 SourceLocation StartLoc, SourceLocation IdLoc,
2960 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2961 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002962}
2963
David Blaikie99ba9e32011-12-20 02:48:34 +00002964void TypedefNameDecl::anchor() { }
2965
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002966TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2967 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2968 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2969}
2970
Richard Smith162e1c12011-04-15 14:24:37 +00002971TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2972 SourceLocation StartLoc,
2973 SourceLocation IdLoc, IdentifierInfo *Id,
2974 TypeSourceInfo *TInfo) {
2975 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2976}
2977
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002978TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2979 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2980 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2981}
2982
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002983SourceRange TypedefDecl::getSourceRange() const {
2984 SourceLocation RangeEnd = getLocation();
2985 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2986 if (typeIsPostfix(TInfo->getType()))
2987 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2988 }
2989 return SourceRange(getLocStart(), RangeEnd);
2990}
2991
Richard Smith162e1c12011-04-15 14:24:37 +00002992SourceRange TypeAliasDecl::getSourceRange() const {
2993 SourceLocation RangeEnd = getLocStart();
2994 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2995 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2996 return SourceRange(getLocStart(), RangeEnd);
2997}
2998
David Blaikie99ba9e32011-12-20 02:48:34 +00002999void FileScopeAsmDecl::anchor() { }
3000
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003001FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00003002 StringLiteral *Str,
3003 SourceLocation AsmLoc,
3004 SourceLocation RParenLoc) {
3005 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003006}
Douglas Gregor15de72c2011-12-02 23:23:56 +00003007
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003008FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3009 unsigned ID) {
3010 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3011 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3012}
3013
Douglas Gregor15de72c2011-12-02 23:23:56 +00003014//===----------------------------------------------------------------------===//
3015// ImportDecl Implementation
3016//===----------------------------------------------------------------------===//
3017
3018/// \brief Retrieve the number of module identifiers needed to name the given
3019/// module.
3020static unsigned getNumModuleIdentifiers(Module *Mod) {
3021 unsigned Result = 1;
3022 while (Mod->Parent) {
3023 Mod = Mod->Parent;
3024 ++Result;
3025 }
3026 return Result;
3027}
3028
Douglas Gregor5948ae12012-01-03 18:04:46 +00003029ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003030 Module *Imported,
3031 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003032 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003033 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003034{
3035 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3036 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3037 memcpy(StoredLocs, IdentifierLocs.data(),
3038 IdentifierLocs.size() * sizeof(SourceLocation));
3039}
3040
Douglas Gregor5948ae12012-01-03 18:04:46 +00003041ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003042 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003043 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003044 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003045{
3046 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3047}
3048
3049ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003050 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003051 ArrayRef<SourceLocation> IdentifierLocs) {
3052 void *Mem = C.Allocate(sizeof(ImportDecl) +
3053 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003054 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003055}
3056
3057ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003058 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003059 Module *Imported,
3060 SourceLocation EndLoc) {
3061 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003062 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003063 Import->setImplicit();
3064 return Import;
3065}
3066
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003067ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3068 unsigned NumLocations) {
3069 void *Mem = AllocateDeserializedDecl(C, ID,
3070 (sizeof(ImportDecl) +
3071 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003072 return new (Mem) ImportDecl(EmptyShell());
3073}
3074
3075ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3076 if (!ImportedAndComplete.getInt())
3077 return ArrayRef<SourceLocation>();
3078
3079 const SourceLocation *StoredLocs
3080 = reinterpret_cast<const SourceLocation *>(this + 1);
3081 return ArrayRef<SourceLocation>(StoredLocs,
3082 getNumModuleIdentifiers(getImportedModule()));
3083}
3084
3085SourceRange ImportDecl::getSourceRange() const {
3086 if (!ImportedAndComplete.getInt())
3087 return SourceRange(getLocation(),
3088 *reinterpret_cast<const SourceLocation *>(this + 1));
3089
3090 return SourceRange(getLocation(), getIdentifierLocs().back());
3091}