blob: 2137075138fa2f6c73ba07fc377b27f2674e771f [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"
George Burgess IV99db3ea2017-08-09 04:02:49 +000015#include "Linkage.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000016#include "clang/AST/ASTContext.h"
Faisal Valib90b2112014-04-03 16:32:21 +000017#include "clang/AST/ASTLambda.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000018#include "clang/AST/ASTMutationListener.h"
19#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000022#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/AST/DeclTemplate.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000024#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000026#include "clang/AST/PrettyPrinter.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000027#include "clang/AST/Stmt.h"
28#include "clang/AST/TypeLoc.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000029#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000030#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000031#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000032#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000033#include "clang/Basic/TargetInfo.h"
Kostya Serebryany293dc9b2014-10-16 20:54:52 +000034#include "clang/Frontend/FrontendDiagnostic.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000035#include "llvm/Support/ErrorHandling.h"
David Blaikie9c70e042011-09-21 18:16:56 +000036#include <algorithm>
37
Chris Lattner6d9a6852006-10-25 05:11:20 +000038using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000039
Richard Smith0b87e072013-10-07 08:02:11 +000040Decl *clang::getPrimaryMergedDecl(Decl *D) {
41 return D->getASTContext().getPrimaryMergedDecl(D);
42}
43
Richard Smith73b21d82014-09-03 02:33:22 +000044// Defined here so that it can be inlined into its direct callers.
45bool Decl::isOutOfLine() const {
46 return !getLexicalDeclContext()->Equals(getDeclContext());
47}
48
Richard Smith42413142015-05-15 20:05:43 +000049TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx)
50 : Decl(TranslationUnit, nullptr, SourceLocation()),
Richard Smith26342f92017-05-17 00:24:14 +000051 DeclContext(TranslationUnit), Ctx(ctx), AnonymousNamespace(nullptr) {}
Richard Smith42413142015-05-15 20:05:43 +000052
Chris Lattner88f70d62008-03-15 05:43:15 +000053//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000054// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000055//===----------------------------------------------------------------------===//
56
John McCalldf25c432013-02-16 00:17:33 +000057// Visibility rules aren't rigorously externally specified, but here
58// are the basic principles behind what we implement:
59//
60// 1. An explicit visibility attribute is generally a direct expression
61// of the user's intent and should be honored. Only the innermost
62// visibility attribute applies. If no visibility attribute applies,
63// global visibility settings are considered.
64//
65// 2. There is one caveat to the above: on or in a template pattern,
66// an explicit visibility attribute is just a default rule, and
67// visibility can be decreased by the visibility of template
68// arguments. But this, too, has an exception: an attribute on an
69// explicit specialization or instantiation causes all the visibility
70// restrictions of the template arguments to be ignored.
71//
72// 3. A variable that does not otherwise have explicit visibility can
73// be restricted by the visibility of its type.
74//
75// 4. A visibility restriction is explicit if it comes from an
76// attribute (or something like it), not a global visibility setting.
77// When emitting a reference to an external symbol, visibility
78// restrictions are ignored unless they are explicit.
John McCalld041a9b2013-02-20 01:54:26 +000079//
80// 5. When computing the visibility of a non-type, including a
81// non-type member of a class, only non-type visibility restrictions
82// are considered: the 'visibility' attribute, global value-visibility
83// settings, and a few special cases like __private_extern.
84//
85// 6. When computing the visibility of a type, including a type member
86// of a class, only type visibility restrictions are considered:
87// the 'type_visibility' attribute and global type-visibility settings.
88// However, a 'visibility' attribute counts as a 'type_visibility'
89// attribute on any declaration that only has the former.
90//
91// The visibility of a "secondary" entity, like a template argument,
92// is computed using the kind of that entity, not the kind of the
93// primary entity for which we are computing visibility. For example,
94// the visibility of a specialization of either of these templates:
95// template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X);
96// template <class T, bool (&compare)(T, X)> class matcher;
97// is restricted according to the type visibility of the argument 'T',
98// the type visibility of 'bool(&)(T,X)', and the value visibility of
99// the argument function 'compare'. That 'has_match' is a value
100// and 'matcher' is a type only matters when looking for attributes
101// and settings from the immediate context.
John McCalldf25c432013-02-16 00:17:33 +0000102
John McCalld041a9b2013-02-20 01:54:26 +0000103/// Does this computation kind permit us to consider additional
104/// visibility settings from attributes and the like?
105static bool hasExplicitVisibilityAlready(LVComputationKind computation) {
John McCall5f46c482013-02-21 23:42:58 +0000106 return ((unsigned(computation) & IgnoreExplicitVisibilityBit) != 0);
John McCalld041a9b2013-02-20 01:54:26 +0000107}
108
109/// Given an LVComputationKind, return one of the same type/value sort
110/// that records that it already has explicit visibility.
111static LVComputationKind
112withExplicitVisibilityAlready(LVComputationKind oldKind) {
113 LVComputationKind newKind =
John McCall5f46c482013-02-21 23:42:58 +0000114 static_cast<LVComputationKind>(unsigned(oldKind) |
115 IgnoreExplicitVisibilityBit);
John McCalld041a9b2013-02-20 01:54:26 +0000116 assert(oldKind != LVForType || newKind == LVForExplicitType);
117 assert(oldKind != LVForValue || newKind == LVForExplicitValue);
118 assert(oldKind != LVForExplicitType || newKind == LVForExplicitType);
119 assert(oldKind != LVForExplicitValue || newKind == LVForExplicitValue);
120 return newKind;
121}
122
David Blaikie05785d12013-02-20 22:23:23 +0000123static Optional<Visibility> getExplicitVisibility(const NamedDecl *D,
124 LVComputationKind kind) {
John McCalld041a9b2013-02-20 01:54:26 +0000125 assert(!hasExplicitVisibilityAlready(kind) &&
126 "asking for explicit visibility when we shouldn't be");
127 return D->getExplicitVisibility((NamedDecl::ExplicitVisibilityKind) kind);
128}
129
John McCalldf25c432013-02-16 00:17:33 +0000130/// Is the given declaration a "type" or a "value" for the purposes of
131/// visibility computation?
132static bool usesTypeVisibility(const NamedDecl *D) {
John McCallb4a99d32013-02-19 01:57:35 +0000133 return isa<TypeDecl>(D) ||
134 isa<ClassTemplateDecl>(D) ||
135 isa<ObjCInterfaceDecl>(D);
John McCalldf25c432013-02-16 00:17:33 +0000136}
137
John McCall5f46c482013-02-21 23:42:58 +0000138/// Does the given declaration have member specialization information,
139/// and if so, is it an explicit specialization?
140template <class T> static typename
Benjamin Kramered2f4762014-03-07 14:30:23 +0000141std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type
John McCall5f46c482013-02-21 23:42:58 +0000142isExplicitMemberSpecialization(const T *D) {
143 if (const MemberSpecializationInfo *member =
144 D->getMemberSpecializationInfo()) {
145 return member->isExplicitSpecialization();
146 }
147 return false;
148}
149
150/// For templates, this question is easier: a member template can't be
151/// explicitly instantiated, so there's a single bit indicating whether
152/// or not this is an explicit member specialization.
153static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) {
154 return D->isMemberSpecialization();
155}
156
John McCalld041a9b2013-02-20 01:54:26 +0000157/// Given a visibility attribute, return the explicit visibility
158/// associated with it.
159template <class T>
160static Visibility getVisibilityFromAttr(const T *attr) {
161 switch (attr->getVisibility()) {
162 case T::Default:
163 return DefaultVisibility;
164 case T::Hidden:
165 return HiddenVisibility;
166 case T::Protected:
167 return ProtectedVisibility;
168 }
169 llvm_unreachable("bad visibility kind");
170}
171
John McCalldf25c432013-02-16 00:17:33 +0000172/// Return the explicit visibility of the given declaration.
David Blaikie05785d12013-02-20 22:23:23 +0000173static Optional<Visibility> getVisibilityOf(const NamedDecl *D,
John McCalld041a9b2013-02-20 01:54:26 +0000174 NamedDecl::ExplicitVisibilityKind kind) {
175 // If we're ultimately computing the visibility of a type, look for
176 // a 'type_visibility' attribute before looking for 'visibility'.
177 if (kind == NamedDecl::VisibilityForType) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000178 if (const auto *A = D->getAttr<TypeVisibilityAttr>()) {
John McCalld041a9b2013-02-20 01:54:26 +0000179 return getVisibilityFromAttr(A);
180 }
181 }
182
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000183 // If this declaration has an explicit visibility attribute, use it.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000184 if (const auto *A = D->getAttr<VisibilityAttr>()) {
John McCalld041a9b2013-02-20 01:54:26 +0000185 return getVisibilityFromAttr(A);
John McCall457a04e2010-10-22 21:05:15 +0000186 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000187
David Blaikie7a30dc52013-02-21 01:47:18 +0000188 return None;
John McCall457a04e2010-10-22 21:05:15 +0000189}
190
George Burgess IV99db3ea2017-08-09 04:02:49 +0000191LinkageInfo LinkageComputer::getLVForType(const Type &T,
192 LVComputationKind computation) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000193 if (computation == LVForLinkageOnly)
194 return LinkageInfo(T.getLinkage(), DefaultVisibility, true);
George Burgess IV35cb4f82017-08-09 04:12:17 +0000195 return getTypeLinkageAndVisibility(&T);
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000196}
197
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000198/// \brief Get the most restrictive linkage for the types in the given
John McCalldf25c432013-02-16 00:17:33 +0000199/// template parameter list. For visibility purposes, template
200/// parameters are part of the signature of a template.
George Burgess IV99db3ea2017-08-09 04:02:49 +0000201LinkageInfo LinkageComputer::getLVForTemplateParameterList(
202 const TemplateParameterList *Params, LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000203 LinkageInfo LV;
David Majnemerc7b85c42014-04-23 05:16:48 +0000204 for (const NamedDecl *P : *Params) {
John McCalldf25c432013-02-16 00:17:33 +0000205 // Template type parameters are the most common and never
206 // contribute to visibility, pack or not.
David Majnemerc7b85c42014-04-23 05:16:48 +0000207 if (isa<TemplateTypeParmDecl>(P))
John McCalldf25c432013-02-16 00:17:33 +0000208 continue;
209
210 // Non-type template parameters can be restricted by the value type, e.g.
211 // template <enum X> class A { ... };
212 // We have to be careful here, though, because we can be dealing with
213 // dependent types.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000214 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
John McCalldf25c432013-02-16 00:17:33 +0000215 // Handle the non-pack case first.
216 if (!NTTP->isExpandedParameterPack()) {
217 if (!NTTP->getType()->isDependentType()) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000218 LV.merge(getLVForType(*NTTP->getType(), computation));
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000219 }
220 continue;
221 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000222
John McCalldf25c432013-02-16 00:17:33 +0000223 // Look at all the types in an expanded pack.
224 for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) {
225 QualType type = NTTP->getExpansionType(i);
226 if (!type->isDependentType())
George Burgess IV35cb4f82017-08-09 04:12:17 +0000227 LV.merge(getTypeLinkageAndVisibility(type));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000228 }
John McCalldf25c432013-02-16 00:17:33 +0000229 continue;
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000230 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000231
John McCalldf25c432013-02-16 00:17:33 +0000232 // Template template parameters can be restricted by their
233 // template parameters, recursively.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000234 const auto *TTP = cast<TemplateTemplateParmDecl>(P);
John McCalldf25c432013-02-16 00:17:33 +0000235
236 // Handle the non-pack case first.
237 if (!TTP->isExpandedParameterPack()) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000238 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(),
239 computation));
John McCalldf25c432013-02-16 00:17:33 +0000240 continue;
241 }
242
243 // Look at all expansions in an expanded pack.
244 for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters();
245 i != n; ++i) {
246 LV.merge(getLVForTemplateParameterList(
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000247 TTP->getExpansionTemplateParameters(i), computation));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000248 }
249 }
250
John McCall457a04e2010-10-22 21:05:15 +0000251 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000252}
253
Eli Friedman7e346a82013-07-01 20:22:57 +0000254static const Decl *getOutermostFuncOrBlockContext(const Decl *D) {
Craig Topper36250ad2014-05-12 05:36:57 +0000255 const Decl *Ret = nullptr;
Rafael Espindolac1b38a22013-05-16 04:30:21 +0000256 const DeclContext *DC = D->getDeclContext();
257 while (DC->getDeclKind() != Decl::TranslationUnit) {
Eli Friedman7e346a82013-07-01 20:22:57 +0000258 if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC))
259 Ret = cast<Decl>(DC);
Rafael Espindolac1b38a22013-05-16 04:30:21 +0000260 DC = DC->getParent();
261 }
262 return Ret;
263}
264
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000265/// \brief Get the most restrictive linkage for the types and
266/// declarations in the given template argument list.
John McCalldf25c432013-02-16 00:17:33 +0000267///
268/// Note that we don't take an LVComputationKind because we always
269/// want to honor the visibility of template arguments in the same way.
George Burgess IV99db3ea2017-08-09 04:02:49 +0000270LinkageInfo
271LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args,
272 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000273 LinkageInfo LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000274
David Majnemerc7b85c42014-04-23 05:16:48 +0000275 for (const TemplateArgument &Arg : Args) {
276 switch (Arg.getKind()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000277 case TemplateArgument::Null:
278 case TemplateArgument::Integral:
279 case TemplateArgument::Expression:
John McCalldf25c432013-02-16 00:17:33 +0000280 continue;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000281
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000282 case TemplateArgument::Type:
David Majnemerc7b85c42014-04-23 05:16:48 +0000283 LV.merge(getLVForType(*Arg.getAsType(), computation));
John McCalldf25c432013-02-16 00:17:33 +0000284 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000285
286 case TemplateArgument::Declaration:
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000287 if (const auto *ND = dyn_cast<NamedDecl>(Arg.getAsDecl())) {
John McCalldf25c432013-02-16 00:17:33 +0000288 assert(!usesTypeVisibility(ND));
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000289 LV.merge(getLVForDecl(ND, computation));
John McCalldf25c432013-02-16 00:17:33 +0000290 }
291 continue;
Eli Friedmanb826a002012-09-26 02:36:12 +0000292
293 case TemplateArgument::NullPtr:
George Burgess IV35cb4f82017-08-09 04:12:17 +0000294 LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType()));
John McCalldf25c432013-02-16 00:17:33 +0000295 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000296
297 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000298 case TemplateArgument::TemplateExpansion:
David Majnemerc7b85c42014-04-23 05:16:48 +0000299 if (TemplateDecl *Template =
300 Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000301 LV.merge(getLVForDecl(Template, computation));
John McCalldf25c432013-02-16 00:17:33 +0000302 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000303
304 case TemplateArgument::Pack:
David Majnemerc7b85c42014-04-23 05:16:48 +0000305 LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation));
John McCalldf25c432013-02-16 00:17:33 +0000306 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000307 }
John McCalldf25c432013-02-16 00:17:33 +0000308 llvm_unreachable("bad template argument kind");
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000309 }
310
John McCall457a04e2010-10-22 21:05:15 +0000311 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000312}
313
George Burgess IV99db3ea2017-08-09 04:02:49 +0000314LinkageInfo
315LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
316 LVComputationKind computation) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000317 return getLVForTemplateArgumentList(TArgs.asArray(), computation);
John McCall8823c652010-08-13 08:35:10 +0000318}
319
John McCall5f46c482013-02-21 23:42:58 +0000320static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
321 const FunctionTemplateSpecializationInfo *specInfo) {
322 // Include visibility from the template parameters and arguments
323 // only if this is not an explicit instantiation or specialization
324 // with direct explicit visibility. (Implicit instantiations won't
325 // have a direct attribute.)
326 if (!specInfo->isExplicitInstantiationOrSpecialization())
327 return true;
328
329 return !fn->hasAttr<VisibilityAttr>();
330}
331
John McCalldf25c432013-02-16 00:17:33 +0000332/// Merge in template-related linkage and visibility for the given
333/// function template specialization.
334///
335/// We don't need a computation kind here because we can assume
336/// LVForValue.
John McCall5f46c482013-02-21 23:42:58 +0000337///
NAKAMURA Takumi62eae082013-02-22 04:06:28 +0000338/// \param[out] LV the computation to use for the parent
George Burgess IV99db3ea2017-08-09 04:02:49 +0000339void LinkageComputer::mergeTemplateLV(
340 LinkageInfo &LV, const FunctionDecl *fn,
341 const FunctionTemplateSpecializationInfo *specInfo,
342 LVComputationKind computation) {
John McCall5f46c482013-02-21 23:42:58 +0000343 bool considerVisibility =
344 shouldConsiderTemplateVisibility(fn, specInfo);
John McCalldf25c432013-02-16 00:17:33 +0000345
346 // Merge information from the template parameters.
John McCall5f46c482013-02-21 23:42:58 +0000347 FunctionTemplateDecl *temp = specInfo->getTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000348 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000349 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-02-16 00:17:33 +0000350 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
351
352 // Merge information from the template arguments.
353 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000354 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
John McCalldf25c432013-02-16 00:17:33 +0000355 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000356}
357
John McCall5f46c482013-02-21 23:42:58 +0000358/// Does the given declaration have a direct visibility attribute
359/// that would match the given rules?
360static bool hasDirectVisibilityAttribute(const NamedDecl *D,
361 LVComputationKind computation) {
362 switch (computation) {
363 case LVForType:
364 case LVForExplicitType:
365 if (D->hasAttr<TypeVisibilityAttr>())
366 return true;
367 // fallthrough
368 case LVForValue:
369 case LVForExplicitValue:
370 if (D->hasAttr<VisibilityAttr>())
371 return true;
372 return false;
Rafael Espindola9551d3b2013-05-28 19:43:11 +0000373 case LVForLinkageOnly:
374 return false;
John McCall5f46c482013-02-21 23:42:58 +0000375 }
376 llvm_unreachable("bad visibility computation kind");
377}
378
John McCalld041a9b2013-02-20 01:54:26 +0000379/// Should we consider visibility associated with the template
380/// arguments and parameters of the given class template specialization?
381static bool shouldConsiderTemplateVisibility(
382 const ClassTemplateSpecializationDecl *spec,
383 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000384 // Include visibility from the template parameters and arguments
385 // only if this is not an explicit instantiation or specialization
386 // with direct explicit visibility (and note that implicit
387 // instantiations won't have a direct attribute).
388 //
389 // Furthermore, we want to ignore template parameters and arguments
John McCalld041a9b2013-02-20 01:54:26 +0000390 // for an explicit specialization when computing the visibility of a
391 // member thereof with explicit visibility.
John McCalldf25c432013-02-16 00:17:33 +0000392 //
393 // This is a bit complex; let's unpack it.
394 //
395 // An explicit class specialization is an independent, top-level
396 // declaration. As such, if it or any of its members has an
397 // explicit visibility attribute, that must directly express the
398 // user's intent, and we should honor it. The same logic applies to
399 // an explicit instantiation of a member of such a thing.
John McCalld041a9b2013-02-20 01:54:26 +0000400
401 // Fast path: if this is not an explicit instantiation or
402 // specialization, we always want to consider template-related
403 // visibility restrictions.
404 if (!spec->isExplicitInstantiationOrSpecialization())
405 return true;
406
407 // This is the 'member thereof' check.
408 if (spec->isExplicitSpecialization() &&
409 hasExplicitVisibilityAlready(computation))
410 return false;
411
John McCall5f46c482013-02-21 23:42:58 +0000412 return !hasDirectVisibilityAttribute(spec, computation);
John McCalld041a9b2013-02-20 01:54:26 +0000413}
414
415/// Merge in template-related linkage and visibility for the given
416/// class template specialization.
George Burgess IV99db3ea2017-08-09 04:02:49 +0000417void LinkageComputer::mergeTemplateLV(
418 LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec,
419 LVComputationKind computation) {
John McCalld041a9b2013-02-20 01:54:26 +0000420 bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
John McCalldf25c432013-02-16 00:17:33 +0000421
422 // Merge information from the template parameters, but ignore
423 // visibility if we're only considering template arguments.
424
John McCalld041a9b2013-02-20 01:54:26 +0000425 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000426 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000427 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-02-16 00:17:33 +0000428 LV.mergeMaybeWithVisibility(tempLV,
John McCalld041a9b2013-02-20 01:54:26 +0000429 considerVisibility && !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000430
431 // Merge information from the template arguments. We ignore
432 // template-argument visibility if we've got an explicit
433 // instantiation with a visibility attribute.
434 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000435 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
Rafael Espindola503276b2013-05-30 21:23:15 +0000436 if (considerVisibility)
437 LV.mergeVisibility(argsLV);
438 LV.mergeExternalVisibility(argsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000439}
440
Larisse Voufo5899e192014-07-02 23:08:34 +0000441/// Should we consider visibility associated with the template
442/// arguments and parameters of the given variable template
443/// specialization? As usual, follow class template specialization
444/// logic up to initialization.
445static bool shouldConsiderTemplateVisibility(
446 const VarTemplateSpecializationDecl *spec,
447 LVComputationKind computation) {
448 // Include visibility from the template parameters and arguments
449 // only if this is not an explicit instantiation or specialization
450 // with direct explicit visibility (and note that implicit
451 // instantiations won't have a direct attribute).
452 if (!spec->isExplicitInstantiationOrSpecialization())
453 return true;
454
455 // An explicit variable specialization is an independent, top-level
456 // declaration. As such, if it has an explicit visibility attribute,
457 // that must directly express the user's intent, and we should honor
458 // it.
459 if (spec->isExplicitSpecialization() &&
460 hasExplicitVisibilityAlready(computation))
461 return false;
462
463 return !hasDirectVisibilityAttribute(spec, computation);
464}
465
466/// Merge in template-related linkage and visibility for the given
467/// variable template specialization. As usual, follow class template
468/// specialization logic up to initialization.
George Burgess IV99db3ea2017-08-09 04:02:49 +0000469void LinkageComputer::mergeTemplateLV(LinkageInfo &LV,
470 const VarTemplateSpecializationDecl *spec,
471 LVComputationKind computation) {
Larisse Voufo5899e192014-07-02 23:08:34 +0000472 bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
473
474 // Merge information from the template parameters, but ignore
475 // visibility if we're only considering template arguments.
476
477 VarTemplateDecl *temp = spec->getSpecializedTemplate();
478 LinkageInfo tempLV =
479 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
480 LV.mergeMaybeWithVisibility(tempLV,
481 considerVisibility && !hasExplicitVisibilityAlready(computation));
482
483 // Merge information from the template arguments. We ignore
484 // template-argument visibility if we've got an explicit
485 // instantiation with a visibility attribute.
486 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
487 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
488 if (considerVisibility)
489 LV.mergeVisibility(argsLV);
490 LV.mergeExternalVisibility(argsLV);
491}
492
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000493static bool useInlineVisibilityHidden(const NamedDecl *D) {
494 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000495 const LangOptions &Opts = D->getASTContext().getLangOpts();
496 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000497 return false;
498
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000499 const auto *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000500 if (!FD)
501 return false;
502
503 TemplateSpecializationKind TSK = TSK_Undeclared;
504 if (FunctionTemplateSpecializationInfo *spec
505 = FD->getTemplateSpecializationInfo()) {
506 TSK = spec->getTemplateSpecializationKind();
507 } else if (MemberSpecializationInfo *MSI =
508 FD->getMemberSpecializationInfo()) {
509 TSK = MSI->getTemplateSpecializationKind();
510 }
511
Craig Topper36250ad2014-05-12 05:36:57 +0000512 const FunctionDecl *Def = nullptr;
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000513 // InlineVisibilityHidden only applies to definitions, and
514 // isInlined() only gives meaningful answers on definitions
515 // anyway.
516 return TSK != TSK_ExplicitInstantiationDeclaration &&
517 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000518 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000519}
520
Rafael Espindola593537a2013-05-05 20:15:21 +0000521template <typename T> static bool isFirstInExternCContext(T *D) {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000522 const T *First = D->getFirstDecl();
Rafael Espindola593537a2013-05-05 20:15:21 +0000523 return First->isInExternCContext();
Rafael Espindolaf4187652013-02-14 01:18:37 +0000524}
525
Richard Smith03c05032014-02-17 23:34:47 +0000526static bool isSingleLineLanguageLinkage(const Decl &D) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000527 if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
Richard Smith03c05032014-02-17 23:34:47 +0000528 if (!SD->hasBraces())
Rafael Espindola327be3c2013-04-26 01:30:23 +0000529 return true;
530 return false;
531}
532
Richard Smith1283e982017-07-07 20:04:28 +0000533static bool isExportedFromModuleIntefaceUnit(const NamedDecl *D) {
534 switch (D->getModuleOwnershipKind()) {
535 case Decl::ModuleOwnershipKind::Unowned:
536 case Decl::ModuleOwnershipKind::ModulePrivate:
537 return false;
538 case Decl::ModuleOwnershipKind::Visible:
539 case Decl::ModuleOwnershipKind::VisibleWhenImported:
540 if (auto *M = D->getOwningModule())
541 return M->Kind == Module::ModuleInterfaceUnit;
542 }
543 llvm_unreachable("unexpected module ownership kind");
544}
545
546static LinkageInfo getInternalLinkageFor(const NamedDecl *D) {
547 // Internal linkage declarations within a module interface unit are modeled
548 // as "module-internal linkage", which means that they have internal linkage
549 // formally but can be indirectly accessed from outside the module via inline
550 // functions and templates defined within the module.
551 if (auto *M = D->getOwningModule())
552 if (M->Kind == Module::ModuleInterfaceUnit)
553 return LinkageInfo(ModuleInternalLinkage, DefaultVisibility, false);
554
555 return LinkageInfo::internal();
556}
557
558static LinkageInfo getExternalLinkageFor(const NamedDecl *D) {
559 // C++ Modules TS [basic.link]/6.8:
560 // - A name declared at namespace scope that does not have internal linkage
561 // by the previous rules and that is introduced by a non-exported
562 // declaration has module linkage.
563 if (auto *M = D->getOwningModule())
564 if (M->Kind == Module::ModuleInterfaceUnit)
565 if (!isExportedFromModuleIntefaceUnit(D))
566 return LinkageInfo(ModuleLinkage, DefaultVisibility, false);
567
568 return LinkageInfo::external();
569}
570
George Burgess IV99db3ea2017-08-09 04:02:49 +0000571LinkageInfo
572LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
573 LVComputationKind computation) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000574 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000575 "Not a name having namespace scope");
576 ASTContext &Context = D->getASTContext();
577
578 // C++ [basic.link]p3:
579 // A name having namespace scope (3.3.6) has internal linkage if it
580 // is the name of
581 // - an object, reference, function or function template that is
582 // explicitly declared static; or,
583 // (This bullet corresponds to C99 6.2.2p3.)
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000584 if (const auto *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000585 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000586 if (Var->getStorageClass() == SC_Static)
Richard Smith1283e982017-07-07 20:04:28 +0000587 return getInternalLinkageFor(Var);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000588
Richard Smith62f19e72016-06-25 00:15:56 +0000589 // - a non-inline, non-volatile object or reference that is explicitly
590 // declared const or constexpr and neither explicitly declared extern
591 // nor previously declared to have external linkage; or (there is no
592 // equivalent in C99)
Richard Smith1283e982017-07-07 20:04:28 +0000593 // The C++ modules TS adds "non-exported" to this list.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000594 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000595 Var->getType().isConstQualified() &&
Richard Smith62f19e72016-06-25 00:15:56 +0000596 !Var->getType().isVolatileQualified() &&
Richard Smith1283e982017-07-07 20:04:28 +0000597 !Var->isInline() &&
598 !isExportedFromModuleIntefaceUnit(Var)) {
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000599 const VarDecl *PrevVar = Var->getPreviousDecl();
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000600 if (PrevVar)
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000601 return getLVForDecl(PrevVar, computation);
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000602
603 if (Var->getStorageClass() != SC_Extern &&
Rafael Espindola327be3c2013-04-26 01:30:23 +0000604 Var->getStorageClass() != SC_PrivateExtern &&
Richard Smith03c05032014-02-17 23:34:47 +0000605 !isSingleLineLanguageLinkage(*Var))
Richard Smith1283e982017-07-07 20:04:28 +0000606 return getInternalLinkageFor(Var);
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000607 }
608
609 for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar;
610 PrevVar = PrevVar->getPreviousDecl()) {
611 if (PrevVar->getStorageClass() == SC_PrivateExtern &&
612 Var->getStorageClass() == SC_None)
George Burgess IV35cb4f82017-08-09 04:12:17 +0000613 return getDeclLinkageAndVisibility(PrevVar);
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000614 // Explicitly declared static.
615 if (PrevVar->getStorageClass() == SC_Static)
Richard Smith1283e982017-07-07 20:04:28 +0000616 return getInternalLinkageFor(Var);
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000617 }
Alp Tokera2794f92014-01-22 07:29:52 +0000618 } else if (const FunctionDecl *Function = D->getAsFunction()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000619 // C++ [temp]p4:
620 // A non-member function template can have internal linkage; any
621 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000622
623 // Explicitly declared static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000624 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
Richard Smith1283e982017-07-07 20:04:28 +0000625 return getInternalLinkageFor(Function);
David Majnemer18fac3b2014-12-10 22:58:14 +0000626 } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) {
627 // - a data member of an anonymous union.
628 const VarDecl *VD = IFD->getVarDecl();
629 assert(VD && "Expected a VarDecl in this IndirectFieldDecl!");
630 return getLVForNamespaceScopeDecl(VD, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000631 }
David Majnemer0eb8bbd2013-10-23 20:52:43 +0000632 assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!");
Douglas Gregorf73b2822009-11-25 22:24:25 +0000633
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000634 if (D->isInAnonymousNamespace()) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000635 const auto *Var = dyn_cast<VarDecl>(D);
636 const auto *Func = dyn_cast<FunctionDecl>(D);
Richard Smith97135cc2015-11-12 22:19:45 +0000637 // FIXME: In C++11 onwards, anonymous namespaces should give decls
Richard Smith1283e982017-07-07 20:04:28 +0000638 // within them (including those inside extern "C" contexts) internal
639 // linkage, not unique external linkage:
640 //
641 // C++11 [basic.link]p4:
642 // An unnamed namespace or a namespace declared directly or indirectly
643 // within an unnamed namespace has internal linkage.
Rafael Espindola593537a2013-05-05 20:15:21 +0000644 if ((!Var || !isFirstInExternCContext(Var)) &&
645 (!Func || !isFirstInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000646 return LinkageInfo::uniqueExternal();
647 }
John McCallb7139c42010-10-28 04:18:25 +0000648
John McCall457a04e2010-10-22 21:05:15 +0000649 // Set up the defaults.
650
651 // C99 6.2.2p5:
652 // If the declaration of an identifier for an object has file
653 // scope and no storage-class specifier, its linkage is
654 // external.
John McCallc273f242010-10-30 11:50:40 +0000655 LinkageInfo LV;
656
John McCalld041a9b2013-02-20 01:54:26 +0000657 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000658 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000659 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000660 } else {
661 // If we're declared in a namespace with a visibility attribute,
John McCalldf25c432013-02-16 00:17:33 +0000662 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindola78158af2012-04-16 18:46:26 +0000663 for (const DeclContext *DC = D->getDeclContext();
664 !isa<TranslationUnitDecl>(DC);
665 DC = DC->getParent()) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000666 const auto *ND = dyn_cast<NamespaceDecl>(DC);
Rafael Espindola78158af2012-04-16 18:46:26 +0000667 if (!ND) continue;
David Blaikie05785d12013-02-20 22:23:23 +0000668 if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000669 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000670 break;
671 }
672 }
673 }
Rafael Espindola78158af2012-04-16 18:46:26 +0000674
John McCalldf25c432013-02-16 00:17:33 +0000675 // Add in global settings if the above didn't give us direct visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000676 if (!LV.isVisibilityExplicit()) {
John McCallb4a99d32013-02-19 01:57:35 +0000677 // Use global type/value visibility as appropriate.
678 Visibility globalVisibility;
679 if (computation == LVForValue) {
Larisse Voufo404e1422015-02-04 02:34:32 +0000680 globalVisibility = Context.getLangOpts().getValueVisibilityMode();
John McCallb4a99d32013-02-19 01:57:35 +0000681 } else {
682 assert(computation == LVForType);
683 globalVisibility = Context.getLangOpts().getTypeVisibilityMode();
684 }
685 LV.mergeVisibility(globalVisibility, /*explicit*/ false);
John McCalldf25c432013-02-16 00:17:33 +0000686
687 // If we're paying attention to global visibility, apply
688 // -finline-visibility-hidden if this is an inline method.
689 if (useInlineVisibilityHidden(D))
690 LV.mergeVisibility(HiddenVisibility, true);
691 }
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000692 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000693
Douglas Gregorf73b2822009-11-25 22:24:25 +0000694 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000695
Douglas Gregorf73b2822009-11-25 22:24:25 +0000696 // A name having namespace scope has external linkage if it is the
697 // name of
698 //
699 // - an object or reference, unless it has internal linkage; or
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000700 if (const auto *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000701 // GCC applies the following optimization to variables and static
702 // data members, but not to functions:
703 //
John McCall457a04e2010-10-22 21:05:15 +0000704 // Modify the variable's LV by the LV of its type unless this is
705 // C or extern "C". This follows from [basic.link]p9:
706 // A type without linkage shall not be used as the type of a
707 // variable or function with external linkage unless
708 // - the entity has C language linkage, or
709 // - the entity is declared within an unnamed namespace, or
710 // - the entity is not used or is defined in the same
711 // translation unit.
712 // and [basic.link]p10:
713 // ...the types specified by all declarations referring to a
714 // given variable or function shall be identical...
715 // C does not have an equivalent rule.
716 //
John McCall5fe84122010-10-26 04:59:26 +0000717 // Ignore this if we've got an explicit attribute; the user
718 // probably knows what they're doing.
719 //
John McCall457a04e2010-10-22 21:05:15 +0000720 // Note that we don't want to make the variable non-external
721 // because of this, but unique-external linkage suits us.
Rafael Espindola593537a2013-05-05 20:15:21 +0000722 if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000723 LinkageInfo TypeLV = getLVForType(*Var->getType(), computation);
Richard Smith1283e982017-07-07 20:04:28 +0000724 if (TypeLV.getLinkage() != ExternalLinkage &&
725 TypeLV.getLinkage() != ModuleLinkage)
John McCallc273f242010-10-30 11:50:40 +0000726 return LinkageInfo::uniqueExternal();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000727 if (!LV.isVisibilityExplicit())
John McCalldf25c432013-02-16 00:17:33 +0000728 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000729 }
730
John McCall23032652010-11-02 18:38:13 +0000731 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000732 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000733
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000734 // Note that Sema::MergeVarDecl already takes care of implementing
735 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
736 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000737
Larisse Voufo5899e192014-07-02 23:08:34 +0000738 // As per function and class template specializations (below),
739 // consider LV for the template and template arguments. We're at file
740 // scope, so we do not need to worry about nested specializations.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000741 if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) {
Larisse Voufo5899e192014-07-02 23:08:34 +0000742 mergeTemplateLV(LV, spec, computation);
743 }
744
Douglas Gregorf73b2822009-11-25 22:24:25 +0000745 // - a function, unless it has internal linkage; or
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000746 } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000747 // In theory, we can modify the function's LV by the LV of its
748 // type unless it has C linkage (see comment above about variables
749 // for justification). In practice, GCC doesn't do this, so it's
750 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000751
John McCall23032652010-11-02 18:38:13 +0000752 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000753 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000754
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000755 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
756 // merging storage classes and visibility attributes, so we don't have to
757 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000758
John McCallf768aa72011-02-10 06:50:24 +0000759 // In C++, then if the type of the function uses a type with
760 // unique-external linkage, it's not legally usable from outside
761 // this translation unit. However, we should use the C linkage
762 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000763 if (Context.getLangOpts().CPlusPlus &&
Richard Smith50f4afc2013-05-12 23:17:59 +0000764 !Function->isInExternCContext()) {
765 // Only look at the type-as-written. If this function has an auto-deduced
766 // return type, we can't compute the linkage of that type because it could
767 // require looking at the linkage of this function, and we don't need this
768 // for correctness because the type is not part of the function's
769 // signature.
Faisal Valid2598e92013-10-08 04:15:04 +0000770 // FIXME: This is a hack. We should be able to solve this circularity and
771 // the one in getLVForClassMember for Functions some other way.
Richard Smith50f4afc2013-05-12 23:17:59 +0000772 QualType TypeAsWritten = Function->getType();
773 if (TypeSourceInfo *TSI = Function->getTypeSourceInfo())
774 TypeAsWritten = TSI->getType();
775 if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
776 return LinkageInfo::uniqueExternal();
777 }
John McCallf768aa72011-02-10 06:50:24 +0000778
John McCall5f46c482013-02-21 23:42:58 +0000779 // Consider LV from the template and the template arguments.
780 // We're at file scope, so we do not need to worry about nested
781 // specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000782 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000783 = Function->getTemplateSpecializationInfo()) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000784 mergeTemplateLV(LV, Function, specInfo, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000785 }
786
Douglas Gregorf73b2822009-11-25 22:24:25 +0000787 // - a named class (Clause 9), or an unnamed class defined in a
788 // typedef declaration in which the class has the typedef name
789 // for linkage purposes (7.1.3); or
790 // - a named enumeration (7.2), or an unnamed enumeration
791 // defined in a typedef declaration in which the enumeration
792 // has the typedef name for linkage purposes (7.1.3); or
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000793 } else if (const auto *Tag = dyn_cast<TagDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000794 // Unnamed tags have no linkage.
John McCall5ea95772013-03-09 00:54:27 +0000795 if (!Tag->hasNameForLinkage())
John McCallc273f242010-10-30 11:50:40 +0000796 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000797
John McCall457a04e2010-10-22 21:05:15 +0000798 // If this is a class template specialization, consider the
John McCall5f46c482013-02-21 23:42:58 +0000799 // linkage of the template and template arguments. We're at file
800 // scope, so we do not need to worry about nested specializations.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000801 if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCalldf25c432013-02-16 00:17:33 +0000802 mergeTemplateLV(LV, spec, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000803 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000804
805 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000806 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000807 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCalldf25c432013-02-16 00:17:33 +0000808 computation);
Rafael Espindolab97e8962013-05-27 14:14:42 +0000809 if (!isExternalFormalLinkage(EnumLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000810 return LinkageInfo::none();
811 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000812
813 // - a template, unless it is a function template that has
814 // internal linkage (Clause 14);
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000815 } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) {
John McCalld041a9b2013-02-20 01:54:26 +0000816 bool considerVisibility = !hasExplicitVisibilityAlready(computation);
John McCalldf25c432013-02-16 00:17:33 +0000817 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000818 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-02-16 00:17:33 +0000819 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
820
Douglas Gregorf73b2822009-11-25 22:24:25 +0000821 // - a namespace (7.3), unless it is declared within an unnamed
822 // namespace.
Richard Smith1283e982017-07-07 20:04:28 +0000823 //
824 // We handled names in anonymous namespaces above.
825 } else if (isa<NamespaceDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000826 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000827
John McCall457a04e2010-10-22 21:05:15 +0000828 // By extension, we assign external linkage to Objective-C
829 // interfaces.
830 } else if (isa<ObjCInterfaceDecl>(D)) {
831 // fallout
832
Richard Smith97135cc2015-11-12 22:19:45 +0000833 } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) {
834 // A typedef declaration has linkage if it gives a type a name for
835 // linkage purposes.
836 if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true))
837 return LinkageInfo::none();
838
John McCall457a04e2010-10-22 21:05:15 +0000839 // Everything not covered here has no linkage.
840 } else {
John McCallc273f242010-10-30 11:50:40 +0000841 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000842 }
843
844 // If we ended up with non-external linkage, visibility should
845 // always be default.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000846 if (LV.getLinkage() != ExternalLinkage)
847 return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000848
John McCall457a04e2010-10-22 21:05:15 +0000849 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000850}
851
George Burgess IV99db3ea2017-08-09 04:02:49 +0000852LinkageInfo
853LinkageComputer::getLVForClassMember(const NamedDecl *D,
854 LVComputationKind computation) {
John McCall457a04e2010-10-22 21:05:15 +0000855 // Only certain class members have linkage. Note that fields don't
856 // really have linkage, but it's convenient to say they do for the
857 // purposes of calculating linkage of pointer-to-data-member
858 // template arguments.
Richard Smith26d11be2014-01-08 01:51:59 +0000859 //
860 // Templates also don't officially have linkage, but since we ignore
861 // the C++ standard and look at template arguments when determining
862 // linkage and visibility of a template specialization, we might hit
863 // a template template argument that way. If we do, we need to
864 // consider its linkage.
John McCall8823c652010-08-13 08:35:10 +0000865 if (!(isa<CXXMethodDecl>(D) ||
866 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000867 isa<FieldDecl>(D) ||
David Majnemer0eb8bbd2013-10-23 20:52:43 +0000868 isa<IndirectFieldDecl>(D) ||
Richard Smith26d11be2014-01-08 01:51:59 +0000869 isa<TagDecl>(D) ||
870 isa<TemplateDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000871 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000872
John McCall07072662010-11-02 01:45:15 +0000873 LinkageInfo LV;
874
John McCall07072662010-11-02 01:45:15 +0000875 // If we have an explicit visibility attribute, merge that in.
John McCalld041a9b2013-02-20 01:54:26 +0000876 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000877 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000878 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000879 // If we're paying attention to global visibility, apply
880 // -finline-visibility-hidden if this is an inline method.
881 //
882 // Note that we do this before merging information about
883 // the class visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000884 if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000885 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000886 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000887
888 // If this class member has an explicit visibility attribute, the only
889 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000890 // only look for them when processing the class.
John McCalld041a9b2013-02-20 01:54:26 +0000891 LVComputationKind classComputation = computation;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000892 if (LV.isVisibilityExplicit())
John McCalld041a9b2013-02-20 01:54:26 +0000893 classComputation = withExplicitVisibilityAlready(computation);
Rafael Espindola505a7c82012-04-16 18:25:01 +0000894
John McCall5f46c482013-02-21 23:42:58 +0000895 LinkageInfo classLV =
896 getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
John McCall8823c652010-08-13 08:35:10 +0000897 // If the class already has unique-external linkage, we can't improve.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000898 if (classLV.getLinkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000899 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000900
Rafael Espindolae6190db2013-05-28 02:22:10 +0000901 if (!isExternallyVisible(classLV.getLinkage()))
902 return LinkageInfo::none();
903
904
John McCall5f46c482013-02-21 23:42:58 +0000905 // Otherwise, don't merge in classLV yet, because in certain cases
906 // we need to completely ignore the visibility from it.
907
908 // Specifically, if this decl exists and has an explicit attribute.
Craig Topper36250ad2014-05-12 05:36:57 +0000909 const NamedDecl *explicitSpecSuppressor = nullptr;
John McCall5f46c482013-02-21 23:42:58 +0000910
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000911 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000912 // If the type of the function uses a type with unique-external
913 // linkage, it's not legally usable from outside this translation unit.
Nico Weber38267342015-04-22 03:44:51 +0000914 // But only look at the type-as-written. If this function has an
915 // auto-deduced return type, we can't compute the linkage of that type
916 // because it could require looking at the linkage of this function, and we
917 // don't need this for correctness because the type is not part of the
918 // function's signature.
919 // FIXME: This is a hack. We should be able to solve this circularity and
920 // the one in getLVForNamespaceScopeDecl for Functions some other way.
Faisal Valid2598e92013-10-08 04:15:04 +0000921 {
922 QualType TypeAsWritten = MD->getType();
923 if (TypeSourceInfo *TSI = MD->getTypeSourceInfo())
924 TypeAsWritten = TSI->getType();
925 if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
926 return LinkageInfo::uniqueExternal();
927 }
John McCall457a04e2010-10-22 21:05:15 +0000928 // If this is a method template specialization, use the linkage for
929 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000930 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000931 = MD->getTemplateSpecializationInfo()) {
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000932 mergeTemplateLV(LV, MD, spec, computation);
John McCall5f46c482013-02-21 23:42:58 +0000933 if (spec->isExplicitSpecialization()) {
934 explicitSpecSuppressor = MD;
935 } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
936 explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
937 }
938 } else if (isExplicitMemberSpecialization(MD)) {
939 explicitSpecSuppressor = MD;
John McCalle6e622e2010-11-01 01:29:57 +0000940 }
John McCall457a04e2010-10-22 21:05:15 +0000941
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000942 } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
943 if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCalldf25c432013-02-16 00:17:33 +0000944 mergeTemplateLV(LV, spec, computation);
John McCall5f46c482013-02-21 23:42:58 +0000945 if (spec->isExplicitSpecialization()) {
946 explicitSpecSuppressor = spec;
947 } else {
948 const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
949 if (isExplicitMemberSpecialization(temp)) {
950 explicitSpecSuppressor = temp->getTemplatedDecl();
951 }
952 }
953 } else if (isExplicitMemberSpecialization(RD)) {
954 explicitSpecSuppressor = RD;
John McCall37bb6c92010-10-29 22:22:43 +0000955 }
956
John McCall37bb6c92010-10-29 22:22:43 +0000957 // Static data members.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000958 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
959 if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD))
Larisse Voufo5899e192014-07-02 23:08:34 +0000960 mergeTemplateLV(LV, spec, computation);
961
John McCall36cd5cc2010-10-30 09:18:49 +0000962 // Modify the variable's linkage by its type, but ignore the
963 // type's visibility unless it's a definition.
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000964 LinkageInfo typeLV = getLVForType(*VD->getType(), computation);
Rafael Espindola503276b2013-05-30 21:23:15 +0000965 if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit())
966 LV.mergeVisibility(typeLV);
967 LV.mergeExternalVisibility(typeLV);
John McCall5f46c482013-02-21 23:42:58 +0000968
969 if (isExplicitMemberSpecialization(VD)) {
970 explicitSpecSuppressor = VD;
971 }
John McCalldf25c432013-02-16 00:17:33 +0000972
973 // Template members.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000974 } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) {
John McCalldf25c432013-02-16 00:17:33 +0000975 bool considerVisibility =
Rafael Espindola4a5da442013-02-27 02:56:45 +0000976 (!LV.isVisibilityExplicit() &&
977 !classLV.isVisibilityExplicit() &&
John McCalld041a9b2013-02-20 01:54:26 +0000978 !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000979 LinkageInfo tempLV =
Rafael Espindolaecf63c62013-05-29 04:55:30 +0000980 getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
John McCalldf25c432013-02-16 00:17:33 +0000981 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall5f46c482013-02-21 23:42:58 +0000982
Alexander Kornienkofd6ce042015-11-09 17:53:06 +0000983 if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) {
John McCall5f46c482013-02-21 23:42:58 +0000984 if (isExplicitMemberSpecialization(redeclTemp)) {
985 explicitSpecSuppressor = temp->getTemplatedDecl();
986 }
987 }
John McCall37bb6c92010-10-29 22:22:43 +0000988 }
989
John McCall5f46c482013-02-21 23:42:58 +0000990 // We should never be looking for an attribute directly on a template.
991 assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
992
993 // If this member is an explicit member specialization, and it has
994 // an explicit attribute, ignore visibility from the parent.
995 bool considerClassVisibility = true;
996 if (explicitSpecSuppressor &&
Rafael Espindola4a5da442013-02-27 02:56:45 +0000997 // optimization: hasDVA() is true only with explicit visibility.
998 LV.isVisibilityExplicit() &&
999 classLV.getVisibility() != DefaultVisibility &&
John McCall5f46c482013-02-21 23:42:58 +00001000 hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) {
1001 considerClassVisibility = false;
1002 }
1003
1004 // Finally, merge in information from the class.
1005 LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
John McCall457a04e2010-10-22 21:05:15 +00001006 return LV;
John McCall8823c652010-08-13 08:35:10 +00001007}
1008
David Blaikie68e081d2011-12-20 02:48:34 +00001009void NamedDecl::anchor() { }
1010
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001011bool NamedDecl::isLinkageValid() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +00001012 if (!hasCachedLinkage())
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001013 return true;
John McCalld396b972011-02-08 19:01:05 +00001014
George Burgess IV99db3ea2017-08-09 04:02:49 +00001015 Linkage L =
1016 LinkageComputer{}.computeLVForDecl(this, LVForLinkageOnly).getLinkage();
1017 return L == getCachedLinkage();
John McCalld396b972011-02-08 19:01:05 +00001018}
1019
Fariborz Jahanian6485fe42014-09-09 23:10:54 +00001020ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const {
1021 StringRef name = getName();
1022 if (name.empty()) return SFF_None;
1023
1024 if (name.front() == 'C')
1025 if (name == "CFStringCreateWithFormat" ||
1026 name == "CFStringCreateWithFormatAndArguments" ||
1027 name == "CFStringAppendFormat" ||
1028 name == "CFStringAppendFormatAndArguments")
1029 return SFF_CFString;
1030 return SFF_None;
1031}
1032
Rafael Espindola3ae00052013-05-13 00:12:11 +00001033Linkage NamedDecl::getLinkageInternal() const {
John McCalld041a9b2013-02-20 01:54:26 +00001034 // We don't care about visibility here, so ask for the cheapest
1035 // possible visibility analysis.
George Burgess IV99db3ea2017-08-09 04:02:49 +00001036 return LinkageComputer{}.getLVForDecl(this, LVForLinkageOnly).getLinkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001037}
1038
John McCallc273f242010-10-30 11:50:40 +00001039LinkageInfo NamedDecl::getLinkageAndVisibility() const {
George Burgess IV99db3ea2017-08-09 04:02:49 +00001040 return LinkageComputer{}.getDeclLinkageAndVisibility(this);
John McCall033caa52010-10-29 00:29:13 +00001041}
Ted Kremenek926d8602010-04-20 23:15:35 +00001042
Rafael Espindola9ad61122013-11-26 16:09:08 +00001043static Optional<Visibility>
1044getExplicitVisibilityAux(const NamedDecl *ND,
1045 NamedDecl::ExplicitVisibilityKind kind,
1046 bool IsMostRecent) {
1047 assert(!IsMostRecent || ND == ND->getMostRecentDecl());
1048
Rafael Espindola3a52c442013-02-26 19:33:14 +00001049 // Check the declaration itself first.
Rafael Espindola9ad61122013-11-26 16:09:08 +00001050 if (Optional<Visibility> V = getVisibilityOf(ND, kind))
Rafael Espindola3a52c442013-02-26 19:33:14 +00001051 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001052
Rafael Espindola3a52c442013-02-26 19:33:14 +00001053 // If this is a member class of a specialization of a class template
1054 // and the corresponding decl has explicit visibility, use that.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001055 if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
Rafael Espindola3a52c442013-02-26 19:33:14 +00001056 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
1057 if (InstantiatedFrom)
1058 return getVisibilityOf(InstantiatedFrom, kind);
1059 }
1060
1061 // If there wasn't explicit visibility there, and this is a
1062 // specialization of a class template, check for visibility
1063 // on the pattern.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001064 if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND))
Rafael Espindola3a52c442013-02-26 19:33:14 +00001065 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(),
1066 kind);
1067
1068 // Use the most recent declaration.
Rafael Espindola7ab1ce02013-12-08 01:13:22 +00001069 if (!IsMostRecent && !isa<NamespaceDecl>(ND)) {
Rafael Espindola9ad61122013-11-26 16:09:08 +00001070 const NamedDecl *MostRecent = ND->getMostRecentDecl();
1071 if (MostRecent != ND)
1072 return getExplicitVisibilityAux(MostRecent, kind, true);
1073 }
Rafael Espindola3a52c442013-02-26 19:33:14 +00001074
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001075 if (const auto *Var = dyn_cast<VarDecl>(ND)) {
Rafael Espindola96e68242012-05-16 02:10:38 +00001076 if (Var->isStaticDataMember()) {
1077 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
1078 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +00001079 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola96e68242012-05-16 02:10:38 +00001080 }
1081
David Majnemer35b02bc2014-04-29 07:32:26 +00001082 if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var))
1083 return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(),
1084 kind);
1085
David Blaikie7a30dc52013-02-21 01:47:18 +00001086 return None;
Rafael Espindola96e68242012-05-16 02:10:38 +00001087 }
Rafael Espindola3a52c442013-02-26 19:33:14 +00001088 // Also handle function template specializations.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001089 if (const auto *fn = dyn_cast<FunctionDecl>(ND)) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001090 // If the function is a specialization of a template with an
1091 // explicit visibility attribute, use that.
1092 if (FunctionTemplateSpecializationInfo *templateInfo
1093 = fn->getTemplateSpecializationInfo())
John McCalld041a9b2013-02-20 01:54:26 +00001094 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
1095 kind);
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001096
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001097 // If the function is a member of a specialization of a class template
1098 // and the corresponding decl has explicit visibility, use that.
1099 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
1100 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +00001101 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001102
David Blaikie7a30dc52013-02-21 01:47:18 +00001103 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001104 }
1105
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001106 // The visibility of a template is stored in the templated decl.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001107 if (const auto *TD = dyn_cast<TemplateDecl>(ND))
John McCalld041a9b2013-02-20 01:54:26 +00001108 return getVisibilityOf(TD->getTemplatedDecl(), kind);
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001109
David Blaikie7a30dc52013-02-21 01:47:18 +00001110 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001111}
1112
Rafael Espindola9ad61122013-11-26 16:09:08 +00001113Optional<Visibility>
1114NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
1115 return getExplicitVisibilityAux(this, kind, false);
1116}
1117
George Burgess IV99db3ea2017-08-09 04:02:49 +00001118LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC,
1119 Decl *ContextDecl,
1120 LVComputationKind computation) {
Eli Friedman7e346a82013-07-01 20:22:57 +00001121 // This lambda has its linkage/visibility determined by its owner.
1122 if (ContextDecl) {
1123 if (isa<ParmVarDecl>(ContextDecl))
1124 DC = ContextDecl->getDeclContext()->getRedeclContext();
1125 else
1126 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
1127 }
Faisal Vali98b8e182013-09-29 20:27:06 +00001128
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001129 if (const auto *ND = dyn_cast<NamedDecl>(DC))
Eli Friedman7e346a82013-07-01 20:22:57 +00001130 return getLVForDecl(ND, computation);
Faisal Vali98b8e182013-09-29 20:27:06 +00001131
Richard Smith1283e982017-07-07 20:04:28 +00001132 // FIXME: We have a closure at TU scope with no context declaration. This
1133 // should probably have no linkage.
Eli Friedman7e346a82013-07-01 20:22:57 +00001134 return LinkageInfo::external();
1135}
1136
George Burgess IV99db3ea2017-08-09 04:02:49 +00001137LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D,
1138 LVComputationKind computation) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001139 if (const auto *Function = dyn_cast<FunctionDecl>(D)) {
John McCalldf25c432013-02-16 00:17:33 +00001140 if (Function->isInAnonymousNamespace() &&
Rafael Espindola593537a2013-05-05 20:15:21 +00001141 !Function->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001142 return LinkageInfo::uniqueExternal();
1143
1144 // This is a "void f();" which got merged with a file static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001145 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
Richard Smith1283e982017-07-07 20:04:28 +00001146 return getInternalLinkageFor(Function);
John McCalldf25c432013-02-16 00:17:33 +00001147
1148 LinkageInfo LV;
John McCalld041a9b2013-02-20 01:54:26 +00001149 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001150 if (Optional<Visibility> Vis =
1151 getExplicitVisibility(Function, computation))
John McCalldf25c432013-02-16 00:17:33 +00001152 LV.mergeVisibility(*Vis, true);
1153 }
1154
1155 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1156 // merging storage classes and visibility attributes, so we don't have to
1157 // look at previous decls in here.
1158
1159 return LV;
1160 }
1161
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001162 if (const auto *Var = dyn_cast<VarDecl>(D)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001163 if (Var->hasExternalStorage()) {
Rafael Espindola593537a2013-05-05 20:15:21 +00001164 if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001165 return LinkageInfo::uniqueExternal();
1166
John McCalldf25c432013-02-16 00:17:33 +00001167 LinkageInfo LV;
1168 if (Var->getStorageClass() == SC_PrivateExtern)
1169 LV.mergeVisibility(HiddenVisibility, true);
John McCalld041a9b2013-02-20 01:54:26 +00001170 else if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001171 if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
John McCalldf25c432013-02-16 00:17:33 +00001172 LV.mergeVisibility(*Vis, true);
1173 }
1174
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001175 if (const VarDecl *Prev = Var->getPreviousDecl()) {
1176 LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1177 if (PrevLV.getLinkage())
1178 LV.setLinkage(PrevLV.getLinkage());
1179 LV.mergeVisibility(PrevLV);
1180 }
1181
John McCalldf25c432013-02-16 00:17:33 +00001182 return LV;
1183 }
Rafael Espindolaa4184182013-06-17 20:04:51 +00001184
1185 if (!Var->isStaticLocal())
1186 return LinkageInfo::none();
John McCalldf25c432013-02-16 00:17:33 +00001187 }
1188
Rafael Espindolaa4184182013-06-17 20:04:51 +00001189 ASTContext &Context = D->getASTContext();
1190 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindola50df3a02013-05-25 17:16:20 +00001191 return LinkageInfo::none();
1192
Eli Friedman7e346a82013-07-01 20:22:57 +00001193 const Decl *OuterD = getOutermostFuncOrBlockContext(D);
Alexey Bataev0a47e652016-01-12 09:01:25 +00001194 if (!OuterD || OuterD->isInvalidDecl())
Rafael Espindola50df3a02013-05-25 17:16:20 +00001195 return LinkageInfo::none();
Rafael Espindola52189362013-06-04 13:43:35 +00001196
Eli Friedman7e346a82013-07-01 20:22:57 +00001197 LinkageInfo LV;
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001198 if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) {
Eli Friedman7e346a82013-07-01 20:22:57 +00001199 if (!BD->getBlockManglingNumber())
1200 return LinkageInfo::none();
Rafael Espindola52189362013-06-04 13:43:35 +00001201
Eli Friedman7e346a82013-07-01 20:22:57 +00001202 LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(),
1203 BD->getBlockManglingContextDecl(), computation);
1204 } else {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001205 const auto *FD = cast<FunctionDecl>(OuterD);
Eli Friedman7e346a82013-07-01 20:22:57 +00001206 if (!FD->isInlined() &&
David Majnemer9963ade2014-12-16 04:52:14 +00001207 !isTemplateInstantiation(FD->getTemplateSpecializationKind()))
Eli Friedman7e346a82013-07-01 20:22:57 +00001208 return LinkageInfo::none();
1209
1210 LV = getLVForDecl(FD, computation);
1211 }
Rafael Espindola111bb2e2013-05-27 14:50:21 +00001212 if (!isExternallyVisible(LV.getLinkage()))
Rafael Espindola50df3a02013-05-25 17:16:20 +00001213 return LinkageInfo::none();
1214 return LinkageInfo(VisibleNoLinkage, LV.getVisibility(),
1215 LV.isVisibilityExplicit());
John McCalldf25c432013-02-16 00:17:33 +00001216}
1217
Faisal Vali49b4c1f2013-10-01 02:51:53 +00001218static inline const CXXRecordDecl*
1219getOutermostEnclosingLambda(const CXXRecordDecl *Record) {
1220 const CXXRecordDecl *Ret = Record;
1221 while (Record && Record->isLambda()) {
1222 Ret = Record;
1223 if (!Record->getParent()) break;
1224 // Get the Containing Class of this Lambda Class
1225 Record = dyn_cast_or_null<CXXRecordDecl>(
1226 Record->getParent()->getParent());
1227 }
1228 return Ret;
1229}
1230
George Burgess IV99db3ea2017-08-09 04:02:49 +00001231LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D,
1232 LVComputationKind computation) {
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001233 // Internal_linkage attribute overrides other considerations.
1234 if (D->hasAttr<InternalLinkageAttr>())
Richard Smith1283e982017-07-07 20:04:28 +00001235 return getInternalLinkageFor(D);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001236
Ted Kremenek926d8602010-04-20 23:15:35 +00001237 // Objective-C: treat all Objective-C declarations as having external
1238 // linkage.
John McCall033caa52010-10-29 00:29:13 +00001239 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001240 default:
1241 break;
Richard Smith97135cc2015-11-12 22:19:45 +00001242
1243 // Per C++ [basic.link]p2, only the names of objects, references,
1244 // functions, types, templates, namespaces, and values ever have linkage.
1245 //
1246 // Note that the name of a typedef, namespace alias, using declaration,
1247 // and so on are not the name of the corresponding type, namespace, or
1248 // declaration, so they do *not* have linkage.
Richard Smith97135cc2015-11-12 22:19:45 +00001249 case Decl::ImplicitParam:
1250 case Decl::Label:
1251 case Decl::NamespaceAlias:
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +00001252 case Decl::ParmVar:
Richard Smith97135cc2015-11-12 22:19:45 +00001253 case Decl::Using:
1254 case Decl::UsingShadow:
1255 case Decl::UsingDirective:
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +00001256 return LinkageInfo::none();
Richard Smith97135cc2015-11-12 22:19:45 +00001257
Richard Smith26210db2015-11-13 03:52:13 +00001258 case Decl::EnumConstant:
1259 // C++ [basic.link]p4: an enumerator has the linkage of its enumeration.
Bruno Cardoso Lopes057c82c2017-07-01 00:06:27 +00001260 if (D->getASTContext().getLangOpts().CPlusPlus)
1261 return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation);
1262 return LinkageInfo::visible_none();
Richard Smith26210db2015-11-13 03:52:13 +00001263
Richard Smith97135cc2015-11-12 22:19:45 +00001264 case Decl::Typedef:
1265 case Decl::TypeAlias:
1266 // A typedef declaration has linkage if it gives a type a name for
1267 // linkage purposes.
Vassil Vassilevb00ea082017-07-01 20:44:49 +00001268 if (!cast<TypedefNameDecl>(D)
Richard Smith97135cc2015-11-12 22:19:45 +00001269 ->getAnonDeclWithTypedefName(/*AnyRedecl*/true))
1270 return LinkageInfo::none();
1271 break;
1272
John McCall457a04e2010-10-22 21:05:15 +00001273 case Decl::TemplateTemplateParm: // count these as external
1274 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +00001275 case Decl::ObjCAtDefsField:
1276 case Decl::ObjCCategory:
1277 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +00001278 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +00001279 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +00001280 case Decl::ObjCMethod:
1281 case Decl::ObjCProperty:
1282 case Decl::ObjCPropertyImpl:
1283 case Decl::ObjCProtocol:
Richard Smith1283e982017-07-07 20:04:28 +00001284 return getExternalLinkageFor(D);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001285
1286 case Decl::CXXRecord: {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001287 const auto *Record = cast<CXXRecordDecl>(D);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001288 if (Record->isLambda()) {
1289 if (!Record->getLambdaManglingNumber()) {
1290 // This lambda has no mangling number, so it's internal.
Richard Smith1283e982017-07-07 20:04:28 +00001291 return getInternalLinkageFor(D);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001292 }
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001293
Faisal Vali49b4c1f2013-10-01 02:51:53 +00001294 // This lambda has its linkage/visibility determined:
1295 // - either by the outermost lambda if that lambda has no mangling
1296 // number.
1297 // - or by the parent of the outer most lambda
1298 // This prevents infinite recursion in settings such as nested lambdas
1299 // used in NSDMI's, for e.g.
1300 // struct L {
1301 // int t{};
1302 // int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
1303 // };
1304 const CXXRecordDecl *OuterMostLambda =
1305 getOutermostEnclosingLambda(Record);
1306 if (!OuterMostLambda->getLambdaManglingNumber())
Richard Smith1283e982017-07-07 20:04:28 +00001307 return getInternalLinkageFor(D);
Faisal Vali49b4c1f2013-10-01 02:51:53 +00001308
1309 return getLVForClosure(
1310 OuterMostLambda->getDeclContext()->getRedeclContext(),
1311 OuterMostLambda->getLambdaContextDecl(), computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001312 }
1313
1314 break;
1315 }
Ted Kremenek926d8602010-04-20 23:15:35 +00001316 }
1317
Douglas Gregorf73b2822009-11-25 22:24:25 +00001318 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +00001319 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCalldf25c432013-02-16 00:17:33 +00001320 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001321
1322 // C++ [basic.link]p5:
1323 // In addition, a member function, static data member, a named
1324 // class or enumeration of class scope, or an unnamed class or
1325 // enumeration defined in a class-scope typedef declaration such
1326 // that the class or enumeration has the typedef name for linkage
1327 // purposes (7.1.3), has external linkage if the name of the class
1328 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +00001329 if (D->getDeclContext()->isRecord())
John McCalldf25c432013-02-16 00:17:33 +00001330 return getLVForClassMember(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001331
1332 // C++ [basic.link]p6:
1333 // The name of a function declared in block scope and the name of
1334 // an object declared by a block scope extern declaration have
1335 // linkage. If there is a visible declaration of an entity with
1336 // linkage having the same name and type, ignoring entities
1337 // declared outside the innermost enclosing namespace scope, the
1338 // block scope declaration declares that same entity and receives
1339 // the linkage of the previous declaration. If there is more than
1340 // one such matching entity, the program is ill-formed. Otherwise,
1341 // if no matching entity is found, the block scope entity receives
1342 // external linkage.
John McCalldf25c432013-02-16 00:17:33 +00001343 if (D->getDeclContext()->isFunctionOrMethod())
1344 return getLVForLocalDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001345
1346 // C++ [basic.link]p6:
1347 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +00001348 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001349}
Douglas Gregorf73b2822009-11-25 22:24:25 +00001350
George Burgess IV99db3ea2017-08-09 04:02:49 +00001351/// getLVForDecl - Get the linkage and visibility for the given declaration.
1352LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D,
1353 LVComputationKind computation) {
1354 // Internal_linkage attribute overrides other considerations.
1355 if (D->hasAttr<InternalLinkageAttr>())
1356 return getInternalLinkageFor(D);
Evgeniy Stepanovae6ebd32015-11-10 21:28:44 +00001357
George Burgess IV99db3ea2017-08-09 04:02:49 +00001358 if (computation == LVForLinkageOnly && D->hasCachedLinkage())
1359 return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false);
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001360
George Burgess IV35cb4f82017-08-09 04:12:17 +00001361 if (llvm::Optional<LinkageInfo> LI = lookup(D, computation))
1362 return *LI;
1363
George Burgess IV99db3ea2017-08-09 04:02:49 +00001364 LinkageInfo LV = computeLVForDecl(D, computation);
1365 if (D->hasCachedLinkage())
1366 assert(D->getCachedLinkage() == LV.getLinkage());
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001367
George Burgess IV99db3ea2017-08-09 04:02:49 +00001368 D->setCachedLinkage(LV.getLinkage());
George Burgess IV35cb4f82017-08-09 04:12:17 +00001369 cache(D, computation, LV);
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001370
1371#ifndef NDEBUG
George Burgess IV99db3ea2017-08-09 04:02:49 +00001372 // In C (because of gnu inline) and in c++ with microsoft extensions an
1373 // static can follow an extern, so we can have two decls with different
1374 // linkages.
1375 const LangOptions &Opts = D->getASTContext().getLangOpts();
1376 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
1377 return LV;
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001378
George Burgess IV99db3ea2017-08-09 04:02:49 +00001379 // We have just computed the linkage for this decl. By induction we know
1380 // that all other computed linkages match, check that the one we just
1381 // computed also does.
1382 NamedDecl *Old = nullptr;
1383 for (auto I : D->redecls()) {
1384 auto *T = cast<NamedDecl>(I);
1385 if (T == D)
1386 continue;
1387 if (!T->isInvalidDecl() && T->hasCachedLinkage()) {
1388 Old = T;
1389 break;
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001390 }
George Burgess IV99db3ea2017-08-09 04:02:49 +00001391 }
1392 assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage());
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001393#endif
1394
George Burgess IV99db3ea2017-08-09 04:02:49 +00001395 return LV;
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001396}
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001397
George Burgess IV99db3ea2017-08-09 04:02:49 +00001398LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) {
1399 return getLVForDecl(D, usesTypeVisibility(D) ? LVForType : LVForValue);
Rafael Espindola9551d3b2013-05-28 19:43:11 +00001400}
1401
Richard Smith7873de02016-08-11 22:25:46 +00001402void NamedDecl::printName(raw_ostream &os) const {
1403 os << Name;
1404}
1405
Douglas Gregor2ada0482009-02-04 17:27:36 +00001406std::string NamedDecl::getQualifiedNameAsString() const {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001407 std::string QualName;
1408 llvm::raw_string_ostream OS(QualName);
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001409 printQualifiedName(OS, getASTContext().getPrintingPolicy());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001410 return OS.str();
1411}
1412
1413void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1414 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1415}
1416
1417void NamedDecl::printQualifiedName(raw_ostream &OS,
1418 const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +00001419 const DeclContext *Ctx = getDeclContext();
1420
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +00001421 // For ObjC methods, look through categories and use the interface as context.
1422 if (auto *MD = dyn_cast<ObjCMethodDecl>(this))
1423 if (auto *ID = MD->getClassInterface())
1424 Ctx = ID;
1425
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001426 if (Ctx->isFunctionOrMethod()) {
1427 printName(OS);
1428 return;
1429 }
Douglas Gregor2ada0482009-02-04 17:27:36 +00001430
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001431 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001432 ContextsTy Contexts;
1433
1434 // Collect contexts.
1435 while (Ctx && isa<NamedDecl>(Ctx)) {
1436 Contexts.push_back(Ctx);
1437 Ctx = Ctx->getParent();
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001438 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001439
David Majnemerf7e36092016-06-23 00:15:04 +00001440 for (const DeclContext *DC : reverse(Contexts)) {
1441 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Benjamin Kramer9170e912013-02-22 15:46:01 +00001442 OS << Spec->getName();
Douglas Gregor85673582009-05-18 17:01:57 +00001443 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
David Majnemer6fbeee32016-07-07 04:43:07 +00001444 TemplateSpecializationType::PrintTemplateArgumentList(
1445 OS, TemplateArgs.asArray(), P);
David Majnemerf7e36092016-06-23 00:15:04 +00001446 } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
Richard Smith46dd5bb2014-05-30 22:16:51 +00001447 if (P.SuppressUnwrittenScope &&
1448 (ND->isAnonymousNamespace() || ND->isInline()))
1449 continue;
Reid Kleckner60103382015-12-16 02:04:40 +00001450 if (ND->isAnonymousNamespace()) {
1451 OS << (P.MSVCFormatting ? "`anonymous namespace\'"
1452 : "(anonymous namespace)");
1453 }
Sam Weinig07d211e2009-12-24 23:15:03 +00001454 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001455 OS << *ND;
David Majnemerf7e36092016-06-23 00:15:04 +00001456 } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) {
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001457 if (!RD->getIdentifier())
David Blaikieabe1a392014-04-02 05:58:29 +00001458 OS << "(anonymous " << RD->getKindName() << ')';
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001459 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001460 OS << *RD;
David Majnemerf7e36092016-06-23 00:15:04 +00001461 } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
Craig Topper36250ad2014-05-12 05:36:57 +00001462 const FunctionProtoType *FT = nullptr;
Sam Weinigb999f682009-12-28 03:19:38 +00001463 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001464 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001465
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001466 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001467 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001468 unsigned NumParams = FD->getNumParams();
1469 for (unsigned i = 0; i < NumParams; ++i) {
1470 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001471 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001472 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001473 }
1474
1475 if (FT->isVariadic()) {
1476 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001477 OS << ", ";
1478 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001479 }
1480 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001481 OS << ')';
David Majnemerf7e36092016-06-23 00:15:04 +00001482 } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) {
Alexander Kornienko4f355322015-11-09 16:45:17 +00001483 // C++ [dcl.enum]p10: Each enum-name and each unscoped
1484 // enumerator is declared in the scope that immediately contains
1485 // the enum-specifier. Each scoped enumerator is declared in the
1486 // scope of the enumeration.
1487 if (ED->isScoped() || ED->getIdentifier())
1488 OS << *ED;
1489 else
1490 continue;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001491 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001492 OS << *cast<NamedDecl>(DC);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001493 }
1494 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001495 }
1496
Richard Smith7873de02016-08-11 22:25:46 +00001497 if (getDeclName() || isa<DecompositionDecl>(this))
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001498 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001499 else
David Blaikieabe1a392014-04-02 05:58:29 +00001500 OS << "(anonymous)";
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001501}
Douglas Gregor2ada0482009-02-04 17:27:36 +00001502
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001503void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1504 const PrintingPolicy &Policy,
1505 bool Qualified) const {
1506 if (Qualified)
1507 printQualifiedName(OS, Policy);
1508 else
1509 printName(OS);
Douglas Gregor2ada0482009-02-04 17:27:36 +00001510}
1511
Richard Smithe8292b12015-02-10 03:28:10 +00001512template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1513 return true;
1514}
1515static bool isRedeclarableImpl(...) { return false; }
1516static bool isRedeclarable(Decl::Kind K) {
1517 switch (K) {
1518#define DECL(Type, Base) \
1519 case Decl::Type: \
1520 return isRedeclarableImpl((Type##Decl *)nullptr);
1521#define ABSTRACT_DECL(DECL)
1522#include "clang/AST/DeclNodes.inc"
1523 }
1524 llvm_unreachable("unknown decl kind");
1525}
1526
1527bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001528 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1529
Richard Smithfe620d22015-03-05 23:24:12 +00001530 // Never replace one imported declaration with another; we need both results
1531 // when re-exporting.
1532 if (OldD->isFromASTFile() && isFromASTFile())
1533 return false;
1534
Richard Smith5cd86f82015-11-03 03:13:11 +00001535 // A kind mismatch implies that the declaration is not replaced.
1536 if (OldD->getKind() != getKind())
Richard Smithe8292b12015-02-10 03:28:10 +00001537 return false;
1538
Richard Smith5cd86f82015-11-03 03:13:11 +00001539 // For method declarations, we never replace. (Why?)
1540 if (isa<ObjCMethodDecl>(this))
1541 return false;
1542
1543 // For parameters, pick the newer one. This is either an error or (in
1544 // Objective-C) permitted as an extension.
1545 if (isa<ParmVarDecl>(this))
1546 return true;
1547
Richard Smithe8292b12015-02-10 03:28:10 +00001548 // Inline namespaces can give us two declarations with the same
1549 // name and kind in the same scope but different contexts; we should
1550 // keep both declarations in this case.
1551 if (!this->getDeclContext()->getRedeclContext()->Equals(
1552 OldD->getDeclContext()->getRedeclContext()))
1553 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Richard Smith5cd86f82015-11-03 03:13:11 +00001555 // Using declarations can be replaced if they import the same name from the
1556 // same context.
Richard Smithe8292b12015-02-10 03:28:10 +00001557 if (auto *UD = dyn_cast<UsingDecl>(this)) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001558 ASTContext &Context = getASTContext();
Richard Smithe8292b12015-02-10 03:28:10 +00001559 return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) ==
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001560 Context.getCanonicalNestedNameSpecifier(
Richard Smithe8292b12015-02-10 03:28:10 +00001561 cast<UsingDecl>(OldD)->getQualifier());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001562 }
Richard Smithe8292b12015-02-10 03:28:10 +00001563 if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) {
Eli Friedman0eaf10b2013-08-20 00:39:40 +00001564 ASTContext &Context = getASTContext();
Richard Smithe8292b12015-02-10 03:28:10 +00001565 return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) ==
Eli Friedman0eaf10b2013-08-20 00:39:40 +00001566 Context.getCanonicalNestedNameSpecifier(
1567 cast<UnresolvedUsingValueDecl>(OldD)->getQualifier());
1568 }
1569
Richard Smithe8292b12015-02-10 03:28:10 +00001570 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
Richard Smith5cd86f82015-11-03 03:13:11 +00001571 // They can be replaced if they nominate the same namespace.
1572 // FIXME: Is this true even if they have different module visibility?
Richard Smithe8292b12015-02-10 03:28:10 +00001573 if (auto *UD = dyn_cast<UsingDirectiveDecl>(this))
1574 return UD->getNominatedNamespace()->getOriginalNamespace() ==
1575 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1576 ->getOriginalNamespace();
Richard Smithbaf3ca52014-03-17 21:46:03 +00001577
Richard Smith5cd86f82015-11-03 03:13:11 +00001578 if (isRedeclarable(getKind())) {
1579 if (getCanonicalDecl() != OldD->getCanonicalDecl())
1580 return false;
1581
1582 if (IsKnownNewer)
1583 return true;
1584
Richard Smithe8292b12015-02-10 03:28:10 +00001585 // Check whether this is actually newer than OldD. We want to keep the
1586 // newer declaration. This loop will usually only iterate once, because
1587 // OldD is usually the previous declaration.
1588 for (auto D : redecls()) {
1589 if (D == OldD)
1590 break;
1591
1592 // If we reach the canonical declaration, then OldD is not actually older
1593 // than this one.
1594 //
1595 // FIXME: In this case, we should not add this decl to the lookup table.
1596 if (D->isCanonicalDecl())
1597 return false;
1598 }
Richard Smith5cd86f82015-11-03 03:13:11 +00001599
1600 // It's a newer declaration of the same kind of declaration in the same
1601 // scope: we want this decl instead of the existing one.
1602 return true;
Richard Smithe8292b12015-02-10 03:28:10 +00001603 }
1604
Richard Smith5cd86f82015-11-03 03:13:11 +00001605 // In all other cases, we need to keep both declarations in case they have
1606 // different visibility. Any attempt to use the name will result in an
1607 // ambiguity if more than one is visible.
1608 return false;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001609}
1610
Douglas Gregoreddf4332009-02-24 20:03:32 +00001611bool NamedDecl::hasLinkage() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +00001612 return getFormalLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001613}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001614
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001615NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001616 NamedDecl *ND = this;
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001617 while (auto *UD = dyn_cast<UsingShadowDecl>(ND))
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001618 ND = UD->getTargetDecl();
1619
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001620 if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001621 return AD->getClassInterface();
1622
Richard Smithf2005d32015-12-29 23:34:32 +00001623 if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND))
1624 return AD->getNamespace();
1625
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001626 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001627}
1628
John McCalla8ae2222010-04-06 21:38:20 +00001629bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001630 if (!isCXXClassMember())
1631 return false;
1632
John McCalla8ae2222010-04-06 21:38:20 +00001633 const NamedDecl *D = this;
1634 if (isa<UsingShadowDecl>(D))
1635 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1636
John McCall5e77d762013-04-16 07:28:30 +00001637 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001638 return true;
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001639 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()))
Alp Tokera2794f92014-01-22 07:29:52 +00001640 return MD->isInstance();
John McCalla8ae2222010-04-06 21:38:20 +00001641 return false;
1642}
1643
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001644//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001645// DeclaratorDecl Implementation
1646//===----------------------------------------------------------------------===//
1647
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001648template <typename DeclT>
1649static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1650 if (decl->getNumTemplateParameterLists() > 0)
1651 return decl->getTemplateParameterList(0)->getTemplateLoc();
1652 else
1653 return decl->getInnerLocStart();
1654}
1655
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001656SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001657 TypeSourceInfo *TSI = getTypeSourceInfo();
1658 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001659 return SourceLocation();
1660}
1661
Douglas Gregor14454802011-02-25 02:25:35 +00001662void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1663 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001664 // Make sure the extended decl info is allocated.
1665 if (!hasExtInfo()) {
1666 // Save (non-extended) type source info pointer.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001667 auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
John McCall3e11ebe2010-03-15 10:12:16 +00001668 // Allocate external info struct.
1669 DeclInfo = new (getASTContext()) ExtInfo;
1670 // Restore savedTInfo into (extended) decl info.
1671 getExtInfo()->TInfo = savedTInfo;
1672 }
1673 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001674 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001675 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001676 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001677 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001678 if (getExtInfo()->NumTemplParamLists == 0) {
1679 // Save type source info pointer.
1680 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1681 // Deallocate the extended decl info.
1682 getASTContext().Deallocate(getExtInfo());
1683 // Restore savedTInfo into (non-extended) decl info.
1684 DeclInfo = savedTInfo;
1685 }
1686 else
1687 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001688 }
1689 }
1690}
1691
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001692void DeclaratorDecl::setTemplateParameterListsInfo(
1693 ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
1694 assert(!TPLists.empty());
Abramo Bagnara60804e12011-03-18 15:16:37 +00001695 // Make sure the extended decl info is allocated.
1696 if (!hasExtInfo()) {
1697 // Save (non-extended) type source info pointer.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001698 auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
Abramo Bagnara60804e12011-03-18 15:16:37 +00001699 // Allocate external info struct.
1700 DeclInfo = new (getASTContext()) ExtInfo;
1701 // Restore savedTInfo into (extended) decl info.
1702 getExtInfo()->TInfo = savedTInfo;
1703 }
1704 // Set the template parameter lists info.
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001705 getExtInfo()->setTemplateParameterListsInfo(Context, TPLists);
Abramo Bagnara60804e12011-03-18 15:16:37 +00001706}
1707
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001708SourceLocation DeclaratorDecl::getOuterLocStart() const {
1709 return getTemplateOrInnerLocStart(this);
1710}
1711
Abramo Bagnaraea947882011-03-08 16:41:52 +00001712namespace {
1713
1714// Helper function: returns true if QT is or contains a type
1715// having a postfix component.
1716bool typeIsPostfix(clang::QualType QT) {
1717 while (true) {
1718 const Type* T = QT.getTypePtr();
1719 switch (T->getTypeClass()) {
1720 default:
1721 return false;
1722 case Type::Pointer:
1723 QT = cast<PointerType>(T)->getPointeeType();
1724 break;
1725 case Type::BlockPointer:
1726 QT = cast<BlockPointerType>(T)->getPointeeType();
1727 break;
1728 case Type::MemberPointer:
1729 QT = cast<MemberPointerType>(T)->getPointeeType();
1730 break;
1731 case Type::LValueReference:
1732 case Type::RValueReference:
1733 QT = cast<ReferenceType>(T)->getPointeeType();
1734 break;
1735 case Type::PackExpansion:
1736 QT = cast<PackExpansionType>(T)->getPattern();
1737 break;
1738 case Type::Paren:
1739 case Type::ConstantArray:
1740 case Type::DependentSizedArray:
1741 case Type::IncompleteArray:
1742 case Type::VariableArray:
1743 case Type::FunctionProto:
1744 case Type::FunctionNoProto:
1745 return true;
1746 }
1747 }
1748}
1749
1750} // namespace
1751
1752SourceRange DeclaratorDecl::getSourceRange() const {
1753 SourceLocation RangeEnd = getLocation();
1754 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
Benjamin Kramer3a7cc812014-02-02 15:28:46 +00001755 // If the declaration has no name or the type extends past the name take the
1756 // end location of the type.
1757 if (!getDeclName() || typeIsPostfix(TInfo->getType()))
Abramo Bagnaraea947882011-03-08 16:41:52 +00001758 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1759 }
1760 return SourceRange(getOuterLocStart(), RangeEnd);
1761}
1762
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001763void QualifierInfo::setTemplateParameterListsInfo(
1764 ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001765 // Free previous template parameters (if any).
1766 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001767 Context.Deallocate(TemplParamLists);
Craig Topper36250ad2014-05-12 05:36:57 +00001768 TemplParamLists = nullptr;
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001769 NumTemplParamLists = 0;
1770 }
1771 // Set info on matched template parameter lists (if any).
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001772 if (!TPLists.empty()) {
1773 TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()];
1774 NumTemplParamLists = TPLists.size();
1775 std::copy(TPLists.begin(), TPLists.end(), TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001776 }
1777}
1778
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001779//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001780// VarDecl Implementation
1781//===----------------------------------------------------------------------===//
1782
Sebastian Redl833ef452010-01-26 22:01:41 +00001783const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1784 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001785 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001786 case SC_Auto: return "auto";
1787 case SC_Extern: return "extern";
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001788 case SC_PrivateExtern: return "__private_extern__";
1789 case SC_Register: return "register";
1790 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001791 }
1792
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001793 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001794}
1795
Richard Smith053f6c62014-05-16 23:01:30 +00001796VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC,
1797 SourceLocation StartLoc, SourceLocation IdLoc,
1798 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
1799 StorageClass SC)
1800 : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc),
1801 redeclarable_base(C), Init() {
Benjamin Kramera939c232014-03-15 18:54:13 +00001802 static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned),
1803 "VarDeclBitfields too large!");
1804 static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned),
1805 "ParmVarDeclBitfields too large!");
David Majnemerfa7bc782015-05-19 00:57:16 +00001806 static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned),
1807 "NonParmVarDeclBitfields too large!");
Larisse Voufo39a1e502013-08-06 01:03:05 +00001808 AllBits = 0;
1809 VarDeclBits.SClass = SC;
1810 // Everything else is implicitly initialized to false.
1811}
1812
Abramo Bagnaradff19302011-03-08 08:55:46 +00001813VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1814 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001815 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001816 StorageClass S) {
Richard Smith053f6c62014-05-16 23:01:30 +00001817 return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001818}
1819
Douglas Gregor72172e92012-01-05 21:55:30 +00001820VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001821 return new (C, ID)
1822 VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
1823 QualType(), nullptr, SC_None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001824}
1825
Douglas Gregorbf62d642010-12-06 18:36:25 +00001826void VarDecl::setStorageClass(StorageClass SC) {
1827 assert(isLegalForVariable(SC));
John McCallbeaa11c2011-05-01 02:13:58 +00001828 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001829}
1830
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001831VarDecl::TLSKind VarDecl::getTLSKind() const {
1832 switch (VarDeclBits.TSCSpec) {
1833 case TSCS_unspecified:
Samuel Antaof8b50122015-07-13 22:54:53 +00001834 if (!hasAttr<ThreadAttr>() &&
1835 !(getASTContext().getLangOpts().OpenMPUseTLS &&
1836 getASTContext().getTargetInfo().isTLSSupported() &&
1837 hasAttr<OMPThreadPrivateDeclAttr>()))
David Majnemer1b5baff2015-05-14 05:19:23 +00001838 return TLS_None;
Samuel Antaof8b50122015-07-13 22:54:53 +00001839 return ((getASTContext().getLangOpts().isCompatibleWithMSVC(
1840 LangOptions::MSVC2015)) ||
1841 hasAttr<OMPThreadPrivateDeclAttr>())
David Majnemer1b5baff2015-05-14 05:19:23 +00001842 ? TLS_Dynamic
1843 : TLS_Static;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001844 case TSCS___thread: // Fall through.
1845 case TSCS__Thread_local:
Samuel Antaof8b50122015-07-13 22:54:53 +00001846 return TLS_Static;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001847 case TSCS_thread_local:
1848 return TLS_Dynamic;
1849 }
1850 llvm_unreachable("Unknown thread storage class specifier!");
1851}
1852
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001853SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001854 if (const Expr *Init = getInit()) {
1855 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001856 // If Init is implicit, ignore its source range and fallback on
1857 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1858 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001859 return SourceRange(getOuterLocStart(), InitEnd);
1860 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001861 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001862}
1863
Rafael Espindola88510672013-01-04 21:18:45 +00001864template<typename T>
Alp Toker84212df2014-05-31 06:11:02 +00001865static LanguageLinkage getDeclLanguageLinkage(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001866 // C++ [dcl.link]p1: All function types, function names with external linkage,
1867 // and variable names with external linkage have a language linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +00001868 if (!D.hasExternalFormalLinkage())
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001869 return NoLanguageLinkage;
1870
1871 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001872 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001873 ASTContext &Context = D.getASTContext();
1874 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001875 return CLanguageLinkage;
1876
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001877 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1878 // language linkage of the names of class members and the function type of
1879 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001880 const DeclContext *DC = D.getDeclContext();
1881 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001882 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001883
1884 // If the first decl is in an extern "C" context, any other redeclaration
1885 // will have C language linkage. If the first one is not in an extern "C"
1886 // context, we would have reported an error for any other decl being in one.
Rafael Espindola593537a2013-05-05 20:15:21 +00001887 if (isFirstInExternCContext(&D))
Rafael Espindolaf4187652013-02-14 01:18:37 +00001888 return CLanguageLinkage;
1889 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001890}
1891
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001892template<typename T>
Alp Toker84212df2014-05-31 06:11:02 +00001893static bool isDeclExternC(const T &D) {
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001894 // Since the context is ignored for class members, they can only have C++
1895 // language linkage or no language linkage.
1896 const DeclContext *DC = D.getDeclContext();
1897 if (DC->isRecord()) {
1898 assert(D.getASTContext().getLangOpts().CPlusPlus);
1899 return false;
1900 }
1901
1902 return D.getLanguageLinkage() == CLanguageLinkage;
1903}
1904
Rafael Espindolaf4187652013-02-14 01:18:37 +00001905LanguageLinkage VarDecl::getLanguageLinkage() const {
Alp Toker84212df2014-05-31 06:11:02 +00001906 return getDeclLanguageLinkage(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001907}
1908
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001909bool VarDecl::isExternC() const {
Alp Toker84212df2014-05-31 06:11:02 +00001910 return isDeclExternC(*this);
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001911}
1912
Rafael Espindola593537a2013-05-05 20:15:21 +00001913bool VarDecl::isInExternCContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00001914 return getLexicalDeclContext()->isExternCContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00001915}
1916
1917bool VarDecl::isInExternCXXContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00001918 return getLexicalDeclContext()->isExternCXXContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00001919}
1920
Rafael Espindola8db352d2013-10-17 15:37:26 +00001921VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); }
Sebastian Redl833ef452010-01-26 22:01:41 +00001922
Nico Weber7f5015a2015-04-15 21:53:00 +00001923VarDecl::DefinitionKind
1924VarDecl::isThisDeclarationADefinition(ASTContext &C) const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001925 // C++ [basic.def]p2:
1926 // A declaration is a definition unless [...] it contains the 'extern'
1927 // specifier or a linkage-specification and neither an initializer [...],
Richard Smith62f19e72016-06-25 00:15:56 +00001928 // it declares a non-inline static data member in a class declaration [...],
1929 // it declares a static data member outside a class definition and the variable
1930 // was defined within the class with the constexpr specifier [...],
Richard Smith8809a0c2013-09-27 20:14:12 +00001931 // C++1y [temp.expl.spec]p15:
1932 // An explicit specialization of a static data member or an explicit
1933 // specialization of a static data member template is a definition if the
1934 // declaration includes an initializer; otherwise, it is a declaration.
1935 //
1936 // FIXME: How do you declare (but not define) a partial specialization of
1937 // a static data member template outside the containing class?
Richard Smithedbc6e92016-10-14 21:41:24 +00001938 if (isThisDeclarationADemotedDefinition())
1939 return DeclarationOnly;
1940
Sebastian Redl35351a92010-01-31 22:27:38 +00001941 if (isStaticDataMember()) {
Richard Smith8809a0c2013-09-27 20:14:12 +00001942 if (isOutOfLine() &&
Richard Smith62f19e72016-06-25 00:15:56 +00001943 !(getCanonicalDecl()->isInline() &&
1944 getCanonicalDecl()->isConstexpr()) &&
Richard Smith8809a0c2013-09-27 20:14:12 +00001945 (hasInit() ||
1946 // If the first declaration is out-of-line, this may be an
1947 // instantiation of an out-of-line partial specialization of a variable
1948 // template for which we have not yet instantiated the initializer.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001949 (getFirstDecl()->isOutOfLine()
Richard Smith8809a0c2013-09-27 20:14:12 +00001950 ? getTemplateSpecializationKind() == TSK_Undeclared
1951 : getTemplateSpecializationKind() !=
1952 TSK_ExplicitSpecialization) ||
1953 isa<VarTemplatePartialSpecializationDecl>(this)))
Sebastian Redl35351a92010-01-31 22:27:38 +00001954 return Definition;
Richard Smith62f19e72016-06-25 00:15:56 +00001955 else if (!isOutOfLine() && isInline())
1956 return Definition;
Sebastian Redl35351a92010-01-31 22:27:38 +00001957 else
1958 return DeclarationOnly;
1959 }
1960 // C99 6.7p5:
1961 // A definition of an identifier is a declaration for that identifier that
1962 // [...] causes storage to be reserved for that object.
1963 // Note: that applies for all non-file-scope objects.
1964 // C99 6.9.2p1:
1965 // If the declaration of an identifier for an object has file scope and an
1966 // initializer, the declaration is an external definition for the identifier
1967 if (hasInit())
1968 return Definition;
Rafael Espindolabff59562013-04-25 12:11:36 +00001969
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001970 if (hasDefiningAttr())
Rafael Espindolad53ffa02013-10-22 21:39:03 +00001971 return Definition;
1972
David Majnemerdd0bed12015-05-14 05:19:20 +00001973 if (const auto *SAA = getAttr<SelectAnyAttr>())
1974 if (!SAA->isInherited())
1975 return Definition;
1976
Richard Smith8809a0c2013-09-27 20:14:12 +00001977 // A variable template specialization (other than a static data member
1978 // template or an explicit specialization) is a declaration until we
1979 // instantiate its initializer.
1980 if (isa<VarTemplateSpecializationDecl>(this) &&
1981 getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1982 return DeclarationOnly;
1983
Nico Weber608e7682015-04-15 23:04:24 +00001984 if (hasExternalStorage())
Sebastian Redl35351a92010-01-31 22:27:38 +00001985 return DeclarationOnly;
Rafael Espindola8f326a52013-03-07 01:42:44 +00001986
Rafael Espindolabff59562013-04-25 12:11:36 +00001987 // [dcl.link] p7:
1988 // A declaration directly contained in a linkage-specification is treated
1989 // as if it contains the extern specifier for the purpose of determining
1990 // the linkage of the declared name and whether it is a definition.
Nico Weber608e7682015-04-15 23:04:24 +00001991 if (isSingleLineLanguageLinkage(*this))
Rafael Espindola327be3c2013-04-26 01:30:23 +00001992 return DeclarationOnly;
Rafael Espindolabff59562013-04-25 12:11:36 +00001993
Sebastian Redl35351a92010-01-31 22:27:38 +00001994 // C99 6.9.2p2:
1995 // A declaration of an object that has file scope without an initializer,
1996 // and without a storage class specifier or the scs 'static', constitutes
1997 // a tentative definition.
1998 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001999 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00002000 return TentativeDefinition;
2001
2002 // What's left is (in C, block-scope) declarations without initializers or
2003 // external storage. These are definitions.
2004 return Definition;
2005}
2006
Sebastian Redl35351a92010-01-31 22:27:38 +00002007VarDecl *VarDecl::getActingDefinition() {
2008 DefinitionKind Kind = isThisDeclarationADefinition();
2009 if (Kind != TentativeDefinition)
Craig Topper36250ad2014-05-12 05:36:57 +00002010 return nullptr;
Sebastian Redl35351a92010-01-31 22:27:38 +00002011
Craig Topper36250ad2014-05-12 05:36:57 +00002012 VarDecl *LastTentative = nullptr;
Rafael Espindola8db352d2013-10-17 15:37:26 +00002013 VarDecl *First = getFirstDecl();
Aaron Ballman86c93902014-03-06 23:45:36 +00002014 for (auto I : First->redecls()) {
2015 Kind = I->isThisDeclarationADefinition();
Sebastian Redl35351a92010-01-31 22:27:38 +00002016 if (Kind == Definition)
Craig Topper36250ad2014-05-12 05:36:57 +00002017 return nullptr;
Sebastian Redl35351a92010-01-31 22:27:38 +00002018 else if (Kind == TentativeDefinition)
Aaron Ballman86c93902014-03-06 23:45:36 +00002019 LastTentative = I;
Sebastian Redl35351a92010-01-31 22:27:38 +00002020 }
2021 return LastTentative;
2022}
2023
Daniel Dunbar9d355812012-03-09 01:51:51 +00002024VarDecl *VarDecl::getDefinition(ASTContext &C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002025 VarDecl *First = getFirstDecl();
Aaron Ballman86c93902014-03-06 23:45:36 +00002026 for (auto I : First->redecls()) {
2027 if (I->isThisDeclarationADefinition(C) == Definition)
2028 return I;
Sebastian Redl5ca79842010-02-01 20:16:42 +00002029 }
Craig Topper36250ad2014-05-12 05:36:57 +00002030 return nullptr;
Sebastian Redl5ca79842010-02-01 20:16:42 +00002031}
2032
Daniel Dunbar9d355812012-03-09 01:51:51 +00002033VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00002034 DefinitionKind Kind = DeclarationOnly;
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002035
Rafael Espindola8db352d2013-10-17 15:37:26 +00002036 const VarDecl *First = getFirstDecl();
Aaron Ballman86c93902014-03-06 23:45:36 +00002037 for (auto I : First->redecls()) {
2038 Kind = std::max(Kind, I->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00002039 if (Kind == Definition)
2040 break;
2041 }
John McCall37bb6c92010-10-29 22:22:43 +00002042
2043 return Kind;
2044}
2045
Sebastian Redl5ca79842010-02-01 20:16:42 +00002046const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Aaron Ballman86c93902014-03-06 23:45:36 +00002047 for (auto I : redecls()) {
2048 if (auto Expr = I->getInit()) {
2049 D = I;
2050 return Expr;
2051 }
Sebastian Redl833ef452010-01-26 22:01:41 +00002052 }
Craig Topper36250ad2014-05-12 05:36:57 +00002053 return nullptr;
Sebastian Redl833ef452010-01-26 22:01:41 +00002054}
2055
Chandler Carruth813faed2015-12-30 02:51:00 +00002056bool VarDecl::hasInit() const {
2057 if (auto *P = dyn_cast<ParmVarDecl>(this))
2058 if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg())
2059 return false;
2060
2061 return !Init.isNull();
2062}
2063
2064Expr *VarDecl::getInit() {
2065 if (!hasInit())
2066 return nullptr;
2067
2068 if (auto *S = Init.dyn_cast<Stmt *>())
2069 return cast<Expr>(S);
2070
2071 return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value);
2072}
2073
2074Stmt **VarDecl::getInitAddress() {
2075 if (auto *ES = Init.dyn_cast<EvaluatedStmt *>())
2076 return &ES->Value;
2077
2078 return Init.getAddrOfPtr1();
2079}
2080
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002081bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002082 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002083 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00002084
2085 if (!isStaticDataMember())
2086 return false;
2087
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002088 // If this static data member was instantiated from a static data member of
2089 // a class template, check whether that static data member was defined
2090 // out-of-line.
2091 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
2092 return VD->isOutOfLine();
2093
2094 return false;
2095}
2096
Douglas Gregord5058122010-02-11 01:19:42 +00002097void VarDecl::setInit(Expr *I) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002098 if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002099 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00002100 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00002101 }
2102
2103 Init = I;
2104}
2105
Daniel Dunbar9d355812012-03-09 01:51:51 +00002106bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002107 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00002108
Richard Smith35ecb362012-03-02 04:14:40 +00002109 if (!Lang.CPlusPlus)
2110 return false;
2111
2112 // In C++11, any variable of reference type can be used in a constant
2113 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002114 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00002115 return true;
2116
2117 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00002118 // not require the variable to be non-volatile, but we consider this to be a
2119 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00002120 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00002121 return false;
2122
2123 // In C++, const, non-volatile variables of integral or enumeration types
2124 // can be used in constant expressions.
2125 if (getType()->isIntegralOrEnumerationType())
2126 return true;
2127
Richard Smith35ecb362012-03-02 04:14:40 +00002128 // Additionally, in C++11, non-volatile constexpr variables can be used in
2129 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002130 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00002131}
2132
Richard Smithd0b4dd62011-12-19 06:19:21 +00002133/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
2134/// form, which contains extra information on the evaluated value of the
2135/// initializer.
2136EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002137 auto *Eval = Init.dyn_cast<EvaluatedStmt *>();
Richard Smithd0b4dd62011-12-19 06:19:21 +00002138 if (!Eval) {
Manuel Klimeka7328992013-06-03 13:51:33 +00002139 // Note: EvaluatedStmt contains an APValue, which usually holds
2140 // resources not allocated from the ASTContext. We need to do some
2141 // work to avoid leaking those, but we do so in VarDecl::evaluateValue
2142 // where we can detect whether there's anything to clean up or not.
Richard Smithd0b4dd62011-12-19 06:19:21 +00002143 Eval = new (getASTContext()) EvaluatedStmt;
Chandler Carruth813faed2015-12-30 02:51:00 +00002144 Eval->Value = Init.get<Stmt *>();
Richard Smithd0b4dd62011-12-19 06:19:21 +00002145 Init = Eval;
2146 }
2147 return Eval;
2148}
2149
Richard Smithdafff942012-01-14 04:30:29 +00002150APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002151 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00002152 return evaluateValue(Notes);
2153}
2154
2155APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002156 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00002157 EvaluatedStmt *Eval = ensureEvaluatedStmt();
2158
2159 // We only produce notes indicating why an initializer is non-constant the
2160 // first time it is evaluated. FIXME: The notes won't always be emitted the
2161 // first time we try evaluation, so might not be produced at all.
2162 if (Eval->WasEvaluated)
Craig Topper36250ad2014-05-12 05:36:57 +00002163 return Eval->Evaluated.isUninit() ? nullptr : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002164
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002165 const auto *Init = cast<Expr>(Eval->Value);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002166 assert(!Init->isValueDependent());
2167
2168 if (Eval->IsEvaluating) {
2169 // FIXME: Produce a diagnostic for self-initialization.
2170 Eval->CheckedICE = true;
2171 Eval->IsICE = false;
Craig Topper36250ad2014-05-12 05:36:57 +00002172 return nullptr;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002173 }
2174
2175 Eval->IsEvaluating = true;
2176
2177 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
2178 this, Notes);
2179
Manuel Klimeka7328992013-06-03 13:51:33 +00002180 // Ensure the computed APValue is cleaned up later if evaluation succeeded,
2181 // or that it's empty (so that there's nothing to clean up) if evaluation
2182 // failed.
Richard Smithd0b4dd62011-12-19 06:19:21 +00002183 if (!Result)
2184 Eval->Evaluated = APValue();
Manuel Klimeka7328992013-06-03 13:51:33 +00002185 else if (Eval->Evaluated.needsCleanup())
George Burgess IVb61bfbd2017-02-14 05:37:36 +00002186 getASTContext().addDestruction(&Eval->Evaluated);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002187
2188 Eval->IsEvaluating = false;
2189 Eval->WasEvaluated = true;
2190
2191 // In C++11, we have determined whether the initializer was a constant
2192 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002193 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00002194 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00002195 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00002196 }
2197
Craig Topper36250ad2014-05-12 05:36:57 +00002198 return Result ? &Eval->Evaluated : nullptr;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002199}
2200
Chandler Carruth813faed2015-12-30 02:51:00 +00002201APValue *VarDecl::getEvaluatedValue() const {
2202 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
2203 if (Eval->WasEvaluated)
2204 return &Eval->Evaluated;
2205
2206 return nullptr;
2207}
2208
2209bool VarDecl::isInitKnownICE() const {
2210 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
2211 return Eval->CheckedICE;
2212
2213 return false;
2214}
2215
2216bool VarDecl::isInitICE() const {
2217 assert(isInitKnownICE() &&
2218 "Check whether we already know that the initializer is an ICE");
2219 return Init.get<EvaluatedStmt *>()->IsICE;
2220}
2221
Richard Smithd0b4dd62011-12-19 06:19:21 +00002222bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00002223 // Initializers of weak variables are never ICEs.
2224 if (isWeak())
2225 return false;
2226
Richard Smithd0b4dd62011-12-19 06:19:21 +00002227 EvaluatedStmt *Eval = ensureEvaluatedStmt();
2228 if (Eval->CheckedICE)
2229 // We have already checked whether this subexpression is an
2230 // integral constant expression.
2231 return Eval->IsICE;
2232
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002233 const auto *Init = cast<Expr>(Eval->Value);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002234 assert(!Init->isValueDependent());
2235
2236 // In C++11, evaluate the initializer to check whether it's a constant
2237 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002238 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002239 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002240 evaluateValue(Notes);
2241 return Eval->IsICE;
2242 }
2243
2244 // It's an ICE whether or not the definition we found is
2245 // out-of-line. See DR 721 and the discussion in Clang PR
2246 // 6206 for details.
2247
2248 if (Eval->CheckingICE)
2249 return false;
2250 Eval->CheckingICE = true;
2251
2252 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
2253 Eval->CheckingICE = false;
2254 Eval->CheckedICE = true;
2255 return Eval->IsICE;
2256}
2257
Richard Smith2195ec92017-04-21 01:15:13 +00002258template<typename DeclT>
2259static DeclT *getDefinitionOrSelf(DeclT *D) {
2260 assert(D);
2261 if (auto *Def = D->getDefinition())
2262 return Def;
2263 return D;
2264}
2265
Richard Smithedbc6e92016-10-14 21:41:24 +00002266VarDecl *VarDecl::getTemplateInstantiationPattern() const {
2267 // If it's a variable template specialization, find the template or partial
2268 // specialization from which it was instantiated.
2269 if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(this)) {
2270 auto From = VDTemplSpec->getInstantiatedFrom();
2271 if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) {
2272 while (auto *NewVTD = VTD->getInstantiatedFromMemberTemplate()) {
2273 if (NewVTD->isMemberSpecialization())
2274 break;
2275 VTD = NewVTD;
2276 }
Richard Smith2195ec92017-04-21 01:15:13 +00002277 return getDefinitionOrSelf(VTD->getTemplatedDecl());
Richard Smithedbc6e92016-10-14 21:41:24 +00002278 }
2279 if (auto *VTPSD =
2280 From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
2281 while (auto *NewVTPSD = VTPSD->getInstantiatedFromMember()) {
2282 if (NewVTPSD->isMemberSpecialization())
2283 break;
2284 VTPSD = NewVTPSD;
2285 }
Richard Smith2195ec92017-04-21 01:15:13 +00002286 return getDefinitionOrSelf<VarDecl>(VTPSD);
Richard Smithedbc6e92016-10-14 21:41:24 +00002287 }
2288 }
2289
2290 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
2291 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
2292 VarDecl *VD = getInstantiatedFromStaticDataMember();
2293 while (auto *NewVD = VD->getInstantiatedFromStaticDataMember())
2294 VD = NewVD;
Richard Smith2195ec92017-04-21 01:15:13 +00002295 return getDefinitionOrSelf(VD);
Richard Smithedbc6e92016-10-14 21:41:24 +00002296 }
2297 }
2298
2299 if (VarTemplateDecl *VarTemplate = getDescribedVarTemplate()) {
Richard Smithedbc6e92016-10-14 21:41:24 +00002300 while (VarTemplate->getInstantiatedFromMemberTemplate()) {
2301 if (VarTemplate->isMemberSpecialization())
2302 break;
2303 VarTemplate = VarTemplate->getInstantiatedFromMemberTemplate();
2304 }
2305
Richard Smith2195ec92017-04-21 01:15:13 +00002306 return getDefinitionOrSelf(VarTemplate->getTemplatedDecl());
Richard Smithedbc6e92016-10-14 21:41:24 +00002307 }
2308 return nullptr;
2309}
2310
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002311VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002312 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00002313 return cast<VarDecl>(MSI->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00002314
2315 return nullptr;
Douglas Gregor86d142a2009-10-08 07:24:58 +00002316}
2317
Douglas Gregor3c74d412009-10-14 20:14:33 +00002318TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002319 if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this))
Larisse Voufo39a1e502013-08-06 01:03:05 +00002320 return Spec->getSpecializationKind();
2321
Sebastian Redl35351a92010-01-31 22:27:38 +00002322 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00002323 return MSI->getTemplateSpecializationKind();
Richard Smith8809a0c2013-09-27 20:14:12 +00002324
Douglas Gregor86d142a2009-10-08 07:24:58 +00002325 return TSK_Undeclared;
2326}
2327
Richard Smith8809a0c2013-09-27 20:14:12 +00002328SourceLocation VarDecl::getPointOfInstantiation() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002329 if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this))
Richard Smith8809a0c2013-09-27 20:14:12 +00002330 return Spec->getPointOfInstantiation();
2331
2332 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2333 return MSI->getPointOfInstantiation();
2334
2335 return SourceLocation();
2336}
2337
Larisse Voufo39a1e502013-08-06 01:03:05 +00002338VarTemplateDecl *VarDecl::getDescribedVarTemplate() const {
2339 return getASTContext().getTemplateOrSpecializationInfo(this)
2340 .dyn_cast<VarTemplateDecl *>();
2341}
2342
2343void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) {
2344 getASTContext().setTemplateOrSpecializationInfo(this, Template);
2345}
2346
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002347MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Richard Smithb71782b2013-08-01 04:12:04 +00002348 if (isStaticDataMember())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002349 // FIXME: Remove ?
2350 // return getASTContext().getInstantiatedFromStaticDataMember(this);
2351 return getASTContext().getTemplateOrSpecializationInfo(this)
2352 .dyn_cast<MemberSpecializationInfo *>();
Craig Topper36250ad2014-05-12 05:36:57 +00002353 return nullptr;
Douglas Gregor06db9f52009-10-12 20:18:28 +00002354}
2355
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002356void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2357 SourceLocation PointOfInstantiation) {
Richard Smith8809a0c2013-09-27 20:14:12 +00002358 assert((isa<VarTemplateSpecializationDecl>(this) ||
2359 getMemberSpecializationInfo()) &&
2360 "not a variable or static data member template specialization");
2361
Larisse Voufo39a1e502013-08-06 01:03:05 +00002362 if (VarTemplateSpecializationDecl *Spec =
2363 dyn_cast<VarTemplateSpecializationDecl>(this)) {
2364 Spec->setSpecializationKind(TSK);
2365 if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() &&
2366 Spec->getPointOfInstantiation().isInvalid())
2367 Spec->setPointOfInstantiation(PointOfInstantiation);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002368 }
2369
2370 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) {
2371 MSI->setTemplateSpecializationKind(TSK);
2372 if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() &&
2373 MSI->getPointOfInstantiation().isInvalid())
2374 MSI->setPointOfInstantiation(PointOfInstantiation);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002375 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00002376}
2377
2378void
2379VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD,
2380 TemplateSpecializationKind TSK) {
2381 assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() &&
2382 "Previous template or instantiation?");
2383 getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00002384}
2385
Sebastian Redl833ef452010-01-26 22:01:41 +00002386//===----------------------------------------------------------------------===//
2387// ParmVarDecl Implementation
2388//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00002389
Sebastian Redl833ef452010-01-26 22:01:41 +00002390ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002391 SourceLocation StartLoc,
2392 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00002393 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002394 StorageClass S, Expr *DefArg) {
Richard Smith053f6c62014-05-16 23:01:30 +00002395 return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002396 S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00002397}
2398
Reid Kleckner8a365022013-06-24 17:51:48 +00002399QualType ParmVarDecl::getOriginalType() const {
2400 TypeSourceInfo *TSI = getTypeSourceInfo();
2401 QualType T = TSI ? TSI->getType() : getType();
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002402 if (const auto *DT = dyn_cast<DecayedType>(T))
Reid Kleckner8a365022013-06-24 17:51:48 +00002403 return DT->getOriginalType();
2404 return T;
2405}
2406
Douglas Gregor72172e92012-01-05 21:55:30 +00002407ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002408 return new (C, ID)
2409 ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(),
2410 nullptr, QualType(), nullptr, SC_None, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002411}
2412
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00002413SourceRange ParmVarDecl::getSourceRange() const {
2414 if (!hasInheritedDefaultArg()) {
2415 SourceRange ArgRange = getDefaultArgRange();
2416 if (ArgRange.isValid())
2417 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
2418 }
2419
Argyrios Kyrtzidisa0772792013-04-17 01:56:48 +00002420 // DeclaratorDecl considers the range of postfix types as overlapping with the
2421 // declaration name, but this is not the case with parameters in ObjC methods.
2422 if (isa<ObjCMethodDecl>(getDeclContext()))
2423 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
2424
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00002425 return DeclaratorDecl::getSourceRange();
2426}
2427
Sebastian Redl833ef452010-01-26 22:01:41 +00002428Expr *ParmVarDecl::getDefaultArg() {
2429 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
2430 assert(!hasUninstantiatedDefaultArg() &&
2431 "Default argument is not yet instantiated!");
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002432
Sebastian Redl833ef452010-01-26 22:01:41 +00002433 Expr *Arg = getInit();
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002434 if (auto *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00002435 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00002436
Sebastian Redl833ef452010-01-26 22:01:41 +00002437 return Arg;
2438}
2439
Chandler Carruth813faed2015-12-30 02:51:00 +00002440void ParmVarDecl::setDefaultArg(Expr *defarg) {
2441 ParmVarDeclBits.DefaultArgKind = DAK_Normal;
2442 Init = defarg;
2443}
Sebastian Redl833ef452010-01-26 22:01:41 +00002444
Chandler Carruth813faed2015-12-30 02:51:00 +00002445SourceRange ParmVarDecl::getDefaultArgRange() const {
2446 switch (ParmVarDeclBits.DefaultArgKind) {
2447 case DAK_None:
2448 case DAK_Unparsed:
2449 // Nothing we can do here.
2450 return SourceRange();
2451
2452 case DAK_Uninstantiated:
Sebastian Redl833ef452010-01-26 22:01:41 +00002453 return getUninstantiatedDefaultArg()->getSourceRange();
2454
Chandler Carruth813faed2015-12-30 02:51:00 +00002455 case DAK_Normal:
2456 if (const Expr *E = getInit())
2457 return E->getSourceRange();
2458
2459 // Missing an actual expression, may be invalid.
2460 return SourceRange();
2461 }
2462 llvm_unreachable("Invalid default argument kind.");
2463}
2464
2465void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) {
2466 ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated;
2467 Init = arg;
2468}
2469
2470Expr *ParmVarDecl::getUninstantiatedDefaultArg() {
2471 assert(hasUninstantiatedDefaultArg() &&
2472 "Wrong kind of initialization expression!");
2473 return cast_or_null<Expr>(Init.get<Stmt *>());
2474}
2475
2476bool ParmVarDecl::hasDefaultArg() const {
2477 // FIXME: We should just return false for DAK_None here once callers are
2478 // prepared for the case that we encountered an invalid default argument and
2479 // were unable to even build an invalid expression.
2480 return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() ||
2481 !Init.isNull();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00002482}
2483
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00002484bool ParmVarDecl::isParameterPack() const {
2485 return isa<PackExpansionType>(getType());
2486}
2487
Ted Kremenek540017e2011-10-06 05:00:56 +00002488void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2489 getASTContext().setParameterIndex(this, parameterIndex);
2490 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2491}
2492
2493unsigned ParmVarDecl::getParameterIndexLarge() const {
2494 return getASTContext().getParameterIndex(this);
2495}
2496
Nuno Lopes394ec982008-12-17 23:39:55 +00002497//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002498// FunctionDecl Implementation
2499//===----------------------------------------------------------------------===//
2500
Benjamin Kramer9170e912013-02-22 15:46:01 +00002501void FunctionDecl::getNameForDiagnostic(
2502 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2503 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002504 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2505 if (TemplateArgs)
Benjamin Kramer9170e912013-02-22 15:46:01 +00002506 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00002507 OS, TemplateArgs->asArray(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002508}
2509
Ted Kremenek186a0742010-04-29 16:49:01 +00002510bool FunctionDecl::isVariadic() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002511 if (const auto *FT = getType()->getAs<FunctionProtoType>())
Ted Kremenek186a0742010-04-29 16:49:01 +00002512 return FT->isVariadic();
2513 return false;
2514}
2515
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002516bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
Aaron Ballman86c93902014-03-06 23:45:36 +00002517 for (auto I : redecls()) {
Serge Pavlov9a618e12017-02-15 12:30:35 +00002518 if (I->doesThisDeclarationHaveABody()) {
Aaron Ballman86c93902014-03-06 23:45:36 +00002519 Definition = I;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002520 return true;
2521 }
2522 }
2523
2524 return false;
2525}
2526
Anders Carlsson9bd7d162011-05-14 23:26:09 +00002527bool FunctionDecl::hasTrivialBody() const
2528{
2529 Stmt *S = getBody();
2530 if (!S) {
2531 // Since we don't have a body for this function, we don't know if it's
2532 // trivial or not.
2533 return false;
2534 }
2535
2536 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2537 return true;
2538 return false;
2539}
2540
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002541bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
Aaron Ballman86c93902014-03-06 23:45:36 +00002542 for (auto I : redecls()) {
Serge Pavlov10673c92017-06-04 12:53:12 +00002543 if (I->isThisDeclarationADefinition()) {
2544 Definition = I;
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002545 return true;
2546 }
2547 }
2548
2549 return false;
2550}
2551
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002552Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Rafael Espindola44503012013-10-19 01:37:17 +00002553 if (!hasBody(Definition))
Craig Topper36250ad2014-05-12 05:36:57 +00002554 return nullptr;
Rafael Espindola44503012013-10-19 01:37:17 +00002555
2556 if (Definition->Body)
2557 return Definition->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00002558
Craig Topper36250ad2014-05-12 05:36:57 +00002559 return nullptr;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002560}
2561
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002562void FunctionDecl::setBody(Stmt *B) {
2563 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00002564 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002565 EndRangeLoc = B->getLocEnd();
2566}
2567
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002568void FunctionDecl::setPure(bool P) {
2569 IsPure = P;
2570 if (P)
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002571 if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002572 Parent->markedVirtualFunctionPure();
2573}
2574
Richard Smith8d0dc312013-07-21 23:12:18 +00002575template<std::size_t Len>
2576static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) {
2577 IdentifierInfo *II = ND->getIdentifier();
2578 return II && II->isStr(Str);
2579}
2580
Douglas Gregor16618f22009-09-12 00:17:51 +00002581bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00002582 const TranslationUnitDecl *tunit =
2583 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2584 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002585 !tunit->getASTContext().getLangOpts().Freestanding &&
Richard Smith8d0dc312013-07-21 23:12:18 +00002586 isNamed(this, "main");
John McCall53ffd372011-05-15 17:49:20 +00002587}
2588
David Majnemerc729b0b2013-09-16 22:44:20 +00002589bool FunctionDecl::isMSVCRTEntryPoint() const {
2590 const TranslationUnitDecl *TUnit =
2591 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2592 if (!TUnit)
2593 return false;
2594
2595 // Even though we aren't really targeting MSVCRT if we are freestanding,
2596 // semantic analysis for these functions remains the same.
2597
2598 // MSVCRT entry points only exist on MSVCRT targets.
2599 if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT())
2600 return false;
2601
2602 // Nameless functions like constructors cannot be entry points.
2603 if (!getIdentifier())
2604 return false;
2605
2606 return llvm::StringSwitch<bool>(getName())
2607 .Cases("main", // an ANSI console app
2608 "wmain", // a Unicode console App
2609 "WinMain", // an ANSI GUI app
2610 "wWinMain", // a Unicode GUI app
2611 "DllMain", // a DLL
2612 true)
2613 .Default(false);
2614}
2615
John McCall53ffd372011-05-15 17:49:20 +00002616bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2617 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2618 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2619 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2620 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2621 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2622
Richard Smithf6004412014-01-19 23:25:37 +00002623 if (!getDeclContext()->getRedeclContext()->isTranslationUnit())
2624 return false;
John McCall53ffd372011-05-15 17:49:20 +00002625
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002626 const auto *proto = getType()->castAs<FunctionProtoType>();
Alp Toker9cacbab2014-01-20 20:26:09 +00002627 if (proto->getNumParams() != 2 || proto->isVariadic())
2628 return false;
John McCall53ffd372011-05-15 17:49:20 +00002629
2630 ASTContext &Context =
2631 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2632 ->getASTContext();
2633
2634 // The result type and first argument type are constant across all
2635 // these operators. The second argument must be exactly void*.
Alp Toker9cacbab2014-01-20 20:26:09 +00002636 return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00002637}
2638
Akira Hatanakacae83f72017-06-29 18:48:40 +00002639bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const {
Richard Smith8d0dc312013-07-21 23:12:18 +00002640 if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName)
2641 return false;
2642 if (getDeclName().getCXXOverloadedOperator() != OO_New &&
2643 getDeclName().getCXXOverloadedOperator() != OO_Delete &&
2644 getDeclName().getCXXOverloadedOperator() != OO_Array_New &&
2645 getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
2646 return false;
2647
2648 if (isa<CXXRecordDecl>(getDeclContext()))
2649 return false;
Richard Smithf6004412014-01-19 23:25:37 +00002650
2651 // This can only fail for an invalid 'operator new' declaration.
2652 if (!getDeclContext()->getRedeclContext()->isTranslationUnit())
2653 return false;
Richard Smith8d0dc312013-07-21 23:12:18 +00002654
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002655 const auto *FPT = getType()->castAs<FunctionProtoType>();
Richard Smithb2f0f052016-10-10 18:54:32 +00002656 if (FPT->getNumParams() == 0 || FPT->getNumParams() > 3 || FPT->isVariadic())
Richard Smith8d0dc312013-07-21 23:12:18 +00002657 return false;
2658
2659 // If this is a single-parameter function, it must be a replaceable global
2660 // allocation or deallocation function.
Alp Toker9cacbab2014-01-20 20:26:09 +00002661 if (FPT->getNumParams() == 1)
Richard Smith8d0dc312013-07-21 23:12:18 +00002662 return true;
2663
Richard Smithb2f0f052016-10-10 18:54:32 +00002664 unsigned Params = 1;
2665 QualType Ty = FPT->getParamType(Params);
Richard Smith1cdec012013-09-29 04:40:38 +00002666 ASTContext &Ctx = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002667
2668 auto Consume = [&] {
2669 ++Params;
2670 Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType();
2671 };
2672
2673 // In C++14, the next parameter can be a 'std::size_t' for sized delete.
2674 bool IsSizedDelete = false;
Richard Smithb47c36f2013-11-05 09:12:18 +00002675 if (Ctx.getLangOpts().SizedDeallocation &&
Richard Smithb2f0f052016-10-10 18:54:32 +00002676 (getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2677 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) &&
2678 Ctx.hasSameType(Ty, Ctx.getSizeType())) {
2679 IsSizedDelete = true;
2680 Consume();
2681 }
2682
2683 // In C++17, the next parameter can be a 'std::align_val_t' for aligned
2684 // new/delete.
Akira Hatanakacae83f72017-06-29 18:48:40 +00002685 if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) {
2686 if (IsAligned)
2687 *IsAligned = true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002688 Consume();
Akira Hatanakacae83f72017-06-29 18:48:40 +00002689 }
Richard Smithb2f0f052016-10-10 18:54:32 +00002690
2691 // Finally, if this is not a sized delete, the final parameter can
2692 // be a 'const std::nothrow_t&'.
2693 if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) {
2694 Ty = Ty->getPointeeType();
2695 if (Ty.getCVRQualifiers() != Qualifiers::Const)
2696 return false;
2697 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2698 if (RD && isNamed(RD, "nothrow_t") && RD->isInStdNamespace())
2699 Consume();
2700 }
2701
2702 return Params == FPT->getNumParams();
Richard Smith8d0dc312013-07-21 23:12:18 +00002703}
2704
Rafael Espindolaf4187652013-02-14 01:18:37 +00002705LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Alp Toker84212df2014-05-31 06:11:02 +00002706 return getDeclLanguageLinkage(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00002707}
2708
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002709bool FunctionDecl::isExternC() const {
Alp Toker84212df2014-05-31 06:11:02 +00002710 return isDeclExternC(*this);
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002711}
2712
Rafael Espindola593537a2013-05-05 20:15:21 +00002713bool FunctionDecl::isInExternCContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00002714 return getLexicalDeclContext()->isExternCContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00002715}
2716
2717bool FunctionDecl::isInExternCXXContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00002718 return getLexicalDeclContext()->isExternCXXContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00002719}
2720
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002721bool FunctionDecl::isGlobal() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002722 if (const auto *Method = dyn_cast<CXXMethodDecl>(this))
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002723 return Method->isStatic();
2724
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002725 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002726 return false;
2727
Mike Stump11289f42009-09-09 15:08:12 +00002728 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002729 DC->isNamespace();
2730 DC = DC->getParent()) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002731 if (const auto *Namespace = cast<NamespaceDecl>(DC)) {
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002732 if (!Namespace->getDeclName())
2733 return false;
2734 break;
2735 }
2736 }
2737
2738 return true;
2739}
2740
Richard Smith10876ef2013-01-17 01:30:42 +00002741bool FunctionDecl::isNoReturn() const {
Adrian Prantl721c7cb2016-08-17 16:42:15 +00002742 if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
2743 hasAttr<C11NoReturnAttr>())
2744 return true;
2745
2746 if (auto *FnTy = getType()->getAs<FunctionType>())
2747 return FnTy->getNoReturnAttr();
2748
2749 return false;
Richard Smith10876ef2013-01-17 01:30:42 +00002750}
2751
Sebastian Redl833ef452010-01-26 22:01:41 +00002752void
2753FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002754 redeclarable_base::setPreviousDecl(PrevDecl);
Sebastian Redl833ef452010-01-26 22:01:41 +00002755
2756 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2757 FunctionTemplateDecl *PrevFunTmpl
Craig Topper36250ad2014-05-12 05:36:57 +00002758 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr;
Sebastian Redl833ef452010-01-26 22:01:41 +00002759 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
Rafael Espindola8db352d2013-10-17 15:37:26 +00002760 FunTmpl->setPreviousDecl(PrevFunTmpl);
Sebastian Redl833ef452010-01-26 22:01:41 +00002761 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00002762
Axel Naumannfbc7b982011-11-08 18:21:06 +00002763 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00002764 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00002765}
2766
Rafael Espindola8db352d2013-10-17 15:37:26 +00002767FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); }
Sebastian Redl833ef452010-01-26 22:01:41 +00002768
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002769/// \brief Returns a value indicating whether this function
2770/// corresponds to a builtin function.
2771///
2772/// The function corresponds to a built-in function if it is
2773/// declared at translation scope or within an extern "C" block and
2774/// its name matches with the name of a builtin. The returned value
2775/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00002776/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002777/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00002778unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00002779 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00002780 return 0;
2781
2782 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00002783 if (!BuiltinID)
2784 return 0;
2785
2786 ASTContext &Context = getASTContext();
Warren Hunt445d83e2013-11-01 23:46:51 +00002787 if (Context.getLangOpts().CPlusPlus) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002788 const auto *LinkageDecl =
2789 dyn_cast<LinkageSpecDecl>(getFirstDecl()->getDeclContext());
Warren Hunt445d83e2013-11-01 23:46:51 +00002790 // In C++, the first declaration of a builtin is always inside an implicit
2791 // extern "C".
2792 // FIXME: A recognised library function may not be directly in an extern "C"
2793 // declaration, for instance "extern "C" { namespace std { decl } }".
David Majnemerba3e5ec2015-03-13 18:26:17 +00002794 if (!LinkageDecl) {
2795 if (BuiltinID == Builtin::BI__GetExceptionInfo &&
David Majnemer145abde2016-01-26 01:12:17 +00002796 Context.getTargetInfo().getCXXABI().isMicrosoft())
David Majnemerba3e5ec2015-03-13 18:26:17 +00002797 return Builtin::BI__GetExceptionInfo;
2798 return 0;
2799 }
2800 if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c)
Warren Hunt445d83e2013-11-01 23:46:51 +00002801 return 0;
2802 }
2803
2804 // If the function is marked "overloadable", it has a different mangled name
2805 // and is not the C library function.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002806 if (hasAttr<OverloadableAttr>())
Warren Hunt445d83e2013-11-01 23:46:51 +00002807 return 0;
2808
Douglas Gregore711f702009-02-14 18:57:46 +00002809 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2810 return BuiltinID;
2811
2812 // This function has the name of a known C library
2813 // function. Determine whether it actually refers to the C library
2814 // function or whether it just has the same name.
2815
Douglas Gregora908e7f2009-02-17 03:23:10 +00002816 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00002817 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00002818 return 0;
2819
Anastasia Stulova1202de32016-02-12 12:07:04 +00002820 // OpenCL v1.2 s6.9.f - The library functions defined in
2821 // the C99 standard headers are not available.
2822 if (Context.getLangOpts().OpenCL &&
2823 Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2824 return 0;
2825
Warren Hunt445d83e2013-11-01 23:46:51 +00002826 return BuiltinID;
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002827}
2828
2829
Chris Lattner47c0d002009-04-25 06:03:53 +00002830/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00002831/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00002832/// after it has been created.
2833unsigned FunctionDecl::getNumParams() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002834 const auto *FPT = getType()->getAs<FunctionProtoType>();
Alp Toker9cacbab2014-01-20 20:26:09 +00002835 return FPT ? FPT->getNumParams() : 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002836}
2837
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002838void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002839 ArrayRef<ParmVarDecl *> NewParamInfo) {
Craig Topper36250ad2014-05-12 05:36:57 +00002840 assert(!ParamInfo && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002841 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002842
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002843 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002844 if (!NewParamInfo.empty()) {
2845 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2846 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002847 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002848}
Chris Lattner41943152007-01-25 04:52:46 +00002849
Chris Lattner58258242008-04-10 02:22:51 +00002850/// getMinRequiredArguments - Returns the minimum number of arguments
2851/// needed to call this function. This may be fewer than the number of
2852/// function parameters, if some of the parameters have default
Richard Smith91151782014-04-01 19:18:16 +00002853/// arguments (in C++) or are parameter packs (C++11).
Chris Lattner58258242008-04-10 02:22:51 +00002854unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002855 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002856 return getNumParams();
Chris Lattner58258242008-04-10 02:22:51 +00002857
Richard Smith91151782014-04-01 19:18:16 +00002858 unsigned NumRequiredArgs = 0;
David Majnemer59f77922016-06-24 04:05:48 +00002859 for (auto *Param : parameters())
Richard Smith91151782014-04-01 19:18:16 +00002860 if (!Param->isParameterPack() && !Param->hasDefaultArg())
2861 ++NumRequiredArgs;
Chris Lattner58258242008-04-10 02:22:51 +00002862 return NumRequiredArgs;
2863}
2864
David Majnemer54e3ba52014-04-02 23:17:29 +00002865/// \brief The combination of the extern and inline keywords under MSVC forces
2866/// the function to be required.
2867///
2868/// Note: This function assumes that we will only get called when isInlined()
2869/// would return true for this FunctionDecl.
2870bool FunctionDecl::isMSExternInline() const {
2871 assert(isInlined() && "expected to get called on an inlined function!");
2872
2873 const ASTContext &Context = getASTContext();
David Majnemer3f021502015-10-08 04:53:31 +00002874 if (!Context.getTargetInfo().getCXXABI().isMicrosoft() &&
2875 !hasAttr<DLLExportAttr>())
David Majnemer54e3ba52014-04-02 23:17:29 +00002876 return false;
2877
David Majnemer73768702015-03-20 00:02:27 +00002878 for (const FunctionDecl *FD = getMostRecentDecl(); FD;
2879 FD = FD->getPreviousDecl())
David Majnemerc0c42f32015-07-10 20:55:38 +00002880 if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern)
David Majnemer54e3ba52014-04-02 23:17:29 +00002881 return true;
2882
2883 return false;
2884}
2885
2886static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) {
2887 if (Redecl->getStorageClass() != SC_Extern)
2888 return false;
2889
2890 for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD;
2891 FD = FD->getPreviousDecl())
David Majnemerc0c42f32015-07-10 20:55:38 +00002892 if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern)
David Majnemer54e3ba52014-04-02 23:17:29 +00002893 return false;
2894
2895 return true;
2896}
2897
Eli Friedman1b125c32012-02-07 03:50:18 +00002898static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2899 // Only consider file-scope declarations in this test.
2900 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2901 return false;
2902
2903 // Only consider explicit declarations; the presence of a builtin for a
2904 // libcall shouldn't affect whether a definition is externally visible.
2905 if (Redecl->isImplicit())
2906 return false;
2907
2908 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2909 return true; // Not an inline definition
2910
2911 return false;
2912}
2913
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002914/// \brief For a function declaration in C or C++, determine whether this
2915/// declaration causes the definition to be externally visible.
2916///
David Majnemer54e3ba52014-04-02 23:17:29 +00002917/// For instance, this determines if adding the current declaration to the set
Eli Friedman1b125c32012-02-07 03:50:18 +00002918/// of redeclarations of the given functions causes
2919/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002920bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
Justin Lebar0241c962016-10-28 16:46:39 +00002921 assert(!doesThisDeclarationHaveABody() &&
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002922 "Must have a declaration without a body.");
2923
2924 ASTContext &Context = getASTContext();
2925
David Majnemer54e3ba52014-04-02 23:17:29 +00002926 if (Context.getLangOpts().MSVCCompat) {
2927 const FunctionDecl *Definition;
2928 if (hasBody(Definition) && Definition->isInlined() &&
2929 redeclForcesDefMSVC(this))
2930 return true;
2931 }
2932
David Blaikiebbafb8a2012-03-11 07:00:24 +00002933 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002934 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2935 // an externally visible definition.
2936 //
2937 // FIXME: What happens if gnu_inline gets added on after the first
2938 // declaration?
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002939 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002940 return false;
2941
2942 const FunctionDecl *Prev = this;
2943 bool FoundBody = false;
2944 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002945 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002946
2947 if (Prev->Body) {
2948 // If it's not the case that both 'inline' and 'extern' are
2949 // specified on the definition, then it is always externally visible.
2950 if (!Prev->isInlineSpecified() ||
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002951 Prev->getStorageClass() != SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002952 return false;
2953 } else if (Prev->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002954 Prev->getStorageClass() != SC_Extern) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002955 return false;
2956 }
2957 }
2958 return FoundBody;
2959 }
2960
David Blaikiebbafb8a2012-03-11 07:00:24 +00002961 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002962 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002963
2964 // C99 6.7.4p6:
2965 // [...] If all of the file scope declarations for a function in a
2966 // translation unit include the inline function specifier without extern,
2967 // then the definition in that translation unit is an inline definition.
2968 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002969 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002970 const FunctionDecl *Prev = this;
2971 bool FoundBody = false;
2972 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002973 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002974 if (RedeclForcesDefC99(Prev))
2975 return false;
2976 }
2977 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002978}
2979
Alp Tokerd0787eb2014-07-02 01:47:15 +00002980SourceRange FunctionDecl::getReturnTypeSourceRange() const {
2981 const TypeSourceInfo *TSI = getTypeSourceInfo();
2982 if (!TSI)
2983 return SourceRange();
Alp Tokerf5b10792014-07-02 12:55:58 +00002984 FunctionTypeLoc FTL =
2985 TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
2986 if (!FTL)
Alp Tokerd0787eb2014-07-02 01:47:15 +00002987 return SourceRange();
2988
Alp Tokerf5b10792014-07-02 12:55:58 +00002989 // Skip self-referential return types.
2990 const SourceManager &SM = getASTContext().getSourceManager();
2991 SourceRange RTRange = FTL.getReturnLoc().getSourceRange();
2992 SourceLocation Boundary = getNameInfo().getLocStart();
2993 if (RTRange.isInvalid() || Boundary.isInvalid() ||
2994 !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary))
2995 return SourceRange();
Alp Tokerd0787eb2014-07-02 01:47:15 +00002996
Alp Tokerf5b10792014-07-02 12:55:58 +00002997 return RTRange;
Alp Tokerd0787eb2014-07-02 01:47:15 +00002998}
2999
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +00003000SourceRange FunctionDecl::getExceptionSpecSourceRange() const {
3001 const TypeSourceInfo *TSI = getTypeSourceInfo();
3002 if (!TSI)
3003 return SourceRange();
3004 FunctionTypeLoc FTL =
3005 TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
3006 if (!FTL)
3007 return SourceRange();
3008
3009 return FTL.getExceptionSpecRange();
3010}
3011
Aaron Ballmane7964782016-03-07 22:44:55 +00003012const Attr *FunctionDecl::getUnusedResultAttr() const {
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00003013 QualType RetType = getReturnType();
3014 if (RetType->isRecordType()) {
Erich Keane4b87d812017-04-19 21:24:55 +00003015 if (const CXXRecordDecl *Ret = RetType->getAsCXXRecordDecl()) {
Aaron Ballmane7964782016-03-07 22:44:55 +00003016 if (const auto *R = Ret->getAttr<WarnUnusedResultAttr>())
3017 return R;
3018 }
3019 } else if (const auto *ET = RetType->getAs<EnumType>()) {
3020 if (const EnumDecl *ED = ET->getDecl()) {
3021 if (const auto *R = ED->getAttr<WarnUnusedResultAttr>())
3022 return R;
3023 }
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00003024 }
Aaron Ballmane7964782016-03-07 22:44:55 +00003025 return getAttr<WarnUnusedResultAttr>();
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00003026}
3027
Richard Smithf3814ad2013-01-25 00:08:28 +00003028/// \brief For an inline function definition in C, or for a gnu_inline function
3029/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00003030///
3031/// Inline function definitions are always available for inlining optimizations.
3032/// However, depending on the language dialect, declaration specifiers, and
3033/// attributes, the definition of an inline function may or may not be
3034/// "externally" visible to other translation units in the program.
3035///
3036/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00003037/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00003038/// inline definition becomes externally visible (C99 6.7.4p6).
3039///
3040/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
3041/// definition, we use the GNU semantics for inline, which are nearly the
3042/// opposite of C99 semantics. In particular, "inline" by itself will create
3043/// an externally visible symbol, but "extern inline" will not create an
3044/// externally visible symbol.
3045bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Manuel Klimekda7456a2016-11-01 10:30:50 +00003046 assert((doesThisDeclarationHaveABody() || willHaveBody()) &&
3047 "Must be a function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00003048 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00003049 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00003050
David Blaikiebbafb8a2012-03-11 07:00:24 +00003051 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00003052 // Note: If you change the logic here, please change
3053 // doesDeclarationForceExternallyVisibleDefinition as well.
3054 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00003055 // If it's not the case that both 'inline' and 'extern' are
3056 // specified on the definition, then this inline definition is
3057 // externally visible.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003058 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregorff76cb92010-12-09 16:59:22 +00003059 return true;
3060
3061 // If any declaration is 'inline' but not 'extern', then this definition
3062 // is externally visible.
Aaron Ballman86c93902014-03-06 23:45:36 +00003063 for (auto Redecl : redecls()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00003064 if (Redecl->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003065 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00003066 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00003067 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00003068
Douglas Gregor76fe50c2009-04-28 06:37:30 +00003069 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00003070 }
Eli Friedman1b125c32012-02-07 03:50:18 +00003071
Richard Smithf3814ad2013-01-25 00:08:28 +00003072 // The rest of this function is C-only.
3073 assert(!Context.getLangOpts().CPlusPlus &&
3074 "should not use C inline rules in C++");
3075
Douglas Gregor299d76e2009-09-13 07:46:26 +00003076 // C99 6.7.4p6:
3077 // [...] If all of the file scope declarations for a function in a
3078 // translation unit include the inline function specifier without extern,
3079 // then the definition in that translation unit is an inline definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00003080 for (auto Redecl : redecls()) {
3081 if (RedeclForcesDefC99(Redecl))
Eli Friedman1b125c32012-02-07 03:50:18 +00003082 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00003083 }
3084
3085 // C99 6.7.4p6:
3086 // An inline definition does not provide an external definition for the
3087 // function, and does not forbid an external definition in another
3088 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00003089 return false;
3090}
3091
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003092/// getOverloadedOperator - Which C++ overloaded operator this
3093/// function represents, if any.
3094OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00003095 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
3096 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003097 else
3098 return OO_None;
3099}
3100
Alexis Huntc88db062010-01-13 09:01:02 +00003101/// getLiteralIdentifier - The literal suffix identifier this function
3102/// represents, if any.
3103const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
3104 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
3105 return getDeclName().getCXXLiteralIdentifier();
3106 else
Craig Topper36250ad2014-05-12 05:36:57 +00003107 return nullptr;
Alexis Huntc88db062010-01-13 09:01:02 +00003108}
3109
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00003110FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
3111 if (TemplateOrSpecialization.isNull())
3112 return TK_NonTemplate;
3113 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
3114 return TK_FunctionTemplate;
3115 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
3116 return TK_MemberSpecialization;
3117 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
3118 return TK_FunctionTemplateSpecialization;
3119 if (TemplateOrSpecialization.is
3120 <DependentFunctionTemplateSpecializationInfo*>())
3121 return TK_DependentFunctionTemplateSpecialization;
3122
David Blaikie83d382b2011-09-23 05:06:16 +00003123 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00003124}
3125
Douglas Gregord801b062009-10-07 23:56:10 +00003126FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00003127 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00003128 return cast<FunctionDecl>(Info->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00003129
3130 return nullptr;
Douglas Gregord801b062009-10-07 23:56:10 +00003131}
3132
Chandler Carruth21c90602015-12-30 03:24:14 +00003133MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
3134 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>();
3135}
3136
Douglas Gregord801b062009-10-07 23:56:10 +00003137void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00003138FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
3139 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00003140 TemplateSpecializationKind TSK) {
3141 assert(TemplateOrSpecialization.isNull() &&
3142 "Member function is already a specialization");
3143 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00003144 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00003145 TemplateOrSpecialization = Info;
3146}
3147
Chandler Carruth21c90602015-12-30 03:24:14 +00003148FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const {
3149 return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>();
3150}
3151
3152void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) {
3153 TemplateOrSpecialization = Template;
3154}
3155
Douglas Gregorafca3b42009-10-27 20:53:28 +00003156bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00003157 // If the function is invalid, it can't be implicitly instantiated.
3158 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00003159 return false;
3160
3161 switch (getTemplateSpecializationKind()) {
3162 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00003163 case TSK_ExplicitInstantiationDefinition:
3164 return false;
3165
3166 case TSK_ImplicitInstantiation:
3167 return true;
3168
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003169 // It is possible to instantiate TSK_ExplicitSpecialization kind
3170 // if the FunctionDecl has a class scope specialization pattern.
3171 case TSK_ExplicitSpecialization:
Craig Topper36250ad2014-05-12 05:36:57 +00003172 return getClassScopeSpecializationPattern() != nullptr;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003173
Douglas Gregorafca3b42009-10-27 20:53:28 +00003174 case TSK_ExplicitInstantiationDeclaration:
3175 // Handled below.
3176 break;
3177 }
3178
3179 // Find the actual template from which we will instantiate.
3180 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003181 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00003182 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003183 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00003184
3185 // C++0x [temp.explicit]p9:
3186 // Except for inline functions, other explicit instantiation declarations
3187 // have the effect of suppressing the implicit instantiation of the entity
3188 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003189 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00003190 return true;
3191
Douglas Gregor583dcaf2009-10-27 21:11:48 +00003192 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00003193}
3194
3195bool FunctionDecl::isTemplateInstantiation() const {
3196 switch (getTemplateSpecializationKind()) {
3197 case TSK_Undeclared:
3198 case TSK_ExplicitSpecialization:
3199 return false;
3200 case TSK_ImplicitInstantiation:
3201 case TSK_ExplicitInstantiationDeclaration:
3202 case TSK_ExplicitInstantiationDefinition:
3203 return true;
3204 }
3205 llvm_unreachable("All TSK values handled.");
3206}
Douglas Gregorafca3b42009-10-27 20:53:28 +00003207
3208FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003209 // Handle class scope explicit specialization special case.
Richard Smith2195ec92017-04-21 01:15:13 +00003210 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {
3211 if (auto *Spec = getClassScopeSpecializationPattern())
3212 return getDefinitionOrSelf(Spec);
3213 return nullptr;
3214 }
3215
Faisal Valib90b2112014-04-03 16:32:21 +00003216 // If this is a generic lambda call operator specialization, its
3217 // instantiation pattern is always its primary template's pattern
3218 // even if its primary template was instantiated from another
3219 // member template (which happens with nested generic lambdas).
3220 // Since a lambda's call operator's body is transformed eagerly,
3221 // we don't have to go hunting for a prototype definition template
3222 // (i.e. instantiated-from-member-template) to use as an instantiation
3223 // pattern.
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003224
Faisal Valib90b2112014-04-03 16:32:21 +00003225 if (isGenericLambdaCallOperatorSpecialization(
3226 dyn_cast<CXXMethodDecl>(this))) {
Richard Smith2195ec92017-04-21 01:15:13 +00003227 assert(getPrimaryTemplate() && "not a generic lambda call operator?");
3228 return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl());
Faisal Valib90b2112014-04-03 16:32:21 +00003229 }
Richard Smith2195ec92017-04-21 01:15:13 +00003230
Douglas Gregorafca3b42009-10-27 20:53:28 +00003231 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
3232 while (Primary->getInstantiatedFromMemberTemplate()) {
3233 // If we have hit a point where the user provided a specialization of
3234 // this template, we're done looking.
3235 if (Primary->isMemberSpecialization())
3236 break;
Douglas Gregorafca3b42009-10-27 20:53:28 +00003237 Primary = Primary->getInstantiatedFromMemberTemplate();
3238 }
Richard Smith2195ec92017-04-21 01:15:13 +00003239
3240 return getDefinitionOrSelf(Primary->getTemplatedDecl());
Douglas Gregorafca3b42009-10-27 20:53:28 +00003241 }
Richard Smith2195ec92017-04-21 01:15:13 +00003242
3243 if (auto *MFD = getInstantiatedFromMemberFunction())
3244 return getDefinitionOrSelf(MFD);
3245
3246 return nullptr;
Douglas Gregorafca3b42009-10-27 20:53:28 +00003247}
3248
Douglas Gregor70d83e22009-06-29 17:30:29 +00003249FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00003250 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00003251 = TemplateOrSpecialization
3252 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00003253 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00003254 }
Craig Topper36250ad2014-05-12 05:36:57 +00003255 return nullptr;
Douglas Gregor70d83e22009-06-29 17:30:29 +00003256}
3257
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003258FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
3259 return getASTContext().getClassScopeSpecializationPattern(this);
3260}
3261
Chandler Carruth21c90602015-12-30 03:24:14 +00003262FunctionTemplateSpecializationInfo *
3263FunctionDecl::getTemplateSpecializationInfo() const {
3264 return TemplateOrSpecialization
3265 .dyn_cast<FunctionTemplateSpecializationInfo *>();
3266}
3267
Douglas Gregor70d83e22009-06-29 17:30:29 +00003268const TemplateArgumentList *
3269FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00003270 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00003271 = TemplateOrSpecialization
3272 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00003273 return Info->TemplateArguments;
3274 }
Craig Topper36250ad2014-05-12 05:36:57 +00003275 return nullptr;
Douglas Gregor70d83e22009-06-29 17:30:29 +00003276}
3277
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00003278const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00003279FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
3280 if (FunctionTemplateSpecializationInfo *Info
3281 = TemplateOrSpecialization
3282 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
3283 return Info->TemplateArgumentsAsWritten;
3284 }
Craig Topper36250ad2014-05-12 05:36:57 +00003285 return nullptr;
Abramo Bagnara02ccd282010-05-20 15:32:11 +00003286}
3287
Mike Stump11289f42009-09-09 15:08:12 +00003288void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00003289FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
3290 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00003291 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003292 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00003293 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00003294 const TemplateArgumentListInfo *TemplateArgsAsWritten,
3295 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003296 assert(TSK != TSK_Undeclared &&
3297 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00003298 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00003299 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00003300 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00003301 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
3302 TemplateArgs,
3303 TemplateArgsAsWritten,
3304 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00003305 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00003306 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00003307}
3308
John McCallb9c78482010-04-08 09:05:18 +00003309void
3310FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
3311 const UnresolvedSetImpl &Templates,
3312 const TemplateArgumentListInfo &TemplateArgs) {
3313 assert(TemplateOrSpecialization.isNull());
John McCallb9c78482010-04-08 09:05:18 +00003314 DependentFunctionTemplateSpecializationInfo *Info =
James Y Knight7a22b242015-08-06 20:26:32 +00003315 DependentFunctionTemplateSpecializationInfo::Create(Context, Templates,
3316 TemplateArgs);
John McCallb9c78482010-04-08 09:05:18 +00003317 TemplateOrSpecialization = Info;
3318}
3319
James Y Knight7a22b242015-08-06 20:26:32 +00003320DependentFunctionTemplateSpecializationInfo *
Chandler Carruth21c90602015-12-30 03:24:14 +00003321FunctionDecl::getDependentSpecializationInfo() const {
3322 return TemplateOrSpecialization
3323 .dyn_cast<DependentFunctionTemplateSpecializationInfo *>();
3324}
3325
3326DependentFunctionTemplateSpecializationInfo *
James Y Knight7a22b242015-08-06 20:26:32 +00003327DependentFunctionTemplateSpecializationInfo::Create(
3328 ASTContext &Context, const UnresolvedSetImpl &Ts,
3329 const TemplateArgumentListInfo &TArgs) {
3330 void *Buffer = Context.Allocate(
3331 totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>(
3332 TArgs.size(), Ts.size()));
3333 return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs);
3334}
3335
John McCallb9c78482010-04-08 09:05:18 +00003336DependentFunctionTemplateSpecializationInfo::
3337DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
3338 const TemplateArgumentListInfo &TArgs)
Benjamin Kramer3ebba522015-04-02 12:43:31 +00003339 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
John McCallb9c78482010-04-08 09:05:18 +00003340
James Y Knight53c76162015-07-17 18:21:37 +00003341 NumTemplates = Ts.size();
3342 NumArgs = TArgs.size();
Benjamin Kramer3ebba522015-04-02 12:43:31 +00003343
James Y Knight7a22b242015-08-06 20:26:32 +00003344 FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>();
John McCallb9c78482010-04-08 09:05:18 +00003345 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
3346 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
3347
James Y Knight7a22b242015-08-06 20:26:32 +00003348 TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>();
John McCallb9c78482010-04-08 09:05:18 +00003349 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
3350 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
3351}
3352
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003353TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00003354 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003355 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00003356 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00003357 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00003358 if (FTSInfo)
3359 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00003360
Douglas Gregord801b062009-10-07 23:56:10 +00003361 MemberSpecializationInfo *MSInfo
3362 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
3363 if (MSInfo)
3364 return MSInfo->getTemplateSpecializationKind();
3365
3366 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003367}
3368
Mike Stump11289f42009-09-09 15:08:12 +00003369void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003370FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3371 SourceLocation PointOfInstantiation) {
3372 if (FunctionTemplateSpecializationInfo *FTSInfo
3373 = TemplateOrSpecialization.dyn_cast<
3374 FunctionTemplateSpecializationInfo*>()) {
3375 FTSInfo->setTemplateSpecializationKind(TSK);
3376 if (TSK != TSK_ExplicitSpecialization &&
3377 PointOfInstantiation.isValid() &&
3378 FTSInfo->getPointOfInstantiation().isInvalid())
3379 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
3380 } else if (MemberSpecializationInfo *MSInfo
3381 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
3382 MSInfo->setTemplateSpecializationKind(TSK);
3383 if (TSK != TSK_ExplicitSpecialization &&
3384 PointOfInstantiation.isValid() &&
3385 MSInfo->getPointOfInstantiation().isInvalid())
3386 MSInfo->setPointOfInstantiation(PointOfInstantiation);
3387 } else
David Blaikie83d382b2011-09-23 05:06:16 +00003388 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003389}
3390
3391SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00003392 if (FunctionTemplateSpecializationInfo *FTSInfo
3393 = TemplateOrSpecialization.dyn_cast<
3394 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003395 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00003396 else if (MemberSpecializationInfo *MSInfo
3397 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003398 return MSInfo->getPointOfInstantiation();
3399
3400 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00003401}
3402
Douglas Gregor6411b922009-09-11 20:15:17 +00003403bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00003404 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00003405 return true;
3406
3407 // If this function was instantiated from a member function of a
3408 // class template, check whether that member function was defined out-of-line.
3409 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
3410 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003411 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00003412 return Definition->isOutOfLine();
3413 }
3414
3415 // If this function was instantiated from a function template,
3416 // check whether that function template was defined out-of-line.
3417 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
3418 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003419 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00003420 return Definition->isOutOfLine();
3421 }
3422
3423 return false;
3424}
3425
Abramo Bagnaraea947882011-03-08 16:41:52 +00003426SourceRange FunctionDecl::getSourceRange() const {
3427 return SourceRange(getOuterLocStart(), EndRangeLoc);
3428}
3429
Anna Zaks28db7ce2012-01-18 02:45:01 +00003430unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00003431 IdentifierInfo *FnInfo = getIdentifier();
3432
3433 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00003434 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00003435
3436 // Builtin handling.
3437 switch (getBuiltinID()) {
3438 case Builtin::BI__builtin_memset:
3439 case Builtin::BI__builtin___memset_chk:
3440 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00003441 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00003442
3443 case Builtin::BI__builtin_memcpy:
3444 case Builtin::BI__builtin___memcpy_chk:
3445 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00003446 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003447
3448 case Builtin::BI__builtin_memmove:
3449 case Builtin::BI__builtin___memmove_chk:
3450 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00003451 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00003452
3453 case Builtin::BIstrlcpy:
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00003454 case Builtin::BI__builtin___strlcpy_chk:
Anna Zaks22122702012-01-17 00:37:07 +00003455 return Builtin::BIstrlcpy;
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00003456
Anna Zaks201d4892012-01-13 21:52:01 +00003457 case Builtin::BIstrlcat:
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00003458 case Builtin::BI__builtin___strlcat_chk:
Anna Zaks22122702012-01-17 00:37:07 +00003459 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00003460
3461 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00003462 case Builtin::BImemcmp:
3463 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003464
3465 case Builtin::BI__builtin_strncpy:
3466 case Builtin::BI__builtin___strncpy_chk:
3467 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00003468 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003469
3470 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00003471 case Builtin::BIstrncmp:
3472 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003473
3474 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00003475 case Builtin::BIstrncasecmp:
3476 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003477
3478 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00003479 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00003480 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00003481 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00003482
3483 case Builtin::BI__builtin_strndup:
3484 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00003485 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00003486
Anna Zaks314cd092012-02-01 19:08:57 +00003487 case Builtin::BI__builtin_strlen:
3488 case Builtin::BIstrlen:
3489 return Builtin::BIstrlen;
3490
Bruno Cardoso Lopes7ea9fd22016-08-10 18:34:47 +00003491 case Builtin::BI__builtin_bzero:
3492 case Builtin::BIbzero:
3493 return Builtin::BIbzero;
3494
Anna Zaks201d4892012-01-13 21:52:01 +00003495 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00003496 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00003497 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00003498 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00003499 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00003500 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003501 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00003502 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00003503 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00003504 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003505 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00003506 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003507 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00003508 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003509 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00003510 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003511 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00003512 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00003513 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00003514 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00003515 else if (FnInfo->isStr("strlen"))
3516 return Builtin::BIstrlen;
Bruno Cardoso Lopes7ea9fd22016-08-10 18:34:47 +00003517 else if (FnInfo->isStr("bzero"))
3518 return Builtin::BIbzero;
Anna Zaks201d4892012-01-13 21:52:01 +00003519 }
3520 break;
3521 }
Anna Zaks22122702012-01-17 00:37:07 +00003522 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00003523}
3524
Chris Lattner59a25942008-03-31 00:36:02 +00003525//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003526// FieldDecl Implementation
3527//===----------------------------------------------------------------------===//
3528
Jay Foad39c79802011-01-12 09:06:06 +00003529FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003530 SourceLocation StartLoc, SourceLocation IdLoc,
3531 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00003532 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00003533 InClassInitStyle InitStyle) {
Richard Smithf7981722013-11-22 09:01:48 +00003534 return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
3535 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00003536}
3537
Douglas Gregor72172e92012-01-05 21:55:30 +00003538FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00003539 return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(),
3540 SourceLocation(), nullptr, QualType(), nullptr,
3541 nullptr, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00003542}
3543
Sebastian Redl833ef452010-01-26 22:01:41 +00003544bool FieldDecl::isAnonymousStructOrUnion() const {
3545 if (!isImplicit() || getDeclName())
3546 return false;
3547
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003548 if (const auto *Record = getType()->getAs<RecordType>())
Sebastian Redl833ef452010-01-26 22:01:41 +00003549 return Record->getDecl()->isAnonymousStructOrUnion();
3550
3551 return false;
3552}
3553
Richard Smithcaf33902011-10-10 18:28:20 +00003554unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
3555 assert(isBitField() && "not a bitfield");
Richard Smith6b8e3c02017-08-28 00:28:14 +00003556 return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
Richard Smithcaf33902011-10-10 18:28:20 +00003557}
3558
John McCall4e819612011-01-20 07:57:12 +00003559unsigned FieldDecl::getFieldIndex() const {
Richard Smith0b87e072013-10-07 08:02:11 +00003560 const FieldDecl *Canonical = getCanonicalDecl();
3561 if (Canonical != this)
3562 return Canonical->getFieldIndex();
3563
John McCall4e819612011-01-20 07:57:12 +00003564 if (CachedFieldIndex) return CachedFieldIndex - 1;
3565
Richard Smithd62306a2011-11-10 06:34:14 +00003566 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00003567 const RecordDecl *RD = getParent();
Richard Smithd62306a2011-11-10 06:34:14 +00003568
Hans Wennborga302cd92014-08-21 16:06:57 +00003569 for (auto *Field : RD->fields()) {
3570 Field->getCanonicalDecl()->CachedFieldIndex = Index + 1;
3571 ++Index;
3572 }
John McCall4e819612011-01-20 07:57:12 +00003573
Richard Smithd62306a2011-11-10 06:34:14 +00003574 assert(CachedFieldIndex && "failed to find field in parent");
3575 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00003576}
3577
Abramo Bagnara20c9e242011-03-08 11:07:11 +00003578SourceRange FieldDecl::getSourceRange() const {
Richard Smith6b8e3c02017-08-28 00:28:14 +00003579 const Expr *FinalExpr = getInClassInitializer();
3580 if (!FinalExpr)
3581 FinalExpr = getBitWidth();
3582 if (FinalExpr)
3583 return SourceRange(getInnerLocStart(), FinalExpr->getLocEnd());
3584 return DeclaratorDecl::getSourceRange();
Alexey Bataev39c81e22014-08-28 04:28:19 +00003585}
3586
3587void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) {
David Blaikie11ab0782014-10-31 17:18:09 +00003588 assert((getParent()->isLambda() || getParent()->isCapturedRecord()) &&
Alexey Bataev330de032014-10-29 12:21:55 +00003589 "capturing type in non-lambda or captured record.");
Richard Smith6b8e3c02017-08-28 00:28:14 +00003590 assert(InitStorage.getInt() == ISK_NoInit &&
John McCallc90c1492014-10-10 18:44:34 +00003591 InitStorage.getPointer() == nullptr &&
Alexey Bataev39c81e22014-08-28 04:28:19 +00003592 "bit width, initializer or captured type already set");
John McCallc90c1492014-10-10 18:44:34 +00003593 InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType),
3594 ISK_CapturedVLAType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00003595}
3596
Sebastian Redl833ef452010-01-26 22:01:41 +00003597//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003598// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00003599//===----------------------------------------------------------------------===//
3600
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00003601SourceLocation TagDecl::getOuterLocStart() const {
3602 return getTemplateOrInnerLocStart(this);
3603}
3604
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003605SourceRange TagDecl::getSourceRange() const {
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00003606 SourceLocation RBraceLoc = BraceRange.getEnd();
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003607 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00003608 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003609}
3610
Rafael Espindola8db352d2013-10-17 15:37:26 +00003611TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); }
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00003612
Rafael Espindolabf5c33b2013-03-12 21:06:00 +00003613void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
David Majnemer00350522015-08-31 18:48:39 +00003614 TypedefNameDeclOrQualifier = TDD;
Reid Klecknercae82a22014-04-23 22:03:04 +00003615 if (const Type *T = getTypeForDecl()) {
3616 (void)T;
Richard Smith5b21db82014-04-23 18:20:42 +00003617 assert(T->isLinkageValid());
Reid Klecknercae82a22014-04-23 22:03:04 +00003618 }
Rafael Espindola0e0d0092013-03-14 03:07:35 +00003619 assert(isLinkageValid());
Douglas Gregora72a4e32010-05-19 18:39:18 +00003620}
3621
Douglas Gregordee1be82009-01-17 00:42:38 +00003622void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00003623 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00003624
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003625 if (auto *D = dyn_cast<CXXRecordDecl>(this)) {
Richard Smith053f6c62014-05-16 23:01:30 +00003626 struct CXXRecordDecl::DefinitionData *Data =
John McCall67da35c2010-02-04 22:26:26 +00003627 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
Aaron Ballman86c93902014-03-06 23:45:36 +00003628 for (auto I : redecls())
Richard Smith64c06302014-05-22 23:19:02 +00003629 cast<CXXRecordDecl>(I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00003630 }
Douglas Gregordee1be82009-01-17 00:42:38 +00003631}
3632
3633void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00003634 assert((!isa<CXXRecordDecl>(this) ||
3635 cast<CXXRecordDecl>(this)->hasDefinition()) &&
3636 "definition completed but not started");
3637
John McCallf937c022011-10-07 06:10:15 +00003638 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00003639 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003640
3641 if (ASTMutationListener *L = getASTMutationListener())
3642 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00003643}
3644
John McCallf937c022011-10-07 06:10:15 +00003645TagDecl *TagDecl::getDefinition() const {
3646 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00003647 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003648
3649 // If it's possible for us to have an out-of-date definition, check now.
3650 if (MayHaveOutOfDateDef) {
3651 if (IdentifierInfo *II = getIdentifier()) {
3652 if (II->isOutOfDate()) {
3653 updateOutOfDate(*II);
3654 }
3655 }
3656 }
3657
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003658 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this))
Andrew Trickba266ee2010-10-19 21:54:32 +00003659 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003660
Aaron Ballman86c93902014-03-06 23:45:36 +00003661 for (auto R : redecls())
John McCallf937c022011-10-07 06:10:15 +00003662 if (R->isCompleteDefinition())
Aaron Ballman86c93902014-03-06 23:45:36 +00003663 return R;
Mike Stump11289f42009-09-09 15:08:12 +00003664
Craig Topper36250ad2014-05-12 05:36:57 +00003665 return nullptr;
Ted Kremenek21475702008-09-05 17:16:31 +00003666}
3667
Douglas Gregor14454802011-02-25 02:25:35 +00003668void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
3669 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00003670 // Make sure the extended qualifier info is allocated.
3671 if (!hasExtInfo())
David Majnemer00350522015-08-31 18:48:39 +00003672 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00003673 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00003674 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00003675 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00003676 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00003677 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00003678 if (getExtInfo()->NumTemplParamLists == 0) {
3679 getASTContext().Deallocate(getExtInfo());
David Majnemer00350522015-08-31 18:48:39 +00003680 TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003681 }
3682 else
3683 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00003684 }
3685 }
3686}
3687
Benjamin Kramer9cc210652015-08-05 09:40:49 +00003688void TagDecl::setTemplateParameterListsInfo(
3689 ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
3690 assert(!TPLists.empty());
Abramo Bagnara60804e12011-03-18 15:16:37 +00003691 // Make sure the extended decl info is allocated.
3692 if (!hasExtInfo())
3693 // Allocate external info struct.
David Majnemer00350522015-08-31 18:48:39 +00003694 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003695 // Set the template parameter lists info.
Benjamin Kramer9cc210652015-08-05 09:40:49 +00003696 getExtInfo()->setTemplateParameterListsInfo(Context, TPLists);
Abramo Bagnara60804e12011-03-18 15:16:37 +00003697}
3698
Ted Kremenek21475702008-09-05 17:16:31 +00003699//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003700// EnumDecl Implementation
3701//===----------------------------------------------------------------------===//
3702
David Blaikie68e081d2011-12-20 02:48:34 +00003703void EnumDecl::anchor() { }
3704
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003705EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3706 SourceLocation StartLoc, SourceLocation IdLoc,
3707 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003708 EnumDecl *PrevDecl, bool IsScoped,
3709 bool IsScopedUsingClassTag, bool IsFixed) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003710 auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl,
3711 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003712 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00003713 C.getTypeDeclType(Enum, PrevDecl);
3714 return Enum;
3715}
3716
Douglas Gregor72172e92012-01-05 21:55:30 +00003717EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00003718 EnumDecl *Enum =
3719 new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(),
3720 nullptr, nullptr, false, false, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003721 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3722 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003723}
3724
Alp Tokerb9fa5122014-01-06 11:31:18 +00003725SourceRange EnumDecl::getIntegerTypeRange() const {
3726 if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo())
3727 return TI->getTypeLoc().getSourceRange();
3728 return SourceRange();
3729}
3730
Douglas Gregord5058122010-02-11 01:19:42 +00003731void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00003732 QualType NewPromotionType,
3733 unsigned NumPositiveBits,
3734 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00003735 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00003736 if (!IntegerType)
3737 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00003738 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00003739 setNumPositiveBits(NumPositiveBits);
3740 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00003741 TagDecl::completeDefinition();
3742}
3743
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003744bool EnumDecl::isClosed() const {
3745 if (const auto *A = getAttr<EnumExtensibilityAttr>())
3746 return A->getExtensibility() == EnumExtensibilityAttr::Closed;
3747 return true;
3748}
3749
3750bool EnumDecl::isClosedFlag() const {
3751 return isClosed() && hasAttr<FlagEnumAttr>();
3752}
3753
3754bool EnumDecl::isClosedNonFlag() const {
3755 return isClosed() && !hasAttr<FlagEnumAttr>();
3756}
3757
Richard Smith7d137e32012-03-23 03:33:32 +00003758TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3759 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3760 return MSI->getTemplateSpecializationKind();
3761
3762 return TSK_Undeclared;
3763}
3764
3765void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3766 SourceLocation PointOfInstantiation) {
3767 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3768 assert(MSI && "Not an instantiated member enumeration?");
3769 MSI->setTemplateSpecializationKind(TSK);
3770 if (TSK != TSK_ExplicitSpecialization &&
3771 PointOfInstantiation.isValid() &&
3772 MSI->getPointOfInstantiation().isInvalid())
3773 MSI->setPointOfInstantiation(PointOfInstantiation);
3774}
3775
Richard Smith6739a102016-05-05 00:56:12 +00003776EnumDecl *EnumDecl::getTemplateInstantiationPattern() const {
3777 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
3778 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
3779 EnumDecl *ED = getInstantiatedFromMemberEnum();
3780 while (auto *NewED = ED->getInstantiatedFromMemberEnum())
3781 ED = NewED;
Richard Smith2195ec92017-04-21 01:15:13 +00003782 return getDefinitionOrSelf(ED);
Richard Smith6739a102016-05-05 00:56:12 +00003783 }
3784 }
3785
3786 assert(!isTemplateInstantiation(getTemplateSpecializationKind()) &&
3787 "couldn't find pattern for enum instantiation");
3788 return nullptr;
3789}
3790
Richard Smith4b38ded2012-03-14 23:13:10 +00003791EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3792 if (SpecializationInfo)
3793 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3794
Craig Topper36250ad2014-05-12 05:36:57 +00003795 return nullptr;
Richard Smith4b38ded2012-03-14 23:13:10 +00003796}
3797
3798void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3799 TemplateSpecializationKind TSK) {
3800 assert(!SpecializationInfo && "Member enum is already a specialization");
3801 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3802}
3803
Sebastian Redl833ef452010-01-26 22:01:41 +00003804//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00003805// RecordDecl Implementation
3806//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00003807
Richard Smith053f6c62014-05-16 23:01:30 +00003808RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C,
3809 DeclContext *DC, SourceLocation StartLoc,
3810 SourceLocation IdLoc, IdentifierInfo *Id,
3811 RecordDecl *PrevDecl)
3812 : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00003813 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003814 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003815 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003816 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003817 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00003818 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00003819}
3820
Jay Foad39c79802011-01-12 09:06:06 +00003821RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003822 SourceLocation StartLoc, SourceLocation IdLoc,
3823 IdentifierInfo *Id, RecordDecl* PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00003824 RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC,
3825 StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003826 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3827
Ted Kremenek21475702008-09-05 17:16:31 +00003828 C.getTypeDeclType(R, PrevDecl);
3829 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00003830}
3831
Douglas Gregor72172e92012-01-05 21:55:30 +00003832RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00003833 RecordDecl *R =
3834 new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(),
3835 SourceLocation(), nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003836 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3837 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003838}
3839
Douglas Gregordfcad112009-03-25 15:59:44 +00003840bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00003841 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00003842 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3843}
3844
Alexey Bataev39c81e22014-08-28 04:28:19 +00003845bool RecordDecl::isLambda() const {
3846 if (auto RD = dyn_cast<CXXRecordDecl>(this))
3847 return RD->isLambda();
3848 return false;
3849}
3850
Alexey Bataev330de032014-10-29 12:21:55 +00003851bool RecordDecl::isCapturedRecord() const {
3852 return hasAttr<CapturedRecordAttr>();
3853}
3854
3855void RecordDecl::setCapturedRecord() {
3856 addAttr(CapturedRecordAttr::CreateImplicit(getASTContext()));
3857}
3858
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003859RecordDecl::field_iterator RecordDecl::field_begin() const {
3860 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3861 LoadFieldsFromExternalStorage();
3862
3863 return field_iterator(decl_iterator(FirstDecl));
3864}
3865
Douglas Gregorb11aad82011-02-19 18:51:44 +00003866/// completeDefinition - Notes that the definition of this type is now
3867/// complete.
3868void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00003869 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00003870 TagDecl::completeDefinition();
3871}
3872
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003873/// isMsStruct - Get whether or not this record uses ms_struct layout.
3874/// This which can be turned on with an attribute, pragma, or the
3875/// -mms-bitfields command-line option.
3876bool RecordDecl::isMsStruct(const ASTContext &C) const {
David Majnemer8ab003a2015-02-02 19:30:52 +00003877 return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003878}
3879
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003880void RecordDecl::LoadFieldsFromExternalStorage() const {
3881 ExternalASTSource *Source = getASTContext().getExternalSource();
3882 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3883
3884 // Notify that we have a RecordDecl doing some initialization.
3885 ExternalASTSource::Deserializing TheFields(Source);
3886
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003887 SmallVector<Decl*, 64> Decls;
Richard Smith3cb15722015-08-05 22:41:45 +00003888 LoadedFieldsFromExternalStorage = true;
3889 Source->FindExternalLexicalDecls(this, [](Decl::Kind K) {
3890 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3891 }, Decls);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003892
3893#ifndef NDEBUG
3894 // Check that all decls we got were FieldDecls.
3895 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003896 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003897#endif
3898
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003899 if (Decls.empty())
3900 return;
3901
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00003902 std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00003903 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003904}
3905
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003906bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const {
3907 ASTContext &Context = getASTContext();
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +00003908 if (!Context.getLangOpts().Sanitize.hasOneOf(
3909 SanitizerKind::Address | SanitizerKind::KernelAddress) ||
Alexey Samsonova0416102014-11-11 01:26:14 +00003910 !Context.getLangOpts().SanitizeAddressFieldPadding)
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003911 return false;
Alexey Samsonov33e00e22014-10-16 23:50:26 +00003912 const auto &Blacklist = Context.getSanitizerBlacklist();
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003913 const auto *CXXRD = dyn_cast<CXXRecordDecl>(this);
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003914 // We may be able to relax some of these requirements.
3915 int ReasonToReject = -1;
3916 if (!CXXRD || CXXRD->isExternCContext())
3917 ReasonToReject = 0; // is not C++.
3918 else if (CXXRD->hasAttr<PackedAttr>())
3919 ReasonToReject = 1; // is packed.
3920 else if (CXXRD->isUnion())
3921 ReasonToReject = 2; // is a union.
3922 else if (CXXRD->isTriviallyCopyable())
3923 ReasonToReject = 3; // is trivially copyable.
3924 else if (CXXRD->hasTrivialDestructor())
3925 ReasonToReject = 4; // has trivial destructor.
3926 else if (CXXRD->isStandardLayout())
3927 ReasonToReject = 5; // is standard layout.
Alexey Samsonov33e00e22014-10-16 23:50:26 +00003928 else if (Blacklist.isBlacklistedLocation(getLocation(), "field-padding"))
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003929 ReasonToReject = 6; // is in a blacklisted file.
3930 else if (Blacklist.isBlacklistedType(getQualifiedNameAsString(),
3931 "field-padding"))
3932 ReasonToReject = 7; // is blacklisted.
3933
3934 if (EmitRemark) {
3935 if (ReasonToReject >= 0)
3936 Context.getDiagnostics().Report(
3937 getLocation(),
3938 diag::remark_sanitize_address_insert_extra_padding_rejected)
3939 << getQualifiedNameAsString() << ReasonToReject;
3940 else
3941 Context.getDiagnostics().Report(
3942 getLocation(),
3943 diag::remark_sanitize_address_insert_extra_padding_accepted)
3944 << getQualifiedNameAsString();
3945 }
3946 return ReasonToReject < 0;
3947}
3948
Evgeny Astigeevich665027d2014-12-12 16:17:46 +00003949const FieldDecl *RecordDecl::findFirstNamedDataMember() const {
3950 for (const auto *I : fields()) {
3951 if (I->getIdentifier())
3952 return I;
3953
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003954 if (const auto *RT = I->getType()->getAs<RecordType>())
Evgeny Astigeevich665027d2014-12-12 16:17:46 +00003955 if (const FieldDecl *NamedDataMember =
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003956 RT->getDecl()->findFirstNamedDataMember())
Evgeny Astigeevich665027d2014-12-12 16:17:46 +00003957 return NamedDataMember;
3958 }
3959
3960 // We didn't find a named data member.
3961 return nullptr;
3962}
3963
3964
Steve Naroff415d3d52008-10-08 17:01:13 +00003965//===----------------------------------------------------------------------===//
3966// BlockDecl Implementation
3967//===----------------------------------------------------------------------===//
3968
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003969void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Craig Topper36250ad2014-05-12 05:36:57 +00003970 assert(!ParamInfo && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00003971
Steve Naroffc4b30e52009-03-13 16:56:44 +00003972 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00003973 if (!NewParamInfo.empty()) {
3974 NumParams = NewParamInfo.size();
3975 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3976 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003977 }
3978}
3979
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00003980void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures,
3981 bool CapturesCXXThis) {
3982 this->CapturesCXXThis = CapturesCXXThis;
3983 this->NumCaptures = Captures.size();
John McCallc63de662011-02-02 13:00:07 +00003984
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00003985 if (Captures.empty()) {
3986 this->Captures = nullptr;
John McCallc63de662011-02-02 13:00:07 +00003987 return;
3988 }
3989
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00003990 this->Captures = Captures.copy(Context).data();
Steve Naroffc4b30e52009-03-13 16:56:44 +00003991}
Sebastian Redl833ef452010-01-26 22:01:41 +00003992
John McCallce45f882011-06-15 22:51:16 +00003993bool BlockDecl::capturesVariable(const VarDecl *variable) const {
Aaron Ballman9371dd22014-03-14 18:34:04 +00003994 for (const auto &I : captures())
John McCallce45f882011-06-15 22:51:16 +00003995 // Only auto vars can be captured, so no redeclaration worries.
Aaron Ballman9371dd22014-03-14 18:34:04 +00003996 if (I.getVariable() == variable)
John McCallce45f882011-06-15 22:51:16 +00003997 return true;
3998
3999 return false;
4000}
4001
Douglas Gregor70226da2010-12-21 16:27:07 +00004002SourceRange BlockDecl::getSourceRange() const {
4003 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
4004}
Sebastian Redl833ef452010-01-26 22:01:41 +00004005
4006//===----------------------------------------------------------------------===//
4007// Other Decl Allocation/Deallocation Method Implementations
4008//===----------------------------------------------------------------------===//
4009
David Blaikie68e081d2011-12-20 02:48:34 +00004010void TranslationUnitDecl::anchor() { }
4011
Sebastian Redl833ef452010-01-26 22:01:41 +00004012TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Craig Topper36250ad2014-05-12 05:36:57 +00004013 return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00004014}
4015
Nico Weber66220292016-03-02 17:28:48 +00004016void PragmaCommentDecl::anchor() { }
4017
4018PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C,
4019 TranslationUnitDecl *DC,
4020 SourceLocation CommentLoc,
4021 PragmaMSCommentKind CommentKind,
4022 StringRef Arg) {
4023 PragmaCommentDecl *PCD =
4024 new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1))
4025 PragmaCommentDecl(DC, CommentLoc, CommentKind);
4026 memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size());
4027 PCD->getTrailingObjects<char>()[Arg.size()] = '\0';
4028 return PCD;
4029}
4030
4031PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C,
4032 unsigned ID,
4033 unsigned ArgSize) {
4034 return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1))
4035 PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown);
4036}
4037
Nico Webercbbaeb12016-03-02 19:28:54 +00004038void PragmaDetectMismatchDecl::anchor() { }
4039
4040PragmaDetectMismatchDecl *
4041PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC,
4042 SourceLocation Loc, StringRef Name,
4043 StringRef Value) {
4044 size_t ValueStart = Name.size() + 1;
4045 PragmaDetectMismatchDecl *PDMD =
4046 new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1))
4047 PragmaDetectMismatchDecl(DC, Loc, ValueStart);
4048 memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size());
4049 PDMD->getTrailingObjects<char>()[Name.size()] = '\0';
4050 memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(),
4051 Value.size());
4052 PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0';
4053 return PDMD;
4054}
4055
4056PragmaDetectMismatchDecl *
4057PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID,
4058 unsigned NameValueSize) {
4059 return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1))
4060 PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0);
4061}
Nico Weber66220292016-03-02 17:28:48 +00004062
Richard Smithf19e1272015-03-07 00:04:49 +00004063void ExternCContextDecl::anchor() { }
4064
4065ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C,
4066 TranslationUnitDecl *DC) {
4067 return new (C, DC) ExternCContextDecl(DC);
4068}
4069
David Blaikie68e081d2011-12-20 02:48:34 +00004070void LabelDecl::anchor() { }
4071
Chris Lattnerc8e630e2011-02-17 07:39:24 +00004072LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00004073 SourceLocation IdentL, IdentifierInfo *II) {
Craig Topper36250ad2014-05-12 05:36:57 +00004074 return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL);
Abramo Bagnara1c3af962011-03-05 18:21:20 +00004075}
4076
4077LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
4078 SourceLocation IdentL, IdentifierInfo *II,
4079 SourceLocation GnuLabelL) {
4080 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
Craig Topper36250ad2014-05-12 05:36:57 +00004081 return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00004082}
4083
Douglas Gregor72172e92012-01-05 21:55:30 +00004084LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004085 return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr,
4086 SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00004087}
4088
Ehsan Akhgari31097582014-09-22 02:21:54 +00004089void LabelDecl::setMSAsmLabel(StringRef Name) {
4090 char *Buffer = new (getASTContext(), 1) char[Name.size() + 1];
4091 memcpy(Buffer, Name.data(), Name.size());
4092 Buffer[Name.size()] = '\0';
4093 MSAsmName = Buffer;
4094}
4095
David Blaikie68e081d2011-12-20 02:48:34 +00004096void ValueDecl::anchor() { }
4097
Benjamin Kramerea70eb32012-12-01 15:09:41 +00004098bool ValueDecl::isWeak() const {
Aaron Ballmanb97112e2014-03-08 22:19:01 +00004099 for (const auto *I : attrs())
4100 if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I))
Benjamin Kramerea70eb32012-12-01 15:09:41 +00004101 return true;
4102
4103 return isWeakImported();
4104}
4105
David Blaikie68e081d2011-12-20 02:48:34 +00004106void ImplicitParamDecl::anchor() { }
4107
Sebastian Redl833ef452010-01-26 22:01:41 +00004108ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00004109 SourceLocation IdLoc,
Alexey Bataev56223232017-06-09 13:40:18 +00004110 IdentifierInfo *Id, QualType Type,
4111 ImplicitParamKind ParamKind) {
4112 return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind);
4113}
4114
4115ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type,
4116 ImplicitParamKind ParamKind) {
4117 return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind);
Sebastian Redl833ef452010-01-26 22:01:41 +00004118}
4119
Richard Smithf7981722013-11-22 09:01:48 +00004120ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00004121 unsigned ID) {
Alexey Bataev56223232017-06-09 13:40:18 +00004122 return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other);
Douglas Gregor72172e92012-01-05 21:55:30 +00004123}
4124
Sebastian Redl833ef452010-01-26 22:01:41 +00004125FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00004126 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00004127 const DeclarationNameInfo &NameInfo,
4128 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004129 StorageClass SC,
Richard Smithf7981722013-11-22 09:01:48 +00004130 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00004131 bool hasWrittenPrototype,
4132 bool isConstexprSpecified) {
Richard Smithf7981722013-11-22 09:01:48 +00004133 FunctionDecl *New =
Richard Smith053f6c62014-05-16 23:01:30 +00004134 new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo,
4135 SC, isInlineSpecified, isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00004136 New->HasWrittenPrototype = hasWrittenPrototype;
4137 return New;
4138}
4139
Douglas Gregor72172e92012-01-05 21:55:30 +00004140FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00004141 return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00004142 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00004143 SC_None, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00004144}
4145
Sebastian Redl833ef452010-01-26 22:01:41 +00004146BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Richard Smithf7981722013-11-22 09:01:48 +00004147 return new (C, DC) BlockDecl(DC, L);
Sebastian Redl833ef452010-01-26 22:01:41 +00004148}
4149
Douglas Gregor72172e92012-01-05 21:55:30 +00004150BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004151 return new (C, ID) BlockDecl(nullptr, SourceLocation());
John McCall5e77d762013-04-16 07:28:30 +00004152}
4153
Chandler Carruth21c90602015-12-30 03:24:14 +00004154CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams)
4155 : Decl(Captured, DC, SourceLocation()), DeclContext(Captured),
4156 NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {}
4157
Ben Langmuir37943a72013-05-03 19:00:33 +00004158CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
4159 unsigned NumParams) {
James Y Knight7a22b242015-08-06 20:26:32 +00004160 return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
Richard Smithf7981722013-11-22 09:01:48 +00004161 CapturedDecl(DC, NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00004162}
4163
Ben Langmuirce914fc2013-05-03 19:20:19 +00004164CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
Richard Smithf7981722013-11-22 09:01:48 +00004165 unsigned NumParams) {
James Y Knight7a22b242015-08-06 20:26:32 +00004166 return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
Craig Topper36250ad2014-05-12 05:36:57 +00004167 CapturedDecl(nullptr, NumParams);
Ben Langmuirce914fc2013-05-03 19:20:19 +00004168}
4169
Chandler Carruth21c90602015-12-30 03:24:14 +00004170Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); }
4171void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); }
4172
4173bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); }
4174void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); }
4175
Sebastian Redl833ef452010-01-26 22:01:41 +00004176EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
4177 SourceLocation L,
4178 IdentifierInfo *Id, QualType T,
4179 Expr *E, const llvm::APSInt &V) {
Richard Smithf7981722013-11-22 09:01:48 +00004180 return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V);
Sebastian Redl833ef452010-01-26 22:01:41 +00004181}
4182
Douglas Gregor72172e92012-01-05 21:55:30 +00004183EnumConstantDecl *
4184EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004185 return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr,
4186 QualType(), nullptr, llvm::APSInt());
Douglas Gregor72172e92012-01-05 21:55:30 +00004187}
4188
David Blaikie68e081d2011-12-20 02:48:34 +00004189void IndirectFieldDecl::anchor() { }
4190
Richard Smith9f951822016-01-06 22:49:11 +00004191IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC,
4192 SourceLocation L, DeclarationName N,
David Majnemer59f77922016-06-24 04:05:48 +00004193 QualType T,
4194 MutableArrayRef<NamedDecl *> CH)
4195 : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()),
4196 ChainingSize(CH.size()) {
Richard Smith9f951822016-01-06 22:49:11 +00004197 // In C++, indirect field declarations conflict with tag declarations in the
4198 // same scope, so add them to IDNS_Tag so that tag redeclaration finds them.
4199 if (C.getLangOpts().CPlusPlus)
4200 IdentifierNamespace |= IDNS_Tag;
4201}
4202
Benjamin Kramer39593702010-11-21 14:11:41 +00004203IndirectFieldDecl *
4204IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
David Majnemer59f77922016-06-24 04:05:48 +00004205 IdentifierInfo *Id, QualType T,
4206 llvm::MutableArrayRef<NamedDecl *> CH) {
4207 return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH);
Francois Pichet783dd6e2010-11-21 06:08:52 +00004208}
4209
Douglas Gregor72172e92012-01-05 21:55:30 +00004210IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
4211 unsigned ID) {
Richard Smith9f951822016-01-06 22:49:11 +00004212 return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(),
David Majnemer59f77922016-06-24 04:05:48 +00004213 DeclarationName(), QualType(), None);
Douglas Gregor72172e92012-01-05 21:55:30 +00004214}
4215
Douglas Gregorbe996932010-09-01 20:41:53 +00004216SourceRange EnumConstantDecl::getSourceRange() const {
4217 SourceLocation End = getLocation();
4218 if (Init)
4219 End = Init->getLocEnd();
4220 return SourceRange(getLocation(), End);
4221}
4222
David Blaikie68e081d2011-12-20 02:48:34 +00004223void TypeDecl::anchor() { }
4224
Sebastian Redl833ef452010-01-26 22:01:41 +00004225TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004226 SourceLocation StartLoc, SourceLocation IdLoc,
4227 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
Richard Smith053f6c62014-05-16 23:01:30 +00004228 return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00004229}
4230
David Blaikie68e081d2011-12-20 02:48:34 +00004231void TypedefNameDecl::anchor() { }
4232
Richard Smith42413142015-05-15 20:05:43 +00004233TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const {
4234 if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) {
4235 auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl();
4236 auto *ThisTypedef = this;
4237 if (AnyRedecl && OwningTypedef) {
4238 OwningTypedef = OwningTypedef->getCanonicalDecl();
4239 ThisTypedef = ThisTypedef->getCanonicalDecl();
4240 }
4241 if (OwningTypedef == ThisTypedef)
Richard Smitha5230222015-03-27 01:37:43 +00004242 return TT->getDecl();
Richard Smith42413142015-05-15 20:05:43 +00004243 }
Richard Smitha5230222015-03-27 01:37:43 +00004244
4245 return nullptr;
4246}
4247
Argyrios Kyrtzidis3b25c912017-03-21 16:56:02 +00004248bool TypedefNameDecl::isTransparentTagSlow() const {
4249 auto determineIsTransparent = [&]() {
4250 if (auto *TT = getUnderlyingType()->getAs<TagType>()) {
4251 if (auto *TD = TT->getDecl()) {
4252 if (TD->getName() != getName())
4253 return false;
4254 SourceLocation TTLoc = getLocation();
4255 SourceLocation TDLoc = TD->getLocation();
4256 if (!TTLoc.isMacroID() || !TDLoc.isMacroID())
4257 return false;
4258 SourceManager &SM = getASTContext().getSourceManager();
4259 return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc);
4260 }
4261 }
4262 return false;
4263 };
4264
4265 bool isTransparent = determineIsTransparent();
4266 CacheIsTransparentTag = 1;
4267 if (isTransparent)
4268 CacheIsTransparentTag |= 0x2;
4269 return isTransparent;
4270}
4271
Douglas Gregor72172e92012-01-05 21:55:30 +00004272TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00004273 return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00004274 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00004275}
4276
Richard Smithdda56e42011-04-15 14:24:37 +00004277TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
4278 SourceLocation StartLoc,
4279 SourceLocation IdLoc, IdentifierInfo *Id,
4280 TypeSourceInfo *TInfo) {
Richard Smith053f6c62014-05-16 23:01:30 +00004281 return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo);
Richard Smithdda56e42011-04-15 14:24:37 +00004282}
4283
Douglas Gregor72172e92012-01-05 21:55:30 +00004284TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00004285 return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(),
4286 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00004287}
4288
Abramo Bagnaraea947882011-03-08 16:41:52 +00004289SourceRange TypedefDecl::getSourceRange() const {
4290 SourceLocation RangeEnd = getLocation();
4291 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
4292 if (typeIsPostfix(TInfo->getType()))
4293 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
4294 }
4295 return SourceRange(getLocStart(), RangeEnd);
4296}
4297
Richard Smithdda56e42011-04-15 14:24:37 +00004298SourceRange TypeAliasDecl::getSourceRange() const {
4299 SourceLocation RangeEnd = getLocStart();
4300 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
4301 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
4302 return SourceRange(getLocStart(), RangeEnd);
4303}
4304
David Blaikie68e081d2011-12-20 02:48:34 +00004305void FileScopeAsmDecl::anchor() { }
4306
Sebastian Redl833ef452010-01-26 22:01:41 +00004307FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00004308 StringLiteral *Str,
4309 SourceLocation AsmLoc,
4310 SourceLocation RParenLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00004311 return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00004312}
Douglas Gregorba345522011-12-02 23:23:56 +00004313
Richard Smithf7981722013-11-22 09:01:48 +00004314FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00004315 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004316 return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(),
4317 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00004318}
4319
Michael Han84324352013-02-22 17:15:32 +00004320void EmptyDecl::anchor() {}
4321
4322EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Richard Smithf7981722013-11-22 09:01:48 +00004323 return new (C, DC) EmptyDecl(DC, L);
Michael Han84324352013-02-22 17:15:32 +00004324}
4325
4326EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004327 return new (C, ID) EmptyDecl(nullptr, SourceLocation());
Michael Han84324352013-02-22 17:15:32 +00004328}
4329
Douglas Gregorba345522011-12-02 23:23:56 +00004330//===----------------------------------------------------------------------===//
4331// ImportDecl Implementation
4332//===----------------------------------------------------------------------===//
4333
4334/// \brief Retrieve the number of module identifiers needed to name the given
4335/// module.
4336static unsigned getNumModuleIdentifiers(Module *Mod) {
4337 unsigned Result = 1;
4338 while (Mod->Parent) {
4339 Mod = Mod->Parent;
4340 ++Result;
4341 }
4342 return Result;
4343}
4344
Douglas Gregor22d09742012-01-03 18:04:46 +00004345ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00004346 Module *Imported,
4347 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00004348 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00004349 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00004350{
4351 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00004352 auto *StoredLocs = getTrailingObjects<SourceLocation>();
James Y Knight7a22b242015-08-06 20:26:32 +00004353 std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(),
4354 StoredLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00004355}
4356
Douglas Gregor22d09742012-01-03 18:04:46 +00004357ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00004358 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00004359 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00004360 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00004361{
James Y Knight7a22b242015-08-06 20:26:32 +00004362 *getTrailingObjects<SourceLocation>() = EndLoc;
Douglas Gregorba345522011-12-02 23:23:56 +00004363}
4364
Richard Smithf7981722013-11-22 09:01:48 +00004365ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00004366 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004367 ArrayRef<SourceLocation> IdentifierLocs) {
James Y Knight7a22b242015-08-06 20:26:32 +00004368 return new (C, DC,
4369 additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size()))
Richard Smithf7981722013-11-22 09:01:48 +00004370 ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00004371}
4372
Richard Smithf7981722013-11-22 09:01:48 +00004373ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00004374 SourceLocation StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +00004375 Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004376 SourceLocation EndLoc) {
James Y Knight7a22b242015-08-06 20:26:32 +00004377 ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1))
4378 ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00004379 Import->setImplicit();
4380 return Import;
4381}
4382
Douglas Gregor72172e92012-01-05 21:55:30 +00004383ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
4384 unsigned NumLocations) {
James Y Knight7a22b242015-08-06 20:26:32 +00004385 return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations))
Richard Smithf7981722013-11-22 09:01:48 +00004386 ImportDecl(EmptyShell());
Douglas Gregorba345522011-12-02 23:23:56 +00004387}
4388
4389ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
4390 if (!ImportedAndComplete.getInt())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00004391 return None;
Douglas Gregorba345522011-12-02 23:23:56 +00004392
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00004393 const auto *StoredLocs = getTrailingObjects<SourceLocation>();
Craig Topper5fc8fc22014-08-27 06:28:36 +00004394 return llvm::makeArrayRef(StoredLocs,
4395 getNumModuleIdentifiers(getImportedModule()));
Douglas Gregorba345522011-12-02 23:23:56 +00004396}
4397
4398SourceRange ImportDecl::getSourceRange() const {
4399 if (!ImportedAndComplete.getInt())
James Y Knight7a22b242015-08-06 20:26:32 +00004400 return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>());
4401
Douglas Gregorba345522011-12-02 23:23:56 +00004402 return SourceRange(getLocation(), getIdentifierLocs().back());
4403}
Richard Smith8df390f2016-09-08 23:14:54 +00004404
4405//===----------------------------------------------------------------------===//
4406// ExportDecl Implementation
4407//===----------------------------------------------------------------------===//
4408
4409void ExportDecl::anchor() {}
4410
4411ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC,
4412 SourceLocation ExportLoc) {
4413 return new (C, DC) ExportDecl(DC, ExportLoc);
4414}
4415
4416ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4417 return new (C, ID) ExportDecl(nullptr, SourceLocation());
4418}