blob: 80a32c5870db3d769df2766056e65f9463688d3f [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000024#include "clang/AST/Stmt.h"
25#include "clang/AST/TypeLoc.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000027#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000028#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000029#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000030#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000031#include "llvm/Support/ErrorHandling.h"
John McCall5f46c482013-02-21 23:42:58 +000032#include "llvm/Support/type_traits.h"
David Blaikie9c70e042011-09-21 18:16:56 +000033#include <algorithm>
34
Chris Lattner6d9a6852006-10-25 05:11:20 +000035using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000036
Chris Lattner88f70d62008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
John McCalldf25c432013-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 McCalld041a9b2013-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 McCalldf25c432013-02-16 00:17:33 +000086
John McCall5f46c482013-02-21 23:42:58 +000087const unsigned IgnoreExplicitVisibilityBit = 2;
Rafael Espindola9551d3b2013-05-28 19:43:11 +000088const unsigned IgnoreAllVisibilityBit = 4;
John McCall5f46c482013-02-21 23:42:58 +000089
John McCalldf25c432013-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 McCall5f46c482013-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 McCalld041a9b2013-02-20 01:54:26 +000097 LVForType = NamedDecl::VisibilityForType,
John McCalldf25c432013-02-16 00:17:33 +000098
John McCall5f46c482013-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 McCalld041a9b2013-02-20 01:54:26 +0000102 LVForValue = NamedDecl::VisibilityForValue,
103
John McCall5f46c482013-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 McCalld041a9b2013-02-20 01:54:26 +0000108
John McCall5f46c482013-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 Espindola9551d3b2013-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 McCalldf25c432013-02-16 00:17:33 +0000117};
118
John McCalld041a9b2013-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 McCall5f46c482013-02-21 23:42:58 +0000122 return ((unsigned(computation) & IgnoreExplicitVisibilityBit) != 0);
John McCalld041a9b2013-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 McCall5f46c482013-02-21 23:42:58 +0000130 static_cast<LVComputationKind>(unsigned(oldKind) |
131 IgnoreExplicitVisibilityBit);
John McCalld041a9b2013-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 Blaikie05785d12013-02-20 22:23:23 +0000139static Optional<Visibility> getExplicitVisibility(const NamedDecl *D,
140 LVComputationKind kind) {
John McCalld041a9b2013-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 McCalldf25c432013-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 McCallb4a99d32013-02-19 01:57:35 +0000149 return isa<TypeDecl>(D) ||
150 isa<ClassTemplateDecl>(D) ||
151 isa<ObjCInterfaceDecl>(D);
John McCalldf25c432013-02-16 00:17:33 +0000152}
153
John McCall5f46c482013-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 McCalld041a9b2013-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 McCalldf25c432013-02-16 00:17:33 +0000189/// Return the explicit visibility of the given declaration.
David Blaikie05785d12013-02-20 22:23:23 +0000190static Optional<Visibility> getVisibilityOf(const NamedDecl *D,
John McCalld041a9b2013-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 Gregor1baf38f2011-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 McCalld041a9b2013-02-20 01:54:26 +0000202 return getVisibilityFromAttr(A);
John McCall457a04e2010-10-22 21:05:15 +0000203 }
Douglas Gregor1baf38f2011-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 Gregore8bbc122011-09-02 00:18:52 +0000207 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-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 Blaikie7a30dc52013-02-21 01:47:18 +0000216 return None;
John McCall457a04e2010-10-22 21:05:15 +0000217}
218
Rafael Espindolaecf63c62013-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 Gregor7dc5c172010-02-03 09:33:45 +0000226/// \brief Get the most restrictive linkage for the types in the given
John McCalldf25c432013-02-16 00:17:33 +0000227/// template parameter list. For visibility purposes, template
228/// parameters are part of the signature of a template.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000229static LinkageInfo
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000230getLVForTemplateParameterList(const TemplateParameterList *params,
231 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000232 LinkageInfo LV;
233 for (TemplateParameterList::const_iterator P = params->begin(),
234 PEnd = params->end();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000235 P != PEnd; ++P) {
John McCalldf25c432013-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 Gregor0231d8d2011-01-19 20:10:05 +0000246 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
John McCalldf25c432013-02-16 00:17:33 +0000247 // Handle the non-pack case first.
248 if (!NTTP->isExpandedParameterPack()) {
249 if (!NTTP->getType()->isDependentType()) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000250 LV.merge(getLVForType(*NTTP->getType(), computation));
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000251 }
252 continue;
253 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000254
John McCalldf25c432013-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 Espindola6fbfafe2013-02-27 02:27:19 +0000259 LV.merge(type->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000260 }
John McCalldf25c432013-02-16 00:17:33 +0000261 continue;
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000262 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000263
John McCalldf25c432013-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 Espindolaecf63c62013-05-29 04:55:30 +0000270 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(),
271 computation));
John McCalldf25c432013-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 Espindolaecf63c62013-05-29 04:55:30 +0000279 TTP->getExpansionTemplateParameters(i), computation));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000280 }
281 }
282
John McCall457a04e2010-10-22 21:05:15 +0000283 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000284}
285
Rafael Espindola19de5612013-01-12 06:42:30 +0000286/// getLVForDecl - Get the linkage and visibility for the given declaration.
John McCalldf25c432013-02-16 00:17:33 +0000287static LinkageInfo getLVForDecl(const NamedDecl *D,
288 LVComputationKind computation);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000289
Rafael Espindolac1b38a22013-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 Gregor7dc5c172010-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 McCalldf25c432013-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 Espindolaecf63c62013-05-29 04:55:30 +0000308getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args,
309 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000310 LinkageInfo LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000311
John McCalldf25c432013-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 Gregor7dc5c172010-02-03 09:33:45 +0000315 case TemplateArgument::Null:
316 case TemplateArgument::Integral:
317 case TemplateArgument::Expression:
John McCalldf25c432013-02-16 00:17:33 +0000318 continue;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000319
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000320 case TemplateArgument::Type:
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000321 LV.merge(getLVForType(*arg.getAsType(), computation));
John McCalldf25c432013-02-16 00:17:33 +0000322 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000323
324 case TemplateArgument::Declaration:
John McCalldf25c432013-02-16 00:17:33 +0000325 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
326 assert(!usesTypeVisibility(ND));
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000327 LV.merge(getLVForDecl(ND, computation));
John McCalldf25c432013-02-16 00:17:33 +0000328 }
329 continue;
Eli Friedmanb826a002012-09-26 02:36:12 +0000330
331 case TemplateArgument::NullPtr:
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000332 LV.merge(arg.getNullPtrType()->getLinkageAndVisibility());
John McCalldf25c432013-02-16 00:17:33 +0000333 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000334
335 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000336 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000337 if (TemplateDecl *Template
John McCalldf25c432013-02-16 00:17:33 +0000338 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000339 LV.merge(getLVForDecl(Template, computation));
John McCalldf25c432013-02-16 00:17:33 +0000340 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000341
342 case TemplateArgument::Pack:
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000343 LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray(), computation));
John McCalldf25c432013-02-16 00:17:33 +0000344 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000345 }
John McCalldf25c432013-02-16 00:17:33 +0000346 llvm_unreachable("bad template argument kind");
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000347 }
348
John McCall457a04e2010-10-22 21:05:15 +0000349 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000350}
351
Rafael Espindola2f869a32012-01-14 00:30:36 +0000352static LinkageInfo
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000353getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
354 LVComputationKind computation) {
355 return getLVForTemplateArgumentList(TArgs.asArray(), computation);
John McCall8823c652010-08-13 08:35:10 +0000356}
357
John McCall5f46c482013-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 McCalldf25c432013-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 McCall5f46c482013-02-21 23:42:58 +0000375///
NAKAMURA Takumi62eae082013-02-22 04:06:28 +0000376/// \param[out] LV the computation to use for the parent
John McCall5f46c482013-02-21 23:42:58 +0000377static void
378mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000379 const FunctionTemplateSpecializationInfo *specInfo,
380 LVComputationKind computation) {
John McCall5f46c482013-02-21 23:42:58 +0000381 bool considerVisibility =
382 shouldConsiderTemplateVisibility(fn, specInfo);
John McCalldf25c432013-02-16 00:17:33 +0000383
384 // Merge information from the template parameters.
John McCall5f46c482013-02-21 23:42:58 +0000385 FunctionTemplateDecl *temp = specInfo->getTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000386 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000387 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-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 Espindolaecf63c62013-05-29 04:55:30 +0000392 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
John McCalldf25c432013-02-16 00:17:33 +0000393 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000394}
395
John McCall5f46c482013-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 Espindola9551d3b2013-05-28 19:43:11 +0000411 case LVForLinkageOnly:
412 return false;
John McCall5f46c482013-02-21 23:42:58 +0000413 }
414 llvm_unreachable("bad visibility computation kind");
415}
416
John McCalld041a9b2013-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 McCalldf25c432013-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 McCalld041a9b2013-02-20 01:54:26 +0000428 // for an explicit specialization when computing the visibility of a
429 // member thereof with explicit visibility.
John McCalldf25c432013-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 McCalld041a9b2013-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 McCall5f46c482013-02-21 23:42:58 +0000450 return !hasDirectVisibilityAttribute(spec, computation);
John McCalld041a9b2013-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 McCalldf25c432013-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 McCalld041a9b2013-02-20 01:54:26 +0000463 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000464 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000465 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-02-16 00:17:33 +0000466 LV.mergeMaybeWithVisibility(tempLV,
John McCalld041a9b2013-02-20 01:54:26 +0000467 considerVisibility && !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-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 Espindolaecf63c62013-05-29 04:55:30 +0000473 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
Rafael Espindola503276b2013-05-30 21:23:15 +0000474 if (considerVisibility)
475 LV.mergeVisibility(argsLV);
476 LV.mergeExternalVisibility(argsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000477}
478
Rafael Espindolac7c7ad52012-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 Espindola5cc78902012-07-13 23:26:43 +0000481 const LangOptions &Opts = D->getASTContext().getLangOpts();
482 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-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 Espindolafb9d4b42012-10-11 16:32:25 +0000504 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000505}
506
Rafael Espindola593537a2013-05-05 20:15:21 +0000507template <typename T> static bool isFirstInExternCContext(T *D) {
Rafael Espindolaf4187652013-02-14 01:18:37 +0000508 const T *First = D->getFirstDeclaration();
Rafael Espindola593537a2013-05-05 20:15:21 +0000509 return First->isInExternCContext();
Rafael Espindolaf4187652013-02-14 01:18:37 +0000510}
511
Rafael Espindola327be3c2013-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 Espindola46cb6f12012-04-21 23:28:21 +0000519static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCalldf25c432013-02-16 00:17:33 +0000520 LVComputationKind computation) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000521 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-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 McCall8e7d6562010-08-26 03:08:43 +0000533 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000534 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000535
Richard Smithdc0ef452012-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000539 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000540 Var->getType().isConstQualified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000541 !Var->getType().isVolatileQualified()) {
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000542 const VarDecl *PrevVar = Var->getPreviousDecl();
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000543 if (PrevVar)
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000544 return getLVForDecl(PrevVar, computation);
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000545
546 if (Var->getStorageClass() != SC_Extern &&
Rafael Espindola327be3c2013-04-26 01:30:23 +0000547 Var->getStorageClass() != SC_PrivateExtern &&
548 !isSingleLineExternC(*Var))
Rafael Espindola6ae7e502013-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 Jahanian8feee2d2011-06-16 20:14:50 +0000560 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000561 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-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 Gregorf73b2822009-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 Espindola6ae7e502013-04-03 19:27:57 +0000573 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000574 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-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 McCallc273f242010-10-30 11:50:40 +0000578 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000579 }
580
Chandler Carruth9682a2fd2011-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 Espindola593537a2013-05-05 20:15:21 +0000584 if ((!Var || !isFirstInExternCContext(Var)) &&
585 (!Func || !isFirstInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000586 return LinkageInfo::uniqueExternal();
587 }
John McCallb7139c42010-10-28 04:18:25 +0000588
John McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +0000595 LinkageInfo LV;
596
John McCalld041a9b2013-02-20 01:54:26 +0000597 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000598 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000599 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000600 } else {
601 // If we're declared in a namespace with a visibility attribute,
John McCalldf25c432013-02-16 00:17:33 +0000602 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindola78158af2012-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 Blaikie05785d12013-02-20 22:23:23 +0000608 if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000609 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000610 break;
611 }
612 }
613 }
Rafael Espindola78158af2012-04-16 18:46:26 +0000614
John McCalldf25c432013-02-16 00:17:33 +0000615 // Add in global settings if the above didn't give us direct visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000616 if (!LV.isVisibilityExplicit()) {
John McCallb4a99d32013-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 McCalldf25c432013-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 Espindolac7c7ad52012-07-13 14:25:36 +0000632 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000633
Douglas Gregorf73b2822009-11-25 22:24:25 +0000634 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000635
Douglas Gregorf73b2822009-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 McCall37bb6c92010-10-29 22:22:43 +0000641 // GCC applies the following optimization to variables and static
642 // data members, but not to functions:
643 //
John McCall457a04e2010-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 McCall5fe84122010-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 McCall457a04e2010-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 Espindola593537a2013-05-05 20:15:21 +0000662 if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000663 LinkageInfo TypeLV = getLVForType(*Var->getType(), computation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000664 if (TypeLV.getLinkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000665 return LinkageInfo::uniqueExternal();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000666 if (!LV.isVisibilityExplicit())
John McCalldf25c432013-02-16 00:17:33 +0000667 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000668 }
669
John McCall23032652010-11-02 18:38:13 +0000670 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000671 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000672
Rafael Espindolad5ed0332012-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 Gregorf73b2822009-11-25 22:24:25 +0000676
Douglas Gregorf73b2822009-11-25 22:24:25 +0000677 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000678 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-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 McCall457a04e2010-10-22 21:05:15 +0000683
John McCall23032652010-11-02 18:38:13 +0000684 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000685 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000686
Rafael Espindolaa508c5d2012-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 Gregorf73b2822009-11-25 22:24:25 +0000690
John McCallf768aa72011-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000695 if (Context.getLangOpts().CPlusPlus &&
Richard Smith50f4afc2013-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 McCallf768aa72011-02-10 06:50:24 +0000710
John McCall5f46c482013-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 McCallb8c604a2011-06-27 23:06:04 +0000714 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000715 = Function->getTemplateSpecializationInfo()) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000716 mergeTemplateLV(LV, Function, specInfo, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000717 }
718
Douglas Gregorf73b2822009-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 McCall457a04e2010-10-22 21:05:15 +0000725 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
726 // Unnamed tags have no linkage.
John McCall5ea95772013-03-09 00:54:27 +0000727 if (!Tag->hasNameForLinkage())
John McCallc273f242010-10-30 11:50:40 +0000728 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000729
John McCall457a04e2010-10-22 21:05:15 +0000730 // If this is a class template specialization, consider the
John McCall5f46c482013-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 McCallb8c604a2011-06-27 23:06:04 +0000733 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000734 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCalldf25c432013-02-16 00:17:33 +0000735 mergeTemplateLV(LV, spec, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000736 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000737
738 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000739 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000740 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCalldf25c432013-02-16 00:17:33 +0000741 computation);
Rafael Espindolab97e8962013-05-27 14:14:42 +0000742 if (!isExternalFormalLinkage(EnumLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000743 return LinkageInfo::none();
744 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000745
746 // - a template, unless it is a function template that has
747 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000748 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
John McCalld041a9b2013-02-20 01:54:26 +0000749 bool considerVisibility = !hasExplicitVisibilityAlready(computation);
John McCalldf25c432013-02-16 00:17:33 +0000750 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000751 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-02-16 00:17:33 +0000752 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
753
Douglas Gregorf73b2822009-11-25 22:24:25 +0000754 // - a namespace (7.3), unless it is declared within an unnamed
755 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000756 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
757 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000758
John McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +0000766 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000767 }
768
769 // If we ended up with non-external linkage, visibility should
770 // always be default.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000771 if (LV.getLinkage() != ExternalLinkage)
772 return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000773
John McCall457a04e2010-10-22 21:05:15 +0000774 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000775}
776
John McCalldf25c432013-02-16 00:17:33 +0000777static LinkageInfo getLVForClassMember(const NamedDecl *D,
778 LVComputationKind computation) {
John McCall457a04e2010-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 McCall8823c652010-08-13 08:35:10 +0000783 if (!(isa<CXXMethodDecl>(D) ||
784 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000785 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000786 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000787 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000788
John McCall07072662010-11-02 01:45:15 +0000789 LinkageInfo LV;
790
John McCall07072662010-11-02 01:45:15 +0000791 // If we have an explicit visibility attribute, merge that in.
John McCalld041a9b2013-02-20 01:54:26 +0000792 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000793 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000794 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-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 Espindola4a5da442013-02-27 02:56:45 +0000800 if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000801 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000802 }
Rafael Espindola53cf2192012-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 Ledru830885c2012-07-23 08:59:39 +0000806 // only look for them when processing the class.
John McCalld041a9b2013-02-20 01:54:26 +0000807 LVComputationKind classComputation = computation;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000808 if (LV.isVisibilityExplicit())
John McCalld041a9b2013-02-20 01:54:26 +0000809 classComputation = withExplicitVisibilityAlready(computation);
Rafael Espindola505a7c82012-04-16 18:25:01 +0000810
John McCall5f46c482013-02-21 23:42:58 +0000811 LinkageInfo classLV =
812 getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
John McCall8823c652010-08-13 08:35:10 +0000813 // If the class already has unique-external linkage, we can't improve.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000814 if (classLV.getLinkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000815 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000816
Rafael Espindolae6190db2013-05-28 02:22:10 +0000817 if (!isExternallyVisible(classLV.getLinkage()))
818 return LinkageInfo::none();
819
820
John McCall5f46c482013-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 McCall8823c652010-08-13 08:35:10 +0000827 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-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 McCall457a04e2010-10-22 21:05:15 +0000833 // If this is a method template specialization, use the linkage for
834 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000835 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000836 = MD->getTemplateSpecializationInfo()) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000837 mergeTemplateLV(LV, MD, spec, computation);
John McCall5f46c482013-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 McCalle6e622e2010-11-01 01:29:57 +0000845 }
John McCall457a04e2010-10-22 21:05:15 +0000846
John McCall37bb6c92010-10-29 22:22:43 +0000847 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000848 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000849 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCalldf25c432013-02-16 00:17:33 +0000850 mergeTemplateLV(LV, spec, computation);
John McCall5f46c482013-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 McCall37bb6c92010-10-29 22:22:43 +0000861 }
862
John McCall37bb6c92010-10-29 22:22:43 +0000863 // Static data members.
864 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-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 Espindolaecf63c62013-05-29 04:55:30 +0000867 LinkageInfo typeLV = getLVForType(*VD->getType(), computation);
Rafael Espindola503276b2013-05-30 21:23:15 +0000868 if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit())
869 LV.mergeVisibility(typeLV);
870 LV.mergeExternalVisibility(typeLV);
John McCall5f46c482013-02-21 23:42:58 +0000871
872 if (isExplicitMemberSpecialization(VD)) {
873 explicitSpecSuppressor = VD;
874 }
John McCalldf25c432013-02-16 00:17:33 +0000875
876 // Template members.
877 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
878 bool considerVisibility =
Rafael Espindola4a5da442013-02-27 02:56:45 +0000879 (!LV.isVisibilityExplicit() &&
880 !classLV.isVisibilityExplicit() &&
John McCalld041a9b2013-02-20 01:54:26 +0000881 !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000882 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000883 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-02-16 00:17:33 +0000884 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall5f46c482013-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 McCall37bb6c92010-10-29 22:22:43 +0000892 }
893
John McCall5f46c482013-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 Espindola4a5da442013-02-27 02:56:45 +0000901 // optimization: hasDVA() is true only with explicit visibility.
902 LV.isVisibilityExplicit() &&
903 classLV.getVisibility() != DefaultVisibility &&
John McCall5f46c482013-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 McCall457a04e2010-10-22 21:05:15 +0000910 return LV;
John McCall8823c652010-08-13 08:35:10 +0000911}
912
David Blaikie68e081d2011-12-20 02:48:34 +0000913void NamedDecl::anchor() { }
914
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000915static LinkageInfo computeLVForDecl(const NamedDecl *D,
916 LVComputationKind computation);
917
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000918bool NamedDecl::isLinkageValid() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +0000919 if (!hasCachedLinkage())
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000920 return true;
John McCalld396b972011-02-08 19:01:05 +0000921
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000922 return computeLVForDecl(this, LVForLinkageOnly).getLinkage() ==
Rafael Espindola50df3a02013-05-25 17:16:20 +0000923 getCachedLinkage();
John McCalld396b972011-02-08 19:01:05 +0000924}
925
Rafael Espindola3ae00052013-05-13 00:12:11 +0000926Linkage NamedDecl::getLinkageInternal() const {
John McCalld041a9b2013-02-20 01:54:26 +0000927 // We don't care about visibility here, so ask for the cheapest
928 // possible visibility analysis.
Rafael Espindola9551d3b2013-05-28 19:43:11 +0000929 return getLVForDecl(this, LVForLinkageOnly).getLinkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000930}
931
John McCallc273f242010-10-30 11:50:40 +0000932LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCalldf25c432013-02-16 00:17:33 +0000933 LVComputationKind computation =
934 (usesTypeVisibility(this) ? LVForType : LVForValue);
Rafael Espindola9551d3b2013-05-28 19:43:11 +0000935 return getLVForDecl(this, computation);
John McCall033caa52010-10-29 00:29:13 +0000936}
Ted Kremenek926d8602010-04-20 23:15:35 +0000937
David Blaikie05785d12013-02-20 22:23:23 +0000938Optional<Visibility>
John McCalld041a9b2013-02-20 01:54:26 +0000939NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
Rafael Espindola3a52c442013-02-26 19:33:14 +0000940 // Check the declaration itself first.
941 if (Optional<Visibility> V = getVisibilityOf(this, kind))
942 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000943
Rafael Espindola3a52c442013-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 Espindola96e68242012-05-16 02:10:38 +0000966 if (Var->isStaticDataMember()) {
967 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
968 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +0000969 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola96e68242012-05-16 02:10:38 +0000970 }
971
David Blaikie7a30dc52013-02-21 01:47:18 +0000972 return None;
Rafael Espindola96e68242012-05-16 02:10:38 +0000973 }
Rafael Espindola3a52c442013-02-26 19:33:14 +0000974 // Also handle function template specializations.
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000975 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor1baf38f2011-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 McCalld041a9b2013-02-20 01:54:26 +0000980 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
981 kind);
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000982
Rafael Espindola8093fdf2012-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 McCalld041a9b2013-02-20 01:54:26 +0000987 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000988
David Blaikie7a30dc52013-02-21 01:47:18 +0000989 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000990 }
991
Rafael Espindolafb4263f2012-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 McCalld041a9b2013-02-20 01:54:26 +0000994 return getVisibilityOf(TD->getTemplatedDecl(), kind);
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000995
David Blaikie7a30dc52013-02-21 01:47:18 +0000996 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000997}
998
John McCalldf25c432013-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 Espindola593537a2013-05-05 20:15:21 +00001003 !Function->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001004 return LinkageInfo::uniqueExternal();
1005
1006 // This is a "void f();" which got merged with a file static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001007 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCalldf25c432013-02-16 00:17:33 +00001008 return LinkageInfo::internal();
1009
1010 LinkageInfo LV;
John McCalld041a9b2013-02-20 01:54:26 +00001011 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001012 if (Optional<Visibility> Vis =
1013 getExplicitVisibility(Function, computation))
John McCalldf25c432013-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 Espindola6ae7e502013-04-03 19:27:57 +00001025 if (Var->hasExternalStorage()) {
Rafael Espindola593537a2013-05-05 20:15:21 +00001026 if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001027 return LinkageInfo::uniqueExternal();
1028
John McCalldf25c432013-02-16 00:17:33 +00001029 LinkageInfo LV;
1030 if (Var->getStorageClass() == SC_PrivateExtern)
1031 LV.mergeVisibility(HiddenVisibility, true);
John McCalld041a9b2013-02-20 01:54:26 +00001032 else if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001033 if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
John McCalldf25c432013-02-16 00:17:33 +00001034 LV.mergeVisibility(*Vis, true);
1035 }
1036
Rafael Espindola6ae7e502013-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 McCalldf25c432013-02-16 00:17:33 +00001044 return LV;
1045 }
1046 }
1047
Rafael Espindola50df3a02013-05-25 17:16:20 +00001048 if (!isa<TagDecl>(D))
1049 return LinkageInfo::none();
1050
1051 const FunctionDecl *FD = getOutermostFunctionContext(D);
1052 if (!FD || !FD->isInlined())
1053 return LinkageInfo::none();
Rafael Espindola692177e2013-05-28 02:13:28 +00001054 LinkageInfo LV = getLVForDecl(FD, computation);
Rafael Espindola111bb2e2013-05-27 14:50:21 +00001055 if (!isExternallyVisible(LV.getLinkage()))
Rafael Espindola50df3a02013-05-25 17:16:20 +00001056 return LinkageInfo::none();
1057 return LinkageInfo(VisibleNoLinkage, LV.getVisibility(),
1058 LV.isVisibilityExplicit());
John McCalldf25c432013-02-16 00:17:33 +00001059}
1060
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001061static LinkageInfo computeLVForDecl(const NamedDecl *D,
1062 LVComputationKind computation) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001063 // Objective-C: treat all Objective-C declarations as having external
1064 // linkage.
John McCall033caa52010-10-29 00:29:13 +00001065 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001066 default:
1067 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +00001068 case Decl::ParmVar:
1069 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001070 case Decl::TemplateTemplateParm: // count these as external
1071 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +00001072 case Decl::ObjCAtDefsField:
1073 case Decl::ObjCCategory:
1074 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +00001075 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +00001076 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +00001077 case Decl::ObjCMethod:
1078 case Decl::ObjCProperty:
1079 case Decl::ObjCPropertyImpl:
1080 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +00001081 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001082
1083 case Decl::CXXRecord: {
1084 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
1085 if (Record->isLambda()) {
1086 if (!Record->getLambdaManglingNumber()) {
1087 // This lambda has no mangling number, so it's internal.
1088 return LinkageInfo::internal();
1089 }
1090
1091 // This lambda has its linkage/visibility determined by its owner.
1092 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
1093 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
1094 if (isa<ParmVarDecl>(ContextDecl))
1095 DC = ContextDecl->getDeclContext()->getRedeclContext();
1096 else
John McCalldf25c432013-02-16 00:17:33 +00001097 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001098 }
1099
1100 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
John McCalldf25c432013-02-16 00:17:33 +00001101 return getLVForDecl(ND, computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001102
1103 return LinkageInfo::external();
1104 }
1105
1106 break;
1107 }
Ted Kremenek926d8602010-04-20 23:15:35 +00001108 }
1109
Douglas Gregorf73b2822009-11-25 22:24:25 +00001110 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +00001111 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCalldf25c432013-02-16 00:17:33 +00001112 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001113
1114 // C++ [basic.link]p5:
1115 // In addition, a member function, static data member, a named
1116 // class or enumeration of class scope, or an unnamed class or
1117 // enumeration defined in a class-scope typedef declaration such
1118 // that the class or enumeration has the typedef name for linkage
1119 // purposes (7.1.3), has external linkage if the name of the class
1120 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +00001121 if (D->getDeclContext()->isRecord())
John McCalldf25c432013-02-16 00:17:33 +00001122 return getLVForClassMember(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001123
1124 // C++ [basic.link]p6:
1125 // The name of a function declared in block scope and the name of
1126 // an object declared by a block scope extern declaration have
1127 // linkage. If there is a visible declaration of an entity with
1128 // linkage having the same name and type, ignoring entities
1129 // declared outside the innermost enclosing namespace scope, the
1130 // block scope declaration declares that same entity and receives
1131 // the linkage of the previous declaration. If there is more than
1132 // one such matching entity, the program is ill-formed. Otherwise,
1133 // if no matching entity is found, the block scope entity receives
1134 // external linkage.
John McCalldf25c432013-02-16 00:17:33 +00001135 if (D->getDeclContext()->isFunctionOrMethod())
1136 return getLVForLocalDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001137
1138 // C++ [basic.link]p6:
1139 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +00001140 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001141}
Douglas Gregorf73b2822009-11-25 22:24:25 +00001142
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001143namespace clang {
1144class LinkageComputer {
1145public:
1146 static LinkageInfo getLVForDecl(const NamedDecl *D,
1147 LVComputationKind computation) {
1148 if (computation == LVForLinkageOnly && D->hasCachedLinkage())
1149 return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false);
1150
1151 LinkageInfo LV = computeLVForDecl(D, computation);
1152 if (D->hasCachedLinkage())
1153 assert(D->getCachedLinkage() == LV.getLinkage());
1154
1155 D->setCachedLinkage(LV.getLinkage());
1156
1157#ifndef NDEBUG
1158 // In C (because of gnu inline) and in c++ with microsoft extensions an
1159 // static can follow an extern, so we can have two decls with different
1160 // linkages.
1161 const LangOptions &Opts = D->getASTContext().getLangOpts();
1162 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
1163 return LV;
1164
1165 // We have just computed the linkage for this decl. By induction we know
1166 // that all other computed linkages match, check that the one we just
1167 // computed
1168 // also does.
1169 NamedDecl *Old = NULL;
1170 for (NamedDecl::redecl_iterator I = D->redecls_begin(),
1171 E = D->redecls_end();
1172 I != E; ++I) {
1173 NamedDecl *T = cast<NamedDecl>(*I);
1174 if (T == D)
1175 continue;
1176 if (T->hasCachedLinkage()) {
1177 Old = T;
1178 break;
1179 }
1180 }
1181 assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage());
1182#endif
1183
1184 return LV;
1185 }
1186};
1187}
1188
1189static LinkageInfo getLVForDecl(const NamedDecl *D,
1190 LVComputationKind computation) {
1191 return clang::LinkageComputer::getLVForDecl(D, computation);
1192}
1193
Douglas Gregor2ada0482009-02-04 17:27:36 +00001194std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +00001195 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +00001196}
1197
1198std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001199 std::string QualName;
1200 llvm::raw_string_ostream OS(QualName);
1201 printQualifiedName(OS, P);
1202 return OS.str();
1203}
1204
1205void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1206 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1207}
1208
1209void NamedDecl::printQualifiedName(raw_ostream &OS,
1210 const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +00001211 const DeclContext *Ctx = getDeclContext();
1212
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001213 if (Ctx->isFunctionOrMethod()) {
1214 printName(OS);
1215 return;
1216 }
Douglas Gregor2ada0482009-02-04 17:27:36 +00001217
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001218 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001219 ContextsTy Contexts;
1220
1221 // Collect contexts.
1222 while (Ctx && isa<NamedDecl>(Ctx)) {
1223 Contexts.push_back(Ctx);
1224 Ctx = Ctx->getParent();
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001225 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001226
1227 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1228 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001229 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001230 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Benjamin Kramer9170e912013-02-22 15:46:01 +00001231 OS << Spec->getName();
Douglas Gregor85673582009-05-18 17:01:57 +00001232 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001233 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1234 TemplateArgs.data(),
1235 TemplateArgs.size(),
1236 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001237 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +00001238 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001239 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +00001240 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001241 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001242 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1243 if (!RD->getIdentifier())
1244 OS << "<anonymous " << RD->getKindName() << '>';
1245 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001246 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001247 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +00001248 const FunctionProtoType *FT = 0;
1249 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001250 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001251
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001252 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001253 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001254 unsigned NumParams = FD->getNumParams();
1255 for (unsigned i = 0; i < NumParams; ++i) {
1256 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001257 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001258 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001259 }
1260
1261 if (FT->isVariadic()) {
1262 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001263 OS << ", ";
1264 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001265 }
1266 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001267 OS << ')';
1268 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001269 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001270 }
1271 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001272 }
1273
John McCalla2a3f7d2010-03-16 21:48:18 +00001274 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001275 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001276 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001277 OS << "<anonymous>";
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001278}
Douglas Gregor2ada0482009-02-04 17:27:36 +00001279
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001280void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1281 const PrintingPolicy &Policy,
1282 bool Qualified) const {
1283 if (Qualified)
1284 printQualifiedName(OS, Policy);
1285 else
1286 printName(OS);
Douglas Gregor2ada0482009-02-04 17:27:36 +00001287}
1288
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001289bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001290 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1291
Douglas Gregor889ceb72009-02-03 19:21:40 +00001292 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1293 // We want to keep it, unless it nominates same namespace.
1294 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +00001295 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1296 ->getOriginalNamespace() ==
1297 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1298 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +00001299 }
Mike Stump11289f42009-09-09 15:08:12 +00001300
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001301 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1302 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +00001303 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001304
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001305 // For function templates, the underlying function declarations are linked.
1306 if (const FunctionTemplateDecl *FunctionTemplate
1307 = dyn_cast<FunctionTemplateDecl>(this))
1308 if (const FunctionTemplateDecl *OldFunctionTemplate
1309 = dyn_cast<FunctionTemplateDecl>(OldD))
1310 return FunctionTemplate->getTemplatedDecl()
1311 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001312
Steve Naroffc4173fa2009-02-22 19:35:57 +00001313 // For method declarations, we keep track of redeclarations.
1314 if (isa<ObjCMethodDecl>(this))
1315 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001316
John McCall9f3059a2009-10-09 21:13:30 +00001317 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1318 return true;
1319
John McCall3f746822009-11-17 05:59:44 +00001320 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1321 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1322 cast<UsingShadowDecl>(OldD)->getTargetDecl();
1323
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001324 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1325 ASTContext &Context = getASTContext();
1326 return Context.getCanonicalNestedNameSpecifier(
1327 cast<UsingDecl>(this)->getQualifier()) ==
1328 Context.getCanonicalNestedNameSpecifier(
1329 cast<UsingDecl>(OldD)->getQualifier());
1330 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +00001331
Douglas Gregorb59643b2012-01-03 23:26:26 +00001332 // A typedef of an Objective-C class type can replace an Objective-C class
1333 // declaration or definition, and vice versa.
1334 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1335 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1336 return true;
1337
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001338 // For non-function declarations, if the declarations are of the
1339 // same kind then this must be a redeclaration, or semantic analysis
1340 // would not have given us the new declaration.
1341 return this->getKind() == OldD->getKind();
1342}
1343
Douglas Gregoreddf4332009-02-24 20:03:32 +00001344bool NamedDecl::hasLinkage() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +00001345 return getFormalLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001346}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001347
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001348NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001349 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001350 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1351 ND = UD->getTargetDecl();
1352
1353 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1354 return AD->getClassInterface();
1355
1356 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001357}
1358
John McCalla8ae2222010-04-06 21:38:20 +00001359bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001360 if (!isCXXClassMember())
1361 return false;
1362
John McCalla8ae2222010-04-06 21:38:20 +00001363 const NamedDecl *D = this;
1364 if (isa<UsingShadowDecl>(D))
1365 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1366
John McCall5e77d762013-04-16 07:28:30 +00001367 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001368 return true;
1369 if (isa<CXXMethodDecl>(D))
1370 return cast<CXXMethodDecl>(D)->isInstance();
1371 if (isa<FunctionTemplateDecl>(D))
1372 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1373 ->getTemplatedDecl())->isInstance();
1374 return false;
1375}
1376
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001377//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001378// DeclaratorDecl Implementation
1379//===----------------------------------------------------------------------===//
1380
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001381template <typename DeclT>
1382static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1383 if (decl->getNumTemplateParameterLists() > 0)
1384 return decl->getTemplateParameterList(0)->getTemplateLoc();
1385 else
1386 return decl->getInnerLocStart();
1387}
1388
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001389SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001390 TypeSourceInfo *TSI = getTypeSourceInfo();
1391 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001392 return SourceLocation();
1393}
1394
Douglas Gregor14454802011-02-25 02:25:35 +00001395void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1396 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001397 // Make sure the extended decl info is allocated.
1398 if (!hasExtInfo()) {
1399 // Save (non-extended) type source info pointer.
1400 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1401 // Allocate external info struct.
1402 DeclInfo = new (getASTContext()) ExtInfo;
1403 // Restore savedTInfo into (extended) decl info.
1404 getExtInfo()->TInfo = savedTInfo;
1405 }
1406 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001407 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001408 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001409 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001410 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001411 if (getExtInfo()->NumTemplParamLists == 0) {
1412 // Save type source info pointer.
1413 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1414 // Deallocate the extended decl info.
1415 getASTContext().Deallocate(getExtInfo());
1416 // Restore savedTInfo into (non-extended) decl info.
1417 DeclInfo = savedTInfo;
1418 }
1419 else
1420 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001421 }
1422 }
1423}
1424
Abramo Bagnara60804e12011-03-18 15:16:37 +00001425void
1426DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1427 unsigned NumTPLists,
1428 TemplateParameterList **TPLists) {
1429 assert(NumTPLists > 0);
1430 // Make sure the extended decl info is allocated.
1431 if (!hasExtInfo()) {
1432 // Save (non-extended) type source info pointer.
1433 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1434 // Allocate external info struct.
1435 DeclInfo = new (getASTContext()) ExtInfo;
1436 // Restore savedTInfo into (extended) decl info.
1437 getExtInfo()->TInfo = savedTInfo;
1438 }
1439 // Set the template parameter lists info.
1440 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1441}
1442
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001443SourceLocation DeclaratorDecl::getOuterLocStart() const {
1444 return getTemplateOrInnerLocStart(this);
1445}
1446
Abramo Bagnaraea947882011-03-08 16:41:52 +00001447namespace {
1448
1449// Helper function: returns true if QT is or contains a type
1450// having a postfix component.
1451bool typeIsPostfix(clang::QualType QT) {
1452 while (true) {
1453 const Type* T = QT.getTypePtr();
1454 switch (T->getTypeClass()) {
1455 default:
1456 return false;
1457 case Type::Pointer:
1458 QT = cast<PointerType>(T)->getPointeeType();
1459 break;
1460 case Type::BlockPointer:
1461 QT = cast<BlockPointerType>(T)->getPointeeType();
1462 break;
1463 case Type::MemberPointer:
1464 QT = cast<MemberPointerType>(T)->getPointeeType();
1465 break;
1466 case Type::LValueReference:
1467 case Type::RValueReference:
1468 QT = cast<ReferenceType>(T)->getPointeeType();
1469 break;
1470 case Type::PackExpansion:
1471 QT = cast<PackExpansionType>(T)->getPattern();
1472 break;
1473 case Type::Paren:
1474 case Type::ConstantArray:
1475 case Type::DependentSizedArray:
1476 case Type::IncompleteArray:
1477 case Type::VariableArray:
1478 case Type::FunctionProto:
1479 case Type::FunctionNoProto:
1480 return true;
1481 }
1482 }
1483}
1484
1485} // namespace
1486
1487SourceRange DeclaratorDecl::getSourceRange() const {
1488 SourceLocation RangeEnd = getLocation();
1489 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1490 if (typeIsPostfix(TInfo->getType()))
1491 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1492 }
1493 return SourceRange(getOuterLocStart(), RangeEnd);
1494}
1495
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001496void
Douglas Gregor20527e22010-06-15 17:44:38 +00001497QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1498 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001499 TemplateParameterList **TPLists) {
1500 assert((NumTPLists == 0 || TPLists != 0) &&
1501 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001502
1503 // Free previous template parameters (if any).
1504 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001505 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001506 TemplParamLists = 0;
1507 NumTemplParamLists = 0;
1508 }
1509 // Set info on matched template parameter lists (if any).
1510 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001511 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001512 NumTemplParamLists = NumTPLists;
1513 for (unsigned i = NumTPLists; i-- > 0; )
1514 TemplParamLists[i] = TPLists[i];
1515 }
1516}
1517
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001518//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001519// VarDecl Implementation
1520//===----------------------------------------------------------------------===//
1521
Sebastian Redl833ef452010-01-26 22:01:41 +00001522const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1523 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001524 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001525 case SC_Auto: return "auto";
1526 case SC_Extern: return "extern";
1527 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1528 case SC_PrivateExtern: return "__private_extern__";
1529 case SC_Register: return "register";
1530 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001531 }
1532
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001533 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001534}
1535
Abramo Bagnaradff19302011-03-08 08:55:46 +00001536VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1537 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001538 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001539 StorageClass S) {
1540 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001541}
1542
Douglas Gregor72172e92012-01-05 21:55:30 +00001543VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1544 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1545 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001546 QualType(), 0, SC_None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001547}
1548
Douglas Gregorbf62d642010-12-06 18:36:25 +00001549void VarDecl::setStorageClass(StorageClass SC) {
1550 assert(isLegalForVariable(SC));
John McCallbeaa11c2011-05-01 02:13:58 +00001551 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001552}
1553
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001554SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001555 if (const Expr *Init = getInit()) {
1556 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001557 // If Init is implicit, ignore its source range and fallback on
1558 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1559 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001560 return SourceRange(getOuterLocStart(), InitEnd);
1561 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001562 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001563}
1564
Rafael Espindola88510672013-01-04 21:18:45 +00001565template<typename T>
Rafael Espindolaf4187652013-02-14 01:18:37 +00001566static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001567 // C++ [dcl.link]p1: All function types, function names with external linkage,
1568 // and variable names with external linkage have a language linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +00001569 if (!D.hasExternalFormalLinkage())
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001570 return NoLanguageLinkage;
1571
1572 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001573 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001574 ASTContext &Context = D.getASTContext();
1575 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001576 return CLanguageLinkage;
1577
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001578 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1579 // language linkage of the names of class members and the function type of
1580 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001581 const DeclContext *DC = D.getDeclContext();
1582 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001583 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001584
1585 // If the first decl is in an extern "C" context, any other redeclaration
1586 // will have C language linkage. If the first one is not in an extern "C"
1587 // context, we would have reported an error for any other decl being in one.
Rafael Espindola593537a2013-05-05 20:15:21 +00001588 if (isFirstInExternCContext(&D))
Rafael Espindolaf4187652013-02-14 01:18:37 +00001589 return CLanguageLinkage;
1590 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001591}
1592
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001593template<typename T>
1594static bool isExternCTemplate(const T &D) {
1595 // Since the context is ignored for class members, they can only have C++
1596 // language linkage or no language linkage.
1597 const DeclContext *DC = D.getDeclContext();
1598 if (DC->isRecord()) {
1599 assert(D.getASTContext().getLangOpts().CPlusPlus);
1600 return false;
1601 }
1602
1603 return D.getLanguageLinkage() == CLanguageLinkage;
1604}
1605
Rafael Espindolaf4187652013-02-14 01:18:37 +00001606LanguageLinkage VarDecl::getLanguageLinkage() const {
1607 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001608}
1609
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001610bool VarDecl::isExternC() const {
1611 return isExternCTemplate(*this);
1612}
1613
Rafael Espindola593537a2013-05-05 20:15:21 +00001614static bool isLinkageSpecContext(const DeclContext *DC,
1615 LinkageSpecDecl::LanguageIDs ID) {
1616 while (DC->getDeclKind() != Decl::TranslationUnit) {
1617 if (DC->getDeclKind() == Decl::LinkageSpec)
1618 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1619 DC = DC->getParent();
1620 }
1621 return false;
1622}
1623
1624template <typename T>
1625static bool isInLanguageSpecContext(T *D, LinkageSpecDecl::LanguageIDs ID) {
1626 return isLinkageSpecContext(D->getLexicalDeclContext(), ID);
1627}
1628
1629bool VarDecl::isInExternCContext() const {
1630 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
1631}
1632
1633bool VarDecl::isInExternCXXContext() const {
1634 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
1635}
1636
Sebastian Redl833ef452010-01-26 22:01:41 +00001637VarDecl *VarDecl::getCanonicalDecl() {
1638 return getFirstDeclaration();
1639}
1640
Daniel Dunbar9d355812012-03-09 01:51:51 +00001641VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1642 ASTContext &C) const
1643{
Sebastian Redl35351a92010-01-31 22:27:38 +00001644 // C++ [basic.def]p2:
1645 // A declaration is a definition unless [...] it contains the 'extern'
1646 // specifier or a linkage-specification and neither an initializer [...],
1647 // it declares a static data member in a class declaration [...].
1648 // C++ [temp.expl.spec]p15:
1649 // An explicit specialization of a static data member of a template is a
1650 // definition if the declaration includes an initializer; otherwise, it is
1651 // a declaration.
1652 if (isStaticDataMember()) {
1653 if (isOutOfLine() && (hasInit() ||
1654 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1655 return Definition;
1656 else
1657 return DeclarationOnly;
1658 }
1659 // C99 6.7p5:
1660 // A definition of an identifier is a declaration for that identifier that
1661 // [...] causes storage to be reserved for that object.
1662 // Note: that applies for all non-file-scope objects.
1663 // C99 6.9.2p1:
1664 // If the declaration of an identifier for an object has file scope and an
1665 // initializer, the declaration is an external definition for the identifier
1666 if (hasInit())
1667 return Definition;
Rafael Espindolabff59562013-04-25 12:11:36 +00001668
Sebastian Redl35351a92010-01-31 22:27:38 +00001669 if (hasExternalStorage())
1670 return DeclarationOnly;
Rafael Espindola8f326a52013-03-07 01:42:44 +00001671
Rafael Espindolabff59562013-04-25 12:11:36 +00001672 // [dcl.link] p7:
1673 // A declaration directly contained in a linkage-specification is treated
1674 // as if it contains the extern specifier for the purpose of determining
1675 // the linkage of the declared name and whether it is a definition.
Rafael Espindola327be3c2013-04-26 01:30:23 +00001676 if (isSingleLineExternC(*this))
1677 return DeclarationOnly;
Rafael Espindolabff59562013-04-25 12:11:36 +00001678
Sebastian Redl35351a92010-01-31 22:27:38 +00001679 // C99 6.9.2p2:
1680 // A declaration of an object that has file scope without an initializer,
1681 // and without a storage class specifier or the scs 'static', constitutes
1682 // a tentative definition.
1683 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001684 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001685 return TentativeDefinition;
1686
1687 // What's left is (in C, block-scope) declarations without initializers or
1688 // external storage. These are definitions.
1689 return Definition;
1690}
1691
Sebastian Redl35351a92010-01-31 22:27:38 +00001692VarDecl *VarDecl::getActingDefinition() {
1693 DefinitionKind Kind = isThisDeclarationADefinition();
1694 if (Kind != TentativeDefinition)
1695 return 0;
1696
Chris Lattner48eb14d2010-06-14 18:31:46 +00001697 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001698 VarDecl *First = getFirstDeclaration();
1699 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1700 I != E; ++I) {
1701 Kind = (*I)->isThisDeclarationADefinition();
1702 if (Kind == Definition)
1703 return 0;
1704 else if (Kind == TentativeDefinition)
1705 LastTentative = *I;
1706 }
1707 return LastTentative;
1708}
1709
1710bool VarDecl::isTentativeDefinitionNow() const {
1711 DefinitionKind Kind = isThisDeclarationADefinition();
1712 if (Kind != TentativeDefinition)
1713 return false;
1714
1715 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1716 if ((*I)->isThisDeclarationADefinition() == Definition)
1717 return false;
1718 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001719 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001720}
1721
Daniel Dunbar9d355812012-03-09 01:51:51 +00001722VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001723 VarDecl *First = getFirstDeclaration();
1724 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1725 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001726 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001727 return *I;
1728 }
1729 return 0;
1730}
1731
Daniel Dunbar9d355812012-03-09 01:51:51 +00001732VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001733 DefinitionKind Kind = DeclarationOnly;
1734
1735 const VarDecl *First = getFirstDeclaration();
1736 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001737 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001738 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001739 if (Kind == Definition)
1740 break;
1741 }
John McCall37bb6c92010-10-29 22:22:43 +00001742
1743 return Kind;
1744}
1745
Sebastian Redl5ca79842010-02-01 20:16:42 +00001746const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001747 redecl_iterator I = redecls_begin(), E = redecls_end();
1748 while (I != E && !I->getInit())
1749 ++I;
1750
1751 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001752 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001753 return I->getInit();
1754 }
1755 return 0;
1756}
1757
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001758bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001759 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001760 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001761
1762 if (!isStaticDataMember())
1763 return false;
1764
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001765 // If this static data member was instantiated from a static data member of
1766 // a class template, check whether that static data member was defined
1767 // out-of-line.
1768 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1769 return VD->isOutOfLine();
1770
1771 return false;
1772}
1773
Douglas Gregor1d957a32009-10-27 18:42:08 +00001774VarDecl *VarDecl::getOutOfLineDefinition() {
1775 if (!isStaticDataMember())
1776 return 0;
1777
1778 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1779 RD != RDEnd; ++RD) {
1780 if (RD->getLexicalDeclContext()->isFileContext())
1781 return *RD;
1782 }
1783
1784 return 0;
1785}
1786
Douglas Gregord5058122010-02-11 01:19:42 +00001787void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001788 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1789 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001790 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001791 }
1792
1793 Init = I;
1794}
1795
Daniel Dunbar9d355812012-03-09 01:51:51 +00001796bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001797 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001798
Richard Smith35ecb362012-03-02 04:14:40 +00001799 if (!Lang.CPlusPlus)
1800 return false;
1801
1802 // In C++11, any variable of reference type can be used in a constant
1803 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001804 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001805 return true;
1806
1807 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001808 // not require the variable to be non-volatile, but we consider this to be a
1809 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001810 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001811 return false;
1812
1813 // In C++, const, non-volatile variables of integral or enumeration types
1814 // can be used in constant expressions.
1815 if (getType()->isIntegralOrEnumerationType())
1816 return true;
1817
Richard Smith35ecb362012-03-02 04:14:40 +00001818 // Additionally, in C++11, non-volatile constexpr variables can be used in
1819 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001820 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001821}
1822
Richard Smithd0b4dd62011-12-19 06:19:21 +00001823/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1824/// form, which contains extra information on the evaluated value of the
1825/// initializer.
1826EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1827 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1828 if (!Eval) {
1829 Stmt *S = Init.get<Stmt *>();
Manuel Klimeka7328992013-06-03 13:51:33 +00001830 // Note: EvaluatedStmt contains an APValue, which usually holds
1831 // resources not allocated from the ASTContext. We need to do some
1832 // work to avoid leaking those, but we do so in VarDecl::evaluateValue
1833 // where we can detect whether there's anything to clean up or not.
Richard Smithd0b4dd62011-12-19 06:19:21 +00001834 Eval = new (getASTContext()) EvaluatedStmt;
1835 Eval->Value = S;
1836 Init = Eval;
1837 }
1838 return Eval;
1839}
1840
Richard Smithdafff942012-01-14 04:30:29 +00001841APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001842 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00001843 return evaluateValue(Notes);
1844}
1845
Manuel Klimeka7328992013-06-03 13:51:33 +00001846namespace {
1847// Destroy an APValue that was allocated in an ASTContext.
1848void DestroyAPValue(void* UntypedValue) {
1849 static_cast<APValue*>(UntypedValue)->~APValue();
1850}
1851} // namespace
1852
Richard Smithdafff942012-01-14 04:30:29 +00001853APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001854 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001855 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1856
1857 // We only produce notes indicating why an initializer is non-constant the
1858 // first time it is evaluated. FIXME: The notes won't always be emitted the
1859 // first time we try evaluation, so might not be produced at all.
1860 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001861 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001862
1863 const Expr *Init = cast<Expr>(Eval->Value);
1864 assert(!Init->isValueDependent());
1865
1866 if (Eval->IsEvaluating) {
1867 // FIXME: Produce a diagnostic for self-initialization.
1868 Eval->CheckedICE = true;
1869 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001870 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001871 }
1872
1873 Eval->IsEvaluating = true;
1874
1875 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1876 this, Notes);
1877
Manuel Klimeka7328992013-06-03 13:51:33 +00001878 // Ensure the computed APValue is cleaned up later if evaluation succeeded,
1879 // or that it's empty (so that there's nothing to clean up) if evaluation
1880 // failed.
Richard Smithd0b4dd62011-12-19 06:19:21 +00001881 if (!Result)
1882 Eval->Evaluated = APValue();
Manuel Klimeka7328992013-06-03 13:51:33 +00001883 else if (Eval->Evaluated.needsCleanup())
1884 getASTContext().AddDeallocation(DestroyAPValue, &Eval->Evaluated);
Richard Smithd0b4dd62011-12-19 06:19:21 +00001885
1886 Eval->IsEvaluating = false;
1887 Eval->WasEvaluated = true;
1888
1889 // In C++11, we have determined whether the initializer was a constant
1890 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001891 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001892 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001893 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001894 }
1895
Richard Smithdafff942012-01-14 04:30:29 +00001896 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001897}
1898
1899bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001900 // Initializers of weak variables are never ICEs.
1901 if (isWeak())
1902 return false;
1903
Richard Smithd0b4dd62011-12-19 06:19:21 +00001904 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1905 if (Eval->CheckedICE)
1906 // We have already checked whether this subexpression is an
1907 // integral constant expression.
1908 return Eval->IsICE;
1909
1910 const Expr *Init = cast<Expr>(Eval->Value);
1911 assert(!Init->isValueDependent());
1912
1913 // In C++11, evaluate the initializer to check whether it's a constant
1914 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001915 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001916 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001917 evaluateValue(Notes);
1918 return Eval->IsICE;
1919 }
1920
1921 // It's an ICE whether or not the definition we found is
1922 // out-of-line. See DR 721 and the discussion in Clang PR
1923 // 6206 for details.
1924
1925 if (Eval->CheckingICE)
1926 return false;
1927 Eval->CheckingICE = true;
1928
1929 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1930 Eval->CheckingICE = false;
1931 Eval->CheckedICE = true;
1932 return Eval->IsICE;
1933}
1934
Douglas Gregorfe314812011-06-21 17:03:29 +00001935bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001936 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001937
1938 const Expr *E = getInit();
1939 if (!E)
1940 return false;
1941
1942 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1943 E = Cleanups->getSubExpr();
1944
1945 return isa<MaterializeTemporaryExpr>(E);
1946}
1947
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001948VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001949 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001950 return cast<VarDecl>(MSI->getInstantiatedFrom());
1951
1952 return 0;
1953}
1954
Douglas Gregor3c74d412009-10-14 20:14:33 +00001955TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001956 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001957 return MSI->getTemplateSpecializationKind();
1958
1959 return TSK_Undeclared;
1960}
1961
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001962MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001963 return getASTContext().getInstantiatedFromStaticDataMember(this);
1964}
1965
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001966void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1967 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001968 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001969 assert(MSI && "Not an instantiated static data member?");
1970 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001971 if (TSK != TSK_ExplicitSpecialization &&
1972 PointOfInstantiation.isValid() &&
1973 MSI->getPointOfInstantiation().isInvalid())
1974 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001975}
1976
Sebastian Redl833ef452010-01-26 22:01:41 +00001977//===----------------------------------------------------------------------===//
1978// ParmVarDecl Implementation
1979//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001980
Sebastian Redl833ef452010-01-26 22:01:41 +00001981ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001982 SourceLocation StartLoc,
1983 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001984 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001985 StorageClass S, Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001986 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001987 S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001988}
1989
Douglas Gregor72172e92012-01-05 21:55:30 +00001990ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1991 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1992 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001993 0, QualType(), 0, SC_None, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001994}
1995
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001996SourceRange ParmVarDecl::getSourceRange() const {
1997 if (!hasInheritedDefaultArg()) {
1998 SourceRange ArgRange = getDefaultArgRange();
1999 if (ArgRange.isValid())
2000 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
2001 }
2002
Argyrios Kyrtzidisa0772792013-04-17 01:56:48 +00002003 // DeclaratorDecl considers the range of postfix types as overlapping with the
2004 // declaration name, but this is not the case with parameters in ObjC methods.
2005 if (isa<ObjCMethodDecl>(getDeclContext()))
2006 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
2007
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00002008 return DeclaratorDecl::getSourceRange();
2009}
2010
Sebastian Redl833ef452010-01-26 22:01:41 +00002011Expr *ParmVarDecl::getDefaultArg() {
2012 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
2013 assert(!hasUninstantiatedDefaultArg() &&
2014 "Default argument is not yet instantiated!");
2015
2016 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00002017 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00002018 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00002019
Sebastian Redl833ef452010-01-26 22:01:41 +00002020 return Arg;
2021}
2022
Sebastian Redl833ef452010-01-26 22:01:41 +00002023SourceRange ParmVarDecl::getDefaultArgRange() const {
2024 if (const Expr *E = getInit())
2025 return E->getSourceRange();
2026
2027 if (hasUninstantiatedDefaultArg())
2028 return getUninstantiatedDefaultArg()->getSourceRange();
2029
2030 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00002031}
2032
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00002033bool ParmVarDecl::isParameterPack() const {
2034 return isa<PackExpansionType>(getType());
2035}
2036
Ted Kremenek540017e2011-10-06 05:00:56 +00002037void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2038 getASTContext().setParameterIndex(this, parameterIndex);
2039 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2040}
2041
2042unsigned ParmVarDecl::getParameterIndexLarge() const {
2043 return getASTContext().getParameterIndex(this);
2044}
2045
Nuno Lopes394ec982008-12-17 23:39:55 +00002046//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002047// FunctionDecl Implementation
2048//===----------------------------------------------------------------------===//
2049
Benjamin Kramer9170e912013-02-22 15:46:01 +00002050void FunctionDecl::getNameForDiagnostic(
2051 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2052 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002053 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2054 if (TemplateArgs)
Benjamin Kramer9170e912013-02-22 15:46:01 +00002055 TemplateSpecializationType::PrintTemplateArgumentList(
2056 OS, TemplateArgs->data(), TemplateArgs->size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002057}
2058
Ted Kremenek186a0742010-04-29 16:49:01 +00002059bool FunctionDecl::isVariadic() const {
2060 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
2061 return FT->isVariadic();
2062 return false;
2063}
2064
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002065bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
2066 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00002067 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002068 Definition = *I;
2069 return true;
2070 }
2071 }
2072
2073 return false;
2074}
2075
Anders Carlsson9bd7d162011-05-14 23:26:09 +00002076bool FunctionDecl::hasTrivialBody() const
2077{
2078 Stmt *S = getBody();
2079 if (!S) {
2080 // Since we don't have a body for this function, we don't know if it's
2081 // trivial or not.
2082 return false;
2083 }
2084
2085 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2086 return true;
2087 return false;
2088}
2089
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002090bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2091 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00002092 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002093 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
2094 return true;
2095 }
2096 }
2097
2098 return false;
2099}
2100
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002101Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00002102 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2103 if (I->Body) {
2104 Definition = *I;
2105 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00002106 } else if (I->IsLateTemplateParsed) {
2107 Definition = *I;
2108 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00002109 }
2110 }
2111
2112 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002113}
2114
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002115void FunctionDecl::setBody(Stmt *B) {
2116 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00002117 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002118 EndRangeLoc = B->getLocEnd();
2119}
2120
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002121void FunctionDecl::setPure(bool P) {
2122 IsPure = P;
2123 if (P)
2124 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2125 Parent->markedVirtualFunctionPure();
2126}
2127
Douglas Gregor16618f22009-09-12 00:17:51 +00002128bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00002129 const TranslationUnitDecl *tunit =
2130 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2131 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002132 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00002133 getIdentifier() &&
2134 getIdentifier()->isStr("main");
2135}
2136
2137bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2138 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2139 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2140 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2141 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2142 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2143
2144 if (isa<CXXRecordDecl>(getDeclContext())) return false;
2145 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2146
2147 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
2148 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
2149
2150 ASTContext &Context =
2151 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2152 ->getASTContext();
2153
2154 // The result type and first argument type are constant across all
2155 // these operators. The second argument must be exactly void*.
2156 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00002157}
2158
Rafael Espindolaf4187652013-02-14 01:18:37 +00002159LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6239e052013-01-12 15:27:44 +00002160 // Users expect to be able to write
2161 // extern "C" void *__builtin_alloca (size_t);
2162 // so consider builtins as having C language linkage.
Rafael Espindolac48f7342013-01-12 15:27:43 +00002163 if (getBuiltinID())
Rafael Espindolaf4187652013-02-14 01:18:37 +00002164 return CLanguageLinkage;
Rafael Espindolac48f7342013-01-12 15:27:43 +00002165
Rafael Espindolaf4187652013-02-14 01:18:37 +00002166 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00002167}
2168
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002169bool FunctionDecl::isExternC() const {
2170 return isExternCTemplate(*this);
2171}
2172
Rafael Espindola593537a2013-05-05 20:15:21 +00002173bool FunctionDecl::isInExternCContext() const {
2174 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
2175}
2176
2177bool FunctionDecl::isInExternCXXContext() const {
2178 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
2179}
2180
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002181bool FunctionDecl::isGlobal() const {
2182 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
2183 return Method->isStatic();
2184
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002185 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002186 return false;
2187
Mike Stump11289f42009-09-09 15:08:12 +00002188 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002189 DC->isNamespace();
2190 DC = DC->getParent()) {
2191 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
2192 if (!Namespace->getDeclName())
2193 return false;
2194 break;
2195 }
2196 }
2197
2198 return true;
2199}
2200
Richard Smith10876ef2013-01-17 01:30:42 +00002201bool FunctionDecl::isNoReturn() const {
2202 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smithdebc59d2013-01-30 05:45:05 +00002203 hasAttr<C11NoReturnAttr>() ||
Richard Smith10876ef2013-01-17 01:30:42 +00002204 getType()->getAs<FunctionType>()->getNoReturnAttr();
2205}
2206
Sebastian Redl833ef452010-01-26 22:01:41 +00002207void
2208FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
2209 redeclarable_base::setPreviousDeclaration(PrevDecl);
2210
2211 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2212 FunctionTemplateDecl *PrevFunTmpl
2213 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
2214 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
2215 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
2216 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00002217
Axel Naumannfbc7b982011-11-08 18:21:06 +00002218 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00002219 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00002220}
2221
2222const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
2223 return getFirstDeclaration();
2224}
2225
2226FunctionDecl *FunctionDecl::getCanonicalDecl() {
2227 return getFirstDeclaration();
2228}
2229
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002230/// \brief Returns a value indicating whether this function
2231/// corresponds to a builtin function.
2232///
2233/// The function corresponds to a built-in function if it is
2234/// declared at translation scope or within an extern "C" block and
2235/// its name matches with the name of a builtin. The returned value
2236/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00002237/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002238/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00002239unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00002240 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00002241 return 0;
2242
2243 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00002244 if (!BuiltinID)
2245 return 0;
2246
2247 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00002248 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2249 return BuiltinID;
2250
2251 // This function has the name of a known C library
2252 // function. Determine whether it actually refers to the C library
2253 // function or whether it just has the same name.
2254
Douglas Gregora908e7f2009-02-17 03:23:10 +00002255 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00002256 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00002257 return 0;
2258
Douglas Gregore711f702009-02-14 18:57:46 +00002259 // If this function is at translation-unit scope and we're not in
2260 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002261 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00002262 getDeclContext()->isTranslationUnit())
2263 return BuiltinID;
2264
2265 // If the function is in an extern "C" linkage specification and is
2266 // not marked "overloadable", it's the real function.
2267 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00002268 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00002269 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002270 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00002271 return BuiltinID;
2272
2273 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002274 return 0;
2275}
2276
2277
Chris Lattner47c0d002009-04-25 06:03:53 +00002278/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00002279/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00002280/// after it has been created.
2281unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00002282 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002283 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00002284 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002285 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00002286
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002287}
2288
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002289void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002290 ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002291 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002292 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002293
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002294 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002295 if (!NewParamInfo.empty()) {
2296 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2297 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002298 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002299}
Chris Lattner41943152007-01-25 04:52:46 +00002300
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002301void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy6f8780b2012-02-29 10:24:19 +00002302 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2303
2304 if (!NewDecls.empty()) {
2305 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2306 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002307 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy6f8780b2012-02-29 10:24:19 +00002308 }
2309}
2310
Chris Lattner58258242008-04-10 02:22:51 +00002311/// getMinRequiredArguments - Returns the minimum number of arguments
2312/// needed to call this function. This may be fewer than the number of
2313/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00002314/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00002315unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002316 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002317 return getNumParams();
2318
Douglas Gregor7825bf32011-01-06 22:09:01 +00002319 unsigned NumRequiredArgs = getNumParams();
2320
2321 // If the last parameter is a parameter pack, we don't need an argument for
2322 // it.
2323 if (NumRequiredArgs > 0 &&
2324 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2325 --NumRequiredArgs;
2326
2327 // If this parameter has a default argument, we don't need an argument for
2328 // it.
2329 while (NumRequiredArgs > 0 &&
2330 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00002331 --NumRequiredArgs;
2332
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002333 // We might have parameter packs before the end. These can't be deduced,
2334 // but they can still handle multiple arguments.
2335 unsigned ArgIdx = NumRequiredArgs;
2336 while (ArgIdx > 0) {
2337 if (getParamDecl(ArgIdx - 1)->isParameterPack())
2338 NumRequiredArgs = ArgIdx;
2339
2340 --ArgIdx;
2341 }
2342
Chris Lattner58258242008-04-10 02:22:51 +00002343 return NumRequiredArgs;
2344}
2345
Eli Friedman1b125c32012-02-07 03:50:18 +00002346static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2347 // Only consider file-scope declarations in this test.
2348 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2349 return false;
2350
2351 // Only consider explicit declarations; the presence of a builtin for a
2352 // libcall shouldn't affect whether a definition is externally visible.
2353 if (Redecl->isImplicit())
2354 return false;
2355
2356 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2357 return true; // Not an inline definition
2358
2359 return false;
2360}
2361
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002362/// \brief For a function declaration in C or C++, determine whether this
2363/// declaration causes the definition to be externally visible.
2364///
Eli Friedman1b125c32012-02-07 03:50:18 +00002365/// Specifically, this determines if adding the current declaration to the set
2366/// of redeclarations of the given functions causes
2367/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002368bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2369 assert(!doesThisDeclarationHaveABody() &&
2370 "Must have a declaration without a body.");
2371
2372 ASTContext &Context = getASTContext();
2373
David Blaikiebbafb8a2012-03-11 07:00:24 +00002374 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002375 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2376 // an externally visible definition.
2377 //
2378 // FIXME: What happens if gnu_inline gets added on after the first
2379 // declaration?
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002380 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002381 return false;
2382
2383 const FunctionDecl *Prev = this;
2384 bool FoundBody = false;
2385 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002386 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002387
2388 if (Prev->Body) {
2389 // If it's not the case that both 'inline' and 'extern' are
2390 // specified on the definition, then it is always externally visible.
2391 if (!Prev->isInlineSpecified() ||
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002392 Prev->getStorageClass() != SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002393 return false;
2394 } else if (Prev->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002395 Prev->getStorageClass() != SC_Extern) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002396 return false;
2397 }
2398 }
2399 return FoundBody;
2400 }
2401
David Blaikiebbafb8a2012-03-11 07:00:24 +00002402 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002403 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002404
2405 // C99 6.7.4p6:
2406 // [...] If all of the file scope declarations for a function in a
2407 // translation unit include the inline function specifier without extern,
2408 // then the definition in that translation unit is an inline definition.
2409 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002410 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002411 const FunctionDecl *Prev = this;
2412 bool FoundBody = false;
2413 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002414 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002415 if (RedeclForcesDefC99(Prev))
2416 return false;
2417 }
2418 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002419}
2420
Richard Smithf3814ad2013-01-25 00:08:28 +00002421/// \brief For an inline function definition in C, or for a gnu_inline function
2422/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002423///
2424/// Inline function definitions are always available for inlining optimizations.
2425/// However, depending on the language dialect, declaration specifiers, and
2426/// attributes, the definition of an inline function may or may not be
2427/// "externally" visible to other translation units in the program.
2428///
2429/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002430/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002431/// inline definition becomes externally visible (C99 6.7.4p6).
2432///
2433/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2434/// definition, we use the GNU semantics for inline, which are nearly the
2435/// opposite of C99 semantics. In particular, "inline" by itself will create
2436/// an externally visible symbol, but "extern inline" will not create an
2437/// externally visible symbol.
2438bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002439 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002440 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002441 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002442
David Blaikiebbafb8a2012-03-11 07:00:24 +00002443 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002444 // Note: If you change the logic here, please change
2445 // doesDeclarationForceExternallyVisibleDefinition as well.
2446 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002447 // If it's not the case that both 'inline' and 'extern' are
2448 // specified on the definition, then this inline definition is
2449 // externally visible.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002450 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregorff76cb92010-12-09 16:59:22 +00002451 return true;
2452
2453 // If any declaration is 'inline' but not 'extern', then this definition
2454 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002455 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2456 Redecl != RedeclEnd;
2457 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002458 if (Redecl->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002459 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002460 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002461 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002462
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002463 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002464 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002465
Richard Smithf3814ad2013-01-25 00:08:28 +00002466 // The rest of this function is C-only.
2467 assert(!Context.getLangOpts().CPlusPlus &&
2468 "should not use C inline rules in C++");
2469
Douglas Gregor299d76e2009-09-13 07:46:26 +00002470 // C99 6.7.4p6:
2471 // [...] If all of the file scope declarations for a function in a
2472 // translation unit include the inline function specifier without extern,
2473 // then the definition in that translation unit is an inline definition.
2474 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2475 Redecl != RedeclEnd;
2476 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002477 if (RedeclForcesDefC99(*Redecl))
2478 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002479 }
2480
2481 // C99 6.7.4p6:
2482 // An inline definition does not provide an external definition for the
2483 // function, and does not forbid an external definition in another
2484 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002485 return false;
2486}
2487
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002488/// getOverloadedOperator - Which C++ overloaded operator this
2489/// function represents, if any.
2490OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002491 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2492 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002493 else
2494 return OO_None;
2495}
2496
Alexis Huntc88db062010-01-13 09:01:02 +00002497/// getLiteralIdentifier - The literal suffix identifier this function
2498/// represents, if any.
2499const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2500 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2501 return getDeclName().getCXXLiteralIdentifier();
2502 else
2503 return 0;
2504}
2505
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002506FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2507 if (TemplateOrSpecialization.isNull())
2508 return TK_NonTemplate;
2509 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2510 return TK_FunctionTemplate;
2511 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2512 return TK_MemberSpecialization;
2513 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2514 return TK_FunctionTemplateSpecialization;
2515 if (TemplateOrSpecialization.is
2516 <DependentFunctionTemplateSpecializationInfo*>())
2517 return TK_DependentFunctionTemplateSpecialization;
2518
David Blaikie83d382b2011-09-23 05:06:16 +00002519 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002520}
2521
Douglas Gregord801b062009-10-07 23:56:10 +00002522FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002523 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002524 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2525
2526 return 0;
2527}
2528
2529void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002530FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2531 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002532 TemplateSpecializationKind TSK) {
2533 assert(TemplateOrSpecialization.isNull() &&
2534 "Member function is already a specialization");
2535 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002536 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002537 TemplateOrSpecialization = Info;
2538}
2539
Douglas Gregorafca3b42009-10-27 20:53:28 +00002540bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002541 // If the function is invalid, it can't be implicitly instantiated.
2542 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002543 return false;
2544
2545 switch (getTemplateSpecializationKind()) {
2546 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002547 case TSK_ExplicitInstantiationDefinition:
2548 return false;
2549
2550 case TSK_ImplicitInstantiation:
2551 return true;
2552
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002553 // It is possible to instantiate TSK_ExplicitSpecialization kind
2554 // if the FunctionDecl has a class scope specialization pattern.
2555 case TSK_ExplicitSpecialization:
2556 return getClassScopeSpecializationPattern() != 0;
2557
Douglas Gregorafca3b42009-10-27 20:53:28 +00002558 case TSK_ExplicitInstantiationDeclaration:
2559 // Handled below.
2560 break;
2561 }
2562
2563 // Find the actual template from which we will instantiate.
2564 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002565 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002566 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002567 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002568
2569 // C++0x [temp.explicit]p9:
2570 // Except for inline functions, other explicit instantiation declarations
2571 // have the effect of suppressing the implicit instantiation of the entity
2572 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002573 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002574 return true;
2575
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002576 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002577}
2578
2579bool FunctionDecl::isTemplateInstantiation() const {
2580 switch (getTemplateSpecializationKind()) {
2581 case TSK_Undeclared:
2582 case TSK_ExplicitSpecialization:
2583 return false;
2584 case TSK_ImplicitInstantiation:
2585 case TSK_ExplicitInstantiationDeclaration:
2586 case TSK_ExplicitInstantiationDefinition:
2587 return true;
2588 }
2589 llvm_unreachable("All TSK values handled.");
2590}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002591
2592FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002593 // Handle class scope explicit specialization special case.
2594 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2595 return getClassScopeSpecializationPattern();
2596
Douglas Gregorafca3b42009-10-27 20:53:28 +00002597 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2598 while (Primary->getInstantiatedFromMemberTemplate()) {
2599 // If we have hit a point where the user provided a specialization of
2600 // this template, we're done looking.
2601 if (Primary->isMemberSpecialization())
2602 break;
2603
2604 Primary = Primary->getInstantiatedFromMemberTemplate();
2605 }
2606
2607 return Primary->getTemplatedDecl();
2608 }
2609
2610 return getInstantiatedFromMemberFunction();
2611}
2612
Douglas Gregor70d83e22009-06-29 17:30:29 +00002613FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002614 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002615 = TemplateOrSpecialization
2616 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002617 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002618 }
2619 return 0;
2620}
2621
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002622FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2623 return getASTContext().getClassScopeSpecializationPattern(this);
2624}
2625
Douglas Gregor70d83e22009-06-29 17:30:29 +00002626const TemplateArgumentList *
2627FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002628 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002629 = TemplateOrSpecialization
2630 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002631 return Info->TemplateArguments;
2632 }
2633 return 0;
2634}
2635
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002636const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002637FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2638 if (FunctionTemplateSpecializationInfo *Info
2639 = TemplateOrSpecialization
2640 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2641 return Info->TemplateArgumentsAsWritten;
2642 }
2643 return 0;
2644}
2645
Mike Stump11289f42009-09-09 15:08:12 +00002646void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002647FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2648 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002649 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002650 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002651 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002652 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2653 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002654 assert(TSK != TSK_Undeclared &&
2655 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002656 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002657 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002658 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002659 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2660 TemplateArgs,
2661 TemplateArgsAsWritten,
2662 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002663 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002664 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002665}
2666
John McCallb9c78482010-04-08 09:05:18 +00002667void
2668FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2669 const UnresolvedSetImpl &Templates,
2670 const TemplateArgumentListInfo &TemplateArgs) {
2671 assert(TemplateOrSpecialization.isNull());
2672 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2673 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002674 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002675 void *Buffer = Context.Allocate(Size);
2676 DependentFunctionTemplateSpecializationInfo *Info =
2677 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2678 TemplateArgs);
2679 TemplateOrSpecialization = Info;
2680}
2681
2682DependentFunctionTemplateSpecializationInfo::
2683DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2684 const TemplateArgumentListInfo &TArgs)
2685 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2686
2687 d.NumTemplates = Ts.size();
2688 d.NumArgs = TArgs.size();
2689
2690 FunctionTemplateDecl **TsArray =
2691 const_cast<FunctionTemplateDecl**>(getTemplates());
2692 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2693 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2694
2695 TemplateArgumentLoc *ArgsArray =
2696 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2697 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2698 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2699}
2700
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002701TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002702 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002703 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002704 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002705 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002706 if (FTSInfo)
2707 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002708
Douglas Gregord801b062009-10-07 23:56:10 +00002709 MemberSpecializationInfo *MSInfo
2710 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2711 if (MSInfo)
2712 return MSInfo->getTemplateSpecializationKind();
2713
2714 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002715}
2716
Mike Stump11289f42009-09-09 15:08:12 +00002717void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002718FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2719 SourceLocation PointOfInstantiation) {
2720 if (FunctionTemplateSpecializationInfo *FTSInfo
2721 = TemplateOrSpecialization.dyn_cast<
2722 FunctionTemplateSpecializationInfo*>()) {
2723 FTSInfo->setTemplateSpecializationKind(TSK);
2724 if (TSK != TSK_ExplicitSpecialization &&
2725 PointOfInstantiation.isValid() &&
2726 FTSInfo->getPointOfInstantiation().isInvalid())
2727 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2728 } else if (MemberSpecializationInfo *MSInfo
2729 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2730 MSInfo->setTemplateSpecializationKind(TSK);
2731 if (TSK != TSK_ExplicitSpecialization &&
2732 PointOfInstantiation.isValid() &&
2733 MSInfo->getPointOfInstantiation().isInvalid())
2734 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2735 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002736 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002737}
2738
2739SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002740 if (FunctionTemplateSpecializationInfo *FTSInfo
2741 = TemplateOrSpecialization.dyn_cast<
2742 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002743 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002744 else if (MemberSpecializationInfo *MSInfo
2745 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002746 return MSInfo->getPointOfInstantiation();
2747
2748 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002749}
2750
Douglas Gregor6411b922009-09-11 20:15:17 +00002751bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002752 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002753 return true;
2754
2755 // If this function was instantiated from a member function of a
2756 // class template, check whether that member function was defined out-of-line.
2757 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2758 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002759 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002760 return Definition->isOutOfLine();
2761 }
2762
2763 // If this function was instantiated from a function template,
2764 // check whether that function template was defined out-of-line.
2765 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2766 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002767 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002768 return Definition->isOutOfLine();
2769 }
2770
2771 return false;
2772}
2773
Abramo Bagnaraea947882011-03-08 16:41:52 +00002774SourceRange FunctionDecl::getSourceRange() const {
2775 return SourceRange(getOuterLocStart(), EndRangeLoc);
2776}
2777
Anna Zaks28db7ce2012-01-18 02:45:01 +00002778unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002779 IdentifierInfo *FnInfo = getIdentifier();
2780
2781 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002782 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002783
2784 // Builtin handling.
2785 switch (getBuiltinID()) {
2786 case Builtin::BI__builtin_memset:
2787 case Builtin::BI__builtin___memset_chk:
2788 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002789 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002790
2791 case Builtin::BI__builtin_memcpy:
2792 case Builtin::BI__builtin___memcpy_chk:
2793 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002794 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002795
2796 case Builtin::BI__builtin_memmove:
2797 case Builtin::BI__builtin___memmove_chk:
2798 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002799 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002800
2801 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002802 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002803 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002804 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002805
2806 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002807 case Builtin::BImemcmp:
2808 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002809
2810 case Builtin::BI__builtin_strncpy:
2811 case Builtin::BI__builtin___strncpy_chk:
2812 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002813 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002814
2815 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002816 case Builtin::BIstrncmp:
2817 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002818
2819 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002820 case Builtin::BIstrncasecmp:
2821 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002822
2823 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002824 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002825 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002826 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002827
2828 case Builtin::BI__builtin_strndup:
2829 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002830 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002831
Anna Zaks314cd092012-02-01 19:08:57 +00002832 case Builtin::BI__builtin_strlen:
2833 case Builtin::BIstrlen:
2834 return Builtin::BIstrlen;
2835
Anna Zaks201d4892012-01-13 21:52:01 +00002836 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002837 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002838 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002839 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002840 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002841 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002842 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002843 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002844 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002845 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002846 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002847 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002848 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002849 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002850 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002851 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002852 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002853 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002854 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002855 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002856 else if (FnInfo->isStr("strlen"))
2857 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002858 }
2859 break;
2860 }
Anna Zaks22122702012-01-17 00:37:07 +00002861 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002862}
2863
Chris Lattner59a25942008-03-31 00:36:02 +00002864//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002865// FieldDecl Implementation
2866//===----------------------------------------------------------------------===//
2867
Jay Foad39c79802011-01-12 09:06:06 +00002868FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002869 SourceLocation StartLoc, SourceLocation IdLoc,
2870 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002871 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002872 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002873 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002874 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002875}
2876
Douglas Gregor72172e92012-01-05 21:55:30 +00002877FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2878 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2879 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002880 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002881}
2882
Sebastian Redl833ef452010-01-26 22:01:41 +00002883bool FieldDecl::isAnonymousStructOrUnion() const {
2884 if (!isImplicit() || getDeclName())
2885 return false;
2886
2887 if (const RecordType *Record = getType()->getAs<RecordType>())
2888 return Record->getDecl()->isAnonymousStructOrUnion();
2889
2890 return false;
2891}
2892
Richard Smithcaf33902011-10-10 18:28:20 +00002893unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2894 assert(isBitField() && "not a bitfield");
2895 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2896 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2897}
2898
John McCall4e819612011-01-20 07:57:12 +00002899unsigned FieldDecl::getFieldIndex() const {
2900 if (CachedFieldIndex) return CachedFieldIndex - 1;
2901
Richard Smithd62306a2011-11-10 06:34:14 +00002902 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002903 const RecordDecl *RD = getParent();
2904 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002905 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002906
2907 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2908 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002909 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002910
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002911 if (IsMsStruct) {
2912 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002913 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002914 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002915 continue;
2916 }
David Blaikie40ed2972012-06-06 20:45:41 +00002917 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002918 }
John McCall4e819612011-01-20 07:57:12 +00002919 }
2920
Richard Smithd62306a2011-11-10 06:34:14 +00002921 assert(CachedFieldIndex && "failed to find field in parent");
2922 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002923}
2924
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002925SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002926 if (const Expr *E = InitializerOrBitWidth.getPointer())
2927 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002928 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002929}
2930
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002931void FieldDecl::setBitWidth(Expr *Width) {
2932 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2933 "bit width or initializer already set");
2934 InitializerOrBitWidth.setPointer(Width);
2935}
2936
Richard Smith938f40b2011-06-11 17:19:42 +00002937void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002938 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002939 "bit width or initializer already set");
2940 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002941}
2942
Sebastian Redl833ef452010-01-26 22:01:41 +00002943//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002944// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002945//===----------------------------------------------------------------------===//
2946
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002947SourceLocation TagDecl::getOuterLocStart() const {
2948 return getTemplateOrInnerLocStart(this);
2949}
2950
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002951SourceRange TagDecl::getSourceRange() const {
2952 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002953 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002954}
2955
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002956TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002957 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002958}
2959
Rafael Espindolabf5c33b2013-03-12 21:06:00 +00002960void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2961 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002962 if (TypeForDecl)
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002963 assert(TypeForDecl->isLinkageValid());
2964 assert(isLinkageValid());
Douglas Gregora72a4e32010-05-19 18:39:18 +00002965}
2966
Douglas Gregordee1be82009-01-17 00:42:38 +00002967void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002968 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002969
David Blaikie095deba2012-11-14 01:52:05 +00002970 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002971 struct CXXRecordDecl::DefinitionData *Data =
2972 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002973 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2974 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002975 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002976}
2977
2978void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002979 assert((!isa<CXXRecordDecl>(this) ||
2980 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2981 "definition completed but not started");
2982
John McCallf937c022011-10-07 06:10:15 +00002983 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002984 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002985
2986 if (ASTMutationListener *L = getASTMutationListener())
2987 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002988}
2989
John McCallf937c022011-10-07 06:10:15 +00002990TagDecl *TagDecl::getDefinition() const {
2991 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002992 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002993
2994 // If it's possible for us to have an out-of-date definition, check now.
2995 if (MayHaveOutOfDateDef) {
2996 if (IdentifierInfo *II = getIdentifier()) {
2997 if (II->isOutOfDate()) {
2998 updateOutOfDate(*II);
2999 }
3000 }
3001 }
3002
Andrew Trickba266ee2010-10-19 21:54:32 +00003003 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
3004 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003005
3006 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00003007 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00003008 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00003009 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00003010
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00003011 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00003012}
3013
Douglas Gregor14454802011-02-25 02:25:35 +00003014void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
3015 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00003016 // Make sure the extended qualifier info is allocated.
3017 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00003018 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00003019 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00003020 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00003021 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00003022 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00003023 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00003024 if (getExtInfo()->NumTemplParamLists == 0) {
3025 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00003026 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003027 }
3028 else
3029 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00003030 }
3031 }
3032}
3033
Abramo Bagnara60804e12011-03-18 15:16:37 +00003034void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
3035 unsigned NumTPLists,
3036 TemplateParameterList **TPLists) {
3037 assert(NumTPLists > 0);
3038 // Make sure the extended decl info is allocated.
3039 if (!hasExtInfo())
3040 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00003041 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003042 // Set the template parameter lists info.
3043 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
3044}
3045
Ted Kremenek21475702008-09-05 17:16:31 +00003046//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003047// EnumDecl Implementation
3048//===----------------------------------------------------------------------===//
3049
David Blaikie68e081d2011-12-20 02:48:34 +00003050void EnumDecl::anchor() { }
3051
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003052EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3053 SourceLocation StartLoc, SourceLocation IdLoc,
3054 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003055 EnumDecl *PrevDecl, bool IsScoped,
3056 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003057 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003058 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003059 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00003060 C.getTypeDeclType(Enum, PrevDecl);
3061 return Enum;
3062}
3063
Douglas Gregor72172e92012-01-05 21:55:30 +00003064EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3065 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003066 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
3067 0, 0, false, false, false);
3068 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3069 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003070}
3071
Douglas Gregord5058122010-02-11 01:19:42 +00003072void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00003073 QualType NewPromotionType,
3074 unsigned NumPositiveBits,
3075 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00003076 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00003077 if (!IntegerType)
3078 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00003079 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00003080 setNumPositiveBits(NumPositiveBits);
3081 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00003082 TagDecl::completeDefinition();
3083}
3084
Richard Smith7d137e32012-03-23 03:33:32 +00003085TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3086 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3087 return MSI->getTemplateSpecializationKind();
3088
3089 return TSK_Undeclared;
3090}
3091
3092void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3093 SourceLocation PointOfInstantiation) {
3094 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3095 assert(MSI && "Not an instantiated member enumeration?");
3096 MSI->setTemplateSpecializationKind(TSK);
3097 if (TSK != TSK_ExplicitSpecialization &&
3098 PointOfInstantiation.isValid() &&
3099 MSI->getPointOfInstantiation().isInvalid())
3100 MSI->setPointOfInstantiation(PointOfInstantiation);
3101}
3102
Richard Smith4b38ded2012-03-14 23:13:10 +00003103EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3104 if (SpecializationInfo)
3105 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3106
3107 return 0;
3108}
3109
3110void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3111 TemplateSpecializationKind TSK) {
3112 assert(!SpecializationInfo && "Member enum is already a specialization");
3113 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3114}
3115
Sebastian Redl833ef452010-01-26 22:01:41 +00003116//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00003117// RecordDecl Implementation
3118//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00003119
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003120RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
3121 SourceLocation StartLoc, SourceLocation IdLoc,
3122 IdentifierInfo *Id, RecordDecl *PrevDecl)
3123 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00003124 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003125 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003126 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003127 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003128 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00003129 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00003130}
3131
Jay Foad39c79802011-01-12 09:06:06 +00003132RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003133 SourceLocation StartLoc, SourceLocation IdLoc,
3134 IdentifierInfo *Id, RecordDecl* PrevDecl) {
3135 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
3136 PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003137 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3138
Ted Kremenek21475702008-09-05 17:16:31 +00003139 C.getTypeDeclType(R, PrevDecl);
3140 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00003141}
3142
Douglas Gregor72172e92012-01-05 21:55:30 +00003143RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
3144 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003145 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
3146 SourceLocation(), 0, 0);
3147 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3148 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003149}
3150
Douglas Gregordfcad112009-03-25 15:59:44 +00003151bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00003152 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00003153 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3154}
3155
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003156RecordDecl::field_iterator RecordDecl::field_begin() const {
3157 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3158 LoadFieldsFromExternalStorage();
3159
3160 return field_iterator(decl_iterator(FirstDecl));
3161}
3162
Douglas Gregorb11aad82011-02-19 18:51:44 +00003163/// completeDefinition - Notes that the definition of this type is now
3164/// complete.
3165void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00003166 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00003167 TagDecl::completeDefinition();
3168}
3169
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003170/// isMsStruct - Get whether or not this record uses ms_struct layout.
3171/// This which can be turned on with an attribute, pragma, or the
3172/// -mms-bitfields command-line option.
3173bool RecordDecl::isMsStruct(const ASTContext &C) const {
3174 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
3175}
3176
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003177static bool isFieldOrIndirectField(Decl::Kind K) {
3178 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3179}
3180
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003181void RecordDecl::LoadFieldsFromExternalStorage() const {
3182 ExternalASTSource *Source = getASTContext().getExternalSource();
3183 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3184
3185 // Notify that we have a RecordDecl doing some initialization.
3186 ExternalASTSource::Deserializing TheFields(Source);
3187
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003188 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003189 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003190 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
3191 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003192 case ELR_Success:
3193 break;
3194
3195 case ELR_AlreadyLoaded:
3196 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003197 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003198 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003199
3200#ifndef NDEBUG
3201 // Check that all decls we got were FieldDecls.
3202 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003203 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003204#endif
3205
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003206 if (Decls.empty())
3207 return;
3208
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00003209 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
3210 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003211}
3212
Steve Naroff415d3d52008-10-08 17:01:13 +00003213//===----------------------------------------------------------------------===//
3214// BlockDecl Implementation
3215//===----------------------------------------------------------------------===//
3216
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003217void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00003218 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00003219
Steve Naroffc4b30e52009-03-13 16:56:44 +00003220 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00003221 if (!NewParamInfo.empty()) {
3222 NumParams = NewParamInfo.size();
3223 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3224 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003225 }
3226}
3227
John McCall351762c2011-02-07 10:33:21 +00003228void BlockDecl::setCaptures(ASTContext &Context,
3229 const Capture *begin,
3230 const Capture *end,
3231 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00003232 CapturesCXXThis = capturesCXXThis;
3233
3234 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00003235 NumCaptures = 0;
3236 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00003237 return;
3238 }
3239
John McCall351762c2011-02-07 10:33:21 +00003240 NumCaptures = end - begin;
3241
3242 // Avoid new Capture[] because we don't want to provide a default
3243 // constructor.
3244 size_t allocationSize = NumCaptures * sizeof(Capture);
3245 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
3246 memcpy(buffer, begin, allocationSize);
3247 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003248}
Sebastian Redl833ef452010-01-26 22:01:41 +00003249
John McCallce45f882011-06-15 22:51:16 +00003250bool BlockDecl::capturesVariable(const VarDecl *variable) const {
3251 for (capture_const_iterator
3252 i = capture_begin(), e = capture_end(); i != e; ++i)
3253 // Only auto vars can be captured, so no redeclaration worries.
3254 if (i->getVariable() == variable)
3255 return true;
3256
3257 return false;
3258}
3259
Douglas Gregor70226da2010-12-21 16:27:07 +00003260SourceRange BlockDecl::getSourceRange() const {
3261 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
3262}
Sebastian Redl833ef452010-01-26 22:01:41 +00003263
3264//===----------------------------------------------------------------------===//
3265// Other Decl Allocation/Deallocation Method Implementations
3266//===----------------------------------------------------------------------===//
3267
David Blaikie68e081d2011-12-20 02:48:34 +00003268void TranslationUnitDecl::anchor() { }
3269
Sebastian Redl833ef452010-01-26 22:01:41 +00003270TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
3271 return new (C) TranslationUnitDecl(C);
3272}
3273
David Blaikie68e081d2011-12-20 02:48:34 +00003274void LabelDecl::anchor() { }
3275
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003276LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00003277 SourceLocation IdentL, IdentifierInfo *II) {
3278 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
3279}
3280
3281LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3282 SourceLocation IdentL, IdentifierInfo *II,
3283 SourceLocation GnuLabelL) {
3284 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3285 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003286}
3287
Douglas Gregor72172e92012-01-05 21:55:30 +00003288LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3289 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3290 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00003291}
3292
David Blaikie68e081d2011-12-20 02:48:34 +00003293void ValueDecl::anchor() { }
3294
Benjamin Kramerea70eb32012-12-01 15:09:41 +00003295bool ValueDecl::isWeak() const {
3296 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3297 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3298 return true;
3299
3300 return isWeakImported();
3301}
3302
David Blaikie68e081d2011-12-20 02:48:34 +00003303void ImplicitParamDecl::anchor() { }
3304
Sebastian Redl833ef452010-01-26 22:01:41 +00003305ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003306 SourceLocation IdLoc,
3307 IdentifierInfo *Id,
3308 QualType Type) {
3309 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00003310}
3311
Douglas Gregor72172e92012-01-05 21:55:30 +00003312ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3313 unsigned ID) {
3314 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3315 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3316}
3317
Sebastian Redl833ef452010-01-26 22:01:41 +00003318FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003319 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003320 const DeclarationNameInfo &NameInfo,
3321 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003322 StorageClass SC,
Douglas Gregorff76cb92010-12-09 16:59:22 +00003323 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00003324 bool hasWrittenPrototype,
3325 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003326 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003327 T, TInfo, SC,
Richard Smitha77a0a62011-08-15 21:04:07 +00003328 isInlineSpecified,
3329 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00003330 New->HasWrittenPrototype = hasWrittenPrototype;
3331 return New;
3332}
3333
Douglas Gregor72172e92012-01-05 21:55:30 +00003334FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3335 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3336 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3337 DeclarationNameInfo(), QualType(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003338 SC_None, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00003339}
3340
Sebastian Redl833ef452010-01-26 22:01:41 +00003341BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3342 return new (C) BlockDecl(DC, L);
3343}
3344
Douglas Gregor72172e92012-01-05 21:55:30 +00003345BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3346 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3347 return new (Mem) BlockDecl(0, SourceLocation());
3348}
3349
John McCall5e77d762013-04-16 07:28:30 +00003350MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3351 unsigned ID) {
3352 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(MSPropertyDecl));
3353 return new (Mem) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
3354 QualType(), 0, SourceLocation(),
3355 0, 0);
3356}
3357
Ben Langmuir37943a72013-05-03 19:00:33 +00003358CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
3359 unsigned NumParams) {
Ben Langmuirce914fc2013-05-03 19:20:19 +00003360 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
Ben Langmuir37943a72013-05-03 19:00:33 +00003361 return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003362}
3363
Ben Langmuirce914fc2013-05-03 19:20:19 +00003364CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3365 unsigned NumParams) {
3366 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3367 void *Mem = AllocateDeserializedDecl(C, ID, Size);
3368 return new (Mem) CapturedDecl(0, NumParams);
3369}
3370
Sebastian Redl833ef452010-01-26 22:01:41 +00003371EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3372 SourceLocation L,
3373 IdentifierInfo *Id, QualType T,
3374 Expr *E, const llvm::APSInt &V) {
3375 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3376}
3377
Douglas Gregor72172e92012-01-05 21:55:30 +00003378EnumConstantDecl *
3379EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3380 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3381 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3382 llvm::APSInt());
3383}
3384
David Blaikie68e081d2011-12-20 02:48:34 +00003385void IndirectFieldDecl::anchor() { }
3386
Benjamin Kramer39593702010-11-21 14:11:41 +00003387IndirectFieldDecl *
3388IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3389 IdentifierInfo *Id, QualType T, NamedDecl **CH,
3390 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003391 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3392}
3393
Douglas Gregor72172e92012-01-05 21:55:30 +00003394IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3395 unsigned ID) {
3396 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3397 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3398 QualType(), 0, 0);
3399}
3400
Douglas Gregorbe996932010-09-01 20:41:53 +00003401SourceRange EnumConstantDecl::getSourceRange() const {
3402 SourceLocation End = getLocation();
3403 if (Init)
3404 End = Init->getLocEnd();
3405 return SourceRange(getLocation(), End);
3406}
3407
David Blaikie68e081d2011-12-20 02:48:34 +00003408void TypeDecl::anchor() { }
3409
Sebastian Redl833ef452010-01-26 22:01:41 +00003410TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003411 SourceLocation StartLoc, SourceLocation IdLoc,
3412 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3413 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003414}
3415
David Blaikie68e081d2011-12-20 02:48:34 +00003416void TypedefNameDecl::anchor() { }
3417
Douglas Gregor72172e92012-01-05 21:55:30 +00003418TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3419 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3420 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3421}
3422
Richard Smithdda56e42011-04-15 14:24:37 +00003423TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3424 SourceLocation StartLoc,
3425 SourceLocation IdLoc, IdentifierInfo *Id,
3426 TypeSourceInfo *TInfo) {
3427 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3428}
3429
Douglas Gregor72172e92012-01-05 21:55:30 +00003430TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3431 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3432 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3433}
3434
Abramo Bagnaraea947882011-03-08 16:41:52 +00003435SourceRange TypedefDecl::getSourceRange() const {
3436 SourceLocation RangeEnd = getLocation();
3437 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3438 if (typeIsPostfix(TInfo->getType()))
3439 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3440 }
3441 return SourceRange(getLocStart(), RangeEnd);
3442}
3443
Richard Smithdda56e42011-04-15 14:24:37 +00003444SourceRange TypeAliasDecl::getSourceRange() const {
3445 SourceLocation RangeEnd = getLocStart();
3446 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3447 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3448 return SourceRange(getLocStart(), RangeEnd);
3449}
3450
David Blaikie68e081d2011-12-20 02:48:34 +00003451void FileScopeAsmDecl::anchor() { }
3452
Sebastian Redl833ef452010-01-26 22:01:41 +00003453FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003454 StringLiteral *Str,
3455 SourceLocation AsmLoc,
3456 SourceLocation RParenLoc) {
3457 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003458}
Douglas Gregorba345522011-12-02 23:23:56 +00003459
Douglas Gregor72172e92012-01-05 21:55:30 +00003460FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3461 unsigned ID) {
3462 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3463 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3464}
3465
Michael Han84324352013-02-22 17:15:32 +00003466void EmptyDecl::anchor() {}
3467
3468EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3469 return new (C) EmptyDecl(DC, L);
3470}
3471
3472EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3473 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EmptyDecl));
3474 return new (Mem) EmptyDecl(0, SourceLocation());
3475}
3476
Douglas Gregorba345522011-12-02 23:23:56 +00003477//===----------------------------------------------------------------------===//
3478// ImportDecl Implementation
3479//===----------------------------------------------------------------------===//
3480
3481/// \brief Retrieve the number of module identifiers needed to name the given
3482/// module.
3483static unsigned getNumModuleIdentifiers(Module *Mod) {
3484 unsigned Result = 1;
3485 while (Mod->Parent) {
3486 Mod = Mod->Parent;
3487 ++Result;
3488 }
3489 return Result;
3490}
3491
Douglas Gregor22d09742012-01-03 18:04:46 +00003492ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003493 Module *Imported,
3494 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003495 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003496 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003497{
3498 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3499 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3500 memcpy(StoredLocs, IdentifierLocs.data(),
3501 IdentifierLocs.size() * sizeof(SourceLocation));
3502}
3503
Douglas Gregor22d09742012-01-03 18:04:46 +00003504ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003505 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003506 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003507 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003508{
3509 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3510}
3511
3512ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003513 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003514 ArrayRef<SourceLocation> IdentifierLocs) {
3515 void *Mem = C.Allocate(sizeof(ImportDecl) +
3516 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003517 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003518}
3519
3520ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003521 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003522 Module *Imported,
3523 SourceLocation EndLoc) {
3524 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003525 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003526 Import->setImplicit();
3527 return Import;
3528}
3529
Douglas Gregor72172e92012-01-05 21:55:30 +00003530ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3531 unsigned NumLocations) {
3532 void *Mem = AllocateDeserializedDecl(C, ID,
3533 (sizeof(ImportDecl) +
3534 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003535 return new (Mem) ImportDecl(EmptyShell());
3536}
3537
3538ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3539 if (!ImportedAndComplete.getInt())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00003540 return None;
Douglas Gregorba345522011-12-02 23:23:56 +00003541
3542 const SourceLocation *StoredLocs
3543 = reinterpret_cast<const SourceLocation *>(this + 1);
3544 return ArrayRef<SourceLocation>(StoredLocs,
3545 getNumModuleIdentifiers(getImportedModule()));
3546}
3547
3548SourceRange ImportDecl::getSourceRange() const {
3549 if (!ImportedAndComplete.getInt())
3550 return SourceRange(getLocation(),
3551 *reinterpret_cast<const SourceLocation *>(this + 1));
3552
3553 return SourceRange(getLocation(), getIdentifierLocs().back());
3554}