blob: 8a523d6a0deacb61c2ddf82191d95600c4cf6a8a [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000024#include "clang/AST/Stmt.h"
25#include "clang/AST/TypeLoc.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000027#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000028#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000029#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000030#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000031#include "llvm/Support/ErrorHandling.h"
John McCall5f46c482013-02-21 23:42:58 +000032#include "llvm/Support/type_traits.h"
David Blaikie9c70e042011-09-21 18:16:56 +000033#include <algorithm>
34
Chris Lattner6d9a6852006-10-25 05:11:20 +000035using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000036
Chris Lattner88f70d62008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
John McCalldf25c432013-02-16 00:17:33 +000041// Visibility rules aren't rigorously externally specified, but here
42// are the basic principles behind what we implement:
43//
44// 1. An explicit visibility attribute is generally a direct expression
45// of the user's intent and should be honored. Only the innermost
46// visibility attribute applies. If no visibility attribute applies,
47// global visibility settings are considered.
48//
49// 2. There is one caveat to the above: on or in a template pattern,
50// an explicit visibility attribute is just a default rule, and
51// visibility can be decreased by the visibility of template
52// arguments. But this, too, has an exception: an attribute on an
53// explicit specialization or instantiation causes all the visibility
54// restrictions of the template arguments to be ignored.
55//
56// 3. A variable that does not otherwise have explicit visibility can
57// be restricted by the visibility of its type.
58//
59// 4. A visibility restriction is explicit if it comes from an
60// attribute (or something like it), not a global visibility setting.
61// When emitting a reference to an external symbol, visibility
62// restrictions are ignored unless they are explicit.
John McCalld041a9b2013-02-20 01:54:26 +000063//
64// 5. When computing the visibility of a non-type, including a
65// non-type member of a class, only non-type visibility restrictions
66// are considered: the 'visibility' attribute, global value-visibility
67// settings, and a few special cases like __private_extern.
68//
69// 6. When computing the visibility of a type, including a type member
70// of a class, only type visibility restrictions are considered:
71// the 'type_visibility' attribute and global type-visibility settings.
72// However, a 'visibility' attribute counts as a 'type_visibility'
73// attribute on any declaration that only has the former.
74//
75// The visibility of a "secondary" entity, like a template argument,
76// is computed using the kind of that entity, not the kind of the
77// primary entity for which we are computing visibility. For example,
78// the visibility of a specialization of either of these templates:
79// template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X);
80// template <class T, bool (&compare)(T, X)> class matcher;
81// is restricted according to the type visibility of the argument 'T',
82// the type visibility of 'bool(&)(T,X)', and the value visibility of
83// the argument function 'compare'. That 'has_match' is a value
84// and 'matcher' is a type only matters when looking for attributes
85// and settings from the immediate context.
John McCalldf25c432013-02-16 00:17:33 +000086
John McCall5f46c482013-02-21 23:42:58 +000087const unsigned IgnoreExplicitVisibilityBit = 2;
88
John McCalldf25c432013-02-16 00:17:33 +000089/// Kinds of LV computation. The linkage side of the computation is
90/// always the same, but different things can change how visibility is
91/// computed.
92enum LVComputationKind {
John McCall5f46c482013-02-21 23:42:58 +000093 /// Do an LV computation for, ultimately, a type.
94 /// Visibility may be restricted by type visibility settings and
95 /// the visibility of template arguments.
John McCalld041a9b2013-02-20 01:54:26 +000096 LVForType = NamedDecl::VisibilityForType,
John McCalldf25c432013-02-16 00:17:33 +000097
John McCall5f46c482013-02-21 23:42:58 +000098 /// Do an LV computation for, ultimately, a non-type declaration.
99 /// Visibility may be restricted by value visibility settings and
100 /// the visibility of template arguments.
John McCalld041a9b2013-02-20 01:54:26 +0000101 LVForValue = NamedDecl::VisibilityForValue,
102
John McCall5f46c482013-02-21 23:42:58 +0000103 /// Do an LV computation for, ultimately, a type that already has
104 /// some sort of explicit visibility. Visibility may only be
105 /// restricted by the visibility of template arguments.
106 LVForExplicitType = (LVForType | IgnoreExplicitVisibilityBit),
John McCalld041a9b2013-02-20 01:54:26 +0000107
John McCall5f46c482013-02-21 23:42:58 +0000108 /// Do an LV computation for, ultimately, a non-type declaration
109 /// that already has some sort of explicit visibility. Visibility
110 /// may only be restricted by the visibility of template arguments.
111 LVForExplicitValue = (LVForValue | IgnoreExplicitVisibilityBit)
John McCalldf25c432013-02-16 00:17:33 +0000112};
113
John McCalld041a9b2013-02-20 01:54:26 +0000114/// Does this computation kind permit us to consider additional
115/// visibility settings from attributes and the like?
116static bool hasExplicitVisibilityAlready(LVComputationKind computation) {
John McCall5f46c482013-02-21 23:42:58 +0000117 return ((unsigned(computation) & IgnoreExplicitVisibilityBit) != 0);
John McCalld041a9b2013-02-20 01:54:26 +0000118}
119
120/// Given an LVComputationKind, return one of the same type/value sort
121/// that records that it already has explicit visibility.
122static LVComputationKind
123withExplicitVisibilityAlready(LVComputationKind oldKind) {
124 LVComputationKind newKind =
John McCall5f46c482013-02-21 23:42:58 +0000125 static_cast<LVComputationKind>(unsigned(oldKind) |
126 IgnoreExplicitVisibilityBit);
John McCalld041a9b2013-02-20 01:54:26 +0000127 assert(oldKind != LVForType || newKind == LVForExplicitType);
128 assert(oldKind != LVForValue || newKind == LVForExplicitValue);
129 assert(oldKind != LVForExplicitType || newKind == LVForExplicitType);
130 assert(oldKind != LVForExplicitValue || newKind == LVForExplicitValue);
131 return newKind;
132}
133
David Blaikie05785d12013-02-20 22:23:23 +0000134static Optional<Visibility> getExplicitVisibility(const NamedDecl *D,
135 LVComputationKind kind) {
John McCalld041a9b2013-02-20 01:54:26 +0000136 assert(!hasExplicitVisibilityAlready(kind) &&
137 "asking for explicit visibility when we shouldn't be");
138 return D->getExplicitVisibility((NamedDecl::ExplicitVisibilityKind) kind);
139}
140
John McCalldf25c432013-02-16 00:17:33 +0000141/// Is the given declaration a "type" or a "value" for the purposes of
142/// visibility computation?
143static bool usesTypeVisibility(const NamedDecl *D) {
John McCallb4a99d32013-02-19 01:57:35 +0000144 return isa<TypeDecl>(D) ||
145 isa<ClassTemplateDecl>(D) ||
146 isa<ObjCInterfaceDecl>(D);
John McCalldf25c432013-02-16 00:17:33 +0000147}
148
John McCall5f46c482013-02-21 23:42:58 +0000149/// Does the given declaration have member specialization information,
150/// and if so, is it an explicit specialization?
151template <class T> static typename
152llvm::enable_if_c<!llvm::is_base_of<RedeclarableTemplateDecl, T>::value,
153 bool>::type
154isExplicitMemberSpecialization(const T *D) {
155 if (const MemberSpecializationInfo *member =
156 D->getMemberSpecializationInfo()) {
157 return member->isExplicitSpecialization();
158 }
159 return false;
160}
161
162/// For templates, this question is easier: a member template can't be
163/// explicitly instantiated, so there's a single bit indicating whether
164/// or not this is an explicit member specialization.
165static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) {
166 return D->isMemberSpecialization();
167}
168
John McCalld041a9b2013-02-20 01:54:26 +0000169/// Given a visibility attribute, return the explicit visibility
170/// associated with it.
171template <class T>
172static Visibility getVisibilityFromAttr(const T *attr) {
173 switch (attr->getVisibility()) {
174 case T::Default:
175 return DefaultVisibility;
176 case T::Hidden:
177 return HiddenVisibility;
178 case T::Protected:
179 return ProtectedVisibility;
180 }
181 llvm_unreachable("bad visibility kind");
182}
183
John McCalldf25c432013-02-16 00:17:33 +0000184/// Return the explicit visibility of the given declaration.
David Blaikie05785d12013-02-20 22:23:23 +0000185static Optional<Visibility> getVisibilityOf(const NamedDecl *D,
John McCalld041a9b2013-02-20 01:54:26 +0000186 NamedDecl::ExplicitVisibilityKind kind) {
187 // If we're ultimately computing the visibility of a type, look for
188 // a 'type_visibility' attribute before looking for 'visibility'.
189 if (kind == NamedDecl::VisibilityForType) {
190 if (const TypeVisibilityAttr *A = D->getAttr<TypeVisibilityAttr>()) {
191 return getVisibilityFromAttr(A);
192 }
193 }
194
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000195 // If this declaration has an explicit visibility attribute, use it.
196 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
John McCalld041a9b2013-02-20 01:54:26 +0000197 return getVisibilityFromAttr(A);
John McCall457a04e2010-10-22 21:05:15 +0000198 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000199
200 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
201 // implies visibility(default).
Douglas Gregore8bbc122011-09-02 00:18:52 +0000202 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000203 for (specific_attr_iterator<AvailabilityAttr>
204 A = D->specific_attr_begin<AvailabilityAttr>(),
205 AEnd = D->specific_attr_end<AvailabilityAttr>();
206 A != AEnd; ++A)
207 if ((*A)->getPlatform()->getName().equals("macosx"))
208 return DefaultVisibility;
209 }
210
David Blaikie7a30dc52013-02-21 01:47:18 +0000211 return None;
John McCall457a04e2010-10-22 21:05:15 +0000212}
213
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000214/// \brief Get the most restrictive linkage for the types in the given
John McCalldf25c432013-02-16 00:17:33 +0000215/// template parameter list. For visibility purposes, template
216/// parameters are part of the signature of a template.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000217static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000218getLVForTemplateParameterList(const TemplateParameterList *params) {
219 LinkageInfo LV;
220 for (TemplateParameterList::const_iterator P = params->begin(),
221 PEnd = params->end();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000222 P != PEnd; ++P) {
John McCalldf25c432013-02-16 00:17:33 +0000223
224 // Template type parameters are the most common and never
225 // contribute to visibility, pack or not.
226 if (isa<TemplateTypeParmDecl>(*P))
227 continue;
228
229 // Non-type template parameters can be restricted by the value type, e.g.
230 // template <enum X> class A { ... };
231 // We have to be careful here, though, because we can be dealing with
232 // dependent types.
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000233 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
John McCalldf25c432013-02-16 00:17:33 +0000234 // Handle the non-pack case first.
235 if (!NTTP->isExpandedParameterPack()) {
236 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000237 LV.merge(NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000238 }
239 continue;
240 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000241
John McCalldf25c432013-02-16 00:17:33 +0000242 // Look at all the types in an expanded pack.
243 for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) {
244 QualType type = NTTP->getExpansionType(i);
245 if (!type->isDependentType())
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000246 LV.merge(type->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000247 }
John McCalldf25c432013-02-16 00:17:33 +0000248 continue;
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000249 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000250
John McCalldf25c432013-02-16 00:17:33 +0000251 // Template template parameters can be restricted by their
252 // template parameters, recursively.
253 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
254
255 // Handle the non-pack case first.
256 if (!TTP->isExpandedParameterPack()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000257 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
John McCalldf25c432013-02-16 00:17:33 +0000258 continue;
259 }
260
261 // Look at all expansions in an expanded pack.
262 for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters();
263 i != n; ++i) {
264 LV.merge(getLVForTemplateParameterList(
265 TTP->getExpansionTemplateParameters(i)));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000266 }
267 }
268
John McCall457a04e2010-10-22 21:05:15 +0000269 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000270}
271
Rafael Espindola19de5612013-01-12 06:42:30 +0000272/// getLVForDecl - Get the linkage and visibility for the given declaration.
John McCalldf25c432013-02-16 00:17:33 +0000273static LinkageInfo getLVForDecl(const NamedDecl *D,
274 LVComputationKind computation);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000275
Rafael Espindolac1b38a22013-05-16 04:30:21 +0000276static const FunctionDecl *getOutermostFunctionContext(const Decl *D) {
277 const FunctionDecl *Ret = NULL;
278 const DeclContext *DC = D->getDeclContext();
279 while (DC->getDeclKind() != Decl::TranslationUnit) {
280 const FunctionDecl *F = dyn_cast<FunctionDecl>(DC);
281 if (F)
282 Ret = F;
283 DC = DC->getParent();
284 }
285 return Ret;
286}
287
288/// Get the linkage and visibility to be used when this type is a template
289/// argument. This is normally just the linkage and visibility of the type,
290/// but for function local types we need to check the linkage and visibility
291/// of the function.
292static LinkageInfo getLIForTemplateTypeArgument(QualType T) {
293 LinkageInfo LI = T->getLinkageAndVisibility();
294 if (LI.getLinkage() != NoLinkage)
295 return LI;
296
297 const TagType *TT = dyn_cast<TagType>(T);
298 if (!TT)
299 return LI;
300
Rafael Espindolab6ed1532013-05-18 00:33:28 +0000301 const Decl *D = TT->getDecl();
302 const FunctionDecl *FD = getOutermostFunctionContext(D);
Rafael Espindolac1b38a22013-05-16 04:30:21 +0000303 if (!FD)
304 return LI;
305
306 if (!FD->isInlined())
307 return LI;
308
309 return FD->getLinkageAndVisibility();
310}
311
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000312/// \brief Get the most restrictive linkage for the types and
313/// declarations in the given template argument list.
John McCalldf25c432013-02-16 00:17:33 +0000314///
315/// Note that we don't take an LVComputationKind because we always
316/// want to honor the visibility of template arguments in the same way.
317static LinkageInfo
318getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args) {
319 LinkageInfo LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000320
John McCalldf25c432013-02-16 00:17:33 +0000321 for (unsigned i = 0, e = args.size(); i != e; ++i) {
322 const TemplateArgument &arg = args[i];
323 switch (arg.getKind()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000324 case TemplateArgument::Null:
325 case TemplateArgument::Integral:
326 case TemplateArgument::Expression:
John McCalldf25c432013-02-16 00:17:33 +0000327 continue;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000328
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000329 case TemplateArgument::Type:
Rafael Espindolac1b38a22013-05-16 04:30:21 +0000330 LV.merge(getLIForTemplateTypeArgument(arg.getAsType()));
John McCalldf25c432013-02-16 00:17:33 +0000331 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000332
333 case TemplateArgument::Declaration:
John McCalldf25c432013-02-16 00:17:33 +0000334 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
335 assert(!usesTypeVisibility(ND));
336 LV.merge(getLVForDecl(ND, LVForValue));
337 }
338 continue;
Eli Friedmanb826a002012-09-26 02:36:12 +0000339
340 case TemplateArgument::NullPtr:
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000341 LV.merge(arg.getNullPtrType()->getLinkageAndVisibility());
John McCalldf25c432013-02-16 00:17:33 +0000342 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000343
344 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000345 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000346 if (TemplateDecl *Template
John McCalldf25c432013-02-16 00:17:33 +0000347 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
348 LV.merge(getLVForDecl(Template, LVForValue));
349 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000350
351 case TemplateArgument::Pack:
John McCalldf25c432013-02-16 00:17:33 +0000352 LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray()));
353 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000354 }
John McCalldf25c432013-02-16 00:17:33 +0000355 llvm_unreachable("bad template argument kind");
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000356 }
357
John McCall457a04e2010-10-22 21:05:15 +0000358 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000359}
360
Rafael Espindola2f869a32012-01-14 00:30:36 +0000361static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000362getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
363 return getLVForTemplateArgumentList(TArgs.asArray());
John McCall8823c652010-08-13 08:35:10 +0000364}
365
John McCall5f46c482013-02-21 23:42:58 +0000366static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
367 const FunctionTemplateSpecializationInfo *specInfo) {
368 // Include visibility from the template parameters and arguments
369 // only if this is not an explicit instantiation or specialization
370 // with direct explicit visibility. (Implicit instantiations won't
371 // have a direct attribute.)
372 if (!specInfo->isExplicitInstantiationOrSpecialization())
373 return true;
374
375 return !fn->hasAttr<VisibilityAttr>();
376}
377
John McCalldf25c432013-02-16 00:17:33 +0000378/// Merge in template-related linkage and visibility for the given
379/// function template specialization.
380///
381/// We don't need a computation kind here because we can assume
382/// LVForValue.
John McCall5f46c482013-02-21 23:42:58 +0000383///
NAKAMURA Takumi62eae082013-02-22 04:06:28 +0000384/// \param[out] LV the computation to use for the parent
John McCall5f46c482013-02-21 23:42:58 +0000385static void
386mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
387 const FunctionTemplateSpecializationInfo *specInfo) {
388 bool considerVisibility =
389 shouldConsiderTemplateVisibility(fn, specInfo);
John McCalldf25c432013-02-16 00:17:33 +0000390
391 // Merge information from the template parameters.
John McCall5f46c482013-02-21 23:42:58 +0000392 FunctionTemplateDecl *temp = specInfo->getTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000393 LinkageInfo tempLV =
394 getLVForTemplateParameterList(temp->getTemplateParameters());
395 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
396
397 // Merge information from the template arguments.
398 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
399 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
400 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000401}
402
John McCall5f46c482013-02-21 23:42:58 +0000403/// Does the given declaration have a direct visibility attribute
404/// that would match the given rules?
405static bool hasDirectVisibilityAttribute(const NamedDecl *D,
406 LVComputationKind computation) {
407 switch (computation) {
408 case LVForType:
409 case LVForExplicitType:
410 if (D->hasAttr<TypeVisibilityAttr>())
411 return true;
412 // fallthrough
413 case LVForValue:
414 case LVForExplicitValue:
415 if (D->hasAttr<VisibilityAttr>())
416 return true;
417 return false;
418 }
419 llvm_unreachable("bad visibility computation kind");
420}
421
John McCalld041a9b2013-02-20 01:54:26 +0000422/// Should we consider visibility associated with the template
423/// arguments and parameters of the given class template specialization?
424static bool shouldConsiderTemplateVisibility(
425 const ClassTemplateSpecializationDecl *spec,
426 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000427 // Include visibility from the template parameters and arguments
428 // only if this is not an explicit instantiation or specialization
429 // with direct explicit visibility (and note that implicit
430 // instantiations won't have a direct attribute).
431 //
432 // Furthermore, we want to ignore template parameters and arguments
John McCalld041a9b2013-02-20 01:54:26 +0000433 // for an explicit specialization when computing the visibility of a
434 // member thereof with explicit visibility.
John McCalldf25c432013-02-16 00:17:33 +0000435 //
436 // This is a bit complex; let's unpack it.
437 //
438 // An explicit class specialization is an independent, top-level
439 // declaration. As such, if it or any of its members has an
440 // explicit visibility attribute, that must directly express the
441 // user's intent, and we should honor it. The same logic applies to
442 // an explicit instantiation of a member of such a thing.
John McCalld041a9b2013-02-20 01:54:26 +0000443
444 // Fast path: if this is not an explicit instantiation or
445 // specialization, we always want to consider template-related
446 // visibility restrictions.
447 if (!spec->isExplicitInstantiationOrSpecialization())
448 return true;
449
450 // This is the 'member thereof' check.
451 if (spec->isExplicitSpecialization() &&
452 hasExplicitVisibilityAlready(computation))
453 return false;
454
John McCall5f46c482013-02-21 23:42:58 +0000455 return !hasDirectVisibilityAttribute(spec, computation);
John McCalld041a9b2013-02-20 01:54:26 +0000456}
457
458/// Merge in template-related linkage and visibility for the given
459/// class template specialization.
460static void mergeTemplateLV(LinkageInfo &LV,
461 const ClassTemplateSpecializationDecl *spec,
462 LVComputationKind computation) {
463 bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
John McCalldf25c432013-02-16 00:17:33 +0000464
465 // Merge information from the template parameters, but ignore
466 // visibility if we're only considering template arguments.
467
John McCalld041a9b2013-02-20 01:54:26 +0000468 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000469 LinkageInfo tempLV =
470 getLVForTemplateParameterList(temp->getTemplateParameters());
471 LV.mergeMaybeWithVisibility(tempLV,
John McCalld041a9b2013-02-20 01:54:26 +0000472 considerVisibility && !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000473
474 // Merge information from the template arguments. We ignore
475 // template-argument visibility if we've got an explicit
476 // instantiation with a visibility attribute.
477 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
478 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
479 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000480}
481
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000482static bool useInlineVisibilityHidden(const NamedDecl *D) {
483 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000484 const LangOptions &Opts = D->getASTContext().getLangOpts();
485 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000486 return false;
487
488 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
489 if (!FD)
490 return false;
491
492 TemplateSpecializationKind TSK = TSK_Undeclared;
493 if (FunctionTemplateSpecializationInfo *spec
494 = FD->getTemplateSpecializationInfo()) {
495 TSK = spec->getTemplateSpecializationKind();
496 } else if (MemberSpecializationInfo *MSI =
497 FD->getMemberSpecializationInfo()) {
498 TSK = MSI->getTemplateSpecializationKind();
499 }
500
501 const FunctionDecl *Def = 0;
502 // InlineVisibilityHidden only applies to definitions, and
503 // isInlined() only gives meaningful answers on definitions
504 // anyway.
505 return TSK != TSK_ExplicitInstantiationDeclaration &&
506 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000507 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000508}
509
Rafael Espindola593537a2013-05-05 20:15:21 +0000510template <typename T> static bool isFirstInExternCContext(T *D) {
Rafael Espindolaf4187652013-02-14 01:18:37 +0000511 const T *First = D->getFirstDeclaration();
Rafael Espindola593537a2013-05-05 20:15:21 +0000512 return First->isInExternCContext();
Rafael Espindolaf4187652013-02-14 01:18:37 +0000513}
514
Rafael Espindola327be3c2013-04-26 01:30:23 +0000515static bool isSingleLineExternC(const Decl &D) {
516 if (const LinkageSpecDecl *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
517 if (SD->getLanguage() == LinkageSpecDecl::lang_c && !SD->hasBraces())
518 return true;
519 return false;
520}
521
Rafael Espindola3ae00052013-05-13 00:12:11 +0000522static bool isExternalLinkage(Linkage L) {
523 return L == UniqueExternalLinkage || L == ExternalLinkage;
524}
525
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000526static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCalldf25c432013-02-16 00:17:33 +0000527 LVComputationKind computation) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000528 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000529 "Not a name having namespace scope");
530 ASTContext &Context = D->getASTContext();
531
532 // C++ [basic.link]p3:
533 // A name having namespace scope (3.3.6) has internal linkage if it
534 // is the name of
535 // - an object, reference, function or function template that is
536 // explicitly declared static; or,
537 // (This bullet corresponds to C99 6.2.2p3.)
538 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
539 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000540 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000541 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000542
Richard Smithdc0ef452012-10-19 06:37:48 +0000543 // - a non-volatile object or reference that is explicitly declared const
544 // or constexpr and neither explicitly declared extern nor previously
545 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000546 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000547 Var->getType().isConstQualified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000548 !Var->getType().isVolatileQualified()) {
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000549 const VarDecl *PrevVar = Var->getPreviousDecl();
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000550 if (PrevVar)
Rafael Espindolaadea16b2013-04-03 15:50:00 +0000551 return PrevVar->getLinkageAndVisibility();
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000552
553 if (Var->getStorageClass() != SC_Extern &&
Rafael Espindola327be3c2013-04-26 01:30:23 +0000554 Var->getStorageClass() != SC_PrivateExtern &&
555 !isSingleLineExternC(*Var))
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000556 return LinkageInfo::internal();
557 }
558
559 for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar;
560 PrevVar = PrevVar->getPreviousDecl()) {
561 if (PrevVar->getStorageClass() == SC_PrivateExtern &&
562 Var->getStorageClass() == SC_None)
563 return PrevVar->getLinkageAndVisibility();
564 // Explicitly declared static.
565 if (PrevVar->getStorageClass() == SC_Static)
566 return LinkageInfo::internal();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000567 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000568 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000569 // C++ [temp]p4:
570 // A non-member function template can have internal linkage; any
571 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000572 const FunctionDecl *Function = 0;
573 if (const FunctionTemplateDecl *FunTmpl
574 = dyn_cast<FunctionTemplateDecl>(D))
575 Function = FunTmpl->getTemplatedDecl();
576 else
577 Function = cast<FunctionDecl>(D);
578
579 // Explicitly declared static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000580 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000581 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000582 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
583 // - a data member of an anonymous union.
584 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000585 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000586 }
587
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000588 if (D->isInAnonymousNamespace()) {
589 const VarDecl *Var = dyn_cast<VarDecl>(D);
590 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindola593537a2013-05-05 20:15:21 +0000591 if ((!Var || !isFirstInExternCContext(Var)) &&
592 (!Func || !isFirstInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000593 return LinkageInfo::uniqueExternal();
594 }
John McCallb7139c42010-10-28 04:18:25 +0000595
John McCall457a04e2010-10-22 21:05:15 +0000596 // Set up the defaults.
597
598 // C99 6.2.2p5:
599 // If the declaration of an identifier for an object has file
600 // scope and no storage-class specifier, its linkage is
601 // external.
John McCallc273f242010-10-30 11:50:40 +0000602 LinkageInfo LV;
603
John McCalld041a9b2013-02-20 01:54:26 +0000604 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000605 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000606 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000607 } else {
608 // If we're declared in a namespace with a visibility attribute,
John McCalldf25c432013-02-16 00:17:33 +0000609 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindola78158af2012-04-16 18:46:26 +0000610 for (const DeclContext *DC = D->getDeclContext();
611 !isa<TranslationUnitDecl>(DC);
612 DC = DC->getParent()) {
613 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
614 if (!ND) continue;
David Blaikie05785d12013-02-20 22:23:23 +0000615 if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000616 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000617 break;
618 }
619 }
620 }
Rafael Espindola78158af2012-04-16 18:46:26 +0000621
John McCalldf25c432013-02-16 00:17:33 +0000622 // Add in global settings if the above didn't give us direct visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000623 if (!LV.isVisibilityExplicit()) {
John McCallb4a99d32013-02-19 01:57:35 +0000624 // Use global type/value visibility as appropriate.
625 Visibility globalVisibility;
626 if (computation == LVForValue) {
627 globalVisibility = Context.getLangOpts().getValueVisibilityMode();
628 } else {
629 assert(computation == LVForType);
630 globalVisibility = Context.getLangOpts().getTypeVisibilityMode();
631 }
632 LV.mergeVisibility(globalVisibility, /*explicit*/ false);
John McCalldf25c432013-02-16 00:17:33 +0000633
634 // If we're paying attention to global visibility, apply
635 // -finline-visibility-hidden if this is an inline method.
636 if (useInlineVisibilityHidden(D))
637 LV.mergeVisibility(HiddenVisibility, true);
638 }
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000639 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000640
Douglas Gregorf73b2822009-11-25 22:24:25 +0000641 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000642
Douglas Gregorf73b2822009-11-25 22:24:25 +0000643 // A name having namespace scope has external linkage if it is the
644 // name of
645 //
646 // - an object or reference, unless it has internal linkage; or
647 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000648 // GCC applies the following optimization to variables and static
649 // data members, but not to functions:
650 //
John McCall457a04e2010-10-22 21:05:15 +0000651 // Modify the variable's LV by the LV of its type unless this is
652 // C or extern "C". This follows from [basic.link]p9:
653 // A type without linkage shall not be used as the type of a
654 // variable or function with external linkage unless
655 // - the entity has C language linkage, or
656 // - the entity is declared within an unnamed namespace, or
657 // - the entity is not used or is defined in the same
658 // translation unit.
659 // and [basic.link]p10:
660 // ...the types specified by all declarations referring to a
661 // given variable or function shall be identical...
662 // C does not have an equivalent rule.
663 //
John McCall5fe84122010-10-26 04:59:26 +0000664 // Ignore this if we've got an explicit attribute; the user
665 // probably knows what they're doing.
666 //
John McCall457a04e2010-10-22 21:05:15 +0000667 // Note that we don't want to make the variable non-external
668 // because of this, but unique-external linkage suits us.
Rafael Espindola593537a2013-05-05 20:15:21 +0000669 if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000670 LinkageInfo TypeLV = Var->getType()->getLinkageAndVisibility();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000671 if (TypeLV.getLinkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000672 return LinkageInfo::uniqueExternal();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000673 if (!LV.isVisibilityExplicit())
John McCalldf25c432013-02-16 00:17:33 +0000674 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000675 }
676
John McCall23032652010-11-02 18:38:13 +0000677 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000678 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000679
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000680 // Note that Sema::MergeVarDecl already takes care of implementing
681 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
682 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000683
Douglas Gregorf73b2822009-11-25 22:24:25 +0000684 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000685 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000686 // In theory, we can modify the function's LV by the LV of its
687 // type unless it has C linkage (see comment above about variables
688 // for justification). In practice, GCC doesn't do this, so it's
689 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000690
John McCall23032652010-11-02 18:38:13 +0000691 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000692 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000693
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000694 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
695 // merging storage classes and visibility attributes, so we don't have to
696 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000697
John McCallf768aa72011-02-10 06:50:24 +0000698 // In C++, then if the type of the function uses a type with
699 // unique-external linkage, it's not legally usable from outside
700 // this translation unit. However, we should use the C linkage
701 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000702 if (Context.getLangOpts().CPlusPlus &&
Richard Smith50f4afc2013-05-12 23:17:59 +0000703 !Function->isInExternCContext()) {
704 // Only look at the type-as-written. If this function has an auto-deduced
705 // return type, we can't compute the linkage of that type because it could
706 // require looking at the linkage of this function, and we don't need this
707 // for correctness because the type is not part of the function's
708 // signature.
709 // FIXME: This is a hack. We should be able to solve this circularity some
710 // other way.
711 QualType TypeAsWritten = Function->getType();
712 if (TypeSourceInfo *TSI = Function->getTypeSourceInfo())
713 TypeAsWritten = TSI->getType();
714 if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
715 return LinkageInfo::uniqueExternal();
716 }
John McCallf768aa72011-02-10 06:50:24 +0000717
John McCall5f46c482013-02-21 23:42:58 +0000718 // Consider LV from the template and the template arguments.
719 // We're at file scope, so we do not need to worry about nested
720 // specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000721 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000722 = Function->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000723 mergeTemplateLV(LV, Function, specInfo);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000724 }
725
Douglas Gregorf73b2822009-11-25 22:24:25 +0000726 // - a named class (Clause 9), or an unnamed class defined in a
727 // typedef declaration in which the class has the typedef name
728 // for linkage purposes (7.1.3); or
729 // - a named enumeration (7.2), or an unnamed enumeration
730 // defined in a typedef declaration in which the enumeration
731 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000732 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
733 // Unnamed tags have no linkage.
John McCall5ea95772013-03-09 00:54:27 +0000734 if (!Tag->hasNameForLinkage())
John McCallc273f242010-10-30 11:50:40 +0000735 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000736
John McCall457a04e2010-10-22 21:05:15 +0000737 // If this is a class template specialization, consider the
John McCall5f46c482013-02-21 23:42:58 +0000738 // linkage of the template and template arguments. We're at file
739 // scope, so we do not need to worry about nested specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000740 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000741 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCalldf25c432013-02-16 00:17:33 +0000742 mergeTemplateLV(LV, spec, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000743 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000744
745 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000746 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000747 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCalldf25c432013-02-16 00:17:33 +0000748 computation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000749 if (!isExternalLinkage(EnumLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000750 return LinkageInfo::none();
751 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000752
753 // - a template, unless it is a function template that has
754 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000755 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
John McCalld041a9b2013-02-20 01:54:26 +0000756 bool considerVisibility = !hasExplicitVisibilityAlready(computation);
John McCalldf25c432013-02-16 00:17:33 +0000757 LinkageInfo tempLV =
758 getLVForTemplateParameterList(temp->getTemplateParameters());
759 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
760
Douglas Gregorf73b2822009-11-25 22:24:25 +0000761 // - a namespace (7.3), unless it is declared within an unnamed
762 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000763 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
764 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000765
John McCall457a04e2010-10-22 21:05:15 +0000766 // By extension, we assign external linkage to Objective-C
767 // interfaces.
768 } else if (isa<ObjCInterfaceDecl>(D)) {
769 // fallout
770
771 // Everything not covered here has no linkage.
772 } else {
John McCallc273f242010-10-30 11:50:40 +0000773 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000774 }
775
776 // If we ended up with non-external linkage, visibility should
777 // always be default.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000778 if (LV.getLinkage() != ExternalLinkage)
779 return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000780
John McCall457a04e2010-10-22 21:05:15 +0000781 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000782}
783
John McCalldf25c432013-02-16 00:17:33 +0000784static LinkageInfo getLVForClassMember(const NamedDecl *D,
785 LVComputationKind computation) {
John McCall457a04e2010-10-22 21:05:15 +0000786 // Only certain class members have linkage. Note that fields don't
787 // really have linkage, but it's convenient to say they do for the
788 // purposes of calculating linkage of pointer-to-data-member
789 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000790 if (!(isa<CXXMethodDecl>(D) ||
791 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000792 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000793 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000794 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000795
John McCall07072662010-11-02 01:45:15 +0000796 LinkageInfo LV;
797
John McCall07072662010-11-02 01:45:15 +0000798 // If we have an explicit visibility attribute, merge that in.
John McCalld041a9b2013-02-20 01:54:26 +0000799 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000800 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000801 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000802 // If we're paying attention to global visibility, apply
803 // -finline-visibility-hidden if this is an inline method.
804 //
805 // Note that we do this before merging information about
806 // the class visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000807 if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000808 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000809 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000810
811 // If this class member has an explicit visibility attribute, the only
812 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000813 // only look for them when processing the class.
John McCalld041a9b2013-02-20 01:54:26 +0000814 LVComputationKind classComputation = computation;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000815 if (LV.isVisibilityExplicit())
John McCalld041a9b2013-02-20 01:54:26 +0000816 classComputation = withExplicitVisibilityAlready(computation);
Rafael Espindola505a7c82012-04-16 18:25:01 +0000817
John McCall5f46c482013-02-21 23:42:58 +0000818 LinkageInfo classLV =
819 getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000820 if (!isExternalLinkage(classLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000821 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000822
823 // If the class already has unique-external linkage, we can't improve.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000824 if (classLV.getLinkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000825 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000826
John McCall5f46c482013-02-21 23:42:58 +0000827 // Otherwise, don't merge in classLV yet, because in certain cases
828 // we need to completely ignore the visibility from it.
829
830 // Specifically, if this decl exists and has an explicit attribute.
831 const NamedDecl *explicitSpecSuppressor = 0;
832
John McCall8823c652010-08-13 08:35:10 +0000833 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000834 // If the type of the function uses a type with unique-external
835 // linkage, it's not legally usable from outside this translation unit.
836 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
837 return LinkageInfo::uniqueExternal();
838
John McCall457a04e2010-10-22 21:05:15 +0000839 // If this is a method template specialization, use the linkage for
840 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000841 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000842 = MD->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000843 mergeTemplateLV(LV, MD, spec);
John McCall5f46c482013-02-21 23:42:58 +0000844 if (spec->isExplicitSpecialization()) {
845 explicitSpecSuppressor = MD;
846 } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
847 explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
848 }
849 } else if (isExplicitMemberSpecialization(MD)) {
850 explicitSpecSuppressor = MD;
John McCalle6e622e2010-11-01 01:29:57 +0000851 }
John McCall457a04e2010-10-22 21:05:15 +0000852
John McCall37bb6c92010-10-29 22:22:43 +0000853 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000854 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000855 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCalldf25c432013-02-16 00:17:33 +0000856 mergeTemplateLV(LV, spec, computation);
John McCall5f46c482013-02-21 23:42:58 +0000857 if (spec->isExplicitSpecialization()) {
858 explicitSpecSuppressor = spec;
859 } else {
860 const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
861 if (isExplicitMemberSpecialization(temp)) {
862 explicitSpecSuppressor = temp->getTemplatedDecl();
863 }
864 }
865 } else if (isExplicitMemberSpecialization(RD)) {
866 explicitSpecSuppressor = RD;
John McCall37bb6c92010-10-29 22:22:43 +0000867 }
868
John McCall37bb6c92010-10-29 22:22:43 +0000869 // Static data members.
870 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000871 // Modify the variable's linkage by its type, but ignore the
872 // type's visibility unless it's a definition.
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000873 LinkageInfo typeLV = VD->getType()->getLinkageAndVisibility();
John McCall5f46c482013-02-21 23:42:58 +0000874 LV.mergeMaybeWithVisibility(typeLV,
Rafael Espindola4a5da442013-02-27 02:56:45 +0000875 !LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit());
John McCall5f46c482013-02-21 23:42:58 +0000876
877 if (isExplicitMemberSpecialization(VD)) {
878 explicitSpecSuppressor = VD;
879 }
John McCalldf25c432013-02-16 00:17:33 +0000880
881 // Template members.
882 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
883 bool considerVisibility =
Rafael Espindola4a5da442013-02-27 02:56:45 +0000884 (!LV.isVisibilityExplicit() &&
885 !classLV.isVisibilityExplicit() &&
John McCalld041a9b2013-02-20 01:54:26 +0000886 !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000887 LinkageInfo tempLV =
888 getLVForTemplateParameterList(temp->getTemplateParameters());
889 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall5f46c482013-02-21 23:42:58 +0000890
891 if (const RedeclarableTemplateDecl *redeclTemp =
892 dyn_cast<RedeclarableTemplateDecl>(temp)) {
893 if (isExplicitMemberSpecialization(redeclTemp)) {
894 explicitSpecSuppressor = temp->getTemplatedDecl();
895 }
896 }
John McCall37bb6c92010-10-29 22:22:43 +0000897 }
898
John McCall5f46c482013-02-21 23:42:58 +0000899 // We should never be looking for an attribute directly on a template.
900 assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
901
902 // If this member is an explicit member specialization, and it has
903 // an explicit attribute, ignore visibility from the parent.
904 bool considerClassVisibility = true;
905 if (explicitSpecSuppressor &&
Rafael Espindola4a5da442013-02-27 02:56:45 +0000906 // optimization: hasDVA() is true only with explicit visibility.
907 LV.isVisibilityExplicit() &&
908 classLV.getVisibility() != DefaultVisibility &&
John McCall5f46c482013-02-21 23:42:58 +0000909 hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) {
910 considerClassVisibility = false;
911 }
912
913 // Finally, merge in information from the class.
914 LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
John McCall457a04e2010-10-22 21:05:15 +0000915 return LV;
John McCall8823c652010-08-13 08:35:10 +0000916}
917
David Blaikie68e081d2011-12-20 02:48:34 +0000918void NamedDecl::anchor() { }
919
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000920bool NamedDecl::isLinkageValid() const {
921 if (!HasCachedLinkage)
922 return true;
John McCalld396b972011-02-08 19:01:05 +0000923
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000924 return getLVForDecl(this, LVForExplicitValue).getLinkage() ==
925 Linkage(CachedLinkage);
John McCalld396b972011-02-08 19:01:05 +0000926}
927
Rafael Espindola3ae00052013-05-13 00:12:11 +0000928Linkage NamedDecl::getLinkageInternal() const {
Richard Smith88581592013-02-12 05:48:23 +0000929 if (HasCachedLinkage)
Rafael Espindola19de5612013-01-12 06:42:30 +0000930 return Linkage(CachedLinkage);
Rafael Espindola19de5612013-01-12 06:42:30 +0000931
John McCalld041a9b2013-02-20 01:54:26 +0000932 // We don't care about visibility here, so ask for the cheapest
933 // possible visibility analysis.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000934 CachedLinkage = getLVForDecl(this, LVForExplicitValue).getLinkage();
Rafael Espindola19de5612013-01-12 06:42:30 +0000935 HasCachedLinkage = 1;
936
937#ifndef NDEBUG
938 verifyLinkage();
939#endif
940
941 return Linkage(CachedLinkage);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000942}
943
John McCallc273f242010-10-30 11:50:40 +0000944LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCalldf25c432013-02-16 00:17:33 +0000945 LVComputationKind computation =
946 (usesTypeVisibility(this) ? LVForType : LVForValue);
947 LinkageInfo LI = getLVForDecl(this, computation);
Rafael Espindola19de5612013-01-12 06:42:30 +0000948 if (HasCachedLinkage) {
Rafael Espindola4a5da442013-02-27 02:56:45 +0000949 assert(Linkage(CachedLinkage) == LI.getLinkage());
Rafael Espindola19de5612013-01-12 06:42:30 +0000950 return LI;
Rafael Espindola54606d52012-12-25 07:31:49 +0000951 }
Rafael Espindola19de5612013-01-12 06:42:30 +0000952 HasCachedLinkage = 1;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000953 CachedLinkage = LI.getLinkage();
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000954
955#ifndef NDEBUG
Rafael Espindola19de5612013-01-12 06:42:30 +0000956 verifyLinkage();
957#endif
958
959 return LI;
960}
961
962void NamedDecl::verifyLinkage() const {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000963 // In C (because of gnu inline) and in c++ with microsoft extensions an
964 // static can follow an extern, so we can have two decls with different
965 // linkages.
966 const LangOptions &Opts = getASTContext().getLangOpts();
967 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
Rafael Espindola19de5612013-01-12 06:42:30 +0000968 return;
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000969
970 // We have just computed the linkage for this decl. By induction we know
971 // that all other computed linkages match, check that the one we just computed
972 // also does.
973 NamedDecl *D = NULL;
974 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
975 NamedDecl *T = cast<NamedDecl>(*I);
976 if (T == this)
977 continue;
Rafael Espindola19de5612013-01-12 06:42:30 +0000978 if (T->HasCachedLinkage != 0) {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000979 D = T;
980 break;
981 }
982 }
983 assert(!D || D->CachedLinkage == CachedLinkage);
John McCall033caa52010-10-29 00:29:13 +0000984}
Ted Kremenek926d8602010-04-20 23:15:35 +0000985
David Blaikie05785d12013-02-20 22:23:23 +0000986Optional<Visibility>
John McCalld041a9b2013-02-20 01:54:26 +0000987NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
Rafael Espindola3a52c442013-02-26 19:33:14 +0000988 // Check the declaration itself first.
989 if (Optional<Visibility> V = getVisibilityOf(this, kind))
990 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000991
Rafael Espindola3a52c442013-02-26 19:33:14 +0000992 // If this is a member class of a specialization of a class template
993 // and the corresponding decl has explicit visibility, use that.
994 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
995 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
996 if (InstantiatedFrom)
997 return getVisibilityOf(InstantiatedFrom, kind);
998 }
999
1000 // If there wasn't explicit visibility there, and this is a
1001 // specialization of a class template, check for visibility
1002 // on the pattern.
1003 if (const ClassTemplateSpecializationDecl *spec
1004 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1005 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(),
1006 kind);
1007
1008 // Use the most recent declaration.
1009 const NamedDecl *MostRecent = cast<NamedDecl>(this->getMostRecentDecl());
1010 if (MostRecent != this)
1011 return MostRecent->getExplicitVisibility(kind);
1012
1013 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola96e68242012-05-16 02:10:38 +00001014 if (Var->isStaticDataMember()) {
1015 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
1016 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +00001017 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola96e68242012-05-16 02:10:38 +00001018 }
1019
David Blaikie7a30dc52013-02-21 01:47:18 +00001020 return None;
Rafael Espindola96e68242012-05-16 02:10:38 +00001021 }
Rafael Espindola3a52c442013-02-26 19:33:14 +00001022 // Also handle function template specializations.
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001023 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001024 // If the function is a specialization of a template with an
1025 // explicit visibility attribute, use that.
1026 if (FunctionTemplateSpecializationInfo *templateInfo
1027 = fn->getTemplateSpecializationInfo())
John McCalld041a9b2013-02-20 01:54:26 +00001028 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
1029 kind);
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001030
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001031 // If the function is a member of a specialization of a class template
1032 // and the corresponding decl has explicit visibility, use that.
1033 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
1034 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +00001035 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001036
David Blaikie7a30dc52013-02-21 01:47:18 +00001037 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001038 }
1039
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001040 // The visibility of a template is stored in the templated decl.
1041 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
John McCalld041a9b2013-02-20 01:54:26 +00001042 return getVisibilityOf(TD->getTemplatedDecl(), kind);
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001043
David Blaikie7a30dc52013-02-21 01:47:18 +00001044 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001045}
1046
John McCalldf25c432013-02-16 00:17:33 +00001047static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
1048 LVComputationKind computation) {
1049 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1050 if (Function->isInAnonymousNamespace() &&
Rafael Espindola593537a2013-05-05 20:15:21 +00001051 !Function->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001052 return LinkageInfo::uniqueExternal();
1053
1054 // This is a "void f();" which got merged with a file static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001055 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCalldf25c432013-02-16 00:17:33 +00001056 return LinkageInfo::internal();
1057
1058 LinkageInfo LV;
John McCalld041a9b2013-02-20 01:54:26 +00001059 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001060 if (Optional<Visibility> Vis =
1061 getExplicitVisibility(Function, computation))
John McCalldf25c432013-02-16 00:17:33 +00001062 LV.mergeVisibility(*Vis, true);
1063 }
1064
1065 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1066 // merging storage classes and visibility attributes, so we don't have to
1067 // look at previous decls in here.
1068
1069 return LV;
1070 }
1071
1072 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001073 if (Var->hasExternalStorage()) {
Rafael Espindola593537a2013-05-05 20:15:21 +00001074 if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001075 return LinkageInfo::uniqueExternal();
1076
John McCalldf25c432013-02-16 00:17:33 +00001077 LinkageInfo LV;
1078 if (Var->getStorageClass() == SC_PrivateExtern)
1079 LV.mergeVisibility(HiddenVisibility, true);
John McCalld041a9b2013-02-20 01:54:26 +00001080 else if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001081 if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
John McCalldf25c432013-02-16 00:17:33 +00001082 LV.mergeVisibility(*Vis, true);
1083 }
1084
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001085 if (const VarDecl *Prev = Var->getPreviousDecl()) {
1086 LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1087 if (PrevLV.getLinkage())
1088 LV.setLinkage(PrevLV.getLinkage());
1089 LV.mergeVisibility(PrevLV);
1090 }
1091
John McCalldf25c432013-02-16 00:17:33 +00001092 return LV;
1093 }
1094 }
1095
1096 return LinkageInfo::none();
1097}
1098
1099static LinkageInfo getLVForDecl(const NamedDecl *D,
1100 LVComputationKind computation) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001101 // Objective-C: treat all Objective-C declarations as having external
1102 // linkage.
John McCall033caa52010-10-29 00:29:13 +00001103 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001104 default:
1105 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +00001106 case Decl::ParmVar:
1107 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001108 case Decl::TemplateTemplateParm: // count these as external
1109 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +00001110 case Decl::ObjCAtDefsField:
1111 case Decl::ObjCCategory:
1112 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +00001113 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +00001114 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +00001115 case Decl::ObjCMethod:
1116 case Decl::ObjCProperty:
1117 case Decl::ObjCPropertyImpl:
1118 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +00001119 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001120
1121 case Decl::CXXRecord: {
1122 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
1123 if (Record->isLambda()) {
1124 if (!Record->getLambdaManglingNumber()) {
1125 // This lambda has no mangling number, so it's internal.
1126 return LinkageInfo::internal();
1127 }
1128
1129 // This lambda has its linkage/visibility determined by its owner.
1130 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
1131 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
1132 if (isa<ParmVarDecl>(ContextDecl))
1133 DC = ContextDecl->getDeclContext()->getRedeclContext();
1134 else
John McCalldf25c432013-02-16 00:17:33 +00001135 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001136 }
1137
1138 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
John McCalldf25c432013-02-16 00:17:33 +00001139 return getLVForDecl(ND, computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001140
1141 return LinkageInfo::external();
1142 }
1143
1144 break;
1145 }
Ted Kremenek926d8602010-04-20 23:15:35 +00001146 }
1147
Douglas Gregorf73b2822009-11-25 22:24:25 +00001148 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +00001149 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCalldf25c432013-02-16 00:17:33 +00001150 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001151
1152 // C++ [basic.link]p5:
1153 // In addition, a member function, static data member, a named
1154 // class or enumeration of class scope, or an unnamed class or
1155 // enumeration defined in a class-scope typedef declaration such
1156 // that the class or enumeration has the typedef name for linkage
1157 // purposes (7.1.3), has external linkage if the name of the class
1158 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +00001159 if (D->getDeclContext()->isRecord())
John McCalldf25c432013-02-16 00:17:33 +00001160 return getLVForClassMember(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001161
1162 // C++ [basic.link]p6:
1163 // The name of a function declared in block scope and the name of
1164 // an object declared by a block scope extern declaration have
1165 // linkage. If there is a visible declaration of an entity with
1166 // linkage having the same name and type, ignoring entities
1167 // declared outside the innermost enclosing namespace scope, the
1168 // block scope declaration declares that same entity and receives
1169 // the linkage of the previous declaration. If there is more than
1170 // one such matching entity, the program is ill-formed. Otherwise,
1171 // if no matching entity is found, the block scope entity receives
1172 // external linkage.
John McCalldf25c432013-02-16 00:17:33 +00001173 if (D->getDeclContext()->isFunctionOrMethod())
1174 return getLVForLocalDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001175
1176 // C++ [basic.link]p6:
1177 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +00001178 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001179}
Douglas Gregorf73b2822009-11-25 22:24:25 +00001180
Douglas Gregor2ada0482009-02-04 17:27:36 +00001181std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +00001182 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +00001183}
1184
1185std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001186 std::string QualName;
1187 llvm::raw_string_ostream OS(QualName);
1188 printQualifiedName(OS, P);
1189 return OS.str();
1190}
1191
1192void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1193 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1194}
1195
1196void NamedDecl::printQualifiedName(raw_ostream &OS,
1197 const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +00001198 const DeclContext *Ctx = getDeclContext();
1199
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001200 if (Ctx->isFunctionOrMethod()) {
1201 printName(OS);
1202 return;
1203 }
Douglas Gregor2ada0482009-02-04 17:27:36 +00001204
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001205 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001206 ContextsTy Contexts;
1207
1208 // Collect contexts.
1209 while (Ctx && isa<NamedDecl>(Ctx)) {
1210 Contexts.push_back(Ctx);
1211 Ctx = Ctx->getParent();
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001212 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001213
1214 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1215 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001216 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001217 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Benjamin Kramer9170e912013-02-22 15:46:01 +00001218 OS << Spec->getName();
Douglas Gregor85673582009-05-18 17:01:57 +00001219 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001220 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1221 TemplateArgs.data(),
1222 TemplateArgs.size(),
1223 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001224 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +00001225 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001226 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +00001227 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001228 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001229 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1230 if (!RD->getIdentifier())
1231 OS << "<anonymous " << RD->getKindName() << '>';
1232 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001233 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001234 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +00001235 const FunctionProtoType *FT = 0;
1236 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001237 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001238
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001239 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001240 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001241 unsigned NumParams = FD->getNumParams();
1242 for (unsigned i = 0; i < NumParams; ++i) {
1243 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001244 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001245 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001246 }
1247
1248 if (FT->isVariadic()) {
1249 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001250 OS << ", ";
1251 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001252 }
1253 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001254 OS << ')';
1255 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001256 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001257 }
1258 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001259 }
1260
John McCalla2a3f7d2010-03-16 21:48:18 +00001261 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001262 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001263 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001264 OS << "<anonymous>";
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001265}
Douglas Gregor2ada0482009-02-04 17:27:36 +00001266
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001267void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1268 const PrintingPolicy &Policy,
1269 bool Qualified) const {
1270 if (Qualified)
1271 printQualifiedName(OS, Policy);
1272 else
1273 printName(OS);
Douglas Gregor2ada0482009-02-04 17:27:36 +00001274}
1275
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001276bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001277 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1278
Douglas Gregor889ceb72009-02-03 19:21:40 +00001279 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1280 // We want to keep it, unless it nominates same namespace.
1281 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +00001282 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1283 ->getOriginalNamespace() ==
1284 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1285 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +00001286 }
Mike Stump11289f42009-09-09 15:08:12 +00001287
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001288 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1289 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +00001290 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001291
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001292 // For function templates, the underlying function declarations are linked.
1293 if (const FunctionTemplateDecl *FunctionTemplate
1294 = dyn_cast<FunctionTemplateDecl>(this))
1295 if (const FunctionTemplateDecl *OldFunctionTemplate
1296 = dyn_cast<FunctionTemplateDecl>(OldD))
1297 return FunctionTemplate->getTemplatedDecl()
1298 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001299
Steve Naroffc4173fa2009-02-22 19:35:57 +00001300 // For method declarations, we keep track of redeclarations.
1301 if (isa<ObjCMethodDecl>(this))
1302 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001303
John McCall9f3059a2009-10-09 21:13:30 +00001304 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1305 return true;
1306
John McCall3f746822009-11-17 05:59:44 +00001307 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1308 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1309 cast<UsingShadowDecl>(OldD)->getTargetDecl();
1310
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001311 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1312 ASTContext &Context = getASTContext();
1313 return Context.getCanonicalNestedNameSpecifier(
1314 cast<UsingDecl>(this)->getQualifier()) ==
1315 Context.getCanonicalNestedNameSpecifier(
1316 cast<UsingDecl>(OldD)->getQualifier());
1317 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +00001318
Douglas Gregorb59643b2012-01-03 23:26:26 +00001319 // A typedef of an Objective-C class type can replace an Objective-C class
1320 // declaration or definition, and vice versa.
1321 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1322 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1323 return true;
1324
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001325 // For non-function declarations, if the declarations are of the
1326 // same kind then this must be a redeclaration, or semantic analysis
1327 // would not have given us the new declaration.
1328 return this->getKind() == OldD->getKind();
1329}
1330
Douglas Gregoreddf4332009-02-24 20:03:32 +00001331bool NamedDecl::hasLinkage() const {
Rafael Espindola3ae00052013-05-13 00:12:11 +00001332 return getLinkageInternal() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001333}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001334
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001335NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001336 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001337 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1338 ND = UD->getTargetDecl();
1339
1340 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1341 return AD->getClassInterface();
1342
1343 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001344}
1345
John McCalla8ae2222010-04-06 21:38:20 +00001346bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001347 if (!isCXXClassMember())
1348 return false;
1349
John McCalla8ae2222010-04-06 21:38:20 +00001350 const NamedDecl *D = this;
1351 if (isa<UsingShadowDecl>(D))
1352 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1353
John McCall5e77d762013-04-16 07:28:30 +00001354 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001355 return true;
1356 if (isa<CXXMethodDecl>(D))
1357 return cast<CXXMethodDecl>(D)->isInstance();
1358 if (isa<FunctionTemplateDecl>(D))
1359 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1360 ->getTemplatedDecl())->isInstance();
1361 return false;
1362}
1363
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001364//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001365// DeclaratorDecl Implementation
1366//===----------------------------------------------------------------------===//
1367
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001368template <typename DeclT>
1369static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1370 if (decl->getNumTemplateParameterLists() > 0)
1371 return decl->getTemplateParameterList(0)->getTemplateLoc();
1372 else
1373 return decl->getInnerLocStart();
1374}
1375
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001376SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001377 TypeSourceInfo *TSI = getTypeSourceInfo();
1378 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001379 return SourceLocation();
1380}
1381
Douglas Gregor14454802011-02-25 02:25:35 +00001382void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1383 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001384 // Make sure the extended decl info is allocated.
1385 if (!hasExtInfo()) {
1386 // Save (non-extended) type source info pointer.
1387 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1388 // Allocate external info struct.
1389 DeclInfo = new (getASTContext()) ExtInfo;
1390 // Restore savedTInfo into (extended) decl info.
1391 getExtInfo()->TInfo = savedTInfo;
1392 }
1393 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001394 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001395 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001396 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001397 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001398 if (getExtInfo()->NumTemplParamLists == 0) {
1399 // Save type source info pointer.
1400 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1401 // Deallocate the extended decl info.
1402 getASTContext().Deallocate(getExtInfo());
1403 // Restore savedTInfo into (non-extended) decl info.
1404 DeclInfo = savedTInfo;
1405 }
1406 else
1407 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001408 }
1409 }
1410}
1411
Abramo Bagnara60804e12011-03-18 15:16:37 +00001412void
1413DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1414 unsigned NumTPLists,
1415 TemplateParameterList **TPLists) {
1416 assert(NumTPLists > 0);
1417 // Make sure the extended decl info is allocated.
1418 if (!hasExtInfo()) {
1419 // Save (non-extended) type source info pointer.
1420 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1421 // Allocate external info struct.
1422 DeclInfo = new (getASTContext()) ExtInfo;
1423 // Restore savedTInfo into (extended) decl info.
1424 getExtInfo()->TInfo = savedTInfo;
1425 }
1426 // Set the template parameter lists info.
1427 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1428}
1429
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001430SourceLocation DeclaratorDecl::getOuterLocStart() const {
1431 return getTemplateOrInnerLocStart(this);
1432}
1433
Abramo Bagnaraea947882011-03-08 16:41:52 +00001434namespace {
1435
1436// Helper function: returns true if QT is or contains a type
1437// having a postfix component.
1438bool typeIsPostfix(clang::QualType QT) {
1439 while (true) {
1440 const Type* T = QT.getTypePtr();
1441 switch (T->getTypeClass()) {
1442 default:
1443 return false;
1444 case Type::Pointer:
1445 QT = cast<PointerType>(T)->getPointeeType();
1446 break;
1447 case Type::BlockPointer:
1448 QT = cast<BlockPointerType>(T)->getPointeeType();
1449 break;
1450 case Type::MemberPointer:
1451 QT = cast<MemberPointerType>(T)->getPointeeType();
1452 break;
1453 case Type::LValueReference:
1454 case Type::RValueReference:
1455 QT = cast<ReferenceType>(T)->getPointeeType();
1456 break;
1457 case Type::PackExpansion:
1458 QT = cast<PackExpansionType>(T)->getPattern();
1459 break;
1460 case Type::Paren:
1461 case Type::ConstantArray:
1462 case Type::DependentSizedArray:
1463 case Type::IncompleteArray:
1464 case Type::VariableArray:
1465 case Type::FunctionProto:
1466 case Type::FunctionNoProto:
1467 return true;
1468 }
1469 }
1470}
1471
1472} // namespace
1473
1474SourceRange DeclaratorDecl::getSourceRange() const {
1475 SourceLocation RangeEnd = getLocation();
1476 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1477 if (typeIsPostfix(TInfo->getType()))
1478 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1479 }
1480 return SourceRange(getOuterLocStart(), RangeEnd);
1481}
1482
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001483void
Douglas Gregor20527e22010-06-15 17:44:38 +00001484QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1485 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001486 TemplateParameterList **TPLists) {
1487 assert((NumTPLists == 0 || TPLists != 0) &&
1488 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001489
1490 // Free previous template parameters (if any).
1491 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001492 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001493 TemplParamLists = 0;
1494 NumTemplParamLists = 0;
1495 }
1496 // Set info on matched template parameter lists (if any).
1497 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001498 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001499 NumTemplParamLists = NumTPLists;
1500 for (unsigned i = NumTPLists; i-- > 0; )
1501 TemplParamLists[i] = TPLists[i];
1502 }
1503}
1504
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001505//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001506// VarDecl Implementation
1507//===----------------------------------------------------------------------===//
1508
Sebastian Redl833ef452010-01-26 22:01:41 +00001509const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1510 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001511 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001512 case SC_Auto: return "auto";
1513 case SC_Extern: return "extern";
1514 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1515 case SC_PrivateExtern: return "__private_extern__";
1516 case SC_Register: return "register";
1517 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001518 }
1519
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001520 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001521}
1522
Abramo Bagnaradff19302011-03-08 08:55:46 +00001523VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1524 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001525 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001526 StorageClass S) {
1527 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001528}
1529
Douglas Gregor72172e92012-01-05 21:55:30 +00001530VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1531 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1532 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001533 QualType(), 0, SC_None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001534}
1535
Douglas Gregorbf62d642010-12-06 18:36:25 +00001536void VarDecl::setStorageClass(StorageClass SC) {
1537 assert(isLegalForVariable(SC));
John McCallbeaa11c2011-05-01 02:13:58 +00001538 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001539}
1540
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001541SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001542 if (const Expr *Init = getInit()) {
1543 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001544 // If Init is implicit, ignore its source range and fallback on
1545 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1546 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001547 return SourceRange(getOuterLocStart(), InitEnd);
1548 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001549 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001550}
1551
Rafael Espindola88510672013-01-04 21:18:45 +00001552template<typename T>
Rafael Espindolaf4187652013-02-14 01:18:37 +00001553static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001554 // C++ [dcl.link]p1: All function types, function names with external linkage,
1555 // and variable names with external linkage have a language linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +00001556 if (!D.hasExternalFormalLinkage())
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001557 return NoLanguageLinkage;
1558
1559 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001560 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001561 ASTContext &Context = D.getASTContext();
1562 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001563 return CLanguageLinkage;
1564
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001565 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1566 // language linkage of the names of class members and the function type of
1567 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001568 const DeclContext *DC = D.getDeclContext();
1569 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001570 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001571
1572 // If the first decl is in an extern "C" context, any other redeclaration
1573 // will have C language linkage. If the first one is not in an extern "C"
1574 // context, we would have reported an error for any other decl being in one.
Rafael Espindola593537a2013-05-05 20:15:21 +00001575 if (isFirstInExternCContext(&D))
Rafael Espindolaf4187652013-02-14 01:18:37 +00001576 return CLanguageLinkage;
1577 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001578}
1579
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001580template<typename T>
1581static bool isExternCTemplate(const T &D) {
1582 // Since the context is ignored for class members, they can only have C++
1583 // language linkage or no language linkage.
1584 const DeclContext *DC = D.getDeclContext();
1585 if (DC->isRecord()) {
1586 assert(D.getASTContext().getLangOpts().CPlusPlus);
1587 return false;
1588 }
1589
1590 return D.getLanguageLinkage() == CLanguageLinkage;
1591}
1592
Rafael Espindolaf4187652013-02-14 01:18:37 +00001593LanguageLinkage VarDecl::getLanguageLinkage() const {
1594 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001595}
1596
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001597bool VarDecl::isExternC() const {
1598 return isExternCTemplate(*this);
1599}
1600
Rafael Espindola593537a2013-05-05 20:15:21 +00001601static bool isLinkageSpecContext(const DeclContext *DC,
1602 LinkageSpecDecl::LanguageIDs ID) {
1603 while (DC->getDeclKind() != Decl::TranslationUnit) {
1604 if (DC->getDeclKind() == Decl::LinkageSpec)
1605 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1606 DC = DC->getParent();
1607 }
1608 return false;
1609}
1610
1611template <typename T>
1612static bool isInLanguageSpecContext(T *D, LinkageSpecDecl::LanguageIDs ID) {
1613 return isLinkageSpecContext(D->getLexicalDeclContext(), ID);
1614}
1615
1616bool VarDecl::isInExternCContext() const {
1617 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
1618}
1619
1620bool VarDecl::isInExternCXXContext() const {
1621 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
1622}
1623
Sebastian Redl833ef452010-01-26 22:01:41 +00001624VarDecl *VarDecl::getCanonicalDecl() {
1625 return getFirstDeclaration();
1626}
1627
Daniel Dunbar9d355812012-03-09 01:51:51 +00001628VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1629 ASTContext &C) const
1630{
Sebastian Redl35351a92010-01-31 22:27:38 +00001631 // C++ [basic.def]p2:
1632 // A declaration is a definition unless [...] it contains the 'extern'
1633 // specifier or a linkage-specification and neither an initializer [...],
1634 // it declares a static data member in a class declaration [...].
1635 // C++ [temp.expl.spec]p15:
1636 // An explicit specialization of a static data member of a template is a
1637 // definition if the declaration includes an initializer; otherwise, it is
1638 // a declaration.
1639 if (isStaticDataMember()) {
1640 if (isOutOfLine() && (hasInit() ||
1641 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1642 return Definition;
1643 else
1644 return DeclarationOnly;
1645 }
1646 // C99 6.7p5:
1647 // A definition of an identifier is a declaration for that identifier that
1648 // [...] causes storage to be reserved for that object.
1649 // Note: that applies for all non-file-scope objects.
1650 // C99 6.9.2p1:
1651 // If the declaration of an identifier for an object has file scope and an
1652 // initializer, the declaration is an external definition for the identifier
1653 if (hasInit())
1654 return Definition;
Rafael Espindolabff59562013-04-25 12:11:36 +00001655
Sebastian Redl35351a92010-01-31 22:27:38 +00001656 if (hasExternalStorage())
1657 return DeclarationOnly;
Rafael Espindola8f326a52013-03-07 01:42:44 +00001658
Rafael Espindolabff59562013-04-25 12:11:36 +00001659 // [dcl.link] p7:
1660 // A declaration directly contained in a linkage-specification is treated
1661 // as if it contains the extern specifier for the purpose of determining
1662 // the linkage of the declared name and whether it is a definition.
Rafael Espindola327be3c2013-04-26 01:30:23 +00001663 if (isSingleLineExternC(*this))
1664 return DeclarationOnly;
Rafael Espindolabff59562013-04-25 12:11:36 +00001665
Sebastian Redl35351a92010-01-31 22:27:38 +00001666 // C99 6.9.2p2:
1667 // A declaration of an object that has file scope without an initializer,
1668 // and without a storage class specifier or the scs 'static', constitutes
1669 // a tentative definition.
1670 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001671 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001672 return TentativeDefinition;
1673
1674 // What's left is (in C, block-scope) declarations without initializers or
1675 // external storage. These are definitions.
1676 return Definition;
1677}
1678
Sebastian Redl35351a92010-01-31 22:27:38 +00001679VarDecl *VarDecl::getActingDefinition() {
1680 DefinitionKind Kind = isThisDeclarationADefinition();
1681 if (Kind != TentativeDefinition)
1682 return 0;
1683
Chris Lattner48eb14d2010-06-14 18:31:46 +00001684 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001685 VarDecl *First = getFirstDeclaration();
1686 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1687 I != E; ++I) {
1688 Kind = (*I)->isThisDeclarationADefinition();
1689 if (Kind == Definition)
1690 return 0;
1691 else if (Kind == TentativeDefinition)
1692 LastTentative = *I;
1693 }
1694 return LastTentative;
1695}
1696
1697bool VarDecl::isTentativeDefinitionNow() const {
1698 DefinitionKind Kind = isThisDeclarationADefinition();
1699 if (Kind != TentativeDefinition)
1700 return false;
1701
1702 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1703 if ((*I)->isThisDeclarationADefinition() == Definition)
1704 return false;
1705 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001706 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001707}
1708
Daniel Dunbar9d355812012-03-09 01:51:51 +00001709VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001710 VarDecl *First = getFirstDeclaration();
1711 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1712 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001713 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001714 return *I;
1715 }
1716 return 0;
1717}
1718
Daniel Dunbar9d355812012-03-09 01:51:51 +00001719VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001720 DefinitionKind Kind = DeclarationOnly;
1721
1722 const VarDecl *First = getFirstDeclaration();
1723 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001724 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001725 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001726 if (Kind == Definition)
1727 break;
1728 }
John McCall37bb6c92010-10-29 22:22:43 +00001729
1730 return Kind;
1731}
1732
Sebastian Redl5ca79842010-02-01 20:16:42 +00001733const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001734 redecl_iterator I = redecls_begin(), E = redecls_end();
1735 while (I != E && !I->getInit())
1736 ++I;
1737
1738 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001739 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001740 return I->getInit();
1741 }
1742 return 0;
1743}
1744
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001745bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001746 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001747 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001748
1749 if (!isStaticDataMember())
1750 return false;
1751
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001752 // If this static data member was instantiated from a static data member of
1753 // a class template, check whether that static data member was defined
1754 // out-of-line.
1755 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1756 return VD->isOutOfLine();
1757
1758 return false;
1759}
1760
Douglas Gregor1d957a32009-10-27 18:42:08 +00001761VarDecl *VarDecl::getOutOfLineDefinition() {
1762 if (!isStaticDataMember())
1763 return 0;
1764
1765 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1766 RD != RDEnd; ++RD) {
1767 if (RD->getLexicalDeclContext()->isFileContext())
1768 return *RD;
1769 }
1770
1771 return 0;
1772}
1773
Douglas Gregord5058122010-02-11 01:19:42 +00001774void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001775 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1776 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001777 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001778 }
1779
1780 Init = I;
1781}
1782
Daniel Dunbar9d355812012-03-09 01:51:51 +00001783bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001784 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001785
Richard Smith35ecb362012-03-02 04:14:40 +00001786 if (!Lang.CPlusPlus)
1787 return false;
1788
1789 // In C++11, any variable of reference type can be used in a constant
1790 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001791 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001792 return true;
1793
1794 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001795 // not require the variable to be non-volatile, but we consider this to be a
1796 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001797 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001798 return false;
1799
1800 // In C++, const, non-volatile variables of integral or enumeration types
1801 // can be used in constant expressions.
1802 if (getType()->isIntegralOrEnumerationType())
1803 return true;
1804
Richard Smith35ecb362012-03-02 04:14:40 +00001805 // Additionally, in C++11, non-volatile constexpr variables can be used in
1806 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001807 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001808}
1809
Richard Smithd0b4dd62011-12-19 06:19:21 +00001810/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1811/// form, which contains extra information on the evaluated value of the
1812/// initializer.
1813EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1814 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1815 if (!Eval) {
1816 Stmt *S = Init.get<Stmt *>();
1817 Eval = new (getASTContext()) EvaluatedStmt;
1818 Eval->Value = S;
1819 Init = Eval;
1820 }
1821 return Eval;
1822}
1823
Richard Smithdafff942012-01-14 04:30:29 +00001824APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001825 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00001826 return evaluateValue(Notes);
1827}
1828
1829APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001830 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001831 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1832
1833 // We only produce notes indicating why an initializer is non-constant the
1834 // first time it is evaluated. FIXME: The notes won't always be emitted the
1835 // first time we try evaluation, so might not be produced at all.
1836 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001837 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001838
1839 const Expr *Init = cast<Expr>(Eval->Value);
1840 assert(!Init->isValueDependent());
1841
1842 if (Eval->IsEvaluating) {
1843 // FIXME: Produce a diagnostic for self-initialization.
1844 Eval->CheckedICE = true;
1845 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001846 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001847 }
1848
1849 Eval->IsEvaluating = true;
1850
1851 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1852 this, Notes);
1853
1854 // Ensure the result is an uninitialized APValue if evaluation fails.
1855 if (!Result)
1856 Eval->Evaluated = APValue();
1857
1858 Eval->IsEvaluating = false;
1859 Eval->WasEvaluated = true;
1860
1861 // In C++11, we have determined whether the initializer was a constant
1862 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001863 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001864 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001865 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001866 }
1867
Richard Smithdafff942012-01-14 04:30:29 +00001868 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001869}
1870
1871bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001872 // Initializers of weak variables are never ICEs.
1873 if (isWeak())
1874 return false;
1875
Richard Smithd0b4dd62011-12-19 06:19:21 +00001876 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1877 if (Eval->CheckedICE)
1878 // We have already checked whether this subexpression is an
1879 // integral constant expression.
1880 return Eval->IsICE;
1881
1882 const Expr *Init = cast<Expr>(Eval->Value);
1883 assert(!Init->isValueDependent());
1884
1885 // In C++11, evaluate the initializer to check whether it's a constant
1886 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001887 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001888 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001889 evaluateValue(Notes);
1890 return Eval->IsICE;
1891 }
1892
1893 // It's an ICE whether or not the definition we found is
1894 // out-of-line. See DR 721 and the discussion in Clang PR
1895 // 6206 for details.
1896
1897 if (Eval->CheckingICE)
1898 return false;
1899 Eval->CheckingICE = true;
1900
1901 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1902 Eval->CheckingICE = false;
1903 Eval->CheckedICE = true;
1904 return Eval->IsICE;
1905}
1906
Douglas Gregorfe314812011-06-21 17:03:29 +00001907bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001908 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001909
1910 const Expr *E = getInit();
1911 if (!E)
1912 return false;
1913
1914 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1915 E = Cleanups->getSubExpr();
1916
1917 return isa<MaterializeTemporaryExpr>(E);
1918}
1919
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001920VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001921 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001922 return cast<VarDecl>(MSI->getInstantiatedFrom());
1923
1924 return 0;
1925}
1926
Douglas Gregor3c74d412009-10-14 20:14:33 +00001927TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001928 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001929 return MSI->getTemplateSpecializationKind();
1930
1931 return TSK_Undeclared;
1932}
1933
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001934MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001935 return getASTContext().getInstantiatedFromStaticDataMember(this);
1936}
1937
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001938void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1939 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001940 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001941 assert(MSI && "Not an instantiated static data member?");
1942 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001943 if (TSK != TSK_ExplicitSpecialization &&
1944 PointOfInstantiation.isValid() &&
1945 MSI->getPointOfInstantiation().isInvalid())
1946 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001947}
1948
Sebastian Redl833ef452010-01-26 22:01:41 +00001949//===----------------------------------------------------------------------===//
1950// ParmVarDecl Implementation
1951//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001952
Sebastian Redl833ef452010-01-26 22:01:41 +00001953ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001954 SourceLocation StartLoc,
1955 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001956 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001957 StorageClass S, Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001958 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001959 S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001960}
1961
Douglas Gregor72172e92012-01-05 21:55:30 +00001962ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1964 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001965 0, QualType(), 0, SC_None, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001966}
1967
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001968SourceRange ParmVarDecl::getSourceRange() const {
1969 if (!hasInheritedDefaultArg()) {
1970 SourceRange ArgRange = getDefaultArgRange();
1971 if (ArgRange.isValid())
1972 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1973 }
1974
Argyrios Kyrtzidisa0772792013-04-17 01:56:48 +00001975 // DeclaratorDecl considers the range of postfix types as overlapping with the
1976 // declaration name, but this is not the case with parameters in ObjC methods.
1977 if (isa<ObjCMethodDecl>(getDeclContext()))
1978 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
1979
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001980 return DeclaratorDecl::getSourceRange();
1981}
1982
Sebastian Redl833ef452010-01-26 22:01:41 +00001983Expr *ParmVarDecl::getDefaultArg() {
1984 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1985 assert(!hasUninstantiatedDefaultArg() &&
1986 "Default argument is not yet instantiated!");
1987
1988 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001989 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001990 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001991
Sebastian Redl833ef452010-01-26 22:01:41 +00001992 return Arg;
1993}
1994
Sebastian Redl833ef452010-01-26 22:01:41 +00001995SourceRange ParmVarDecl::getDefaultArgRange() const {
1996 if (const Expr *E = getInit())
1997 return E->getSourceRange();
1998
1999 if (hasUninstantiatedDefaultArg())
2000 return getUninstantiatedDefaultArg()->getSourceRange();
2001
2002 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00002003}
2004
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00002005bool ParmVarDecl::isParameterPack() const {
2006 return isa<PackExpansionType>(getType());
2007}
2008
Ted Kremenek540017e2011-10-06 05:00:56 +00002009void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2010 getASTContext().setParameterIndex(this, parameterIndex);
2011 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2012}
2013
2014unsigned ParmVarDecl::getParameterIndexLarge() const {
2015 return getASTContext().getParameterIndex(this);
2016}
2017
Nuno Lopes394ec982008-12-17 23:39:55 +00002018//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002019// FunctionDecl Implementation
2020//===----------------------------------------------------------------------===//
2021
Benjamin Kramer9170e912013-02-22 15:46:01 +00002022void FunctionDecl::getNameForDiagnostic(
2023 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2024 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002025 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2026 if (TemplateArgs)
Benjamin Kramer9170e912013-02-22 15:46:01 +00002027 TemplateSpecializationType::PrintTemplateArgumentList(
2028 OS, TemplateArgs->data(), TemplateArgs->size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002029}
2030
Ted Kremenek186a0742010-04-29 16:49:01 +00002031bool FunctionDecl::isVariadic() const {
2032 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
2033 return FT->isVariadic();
2034 return false;
2035}
2036
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002037bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
2038 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00002039 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002040 Definition = *I;
2041 return true;
2042 }
2043 }
2044
2045 return false;
2046}
2047
Anders Carlsson9bd7d162011-05-14 23:26:09 +00002048bool FunctionDecl::hasTrivialBody() const
2049{
2050 Stmt *S = getBody();
2051 if (!S) {
2052 // Since we don't have a body for this function, we don't know if it's
2053 // trivial or not.
2054 return false;
2055 }
2056
2057 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2058 return true;
2059 return false;
2060}
2061
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002062bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2063 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00002064 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002065 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
2066 return true;
2067 }
2068 }
2069
2070 return false;
2071}
2072
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002073Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00002074 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2075 if (I->Body) {
2076 Definition = *I;
2077 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00002078 } else if (I->IsLateTemplateParsed) {
2079 Definition = *I;
2080 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00002081 }
2082 }
2083
2084 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002085}
2086
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002087void FunctionDecl::setBody(Stmt *B) {
2088 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00002089 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002090 EndRangeLoc = B->getLocEnd();
2091}
2092
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002093void FunctionDecl::setPure(bool P) {
2094 IsPure = P;
2095 if (P)
2096 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2097 Parent->markedVirtualFunctionPure();
2098}
2099
Douglas Gregor16618f22009-09-12 00:17:51 +00002100bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00002101 const TranslationUnitDecl *tunit =
2102 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2103 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002104 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00002105 getIdentifier() &&
2106 getIdentifier()->isStr("main");
2107}
2108
2109bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2110 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2111 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2112 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2113 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2114 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2115
2116 if (isa<CXXRecordDecl>(getDeclContext())) return false;
2117 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2118
2119 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
2120 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
2121
2122 ASTContext &Context =
2123 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2124 ->getASTContext();
2125
2126 // The result type and first argument type are constant across all
2127 // these operators. The second argument must be exactly void*.
2128 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00002129}
2130
Rafael Espindolaf4187652013-02-14 01:18:37 +00002131LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6239e052013-01-12 15:27:44 +00002132 // Users expect to be able to write
2133 // extern "C" void *__builtin_alloca (size_t);
2134 // so consider builtins as having C language linkage.
Rafael Espindolac48f7342013-01-12 15:27:43 +00002135 if (getBuiltinID())
Rafael Espindolaf4187652013-02-14 01:18:37 +00002136 return CLanguageLinkage;
Rafael Espindolac48f7342013-01-12 15:27:43 +00002137
Rafael Espindolaf4187652013-02-14 01:18:37 +00002138 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00002139}
2140
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002141bool FunctionDecl::isExternC() const {
2142 return isExternCTemplate(*this);
2143}
2144
Rafael Espindola593537a2013-05-05 20:15:21 +00002145bool FunctionDecl::isInExternCContext() const {
2146 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
2147}
2148
2149bool FunctionDecl::isInExternCXXContext() const {
2150 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
2151}
2152
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002153bool FunctionDecl::isGlobal() const {
2154 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
2155 return Method->isStatic();
2156
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002157 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002158 return false;
2159
Mike Stump11289f42009-09-09 15:08:12 +00002160 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002161 DC->isNamespace();
2162 DC = DC->getParent()) {
2163 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
2164 if (!Namespace->getDeclName())
2165 return false;
2166 break;
2167 }
2168 }
2169
2170 return true;
2171}
2172
Richard Smith10876ef2013-01-17 01:30:42 +00002173bool FunctionDecl::isNoReturn() const {
2174 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smithdebc59d2013-01-30 05:45:05 +00002175 hasAttr<C11NoReturnAttr>() ||
Richard Smith10876ef2013-01-17 01:30:42 +00002176 getType()->getAs<FunctionType>()->getNoReturnAttr();
2177}
2178
Sebastian Redl833ef452010-01-26 22:01:41 +00002179void
2180FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
2181 redeclarable_base::setPreviousDeclaration(PrevDecl);
2182
2183 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2184 FunctionTemplateDecl *PrevFunTmpl
2185 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
2186 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
2187 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
2188 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00002189
Axel Naumannfbc7b982011-11-08 18:21:06 +00002190 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00002191 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00002192}
2193
2194const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
2195 return getFirstDeclaration();
2196}
2197
2198FunctionDecl *FunctionDecl::getCanonicalDecl() {
2199 return getFirstDeclaration();
2200}
2201
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002202/// \brief Returns a value indicating whether this function
2203/// corresponds to a builtin function.
2204///
2205/// The function corresponds to a built-in function if it is
2206/// declared at translation scope or within an extern "C" block and
2207/// its name matches with the name of a builtin. The returned value
2208/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00002209/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002210/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00002211unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00002212 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00002213 return 0;
2214
2215 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00002216 if (!BuiltinID)
2217 return 0;
2218
2219 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00002220 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2221 return BuiltinID;
2222
2223 // This function has the name of a known C library
2224 // function. Determine whether it actually refers to the C library
2225 // function or whether it just has the same name.
2226
Douglas Gregora908e7f2009-02-17 03:23:10 +00002227 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00002228 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00002229 return 0;
2230
Douglas Gregore711f702009-02-14 18:57:46 +00002231 // If this function is at translation-unit scope and we're not in
2232 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002233 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00002234 getDeclContext()->isTranslationUnit())
2235 return BuiltinID;
2236
2237 // If the function is in an extern "C" linkage specification and is
2238 // not marked "overloadable", it's the real function.
2239 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00002240 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00002241 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002242 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00002243 return BuiltinID;
2244
2245 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002246 return 0;
2247}
2248
2249
Chris Lattner47c0d002009-04-25 06:03:53 +00002250/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00002251/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00002252/// after it has been created.
2253unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00002254 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002255 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00002256 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002257 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00002258
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002259}
2260
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002261void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002262 ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002263 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002264 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002265
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002266 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002267 if (!NewParamInfo.empty()) {
2268 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2269 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002270 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002271}
Chris Lattner41943152007-01-25 04:52:46 +00002272
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002273void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy6f8780b2012-02-29 10:24:19 +00002274 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2275
2276 if (!NewDecls.empty()) {
2277 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2278 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002279 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy6f8780b2012-02-29 10:24:19 +00002280 }
2281}
2282
Chris Lattner58258242008-04-10 02:22:51 +00002283/// getMinRequiredArguments - Returns the minimum number of arguments
2284/// needed to call this function. This may be fewer than the number of
2285/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00002286/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00002287unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002288 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002289 return getNumParams();
2290
Douglas Gregor7825bf32011-01-06 22:09:01 +00002291 unsigned NumRequiredArgs = getNumParams();
2292
2293 // If the last parameter is a parameter pack, we don't need an argument for
2294 // it.
2295 if (NumRequiredArgs > 0 &&
2296 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2297 --NumRequiredArgs;
2298
2299 // If this parameter has a default argument, we don't need an argument for
2300 // it.
2301 while (NumRequiredArgs > 0 &&
2302 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00002303 --NumRequiredArgs;
2304
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002305 // We might have parameter packs before the end. These can't be deduced,
2306 // but they can still handle multiple arguments.
2307 unsigned ArgIdx = NumRequiredArgs;
2308 while (ArgIdx > 0) {
2309 if (getParamDecl(ArgIdx - 1)->isParameterPack())
2310 NumRequiredArgs = ArgIdx;
2311
2312 --ArgIdx;
2313 }
2314
Chris Lattner58258242008-04-10 02:22:51 +00002315 return NumRequiredArgs;
2316}
2317
Eli Friedman1b125c32012-02-07 03:50:18 +00002318static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2319 // Only consider file-scope declarations in this test.
2320 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2321 return false;
2322
2323 // Only consider explicit declarations; the presence of a builtin for a
2324 // libcall shouldn't affect whether a definition is externally visible.
2325 if (Redecl->isImplicit())
2326 return false;
2327
2328 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2329 return true; // Not an inline definition
2330
2331 return false;
2332}
2333
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002334/// \brief For a function declaration in C or C++, determine whether this
2335/// declaration causes the definition to be externally visible.
2336///
Eli Friedman1b125c32012-02-07 03:50:18 +00002337/// Specifically, this determines if adding the current declaration to the set
2338/// of redeclarations of the given functions causes
2339/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002340bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2341 assert(!doesThisDeclarationHaveABody() &&
2342 "Must have a declaration without a body.");
2343
2344 ASTContext &Context = getASTContext();
2345
David Blaikiebbafb8a2012-03-11 07:00:24 +00002346 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002347 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2348 // an externally visible definition.
2349 //
2350 // FIXME: What happens if gnu_inline gets added on after the first
2351 // declaration?
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002352 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002353 return false;
2354
2355 const FunctionDecl *Prev = this;
2356 bool FoundBody = false;
2357 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002358 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002359
2360 if (Prev->Body) {
2361 // If it's not the case that both 'inline' and 'extern' are
2362 // specified on the definition, then it is always externally visible.
2363 if (!Prev->isInlineSpecified() ||
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002364 Prev->getStorageClass() != SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002365 return false;
2366 } else if (Prev->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002367 Prev->getStorageClass() != SC_Extern) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002368 return false;
2369 }
2370 }
2371 return FoundBody;
2372 }
2373
David Blaikiebbafb8a2012-03-11 07:00:24 +00002374 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002375 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002376
2377 // C99 6.7.4p6:
2378 // [...] If all of the file scope declarations for a function in a
2379 // translation unit include the inline function specifier without extern,
2380 // then the definition in that translation unit is an inline definition.
2381 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002382 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002383 const FunctionDecl *Prev = this;
2384 bool FoundBody = false;
2385 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002386 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002387 if (RedeclForcesDefC99(Prev))
2388 return false;
2389 }
2390 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002391}
2392
Richard Smithf3814ad2013-01-25 00:08:28 +00002393/// \brief For an inline function definition in C, or for a gnu_inline function
2394/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002395///
2396/// Inline function definitions are always available for inlining optimizations.
2397/// However, depending on the language dialect, declaration specifiers, and
2398/// attributes, the definition of an inline function may or may not be
2399/// "externally" visible to other translation units in the program.
2400///
2401/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002402/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002403/// inline definition becomes externally visible (C99 6.7.4p6).
2404///
2405/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2406/// definition, we use the GNU semantics for inline, which are nearly the
2407/// opposite of C99 semantics. In particular, "inline" by itself will create
2408/// an externally visible symbol, but "extern inline" will not create an
2409/// externally visible symbol.
2410bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002411 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002412 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002413 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002414
David Blaikiebbafb8a2012-03-11 07:00:24 +00002415 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002416 // Note: If you change the logic here, please change
2417 // doesDeclarationForceExternallyVisibleDefinition as well.
2418 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002419 // If it's not the case that both 'inline' and 'extern' are
2420 // specified on the definition, then this inline definition is
2421 // externally visible.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002422 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregorff76cb92010-12-09 16:59:22 +00002423 return true;
2424
2425 // If any declaration is 'inline' but not 'extern', then this definition
2426 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002427 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2428 Redecl != RedeclEnd;
2429 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002430 if (Redecl->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002431 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002432 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002433 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002434
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002435 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002436 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002437
Richard Smithf3814ad2013-01-25 00:08:28 +00002438 // The rest of this function is C-only.
2439 assert(!Context.getLangOpts().CPlusPlus &&
2440 "should not use C inline rules in C++");
2441
Douglas Gregor299d76e2009-09-13 07:46:26 +00002442 // C99 6.7.4p6:
2443 // [...] If all of the file scope declarations for a function in a
2444 // translation unit include the inline function specifier without extern,
2445 // then the definition in that translation unit is an inline definition.
2446 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2447 Redecl != RedeclEnd;
2448 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002449 if (RedeclForcesDefC99(*Redecl))
2450 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002451 }
2452
2453 // C99 6.7.4p6:
2454 // An inline definition does not provide an external definition for the
2455 // function, and does not forbid an external definition in another
2456 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002457 return false;
2458}
2459
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002460/// getOverloadedOperator - Which C++ overloaded operator this
2461/// function represents, if any.
2462OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002463 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2464 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002465 else
2466 return OO_None;
2467}
2468
Alexis Huntc88db062010-01-13 09:01:02 +00002469/// getLiteralIdentifier - The literal suffix identifier this function
2470/// represents, if any.
2471const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2472 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2473 return getDeclName().getCXXLiteralIdentifier();
2474 else
2475 return 0;
2476}
2477
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002478FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2479 if (TemplateOrSpecialization.isNull())
2480 return TK_NonTemplate;
2481 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2482 return TK_FunctionTemplate;
2483 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2484 return TK_MemberSpecialization;
2485 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2486 return TK_FunctionTemplateSpecialization;
2487 if (TemplateOrSpecialization.is
2488 <DependentFunctionTemplateSpecializationInfo*>())
2489 return TK_DependentFunctionTemplateSpecialization;
2490
David Blaikie83d382b2011-09-23 05:06:16 +00002491 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002492}
2493
Douglas Gregord801b062009-10-07 23:56:10 +00002494FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002495 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002496 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2497
2498 return 0;
2499}
2500
2501void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002502FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2503 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002504 TemplateSpecializationKind TSK) {
2505 assert(TemplateOrSpecialization.isNull() &&
2506 "Member function is already a specialization");
2507 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002508 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002509 TemplateOrSpecialization = Info;
2510}
2511
Douglas Gregorafca3b42009-10-27 20:53:28 +00002512bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002513 // If the function is invalid, it can't be implicitly instantiated.
2514 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002515 return false;
2516
2517 switch (getTemplateSpecializationKind()) {
2518 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002519 case TSK_ExplicitInstantiationDefinition:
2520 return false;
2521
2522 case TSK_ImplicitInstantiation:
2523 return true;
2524
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002525 // It is possible to instantiate TSK_ExplicitSpecialization kind
2526 // if the FunctionDecl has a class scope specialization pattern.
2527 case TSK_ExplicitSpecialization:
2528 return getClassScopeSpecializationPattern() != 0;
2529
Douglas Gregorafca3b42009-10-27 20:53:28 +00002530 case TSK_ExplicitInstantiationDeclaration:
2531 // Handled below.
2532 break;
2533 }
2534
2535 // Find the actual template from which we will instantiate.
2536 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002537 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002538 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002539 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002540
2541 // C++0x [temp.explicit]p9:
2542 // Except for inline functions, other explicit instantiation declarations
2543 // have the effect of suppressing the implicit instantiation of the entity
2544 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002545 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002546 return true;
2547
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002548 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002549}
2550
2551bool FunctionDecl::isTemplateInstantiation() const {
2552 switch (getTemplateSpecializationKind()) {
2553 case TSK_Undeclared:
2554 case TSK_ExplicitSpecialization:
2555 return false;
2556 case TSK_ImplicitInstantiation:
2557 case TSK_ExplicitInstantiationDeclaration:
2558 case TSK_ExplicitInstantiationDefinition:
2559 return true;
2560 }
2561 llvm_unreachable("All TSK values handled.");
2562}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002563
2564FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002565 // Handle class scope explicit specialization special case.
2566 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2567 return getClassScopeSpecializationPattern();
2568
Douglas Gregorafca3b42009-10-27 20:53:28 +00002569 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2570 while (Primary->getInstantiatedFromMemberTemplate()) {
2571 // If we have hit a point where the user provided a specialization of
2572 // this template, we're done looking.
2573 if (Primary->isMemberSpecialization())
2574 break;
2575
2576 Primary = Primary->getInstantiatedFromMemberTemplate();
2577 }
2578
2579 return Primary->getTemplatedDecl();
2580 }
2581
2582 return getInstantiatedFromMemberFunction();
2583}
2584
Douglas Gregor70d83e22009-06-29 17:30:29 +00002585FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002586 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002587 = TemplateOrSpecialization
2588 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002589 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002590 }
2591 return 0;
2592}
2593
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002594FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2595 return getASTContext().getClassScopeSpecializationPattern(this);
2596}
2597
Douglas Gregor70d83e22009-06-29 17:30:29 +00002598const TemplateArgumentList *
2599FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002600 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002601 = TemplateOrSpecialization
2602 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002603 return Info->TemplateArguments;
2604 }
2605 return 0;
2606}
2607
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002608const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002609FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2610 if (FunctionTemplateSpecializationInfo *Info
2611 = TemplateOrSpecialization
2612 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2613 return Info->TemplateArgumentsAsWritten;
2614 }
2615 return 0;
2616}
2617
Mike Stump11289f42009-09-09 15:08:12 +00002618void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002619FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2620 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002621 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002622 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002623 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002624 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2625 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002626 assert(TSK != TSK_Undeclared &&
2627 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002628 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002629 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002630 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002631 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2632 TemplateArgs,
2633 TemplateArgsAsWritten,
2634 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002635 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002636 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002637}
2638
John McCallb9c78482010-04-08 09:05:18 +00002639void
2640FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2641 const UnresolvedSetImpl &Templates,
2642 const TemplateArgumentListInfo &TemplateArgs) {
2643 assert(TemplateOrSpecialization.isNull());
2644 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2645 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002646 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002647 void *Buffer = Context.Allocate(Size);
2648 DependentFunctionTemplateSpecializationInfo *Info =
2649 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2650 TemplateArgs);
2651 TemplateOrSpecialization = Info;
2652}
2653
2654DependentFunctionTemplateSpecializationInfo::
2655DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2656 const TemplateArgumentListInfo &TArgs)
2657 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2658
2659 d.NumTemplates = Ts.size();
2660 d.NumArgs = TArgs.size();
2661
2662 FunctionTemplateDecl **TsArray =
2663 const_cast<FunctionTemplateDecl**>(getTemplates());
2664 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2665 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2666
2667 TemplateArgumentLoc *ArgsArray =
2668 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2669 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2670 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2671}
2672
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002673TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002674 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002675 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002676 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002677 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002678 if (FTSInfo)
2679 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002680
Douglas Gregord801b062009-10-07 23:56:10 +00002681 MemberSpecializationInfo *MSInfo
2682 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2683 if (MSInfo)
2684 return MSInfo->getTemplateSpecializationKind();
2685
2686 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002687}
2688
Mike Stump11289f42009-09-09 15:08:12 +00002689void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002690FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2691 SourceLocation PointOfInstantiation) {
2692 if (FunctionTemplateSpecializationInfo *FTSInfo
2693 = TemplateOrSpecialization.dyn_cast<
2694 FunctionTemplateSpecializationInfo*>()) {
2695 FTSInfo->setTemplateSpecializationKind(TSK);
2696 if (TSK != TSK_ExplicitSpecialization &&
2697 PointOfInstantiation.isValid() &&
2698 FTSInfo->getPointOfInstantiation().isInvalid())
2699 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2700 } else if (MemberSpecializationInfo *MSInfo
2701 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2702 MSInfo->setTemplateSpecializationKind(TSK);
2703 if (TSK != TSK_ExplicitSpecialization &&
2704 PointOfInstantiation.isValid() &&
2705 MSInfo->getPointOfInstantiation().isInvalid())
2706 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2707 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002708 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002709}
2710
2711SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002712 if (FunctionTemplateSpecializationInfo *FTSInfo
2713 = TemplateOrSpecialization.dyn_cast<
2714 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002715 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002716 else if (MemberSpecializationInfo *MSInfo
2717 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002718 return MSInfo->getPointOfInstantiation();
2719
2720 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002721}
2722
Douglas Gregor6411b922009-09-11 20:15:17 +00002723bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002724 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002725 return true;
2726
2727 // If this function was instantiated from a member function of a
2728 // class template, check whether that member function was defined out-of-line.
2729 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2730 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002731 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002732 return Definition->isOutOfLine();
2733 }
2734
2735 // If this function was instantiated from a function template,
2736 // check whether that function template was defined out-of-line.
2737 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2738 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002739 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002740 return Definition->isOutOfLine();
2741 }
2742
2743 return false;
2744}
2745
Abramo Bagnaraea947882011-03-08 16:41:52 +00002746SourceRange FunctionDecl::getSourceRange() const {
2747 return SourceRange(getOuterLocStart(), EndRangeLoc);
2748}
2749
Anna Zaks28db7ce2012-01-18 02:45:01 +00002750unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002751 IdentifierInfo *FnInfo = getIdentifier();
2752
2753 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002754 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002755
2756 // Builtin handling.
2757 switch (getBuiltinID()) {
2758 case Builtin::BI__builtin_memset:
2759 case Builtin::BI__builtin___memset_chk:
2760 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002761 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002762
2763 case Builtin::BI__builtin_memcpy:
2764 case Builtin::BI__builtin___memcpy_chk:
2765 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002766 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002767
2768 case Builtin::BI__builtin_memmove:
2769 case Builtin::BI__builtin___memmove_chk:
2770 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002771 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002772
2773 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002774 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002775 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002776 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002777
2778 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002779 case Builtin::BImemcmp:
2780 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002781
2782 case Builtin::BI__builtin_strncpy:
2783 case Builtin::BI__builtin___strncpy_chk:
2784 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002785 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002786
2787 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002788 case Builtin::BIstrncmp:
2789 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002790
2791 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002792 case Builtin::BIstrncasecmp:
2793 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002794
2795 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002796 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002797 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002798 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002799
2800 case Builtin::BI__builtin_strndup:
2801 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002802 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002803
Anna Zaks314cd092012-02-01 19:08:57 +00002804 case Builtin::BI__builtin_strlen:
2805 case Builtin::BIstrlen:
2806 return Builtin::BIstrlen;
2807
Anna Zaks201d4892012-01-13 21:52:01 +00002808 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002809 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002810 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002811 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002812 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002813 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002814 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002815 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002816 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002817 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002818 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002819 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002820 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002821 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002822 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002823 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002824 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002825 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002826 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002827 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002828 else if (FnInfo->isStr("strlen"))
2829 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002830 }
2831 break;
2832 }
Anna Zaks22122702012-01-17 00:37:07 +00002833 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002834}
2835
Chris Lattner59a25942008-03-31 00:36:02 +00002836//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002837// FieldDecl Implementation
2838//===----------------------------------------------------------------------===//
2839
Jay Foad39c79802011-01-12 09:06:06 +00002840FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002841 SourceLocation StartLoc, SourceLocation IdLoc,
2842 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002843 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002844 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002845 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002846 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002847}
2848
Douglas Gregor72172e92012-01-05 21:55:30 +00002849FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2850 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2851 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002852 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002853}
2854
Sebastian Redl833ef452010-01-26 22:01:41 +00002855bool FieldDecl::isAnonymousStructOrUnion() const {
2856 if (!isImplicit() || getDeclName())
2857 return false;
2858
2859 if (const RecordType *Record = getType()->getAs<RecordType>())
2860 return Record->getDecl()->isAnonymousStructOrUnion();
2861
2862 return false;
2863}
2864
Richard Smithcaf33902011-10-10 18:28:20 +00002865unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2866 assert(isBitField() && "not a bitfield");
2867 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2868 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2869}
2870
John McCall4e819612011-01-20 07:57:12 +00002871unsigned FieldDecl::getFieldIndex() const {
2872 if (CachedFieldIndex) return CachedFieldIndex - 1;
2873
Richard Smithd62306a2011-11-10 06:34:14 +00002874 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002875 const RecordDecl *RD = getParent();
2876 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002877 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002878
2879 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2880 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002881 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002882
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002883 if (IsMsStruct) {
2884 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002885 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002886 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002887 continue;
2888 }
David Blaikie40ed2972012-06-06 20:45:41 +00002889 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002890 }
John McCall4e819612011-01-20 07:57:12 +00002891 }
2892
Richard Smithd62306a2011-11-10 06:34:14 +00002893 assert(CachedFieldIndex && "failed to find field in parent");
2894 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002895}
2896
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002897SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002898 if (const Expr *E = InitializerOrBitWidth.getPointer())
2899 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002900 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002901}
2902
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002903void FieldDecl::setBitWidth(Expr *Width) {
2904 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2905 "bit width or initializer already set");
2906 InitializerOrBitWidth.setPointer(Width);
2907}
2908
Richard Smith938f40b2011-06-11 17:19:42 +00002909void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002910 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002911 "bit width or initializer already set");
2912 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002913}
2914
Sebastian Redl833ef452010-01-26 22:01:41 +00002915//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002916// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002917//===----------------------------------------------------------------------===//
2918
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002919SourceLocation TagDecl::getOuterLocStart() const {
2920 return getTemplateOrInnerLocStart(this);
2921}
2922
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002923SourceRange TagDecl::getSourceRange() const {
2924 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002925 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002926}
2927
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002928TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002929 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002930}
2931
Rafael Espindolabf5c33b2013-03-12 21:06:00 +00002932void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2933 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002934 if (TypeForDecl)
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002935 assert(TypeForDecl->isLinkageValid());
2936 assert(isLinkageValid());
Douglas Gregora72a4e32010-05-19 18:39:18 +00002937}
2938
Douglas Gregordee1be82009-01-17 00:42:38 +00002939void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002940 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002941
David Blaikie095deba2012-11-14 01:52:05 +00002942 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002943 struct CXXRecordDecl::DefinitionData *Data =
2944 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002945 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2946 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002947 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002948}
2949
2950void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002951 assert((!isa<CXXRecordDecl>(this) ||
2952 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2953 "definition completed but not started");
2954
John McCallf937c022011-10-07 06:10:15 +00002955 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002956 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002957
2958 if (ASTMutationListener *L = getASTMutationListener())
2959 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002960}
2961
John McCallf937c022011-10-07 06:10:15 +00002962TagDecl *TagDecl::getDefinition() const {
2963 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002964 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002965
2966 // If it's possible for us to have an out-of-date definition, check now.
2967 if (MayHaveOutOfDateDef) {
2968 if (IdentifierInfo *II = getIdentifier()) {
2969 if (II->isOutOfDate()) {
2970 updateOutOfDate(*II);
2971 }
2972 }
2973 }
2974
Andrew Trickba266ee2010-10-19 21:54:32 +00002975 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2976 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002977
2978 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002979 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002980 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002981 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002982
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002983 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002984}
2985
Douglas Gregor14454802011-02-25 02:25:35 +00002986void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2987 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002988 // Make sure the extended qualifier info is allocated.
2989 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002990 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002991 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002992 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002993 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002994 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002995 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002996 if (getExtInfo()->NumTemplParamLists == 0) {
2997 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002998 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002999 }
3000 else
3001 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00003002 }
3003 }
3004}
3005
Abramo Bagnara60804e12011-03-18 15:16:37 +00003006void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
3007 unsigned NumTPLists,
3008 TemplateParameterList **TPLists) {
3009 assert(NumTPLists > 0);
3010 // Make sure the extended decl info is allocated.
3011 if (!hasExtInfo())
3012 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00003013 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003014 // Set the template parameter lists info.
3015 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
3016}
3017
Ted Kremenek21475702008-09-05 17:16:31 +00003018//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003019// EnumDecl Implementation
3020//===----------------------------------------------------------------------===//
3021
David Blaikie68e081d2011-12-20 02:48:34 +00003022void EnumDecl::anchor() { }
3023
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003024EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3025 SourceLocation StartLoc, SourceLocation IdLoc,
3026 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003027 EnumDecl *PrevDecl, bool IsScoped,
3028 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003029 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003030 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003031 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00003032 C.getTypeDeclType(Enum, PrevDecl);
3033 return Enum;
3034}
3035
Douglas Gregor72172e92012-01-05 21:55:30 +00003036EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3037 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003038 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
3039 0, 0, false, false, false);
3040 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3041 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003042}
3043
Douglas Gregord5058122010-02-11 01:19:42 +00003044void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00003045 QualType NewPromotionType,
3046 unsigned NumPositiveBits,
3047 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00003048 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00003049 if (!IntegerType)
3050 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00003051 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00003052 setNumPositiveBits(NumPositiveBits);
3053 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00003054 TagDecl::completeDefinition();
3055}
3056
Richard Smith7d137e32012-03-23 03:33:32 +00003057TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3058 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3059 return MSI->getTemplateSpecializationKind();
3060
3061 return TSK_Undeclared;
3062}
3063
3064void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3065 SourceLocation PointOfInstantiation) {
3066 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3067 assert(MSI && "Not an instantiated member enumeration?");
3068 MSI->setTemplateSpecializationKind(TSK);
3069 if (TSK != TSK_ExplicitSpecialization &&
3070 PointOfInstantiation.isValid() &&
3071 MSI->getPointOfInstantiation().isInvalid())
3072 MSI->setPointOfInstantiation(PointOfInstantiation);
3073}
3074
Richard Smith4b38ded2012-03-14 23:13:10 +00003075EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3076 if (SpecializationInfo)
3077 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3078
3079 return 0;
3080}
3081
3082void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3083 TemplateSpecializationKind TSK) {
3084 assert(!SpecializationInfo && "Member enum is already a specialization");
3085 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3086}
3087
Sebastian Redl833ef452010-01-26 22:01:41 +00003088//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00003089// RecordDecl Implementation
3090//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00003091
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003092RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
3093 SourceLocation StartLoc, SourceLocation IdLoc,
3094 IdentifierInfo *Id, RecordDecl *PrevDecl)
3095 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00003096 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003097 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003098 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003099 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003100 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00003101 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00003102}
3103
Jay Foad39c79802011-01-12 09:06:06 +00003104RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003105 SourceLocation StartLoc, SourceLocation IdLoc,
3106 IdentifierInfo *Id, RecordDecl* PrevDecl) {
3107 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
3108 PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003109 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3110
Ted Kremenek21475702008-09-05 17:16:31 +00003111 C.getTypeDeclType(R, PrevDecl);
3112 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00003113}
3114
Douglas Gregor72172e92012-01-05 21:55:30 +00003115RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
3116 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003117 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
3118 SourceLocation(), 0, 0);
3119 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3120 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003121}
3122
Douglas Gregordfcad112009-03-25 15:59:44 +00003123bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00003124 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00003125 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3126}
3127
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003128RecordDecl::field_iterator RecordDecl::field_begin() const {
3129 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3130 LoadFieldsFromExternalStorage();
3131
3132 return field_iterator(decl_iterator(FirstDecl));
3133}
3134
Douglas Gregorb11aad82011-02-19 18:51:44 +00003135/// completeDefinition - Notes that the definition of this type is now
3136/// complete.
3137void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00003138 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00003139 TagDecl::completeDefinition();
3140}
3141
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003142/// isMsStruct - Get whether or not this record uses ms_struct layout.
3143/// This which can be turned on with an attribute, pragma, or the
3144/// -mms-bitfields command-line option.
3145bool RecordDecl::isMsStruct(const ASTContext &C) const {
3146 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
3147}
3148
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003149static bool isFieldOrIndirectField(Decl::Kind K) {
3150 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3151}
3152
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003153void RecordDecl::LoadFieldsFromExternalStorage() const {
3154 ExternalASTSource *Source = getASTContext().getExternalSource();
3155 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3156
3157 // Notify that we have a RecordDecl doing some initialization.
3158 ExternalASTSource::Deserializing TheFields(Source);
3159
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003160 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003161 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003162 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
3163 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003164 case ELR_Success:
3165 break;
3166
3167 case ELR_AlreadyLoaded:
3168 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003169 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003170 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003171
3172#ifndef NDEBUG
3173 // Check that all decls we got were FieldDecls.
3174 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003175 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003176#endif
3177
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003178 if (Decls.empty())
3179 return;
3180
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00003181 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
3182 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003183}
3184
Steve Naroff415d3d52008-10-08 17:01:13 +00003185//===----------------------------------------------------------------------===//
3186// BlockDecl Implementation
3187//===----------------------------------------------------------------------===//
3188
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003189void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00003190 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00003191
Steve Naroffc4b30e52009-03-13 16:56:44 +00003192 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00003193 if (!NewParamInfo.empty()) {
3194 NumParams = NewParamInfo.size();
3195 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3196 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003197 }
3198}
3199
John McCall351762c2011-02-07 10:33:21 +00003200void BlockDecl::setCaptures(ASTContext &Context,
3201 const Capture *begin,
3202 const Capture *end,
3203 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00003204 CapturesCXXThis = capturesCXXThis;
3205
3206 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00003207 NumCaptures = 0;
3208 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00003209 return;
3210 }
3211
John McCall351762c2011-02-07 10:33:21 +00003212 NumCaptures = end - begin;
3213
3214 // Avoid new Capture[] because we don't want to provide a default
3215 // constructor.
3216 size_t allocationSize = NumCaptures * sizeof(Capture);
3217 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
3218 memcpy(buffer, begin, allocationSize);
3219 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003220}
Sebastian Redl833ef452010-01-26 22:01:41 +00003221
John McCallce45f882011-06-15 22:51:16 +00003222bool BlockDecl::capturesVariable(const VarDecl *variable) const {
3223 for (capture_const_iterator
3224 i = capture_begin(), e = capture_end(); i != e; ++i)
3225 // Only auto vars can be captured, so no redeclaration worries.
3226 if (i->getVariable() == variable)
3227 return true;
3228
3229 return false;
3230}
3231
Douglas Gregor70226da2010-12-21 16:27:07 +00003232SourceRange BlockDecl::getSourceRange() const {
3233 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
3234}
Sebastian Redl833ef452010-01-26 22:01:41 +00003235
3236//===----------------------------------------------------------------------===//
3237// Other Decl Allocation/Deallocation Method Implementations
3238//===----------------------------------------------------------------------===//
3239
David Blaikie68e081d2011-12-20 02:48:34 +00003240void TranslationUnitDecl::anchor() { }
3241
Sebastian Redl833ef452010-01-26 22:01:41 +00003242TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
3243 return new (C) TranslationUnitDecl(C);
3244}
3245
David Blaikie68e081d2011-12-20 02:48:34 +00003246void LabelDecl::anchor() { }
3247
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003248LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00003249 SourceLocation IdentL, IdentifierInfo *II) {
3250 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
3251}
3252
3253LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3254 SourceLocation IdentL, IdentifierInfo *II,
3255 SourceLocation GnuLabelL) {
3256 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3257 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003258}
3259
Douglas Gregor72172e92012-01-05 21:55:30 +00003260LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3261 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3262 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00003263}
3264
David Blaikie68e081d2011-12-20 02:48:34 +00003265void ValueDecl::anchor() { }
3266
Benjamin Kramerea70eb32012-12-01 15:09:41 +00003267bool ValueDecl::isWeak() const {
3268 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3269 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3270 return true;
3271
3272 return isWeakImported();
3273}
3274
David Blaikie68e081d2011-12-20 02:48:34 +00003275void ImplicitParamDecl::anchor() { }
3276
Sebastian Redl833ef452010-01-26 22:01:41 +00003277ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003278 SourceLocation IdLoc,
3279 IdentifierInfo *Id,
3280 QualType Type) {
3281 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00003282}
3283
Douglas Gregor72172e92012-01-05 21:55:30 +00003284ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3285 unsigned ID) {
3286 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3287 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3288}
3289
Sebastian Redl833ef452010-01-26 22:01:41 +00003290FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003291 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003292 const DeclarationNameInfo &NameInfo,
3293 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003294 StorageClass SC,
Douglas Gregorff76cb92010-12-09 16:59:22 +00003295 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00003296 bool hasWrittenPrototype,
3297 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003298 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003299 T, TInfo, SC,
Richard Smitha77a0a62011-08-15 21:04:07 +00003300 isInlineSpecified,
3301 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00003302 New->HasWrittenPrototype = hasWrittenPrototype;
3303 return New;
3304}
3305
Douglas Gregor72172e92012-01-05 21:55:30 +00003306FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3307 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3308 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3309 DeclarationNameInfo(), QualType(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003310 SC_None, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00003311}
3312
Sebastian Redl833ef452010-01-26 22:01:41 +00003313BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3314 return new (C) BlockDecl(DC, L);
3315}
3316
Douglas Gregor72172e92012-01-05 21:55:30 +00003317BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3318 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3319 return new (Mem) BlockDecl(0, SourceLocation());
3320}
3321
John McCall5e77d762013-04-16 07:28:30 +00003322MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3323 unsigned ID) {
3324 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(MSPropertyDecl));
3325 return new (Mem) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
3326 QualType(), 0, SourceLocation(),
3327 0, 0);
3328}
3329
Ben Langmuir37943a72013-05-03 19:00:33 +00003330CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
3331 unsigned NumParams) {
Ben Langmuirce914fc2013-05-03 19:20:19 +00003332 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
Ben Langmuir37943a72013-05-03 19:00:33 +00003333 return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003334}
3335
Ben Langmuirce914fc2013-05-03 19:20:19 +00003336CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3337 unsigned NumParams) {
3338 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3339 void *Mem = AllocateDeserializedDecl(C, ID, Size);
3340 return new (Mem) CapturedDecl(0, NumParams);
3341}
3342
Sebastian Redl833ef452010-01-26 22:01:41 +00003343EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3344 SourceLocation L,
3345 IdentifierInfo *Id, QualType T,
3346 Expr *E, const llvm::APSInt &V) {
3347 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3348}
3349
Douglas Gregor72172e92012-01-05 21:55:30 +00003350EnumConstantDecl *
3351EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3352 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3353 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3354 llvm::APSInt());
3355}
3356
David Blaikie68e081d2011-12-20 02:48:34 +00003357void IndirectFieldDecl::anchor() { }
3358
Benjamin Kramer39593702010-11-21 14:11:41 +00003359IndirectFieldDecl *
3360IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3361 IdentifierInfo *Id, QualType T, NamedDecl **CH,
3362 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003363 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3364}
3365
Douglas Gregor72172e92012-01-05 21:55:30 +00003366IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3367 unsigned ID) {
3368 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3369 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3370 QualType(), 0, 0);
3371}
3372
Douglas Gregorbe996932010-09-01 20:41:53 +00003373SourceRange EnumConstantDecl::getSourceRange() const {
3374 SourceLocation End = getLocation();
3375 if (Init)
3376 End = Init->getLocEnd();
3377 return SourceRange(getLocation(), End);
3378}
3379
David Blaikie68e081d2011-12-20 02:48:34 +00003380void TypeDecl::anchor() { }
3381
Sebastian Redl833ef452010-01-26 22:01:41 +00003382TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003383 SourceLocation StartLoc, SourceLocation IdLoc,
3384 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3385 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003386}
3387
David Blaikie68e081d2011-12-20 02:48:34 +00003388void TypedefNameDecl::anchor() { }
3389
Douglas Gregor72172e92012-01-05 21:55:30 +00003390TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3391 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3392 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3393}
3394
Richard Smithdda56e42011-04-15 14:24:37 +00003395TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3396 SourceLocation StartLoc,
3397 SourceLocation IdLoc, IdentifierInfo *Id,
3398 TypeSourceInfo *TInfo) {
3399 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3400}
3401
Douglas Gregor72172e92012-01-05 21:55:30 +00003402TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3403 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3404 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3405}
3406
Abramo Bagnaraea947882011-03-08 16:41:52 +00003407SourceRange TypedefDecl::getSourceRange() const {
3408 SourceLocation RangeEnd = getLocation();
3409 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3410 if (typeIsPostfix(TInfo->getType()))
3411 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3412 }
3413 return SourceRange(getLocStart(), RangeEnd);
3414}
3415
Richard Smithdda56e42011-04-15 14:24:37 +00003416SourceRange TypeAliasDecl::getSourceRange() const {
3417 SourceLocation RangeEnd = getLocStart();
3418 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3419 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3420 return SourceRange(getLocStart(), RangeEnd);
3421}
3422
David Blaikie68e081d2011-12-20 02:48:34 +00003423void FileScopeAsmDecl::anchor() { }
3424
Sebastian Redl833ef452010-01-26 22:01:41 +00003425FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003426 StringLiteral *Str,
3427 SourceLocation AsmLoc,
3428 SourceLocation RParenLoc) {
3429 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003430}
Douglas Gregorba345522011-12-02 23:23:56 +00003431
Douglas Gregor72172e92012-01-05 21:55:30 +00003432FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3433 unsigned ID) {
3434 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3435 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3436}
3437
Michael Han84324352013-02-22 17:15:32 +00003438void EmptyDecl::anchor() {}
3439
3440EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3441 return new (C) EmptyDecl(DC, L);
3442}
3443
3444EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3445 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EmptyDecl));
3446 return new (Mem) EmptyDecl(0, SourceLocation());
3447}
3448
Douglas Gregorba345522011-12-02 23:23:56 +00003449//===----------------------------------------------------------------------===//
3450// ImportDecl Implementation
3451//===----------------------------------------------------------------------===//
3452
3453/// \brief Retrieve the number of module identifiers needed to name the given
3454/// module.
3455static unsigned getNumModuleIdentifiers(Module *Mod) {
3456 unsigned Result = 1;
3457 while (Mod->Parent) {
3458 Mod = Mod->Parent;
3459 ++Result;
3460 }
3461 return Result;
3462}
3463
Douglas Gregor22d09742012-01-03 18:04:46 +00003464ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003465 Module *Imported,
3466 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003467 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003468 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003469{
3470 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3471 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3472 memcpy(StoredLocs, IdentifierLocs.data(),
3473 IdentifierLocs.size() * sizeof(SourceLocation));
3474}
3475
Douglas Gregor22d09742012-01-03 18:04:46 +00003476ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003477 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003478 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003479 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003480{
3481 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3482}
3483
3484ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003485 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003486 ArrayRef<SourceLocation> IdentifierLocs) {
3487 void *Mem = C.Allocate(sizeof(ImportDecl) +
3488 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003489 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003490}
3491
3492ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003493 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003494 Module *Imported,
3495 SourceLocation EndLoc) {
3496 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003497 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003498 Import->setImplicit();
3499 return Import;
3500}
3501
Douglas Gregor72172e92012-01-05 21:55:30 +00003502ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3503 unsigned NumLocations) {
3504 void *Mem = AllocateDeserializedDecl(C, ID,
3505 (sizeof(ImportDecl) +
3506 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003507 return new (Mem) ImportDecl(EmptyShell());
3508}
3509
3510ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3511 if (!ImportedAndComplete.getInt())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00003512 return None;
Douglas Gregorba345522011-12-02 23:23:56 +00003513
3514 const SourceLocation *StoredLocs
3515 = reinterpret_cast<const SourceLocation *>(this + 1);
3516 return ArrayRef<SourceLocation>(StoredLocs,
3517 getNumModuleIdentifiers(getImportedModule()));
3518}
3519
3520SourceRange ImportDecl::getSourceRange() const {
3521 if (!ImportedAndComplete.getInt())
3522 return SourceRange(getLocation(),
3523 *reinterpret_cast<const SourceLocation *>(this + 1));
3524
3525 return SourceRange(getLocation(), getIdentifierLocs().back());
3526}