blob: cbd55889c9e4e4b200c942d57a8c3abc2336d6ae [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
301 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TT->getDecl());
302 if (!RD)
303 return LI;
304
305 const FunctionDecl *FD = getOutermostFunctionContext(RD);
306 if (!FD)
307 return LI;
308
309 if (!FD->isInlined())
310 return LI;
311
312 return FD->getLinkageAndVisibility();
313}
314
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000315/// \brief Get the most restrictive linkage for the types and
316/// declarations in the given template argument list.
John McCalldf25c432013-02-16 00:17:33 +0000317///
318/// Note that we don't take an LVComputationKind because we always
319/// want to honor the visibility of template arguments in the same way.
320static LinkageInfo
321getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args) {
322 LinkageInfo LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000323
John McCalldf25c432013-02-16 00:17:33 +0000324 for (unsigned i = 0, e = args.size(); i != e; ++i) {
325 const TemplateArgument &arg = args[i];
326 switch (arg.getKind()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000327 case TemplateArgument::Null:
328 case TemplateArgument::Integral:
329 case TemplateArgument::Expression:
John McCalldf25c432013-02-16 00:17:33 +0000330 continue;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000331
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000332 case TemplateArgument::Type:
Rafael Espindolac1b38a22013-05-16 04:30:21 +0000333 LV.merge(getLIForTemplateTypeArgument(arg.getAsType()));
John McCalldf25c432013-02-16 00:17:33 +0000334 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000335
336 case TemplateArgument::Declaration:
John McCalldf25c432013-02-16 00:17:33 +0000337 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
338 assert(!usesTypeVisibility(ND));
339 LV.merge(getLVForDecl(ND, LVForValue));
340 }
341 continue;
Eli Friedmanb826a002012-09-26 02:36:12 +0000342
343 case TemplateArgument::NullPtr:
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000344 LV.merge(arg.getNullPtrType()->getLinkageAndVisibility());
John McCalldf25c432013-02-16 00:17:33 +0000345 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000346
347 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000348 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000349 if (TemplateDecl *Template
John McCalldf25c432013-02-16 00:17:33 +0000350 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
351 LV.merge(getLVForDecl(Template, LVForValue));
352 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000353
354 case TemplateArgument::Pack:
John McCalldf25c432013-02-16 00:17:33 +0000355 LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray()));
356 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000357 }
John McCalldf25c432013-02-16 00:17:33 +0000358 llvm_unreachable("bad template argument kind");
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000359 }
360
John McCall457a04e2010-10-22 21:05:15 +0000361 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000362}
363
Rafael Espindola2f869a32012-01-14 00:30:36 +0000364static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000365getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
366 return getLVForTemplateArgumentList(TArgs.asArray());
John McCall8823c652010-08-13 08:35:10 +0000367}
368
John McCall5f46c482013-02-21 23:42:58 +0000369static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
370 const FunctionTemplateSpecializationInfo *specInfo) {
371 // Include visibility from the template parameters and arguments
372 // only if this is not an explicit instantiation or specialization
373 // with direct explicit visibility. (Implicit instantiations won't
374 // have a direct attribute.)
375 if (!specInfo->isExplicitInstantiationOrSpecialization())
376 return true;
377
378 return !fn->hasAttr<VisibilityAttr>();
379}
380
John McCalldf25c432013-02-16 00:17:33 +0000381/// Merge in template-related linkage and visibility for the given
382/// function template specialization.
383///
384/// We don't need a computation kind here because we can assume
385/// LVForValue.
John McCall5f46c482013-02-21 23:42:58 +0000386///
NAKAMURA Takumi62eae082013-02-22 04:06:28 +0000387/// \param[out] LV the computation to use for the parent
John McCall5f46c482013-02-21 23:42:58 +0000388static void
389mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
390 const FunctionTemplateSpecializationInfo *specInfo) {
391 bool considerVisibility =
392 shouldConsiderTemplateVisibility(fn, specInfo);
John McCalldf25c432013-02-16 00:17:33 +0000393
394 // Merge information from the template parameters.
John McCall5f46c482013-02-21 23:42:58 +0000395 FunctionTemplateDecl *temp = specInfo->getTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000396 LinkageInfo tempLV =
397 getLVForTemplateParameterList(temp->getTemplateParameters());
398 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
399
400 // Merge information from the template arguments.
401 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
402 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
403 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000404}
405
John McCall5f46c482013-02-21 23:42:58 +0000406/// Does the given declaration have a direct visibility attribute
407/// that would match the given rules?
408static bool hasDirectVisibilityAttribute(const NamedDecl *D,
409 LVComputationKind computation) {
410 switch (computation) {
411 case LVForType:
412 case LVForExplicitType:
413 if (D->hasAttr<TypeVisibilityAttr>())
414 return true;
415 // fallthrough
416 case LVForValue:
417 case LVForExplicitValue:
418 if (D->hasAttr<VisibilityAttr>())
419 return true;
420 return false;
421 }
422 llvm_unreachable("bad visibility computation kind");
423}
424
John McCalld041a9b2013-02-20 01:54:26 +0000425/// Should we consider visibility associated with the template
426/// arguments and parameters of the given class template specialization?
427static bool shouldConsiderTemplateVisibility(
428 const ClassTemplateSpecializationDecl *spec,
429 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000430 // Include visibility from the template parameters and arguments
431 // only if this is not an explicit instantiation or specialization
432 // with direct explicit visibility (and note that implicit
433 // instantiations won't have a direct attribute).
434 //
435 // Furthermore, we want to ignore template parameters and arguments
John McCalld041a9b2013-02-20 01:54:26 +0000436 // for an explicit specialization when computing the visibility of a
437 // member thereof with explicit visibility.
John McCalldf25c432013-02-16 00:17:33 +0000438 //
439 // This is a bit complex; let's unpack it.
440 //
441 // An explicit class specialization is an independent, top-level
442 // declaration. As such, if it or any of its members has an
443 // explicit visibility attribute, that must directly express the
444 // user's intent, and we should honor it. The same logic applies to
445 // an explicit instantiation of a member of such a thing.
John McCalld041a9b2013-02-20 01:54:26 +0000446
447 // Fast path: if this is not an explicit instantiation or
448 // specialization, we always want to consider template-related
449 // visibility restrictions.
450 if (!spec->isExplicitInstantiationOrSpecialization())
451 return true;
452
453 // This is the 'member thereof' check.
454 if (spec->isExplicitSpecialization() &&
455 hasExplicitVisibilityAlready(computation))
456 return false;
457
John McCall5f46c482013-02-21 23:42:58 +0000458 return !hasDirectVisibilityAttribute(spec, computation);
John McCalld041a9b2013-02-20 01:54:26 +0000459}
460
461/// Merge in template-related linkage and visibility for the given
462/// class template specialization.
463static void mergeTemplateLV(LinkageInfo &LV,
464 const ClassTemplateSpecializationDecl *spec,
465 LVComputationKind computation) {
466 bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
John McCalldf25c432013-02-16 00:17:33 +0000467
468 // Merge information from the template parameters, but ignore
469 // visibility if we're only considering template arguments.
470
John McCalld041a9b2013-02-20 01:54:26 +0000471 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000472 LinkageInfo tempLV =
473 getLVForTemplateParameterList(temp->getTemplateParameters());
474 LV.mergeMaybeWithVisibility(tempLV,
John McCalld041a9b2013-02-20 01:54:26 +0000475 considerVisibility && !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000476
477 // Merge information from the template arguments. We ignore
478 // template-argument visibility if we've got an explicit
479 // instantiation with a visibility attribute.
480 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
481 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
482 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000483}
484
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000485static bool useInlineVisibilityHidden(const NamedDecl *D) {
486 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000487 const LangOptions &Opts = D->getASTContext().getLangOpts();
488 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000489 return false;
490
491 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
492 if (!FD)
493 return false;
494
495 TemplateSpecializationKind TSK = TSK_Undeclared;
496 if (FunctionTemplateSpecializationInfo *spec
497 = FD->getTemplateSpecializationInfo()) {
498 TSK = spec->getTemplateSpecializationKind();
499 } else if (MemberSpecializationInfo *MSI =
500 FD->getMemberSpecializationInfo()) {
501 TSK = MSI->getTemplateSpecializationKind();
502 }
503
504 const FunctionDecl *Def = 0;
505 // InlineVisibilityHidden only applies to definitions, and
506 // isInlined() only gives meaningful answers on definitions
507 // anyway.
508 return TSK != TSK_ExplicitInstantiationDeclaration &&
509 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000510 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000511}
512
Rafael Espindola593537a2013-05-05 20:15:21 +0000513template <typename T> static bool isFirstInExternCContext(T *D) {
Rafael Espindolaf4187652013-02-14 01:18:37 +0000514 const T *First = D->getFirstDeclaration();
Rafael Espindola593537a2013-05-05 20:15:21 +0000515 return First->isInExternCContext();
Rafael Espindolaf4187652013-02-14 01:18:37 +0000516}
517
Rafael Espindola327be3c2013-04-26 01:30:23 +0000518static bool isSingleLineExternC(const Decl &D) {
519 if (const LinkageSpecDecl *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
520 if (SD->getLanguage() == LinkageSpecDecl::lang_c && !SD->hasBraces())
521 return true;
522 return false;
523}
524
Rafael Espindola3ae00052013-05-13 00:12:11 +0000525static bool isExternalLinkage(Linkage L) {
526 return L == UniqueExternalLinkage || L == ExternalLinkage;
527}
528
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000529static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCalldf25c432013-02-16 00:17:33 +0000530 LVComputationKind computation) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000531 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000532 "Not a name having namespace scope");
533 ASTContext &Context = D->getASTContext();
534
535 // C++ [basic.link]p3:
536 // A name having namespace scope (3.3.6) has internal linkage if it
537 // is the name of
538 // - an object, reference, function or function template that is
539 // explicitly declared static; or,
540 // (This bullet corresponds to C99 6.2.2p3.)
541 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
542 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000543 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000544 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000545
Richard Smithdc0ef452012-10-19 06:37:48 +0000546 // - a non-volatile object or reference that is explicitly declared const
547 // or constexpr and neither explicitly declared extern nor previously
548 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000549 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000550 Var->getType().isConstQualified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000551 !Var->getType().isVolatileQualified()) {
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000552 const VarDecl *PrevVar = Var->getPreviousDecl();
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000553 if (PrevVar)
Rafael Espindolaadea16b2013-04-03 15:50:00 +0000554 return PrevVar->getLinkageAndVisibility();
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000555
556 if (Var->getStorageClass() != SC_Extern &&
Rafael Espindola327be3c2013-04-26 01:30:23 +0000557 Var->getStorageClass() != SC_PrivateExtern &&
558 !isSingleLineExternC(*Var))
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000559 return LinkageInfo::internal();
560 }
561
562 for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar;
563 PrevVar = PrevVar->getPreviousDecl()) {
564 if (PrevVar->getStorageClass() == SC_PrivateExtern &&
565 Var->getStorageClass() == SC_None)
566 return PrevVar->getLinkageAndVisibility();
567 // Explicitly declared static.
568 if (PrevVar->getStorageClass() == SC_Static)
569 return LinkageInfo::internal();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000570 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000571 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000572 // C++ [temp]p4:
573 // A non-member function template can have internal linkage; any
574 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000575 const FunctionDecl *Function = 0;
576 if (const FunctionTemplateDecl *FunTmpl
577 = dyn_cast<FunctionTemplateDecl>(D))
578 Function = FunTmpl->getTemplatedDecl();
579 else
580 Function = cast<FunctionDecl>(D);
581
582 // Explicitly declared static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000583 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000584 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000585 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
586 // - a data member of an anonymous union.
587 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000588 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000589 }
590
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000591 if (D->isInAnonymousNamespace()) {
592 const VarDecl *Var = dyn_cast<VarDecl>(D);
593 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindola593537a2013-05-05 20:15:21 +0000594 if ((!Var || !isFirstInExternCContext(Var)) &&
595 (!Func || !isFirstInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000596 return LinkageInfo::uniqueExternal();
597 }
John McCallb7139c42010-10-28 04:18:25 +0000598
John McCall457a04e2010-10-22 21:05:15 +0000599 // Set up the defaults.
600
601 // C99 6.2.2p5:
602 // If the declaration of an identifier for an object has file
603 // scope and no storage-class specifier, its linkage is
604 // external.
John McCallc273f242010-10-30 11:50:40 +0000605 LinkageInfo LV;
606
John McCalld041a9b2013-02-20 01:54:26 +0000607 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000608 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000609 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000610 } else {
611 // If we're declared in a namespace with a visibility attribute,
John McCalldf25c432013-02-16 00:17:33 +0000612 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindola78158af2012-04-16 18:46:26 +0000613 for (const DeclContext *DC = D->getDeclContext();
614 !isa<TranslationUnitDecl>(DC);
615 DC = DC->getParent()) {
616 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
617 if (!ND) continue;
David Blaikie05785d12013-02-20 22:23:23 +0000618 if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000619 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000620 break;
621 }
622 }
623 }
Rafael Espindola78158af2012-04-16 18:46:26 +0000624
John McCalldf25c432013-02-16 00:17:33 +0000625 // Add in global settings if the above didn't give us direct visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000626 if (!LV.isVisibilityExplicit()) {
John McCallb4a99d32013-02-19 01:57:35 +0000627 // Use global type/value visibility as appropriate.
628 Visibility globalVisibility;
629 if (computation == LVForValue) {
630 globalVisibility = Context.getLangOpts().getValueVisibilityMode();
631 } else {
632 assert(computation == LVForType);
633 globalVisibility = Context.getLangOpts().getTypeVisibilityMode();
634 }
635 LV.mergeVisibility(globalVisibility, /*explicit*/ false);
John McCalldf25c432013-02-16 00:17:33 +0000636
637 // If we're paying attention to global visibility, apply
638 // -finline-visibility-hidden if this is an inline method.
639 if (useInlineVisibilityHidden(D))
640 LV.mergeVisibility(HiddenVisibility, true);
641 }
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000642 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000643
Douglas Gregorf73b2822009-11-25 22:24:25 +0000644 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000645
Douglas Gregorf73b2822009-11-25 22:24:25 +0000646 // A name having namespace scope has external linkage if it is the
647 // name of
648 //
649 // - an object or reference, unless it has internal linkage; or
650 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000651 // GCC applies the following optimization to variables and static
652 // data members, but not to functions:
653 //
John McCall457a04e2010-10-22 21:05:15 +0000654 // Modify the variable's LV by the LV of its type unless this is
655 // C or extern "C". This follows from [basic.link]p9:
656 // A type without linkage shall not be used as the type of a
657 // variable or function with external linkage unless
658 // - the entity has C language linkage, or
659 // - the entity is declared within an unnamed namespace, or
660 // - the entity is not used or is defined in the same
661 // translation unit.
662 // and [basic.link]p10:
663 // ...the types specified by all declarations referring to a
664 // given variable or function shall be identical...
665 // C does not have an equivalent rule.
666 //
John McCall5fe84122010-10-26 04:59:26 +0000667 // Ignore this if we've got an explicit attribute; the user
668 // probably knows what they're doing.
669 //
John McCall457a04e2010-10-22 21:05:15 +0000670 // Note that we don't want to make the variable non-external
671 // because of this, but unique-external linkage suits us.
Rafael Espindola593537a2013-05-05 20:15:21 +0000672 if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000673 LinkageInfo TypeLV = Var->getType()->getLinkageAndVisibility();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000674 if (TypeLV.getLinkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000675 return LinkageInfo::uniqueExternal();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000676 if (!LV.isVisibilityExplicit())
John McCalldf25c432013-02-16 00:17:33 +0000677 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000678 }
679
John McCall23032652010-11-02 18:38:13 +0000680 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000681 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000682
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000683 // Note that Sema::MergeVarDecl already takes care of implementing
684 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
685 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000686
Douglas Gregorf73b2822009-11-25 22:24:25 +0000687 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000688 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000689 // In theory, we can modify the function's LV by the LV of its
690 // type unless it has C linkage (see comment above about variables
691 // for justification). In practice, GCC doesn't do this, so it's
692 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000693
John McCall23032652010-11-02 18:38:13 +0000694 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000695 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000696
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000697 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
698 // merging storage classes and visibility attributes, so we don't have to
699 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000700
John McCallf768aa72011-02-10 06:50:24 +0000701 // In C++, then if the type of the function uses a type with
702 // unique-external linkage, it's not legally usable from outside
703 // this translation unit. However, we should use the C linkage
704 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000705 if (Context.getLangOpts().CPlusPlus &&
Richard Smith50f4afc2013-05-12 23:17:59 +0000706 !Function->isInExternCContext()) {
707 // Only look at the type-as-written. If this function has an auto-deduced
708 // return type, we can't compute the linkage of that type because it could
709 // require looking at the linkage of this function, and we don't need this
710 // for correctness because the type is not part of the function's
711 // signature.
712 // FIXME: This is a hack. We should be able to solve this circularity some
713 // other way.
714 QualType TypeAsWritten = Function->getType();
715 if (TypeSourceInfo *TSI = Function->getTypeSourceInfo())
716 TypeAsWritten = TSI->getType();
717 if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
718 return LinkageInfo::uniqueExternal();
719 }
John McCallf768aa72011-02-10 06:50:24 +0000720
John McCall5f46c482013-02-21 23:42:58 +0000721 // Consider LV from the template and the template arguments.
722 // We're at file scope, so we do not need to worry about nested
723 // specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000724 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000725 = Function->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000726 mergeTemplateLV(LV, Function, specInfo);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000727 }
728
Douglas Gregorf73b2822009-11-25 22:24:25 +0000729 // - a named class (Clause 9), or an unnamed class defined in a
730 // typedef declaration in which the class has the typedef name
731 // for linkage purposes (7.1.3); or
732 // - a named enumeration (7.2), or an unnamed enumeration
733 // defined in a typedef declaration in which the enumeration
734 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000735 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
736 // Unnamed tags have no linkage.
John McCall5ea95772013-03-09 00:54:27 +0000737 if (!Tag->hasNameForLinkage())
John McCallc273f242010-10-30 11:50:40 +0000738 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000739
John McCall457a04e2010-10-22 21:05:15 +0000740 // If this is a class template specialization, consider the
John McCall5f46c482013-02-21 23:42:58 +0000741 // linkage of the template and template arguments. We're at file
742 // scope, so we do not need to worry about nested specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000743 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000744 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCalldf25c432013-02-16 00:17:33 +0000745 mergeTemplateLV(LV, spec, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000746 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000747
748 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000749 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000750 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCalldf25c432013-02-16 00:17:33 +0000751 computation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000752 if (!isExternalLinkage(EnumLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000753 return LinkageInfo::none();
754 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000755
756 // - a template, unless it is a function template that has
757 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000758 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
John McCalld041a9b2013-02-20 01:54:26 +0000759 bool considerVisibility = !hasExplicitVisibilityAlready(computation);
John McCalldf25c432013-02-16 00:17:33 +0000760 LinkageInfo tempLV =
761 getLVForTemplateParameterList(temp->getTemplateParameters());
762 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
763
Douglas Gregorf73b2822009-11-25 22:24:25 +0000764 // - a namespace (7.3), unless it is declared within an unnamed
765 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000766 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
767 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000768
John McCall457a04e2010-10-22 21:05:15 +0000769 // By extension, we assign external linkage to Objective-C
770 // interfaces.
771 } else if (isa<ObjCInterfaceDecl>(D)) {
772 // fallout
773
774 // Everything not covered here has no linkage.
775 } else {
John McCallc273f242010-10-30 11:50:40 +0000776 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000777 }
778
779 // If we ended up with non-external linkage, visibility should
780 // always be default.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000781 if (LV.getLinkage() != ExternalLinkage)
782 return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000783
John McCall457a04e2010-10-22 21:05:15 +0000784 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000785}
786
John McCalldf25c432013-02-16 00:17:33 +0000787static LinkageInfo getLVForClassMember(const NamedDecl *D,
788 LVComputationKind computation) {
John McCall457a04e2010-10-22 21:05:15 +0000789 // Only certain class members have linkage. Note that fields don't
790 // really have linkage, but it's convenient to say they do for the
791 // purposes of calculating linkage of pointer-to-data-member
792 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000793 if (!(isa<CXXMethodDecl>(D) ||
794 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000795 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000796 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000797 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000798
John McCall07072662010-11-02 01:45:15 +0000799 LinkageInfo LV;
800
John McCall07072662010-11-02 01:45:15 +0000801 // If we have an explicit visibility attribute, merge that in.
John McCalld041a9b2013-02-20 01:54:26 +0000802 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000803 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000804 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000805 // If we're paying attention to global visibility, apply
806 // -finline-visibility-hidden if this is an inline method.
807 //
808 // Note that we do this before merging information about
809 // the class visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000810 if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000811 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000812 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000813
814 // If this class member has an explicit visibility attribute, the only
815 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000816 // only look for them when processing the class.
John McCalld041a9b2013-02-20 01:54:26 +0000817 LVComputationKind classComputation = computation;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000818 if (LV.isVisibilityExplicit())
John McCalld041a9b2013-02-20 01:54:26 +0000819 classComputation = withExplicitVisibilityAlready(computation);
Rafael Espindola505a7c82012-04-16 18:25:01 +0000820
John McCall5f46c482013-02-21 23:42:58 +0000821 LinkageInfo classLV =
822 getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000823 if (!isExternalLinkage(classLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000824 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000825
826 // If the class already has unique-external linkage, we can't improve.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000827 if (classLV.getLinkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000828 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000829
John McCall5f46c482013-02-21 23:42:58 +0000830 // Otherwise, don't merge in classLV yet, because in certain cases
831 // we need to completely ignore the visibility from it.
832
833 // Specifically, if this decl exists and has an explicit attribute.
834 const NamedDecl *explicitSpecSuppressor = 0;
835
John McCall8823c652010-08-13 08:35:10 +0000836 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000837 // If the type of the function uses a type with unique-external
838 // linkage, it's not legally usable from outside this translation unit.
839 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
840 return LinkageInfo::uniqueExternal();
841
John McCall457a04e2010-10-22 21:05:15 +0000842 // If this is a method template specialization, use the linkage for
843 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000844 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000845 = MD->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000846 mergeTemplateLV(LV, MD, spec);
John McCall5f46c482013-02-21 23:42:58 +0000847 if (spec->isExplicitSpecialization()) {
848 explicitSpecSuppressor = MD;
849 } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
850 explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
851 }
852 } else if (isExplicitMemberSpecialization(MD)) {
853 explicitSpecSuppressor = MD;
John McCalle6e622e2010-11-01 01:29:57 +0000854 }
John McCall457a04e2010-10-22 21:05:15 +0000855
John McCall37bb6c92010-10-29 22:22:43 +0000856 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000857 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000858 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCalldf25c432013-02-16 00:17:33 +0000859 mergeTemplateLV(LV, spec, computation);
John McCall5f46c482013-02-21 23:42:58 +0000860 if (spec->isExplicitSpecialization()) {
861 explicitSpecSuppressor = spec;
862 } else {
863 const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
864 if (isExplicitMemberSpecialization(temp)) {
865 explicitSpecSuppressor = temp->getTemplatedDecl();
866 }
867 }
868 } else if (isExplicitMemberSpecialization(RD)) {
869 explicitSpecSuppressor = RD;
John McCall37bb6c92010-10-29 22:22:43 +0000870 }
871
John McCall37bb6c92010-10-29 22:22:43 +0000872 // Static data members.
873 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000874 // Modify the variable's linkage by its type, but ignore the
875 // type's visibility unless it's a definition.
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000876 LinkageInfo typeLV = VD->getType()->getLinkageAndVisibility();
John McCall5f46c482013-02-21 23:42:58 +0000877 LV.mergeMaybeWithVisibility(typeLV,
Rafael Espindola4a5da442013-02-27 02:56:45 +0000878 !LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit());
John McCall5f46c482013-02-21 23:42:58 +0000879
880 if (isExplicitMemberSpecialization(VD)) {
881 explicitSpecSuppressor = VD;
882 }
John McCalldf25c432013-02-16 00:17:33 +0000883
884 // Template members.
885 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
886 bool considerVisibility =
Rafael Espindola4a5da442013-02-27 02:56:45 +0000887 (!LV.isVisibilityExplicit() &&
888 !classLV.isVisibilityExplicit() &&
John McCalld041a9b2013-02-20 01:54:26 +0000889 !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000890 LinkageInfo tempLV =
891 getLVForTemplateParameterList(temp->getTemplateParameters());
892 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall5f46c482013-02-21 23:42:58 +0000893
894 if (const RedeclarableTemplateDecl *redeclTemp =
895 dyn_cast<RedeclarableTemplateDecl>(temp)) {
896 if (isExplicitMemberSpecialization(redeclTemp)) {
897 explicitSpecSuppressor = temp->getTemplatedDecl();
898 }
899 }
John McCall37bb6c92010-10-29 22:22:43 +0000900 }
901
John McCall5f46c482013-02-21 23:42:58 +0000902 // We should never be looking for an attribute directly on a template.
903 assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
904
905 // If this member is an explicit member specialization, and it has
906 // an explicit attribute, ignore visibility from the parent.
907 bool considerClassVisibility = true;
908 if (explicitSpecSuppressor &&
Rafael Espindola4a5da442013-02-27 02:56:45 +0000909 // optimization: hasDVA() is true only with explicit visibility.
910 LV.isVisibilityExplicit() &&
911 classLV.getVisibility() != DefaultVisibility &&
John McCall5f46c482013-02-21 23:42:58 +0000912 hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) {
913 considerClassVisibility = false;
914 }
915
916 // Finally, merge in information from the class.
917 LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
John McCall457a04e2010-10-22 21:05:15 +0000918 return LV;
John McCall8823c652010-08-13 08:35:10 +0000919}
920
David Blaikie68e081d2011-12-20 02:48:34 +0000921void NamedDecl::anchor() { }
922
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000923bool NamedDecl::isLinkageValid() const {
924 if (!HasCachedLinkage)
925 return true;
John McCalld396b972011-02-08 19:01:05 +0000926
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000927 return getLVForDecl(this, LVForExplicitValue).getLinkage() ==
928 Linkage(CachedLinkage);
John McCalld396b972011-02-08 19:01:05 +0000929}
930
Rafael Espindola3ae00052013-05-13 00:12:11 +0000931Linkage NamedDecl::getLinkageInternal() const {
Richard Smith88581592013-02-12 05:48:23 +0000932 if (HasCachedLinkage)
Rafael Espindola19de5612013-01-12 06:42:30 +0000933 return Linkage(CachedLinkage);
Rafael Espindola19de5612013-01-12 06:42:30 +0000934
John McCalld041a9b2013-02-20 01:54:26 +0000935 // We don't care about visibility here, so ask for the cheapest
936 // possible visibility analysis.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000937 CachedLinkage = getLVForDecl(this, LVForExplicitValue).getLinkage();
Rafael Espindola19de5612013-01-12 06:42:30 +0000938 HasCachedLinkage = 1;
939
940#ifndef NDEBUG
941 verifyLinkage();
942#endif
943
944 return Linkage(CachedLinkage);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000945}
946
John McCallc273f242010-10-30 11:50:40 +0000947LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCalldf25c432013-02-16 00:17:33 +0000948 LVComputationKind computation =
949 (usesTypeVisibility(this) ? LVForType : LVForValue);
950 LinkageInfo LI = getLVForDecl(this, computation);
Rafael Espindola19de5612013-01-12 06:42:30 +0000951 if (HasCachedLinkage) {
Rafael Espindola4a5da442013-02-27 02:56:45 +0000952 assert(Linkage(CachedLinkage) == LI.getLinkage());
Rafael Espindola19de5612013-01-12 06:42:30 +0000953 return LI;
Rafael Espindola54606d52012-12-25 07:31:49 +0000954 }
Rafael Espindola19de5612013-01-12 06:42:30 +0000955 HasCachedLinkage = 1;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000956 CachedLinkage = LI.getLinkage();
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000957
958#ifndef NDEBUG
Rafael Espindola19de5612013-01-12 06:42:30 +0000959 verifyLinkage();
960#endif
961
962 return LI;
963}
964
965void NamedDecl::verifyLinkage() const {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000966 // In C (because of gnu inline) and in c++ with microsoft extensions an
967 // static can follow an extern, so we can have two decls with different
968 // linkages.
969 const LangOptions &Opts = getASTContext().getLangOpts();
970 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
Rafael Espindola19de5612013-01-12 06:42:30 +0000971 return;
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000972
973 // We have just computed the linkage for this decl. By induction we know
974 // that all other computed linkages match, check that the one we just computed
975 // also does.
976 NamedDecl *D = NULL;
977 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
978 NamedDecl *T = cast<NamedDecl>(*I);
979 if (T == this)
980 continue;
Rafael Espindola19de5612013-01-12 06:42:30 +0000981 if (T->HasCachedLinkage != 0) {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000982 D = T;
983 break;
984 }
985 }
986 assert(!D || D->CachedLinkage == CachedLinkage);
John McCall033caa52010-10-29 00:29:13 +0000987}
Ted Kremenek926d8602010-04-20 23:15:35 +0000988
David Blaikie05785d12013-02-20 22:23:23 +0000989Optional<Visibility>
John McCalld041a9b2013-02-20 01:54:26 +0000990NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
Rafael Espindola3a52c442013-02-26 19:33:14 +0000991 // Check the declaration itself first.
992 if (Optional<Visibility> V = getVisibilityOf(this, kind))
993 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000994
Rafael Espindola3a52c442013-02-26 19:33:14 +0000995 // If this is a member class of a specialization of a class template
996 // and the corresponding decl has explicit visibility, use that.
997 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
998 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
999 if (InstantiatedFrom)
1000 return getVisibilityOf(InstantiatedFrom, kind);
1001 }
1002
1003 // If there wasn't explicit visibility there, and this is a
1004 // specialization of a class template, check for visibility
1005 // on the pattern.
1006 if (const ClassTemplateSpecializationDecl *spec
1007 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1008 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(),
1009 kind);
1010
1011 // Use the most recent declaration.
1012 const NamedDecl *MostRecent = cast<NamedDecl>(this->getMostRecentDecl());
1013 if (MostRecent != this)
1014 return MostRecent->getExplicitVisibility(kind);
1015
1016 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola96e68242012-05-16 02:10:38 +00001017 if (Var->isStaticDataMember()) {
1018 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
1019 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +00001020 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola96e68242012-05-16 02:10:38 +00001021 }
1022
David Blaikie7a30dc52013-02-21 01:47:18 +00001023 return None;
Rafael Espindola96e68242012-05-16 02:10:38 +00001024 }
Rafael Espindola3a52c442013-02-26 19:33:14 +00001025 // Also handle function template specializations.
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001026 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001027 // If the function is a specialization of a template with an
1028 // explicit visibility attribute, use that.
1029 if (FunctionTemplateSpecializationInfo *templateInfo
1030 = fn->getTemplateSpecializationInfo())
John McCalld041a9b2013-02-20 01:54:26 +00001031 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
1032 kind);
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001033
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001034 // If the function is a member of a specialization of a class template
1035 // and the corresponding decl has explicit visibility, use that.
1036 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
1037 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +00001038 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001039
David Blaikie7a30dc52013-02-21 01:47:18 +00001040 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001041 }
1042
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001043 // The visibility of a template is stored in the templated decl.
1044 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
John McCalld041a9b2013-02-20 01:54:26 +00001045 return getVisibilityOf(TD->getTemplatedDecl(), kind);
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001046
David Blaikie7a30dc52013-02-21 01:47:18 +00001047 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001048}
1049
John McCalldf25c432013-02-16 00:17:33 +00001050static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
1051 LVComputationKind computation) {
1052 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1053 if (Function->isInAnonymousNamespace() &&
Rafael Espindola593537a2013-05-05 20:15:21 +00001054 !Function->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001055 return LinkageInfo::uniqueExternal();
1056
1057 // This is a "void f();" which got merged with a file static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001058 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCalldf25c432013-02-16 00:17:33 +00001059 return LinkageInfo::internal();
1060
1061 LinkageInfo LV;
John McCalld041a9b2013-02-20 01:54:26 +00001062 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001063 if (Optional<Visibility> Vis =
1064 getExplicitVisibility(Function, computation))
John McCalldf25c432013-02-16 00:17:33 +00001065 LV.mergeVisibility(*Vis, true);
1066 }
1067
1068 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1069 // merging storage classes and visibility attributes, so we don't have to
1070 // look at previous decls in here.
1071
1072 return LV;
1073 }
1074
1075 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001076 if (Var->hasExternalStorage()) {
Rafael Espindola593537a2013-05-05 20:15:21 +00001077 if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001078 return LinkageInfo::uniqueExternal();
1079
John McCalldf25c432013-02-16 00:17:33 +00001080 LinkageInfo LV;
1081 if (Var->getStorageClass() == SC_PrivateExtern)
1082 LV.mergeVisibility(HiddenVisibility, true);
John McCalld041a9b2013-02-20 01:54:26 +00001083 else if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001084 if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
John McCalldf25c432013-02-16 00:17:33 +00001085 LV.mergeVisibility(*Vis, true);
1086 }
1087
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001088 if (const VarDecl *Prev = Var->getPreviousDecl()) {
1089 LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1090 if (PrevLV.getLinkage())
1091 LV.setLinkage(PrevLV.getLinkage());
1092 LV.mergeVisibility(PrevLV);
1093 }
1094
John McCalldf25c432013-02-16 00:17:33 +00001095 return LV;
1096 }
1097 }
1098
1099 return LinkageInfo::none();
1100}
1101
1102static LinkageInfo getLVForDecl(const NamedDecl *D,
1103 LVComputationKind computation) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001104 // Objective-C: treat all Objective-C declarations as having external
1105 // linkage.
John McCall033caa52010-10-29 00:29:13 +00001106 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001107 default:
1108 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +00001109 case Decl::ParmVar:
1110 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001111 case Decl::TemplateTemplateParm: // count these as external
1112 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +00001113 case Decl::ObjCAtDefsField:
1114 case Decl::ObjCCategory:
1115 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +00001116 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +00001117 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +00001118 case Decl::ObjCMethod:
1119 case Decl::ObjCProperty:
1120 case Decl::ObjCPropertyImpl:
1121 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +00001122 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001123
1124 case Decl::CXXRecord: {
1125 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
1126 if (Record->isLambda()) {
1127 if (!Record->getLambdaManglingNumber()) {
1128 // This lambda has no mangling number, so it's internal.
1129 return LinkageInfo::internal();
1130 }
1131
1132 // This lambda has its linkage/visibility determined by its owner.
1133 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
1134 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
1135 if (isa<ParmVarDecl>(ContextDecl))
1136 DC = ContextDecl->getDeclContext()->getRedeclContext();
1137 else
John McCalldf25c432013-02-16 00:17:33 +00001138 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001139 }
1140
1141 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
John McCalldf25c432013-02-16 00:17:33 +00001142 return getLVForDecl(ND, computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001143
1144 return LinkageInfo::external();
1145 }
1146
1147 break;
1148 }
Ted Kremenek926d8602010-04-20 23:15:35 +00001149 }
1150
Douglas Gregorf73b2822009-11-25 22:24:25 +00001151 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +00001152 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCalldf25c432013-02-16 00:17:33 +00001153 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001154
1155 // C++ [basic.link]p5:
1156 // In addition, a member function, static data member, a named
1157 // class or enumeration of class scope, or an unnamed class or
1158 // enumeration defined in a class-scope typedef declaration such
1159 // that the class or enumeration has the typedef name for linkage
1160 // purposes (7.1.3), has external linkage if the name of the class
1161 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +00001162 if (D->getDeclContext()->isRecord())
John McCalldf25c432013-02-16 00:17:33 +00001163 return getLVForClassMember(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001164
1165 // C++ [basic.link]p6:
1166 // The name of a function declared in block scope and the name of
1167 // an object declared by a block scope extern declaration have
1168 // linkage. If there is a visible declaration of an entity with
1169 // linkage having the same name and type, ignoring entities
1170 // declared outside the innermost enclosing namespace scope, the
1171 // block scope declaration declares that same entity and receives
1172 // the linkage of the previous declaration. If there is more than
1173 // one such matching entity, the program is ill-formed. Otherwise,
1174 // if no matching entity is found, the block scope entity receives
1175 // external linkage.
John McCalldf25c432013-02-16 00:17:33 +00001176 if (D->getDeclContext()->isFunctionOrMethod())
1177 return getLVForLocalDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001178
1179 // C++ [basic.link]p6:
1180 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +00001181 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001182}
Douglas Gregorf73b2822009-11-25 22:24:25 +00001183
Douglas Gregor2ada0482009-02-04 17:27:36 +00001184std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +00001185 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +00001186}
1187
1188std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001189 std::string QualName;
1190 llvm::raw_string_ostream OS(QualName);
1191 printQualifiedName(OS, P);
1192 return OS.str();
1193}
1194
1195void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1196 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1197}
1198
1199void NamedDecl::printQualifiedName(raw_ostream &OS,
1200 const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +00001201 const DeclContext *Ctx = getDeclContext();
1202
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001203 if (Ctx->isFunctionOrMethod()) {
1204 printName(OS);
1205 return;
1206 }
Douglas Gregor2ada0482009-02-04 17:27:36 +00001207
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001208 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001209 ContextsTy Contexts;
1210
1211 // Collect contexts.
1212 while (Ctx && isa<NamedDecl>(Ctx)) {
1213 Contexts.push_back(Ctx);
1214 Ctx = Ctx->getParent();
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001215 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001216
1217 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1218 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001219 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001220 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Benjamin Kramer9170e912013-02-22 15:46:01 +00001221 OS << Spec->getName();
Douglas Gregor85673582009-05-18 17:01:57 +00001222 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001223 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1224 TemplateArgs.data(),
1225 TemplateArgs.size(),
1226 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001227 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +00001228 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001229 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +00001230 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001231 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001232 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1233 if (!RD->getIdentifier())
1234 OS << "<anonymous " << RD->getKindName() << '>';
1235 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001236 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001237 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +00001238 const FunctionProtoType *FT = 0;
1239 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001240 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001241
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001242 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001243 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001244 unsigned NumParams = FD->getNumParams();
1245 for (unsigned i = 0; i < NumParams; ++i) {
1246 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001247 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001248 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001249 }
1250
1251 if (FT->isVariadic()) {
1252 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001253 OS << ", ";
1254 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001255 }
1256 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001257 OS << ')';
1258 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001259 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001260 }
1261 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001262 }
1263
John McCalla2a3f7d2010-03-16 21:48:18 +00001264 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001265 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001266 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001267 OS << "<anonymous>";
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001268}
Douglas Gregor2ada0482009-02-04 17:27:36 +00001269
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001270void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1271 const PrintingPolicy &Policy,
1272 bool Qualified) const {
1273 if (Qualified)
1274 printQualifiedName(OS, Policy);
1275 else
1276 printName(OS);
Douglas Gregor2ada0482009-02-04 17:27:36 +00001277}
1278
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001279bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001280 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1281
Douglas Gregor889ceb72009-02-03 19:21:40 +00001282 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1283 // We want to keep it, unless it nominates same namespace.
1284 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +00001285 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1286 ->getOriginalNamespace() ==
1287 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1288 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +00001289 }
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001291 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1292 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +00001293 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001294
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001295 // For function templates, the underlying function declarations are linked.
1296 if (const FunctionTemplateDecl *FunctionTemplate
1297 = dyn_cast<FunctionTemplateDecl>(this))
1298 if (const FunctionTemplateDecl *OldFunctionTemplate
1299 = dyn_cast<FunctionTemplateDecl>(OldD))
1300 return FunctionTemplate->getTemplatedDecl()
1301 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001302
Steve Naroffc4173fa2009-02-22 19:35:57 +00001303 // For method declarations, we keep track of redeclarations.
1304 if (isa<ObjCMethodDecl>(this))
1305 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001306
John McCall9f3059a2009-10-09 21:13:30 +00001307 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1308 return true;
1309
John McCall3f746822009-11-17 05:59:44 +00001310 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1311 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1312 cast<UsingShadowDecl>(OldD)->getTargetDecl();
1313
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001314 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1315 ASTContext &Context = getASTContext();
1316 return Context.getCanonicalNestedNameSpecifier(
1317 cast<UsingDecl>(this)->getQualifier()) ==
1318 Context.getCanonicalNestedNameSpecifier(
1319 cast<UsingDecl>(OldD)->getQualifier());
1320 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +00001321
Douglas Gregorb59643b2012-01-03 23:26:26 +00001322 // A typedef of an Objective-C class type can replace an Objective-C class
1323 // declaration or definition, and vice versa.
1324 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1325 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1326 return true;
1327
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001328 // For non-function declarations, if the declarations are of the
1329 // same kind then this must be a redeclaration, or semantic analysis
1330 // would not have given us the new declaration.
1331 return this->getKind() == OldD->getKind();
1332}
1333
Douglas Gregoreddf4332009-02-24 20:03:32 +00001334bool NamedDecl::hasLinkage() const {
Rafael Espindola3ae00052013-05-13 00:12:11 +00001335 return getLinkageInternal() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001336}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001337
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001338NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001339 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001340 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1341 ND = UD->getTargetDecl();
1342
1343 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1344 return AD->getClassInterface();
1345
1346 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001347}
1348
John McCalla8ae2222010-04-06 21:38:20 +00001349bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001350 if (!isCXXClassMember())
1351 return false;
1352
John McCalla8ae2222010-04-06 21:38:20 +00001353 const NamedDecl *D = this;
1354 if (isa<UsingShadowDecl>(D))
1355 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1356
John McCall5e77d762013-04-16 07:28:30 +00001357 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001358 return true;
1359 if (isa<CXXMethodDecl>(D))
1360 return cast<CXXMethodDecl>(D)->isInstance();
1361 if (isa<FunctionTemplateDecl>(D))
1362 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1363 ->getTemplatedDecl())->isInstance();
1364 return false;
1365}
1366
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001367//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001368// DeclaratorDecl Implementation
1369//===----------------------------------------------------------------------===//
1370
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001371template <typename DeclT>
1372static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1373 if (decl->getNumTemplateParameterLists() > 0)
1374 return decl->getTemplateParameterList(0)->getTemplateLoc();
1375 else
1376 return decl->getInnerLocStart();
1377}
1378
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001379SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001380 TypeSourceInfo *TSI = getTypeSourceInfo();
1381 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001382 return SourceLocation();
1383}
1384
Douglas Gregor14454802011-02-25 02:25:35 +00001385void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1386 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001387 // Make sure the extended decl info is allocated.
1388 if (!hasExtInfo()) {
1389 // Save (non-extended) type source info pointer.
1390 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1391 // Allocate external info struct.
1392 DeclInfo = new (getASTContext()) ExtInfo;
1393 // Restore savedTInfo into (extended) decl info.
1394 getExtInfo()->TInfo = savedTInfo;
1395 }
1396 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001397 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001398 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001399 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001400 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001401 if (getExtInfo()->NumTemplParamLists == 0) {
1402 // Save type source info pointer.
1403 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1404 // Deallocate the extended decl info.
1405 getASTContext().Deallocate(getExtInfo());
1406 // Restore savedTInfo into (non-extended) decl info.
1407 DeclInfo = savedTInfo;
1408 }
1409 else
1410 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001411 }
1412 }
1413}
1414
Abramo Bagnara60804e12011-03-18 15:16:37 +00001415void
1416DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1417 unsigned NumTPLists,
1418 TemplateParameterList **TPLists) {
1419 assert(NumTPLists > 0);
1420 // Make sure the extended decl info is allocated.
1421 if (!hasExtInfo()) {
1422 // Save (non-extended) type source info pointer.
1423 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1424 // Allocate external info struct.
1425 DeclInfo = new (getASTContext()) ExtInfo;
1426 // Restore savedTInfo into (extended) decl info.
1427 getExtInfo()->TInfo = savedTInfo;
1428 }
1429 // Set the template parameter lists info.
1430 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1431}
1432
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001433SourceLocation DeclaratorDecl::getOuterLocStart() const {
1434 return getTemplateOrInnerLocStart(this);
1435}
1436
Abramo Bagnaraea947882011-03-08 16:41:52 +00001437namespace {
1438
1439// Helper function: returns true if QT is or contains a type
1440// having a postfix component.
1441bool typeIsPostfix(clang::QualType QT) {
1442 while (true) {
1443 const Type* T = QT.getTypePtr();
1444 switch (T->getTypeClass()) {
1445 default:
1446 return false;
1447 case Type::Pointer:
1448 QT = cast<PointerType>(T)->getPointeeType();
1449 break;
1450 case Type::BlockPointer:
1451 QT = cast<BlockPointerType>(T)->getPointeeType();
1452 break;
1453 case Type::MemberPointer:
1454 QT = cast<MemberPointerType>(T)->getPointeeType();
1455 break;
1456 case Type::LValueReference:
1457 case Type::RValueReference:
1458 QT = cast<ReferenceType>(T)->getPointeeType();
1459 break;
1460 case Type::PackExpansion:
1461 QT = cast<PackExpansionType>(T)->getPattern();
1462 break;
1463 case Type::Paren:
1464 case Type::ConstantArray:
1465 case Type::DependentSizedArray:
1466 case Type::IncompleteArray:
1467 case Type::VariableArray:
1468 case Type::FunctionProto:
1469 case Type::FunctionNoProto:
1470 return true;
1471 }
1472 }
1473}
1474
1475} // namespace
1476
1477SourceRange DeclaratorDecl::getSourceRange() const {
1478 SourceLocation RangeEnd = getLocation();
1479 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1480 if (typeIsPostfix(TInfo->getType()))
1481 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1482 }
1483 return SourceRange(getOuterLocStart(), RangeEnd);
1484}
1485
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001486void
Douglas Gregor20527e22010-06-15 17:44:38 +00001487QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1488 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001489 TemplateParameterList **TPLists) {
1490 assert((NumTPLists == 0 || TPLists != 0) &&
1491 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001492
1493 // Free previous template parameters (if any).
1494 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001495 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001496 TemplParamLists = 0;
1497 NumTemplParamLists = 0;
1498 }
1499 // Set info on matched template parameter lists (if any).
1500 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001501 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001502 NumTemplParamLists = NumTPLists;
1503 for (unsigned i = NumTPLists; i-- > 0; )
1504 TemplParamLists[i] = TPLists[i];
1505 }
1506}
1507
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001508//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001509// VarDecl Implementation
1510//===----------------------------------------------------------------------===//
1511
Sebastian Redl833ef452010-01-26 22:01:41 +00001512const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1513 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001514 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001515 case SC_Auto: return "auto";
1516 case SC_Extern: return "extern";
1517 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1518 case SC_PrivateExtern: return "__private_extern__";
1519 case SC_Register: return "register";
1520 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001521 }
1522
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001523 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001524}
1525
Abramo Bagnaradff19302011-03-08 08:55:46 +00001526VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1527 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001528 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001529 StorageClass S) {
1530 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001531}
1532
Douglas Gregor72172e92012-01-05 21:55:30 +00001533VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1534 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1535 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001536 QualType(), 0, SC_None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001537}
1538
Douglas Gregorbf62d642010-12-06 18:36:25 +00001539void VarDecl::setStorageClass(StorageClass SC) {
1540 assert(isLegalForVariable(SC));
John McCallbeaa11c2011-05-01 02:13:58 +00001541 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001542}
1543
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001544SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001545 if (const Expr *Init = getInit()) {
1546 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001547 // If Init is implicit, ignore its source range and fallback on
1548 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1549 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001550 return SourceRange(getOuterLocStart(), InitEnd);
1551 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001552 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001553}
1554
Rafael Espindola88510672013-01-04 21:18:45 +00001555template<typename T>
Rafael Espindolaf4187652013-02-14 01:18:37 +00001556static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001557 // C++ [dcl.link]p1: All function types, function names with external linkage,
1558 // and variable names with external linkage have a language linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +00001559 if (!D.hasExternalFormalLinkage())
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001560 return NoLanguageLinkage;
1561
1562 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001563 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001564 ASTContext &Context = D.getASTContext();
1565 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001566 return CLanguageLinkage;
1567
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001568 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1569 // language linkage of the names of class members and the function type of
1570 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001571 const DeclContext *DC = D.getDeclContext();
1572 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001573 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001574
1575 // If the first decl is in an extern "C" context, any other redeclaration
1576 // will have C language linkage. If the first one is not in an extern "C"
1577 // context, we would have reported an error for any other decl being in one.
Rafael Espindola593537a2013-05-05 20:15:21 +00001578 if (isFirstInExternCContext(&D))
Rafael Espindolaf4187652013-02-14 01:18:37 +00001579 return CLanguageLinkage;
1580 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001581}
1582
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001583template<typename T>
1584static bool isExternCTemplate(const T &D) {
1585 // Since the context is ignored for class members, they can only have C++
1586 // language linkage or no language linkage.
1587 const DeclContext *DC = D.getDeclContext();
1588 if (DC->isRecord()) {
1589 assert(D.getASTContext().getLangOpts().CPlusPlus);
1590 return false;
1591 }
1592
1593 return D.getLanguageLinkage() == CLanguageLinkage;
1594}
1595
Rafael Espindolaf4187652013-02-14 01:18:37 +00001596LanguageLinkage VarDecl::getLanguageLinkage() const {
1597 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001598}
1599
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001600bool VarDecl::isExternC() const {
1601 return isExternCTemplate(*this);
1602}
1603
Rafael Espindola593537a2013-05-05 20:15:21 +00001604static bool isLinkageSpecContext(const DeclContext *DC,
1605 LinkageSpecDecl::LanguageIDs ID) {
1606 while (DC->getDeclKind() != Decl::TranslationUnit) {
1607 if (DC->getDeclKind() == Decl::LinkageSpec)
1608 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1609 DC = DC->getParent();
1610 }
1611 return false;
1612}
1613
1614template <typename T>
1615static bool isInLanguageSpecContext(T *D, LinkageSpecDecl::LanguageIDs ID) {
1616 return isLinkageSpecContext(D->getLexicalDeclContext(), ID);
1617}
1618
1619bool VarDecl::isInExternCContext() const {
1620 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
1621}
1622
1623bool VarDecl::isInExternCXXContext() const {
1624 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
1625}
1626
Sebastian Redl833ef452010-01-26 22:01:41 +00001627VarDecl *VarDecl::getCanonicalDecl() {
1628 return getFirstDeclaration();
1629}
1630
Daniel Dunbar9d355812012-03-09 01:51:51 +00001631VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1632 ASTContext &C) const
1633{
Sebastian Redl35351a92010-01-31 22:27:38 +00001634 // C++ [basic.def]p2:
1635 // A declaration is a definition unless [...] it contains the 'extern'
1636 // specifier or a linkage-specification and neither an initializer [...],
1637 // it declares a static data member in a class declaration [...].
1638 // C++ [temp.expl.spec]p15:
1639 // An explicit specialization of a static data member of a template is a
1640 // definition if the declaration includes an initializer; otherwise, it is
1641 // a declaration.
1642 if (isStaticDataMember()) {
1643 if (isOutOfLine() && (hasInit() ||
1644 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1645 return Definition;
1646 else
1647 return DeclarationOnly;
1648 }
1649 // C99 6.7p5:
1650 // A definition of an identifier is a declaration for that identifier that
1651 // [...] causes storage to be reserved for that object.
1652 // Note: that applies for all non-file-scope objects.
1653 // C99 6.9.2p1:
1654 // If the declaration of an identifier for an object has file scope and an
1655 // initializer, the declaration is an external definition for the identifier
1656 if (hasInit())
1657 return Definition;
Rafael Espindolabff59562013-04-25 12:11:36 +00001658
Sebastian Redl35351a92010-01-31 22:27:38 +00001659 if (hasExternalStorage())
1660 return DeclarationOnly;
Rafael Espindola8f326a52013-03-07 01:42:44 +00001661
Rafael Espindolabff59562013-04-25 12:11:36 +00001662 // [dcl.link] p7:
1663 // A declaration directly contained in a linkage-specification is treated
1664 // as if it contains the extern specifier for the purpose of determining
1665 // the linkage of the declared name and whether it is a definition.
Rafael Espindola327be3c2013-04-26 01:30:23 +00001666 if (isSingleLineExternC(*this))
1667 return DeclarationOnly;
Rafael Espindolabff59562013-04-25 12:11:36 +00001668
Sebastian Redl35351a92010-01-31 22:27:38 +00001669 // C99 6.9.2p2:
1670 // A declaration of an object that has file scope without an initializer,
1671 // and without a storage class specifier or the scs 'static', constitutes
1672 // a tentative definition.
1673 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001674 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001675 return TentativeDefinition;
1676
1677 // What's left is (in C, block-scope) declarations without initializers or
1678 // external storage. These are definitions.
1679 return Definition;
1680}
1681
Sebastian Redl35351a92010-01-31 22:27:38 +00001682VarDecl *VarDecl::getActingDefinition() {
1683 DefinitionKind Kind = isThisDeclarationADefinition();
1684 if (Kind != TentativeDefinition)
1685 return 0;
1686
Chris Lattner48eb14d2010-06-14 18:31:46 +00001687 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001688 VarDecl *First = getFirstDeclaration();
1689 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1690 I != E; ++I) {
1691 Kind = (*I)->isThisDeclarationADefinition();
1692 if (Kind == Definition)
1693 return 0;
1694 else if (Kind == TentativeDefinition)
1695 LastTentative = *I;
1696 }
1697 return LastTentative;
1698}
1699
1700bool VarDecl::isTentativeDefinitionNow() const {
1701 DefinitionKind Kind = isThisDeclarationADefinition();
1702 if (Kind != TentativeDefinition)
1703 return false;
1704
1705 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1706 if ((*I)->isThisDeclarationADefinition() == Definition)
1707 return false;
1708 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001709 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001710}
1711
Daniel Dunbar9d355812012-03-09 01:51:51 +00001712VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001713 VarDecl *First = getFirstDeclaration();
1714 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1715 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001716 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001717 return *I;
1718 }
1719 return 0;
1720}
1721
Daniel Dunbar9d355812012-03-09 01:51:51 +00001722VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001723 DefinitionKind Kind = DeclarationOnly;
1724
1725 const VarDecl *First = getFirstDeclaration();
1726 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001727 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001728 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001729 if (Kind == Definition)
1730 break;
1731 }
John McCall37bb6c92010-10-29 22:22:43 +00001732
1733 return Kind;
1734}
1735
Sebastian Redl5ca79842010-02-01 20:16:42 +00001736const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001737 redecl_iterator I = redecls_begin(), E = redecls_end();
1738 while (I != E && !I->getInit())
1739 ++I;
1740
1741 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001742 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001743 return I->getInit();
1744 }
1745 return 0;
1746}
1747
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001748bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001749 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001750 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001751
1752 if (!isStaticDataMember())
1753 return false;
1754
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001755 // If this static data member was instantiated from a static data member of
1756 // a class template, check whether that static data member was defined
1757 // out-of-line.
1758 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1759 return VD->isOutOfLine();
1760
1761 return false;
1762}
1763
Douglas Gregor1d957a32009-10-27 18:42:08 +00001764VarDecl *VarDecl::getOutOfLineDefinition() {
1765 if (!isStaticDataMember())
1766 return 0;
1767
1768 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1769 RD != RDEnd; ++RD) {
1770 if (RD->getLexicalDeclContext()->isFileContext())
1771 return *RD;
1772 }
1773
1774 return 0;
1775}
1776
Douglas Gregord5058122010-02-11 01:19:42 +00001777void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001778 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1779 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001780 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001781 }
1782
1783 Init = I;
1784}
1785
Daniel Dunbar9d355812012-03-09 01:51:51 +00001786bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001787 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001788
Richard Smith35ecb362012-03-02 04:14:40 +00001789 if (!Lang.CPlusPlus)
1790 return false;
1791
1792 // In C++11, any variable of reference type can be used in a constant
1793 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001794 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001795 return true;
1796
1797 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001798 // not require the variable to be non-volatile, but we consider this to be a
1799 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001800 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001801 return false;
1802
1803 // In C++, const, non-volatile variables of integral or enumeration types
1804 // can be used in constant expressions.
1805 if (getType()->isIntegralOrEnumerationType())
1806 return true;
1807
Richard Smith35ecb362012-03-02 04:14:40 +00001808 // Additionally, in C++11, non-volatile constexpr variables can be used in
1809 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001810 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001811}
1812
Richard Smithd0b4dd62011-12-19 06:19:21 +00001813/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1814/// form, which contains extra information on the evaluated value of the
1815/// initializer.
1816EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1817 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1818 if (!Eval) {
1819 Stmt *S = Init.get<Stmt *>();
1820 Eval = new (getASTContext()) EvaluatedStmt;
1821 Eval->Value = S;
1822 Init = Eval;
1823 }
1824 return Eval;
1825}
1826
Richard Smithdafff942012-01-14 04:30:29 +00001827APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001828 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00001829 return evaluateValue(Notes);
1830}
1831
1832APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001833 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001834 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1835
1836 // We only produce notes indicating why an initializer is non-constant the
1837 // first time it is evaluated. FIXME: The notes won't always be emitted the
1838 // first time we try evaluation, so might not be produced at all.
1839 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001840 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001841
1842 const Expr *Init = cast<Expr>(Eval->Value);
1843 assert(!Init->isValueDependent());
1844
1845 if (Eval->IsEvaluating) {
1846 // FIXME: Produce a diagnostic for self-initialization.
1847 Eval->CheckedICE = true;
1848 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001849 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001850 }
1851
1852 Eval->IsEvaluating = true;
1853
1854 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1855 this, Notes);
1856
1857 // Ensure the result is an uninitialized APValue if evaluation fails.
1858 if (!Result)
1859 Eval->Evaluated = APValue();
1860
1861 Eval->IsEvaluating = false;
1862 Eval->WasEvaluated = true;
1863
1864 // In C++11, we have determined whether the initializer was a constant
1865 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001866 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001867 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001868 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001869 }
1870
Richard Smithdafff942012-01-14 04:30:29 +00001871 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001872}
1873
1874bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001875 // Initializers of weak variables are never ICEs.
1876 if (isWeak())
1877 return false;
1878
Richard Smithd0b4dd62011-12-19 06:19:21 +00001879 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1880 if (Eval->CheckedICE)
1881 // We have already checked whether this subexpression is an
1882 // integral constant expression.
1883 return Eval->IsICE;
1884
1885 const Expr *Init = cast<Expr>(Eval->Value);
1886 assert(!Init->isValueDependent());
1887
1888 // In C++11, evaluate the initializer to check whether it's a constant
1889 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001890 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001891 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001892 evaluateValue(Notes);
1893 return Eval->IsICE;
1894 }
1895
1896 // It's an ICE whether or not the definition we found is
1897 // out-of-line. See DR 721 and the discussion in Clang PR
1898 // 6206 for details.
1899
1900 if (Eval->CheckingICE)
1901 return false;
1902 Eval->CheckingICE = true;
1903
1904 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1905 Eval->CheckingICE = false;
1906 Eval->CheckedICE = true;
1907 return Eval->IsICE;
1908}
1909
Douglas Gregorfe314812011-06-21 17:03:29 +00001910bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001911 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001912
1913 const Expr *E = getInit();
1914 if (!E)
1915 return false;
1916
1917 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1918 E = Cleanups->getSubExpr();
1919
1920 return isa<MaterializeTemporaryExpr>(E);
1921}
1922
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001923VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001924 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001925 return cast<VarDecl>(MSI->getInstantiatedFrom());
1926
1927 return 0;
1928}
1929
Douglas Gregor3c74d412009-10-14 20:14:33 +00001930TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001931 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001932 return MSI->getTemplateSpecializationKind();
1933
1934 return TSK_Undeclared;
1935}
1936
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001937MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001938 return getASTContext().getInstantiatedFromStaticDataMember(this);
1939}
1940
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001941void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1942 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001943 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001944 assert(MSI && "Not an instantiated static data member?");
1945 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001946 if (TSK != TSK_ExplicitSpecialization &&
1947 PointOfInstantiation.isValid() &&
1948 MSI->getPointOfInstantiation().isInvalid())
1949 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001950}
1951
Sebastian Redl833ef452010-01-26 22:01:41 +00001952//===----------------------------------------------------------------------===//
1953// ParmVarDecl Implementation
1954//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001955
Sebastian Redl833ef452010-01-26 22:01:41 +00001956ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001957 SourceLocation StartLoc,
1958 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001959 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001960 StorageClass S, Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001961 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001962 S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001963}
1964
Douglas Gregor72172e92012-01-05 21:55:30 +00001965ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1966 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1967 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001968 0, QualType(), 0, SC_None, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001969}
1970
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001971SourceRange ParmVarDecl::getSourceRange() const {
1972 if (!hasInheritedDefaultArg()) {
1973 SourceRange ArgRange = getDefaultArgRange();
1974 if (ArgRange.isValid())
1975 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1976 }
1977
Argyrios Kyrtzidisa0772792013-04-17 01:56:48 +00001978 // DeclaratorDecl considers the range of postfix types as overlapping with the
1979 // declaration name, but this is not the case with parameters in ObjC methods.
1980 if (isa<ObjCMethodDecl>(getDeclContext()))
1981 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
1982
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001983 return DeclaratorDecl::getSourceRange();
1984}
1985
Sebastian Redl833ef452010-01-26 22:01:41 +00001986Expr *ParmVarDecl::getDefaultArg() {
1987 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1988 assert(!hasUninstantiatedDefaultArg() &&
1989 "Default argument is not yet instantiated!");
1990
1991 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001992 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001993 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001994
Sebastian Redl833ef452010-01-26 22:01:41 +00001995 return Arg;
1996}
1997
Sebastian Redl833ef452010-01-26 22:01:41 +00001998SourceRange ParmVarDecl::getDefaultArgRange() const {
1999 if (const Expr *E = getInit())
2000 return E->getSourceRange();
2001
2002 if (hasUninstantiatedDefaultArg())
2003 return getUninstantiatedDefaultArg()->getSourceRange();
2004
2005 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00002006}
2007
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00002008bool ParmVarDecl::isParameterPack() const {
2009 return isa<PackExpansionType>(getType());
2010}
2011
Ted Kremenek540017e2011-10-06 05:00:56 +00002012void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2013 getASTContext().setParameterIndex(this, parameterIndex);
2014 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2015}
2016
2017unsigned ParmVarDecl::getParameterIndexLarge() const {
2018 return getASTContext().getParameterIndex(this);
2019}
2020
Nuno Lopes394ec982008-12-17 23:39:55 +00002021//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002022// FunctionDecl Implementation
2023//===----------------------------------------------------------------------===//
2024
Benjamin Kramer9170e912013-02-22 15:46:01 +00002025void FunctionDecl::getNameForDiagnostic(
2026 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2027 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002028 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2029 if (TemplateArgs)
Benjamin Kramer9170e912013-02-22 15:46:01 +00002030 TemplateSpecializationType::PrintTemplateArgumentList(
2031 OS, TemplateArgs->data(), TemplateArgs->size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002032}
2033
Ted Kremenek186a0742010-04-29 16:49:01 +00002034bool FunctionDecl::isVariadic() const {
2035 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
2036 return FT->isVariadic();
2037 return false;
2038}
2039
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002040bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
2041 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00002042 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002043 Definition = *I;
2044 return true;
2045 }
2046 }
2047
2048 return false;
2049}
2050
Anders Carlsson9bd7d162011-05-14 23:26:09 +00002051bool FunctionDecl::hasTrivialBody() const
2052{
2053 Stmt *S = getBody();
2054 if (!S) {
2055 // Since we don't have a body for this function, we don't know if it's
2056 // trivial or not.
2057 return false;
2058 }
2059
2060 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2061 return true;
2062 return false;
2063}
2064
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002065bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2066 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00002067 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002068 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
2069 return true;
2070 }
2071 }
2072
2073 return false;
2074}
2075
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002076Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00002077 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2078 if (I->Body) {
2079 Definition = *I;
2080 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00002081 } else if (I->IsLateTemplateParsed) {
2082 Definition = *I;
2083 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00002084 }
2085 }
2086
2087 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002088}
2089
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002090void FunctionDecl::setBody(Stmt *B) {
2091 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00002092 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002093 EndRangeLoc = B->getLocEnd();
2094}
2095
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002096void FunctionDecl::setPure(bool P) {
2097 IsPure = P;
2098 if (P)
2099 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2100 Parent->markedVirtualFunctionPure();
2101}
2102
Douglas Gregor16618f22009-09-12 00:17:51 +00002103bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00002104 const TranslationUnitDecl *tunit =
2105 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2106 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002107 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00002108 getIdentifier() &&
2109 getIdentifier()->isStr("main");
2110}
2111
2112bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2113 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2114 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2115 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2116 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2117 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2118
2119 if (isa<CXXRecordDecl>(getDeclContext())) return false;
2120 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2121
2122 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
2123 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
2124
2125 ASTContext &Context =
2126 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2127 ->getASTContext();
2128
2129 // The result type and first argument type are constant across all
2130 // these operators. The second argument must be exactly void*.
2131 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00002132}
2133
Rafael Espindolaf4187652013-02-14 01:18:37 +00002134LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6239e052013-01-12 15:27:44 +00002135 // Users expect to be able to write
2136 // extern "C" void *__builtin_alloca (size_t);
2137 // so consider builtins as having C language linkage.
Rafael Espindolac48f7342013-01-12 15:27:43 +00002138 if (getBuiltinID())
Rafael Espindolaf4187652013-02-14 01:18:37 +00002139 return CLanguageLinkage;
Rafael Espindolac48f7342013-01-12 15:27:43 +00002140
Rafael Espindolaf4187652013-02-14 01:18:37 +00002141 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00002142}
2143
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002144bool FunctionDecl::isExternC() const {
2145 return isExternCTemplate(*this);
2146}
2147
Rafael Espindola593537a2013-05-05 20:15:21 +00002148bool FunctionDecl::isInExternCContext() const {
2149 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
2150}
2151
2152bool FunctionDecl::isInExternCXXContext() const {
2153 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
2154}
2155
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002156bool FunctionDecl::isGlobal() const {
2157 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
2158 return Method->isStatic();
2159
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002160 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002161 return false;
2162
Mike Stump11289f42009-09-09 15:08:12 +00002163 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002164 DC->isNamespace();
2165 DC = DC->getParent()) {
2166 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
2167 if (!Namespace->getDeclName())
2168 return false;
2169 break;
2170 }
2171 }
2172
2173 return true;
2174}
2175
Richard Smith10876ef2013-01-17 01:30:42 +00002176bool FunctionDecl::isNoReturn() const {
2177 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smithdebc59d2013-01-30 05:45:05 +00002178 hasAttr<C11NoReturnAttr>() ||
Richard Smith10876ef2013-01-17 01:30:42 +00002179 getType()->getAs<FunctionType>()->getNoReturnAttr();
2180}
2181
Sebastian Redl833ef452010-01-26 22:01:41 +00002182void
2183FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
2184 redeclarable_base::setPreviousDeclaration(PrevDecl);
2185
2186 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2187 FunctionTemplateDecl *PrevFunTmpl
2188 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
2189 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
2190 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
2191 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00002192
Axel Naumannfbc7b982011-11-08 18:21:06 +00002193 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00002194 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00002195}
2196
2197const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
2198 return getFirstDeclaration();
2199}
2200
2201FunctionDecl *FunctionDecl::getCanonicalDecl() {
2202 return getFirstDeclaration();
2203}
2204
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002205/// \brief Returns a value indicating whether this function
2206/// corresponds to a builtin function.
2207///
2208/// The function corresponds to a built-in function if it is
2209/// declared at translation scope or within an extern "C" block and
2210/// its name matches with the name of a builtin. The returned value
2211/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00002212/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002213/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00002214unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00002215 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00002216 return 0;
2217
2218 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00002219 if (!BuiltinID)
2220 return 0;
2221
2222 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00002223 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2224 return BuiltinID;
2225
2226 // This function has the name of a known C library
2227 // function. Determine whether it actually refers to the C library
2228 // function or whether it just has the same name.
2229
Douglas Gregora908e7f2009-02-17 03:23:10 +00002230 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00002231 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00002232 return 0;
2233
Douglas Gregore711f702009-02-14 18:57:46 +00002234 // If this function is at translation-unit scope and we're not in
2235 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002236 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00002237 getDeclContext()->isTranslationUnit())
2238 return BuiltinID;
2239
2240 // If the function is in an extern "C" linkage specification and is
2241 // not marked "overloadable", it's the real function.
2242 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00002243 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00002244 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002245 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00002246 return BuiltinID;
2247
2248 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002249 return 0;
2250}
2251
2252
Chris Lattner47c0d002009-04-25 06:03:53 +00002253/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00002254/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00002255/// after it has been created.
2256unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00002257 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002258 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00002259 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002260 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00002261
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002262}
2263
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002264void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002265 ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002266 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002267 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002268
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002269 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002270 if (!NewParamInfo.empty()) {
2271 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2272 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002273 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002274}
Chris Lattner41943152007-01-25 04:52:46 +00002275
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002276void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy6f8780b2012-02-29 10:24:19 +00002277 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2278
2279 if (!NewDecls.empty()) {
2280 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2281 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002282 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy6f8780b2012-02-29 10:24:19 +00002283 }
2284}
2285
Chris Lattner58258242008-04-10 02:22:51 +00002286/// getMinRequiredArguments - Returns the minimum number of arguments
2287/// needed to call this function. This may be fewer than the number of
2288/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00002289/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00002290unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002291 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002292 return getNumParams();
2293
Douglas Gregor7825bf32011-01-06 22:09:01 +00002294 unsigned NumRequiredArgs = getNumParams();
2295
2296 // If the last parameter is a parameter pack, we don't need an argument for
2297 // it.
2298 if (NumRequiredArgs > 0 &&
2299 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2300 --NumRequiredArgs;
2301
2302 // If this parameter has a default argument, we don't need an argument for
2303 // it.
2304 while (NumRequiredArgs > 0 &&
2305 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00002306 --NumRequiredArgs;
2307
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002308 // We might have parameter packs before the end. These can't be deduced,
2309 // but they can still handle multiple arguments.
2310 unsigned ArgIdx = NumRequiredArgs;
2311 while (ArgIdx > 0) {
2312 if (getParamDecl(ArgIdx - 1)->isParameterPack())
2313 NumRequiredArgs = ArgIdx;
2314
2315 --ArgIdx;
2316 }
2317
Chris Lattner58258242008-04-10 02:22:51 +00002318 return NumRequiredArgs;
2319}
2320
Eli Friedman1b125c32012-02-07 03:50:18 +00002321static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2322 // Only consider file-scope declarations in this test.
2323 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2324 return false;
2325
2326 // Only consider explicit declarations; the presence of a builtin for a
2327 // libcall shouldn't affect whether a definition is externally visible.
2328 if (Redecl->isImplicit())
2329 return false;
2330
2331 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2332 return true; // Not an inline definition
2333
2334 return false;
2335}
2336
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002337/// \brief For a function declaration in C or C++, determine whether this
2338/// declaration causes the definition to be externally visible.
2339///
Eli Friedman1b125c32012-02-07 03:50:18 +00002340/// Specifically, this determines if adding the current declaration to the set
2341/// of redeclarations of the given functions causes
2342/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002343bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2344 assert(!doesThisDeclarationHaveABody() &&
2345 "Must have a declaration without a body.");
2346
2347 ASTContext &Context = getASTContext();
2348
David Blaikiebbafb8a2012-03-11 07:00:24 +00002349 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002350 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2351 // an externally visible definition.
2352 //
2353 // FIXME: What happens if gnu_inline gets added on after the first
2354 // declaration?
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002355 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002356 return false;
2357
2358 const FunctionDecl *Prev = this;
2359 bool FoundBody = false;
2360 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002361 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002362
2363 if (Prev->Body) {
2364 // If it's not the case that both 'inline' and 'extern' are
2365 // specified on the definition, then it is always externally visible.
2366 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 } else if (Prev->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002370 Prev->getStorageClass() != SC_Extern) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002371 return false;
2372 }
2373 }
2374 return FoundBody;
2375 }
2376
David Blaikiebbafb8a2012-03-11 07:00:24 +00002377 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002378 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002379
2380 // C99 6.7.4p6:
2381 // [...] If all of the file scope declarations for a function in a
2382 // translation unit include the inline function specifier without extern,
2383 // then the definition in that translation unit is an inline definition.
2384 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002385 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002386 const FunctionDecl *Prev = this;
2387 bool FoundBody = false;
2388 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002389 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002390 if (RedeclForcesDefC99(Prev))
2391 return false;
2392 }
2393 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002394}
2395
Richard Smithf3814ad2013-01-25 00:08:28 +00002396/// \brief For an inline function definition in C, or for a gnu_inline function
2397/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002398///
2399/// Inline function definitions are always available for inlining optimizations.
2400/// However, depending on the language dialect, declaration specifiers, and
2401/// attributes, the definition of an inline function may or may not be
2402/// "externally" visible to other translation units in the program.
2403///
2404/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002405/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002406/// inline definition becomes externally visible (C99 6.7.4p6).
2407///
2408/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2409/// definition, we use the GNU semantics for inline, which are nearly the
2410/// opposite of C99 semantics. In particular, "inline" by itself will create
2411/// an externally visible symbol, but "extern inline" will not create an
2412/// externally visible symbol.
2413bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002414 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002415 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002416 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002417
David Blaikiebbafb8a2012-03-11 07:00:24 +00002418 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002419 // Note: If you change the logic here, please change
2420 // doesDeclarationForceExternallyVisibleDefinition as well.
2421 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002422 // If it's not the case that both 'inline' and 'extern' are
2423 // specified on the definition, then this inline definition is
2424 // externally visible.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002425 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregorff76cb92010-12-09 16:59:22 +00002426 return true;
2427
2428 // If any declaration is 'inline' but not 'extern', then this definition
2429 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002430 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2431 Redecl != RedeclEnd;
2432 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002433 if (Redecl->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002434 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002435 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002436 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002437
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002438 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002439 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002440
Richard Smithf3814ad2013-01-25 00:08:28 +00002441 // The rest of this function is C-only.
2442 assert(!Context.getLangOpts().CPlusPlus &&
2443 "should not use C inline rules in C++");
2444
Douglas Gregor299d76e2009-09-13 07:46:26 +00002445 // C99 6.7.4p6:
2446 // [...] If all of the file scope declarations for a function in a
2447 // translation unit include the inline function specifier without extern,
2448 // then the definition in that translation unit is an inline definition.
2449 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2450 Redecl != RedeclEnd;
2451 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002452 if (RedeclForcesDefC99(*Redecl))
2453 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002454 }
2455
2456 // C99 6.7.4p6:
2457 // An inline definition does not provide an external definition for the
2458 // function, and does not forbid an external definition in another
2459 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002460 return false;
2461}
2462
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002463/// getOverloadedOperator - Which C++ overloaded operator this
2464/// function represents, if any.
2465OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002466 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2467 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002468 else
2469 return OO_None;
2470}
2471
Alexis Huntc88db062010-01-13 09:01:02 +00002472/// getLiteralIdentifier - The literal suffix identifier this function
2473/// represents, if any.
2474const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2475 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2476 return getDeclName().getCXXLiteralIdentifier();
2477 else
2478 return 0;
2479}
2480
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002481FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2482 if (TemplateOrSpecialization.isNull())
2483 return TK_NonTemplate;
2484 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2485 return TK_FunctionTemplate;
2486 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2487 return TK_MemberSpecialization;
2488 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2489 return TK_FunctionTemplateSpecialization;
2490 if (TemplateOrSpecialization.is
2491 <DependentFunctionTemplateSpecializationInfo*>())
2492 return TK_DependentFunctionTemplateSpecialization;
2493
David Blaikie83d382b2011-09-23 05:06:16 +00002494 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002495}
2496
Douglas Gregord801b062009-10-07 23:56:10 +00002497FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002498 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002499 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2500
2501 return 0;
2502}
2503
2504void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002505FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2506 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002507 TemplateSpecializationKind TSK) {
2508 assert(TemplateOrSpecialization.isNull() &&
2509 "Member function is already a specialization");
2510 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002511 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002512 TemplateOrSpecialization = Info;
2513}
2514
Douglas Gregorafca3b42009-10-27 20:53:28 +00002515bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002516 // If the function is invalid, it can't be implicitly instantiated.
2517 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002518 return false;
2519
2520 switch (getTemplateSpecializationKind()) {
2521 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002522 case TSK_ExplicitInstantiationDefinition:
2523 return false;
2524
2525 case TSK_ImplicitInstantiation:
2526 return true;
2527
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002528 // It is possible to instantiate TSK_ExplicitSpecialization kind
2529 // if the FunctionDecl has a class scope specialization pattern.
2530 case TSK_ExplicitSpecialization:
2531 return getClassScopeSpecializationPattern() != 0;
2532
Douglas Gregorafca3b42009-10-27 20:53:28 +00002533 case TSK_ExplicitInstantiationDeclaration:
2534 // Handled below.
2535 break;
2536 }
2537
2538 // Find the actual template from which we will instantiate.
2539 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002540 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002541 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002542 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002543
2544 // C++0x [temp.explicit]p9:
2545 // Except for inline functions, other explicit instantiation declarations
2546 // have the effect of suppressing the implicit instantiation of the entity
2547 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002548 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002549 return true;
2550
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002551 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002552}
2553
2554bool FunctionDecl::isTemplateInstantiation() const {
2555 switch (getTemplateSpecializationKind()) {
2556 case TSK_Undeclared:
2557 case TSK_ExplicitSpecialization:
2558 return false;
2559 case TSK_ImplicitInstantiation:
2560 case TSK_ExplicitInstantiationDeclaration:
2561 case TSK_ExplicitInstantiationDefinition:
2562 return true;
2563 }
2564 llvm_unreachable("All TSK values handled.");
2565}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002566
2567FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002568 // Handle class scope explicit specialization special case.
2569 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2570 return getClassScopeSpecializationPattern();
2571
Douglas Gregorafca3b42009-10-27 20:53:28 +00002572 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2573 while (Primary->getInstantiatedFromMemberTemplate()) {
2574 // If we have hit a point where the user provided a specialization of
2575 // this template, we're done looking.
2576 if (Primary->isMemberSpecialization())
2577 break;
2578
2579 Primary = Primary->getInstantiatedFromMemberTemplate();
2580 }
2581
2582 return Primary->getTemplatedDecl();
2583 }
2584
2585 return getInstantiatedFromMemberFunction();
2586}
2587
Douglas Gregor70d83e22009-06-29 17:30:29 +00002588FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002589 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002590 = TemplateOrSpecialization
2591 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002592 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002593 }
2594 return 0;
2595}
2596
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002597FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2598 return getASTContext().getClassScopeSpecializationPattern(this);
2599}
2600
Douglas Gregor70d83e22009-06-29 17:30:29 +00002601const TemplateArgumentList *
2602FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002603 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002604 = TemplateOrSpecialization
2605 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002606 return Info->TemplateArguments;
2607 }
2608 return 0;
2609}
2610
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002611const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002612FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2613 if (FunctionTemplateSpecializationInfo *Info
2614 = TemplateOrSpecialization
2615 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2616 return Info->TemplateArgumentsAsWritten;
2617 }
2618 return 0;
2619}
2620
Mike Stump11289f42009-09-09 15:08:12 +00002621void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002622FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2623 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002624 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002625 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002626 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002627 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2628 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002629 assert(TSK != TSK_Undeclared &&
2630 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002631 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002632 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002633 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002634 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2635 TemplateArgs,
2636 TemplateArgsAsWritten,
2637 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002638 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002639 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002640}
2641
John McCallb9c78482010-04-08 09:05:18 +00002642void
2643FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2644 const UnresolvedSetImpl &Templates,
2645 const TemplateArgumentListInfo &TemplateArgs) {
2646 assert(TemplateOrSpecialization.isNull());
2647 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2648 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002649 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002650 void *Buffer = Context.Allocate(Size);
2651 DependentFunctionTemplateSpecializationInfo *Info =
2652 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2653 TemplateArgs);
2654 TemplateOrSpecialization = Info;
2655}
2656
2657DependentFunctionTemplateSpecializationInfo::
2658DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2659 const TemplateArgumentListInfo &TArgs)
2660 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2661
2662 d.NumTemplates = Ts.size();
2663 d.NumArgs = TArgs.size();
2664
2665 FunctionTemplateDecl **TsArray =
2666 const_cast<FunctionTemplateDecl**>(getTemplates());
2667 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2668 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2669
2670 TemplateArgumentLoc *ArgsArray =
2671 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2672 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2673 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2674}
2675
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002676TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002677 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002678 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002679 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002680 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002681 if (FTSInfo)
2682 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002683
Douglas Gregord801b062009-10-07 23:56:10 +00002684 MemberSpecializationInfo *MSInfo
2685 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2686 if (MSInfo)
2687 return MSInfo->getTemplateSpecializationKind();
2688
2689 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002690}
2691
Mike Stump11289f42009-09-09 15:08:12 +00002692void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002693FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2694 SourceLocation PointOfInstantiation) {
2695 if (FunctionTemplateSpecializationInfo *FTSInfo
2696 = TemplateOrSpecialization.dyn_cast<
2697 FunctionTemplateSpecializationInfo*>()) {
2698 FTSInfo->setTemplateSpecializationKind(TSK);
2699 if (TSK != TSK_ExplicitSpecialization &&
2700 PointOfInstantiation.isValid() &&
2701 FTSInfo->getPointOfInstantiation().isInvalid())
2702 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2703 } else if (MemberSpecializationInfo *MSInfo
2704 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2705 MSInfo->setTemplateSpecializationKind(TSK);
2706 if (TSK != TSK_ExplicitSpecialization &&
2707 PointOfInstantiation.isValid() &&
2708 MSInfo->getPointOfInstantiation().isInvalid())
2709 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2710 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002711 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002712}
2713
2714SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002715 if (FunctionTemplateSpecializationInfo *FTSInfo
2716 = TemplateOrSpecialization.dyn_cast<
2717 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002718 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002719 else if (MemberSpecializationInfo *MSInfo
2720 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002721 return MSInfo->getPointOfInstantiation();
2722
2723 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002724}
2725
Douglas Gregor6411b922009-09-11 20:15:17 +00002726bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002727 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002728 return true;
2729
2730 // If this function was instantiated from a member function of a
2731 // class template, check whether that member function was defined out-of-line.
2732 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2733 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002734 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002735 return Definition->isOutOfLine();
2736 }
2737
2738 // If this function was instantiated from a function template,
2739 // check whether that function template was defined out-of-line.
2740 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2741 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002742 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002743 return Definition->isOutOfLine();
2744 }
2745
2746 return false;
2747}
2748
Abramo Bagnaraea947882011-03-08 16:41:52 +00002749SourceRange FunctionDecl::getSourceRange() const {
2750 return SourceRange(getOuterLocStart(), EndRangeLoc);
2751}
2752
Anna Zaks28db7ce2012-01-18 02:45:01 +00002753unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002754 IdentifierInfo *FnInfo = getIdentifier();
2755
2756 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002757 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002758
2759 // Builtin handling.
2760 switch (getBuiltinID()) {
2761 case Builtin::BI__builtin_memset:
2762 case Builtin::BI__builtin___memset_chk:
2763 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002764 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002765
2766 case Builtin::BI__builtin_memcpy:
2767 case Builtin::BI__builtin___memcpy_chk:
2768 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002769 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002770
2771 case Builtin::BI__builtin_memmove:
2772 case Builtin::BI__builtin___memmove_chk:
2773 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002774 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002775
2776 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002777 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002778 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002779 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002780
2781 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002782 case Builtin::BImemcmp:
2783 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002784
2785 case Builtin::BI__builtin_strncpy:
2786 case Builtin::BI__builtin___strncpy_chk:
2787 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002788 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002789
2790 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002791 case Builtin::BIstrncmp:
2792 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002793
2794 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002795 case Builtin::BIstrncasecmp:
2796 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002797
2798 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002799 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002800 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002801 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002802
2803 case Builtin::BI__builtin_strndup:
2804 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002805 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002806
Anna Zaks314cd092012-02-01 19:08:57 +00002807 case Builtin::BI__builtin_strlen:
2808 case Builtin::BIstrlen:
2809 return Builtin::BIstrlen;
2810
Anna Zaks201d4892012-01-13 21:52:01 +00002811 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002812 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002813 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002814 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002815 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002816 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002817 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002818 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002819 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002820 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002821 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002822 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002823 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002824 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002825 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002826 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002827 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002828 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002829 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002830 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002831 else if (FnInfo->isStr("strlen"))
2832 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002833 }
2834 break;
2835 }
Anna Zaks22122702012-01-17 00:37:07 +00002836 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002837}
2838
Chris Lattner59a25942008-03-31 00:36:02 +00002839//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002840// FieldDecl Implementation
2841//===----------------------------------------------------------------------===//
2842
Jay Foad39c79802011-01-12 09:06:06 +00002843FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002844 SourceLocation StartLoc, SourceLocation IdLoc,
2845 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002846 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002847 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002848 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002849 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002850}
2851
Douglas Gregor72172e92012-01-05 21:55:30 +00002852FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2853 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2854 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002855 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002856}
2857
Sebastian Redl833ef452010-01-26 22:01:41 +00002858bool FieldDecl::isAnonymousStructOrUnion() const {
2859 if (!isImplicit() || getDeclName())
2860 return false;
2861
2862 if (const RecordType *Record = getType()->getAs<RecordType>())
2863 return Record->getDecl()->isAnonymousStructOrUnion();
2864
2865 return false;
2866}
2867
Richard Smithcaf33902011-10-10 18:28:20 +00002868unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2869 assert(isBitField() && "not a bitfield");
2870 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2871 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2872}
2873
John McCall4e819612011-01-20 07:57:12 +00002874unsigned FieldDecl::getFieldIndex() const {
2875 if (CachedFieldIndex) return CachedFieldIndex - 1;
2876
Richard Smithd62306a2011-11-10 06:34:14 +00002877 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002878 const RecordDecl *RD = getParent();
2879 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002880 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002881
2882 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2883 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002884 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002885
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002886 if (IsMsStruct) {
2887 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002888 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002889 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002890 continue;
2891 }
David Blaikie40ed2972012-06-06 20:45:41 +00002892 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002893 }
John McCall4e819612011-01-20 07:57:12 +00002894 }
2895
Richard Smithd62306a2011-11-10 06:34:14 +00002896 assert(CachedFieldIndex && "failed to find field in parent");
2897 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002898}
2899
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002900SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002901 if (const Expr *E = InitializerOrBitWidth.getPointer())
2902 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002903 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002904}
2905
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002906void FieldDecl::setBitWidth(Expr *Width) {
2907 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2908 "bit width or initializer already set");
2909 InitializerOrBitWidth.setPointer(Width);
2910}
2911
Richard Smith938f40b2011-06-11 17:19:42 +00002912void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002913 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002914 "bit width or initializer already set");
2915 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002916}
2917
Sebastian Redl833ef452010-01-26 22:01:41 +00002918//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002919// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002920//===----------------------------------------------------------------------===//
2921
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002922SourceLocation TagDecl::getOuterLocStart() const {
2923 return getTemplateOrInnerLocStart(this);
2924}
2925
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002926SourceRange TagDecl::getSourceRange() const {
2927 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002928 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002929}
2930
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002931TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002932 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002933}
2934
Rafael Espindolabf5c33b2013-03-12 21:06:00 +00002935void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2936 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002937 if (TypeForDecl)
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002938 assert(TypeForDecl->isLinkageValid());
2939 assert(isLinkageValid());
Douglas Gregora72a4e32010-05-19 18:39:18 +00002940}
2941
Douglas Gregordee1be82009-01-17 00:42:38 +00002942void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002943 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002944
David Blaikie095deba2012-11-14 01:52:05 +00002945 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002946 struct CXXRecordDecl::DefinitionData *Data =
2947 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002948 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2949 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002950 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002951}
2952
2953void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002954 assert((!isa<CXXRecordDecl>(this) ||
2955 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2956 "definition completed but not started");
2957
John McCallf937c022011-10-07 06:10:15 +00002958 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002959 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002960
2961 if (ASTMutationListener *L = getASTMutationListener())
2962 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002963}
2964
John McCallf937c022011-10-07 06:10:15 +00002965TagDecl *TagDecl::getDefinition() const {
2966 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002967 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002968
2969 // If it's possible for us to have an out-of-date definition, check now.
2970 if (MayHaveOutOfDateDef) {
2971 if (IdentifierInfo *II = getIdentifier()) {
2972 if (II->isOutOfDate()) {
2973 updateOutOfDate(*II);
2974 }
2975 }
2976 }
2977
Andrew Trickba266ee2010-10-19 21:54:32 +00002978 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2979 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002980
2981 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002982 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002983 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002984 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002985
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002986 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002987}
2988
Douglas Gregor14454802011-02-25 02:25:35 +00002989void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2990 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002991 // Make sure the extended qualifier info is allocated.
2992 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002993 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002994 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002995 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002996 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002997 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002998 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002999 if (getExtInfo()->NumTemplParamLists == 0) {
3000 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00003001 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003002 }
3003 else
3004 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00003005 }
3006 }
3007}
3008
Abramo Bagnara60804e12011-03-18 15:16:37 +00003009void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
3010 unsigned NumTPLists,
3011 TemplateParameterList **TPLists) {
3012 assert(NumTPLists > 0);
3013 // Make sure the extended decl info is allocated.
3014 if (!hasExtInfo())
3015 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00003016 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00003017 // Set the template parameter lists info.
3018 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
3019}
3020
Ted Kremenek21475702008-09-05 17:16:31 +00003021//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003022// EnumDecl Implementation
3023//===----------------------------------------------------------------------===//
3024
David Blaikie68e081d2011-12-20 02:48:34 +00003025void EnumDecl::anchor() { }
3026
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003027EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3028 SourceLocation StartLoc, SourceLocation IdLoc,
3029 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003030 EnumDecl *PrevDecl, bool IsScoped,
3031 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003032 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003033 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003034 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00003035 C.getTypeDeclType(Enum, PrevDecl);
3036 return Enum;
3037}
3038
Douglas Gregor72172e92012-01-05 21:55:30 +00003039EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3040 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003041 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
3042 0, 0, false, false, false);
3043 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3044 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003045}
3046
Douglas Gregord5058122010-02-11 01:19:42 +00003047void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00003048 QualType NewPromotionType,
3049 unsigned NumPositiveBits,
3050 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00003051 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00003052 if (!IntegerType)
3053 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00003054 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00003055 setNumPositiveBits(NumPositiveBits);
3056 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00003057 TagDecl::completeDefinition();
3058}
3059
Richard Smith7d137e32012-03-23 03:33:32 +00003060TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3061 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3062 return MSI->getTemplateSpecializationKind();
3063
3064 return TSK_Undeclared;
3065}
3066
3067void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3068 SourceLocation PointOfInstantiation) {
3069 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3070 assert(MSI && "Not an instantiated member enumeration?");
3071 MSI->setTemplateSpecializationKind(TSK);
3072 if (TSK != TSK_ExplicitSpecialization &&
3073 PointOfInstantiation.isValid() &&
3074 MSI->getPointOfInstantiation().isInvalid())
3075 MSI->setPointOfInstantiation(PointOfInstantiation);
3076}
3077
Richard Smith4b38ded2012-03-14 23:13:10 +00003078EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3079 if (SpecializationInfo)
3080 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3081
3082 return 0;
3083}
3084
3085void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3086 TemplateSpecializationKind TSK) {
3087 assert(!SpecializationInfo && "Member enum is already a specialization");
3088 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3089}
3090
Sebastian Redl833ef452010-01-26 22:01:41 +00003091//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00003092// RecordDecl Implementation
3093//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00003094
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003095RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
3096 SourceLocation StartLoc, SourceLocation IdLoc,
3097 IdentifierInfo *Id, RecordDecl *PrevDecl)
3098 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00003099 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003100 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003101 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003102 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003103 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00003104 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00003105}
3106
Jay Foad39c79802011-01-12 09:06:06 +00003107RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003108 SourceLocation StartLoc, SourceLocation IdLoc,
3109 IdentifierInfo *Id, RecordDecl* PrevDecl) {
3110 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
3111 PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003112 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3113
Ted Kremenek21475702008-09-05 17:16:31 +00003114 C.getTypeDeclType(R, PrevDecl);
3115 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00003116}
3117
Douglas Gregor72172e92012-01-05 21:55:30 +00003118RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
3119 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003120 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
3121 SourceLocation(), 0, 0);
3122 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3123 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003124}
3125
Douglas Gregordfcad112009-03-25 15:59:44 +00003126bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00003127 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00003128 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3129}
3130
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003131RecordDecl::field_iterator RecordDecl::field_begin() const {
3132 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3133 LoadFieldsFromExternalStorage();
3134
3135 return field_iterator(decl_iterator(FirstDecl));
3136}
3137
Douglas Gregorb11aad82011-02-19 18:51:44 +00003138/// completeDefinition - Notes that the definition of this type is now
3139/// complete.
3140void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00003141 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00003142 TagDecl::completeDefinition();
3143}
3144
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003145/// isMsStruct - Get whether or not this record uses ms_struct layout.
3146/// This which can be turned on with an attribute, pragma, or the
3147/// -mms-bitfields command-line option.
3148bool RecordDecl::isMsStruct(const ASTContext &C) const {
3149 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
3150}
3151
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003152static bool isFieldOrIndirectField(Decl::Kind K) {
3153 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3154}
3155
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003156void RecordDecl::LoadFieldsFromExternalStorage() const {
3157 ExternalASTSource *Source = getASTContext().getExternalSource();
3158 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3159
3160 // Notify that we have a RecordDecl doing some initialization.
3161 ExternalASTSource::Deserializing TheFields(Source);
3162
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003163 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003164 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003165 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
3166 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003167 case ELR_Success:
3168 break;
3169
3170 case ELR_AlreadyLoaded:
3171 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003172 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003173 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003174
3175#ifndef NDEBUG
3176 // Check that all decls we got were FieldDecls.
3177 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003178 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003179#endif
3180
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003181 if (Decls.empty())
3182 return;
3183
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00003184 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
3185 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003186}
3187
Steve Naroff415d3d52008-10-08 17:01:13 +00003188//===----------------------------------------------------------------------===//
3189// BlockDecl Implementation
3190//===----------------------------------------------------------------------===//
3191
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003192void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00003193 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00003194
Steve Naroffc4b30e52009-03-13 16:56:44 +00003195 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00003196 if (!NewParamInfo.empty()) {
3197 NumParams = NewParamInfo.size();
3198 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3199 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003200 }
3201}
3202
John McCall351762c2011-02-07 10:33:21 +00003203void BlockDecl::setCaptures(ASTContext &Context,
3204 const Capture *begin,
3205 const Capture *end,
3206 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00003207 CapturesCXXThis = capturesCXXThis;
3208
3209 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00003210 NumCaptures = 0;
3211 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00003212 return;
3213 }
3214
John McCall351762c2011-02-07 10:33:21 +00003215 NumCaptures = end - begin;
3216
3217 // Avoid new Capture[] because we don't want to provide a default
3218 // constructor.
3219 size_t allocationSize = NumCaptures * sizeof(Capture);
3220 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
3221 memcpy(buffer, begin, allocationSize);
3222 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003223}
Sebastian Redl833ef452010-01-26 22:01:41 +00003224
John McCallce45f882011-06-15 22:51:16 +00003225bool BlockDecl::capturesVariable(const VarDecl *variable) const {
3226 for (capture_const_iterator
3227 i = capture_begin(), e = capture_end(); i != e; ++i)
3228 // Only auto vars can be captured, so no redeclaration worries.
3229 if (i->getVariable() == variable)
3230 return true;
3231
3232 return false;
3233}
3234
Douglas Gregor70226da2010-12-21 16:27:07 +00003235SourceRange BlockDecl::getSourceRange() const {
3236 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
3237}
Sebastian Redl833ef452010-01-26 22:01:41 +00003238
3239//===----------------------------------------------------------------------===//
3240// Other Decl Allocation/Deallocation Method Implementations
3241//===----------------------------------------------------------------------===//
3242
David Blaikie68e081d2011-12-20 02:48:34 +00003243void TranslationUnitDecl::anchor() { }
3244
Sebastian Redl833ef452010-01-26 22:01:41 +00003245TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
3246 return new (C) TranslationUnitDecl(C);
3247}
3248
David Blaikie68e081d2011-12-20 02:48:34 +00003249void LabelDecl::anchor() { }
3250
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003251LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00003252 SourceLocation IdentL, IdentifierInfo *II) {
3253 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
3254}
3255
3256LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3257 SourceLocation IdentL, IdentifierInfo *II,
3258 SourceLocation GnuLabelL) {
3259 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3260 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003261}
3262
Douglas Gregor72172e92012-01-05 21:55:30 +00003263LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3264 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3265 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00003266}
3267
David Blaikie68e081d2011-12-20 02:48:34 +00003268void ValueDecl::anchor() { }
3269
Benjamin Kramerea70eb32012-12-01 15:09:41 +00003270bool ValueDecl::isWeak() const {
3271 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3272 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3273 return true;
3274
3275 return isWeakImported();
3276}
3277
David Blaikie68e081d2011-12-20 02:48:34 +00003278void ImplicitParamDecl::anchor() { }
3279
Sebastian Redl833ef452010-01-26 22:01:41 +00003280ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003281 SourceLocation IdLoc,
3282 IdentifierInfo *Id,
3283 QualType Type) {
3284 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00003285}
3286
Douglas Gregor72172e92012-01-05 21:55:30 +00003287ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3288 unsigned ID) {
3289 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3290 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3291}
3292
Sebastian Redl833ef452010-01-26 22:01:41 +00003293FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003294 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003295 const DeclarationNameInfo &NameInfo,
3296 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003297 StorageClass SC,
Douglas Gregorff76cb92010-12-09 16:59:22 +00003298 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00003299 bool hasWrittenPrototype,
3300 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003301 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003302 T, TInfo, SC,
Richard Smitha77a0a62011-08-15 21:04:07 +00003303 isInlineSpecified,
3304 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00003305 New->HasWrittenPrototype = hasWrittenPrototype;
3306 return New;
3307}
3308
Douglas Gregor72172e92012-01-05 21:55:30 +00003309FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3310 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3311 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3312 DeclarationNameInfo(), QualType(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003313 SC_None, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00003314}
3315
Sebastian Redl833ef452010-01-26 22:01:41 +00003316BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3317 return new (C) BlockDecl(DC, L);
3318}
3319
Douglas Gregor72172e92012-01-05 21:55:30 +00003320BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3321 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3322 return new (Mem) BlockDecl(0, SourceLocation());
3323}
3324
John McCall5e77d762013-04-16 07:28:30 +00003325MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3326 unsigned ID) {
3327 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(MSPropertyDecl));
3328 return new (Mem) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
3329 QualType(), 0, SourceLocation(),
3330 0, 0);
3331}
3332
Ben Langmuir37943a72013-05-03 19:00:33 +00003333CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
3334 unsigned NumParams) {
Ben Langmuirce914fc2013-05-03 19:20:19 +00003335 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
Ben Langmuir37943a72013-05-03 19:00:33 +00003336 return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003337}
3338
Ben Langmuirce914fc2013-05-03 19:20:19 +00003339CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3340 unsigned NumParams) {
3341 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3342 void *Mem = AllocateDeserializedDecl(C, ID, Size);
3343 return new (Mem) CapturedDecl(0, NumParams);
3344}
3345
Sebastian Redl833ef452010-01-26 22:01:41 +00003346EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3347 SourceLocation L,
3348 IdentifierInfo *Id, QualType T,
3349 Expr *E, const llvm::APSInt &V) {
3350 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3351}
3352
Douglas Gregor72172e92012-01-05 21:55:30 +00003353EnumConstantDecl *
3354EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3355 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3356 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3357 llvm::APSInt());
3358}
3359
David Blaikie68e081d2011-12-20 02:48:34 +00003360void IndirectFieldDecl::anchor() { }
3361
Benjamin Kramer39593702010-11-21 14:11:41 +00003362IndirectFieldDecl *
3363IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3364 IdentifierInfo *Id, QualType T, NamedDecl **CH,
3365 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003366 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3367}
3368
Douglas Gregor72172e92012-01-05 21:55:30 +00003369IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3370 unsigned ID) {
3371 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3372 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3373 QualType(), 0, 0);
3374}
3375
Douglas Gregorbe996932010-09-01 20:41:53 +00003376SourceRange EnumConstantDecl::getSourceRange() const {
3377 SourceLocation End = getLocation();
3378 if (Init)
3379 End = Init->getLocEnd();
3380 return SourceRange(getLocation(), End);
3381}
3382
David Blaikie68e081d2011-12-20 02:48:34 +00003383void TypeDecl::anchor() { }
3384
Sebastian Redl833ef452010-01-26 22:01:41 +00003385TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003386 SourceLocation StartLoc, SourceLocation IdLoc,
3387 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3388 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003389}
3390
David Blaikie68e081d2011-12-20 02:48:34 +00003391void TypedefNameDecl::anchor() { }
3392
Douglas Gregor72172e92012-01-05 21:55:30 +00003393TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3394 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3395 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3396}
3397
Richard Smithdda56e42011-04-15 14:24:37 +00003398TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3399 SourceLocation StartLoc,
3400 SourceLocation IdLoc, IdentifierInfo *Id,
3401 TypeSourceInfo *TInfo) {
3402 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3403}
3404
Douglas Gregor72172e92012-01-05 21:55:30 +00003405TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3406 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3407 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3408}
3409
Abramo Bagnaraea947882011-03-08 16:41:52 +00003410SourceRange TypedefDecl::getSourceRange() const {
3411 SourceLocation RangeEnd = getLocation();
3412 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3413 if (typeIsPostfix(TInfo->getType()))
3414 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3415 }
3416 return SourceRange(getLocStart(), RangeEnd);
3417}
3418
Richard Smithdda56e42011-04-15 14:24:37 +00003419SourceRange TypeAliasDecl::getSourceRange() const {
3420 SourceLocation RangeEnd = getLocStart();
3421 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3422 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3423 return SourceRange(getLocStart(), RangeEnd);
3424}
3425
David Blaikie68e081d2011-12-20 02:48:34 +00003426void FileScopeAsmDecl::anchor() { }
3427
Sebastian Redl833ef452010-01-26 22:01:41 +00003428FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003429 StringLiteral *Str,
3430 SourceLocation AsmLoc,
3431 SourceLocation RParenLoc) {
3432 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003433}
Douglas Gregorba345522011-12-02 23:23:56 +00003434
Douglas Gregor72172e92012-01-05 21:55:30 +00003435FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3436 unsigned ID) {
3437 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3438 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3439}
3440
Michael Han84324352013-02-22 17:15:32 +00003441void EmptyDecl::anchor() {}
3442
3443EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3444 return new (C) EmptyDecl(DC, L);
3445}
3446
3447EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3448 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EmptyDecl));
3449 return new (Mem) EmptyDecl(0, SourceLocation());
3450}
3451
Douglas Gregorba345522011-12-02 23:23:56 +00003452//===----------------------------------------------------------------------===//
3453// ImportDecl Implementation
3454//===----------------------------------------------------------------------===//
3455
3456/// \brief Retrieve the number of module identifiers needed to name the given
3457/// module.
3458static unsigned getNumModuleIdentifiers(Module *Mod) {
3459 unsigned Result = 1;
3460 while (Mod->Parent) {
3461 Mod = Mod->Parent;
3462 ++Result;
3463 }
3464 return Result;
3465}
3466
Douglas Gregor22d09742012-01-03 18:04:46 +00003467ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003468 Module *Imported,
3469 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003470 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003471 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003472{
3473 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3474 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3475 memcpy(StoredLocs, IdentifierLocs.data(),
3476 IdentifierLocs.size() * sizeof(SourceLocation));
3477}
3478
Douglas Gregor22d09742012-01-03 18:04:46 +00003479ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003480 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003481 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003482 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003483{
3484 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3485}
3486
3487ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003488 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003489 ArrayRef<SourceLocation> IdentifierLocs) {
3490 void *Mem = C.Allocate(sizeof(ImportDecl) +
3491 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003492 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003493}
3494
3495ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003496 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003497 Module *Imported,
3498 SourceLocation EndLoc) {
3499 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003500 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003501 Import->setImplicit();
3502 return Import;
3503}
3504
Douglas Gregor72172e92012-01-05 21:55:30 +00003505ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3506 unsigned NumLocations) {
3507 void *Mem = AllocateDeserializedDecl(C, ID,
3508 (sizeof(ImportDecl) +
3509 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003510 return new (Mem) ImportDecl(EmptyShell());
3511}
3512
3513ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3514 if (!ImportedAndComplete.getInt())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00003515 return None;
Douglas Gregorba345522011-12-02 23:23:56 +00003516
3517 const SourceLocation *StoredLocs
3518 = reinterpret_cast<const SourceLocation *>(this + 1);
3519 return ArrayRef<SourceLocation>(StoredLocs,
3520 getNumModuleIdentifiers(getImportedModule()));
3521}
3522
3523SourceRange ImportDecl::getSourceRange() const {
3524 if (!ImportedAndComplete.getInt())
3525 return SourceRange(getLocation(),
3526 *reinterpret_cast<const SourceLocation *>(this + 1));
3527
3528 return SourceRange(getLocation(), getIdentifierLocs().back());
3529}