blob: 538dceecd18369612fac4f3dc20e4c1eb39d2830 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000024#include "clang/AST/Stmt.h"
25#include "clang/AST/TypeLoc.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000027#include "clang/Basic/IdentifierTable.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000028#include "clang/Basic/Module.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000029#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000030#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000031#include "llvm/Support/ErrorHandling.h"
David Blaikie4278c652011-09-21 18:16:56 +000032#include <algorithm>
33
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattnerd3b90652008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor4421d2b2011-03-26 12:10:19 +000040static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
41 // If this declaration has an explicit visibility attribute, use it.
42 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
43 switch (A->getVisibility()) {
44 case VisibilityAttr::Default:
45 return DefaultVisibility;
46 case VisibilityAttr::Hidden:
47 return HiddenVisibility;
48 case VisibilityAttr::Protected:
49 return ProtectedVisibility;
50 }
John McCall1fb0caa2010-10-22 21:05:15 +000051 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +000052
53 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
54 // implies visibility(default).
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +000056 for (specific_attr_iterator<AvailabilityAttr>
57 A = D->specific_attr_begin<AvailabilityAttr>(),
58 AEnd = D->specific_attr_end<AvailabilityAttr>();
59 A != AEnd; ++A)
60 if ((*A)->getPlatform()->getName().equals("macosx"))
61 return DefaultVisibility;
62 }
63
64 return llvm::Optional<Visibility>();
John McCall1fb0caa2010-10-22 21:05:15 +000065}
66
John McCallaf146032010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallaf146032010-10-30 11:50:40 +000068
Rafael Espindola093ecc92012-01-14 00:30:36 +000069static LinkageInfo getLVForType(QualType T) {
70 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
71 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
72}
73
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000074/// \brief Get the most restrictive linkage for the types in the given
75/// template parameter list.
Rafael Espindola093ecc92012-01-14 00:30:36 +000076static LinkageInfo
John McCall1fb0caa2010-10-22 21:05:15 +000077getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola093ecc92012-01-14 00:30:36 +000078 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000079 for (TemplateParameterList::const_iterator P = Params->begin(),
80 PEnd = Params->end();
81 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +000082 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
83 if (NTTP->isExpandedParameterPack()) {
84 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
85 QualType T = NTTP->getExpansionType(I);
86 if (!T->isDependentType())
Rafael Espindola093ecc92012-01-14 00:30:36 +000087 LV.merge(getLVForType(T));
Douglas Gregor6952f1e2011-01-19 20:10:05 +000088 }
89 continue;
90 }
Rafael Espindolab5d763d2012-01-02 06:26:22 +000091
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000092 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola093ecc92012-01-14 00:30:36 +000093 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000094 continue;
95 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +000096 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000097
98 if (TemplateTemplateParmDecl *TTP
99 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola093ecc92012-01-14 00:30:36 +0000100 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000101 }
102 }
103
John McCall1fb0caa2010-10-22 21:05:15 +0000104 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000105}
106
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
Rafael Espindola51758612012-11-21 02:47:19 +0000358 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
359 // merging storage classes and visibility attributes, so we don't have to
360 // look at previous decls in here.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000361
John McCallaf8ca372011-02-10 06:50:24 +0000362 // In C++, then if the type of the function uses a type with
363 // unique-external linkage, it's not legally usable from outside
364 // this translation unit. However, we should use the C linkage
365 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000366 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman750dc2b2012-01-15 01:23:58 +0000367 !Function->getDeclContext()->isExternCContext() &&
John McCallaf8ca372011-02-10 06:50:24 +0000368 Function->getType()->getLinkage() == UniqueExternalLinkage)
369 return LinkageInfo::uniqueExternal();
370
John McCall6ce51ee2011-06-27 23:06:04 +0000371 // Consider LV from the template and the template arguments unless
372 // this is an explicit specialization with a visibility attribute.
373 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000374 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola9db614f2012-05-25 16:41:35 +0000375 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
376 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
377 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
378 OnlyTemplate);
379 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000380 LV.mergeWithMin(TempLV);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000381 LV.mergeWithMin(ArgsLV);
382 } else {
383 LV.mergeLinkage(TempLV);
384 LV.mergeLinkage(ArgsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000385 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000386 }
387
Douglas Gregord85b5b92009-11-25 22:24:25 +0000388 // - a named class (Clause 9), or an unnamed class defined in a
389 // typedef declaration in which the class has the typedef name
390 // for linkage purposes (7.1.3); or
391 // - a named enumeration (7.2), or an unnamed enumeration
392 // defined in a typedef declaration in which the enumeration
393 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000394 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
395 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000396 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000397 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000398
John McCall1fb0caa2010-10-22 21:05:15 +0000399 // If this is a class template specialization, consider the
400 // linkage of the template and template arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000401 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000402 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindolaad359be2012-05-25 14:47:05 +0000403 // From the template.
404 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000405
Rafael Espindolaad359be2012-05-25 14:47:05 +0000406 // The arguments at which the template was instantiated.
407 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
408 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
409 OnlyTemplate);
410 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000411 LV.mergeWithMin(TempLV);
Rafael Espindolaad359be2012-05-25 14:47:05 +0000412 LV.mergeWithMin(ArgsLV);
413 } else {
414 LV.mergeLinkage(TempLV);
415 LV.mergeLinkage(ArgsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000416 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000417 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000418
419 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000420 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000421 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
422 OnlyTemplate);
John McCallaf146032010-10-30 11:50:40 +0000423 if (!isExternalLinkage(EnumLV.linkage()))
424 return LinkageInfo::none();
425 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000426
427 // - a template, unless it is a function template that has
428 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000429 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola60115a02012-04-22 00:43:48 +0000430 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregord85b5b92009-11-25 22:24:25 +0000431 // - a namespace (7.3), unless it is declared within an unnamed
432 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000433 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
434 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000435
John McCall1fb0caa2010-10-22 21:05:15 +0000436 // By extension, we assign external linkage to Objective-C
437 // interfaces.
438 } else if (isa<ObjCInterfaceDecl>(D)) {
439 // fallout
440
441 // Everything not covered here has no linkage.
442 } else {
John McCallaf146032010-10-30 11:50:40 +0000443 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000444 }
445
446 // If we ended up with non-external linkage, visibility should
447 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000448 if (LV.linkage() != ExternalLinkage)
449 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000450
John McCall1fb0caa2010-10-22 21:05:15 +0000451 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000452}
453
Rafael Espindola1266b612012-04-21 23:28:21 +0000454static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall1fb0caa2010-10-22 21:05:15 +0000455 // Only certain class members have linkage. Note that fields don't
456 // really have linkage, but it's convenient to say they do for the
457 // purposes of calculating linkage of pointer-to-data-member
458 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000459 if (!(isa<CXXMethodDecl>(D) ||
460 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000461 isa<FieldDecl>(D) ||
David Blaikie66cff722012-11-14 01:52:05 +0000462 isa<TagDecl>(D)))
John McCallaf146032010-10-30 11:50:40 +0000463 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000464
John McCall36987482010-11-02 01:45:15 +0000465 LinkageInfo LV;
466
John McCall36987482010-11-02 01:45:15 +0000467 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola1266b612012-04-21 23:28:21 +0000468 if (!OnlyTemplate) {
Rafael Espindola41574542012-04-19 04:27:47 +0000469 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000470 LV.mergeVisibility(*Vis, true);
Rafael Espindolab04b7312012-07-13 14:25:36 +0000471 // If we're paying attention to global visibility, apply
472 // -finline-visibility-hidden if this is an inline method.
473 //
474 // Note that we do this before merging information about
475 // the class visibility.
476 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
477 LV.mergeVisibility(HiddenVisibility, true);
John McCall36987482010-11-02 01:45:15 +0000478 }
Rafael Espindolac7e60602012-04-19 05:50:08 +0000479
480 // If this class member has an explicit visibility attribute, the only
481 // thing that can change its visibility is the template arguments, so
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +0000482 // only look for them when processing the class.
Rafael Espindola1266b612012-04-21 23:28:21 +0000483 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola0f905902012-04-16 18:25:01 +0000484
Rafael Espindolac7e60602012-04-19 05:50:08 +0000485 // If this member has an visibility attribute, ClassF will exclude
486 // attributes on the class or command line options, keeping only information
487 // about the template instantiation. If the member has no visibility
488 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
489 // produces the desired result.
Rafael Espindola1266b612012-04-21 23:28:21 +0000490 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
491 ClassOnlyTemplate));
John McCall36987482010-11-02 01:45:15 +0000492 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000493 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000494
495 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000496 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000497 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000498
Rafael Espindola1266b612012-04-21 23:28:21 +0000499 if (!OnlyTemplate)
Rafael Espindola4fc14902012-04-19 04:37:16 +0000500 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaff257982012-04-19 02:55:01 +0000501
John McCall3cdfc4d2010-08-13 08:35:10 +0000502 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000503 // If the type of the function uses a type with unique-external
504 // linkage, it's not legally usable from outside this translation unit.
505 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
506 return LinkageInfo::uniqueExternal();
507
John McCall1fb0caa2010-10-22 21:05:15 +0000508 // If this is a method template specialization, use the linkage for
509 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000510 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000511 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000512 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
513 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
514 OnlyTemplate);
515 TemplateParameterList *TemplateParams =
516 spec->getTemplate()->getTemplateParameters();
517 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola9db614f2012-05-25 16:41:35 +0000518 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000519 LV.mergeWithMin(ArgsLV);
Rafael Espindola1266b612012-04-21 23:28:21 +0000520 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000521 LV.mergeWithMin(ParamsLV);
Rafael Espindola41be8cd2012-05-25 17:22:33 +0000522 } else {
523 LV.mergeLinkage(ArgsLV);
524 if (!OnlyTemplate)
525 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000526 }
John McCall66cbcf32010-11-01 01:29:57 +0000527 }
John McCall1fb0caa2010-10-22 21:05:15 +0000528
John McCall110e8e52010-10-29 22:22:43 +0000529 // Note that in contrast to basically every other situation, we
530 // *do* apply -fvisibility to method declarations.
531
532 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000533 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000534 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000535 // Merge template argument/parameter information for member
536 // class template specializations.
537 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
538 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
539 OnlyTemplate);
540 TemplateParameterList *TemplateParams =
541 spec->getSpecializedTemplate()->getTemplateParameters();
542 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindolaad359be2012-05-25 14:47:05 +0000543 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindola20831e22012-05-25 15:51:26 +0000544 LV.mergeWithMin(ArgsLV);
Rafael Espindola59073bb2012-05-25 14:17:45 +0000545 if (!OnlyTemplate)
Rafael Espindolaedb4b622012-06-11 14:29:58 +0000546 LV.mergeWithMin(ParamsLV);
Rafael Espindola20831e22012-05-25 15:51:26 +0000547 } else {
548 LV.mergeLinkage(ArgsLV);
549 if (!OnlyTemplate)
550 LV.mergeLinkage(ParamsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000551 }
John McCall110e8e52010-10-29 22:22:43 +0000552 }
553
John McCall110e8e52010-10-29 22:22:43 +0000554 // Static data members.
555 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000556 // Modify the variable's linkage by its type, but ignore the
557 // type's visibility unless it's a definition.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000558 LinkageInfo TypeLV = getLVForType(VD->getType());
559 if (TypeLV.linkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000560 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindolac7e60602012-04-19 05:50:08 +0000561 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000562 }
563
John McCall1fb0caa2010-10-22 21:05:15 +0000564 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000565}
566
John McCallf76b0922011-02-08 19:01:05 +0000567static void clearLinkageForClass(const CXXRecordDecl *record) {
568 for (CXXRecordDecl::decl_iterator
569 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
570 Decl *child = *i;
571 if (isa<NamedDecl>(child))
572 cast<NamedDecl>(child)->ClearLinkageCache();
573 }
574}
575
David Blaikie99ba9e32011-12-20 02:48:34 +0000576void NamedDecl::anchor() { }
577
John McCallf76b0922011-02-08 19:01:05 +0000578void NamedDecl::ClearLinkageCache() {
579 // Note that we can't skip clearing the linkage of children just
580 // because the parent doesn't have cached linkage: we don't cache
581 // when computing linkage for parent contexts.
582
583 HasCachedLinkage = 0;
584
585 // If we're changing the linkage of a class, we need to reset the
586 // linkage of child declarations, too.
587 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
588 clearLinkageForClass(record);
589
John McCall15e310a2011-02-19 02:53:41 +0000590 if (ClassTemplateDecl *temp =
591 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000592 // Clear linkage for the template pattern.
593 CXXRecordDecl *record = temp->getTemplatedDecl();
594 record->HasCachedLinkage = 0;
595 clearLinkageForClass(record);
596
John McCall15e310a2011-02-19 02:53:41 +0000597 // We need to clear linkage for specializations, too.
598 for (ClassTemplateDecl::spec_iterator
599 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
600 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000601 }
John McCall15e310a2011-02-19 02:53:41 +0000602
603 // Clear cached linkage for function template decls, too.
604 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000605 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
606 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000607 for (FunctionTemplateDecl::spec_iterator
608 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
609 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000610 }
John McCall15e310a2011-02-19 02:53:41 +0000611
John McCallf76b0922011-02-08 19:01:05 +0000612}
613
Douglas Gregor381d34e2010-12-06 18:36:25 +0000614Linkage NamedDecl::getLinkage() const {
615 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000616 assert(Linkage(CachedLinkage) ==
Rafael Espindola1266b612012-04-21 23:28:21 +0000617 getLVForDecl(this, true).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000618 return Linkage(CachedLinkage);
619 }
620
Rafael Espindola1266b612012-04-21 23:28:21 +0000621 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000622 HasCachedLinkage = 1;
623 return Linkage(CachedLinkage);
624}
625
John McCallaf146032010-10-30 11:50:40 +0000626LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola1266b612012-04-21 23:28:21 +0000627 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000628 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000629 HasCachedLinkage = 1;
630 CachedLinkage = LI.linkage();
631 return LI;
John McCall0df95872010-10-29 00:29:13 +0000632}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000633
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000634llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
635 // Use the most recent declaration of a variable.
Rafael Espindola797105a2012-05-16 02:10:38 +0000636 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolac6b82c32012-11-12 04:32:23 +0000637 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola797105a2012-05-16 02:10:38 +0000638 return V;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000639
Rafael Espindola797105a2012-05-16 02:10:38 +0000640 if (Var->isStaticDataMember()) {
641 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
642 if (InstantiatedFrom)
643 return getVisibilityOf(InstantiatedFrom);
644 }
645
646 return llvm::Optional<Visibility>();
647 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000648 // Use the most recent declaration of a function, and also handle
649 // function template specializations.
650 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolac6b82c32012-11-12 04:32:23 +0000651 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000652 return V;
653
654 // If the function is a specialization of a template with an
655 // explicit visibility attribute, use that.
656 if (FunctionTemplateSpecializationInfo *templateInfo
657 = fn->getTemplateSpecializationInfo())
658 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
659
Rafael Espindola860097c2012-02-23 04:17:32 +0000660 // If the function is a member of a specialization of a class template
661 // and the corresponding decl has explicit visibility, use that.
662 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
663 if (InstantiatedFrom)
664 return getVisibilityOf(InstantiatedFrom);
665
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000666 return llvm::Optional<Visibility>();
667 }
668
669 // Otherwise, just check the declaration itself first.
670 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
671 return V;
672
Rafael Espindola98499012012-07-31 19:02:02 +0000673 // The visibility of a template is stored in the templated decl.
674 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
675 return getVisibilityOf(TD->getTemplatedDecl());
676
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000677 // If there wasn't explicit visibility there, and this is a
678 // specialization of a class template, check for visibility
679 // on the pattern.
680 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolad3d02dd2012-07-13 01:19:08 +0000681 = dyn_cast<ClassTemplateSpecializationDecl>(this))
682 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000683
Rafael Espindola860097c2012-02-23 04:17:32 +0000684 // If this is a member class of a specialization of a class template
685 // and the corresponding decl has explicit visibility, use that.
686 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
687 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
688 if (InstantiatedFrom)
689 return getVisibilityOf(InstantiatedFrom);
690 }
691
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000692 return llvm::Optional<Visibility>();
693}
694
Rafael Espindola1266b612012-04-21 23:28:21 +0000695static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000696 // Objective-C: treat all Objective-C declarations as having external
697 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000698 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000699 default:
700 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +0000701 case Decl::ParmVar:
702 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000703 case Decl::TemplateTemplateParm: // count these as external
704 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000705 case Decl::ObjCAtDefsField:
706 case Decl::ObjCCategory:
707 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000708 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000709 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000710 case Decl::ObjCMethod:
711 case Decl::ObjCProperty:
712 case Decl::ObjCPropertyImpl:
713 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000714 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000715
716 case Decl::CXXRecord: {
717 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
718 if (Record->isLambda()) {
719 if (!Record->getLambdaManglingNumber()) {
720 // This lambda has no mangling number, so it's internal.
721 return LinkageInfo::internal();
722 }
723
724 // This lambda has its linkage/visibility determined by its owner.
725 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
726 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
727 if (isa<ParmVarDecl>(ContextDecl))
728 DC = ContextDecl->getDeclContext()->getRedeclContext();
729 else
Rafael Espindola1266b612012-04-21 23:28:21 +0000730 return getLVForDecl(cast<NamedDecl>(ContextDecl),
731 OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000732 }
733
734 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola1266b612012-04-21 23:28:21 +0000735 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000736
737 return LinkageInfo::external();
738 }
739
740 break;
741 }
Ted Kremenekbecc3082010-04-20 23:15:35 +0000742 }
743
Douglas Gregord85b5b92009-11-25 22:24:25 +0000744 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000745 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola1266b612012-04-21 23:28:21 +0000746 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000747
748 // C++ [basic.link]p5:
749 // In addition, a member function, static data member, a named
750 // class or enumeration of class scope, or an unnamed class or
751 // enumeration defined in a class-scope typedef declaration such
752 // that the class or enumeration has the typedef name for linkage
753 // purposes (7.1.3), has external linkage if the name of the class
754 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000755 if (D->getDeclContext()->isRecord())
Rafael Espindola1266b612012-04-21 23:28:21 +0000756 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000757
758 // C++ [basic.link]p6:
759 // The name of a function declared in block scope and the name of
760 // an object declared by a block scope extern declaration have
761 // linkage. If there is a visible declaration of an entity with
762 // linkage having the same name and type, ignoring entities
763 // declared outside the innermost enclosing namespace scope, the
764 // block scope declaration declares that same entity and receives
765 // the linkage of the previous declaration. If there is more than
766 // one such matching entity, the program is ill-formed. Otherwise,
767 // if no matching entity is found, the block scope entity receives
768 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000769 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
770 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000771 if (Function->isInAnonymousNamespace() &&
772 !Function->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000773 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000774
John McCallaf146032010-10-30 11:50:40 +0000775 LinkageInfo LV;
Rafael Espindola1266b612012-04-21 23:28:21 +0000776 if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000777 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000778 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000779 }
Rafael Espindolab2829202012-11-29 16:38:22 +0000780
781 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
782 // merging storage classes and visibility attributes, so we don't have to
783 // look at previous decls in here.
John McCall1fb0caa2010-10-22 21:05:15 +0000784
785 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000786 }
787
John McCall0df95872010-10-29 00:29:13 +0000788 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000789 if (Var->getStorageClass() == SC_Extern ||
790 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman750dc2b2012-01-15 01:23:58 +0000791 if (Var->isInAnonymousNamespace() &&
792 !Var->getDeclContext()->isExternCContext())
John McCallaf146032010-10-30 11:50:40 +0000793 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000794
John McCallaf146032010-10-30 11:50:40 +0000795 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000796 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000797 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola1266b612012-04-21 23:28:21 +0000798 else if (!OnlyTemplate) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000799 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola5727cf52012-04-19 02:22:07 +0000800 LV.mergeVisibility(*Vis, true);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000801 }
John McCall1fb0caa2010-10-22 21:05:15 +0000802
Rafael Espindola538fb982012-11-12 04:10:23 +0000803 // Note that Sema::MergeVarDecl already takes care of implementing
804 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
805 // have to do it here.
John McCall1fb0caa2010-10-22 21:05:15 +0000806 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000807 }
808 }
809
810 // C++ [basic.link]p6:
811 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000812 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000813}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000814
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000815std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +0000816 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000817}
818
819std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000820 const DeclContext *Ctx = getDeclContext();
821
822 if (Ctx->isFunctionOrMethod())
823 return getNameAsString();
824
Chris Lattner5f9e2722011-07-23 10:55:15 +0000825 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000826 ContextsTy Contexts;
827
828 // Collect contexts.
829 while (Ctx && isa<NamedDecl>(Ctx)) {
830 Contexts.push_back(Ctx);
831 Ctx = Ctx->getParent();
832 };
833
834 std::string QualName;
835 llvm::raw_string_ostream OS(QualName);
836
837 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
838 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000839 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000840 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000841 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
842 std::string TemplateArgsStr
843 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000844 TemplateArgs.data(),
845 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000846 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000847 OS << Spec->getName() << TemplateArgsStr;
848 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000849 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000850 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000851 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000852 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000853 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
854 if (!RD->getIdentifier())
855 OS << "<anonymous " << RD->getKindName() << '>';
856 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000857 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000858 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000859 const FunctionProtoType *FT = 0;
860 if (FD->hasWrittenPrototype())
Eli Friedman482466b2012-08-30 22:22:09 +0000861 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinig3521d012009-12-28 03:19:38 +0000862
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000863 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000864 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000865 unsigned NumParams = FD->getNumParams();
866 for (unsigned i = 0; i < NumParams; ++i) {
867 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000868 OS << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000869 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinig3521d012009-12-28 03:19:38 +0000870 }
871
872 if (FT->isVariadic()) {
873 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000874 OS << ", ";
875 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000876 }
877 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000878 OS << ')';
879 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000880 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000881 }
882 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000883 }
884
John McCall8472af42010-03-16 21:48:18 +0000885 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000886 OS << *this;
John McCall8472af42010-03-16 21:48:18 +0000887 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000888 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000889
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000890 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000891}
892
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000893bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000894 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
895
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000896 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
897 // We want to keep it, unless it nominates same namespace.
898 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000899 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
900 ->getOriginalNamespace() ==
901 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
902 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000905 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
906 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000907 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000908
Douglas Gregore53060f2009-06-25 22:08:12 +0000909 // For function templates, the underlying function declarations are linked.
910 if (const FunctionTemplateDecl *FunctionTemplate
911 = dyn_cast<FunctionTemplateDecl>(this))
912 if (const FunctionTemplateDecl *OldFunctionTemplate
913 = dyn_cast<FunctionTemplateDecl>(OldD))
914 return FunctionTemplate->getTemplatedDecl()
915 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Steve Naroff0de21fd2009-02-22 19:35:57 +0000917 // For method declarations, we keep track of redeclarations.
918 if (isa<ObjCMethodDecl>(this))
919 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000920
John McCallf36e02d2009-10-09 21:13:30 +0000921 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
922 return true;
923
John McCall9488ea12009-11-17 05:59:44 +0000924 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
925 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
926 cast<UsingShadowDecl>(OldD)->getTargetDecl();
927
Douglas Gregordc355712011-02-25 00:36:19 +0000928 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
929 ASTContext &Context = getASTContext();
930 return Context.getCanonicalNestedNameSpecifier(
931 cast<UsingDecl>(this)->getQualifier()) ==
932 Context.getCanonicalNestedNameSpecifier(
933 cast<UsingDecl>(OldD)->getQualifier());
934 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000935
Douglas Gregor7a537402012-01-03 23:26:26 +0000936 // A typedef of an Objective-C class type can replace an Objective-C class
937 // declaration or definition, and vice versa.
938 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
939 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
940 return true;
941
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000942 // For non-function declarations, if the declarations are of the
943 // same kind then this must be a redeclaration, or semantic analysis
944 // would not have given us the new declaration.
945 return this->getKind() == OldD->getKind();
946}
947
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000948bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000949 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000950}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000951
Daniel Dunbar6daffa52012-03-08 18:20:41 +0000952NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +0000953 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +0000954 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
955 ND = UD->getTargetDecl();
956
957 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
958 return AD->getClassInterface();
959
960 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +0000961}
962
John McCall161755a2010-04-06 21:38:20 +0000963bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +0000964 if (!isCXXClassMember())
965 return false;
966
John McCall161755a2010-04-06 21:38:20 +0000967 const NamedDecl *D = this;
968 if (isa<UsingShadowDecl>(D))
969 D = cast<UsingShadowDecl>(D)->getTargetDecl();
970
Francois Pichet87c2e122010-11-21 06:08:52 +0000971 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000972 return true;
973 if (isa<CXXMethodDecl>(D))
974 return cast<CXXMethodDecl>(D)->isInstance();
975 if (isa<FunctionTemplateDecl>(D))
976 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
977 ->getTemplatedDecl())->isInstance();
978 return false;
979}
980
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000981//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000982// DeclaratorDecl Implementation
983//===----------------------------------------------------------------------===//
984
Douglas Gregor1693e152010-07-06 18:42:40 +0000985template <typename DeclT>
986static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
987 if (decl->getNumTemplateParameterLists() > 0)
988 return decl->getTemplateParameterList(0)->getTemplateLoc();
989 else
990 return decl->getInnerLocStart();
991}
992
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000993SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000994 TypeSourceInfo *TSI = getTypeSourceInfo();
995 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000996 return SourceLocation();
997}
998
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000999void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1000 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001001 // Make sure the extended decl info is allocated.
1002 if (!hasExtInfo()) {
1003 // Save (non-extended) type source info pointer.
1004 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1005 // Allocate external info struct.
1006 DeclInfo = new (getASTContext()) ExtInfo;
1007 // Restore savedTInfo into (extended) decl info.
1008 getExtInfo()->TInfo = savedTInfo;
1009 }
1010 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001011 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001012 } else {
John McCallb6217662010-03-15 10:12:16 +00001013 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001014 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001015 if (getExtInfo()->NumTemplParamLists == 0) {
1016 // Save type source info pointer.
1017 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1018 // Deallocate the extended decl info.
1019 getASTContext().Deallocate(getExtInfo());
1020 // Restore savedTInfo into (non-extended) decl info.
1021 DeclInfo = savedTInfo;
1022 }
1023 else
1024 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001025 }
1026 }
1027}
1028
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001029void
1030DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1031 unsigned NumTPLists,
1032 TemplateParameterList **TPLists) {
1033 assert(NumTPLists > 0);
1034 // Make sure the extended decl info is allocated.
1035 if (!hasExtInfo()) {
1036 // Save (non-extended) type source info pointer.
1037 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1038 // Allocate external info struct.
1039 DeclInfo = new (getASTContext()) ExtInfo;
1040 // Restore savedTInfo into (extended) decl info.
1041 getExtInfo()->TInfo = savedTInfo;
1042 }
1043 // Set the template parameter lists info.
1044 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1045}
1046
Douglas Gregor1693e152010-07-06 18:42:40 +00001047SourceLocation DeclaratorDecl::getOuterLocStart() const {
1048 return getTemplateOrInnerLocStart(this);
1049}
1050
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001051namespace {
1052
1053// Helper function: returns true if QT is or contains a type
1054// having a postfix component.
1055bool typeIsPostfix(clang::QualType QT) {
1056 while (true) {
1057 const Type* T = QT.getTypePtr();
1058 switch (T->getTypeClass()) {
1059 default:
1060 return false;
1061 case Type::Pointer:
1062 QT = cast<PointerType>(T)->getPointeeType();
1063 break;
1064 case Type::BlockPointer:
1065 QT = cast<BlockPointerType>(T)->getPointeeType();
1066 break;
1067 case Type::MemberPointer:
1068 QT = cast<MemberPointerType>(T)->getPointeeType();
1069 break;
1070 case Type::LValueReference:
1071 case Type::RValueReference:
1072 QT = cast<ReferenceType>(T)->getPointeeType();
1073 break;
1074 case Type::PackExpansion:
1075 QT = cast<PackExpansionType>(T)->getPattern();
1076 break;
1077 case Type::Paren:
1078 case Type::ConstantArray:
1079 case Type::DependentSizedArray:
1080 case Type::IncompleteArray:
1081 case Type::VariableArray:
1082 case Type::FunctionProto:
1083 case Type::FunctionNoProto:
1084 return true;
1085 }
1086 }
1087}
1088
1089} // namespace
1090
1091SourceRange DeclaratorDecl::getSourceRange() const {
1092 SourceLocation RangeEnd = getLocation();
1093 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1094 if (typeIsPostfix(TInfo->getType()))
1095 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1096 }
1097 return SourceRange(getOuterLocStart(), RangeEnd);
1098}
1099
Abramo Bagnara9b934882010-06-12 08:15:14 +00001100void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001101QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1102 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001103 TemplateParameterList **TPLists) {
1104 assert((NumTPLists == 0 || TPLists != 0) &&
1105 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001106
1107 // Free previous template parameters (if any).
1108 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001109 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001110 TemplParamLists = 0;
1111 NumTemplParamLists = 0;
1112 }
1113 // Set info on matched template parameter lists (if any).
1114 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001115 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001116 NumTemplParamLists = NumTPLists;
1117 for (unsigned i = NumTPLists; i-- > 0; )
1118 TemplParamLists[i] = TPLists[i];
1119 }
1120}
1121
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001122//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001123// VarDecl Implementation
1124//===----------------------------------------------------------------------===//
1125
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001126const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1127 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001128 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001129 case SC_Auto: return "auto";
1130 case SC_Extern: return "extern";
1131 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1132 case SC_PrivateExtern: return "__private_extern__";
1133 case SC_Register: return "register";
1134 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001135 }
1136
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001137 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001138}
1139
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001140VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1141 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001142 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001143 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001144 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001145}
1146
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001147VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1148 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1149 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1150 QualType(), 0, SC_None, SC_None);
1151}
1152
Douglas Gregor381d34e2010-12-06 18:36:25 +00001153void VarDecl::setStorageClass(StorageClass SC) {
1154 assert(isLegalForVariable(SC));
1155 if (getStorageClass() != SC)
1156 ClearLinkageCache();
1157
John McCallf1e4fbf2011-05-01 02:13:58 +00001158 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001159}
1160
Douglas Gregor1693e152010-07-06 18:42:40 +00001161SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisd69f31c2012-10-08 23:08:41 +00001162 if (const Expr *Init = getInit()) {
1163 SourceLocation InitEnd = Init->getLocEnd();
1164 if (InitEnd.isValid())
1165 return SourceRange(getOuterLocStart(), InitEnd);
1166 }
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001167 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001168}
1169
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001170bool VarDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001171 if (getLinkage() != ExternalLinkage)
Chandler Carruth10aad442011-02-25 00:05:02 +00001172 return false;
1173
Eli Friedman750dc2b2012-01-15 01:23:58 +00001174 const DeclContext *DC = getDeclContext();
1175 if (DC->isRecord())
1176 return false;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001177
Eli Friedman750dc2b2012-01-15 01:23:58 +00001178 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001179 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001180 return true;
1181 return DC->isExternCContext();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001182}
1183
1184VarDecl *VarDecl::getCanonicalDecl() {
1185 return getFirstDeclaration();
1186}
1187
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001188VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1189 ASTContext &C) const
1190{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001191 // C++ [basic.def]p2:
1192 // A declaration is a definition unless [...] it contains the 'extern'
1193 // specifier or a linkage-specification and neither an initializer [...],
1194 // it declares a static data member in a class declaration [...].
1195 // C++ [temp.expl.spec]p15:
1196 // An explicit specialization of a static data member of a template is a
1197 // definition if the declaration includes an initializer; otherwise, it is
1198 // a declaration.
1199 if (isStaticDataMember()) {
1200 if (isOutOfLine() && (hasInit() ||
1201 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1202 return Definition;
1203 else
1204 return DeclarationOnly;
1205 }
1206 // C99 6.7p5:
1207 // A definition of an identifier is a declaration for that identifier that
1208 // [...] causes storage to be reserved for that object.
1209 // Note: that applies for all non-file-scope objects.
1210 // C99 6.9.2p1:
1211 // If the declaration of an identifier for an object has file scope and an
1212 // initializer, the declaration is an external definition for the identifier
1213 if (hasInit())
1214 return Definition;
1215 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1216 if (hasExternalStorage())
1217 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001218
John McCalld931b082010-08-26 03:08:43 +00001219 if (getStorageClassAsWritten() == SC_Extern ||
1220 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregoref96ee02012-01-14 16:38:05 +00001221 for (const VarDecl *PrevVar = getPreviousDecl();
1222 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001223 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1224 return DeclarationOnly;
1225 }
1226 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001227 // C99 6.9.2p2:
1228 // A declaration of an object that has file scope without an initializer,
1229 // and without a storage class specifier or the scs 'static', constitutes
1230 // a tentative definition.
1231 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001232 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001233 return TentativeDefinition;
1234
1235 // What's left is (in C, block-scope) declarations without initializers or
1236 // external storage. These are definitions.
1237 return Definition;
1238}
1239
Sebastian Redle9d12b62010-01-31 22:27:38 +00001240VarDecl *VarDecl::getActingDefinition() {
1241 DefinitionKind Kind = isThisDeclarationADefinition();
1242 if (Kind != TentativeDefinition)
1243 return 0;
1244
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001245 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001246 VarDecl *First = getFirstDeclaration();
1247 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1248 I != E; ++I) {
1249 Kind = (*I)->isThisDeclarationADefinition();
1250 if (Kind == Definition)
1251 return 0;
1252 else if (Kind == TentativeDefinition)
1253 LastTentative = *I;
1254 }
1255 return LastTentative;
1256}
1257
1258bool VarDecl::isTentativeDefinitionNow() const {
1259 DefinitionKind Kind = isThisDeclarationADefinition();
1260 if (Kind != TentativeDefinition)
1261 return false;
1262
1263 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1264 if ((*I)->isThisDeclarationADefinition() == Definition)
1265 return false;
1266 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001267 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001268}
1269
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001270VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001271 VarDecl *First = getFirstDeclaration();
1272 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1273 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001274 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001275 return *I;
1276 }
1277 return 0;
1278}
1279
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001280VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001281 DefinitionKind Kind = DeclarationOnly;
1282
1283 const VarDecl *First = getFirstDeclaration();
1284 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001285 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001286 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001287 if (Kind == Definition)
1288 break;
1289 }
John McCall110e8e52010-10-29 22:22:43 +00001290
1291 return Kind;
1292}
1293
Sebastian Redl31310a22010-02-01 20:16:42 +00001294const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001295 redecl_iterator I = redecls_begin(), E = redecls_end();
1296 while (I != E && !I->getInit())
1297 ++I;
1298
1299 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001300 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001301 return I->getInit();
1302 }
1303 return 0;
1304}
1305
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001306bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001307 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001308 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001309
1310 if (!isStaticDataMember())
1311 return false;
1312
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001313 // If this static data member was instantiated from a static data member of
1314 // a class template, check whether that static data member was defined
1315 // out-of-line.
1316 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1317 return VD->isOutOfLine();
1318
1319 return false;
1320}
1321
Douglas Gregor0d035142009-10-27 18:42:08 +00001322VarDecl *VarDecl::getOutOfLineDefinition() {
1323 if (!isStaticDataMember())
1324 return 0;
1325
1326 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1327 RD != RDEnd; ++RD) {
1328 if (RD->getLexicalDeclContext()->isFileContext())
1329 return *RD;
1330 }
1331
1332 return 0;
1333}
1334
Douglas Gregor838db382010-02-11 01:19:42 +00001335void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001336 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1337 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001338 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001339 }
1340
1341 Init = I;
1342}
1343
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001344bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001345 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001346
Richard Smith16581332012-03-02 04:14:40 +00001347 if (!Lang.CPlusPlus)
1348 return false;
1349
1350 // In C++11, any variable of reference type can be used in a constant
1351 // expression if it is initialized by a constant expression.
1352 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1353 return true;
1354
1355 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001356 // not require the variable to be non-volatile, but we consider this to be a
1357 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001358 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001359 return false;
1360
1361 // In C++, const, non-volatile variables of integral or enumeration types
1362 // can be used in constant expressions.
1363 if (getType()->isIntegralOrEnumerationType())
1364 return true;
1365
Richard Smith16581332012-03-02 04:14:40 +00001366 // Additionally, in C++11, non-volatile constexpr variables can be used in
1367 // constant expressions.
1368 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001369}
1370
Richard Smith099e7f62011-12-19 06:19:21 +00001371/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1372/// form, which contains extra information on the evaluated value of the
1373/// initializer.
1374EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1375 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1376 if (!Eval) {
1377 Stmt *S = Init.get<Stmt *>();
1378 Eval = new (getASTContext()) EvaluatedStmt;
1379 Eval->Value = S;
1380 Init = Eval;
1381 }
1382 return Eval;
1383}
1384
Richard Smith2d6a5672012-01-14 04:30:29 +00001385APValue *VarDecl::evaluateValue() const {
1386 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1387 return evaluateValue(Notes);
1388}
1389
1390APValue *VarDecl::evaluateValue(
1391 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001392 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1393
1394 // We only produce notes indicating why an initializer is non-constant the
1395 // first time it is evaluated. FIXME: The notes won't always be emitted the
1396 // first time we try evaluation, so might not be produced at all.
1397 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001398 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001399
1400 const Expr *Init = cast<Expr>(Eval->Value);
1401 assert(!Init->isValueDependent());
1402
1403 if (Eval->IsEvaluating) {
1404 // FIXME: Produce a diagnostic for self-initialization.
1405 Eval->CheckedICE = true;
1406 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001407 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001408 }
1409
1410 Eval->IsEvaluating = true;
1411
1412 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1413 this, Notes);
1414
1415 // Ensure the result is an uninitialized APValue if evaluation fails.
1416 if (!Result)
1417 Eval->Evaluated = APValue();
1418
1419 Eval->IsEvaluating = false;
1420 Eval->WasEvaluated = true;
1421
1422 // In C++11, we have determined whether the initializer was a constant
1423 // expression as a side-effect.
David Blaikie4e4d0842012-03-11 07:00:24 +00001424 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001425 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001426 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001427 }
1428
Richard Smith2d6a5672012-01-14 04:30:29 +00001429 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001430}
1431
1432bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001433 // Initializers of weak variables are never ICEs.
1434 if (isWeak())
1435 return false;
1436
Richard Smith099e7f62011-12-19 06:19:21 +00001437 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1438 if (Eval->CheckedICE)
1439 // We have already checked whether this subexpression is an
1440 // integral constant expression.
1441 return Eval->IsICE;
1442
1443 const Expr *Init = cast<Expr>(Eval->Value);
1444 assert(!Init->isValueDependent());
1445
1446 // In C++11, evaluate the initializer to check whether it's a constant
1447 // expression.
David Blaikie4e4d0842012-03-11 07:00:24 +00001448 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smith099e7f62011-12-19 06:19:21 +00001449 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1450 evaluateValue(Notes);
1451 return Eval->IsICE;
1452 }
1453
1454 // It's an ICE whether or not the definition we found is
1455 // out-of-line. See DR 721 and the discussion in Clang PR
1456 // 6206 for details.
1457
1458 if (Eval->CheckingICE)
1459 return false;
1460 Eval->CheckingICE = true;
1461
1462 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1463 Eval->CheckingICE = false;
1464 Eval->CheckedICE = true;
1465 return Eval->IsICE;
1466}
1467
Douglas Gregor03e80032011-06-21 17:03:29 +00001468bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001469 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001470
1471 const Expr *E = getInit();
1472 if (!E)
1473 return false;
1474
1475 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1476 E = Cleanups->getSubExpr();
1477
1478 return isa<MaterializeTemporaryExpr>(E);
1479}
1480
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001481VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001482 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001483 return cast<VarDecl>(MSI->getInstantiatedFrom());
1484
1485 return 0;
1486}
1487
Douglas Gregor663b5a02009-10-14 20:14:33 +00001488TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001489 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001490 return MSI->getTemplateSpecializationKind();
1491
1492 return TSK_Undeclared;
1493}
1494
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001495MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001496 return getASTContext().getInstantiatedFromStaticDataMember(this);
1497}
1498
Douglas Gregor0a897e32009-10-15 17:21:20 +00001499void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1500 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001501 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001502 assert(MSI && "Not an instantiated static data member?");
1503 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001504 if (TSK != TSK_ExplicitSpecialization &&
1505 PointOfInstantiation.isValid() &&
1506 MSI->getPointOfInstantiation().isInvalid())
1507 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001508}
1509
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001510//===----------------------------------------------------------------------===//
1511// ParmVarDecl Implementation
1512//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001513
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001514ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001515 SourceLocation StartLoc,
1516 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001517 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001518 StorageClass S, StorageClass SCAsWritten,
1519 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001520 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001521 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001522}
1523
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001524ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1525 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1526 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1527 0, QualType(), 0, SC_None, SC_None, 0);
1528}
1529
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00001530SourceRange ParmVarDecl::getSourceRange() const {
1531 if (!hasInheritedDefaultArg()) {
1532 SourceRange ArgRange = getDefaultArgRange();
1533 if (ArgRange.isValid())
1534 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1535 }
1536
1537 return DeclaratorDecl::getSourceRange();
1538}
1539
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001540Expr *ParmVarDecl::getDefaultArg() {
1541 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1542 assert(!hasUninstantiatedDefaultArg() &&
1543 "Default argument is not yet instantiated!");
1544
1545 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001546 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001547 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001548
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001549 return Arg;
1550}
1551
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001552SourceRange ParmVarDecl::getDefaultArgRange() const {
1553 if (const Expr *E = getInit())
1554 return E->getSourceRange();
1555
1556 if (hasUninstantiatedDefaultArg())
1557 return getUninstantiatedDefaultArg()->getSourceRange();
1558
1559 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001560}
1561
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001562bool ParmVarDecl::isParameterPack() const {
1563 return isa<PackExpansionType>(getType());
1564}
1565
Ted Kremenekd211cb72011-10-06 05:00:56 +00001566void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1567 getASTContext().setParameterIndex(this, parameterIndex);
1568 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1569}
1570
1571unsigned ParmVarDecl::getParameterIndexLarge() const {
1572 return getASTContext().getParameterIndex(this);
1573}
1574
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001575//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001576// FunctionDecl Implementation
1577//===----------------------------------------------------------------------===//
1578
Douglas Gregorda2142f2011-02-19 18:51:44 +00001579void FunctionDecl::getNameForDiagnostic(std::string &S,
1580 const PrintingPolicy &Policy,
1581 bool Qualified) const {
1582 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1583 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1584 if (TemplateArgs)
1585 S += TemplateSpecializationType::PrintTemplateArgumentList(
1586 TemplateArgs->data(),
1587 TemplateArgs->size(),
1588 Policy);
1589
1590}
1591
Ted Kremenek9498d382010-04-29 16:49:01 +00001592bool FunctionDecl::isVariadic() const {
1593 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1594 return FT->isVariadic();
1595 return false;
1596}
1597
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001598bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1599 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001600 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001601 Definition = *I;
1602 return true;
1603 }
1604 }
1605
1606 return false;
1607}
1608
Anders Carlssonffb945f2011-05-14 23:26:09 +00001609bool FunctionDecl::hasTrivialBody() const
1610{
1611 Stmt *S = getBody();
1612 if (!S) {
1613 // Since we don't have a body for this function, we don't know if it's
1614 // trivial or not.
1615 return false;
1616 }
1617
1618 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1619 return true;
1620 return false;
1621}
1622
Sean Hunt10620eb2011-05-06 20:44:56 +00001623bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1624 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00001625 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00001626 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1627 return true;
1628 }
1629 }
1630
1631 return false;
1632}
1633
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001634Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001635 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1636 if (I->Body) {
1637 Definition = *I;
1638 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001639 } else if (I->IsLateTemplateParsed) {
1640 Definition = *I;
1641 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001642 }
1643 }
1644
1645 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001646}
1647
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001648void FunctionDecl::setBody(Stmt *B) {
1649 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001650 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001651 EndRangeLoc = B->getLocEnd();
1652}
1653
Douglas Gregor21386642010-09-28 21:55:22 +00001654void FunctionDecl::setPure(bool P) {
1655 IsPure = P;
1656 if (P)
1657 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1658 Parent->markedVirtualFunctionPure();
1659}
1660
Douglas Gregor48a83b52009-09-12 00:17:51 +00001661bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00001662 const TranslationUnitDecl *tunit =
1663 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1664 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001665 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00001666 getIdentifier() &&
1667 getIdentifier()->isStr("main");
1668}
1669
1670bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1671 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1672 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1673 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1674 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1675 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1676
1677 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1678 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1679
1680 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1681 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1682
1683 ASTContext &Context =
1684 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1685 ->getASTContext();
1686
1687 // The result type and first argument type are constant across all
1688 // these operators. The second argument must be exactly void*.
1689 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00001690}
1691
Douglas Gregor48a83b52009-09-12 00:17:51 +00001692bool FunctionDecl::isExternC() const {
Eli Friedman750dc2b2012-01-15 01:23:58 +00001693 if (getLinkage() != ExternalLinkage)
1694 return false;
1695
1696 if (getAttr<OverloadableAttr>())
1697 return false;
Douglas Gregor63935192009-03-02 00:19:53 +00001698
Chandler Carruth10aad442011-02-25 00:05:02 +00001699 const DeclContext *DC = getDeclContext();
1700 if (DC->isRecord())
1701 return false;
1702
Eli Friedman750dc2b2012-01-15 01:23:58 +00001703 ASTContext &Context = getASTContext();
David Blaikie4e4d0842012-03-11 07:00:24 +00001704 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman750dc2b2012-01-15 01:23:58 +00001705 return true;
Douglas Gregor63935192009-03-02 00:19:53 +00001706
Eli Friedman750dc2b2012-01-15 01:23:58 +00001707 return isMain() || DC->isExternCContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001708}
1709
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001710bool FunctionDecl::isGlobal() const {
1711 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1712 return Method->isStatic();
1713
John McCalld931b082010-08-26 03:08:43 +00001714 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001715 return false;
1716
Mike Stump1eb44332009-09-09 15:08:12 +00001717 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001718 DC->isNamespace();
1719 DC = DC->getParent()) {
1720 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1721 if (!Namespace->getDeclName())
1722 return false;
1723 break;
1724 }
1725 }
1726
1727 return true;
1728}
1729
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001730void
1731FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1732 redeclarable_base::setPreviousDeclaration(PrevDecl);
1733
1734 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1735 FunctionTemplateDecl *PrevFunTmpl
1736 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1737 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1738 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1739 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001740
Axel Naumannd9d137e2011-11-08 18:21:06 +00001741 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00001742 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001743}
1744
1745const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1746 return getFirstDeclaration();
1747}
1748
1749FunctionDecl *FunctionDecl::getCanonicalDecl() {
1750 return getFirstDeclaration();
1751}
1752
Douglas Gregor381d34e2010-12-06 18:36:25 +00001753void FunctionDecl::setStorageClass(StorageClass SC) {
1754 assert(isLegalForFunction(SC));
1755 if (getStorageClass() != SC)
1756 ClearLinkageCache();
1757
1758 SClass = SC;
1759}
1760
Douglas Gregor3e41d602009-02-13 23:20:09 +00001761/// \brief Returns a value indicating whether this function
1762/// corresponds to a builtin function.
1763///
1764/// The function corresponds to a built-in function if it is
1765/// declared at translation scope or within an extern "C" block and
1766/// its name matches with the name of a builtin. The returned value
1767/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001768/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001769/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001770unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001771 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001772 return 0;
1773
1774 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00001775 if (!BuiltinID)
1776 return 0;
1777
1778 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001779 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1780 return BuiltinID;
1781
1782 // This function has the name of a known C library
1783 // function. Determine whether it actually refers to the C library
1784 // function or whether it just has the same name.
1785
Douglas Gregor9add3172009-02-17 03:23:10 +00001786 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001787 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001788 return 0;
1789
Douglas Gregor3c385e52009-02-14 18:57:46 +00001790 // If this function is at translation-unit scope and we're not in
1791 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00001792 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00001793 getDeclContext()->isTranslationUnit())
1794 return BuiltinID;
1795
1796 // If the function is in an extern "C" linkage specification and is
1797 // not marked "overloadable", it's the real function.
1798 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001799 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001800 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001801 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001802 return BuiltinID;
1803
1804 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001805 return 0;
1806}
1807
1808
Chris Lattner1ad9b282009-04-25 06:03:53 +00001809/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001810/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001811/// after it has been created.
1812unsigned FunctionDecl::getNumParams() const {
Eli Friedman482466b2012-08-30 22:22:09 +00001813 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001814 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001815 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001816 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Reid Spencer5f016e22007-07-11 17:01:13 +00001818}
1819
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001820void FunctionDecl::setParams(ASTContext &C,
David Blaikie4278c652011-09-21 18:16:56 +00001821 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001822 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00001823 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Reid Spencer5f016e22007-07-11 17:01:13 +00001825 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00001826 if (!NewParamInfo.empty()) {
1827 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1828 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 }
1830}
1831
James Molloy16f1f712012-02-29 10:24:19 +00001832void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1833 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1834
1835 if (!NewDecls.empty()) {
1836 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1837 std::copy(NewDecls.begin(), NewDecls.end(), A);
1838 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1839 }
1840}
1841
Chris Lattner8123a952008-04-10 02:22:51 +00001842/// getMinRequiredArguments - Returns the minimum number of arguments
1843/// needed to call this function. This may be fewer than the number of
1844/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001845/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001846unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001847 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001848 return getNumParams();
1849
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001850 unsigned NumRequiredArgs = getNumParams();
1851
1852 // If the last parameter is a parameter pack, we don't need an argument for
1853 // it.
1854 if (NumRequiredArgs > 0 &&
1855 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1856 --NumRequiredArgs;
1857
1858 // If this parameter has a default argument, we don't need an argument for
1859 // it.
1860 while (NumRequiredArgs > 0 &&
1861 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001862 --NumRequiredArgs;
1863
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001864 // We might have parameter packs before the end. These can't be deduced,
1865 // but they can still handle multiple arguments.
1866 unsigned ArgIdx = NumRequiredArgs;
1867 while (ArgIdx > 0) {
1868 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1869 NumRequiredArgs = ArgIdx;
1870
1871 --ArgIdx;
1872 }
1873
Chris Lattner8123a952008-04-10 02:22:51 +00001874 return NumRequiredArgs;
1875}
1876
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001877bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001878 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001879 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001880
1881 if (isa<CXXMethodDecl>(this)) {
1882 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1883 return true;
1884 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001885
1886 switch (getTemplateSpecializationKind()) {
1887 case TSK_Undeclared:
1888 case TSK_ExplicitSpecialization:
1889 return false;
1890
1891 case TSK_ImplicitInstantiation:
1892 case TSK_ExplicitInstantiationDeclaration:
1893 case TSK_ExplicitInstantiationDefinition:
1894 // Handle below.
1895 break;
1896 }
1897
1898 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001899 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001900 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001901 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001902
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001903 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001904 return PatternDecl->isInlined();
1905
1906 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001907}
1908
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001909static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1910 // Only consider file-scope declarations in this test.
1911 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1912 return false;
1913
1914 // Only consider explicit declarations; the presence of a builtin for a
1915 // libcall shouldn't affect whether a definition is externally visible.
1916 if (Redecl->isImplicit())
1917 return false;
1918
1919 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1920 return true; // Not an inline definition
1921
1922 return false;
1923}
1924
Nick Lewyckydce67a72011-07-18 05:26:13 +00001925/// \brief For a function declaration in C or C++, determine whether this
1926/// declaration causes the definition to be externally visible.
1927///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001928/// Specifically, this determines if adding the current declaration to the set
1929/// of redeclarations of the given functions causes
1930/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00001931bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1932 assert(!doesThisDeclarationHaveABody() &&
1933 "Must have a declaration without a body.");
1934
1935 ASTContext &Context = getASTContext();
1936
David Blaikie4e4d0842012-03-11 07:00:24 +00001937 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001938 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1939 // an externally visible definition.
1940 //
1941 // FIXME: What happens if gnu_inline gets added on after the first
1942 // declaration?
1943 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1944 return false;
1945
1946 const FunctionDecl *Prev = this;
1947 bool FoundBody = false;
1948 while ((Prev = Prev->getPreviousDecl())) {
1949 FoundBody |= Prev->Body;
1950
1951 if (Prev->Body) {
1952 // If it's not the case that both 'inline' and 'extern' are
1953 // specified on the definition, then it is always externally visible.
1954 if (!Prev->isInlineSpecified() ||
1955 Prev->getStorageClassAsWritten() != SC_Extern)
1956 return false;
1957 } else if (Prev->isInlineSpecified() &&
1958 Prev->getStorageClassAsWritten() != SC_Extern) {
1959 return false;
1960 }
1961 }
1962 return FoundBody;
1963 }
1964
David Blaikie4e4d0842012-03-11 07:00:24 +00001965 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001966 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001967
1968 // C99 6.7.4p6:
1969 // [...] If all of the file scope declarations for a function in a
1970 // translation unit include the inline function specifier without extern,
1971 // then the definition in that translation unit is an inline definition.
1972 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00001973 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00001974 const FunctionDecl *Prev = this;
1975 bool FoundBody = false;
1976 while ((Prev = Prev->getPreviousDecl())) {
1977 FoundBody |= Prev->Body;
1978 if (RedeclForcesDefC99(Prev))
1979 return false;
1980 }
1981 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00001982}
1983
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001984/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001985/// definition will be externally visible.
1986///
1987/// Inline function definitions are always available for inlining optimizations.
1988/// However, depending on the language dialect, declaration specifiers, and
1989/// attributes, the definition of an inline function may or may not be
1990/// "externally" visible to other translation units in the program.
1991///
1992/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001993/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001994/// inline definition becomes externally visible (C99 6.7.4p6).
1995///
1996/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1997/// definition, we use the GNU semantics for inline, which are nearly the
1998/// opposite of C99 semantics. In particular, "inline" by itself will create
1999/// an externally visible symbol, but "extern inline" will not create an
2000/// externally visible symbol.
2001bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002002 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002003 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002004 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002005
David Blaikie4e4d0842012-03-11 07:00:24 +00002006 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002007 // Note: If you change the logic here, please change
2008 // doesDeclarationForceExternallyVisibleDefinition as well.
2009 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002010 // If it's not the case that both 'inline' and 'extern' are
2011 // specified on the definition, then this inline definition is
2012 // externally visible.
2013 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2014 return true;
2015
2016 // If any declaration is 'inline' but not 'extern', then this definition
2017 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002018 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2019 Redecl != RedeclEnd;
2020 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002021 if (Redecl->isInlineSpecified() &&
2022 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002023 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002024 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002025
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002026 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002027 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002028
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002029 // C99 6.7.4p6:
2030 // [...] If all of the file scope declarations for a function in a
2031 // translation unit include the inline function specifier without extern,
2032 // then the definition in that translation unit is an inline definition.
2033 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2034 Redecl != RedeclEnd;
2035 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002036 if (RedeclForcesDefC99(*Redecl))
2037 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002038 }
2039
2040 // C99 6.7.4p6:
2041 // An inline definition does not provide an external definition for the
2042 // function, and does not forbid an external definition in another
2043 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002044 return false;
2045}
2046
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002047/// getOverloadedOperator - Which C++ overloaded operator this
2048/// function represents, if any.
2049OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002050 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2051 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002052 else
2053 return OO_None;
2054}
2055
Sean Hunta6c058d2010-01-13 09:01:02 +00002056/// getLiteralIdentifier - The literal suffix identifier this function
2057/// represents, if any.
2058const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2059 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2060 return getDeclName().getCXXLiteralIdentifier();
2061 else
2062 return 0;
2063}
2064
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002065FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2066 if (TemplateOrSpecialization.isNull())
2067 return TK_NonTemplate;
2068 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2069 return TK_FunctionTemplate;
2070 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2071 return TK_MemberSpecialization;
2072 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2073 return TK_FunctionTemplateSpecialization;
2074 if (TemplateOrSpecialization.is
2075 <DependentFunctionTemplateSpecializationInfo*>())
2076 return TK_DependentFunctionTemplateSpecialization;
2077
David Blaikieb219cfc2011-09-23 05:06:16 +00002078 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002079}
2080
Douglas Gregor2db32322009-10-07 23:56:10 +00002081FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002082 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002083 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2084
2085 return 0;
2086}
2087
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002088MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2089 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2090}
2091
Douglas Gregor2db32322009-10-07 23:56:10 +00002092void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002093FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2094 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002095 TemplateSpecializationKind TSK) {
2096 assert(TemplateOrSpecialization.isNull() &&
2097 "Member function is already a specialization");
2098 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002099 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002100 TemplateOrSpecialization = Info;
2101}
2102
Douglas Gregor3b846b62009-10-27 20:53:28 +00002103bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002104 // If the function is invalid, it can't be implicitly instantiated.
2105 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002106 return false;
2107
2108 switch (getTemplateSpecializationKind()) {
2109 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002110 case TSK_ExplicitInstantiationDefinition:
2111 return false;
2112
2113 case TSK_ImplicitInstantiation:
2114 return true;
2115
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002116 // It is possible to instantiate TSK_ExplicitSpecialization kind
2117 // if the FunctionDecl has a class scope specialization pattern.
2118 case TSK_ExplicitSpecialization:
2119 return getClassScopeSpecializationPattern() != 0;
2120
Douglas Gregor3b846b62009-10-27 20:53:28 +00002121 case TSK_ExplicitInstantiationDeclaration:
2122 // Handled below.
2123 break;
2124 }
2125
2126 // Find the actual template from which we will instantiate.
2127 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002128 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002129 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002130 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002131
2132 // C++0x [temp.explicit]p9:
2133 // Except for inline functions, other explicit instantiation declarations
2134 // have the effect of suppressing the implicit instantiation of the entity
2135 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002136 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002137 return true;
2138
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002139 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002140}
2141
2142bool FunctionDecl::isTemplateInstantiation() const {
2143 switch (getTemplateSpecializationKind()) {
2144 case TSK_Undeclared:
2145 case TSK_ExplicitSpecialization:
2146 return false;
2147 case TSK_ImplicitInstantiation:
2148 case TSK_ExplicitInstantiationDeclaration:
2149 case TSK_ExplicitInstantiationDefinition:
2150 return true;
2151 }
2152 llvm_unreachable("All TSK values handled.");
2153}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002154
2155FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002156 // Handle class scope explicit specialization special case.
2157 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2158 return getClassScopeSpecializationPattern();
2159
Douglas Gregor3b846b62009-10-27 20:53:28 +00002160 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2161 while (Primary->getInstantiatedFromMemberTemplate()) {
2162 // If we have hit a point where the user provided a specialization of
2163 // this template, we're done looking.
2164 if (Primary->isMemberSpecialization())
2165 break;
2166
2167 Primary = Primary->getInstantiatedFromMemberTemplate();
2168 }
2169
2170 return Primary->getTemplatedDecl();
2171 }
2172
2173 return getInstantiatedFromMemberFunction();
2174}
2175
Douglas Gregor16e8be22009-06-29 17:30:29 +00002176FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002177 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002178 = TemplateOrSpecialization
2179 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002180 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002181 }
2182 return 0;
2183}
2184
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002185FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2186 return getASTContext().getClassScopeSpecializationPattern(this);
2187}
2188
Douglas Gregor16e8be22009-06-29 17:30:29 +00002189const TemplateArgumentList *
2190FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002191 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002192 = TemplateOrSpecialization
2193 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002194 return Info->TemplateArguments;
2195 }
2196 return 0;
2197}
2198
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002199const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002200FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2201 if (FunctionTemplateSpecializationInfo *Info
2202 = TemplateOrSpecialization
2203 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2204 return Info->TemplateArgumentsAsWritten;
2205 }
2206 return 0;
2207}
2208
Mike Stump1eb44332009-09-09 15:08:12 +00002209void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002210FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2211 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002212 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002213 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002214 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002215 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2216 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002217 assert(TSK != TSK_Undeclared &&
2218 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002219 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002220 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002221 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002222 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2223 TemplateArgs,
2224 TemplateArgsAsWritten,
2225 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002226 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002227 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002228}
2229
John McCallaf2094e2010-04-08 09:05:18 +00002230void
2231FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2232 const UnresolvedSetImpl &Templates,
2233 const TemplateArgumentListInfo &TemplateArgs) {
2234 assert(TemplateOrSpecialization.isNull());
2235 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2236 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002237 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002238 void *Buffer = Context.Allocate(Size);
2239 DependentFunctionTemplateSpecializationInfo *Info =
2240 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2241 TemplateArgs);
2242 TemplateOrSpecialization = Info;
2243}
2244
2245DependentFunctionTemplateSpecializationInfo::
2246DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2247 const TemplateArgumentListInfo &TArgs)
2248 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2249
2250 d.NumTemplates = Ts.size();
2251 d.NumArgs = TArgs.size();
2252
2253 FunctionTemplateDecl **TsArray =
2254 const_cast<FunctionTemplateDecl**>(getTemplates());
2255 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2256 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2257
2258 TemplateArgumentLoc *ArgsArray =
2259 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2260 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2261 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2262}
2263
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002264TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002265 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002266 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002267 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002268 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002269 if (FTSInfo)
2270 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Douglas Gregor2db32322009-10-07 23:56:10 +00002272 MemberSpecializationInfo *MSInfo
2273 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2274 if (MSInfo)
2275 return MSInfo->getTemplateSpecializationKind();
2276
2277 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002278}
2279
Mike Stump1eb44332009-09-09 15:08:12 +00002280void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002281FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2282 SourceLocation PointOfInstantiation) {
2283 if (FunctionTemplateSpecializationInfo *FTSInfo
2284 = TemplateOrSpecialization.dyn_cast<
2285 FunctionTemplateSpecializationInfo*>()) {
2286 FTSInfo->setTemplateSpecializationKind(TSK);
2287 if (TSK != TSK_ExplicitSpecialization &&
2288 PointOfInstantiation.isValid() &&
2289 FTSInfo->getPointOfInstantiation().isInvalid())
2290 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2291 } else if (MemberSpecializationInfo *MSInfo
2292 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2293 MSInfo->setTemplateSpecializationKind(TSK);
2294 if (TSK != TSK_ExplicitSpecialization &&
2295 PointOfInstantiation.isValid() &&
2296 MSInfo->getPointOfInstantiation().isInvalid())
2297 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2298 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002299 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002300}
2301
2302SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002303 if (FunctionTemplateSpecializationInfo *FTSInfo
2304 = TemplateOrSpecialization.dyn_cast<
2305 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002306 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002307 else if (MemberSpecializationInfo *MSInfo
2308 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002309 return MSInfo->getPointOfInstantiation();
2310
2311 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002312}
2313
Douglas Gregor9f185072009-09-11 20:15:17 +00002314bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002315 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002316 return true;
2317
2318 // If this function was instantiated from a member function of a
2319 // class template, check whether that member function was defined out-of-line.
2320 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2321 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002322 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002323 return Definition->isOutOfLine();
2324 }
2325
2326 // If this function was instantiated from a function template,
2327 // check whether that function template was defined out-of-line.
2328 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2329 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002330 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002331 return Definition->isOutOfLine();
2332 }
2333
2334 return false;
2335}
2336
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002337SourceRange FunctionDecl::getSourceRange() const {
2338 return SourceRange(getOuterLocStart(), EndRangeLoc);
2339}
2340
Anna Zaks9392d4e2012-01-18 02:45:01 +00002341unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002342 IdentifierInfo *FnInfo = getIdentifier();
2343
2344 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002345 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002346
2347 // Builtin handling.
2348 switch (getBuiltinID()) {
2349 case Builtin::BI__builtin_memset:
2350 case Builtin::BI__builtin___memset_chk:
2351 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002352 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002353
2354 case Builtin::BI__builtin_memcpy:
2355 case Builtin::BI__builtin___memcpy_chk:
2356 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002357 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002358
2359 case Builtin::BI__builtin_memmove:
2360 case Builtin::BI__builtin___memmove_chk:
2361 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002362 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002363
2364 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002365 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002366 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002367 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002368
2369 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002370 case Builtin::BImemcmp:
2371 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002372
2373 case Builtin::BI__builtin_strncpy:
2374 case Builtin::BI__builtin___strncpy_chk:
2375 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002376 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002377
2378 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002379 case Builtin::BIstrncmp:
2380 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002381
2382 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002383 case Builtin::BIstrncasecmp:
2384 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002385
2386 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002387 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002388 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002389 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002390
2391 case Builtin::BI__builtin_strndup:
2392 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002393 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002394
Anna Zaksc36bedc2012-02-01 19:08:57 +00002395 case Builtin::BI__builtin_strlen:
2396 case Builtin::BIstrlen:
2397 return Builtin::BIstrlen;
2398
Anna Zaksd9b859a2012-01-13 21:52:01 +00002399 default:
Eli Friedman750dc2b2012-01-15 01:23:58 +00002400 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002401 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002402 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002403 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002404 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002405 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002406 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002407 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002408 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002409 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002410 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002411 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002412 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002413 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002414 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002415 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002416 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002417 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002418 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002419 else if (FnInfo->isStr("strlen"))
2420 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002421 }
2422 break;
2423 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002424 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002425}
2426
Chris Lattner8a934232008-03-31 00:36:02 +00002427//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002428// FieldDecl Implementation
2429//===----------------------------------------------------------------------===//
2430
Jay Foad4ba2a172011-01-12 09:06:06 +00002431FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002432 SourceLocation StartLoc, SourceLocation IdLoc,
2433 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002434 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smithca523302012-06-10 03:12:00 +00002435 InClassInitStyle InitStyle) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002436 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithca523302012-06-10 03:12:00 +00002437 BW, Mutable, InitStyle);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002438}
2439
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002440FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2441 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2442 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smithca523302012-06-10 03:12:00 +00002443 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002444}
2445
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002446bool FieldDecl::isAnonymousStructOrUnion() const {
2447 if (!isImplicit() || getDeclName())
2448 return false;
2449
2450 if (const RecordType *Record = getType()->getAs<RecordType>())
2451 return Record->getDecl()->isAnonymousStructOrUnion();
2452
2453 return false;
2454}
2455
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002456unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2457 assert(isBitField() && "not a bitfield");
2458 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2459 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2460}
2461
John McCallba4f5d52011-01-20 07:57:12 +00002462unsigned FieldDecl::getFieldIndex() const {
2463 if (CachedFieldIndex) return CachedFieldIndex - 1;
2464
Richard Smith180f4792011-11-10 06:34:14 +00002465 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002466 const RecordDecl *RD = getParent();
2467 const FieldDecl *LastFD = 0;
Eli Friedman5f608ae2012-10-12 23:29:20 +00002468 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smith180f4792011-11-10 06:34:14 +00002469
2470 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2471 I != E; ++I, ++Index) {
David Blaikie262bc182012-04-30 02:36:29 +00002472 I->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002473
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002474 if (IsMsStruct) {
2475 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie581deb32012-06-06 20:45:41 +00002476 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smith180f4792011-11-10 06:34:14 +00002477 --Index;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002478 continue;
2479 }
David Blaikie581deb32012-06-06 20:45:41 +00002480 LastFD = *I;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002481 }
John McCallba4f5d52011-01-20 07:57:12 +00002482 }
2483
Richard Smith180f4792011-11-10 06:34:14 +00002484 assert(CachedFieldIndex && "failed to find field in parent");
2485 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002486}
2487
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002488SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002489 if (const Expr *E = InitializerOrBitWidth.getPointer())
2490 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002491 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002492}
2493
Abramo Bagnaraa5335762012-07-02 20:35:48 +00002494void FieldDecl::setBitWidth(Expr *Width) {
2495 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2496 "bit width or initializer already set");
2497 InitializerOrBitWidth.setPointer(Width);
2498}
2499
Richard Smith7a614d82011-06-11 17:19:42 +00002500void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smithca523302012-06-10 03:12:00 +00002501 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith7a614d82011-06-11 17:19:42 +00002502 "bit width or initializer already set");
2503 InitializerOrBitWidth.setPointer(Init);
Richard Smith7a614d82011-06-11 17:19:42 +00002504}
2505
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002506//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002507// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002508//===----------------------------------------------------------------------===//
2509
Douglas Gregor1693e152010-07-06 18:42:40 +00002510SourceLocation TagDecl::getOuterLocStart() const {
2511 return getTemplateOrInnerLocStart(this);
2512}
2513
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002514SourceRange TagDecl::getSourceRange() const {
2515 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002516 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002517}
2518
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002519TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002520 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002521}
2522
Richard Smith162e1c12011-04-15 14:24:37 +00002523void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2524 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002525 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002526 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002527 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002528}
2529
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002530void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002531 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002532
David Blaikie66cff722012-11-14 01:52:05 +00002533 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall86ff3082010-02-04 22:26:26 +00002534 struct CXXRecordDecl::DefinitionData *Data =
2535 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002536 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2537 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002538 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002539}
2540
2541void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002542 assert((!isa<CXXRecordDecl>(this) ||
2543 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2544 "definition completed but not started");
2545
John McCall5e1cdac2011-10-07 06:10:15 +00002546 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002547 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002548
2549 if (ASTMutationListener *L = getASTMutationListener())
2550 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002551}
2552
John McCall5e1cdac2011-10-07 06:10:15 +00002553TagDecl *TagDecl::getDefinition() const {
2554 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002555 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002556 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2557 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002558
2559 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002560 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00002561 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002562 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002563
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002564 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002565}
2566
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002567void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2568 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002569 // Make sure the extended qualifier info is allocated.
2570 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002571 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002572 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002573 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00002574 } else {
John McCallb6217662010-03-15 10:12:16 +00002575 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002576 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002577 if (getExtInfo()->NumTemplParamLists == 0) {
2578 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002579 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002580 }
2581 else
2582 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002583 }
2584 }
2585}
2586
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002587void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2588 unsigned NumTPLists,
2589 TemplateParameterList **TPLists) {
2590 assert(NumTPLists > 0);
2591 // Make sure the extended decl info is allocated.
2592 if (!hasExtInfo())
2593 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002594 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002595 // Set the template parameter lists info.
2596 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2597}
2598
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002599//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002600// EnumDecl Implementation
2601//===----------------------------------------------------------------------===//
2602
David Blaikie99ba9e32011-12-20 02:48:34 +00002603void EnumDecl::anchor() { }
2604
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002605EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2606 SourceLocation StartLoc, SourceLocation IdLoc,
2607 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002608 EnumDecl *PrevDecl, bool IsScoped,
2609 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002610 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002611 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002612 C.getTypeDeclType(Enum, PrevDecl);
2613 return Enum;
2614}
2615
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002616EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2617 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2618 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2619 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002620}
2621
Douglas Gregor838db382010-02-11 01:19:42 +00002622void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002623 QualType NewPromotionType,
2624 unsigned NumPositiveBits,
2625 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00002626 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002627 if (!IntegerType)
2628 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002629 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002630 setNumPositiveBits(NumPositiveBits);
2631 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002632 TagDecl::completeDefinition();
2633}
2634
Richard Smith1af83c42012-03-23 03:33:32 +00002635TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2636 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2637 return MSI->getTemplateSpecializationKind();
2638
2639 return TSK_Undeclared;
2640}
2641
2642void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2643 SourceLocation PointOfInstantiation) {
2644 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2645 assert(MSI && "Not an instantiated member enumeration?");
2646 MSI->setTemplateSpecializationKind(TSK);
2647 if (TSK != TSK_ExplicitSpecialization &&
2648 PointOfInstantiation.isValid() &&
2649 MSI->getPointOfInstantiation().isInvalid())
2650 MSI->setPointOfInstantiation(PointOfInstantiation);
2651}
2652
Richard Smithf1c66b42012-03-14 23:13:10 +00002653EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2654 if (SpecializationInfo)
2655 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2656
2657 return 0;
2658}
2659
2660void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2661 TemplateSpecializationKind TSK) {
2662 assert(!SpecializationInfo && "Member enum is already a specialization");
2663 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2664}
2665
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002666//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002667// RecordDecl Implementation
2668//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002669
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002670RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2671 SourceLocation StartLoc, SourceLocation IdLoc,
2672 IdentifierInfo *Id, RecordDecl *PrevDecl)
2673 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002674 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002675 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002676 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002677 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002678 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002679}
2680
Jay Foad4ba2a172011-01-12 09:06:06 +00002681RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002682 SourceLocation StartLoc, SourceLocation IdLoc,
2683 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2684 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2685 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002686 C.getTypeDeclType(R, PrevDecl);
2687 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002688}
2689
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002690RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2691 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2692 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2693 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002694}
2695
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002696bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002697 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002698 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2699}
2700
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002701RecordDecl::field_iterator RecordDecl::field_begin() const {
2702 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2703 LoadFieldsFromExternalStorage();
2704
2705 return field_iterator(decl_iterator(FirstDecl));
2706}
2707
Douglas Gregorda2142f2011-02-19 18:51:44 +00002708/// completeDefinition - Notes that the definition of this type is now
2709/// complete.
2710void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00002711 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00002712 TagDecl::completeDefinition();
2713}
2714
Eli Friedman5f608ae2012-10-12 23:29:20 +00002715/// isMsStruct - Get whether or not this record uses ms_struct layout.
2716/// This which can be turned on with an attribute, pragma, or the
2717/// -mms-bitfields command-line option.
2718bool RecordDecl::isMsStruct(const ASTContext &C) const {
2719 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2720}
2721
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002722static bool isFieldOrIndirectField(Decl::Kind K) {
2723 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2724}
2725
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002726void RecordDecl::LoadFieldsFromExternalStorage() const {
2727 ExternalASTSource *Source = getASTContext().getExternalSource();
2728 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2729
2730 // Notify that we have a RecordDecl doing some initialization.
2731 ExternalASTSource::Deserializing TheFields(Source);
2732
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002734 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002735 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2736 Decls)) {
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002737 case ELR_Success:
2738 break;
2739
2740 case ELR_AlreadyLoaded:
2741 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002742 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00002743 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002744
2745#ifndef NDEBUG
2746 // Check that all decls we got were FieldDecls.
2747 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00002748 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002749#endif
2750
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002751 if (Decls.empty())
2752 return;
2753
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00002754 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2755 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002756}
2757
Steve Naroff56ee6892008-10-08 17:01:13 +00002758//===----------------------------------------------------------------------===//
2759// BlockDecl Implementation
2760//===----------------------------------------------------------------------===//
2761
David Blaikie4278c652011-09-21 18:16:56 +00002762void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00002763 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002764
Steve Naroffe78b8092009-03-13 16:56:44 +00002765 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002766 if (!NewParamInfo.empty()) {
2767 NumParams = NewParamInfo.size();
2768 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2769 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00002770 }
2771}
2772
John McCall6b5a61b2011-02-07 10:33:21 +00002773void BlockDecl::setCaptures(ASTContext &Context,
2774 const Capture *begin,
2775 const Capture *end,
2776 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002777 CapturesCXXThis = capturesCXXThis;
2778
2779 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002780 NumCaptures = 0;
2781 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002782 return;
2783 }
2784
John McCall6b5a61b2011-02-07 10:33:21 +00002785 NumCaptures = end - begin;
2786
2787 // Avoid new Capture[] because we don't want to provide a default
2788 // constructor.
2789 size_t allocationSize = NumCaptures * sizeof(Capture);
2790 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2791 memcpy(buffer, begin, allocationSize);
2792 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002793}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002794
John McCall204e1332011-06-15 22:51:16 +00002795bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2796 for (capture_const_iterator
2797 i = capture_begin(), e = capture_end(); i != e; ++i)
2798 // Only auto vars can be captured, so no redeclaration worries.
2799 if (i->getVariable() == variable)
2800 return true;
2801
2802 return false;
2803}
2804
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002805SourceRange BlockDecl::getSourceRange() const {
2806 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2807}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002808
2809//===----------------------------------------------------------------------===//
2810// Other Decl Allocation/Deallocation Method Implementations
2811//===----------------------------------------------------------------------===//
2812
David Blaikie99ba9e32011-12-20 02:48:34 +00002813void TranslationUnitDecl::anchor() { }
2814
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002815TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2816 return new (C) TranslationUnitDecl(C);
2817}
2818
David Blaikie99ba9e32011-12-20 02:48:34 +00002819void LabelDecl::anchor() { }
2820
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002821LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002822 SourceLocation IdentL, IdentifierInfo *II) {
2823 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2824}
2825
2826LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2827 SourceLocation IdentL, IdentifierInfo *II,
2828 SourceLocation GnuLabelL) {
2829 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2830 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002831}
2832
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002833LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2834 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2835 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00002836}
2837
David Blaikie99ba9e32011-12-20 02:48:34 +00002838void ValueDecl::anchor() { }
2839
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +00002840bool ValueDecl::isWeak() const {
2841 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
2842 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
2843 return true;
2844
2845 return isWeakImported();
2846}
2847
David Blaikie99ba9e32011-12-20 02:48:34 +00002848void ImplicitParamDecl::anchor() { }
2849
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002850ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002851 SourceLocation IdLoc,
2852 IdentifierInfo *Id,
2853 QualType Type) {
2854 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002855}
2856
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002857ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2858 unsigned ID) {
2859 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2860 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2861}
2862
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002863FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002864 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002865 const DeclarationNameInfo &NameInfo,
2866 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002867 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002868 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002869 bool hasWrittenPrototype,
2870 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002871 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2872 T, TInfo, SC, SCAsWritten,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002873 isInlineSpecified,
2874 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002875 New->HasWrittenPrototype = hasWrittenPrototype;
2876 return New;
2877}
2878
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002879FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2880 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2881 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2882 DeclarationNameInfo(), QualType(), 0,
2883 SC_None, SC_None, false, false);
2884}
2885
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002886BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2887 return new (C) BlockDecl(DC, L);
2888}
2889
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002890BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2891 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2892 return new (Mem) BlockDecl(0, SourceLocation());
2893}
2894
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002895EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2896 SourceLocation L,
2897 IdentifierInfo *Id, QualType T,
2898 Expr *E, const llvm::APSInt &V) {
2899 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2900}
2901
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002902EnumConstantDecl *
2903EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2904 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2905 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2906 llvm::APSInt());
2907}
2908
David Blaikie99ba9e32011-12-20 02:48:34 +00002909void IndirectFieldDecl::anchor() { }
2910
Benjamin Kramerd9811462010-11-21 14:11:41 +00002911IndirectFieldDecl *
2912IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2913 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2914 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002915 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2916}
2917
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002918IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2919 unsigned ID) {
2920 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2921 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2922 QualType(), 0, 0);
2923}
2924
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002925SourceRange EnumConstantDecl::getSourceRange() const {
2926 SourceLocation End = getLocation();
2927 if (Init)
2928 End = Init->getLocEnd();
2929 return SourceRange(getLocation(), End);
2930}
2931
David Blaikie99ba9e32011-12-20 02:48:34 +00002932void TypeDecl::anchor() { }
2933
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002934TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002935 SourceLocation StartLoc, SourceLocation IdLoc,
2936 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2937 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002938}
2939
David Blaikie99ba9e32011-12-20 02:48:34 +00002940void TypedefNameDecl::anchor() { }
2941
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002942TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2943 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2944 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2945}
2946
Richard Smith162e1c12011-04-15 14:24:37 +00002947TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2948 SourceLocation StartLoc,
2949 SourceLocation IdLoc, IdentifierInfo *Id,
2950 TypeSourceInfo *TInfo) {
2951 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2952}
2953
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002954TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2955 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2956 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2957}
2958
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002959SourceRange TypedefDecl::getSourceRange() const {
2960 SourceLocation RangeEnd = getLocation();
2961 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2962 if (typeIsPostfix(TInfo->getType()))
2963 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2964 }
2965 return SourceRange(getLocStart(), RangeEnd);
2966}
2967
Richard Smith162e1c12011-04-15 14:24:37 +00002968SourceRange TypeAliasDecl::getSourceRange() const {
2969 SourceLocation RangeEnd = getLocStart();
2970 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2971 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2972 return SourceRange(getLocStart(), RangeEnd);
2973}
2974
David Blaikie99ba9e32011-12-20 02:48:34 +00002975void FileScopeAsmDecl::anchor() { }
2976
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002977FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002978 StringLiteral *Str,
2979 SourceLocation AsmLoc,
2980 SourceLocation RParenLoc) {
2981 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002982}
Douglas Gregor15de72c2011-12-02 23:23:56 +00002983
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002984FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2985 unsigned ID) {
2986 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2987 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2988}
2989
Douglas Gregor15de72c2011-12-02 23:23:56 +00002990//===----------------------------------------------------------------------===//
2991// ImportDecl Implementation
2992//===----------------------------------------------------------------------===//
2993
2994/// \brief Retrieve the number of module identifiers needed to name the given
2995/// module.
2996static unsigned getNumModuleIdentifiers(Module *Mod) {
2997 unsigned Result = 1;
2998 while (Mod->Parent) {
2999 Mod = Mod->Parent;
3000 ++Result;
3001 }
3002 return Result;
3003}
3004
Douglas Gregor5948ae12012-01-03 18:04:46 +00003005ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003006 Module *Imported,
3007 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003008 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003009 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003010{
3011 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3012 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3013 memcpy(StoredLocs, IdentifierLocs.data(),
3014 IdentifierLocs.size() * sizeof(SourceLocation));
3015}
3016
Douglas Gregor5948ae12012-01-03 18:04:46 +00003017ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003018 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003019 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003020 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003021{
3022 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3023}
3024
3025ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003026 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003027 ArrayRef<SourceLocation> IdentifierLocs) {
3028 void *Mem = C.Allocate(sizeof(ImportDecl) +
3029 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003030 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003031}
3032
3033ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003034 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003035 Module *Imported,
3036 SourceLocation EndLoc) {
3037 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003038 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003039 Import->setImplicit();
3040 return Import;
3041}
3042
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003043ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3044 unsigned NumLocations) {
3045 void *Mem = AllocateDeserializedDecl(C, ID,
3046 (sizeof(ImportDecl) +
3047 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003048 return new (Mem) ImportDecl(EmptyShell());
3049}
3050
3051ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3052 if (!ImportedAndComplete.getInt())
3053 return ArrayRef<SourceLocation>();
3054
3055 const SourceLocation *StoredLocs
3056 = reinterpret_cast<const SourceLocation *>(this + 1);
3057 return ArrayRef<SourceLocation>(StoredLocs,
3058 getNumModuleIdentifiers(getImportedModule()));
3059}
3060
3061SourceRange ImportDecl::getSourceRange() const {
3062 if (!ImportedAndComplete.getInt())
3063 return SourceRange(getLocation(),
3064 *reinterpret_cast<const SourceLocation *>(this + 1));
3065
3066 return SourceRange(getLocation(), getIdentifierLocs().back());
3067}