blob: 0a4c4a4a7762b59444cd528fc364c9eb9050ccbf [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.
Richard Smithdd8b5332017-09-04 05:37:53 +0000655 LinkageInfo LV = getExternalLinkageFor(D);
John McCallc273f242010-10-30 11:50:40 +0000656
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 Smithdd8b5332017-09-04 05:37:53 +00001402Module *NamedDecl::getOwningModuleForLinkage() const {
1403 Module *M = getOwningModule();
1404 if (!M)
1405 return nullptr;
1406
1407 switch (M->Kind) {
1408 case Module::ModuleMapModule:
1409 // Module map modules have no special linkage semantics.
1410 return nullptr;
1411
1412 case Module::ModuleInterfaceUnit:
1413 return M;
1414
1415 case Module::GlobalModuleFragment:
1416 // External linkage declarations in the global module have no owning module
1417 // for linkage purposes. But internal linkage declarations in the global
1418 // module fragment of a particular module are owned by that module for
1419 // linkage purposes.
1420 return hasExternalFormalLinkage() ? nullptr : M->Parent;
1421 }
1422
1423 llvm_unreachable("unknown module kind");
1424}
1425
Richard Smith7873de02016-08-11 22:25:46 +00001426void NamedDecl::printName(raw_ostream &os) const {
1427 os << Name;
1428}
1429
Douglas Gregor2ada0482009-02-04 17:27:36 +00001430std::string NamedDecl::getQualifiedNameAsString() const {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001431 std::string QualName;
1432 llvm::raw_string_ostream OS(QualName);
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001433 printQualifiedName(OS, getASTContext().getPrintingPolicy());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001434 return OS.str();
1435}
1436
1437void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1438 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1439}
1440
1441void NamedDecl::printQualifiedName(raw_ostream &OS,
1442 const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +00001443 const DeclContext *Ctx = getDeclContext();
1444
Argyrios Kyrtzidisa166a2b2017-03-07 09:26:07 +00001445 // For ObjC methods, look through categories and use the interface as context.
1446 if (auto *MD = dyn_cast<ObjCMethodDecl>(this))
1447 if (auto *ID = MD->getClassInterface())
1448 Ctx = ID;
1449
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001450 if (Ctx->isFunctionOrMethod()) {
1451 printName(OS);
1452 return;
1453 }
Douglas Gregor2ada0482009-02-04 17:27:36 +00001454
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001455 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001456 ContextsTy Contexts;
1457
1458 // Collect contexts.
1459 while (Ctx && isa<NamedDecl>(Ctx)) {
1460 Contexts.push_back(Ctx);
1461 Ctx = Ctx->getParent();
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001462 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001463
David Majnemerf7e36092016-06-23 00:15:04 +00001464 for (const DeclContext *DC : reverse(Contexts)) {
1465 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Benjamin Kramer9170e912013-02-22 15:46:01 +00001466 OS << Spec->getName();
Douglas Gregor85673582009-05-18 17:01:57 +00001467 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
David Majnemer6fbeee32016-07-07 04:43:07 +00001468 TemplateSpecializationType::PrintTemplateArgumentList(
1469 OS, TemplateArgs.asArray(), P);
David Majnemerf7e36092016-06-23 00:15:04 +00001470 } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
Richard Smith46dd5bb2014-05-30 22:16:51 +00001471 if (P.SuppressUnwrittenScope &&
1472 (ND->isAnonymousNamespace() || ND->isInline()))
1473 continue;
Reid Kleckner60103382015-12-16 02:04:40 +00001474 if (ND->isAnonymousNamespace()) {
1475 OS << (P.MSVCFormatting ? "`anonymous namespace\'"
1476 : "(anonymous namespace)");
1477 }
Sam Weinig07d211e2009-12-24 23:15:03 +00001478 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001479 OS << *ND;
David Majnemerf7e36092016-06-23 00:15:04 +00001480 } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) {
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001481 if (!RD->getIdentifier())
David Blaikieabe1a392014-04-02 05:58:29 +00001482 OS << "(anonymous " << RD->getKindName() << ')';
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001483 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001484 OS << *RD;
David Majnemerf7e36092016-06-23 00:15:04 +00001485 } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
Craig Topper36250ad2014-05-12 05:36:57 +00001486 const FunctionProtoType *FT = nullptr;
Sam Weinigb999f682009-12-28 03:19:38 +00001487 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001488 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001489
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001490 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001491 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001492 unsigned NumParams = FD->getNumParams();
1493 for (unsigned i = 0; i < NumParams; ++i) {
1494 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001495 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001496 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001497 }
1498
1499 if (FT->isVariadic()) {
1500 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001501 OS << ", ";
1502 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001503 }
1504 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001505 OS << ')';
David Majnemerf7e36092016-06-23 00:15:04 +00001506 } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) {
Alexander Kornienko4f355322015-11-09 16:45:17 +00001507 // C++ [dcl.enum]p10: Each enum-name and each unscoped
1508 // enumerator is declared in the scope that immediately contains
1509 // the enum-specifier. Each scoped enumerator is declared in the
1510 // scope of the enumeration.
1511 if (ED->isScoped() || ED->getIdentifier())
1512 OS << *ED;
1513 else
1514 continue;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001515 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001516 OS << *cast<NamedDecl>(DC);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001517 }
1518 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001519 }
1520
Richard Smith7873de02016-08-11 22:25:46 +00001521 if (getDeclName() || isa<DecompositionDecl>(this))
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001522 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001523 else
David Blaikieabe1a392014-04-02 05:58:29 +00001524 OS << "(anonymous)";
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001525}
Douglas Gregor2ada0482009-02-04 17:27:36 +00001526
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001527void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1528 const PrintingPolicy &Policy,
1529 bool Qualified) const {
1530 if (Qualified)
1531 printQualifiedName(OS, Policy);
1532 else
1533 printName(OS);
Douglas Gregor2ada0482009-02-04 17:27:36 +00001534}
1535
Richard Smithe8292b12015-02-10 03:28:10 +00001536template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1537 return true;
1538}
1539static bool isRedeclarableImpl(...) { return false; }
1540static bool isRedeclarable(Decl::Kind K) {
1541 switch (K) {
1542#define DECL(Type, Base) \
1543 case Decl::Type: \
1544 return isRedeclarableImpl((Type##Decl *)nullptr);
1545#define ABSTRACT_DECL(DECL)
1546#include "clang/AST/DeclNodes.inc"
1547 }
1548 llvm_unreachable("unknown decl kind");
1549}
1550
1551bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001552 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1553
Richard Smithfe620d22015-03-05 23:24:12 +00001554 // Never replace one imported declaration with another; we need both results
1555 // when re-exporting.
1556 if (OldD->isFromASTFile() && isFromASTFile())
1557 return false;
1558
Richard Smith5cd86f82015-11-03 03:13:11 +00001559 // A kind mismatch implies that the declaration is not replaced.
1560 if (OldD->getKind() != getKind())
Richard Smithe8292b12015-02-10 03:28:10 +00001561 return false;
1562
Richard Smith5cd86f82015-11-03 03:13:11 +00001563 // For method declarations, we never replace. (Why?)
1564 if (isa<ObjCMethodDecl>(this))
1565 return false;
1566
1567 // For parameters, pick the newer one. This is either an error or (in
1568 // Objective-C) permitted as an extension.
1569 if (isa<ParmVarDecl>(this))
1570 return true;
1571
Richard Smithe8292b12015-02-10 03:28:10 +00001572 // Inline namespaces can give us two declarations with the same
1573 // name and kind in the same scope but different contexts; we should
1574 // keep both declarations in this case.
1575 if (!this->getDeclContext()->getRedeclContext()->Equals(
1576 OldD->getDeclContext()->getRedeclContext()))
1577 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001578
Richard Smith5cd86f82015-11-03 03:13:11 +00001579 // Using declarations can be replaced if they import the same name from the
1580 // same context.
Richard Smithe8292b12015-02-10 03:28:10 +00001581 if (auto *UD = dyn_cast<UsingDecl>(this)) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001582 ASTContext &Context = getASTContext();
Richard Smithe8292b12015-02-10 03:28:10 +00001583 return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) ==
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001584 Context.getCanonicalNestedNameSpecifier(
Richard Smithe8292b12015-02-10 03:28:10 +00001585 cast<UsingDecl>(OldD)->getQualifier());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001586 }
Richard Smithe8292b12015-02-10 03:28:10 +00001587 if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) {
Eli Friedman0eaf10b2013-08-20 00:39:40 +00001588 ASTContext &Context = getASTContext();
Richard Smithe8292b12015-02-10 03:28:10 +00001589 return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) ==
Eli Friedman0eaf10b2013-08-20 00:39:40 +00001590 Context.getCanonicalNestedNameSpecifier(
1591 cast<UnresolvedUsingValueDecl>(OldD)->getQualifier());
1592 }
1593
Richard Smithe8292b12015-02-10 03:28:10 +00001594 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
Richard Smith5cd86f82015-11-03 03:13:11 +00001595 // They can be replaced if they nominate the same namespace.
1596 // FIXME: Is this true even if they have different module visibility?
Richard Smithe8292b12015-02-10 03:28:10 +00001597 if (auto *UD = dyn_cast<UsingDirectiveDecl>(this))
1598 return UD->getNominatedNamespace()->getOriginalNamespace() ==
1599 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1600 ->getOriginalNamespace();
Richard Smithbaf3ca52014-03-17 21:46:03 +00001601
Richard Smith5cd86f82015-11-03 03:13:11 +00001602 if (isRedeclarable(getKind())) {
1603 if (getCanonicalDecl() != OldD->getCanonicalDecl())
1604 return false;
1605
1606 if (IsKnownNewer)
1607 return true;
1608
Richard Smithe8292b12015-02-10 03:28:10 +00001609 // Check whether this is actually newer than OldD. We want to keep the
1610 // newer declaration. This loop will usually only iterate once, because
1611 // OldD is usually the previous declaration.
1612 for (auto D : redecls()) {
1613 if (D == OldD)
1614 break;
1615
1616 // If we reach the canonical declaration, then OldD is not actually older
1617 // than this one.
1618 //
1619 // FIXME: In this case, we should not add this decl to the lookup table.
1620 if (D->isCanonicalDecl())
1621 return false;
1622 }
Richard Smith5cd86f82015-11-03 03:13:11 +00001623
1624 // It's a newer declaration of the same kind of declaration in the same
1625 // scope: we want this decl instead of the existing one.
1626 return true;
Richard Smithe8292b12015-02-10 03:28:10 +00001627 }
1628
Richard Smith5cd86f82015-11-03 03:13:11 +00001629 // In all other cases, we need to keep both declarations in case they have
1630 // different visibility. Any attempt to use the name will result in an
1631 // ambiguity if more than one is visible.
1632 return false;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001633}
1634
Douglas Gregoreddf4332009-02-24 20:03:32 +00001635bool NamedDecl::hasLinkage() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +00001636 return getFormalLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001637}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001638
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001639NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001640 NamedDecl *ND = this;
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001641 while (auto *UD = dyn_cast<UsingShadowDecl>(ND))
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001642 ND = UD->getTargetDecl();
1643
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001644 if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001645 return AD->getClassInterface();
1646
Richard Smithf2005d32015-12-29 23:34:32 +00001647 if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND))
1648 return AD->getNamespace();
1649
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001650 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001651}
1652
John McCalla8ae2222010-04-06 21:38:20 +00001653bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001654 if (!isCXXClassMember())
1655 return false;
1656
John McCalla8ae2222010-04-06 21:38:20 +00001657 const NamedDecl *D = this;
1658 if (isa<UsingShadowDecl>(D))
1659 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1660
John McCall5e77d762013-04-16 07:28:30 +00001661 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001662 return true;
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001663 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()))
Alp Tokera2794f92014-01-22 07:29:52 +00001664 return MD->isInstance();
John McCalla8ae2222010-04-06 21:38:20 +00001665 return false;
1666}
1667
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001668//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001669// DeclaratorDecl Implementation
1670//===----------------------------------------------------------------------===//
1671
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001672template <typename DeclT>
1673static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1674 if (decl->getNumTemplateParameterLists() > 0)
1675 return decl->getTemplateParameterList(0)->getTemplateLoc();
1676 else
1677 return decl->getInnerLocStart();
1678}
1679
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001680SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001681 TypeSourceInfo *TSI = getTypeSourceInfo();
1682 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001683 return SourceLocation();
1684}
1685
Douglas Gregor14454802011-02-25 02:25:35 +00001686void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1687 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001688 // Make sure the extended decl info is allocated.
1689 if (!hasExtInfo()) {
1690 // Save (non-extended) type source info pointer.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001691 auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
John McCall3e11ebe2010-03-15 10:12:16 +00001692 // Allocate external info struct.
1693 DeclInfo = new (getASTContext()) ExtInfo;
1694 // Restore savedTInfo into (extended) decl info.
1695 getExtInfo()->TInfo = savedTInfo;
1696 }
1697 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001698 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001699 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001700 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001701 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001702 if (getExtInfo()->NumTemplParamLists == 0) {
1703 // Save type source info pointer.
1704 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1705 // Deallocate the extended decl info.
1706 getASTContext().Deallocate(getExtInfo());
1707 // Restore savedTInfo into (non-extended) decl info.
1708 DeclInfo = savedTInfo;
1709 }
1710 else
1711 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001712 }
1713 }
1714}
1715
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001716void DeclaratorDecl::setTemplateParameterListsInfo(
1717 ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
1718 assert(!TPLists.empty());
Abramo Bagnara60804e12011-03-18 15:16:37 +00001719 // Make sure the extended decl info is allocated.
1720 if (!hasExtInfo()) {
1721 // Save (non-extended) type source info pointer.
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00001722 auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
Abramo Bagnara60804e12011-03-18 15:16:37 +00001723 // Allocate external info struct.
1724 DeclInfo = new (getASTContext()) ExtInfo;
1725 // Restore savedTInfo into (extended) decl info.
1726 getExtInfo()->TInfo = savedTInfo;
1727 }
1728 // Set the template parameter lists info.
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001729 getExtInfo()->setTemplateParameterListsInfo(Context, TPLists);
Abramo Bagnara60804e12011-03-18 15:16:37 +00001730}
1731
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001732SourceLocation DeclaratorDecl::getOuterLocStart() const {
1733 return getTemplateOrInnerLocStart(this);
1734}
1735
Abramo Bagnaraea947882011-03-08 16:41:52 +00001736namespace {
1737
1738// Helper function: returns true if QT is or contains a type
1739// having a postfix component.
1740bool typeIsPostfix(clang::QualType QT) {
1741 while (true) {
1742 const Type* T = QT.getTypePtr();
1743 switch (T->getTypeClass()) {
1744 default:
1745 return false;
1746 case Type::Pointer:
1747 QT = cast<PointerType>(T)->getPointeeType();
1748 break;
1749 case Type::BlockPointer:
1750 QT = cast<BlockPointerType>(T)->getPointeeType();
1751 break;
1752 case Type::MemberPointer:
1753 QT = cast<MemberPointerType>(T)->getPointeeType();
1754 break;
1755 case Type::LValueReference:
1756 case Type::RValueReference:
1757 QT = cast<ReferenceType>(T)->getPointeeType();
1758 break;
1759 case Type::PackExpansion:
1760 QT = cast<PackExpansionType>(T)->getPattern();
1761 break;
1762 case Type::Paren:
1763 case Type::ConstantArray:
1764 case Type::DependentSizedArray:
1765 case Type::IncompleteArray:
1766 case Type::VariableArray:
1767 case Type::FunctionProto:
1768 case Type::FunctionNoProto:
1769 return true;
1770 }
1771 }
1772}
1773
1774} // namespace
1775
1776SourceRange DeclaratorDecl::getSourceRange() const {
1777 SourceLocation RangeEnd = getLocation();
1778 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
Benjamin Kramer3a7cc812014-02-02 15:28:46 +00001779 // If the declaration has no name or the type extends past the name take the
1780 // end location of the type.
1781 if (!getDeclName() || typeIsPostfix(TInfo->getType()))
Abramo Bagnaraea947882011-03-08 16:41:52 +00001782 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1783 }
1784 return SourceRange(getOuterLocStart(), RangeEnd);
1785}
1786
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001787void QualifierInfo::setTemplateParameterListsInfo(
1788 ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001789 // Free previous template parameters (if any).
1790 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001791 Context.Deallocate(TemplParamLists);
Craig Topper36250ad2014-05-12 05:36:57 +00001792 TemplParamLists = nullptr;
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001793 NumTemplParamLists = 0;
1794 }
1795 // Set info on matched template parameter lists (if any).
Benjamin Kramer9cc210652015-08-05 09:40:49 +00001796 if (!TPLists.empty()) {
1797 TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()];
1798 NumTemplParamLists = TPLists.size();
1799 std::copy(TPLists.begin(), TPLists.end(), TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001800 }
1801}
1802
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001803//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001804// VarDecl Implementation
1805//===----------------------------------------------------------------------===//
1806
Sebastian Redl833ef452010-01-26 22:01:41 +00001807const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1808 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001809 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001810 case SC_Auto: return "auto";
1811 case SC_Extern: return "extern";
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001812 case SC_PrivateExtern: return "__private_extern__";
1813 case SC_Register: return "register";
1814 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001815 }
1816
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001817 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001818}
1819
Richard Smith053f6c62014-05-16 23:01:30 +00001820VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC,
1821 SourceLocation StartLoc, SourceLocation IdLoc,
1822 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
1823 StorageClass SC)
1824 : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc),
1825 redeclarable_base(C), Init() {
Benjamin Kramera939c232014-03-15 18:54:13 +00001826 static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned),
1827 "VarDeclBitfields too large!");
1828 static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned),
1829 "ParmVarDeclBitfields too large!");
David Majnemerfa7bc782015-05-19 00:57:16 +00001830 static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned),
1831 "NonParmVarDeclBitfields too large!");
Larisse Voufo39a1e502013-08-06 01:03:05 +00001832 AllBits = 0;
1833 VarDeclBits.SClass = SC;
1834 // Everything else is implicitly initialized to false.
1835}
1836
Abramo Bagnaradff19302011-03-08 08:55:46 +00001837VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1838 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001839 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001840 StorageClass S) {
Richard Smith053f6c62014-05-16 23:01:30 +00001841 return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001842}
1843
Douglas Gregor72172e92012-01-05 21:55:30 +00001844VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001845 return new (C, ID)
1846 VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
1847 QualType(), nullptr, SC_None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001848}
1849
Douglas Gregorbf62d642010-12-06 18:36:25 +00001850void VarDecl::setStorageClass(StorageClass SC) {
1851 assert(isLegalForVariable(SC));
John McCallbeaa11c2011-05-01 02:13:58 +00001852 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001853}
1854
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001855VarDecl::TLSKind VarDecl::getTLSKind() const {
1856 switch (VarDeclBits.TSCSpec) {
1857 case TSCS_unspecified:
Samuel Antaof8b50122015-07-13 22:54:53 +00001858 if (!hasAttr<ThreadAttr>() &&
1859 !(getASTContext().getLangOpts().OpenMPUseTLS &&
1860 getASTContext().getTargetInfo().isTLSSupported() &&
1861 hasAttr<OMPThreadPrivateDeclAttr>()))
David Majnemer1b5baff2015-05-14 05:19:23 +00001862 return TLS_None;
Samuel Antaof8b50122015-07-13 22:54:53 +00001863 return ((getASTContext().getLangOpts().isCompatibleWithMSVC(
1864 LangOptions::MSVC2015)) ||
1865 hasAttr<OMPThreadPrivateDeclAttr>())
David Majnemer1b5baff2015-05-14 05:19:23 +00001866 ? TLS_Dynamic
1867 : TLS_Static;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001868 case TSCS___thread: // Fall through.
1869 case TSCS__Thread_local:
Samuel Antaof8b50122015-07-13 22:54:53 +00001870 return TLS_Static;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001871 case TSCS_thread_local:
1872 return TLS_Dynamic;
1873 }
1874 llvm_unreachable("Unknown thread storage class specifier!");
1875}
1876
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001877SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001878 if (const Expr *Init = getInit()) {
1879 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001880 // If Init is implicit, ignore its source range and fallback on
1881 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1882 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001883 return SourceRange(getOuterLocStart(), InitEnd);
1884 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001885 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001886}
1887
Rafael Espindola88510672013-01-04 21:18:45 +00001888template<typename T>
Alp Toker84212df2014-05-31 06:11:02 +00001889static LanguageLinkage getDeclLanguageLinkage(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001890 // C++ [dcl.link]p1: All function types, function names with external linkage,
1891 // and variable names with external linkage have a language linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +00001892 if (!D.hasExternalFormalLinkage())
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001893 return NoLanguageLinkage;
1894
1895 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001896 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001897 ASTContext &Context = D.getASTContext();
1898 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001899 return CLanguageLinkage;
1900
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001901 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1902 // language linkage of the names of class members and the function type of
1903 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001904 const DeclContext *DC = D.getDeclContext();
1905 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001906 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001907
1908 // If the first decl is in an extern "C" context, any other redeclaration
1909 // will have C language linkage. If the first one is not in an extern "C"
1910 // context, we would have reported an error for any other decl being in one.
Rafael Espindola593537a2013-05-05 20:15:21 +00001911 if (isFirstInExternCContext(&D))
Rafael Espindolaf4187652013-02-14 01:18:37 +00001912 return CLanguageLinkage;
1913 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001914}
1915
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001916template<typename T>
Alp Toker84212df2014-05-31 06:11:02 +00001917static bool isDeclExternC(const T &D) {
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001918 // Since the context is ignored for class members, they can only have C++
1919 // language linkage or no language linkage.
1920 const DeclContext *DC = D.getDeclContext();
1921 if (DC->isRecord()) {
1922 assert(D.getASTContext().getLangOpts().CPlusPlus);
1923 return false;
1924 }
1925
1926 return D.getLanguageLinkage() == CLanguageLinkage;
1927}
1928
Rafael Espindolaf4187652013-02-14 01:18:37 +00001929LanguageLinkage VarDecl::getLanguageLinkage() const {
Alp Toker84212df2014-05-31 06:11:02 +00001930 return getDeclLanguageLinkage(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001931}
1932
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001933bool VarDecl::isExternC() const {
Alp Toker84212df2014-05-31 06:11:02 +00001934 return isDeclExternC(*this);
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001935}
1936
Rafael Espindola593537a2013-05-05 20:15:21 +00001937bool VarDecl::isInExternCContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00001938 return getLexicalDeclContext()->isExternCContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00001939}
1940
1941bool VarDecl::isInExternCXXContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00001942 return getLexicalDeclContext()->isExternCXXContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00001943}
1944
Rafael Espindola8db352d2013-10-17 15:37:26 +00001945VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); }
Sebastian Redl833ef452010-01-26 22:01:41 +00001946
Nico Weber7f5015a2015-04-15 21:53:00 +00001947VarDecl::DefinitionKind
1948VarDecl::isThisDeclarationADefinition(ASTContext &C) const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001949 // C++ [basic.def]p2:
1950 // A declaration is a definition unless [...] it contains the 'extern'
1951 // specifier or a linkage-specification and neither an initializer [...],
Richard Smith62f19e72016-06-25 00:15:56 +00001952 // it declares a non-inline static data member in a class declaration [...],
1953 // it declares a static data member outside a class definition and the variable
1954 // was defined within the class with the constexpr specifier [...],
Richard Smith8809a0c2013-09-27 20:14:12 +00001955 // C++1y [temp.expl.spec]p15:
1956 // An explicit specialization of a static data member or an explicit
1957 // specialization of a static data member template is a definition if the
1958 // declaration includes an initializer; otherwise, it is a declaration.
1959 //
1960 // FIXME: How do you declare (but not define) a partial specialization of
1961 // a static data member template outside the containing class?
Richard Smithedbc6e92016-10-14 21:41:24 +00001962 if (isThisDeclarationADemotedDefinition())
1963 return DeclarationOnly;
1964
Sebastian Redl35351a92010-01-31 22:27:38 +00001965 if (isStaticDataMember()) {
Richard Smith8809a0c2013-09-27 20:14:12 +00001966 if (isOutOfLine() &&
Richard Smith62f19e72016-06-25 00:15:56 +00001967 !(getCanonicalDecl()->isInline() &&
1968 getCanonicalDecl()->isConstexpr()) &&
Richard Smith8809a0c2013-09-27 20:14:12 +00001969 (hasInit() ||
1970 // If the first declaration is out-of-line, this may be an
1971 // instantiation of an out-of-line partial specialization of a variable
1972 // template for which we have not yet instantiated the initializer.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001973 (getFirstDecl()->isOutOfLine()
Richard Smith8809a0c2013-09-27 20:14:12 +00001974 ? getTemplateSpecializationKind() == TSK_Undeclared
1975 : getTemplateSpecializationKind() !=
1976 TSK_ExplicitSpecialization) ||
1977 isa<VarTemplatePartialSpecializationDecl>(this)))
Sebastian Redl35351a92010-01-31 22:27:38 +00001978 return Definition;
Richard Smith62f19e72016-06-25 00:15:56 +00001979 else if (!isOutOfLine() && isInline())
1980 return Definition;
Sebastian Redl35351a92010-01-31 22:27:38 +00001981 else
1982 return DeclarationOnly;
1983 }
1984 // C99 6.7p5:
1985 // A definition of an identifier is a declaration for that identifier that
1986 // [...] causes storage to be reserved for that object.
1987 // Note: that applies for all non-file-scope objects.
1988 // C99 6.9.2p1:
1989 // If the declaration of an identifier for an object has file scope and an
1990 // initializer, the declaration is an external definition for the identifier
1991 if (hasInit())
1992 return Definition;
Rafael Espindolabff59562013-04-25 12:11:36 +00001993
Dmitry Polukhin85eda122016-04-11 07:48:59 +00001994 if (hasDefiningAttr())
Rafael Espindolad53ffa02013-10-22 21:39:03 +00001995 return Definition;
1996
David Majnemerdd0bed12015-05-14 05:19:20 +00001997 if (const auto *SAA = getAttr<SelectAnyAttr>())
1998 if (!SAA->isInherited())
1999 return Definition;
2000
Richard Smith8809a0c2013-09-27 20:14:12 +00002001 // A variable template specialization (other than a static data member
2002 // template or an explicit specialization) is a declaration until we
2003 // instantiate its initializer.
2004 if (isa<VarTemplateSpecializationDecl>(this) &&
2005 getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
2006 return DeclarationOnly;
2007
Nico Weber608e7682015-04-15 23:04:24 +00002008 if (hasExternalStorage())
Sebastian Redl35351a92010-01-31 22:27:38 +00002009 return DeclarationOnly;
Rafael Espindola8f326a52013-03-07 01:42:44 +00002010
Rafael Espindolabff59562013-04-25 12:11:36 +00002011 // [dcl.link] p7:
2012 // A declaration directly contained in a linkage-specification is treated
2013 // as if it contains the extern specifier for the purpose of determining
2014 // the linkage of the declared name and whether it is a definition.
Nico Weber608e7682015-04-15 23:04:24 +00002015 if (isSingleLineLanguageLinkage(*this))
Rafael Espindola327be3c2013-04-26 01:30:23 +00002016 return DeclarationOnly;
Rafael Espindolabff59562013-04-25 12:11:36 +00002017
Sebastian Redl35351a92010-01-31 22:27:38 +00002018 // C99 6.9.2p2:
2019 // A declaration of an object that has file scope without an initializer,
2020 // and without a storage class specifier or the scs 'static', constitutes
2021 // a tentative definition.
2022 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002023 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00002024 return TentativeDefinition;
2025
2026 // What's left is (in C, block-scope) declarations without initializers or
2027 // external storage. These are definitions.
2028 return Definition;
2029}
2030
Sebastian Redl35351a92010-01-31 22:27:38 +00002031VarDecl *VarDecl::getActingDefinition() {
2032 DefinitionKind Kind = isThisDeclarationADefinition();
2033 if (Kind != TentativeDefinition)
Craig Topper36250ad2014-05-12 05:36:57 +00002034 return nullptr;
Sebastian Redl35351a92010-01-31 22:27:38 +00002035
Craig Topper36250ad2014-05-12 05:36:57 +00002036 VarDecl *LastTentative = nullptr;
Rafael Espindola8db352d2013-10-17 15:37:26 +00002037 VarDecl *First = getFirstDecl();
Aaron Ballman86c93902014-03-06 23:45:36 +00002038 for (auto I : First->redecls()) {
2039 Kind = I->isThisDeclarationADefinition();
Sebastian Redl35351a92010-01-31 22:27:38 +00002040 if (Kind == Definition)
Craig Topper36250ad2014-05-12 05:36:57 +00002041 return nullptr;
Sebastian Redl35351a92010-01-31 22:27:38 +00002042 else if (Kind == TentativeDefinition)
Aaron Ballman86c93902014-03-06 23:45:36 +00002043 LastTentative = I;
Sebastian Redl35351a92010-01-31 22:27:38 +00002044 }
2045 return LastTentative;
2046}
2047
Daniel Dunbar9d355812012-03-09 01:51:51 +00002048VarDecl *VarDecl::getDefinition(ASTContext &C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002049 VarDecl *First = getFirstDecl();
Aaron Ballman86c93902014-03-06 23:45:36 +00002050 for (auto I : First->redecls()) {
2051 if (I->isThisDeclarationADefinition(C) == Definition)
2052 return I;
Sebastian Redl5ca79842010-02-01 20:16:42 +00002053 }
Craig Topper36250ad2014-05-12 05:36:57 +00002054 return nullptr;
Sebastian Redl5ca79842010-02-01 20:16:42 +00002055}
2056
Daniel Dunbar9d355812012-03-09 01:51:51 +00002057VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00002058 DefinitionKind Kind = DeclarationOnly;
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002059
Rafael Espindola8db352d2013-10-17 15:37:26 +00002060 const VarDecl *First = getFirstDecl();
Aaron Ballman86c93902014-03-06 23:45:36 +00002061 for (auto I : First->redecls()) {
2062 Kind = std::max(Kind, I->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00002063 if (Kind == Definition)
2064 break;
2065 }
John McCall37bb6c92010-10-29 22:22:43 +00002066
2067 return Kind;
2068}
2069
Sebastian Redl5ca79842010-02-01 20:16:42 +00002070const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Aaron Ballman86c93902014-03-06 23:45:36 +00002071 for (auto I : redecls()) {
2072 if (auto Expr = I->getInit()) {
2073 D = I;
2074 return Expr;
2075 }
Sebastian Redl833ef452010-01-26 22:01:41 +00002076 }
Craig Topper36250ad2014-05-12 05:36:57 +00002077 return nullptr;
Sebastian Redl833ef452010-01-26 22:01:41 +00002078}
2079
Chandler Carruth813faed2015-12-30 02:51:00 +00002080bool VarDecl::hasInit() const {
2081 if (auto *P = dyn_cast<ParmVarDecl>(this))
2082 if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg())
2083 return false;
2084
2085 return !Init.isNull();
2086}
2087
2088Expr *VarDecl::getInit() {
2089 if (!hasInit())
2090 return nullptr;
2091
2092 if (auto *S = Init.dyn_cast<Stmt *>())
2093 return cast<Expr>(S);
2094
2095 return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value);
2096}
2097
2098Stmt **VarDecl::getInitAddress() {
2099 if (auto *ES = Init.dyn_cast<EvaluatedStmt *>())
2100 return &ES->Value;
2101
2102 return Init.getAddrOfPtr1();
2103}
2104
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002105bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002106 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002107 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00002108
2109 if (!isStaticDataMember())
2110 return false;
2111
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002112 // If this static data member was instantiated from a static data member of
2113 // a class template, check whether that static data member was defined
2114 // out-of-line.
2115 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
2116 return VD->isOutOfLine();
2117
2118 return false;
2119}
2120
Douglas Gregord5058122010-02-11 01:19:42 +00002121void VarDecl::setInit(Expr *I) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002122 if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002123 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00002124 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00002125 }
2126
2127 Init = I;
2128}
2129
Daniel Dunbar9d355812012-03-09 01:51:51 +00002130bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002131 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00002132
Richard Smith35ecb362012-03-02 04:14:40 +00002133 if (!Lang.CPlusPlus)
2134 return false;
2135
2136 // In C++11, any variable of reference type can be used in a constant
2137 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002138 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00002139 return true;
2140
2141 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00002142 // not require the variable to be non-volatile, but we consider this to be a
2143 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00002144 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00002145 return false;
2146
2147 // In C++, const, non-volatile variables of integral or enumeration types
2148 // can be used in constant expressions.
2149 if (getType()->isIntegralOrEnumerationType())
2150 return true;
2151
Richard Smith35ecb362012-03-02 04:14:40 +00002152 // Additionally, in C++11, non-volatile constexpr variables can be used in
2153 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002154 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00002155}
2156
Richard Smithd0b4dd62011-12-19 06:19:21 +00002157/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
2158/// form, which contains extra information on the evaluated value of the
2159/// initializer.
2160EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002161 auto *Eval = Init.dyn_cast<EvaluatedStmt *>();
Richard Smithd0b4dd62011-12-19 06:19:21 +00002162 if (!Eval) {
Manuel Klimeka7328992013-06-03 13:51:33 +00002163 // Note: EvaluatedStmt contains an APValue, which usually holds
2164 // resources not allocated from the ASTContext. We need to do some
2165 // work to avoid leaking those, but we do so in VarDecl::evaluateValue
2166 // where we can detect whether there's anything to clean up or not.
Richard Smithd0b4dd62011-12-19 06:19:21 +00002167 Eval = new (getASTContext()) EvaluatedStmt;
Chandler Carruth813faed2015-12-30 02:51:00 +00002168 Eval->Value = Init.get<Stmt *>();
Richard Smithd0b4dd62011-12-19 06:19:21 +00002169 Init = Eval;
2170 }
2171 return Eval;
2172}
2173
Richard Smithdafff942012-01-14 04:30:29 +00002174APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002175 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00002176 return evaluateValue(Notes);
2177}
2178
2179APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002180 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00002181 EvaluatedStmt *Eval = ensureEvaluatedStmt();
2182
2183 // We only produce notes indicating why an initializer is non-constant the
2184 // first time it is evaluated. FIXME: The notes won't always be emitted the
2185 // first time we try evaluation, so might not be produced at all.
2186 if (Eval->WasEvaluated)
Craig Topper36250ad2014-05-12 05:36:57 +00002187 return Eval->Evaluated.isUninit() ? nullptr : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002188
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002189 const auto *Init = cast<Expr>(Eval->Value);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002190 assert(!Init->isValueDependent());
2191
2192 if (Eval->IsEvaluating) {
2193 // FIXME: Produce a diagnostic for self-initialization.
2194 Eval->CheckedICE = true;
2195 Eval->IsICE = false;
Craig Topper36250ad2014-05-12 05:36:57 +00002196 return nullptr;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002197 }
2198
2199 Eval->IsEvaluating = true;
2200
2201 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
2202 this, Notes);
2203
Manuel Klimeka7328992013-06-03 13:51:33 +00002204 // Ensure the computed APValue is cleaned up later if evaluation succeeded,
2205 // or that it's empty (so that there's nothing to clean up) if evaluation
2206 // failed.
Richard Smithd0b4dd62011-12-19 06:19:21 +00002207 if (!Result)
2208 Eval->Evaluated = APValue();
Manuel Klimeka7328992013-06-03 13:51:33 +00002209 else if (Eval->Evaluated.needsCleanup())
George Burgess IVb61bfbd2017-02-14 05:37:36 +00002210 getASTContext().addDestruction(&Eval->Evaluated);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002211
2212 Eval->IsEvaluating = false;
2213 Eval->WasEvaluated = true;
2214
2215 // In C++11, we have determined whether the initializer was a constant
2216 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002217 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00002218 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00002219 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00002220 }
2221
Craig Topper36250ad2014-05-12 05:36:57 +00002222 return Result ? &Eval->Evaluated : nullptr;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002223}
2224
Chandler Carruth813faed2015-12-30 02:51:00 +00002225APValue *VarDecl::getEvaluatedValue() const {
2226 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
2227 if (Eval->WasEvaluated)
2228 return &Eval->Evaluated;
2229
2230 return nullptr;
2231}
2232
2233bool VarDecl::isInitKnownICE() const {
2234 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
2235 return Eval->CheckedICE;
2236
2237 return false;
2238}
2239
2240bool VarDecl::isInitICE() const {
2241 assert(isInitKnownICE() &&
2242 "Check whether we already know that the initializer is an ICE");
2243 return Init.get<EvaluatedStmt *>()->IsICE;
2244}
2245
Richard Smithd0b4dd62011-12-19 06:19:21 +00002246bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00002247 // Initializers of weak variables are never ICEs.
2248 if (isWeak())
2249 return false;
2250
Richard Smithd0b4dd62011-12-19 06:19:21 +00002251 EvaluatedStmt *Eval = ensureEvaluatedStmt();
2252 if (Eval->CheckedICE)
2253 // We have already checked whether this subexpression is an
2254 // integral constant expression.
2255 return Eval->IsICE;
2256
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002257 const auto *Init = cast<Expr>(Eval->Value);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002258 assert(!Init->isValueDependent());
2259
2260 // In C++11, evaluate the initializer to check whether it's a constant
2261 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002262 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002263 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00002264 evaluateValue(Notes);
2265 return Eval->IsICE;
2266 }
2267
2268 // It's an ICE whether or not the definition we found is
2269 // out-of-line. See DR 721 and the discussion in Clang PR
2270 // 6206 for details.
2271
2272 if (Eval->CheckingICE)
2273 return false;
2274 Eval->CheckingICE = true;
2275
2276 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
2277 Eval->CheckingICE = false;
2278 Eval->CheckedICE = true;
2279 return Eval->IsICE;
2280}
2281
Richard Smith2195ec92017-04-21 01:15:13 +00002282template<typename DeclT>
2283static DeclT *getDefinitionOrSelf(DeclT *D) {
2284 assert(D);
2285 if (auto *Def = D->getDefinition())
2286 return Def;
2287 return D;
2288}
2289
Richard Smithedbc6e92016-10-14 21:41:24 +00002290VarDecl *VarDecl::getTemplateInstantiationPattern() const {
2291 // If it's a variable template specialization, find the template or partial
2292 // specialization from which it was instantiated.
2293 if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(this)) {
2294 auto From = VDTemplSpec->getInstantiatedFrom();
2295 if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) {
2296 while (auto *NewVTD = VTD->getInstantiatedFromMemberTemplate()) {
2297 if (NewVTD->isMemberSpecialization())
2298 break;
2299 VTD = NewVTD;
2300 }
Richard Smith2195ec92017-04-21 01:15:13 +00002301 return getDefinitionOrSelf(VTD->getTemplatedDecl());
Richard Smithedbc6e92016-10-14 21:41:24 +00002302 }
2303 if (auto *VTPSD =
2304 From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
2305 while (auto *NewVTPSD = VTPSD->getInstantiatedFromMember()) {
2306 if (NewVTPSD->isMemberSpecialization())
2307 break;
2308 VTPSD = NewVTPSD;
2309 }
Richard Smith2195ec92017-04-21 01:15:13 +00002310 return getDefinitionOrSelf<VarDecl>(VTPSD);
Richard Smithedbc6e92016-10-14 21:41:24 +00002311 }
2312 }
2313
2314 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
2315 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
2316 VarDecl *VD = getInstantiatedFromStaticDataMember();
2317 while (auto *NewVD = VD->getInstantiatedFromStaticDataMember())
2318 VD = NewVD;
Richard Smith2195ec92017-04-21 01:15:13 +00002319 return getDefinitionOrSelf(VD);
Richard Smithedbc6e92016-10-14 21:41:24 +00002320 }
2321 }
2322
2323 if (VarTemplateDecl *VarTemplate = getDescribedVarTemplate()) {
Richard Smithedbc6e92016-10-14 21:41:24 +00002324 while (VarTemplate->getInstantiatedFromMemberTemplate()) {
2325 if (VarTemplate->isMemberSpecialization())
2326 break;
2327 VarTemplate = VarTemplate->getInstantiatedFromMemberTemplate();
2328 }
2329
Richard Smith2195ec92017-04-21 01:15:13 +00002330 return getDefinitionOrSelf(VarTemplate->getTemplatedDecl());
Richard Smithedbc6e92016-10-14 21:41:24 +00002331 }
2332 return nullptr;
2333}
2334
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002335VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002336 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00002337 return cast<VarDecl>(MSI->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00002338
2339 return nullptr;
Douglas Gregor86d142a2009-10-08 07:24:58 +00002340}
2341
Douglas Gregor3c74d412009-10-14 20:14:33 +00002342TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002343 if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this))
Larisse Voufo39a1e502013-08-06 01:03:05 +00002344 return Spec->getSpecializationKind();
2345
Sebastian Redl35351a92010-01-31 22:27:38 +00002346 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00002347 return MSI->getTemplateSpecializationKind();
Richard Smith8809a0c2013-09-27 20:14:12 +00002348
Douglas Gregor86d142a2009-10-08 07:24:58 +00002349 return TSK_Undeclared;
2350}
2351
Richard Smith8809a0c2013-09-27 20:14:12 +00002352SourceLocation VarDecl::getPointOfInstantiation() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002353 if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this))
Richard Smith8809a0c2013-09-27 20:14:12 +00002354 return Spec->getPointOfInstantiation();
2355
2356 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2357 return MSI->getPointOfInstantiation();
2358
2359 return SourceLocation();
2360}
2361
Larisse Voufo39a1e502013-08-06 01:03:05 +00002362VarTemplateDecl *VarDecl::getDescribedVarTemplate() const {
2363 return getASTContext().getTemplateOrSpecializationInfo(this)
2364 .dyn_cast<VarTemplateDecl *>();
2365}
2366
2367void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) {
2368 getASTContext().setTemplateOrSpecializationInfo(this, Template);
2369}
2370
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00002371MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Richard Smithb71782b2013-08-01 04:12:04 +00002372 if (isStaticDataMember())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002373 // FIXME: Remove ?
2374 // return getASTContext().getInstantiatedFromStaticDataMember(this);
2375 return getASTContext().getTemplateOrSpecializationInfo(this)
2376 .dyn_cast<MemberSpecializationInfo *>();
Craig Topper36250ad2014-05-12 05:36:57 +00002377 return nullptr;
Douglas Gregor06db9f52009-10-12 20:18:28 +00002378}
2379
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002380void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2381 SourceLocation PointOfInstantiation) {
Richard Smith8809a0c2013-09-27 20:14:12 +00002382 assert((isa<VarTemplateSpecializationDecl>(this) ||
2383 getMemberSpecializationInfo()) &&
2384 "not a variable or static data member template specialization");
2385
Larisse Voufo39a1e502013-08-06 01:03:05 +00002386 if (VarTemplateSpecializationDecl *Spec =
2387 dyn_cast<VarTemplateSpecializationDecl>(this)) {
2388 Spec->setSpecializationKind(TSK);
2389 if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() &&
2390 Spec->getPointOfInstantiation().isInvalid())
2391 Spec->setPointOfInstantiation(PointOfInstantiation);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002392 }
2393
2394 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) {
2395 MSI->setTemplateSpecializationKind(TSK);
2396 if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() &&
2397 MSI->getPointOfInstantiation().isInvalid())
2398 MSI->setPointOfInstantiation(PointOfInstantiation);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002399 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00002400}
2401
2402void
2403VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD,
2404 TemplateSpecializationKind TSK) {
2405 assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() &&
2406 "Previous template or instantiation?");
2407 getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00002408}
2409
Sebastian Redl833ef452010-01-26 22:01:41 +00002410//===----------------------------------------------------------------------===//
2411// ParmVarDecl Implementation
2412//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00002413
Sebastian Redl833ef452010-01-26 22:01:41 +00002414ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002415 SourceLocation StartLoc,
2416 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00002417 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002418 StorageClass S, Expr *DefArg) {
Richard Smith053f6c62014-05-16 23:01:30 +00002419 return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002420 S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00002421}
2422
Reid Kleckner8a365022013-06-24 17:51:48 +00002423QualType ParmVarDecl::getOriginalType() const {
2424 TypeSourceInfo *TSI = getTypeSourceInfo();
2425 QualType T = TSI ? TSI->getType() : getType();
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002426 if (const auto *DT = dyn_cast<DecayedType>(T))
Reid Kleckner8a365022013-06-24 17:51:48 +00002427 return DT->getOriginalType();
2428 return T;
2429}
2430
Douglas Gregor72172e92012-01-05 21:55:30 +00002431ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002432 return new (C, ID)
2433 ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(),
2434 nullptr, QualType(), nullptr, SC_None, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002435}
2436
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00002437SourceRange ParmVarDecl::getSourceRange() const {
2438 if (!hasInheritedDefaultArg()) {
2439 SourceRange ArgRange = getDefaultArgRange();
2440 if (ArgRange.isValid())
2441 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
2442 }
2443
Argyrios Kyrtzidisa0772792013-04-17 01:56:48 +00002444 // DeclaratorDecl considers the range of postfix types as overlapping with the
2445 // declaration name, but this is not the case with parameters in ObjC methods.
2446 if (isa<ObjCMethodDecl>(getDeclContext()))
2447 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
2448
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00002449 return DeclaratorDecl::getSourceRange();
2450}
2451
Sebastian Redl833ef452010-01-26 22:01:41 +00002452Expr *ParmVarDecl::getDefaultArg() {
2453 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
2454 assert(!hasUninstantiatedDefaultArg() &&
2455 "Default argument is not yet instantiated!");
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002456
Sebastian Redl833ef452010-01-26 22:01:41 +00002457 Expr *Arg = getInit();
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002458 if (auto *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00002459 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00002460
Sebastian Redl833ef452010-01-26 22:01:41 +00002461 return Arg;
2462}
2463
Chandler Carruth813faed2015-12-30 02:51:00 +00002464void ParmVarDecl::setDefaultArg(Expr *defarg) {
2465 ParmVarDeclBits.DefaultArgKind = DAK_Normal;
2466 Init = defarg;
2467}
Sebastian Redl833ef452010-01-26 22:01:41 +00002468
Chandler Carruth813faed2015-12-30 02:51:00 +00002469SourceRange ParmVarDecl::getDefaultArgRange() const {
2470 switch (ParmVarDeclBits.DefaultArgKind) {
2471 case DAK_None:
2472 case DAK_Unparsed:
2473 // Nothing we can do here.
2474 return SourceRange();
2475
2476 case DAK_Uninstantiated:
Sebastian Redl833ef452010-01-26 22:01:41 +00002477 return getUninstantiatedDefaultArg()->getSourceRange();
2478
Chandler Carruth813faed2015-12-30 02:51:00 +00002479 case DAK_Normal:
2480 if (const Expr *E = getInit())
2481 return E->getSourceRange();
2482
2483 // Missing an actual expression, may be invalid.
2484 return SourceRange();
2485 }
2486 llvm_unreachable("Invalid default argument kind.");
2487}
2488
2489void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) {
2490 ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated;
2491 Init = arg;
2492}
2493
2494Expr *ParmVarDecl::getUninstantiatedDefaultArg() {
2495 assert(hasUninstantiatedDefaultArg() &&
2496 "Wrong kind of initialization expression!");
2497 return cast_or_null<Expr>(Init.get<Stmt *>());
2498}
2499
2500bool ParmVarDecl::hasDefaultArg() const {
2501 // FIXME: We should just return false for DAK_None here once callers are
2502 // prepared for the case that we encountered an invalid default argument and
2503 // were unable to even build an invalid expression.
2504 return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() ||
2505 !Init.isNull();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00002506}
2507
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00002508bool ParmVarDecl::isParameterPack() const {
2509 return isa<PackExpansionType>(getType());
2510}
2511
Ted Kremenek540017e2011-10-06 05:00:56 +00002512void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2513 getASTContext().setParameterIndex(this, parameterIndex);
2514 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2515}
2516
2517unsigned ParmVarDecl::getParameterIndexLarge() const {
2518 return getASTContext().getParameterIndex(this);
2519}
2520
Nuno Lopes394ec982008-12-17 23:39:55 +00002521//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002522// FunctionDecl Implementation
2523//===----------------------------------------------------------------------===//
2524
Benjamin Kramer9170e912013-02-22 15:46:01 +00002525void FunctionDecl::getNameForDiagnostic(
2526 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2527 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002528 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2529 if (TemplateArgs)
Benjamin Kramer9170e912013-02-22 15:46:01 +00002530 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00002531 OS, TemplateArgs->asArray(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002532}
2533
Ted Kremenek186a0742010-04-29 16:49:01 +00002534bool FunctionDecl::isVariadic() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002535 if (const auto *FT = getType()->getAs<FunctionProtoType>())
Ted Kremenek186a0742010-04-29 16:49:01 +00002536 return FT->isVariadic();
2537 return false;
2538}
2539
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002540bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
Aaron Ballman86c93902014-03-06 23:45:36 +00002541 for (auto I : redecls()) {
Serge Pavlov9a618e12017-02-15 12:30:35 +00002542 if (I->doesThisDeclarationHaveABody()) {
Aaron Ballman86c93902014-03-06 23:45:36 +00002543 Definition = I;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002544 return true;
2545 }
2546 }
2547
2548 return false;
2549}
2550
Anders Carlsson9bd7d162011-05-14 23:26:09 +00002551bool FunctionDecl::hasTrivialBody() const
2552{
2553 Stmt *S = getBody();
2554 if (!S) {
2555 // Since we don't have a body for this function, we don't know if it's
2556 // trivial or not.
2557 return false;
2558 }
2559
2560 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2561 return true;
2562 return false;
2563}
2564
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002565bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
Aaron Ballman86c93902014-03-06 23:45:36 +00002566 for (auto I : redecls()) {
Serge Pavlov10673c92017-06-04 12:53:12 +00002567 if (I->isThisDeclarationADefinition()) {
2568 Definition = I;
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002569 return true;
2570 }
2571 }
2572
2573 return false;
2574}
2575
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002576Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Rafael Espindola44503012013-10-19 01:37:17 +00002577 if (!hasBody(Definition))
Craig Topper36250ad2014-05-12 05:36:57 +00002578 return nullptr;
Rafael Espindola44503012013-10-19 01:37:17 +00002579
2580 if (Definition->Body)
2581 return Definition->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00002582
Craig Topper36250ad2014-05-12 05:36:57 +00002583 return nullptr;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002584}
2585
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002586void FunctionDecl::setBody(Stmt *B) {
2587 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00002588 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002589 EndRangeLoc = B->getLocEnd();
2590}
2591
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002592void FunctionDecl::setPure(bool P) {
2593 IsPure = P;
2594 if (P)
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002595 if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002596 Parent->markedVirtualFunctionPure();
2597}
2598
Richard Smith8d0dc312013-07-21 23:12:18 +00002599template<std::size_t Len>
2600static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) {
2601 IdentifierInfo *II = ND->getIdentifier();
2602 return II && II->isStr(Str);
2603}
2604
Douglas Gregor16618f22009-09-12 00:17:51 +00002605bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00002606 const TranslationUnitDecl *tunit =
2607 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2608 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002609 !tunit->getASTContext().getLangOpts().Freestanding &&
Richard Smith8d0dc312013-07-21 23:12:18 +00002610 isNamed(this, "main");
John McCall53ffd372011-05-15 17:49:20 +00002611}
2612
David Majnemerc729b0b2013-09-16 22:44:20 +00002613bool FunctionDecl::isMSVCRTEntryPoint() const {
2614 const TranslationUnitDecl *TUnit =
2615 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2616 if (!TUnit)
2617 return false;
2618
2619 // Even though we aren't really targeting MSVCRT if we are freestanding,
2620 // semantic analysis for these functions remains the same.
2621
2622 // MSVCRT entry points only exist on MSVCRT targets.
2623 if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT())
2624 return false;
2625
2626 // Nameless functions like constructors cannot be entry points.
2627 if (!getIdentifier())
2628 return false;
2629
2630 return llvm::StringSwitch<bool>(getName())
2631 .Cases("main", // an ANSI console app
2632 "wmain", // a Unicode console App
2633 "WinMain", // an ANSI GUI app
2634 "wWinMain", // a Unicode GUI app
2635 "DllMain", // a DLL
2636 true)
2637 .Default(false);
2638}
2639
John McCall53ffd372011-05-15 17:49:20 +00002640bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2641 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2642 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2643 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2644 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2645 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2646
Richard Smithf6004412014-01-19 23:25:37 +00002647 if (!getDeclContext()->getRedeclContext()->isTranslationUnit())
2648 return false;
John McCall53ffd372011-05-15 17:49:20 +00002649
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002650 const auto *proto = getType()->castAs<FunctionProtoType>();
Alp Toker9cacbab2014-01-20 20:26:09 +00002651 if (proto->getNumParams() != 2 || proto->isVariadic())
2652 return false;
John McCall53ffd372011-05-15 17:49:20 +00002653
2654 ASTContext &Context =
2655 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2656 ->getASTContext();
2657
2658 // The result type and first argument type are constant across all
2659 // these operators. The second argument must be exactly void*.
Alp Toker9cacbab2014-01-20 20:26:09 +00002660 return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00002661}
2662
Akira Hatanakacae83f72017-06-29 18:48:40 +00002663bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const {
Richard Smith8d0dc312013-07-21 23:12:18 +00002664 if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName)
2665 return false;
2666 if (getDeclName().getCXXOverloadedOperator() != OO_New &&
2667 getDeclName().getCXXOverloadedOperator() != OO_Delete &&
2668 getDeclName().getCXXOverloadedOperator() != OO_Array_New &&
2669 getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
2670 return false;
2671
2672 if (isa<CXXRecordDecl>(getDeclContext()))
2673 return false;
Richard Smithf6004412014-01-19 23:25:37 +00002674
2675 // This can only fail for an invalid 'operator new' declaration.
2676 if (!getDeclContext()->getRedeclContext()->isTranslationUnit())
2677 return false;
Richard Smith8d0dc312013-07-21 23:12:18 +00002678
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002679 const auto *FPT = getType()->castAs<FunctionProtoType>();
Richard Smithb2f0f052016-10-10 18:54:32 +00002680 if (FPT->getNumParams() == 0 || FPT->getNumParams() > 3 || FPT->isVariadic())
Richard Smith8d0dc312013-07-21 23:12:18 +00002681 return false;
2682
2683 // If this is a single-parameter function, it must be a replaceable global
2684 // allocation or deallocation function.
Alp Toker9cacbab2014-01-20 20:26:09 +00002685 if (FPT->getNumParams() == 1)
Richard Smith8d0dc312013-07-21 23:12:18 +00002686 return true;
2687
Richard Smithb2f0f052016-10-10 18:54:32 +00002688 unsigned Params = 1;
2689 QualType Ty = FPT->getParamType(Params);
Richard Smith1cdec012013-09-29 04:40:38 +00002690 ASTContext &Ctx = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002691
2692 auto Consume = [&] {
2693 ++Params;
2694 Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType();
2695 };
2696
2697 // In C++14, the next parameter can be a 'std::size_t' for sized delete.
2698 bool IsSizedDelete = false;
Richard Smithb47c36f2013-11-05 09:12:18 +00002699 if (Ctx.getLangOpts().SizedDeallocation &&
Richard Smithb2f0f052016-10-10 18:54:32 +00002700 (getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2701 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) &&
2702 Ctx.hasSameType(Ty, Ctx.getSizeType())) {
2703 IsSizedDelete = true;
2704 Consume();
2705 }
2706
2707 // In C++17, the next parameter can be a 'std::align_val_t' for aligned
2708 // new/delete.
Akira Hatanakacae83f72017-06-29 18:48:40 +00002709 if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) {
2710 if (IsAligned)
2711 *IsAligned = true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002712 Consume();
Akira Hatanakacae83f72017-06-29 18:48:40 +00002713 }
Richard Smithb2f0f052016-10-10 18:54:32 +00002714
2715 // Finally, if this is not a sized delete, the final parameter can
2716 // be a 'const std::nothrow_t&'.
2717 if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) {
2718 Ty = Ty->getPointeeType();
2719 if (Ty.getCVRQualifiers() != Qualifiers::Const)
2720 return false;
2721 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2722 if (RD && isNamed(RD, "nothrow_t") && RD->isInStdNamespace())
2723 Consume();
2724 }
2725
2726 return Params == FPT->getNumParams();
Richard Smith8d0dc312013-07-21 23:12:18 +00002727}
2728
Rafael Espindolaf4187652013-02-14 01:18:37 +00002729LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Alp Toker84212df2014-05-31 06:11:02 +00002730 return getDeclLanguageLinkage(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00002731}
2732
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002733bool FunctionDecl::isExternC() const {
Alp Toker84212df2014-05-31 06:11:02 +00002734 return isDeclExternC(*this);
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002735}
2736
Rafael Espindola593537a2013-05-05 20:15:21 +00002737bool FunctionDecl::isInExternCContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00002738 return getLexicalDeclContext()->isExternCContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00002739}
2740
2741bool FunctionDecl::isInExternCXXContext() const {
Serge Pavlov3cb80222013-11-14 02:13:03 +00002742 return getLexicalDeclContext()->isExternCXXContext();
Rafael Espindola593537a2013-05-05 20:15:21 +00002743}
2744
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002745bool FunctionDecl::isGlobal() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002746 if (const auto *Method = dyn_cast<CXXMethodDecl>(this))
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002747 return Method->isStatic();
2748
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002749 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002750 return false;
2751
Mike Stump11289f42009-09-09 15:08:12 +00002752 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002753 DC->isNamespace();
2754 DC = DC->getParent()) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002755 if (const auto *Namespace = cast<NamespaceDecl>(DC)) {
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002756 if (!Namespace->getDeclName())
2757 return false;
2758 break;
2759 }
2760 }
2761
2762 return true;
2763}
2764
Richard Smith10876ef2013-01-17 01:30:42 +00002765bool FunctionDecl::isNoReturn() const {
Adrian Prantl721c7cb2016-08-17 16:42:15 +00002766 if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
2767 hasAttr<C11NoReturnAttr>())
2768 return true;
2769
2770 if (auto *FnTy = getType()->getAs<FunctionType>())
2771 return FnTy->getNoReturnAttr();
2772
2773 return false;
Richard Smith10876ef2013-01-17 01:30:42 +00002774}
2775
Sebastian Redl833ef452010-01-26 22:01:41 +00002776void
2777FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002778 redeclarable_base::setPreviousDecl(PrevDecl);
Sebastian Redl833ef452010-01-26 22:01:41 +00002779
2780 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2781 FunctionTemplateDecl *PrevFunTmpl
Craig Topper36250ad2014-05-12 05:36:57 +00002782 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr;
Sebastian Redl833ef452010-01-26 22:01:41 +00002783 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
Rafael Espindola8db352d2013-10-17 15:37:26 +00002784 FunTmpl->setPreviousDecl(PrevFunTmpl);
Sebastian Redl833ef452010-01-26 22:01:41 +00002785 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00002786
Axel Naumannfbc7b982011-11-08 18:21:06 +00002787 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00002788 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00002789}
2790
Rafael Espindola8db352d2013-10-17 15:37:26 +00002791FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); }
Sebastian Redl833ef452010-01-26 22:01:41 +00002792
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002793/// \brief Returns a value indicating whether this function
2794/// corresponds to a builtin function.
2795///
2796/// The function corresponds to a built-in function if it is
2797/// declared at translation scope or within an extern "C" block and
2798/// its name matches with the name of a builtin. The returned value
2799/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00002800/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002801/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00002802unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00002803 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00002804 return 0;
2805
2806 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00002807 if (!BuiltinID)
2808 return 0;
2809
2810 ASTContext &Context = getASTContext();
Warren Hunt445d83e2013-11-01 23:46:51 +00002811 if (Context.getLangOpts().CPlusPlus) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002812 const auto *LinkageDecl =
2813 dyn_cast<LinkageSpecDecl>(getFirstDecl()->getDeclContext());
Warren Hunt445d83e2013-11-01 23:46:51 +00002814 // In C++, the first declaration of a builtin is always inside an implicit
2815 // extern "C".
2816 // FIXME: A recognised library function may not be directly in an extern "C"
2817 // declaration, for instance "extern "C" { namespace std { decl } }".
David Majnemerba3e5ec2015-03-13 18:26:17 +00002818 if (!LinkageDecl) {
2819 if (BuiltinID == Builtin::BI__GetExceptionInfo &&
David Majnemer145abde2016-01-26 01:12:17 +00002820 Context.getTargetInfo().getCXXABI().isMicrosoft())
David Majnemerba3e5ec2015-03-13 18:26:17 +00002821 return Builtin::BI__GetExceptionInfo;
2822 return 0;
2823 }
2824 if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c)
Warren Hunt445d83e2013-11-01 23:46:51 +00002825 return 0;
2826 }
2827
2828 // If the function is marked "overloadable", it has a different mangled name
2829 // and is not the C library function.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002830 if (hasAttr<OverloadableAttr>())
Warren Hunt445d83e2013-11-01 23:46:51 +00002831 return 0;
2832
Douglas Gregore711f702009-02-14 18:57:46 +00002833 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2834 return BuiltinID;
2835
2836 // This function has the name of a known C library
2837 // function. Determine whether it actually refers to the C library
2838 // function or whether it just has the same name.
2839
Douglas Gregora908e7f2009-02-17 03:23:10 +00002840 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00002841 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00002842 return 0;
2843
Anastasia Stulova1202de32016-02-12 12:07:04 +00002844 // OpenCL v1.2 s6.9.f - The library functions defined in
2845 // the C99 standard headers are not available.
2846 if (Context.getLangOpts().OpenCL &&
2847 Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2848 return 0;
2849
Warren Hunt445d83e2013-11-01 23:46:51 +00002850 return BuiltinID;
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002851}
2852
2853
Chris Lattner47c0d002009-04-25 06:03:53 +00002854/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00002855/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00002856/// after it has been created.
2857unsigned FunctionDecl::getNumParams() const {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00002858 const auto *FPT = getType()->getAs<FunctionProtoType>();
Alp Toker9cacbab2014-01-20 20:26:09 +00002859 return FPT ? FPT->getNumParams() : 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002860}
2861
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002862void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002863 ArrayRef<ParmVarDecl *> NewParamInfo) {
Craig Topper36250ad2014-05-12 05:36:57 +00002864 assert(!ParamInfo && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002865 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002866
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002867 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002868 if (!NewParamInfo.empty()) {
2869 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2870 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002871 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002872}
Chris Lattner41943152007-01-25 04:52:46 +00002873
Chris Lattner58258242008-04-10 02:22:51 +00002874/// getMinRequiredArguments - Returns the minimum number of arguments
2875/// needed to call this function. This may be fewer than the number of
2876/// function parameters, if some of the parameters have default
Richard Smith91151782014-04-01 19:18:16 +00002877/// arguments (in C++) or are parameter packs (C++11).
Chris Lattner58258242008-04-10 02:22:51 +00002878unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002879 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002880 return getNumParams();
Chris Lattner58258242008-04-10 02:22:51 +00002881
Richard Smith91151782014-04-01 19:18:16 +00002882 unsigned NumRequiredArgs = 0;
David Majnemer59f77922016-06-24 04:05:48 +00002883 for (auto *Param : parameters())
Richard Smith91151782014-04-01 19:18:16 +00002884 if (!Param->isParameterPack() && !Param->hasDefaultArg())
2885 ++NumRequiredArgs;
Chris Lattner58258242008-04-10 02:22:51 +00002886 return NumRequiredArgs;
2887}
2888
David Majnemer54e3ba52014-04-02 23:17:29 +00002889/// \brief The combination of the extern and inline keywords under MSVC forces
2890/// the function to be required.
2891///
2892/// Note: This function assumes that we will only get called when isInlined()
2893/// would return true for this FunctionDecl.
2894bool FunctionDecl::isMSExternInline() const {
2895 assert(isInlined() && "expected to get called on an inlined function!");
2896
2897 const ASTContext &Context = getASTContext();
David Majnemer3f021502015-10-08 04:53:31 +00002898 if (!Context.getTargetInfo().getCXXABI().isMicrosoft() &&
2899 !hasAttr<DLLExportAttr>())
David Majnemer54e3ba52014-04-02 23:17:29 +00002900 return false;
2901
David Majnemer73768702015-03-20 00:02:27 +00002902 for (const FunctionDecl *FD = getMostRecentDecl(); FD;
2903 FD = FD->getPreviousDecl())
David Majnemerc0c42f32015-07-10 20:55:38 +00002904 if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern)
David Majnemer54e3ba52014-04-02 23:17:29 +00002905 return true;
2906
2907 return false;
2908}
2909
2910static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) {
2911 if (Redecl->getStorageClass() != SC_Extern)
2912 return false;
2913
2914 for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD;
2915 FD = FD->getPreviousDecl())
David Majnemerc0c42f32015-07-10 20:55:38 +00002916 if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern)
David Majnemer54e3ba52014-04-02 23:17:29 +00002917 return false;
2918
2919 return true;
2920}
2921
Eli Friedman1b125c32012-02-07 03:50:18 +00002922static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2923 // Only consider file-scope declarations in this test.
2924 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2925 return false;
2926
2927 // Only consider explicit declarations; the presence of a builtin for a
2928 // libcall shouldn't affect whether a definition is externally visible.
2929 if (Redecl->isImplicit())
2930 return false;
2931
2932 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2933 return true; // Not an inline definition
2934
2935 return false;
2936}
2937
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002938/// \brief For a function declaration in C or C++, determine whether this
2939/// declaration causes the definition to be externally visible.
2940///
David Majnemer54e3ba52014-04-02 23:17:29 +00002941/// For instance, this determines if adding the current declaration to the set
Eli Friedman1b125c32012-02-07 03:50:18 +00002942/// of redeclarations of the given functions causes
2943/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002944bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
Justin Lebar0241c962016-10-28 16:46:39 +00002945 assert(!doesThisDeclarationHaveABody() &&
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002946 "Must have a declaration without a body.");
2947
2948 ASTContext &Context = getASTContext();
2949
David Majnemer54e3ba52014-04-02 23:17:29 +00002950 if (Context.getLangOpts().MSVCCompat) {
2951 const FunctionDecl *Definition;
2952 if (hasBody(Definition) && Definition->isInlined() &&
2953 redeclForcesDefMSVC(this))
2954 return true;
2955 }
2956
David Blaikiebbafb8a2012-03-11 07:00:24 +00002957 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002958 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2959 // an externally visible definition.
2960 //
2961 // FIXME: What happens if gnu_inline gets added on after the first
2962 // declaration?
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002963 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002964 return false;
2965
2966 const FunctionDecl *Prev = this;
2967 bool FoundBody = false;
2968 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002969 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002970
2971 if (Prev->Body) {
2972 // If it's not the case that both 'inline' and 'extern' are
2973 // specified on the definition, then it is always externally visible.
2974 if (!Prev->isInlineSpecified() ||
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002975 Prev->getStorageClass() != SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002976 return false;
2977 } else if (Prev->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002978 Prev->getStorageClass() != SC_Extern) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002979 return false;
2980 }
2981 }
2982 return FoundBody;
2983 }
2984
David Blaikiebbafb8a2012-03-11 07:00:24 +00002985 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002986 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002987
2988 // C99 6.7.4p6:
2989 // [...] If all of the file scope declarations for a function in a
2990 // translation unit include the inline function specifier without extern,
2991 // then the definition in that translation unit is an inline definition.
2992 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002993 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002994 const FunctionDecl *Prev = this;
2995 bool FoundBody = false;
2996 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002997 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002998 if (RedeclForcesDefC99(Prev))
2999 return false;
3000 }
3001 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00003002}
3003
Alp Tokerd0787eb2014-07-02 01:47:15 +00003004SourceRange FunctionDecl::getReturnTypeSourceRange() const {
3005 const TypeSourceInfo *TSI = getTypeSourceInfo();
3006 if (!TSI)
3007 return SourceRange();
Alp Tokerf5b10792014-07-02 12:55:58 +00003008 FunctionTypeLoc FTL =
3009 TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
3010 if (!FTL)
Alp Tokerd0787eb2014-07-02 01:47:15 +00003011 return SourceRange();
3012
Alp Tokerf5b10792014-07-02 12:55:58 +00003013 // Skip self-referential return types.
3014 const SourceManager &SM = getASTContext().getSourceManager();
3015 SourceRange RTRange = FTL.getReturnLoc().getSourceRange();
3016 SourceLocation Boundary = getNameInfo().getLocStart();
3017 if (RTRange.isInvalid() || Boundary.isInvalid() ||
3018 !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary))
3019 return SourceRange();
Alp Tokerd0787eb2014-07-02 01:47:15 +00003020
Alp Tokerf5b10792014-07-02 12:55:58 +00003021 return RTRange;
Alp Tokerd0787eb2014-07-02 01:47:15 +00003022}
3023
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +00003024SourceRange FunctionDecl::getExceptionSpecSourceRange() const {
3025 const TypeSourceInfo *TSI = getTypeSourceInfo();
3026 if (!TSI)
3027 return SourceRange();
3028 FunctionTypeLoc FTL =
3029 TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
3030 if (!FTL)
3031 return SourceRange();
3032
3033 return FTL.getExceptionSpecRange();
3034}
3035
Aaron Ballmane7964782016-03-07 22:44:55 +00003036const Attr *FunctionDecl::getUnusedResultAttr() const {
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00003037 QualType RetType = getReturnType();
3038 if (RetType->isRecordType()) {
Erich Keane4b87d812017-04-19 21:24:55 +00003039 if (const CXXRecordDecl *Ret = RetType->getAsCXXRecordDecl()) {
Aaron Ballmane7964782016-03-07 22:44:55 +00003040 if (const auto *R = Ret->getAttr<WarnUnusedResultAttr>())
3041 return R;
3042 }
3043 } else if (const auto *ET = RetType->getAs<EnumType>()) {
3044 if (const EnumDecl *ED = ET->getDecl()) {
3045 if (const auto *R = ED->getAttr<WarnUnusedResultAttr>())
3046 return R;
3047 }
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00003048 }
Aaron Ballmane7964782016-03-07 22:44:55 +00003049 return getAttr<WarnUnusedResultAttr>();
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00003050}
3051
Richard Smithf3814ad2013-01-25 00:08:28 +00003052/// \brief For an inline function definition in C, or for a gnu_inline function
3053/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00003054///
3055/// Inline function definitions are always available for inlining optimizations.
3056/// However, depending on the language dialect, declaration specifiers, and
3057/// attributes, the definition of an inline function may or may not be
3058/// "externally" visible to other translation units in the program.
3059///
3060/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00003061/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00003062/// inline definition becomes externally visible (C99 6.7.4p6).
3063///
3064/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
3065/// definition, we use the GNU semantics for inline, which are nearly the
3066/// opposite of C99 semantics. In particular, "inline" by itself will create
3067/// an externally visible symbol, but "extern inline" will not create an
3068/// externally visible symbol.
3069bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Manuel Klimekda7456a2016-11-01 10:30:50 +00003070 assert((doesThisDeclarationHaveABody() || willHaveBody()) &&
3071 "Must be a function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00003072 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00003073 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00003074
David Blaikiebbafb8a2012-03-11 07:00:24 +00003075 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00003076 // Note: If you change the logic here, please change
3077 // doesDeclarationForceExternallyVisibleDefinition as well.
3078 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00003079 // If it's not the case that both 'inline' and 'extern' are
3080 // specified on the definition, then this inline definition is
3081 // externally visible.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003082 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregorff76cb92010-12-09 16:59:22 +00003083 return true;
3084
3085 // If any declaration is 'inline' but not 'extern', then this definition
3086 // is externally visible.
Aaron Ballman86c93902014-03-06 23:45:36 +00003087 for (auto Redecl : redecls()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00003088 if (Redecl->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003089 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00003090 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00003091 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00003092
Douglas Gregor76fe50c2009-04-28 06:37:30 +00003093 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00003094 }
Eli Friedman1b125c32012-02-07 03:50:18 +00003095
Richard Smithf3814ad2013-01-25 00:08:28 +00003096 // The rest of this function is C-only.
3097 assert(!Context.getLangOpts().CPlusPlus &&
3098 "should not use C inline rules in C++");
3099
Douglas Gregor299d76e2009-09-13 07:46:26 +00003100 // C99 6.7.4p6:
3101 // [...] If all of the file scope declarations for a function in a
3102 // translation unit include the inline function specifier without extern,
3103 // then the definition in that translation unit is an inline definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00003104 for (auto Redecl : redecls()) {
3105 if (RedeclForcesDefC99(Redecl))
Eli Friedman1b125c32012-02-07 03:50:18 +00003106 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00003107 }
3108
3109 // C99 6.7.4p6:
3110 // An inline definition does not provide an external definition for the
3111 // function, and does not forbid an external definition in another
3112 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00003113 return false;
3114}
3115
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003116/// getOverloadedOperator - Which C++ overloaded operator this
3117/// function represents, if any.
3118OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00003119 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
3120 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003121 else
3122 return OO_None;
3123}
3124
Alexis Huntc88db062010-01-13 09:01:02 +00003125/// getLiteralIdentifier - The literal suffix identifier this function
3126/// represents, if any.
3127const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
3128 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
3129 return getDeclName().getCXXLiteralIdentifier();
3130 else
Craig Topper36250ad2014-05-12 05:36:57 +00003131 return nullptr;
Alexis Huntc88db062010-01-13 09:01:02 +00003132}
3133
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00003134FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
3135 if (TemplateOrSpecialization.isNull())
3136 return TK_NonTemplate;
3137 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
3138 return TK_FunctionTemplate;
3139 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
3140 return TK_MemberSpecialization;
3141 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
3142 return TK_FunctionTemplateSpecialization;
3143 if (TemplateOrSpecialization.is
3144 <DependentFunctionTemplateSpecializationInfo*>())
3145 return TK_DependentFunctionTemplateSpecialization;
3146
David Blaikie83d382b2011-09-23 05:06:16 +00003147 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00003148}
3149
Douglas Gregord801b062009-10-07 23:56:10 +00003150FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00003151 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00003152 return cast<FunctionDecl>(Info->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00003153
3154 return nullptr;
Douglas Gregord801b062009-10-07 23:56:10 +00003155}
3156
Chandler Carruth21c90602015-12-30 03:24:14 +00003157MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
3158 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>();
3159}
3160
Douglas Gregord801b062009-10-07 23:56:10 +00003161void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00003162FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
3163 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00003164 TemplateSpecializationKind TSK) {
3165 assert(TemplateOrSpecialization.isNull() &&
3166 "Member function is already a specialization");
3167 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00003168 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00003169 TemplateOrSpecialization = Info;
3170}
3171
Chandler Carruth21c90602015-12-30 03:24:14 +00003172FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const {
3173 return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>();
3174}
3175
3176void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) {
3177 TemplateOrSpecialization = Template;
3178}
3179
Douglas Gregorafca3b42009-10-27 20:53:28 +00003180bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00003181 // If the function is invalid, it can't be implicitly instantiated.
3182 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00003183 return false;
3184
3185 switch (getTemplateSpecializationKind()) {
3186 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00003187 case TSK_ExplicitInstantiationDefinition:
3188 return false;
3189
3190 case TSK_ImplicitInstantiation:
3191 return true;
3192
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003193 // It is possible to instantiate TSK_ExplicitSpecialization kind
3194 // if the FunctionDecl has a class scope specialization pattern.
3195 case TSK_ExplicitSpecialization:
Craig Topper36250ad2014-05-12 05:36:57 +00003196 return getClassScopeSpecializationPattern() != nullptr;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003197
Douglas Gregorafca3b42009-10-27 20:53:28 +00003198 case TSK_ExplicitInstantiationDeclaration:
3199 // Handled below.
3200 break;
3201 }
3202
3203 // Find the actual template from which we will instantiate.
3204 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003205 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00003206 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003207 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00003208
3209 // C++0x [temp.explicit]p9:
3210 // Except for inline functions, other explicit instantiation declarations
3211 // have the effect of suppressing the implicit instantiation of the entity
3212 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003213 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00003214 return true;
3215
Douglas Gregor583dcaf2009-10-27 21:11:48 +00003216 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00003217}
3218
3219bool FunctionDecl::isTemplateInstantiation() const {
3220 switch (getTemplateSpecializationKind()) {
3221 case TSK_Undeclared:
3222 case TSK_ExplicitSpecialization:
3223 return false;
3224 case TSK_ImplicitInstantiation:
3225 case TSK_ExplicitInstantiationDeclaration:
3226 case TSK_ExplicitInstantiationDefinition:
3227 return true;
3228 }
3229 llvm_unreachable("All TSK values handled.");
3230}
Douglas Gregorafca3b42009-10-27 20:53:28 +00003231
3232FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003233 // Handle class scope explicit specialization special case.
Richard Smith2195ec92017-04-21 01:15:13 +00003234 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {
3235 if (auto *Spec = getClassScopeSpecializationPattern())
3236 return getDefinitionOrSelf(Spec);
3237 return nullptr;
3238 }
3239
Faisal Valib90b2112014-04-03 16:32:21 +00003240 // If this is a generic lambda call operator specialization, its
3241 // instantiation pattern is always its primary template's pattern
3242 // even if its primary template was instantiated from another
3243 // member template (which happens with nested generic lambdas).
3244 // Since a lambda's call operator's body is transformed eagerly,
3245 // we don't have to go hunting for a prototype definition template
3246 // (i.e. instantiated-from-member-template) to use as an instantiation
3247 // pattern.
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003248
Faisal Valib90b2112014-04-03 16:32:21 +00003249 if (isGenericLambdaCallOperatorSpecialization(
3250 dyn_cast<CXXMethodDecl>(this))) {
Richard Smith2195ec92017-04-21 01:15:13 +00003251 assert(getPrimaryTemplate() && "not a generic lambda call operator?");
3252 return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl());
Faisal Valib90b2112014-04-03 16:32:21 +00003253 }
Richard Smith2195ec92017-04-21 01:15:13 +00003254
Douglas Gregorafca3b42009-10-27 20:53:28 +00003255 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
3256 while (Primary->getInstantiatedFromMemberTemplate()) {
3257 // If we have hit a point where the user provided a specialization of
3258 // this template, we're done looking.
3259 if (Primary->isMemberSpecialization())
3260 break;
Douglas Gregorafca3b42009-10-27 20:53:28 +00003261 Primary = Primary->getInstantiatedFromMemberTemplate();
3262 }
Richard Smith2195ec92017-04-21 01:15:13 +00003263
3264 return getDefinitionOrSelf(Primary->getTemplatedDecl());
Douglas Gregorafca3b42009-10-27 20:53:28 +00003265 }
Richard Smith2195ec92017-04-21 01:15:13 +00003266
3267 if (auto *MFD = getInstantiatedFromMemberFunction())
3268 return getDefinitionOrSelf(MFD);
3269
3270 return nullptr;
Douglas Gregorafca3b42009-10-27 20:53:28 +00003271}
3272
Douglas Gregor70d83e22009-06-29 17:30:29 +00003273FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00003274 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00003275 = TemplateOrSpecialization
3276 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00003277 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00003278 }
Craig Topper36250ad2014-05-12 05:36:57 +00003279 return nullptr;
Douglas Gregor70d83e22009-06-29 17:30:29 +00003280}
3281
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003282FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
3283 return getASTContext().getClassScopeSpecializationPattern(this);
3284}
3285
Chandler Carruth21c90602015-12-30 03:24:14 +00003286FunctionTemplateSpecializationInfo *
3287FunctionDecl::getTemplateSpecializationInfo() const {
3288 return TemplateOrSpecialization
3289 .dyn_cast<FunctionTemplateSpecializationInfo *>();
3290}
3291
Douglas Gregor70d83e22009-06-29 17:30:29 +00003292const TemplateArgumentList *
3293FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00003294 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00003295 = TemplateOrSpecialization
3296 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00003297 return Info->TemplateArguments;
3298 }
Craig Topper36250ad2014-05-12 05:36:57 +00003299 return nullptr;
Douglas Gregor70d83e22009-06-29 17:30:29 +00003300}
3301
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00003302const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00003303FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
3304 if (FunctionTemplateSpecializationInfo *Info
3305 = TemplateOrSpecialization
3306 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
3307 return Info->TemplateArgumentsAsWritten;
3308 }
Craig Topper36250ad2014-05-12 05:36:57 +00003309 return nullptr;
Abramo Bagnara02ccd282010-05-20 15:32:11 +00003310}
3311
Mike Stump11289f42009-09-09 15:08:12 +00003312void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00003313FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
3314 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00003315 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003316 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00003317 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00003318 const TemplateArgumentListInfo *TemplateArgsAsWritten,
3319 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003320 assert(TSK != TSK_Undeclared &&
3321 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00003322 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00003323 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00003324 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00003325 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
3326 TemplateArgs,
3327 TemplateArgsAsWritten,
3328 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00003329 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00003330 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00003331}
3332
John McCallb9c78482010-04-08 09:05:18 +00003333void
3334FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
3335 const UnresolvedSetImpl &Templates,
3336 const TemplateArgumentListInfo &TemplateArgs) {
3337 assert(TemplateOrSpecialization.isNull());
John McCallb9c78482010-04-08 09:05:18 +00003338 DependentFunctionTemplateSpecializationInfo *Info =
James Y Knight7a22b242015-08-06 20:26:32 +00003339 DependentFunctionTemplateSpecializationInfo::Create(Context, Templates,
3340 TemplateArgs);
John McCallb9c78482010-04-08 09:05:18 +00003341 TemplateOrSpecialization = Info;
3342}
3343
James Y Knight7a22b242015-08-06 20:26:32 +00003344DependentFunctionTemplateSpecializationInfo *
Chandler Carruth21c90602015-12-30 03:24:14 +00003345FunctionDecl::getDependentSpecializationInfo() const {
3346 return TemplateOrSpecialization
3347 .dyn_cast<DependentFunctionTemplateSpecializationInfo *>();
3348}
3349
3350DependentFunctionTemplateSpecializationInfo *
James Y Knight7a22b242015-08-06 20:26:32 +00003351DependentFunctionTemplateSpecializationInfo::Create(
3352 ASTContext &Context, const UnresolvedSetImpl &Ts,
3353 const TemplateArgumentListInfo &TArgs) {
3354 void *Buffer = Context.Allocate(
3355 totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>(
3356 TArgs.size(), Ts.size()));
3357 return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs);
3358}
3359
John McCallb9c78482010-04-08 09:05:18 +00003360DependentFunctionTemplateSpecializationInfo::
3361DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
3362 const TemplateArgumentListInfo &TArgs)
Benjamin Kramer3ebba522015-04-02 12:43:31 +00003363 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
John McCallb9c78482010-04-08 09:05:18 +00003364
James Y Knight53c76162015-07-17 18:21:37 +00003365 NumTemplates = Ts.size();
3366 NumArgs = TArgs.size();
Benjamin Kramer3ebba522015-04-02 12:43:31 +00003367
James Y Knight7a22b242015-08-06 20:26:32 +00003368 FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>();
John McCallb9c78482010-04-08 09:05:18 +00003369 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
3370 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
3371
James Y Knight7a22b242015-08-06 20:26:32 +00003372 TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>();
John McCallb9c78482010-04-08 09:05:18 +00003373 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
3374 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
3375}
3376
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003377TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00003378 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003379 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00003380 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00003381 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00003382 if (FTSInfo)
3383 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00003384
Douglas Gregord801b062009-10-07 23:56:10 +00003385 MemberSpecializationInfo *MSInfo
3386 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
3387 if (MSInfo)
3388 return MSInfo->getTemplateSpecializationKind();
3389
3390 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003391}
3392
Mike Stump11289f42009-09-09 15:08:12 +00003393void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003394FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3395 SourceLocation PointOfInstantiation) {
3396 if (FunctionTemplateSpecializationInfo *FTSInfo
3397 = TemplateOrSpecialization.dyn_cast<
3398 FunctionTemplateSpecializationInfo*>()) {
3399 FTSInfo->setTemplateSpecializationKind(TSK);
3400 if (TSK != TSK_ExplicitSpecialization &&
3401 PointOfInstantiation.isValid() &&
3402 FTSInfo->getPointOfInstantiation().isInvalid())
3403 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
3404 } else if (MemberSpecializationInfo *MSInfo
3405 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
3406 MSInfo->setTemplateSpecializationKind(TSK);
3407 if (TSK != TSK_ExplicitSpecialization &&
3408 PointOfInstantiation.isValid() &&
3409 MSInfo->getPointOfInstantiation().isInvalid())
3410 MSInfo->setPointOfInstantiation(PointOfInstantiation);
3411 } else
David Blaikie83d382b2011-09-23 05:06:16 +00003412 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003413}
3414
3415SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00003416 if (FunctionTemplateSpecializationInfo *FTSInfo
3417 = TemplateOrSpecialization.dyn_cast<
3418 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003419 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00003420 else if (MemberSpecializationInfo *MSInfo
3421 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00003422 return MSInfo->getPointOfInstantiation();
3423
3424 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00003425}
3426
Douglas Gregor6411b922009-09-11 20:15:17 +00003427bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00003428 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00003429 return true;
3430
3431 // If this function was instantiated from a member function of a
3432 // class template, check whether that member function was defined out-of-line.
3433 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
3434 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003435 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00003436 return Definition->isOutOfLine();
3437 }
3438
3439 // If this function was instantiated from a function template,
3440 // check whether that function template was defined out-of-line.
3441 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
3442 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00003443 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00003444 return Definition->isOutOfLine();
3445 }
3446
3447 return false;
3448}
3449
Abramo Bagnaraea947882011-03-08 16:41:52 +00003450SourceRange FunctionDecl::getSourceRange() const {
3451 return SourceRange(getOuterLocStart(), EndRangeLoc);
3452}
3453
Anna Zaks28db7ce2012-01-18 02:45:01 +00003454unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00003455 IdentifierInfo *FnInfo = getIdentifier();
3456
3457 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00003458 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00003459
3460 // Builtin handling.
3461 switch (getBuiltinID()) {
3462 case Builtin::BI__builtin_memset:
3463 case Builtin::BI__builtin___memset_chk:
3464 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00003465 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00003466
3467 case Builtin::BI__builtin_memcpy:
3468 case Builtin::BI__builtin___memcpy_chk:
3469 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00003470 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003471
3472 case Builtin::BI__builtin_memmove:
3473 case Builtin::BI__builtin___memmove_chk:
3474 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00003475 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00003476
3477 case Builtin::BIstrlcpy:
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00003478 case Builtin::BI__builtin___strlcpy_chk:
Anna Zaks22122702012-01-17 00:37:07 +00003479 return Builtin::BIstrlcpy;
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00003480
Anna Zaks201d4892012-01-13 21:52:01 +00003481 case Builtin::BIstrlcat:
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00003482 case Builtin::BI__builtin___strlcat_chk:
Anna Zaks22122702012-01-17 00:37:07 +00003483 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00003484
3485 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00003486 case Builtin::BImemcmp:
3487 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003488
3489 case Builtin::BI__builtin_strncpy:
3490 case Builtin::BI__builtin___strncpy_chk:
3491 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00003492 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003493
3494 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00003495 case Builtin::BIstrncmp:
3496 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003497
3498 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00003499 case Builtin::BIstrncasecmp:
3500 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003501
3502 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00003503 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00003504 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00003505 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00003506
3507 case Builtin::BI__builtin_strndup:
3508 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00003509 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00003510
Anna Zaks314cd092012-02-01 19:08:57 +00003511 case Builtin::BI__builtin_strlen:
3512 case Builtin::BIstrlen:
3513 return Builtin::BIstrlen;
3514
Bruno Cardoso Lopes7ea9fd22016-08-10 18:34:47 +00003515 case Builtin::BI__builtin_bzero:
3516 case Builtin::BIbzero:
3517 return Builtin::BIbzero;
3518
Anna Zaks201d4892012-01-13 21:52:01 +00003519 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00003520 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00003521 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00003522 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00003523 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00003524 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003525 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00003526 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00003527 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00003528 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003529 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00003530 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00003531 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00003532 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003533 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00003534 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00003535 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00003536 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00003537 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00003538 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00003539 else if (FnInfo->isStr("strlen"))
3540 return Builtin::BIstrlen;
Bruno Cardoso Lopes7ea9fd22016-08-10 18:34:47 +00003541 else if (FnInfo->isStr("bzero"))
3542 return Builtin::BIbzero;
Anna Zaks201d4892012-01-13 21:52:01 +00003543 }
3544 break;
3545 }
Anna Zaks22122702012-01-17 00:37:07 +00003546 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00003547}
3548
Chris Lattner59a25942008-03-31 00:36:02 +00003549//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003550// FieldDecl Implementation
3551//===----------------------------------------------------------------------===//
3552
Jay Foad39c79802011-01-12 09:06:06 +00003553FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003554 SourceLocation StartLoc, SourceLocation IdLoc,
3555 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00003556 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00003557 InClassInitStyle InitStyle) {
Richard Smithf7981722013-11-22 09:01:48 +00003558 return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
3559 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00003560}
3561
Douglas Gregor72172e92012-01-05 21:55:30 +00003562FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00003563 return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(),
3564 SourceLocation(), nullptr, QualType(), nullptr,
3565 nullptr, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00003566}
3567
Sebastian Redl833ef452010-01-26 22:01:41 +00003568bool FieldDecl::isAnonymousStructOrUnion() const {
3569 if (!isImplicit() || getDeclName())
3570 return false;
3571
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003572 if (const auto *Record = getType()->getAs<RecordType>())
Sebastian Redl833ef452010-01-26 22:01:41 +00003573 return Record->getDecl()->isAnonymousStructOrUnion();
3574
3575 return false;
3576}
3577
Richard Smithcaf33902011-10-10 18:28:20 +00003578unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
3579 assert(isBitField() && "not a bitfield");
Richard Smith6b8e3c02017-08-28 00:28:14 +00003580 return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
Richard Smithcaf33902011-10-10 18:28:20 +00003581}
3582
John McCall4e819612011-01-20 07:57:12 +00003583unsigned FieldDecl::getFieldIndex() const {
Richard Smith0b87e072013-10-07 08:02:11 +00003584 const FieldDecl *Canonical = getCanonicalDecl();
3585 if (Canonical != this)
3586 return Canonical->getFieldIndex();
3587
John McCall4e819612011-01-20 07:57:12 +00003588 if (CachedFieldIndex) return CachedFieldIndex - 1;
3589
Richard Smithd62306a2011-11-10 06:34:14 +00003590 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00003591 const RecordDecl *RD = getParent();
Richard Smithd62306a2011-11-10 06:34:14 +00003592
Hans Wennborga302cd92014-08-21 16:06:57 +00003593 for (auto *Field : RD->fields()) {
3594 Field->getCanonicalDecl()->CachedFieldIndex = Index + 1;
3595 ++Index;
3596 }
John McCall4e819612011-01-20 07:57:12 +00003597
Richard Smithd62306a2011-11-10 06:34:14 +00003598 assert(CachedFieldIndex && "failed to find field in parent");
3599 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00003600}
3601
Abramo Bagnara20c9e242011-03-08 11:07:11 +00003602SourceRange FieldDecl::getSourceRange() const {
Richard Smith6b8e3c02017-08-28 00:28:14 +00003603 const Expr *FinalExpr = getInClassInitializer();
3604 if (!FinalExpr)
3605 FinalExpr = getBitWidth();
3606 if (FinalExpr)
3607 return SourceRange(getInnerLocStart(), FinalExpr->getLocEnd());
3608 return DeclaratorDecl::getSourceRange();
Alexey Bataev39c81e22014-08-28 04:28:19 +00003609}
3610
3611void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) {
David Blaikie11ab0782014-10-31 17:18:09 +00003612 assert((getParent()->isLambda() || getParent()->isCapturedRecord()) &&
Alexey Bataev330de032014-10-29 12:21:55 +00003613 "capturing type in non-lambda or captured record.");
Richard Smith6b8e3c02017-08-28 00:28:14 +00003614 assert(InitStorage.getInt() == ISK_NoInit &&
John McCallc90c1492014-10-10 18:44:34 +00003615 InitStorage.getPointer() == nullptr &&
Alexey Bataev39c81e22014-08-28 04:28:19 +00003616 "bit width, initializer or captured type already set");
John McCallc90c1492014-10-10 18:44:34 +00003617 InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType),
3618 ISK_CapturedVLAType);
Alexey Bataev39c81e22014-08-28 04:28:19 +00003619}
3620
Sebastian Redl833ef452010-01-26 22:01:41 +00003621//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003622// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00003623//===----------------------------------------------------------------------===//
3624
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00003625SourceLocation TagDecl::getOuterLocStart() const {
3626 return getTemplateOrInnerLocStart(this);
3627}
3628
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003629SourceRange TagDecl::getSourceRange() const {
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00003630 SourceLocation RBraceLoc = BraceRange.getEnd();
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003631 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00003632 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003633}
3634
Rafael Espindola8db352d2013-10-17 15:37:26 +00003635TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); }
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00003636
Rafael Espindolabf5c33b2013-03-12 21:06:00 +00003637void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
David Majnemer00350522015-08-31 18:48:39 +00003638 TypedefNameDeclOrQualifier = TDD;
Reid Klecknercae82a22014-04-23 22:03:04 +00003639 if (const Type *T = getTypeForDecl()) {
3640 (void)T;
Richard Smith5b21db82014-04-23 18:20:42 +00003641 assert(T->isLinkageValid());
Reid Klecknercae82a22014-04-23 22:03:04 +00003642 }
Rafael Espindola0e0d0092013-03-14 03:07:35 +00003643 assert(isLinkageValid());
Douglas Gregora72a4e32010-05-19 18:39:18 +00003644}
3645
Douglas Gregordee1be82009-01-17 00:42:38 +00003646void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00003647 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00003648
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003649 if (auto *D = dyn_cast<CXXRecordDecl>(this)) {
Richard Smith053f6c62014-05-16 23:01:30 +00003650 struct CXXRecordDecl::DefinitionData *Data =
John McCall67da35c2010-02-04 22:26:26 +00003651 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
Aaron Ballman86c93902014-03-06 23:45:36 +00003652 for (auto I : redecls())
Richard Smith64c06302014-05-22 23:19:02 +00003653 cast<CXXRecordDecl>(I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00003654 }
Douglas Gregordee1be82009-01-17 00:42:38 +00003655}
3656
3657void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00003658 assert((!isa<CXXRecordDecl>(this) ||
3659 cast<CXXRecordDecl>(this)->hasDefinition()) &&
3660 "definition completed but not started");
3661
John McCallf937c022011-10-07 06:10:15 +00003662 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00003663 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003664
3665 if (ASTMutationListener *L = getASTMutationListener())
3666 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00003667}
3668
John McCallf937c022011-10-07 06:10:15 +00003669TagDecl *TagDecl::getDefinition() const {
3670 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00003671 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003672
3673 // If it's possible for us to have an out-of-date definition, check now.
3674 if (MayHaveOutOfDateDef) {
3675 if (IdentifierInfo *II = getIdentifier()) {
3676 if (II->isOutOfDate()) {
3677 updateOutOfDate(*II);
3678 }
3679 }
3680 }
3681
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003682 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this))
Andrew Trickba266ee2010-10-19 21:54:32 +00003683 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003684
Aaron Ballman86c93902014-03-06 23:45:36 +00003685 for (auto R : redecls())
John McCallf937c022011-10-07 06:10:15 +00003686 if (R->isCompleteDefinition())
Aaron Ballman86c93902014-03-06 23:45:36 +00003687 return R;
Mike Stump11289f42009-09-09 15:08:12 +00003688
Craig Topper36250ad2014-05-12 05:36:57 +00003689 return nullptr;
Ted Kremenek21475702008-09-05 17:16:31 +00003690}
3691
Douglas Gregor14454802011-02-25 02:25:35 +00003692void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
3693 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00003694 // Make sure the extended qualifier info is allocated.
3695 if (!hasExtInfo())
David Majnemer00350522015-08-31 18:48:39 +00003696 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00003697 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00003698 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00003699 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00003700 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00003701 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00003702 if (getExtInfo()->NumTemplParamLists == 0) {
3703 getASTContext().Deallocate(getExtInfo());
David Majnemer00350522015-08-31 18:48:39 +00003704 TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003705 }
3706 else
3707 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00003708 }
3709 }
3710}
3711
Benjamin Kramer9cc210652015-08-05 09:40:49 +00003712void TagDecl::setTemplateParameterListsInfo(
3713 ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
3714 assert(!TPLists.empty());
Abramo Bagnara60804e12011-03-18 15:16:37 +00003715 // Make sure the extended decl info is allocated.
3716 if (!hasExtInfo())
3717 // Allocate external info struct.
David Majnemer00350522015-08-31 18:48:39 +00003718 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003719 // Set the template parameter lists info.
Benjamin Kramer9cc210652015-08-05 09:40:49 +00003720 getExtInfo()->setTemplateParameterListsInfo(Context, TPLists);
Abramo Bagnara60804e12011-03-18 15:16:37 +00003721}
3722
Ted Kremenek21475702008-09-05 17:16:31 +00003723//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003724// EnumDecl Implementation
3725//===----------------------------------------------------------------------===//
3726
David Blaikie68e081d2011-12-20 02:48:34 +00003727void EnumDecl::anchor() { }
3728
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003729EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3730 SourceLocation StartLoc, SourceLocation IdLoc,
3731 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003732 EnumDecl *PrevDecl, bool IsScoped,
3733 bool IsScopedUsingClassTag, bool IsFixed) {
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003734 auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl,
3735 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003736 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00003737 C.getTypeDeclType(Enum, PrevDecl);
3738 return Enum;
3739}
3740
Douglas Gregor72172e92012-01-05 21:55:30 +00003741EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00003742 EnumDecl *Enum =
3743 new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(),
3744 nullptr, nullptr, false, false, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003745 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3746 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003747}
3748
Alp Tokerb9fa5122014-01-06 11:31:18 +00003749SourceRange EnumDecl::getIntegerTypeRange() const {
3750 if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo())
3751 return TI->getTypeLoc().getSourceRange();
3752 return SourceRange();
3753}
3754
Douglas Gregord5058122010-02-11 01:19:42 +00003755void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00003756 QualType NewPromotionType,
3757 unsigned NumPositiveBits,
3758 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00003759 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00003760 if (!IntegerType)
3761 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00003762 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00003763 setNumPositiveBits(NumPositiveBits);
3764 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00003765 TagDecl::completeDefinition();
3766}
3767
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003768bool EnumDecl::isClosed() const {
3769 if (const auto *A = getAttr<EnumExtensibilityAttr>())
3770 return A->getExtensibility() == EnumExtensibilityAttr::Closed;
3771 return true;
3772}
3773
3774bool EnumDecl::isClosedFlag() const {
3775 return isClosed() && hasAttr<FlagEnumAttr>();
3776}
3777
3778bool EnumDecl::isClosedNonFlag() const {
3779 return isClosed() && !hasAttr<FlagEnumAttr>();
3780}
3781
Richard Smith7d137e32012-03-23 03:33:32 +00003782TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3783 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3784 return MSI->getTemplateSpecializationKind();
3785
3786 return TSK_Undeclared;
3787}
3788
3789void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3790 SourceLocation PointOfInstantiation) {
3791 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3792 assert(MSI && "Not an instantiated member enumeration?");
3793 MSI->setTemplateSpecializationKind(TSK);
3794 if (TSK != TSK_ExplicitSpecialization &&
3795 PointOfInstantiation.isValid() &&
3796 MSI->getPointOfInstantiation().isInvalid())
3797 MSI->setPointOfInstantiation(PointOfInstantiation);
3798}
3799
Richard Smith6739a102016-05-05 00:56:12 +00003800EnumDecl *EnumDecl::getTemplateInstantiationPattern() const {
3801 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
3802 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
3803 EnumDecl *ED = getInstantiatedFromMemberEnum();
3804 while (auto *NewED = ED->getInstantiatedFromMemberEnum())
3805 ED = NewED;
Richard Smith2195ec92017-04-21 01:15:13 +00003806 return getDefinitionOrSelf(ED);
Richard Smith6739a102016-05-05 00:56:12 +00003807 }
3808 }
3809
3810 assert(!isTemplateInstantiation(getTemplateSpecializationKind()) &&
3811 "couldn't find pattern for enum instantiation");
3812 return nullptr;
3813}
3814
Richard Smith4b38ded2012-03-14 23:13:10 +00003815EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3816 if (SpecializationInfo)
3817 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3818
Craig Topper36250ad2014-05-12 05:36:57 +00003819 return nullptr;
Richard Smith4b38ded2012-03-14 23:13:10 +00003820}
3821
3822void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3823 TemplateSpecializationKind TSK) {
3824 assert(!SpecializationInfo && "Member enum is already a specialization");
3825 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3826}
3827
Sebastian Redl833ef452010-01-26 22:01:41 +00003828//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00003829// RecordDecl Implementation
3830//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00003831
Richard Smith053f6c62014-05-16 23:01:30 +00003832RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C,
3833 DeclContext *DC, SourceLocation StartLoc,
3834 SourceLocation IdLoc, IdentifierInfo *Id,
3835 RecordDecl *PrevDecl)
3836 : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00003837 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003838 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003839 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003840 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003841 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00003842 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00003843}
3844
Jay Foad39c79802011-01-12 09:06:06 +00003845RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003846 SourceLocation StartLoc, SourceLocation IdLoc,
3847 IdentifierInfo *Id, RecordDecl* PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00003848 RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC,
3849 StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003850 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3851
Ted Kremenek21475702008-09-05 17:16:31 +00003852 C.getTypeDeclType(R, PrevDecl);
3853 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00003854}
3855
Douglas Gregor72172e92012-01-05 21:55:30 +00003856RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00003857 RecordDecl *R =
3858 new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(),
3859 SourceLocation(), nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003860 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3861 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003862}
3863
Douglas Gregordfcad112009-03-25 15:59:44 +00003864bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00003865 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00003866 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3867}
3868
Alexey Bataev39c81e22014-08-28 04:28:19 +00003869bool RecordDecl::isLambda() const {
3870 if (auto RD = dyn_cast<CXXRecordDecl>(this))
3871 return RD->isLambda();
3872 return false;
3873}
3874
Alexey Bataev330de032014-10-29 12:21:55 +00003875bool RecordDecl::isCapturedRecord() const {
3876 return hasAttr<CapturedRecordAttr>();
3877}
3878
3879void RecordDecl::setCapturedRecord() {
3880 addAttr(CapturedRecordAttr::CreateImplicit(getASTContext()));
3881}
3882
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003883RecordDecl::field_iterator RecordDecl::field_begin() const {
3884 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3885 LoadFieldsFromExternalStorage();
3886
3887 return field_iterator(decl_iterator(FirstDecl));
3888}
3889
Douglas Gregorb11aad82011-02-19 18:51:44 +00003890/// completeDefinition - Notes that the definition of this type is now
3891/// complete.
3892void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00003893 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00003894 TagDecl::completeDefinition();
3895}
3896
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003897/// isMsStruct - Get whether or not this record uses ms_struct layout.
3898/// This which can be turned on with an attribute, pragma, or the
3899/// -mms-bitfields command-line option.
3900bool RecordDecl::isMsStruct(const ASTContext &C) const {
David Majnemer8ab003a2015-02-02 19:30:52 +00003901 return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003902}
3903
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003904void RecordDecl::LoadFieldsFromExternalStorage() const {
3905 ExternalASTSource *Source = getASTContext().getExternalSource();
3906 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3907
3908 // Notify that we have a RecordDecl doing some initialization.
3909 ExternalASTSource::Deserializing TheFields(Source);
3910
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003911 SmallVector<Decl*, 64> Decls;
Richard Smith3cb15722015-08-05 22:41:45 +00003912 LoadedFieldsFromExternalStorage = true;
3913 Source->FindExternalLexicalDecls(this, [](Decl::Kind K) {
3914 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3915 }, Decls);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003916
3917#ifndef NDEBUG
3918 // Check that all decls we got were FieldDecls.
3919 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003920 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003921#endif
3922
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003923 if (Decls.empty())
3924 return;
3925
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00003926 std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00003927 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003928}
3929
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003930bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const {
3931 ASTContext &Context = getASTContext();
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +00003932 if (!Context.getLangOpts().Sanitize.hasOneOf(
3933 SanitizerKind::Address | SanitizerKind::KernelAddress) ||
Alexey Samsonova0416102014-11-11 01:26:14 +00003934 !Context.getLangOpts().SanitizeAddressFieldPadding)
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003935 return false;
Alexey Samsonov33e00e22014-10-16 23:50:26 +00003936 const auto &Blacklist = Context.getSanitizerBlacklist();
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003937 const auto *CXXRD = dyn_cast<CXXRecordDecl>(this);
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003938 // We may be able to relax some of these requirements.
3939 int ReasonToReject = -1;
3940 if (!CXXRD || CXXRD->isExternCContext())
3941 ReasonToReject = 0; // is not C++.
3942 else if (CXXRD->hasAttr<PackedAttr>())
3943 ReasonToReject = 1; // is packed.
3944 else if (CXXRD->isUnion())
3945 ReasonToReject = 2; // is a union.
3946 else if (CXXRD->isTriviallyCopyable())
3947 ReasonToReject = 3; // is trivially copyable.
3948 else if (CXXRD->hasTrivialDestructor())
3949 ReasonToReject = 4; // has trivial destructor.
3950 else if (CXXRD->isStandardLayout())
3951 ReasonToReject = 5; // is standard layout.
Alexey Samsonov33e00e22014-10-16 23:50:26 +00003952 else if (Blacklist.isBlacklistedLocation(getLocation(), "field-padding"))
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003953 ReasonToReject = 6; // is in a blacklisted file.
3954 else if (Blacklist.isBlacklistedType(getQualifiedNameAsString(),
3955 "field-padding"))
3956 ReasonToReject = 7; // is blacklisted.
3957
3958 if (EmitRemark) {
3959 if (ReasonToReject >= 0)
3960 Context.getDiagnostics().Report(
3961 getLocation(),
3962 diag::remark_sanitize_address_insert_extra_padding_rejected)
3963 << getQualifiedNameAsString() << ReasonToReject;
3964 else
3965 Context.getDiagnostics().Report(
3966 getLocation(),
3967 diag::remark_sanitize_address_insert_extra_padding_accepted)
3968 << getQualifiedNameAsString();
3969 }
3970 return ReasonToReject < 0;
3971}
3972
Evgeny Astigeevich665027d2014-12-12 16:17:46 +00003973const FieldDecl *RecordDecl::findFirstNamedDataMember() const {
3974 for (const auto *I : fields()) {
3975 if (I->getIdentifier())
3976 return I;
3977
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003978 if (const auto *RT = I->getType()->getAs<RecordType>())
Evgeny Astigeevich665027d2014-12-12 16:17:46 +00003979 if (const FieldDecl *NamedDataMember =
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00003980 RT->getDecl()->findFirstNamedDataMember())
Evgeny Astigeevich665027d2014-12-12 16:17:46 +00003981 return NamedDataMember;
3982 }
3983
3984 // We didn't find a named data member.
3985 return nullptr;
3986}
3987
3988
Steve Naroff415d3d52008-10-08 17:01:13 +00003989//===----------------------------------------------------------------------===//
3990// BlockDecl Implementation
3991//===----------------------------------------------------------------------===//
3992
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003993void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Craig Topper36250ad2014-05-12 05:36:57 +00003994 assert(!ParamInfo && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00003995
Steve Naroffc4b30e52009-03-13 16:56:44 +00003996 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00003997 if (!NewParamInfo.empty()) {
3998 NumParams = NewParamInfo.size();
3999 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
4000 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00004001 }
4002}
4003
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00004004void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures,
4005 bool CapturesCXXThis) {
4006 this->CapturesCXXThis = CapturesCXXThis;
4007 this->NumCaptures = Captures.size();
John McCallc63de662011-02-02 13:00:07 +00004008
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00004009 if (Captures.empty()) {
4010 this->Captures = nullptr;
John McCallc63de662011-02-02 13:00:07 +00004011 return;
4012 }
4013
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00004014 this->Captures = Captures.copy(Context).data();
Steve Naroffc4b30e52009-03-13 16:56:44 +00004015}
Sebastian Redl833ef452010-01-26 22:01:41 +00004016
John McCallce45f882011-06-15 22:51:16 +00004017bool BlockDecl::capturesVariable(const VarDecl *variable) const {
Aaron Ballman9371dd22014-03-14 18:34:04 +00004018 for (const auto &I : captures())
John McCallce45f882011-06-15 22:51:16 +00004019 // Only auto vars can be captured, so no redeclaration worries.
Aaron Ballman9371dd22014-03-14 18:34:04 +00004020 if (I.getVariable() == variable)
John McCallce45f882011-06-15 22:51:16 +00004021 return true;
4022
4023 return false;
4024}
4025
Douglas Gregor70226da2010-12-21 16:27:07 +00004026SourceRange BlockDecl::getSourceRange() const {
4027 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
4028}
Sebastian Redl833ef452010-01-26 22:01:41 +00004029
4030//===----------------------------------------------------------------------===//
4031// Other Decl Allocation/Deallocation Method Implementations
4032//===----------------------------------------------------------------------===//
4033
David Blaikie68e081d2011-12-20 02:48:34 +00004034void TranslationUnitDecl::anchor() { }
4035
Sebastian Redl833ef452010-01-26 22:01:41 +00004036TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Craig Topper36250ad2014-05-12 05:36:57 +00004037 return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C);
Sebastian Redl833ef452010-01-26 22:01:41 +00004038}
4039
Nico Weber66220292016-03-02 17:28:48 +00004040void PragmaCommentDecl::anchor() { }
4041
4042PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C,
4043 TranslationUnitDecl *DC,
4044 SourceLocation CommentLoc,
4045 PragmaMSCommentKind CommentKind,
4046 StringRef Arg) {
4047 PragmaCommentDecl *PCD =
4048 new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1))
4049 PragmaCommentDecl(DC, CommentLoc, CommentKind);
4050 memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size());
4051 PCD->getTrailingObjects<char>()[Arg.size()] = '\0';
4052 return PCD;
4053}
4054
4055PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C,
4056 unsigned ID,
4057 unsigned ArgSize) {
4058 return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1))
4059 PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown);
4060}
4061
Nico Webercbbaeb12016-03-02 19:28:54 +00004062void PragmaDetectMismatchDecl::anchor() { }
4063
4064PragmaDetectMismatchDecl *
4065PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC,
4066 SourceLocation Loc, StringRef Name,
4067 StringRef Value) {
4068 size_t ValueStart = Name.size() + 1;
4069 PragmaDetectMismatchDecl *PDMD =
4070 new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1))
4071 PragmaDetectMismatchDecl(DC, Loc, ValueStart);
4072 memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size());
4073 PDMD->getTrailingObjects<char>()[Name.size()] = '\0';
4074 memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(),
4075 Value.size());
4076 PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0';
4077 return PDMD;
4078}
4079
4080PragmaDetectMismatchDecl *
4081PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID,
4082 unsigned NameValueSize) {
4083 return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1))
4084 PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0);
4085}
Nico Weber66220292016-03-02 17:28:48 +00004086
Richard Smithf19e1272015-03-07 00:04:49 +00004087void ExternCContextDecl::anchor() { }
4088
4089ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C,
4090 TranslationUnitDecl *DC) {
4091 return new (C, DC) ExternCContextDecl(DC);
4092}
4093
David Blaikie68e081d2011-12-20 02:48:34 +00004094void LabelDecl::anchor() { }
4095
Chris Lattnerc8e630e2011-02-17 07:39:24 +00004096LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00004097 SourceLocation IdentL, IdentifierInfo *II) {
Craig Topper36250ad2014-05-12 05:36:57 +00004098 return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL);
Abramo Bagnara1c3af962011-03-05 18:21:20 +00004099}
4100
4101LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
4102 SourceLocation IdentL, IdentifierInfo *II,
4103 SourceLocation GnuLabelL) {
4104 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
Craig Topper36250ad2014-05-12 05:36:57 +00004105 return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00004106}
4107
Douglas Gregor72172e92012-01-05 21:55:30 +00004108LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004109 return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr,
4110 SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00004111}
4112
Ehsan Akhgari31097582014-09-22 02:21:54 +00004113void LabelDecl::setMSAsmLabel(StringRef Name) {
4114 char *Buffer = new (getASTContext(), 1) char[Name.size() + 1];
4115 memcpy(Buffer, Name.data(), Name.size());
4116 Buffer[Name.size()] = '\0';
4117 MSAsmName = Buffer;
4118}
4119
David Blaikie68e081d2011-12-20 02:48:34 +00004120void ValueDecl::anchor() { }
4121
Benjamin Kramerea70eb32012-12-01 15:09:41 +00004122bool ValueDecl::isWeak() const {
Aaron Ballmanb97112e2014-03-08 22:19:01 +00004123 for (const auto *I : attrs())
4124 if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I))
Benjamin Kramerea70eb32012-12-01 15:09:41 +00004125 return true;
4126
4127 return isWeakImported();
4128}
4129
David Blaikie68e081d2011-12-20 02:48:34 +00004130void ImplicitParamDecl::anchor() { }
4131
Sebastian Redl833ef452010-01-26 22:01:41 +00004132ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00004133 SourceLocation IdLoc,
Alexey Bataev56223232017-06-09 13:40:18 +00004134 IdentifierInfo *Id, QualType Type,
4135 ImplicitParamKind ParamKind) {
4136 return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind);
4137}
4138
4139ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type,
4140 ImplicitParamKind ParamKind) {
4141 return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind);
Sebastian Redl833ef452010-01-26 22:01:41 +00004142}
4143
Richard Smithf7981722013-11-22 09:01:48 +00004144ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00004145 unsigned ID) {
Alexey Bataev56223232017-06-09 13:40:18 +00004146 return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other);
Douglas Gregor72172e92012-01-05 21:55:30 +00004147}
4148
Sebastian Redl833ef452010-01-26 22:01:41 +00004149FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00004150 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00004151 const DeclarationNameInfo &NameInfo,
4152 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00004153 StorageClass SC,
Richard Smithf7981722013-11-22 09:01:48 +00004154 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00004155 bool hasWrittenPrototype,
4156 bool isConstexprSpecified) {
Richard Smithf7981722013-11-22 09:01:48 +00004157 FunctionDecl *New =
Richard Smith053f6c62014-05-16 23:01:30 +00004158 new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo,
4159 SC, isInlineSpecified, isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00004160 New->HasWrittenPrototype = hasWrittenPrototype;
4161 return New;
4162}
4163
Douglas Gregor72172e92012-01-05 21:55:30 +00004164FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00004165 return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00004166 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00004167 SC_None, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00004168}
4169
Sebastian Redl833ef452010-01-26 22:01:41 +00004170BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Richard Smithf7981722013-11-22 09:01:48 +00004171 return new (C, DC) BlockDecl(DC, L);
Sebastian Redl833ef452010-01-26 22:01:41 +00004172}
4173
Douglas Gregor72172e92012-01-05 21:55:30 +00004174BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004175 return new (C, ID) BlockDecl(nullptr, SourceLocation());
John McCall5e77d762013-04-16 07:28:30 +00004176}
4177
Chandler Carruth21c90602015-12-30 03:24:14 +00004178CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams)
4179 : Decl(Captured, DC, SourceLocation()), DeclContext(Captured),
4180 NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {}
4181
Ben Langmuir37943a72013-05-03 19:00:33 +00004182CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
4183 unsigned NumParams) {
James Y Knight7a22b242015-08-06 20:26:32 +00004184 return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
Richard Smithf7981722013-11-22 09:01:48 +00004185 CapturedDecl(DC, NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00004186}
4187
Ben Langmuirce914fc2013-05-03 19:20:19 +00004188CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
Richard Smithf7981722013-11-22 09:01:48 +00004189 unsigned NumParams) {
James Y Knight7a22b242015-08-06 20:26:32 +00004190 return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
Craig Topper36250ad2014-05-12 05:36:57 +00004191 CapturedDecl(nullptr, NumParams);
Ben Langmuirce914fc2013-05-03 19:20:19 +00004192}
4193
Chandler Carruth21c90602015-12-30 03:24:14 +00004194Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); }
4195void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); }
4196
4197bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); }
4198void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); }
4199
Sebastian Redl833ef452010-01-26 22:01:41 +00004200EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
4201 SourceLocation L,
4202 IdentifierInfo *Id, QualType T,
4203 Expr *E, const llvm::APSInt &V) {
Richard Smithf7981722013-11-22 09:01:48 +00004204 return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V);
Sebastian Redl833ef452010-01-26 22:01:41 +00004205}
4206
Douglas Gregor72172e92012-01-05 21:55:30 +00004207EnumConstantDecl *
4208EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004209 return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr,
4210 QualType(), nullptr, llvm::APSInt());
Douglas Gregor72172e92012-01-05 21:55:30 +00004211}
4212
David Blaikie68e081d2011-12-20 02:48:34 +00004213void IndirectFieldDecl::anchor() { }
4214
Richard Smith9f951822016-01-06 22:49:11 +00004215IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC,
4216 SourceLocation L, DeclarationName N,
David Majnemer59f77922016-06-24 04:05:48 +00004217 QualType T,
4218 MutableArrayRef<NamedDecl *> CH)
4219 : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()),
4220 ChainingSize(CH.size()) {
Richard Smith9f951822016-01-06 22:49:11 +00004221 // In C++, indirect field declarations conflict with tag declarations in the
4222 // same scope, so add them to IDNS_Tag so that tag redeclaration finds them.
4223 if (C.getLangOpts().CPlusPlus)
4224 IdentifierNamespace |= IDNS_Tag;
4225}
4226
Benjamin Kramer39593702010-11-21 14:11:41 +00004227IndirectFieldDecl *
4228IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
David Majnemer59f77922016-06-24 04:05:48 +00004229 IdentifierInfo *Id, QualType T,
4230 llvm::MutableArrayRef<NamedDecl *> CH) {
4231 return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH);
Francois Pichet783dd6e2010-11-21 06:08:52 +00004232}
4233
Douglas Gregor72172e92012-01-05 21:55:30 +00004234IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
4235 unsigned ID) {
Richard Smith9f951822016-01-06 22:49:11 +00004236 return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(),
David Majnemer59f77922016-06-24 04:05:48 +00004237 DeclarationName(), QualType(), None);
Douglas Gregor72172e92012-01-05 21:55:30 +00004238}
4239
Douglas Gregorbe996932010-09-01 20:41:53 +00004240SourceRange EnumConstantDecl::getSourceRange() const {
4241 SourceLocation End = getLocation();
4242 if (Init)
4243 End = Init->getLocEnd();
4244 return SourceRange(getLocation(), End);
4245}
4246
David Blaikie68e081d2011-12-20 02:48:34 +00004247void TypeDecl::anchor() { }
4248
Sebastian Redl833ef452010-01-26 22:01:41 +00004249TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004250 SourceLocation StartLoc, SourceLocation IdLoc,
4251 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
Richard Smith053f6c62014-05-16 23:01:30 +00004252 return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00004253}
4254
David Blaikie68e081d2011-12-20 02:48:34 +00004255void TypedefNameDecl::anchor() { }
4256
Richard Smith42413142015-05-15 20:05:43 +00004257TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const {
4258 if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) {
4259 auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl();
4260 auto *ThisTypedef = this;
4261 if (AnyRedecl && OwningTypedef) {
4262 OwningTypedef = OwningTypedef->getCanonicalDecl();
4263 ThisTypedef = ThisTypedef->getCanonicalDecl();
4264 }
4265 if (OwningTypedef == ThisTypedef)
Richard Smitha5230222015-03-27 01:37:43 +00004266 return TT->getDecl();
Richard Smith42413142015-05-15 20:05:43 +00004267 }
Richard Smitha5230222015-03-27 01:37:43 +00004268
4269 return nullptr;
4270}
4271
Argyrios Kyrtzidis3b25c912017-03-21 16:56:02 +00004272bool TypedefNameDecl::isTransparentTagSlow() const {
4273 auto determineIsTransparent = [&]() {
4274 if (auto *TT = getUnderlyingType()->getAs<TagType>()) {
4275 if (auto *TD = TT->getDecl()) {
4276 if (TD->getName() != getName())
4277 return false;
4278 SourceLocation TTLoc = getLocation();
4279 SourceLocation TDLoc = TD->getLocation();
4280 if (!TTLoc.isMacroID() || !TDLoc.isMacroID())
4281 return false;
4282 SourceManager &SM = getASTContext().getSourceManager();
4283 return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc);
4284 }
4285 }
4286 return false;
4287 };
4288
4289 bool isTransparent = determineIsTransparent();
4290 CacheIsTransparentTag = 1;
4291 if (isTransparent)
4292 CacheIsTransparentTag |= 0x2;
4293 return isTransparent;
4294}
4295
Douglas Gregor72172e92012-01-05 21:55:30 +00004296TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00004297 return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00004298 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00004299}
4300
Richard Smithdda56e42011-04-15 14:24:37 +00004301TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
4302 SourceLocation StartLoc,
4303 SourceLocation IdLoc, IdentifierInfo *Id,
4304 TypeSourceInfo *TInfo) {
Richard Smith053f6c62014-05-16 23:01:30 +00004305 return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo);
Richard Smithdda56e42011-04-15 14:24:37 +00004306}
4307
Douglas Gregor72172e92012-01-05 21:55:30 +00004308TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00004309 return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(),
4310 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00004311}
4312
Abramo Bagnaraea947882011-03-08 16:41:52 +00004313SourceRange TypedefDecl::getSourceRange() const {
4314 SourceLocation RangeEnd = getLocation();
4315 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
4316 if (typeIsPostfix(TInfo->getType()))
4317 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
4318 }
4319 return SourceRange(getLocStart(), RangeEnd);
4320}
4321
Richard Smithdda56e42011-04-15 14:24:37 +00004322SourceRange TypeAliasDecl::getSourceRange() const {
4323 SourceLocation RangeEnd = getLocStart();
4324 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
4325 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
4326 return SourceRange(getLocStart(), RangeEnd);
4327}
4328
David Blaikie68e081d2011-12-20 02:48:34 +00004329void FileScopeAsmDecl::anchor() { }
4330
Sebastian Redl833ef452010-01-26 22:01:41 +00004331FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00004332 StringLiteral *Str,
4333 SourceLocation AsmLoc,
4334 SourceLocation RParenLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00004335 return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00004336}
Douglas Gregorba345522011-12-02 23:23:56 +00004337
Richard Smithf7981722013-11-22 09:01:48 +00004338FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00004339 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004340 return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(),
4341 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00004342}
4343
Michael Han84324352013-02-22 17:15:32 +00004344void EmptyDecl::anchor() {}
4345
4346EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Richard Smithf7981722013-11-22 09:01:48 +00004347 return new (C, DC) EmptyDecl(DC, L);
Michael Han84324352013-02-22 17:15:32 +00004348}
4349
4350EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00004351 return new (C, ID) EmptyDecl(nullptr, SourceLocation());
Michael Han84324352013-02-22 17:15:32 +00004352}
4353
Douglas Gregorba345522011-12-02 23:23:56 +00004354//===----------------------------------------------------------------------===//
4355// ImportDecl Implementation
4356//===----------------------------------------------------------------------===//
4357
4358/// \brief Retrieve the number of module identifiers needed to name the given
4359/// module.
4360static unsigned getNumModuleIdentifiers(Module *Mod) {
4361 unsigned Result = 1;
4362 while (Mod->Parent) {
4363 Mod = Mod->Parent;
4364 ++Result;
4365 }
4366 return Result;
4367}
4368
Douglas Gregor22d09742012-01-03 18:04:46 +00004369ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00004370 Module *Imported,
4371 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00004372 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00004373 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00004374{
4375 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00004376 auto *StoredLocs = getTrailingObjects<SourceLocation>();
James Y Knight7a22b242015-08-06 20:26:32 +00004377 std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(),
4378 StoredLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00004379}
4380
Douglas Gregor22d09742012-01-03 18:04:46 +00004381ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00004382 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00004383 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00004384 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00004385{
James Y Knight7a22b242015-08-06 20:26:32 +00004386 *getTrailingObjects<SourceLocation>() = EndLoc;
Douglas Gregorba345522011-12-02 23:23:56 +00004387}
4388
Richard Smithf7981722013-11-22 09:01:48 +00004389ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00004390 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004391 ArrayRef<SourceLocation> IdentifierLocs) {
James Y Knight7a22b242015-08-06 20:26:32 +00004392 return new (C, DC,
4393 additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size()))
Richard Smithf7981722013-11-22 09:01:48 +00004394 ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00004395}
4396
Richard Smithf7981722013-11-22 09:01:48 +00004397ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00004398 SourceLocation StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +00004399 Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004400 SourceLocation EndLoc) {
James Y Knight7a22b242015-08-06 20:26:32 +00004401 ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1))
4402 ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00004403 Import->setImplicit();
4404 return Import;
4405}
4406
Douglas Gregor72172e92012-01-05 21:55:30 +00004407ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
4408 unsigned NumLocations) {
James Y Knight7a22b242015-08-06 20:26:32 +00004409 return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations))
Richard Smithf7981722013-11-22 09:01:48 +00004410 ImportDecl(EmptyShell());
Douglas Gregorba345522011-12-02 23:23:56 +00004411}
4412
4413ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
4414 if (!ImportedAndComplete.getInt())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00004415 return None;
Douglas Gregorba345522011-12-02 23:23:56 +00004416
Alexander Kornienkofd6ce042015-11-09 17:53:06 +00004417 const auto *StoredLocs = getTrailingObjects<SourceLocation>();
Craig Topper5fc8fc22014-08-27 06:28:36 +00004418 return llvm::makeArrayRef(StoredLocs,
4419 getNumModuleIdentifiers(getImportedModule()));
Douglas Gregorba345522011-12-02 23:23:56 +00004420}
4421
4422SourceRange ImportDecl::getSourceRange() const {
4423 if (!ImportedAndComplete.getInt())
James Y Knight7a22b242015-08-06 20:26:32 +00004424 return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>());
4425
Douglas Gregorba345522011-12-02 23:23:56 +00004426 return SourceRange(getLocation(), getIdentifierLocs().back());
4427}
Richard Smith8df390f2016-09-08 23:14:54 +00004428
4429//===----------------------------------------------------------------------===//
4430// ExportDecl Implementation
4431//===----------------------------------------------------------------------===//
4432
4433void ExportDecl::anchor() {}
4434
4435ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC,
4436 SourceLocation ExportLoc) {
4437 return new (C, DC) ExportDecl(DC, ExportLoc);
4438}
4439
4440ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4441 return new (C, ID) ExportDecl(nullptr, SourceLocation());
4442}