blob: f934c76a4193d6f2e54e786e200ab076230e1feb [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"
John McCall3892d022013-02-21 23:42:58 +000032#include "llvm/Support/type_traits.h"
David Blaikie4278c652011-09-21 18:16:56 +000033#include <algorithm>
34
Reid Spencer5f016e22007-07-11 17:01:13 +000035using namespace clang;
36
Chris Lattnerd3b90652008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
John McCall5a758de2013-02-16 00:17:33 +000041// Visibility rules aren't rigorously externally specified, but here
42// are the basic principles behind what we implement:
43//
44// 1. An explicit visibility attribute is generally a direct expression
45// of the user's intent and should be honored. Only the innermost
46// visibility attribute applies. If no visibility attribute applies,
47// global visibility settings are considered.
48//
49// 2. There is one caveat to the above: on or in a template pattern,
50// an explicit visibility attribute is just a default rule, and
51// visibility can be decreased by the visibility of template
52// arguments. But this, too, has an exception: an attribute on an
53// explicit specialization or instantiation causes all the visibility
54// restrictions of the template arguments to be ignored.
55//
56// 3. A variable that does not otherwise have explicit visibility can
57// be restricted by the visibility of its type.
58//
59// 4. A visibility restriction is explicit if it comes from an
60// attribute (or something like it), not a global visibility setting.
61// When emitting a reference to an external symbol, visibility
62// restrictions are ignored unless they are explicit.
John McCalld4c3d662013-02-20 01:54:26 +000063//
64// 5. When computing the visibility of a non-type, including a
65// non-type member of a class, only non-type visibility restrictions
66// are considered: the 'visibility' attribute, global value-visibility
67// settings, and a few special cases like __private_extern.
68//
69// 6. When computing the visibility of a type, including a type member
70// of a class, only type visibility restrictions are considered:
71// the 'type_visibility' attribute and global type-visibility settings.
72// However, a 'visibility' attribute counts as a 'type_visibility'
73// attribute on any declaration that only has the former.
74//
75// The visibility of a "secondary" entity, like a template argument,
76// is computed using the kind of that entity, not the kind of the
77// primary entity for which we are computing visibility. For example,
78// the visibility of a specialization of either of these templates:
79// template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X);
80// template <class T, bool (&compare)(T, X)> class matcher;
81// is restricted according to the type visibility of the argument 'T',
82// the type visibility of 'bool(&)(T,X)', and the value visibility of
83// the argument function 'compare'. That 'has_match' is a value
84// and 'matcher' is a type only matters when looking for attributes
85// and settings from the immediate context.
John McCall5a758de2013-02-16 00:17:33 +000086
John McCall3892d022013-02-21 23:42:58 +000087const unsigned IgnoreExplicitVisibilityBit = 2;
Rafael Espindoladc070562013-05-28 19:43:11 +000088const unsigned IgnoreAllVisibilityBit = 4;
John McCall3892d022013-02-21 23:42:58 +000089
John McCall5a758de2013-02-16 00:17:33 +000090/// Kinds of LV computation. The linkage side of the computation is
91/// always the same, but different things can change how visibility is
92/// computed.
93enum LVComputationKind {
John McCall3892d022013-02-21 23:42:58 +000094 /// Do an LV computation for, ultimately, a type.
95 /// Visibility may be restricted by type visibility settings and
96 /// the visibility of template arguments.
John McCalld4c3d662013-02-20 01:54:26 +000097 LVForType = NamedDecl::VisibilityForType,
John McCall5a758de2013-02-16 00:17:33 +000098
John McCall3892d022013-02-21 23:42:58 +000099 /// Do an LV computation for, ultimately, a non-type declaration.
100 /// Visibility may be restricted by value visibility settings and
101 /// the visibility of template arguments.
John McCalld4c3d662013-02-20 01:54:26 +0000102 LVForValue = NamedDecl::VisibilityForValue,
103
John McCall3892d022013-02-21 23:42:58 +0000104 /// Do an LV computation for, ultimately, a type that already has
105 /// some sort of explicit visibility. Visibility may only be
106 /// restricted by the visibility of template arguments.
107 LVForExplicitType = (LVForType | IgnoreExplicitVisibilityBit),
John McCalld4c3d662013-02-20 01:54:26 +0000108
John McCall3892d022013-02-21 23:42:58 +0000109 /// Do an LV computation for, ultimately, a non-type declaration
110 /// that already has some sort of explicit visibility. Visibility
111 /// may only be restricted by the visibility of template arguments.
Rafael Espindoladc070562013-05-28 19:43:11 +0000112 LVForExplicitValue = (LVForValue | IgnoreExplicitVisibilityBit),
113
114 /// Do an LV computation when we only care about the linkage.
115 LVForLinkageOnly =
116 LVForValue | IgnoreExplicitVisibilityBit | IgnoreAllVisibilityBit
John McCall5a758de2013-02-16 00:17:33 +0000117};
118
John McCalld4c3d662013-02-20 01:54:26 +0000119/// Does this computation kind permit us to consider additional
120/// visibility settings from attributes and the like?
121static bool hasExplicitVisibilityAlready(LVComputationKind computation) {
John McCall3892d022013-02-21 23:42:58 +0000122 return ((unsigned(computation) & IgnoreExplicitVisibilityBit) != 0);
John McCalld4c3d662013-02-20 01:54:26 +0000123}
124
125/// Given an LVComputationKind, return one of the same type/value sort
126/// that records that it already has explicit visibility.
127static LVComputationKind
128withExplicitVisibilityAlready(LVComputationKind oldKind) {
129 LVComputationKind newKind =
John McCall3892d022013-02-21 23:42:58 +0000130 static_cast<LVComputationKind>(unsigned(oldKind) |
131 IgnoreExplicitVisibilityBit);
John McCalld4c3d662013-02-20 01:54:26 +0000132 assert(oldKind != LVForType || newKind == LVForExplicitType);
133 assert(oldKind != LVForValue || newKind == LVForExplicitValue);
134 assert(oldKind != LVForExplicitType || newKind == LVForExplicitType);
135 assert(oldKind != LVForExplicitValue || newKind == LVForExplicitValue);
136 return newKind;
137}
138
David Blaikiedc84cd52013-02-20 22:23:23 +0000139static Optional<Visibility> getExplicitVisibility(const NamedDecl *D,
140 LVComputationKind kind) {
John McCalld4c3d662013-02-20 01:54:26 +0000141 assert(!hasExplicitVisibilityAlready(kind) &&
142 "asking for explicit visibility when we shouldn't be");
143 return D->getExplicitVisibility((NamedDecl::ExplicitVisibilityKind) kind);
144}
145
John McCall5a758de2013-02-16 00:17:33 +0000146/// Is the given declaration a "type" or a "value" for the purposes of
147/// visibility computation?
148static bool usesTypeVisibility(const NamedDecl *D) {
John McCalla880b192013-02-19 01:57:35 +0000149 return isa<TypeDecl>(D) ||
150 isa<ClassTemplateDecl>(D) ||
151 isa<ObjCInterfaceDecl>(D);
John McCall5a758de2013-02-16 00:17:33 +0000152}
153
John McCall3892d022013-02-21 23:42:58 +0000154/// Does the given declaration have member specialization information,
155/// and if so, is it an explicit specialization?
156template <class T> static typename
157llvm::enable_if_c<!llvm::is_base_of<RedeclarableTemplateDecl, T>::value,
158 bool>::type
159isExplicitMemberSpecialization(const T *D) {
160 if (const MemberSpecializationInfo *member =
161 D->getMemberSpecializationInfo()) {
162 return member->isExplicitSpecialization();
163 }
164 return false;
165}
166
167/// For templates, this question is easier: a member template can't be
168/// explicitly instantiated, so there's a single bit indicating whether
169/// or not this is an explicit member specialization.
170static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) {
171 return D->isMemberSpecialization();
172}
173
John McCalld4c3d662013-02-20 01:54:26 +0000174/// Given a visibility attribute, return the explicit visibility
175/// associated with it.
176template <class T>
177static Visibility getVisibilityFromAttr(const T *attr) {
178 switch (attr->getVisibility()) {
179 case T::Default:
180 return DefaultVisibility;
181 case T::Hidden:
182 return HiddenVisibility;
183 case T::Protected:
184 return ProtectedVisibility;
185 }
186 llvm_unreachable("bad visibility kind");
187}
188
John McCall5a758de2013-02-16 00:17:33 +0000189/// Return the explicit visibility of the given declaration.
David Blaikiedc84cd52013-02-20 22:23:23 +0000190static Optional<Visibility> getVisibilityOf(const NamedDecl *D,
John McCalld4c3d662013-02-20 01:54:26 +0000191 NamedDecl::ExplicitVisibilityKind kind) {
192 // If we're ultimately computing the visibility of a type, look for
193 // a 'type_visibility' attribute before looking for 'visibility'.
194 if (kind == NamedDecl::VisibilityForType) {
195 if (const TypeVisibilityAttr *A = D->getAttr<TypeVisibilityAttr>()) {
196 return getVisibilityFromAttr(A);
197 }
198 }
199
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000200 // If this declaration has an explicit visibility attribute, use it.
201 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
John McCalld4c3d662013-02-20 01:54:26 +0000202 return getVisibilityFromAttr(A);
John McCall1fb0caa2010-10-22 21:05:15 +0000203 }
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000204
205 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
206 // implies visibility(default).
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000207 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000208 for (specific_attr_iterator<AvailabilityAttr>
209 A = D->specific_attr_begin<AvailabilityAttr>(),
210 AEnd = D->specific_attr_end<AvailabilityAttr>();
211 A != AEnd; ++A)
212 if ((*A)->getPlatform()->getName().equals("macosx"))
213 return DefaultVisibility;
214 }
215
David Blaikie66874fb2013-02-21 01:47:18 +0000216 return None;
John McCall1fb0caa2010-10-22 21:05:15 +0000217}
218
Rafael Espindola74caf012013-05-29 04:55:30 +0000219static LinkageInfo
220getLVForType(const Type &T, LVComputationKind computation) {
221 if (computation == LVForLinkageOnly)
222 return LinkageInfo(T.getLinkage(), DefaultVisibility, true);
223 return T.getLinkageAndVisibility();
224}
225
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000226/// \brief Get the most restrictive linkage for the types in the given
John McCall5a758de2013-02-16 00:17:33 +0000227/// template parameter list. For visibility purposes, template
228/// parameters are part of the signature of a template.
Rafael Espindola093ecc92012-01-14 00:30:36 +0000229static LinkageInfo
Rafael Espindola74caf012013-05-29 04:55:30 +0000230getLVForTemplateParameterList(const TemplateParameterList *params,
231 LVComputationKind computation) {
John McCall5a758de2013-02-16 00:17:33 +0000232 LinkageInfo LV;
233 for (TemplateParameterList::const_iterator P = params->begin(),
234 PEnd = params->end();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000235 P != PEnd; ++P) {
John McCall5a758de2013-02-16 00:17:33 +0000236
237 // Template type parameters are the most common and never
238 // contribute to visibility, pack or not.
239 if (isa<TemplateTypeParmDecl>(*P))
240 continue;
241
242 // Non-type template parameters can be restricted by the value type, e.g.
243 // template <enum X> class A { ... };
244 // We have to be careful here, though, because we can be dealing with
245 // dependent types.
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000246 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
John McCall5a758de2013-02-16 00:17:33 +0000247 // Handle the non-pack case first.
248 if (!NTTP->isExpandedParameterPack()) {
249 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola74caf012013-05-29 04:55:30 +0000250 LV.merge(getLVForType(*NTTP->getType(), computation));
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000251 }
252 continue;
253 }
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000254
John McCall5a758de2013-02-16 00:17:33 +0000255 // Look at all the types in an expanded pack.
256 for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) {
257 QualType type = NTTP->getExpansionType(i);
258 if (!type->isDependentType())
Rafael Espindola18895dc2013-02-27 02:27:19 +0000259 LV.merge(type->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000260 }
John McCall5a758de2013-02-16 00:17:33 +0000261 continue;
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000262 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000263
John McCall5a758de2013-02-16 00:17:33 +0000264 // Template template parameters can be restricted by their
265 // template parameters, recursively.
266 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
267
268 // Handle the non-pack case first.
269 if (!TTP->isExpandedParameterPack()) {
Rafael Espindola74caf012013-05-29 04:55:30 +0000270 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(),
271 computation));
John McCall5a758de2013-02-16 00:17:33 +0000272 continue;
273 }
274
275 // Look at all expansions in an expanded pack.
276 for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters();
277 i != n; ++i) {
278 LV.merge(getLVForTemplateParameterList(
Rafael Espindola74caf012013-05-29 04:55:30 +0000279 TTP->getExpansionTemplateParameters(i), computation));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000280 }
281 }
282
John McCall1fb0caa2010-10-22 21:05:15 +0000283 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000284}
285
Rafael Espindola838dc592013-01-12 06:42:30 +0000286/// getLVForDecl - Get the linkage and visibility for the given declaration.
John McCall5a758de2013-02-16 00:17:33 +0000287static LinkageInfo getLVForDecl(const NamedDecl *D,
288 LVComputationKind computation);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000289
Rafael Espindola1229e202013-05-16 04:30:21 +0000290static const FunctionDecl *getOutermostFunctionContext(const Decl *D) {
291 const FunctionDecl *Ret = NULL;
292 const DeclContext *DC = D->getDeclContext();
293 while (DC->getDeclKind() != Decl::TranslationUnit) {
294 const FunctionDecl *F = dyn_cast<FunctionDecl>(DC);
295 if (F)
296 Ret = F;
297 DC = DC->getParent();
298 }
299 return Ret;
300}
301
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000302/// \brief Get the most restrictive linkage for the types and
303/// declarations in the given template argument list.
John McCall5a758de2013-02-16 00:17:33 +0000304///
305/// Note that we don't take an LVComputationKind because we always
306/// want to honor the visibility of template arguments in the same way.
307static LinkageInfo
Rafael Espindola74caf012013-05-29 04:55:30 +0000308getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args,
309 LVComputationKind computation) {
John McCall5a758de2013-02-16 00:17:33 +0000310 LinkageInfo LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000311
John McCall5a758de2013-02-16 00:17:33 +0000312 for (unsigned i = 0, e = args.size(); i != e; ++i) {
313 const TemplateArgument &arg = args[i];
314 switch (arg.getKind()) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000315 case TemplateArgument::Null:
316 case TemplateArgument::Integral:
317 case TemplateArgument::Expression:
John McCall5a758de2013-02-16 00:17:33 +0000318 continue;
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000319
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000320 case TemplateArgument::Type:
Rafael Espindola74caf012013-05-29 04:55:30 +0000321 LV.merge(getLVForType(*arg.getAsType(), computation));
John McCall5a758de2013-02-16 00:17:33 +0000322 continue;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000323
324 case TemplateArgument::Declaration:
John McCall5a758de2013-02-16 00:17:33 +0000325 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
326 assert(!usesTypeVisibility(ND));
Rafael Espindola74caf012013-05-29 04:55:30 +0000327 LV.merge(getLVForDecl(ND, computation));
John McCall5a758de2013-02-16 00:17:33 +0000328 }
329 continue;
Eli Friedmand7a6b162012-09-26 02:36:12 +0000330
331 case TemplateArgument::NullPtr:
Rafael Espindola18895dc2013-02-27 02:27:19 +0000332 LV.merge(arg.getNullPtrType()->getLinkageAndVisibility());
John McCall5a758de2013-02-16 00:17:33 +0000333 continue;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000334
335 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000336 case TemplateArgument::TemplateExpansion:
Rafael Espindolab5d763d2012-01-02 06:26:22 +0000337 if (TemplateDecl *Template
John McCall5a758de2013-02-16 00:17:33 +0000338 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindola74caf012013-05-29 04:55:30 +0000339 LV.merge(getLVForDecl(Template, computation));
John McCall5a758de2013-02-16 00:17:33 +0000340 continue;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000341
342 case TemplateArgument::Pack:
Rafael Espindola74caf012013-05-29 04:55:30 +0000343 LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray(), computation));
John McCall5a758de2013-02-16 00:17:33 +0000344 continue;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000345 }
John McCall5a758de2013-02-16 00:17:33 +0000346 llvm_unreachable("bad template argument kind");
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000347 }
348
John McCall1fb0caa2010-10-22 21:05:15 +0000349 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000350}
351
Rafael Espindola093ecc92012-01-14 00:30:36 +0000352static LinkageInfo
Rafael Espindola74caf012013-05-29 04:55:30 +0000353getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
354 LVComputationKind computation) {
355 return getLVForTemplateArgumentList(TArgs.asArray(), computation);
John McCall3cdfc4d2010-08-13 08:35:10 +0000356}
357
John McCall3892d022013-02-21 23:42:58 +0000358static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
359 const FunctionTemplateSpecializationInfo *specInfo) {
360 // Include visibility from the template parameters and arguments
361 // only if this is not an explicit instantiation or specialization
362 // with direct explicit visibility. (Implicit instantiations won't
363 // have a direct attribute.)
364 if (!specInfo->isExplicitInstantiationOrSpecialization())
365 return true;
366
367 return !fn->hasAttr<VisibilityAttr>();
368}
369
John McCall5a758de2013-02-16 00:17:33 +0000370/// Merge in template-related linkage and visibility for the given
371/// function template specialization.
372///
373/// We don't need a computation kind here because we can assume
374/// LVForValue.
John McCall3892d022013-02-21 23:42:58 +0000375///
NAKAMURA Takumid9bd83e2013-02-22 04:06:28 +0000376/// \param[out] LV the computation to use for the parent
John McCall3892d022013-02-21 23:42:58 +0000377static void
378mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
Rafael Espindola74caf012013-05-29 04:55:30 +0000379 const FunctionTemplateSpecializationInfo *specInfo,
380 LVComputationKind computation) {
John McCall3892d022013-02-21 23:42:58 +0000381 bool considerVisibility =
382 shouldConsiderTemplateVisibility(fn, specInfo);
John McCall5a758de2013-02-16 00:17:33 +0000383
384 // Merge information from the template parameters.
John McCall3892d022013-02-21 23:42:58 +0000385 FunctionTemplateDecl *temp = specInfo->getTemplate();
John McCall5a758de2013-02-16 00:17:33 +0000386 LinkageInfo tempLV =
Rafael Espindola74caf012013-05-29 04:55:30 +0000387 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCall5a758de2013-02-16 00:17:33 +0000388 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
389
390 // Merge information from the template arguments.
391 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindola74caf012013-05-29 04:55:30 +0000392 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
John McCall5a758de2013-02-16 00:17:33 +0000393 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCall6ce51ee2011-06-27 23:06:04 +0000394}
395
John McCall3892d022013-02-21 23:42:58 +0000396/// Does the given declaration have a direct visibility attribute
397/// that would match the given rules?
398static bool hasDirectVisibilityAttribute(const NamedDecl *D,
399 LVComputationKind computation) {
400 switch (computation) {
401 case LVForType:
402 case LVForExplicitType:
403 if (D->hasAttr<TypeVisibilityAttr>())
404 return true;
405 // fallthrough
406 case LVForValue:
407 case LVForExplicitValue:
408 if (D->hasAttr<VisibilityAttr>())
409 return true;
410 return false;
Rafael Espindoladc070562013-05-28 19:43:11 +0000411 case LVForLinkageOnly:
412 return false;
John McCall3892d022013-02-21 23:42:58 +0000413 }
414 llvm_unreachable("bad visibility computation kind");
415}
416
John McCalld4c3d662013-02-20 01:54:26 +0000417/// Should we consider visibility associated with the template
418/// arguments and parameters of the given class template specialization?
419static bool shouldConsiderTemplateVisibility(
420 const ClassTemplateSpecializationDecl *spec,
421 LVComputationKind computation) {
John McCall5a758de2013-02-16 00:17:33 +0000422 // Include visibility from the template parameters and arguments
423 // only if this is not an explicit instantiation or specialization
424 // with direct explicit visibility (and note that implicit
425 // instantiations won't have a direct attribute).
426 //
427 // Furthermore, we want to ignore template parameters and arguments
John McCalld4c3d662013-02-20 01:54:26 +0000428 // for an explicit specialization when computing the visibility of a
429 // member thereof with explicit visibility.
John McCall5a758de2013-02-16 00:17:33 +0000430 //
431 // This is a bit complex; let's unpack it.
432 //
433 // An explicit class specialization is an independent, top-level
434 // declaration. As such, if it or any of its members has an
435 // explicit visibility attribute, that must directly express the
436 // user's intent, and we should honor it. The same logic applies to
437 // an explicit instantiation of a member of such a thing.
John McCalld4c3d662013-02-20 01:54:26 +0000438
439 // Fast path: if this is not an explicit instantiation or
440 // specialization, we always want to consider template-related
441 // visibility restrictions.
442 if (!spec->isExplicitInstantiationOrSpecialization())
443 return true;
444
445 // This is the 'member thereof' check.
446 if (spec->isExplicitSpecialization() &&
447 hasExplicitVisibilityAlready(computation))
448 return false;
449
John McCall3892d022013-02-21 23:42:58 +0000450 return !hasDirectVisibilityAttribute(spec, computation);
John McCalld4c3d662013-02-20 01:54:26 +0000451}
452
453/// Merge in template-related linkage and visibility for the given
454/// class template specialization.
455static void mergeTemplateLV(LinkageInfo &LV,
456 const ClassTemplateSpecializationDecl *spec,
457 LVComputationKind computation) {
458 bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
John McCall5a758de2013-02-16 00:17:33 +0000459
460 // Merge information from the template parameters, but ignore
461 // visibility if we're only considering template arguments.
462
John McCalld4c3d662013-02-20 01:54:26 +0000463 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
John McCall5a758de2013-02-16 00:17:33 +0000464 LinkageInfo tempLV =
Rafael Espindola74caf012013-05-29 04:55:30 +0000465 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCall5a758de2013-02-16 00:17:33 +0000466 LV.mergeMaybeWithVisibility(tempLV,
John McCalld4c3d662013-02-20 01:54:26 +0000467 considerVisibility && !hasExplicitVisibilityAlready(computation));
John McCall5a758de2013-02-16 00:17:33 +0000468
469 // Merge information from the template arguments. We ignore
470 // template-argument visibility if we've got an explicit
471 // instantiation with a visibility attribute.
472 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
Rafael Espindola74caf012013-05-29 04:55:30 +0000473 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
Rafael Espindolae9808902013-05-30 21:23:15 +0000474 if (considerVisibility)
475 LV.mergeVisibility(argsLV);
476 LV.mergeExternalVisibility(argsLV);
John McCall6ce51ee2011-06-27 23:06:04 +0000477}
478
Rafael Espindolab04b7312012-07-13 14:25:36 +0000479static bool useInlineVisibilityHidden(const NamedDecl *D) {
480 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola0bab9da2012-07-13 23:26:43 +0000481 const LangOptions &Opts = D->getASTContext().getLangOpts();
482 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolab04b7312012-07-13 14:25:36 +0000483 return false;
484
485 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
486 if (!FD)
487 return false;
488
489 TemplateSpecializationKind TSK = TSK_Undeclared;
490 if (FunctionTemplateSpecializationInfo *spec
491 = FD->getTemplateSpecializationInfo()) {
492 TSK = spec->getTemplateSpecializationKind();
493 } else if (MemberSpecializationInfo *MSI =
494 FD->getMemberSpecializationInfo()) {
495 TSK = MSI->getTemplateSpecializationKind();
496 }
497
498 const FunctionDecl *Def = 0;
499 // InlineVisibilityHidden only applies to definitions, and
500 // isInlined() only gives meaningful answers on definitions
501 // anyway.
502 return TSK != TSK_ExplicitInstantiationDeclaration &&
503 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindola0142f0c2012-10-11 16:32:25 +0000504 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolab04b7312012-07-13 14:25:36 +0000505}
506
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +0000507template <typename T> static bool isFirstInExternCContext(T *D) {
Rafael Espindola950fee22013-02-14 01:18:37 +0000508 const T *First = D->getFirstDeclaration();
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +0000509 return First->isInExternCContext();
Rafael Espindola950fee22013-02-14 01:18:37 +0000510}
511
Rafael Espindolae5e575d2013-04-26 01:30:23 +0000512static bool isSingleLineExternC(const Decl &D) {
513 if (const LinkageSpecDecl *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
514 if (SD->getLanguage() == LinkageSpecDecl::lang_c && !SD->hasBraces())
515 return true;
516 return false;
517}
518
Rafael Espindola1266b612012-04-21 23:28:21 +0000519static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCall5a758de2013-02-16 00:17:33 +0000520 LVComputationKind computation) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000521 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000522 "Not a name having namespace scope");
523 ASTContext &Context = D->getASTContext();
524
525 // C++ [basic.link]p3:
526 // A name having namespace scope (3.3.6) has internal linkage if it
527 // is the name of
528 // - an object, reference, function or function template that is
529 // explicitly declared static; or,
530 // (This bullet corresponds to C99 6.2.2p3.)
531 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
532 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000533 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000534 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000535
Richard Smith820e9a72012-10-19 06:37:48 +0000536 // - a non-volatile object or reference that is explicitly declared const
537 // or constexpr and neither explicitly declared extern nor previously
538 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikie4e4d0842012-03-11 07:00:24 +0000539 if (Context.getLangOpts().CPlusPlus &&
Richard Smith820e9a72012-10-19 06:37:48 +0000540 Var->getType().isConstQualified() &&
Rafael Espindolad2615cc2013-04-03 19:27:57 +0000541 !Var->getType().isVolatileQualified()) {
Rafael Espindola4f8a3eb2013-04-03 19:22:20 +0000542 const VarDecl *PrevVar = Var->getPreviousDecl();
Rafael Espindola4f8a3eb2013-04-03 19:22:20 +0000543 if (PrevVar)
Rafael Espindola74caf012013-05-29 04:55:30 +0000544 return getLVForDecl(PrevVar, computation);
Rafael Espindolad2615cc2013-04-03 19:27:57 +0000545
546 if (Var->getStorageClass() != SC_Extern &&
Rafael Espindolae5e575d2013-04-26 01:30:23 +0000547 Var->getStorageClass() != SC_PrivateExtern &&
548 !isSingleLineExternC(*Var))
Rafael Espindolad2615cc2013-04-03 19:27:57 +0000549 return LinkageInfo::internal();
550 }
551
552 for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar;
553 PrevVar = PrevVar->getPreviousDecl()) {
554 if (PrevVar->getStorageClass() == SC_PrivateExtern &&
555 Var->getStorageClass() == SC_None)
556 return PrevVar->getLinkageAndVisibility();
557 // Explicitly declared static.
558 if (PrevVar->getStorageClass() == SC_Static)
559 return LinkageInfo::internal();
Fariborz Jahanianc7c90582011-06-16 20:14:50 +0000560 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000561 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000562 // C++ [temp]p4:
563 // A non-member function template can have internal linkage; any
564 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000565 const FunctionDecl *Function = 0;
566 if (const FunctionTemplateDecl *FunTmpl
567 = dyn_cast<FunctionTemplateDecl>(D))
568 Function = FunTmpl->getTemplatedDecl();
569 else
570 Function = cast<FunctionDecl>(D);
571
572 // Explicitly declared static.
Rafael Espindolad2615cc2013-04-03 19:27:57 +0000573 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000574 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000575 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
576 // - a data member of an anonymous union.
577 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000578 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000579 }
580
Chandler Carruth094b6432011-02-24 19:03:39 +0000581 if (D->isInAnonymousNamespace()) {
582 const VarDecl *Var = dyn_cast<VarDecl>(D);
583 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +0000584 if ((!Var || !isFirstInExternCContext(Var)) &&
585 (!Func || !isFirstInExternCContext(Func)))
Chandler Carruth094b6432011-02-24 19:03:39 +0000586 return LinkageInfo::uniqueExternal();
587 }
John McCalle7bc9722010-10-28 04:18:25 +0000588
John McCall1fb0caa2010-10-22 21:05:15 +0000589 // Set up the defaults.
590
591 // C99 6.2.2p5:
592 // If the declaration of an identifier for an object has file
593 // scope and no storage-class specifier, its linkage is
594 // external.
John McCallaf146032010-10-30 11:50:40 +0000595 LinkageInfo LV;
596
John McCalld4c3d662013-02-20 01:54:26 +0000597 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikiedc84cd52013-02-20 22:23:23 +0000598 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000599 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000600 } else {
601 // If we're declared in a namespace with a visibility attribute,
John McCall5a758de2013-02-16 00:17:33 +0000602 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindolae9836a22012-04-16 18:46:26 +0000603 for (const DeclContext *DC = D->getDeclContext();
604 !isa<TranslationUnitDecl>(DC);
605 DC = DC->getParent()) {
606 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
607 if (!ND) continue;
David Blaikiedc84cd52013-02-20 22:23:23 +0000608 if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
Rafael Espindola5727cf52012-04-19 02:22:07 +0000609 LV.mergeVisibility(*Vis, true);
Rafael Espindolae9836a22012-04-16 18:46:26 +0000610 break;
611 }
612 }
613 }
Rafael Espindolae9836a22012-04-16 18:46:26 +0000614
John McCall5a758de2013-02-16 00:17:33 +0000615 // Add in global settings if the above didn't give us direct visibility.
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000616 if (!LV.isVisibilityExplicit()) {
John McCalla880b192013-02-19 01:57:35 +0000617 // Use global type/value visibility as appropriate.
618 Visibility globalVisibility;
619 if (computation == LVForValue) {
620 globalVisibility = Context.getLangOpts().getValueVisibilityMode();
621 } else {
622 assert(computation == LVForType);
623 globalVisibility = Context.getLangOpts().getTypeVisibilityMode();
624 }
625 LV.mergeVisibility(globalVisibility, /*explicit*/ false);
John McCall5a758de2013-02-16 00:17:33 +0000626
627 // If we're paying attention to global visibility, apply
628 // -finline-visibility-hidden if this is an inline method.
629 if (useInlineVisibilityHidden(D))
630 LV.mergeVisibility(HiddenVisibility, true);
631 }
Rafael Espindolab04b7312012-07-13 14:25:36 +0000632 }
Rafael Espindolaff257982012-04-19 02:55:01 +0000633
Douglas Gregord85b5b92009-11-25 22:24:25 +0000634 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000635
Douglas Gregord85b5b92009-11-25 22:24:25 +0000636 // A name having namespace scope has external linkage if it is the
637 // name of
638 //
639 // - an object or reference, unless it has internal linkage; or
640 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000641 // GCC applies the following optimization to variables and static
642 // data members, but not to functions:
643 //
John McCall1fb0caa2010-10-22 21:05:15 +0000644 // Modify the variable's LV by the LV of its type unless this is
645 // C or extern "C". This follows from [basic.link]p9:
646 // A type without linkage shall not be used as the type of a
647 // variable or function with external linkage unless
648 // - the entity has C language linkage, or
649 // - the entity is declared within an unnamed namespace, or
650 // - the entity is not used or is defined in the same
651 // translation unit.
652 // and [basic.link]p10:
653 // ...the types specified by all declarations referring to a
654 // given variable or function shall be identical...
655 // C does not have an equivalent rule.
656 //
John McCallac65c622010-10-26 04:59:26 +0000657 // Ignore this if we've got an explicit attribute; the user
658 // probably knows what they're doing.
659 //
John McCall1fb0caa2010-10-22 21:05:15 +0000660 // Note that we don't want to make the variable non-external
661 // because of this, but unique-external linkage suits us.
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +0000662 if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
Rafael Espindola74caf012013-05-29 04:55:30 +0000663 LinkageInfo TypeLV = getLVForType(*Var->getType(), computation);
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000664 if (TypeLV.getLinkage() != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000665 return LinkageInfo::uniqueExternal();
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000666 if (!LV.isVisibilityExplicit())
John McCall5a758de2013-02-16 00:17:33 +0000667 LV.mergeVisibility(TypeLV);
John McCall110e8e52010-10-29 22:22:43 +0000668 }
669
John McCall35cebc32010-11-02 18:38:13 +0000670 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000671 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000672
Rafael Espindola538fb982012-11-12 04:10:23 +0000673 // Note that Sema::MergeVarDecl already takes care of implementing
674 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
675 // to do it here.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000676
Douglas Gregord85b5b92009-11-25 22:24:25 +0000677 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000678 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000679 // In theory, we can modify the function's LV by the LV of its
680 // type unless it has C linkage (see comment above about variables
681 // for justification). In practice, GCC doesn't do this, so it's
682 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000683
John McCall35cebc32010-11-02 18:38:13 +0000684 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola5727cf52012-04-19 02:22:07 +0000685 LV.mergeVisibility(HiddenVisibility, true);
John McCall35cebc32010-11-02 18:38:13 +0000686
Rafael Espindola51758612012-11-21 02:47:19 +0000687 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
688 // merging storage classes and visibility attributes, so we don't have to
689 // look at previous decls in here.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000690
John McCallaf8ca372011-02-10 06:50:24 +0000691 // In C++, then if the type of the function uses a type with
692 // unique-external linkage, it's not legally usable from outside
693 // this translation unit. However, we should use the C linkage
694 // rules instead for extern "C" declarations.
David Blaikie4e4d0842012-03-11 07:00:24 +0000695 if (Context.getLangOpts().CPlusPlus &&
Richard Smithd248e582013-05-12 23:17:59 +0000696 !Function->isInExternCContext()) {
697 // Only look at the type-as-written. If this function has an auto-deduced
698 // return type, we can't compute the linkage of that type because it could
699 // require looking at the linkage of this function, and we don't need this
700 // for correctness because the type is not part of the function's
701 // signature.
702 // FIXME: This is a hack. We should be able to solve this circularity some
703 // other way.
704 QualType TypeAsWritten = Function->getType();
705 if (TypeSourceInfo *TSI = Function->getTypeSourceInfo())
706 TypeAsWritten = TSI->getType();
707 if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
708 return LinkageInfo::uniqueExternal();
709 }
John McCallaf8ca372011-02-10 06:50:24 +0000710
John McCall3892d022013-02-21 23:42:58 +0000711 // Consider LV from the template and the template arguments.
712 // We're at file scope, so we do not need to worry about nested
713 // specializations.
John McCall6ce51ee2011-06-27 23:06:04 +0000714 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000715 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola74caf012013-05-29 04:55:30 +0000716 mergeTemplateLV(LV, Function, specInfo, computation);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000717 }
718
Douglas Gregord85b5b92009-11-25 22:24:25 +0000719 // - a named class (Clause 9), or an unnamed class defined in a
720 // typedef declaration in which the class has the typedef name
721 // for linkage purposes (7.1.3); or
722 // - a named enumeration (7.2), or an unnamed enumeration
723 // defined in a typedef declaration in which the enumeration
724 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000725 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
726 // Unnamed tags have no linkage.
John McCall83972f12013-03-09 00:54:27 +0000727 if (!Tag->hasNameForLinkage())
John McCallaf146032010-10-30 11:50:40 +0000728 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000729
John McCall1fb0caa2010-10-22 21:05:15 +0000730 // If this is a class template specialization, consider the
John McCall3892d022013-02-21 23:42:58 +0000731 // linkage of the template and template arguments. We're at file
732 // scope, so we do not need to worry about nested specializations.
John McCall6ce51ee2011-06-27 23:06:04 +0000733 if (const ClassTemplateSpecializationDecl *spec
John McCall1fb0caa2010-10-22 21:05:15 +0000734 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall5a758de2013-02-16 00:17:33 +0000735 mergeTemplateLV(LV, spec, computation);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000736 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000737
738 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000739 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola1266b612012-04-21 23:28:21 +0000740 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCall5a758de2013-02-16 00:17:33 +0000741 computation);
Rafael Espindola88ce12a2013-05-27 14:14:42 +0000742 if (!isExternalFormalLinkage(EnumLV.getLinkage()))
John McCallaf146032010-10-30 11:50:40 +0000743 return LinkageInfo::none();
744 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000745
746 // - a template, unless it is a function template that has
747 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000748 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
John McCalld4c3d662013-02-20 01:54:26 +0000749 bool considerVisibility = !hasExplicitVisibilityAlready(computation);
John McCall5a758de2013-02-16 00:17:33 +0000750 LinkageInfo tempLV =
Rafael Espindola74caf012013-05-29 04:55:30 +0000751 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCall5a758de2013-02-16 00:17:33 +0000752 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
753
Douglas Gregord85b5b92009-11-25 22:24:25 +0000754 // - a namespace (7.3), unless it is declared within an unnamed
755 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000756 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
757 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000758
John McCall1fb0caa2010-10-22 21:05:15 +0000759 // By extension, we assign external linkage to Objective-C
760 // interfaces.
761 } else if (isa<ObjCInterfaceDecl>(D)) {
762 // fallout
763
764 // Everything not covered here has no linkage.
765 } else {
John McCallaf146032010-10-30 11:50:40 +0000766 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000767 }
768
769 // If we ended up with non-external linkage, visibility should
770 // always be default.
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000771 if (LV.getLinkage() != ExternalLinkage)
772 return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000773
John McCall1fb0caa2010-10-22 21:05:15 +0000774 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000775}
776
John McCall5a758de2013-02-16 00:17:33 +0000777static LinkageInfo getLVForClassMember(const NamedDecl *D,
778 LVComputationKind computation) {
John McCall1fb0caa2010-10-22 21:05:15 +0000779 // Only certain class members have linkage. Note that fields don't
780 // really have linkage, but it's convenient to say they do for the
781 // purposes of calculating linkage of pointer-to-data-member
782 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000783 if (!(isa<CXXMethodDecl>(D) ||
784 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000785 isa<FieldDecl>(D) ||
David Blaikie66cff722012-11-14 01:52:05 +0000786 isa<TagDecl>(D)))
John McCallaf146032010-10-30 11:50:40 +0000787 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000788
John McCall36987482010-11-02 01:45:15 +0000789 LinkageInfo LV;
790
John McCall36987482010-11-02 01:45:15 +0000791 // If we have an explicit visibility attribute, merge that in.
John McCalld4c3d662013-02-20 01:54:26 +0000792 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikiedc84cd52013-02-20 22:23:23 +0000793 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000794 LV.mergeVisibility(*Vis, true);
Rafael Espindolab04b7312012-07-13 14:25:36 +0000795 // If we're paying attention to global visibility, apply
796 // -finline-visibility-hidden if this is an inline method.
797 //
798 // Note that we do this before merging information about
799 // the class visibility.
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000800 if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
Rafael Espindolab04b7312012-07-13 14:25:36 +0000801 LV.mergeVisibility(HiddenVisibility, true);
John McCall36987482010-11-02 01:45:15 +0000802 }
Rafael Espindolac7e60602012-04-19 05:50:08 +0000803
804 // If this class member has an explicit visibility attribute, the only
805 // thing that can change its visibility is the template arguments, so
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +0000806 // only look for them when processing the class.
John McCalld4c3d662013-02-20 01:54:26 +0000807 LVComputationKind classComputation = computation;
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000808 if (LV.isVisibilityExplicit())
John McCalld4c3d662013-02-20 01:54:26 +0000809 classComputation = withExplicitVisibilityAlready(computation);
Rafael Espindola0f905902012-04-16 18:25:01 +0000810
John McCall3892d022013-02-21 23:42:58 +0000811 LinkageInfo classLV =
812 getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
John McCall3cdfc4d2010-08-13 08:35:10 +0000813 // If the class already has unique-external linkage, we can't improve.
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000814 if (classLV.getLinkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000815 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000816
Rafael Espindolae8328542013-05-28 02:22:10 +0000817 if (!isExternallyVisible(classLV.getLinkage()))
818 return LinkageInfo::none();
819
820
John McCall3892d022013-02-21 23:42:58 +0000821 // Otherwise, don't merge in classLV yet, because in certain cases
822 // we need to completely ignore the visibility from it.
823
824 // Specifically, if this decl exists and has an explicit attribute.
825 const NamedDecl *explicitSpecSuppressor = 0;
826
John McCall3cdfc4d2010-08-13 08:35:10 +0000827 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000828 // If the type of the function uses a type with unique-external
829 // linkage, it's not legally usable from outside this translation unit.
830 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
831 return LinkageInfo::uniqueExternal();
832
John McCall1fb0caa2010-10-22 21:05:15 +0000833 // If this is a method template specialization, use the linkage for
834 // the template parameters and arguments.
John McCall6ce51ee2011-06-27 23:06:04 +0000835 if (FunctionTemplateSpecializationInfo *spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000836 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola74caf012013-05-29 04:55:30 +0000837 mergeTemplateLV(LV, MD, spec, computation);
John McCall3892d022013-02-21 23:42:58 +0000838 if (spec->isExplicitSpecialization()) {
839 explicitSpecSuppressor = MD;
840 } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
841 explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
842 }
843 } else if (isExplicitMemberSpecialization(MD)) {
844 explicitSpecSuppressor = MD;
John McCall66cbcf32010-11-01 01:29:57 +0000845 }
John McCall1fb0caa2010-10-22 21:05:15 +0000846
John McCall110e8e52010-10-29 22:22:43 +0000847 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall6ce51ee2011-06-27 23:06:04 +0000848 if (const ClassTemplateSpecializationDecl *spec
John McCall110e8e52010-10-29 22:22:43 +0000849 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCall5a758de2013-02-16 00:17:33 +0000850 mergeTemplateLV(LV, spec, computation);
John McCall3892d022013-02-21 23:42:58 +0000851 if (spec->isExplicitSpecialization()) {
852 explicitSpecSuppressor = spec;
853 } else {
854 const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
855 if (isExplicitMemberSpecialization(temp)) {
856 explicitSpecSuppressor = temp->getTemplatedDecl();
857 }
858 }
859 } else if (isExplicitMemberSpecialization(RD)) {
860 explicitSpecSuppressor = RD;
John McCall110e8e52010-10-29 22:22:43 +0000861 }
862
John McCall110e8e52010-10-29 22:22:43 +0000863 // Static data members.
864 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000865 // Modify the variable's linkage by its type, but ignore the
866 // type's visibility unless it's a definition.
Rafael Espindola74caf012013-05-29 04:55:30 +0000867 LinkageInfo typeLV = getLVForType(*VD->getType(), computation);
Rafael Espindolae9808902013-05-30 21:23:15 +0000868 if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit())
869 LV.mergeVisibility(typeLV);
870 LV.mergeExternalVisibility(typeLV);
John McCall3892d022013-02-21 23:42:58 +0000871
872 if (isExplicitMemberSpecialization(VD)) {
873 explicitSpecSuppressor = VD;
874 }
John McCall5a758de2013-02-16 00:17:33 +0000875
876 // Template members.
877 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
878 bool considerVisibility =
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000879 (!LV.isVisibilityExplicit() &&
880 !classLV.isVisibilityExplicit() &&
John McCalld4c3d662013-02-20 01:54:26 +0000881 !hasExplicitVisibilityAlready(computation));
John McCall5a758de2013-02-16 00:17:33 +0000882 LinkageInfo tempLV =
Rafael Espindola74caf012013-05-29 04:55:30 +0000883 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCall5a758de2013-02-16 00:17:33 +0000884 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall3892d022013-02-21 23:42:58 +0000885
886 if (const RedeclarableTemplateDecl *redeclTemp =
887 dyn_cast<RedeclarableTemplateDecl>(temp)) {
888 if (isExplicitMemberSpecialization(redeclTemp)) {
889 explicitSpecSuppressor = temp->getTemplatedDecl();
890 }
891 }
John McCall110e8e52010-10-29 22:22:43 +0000892 }
893
John McCall3892d022013-02-21 23:42:58 +0000894 // We should never be looking for an attribute directly on a template.
895 assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
896
897 // If this member is an explicit member specialization, and it has
898 // an explicit attribute, ignore visibility from the parent.
899 bool considerClassVisibility = true;
900 if (explicitSpecSuppressor &&
Rafael Espindolaf127eb82013-02-27 02:56:45 +0000901 // optimization: hasDVA() is true only with explicit visibility.
902 LV.isVisibilityExplicit() &&
903 classLV.getVisibility() != DefaultVisibility &&
John McCall3892d022013-02-21 23:42:58 +0000904 hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) {
905 considerClassVisibility = false;
906 }
907
908 // Finally, merge in information from the class.
909 LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
John McCall1fb0caa2010-10-22 21:05:15 +0000910 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000911}
912
David Blaikie99ba9e32011-12-20 02:48:34 +0000913void NamedDecl::anchor() { }
914
Rafael Espindola74caf012013-05-29 04:55:30 +0000915static LinkageInfo computeLVForDecl(const NamedDecl *D,
916 LVComputationKind computation);
917
Rafael Espindola2d1b0962013-03-14 03:07:35 +0000918bool NamedDecl::isLinkageValid() const {
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000919 if (!hasCachedLinkage())
Rafael Espindola2d1b0962013-03-14 03:07:35 +0000920 return true;
John McCallf76b0922011-02-08 19:01:05 +0000921
Rafael Espindola74caf012013-05-29 04:55:30 +0000922 return computeLVForDecl(this, LVForLinkageOnly).getLinkage() ==
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000923 getCachedLinkage();
John McCallf76b0922011-02-08 19:01:05 +0000924}
925
Rafael Espindola181e3ec2013-05-13 00:12:11 +0000926Linkage NamedDecl::getLinkageInternal() const {
John McCalld4c3d662013-02-20 01:54:26 +0000927 // We don't care about visibility here, so ask for the cheapest
928 // possible visibility analysis.
Rafael Espindoladc070562013-05-28 19:43:11 +0000929 return getLVForDecl(this, LVForLinkageOnly).getLinkage();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000930}
931
John McCallaf146032010-10-30 11:50:40 +0000932LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCall5a758de2013-02-16 00:17:33 +0000933 LVComputationKind computation =
934 (usesTypeVisibility(this) ? LVForType : LVForValue);
Rafael Espindoladc070562013-05-28 19:43:11 +0000935 return getLVForDecl(this, computation);
John McCall0df95872010-10-29 00:29:13 +0000936}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000937
David Blaikiedc84cd52013-02-20 22:23:23 +0000938Optional<Visibility>
John McCalld4c3d662013-02-20 01:54:26 +0000939NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
Rafael Espindolad3b2f0a2013-02-26 19:33:14 +0000940 // Check the declaration itself first.
941 if (Optional<Visibility> V = getVisibilityOf(this, kind))
942 return V;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000943
Rafael Espindolad3b2f0a2013-02-26 19:33:14 +0000944 // If this is a member class of a specialization of a class template
945 // and the corresponding decl has explicit visibility, use that.
946 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
947 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
948 if (InstantiatedFrom)
949 return getVisibilityOf(InstantiatedFrom, kind);
950 }
951
952 // If there wasn't explicit visibility there, and this is a
953 // specialization of a class template, check for visibility
954 // on the pattern.
955 if (const ClassTemplateSpecializationDecl *spec
956 = dyn_cast<ClassTemplateSpecializationDecl>(this))
957 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(),
958 kind);
959
960 // Use the most recent declaration.
961 const NamedDecl *MostRecent = cast<NamedDecl>(this->getMostRecentDecl());
962 if (MostRecent != this)
963 return MostRecent->getExplicitVisibility(kind);
964
965 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola797105a2012-05-16 02:10:38 +0000966 if (Var->isStaticDataMember()) {
967 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
968 if (InstantiatedFrom)
John McCalld4c3d662013-02-20 01:54:26 +0000969 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola797105a2012-05-16 02:10:38 +0000970 }
971
David Blaikie66874fb2013-02-21 01:47:18 +0000972 return None;
Rafael Espindola797105a2012-05-16 02:10:38 +0000973 }
Rafael Espindolad3b2f0a2013-02-26 19:33:14 +0000974 // Also handle function template specializations.
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000975 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000976 // If the function is a specialization of a template with an
977 // explicit visibility attribute, use that.
978 if (FunctionTemplateSpecializationInfo *templateInfo
979 = fn->getTemplateSpecializationInfo())
John McCalld4c3d662013-02-20 01:54:26 +0000980 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
981 kind);
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000982
Rafael Espindola860097c2012-02-23 04:17:32 +0000983 // If the function is a member of a specialization of a class template
984 // and the corresponding decl has explicit visibility, use that.
985 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
986 if (InstantiatedFrom)
John McCalld4c3d662013-02-20 01:54:26 +0000987 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola860097c2012-02-23 04:17:32 +0000988
David Blaikie66874fb2013-02-21 01:47:18 +0000989 return None;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000990 }
991
Rafael Espindola98499012012-07-31 19:02:02 +0000992 // The visibility of a template is stored in the templated decl.
993 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
John McCalld4c3d662013-02-20 01:54:26 +0000994 return getVisibilityOf(TD->getTemplatedDecl(), kind);
Rafael Espindola98499012012-07-31 19:02:02 +0000995
David Blaikie66874fb2013-02-21 01:47:18 +0000996 return None;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000997}
998
John McCall5a758de2013-02-16 00:17:33 +0000999static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
1000 LVComputationKind computation) {
1001 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1002 if (Function->isInAnonymousNamespace() &&
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +00001003 !Function->isInExternCContext())
John McCall5a758de2013-02-16 00:17:33 +00001004 return LinkageInfo::uniqueExternal();
1005
1006 // This is a "void f();" which got merged with a file static.
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001007 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCall5a758de2013-02-16 00:17:33 +00001008 return LinkageInfo::internal();
1009
1010 LinkageInfo LV;
John McCalld4c3d662013-02-20 01:54:26 +00001011 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikiedc84cd52013-02-20 22:23:23 +00001012 if (Optional<Visibility> Vis =
1013 getExplicitVisibility(Function, computation))
John McCall5a758de2013-02-16 00:17:33 +00001014 LV.mergeVisibility(*Vis, true);
1015 }
1016
1017 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1018 // merging storage classes and visibility attributes, so we don't have to
1019 // look at previous decls in here.
1020
1021 return LV;
1022 }
1023
1024 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001025 if (Var->hasExternalStorage()) {
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +00001026 if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
John McCall5a758de2013-02-16 00:17:33 +00001027 return LinkageInfo::uniqueExternal();
1028
John McCall5a758de2013-02-16 00:17:33 +00001029 LinkageInfo LV;
1030 if (Var->getStorageClass() == SC_PrivateExtern)
1031 LV.mergeVisibility(HiddenVisibility, true);
John McCalld4c3d662013-02-20 01:54:26 +00001032 else if (!hasExplicitVisibilityAlready(computation)) {
David Blaikiedc84cd52013-02-20 22:23:23 +00001033 if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
John McCall5a758de2013-02-16 00:17:33 +00001034 LV.mergeVisibility(*Vis, true);
1035 }
1036
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001037 if (const VarDecl *Prev = Var->getPreviousDecl()) {
1038 LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1039 if (PrevLV.getLinkage())
1040 LV.setLinkage(PrevLV.getLinkage());
1041 LV.mergeVisibility(PrevLV);
1042 }
1043
John McCall5a758de2013-02-16 00:17:33 +00001044 return LV;
1045 }
Rafael Espindola9610d772013-06-17 20:04:51 +00001046
1047 if (!Var->isStaticLocal())
1048 return LinkageInfo::none();
John McCall5a758de2013-02-16 00:17:33 +00001049 }
1050
Rafael Espindola9610d772013-06-17 20:04:51 +00001051 ASTContext &Context = D->getASTContext();
1052 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +00001053 return LinkageInfo::none();
1054
1055 const FunctionDecl *FD = getOutermostFunctionContext(D);
Rafael Espindolaec0d96f2013-06-04 13:43:35 +00001056 if (!FD)
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +00001057 return LinkageInfo::none();
Rafael Espindolaec0d96f2013-06-04 13:43:35 +00001058
1059 if (!FD->isInlined() && FD->getTemplateSpecializationKind() == TSK_Undeclared)
1060 return LinkageInfo::none();
1061
Rafael Espindola24639c42013-05-28 02:13:28 +00001062 LinkageInfo LV = getLVForDecl(FD, computation);
Rafael Espindolabdf2bba2013-05-27 14:50:21 +00001063 if (!isExternallyVisible(LV.getLinkage()))
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +00001064 return LinkageInfo::none();
1065 return LinkageInfo(VisibleNoLinkage, LV.getVisibility(),
1066 LV.isVisibilityExplicit());
John McCall5a758de2013-02-16 00:17:33 +00001067}
1068
Rafael Espindoladc070562013-05-28 19:43:11 +00001069static LinkageInfo computeLVForDecl(const NamedDecl *D,
1070 LVComputationKind computation) {
Ted Kremenekbecc3082010-04-20 23:15:35 +00001071 // Objective-C: treat all Objective-C declarations as having external
1072 // linkage.
John McCall0df95872010-10-29 00:29:13 +00001073 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +00001074 default:
1075 break;
Argyrios Kyrtzidisf8d34ed2011-12-01 01:28:21 +00001076 case Decl::ParmVar:
1077 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +00001078 case Decl::TemplateTemplateParm: // count these as external
1079 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +00001080 case Decl::ObjCAtDefsField:
1081 case Decl::ObjCCategory:
1082 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +00001083 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +00001084 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +00001085 case Decl::ObjCMethod:
1086 case Decl::ObjCProperty:
1087 case Decl::ObjCPropertyImpl:
1088 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +00001089 return LinkageInfo::external();
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001090
1091 case Decl::CXXRecord: {
1092 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
1093 if (Record->isLambda()) {
1094 if (!Record->getLambdaManglingNumber()) {
1095 // This lambda has no mangling number, so it's internal.
1096 return LinkageInfo::internal();
1097 }
1098
1099 // This lambda has its linkage/visibility determined by its owner.
1100 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
1101 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
1102 if (isa<ParmVarDecl>(ContextDecl))
1103 DC = ContextDecl->getDeclContext()->getRedeclContext();
1104 else
John McCall5a758de2013-02-16 00:17:33 +00001105 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001106 }
1107
1108 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
John McCall5a758de2013-02-16 00:17:33 +00001109 return getLVForDecl(ND, computation);
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001110
1111 return LinkageInfo::external();
1112 }
1113
1114 break;
1115 }
Ted Kremenekbecc3082010-04-20 23:15:35 +00001116 }
1117
Douglas Gregord85b5b92009-11-25 22:24:25 +00001118 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +00001119 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall5a758de2013-02-16 00:17:33 +00001120 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregord85b5b92009-11-25 22:24:25 +00001121
1122 // C++ [basic.link]p5:
1123 // In addition, a member function, static data member, a named
1124 // class or enumeration of class scope, or an unnamed class or
1125 // enumeration defined in a class-scope typedef declaration such
1126 // that the class or enumeration has the typedef name for linkage
1127 // purposes (7.1.3), has external linkage if the name of the class
1128 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +00001129 if (D->getDeclContext()->isRecord())
John McCall5a758de2013-02-16 00:17:33 +00001130 return getLVForClassMember(D, computation);
Douglas Gregord85b5b92009-11-25 22:24:25 +00001131
1132 // C++ [basic.link]p6:
1133 // The name of a function declared in block scope and the name of
1134 // an object declared by a block scope extern declaration have
1135 // linkage. If there is a visible declaration of an entity with
1136 // linkage having the same name and type, ignoring entities
1137 // declared outside the innermost enclosing namespace scope, the
1138 // block scope declaration declares that same entity and receives
1139 // the linkage of the previous declaration. If there is more than
1140 // one such matching entity, the program is ill-formed. Otherwise,
1141 // if no matching entity is found, the block scope entity receives
1142 // external linkage.
John McCall5a758de2013-02-16 00:17:33 +00001143 if (D->getDeclContext()->isFunctionOrMethod())
1144 return getLVForLocalDecl(D, computation);
Douglas Gregord85b5b92009-11-25 22:24:25 +00001145
1146 // C++ [basic.link]p6:
1147 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +00001148 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +00001149}
Douglas Gregord85b5b92009-11-25 22:24:25 +00001150
Rafael Espindoladc070562013-05-28 19:43:11 +00001151namespace clang {
1152class LinkageComputer {
1153public:
1154 static LinkageInfo getLVForDecl(const NamedDecl *D,
1155 LVComputationKind computation) {
1156 if (computation == LVForLinkageOnly && D->hasCachedLinkage())
1157 return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false);
1158
1159 LinkageInfo LV = computeLVForDecl(D, computation);
1160 if (D->hasCachedLinkage())
1161 assert(D->getCachedLinkage() == LV.getLinkage());
1162
1163 D->setCachedLinkage(LV.getLinkage());
1164
1165#ifndef NDEBUG
1166 // In C (because of gnu inline) and in c++ with microsoft extensions an
1167 // static can follow an extern, so we can have two decls with different
1168 // linkages.
1169 const LangOptions &Opts = D->getASTContext().getLangOpts();
1170 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
1171 return LV;
1172
1173 // We have just computed the linkage for this decl. By induction we know
1174 // that all other computed linkages match, check that the one we just
1175 // computed
1176 // also does.
1177 NamedDecl *Old = NULL;
1178 for (NamedDecl::redecl_iterator I = D->redecls_begin(),
1179 E = D->redecls_end();
1180 I != E; ++I) {
1181 NamedDecl *T = cast<NamedDecl>(*I);
1182 if (T == D)
1183 continue;
1184 if (T->hasCachedLinkage()) {
1185 Old = T;
1186 break;
1187 }
1188 }
1189 assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage());
1190#endif
1191
1192 return LV;
1193 }
1194};
1195}
1196
1197static LinkageInfo getLVForDecl(const NamedDecl *D,
1198 LVComputationKind computation) {
1199 return clang::LinkageComputer::getLVForDecl(D, computation);
1200}
1201
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001202std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregorba103062012-03-27 23:34:16 +00001203 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson3a082d82009-09-08 18:24:21 +00001204}
1205
1206std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Benjamin Kramerb063ef02013-02-23 13:53:57 +00001207 std::string QualName;
1208 llvm::raw_string_ostream OS(QualName);
1209 printQualifiedName(OS, P);
1210 return OS.str();
1211}
1212
1213void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1214 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1215}
1216
1217void NamedDecl::printQualifiedName(raw_ostream &OS,
1218 const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001219 const DeclContext *Ctx = getDeclContext();
1220
Benjamin Kramerb063ef02013-02-23 13:53:57 +00001221 if (Ctx->isFunctionOrMethod()) {
1222 printName(OS);
1223 return;
1224 }
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001225
Chris Lattner5f9e2722011-07-23 10:55:15 +00001226 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001227 ContextsTy Contexts;
1228
1229 // Collect contexts.
1230 while (Ctx && isa<NamedDecl>(Ctx)) {
1231 Contexts.push_back(Ctx);
1232 Ctx = Ctx->getParent();
Benjamin Kramerb063ef02013-02-23 13:53:57 +00001233 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001234
1235 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1236 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00001237 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001238 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Benjamin Kramer5eada842013-02-22 15:46:01 +00001239 OS << Spec->getName();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001240 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Benjamin Kramer5eada842013-02-22 15:46:01 +00001241 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1242 TemplateArgs.data(),
1243 TemplateArgs.size(),
1244 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001245 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +00001246 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001247 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +00001248 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001249 OS << *ND;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001250 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1251 if (!RD->getIdentifier())
1252 OS << "<anonymous " << RD->getKindName() << '>';
1253 else
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001254 OS << *RD;
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001255 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +00001256 const FunctionProtoType *FT = 0;
1257 if (FD->hasWrittenPrototype())
Eli Friedman482466b2012-08-30 22:22:09 +00001258 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinig3521d012009-12-28 03:19:38 +00001259
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001260 OS << *FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +00001261 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +00001262 unsigned NumParams = FD->getNumParams();
1263 for (unsigned i = 0; i < NumParams; ++i) {
1264 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001265 OS << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +00001266 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinig3521d012009-12-28 03:19:38 +00001267 }
1268
1269 if (FT->isVariadic()) {
1270 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001271 OS << ", ";
1272 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +00001273 }
1274 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001275 OS << ')';
1276 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001277 OS << *cast<NamedDecl>(*I);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001278 }
1279 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001280 }
1281
John McCall8472af42010-03-16 21:48:18 +00001282 if (getDeclName())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001283 OS << *this;
John McCall8472af42010-03-16 21:48:18 +00001284 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +00001285 OS << "<anonymous>";
Benjamin Kramerb063ef02013-02-23 13:53:57 +00001286}
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001287
Benjamin Kramerb063ef02013-02-23 13:53:57 +00001288void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1289 const PrintingPolicy &Policy,
1290 bool Qualified) const {
1291 if (Qualified)
1292 printQualifiedName(OS, Policy);
1293 else
1294 printName(OS);
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001295}
1296
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001297bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +00001298 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1299
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001300 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1301 // We want to keep it, unless it nominates same namespace.
1302 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +00001303 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1304 ->getOriginalNamespace() ==
1305 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1306 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001307 }
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Douglas Gregor6ed40e32008-12-23 21:05:05 +00001309 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1310 // For function declarations, we keep track of redeclarations.
Douglas Gregoref96ee02012-01-14 16:38:05 +00001311 return FD->getPreviousDecl() == OldD;
Douglas Gregor6ed40e32008-12-23 21:05:05 +00001312
Douglas Gregore53060f2009-06-25 22:08:12 +00001313 // For function templates, the underlying function declarations are linked.
1314 if (const FunctionTemplateDecl *FunctionTemplate
1315 = dyn_cast<FunctionTemplateDecl>(this))
1316 if (const FunctionTemplateDecl *OldFunctionTemplate
1317 = dyn_cast<FunctionTemplateDecl>(OldD))
1318 return FunctionTemplate->getTemplatedDecl()
1319 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Steve Naroff0de21fd2009-02-22 19:35:57 +00001321 // For method declarations, we keep track of redeclarations.
1322 if (isa<ObjCMethodDecl>(this))
1323 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001324
John McCallf36e02d2009-10-09 21:13:30 +00001325 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1326 return true;
1327
John McCall9488ea12009-11-17 05:59:44 +00001328 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1329 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1330 cast<UsingShadowDecl>(OldD)->getTargetDecl();
1331
Douglas Gregordc355712011-02-25 00:36:19 +00001332 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1333 ASTContext &Context = getASTContext();
1334 return Context.getCanonicalNestedNameSpecifier(
1335 cast<UsingDecl>(this)->getQualifier()) ==
1336 Context.getCanonicalNestedNameSpecifier(
1337 cast<UsingDecl>(OldD)->getQualifier());
1338 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +00001339
Douglas Gregor7a537402012-01-03 23:26:26 +00001340 // A typedef of an Objective-C class type can replace an Objective-C class
1341 // declaration or definition, and vice versa.
1342 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1343 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1344 return true;
1345
Douglas Gregor6ed40e32008-12-23 21:05:05 +00001346 // For non-function declarations, if the declarations are of the
1347 // same kind then this must be a redeclaration, or semantic analysis
1348 // would not have given us the new declaration.
1349 return this->getKind() == OldD->getKind();
1350}
1351
Douglas Gregord6f7e9d2009-02-24 20:03:32 +00001352bool NamedDecl::hasLinkage() const {
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +00001353 return getFormalLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +00001354}
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001355
Daniel Dunbar6daffa52012-03-08 18:20:41 +00001356NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlssone136e0e2009-06-26 06:29:23 +00001357 NamedDecl *ND = this;
Benjamin Kramer56757e92012-03-08 21:00:45 +00001358 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1359 ND = UD->getTargetDecl();
1360
1361 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1362 return AD->getClassInterface();
1363
1364 return ND;
Anders Carlssone136e0e2009-06-26 06:29:23 +00001365}
1366
John McCall161755a2010-04-06 21:38:20 +00001367bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor5bc37f62012-03-08 02:08:05 +00001368 if (!isCXXClassMember())
1369 return false;
1370
John McCall161755a2010-04-06 21:38:20 +00001371 const NamedDecl *D = this;
1372 if (isa<UsingShadowDecl>(D))
1373 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1374
John McCall76da55d2013-04-16 07:28:30 +00001375 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCall161755a2010-04-06 21:38:20 +00001376 return true;
1377 if (isa<CXXMethodDecl>(D))
1378 return cast<CXXMethodDecl>(D)->isInstance();
1379 if (isa<FunctionTemplateDecl>(D))
1380 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1381 ->getTemplatedDecl())->isInstance();
1382 return false;
1383}
1384
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +00001385//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001386// DeclaratorDecl Implementation
1387//===----------------------------------------------------------------------===//
1388
Douglas Gregor1693e152010-07-06 18:42:40 +00001389template <typename DeclT>
1390static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1391 if (decl->getNumTemplateParameterLists() > 0)
1392 return decl->getTemplateParameterList(0)->getTemplateLoc();
1393 else
1394 return decl->getInnerLocStart();
1395}
1396
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001397SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +00001398 TypeSourceInfo *TSI = getTypeSourceInfo();
1399 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001400 return SourceLocation();
1401}
1402
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001403void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1404 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00001405 // Make sure the extended decl info is allocated.
1406 if (!hasExtInfo()) {
1407 // Save (non-extended) type source info pointer.
1408 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1409 // Allocate external info struct.
1410 DeclInfo = new (getASTContext()) ExtInfo;
1411 // Restore savedTInfo into (extended) decl info.
1412 getExtInfo()->TInfo = savedTInfo;
1413 }
1414 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001415 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00001416 } else {
John McCallb6217662010-03-15 10:12:16 +00001417 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00001418 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001419 if (getExtInfo()->NumTemplParamLists == 0) {
1420 // Save type source info pointer.
1421 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1422 // Deallocate the extended decl info.
1423 getASTContext().Deallocate(getExtInfo());
1424 // Restore savedTInfo into (non-extended) decl info.
1425 DeclInfo = savedTInfo;
1426 }
1427 else
1428 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00001429 }
1430 }
1431}
1432
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001433void
1434DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1435 unsigned NumTPLists,
1436 TemplateParameterList **TPLists) {
1437 assert(NumTPLists > 0);
1438 // Make sure the extended decl info is allocated.
1439 if (!hasExtInfo()) {
1440 // Save (non-extended) type source info pointer.
1441 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1442 // Allocate external info struct.
1443 DeclInfo = new (getASTContext()) ExtInfo;
1444 // Restore savedTInfo into (extended) decl info.
1445 getExtInfo()->TInfo = savedTInfo;
1446 }
1447 // Set the template parameter lists info.
1448 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1449}
1450
Douglas Gregor1693e152010-07-06 18:42:40 +00001451SourceLocation DeclaratorDecl::getOuterLocStart() const {
1452 return getTemplateOrInnerLocStart(this);
1453}
1454
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001455namespace {
1456
1457// Helper function: returns true if QT is or contains a type
1458// having a postfix component.
1459bool typeIsPostfix(clang::QualType QT) {
1460 while (true) {
1461 const Type* T = QT.getTypePtr();
1462 switch (T->getTypeClass()) {
1463 default:
1464 return false;
1465 case Type::Pointer:
1466 QT = cast<PointerType>(T)->getPointeeType();
1467 break;
1468 case Type::BlockPointer:
1469 QT = cast<BlockPointerType>(T)->getPointeeType();
1470 break;
1471 case Type::MemberPointer:
1472 QT = cast<MemberPointerType>(T)->getPointeeType();
1473 break;
1474 case Type::LValueReference:
1475 case Type::RValueReference:
1476 QT = cast<ReferenceType>(T)->getPointeeType();
1477 break;
1478 case Type::PackExpansion:
1479 QT = cast<PackExpansionType>(T)->getPattern();
1480 break;
1481 case Type::Paren:
1482 case Type::ConstantArray:
1483 case Type::DependentSizedArray:
1484 case Type::IncompleteArray:
1485 case Type::VariableArray:
1486 case Type::FunctionProto:
1487 case Type::FunctionNoProto:
1488 return true;
1489 }
1490 }
1491}
1492
1493} // namespace
1494
1495SourceRange DeclaratorDecl::getSourceRange() const {
1496 SourceLocation RangeEnd = getLocation();
1497 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1498 if (typeIsPostfix(TInfo->getType()))
1499 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1500 }
1501 return SourceRange(getOuterLocStart(), RangeEnd);
1502}
1503
Abramo Bagnara9b934882010-06-12 08:15:14 +00001504void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001505QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1506 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001507 TemplateParameterList **TPLists) {
1508 assert((NumTPLists == 0 || TPLists != 0) &&
1509 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001510
1511 // Free previous template parameters (if any).
1512 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001513 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001514 TemplParamLists = 0;
1515 NumTemplParamLists = 0;
1516 }
1517 // Set info on matched template parameter lists (if any).
1518 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001519 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001520 NumTemplParamLists = NumTPLists;
1521 for (unsigned i = NumTPLists; i-- > 0; )
1522 TemplParamLists[i] = TPLists[i];
1523 }
1524}
1525
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001526//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001527// VarDecl Implementation
1528//===----------------------------------------------------------------------===//
1529
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001530const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1531 switch (SC) {
Peter Collingbourne8c25fc52011-09-19 21:14:35 +00001532 case SC_None: break;
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001533 case SC_Auto: return "auto";
1534 case SC_Extern: return "extern";
1535 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1536 case SC_PrivateExtern: return "__private_extern__";
1537 case SC_Register: return "register";
1538 case SC_Static: return "static";
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001539 }
1540
Peter Collingbourne8be0c742011-09-20 12:40:26 +00001541 llvm_unreachable("Invalid storage class");
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001542}
1543
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001544VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1545 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001546 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001547 StorageClass S) {
1548 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001549}
1550
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001551VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1552 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1553 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001554 QualType(), 0, SC_None);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001555}
1556
Douglas Gregor381d34e2010-12-06 18:36:25 +00001557void VarDecl::setStorageClass(StorageClass SC) {
1558 assert(isLegalForVariable(SC));
John McCallf1e4fbf2011-05-01 02:13:58 +00001559 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001560}
1561
Douglas Gregor1693e152010-07-06 18:42:40 +00001562SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisd69f31c2012-10-08 23:08:41 +00001563 if (const Expr *Init = getInit()) {
1564 SourceLocation InitEnd = Init->getLocEnd();
Nico Weber3a344f92013-01-22 17:00:09 +00001565 // If Init is implicit, ignore its source range and fallback on
1566 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1567 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidisd69f31c2012-10-08 23:08:41 +00001568 return SourceRange(getOuterLocStart(), InitEnd);
1569 }
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001570 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001571}
1572
Rafael Espindola7ac928b2013-01-04 21:18:45 +00001573template<typename T>
Rafael Espindola950fee22013-02-14 01:18:37 +00001574static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindolad2fdd422013-02-14 01:47:04 +00001575 // C++ [dcl.link]p1: All function types, function names with external linkage,
1576 // and variable names with external linkage have a language linkage.
Rafael Espindola181e3ec2013-05-13 00:12:11 +00001577 if (!D.hasExternalFormalLinkage())
Rafael Espindolad2fdd422013-02-14 01:47:04 +00001578 return NoLanguageLinkage;
1579
1580 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola2b721f52013-01-04 20:41:40 +00001581 // C language linkage fits the implementation nicely.
Rafael Espindola78eeba82012-12-28 14:21:58 +00001582 ASTContext &Context = D.getASTContext();
1583 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindola950fee22013-02-14 01:18:37 +00001584 return CLanguageLinkage;
1585
Rafael Espindolad2fdd422013-02-14 01:47:04 +00001586 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1587 // language linkage of the names of class members and the function type of
1588 // class member functions.
Rafael Espindola78eeba82012-12-28 14:21:58 +00001589 const DeclContext *DC = D.getDeclContext();
1590 if (DC->isRecord())
Rafael Espindola950fee22013-02-14 01:18:37 +00001591 return CXXLanguageLinkage;
Rafael Espindola78eeba82012-12-28 14:21:58 +00001592
1593 // If the first decl is in an extern "C" context, any other redeclaration
1594 // will have C language linkage. If the first one is not in an extern "C"
1595 // context, we would have reported an error for any other decl being in one.
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +00001596 if (isFirstInExternCContext(&D))
Rafael Espindola950fee22013-02-14 01:18:37 +00001597 return CLanguageLinkage;
1598 return CXXLanguageLinkage;
Rafael Espindola78eeba82012-12-28 14:21:58 +00001599}
1600
Rafael Espindola2d1b0962013-03-14 03:07:35 +00001601template<typename T>
1602static bool isExternCTemplate(const T &D) {
1603 // Since the context is ignored for class members, they can only have C++
1604 // language linkage or no language linkage.
1605 const DeclContext *DC = D.getDeclContext();
1606 if (DC->isRecord()) {
1607 assert(D.getASTContext().getLangOpts().CPlusPlus);
1608 return false;
1609 }
1610
1611 return D.getLanguageLinkage() == CLanguageLinkage;
1612}
1613
Rafael Espindola950fee22013-02-14 01:18:37 +00001614LanguageLinkage VarDecl::getLanguageLinkage() const {
1615 return getLanguageLinkageTemplate(*this);
Rafael Espindola78eeba82012-12-28 14:21:58 +00001616}
1617
Rafael Espindola2d1b0962013-03-14 03:07:35 +00001618bool VarDecl::isExternC() const {
1619 return isExternCTemplate(*this);
1620}
1621
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +00001622static bool isLinkageSpecContext(const DeclContext *DC,
1623 LinkageSpecDecl::LanguageIDs ID) {
1624 while (DC->getDeclKind() != Decl::TranslationUnit) {
1625 if (DC->getDeclKind() == Decl::LinkageSpec)
1626 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1627 DC = DC->getParent();
1628 }
1629 return false;
1630}
1631
1632template <typename T>
1633static bool isInLanguageSpecContext(T *D, LinkageSpecDecl::LanguageIDs ID) {
1634 return isLinkageSpecContext(D->getLexicalDeclContext(), ID);
1635}
1636
1637bool VarDecl::isInExternCContext() const {
1638 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
1639}
1640
1641bool VarDecl::isInExternCXXContext() const {
1642 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
1643}
1644
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001645VarDecl *VarDecl::getCanonicalDecl() {
1646 return getFirstDeclaration();
1647}
1648
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001649VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1650 ASTContext &C) const
1651{
Sebastian Redle9d12b62010-01-31 22:27:38 +00001652 // C++ [basic.def]p2:
1653 // A declaration is a definition unless [...] it contains the 'extern'
1654 // specifier or a linkage-specification and neither an initializer [...],
1655 // it declares a static data member in a class declaration [...].
1656 // C++ [temp.expl.spec]p15:
1657 // An explicit specialization of a static data member of a template is a
1658 // definition if the declaration includes an initializer; otherwise, it is
1659 // a declaration.
1660 if (isStaticDataMember()) {
1661 if (isOutOfLine() && (hasInit() ||
1662 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1663 return Definition;
1664 else
1665 return DeclarationOnly;
1666 }
1667 // C99 6.7p5:
1668 // A definition of an identifier is a declaration for that identifier that
1669 // [...] causes storage to be reserved for that object.
1670 // Note: that applies for all non-file-scope objects.
1671 // C99 6.9.2p1:
1672 // If the declaration of an identifier for an object has file scope and an
1673 // initializer, the declaration is an external definition for the identifier
1674 if (hasInit())
1675 return Definition;
Rafael Espindola65dfa2b2013-04-25 12:11:36 +00001676
Sebastian Redle9d12b62010-01-31 22:27:38 +00001677 if (hasExternalStorage())
1678 return DeclarationOnly;
Rafael Espindola37783002013-03-07 01:42:44 +00001679
Rafael Espindola65dfa2b2013-04-25 12:11:36 +00001680 // [dcl.link] p7:
1681 // A declaration directly contained in a linkage-specification is treated
1682 // as if it contains the extern specifier for the purpose of determining
1683 // the linkage of the declared name and whether it is a definition.
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001684 if (isSingleLineExternC(*this))
1685 return DeclarationOnly;
Rafael Espindola65dfa2b2013-04-25 12:11:36 +00001686
Sebastian Redle9d12b62010-01-31 22:27:38 +00001687 // C99 6.9.2p2:
1688 // A declaration of an object that has file scope without an initializer,
1689 // and without a storage class specifier or the scs 'static', constitutes
1690 // a tentative definition.
1691 // No such thing in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00001692 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redle9d12b62010-01-31 22:27:38 +00001693 return TentativeDefinition;
1694
1695 // What's left is (in C, block-scope) declarations without initializers or
1696 // external storage. These are definitions.
1697 return Definition;
1698}
1699
Sebastian Redle9d12b62010-01-31 22:27:38 +00001700VarDecl *VarDecl::getActingDefinition() {
1701 DefinitionKind Kind = isThisDeclarationADefinition();
1702 if (Kind != TentativeDefinition)
1703 return 0;
1704
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001705 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001706 VarDecl *First = getFirstDeclaration();
1707 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1708 I != E; ++I) {
1709 Kind = (*I)->isThisDeclarationADefinition();
1710 if (Kind == Definition)
1711 return 0;
1712 else if (Kind == TentativeDefinition)
1713 LastTentative = *I;
1714 }
1715 return LastTentative;
1716}
1717
1718bool VarDecl::isTentativeDefinitionNow() const {
1719 DefinitionKind Kind = isThisDeclarationADefinition();
1720 if (Kind != TentativeDefinition)
1721 return false;
1722
1723 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1724 if ((*I)->isThisDeclarationADefinition() == Definition)
1725 return false;
1726 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001727 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001728}
1729
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001730VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001731 VarDecl *First = getFirstDeclaration();
1732 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1733 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001734 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl31310a22010-02-01 20:16:42 +00001735 return *I;
1736 }
1737 return 0;
1738}
1739
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001740VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall110e8e52010-10-29 22:22:43 +00001741 DefinitionKind Kind = DeclarationOnly;
1742
1743 const VarDecl *First = getFirstDeclaration();
1744 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar047da192012-03-06 23:52:46 +00001745 I != E; ++I) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001746 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar047da192012-03-06 23:52:46 +00001747 if (Kind == Definition)
1748 break;
1749 }
John McCall110e8e52010-10-29 22:22:43 +00001750
1751 return Kind;
1752}
1753
Sebastian Redl31310a22010-02-01 20:16:42 +00001754const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001755 redecl_iterator I = redecls_begin(), E = redecls_end();
1756 while (I != E && !I->getInit())
1757 ++I;
1758
1759 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001760 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001761 return I->getInit();
1762 }
1763 return 0;
1764}
1765
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001766bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001767 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001768 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001769
1770 if (!isStaticDataMember())
1771 return false;
1772
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001773 // If this static data member was instantiated from a static data member of
1774 // a class template, check whether that static data member was defined
1775 // out-of-line.
1776 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1777 return VD->isOutOfLine();
1778
1779 return false;
1780}
1781
Douglas Gregor0d035142009-10-27 18:42:08 +00001782VarDecl *VarDecl::getOutOfLineDefinition() {
1783 if (!isStaticDataMember())
1784 return 0;
1785
1786 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1787 RD != RDEnd; ++RD) {
1788 if (RD->getLexicalDeclContext()->isFileContext())
1789 return *RD;
1790 }
1791
1792 return 0;
1793}
1794
Douglas Gregor838db382010-02-11 01:19:42 +00001795void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001796 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1797 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001798 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001799 }
1800
1801 Init = I;
1802}
1803
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00001804bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001805 const LangOptions &Lang = C.getLangOpts();
Richard Smith1d238ea2011-12-21 02:55:12 +00001806
Richard Smith16581332012-03-02 04:14:40 +00001807 if (!Lang.CPlusPlus)
1808 return false;
1809
1810 // In C++11, any variable of reference type can be used in a constant
1811 // expression if it is initialized by a constant expression.
Richard Smith80ad52f2013-01-02 11:42:31 +00001812 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith16581332012-03-02 04:14:40 +00001813 return true;
1814
1815 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith1d238ea2011-12-21 02:55:12 +00001816 // not require the variable to be non-volatile, but we consider this to be a
1817 // defect.
Richard Smith16581332012-03-02 04:14:40 +00001818 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith1d238ea2011-12-21 02:55:12 +00001819 return false;
1820
1821 // In C++, const, non-volatile variables of integral or enumeration types
1822 // can be used in constant expressions.
1823 if (getType()->isIntegralOrEnumerationType())
1824 return true;
1825
Richard Smith16581332012-03-02 04:14:40 +00001826 // Additionally, in C++11, non-volatile constexpr variables can be used in
1827 // constant expressions.
Richard Smith80ad52f2013-01-02 11:42:31 +00001828 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith1d238ea2011-12-21 02:55:12 +00001829}
1830
Richard Smith099e7f62011-12-19 06:19:21 +00001831/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1832/// form, which contains extra information on the evaluated value of the
1833/// initializer.
1834EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1835 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1836 if (!Eval) {
1837 Stmt *S = Init.get<Stmt *>();
Manuel Klimekf0f353b2013-06-03 13:51:33 +00001838 // Note: EvaluatedStmt contains an APValue, which usually holds
1839 // resources not allocated from the ASTContext. We need to do some
1840 // work to avoid leaking those, but we do so in VarDecl::evaluateValue
1841 // where we can detect whether there's anything to clean up or not.
Richard Smith099e7f62011-12-19 06:19:21 +00001842 Eval = new (getASTContext()) EvaluatedStmt;
1843 Eval->Value = S;
1844 Init = Eval;
1845 }
1846 return Eval;
1847}
1848
Richard Smith2d6a5672012-01-14 04:30:29 +00001849APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001850 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smith2d6a5672012-01-14 04:30:29 +00001851 return evaluateValue(Notes);
1852}
1853
Manuel Klimekf0f353b2013-06-03 13:51:33 +00001854namespace {
1855// Destroy an APValue that was allocated in an ASTContext.
1856void DestroyAPValue(void* UntypedValue) {
1857 static_cast<APValue*>(UntypedValue)->~APValue();
1858}
1859} // namespace
1860
Richard Smith2d6a5672012-01-14 04:30:29 +00001861APValue *VarDecl::evaluateValue(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001862 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smith099e7f62011-12-19 06:19:21 +00001863 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1864
1865 // We only produce notes indicating why an initializer is non-constant the
1866 // first time it is evaluated. FIXME: The notes won't always be emitted the
1867 // first time we try evaluation, so might not be produced at all.
1868 if (Eval->WasEvaluated)
Richard Smith2d6a5672012-01-14 04:30:29 +00001869 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smith099e7f62011-12-19 06:19:21 +00001870
1871 const Expr *Init = cast<Expr>(Eval->Value);
1872 assert(!Init->isValueDependent());
1873
1874 if (Eval->IsEvaluating) {
1875 // FIXME: Produce a diagnostic for self-initialization.
1876 Eval->CheckedICE = true;
1877 Eval->IsICE = false;
Richard Smith2d6a5672012-01-14 04:30:29 +00001878 return 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001879 }
1880
1881 Eval->IsEvaluating = true;
1882
1883 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1884 this, Notes);
1885
Manuel Klimekf0f353b2013-06-03 13:51:33 +00001886 // Ensure the computed APValue is cleaned up later if evaluation succeeded,
1887 // or that it's empty (so that there's nothing to clean up) if evaluation
1888 // failed.
Richard Smith099e7f62011-12-19 06:19:21 +00001889 if (!Result)
1890 Eval->Evaluated = APValue();
Manuel Klimekf0f353b2013-06-03 13:51:33 +00001891 else if (Eval->Evaluated.needsCleanup())
1892 getASTContext().AddDeallocation(DestroyAPValue, &Eval->Evaluated);
Richard Smith099e7f62011-12-19 06:19:21 +00001893
1894 Eval->IsEvaluating = false;
1895 Eval->WasEvaluated = true;
1896
1897 // In C++11, we have determined whether the initializer was a constant
1898 // expression as a side-effect.
Richard Smith80ad52f2013-01-02 11:42:31 +00001899 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smith099e7f62011-12-19 06:19:21 +00001900 Eval->CheckedICE = true;
Eli Friedman210386e2012-02-06 21:50:18 +00001901 Eval->IsICE = Result && Notes.empty();
Richard Smith099e7f62011-12-19 06:19:21 +00001902 }
1903
Richard Smith2d6a5672012-01-14 04:30:29 +00001904 return Result ? &Eval->Evaluated : 0;
Richard Smith099e7f62011-12-19 06:19:21 +00001905}
1906
1907bool VarDecl::checkInitIsICE() const {
John McCall73076432012-01-05 00:13:19 +00001908 // Initializers of weak variables are never ICEs.
1909 if (isWeak())
1910 return false;
1911
Richard Smith099e7f62011-12-19 06:19:21 +00001912 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1913 if (Eval->CheckedICE)
1914 // We have already checked whether this subexpression is an
1915 // integral constant expression.
1916 return Eval->IsICE;
1917
1918 const Expr *Init = cast<Expr>(Eval->Value);
1919 assert(!Init->isValueDependent());
1920
1921 // In C++11, evaluate the initializer to check whether it's a constant
1922 // expression.
Richard Smith80ad52f2013-01-02 11:42:31 +00001923 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001924 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smith099e7f62011-12-19 06:19:21 +00001925 evaluateValue(Notes);
1926 return Eval->IsICE;
1927 }
1928
1929 // It's an ICE whether or not the definition we found is
1930 // out-of-line. See DR 721 and the discussion in Clang PR
1931 // 6206 for details.
1932
1933 if (Eval->CheckingICE)
1934 return false;
1935 Eval->CheckingICE = true;
1936
1937 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1938 Eval->CheckingICE = false;
1939 Eval->CheckedICE = true;
1940 return Eval->IsICE;
1941}
1942
Douglas Gregor03e80032011-06-21 17:03:29 +00001943bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregor0b581082011-06-21 18:20:46 +00001944 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregor03e80032011-06-21 17:03:29 +00001945
1946 const Expr *E = getInit();
1947 if (!E)
1948 return false;
1949
1950 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1951 E = Cleanups->getSubExpr();
1952
1953 return isa<MaterializeTemporaryExpr>(E);
1954}
1955
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001956VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001957 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001958 return cast<VarDecl>(MSI->getInstantiatedFrom());
1959
1960 return 0;
1961}
1962
Douglas Gregor663b5a02009-10-14 20:14:33 +00001963TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001964 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001965 return MSI->getTemplateSpecializationKind();
1966
1967 return TSK_Undeclared;
1968}
1969
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001970MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001971 return getASTContext().getInstantiatedFromStaticDataMember(this);
1972}
1973
Douglas Gregor0a897e32009-10-15 17:21:20 +00001974void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1975 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001976 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001977 assert(MSI && "Not an instantiated static data member?");
1978 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001979 if (TSK != TSK_ExplicitSpecialization &&
1980 PointOfInstantiation.isValid() &&
1981 MSI->getPointOfInstantiation().isInvalid())
1982 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001983}
1984
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001985//===----------------------------------------------------------------------===//
1986// ParmVarDecl Implementation
1987//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001988
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001989ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001990 SourceLocation StartLoc,
1991 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001992 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001993 StorageClass S, Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001994 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001995 S, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001996}
1997
Reid Kleckner12df2462013-06-24 17:51:48 +00001998QualType ParmVarDecl::getOriginalType() const {
1999 TypeSourceInfo *TSI = getTypeSourceInfo();
2000 QualType T = TSI ? TSI->getType() : getType();
2001 if (const DecayedType *DT = dyn_cast<DecayedType>(T))
2002 return DT->getOriginalType();
2003 return T;
2004}
2005
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002006ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2007 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
2008 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002009 0, QualType(), 0, SC_None, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002010}
2011
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00002012SourceRange ParmVarDecl::getSourceRange() const {
2013 if (!hasInheritedDefaultArg()) {
2014 SourceRange ArgRange = getDefaultArgRange();
2015 if (ArgRange.isValid())
2016 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
2017 }
2018
Argyrios Kyrtzidis673c5d52013-04-17 01:56:48 +00002019 // DeclaratorDecl considers the range of postfix types as overlapping with the
2020 // declaration name, but this is not the case with parameters in ObjC methods.
2021 if (isa<ObjCMethodDecl>(getDeclContext()))
2022 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
2023
Argyrios Kyrtzidis0bfe83b2011-07-30 17:23:26 +00002024 return DeclaratorDecl::getSourceRange();
2025}
2026
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002027Expr *ParmVarDecl::getDefaultArg() {
2028 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
2029 assert(!hasUninstantiatedDefaultArg() &&
2030 "Default argument is not yet instantiated!");
2031
2032 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00002033 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002034 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00002035
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002036 return Arg;
2037}
2038
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002039SourceRange ParmVarDecl::getDefaultArgRange() const {
2040 if (const Expr *E = getInit())
2041 return E->getSourceRange();
2042
2043 if (hasUninstantiatedDefaultArg())
2044 return getUninstantiatedDefaultArg()->getSourceRange();
2045
2046 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00002047}
2048
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00002049bool ParmVarDecl::isParameterPack() const {
2050 return isa<PackExpansionType>(getType());
2051}
2052
Ted Kremenekd211cb72011-10-06 05:00:56 +00002053void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2054 getASTContext().setParameterIndex(this, parameterIndex);
2055 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2056}
2057
2058unsigned ParmVarDecl::getParameterIndexLarge() const {
2059 return getASTContext().getParameterIndex(this);
2060}
2061
Nuno Lopes99f06ba2008-12-17 23:39:55 +00002062//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002063// FunctionDecl Implementation
2064//===----------------------------------------------------------------------===//
2065
Benjamin Kramer5eada842013-02-22 15:46:01 +00002066void FunctionDecl::getNameForDiagnostic(
2067 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2068 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorda2142f2011-02-19 18:51:44 +00002069 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2070 if (TemplateArgs)
Benjamin Kramer5eada842013-02-22 15:46:01 +00002071 TemplateSpecializationType::PrintTemplateArgumentList(
2072 OS, TemplateArgs->data(), TemplateArgs->size(), Policy);
Douglas Gregorda2142f2011-02-19 18:51:44 +00002073}
2074
Ted Kremenek9498d382010-04-29 16:49:01 +00002075bool FunctionDecl::isVariadic() const {
2076 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
2077 return FT->isVariadic();
2078 return false;
2079}
2080
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002081bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
2082 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00002083 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002084 Definition = *I;
2085 return true;
2086 }
2087 }
2088
2089 return false;
2090}
2091
Anders Carlssonffb945f2011-05-14 23:26:09 +00002092bool FunctionDecl::hasTrivialBody() const
2093{
2094 Stmt *S = getBody();
2095 if (!S) {
2096 // Since we don't have a body for this function, we don't know if it's
2097 // trivial or not.
2098 return false;
2099 }
2100
2101 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2102 return true;
2103 return false;
2104}
2105
Sean Hunt10620eb2011-05-06 20:44:56 +00002106bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2107 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Sean Huntcd10dec2011-05-23 23:14:04 +00002108 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Sean Hunt10620eb2011-05-06 20:44:56 +00002109 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
2110 return true;
2111 }
2112 }
2113
2114 return false;
2115}
2116
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002117Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00002118 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2119 if (I->Body) {
2120 Definition = *I;
2121 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00002122 } else if (I->IsLateTemplateParsed) {
2123 Definition = *I;
2124 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00002125 }
2126 }
2127
2128 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002129}
2130
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00002131void FunctionDecl::setBody(Stmt *B) {
2132 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00002133 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00002134 EndRangeLoc = B->getLocEnd();
2135}
2136
Douglas Gregor21386642010-09-28 21:55:22 +00002137void FunctionDecl::setPure(bool P) {
2138 IsPure = P;
2139 if (P)
2140 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2141 Parent->markedVirtualFunctionPure();
2142}
2143
Douglas Gregor48a83b52009-09-12 00:17:51 +00002144bool FunctionDecl::isMain() const {
John McCall23c608d2011-05-15 17:49:20 +00002145 const TranslationUnitDecl *tunit =
2146 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2147 return tunit &&
David Blaikie4e4d0842012-03-11 07:00:24 +00002148 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall23c608d2011-05-15 17:49:20 +00002149 getIdentifier() &&
2150 getIdentifier()->isStr("main");
2151}
2152
2153bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2154 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2155 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2156 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2157 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2158 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2159
2160 if (isa<CXXRecordDecl>(getDeclContext())) return false;
2161 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2162
2163 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
2164 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
2165
2166 ASTContext &Context =
2167 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2168 ->getASTContext();
2169
2170 // The result type and first argument type are constant across all
2171 // these operators. The second argument must be exactly void*.
2172 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregor04495c82009-02-24 01:23:02 +00002173}
2174
Rafael Espindola950fee22013-02-14 01:18:37 +00002175LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6dcea672013-01-12 15:27:44 +00002176 // Users expect to be able to write
2177 // extern "C" void *__builtin_alloca (size_t);
2178 // so consider builtins as having C language linkage.
Rafael Espindola508276c2013-01-12 15:27:43 +00002179 if (getBuiltinID())
Rafael Espindola950fee22013-02-14 01:18:37 +00002180 return CLanguageLinkage;
Rafael Espindola508276c2013-01-12 15:27:43 +00002181
Rafael Espindola950fee22013-02-14 01:18:37 +00002182 return getLanguageLinkageTemplate(*this);
Rafael Espindola78eeba82012-12-28 14:21:58 +00002183}
2184
Rafael Espindola2d1b0962013-03-14 03:07:35 +00002185bool FunctionDecl::isExternC() const {
2186 return isExternCTemplate(*this);
2187}
2188
Rafael Espindolad8ffd0b2013-05-05 20:15:21 +00002189bool FunctionDecl::isInExternCContext() const {
2190 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
2191}
2192
2193bool FunctionDecl::isInExternCXXContext() const {
2194 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
2195}
2196
Douglas Gregor8499f3f2009-03-31 16:35:03 +00002197bool FunctionDecl::isGlobal() const {
2198 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
2199 return Method->isStatic();
2200
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002201 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00002202 return false;
2203
Mike Stump1eb44332009-09-09 15:08:12 +00002204 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00002205 DC->isNamespace();
2206 DC = DC->getParent()) {
2207 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
2208 if (!Namespace->getDeclName())
2209 return false;
2210 break;
2211 }
2212 }
2213
2214 return true;
2215}
2216
Richard Smithcd8ab512013-01-17 01:30:42 +00002217bool FunctionDecl::isNoReturn() const {
2218 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smith7586a6e2013-01-30 05:45:05 +00002219 hasAttr<C11NoReturnAttr>() ||
Richard Smithcd8ab512013-01-17 01:30:42 +00002220 getType()->getAs<FunctionType>()->getNoReturnAttr();
2221}
2222
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002223void
2224FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
2225 redeclarable_base::setPreviousDeclaration(PrevDecl);
2226
2227 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2228 FunctionTemplateDecl *PrevFunTmpl
2229 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
2230 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
2231 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
2232 }
Douglas Gregor8f150942010-12-09 16:59:22 +00002233
Axel Naumannd9d137e2011-11-08 18:21:06 +00002234 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregor8f150942010-12-09 16:59:22 +00002235 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002236}
2237
2238const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
2239 return getFirstDeclaration();
2240}
2241
2242FunctionDecl *FunctionDecl::getCanonicalDecl() {
2243 return getFirstDeclaration();
2244}
2245
Douglas Gregor3e41d602009-02-13 23:20:09 +00002246/// \brief Returns a value indicating whether this function
2247/// corresponds to a builtin function.
2248///
2249/// The function corresponds to a built-in function if it is
2250/// declared at translation scope or within an extern "C" block and
2251/// its name matches with the name of a builtin. The returned value
2252/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00002253/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00002254/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00002255unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar60d302a2012-03-06 23:52:37 +00002256 if (!getIdentifier())
Douglas Gregor3c385e52009-02-14 18:57:46 +00002257 return 0;
2258
2259 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar60d302a2012-03-06 23:52:37 +00002260 if (!BuiltinID)
2261 return 0;
2262
2263 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00002264 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2265 return BuiltinID;
2266
2267 // This function has the name of a known C library
2268 // function. Determine whether it actually refers to the C library
2269 // function or whether it just has the same name.
2270
Douglas Gregor9add3172009-02-17 03:23:10 +00002271 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00002272 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00002273 return 0;
2274
Douglas Gregor3c385e52009-02-14 18:57:46 +00002275 // If this function is at translation-unit scope and we're not in
2276 // C++, it refers to the C library function.
David Blaikie4e4d0842012-03-11 07:00:24 +00002277 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregor3c385e52009-02-14 18:57:46 +00002278 getDeclContext()->isTranslationUnit())
2279 return BuiltinID;
2280
2281 // If the function is in an extern "C" linkage specification and is
2282 // not marked "overloadable", it's the real function.
2283 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00002284 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00002285 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002286 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00002287 return BuiltinID;
2288
2289 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00002290 return 0;
2291}
2292
2293
Chris Lattner1ad9b282009-04-25 06:03:53 +00002294/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00002295/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00002296/// after it has been created.
2297unsigned FunctionDecl::getNumParams() const {
Eli Friedman482466b2012-08-30 22:22:09 +00002298 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00002299 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00002300 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00002301 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Reid Spencer5f016e22007-07-11 17:01:13 +00002303}
2304
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002305void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002306 ArrayRef<ParmVarDecl *> NewParamInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002307 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie4278c652011-09-21 18:16:56 +00002308 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00002311 if (!NewParamInfo.empty()) {
2312 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2313 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002314 }
2315}
2316
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002317void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy16f1f712012-02-29 10:24:19 +00002318 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2319
2320 if (!NewDecls.empty()) {
2321 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2322 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002323 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy16f1f712012-02-29 10:24:19 +00002324 }
2325}
2326
Chris Lattner8123a952008-04-10 02:22:51 +00002327/// getMinRequiredArguments - Returns the minimum number of arguments
2328/// needed to call this function. This may be fewer than the number of
2329/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002330/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00002331unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00002332 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002333 return getNumParams();
2334
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002335 unsigned NumRequiredArgs = getNumParams();
2336
2337 // If the last parameter is a parameter pack, we don't need an argument for
2338 // it.
2339 if (NumRequiredArgs > 0 &&
2340 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2341 --NumRequiredArgs;
2342
2343 // If this parameter has a default argument, we don't need an argument for
2344 // it.
2345 while (NumRequiredArgs > 0 &&
2346 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00002347 --NumRequiredArgs;
2348
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002349 // We might have parameter packs before the end. These can't be deduced,
2350 // but they can still handle multiple arguments.
2351 unsigned ArgIdx = NumRequiredArgs;
2352 while (ArgIdx > 0) {
2353 if (getParamDecl(ArgIdx - 1)->isParameterPack())
2354 NumRequiredArgs = ArgIdx;
2355
2356 --ArgIdx;
2357 }
2358
Chris Lattner8123a952008-04-10 02:22:51 +00002359 return NumRequiredArgs;
2360}
2361
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002362static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2363 // Only consider file-scope declarations in this test.
2364 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2365 return false;
2366
2367 // Only consider explicit declarations; the presence of a builtin for a
2368 // libcall shouldn't affect whether a definition is externally visible.
2369 if (Redecl->isImplicit())
2370 return false;
2371
2372 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2373 return true; // Not an inline definition
2374
2375 return false;
2376}
2377
Nick Lewyckydce67a72011-07-18 05:26:13 +00002378/// \brief For a function declaration in C or C++, determine whether this
2379/// declaration causes the definition to be externally visible.
2380///
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002381/// Specifically, this determines if adding the current declaration to the set
2382/// of redeclarations of the given functions causes
2383/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewyckydce67a72011-07-18 05:26:13 +00002384bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2385 assert(!doesThisDeclarationHaveABody() &&
2386 "Must have a declaration without a body.");
2387
2388 ASTContext &Context = getASTContext();
2389
David Blaikie4e4d0842012-03-11 07:00:24 +00002390 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002391 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2392 // an externally visible definition.
2393 //
2394 // FIXME: What happens if gnu_inline gets added on after the first
2395 // declaration?
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002396 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002397 return false;
2398
2399 const FunctionDecl *Prev = this;
2400 bool FoundBody = false;
2401 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7247c882013-05-15 07:37:26 +00002402 FoundBody |= Prev->Body.isValid();
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002403
2404 if (Prev->Body) {
2405 // If it's not the case that both 'inline' and 'extern' are
2406 // specified on the definition, then it is always externally visible.
2407 if (!Prev->isInlineSpecified() ||
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002408 Prev->getStorageClass() != SC_Extern)
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002409 return false;
2410 } else if (Prev->isInlineSpecified() &&
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002411 Prev->getStorageClass() != SC_Extern) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002412 return false;
2413 }
2414 }
2415 return FoundBody;
2416 }
2417
David Blaikie4e4d0842012-03-11 07:00:24 +00002418 if (Context.getLangOpts().CPlusPlus)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002419 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002420
2421 // C99 6.7.4p6:
2422 // [...] If all of the file scope declarations for a function in a
2423 // translation unit include the inline function specifier without extern,
2424 // then the definition in that translation unit is an inline definition.
2425 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewyckydce67a72011-07-18 05:26:13 +00002426 return false;
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002427 const FunctionDecl *Prev = this;
2428 bool FoundBody = false;
2429 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7247c882013-05-15 07:37:26 +00002430 FoundBody |= Prev->Body.isValid();
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002431 if (RedeclForcesDefC99(Prev))
2432 return false;
2433 }
2434 return FoundBody;
Nick Lewyckydce67a72011-07-18 05:26:13 +00002435}
2436
Richard Smithd4497dd2013-01-25 00:08:28 +00002437/// \brief For an inline function definition in C, or for a gnu_inline function
2438/// in C++, determine whether the definition will be externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002439///
2440/// Inline function definitions are always available for inlining optimizations.
2441/// However, depending on the language dialect, declaration specifiers, and
2442/// attributes, the definition of an inline function may or may not be
2443/// "externally" visible to other translation units in the program.
2444///
2445/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00002446/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002447/// inline definition becomes externally visible (C99 6.7.4p6).
2448///
2449/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2450/// definition, we use the GNU semantics for inline, which are nearly the
2451/// opposite of C99 semantics. In particular, "inline" by itself will create
2452/// an externally visible symbol, but "extern inline" will not create an
2453/// externally visible symbol.
2454bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Sean Hunt10620eb2011-05-06 20:44:56 +00002455 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002456 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00002457 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002458
David Blaikie4e4d0842012-03-11 07:00:24 +00002459 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002460 // Note: If you change the logic here, please change
2461 // doesDeclarationForceExternallyVisibleDefinition as well.
2462 //
Douglas Gregor8f150942010-12-09 16:59:22 +00002463 // If it's not the case that both 'inline' and 'extern' are
2464 // specified on the definition, then this inline definition is
2465 // externally visible.
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002466 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregor8f150942010-12-09 16:59:22 +00002467 return true;
2468
2469 // If any declaration is 'inline' but not 'extern', then this definition
2470 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002471 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2472 Redecl != RedeclEnd;
2473 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00002474 if (Redecl->isInlineSpecified() &&
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002475 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002476 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00002477 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002478
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002479 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002480 }
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002481
Richard Smithd4497dd2013-01-25 00:08:28 +00002482 // The rest of this function is C-only.
2483 assert(!Context.getLangOpts().CPlusPlus &&
2484 "should not use C inline rules in C++");
2485
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002486 // C99 6.7.4p6:
2487 // [...] If all of the file scope declarations for a function in a
2488 // translation unit include the inline function specifier without extern,
2489 // then the definition in that translation unit is an inline definition.
2490 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2491 Redecl != RedeclEnd;
2492 ++Redecl) {
Eli Friedmana3b9fa22012-02-07 03:50:18 +00002493 if (RedeclForcesDefC99(*Redecl))
2494 return true;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00002495 }
2496
2497 // C99 6.7.4p6:
2498 // An inline definition does not provide an external definition for the
2499 // function, and does not forbid an external definition in another
2500 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00002501 return false;
2502}
2503
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002504/// getOverloadedOperator - Which C++ overloaded operator this
2505/// function represents, if any.
2506OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00002507 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2508 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002509 else
2510 return OO_None;
2511}
2512
Sean Hunta6c058d2010-01-13 09:01:02 +00002513/// getLiteralIdentifier - The literal suffix identifier this function
2514/// represents, if any.
2515const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2516 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2517 return getDeclName().getCXXLiteralIdentifier();
2518 else
2519 return 0;
2520}
2521
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002522FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2523 if (TemplateOrSpecialization.isNull())
2524 return TK_NonTemplate;
2525 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2526 return TK_FunctionTemplate;
2527 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2528 return TK_MemberSpecialization;
2529 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2530 return TK_FunctionTemplateSpecialization;
2531 if (TemplateOrSpecialization.is
2532 <DependentFunctionTemplateSpecializationInfo*>())
2533 return TK_DependentFunctionTemplateSpecialization;
2534
David Blaikieb219cfc2011-09-23 05:06:16 +00002535 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00002536}
2537
Douglas Gregor2db32322009-10-07 23:56:10 +00002538FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002539 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00002540 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2541
2542 return 0;
2543}
2544
2545void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002546FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2547 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00002548 TemplateSpecializationKind TSK) {
2549 assert(TemplateOrSpecialization.isNull() &&
2550 "Member function is already a specialization");
2551 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002552 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00002553 TemplateOrSpecialization = Info;
2554}
2555
Douglas Gregor3b846b62009-10-27 20:53:28 +00002556bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002557 // If the function is invalid, it can't be implicitly instantiated.
2558 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00002559 return false;
2560
2561 switch (getTemplateSpecializationKind()) {
2562 case TSK_Undeclared:
Douglas Gregor3b846b62009-10-27 20:53:28 +00002563 case TSK_ExplicitInstantiationDefinition:
2564 return false;
2565
2566 case TSK_ImplicitInstantiation:
2567 return true;
2568
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002569 // It is possible to instantiate TSK_ExplicitSpecialization kind
2570 // if the FunctionDecl has a class scope specialization pattern.
2571 case TSK_ExplicitSpecialization:
2572 return getClassScopeSpecializationPattern() != 0;
2573
Douglas Gregor3b846b62009-10-27 20:53:28 +00002574 case TSK_ExplicitInstantiationDeclaration:
2575 // Handled below.
2576 break;
2577 }
2578
2579 // Find the actual template from which we will instantiate.
2580 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002581 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00002582 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002583 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00002584
2585 // C++0x [temp.explicit]p9:
2586 // Except for inline functions, other explicit instantiation declarations
2587 // have the effect of suppressing the implicit instantiation of the entity
2588 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002589 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00002590 return true;
2591
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002592 return PatternDecl->isInlined();
Ted Kremenek75df4ee2011-12-01 00:59:17 +00002593}
2594
2595bool FunctionDecl::isTemplateInstantiation() const {
2596 switch (getTemplateSpecializationKind()) {
2597 case TSK_Undeclared:
2598 case TSK_ExplicitSpecialization:
2599 return false;
2600 case TSK_ImplicitInstantiation:
2601 case TSK_ExplicitInstantiationDeclaration:
2602 case TSK_ExplicitInstantiationDefinition:
2603 return true;
2604 }
2605 llvm_unreachable("All TSK values handled.");
2606}
Douglas Gregor3b846b62009-10-27 20:53:28 +00002607
2608FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002609 // Handle class scope explicit specialization special case.
2610 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2611 return getClassScopeSpecializationPattern();
2612
Douglas Gregor3b846b62009-10-27 20:53:28 +00002613 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2614 while (Primary->getInstantiatedFromMemberTemplate()) {
2615 // If we have hit a point where the user provided a specialization of
2616 // this template, we're done looking.
2617 if (Primary->isMemberSpecialization())
2618 break;
2619
2620 Primary = Primary->getInstantiatedFromMemberTemplate();
2621 }
2622
2623 return Primary->getTemplatedDecl();
2624 }
2625
2626 return getInstantiatedFromMemberFunction();
2627}
2628
Douglas Gregor16e8be22009-06-29 17:30:29 +00002629FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002630 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002631 = TemplateOrSpecialization
2632 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002633 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00002634 }
2635 return 0;
2636}
2637
Francois Pichetaf0f4d02011-08-14 03:52:19 +00002638FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2639 return getASTContext().getClassScopeSpecializationPattern(this);
2640}
2641
Douglas Gregor16e8be22009-06-29 17:30:29 +00002642const TemplateArgumentList *
2643FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002644 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002645 = TemplateOrSpecialization
2646 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00002647 return Info->TemplateArguments;
2648 }
2649 return 0;
2650}
2651
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00002652const ASTTemplateArgumentListInfo *
Abramo Bagnarae03db982010-05-20 15:32:11 +00002653FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2654 if (FunctionTemplateSpecializationInfo *Info
2655 = TemplateOrSpecialization
2656 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2657 return Info->TemplateArgumentsAsWritten;
2658 }
2659 return 0;
2660}
2661
Mike Stump1eb44332009-09-09 15:08:12 +00002662void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00002663FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2664 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00002665 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002666 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00002667 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00002668 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2669 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00002670 assert(TSK != TSK_Undeclared &&
2671 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00002672 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00002673 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00002674 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00002675 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2676 TemplateArgs,
2677 TemplateArgsAsWritten,
2678 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00002679 TemplateOrSpecialization = Info;
Douglas Gregor1e1e9722012-03-28 14:34:23 +00002680 Template->addSpecialization(Info, InsertPos);
Douglas Gregor1637be72009-06-26 00:10:03 +00002681}
2682
John McCallaf2094e2010-04-08 09:05:18 +00002683void
2684FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2685 const UnresolvedSetImpl &Templates,
2686 const TemplateArgumentListInfo &TemplateArgs) {
2687 assert(TemplateOrSpecialization.isNull());
2688 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2689 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00002690 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00002691 void *Buffer = Context.Allocate(Size);
2692 DependentFunctionTemplateSpecializationInfo *Info =
2693 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2694 TemplateArgs);
2695 TemplateOrSpecialization = Info;
2696}
2697
2698DependentFunctionTemplateSpecializationInfo::
2699DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2700 const TemplateArgumentListInfo &TArgs)
2701 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2702
2703 d.NumTemplates = Ts.size();
2704 d.NumArgs = TArgs.size();
2705
2706 FunctionTemplateDecl **TsArray =
2707 const_cast<FunctionTemplateDecl**>(getTemplates());
2708 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2709 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2710
2711 TemplateArgumentLoc *ArgsArray =
2712 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2713 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2714 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2715}
2716
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002717TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002718 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002719 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00002720 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002721 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00002722 if (FTSInfo)
2723 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Douglas Gregor2db32322009-10-07 23:56:10 +00002725 MemberSpecializationInfo *MSInfo
2726 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2727 if (MSInfo)
2728 return MSInfo->getTemplateSpecializationKind();
2729
2730 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002731}
2732
Mike Stump1eb44332009-09-09 15:08:12 +00002733void
Douglas Gregor0a897e32009-10-15 17:21:20 +00002734FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2735 SourceLocation PointOfInstantiation) {
2736 if (FunctionTemplateSpecializationInfo *FTSInfo
2737 = TemplateOrSpecialization.dyn_cast<
2738 FunctionTemplateSpecializationInfo*>()) {
2739 FTSInfo->setTemplateSpecializationKind(TSK);
2740 if (TSK != TSK_ExplicitSpecialization &&
2741 PointOfInstantiation.isValid() &&
2742 FTSInfo->getPointOfInstantiation().isInvalid())
2743 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2744 } else if (MemberSpecializationInfo *MSInfo
2745 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2746 MSInfo->setTemplateSpecializationKind(TSK);
2747 if (TSK != TSK_ExplicitSpecialization &&
2748 PointOfInstantiation.isValid() &&
2749 MSInfo->getPointOfInstantiation().isInvalid())
2750 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2751 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00002752 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor0a897e32009-10-15 17:21:20 +00002753}
2754
2755SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00002756 if (FunctionTemplateSpecializationInfo *FTSInfo
2757 = TemplateOrSpecialization.dyn_cast<
2758 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002759 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00002760 else if (MemberSpecializationInfo *MSInfo
2761 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00002762 return MSInfo->getPointOfInstantiation();
2763
2764 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00002765}
2766
Douglas Gregor9f185072009-09-11 20:15:17 +00002767bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00002768 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00002769 return true;
2770
2771 // If this function was instantiated from a member function of a
2772 // class template, check whether that member function was defined out-of-line.
2773 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2774 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002775 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002776 return Definition->isOutOfLine();
2777 }
2778
2779 // If this function was instantiated from a function template,
2780 // check whether that function template was defined out-of-line.
2781 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2782 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002783 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002784 return Definition->isOutOfLine();
2785 }
2786
2787 return false;
2788}
2789
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002790SourceRange FunctionDecl::getSourceRange() const {
2791 return SourceRange(getOuterLocStart(), EndRangeLoc);
2792}
2793
Anna Zaks9392d4e2012-01-18 02:45:01 +00002794unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002795 IdentifierInfo *FnInfo = getIdentifier();
2796
2797 if (!FnInfo)
Anna Zaks0a151a12012-01-17 00:37:07 +00002798 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002799
2800 // Builtin handling.
2801 switch (getBuiltinID()) {
2802 case Builtin::BI__builtin_memset:
2803 case Builtin::BI__builtin___memset_chk:
2804 case Builtin::BImemset:
Anna Zaks0a151a12012-01-17 00:37:07 +00002805 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002806
2807 case Builtin::BI__builtin_memcpy:
2808 case Builtin::BI__builtin___memcpy_chk:
2809 case Builtin::BImemcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002810 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002811
2812 case Builtin::BI__builtin_memmove:
2813 case Builtin::BI__builtin___memmove_chk:
2814 case Builtin::BImemmove:
Anna Zaks0a151a12012-01-17 00:37:07 +00002815 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002816
2817 case Builtin::BIstrlcpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002818 return Builtin::BIstrlcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002819 case Builtin::BIstrlcat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002820 return Builtin::BIstrlcat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002821
2822 case Builtin::BI__builtin_memcmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002823 case Builtin::BImemcmp:
2824 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002825
2826 case Builtin::BI__builtin_strncpy:
2827 case Builtin::BI__builtin___strncpy_chk:
2828 case Builtin::BIstrncpy:
Anna Zaks0a151a12012-01-17 00:37:07 +00002829 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002830
2831 case Builtin::BI__builtin_strncmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002832 case Builtin::BIstrncmp:
2833 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002834
2835 case Builtin::BI__builtin_strncasecmp:
Anna Zaks0a151a12012-01-17 00:37:07 +00002836 case Builtin::BIstrncasecmp:
2837 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002838
2839 case Builtin::BI__builtin_strncat:
Anna Zaksc36bedc2012-02-01 19:08:57 +00002840 case Builtin::BI__builtin___strncat_chk:
Anna Zaksd9b859a2012-01-13 21:52:01 +00002841 case Builtin::BIstrncat:
Anna Zaks0a151a12012-01-17 00:37:07 +00002842 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002843
2844 case Builtin::BI__builtin_strndup:
2845 case Builtin::BIstrndup:
Anna Zaks0a151a12012-01-17 00:37:07 +00002846 return Builtin::BIstrndup;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002847
Anna Zaksc36bedc2012-02-01 19:08:57 +00002848 case Builtin::BI__builtin_strlen:
2849 case Builtin::BIstrlen:
2850 return Builtin::BIstrlen;
2851
Anna Zaksd9b859a2012-01-13 21:52:01 +00002852 default:
Rafael Espindolad2fdd422013-02-14 01:47:04 +00002853 if (isExternC()) {
Anna Zaksd9b859a2012-01-13 21:52:01 +00002854 if (FnInfo->isStr("memset"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002855 return Builtin::BImemset;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002856 else if (FnInfo->isStr("memcpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002857 return Builtin::BImemcpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002858 else if (FnInfo->isStr("memmove"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002859 return Builtin::BImemmove;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002860 else if (FnInfo->isStr("memcmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002861 return Builtin::BImemcmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002862 else if (FnInfo->isStr("strncpy"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002863 return Builtin::BIstrncpy;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002864 else if (FnInfo->isStr("strncmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002865 return Builtin::BIstrncmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002866 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002867 return Builtin::BIstrncasecmp;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002868 else if (FnInfo->isStr("strncat"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002869 return Builtin::BIstrncat;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002870 else if (FnInfo->isStr("strndup"))
Anna Zaks0a151a12012-01-17 00:37:07 +00002871 return Builtin::BIstrndup;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002872 else if (FnInfo->isStr("strlen"))
2873 return Builtin::BIstrlen;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002874 }
2875 break;
2876 }
Anna Zaks0a151a12012-01-17 00:37:07 +00002877 return 0;
Anna Zaksd9b859a2012-01-13 21:52:01 +00002878}
2879
Chris Lattner8a934232008-03-31 00:36:02 +00002880//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002881// FieldDecl Implementation
2882//===----------------------------------------------------------------------===//
2883
Jay Foad4ba2a172011-01-12 09:06:06 +00002884FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002885 SourceLocation StartLoc, SourceLocation IdLoc,
2886 IdentifierInfo *Id, QualType T,
Richard Smith7a614d82011-06-11 17:19:42 +00002887 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smithca523302012-06-10 03:12:00 +00002888 InClassInitStyle InitStyle) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002889 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithca523302012-06-10 03:12:00 +00002890 BW, Mutable, InitStyle);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002891}
2892
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002893FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2894 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2895 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smithca523302012-06-10 03:12:00 +00002896 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002897}
2898
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002899bool FieldDecl::isAnonymousStructOrUnion() const {
2900 if (!isImplicit() || getDeclName())
2901 return false;
2902
2903 if (const RecordType *Record = getType()->getAs<RecordType>())
2904 return Record->getDecl()->isAnonymousStructOrUnion();
2905
2906 return false;
2907}
2908
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002909unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2910 assert(isBitField() && "not a bitfield");
2911 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2912 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2913}
2914
John McCallba4f5d52011-01-20 07:57:12 +00002915unsigned FieldDecl::getFieldIndex() const {
2916 if (CachedFieldIndex) return CachedFieldIndex - 1;
2917
Richard Smith180f4792011-11-10 06:34:14 +00002918 unsigned Index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002919 const RecordDecl *RD = getParent();
Richard Smith180f4792011-11-10 06:34:14 +00002920
2921 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
Eli Friedmancd06f262013-06-26 20:50:34 +00002922 I != E; ++I, ++Index)
David Blaikie262bc182012-04-30 02:36:29 +00002923 I->CachedFieldIndex = Index + 1;
John McCallba4f5d52011-01-20 07:57:12 +00002924
Richard Smith180f4792011-11-10 06:34:14 +00002925 assert(CachedFieldIndex && "failed to find field in parent");
2926 return CachedFieldIndex - 1;
John McCallba4f5d52011-01-20 07:57:12 +00002927}
2928
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002929SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnarad330e232011-08-05 08:02:55 +00002930 if (const Expr *E = InitializerOrBitWidth.getPointer())
2931 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002932 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002933}
2934
Abramo Bagnaraa5335762012-07-02 20:35:48 +00002935void FieldDecl::setBitWidth(Expr *Width) {
2936 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2937 "bit width or initializer already set");
2938 InitializerOrBitWidth.setPointer(Width);
2939}
2940
Richard Smith7a614d82011-06-11 17:19:42 +00002941void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smithca523302012-06-10 03:12:00 +00002942 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith7a614d82011-06-11 17:19:42 +00002943 "bit width or initializer already set");
2944 InitializerOrBitWidth.setPointer(Init);
Richard Smith7a614d82011-06-11 17:19:42 +00002945}
2946
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002947//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002948// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002949//===----------------------------------------------------------------------===//
2950
Douglas Gregor1693e152010-07-06 18:42:40 +00002951SourceLocation TagDecl::getOuterLocStart() const {
2952 return getTemplateOrInnerLocStart(this);
2953}
2954
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002955SourceRange TagDecl::getSourceRange() const {
2956 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002957 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002958}
2959
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002960TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002961 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002962}
2963
Rafael Espindola2d9e8832013-03-12 21:06:00 +00002964void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2965 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002966 if (TypeForDecl)
Rafael Espindola2d1b0962013-03-14 03:07:35 +00002967 assert(TypeForDecl->isLinkageValid());
2968 assert(isLinkageValid());
Douglas Gregor60e70642010-05-19 18:39:18 +00002969}
2970
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002971void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002972 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002973
David Blaikie66cff722012-11-14 01:52:05 +00002974 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall86ff3082010-02-04 22:26:26 +00002975 struct CXXRecordDecl::DefinitionData *Data =
2976 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002977 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2978 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002979 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002980}
2981
2982void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002983 assert((!isa<CXXRecordDecl>(this) ||
2984 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2985 "definition completed but not started");
2986
John McCall5e1cdac2011-10-07 06:10:15 +00002987 IsCompleteDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002988 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002989
2990 if (ASTMutationListener *L = getASTMutationListener())
2991 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002992}
2993
John McCall5e1cdac2011-10-07 06:10:15 +00002994TagDecl *TagDecl::getDefinition() const {
2995 if (isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002996 return const_cast<TagDecl *>(this);
Douglas Gregor6bd99292013-02-09 01:35:03 +00002997
2998 // If it's possible for us to have an out-of-date definition, check now.
2999 if (MayHaveOutOfDateDef) {
3000 if (IdentifierInfo *II = getIdentifier()) {
3001 if (II->isOutOfDate()) {
3002 updateOutOfDate(*II);
3003 }
3004 }
3005 }
3006
Andrew Trick220a9c82010-10-19 21:54:32 +00003007 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
3008 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00003009
3010 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00003011 R != REnd; ++R)
John McCall5e1cdac2011-10-07 06:10:15 +00003012 if (R->isCompleteDefinition())
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00003013 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00003015 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00003016}
3017
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003018void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
3019 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00003020 // Make sure the extended qualifier info is allocated.
3021 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00003022 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00003023 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003024 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier30601782011-08-17 23:08:45 +00003025 } else {
John McCallb6217662010-03-15 10:12:16 +00003026 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00003027 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00003028 if (getExtInfo()->NumTemplParamLists == 0) {
3029 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00003030 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00003031 }
3032 else
3033 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00003034 }
3035 }
3036}
3037
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00003038void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
3039 unsigned NumTPLists,
3040 TemplateParameterList **TPLists) {
3041 assert(NumTPLists > 0);
3042 // Make sure the extended decl info is allocated.
3043 if (!hasExtInfo())
3044 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00003045 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00003046 // Set the template parameter lists info.
3047 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
3048}
3049
Ted Kremenek4b7c9832008-09-05 17:16:31 +00003050//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003051// EnumDecl Implementation
3052//===----------------------------------------------------------------------===//
3053
David Blaikie99ba9e32011-12-20 02:48:34 +00003054void EnumDecl::anchor() { }
3055
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003056EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3057 SourceLocation StartLoc, SourceLocation IdLoc,
3058 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003059 EnumDecl *PrevDecl, bool IsScoped,
3060 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003061 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003062 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor6bd99292013-02-09 01:35:03 +00003063 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003064 C.getTypeDeclType(Enum, PrevDecl);
3065 return Enum;
3066}
3067
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003068EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3069 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00003070 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
3071 0, 0, false, false, false);
3072 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3073 return Enum;
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00003074}
3075
Douglas Gregor838db382010-02-11 01:19:42 +00003076void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00003077 QualType NewPromotionType,
3078 unsigned NumPositiveBits,
3079 unsigned NumNegativeBits) {
John McCall5e1cdac2011-10-07 06:10:15 +00003080 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003081 if (!IntegerType)
3082 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003083 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00003084 setNumPositiveBits(NumPositiveBits);
3085 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003086 TagDecl::completeDefinition();
3087}
3088
Richard Smith1af83c42012-03-23 03:33:32 +00003089TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3090 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3091 return MSI->getTemplateSpecializationKind();
3092
3093 return TSK_Undeclared;
3094}
3095
3096void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3097 SourceLocation PointOfInstantiation) {
3098 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3099 assert(MSI && "Not an instantiated member enumeration?");
3100 MSI->setTemplateSpecializationKind(TSK);
3101 if (TSK != TSK_ExplicitSpecialization &&
3102 PointOfInstantiation.isValid() &&
3103 MSI->getPointOfInstantiation().isInvalid())
3104 MSI->setPointOfInstantiation(PointOfInstantiation);
3105}
3106
Richard Smithf1c66b42012-03-14 23:13:10 +00003107EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3108 if (SpecializationInfo)
3109 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3110
3111 return 0;
3112}
3113
3114void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3115 TemplateSpecializationKind TSK) {
3116 assert(!SpecializationInfo && "Member enum is already a specialization");
3117 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3118}
3119
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003120//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00003121// RecordDecl Implementation
3122//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00003123
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003124RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
3125 SourceLocation StartLoc, SourceLocation IdLoc,
3126 IdentifierInfo *Id, RecordDecl *PrevDecl)
3127 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00003128 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00003129 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003130 HasObjectMember = false;
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +00003131 HasVolatileMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003132 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00003133 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00003134}
3135
Jay Foad4ba2a172011-01-12 09:06:06 +00003136RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003137 SourceLocation StartLoc, SourceLocation IdLoc,
3138 IdentifierInfo *Id, RecordDecl* PrevDecl) {
3139 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
3140 PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00003141 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3142
Ted Kremenek4b7c9832008-09-05 17:16:31 +00003143 C.getTypeDeclType(R, PrevDecl);
3144 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00003145}
3146
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003147RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
3148 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00003149 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
3150 SourceLocation(), 0, 0);
3151 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3152 return R;
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00003153}
3154
Douglas Gregorc9b5b402009-03-25 15:59:44 +00003155bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00003156 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00003157 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3158}
3159
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003160RecordDecl::field_iterator RecordDecl::field_begin() const {
3161 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3162 LoadFieldsFromExternalStorage();
3163
3164 return field_iterator(decl_iterator(FirstDecl));
3165}
3166
Douglas Gregorda2142f2011-02-19 18:51:44 +00003167/// completeDefinition - Notes that the definition of this type is now
3168/// complete.
3169void RecordDecl::completeDefinition() {
John McCall5e1cdac2011-10-07 06:10:15 +00003170 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorda2142f2011-02-19 18:51:44 +00003171 TagDecl::completeDefinition();
3172}
3173
Eli Friedman5f608ae2012-10-12 23:29:20 +00003174/// isMsStruct - Get whether or not this record uses ms_struct layout.
3175/// This which can be turned on with an attribute, pragma, or the
3176/// -mms-bitfields command-line option.
3177bool RecordDecl::isMsStruct(const ASTContext &C) const {
3178 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
3179}
3180
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00003181static bool isFieldOrIndirectField(Decl::Kind K) {
3182 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3183}
3184
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003185void RecordDecl::LoadFieldsFromExternalStorage() const {
3186 ExternalASTSource *Source = getASTContext().getExternalSource();
3187 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3188
3189 // Notify that we have a RecordDecl doing some initialization.
3190 ExternalASTSource::Deserializing TheFields(Source);
3191
Chris Lattner5f9e2722011-07-23 10:55:15 +00003192 SmallVector<Decl*, 64> Decls;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00003193 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00003194 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
3195 Decls)) {
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00003196 case ELR_Success:
3197 break;
3198
3199 case ELR_AlreadyLoaded:
3200 case ELR_Failure:
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003201 return;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00003202 }
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003203
3204#ifndef NDEBUG
3205 // Check that all decls we got were FieldDecls.
3206 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidis22cd9ac2012-09-10 22:04:22 +00003207 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003208#endif
3209
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003210 if (Decls.empty())
3211 return;
3212
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +00003213 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
3214 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003215}
3216
Steve Naroff56ee6892008-10-08 17:01:13 +00003217//===----------------------------------------------------------------------===//
3218// BlockDecl Implementation
3219//===----------------------------------------------------------------------===//
3220
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003221void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffe78b8092009-03-13 16:56:44 +00003222 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00003223
Steve Naroffe78b8092009-03-13 16:56:44 +00003224 // Zero params -> null pointer.
David Blaikie4278c652011-09-21 18:16:56 +00003225 if (!NewParamInfo.empty()) {
3226 NumParams = NewParamInfo.size();
3227 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3228 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffe78b8092009-03-13 16:56:44 +00003229 }
3230}
3231
John McCall6b5a61b2011-02-07 10:33:21 +00003232void BlockDecl::setCaptures(ASTContext &Context,
3233 const Capture *begin,
3234 const Capture *end,
3235 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00003236 CapturesCXXThis = capturesCXXThis;
3237
3238 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00003239 NumCaptures = 0;
3240 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00003241 return;
3242 }
3243
John McCall6b5a61b2011-02-07 10:33:21 +00003244 NumCaptures = end - begin;
3245
3246 // Avoid new Capture[] because we don't want to provide a default
3247 // constructor.
3248 size_t allocationSize = NumCaptures * sizeof(Capture);
3249 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
3250 memcpy(buffer, begin, allocationSize);
3251 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00003252}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003253
John McCall204e1332011-06-15 22:51:16 +00003254bool BlockDecl::capturesVariable(const VarDecl *variable) const {
3255 for (capture_const_iterator
3256 i = capture_begin(), e = capture_end(); i != e; ++i)
3257 // Only auto vars can be captured, so no redeclaration worries.
3258 if (i->getVariable() == variable)
3259 return true;
3260
3261 return false;
3262}
3263
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00003264SourceRange BlockDecl::getSourceRange() const {
3265 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
3266}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003267
3268//===----------------------------------------------------------------------===//
3269// Other Decl Allocation/Deallocation Method Implementations
3270//===----------------------------------------------------------------------===//
3271
David Blaikie99ba9e32011-12-20 02:48:34 +00003272void TranslationUnitDecl::anchor() { }
3273
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003274TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
3275 return new (C) TranslationUnitDecl(C);
3276}
3277
David Blaikie99ba9e32011-12-20 02:48:34 +00003278void LabelDecl::anchor() { }
3279
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003280LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00003281 SourceLocation IdentL, IdentifierInfo *II) {
3282 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
3283}
3284
3285LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3286 SourceLocation IdentL, IdentifierInfo *II,
3287 SourceLocation GnuLabelL) {
3288 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3289 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00003290}
3291
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003292LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3293 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3294 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor06c91932010-10-27 19:49:05 +00003295}
3296
David Blaikie99ba9e32011-12-20 02:48:34 +00003297void ValueDecl::anchor() { }
3298
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +00003299bool ValueDecl::isWeak() const {
3300 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3301 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3302 return true;
3303
3304 return isWeakImported();
3305}
3306
David Blaikie99ba9e32011-12-20 02:48:34 +00003307void ImplicitParamDecl::anchor() { }
3308
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003309ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003310 SourceLocation IdLoc,
3311 IdentifierInfo *Id,
3312 QualType Type) {
3313 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003314}
3315
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003316ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3317 unsigned ID) {
3318 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3319 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3320}
3321
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003322FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003323 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00003324 const DeclarationNameInfo &NameInfo,
3325 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003326 StorageClass SC,
Douglas Gregor8f150942010-12-09 16:59:22 +00003327 bool isInlineSpecified,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00003328 bool hasWrittenPrototype,
3329 bool isConstexprSpecified) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003330 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003331 T, TInfo, SC,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00003332 isInlineSpecified,
3333 isConstexprSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003334 New->HasWrittenPrototype = hasWrittenPrototype;
3335 return New;
3336}
3337
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003338FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3339 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3340 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3341 DeclarationNameInfo(), QualType(), 0,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003342 SC_None, false, false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003343}
3344
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003345BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3346 return new (C) BlockDecl(DC, L);
3347}
3348
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003349BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3350 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3351 return new (Mem) BlockDecl(0, SourceLocation());
3352}
3353
John McCall76da55d2013-04-16 07:28:30 +00003354MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3355 unsigned ID) {
3356 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(MSPropertyDecl));
3357 return new (Mem) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
3358 QualType(), 0, SourceLocation(),
3359 0, 0);
3360}
3361
Ben Langmuir8c045ac2013-05-03 19:00:33 +00003362CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
3363 unsigned NumParams) {
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00003364 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
Ben Langmuir8c045ac2013-05-03 19:00:33 +00003365 return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00003366}
3367
Ben Langmuirdc5be4f2013-05-03 19:20:19 +00003368CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3369 unsigned NumParams) {
3370 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3371 void *Mem = AllocateDeserializedDecl(C, ID, Size);
3372 return new (Mem) CapturedDecl(0, NumParams);
3373}
3374
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003375EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3376 SourceLocation L,
3377 IdentifierInfo *Id, QualType T,
3378 Expr *E, const llvm::APSInt &V) {
3379 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3380}
3381
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003382EnumConstantDecl *
3383EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3384 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3385 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3386 llvm::APSInt());
3387}
3388
David Blaikie99ba9e32011-12-20 02:48:34 +00003389void IndirectFieldDecl::anchor() { }
3390
Benjamin Kramerd9811462010-11-21 14:11:41 +00003391IndirectFieldDecl *
3392IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3393 IdentifierInfo *Id, QualType T, NamedDecl **CH,
3394 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00003395 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3396}
3397
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003398IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3399 unsigned ID) {
3400 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3401 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3402 QualType(), 0, 0);
3403}
3404
Douglas Gregor8e7139c2010-09-01 20:41:53 +00003405SourceRange EnumConstantDecl::getSourceRange() const {
3406 SourceLocation End = getLocation();
3407 if (Init)
3408 End = Init->getLocEnd();
3409 return SourceRange(getLocation(), End);
3410}
3411
David Blaikie99ba9e32011-12-20 02:48:34 +00003412void TypeDecl::anchor() { }
3413
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003414TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00003415 SourceLocation StartLoc, SourceLocation IdLoc,
3416 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3417 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003418}
3419
David Blaikie99ba9e32011-12-20 02:48:34 +00003420void TypedefNameDecl::anchor() { }
3421
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003422TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3423 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3424 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3425}
3426
Richard Smith162e1c12011-04-15 14:24:37 +00003427TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3428 SourceLocation StartLoc,
3429 SourceLocation IdLoc, IdentifierInfo *Id,
3430 TypeSourceInfo *TInfo) {
3431 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3432}
3433
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003434TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3435 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3436 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3437}
3438
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00003439SourceRange TypedefDecl::getSourceRange() const {
3440 SourceLocation RangeEnd = getLocation();
3441 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3442 if (typeIsPostfix(TInfo->getType()))
3443 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3444 }
3445 return SourceRange(getLocStart(), RangeEnd);
3446}
3447
Richard Smith162e1c12011-04-15 14:24:37 +00003448SourceRange TypeAliasDecl::getSourceRange() const {
3449 SourceLocation RangeEnd = getLocStart();
3450 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3451 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3452 return SourceRange(getLocStart(), RangeEnd);
3453}
3454
David Blaikie99ba9e32011-12-20 02:48:34 +00003455void FileScopeAsmDecl::anchor() { }
3456
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003457FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00003458 StringLiteral *Str,
3459 SourceLocation AsmLoc,
3460 SourceLocation RParenLoc) {
3461 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00003462}
Douglas Gregor15de72c2011-12-02 23:23:56 +00003463
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003464FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3465 unsigned ID) {
3466 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3467 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3468}
3469
Michael Han684aa732013-02-22 17:15:32 +00003470void EmptyDecl::anchor() {}
3471
3472EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3473 return new (C) EmptyDecl(DC, L);
3474}
3475
3476EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3477 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EmptyDecl));
3478 return new (Mem) EmptyDecl(0, SourceLocation());
3479}
3480
Douglas Gregor15de72c2011-12-02 23:23:56 +00003481//===----------------------------------------------------------------------===//
3482// ImportDecl Implementation
3483//===----------------------------------------------------------------------===//
3484
3485/// \brief Retrieve the number of module identifiers needed to name the given
3486/// module.
3487static unsigned getNumModuleIdentifiers(Module *Mod) {
3488 unsigned Result = 1;
3489 while (Mod->Parent) {
3490 Mod = Mod->Parent;
3491 ++Result;
3492 }
3493 return Result;
3494}
3495
Douglas Gregor5948ae12012-01-03 18:04:46 +00003496ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003497 Module *Imported,
3498 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003499 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregore6649772011-12-03 00:30:27 +00003500 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003501{
3502 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3503 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3504 memcpy(StoredLocs, IdentifierLocs.data(),
3505 IdentifierLocs.size() * sizeof(SourceLocation));
3506}
3507
Douglas Gregor5948ae12012-01-03 18:04:46 +00003508ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003509 Module *Imported, SourceLocation EndLoc)
Douglas Gregor5948ae12012-01-03 18:04:46 +00003510 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregore6649772011-12-03 00:30:27 +00003511 NextLocalImport()
Douglas Gregor15de72c2011-12-02 23:23:56 +00003512{
3513 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3514}
3515
3516ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003517 SourceLocation StartLoc, Module *Imported,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003518 ArrayRef<SourceLocation> IdentifierLocs) {
3519 void *Mem = C.Allocate(sizeof(ImportDecl) +
3520 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003521 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003522}
3523
3524ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor5948ae12012-01-03 18:04:46 +00003525 SourceLocation StartLoc,
Douglas Gregor15de72c2011-12-02 23:23:56 +00003526 Module *Imported,
3527 SourceLocation EndLoc) {
3528 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor5948ae12012-01-03 18:04:46 +00003529 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregor15de72c2011-12-02 23:23:56 +00003530 Import->setImplicit();
3531 return Import;
3532}
3533
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00003534ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3535 unsigned NumLocations) {
3536 void *Mem = AllocateDeserializedDecl(C, ID,
3537 (sizeof(ImportDecl) +
3538 NumLocations * sizeof(SourceLocation)));
Douglas Gregor15de72c2011-12-02 23:23:56 +00003539 return new (Mem) ImportDecl(EmptyShell());
3540}
3541
3542ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3543 if (!ImportedAndComplete.getInt())
Dmitri Gribenko55431692013-05-05 00:41:58 +00003544 return None;
Douglas Gregor15de72c2011-12-02 23:23:56 +00003545
3546 const SourceLocation *StoredLocs
3547 = reinterpret_cast<const SourceLocation *>(this + 1);
3548 return ArrayRef<SourceLocation>(StoredLocs,
3549 getNumModuleIdentifiers(getImportedModule()));
3550}
3551
3552SourceRange ImportDecl::getSourceRange() const {
3553 if (!ImportedAndComplete.getInt())
3554 return SourceRange(getLocation(),
3555 *reinterpret_cast<const SourceLocation *>(this + 1));
3556
3557 return SourceRange(getLocation(), getIdentifierLocs().back());
3558}