blob: ab9d73b9178e90004bd52b4dd5c01abfddcefc03 [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
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000276/// \brief Get the most restrictive linkage for the types and
277/// declarations in the given template argument list.
John McCalldf25c432013-02-16 00:17:33 +0000278///
279/// Note that we don't take an LVComputationKind because we always
280/// want to honor the visibility of template arguments in the same way.
281static LinkageInfo
282getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args) {
283 LinkageInfo LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000284
John McCalldf25c432013-02-16 00:17:33 +0000285 for (unsigned i = 0, e = args.size(); i != e; ++i) {
286 const TemplateArgument &arg = args[i];
287 switch (arg.getKind()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000288 case TemplateArgument::Null:
289 case TemplateArgument::Integral:
290 case TemplateArgument::Expression:
John McCalldf25c432013-02-16 00:17:33 +0000291 continue;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000292
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000293 case TemplateArgument::Type:
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000294 LV.merge(arg.getAsType()->getLinkageAndVisibility());
John McCalldf25c432013-02-16 00:17:33 +0000295 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000296
297 case TemplateArgument::Declaration:
John McCalldf25c432013-02-16 00:17:33 +0000298 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
299 assert(!usesTypeVisibility(ND));
300 LV.merge(getLVForDecl(ND, LVForValue));
301 }
302 continue;
Eli Friedmanb826a002012-09-26 02:36:12 +0000303
304 case TemplateArgument::NullPtr:
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000305 LV.merge(arg.getNullPtrType()->getLinkageAndVisibility());
John McCalldf25c432013-02-16 00:17:33 +0000306 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000307
308 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000309 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000310 if (TemplateDecl *Template
John McCalldf25c432013-02-16 00:17:33 +0000311 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
312 LV.merge(getLVForDecl(Template, LVForValue));
313 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000314
315 case TemplateArgument::Pack:
John McCalldf25c432013-02-16 00:17:33 +0000316 LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray()));
317 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000318 }
John McCalldf25c432013-02-16 00:17:33 +0000319 llvm_unreachable("bad template argument kind");
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000320 }
321
John McCall457a04e2010-10-22 21:05:15 +0000322 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000323}
324
Rafael Espindola2f869a32012-01-14 00:30:36 +0000325static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000326getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
327 return getLVForTemplateArgumentList(TArgs.asArray());
John McCall8823c652010-08-13 08:35:10 +0000328}
329
John McCall5f46c482013-02-21 23:42:58 +0000330static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
331 const FunctionTemplateSpecializationInfo *specInfo) {
332 // Include visibility from the template parameters and arguments
333 // only if this is not an explicit instantiation or specialization
334 // with direct explicit visibility. (Implicit instantiations won't
335 // have a direct attribute.)
336 if (!specInfo->isExplicitInstantiationOrSpecialization())
337 return true;
338
339 return !fn->hasAttr<VisibilityAttr>();
340}
341
John McCalldf25c432013-02-16 00:17:33 +0000342/// Merge in template-related linkage and visibility for the given
343/// function template specialization.
344///
345/// We don't need a computation kind here because we can assume
346/// LVForValue.
John McCall5f46c482013-02-21 23:42:58 +0000347///
NAKAMURA Takumi62eae082013-02-22 04:06:28 +0000348/// \param[out] LV the computation to use for the parent
John McCall5f46c482013-02-21 23:42:58 +0000349static void
350mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
351 const FunctionTemplateSpecializationInfo *specInfo) {
352 bool considerVisibility =
353 shouldConsiderTemplateVisibility(fn, specInfo);
John McCalldf25c432013-02-16 00:17:33 +0000354
355 // Merge information from the template parameters.
John McCall5f46c482013-02-21 23:42:58 +0000356 FunctionTemplateDecl *temp = specInfo->getTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000357 LinkageInfo tempLV =
358 getLVForTemplateParameterList(temp->getTemplateParameters());
359 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
360
361 // Merge information from the template arguments.
362 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
363 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
364 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000365}
366
John McCall5f46c482013-02-21 23:42:58 +0000367/// Does the given declaration have a direct visibility attribute
368/// that would match the given rules?
369static bool hasDirectVisibilityAttribute(const NamedDecl *D,
370 LVComputationKind computation) {
371 switch (computation) {
372 case LVForType:
373 case LVForExplicitType:
374 if (D->hasAttr<TypeVisibilityAttr>())
375 return true;
376 // fallthrough
377 case LVForValue:
378 case LVForExplicitValue:
379 if (D->hasAttr<VisibilityAttr>())
380 return true;
381 return false;
382 }
383 llvm_unreachable("bad visibility computation kind");
384}
385
John McCalld041a9b2013-02-20 01:54:26 +0000386/// Should we consider visibility associated with the template
387/// arguments and parameters of the given class template specialization?
388static bool shouldConsiderTemplateVisibility(
389 const ClassTemplateSpecializationDecl *spec,
390 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000391 // Include visibility from the template parameters and arguments
392 // only if this is not an explicit instantiation or specialization
393 // with direct explicit visibility (and note that implicit
394 // instantiations won't have a direct attribute).
395 //
396 // Furthermore, we want to ignore template parameters and arguments
John McCalld041a9b2013-02-20 01:54:26 +0000397 // for an explicit specialization when computing the visibility of a
398 // member thereof with explicit visibility.
John McCalldf25c432013-02-16 00:17:33 +0000399 //
400 // This is a bit complex; let's unpack it.
401 //
402 // An explicit class specialization is an independent, top-level
403 // declaration. As such, if it or any of its members has an
404 // explicit visibility attribute, that must directly express the
405 // user's intent, and we should honor it. The same logic applies to
406 // an explicit instantiation of a member of such a thing.
John McCalld041a9b2013-02-20 01:54:26 +0000407
408 // Fast path: if this is not an explicit instantiation or
409 // specialization, we always want to consider template-related
410 // visibility restrictions.
411 if (!spec->isExplicitInstantiationOrSpecialization())
412 return true;
413
414 // This is the 'member thereof' check.
415 if (spec->isExplicitSpecialization() &&
416 hasExplicitVisibilityAlready(computation))
417 return false;
418
John McCall5f46c482013-02-21 23:42:58 +0000419 return !hasDirectVisibilityAttribute(spec, computation);
John McCalld041a9b2013-02-20 01:54:26 +0000420}
421
422/// Merge in template-related linkage and visibility for the given
423/// class template specialization.
424static void mergeTemplateLV(LinkageInfo &LV,
425 const ClassTemplateSpecializationDecl *spec,
426 LVComputationKind computation) {
427 bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
John McCalldf25c432013-02-16 00:17:33 +0000428
429 // Merge information from the template parameters, but ignore
430 // visibility if we're only considering template arguments.
431
John McCalld041a9b2013-02-20 01:54:26 +0000432 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000433 LinkageInfo tempLV =
434 getLVForTemplateParameterList(temp->getTemplateParameters());
435 LV.mergeMaybeWithVisibility(tempLV,
John McCalld041a9b2013-02-20 01:54:26 +0000436 considerVisibility && !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000437
438 // Merge information from the template arguments. We ignore
439 // template-argument visibility if we've got an explicit
440 // instantiation with a visibility attribute.
441 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
442 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
443 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000444}
445
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000446static bool useInlineVisibilityHidden(const NamedDecl *D) {
447 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000448 const LangOptions &Opts = D->getASTContext().getLangOpts();
449 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000450 return false;
451
452 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
453 if (!FD)
454 return false;
455
456 TemplateSpecializationKind TSK = TSK_Undeclared;
457 if (FunctionTemplateSpecializationInfo *spec
458 = FD->getTemplateSpecializationInfo()) {
459 TSK = spec->getTemplateSpecializationKind();
460 } else if (MemberSpecializationInfo *MSI =
461 FD->getMemberSpecializationInfo()) {
462 TSK = MSI->getTemplateSpecializationKind();
463 }
464
465 const FunctionDecl *Def = 0;
466 // InlineVisibilityHidden only applies to definitions, and
467 // isInlined() only gives meaningful answers on definitions
468 // anyway.
469 return TSK != TSK_ExplicitInstantiationDeclaration &&
470 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000471 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000472}
473
Rafael Espindola593537a2013-05-05 20:15:21 +0000474template <typename T> static bool isFirstInExternCContext(T *D) {
Rafael Espindolaf4187652013-02-14 01:18:37 +0000475 const T *First = D->getFirstDeclaration();
Rafael Espindola593537a2013-05-05 20:15:21 +0000476 return First->isInExternCContext();
Rafael Espindolaf4187652013-02-14 01:18:37 +0000477}
478
Rafael Espindola327be3c2013-04-26 01:30:23 +0000479static bool isSingleLineExternC(const Decl &D) {
480 if (const LinkageSpecDecl *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
481 if (SD->getLanguage() == LinkageSpecDecl::lang_c && !SD->hasBraces())
482 return true;
483 return false;
484}
485
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000486static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCalldf25c432013-02-16 00:17:33 +0000487 LVComputationKind computation) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000488 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000489 "Not a name having namespace scope");
490 ASTContext &Context = D->getASTContext();
491
492 // C++ [basic.link]p3:
493 // A name having namespace scope (3.3.6) has internal linkage if it
494 // is the name of
495 // - an object, reference, function or function template that is
496 // explicitly declared static; or,
497 // (This bullet corresponds to C99 6.2.2p3.)
498 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
499 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000500 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000501 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000502
Richard Smithdc0ef452012-10-19 06:37:48 +0000503 // - a non-volatile object or reference that is explicitly declared const
504 // or constexpr and neither explicitly declared extern nor previously
505 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000506 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000507 Var->getType().isConstQualified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000508 !Var->getType().isVolatileQualified()) {
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000509 const VarDecl *PrevVar = Var->getPreviousDecl();
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000510 if (PrevVar)
Rafael Espindolaadea16b2013-04-03 15:50:00 +0000511 return PrevVar->getLinkageAndVisibility();
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000512
513 if (Var->getStorageClass() != SC_Extern &&
Rafael Espindola327be3c2013-04-26 01:30:23 +0000514 Var->getStorageClass() != SC_PrivateExtern &&
515 !isSingleLineExternC(*Var))
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000516 return LinkageInfo::internal();
517 }
518
519 for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar;
520 PrevVar = PrevVar->getPreviousDecl()) {
521 if (PrevVar->getStorageClass() == SC_PrivateExtern &&
522 Var->getStorageClass() == SC_None)
523 return PrevVar->getLinkageAndVisibility();
524 // Explicitly declared static.
525 if (PrevVar->getStorageClass() == SC_Static)
526 return LinkageInfo::internal();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000527 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000528 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000529 // C++ [temp]p4:
530 // A non-member function template can have internal linkage; any
531 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000532 const FunctionDecl *Function = 0;
533 if (const FunctionTemplateDecl *FunTmpl
534 = dyn_cast<FunctionTemplateDecl>(D))
535 Function = FunTmpl->getTemplatedDecl();
536 else
537 Function = cast<FunctionDecl>(D);
538
539 // Explicitly declared static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000540 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000541 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000542 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
543 // - a data member of an anonymous union.
544 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000545 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000546 }
547
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000548 if (D->isInAnonymousNamespace()) {
549 const VarDecl *Var = dyn_cast<VarDecl>(D);
550 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindola593537a2013-05-05 20:15:21 +0000551 if ((!Var || !isFirstInExternCContext(Var)) &&
552 (!Func || !isFirstInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000553 return LinkageInfo::uniqueExternal();
554 }
John McCallb7139c42010-10-28 04:18:25 +0000555
John McCall457a04e2010-10-22 21:05:15 +0000556 // Set up the defaults.
557
558 // C99 6.2.2p5:
559 // If the declaration of an identifier for an object has file
560 // scope and no storage-class specifier, its linkage is
561 // external.
John McCallc273f242010-10-30 11:50:40 +0000562 LinkageInfo LV;
563
John McCalld041a9b2013-02-20 01:54:26 +0000564 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000565 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000566 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000567 } else {
568 // If we're declared in a namespace with a visibility attribute,
John McCalldf25c432013-02-16 00:17:33 +0000569 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindola78158af2012-04-16 18:46:26 +0000570 for (const DeclContext *DC = D->getDeclContext();
571 !isa<TranslationUnitDecl>(DC);
572 DC = DC->getParent()) {
573 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
574 if (!ND) continue;
David Blaikie05785d12013-02-20 22:23:23 +0000575 if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000576 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000577 break;
578 }
579 }
580 }
Rafael Espindola78158af2012-04-16 18:46:26 +0000581
John McCalldf25c432013-02-16 00:17:33 +0000582 // Add in global settings if the above didn't give us direct visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000583 if (!LV.isVisibilityExplicit()) {
John McCallb4a99d32013-02-19 01:57:35 +0000584 // Use global type/value visibility as appropriate.
585 Visibility globalVisibility;
586 if (computation == LVForValue) {
587 globalVisibility = Context.getLangOpts().getValueVisibilityMode();
588 } else {
589 assert(computation == LVForType);
590 globalVisibility = Context.getLangOpts().getTypeVisibilityMode();
591 }
592 LV.mergeVisibility(globalVisibility, /*explicit*/ false);
John McCalldf25c432013-02-16 00:17:33 +0000593
594 // If we're paying attention to global visibility, apply
595 // -finline-visibility-hidden if this is an inline method.
596 if (useInlineVisibilityHidden(D))
597 LV.mergeVisibility(HiddenVisibility, true);
598 }
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000599 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000600
Douglas Gregorf73b2822009-11-25 22:24:25 +0000601 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000602
Douglas Gregorf73b2822009-11-25 22:24:25 +0000603 // A name having namespace scope has external linkage if it is the
604 // name of
605 //
606 // - an object or reference, unless it has internal linkage; or
607 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000608 // GCC applies the following optimization to variables and static
609 // data members, but not to functions:
610 //
John McCall457a04e2010-10-22 21:05:15 +0000611 // Modify the variable's LV by the LV of its type unless this is
612 // C or extern "C". This follows from [basic.link]p9:
613 // A type without linkage shall not be used as the type of a
614 // variable or function with external linkage unless
615 // - the entity has C language linkage, or
616 // - the entity is declared within an unnamed namespace, or
617 // - the entity is not used or is defined in the same
618 // translation unit.
619 // and [basic.link]p10:
620 // ...the types specified by all declarations referring to a
621 // given variable or function shall be identical...
622 // C does not have an equivalent rule.
623 //
John McCall5fe84122010-10-26 04:59:26 +0000624 // Ignore this if we've got an explicit attribute; the user
625 // probably knows what they're doing.
626 //
John McCall457a04e2010-10-22 21:05:15 +0000627 // Note that we don't want to make the variable non-external
628 // because of this, but unique-external linkage suits us.
Rafael Espindola593537a2013-05-05 20:15:21 +0000629 if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000630 LinkageInfo TypeLV = Var->getType()->getLinkageAndVisibility();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000631 if (TypeLV.getLinkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000632 return LinkageInfo::uniqueExternal();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000633 if (!LV.isVisibilityExplicit())
John McCalldf25c432013-02-16 00:17:33 +0000634 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000635 }
636
John McCall23032652010-11-02 18:38:13 +0000637 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000638 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000639
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000640 // Note that Sema::MergeVarDecl already takes care of implementing
641 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
642 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000643
Douglas Gregorf73b2822009-11-25 22:24:25 +0000644 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000645 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000646 // In theory, we can modify the function's LV by the LV of its
647 // type unless it has C linkage (see comment above about variables
648 // for justification). In practice, GCC doesn't do this, so it's
649 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000650
John McCall23032652010-11-02 18:38:13 +0000651 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000652 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000653
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000654 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
655 // merging storage classes and visibility attributes, so we don't have to
656 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000657
John McCallf768aa72011-02-10 06:50:24 +0000658 // In C++, then if the type of the function uses a type with
659 // unique-external linkage, it's not legally usable from outside
660 // this translation unit. However, we should use the C linkage
661 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000662 if (Context.getLangOpts().CPlusPlus &&
Rafael Espindola593537a2013-05-05 20:15:21 +0000663 !Function->isInExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000664 Function->getType()->getLinkage() == UniqueExternalLinkage)
665 return LinkageInfo::uniqueExternal();
666
John McCall5f46c482013-02-21 23:42:58 +0000667 // Consider LV from the template and the template arguments.
668 // We're at file scope, so we do not need to worry about nested
669 // specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000670 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000671 = Function->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000672 mergeTemplateLV(LV, Function, specInfo);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000673 }
674
Douglas Gregorf73b2822009-11-25 22:24:25 +0000675 // - a named class (Clause 9), or an unnamed class defined in a
676 // typedef declaration in which the class has the typedef name
677 // for linkage purposes (7.1.3); or
678 // - a named enumeration (7.2), or an unnamed enumeration
679 // defined in a typedef declaration in which the enumeration
680 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000681 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
682 // Unnamed tags have no linkage.
John McCall5ea95772013-03-09 00:54:27 +0000683 if (!Tag->hasNameForLinkage())
John McCallc273f242010-10-30 11:50:40 +0000684 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000685
John McCall457a04e2010-10-22 21:05:15 +0000686 // If this is a class template specialization, consider the
John McCall5f46c482013-02-21 23:42:58 +0000687 // linkage of the template and template arguments. We're at file
688 // scope, so we do not need to worry about nested specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000689 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000690 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCalldf25c432013-02-16 00:17:33 +0000691 mergeTemplateLV(LV, spec, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000692 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000693
694 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000695 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000696 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCalldf25c432013-02-16 00:17:33 +0000697 computation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000698 if (!isExternalLinkage(EnumLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000699 return LinkageInfo::none();
700 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000701
702 // - a template, unless it is a function template that has
703 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000704 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
John McCalld041a9b2013-02-20 01:54:26 +0000705 bool considerVisibility = !hasExplicitVisibilityAlready(computation);
John McCalldf25c432013-02-16 00:17:33 +0000706 LinkageInfo tempLV =
707 getLVForTemplateParameterList(temp->getTemplateParameters());
708 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
709
Douglas Gregorf73b2822009-11-25 22:24:25 +0000710 // - a namespace (7.3), unless it is declared within an unnamed
711 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000712 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
713 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000714
John McCall457a04e2010-10-22 21:05:15 +0000715 // By extension, we assign external linkage to Objective-C
716 // interfaces.
717 } else if (isa<ObjCInterfaceDecl>(D)) {
718 // fallout
719
720 // Everything not covered here has no linkage.
721 } else {
John McCallc273f242010-10-30 11:50:40 +0000722 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000723 }
724
725 // If we ended up with non-external linkage, visibility should
726 // always be default.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000727 if (LV.getLinkage() != ExternalLinkage)
728 return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000729
John McCall457a04e2010-10-22 21:05:15 +0000730 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000731}
732
John McCalldf25c432013-02-16 00:17:33 +0000733static LinkageInfo getLVForClassMember(const NamedDecl *D,
734 LVComputationKind computation) {
John McCall457a04e2010-10-22 21:05:15 +0000735 // Only certain class members have linkage. Note that fields don't
736 // really have linkage, but it's convenient to say they do for the
737 // purposes of calculating linkage of pointer-to-data-member
738 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000739 if (!(isa<CXXMethodDecl>(D) ||
740 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000741 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000742 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000743 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000744
John McCall07072662010-11-02 01:45:15 +0000745 LinkageInfo LV;
746
John McCall07072662010-11-02 01:45:15 +0000747 // If we have an explicit visibility attribute, merge that in.
John McCalld041a9b2013-02-20 01:54:26 +0000748 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000749 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000750 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000751 // If we're paying attention to global visibility, apply
752 // -finline-visibility-hidden if this is an inline method.
753 //
754 // Note that we do this before merging information about
755 // the class visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000756 if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000757 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000758 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000759
760 // If this class member has an explicit visibility attribute, the only
761 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000762 // only look for them when processing the class.
John McCalld041a9b2013-02-20 01:54:26 +0000763 LVComputationKind classComputation = computation;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000764 if (LV.isVisibilityExplicit())
John McCalld041a9b2013-02-20 01:54:26 +0000765 classComputation = withExplicitVisibilityAlready(computation);
Rafael Espindola505a7c82012-04-16 18:25:01 +0000766
John McCall5f46c482013-02-21 23:42:58 +0000767 LinkageInfo classLV =
768 getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000769 if (!isExternalLinkage(classLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000770 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000771
772 // If the class already has unique-external linkage, we can't improve.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000773 if (classLV.getLinkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000774 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000775
John McCall5f46c482013-02-21 23:42:58 +0000776 // Otherwise, don't merge in classLV yet, because in certain cases
777 // we need to completely ignore the visibility from it.
778
779 // Specifically, if this decl exists and has an explicit attribute.
780 const NamedDecl *explicitSpecSuppressor = 0;
781
John McCall8823c652010-08-13 08:35:10 +0000782 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000783 // If the type of the function uses a type with unique-external
784 // linkage, it's not legally usable from outside this translation unit.
785 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
786 return LinkageInfo::uniqueExternal();
787
John McCall457a04e2010-10-22 21:05:15 +0000788 // If this is a method template specialization, use the linkage for
789 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000790 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000791 = MD->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000792 mergeTemplateLV(LV, MD, spec);
John McCall5f46c482013-02-21 23:42:58 +0000793 if (spec->isExplicitSpecialization()) {
794 explicitSpecSuppressor = MD;
795 } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
796 explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
797 }
798 } else if (isExplicitMemberSpecialization(MD)) {
799 explicitSpecSuppressor = MD;
John McCalle6e622e2010-11-01 01:29:57 +0000800 }
John McCall457a04e2010-10-22 21:05:15 +0000801
John McCall37bb6c92010-10-29 22:22:43 +0000802 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000803 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000804 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCalldf25c432013-02-16 00:17:33 +0000805 mergeTemplateLV(LV, spec, computation);
John McCall5f46c482013-02-21 23:42:58 +0000806 if (spec->isExplicitSpecialization()) {
807 explicitSpecSuppressor = spec;
808 } else {
809 const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
810 if (isExplicitMemberSpecialization(temp)) {
811 explicitSpecSuppressor = temp->getTemplatedDecl();
812 }
813 }
814 } else if (isExplicitMemberSpecialization(RD)) {
815 explicitSpecSuppressor = RD;
John McCall37bb6c92010-10-29 22:22:43 +0000816 }
817
John McCall37bb6c92010-10-29 22:22:43 +0000818 // Static data members.
819 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000820 // Modify the variable's linkage by its type, but ignore the
821 // type's visibility unless it's a definition.
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000822 LinkageInfo typeLV = VD->getType()->getLinkageAndVisibility();
John McCall5f46c482013-02-21 23:42:58 +0000823 LV.mergeMaybeWithVisibility(typeLV,
Rafael Espindola4a5da442013-02-27 02:56:45 +0000824 !LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit());
John McCall5f46c482013-02-21 23:42:58 +0000825
826 if (isExplicitMemberSpecialization(VD)) {
827 explicitSpecSuppressor = VD;
828 }
John McCalldf25c432013-02-16 00:17:33 +0000829
830 // Template members.
831 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
832 bool considerVisibility =
Rafael Espindola4a5da442013-02-27 02:56:45 +0000833 (!LV.isVisibilityExplicit() &&
834 !classLV.isVisibilityExplicit() &&
John McCalld041a9b2013-02-20 01:54:26 +0000835 !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000836 LinkageInfo tempLV =
837 getLVForTemplateParameterList(temp->getTemplateParameters());
838 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall5f46c482013-02-21 23:42:58 +0000839
840 if (const RedeclarableTemplateDecl *redeclTemp =
841 dyn_cast<RedeclarableTemplateDecl>(temp)) {
842 if (isExplicitMemberSpecialization(redeclTemp)) {
843 explicitSpecSuppressor = temp->getTemplatedDecl();
844 }
845 }
John McCall37bb6c92010-10-29 22:22:43 +0000846 }
847
John McCall5f46c482013-02-21 23:42:58 +0000848 // We should never be looking for an attribute directly on a template.
849 assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
850
851 // If this member is an explicit member specialization, and it has
852 // an explicit attribute, ignore visibility from the parent.
853 bool considerClassVisibility = true;
854 if (explicitSpecSuppressor &&
Rafael Espindola4a5da442013-02-27 02:56:45 +0000855 // optimization: hasDVA() is true only with explicit visibility.
856 LV.isVisibilityExplicit() &&
857 classLV.getVisibility() != DefaultVisibility &&
John McCall5f46c482013-02-21 23:42:58 +0000858 hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) {
859 considerClassVisibility = false;
860 }
861
862 // Finally, merge in information from the class.
863 LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
John McCall457a04e2010-10-22 21:05:15 +0000864 return LV;
John McCall8823c652010-08-13 08:35:10 +0000865}
866
David Blaikie68e081d2011-12-20 02:48:34 +0000867void NamedDecl::anchor() { }
868
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000869bool NamedDecl::isLinkageValid() const {
870 if (!HasCachedLinkage)
871 return true;
John McCalld396b972011-02-08 19:01:05 +0000872
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000873 return getLVForDecl(this, LVForExplicitValue).getLinkage() ==
874 Linkage(CachedLinkage);
John McCalld396b972011-02-08 19:01:05 +0000875}
876
Douglas Gregorbf62d642010-12-06 18:36:25 +0000877Linkage NamedDecl::getLinkage() const {
Richard Smith88581592013-02-12 05:48:23 +0000878 if (HasCachedLinkage)
Rafael Espindola19de5612013-01-12 06:42:30 +0000879 return Linkage(CachedLinkage);
Rafael Espindola19de5612013-01-12 06:42:30 +0000880
John McCalld041a9b2013-02-20 01:54:26 +0000881 // We don't care about visibility here, so ask for the cheapest
882 // possible visibility analysis.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000883 CachedLinkage = getLVForDecl(this, LVForExplicitValue).getLinkage();
Rafael Espindola19de5612013-01-12 06:42:30 +0000884 HasCachedLinkage = 1;
885
886#ifndef NDEBUG
887 verifyLinkage();
888#endif
889
890 return Linkage(CachedLinkage);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000891}
892
John McCallc273f242010-10-30 11:50:40 +0000893LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCalldf25c432013-02-16 00:17:33 +0000894 LVComputationKind computation =
895 (usesTypeVisibility(this) ? LVForType : LVForValue);
896 LinkageInfo LI = getLVForDecl(this, computation);
Rafael Espindola19de5612013-01-12 06:42:30 +0000897 if (HasCachedLinkage) {
Rafael Espindola4a5da442013-02-27 02:56:45 +0000898 assert(Linkage(CachedLinkage) == LI.getLinkage());
Rafael Espindola19de5612013-01-12 06:42:30 +0000899 return LI;
Rafael Espindola54606d52012-12-25 07:31:49 +0000900 }
Rafael Espindola19de5612013-01-12 06:42:30 +0000901 HasCachedLinkage = 1;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000902 CachedLinkage = LI.getLinkage();
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000903
904#ifndef NDEBUG
Rafael Espindola19de5612013-01-12 06:42:30 +0000905 verifyLinkage();
906#endif
907
908 return LI;
909}
910
911void NamedDecl::verifyLinkage() const {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000912 // In C (because of gnu inline) and in c++ with microsoft extensions an
913 // static can follow an extern, so we can have two decls with different
914 // linkages.
915 const LangOptions &Opts = getASTContext().getLangOpts();
916 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
Rafael Espindola19de5612013-01-12 06:42:30 +0000917 return;
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000918
919 // We have just computed the linkage for this decl. By induction we know
920 // that all other computed linkages match, check that the one we just computed
921 // also does.
922 NamedDecl *D = NULL;
923 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
924 NamedDecl *T = cast<NamedDecl>(*I);
925 if (T == this)
926 continue;
Rafael Espindola19de5612013-01-12 06:42:30 +0000927 if (T->HasCachedLinkage != 0) {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000928 D = T;
929 break;
930 }
931 }
932 assert(!D || D->CachedLinkage == CachedLinkage);
John McCall033caa52010-10-29 00:29:13 +0000933}
Ted Kremenek926d8602010-04-20 23:15:35 +0000934
David Blaikie05785d12013-02-20 22:23:23 +0000935Optional<Visibility>
John McCalld041a9b2013-02-20 01:54:26 +0000936NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
Rafael Espindola3a52c442013-02-26 19:33:14 +0000937 // Check the declaration itself first.
938 if (Optional<Visibility> V = getVisibilityOf(this, kind))
939 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000940
Rafael Espindola3a52c442013-02-26 19:33:14 +0000941 // If this is a member class of a specialization of a class template
942 // and the corresponding decl has explicit visibility, use that.
943 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
944 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
945 if (InstantiatedFrom)
946 return getVisibilityOf(InstantiatedFrom, kind);
947 }
948
949 // If there wasn't explicit visibility there, and this is a
950 // specialization of a class template, check for visibility
951 // on the pattern.
952 if (const ClassTemplateSpecializationDecl *spec
953 = dyn_cast<ClassTemplateSpecializationDecl>(this))
954 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(),
955 kind);
956
957 // Use the most recent declaration.
958 const NamedDecl *MostRecent = cast<NamedDecl>(this->getMostRecentDecl());
959 if (MostRecent != this)
960 return MostRecent->getExplicitVisibility(kind);
961
962 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola96e68242012-05-16 02:10:38 +0000963 if (Var->isStaticDataMember()) {
964 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
965 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +0000966 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola96e68242012-05-16 02:10:38 +0000967 }
968
David Blaikie7a30dc52013-02-21 01:47:18 +0000969 return None;
Rafael Espindola96e68242012-05-16 02:10:38 +0000970 }
Rafael Espindola3a52c442013-02-26 19:33:14 +0000971 // Also handle function template specializations.
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000972 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000973 // If the function is a specialization of a template with an
974 // explicit visibility attribute, use that.
975 if (FunctionTemplateSpecializationInfo *templateInfo
976 = fn->getTemplateSpecializationInfo())
John McCalld041a9b2013-02-20 01:54:26 +0000977 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
978 kind);
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000979
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000980 // If the function is a member of a specialization of a class template
981 // and the corresponding decl has explicit visibility, use that.
982 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
983 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +0000984 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000985
David Blaikie7a30dc52013-02-21 01:47:18 +0000986 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000987 }
988
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000989 // The visibility of a template is stored in the templated decl.
990 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
John McCalld041a9b2013-02-20 01:54:26 +0000991 return getVisibilityOf(TD->getTemplatedDecl(), kind);
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000992
David Blaikie7a30dc52013-02-21 01:47:18 +0000993 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000994}
995
John McCalldf25c432013-02-16 00:17:33 +0000996static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
997 LVComputationKind computation) {
998 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
999 if (Function->isInAnonymousNamespace() &&
Rafael Espindola593537a2013-05-05 20:15:21 +00001000 !Function->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001001 return LinkageInfo::uniqueExternal();
1002
1003 // This is a "void f();" which got merged with a file static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001004 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCalldf25c432013-02-16 00:17:33 +00001005 return LinkageInfo::internal();
1006
1007 LinkageInfo LV;
John McCalld041a9b2013-02-20 01:54:26 +00001008 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001009 if (Optional<Visibility> Vis =
1010 getExplicitVisibility(Function, computation))
John McCalldf25c432013-02-16 00:17:33 +00001011 LV.mergeVisibility(*Vis, true);
1012 }
1013
1014 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1015 // merging storage classes and visibility attributes, so we don't have to
1016 // look at previous decls in here.
1017
1018 return LV;
1019 }
1020
1021 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001022 if (Var->hasExternalStorage()) {
Rafael Espindola593537a2013-05-05 20:15:21 +00001023 if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001024 return LinkageInfo::uniqueExternal();
1025
John McCalldf25c432013-02-16 00:17:33 +00001026 LinkageInfo LV;
1027 if (Var->getStorageClass() == SC_PrivateExtern)
1028 LV.mergeVisibility(HiddenVisibility, true);
John McCalld041a9b2013-02-20 01:54:26 +00001029 else if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001030 if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
John McCalldf25c432013-02-16 00:17:33 +00001031 LV.mergeVisibility(*Vis, true);
1032 }
1033
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001034 if (const VarDecl *Prev = Var->getPreviousDecl()) {
1035 LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1036 if (PrevLV.getLinkage())
1037 LV.setLinkage(PrevLV.getLinkage());
1038 LV.mergeVisibility(PrevLV);
1039 }
1040
John McCalldf25c432013-02-16 00:17:33 +00001041 return LV;
1042 }
1043 }
1044
1045 return LinkageInfo::none();
1046}
1047
1048static LinkageInfo getLVForDecl(const NamedDecl *D,
1049 LVComputationKind computation) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001050 // Objective-C: treat all Objective-C declarations as having external
1051 // linkage.
John McCall033caa52010-10-29 00:29:13 +00001052 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001053 default:
1054 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +00001055 case Decl::ParmVar:
1056 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001057 case Decl::TemplateTemplateParm: // count these as external
1058 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +00001059 case Decl::ObjCAtDefsField:
1060 case Decl::ObjCCategory:
1061 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +00001062 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +00001063 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +00001064 case Decl::ObjCMethod:
1065 case Decl::ObjCProperty:
1066 case Decl::ObjCPropertyImpl:
1067 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +00001068 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001069
1070 case Decl::CXXRecord: {
1071 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
1072 if (Record->isLambda()) {
1073 if (!Record->getLambdaManglingNumber()) {
1074 // This lambda has no mangling number, so it's internal.
1075 return LinkageInfo::internal();
1076 }
1077
1078 // This lambda has its linkage/visibility determined by its owner.
1079 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
1080 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
1081 if (isa<ParmVarDecl>(ContextDecl))
1082 DC = ContextDecl->getDeclContext()->getRedeclContext();
1083 else
John McCalldf25c432013-02-16 00:17:33 +00001084 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001085 }
1086
1087 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
John McCalldf25c432013-02-16 00:17:33 +00001088 return getLVForDecl(ND, computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001089
1090 return LinkageInfo::external();
1091 }
1092
1093 break;
1094 }
Ted Kremenek926d8602010-04-20 23:15:35 +00001095 }
1096
Douglas Gregorf73b2822009-11-25 22:24:25 +00001097 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +00001098 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCalldf25c432013-02-16 00:17:33 +00001099 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001100
1101 // C++ [basic.link]p5:
1102 // In addition, a member function, static data member, a named
1103 // class or enumeration of class scope, or an unnamed class or
1104 // enumeration defined in a class-scope typedef declaration such
1105 // that the class or enumeration has the typedef name for linkage
1106 // purposes (7.1.3), has external linkage if the name of the class
1107 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +00001108 if (D->getDeclContext()->isRecord())
John McCalldf25c432013-02-16 00:17:33 +00001109 return getLVForClassMember(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001110
1111 // C++ [basic.link]p6:
1112 // The name of a function declared in block scope and the name of
1113 // an object declared by a block scope extern declaration have
1114 // linkage. If there is a visible declaration of an entity with
1115 // linkage having the same name and type, ignoring entities
1116 // declared outside the innermost enclosing namespace scope, the
1117 // block scope declaration declares that same entity and receives
1118 // the linkage of the previous declaration. If there is more than
1119 // one such matching entity, the program is ill-formed. Otherwise,
1120 // if no matching entity is found, the block scope entity receives
1121 // external linkage.
John McCalldf25c432013-02-16 00:17:33 +00001122 if (D->getDeclContext()->isFunctionOrMethod())
1123 return getLVForLocalDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001124
1125 // C++ [basic.link]p6:
1126 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +00001127 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001128}
Douglas Gregorf73b2822009-11-25 22:24:25 +00001129
Douglas Gregor2ada0482009-02-04 17:27:36 +00001130std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +00001131 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +00001132}
1133
1134std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001135 std::string QualName;
1136 llvm::raw_string_ostream OS(QualName);
1137 printQualifiedName(OS, P);
1138 return OS.str();
1139}
1140
1141void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1142 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1143}
1144
1145void NamedDecl::printQualifiedName(raw_ostream &OS,
1146 const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +00001147 const DeclContext *Ctx = getDeclContext();
1148
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001149 if (Ctx->isFunctionOrMethod()) {
1150 printName(OS);
1151 return;
1152 }
Douglas Gregor2ada0482009-02-04 17:27:36 +00001153
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001154 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001155 ContextsTy Contexts;
1156
1157 // Collect contexts.
1158 while (Ctx && isa<NamedDecl>(Ctx)) {
1159 Contexts.push_back(Ctx);
1160 Ctx = Ctx->getParent();
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001161 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001162
1163 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1164 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001165 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001166 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Benjamin Kramer9170e912013-02-22 15:46:01 +00001167 OS << Spec->getName();
Douglas Gregor85673582009-05-18 17:01:57 +00001168 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001169 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1170 TemplateArgs.data(),
1171 TemplateArgs.size(),
1172 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001173 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +00001174 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001175 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +00001176 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001177 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001178 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1179 if (!RD->getIdentifier())
1180 OS << "<anonymous " << RD->getKindName() << '>';
1181 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001182 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001183 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +00001184 const FunctionProtoType *FT = 0;
1185 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001186 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001187
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001188 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001189 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001190 unsigned NumParams = FD->getNumParams();
1191 for (unsigned i = 0; i < NumParams; ++i) {
1192 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001193 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001194 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001195 }
1196
1197 if (FT->isVariadic()) {
1198 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001199 OS << ", ";
1200 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001201 }
1202 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001203 OS << ')';
1204 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001205 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001206 }
1207 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001208 }
1209
John McCalla2a3f7d2010-03-16 21:48:18 +00001210 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001211 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001212 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001213 OS << "<anonymous>";
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001214}
Douglas Gregor2ada0482009-02-04 17:27:36 +00001215
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001216void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1217 const PrintingPolicy &Policy,
1218 bool Qualified) const {
1219 if (Qualified)
1220 printQualifiedName(OS, Policy);
1221 else
1222 printName(OS);
Douglas Gregor2ada0482009-02-04 17:27:36 +00001223}
1224
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001225bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001226 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1227
Douglas Gregor889ceb72009-02-03 19:21:40 +00001228 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1229 // We want to keep it, unless it nominates same namespace.
1230 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +00001231 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1232 ->getOriginalNamespace() ==
1233 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1234 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +00001235 }
Mike Stump11289f42009-09-09 15:08:12 +00001236
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001237 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1238 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +00001239 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001240
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001241 // For function templates, the underlying function declarations are linked.
1242 if (const FunctionTemplateDecl *FunctionTemplate
1243 = dyn_cast<FunctionTemplateDecl>(this))
1244 if (const FunctionTemplateDecl *OldFunctionTemplate
1245 = dyn_cast<FunctionTemplateDecl>(OldD))
1246 return FunctionTemplate->getTemplatedDecl()
1247 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001248
Steve Naroffc4173fa2009-02-22 19:35:57 +00001249 // For method declarations, we keep track of redeclarations.
1250 if (isa<ObjCMethodDecl>(this))
1251 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001252
John McCall9f3059a2009-10-09 21:13:30 +00001253 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1254 return true;
1255
John McCall3f746822009-11-17 05:59:44 +00001256 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1257 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1258 cast<UsingShadowDecl>(OldD)->getTargetDecl();
1259
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001260 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1261 ASTContext &Context = getASTContext();
1262 return Context.getCanonicalNestedNameSpecifier(
1263 cast<UsingDecl>(this)->getQualifier()) ==
1264 Context.getCanonicalNestedNameSpecifier(
1265 cast<UsingDecl>(OldD)->getQualifier());
1266 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +00001267
Douglas Gregorb59643b2012-01-03 23:26:26 +00001268 // A typedef of an Objective-C class type can replace an Objective-C class
1269 // declaration or definition, and vice versa.
1270 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1271 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1272 return true;
1273
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001274 // For non-function declarations, if the declarations are of the
1275 // same kind then this must be a redeclaration, or semantic analysis
1276 // would not have given us the new declaration.
1277 return this->getKind() == OldD->getKind();
1278}
1279
Douglas Gregoreddf4332009-02-24 20:03:32 +00001280bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +00001281 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001282}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001283
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001284NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001285 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001286 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1287 ND = UD->getTargetDecl();
1288
1289 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1290 return AD->getClassInterface();
1291
1292 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001293}
1294
John McCalla8ae2222010-04-06 21:38:20 +00001295bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001296 if (!isCXXClassMember())
1297 return false;
1298
John McCalla8ae2222010-04-06 21:38:20 +00001299 const NamedDecl *D = this;
1300 if (isa<UsingShadowDecl>(D))
1301 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1302
John McCall5e77d762013-04-16 07:28:30 +00001303 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001304 return true;
1305 if (isa<CXXMethodDecl>(D))
1306 return cast<CXXMethodDecl>(D)->isInstance();
1307 if (isa<FunctionTemplateDecl>(D))
1308 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1309 ->getTemplatedDecl())->isInstance();
1310 return false;
1311}
1312
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001313//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001314// DeclaratorDecl Implementation
1315//===----------------------------------------------------------------------===//
1316
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001317template <typename DeclT>
1318static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1319 if (decl->getNumTemplateParameterLists() > 0)
1320 return decl->getTemplateParameterList(0)->getTemplateLoc();
1321 else
1322 return decl->getInnerLocStart();
1323}
1324
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001325SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001326 TypeSourceInfo *TSI = getTypeSourceInfo();
1327 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001328 return SourceLocation();
1329}
1330
Douglas Gregor14454802011-02-25 02:25:35 +00001331void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1332 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001333 // Make sure the extended decl info is allocated.
1334 if (!hasExtInfo()) {
1335 // Save (non-extended) type source info pointer.
1336 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1337 // Allocate external info struct.
1338 DeclInfo = new (getASTContext()) ExtInfo;
1339 // Restore savedTInfo into (extended) decl info.
1340 getExtInfo()->TInfo = savedTInfo;
1341 }
1342 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001343 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001344 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001345 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001346 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001347 if (getExtInfo()->NumTemplParamLists == 0) {
1348 // Save type source info pointer.
1349 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1350 // Deallocate the extended decl info.
1351 getASTContext().Deallocate(getExtInfo());
1352 // Restore savedTInfo into (non-extended) decl info.
1353 DeclInfo = savedTInfo;
1354 }
1355 else
1356 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001357 }
1358 }
1359}
1360
Abramo Bagnara60804e12011-03-18 15:16:37 +00001361void
1362DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1363 unsigned NumTPLists,
1364 TemplateParameterList **TPLists) {
1365 assert(NumTPLists > 0);
1366 // Make sure the extended decl info is allocated.
1367 if (!hasExtInfo()) {
1368 // Save (non-extended) type source info pointer.
1369 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1370 // Allocate external info struct.
1371 DeclInfo = new (getASTContext()) ExtInfo;
1372 // Restore savedTInfo into (extended) decl info.
1373 getExtInfo()->TInfo = savedTInfo;
1374 }
1375 // Set the template parameter lists info.
1376 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1377}
1378
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001379SourceLocation DeclaratorDecl::getOuterLocStart() const {
1380 return getTemplateOrInnerLocStart(this);
1381}
1382
Abramo Bagnaraea947882011-03-08 16:41:52 +00001383namespace {
1384
1385// Helper function: returns true if QT is or contains a type
1386// having a postfix component.
1387bool typeIsPostfix(clang::QualType QT) {
1388 while (true) {
1389 const Type* T = QT.getTypePtr();
1390 switch (T->getTypeClass()) {
1391 default:
1392 return false;
1393 case Type::Pointer:
1394 QT = cast<PointerType>(T)->getPointeeType();
1395 break;
1396 case Type::BlockPointer:
1397 QT = cast<BlockPointerType>(T)->getPointeeType();
1398 break;
1399 case Type::MemberPointer:
1400 QT = cast<MemberPointerType>(T)->getPointeeType();
1401 break;
1402 case Type::LValueReference:
1403 case Type::RValueReference:
1404 QT = cast<ReferenceType>(T)->getPointeeType();
1405 break;
1406 case Type::PackExpansion:
1407 QT = cast<PackExpansionType>(T)->getPattern();
1408 break;
1409 case Type::Paren:
1410 case Type::ConstantArray:
1411 case Type::DependentSizedArray:
1412 case Type::IncompleteArray:
1413 case Type::VariableArray:
1414 case Type::FunctionProto:
1415 case Type::FunctionNoProto:
1416 return true;
1417 }
1418 }
1419}
1420
1421} // namespace
1422
1423SourceRange DeclaratorDecl::getSourceRange() const {
1424 SourceLocation RangeEnd = getLocation();
1425 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1426 if (typeIsPostfix(TInfo->getType()))
1427 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1428 }
1429 return SourceRange(getOuterLocStart(), RangeEnd);
1430}
1431
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001432void
Douglas Gregor20527e22010-06-15 17:44:38 +00001433QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1434 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001435 TemplateParameterList **TPLists) {
1436 assert((NumTPLists == 0 || TPLists != 0) &&
1437 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001438
1439 // Free previous template parameters (if any).
1440 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001441 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001442 TemplParamLists = 0;
1443 NumTemplParamLists = 0;
1444 }
1445 // Set info on matched template parameter lists (if any).
1446 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001447 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001448 NumTemplParamLists = NumTPLists;
1449 for (unsigned i = NumTPLists; i-- > 0; )
1450 TemplParamLists[i] = TPLists[i];
1451 }
1452}
1453
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001454//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001455// VarDecl Implementation
1456//===----------------------------------------------------------------------===//
1457
Sebastian Redl833ef452010-01-26 22:01:41 +00001458const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1459 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001460 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001461 case SC_Auto: return "auto";
1462 case SC_Extern: return "extern";
1463 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1464 case SC_PrivateExtern: return "__private_extern__";
1465 case SC_Register: return "register";
1466 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001467 }
1468
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001469 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001470}
1471
Abramo Bagnaradff19302011-03-08 08:55:46 +00001472VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1473 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001474 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001475 StorageClass S) {
1476 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001477}
1478
Douglas Gregor72172e92012-01-05 21:55:30 +00001479VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1480 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1481 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001482 QualType(), 0, SC_None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001483}
1484
Douglas Gregorbf62d642010-12-06 18:36:25 +00001485void VarDecl::setStorageClass(StorageClass SC) {
1486 assert(isLegalForVariable(SC));
John McCallbeaa11c2011-05-01 02:13:58 +00001487 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001488}
1489
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001490SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001491 if (const Expr *Init = getInit()) {
1492 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001493 // If Init is implicit, ignore its source range and fallback on
1494 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1495 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001496 return SourceRange(getOuterLocStart(), InitEnd);
1497 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001498 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001499}
1500
Rafael Espindola88510672013-01-04 21:18:45 +00001501template<typename T>
Rafael Espindolaf4187652013-02-14 01:18:37 +00001502static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001503 // C++ [dcl.link]p1: All function types, function names with external linkage,
1504 // and variable names with external linkage have a language linkage.
1505 if (!isExternalLinkage(D.getLinkage()))
1506 return NoLanguageLinkage;
1507
1508 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001509 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001510 ASTContext &Context = D.getASTContext();
1511 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001512 return CLanguageLinkage;
1513
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001514 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1515 // language linkage of the names of class members and the function type of
1516 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001517 const DeclContext *DC = D.getDeclContext();
1518 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001519 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001520
1521 // If the first decl is in an extern "C" context, any other redeclaration
1522 // will have C language linkage. If the first one is not in an extern "C"
1523 // context, we would have reported an error for any other decl being in one.
Rafael Espindola593537a2013-05-05 20:15:21 +00001524 if (isFirstInExternCContext(&D))
Rafael Espindolaf4187652013-02-14 01:18:37 +00001525 return CLanguageLinkage;
1526 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001527}
1528
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001529template<typename T>
1530static bool isExternCTemplate(const T &D) {
1531 // Since the context is ignored for class members, they can only have C++
1532 // language linkage or no language linkage.
1533 const DeclContext *DC = D.getDeclContext();
1534 if (DC->isRecord()) {
1535 assert(D.getASTContext().getLangOpts().CPlusPlus);
1536 return false;
1537 }
1538
1539 return D.getLanguageLinkage() == CLanguageLinkage;
1540}
1541
Rafael Espindolaf4187652013-02-14 01:18:37 +00001542LanguageLinkage VarDecl::getLanguageLinkage() const {
1543 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001544}
1545
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001546bool VarDecl::isExternC() const {
1547 return isExternCTemplate(*this);
1548}
1549
Rafael Espindola593537a2013-05-05 20:15:21 +00001550static bool isLinkageSpecContext(const DeclContext *DC,
1551 LinkageSpecDecl::LanguageIDs ID) {
1552 while (DC->getDeclKind() != Decl::TranslationUnit) {
1553 if (DC->getDeclKind() == Decl::LinkageSpec)
1554 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1555 DC = DC->getParent();
1556 }
1557 return false;
1558}
1559
1560template <typename T>
1561static bool isInLanguageSpecContext(T *D, LinkageSpecDecl::LanguageIDs ID) {
1562 return isLinkageSpecContext(D->getLexicalDeclContext(), ID);
1563}
1564
1565bool VarDecl::isInExternCContext() const {
1566 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
1567}
1568
1569bool VarDecl::isInExternCXXContext() const {
1570 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
1571}
1572
Sebastian Redl833ef452010-01-26 22:01:41 +00001573VarDecl *VarDecl::getCanonicalDecl() {
1574 return getFirstDeclaration();
1575}
1576
Daniel Dunbar9d355812012-03-09 01:51:51 +00001577VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1578 ASTContext &C) const
1579{
Sebastian Redl35351a92010-01-31 22:27:38 +00001580 // C++ [basic.def]p2:
1581 // A declaration is a definition unless [...] it contains the 'extern'
1582 // specifier or a linkage-specification and neither an initializer [...],
1583 // it declares a static data member in a class declaration [...].
1584 // C++ [temp.expl.spec]p15:
1585 // An explicit specialization of a static data member of a template is a
1586 // definition if the declaration includes an initializer; otherwise, it is
1587 // a declaration.
1588 if (isStaticDataMember()) {
1589 if (isOutOfLine() && (hasInit() ||
1590 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1591 return Definition;
1592 else
1593 return DeclarationOnly;
1594 }
1595 // C99 6.7p5:
1596 // A definition of an identifier is a declaration for that identifier that
1597 // [...] causes storage to be reserved for that object.
1598 // Note: that applies for all non-file-scope objects.
1599 // C99 6.9.2p1:
1600 // If the declaration of an identifier for an object has file scope and an
1601 // initializer, the declaration is an external definition for the identifier
1602 if (hasInit())
1603 return Definition;
Rafael Espindolabff59562013-04-25 12:11:36 +00001604
Sebastian Redl35351a92010-01-31 22:27:38 +00001605 if (hasExternalStorage())
1606 return DeclarationOnly;
Rafael Espindola8f326a52013-03-07 01:42:44 +00001607
Rafael Espindolabff59562013-04-25 12:11:36 +00001608 // [dcl.link] p7:
1609 // A declaration directly contained in a linkage-specification is treated
1610 // as if it contains the extern specifier for the purpose of determining
1611 // the linkage of the declared name and whether it is a definition.
Rafael Espindola327be3c2013-04-26 01:30:23 +00001612 if (isSingleLineExternC(*this))
1613 return DeclarationOnly;
Rafael Espindolabff59562013-04-25 12:11:36 +00001614
Sebastian Redl35351a92010-01-31 22:27:38 +00001615 // C99 6.9.2p2:
1616 // A declaration of an object that has file scope without an initializer,
1617 // and without a storage class specifier or the scs 'static', constitutes
1618 // a tentative definition.
1619 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001620 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001621 return TentativeDefinition;
1622
1623 // What's left is (in C, block-scope) declarations without initializers or
1624 // external storage. These are definitions.
1625 return Definition;
1626}
1627
Sebastian Redl35351a92010-01-31 22:27:38 +00001628VarDecl *VarDecl::getActingDefinition() {
1629 DefinitionKind Kind = isThisDeclarationADefinition();
1630 if (Kind != TentativeDefinition)
1631 return 0;
1632
Chris Lattner48eb14d2010-06-14 18:31:46 +00001633 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001634 VarDecl *First = getFirstDeclaration();
1635 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1636 I != E; ++I) {
1637 Kind = (*I)->isThisDeclarationADefinition();
1638 if (Kind == Definition)
1639 return 0;
1640 else if (Kind == TentativeDefinition)
1641 LastTentative = *I;
1642 }
1643 return LastTentative;
1644}
1645
1646bool VarDecl::isTentativeDefinitionNow() const {
1647 DefinitionKind Kind = isThisDeclarationADefinition();
1648 if (Kind != TentativeDefinition)
1649 return false;
1650
1651 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1652 if ((*I)->isThisDeclarationADefinition() == Definition)
1653 return false;
1654 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001655 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001656}
1657
Daniel Dunbar9d355812012-03-09 01:51:51 +00001658VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001659 VarDecl *First = getFirstDeclaration();
1660 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1661 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001662 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001663 return *I;
1664 }
1665 return 0;
1666}
1667
Daniel Dunbar9d355812012-03-09 01:51:51 +00001668VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001669 DefinitionKind Kind = DeclarationOnly;
1670
1671 const VarDecl *First = getFirstDeclaration();
1672 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001673 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001674 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001675 if (Kind == Definition)
1676 break;
1677 }
John McCall37bb6c92010-10-29 22:22:43 +00001678
1679 return Kind;
1680}
1681
Sebastian Redl5ca79842010-02-01 20:16:42 +00001682const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001683 redecl_iterator I = redecls_begin(), E = redecls_end();
1684 while (I != E && !I->getInit())
1685 ++I;
1686
1687 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001688 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001689 return I->getInit();
1690 }
1691 return 0;
1692}
1693
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001694bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001695 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001696 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001697
1698 if (!isStaticDataMember())
1699 return false;
1700
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001701 // If this static data member was instantiated from a static data member of
1702 // a class template, check whether that static data member was defined
1703 // out-of-line.
1704 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1705 return VD->isOutOfLine();
1706
1707 return false;
1708}
1709
Douglas Gregor1d957a32009-10-27 18:42:08 +00001710VarDecl *VarDecl::getOutOfLineDefinition() {
1711 if (!isStaticDataMember())
1712 return 0;
1713
1714 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1715 RD != RDEnd; ++RD) {
1716 if (RD->getLexicalDeclContext()->isFileContext())
1717 return *RD;
1718 }
1719
1720 return 0;
1721}
1722
Douglas Gregord5058122010-02-11 01:19:42 +00001723void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001724 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1725 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001726 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001727 }
1728
1729 Init = I;
1730}
1731
Daniel Dunbar9d355812012-03-09 01:51:51 +00001732bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001733 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001734
Richard Smith35ecb362012-03-02 04:14:40 +00001735 if (!Lang.CPlusPlus)
1736 return false;
1737
1738 // In C++11, any variable of reference type can be used in a constant
1739 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001740 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001741 return true;
1742
1743 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001744 // not require the variable to be non-volatile, but we consider this to be a
1745 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001746 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001747 return false;
1748
1749 // In C++, const, non-volatile variables of integral or enumeration types
1750 // can be used in constant expressions.
1751 if (getType()->isIntegralOrEnumerationType())
1752 return true;
1753
Richard Smith35ecb362012-03-02 04:14:40 +00001754 // Additionally, in C++11, non-volatile constexpr variables can be used in
1755 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001756 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001757}
1758
Richard Smithd0b4dd62011-12-19 06:19:21 +00001759/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1760/// form, which contains extra information on the evaluated value of the
1761/// initializer.
1762EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1763 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1764 if (!Eval) {
1765 Stmt *S = Init.get<Stmt *>();
1766 Eval = new (getASTContext()) EvaluatedStmt;
1767 Eval->Value = S;
1768 Init = Eval;
1769 }
1770 return Eval;
1771}
1772
Richard Smithdafff942012-01-14 04:30:29 +00001773APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001774 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00001775 return evaluateValue(Notes);
1776}
1777
1778APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001779 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001780 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1781
1782 // We only produce notes indicating why an initializer is non-constant the
1783 // first time it is evaluated. FIXME: The notes won't always be emitted the
1784 // first time we try evaluation, so might not be produced at all.
1785 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001786 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001787
1788 const Expr *Init = cast<Expr>(Eval->Value);
1789 assert(!Init->isValueDependent());
1790
1791 if (Eval->IsEvaluating) {
1792 // FIXME: Produce a diagnostic for self-initialization.
1793 Eval->CheckedICE = true;
1794 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001795 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001796 }
1797
1798 Eval->IsEvaluating = true;
1799
1800 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1801 this, Notes);
1802
1803 // Ensure the result is an uninitialized APValue if evaluation fails.
1804 if (!Result)
1805 Eval->Evaluated = APValue();
1806
1807 Eval->IsEvaluating = false;
1808 Eval->WasEvaluated = true;
1809
1810 // In C++11, we have determined whether the initializer was a constant
1811 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001812 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001813 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001814 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001815 }
1816
Richard Smithdafff942012-01-14 04:30:29 +00001817 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001818}
1819
1820bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001821 // Initializers of weak variables are never ICEs.
1822 if (isWeak())
1823 return false;
1824
Richard Smithd0b4dd62011-12-19 06:19:21 +00001825 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1826 if (Eval->CheckedICE)
1827 // We have already checked whether this subexpression is an
1828 // integral constant expression.
1829 return Eval->IsICE;
1830
1831 const Expr *Init = cast<Expr>(Eval->Value);
1832 assert(!Init->isValueDependent());
1833
1834 // In C++11, evaluate the initializer to check whether it's a constant
1835 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001836 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001837 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001838 evaluateValue(Notes);
1839 return Eval->IsICE;
1840 }
1841
1842 // It's an ICE whether or not the definition we found is
1843 // out-of-line. See DR 721 and the discussion in Clang PR
1844 // 6206 for details.
1845
1846 if (Eval->CheckingICE)
1847 return false;
1848 Eval->CheckingICE = true;
1849
1850 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1851 Eval->CheckingICE = false;
1852 Eval->CheckedICE = true;
1853 return Eval->IsICE;
1854}
1855
Douglas Gregorfe314812011-06-21 17:03:29 +00001856bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001857 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001858
1859 const Expr *E = getInit();
1860 if (!E)
1861 return false;
1862
1863 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1864 E = Cleanups->getSubExpr();
1865
1866 return isa<MaterializeTemporaryExpr>(E);
1867}
1868
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001869VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001870 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001871 return cast<VarDecl>(MSI->getInstantiatedFrom());
1872
1873 return 0;
1874}
1875
Douglas Gregor3c74d412009-10-14 20:14:33 +00001876TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001877 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001878 return MSI->getTemplateSpecializationKind();
1879
1880 return TSK_Undeclared;
1881}
1882
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001883MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001884 return getASTContext().getInstantiatedFromStaticDataMember(this);
1885}
1886
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001887void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1888 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001889 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001890 assert(MSI && "Not an instantiated static data member?");
1891 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001892 if (TSK != TSK_ExplicitSpecialization &&
1893 PointOfInstantiation.isValid() &&
1894 MSI->getPointOfInstantiation().isInvalid())
1895 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001896}
1897
Sebastian Redl833ef452010-01-26 22:01:41 +00001898//===----------------------------------------------------------------------===//
1899// ParmVarDecl Implementation
1900//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001901
Sebastian Redl833ef452010-01-26 22:01:41 +00001902ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001903 SourceLocation StartLoc,
1904 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001905 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001906 StorageClass S, Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001907 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001908 S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001909}
1910
Douglas Gregor72172e92012-01-05 21:55:30 +00001911ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1912 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1913 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001914 0, QualType(), 0, SC_None, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001915}
1916
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001917SourceRange ParmVarDecl::getSourceRange() const {
1918 if (!hasInheritedDefaultArg()) {
1919 SourceRange ArgRange = getDefaultArgRange();
1920 if (ArgRange.isValid())
1921 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1922 }
1923
Argyrios Kyrtzidisa0772792013-04-17 01:56:48 +00001924 // DeclaratorDecl considers the range of postfix types as overlapping with the
1925 // declaration name, but this is not the case with parameters in ObjC methods.
1926 if (isa<ObjCMethodDecl>(getDeclContext()))
1927 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
1928
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001929 return DeclaratorDecl::getSourceRange();
1930}
1931
Sebastian Redl833ef452010-01-26 22:01:41 +00001932Expr *ParmVarDecl::getDefaultArg() {
1933 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1934 assert(!hasUninstantiatedDefaultArg() &&
1935 "Default argument is not yet instantiated!");
1936
1937 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001938 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001939 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001940
Sebastian Redl833ef452010-01-26 22:01:41 +00001941 return Arg;
1942}
1943
Sebastian Redl833ef452010-01-26 22:01:41 +00001944SourceRange ParmVarDecl::getDefaultArgRange() const {
1945 if (const Expr *E = getInit())
1946 return E->getSourceRange();
1947
1948 if (hasUninstantiatedDefaultArg())
1949 return getUninstantiatedDefaultArg()->getSourceRange();
1950
1951 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001952}
1953
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001954bool ParmVarDecl::isParameterPack() const {
1955 return isa<PackExpansionType>(getType());
1956}
1957
Ted Kremenek540017e2011-10-06 05:00:56 +00001958void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1959 getASTContext().setParameterIndex(this, parameterIndex);
1960 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1961}
1962
1963unsigned ParmVarDecl::getParameterIndexLarge() const {
1964 return getASTContext().getParameterIndex(this);
1965}
1966
Nuno Lopes394ec982008-12-17 23:39:55 +00001967//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001968// FunctionDecl Implementation
1969//===----------------------------------------------------------------------===//
1970
Benjamin Kramer9170e912013-02-22 15:46:01 +00001971void FunctionDecl::getNameForDiagnostic(
1972 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1973 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001974 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1975 if (TemplateArgs)
Benjamin Kramer9170e912013-02-22 15:46:01 +00001976 TemplateSpecializationType::PrintTemplateArgumentList(
1977 OS, TemplateArgs->data(), TemplateArgs->size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001978}
1979
Ted Kremenek186a0742010-04-29 16:49:01 +00001980bool FunctionDecl::isVariadic() const {
1981 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1982 return FT->isVariadic();
1983 return false;
1984}
1985
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001986bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1987 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001988 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001989 Definition = *I;
1990 return true;
1991 }
1992 }
1993
1994 return false;
1995}
1996
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001997bool FunctionDecl::hasTrivialBody() const
1998{
1999 Stmt *S = getBody();
2000 if (!S) {
2001 // Since we don't have a body for this function, we don't know if it's
2002 // trivial or not.
2003 return false;
2004 }
2005
2006 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2007 return true;
2008 return false;
2009}
2010
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002011bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2012 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00002013 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002014 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
2015 return true;
2016 }
2017 }
2018
2019 return false;
2020}
2021
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002022Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00002023 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2024 if (I->Body) {
2025 Definition = *I;
2026 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00002027 } else if (I->IsLateTemplateParsed) {
2028 Definition = *I;
2029 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00002030 }
2031 }
2032
2033 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002034}
2035
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002036void FunctionDecl::setBody(Stmt *B) {
2037 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00002038 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002039 EndRangeLoc = B->getLocEnd();
2040}
2041
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002042void FunctionDecl::setPure(bool P) {
2043 IsPure = P;
2044 if (P)
2045 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2046 Parent->markedVirtualFunctionPure();
2047}
2048
Douglas Gregor16618f22009-09-12 00:17:51 +00002049bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00002050 const TranslationUnitDecl *tunit =
2051 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2052 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002053 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00002054 getIdentifier() &&
2055 getIdentifier()->isStr("main");
2056}
2057
2058bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2059 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2060 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2061 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2062 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2063 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2064
2065 if (isa<CXXRecordDecl>(getDeclContext())) return false;
2066 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2067
2068 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
2069 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
2070
2071 ASTContext &Context =
2072 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2073 ->getASTContext();
2074
2075 // The result type and first argument type are constant across all
2076 // these operators. The second argument must be exactly void*.
2077 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00002078}
2079
Rafael Espindolaf4187652013-02-14 01:18:37 +00002080LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6239e052013-01-12 15:27:44 +00002081 // Users expect to be able to write
2082 // extern "C" void *__builtin_alloca (size_t);
2083 // so consider builtins as having C language linkage.
Rafael Espindolac48f7342013-01-12 15:27:43 +00002084 if (getBuiltinID())
Rafael Espindolaf4187652013-02-14 01:18:37 +00002085 return CLanguageLinkage;
Rafael Espindolac48f7342013-01-12 15:27:43 +00002086
Rafael Espindolaf4187652013-02-14 01:18:37 +00002087 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00002088}
2089
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002090bool FunctionDecl::isExternC() const {
2091 return isExternCTemplate(*this);
2092}
2093
Rafael Espindola593537a2013-05-05 20:15:21 +00002094bool FunctionDecl::isInExternCContext() const {
2095 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
2096}
2097
2098bool FunctionDecl::isInExternCXXContext() const {
2099 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
2100}
2101
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002102bool FunctionDecl::isGlobal() const {
2103 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
2104 return Method->isStatic();
2105
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002106 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002107 return false;
2108
Mike Stump11289f42009-09-09 15:08:12 +00002109 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002110 DC->isNamespace();
2111 DC = DC->getParent()) {
2112 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
2113 if (!Namespace->getDeclName())
2114 return false;
2115 break;
2116 }
2117 }
2118
2119 return true;
2120}
2121
Richard Smith10876ef2013-01-17 01:30:42 +00002122bool FunctionDecl::isNoReturn() const {
2123 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smithdebc59d2013-01-30 05:45:05 +00002124 hasAttr<C11NoReturnAttr>() ||
Richard Smith10876ef2013-01-17 01:30:42 +00002125 getType()->getAs<FunctionType>()->getNoReturnAttr();
2126}
2127
Sebastian Redl833ef452010-01-26 22:01:41 +00002128void
2129FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
2130 redeclarable_base::setPreviousDeclaration(PrevDecl);
2131
2132 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2133 FunctionTemplateDecl *PrevFunTmpl
2134 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
2135 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
2136 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
2137 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00002138
Axel Naumannfbc7b982011-11-08 18:21:06 +00002139 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00002140 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00002141}
2142
2143const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
2144 return getFirstDeclaration();
2145}
2146
2147FunctionDecl *FunctionDecl::getCanonicalDecl() {
2148 return getFirstDeclaration();
2149}
2150
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002151/// \brief Returns a value indicating whether this function
2152/// corresponds to a builtin function.
2153///
2154/// The function corresponds to a built-in function if it is
2155/// declared at translation scope or within an extern "C" block and
2156/// its name matches with the name of a builtin. The returned value
2157/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00002158/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002159/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00002160unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00002161 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00002162 return 0;
2163
2164 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00002165 if (!BuiltinID)
2166 return 0;
2167
2168 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00002169 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2170 return BuiltinID;
2171
2172 // This function has the name of a known C library
2173 // function. Determine whether it actually refers to the C library
2174 // function or whether it just has the same name.
2175
Douglas Gregora908e7f2009-02-17 03:23:10 +00002176 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00002177 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00002178 return 0;
2179
Douglas Gregore711f702009-02-14 18:57:46 +00002180 // If this function is at translation-unit scope and we're not in
2181 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002182 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00002183 getDeclContext()->isTranslationUnit())
2184 return BuiltinID;
2185
2186 // If the function is in an extern "C" linkage specification and is
2187 // not marked "overloadable", it's the real function.
2188 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00002189 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00002190 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002191 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00002192 return BuiltinID;
2193
2194 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002195 return 0;
2196}
2197
2198
Chris Lattner47c0d002009-04-25 06:03:53 +00002199/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00002200/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00002201/// after it has been created.
2202unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00002203 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002204 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00002205 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002206 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00002207
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002208}
2209
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002210void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002211 ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002212 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002213 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002214
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002215 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002216 if (!NewParamInfo.empty()) {
2217 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2218 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002219 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002220}
Chris Lattner41943152007-01-25 04:52:46 +00002221
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002222void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy6f8780b2012-02-29 10:24:19 +00002223 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2224
2225 if (!NewDecls.empty()) {
2226 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2227 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002228 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy6f8780b2012-02-29 10:24:19 +00002229 }
2230}
2231
Chris Lattner58258242008-04-10 02:22:51 +00002232/// getMinRequiredArguments - Returns the minimum number of arguments
2233/// needed to call this function. This may be fewer than the number of
2234/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00002235/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00002236unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002237 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002238 return getNumParams();
2239
Douglas Gregor7825bf32011-01-06 22:09:01 +00002240 unsigned NumRequiredArgs = getNumParams();
2241
2242 // If the last parameter is a parameter pack, we don't need an argument for
2243 // it.
2244 if (NumRequiredArgs > 0 &&
2245 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2246 --NumRequiredArgs;
2247
2248 // If this parameter has a default argument, we don't need an argument for
2249 // it.
2250 while (NumRequiredArgs > 0 &&
2251 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00002252 --NumRequiredArgs;
2253
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002254 // We might have parameter packs before the end. These can't be deduced,
2255 // but they can still handle multiple arguments.
2256 unsigned ArgIdx = NumRequiredArgs;
2257 while (ArgIdx > 0) {
2258 if (getParamDecl(ArgIdx - 1)->isParameterPack())
2259 NumRequiredArgs = ArgIdx;
2260
2261 --ArgIdx;
2262 }
2263
Chris Lattner58258242008-04-10 02:22:51 +00002264 return NumRequiredArgs;
2265}
2266
Eli Friedman1b125c32012-02-07 03:50:18 +00002267static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2268 // Only consider file-scope declarations in this test.
2269 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2270 return false;
2271
2272 // Only consider explicit declarations; the presence of a builtin for a
2273 // libcall shouldn't affect whether a definition is externally visible.
2274 if (Redecl->isImplicit())
2275 return false;
2276
2277 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2278 return true; // Not an inline definition
2279
2280 return false;
2281}
2282
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002283/// \brief For a function declaration in C or C++, determine whether this
2284/// declaration causes the definition to be externally visible.
2285///
Eli Friedman1b125c32012-02-07 03:50:18 +00002286/// Specifically, this determines if adding the current declaration to the set
2287/// of redeclarations of the given functions causes
2288/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002289bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2290 assert(!doesThisDeclarationHaveABody() &&
2291 "Must have a declaration without a body.");
2292
2293 ASTContext &Context = getASTContext();
2294
David Blaikiebbafb8a2012-03-11 07:00:24 +00002295 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002296 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2297 // an externally visible definition.
2298 //
2299 // FIXME: What happens if gnu_inline gets added on after the first
2300 // declaration?
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002301 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002302 return false;
2303
2304 const FunctionDecl *Prev = this;
2305 bool FoundBody = false;
2306 while ((Prev = Prev->getPreviousDecl())) {
2307 FoundBody |= Prev->Body;
2308
2309 if (Prev->Body) {
2310 // If it's not the case that both 'inline' and 'extern' are
2311 // specified on the definition, then it is always externally visible.
2312 if (!Prev->isInlineSpecified() ||
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002313 Prev->getStorageClass() != SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002314 return false;
2315 } else if (Prev->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002316 Prev->getStorageClass() != SC_Extern) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002317 return false;
2318 }
2319 }
2320 return FoundBody;
2321 }
2322
David Blaikiebbafb8a2012-03-11 07:00:24 +00002323 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002324 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002325
2326 // C99 6.7.4p6:
2327 // [...] If all of the file scope declarations for a function in a
2328 // translation unit include the inline function specifier without extern,
2329 // then the definition in that translation unit is an inline definition.
2330 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002331 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002332 const FunctionDecl *Prev = this;
2333 bool FoundBody = false;
2334 while ((Prev = Prev->getPreviousDecl())) {
2335 FoundBody |= Prev->Body;
2336 if (RedeclForcesDefC99(Prev))
2337 return false;
2338 }
2339 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002340}
2341
Richard Smithf3814ad2013-01-25 00:08:28 +00002342/// \brief For an inline function definition in C, or for a gnu_inline function
2343/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002344///
2345/// Inline function definitions are always available for inlining optimizations.
2346/// However, depending on the language dialect, declaration specifiers, and
2347/// attributes, the definition of an inline function may or may not be
2348/// "externally" visible to other translation units in the program.
2349///
2350/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002351/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002352/// inline definition becomes externally visible (C99 6.7.4p6).
2353///
2354/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2355/// definition, we use the GNU semantics for inline, which are nearly the
2356/// opposite of C99 semantics. In particular, "inline" by itself will create
2357/// an externally visible symbol, but "extern inline" will not create an
2358/// externally visible symbol.
2359bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002360 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002361 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002362 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002363
David Blaikiebbafb8a2012-03-11 07:00:24 +00002364 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002365 // Note: If you change the logic here, please change
2366 // doesDeclarationForceExternallyVisibleDefinition as well.
2367 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002368 // If it's not the case that both 'inline' and 'extern' are
2369 // specified on the definition, then this inline definition is
2370 // externally visible.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002371 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregorff76cb92010-12-09 16:59:22 +00002372 return true;
2373
2374 // If any declaration is 'inline' but not 'extern', then this definition
2375 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002376 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2377 Redecl != RedeclEnd;
2378 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002379 if (Redecl->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002380 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002381 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002382 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002383
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002384 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002385 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002386
Richard Smithf3814ad2013-01-25 00:08:28 +00002387 // The rest of this function is C-only.
2388 assert(!Context.getLangOpts().CPlusPlus &&
2389 "should not use C inline rules in C++");
2390
Douglas Gregor299d76e2009-09-13 07:46:26 +00002391 // C99 6.7.4p6:
2392 // [...] If all of the file scope declarations for a function in a
2393 // translation unit include the inline function specifier without extern,
2394 // then the definition in that translation unit is an inline definition.
2395 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2396 Redecl != RedeclEnd;
2397 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002398 if (RedeclForcesDefC99(*Redecl))
2399 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002400 }
2401
2402 // C99 6.7.4p6:
2403 // An inline definition does not provide an external definition for the
2404 // function, and does not forbid an external definition in another
2405 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002406 return false;
2407}
2408
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002409/// getOverloadedOperator - Which C++ overloaded operator this
2410/// function represents, if any.
2411OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002412 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2413 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002414 else
2415 return OO_None;
2416}
2417
Alexis Huntc88db062010-01-13 09:01:02 +00002418/// getLiteralIdentifier - The literal suffix identifier this function
2419/// represents, if any.
2420const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2421 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2422 return getDeclName().getCXXLiteralIdentifier();
2423 else
2424 return 0;
2425}
2426
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002427FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2428 if (TemplateOrSpecialization.isNull())
2429 return TK_NonTemplate;
2430 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2431 return TK_FunctionTemplate;
2432 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2433 return TK_MemberSpecialization;
2434 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2435 return TK_FunctionTemplateSpecialization;
2436 if (TemplateOrSpecialization.is
2437 <DependentFunctionTemplateSpecializationInfo*>())
2438 return TK_DependentFunctionTemplateSpecialization;
2439
David Blaikie83d382b2011-09-23 05:06:16 +00002440 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002441}
2442
Douglas Gregord801b062009-10-07 23:56:10 +00002443FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002444 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002445 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2446
2447 return 0;
2448}
2449
2450void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002451FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2452 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002453 TemplateSpecializationKind TSK) {
2454 assert(TemplateOrSpecialization.isNull() &&
2455 "Member function is already a specialization");
2456 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002457 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002458 TemplateOrSpecialization = Info;
2459}
2460
Douglas Gregorafca3b42009-10-27 20:53:28 +00002461bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002462 // If the function is invalid, it can't be implicitly instantiated.
2463 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002464 return false;
2465
2466 switch (getTemplateSpecializationKind()) {
2467 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002468 case TSK_ExplicitInstantiationDefinition:
2469 return false;
2470
2471 case TSK_ImplicitInstantiation:
2472 return true;
2473
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002474 // It is possible to instantiate TSK_ExplicitSpecialization kind
2475 // if the FunctionDecl has a class scope specialization pattern.
2476 case TSK_ExplicitSpecialization:
2477 return getClassScopeSpecializationPattern() != 0;
2478
Douglas Gregorafca3b42009-10-27 20:53:28 +00002479 case TSK_ExplicitInstantiationDeclaration:
2480 // Handled below.
2481 break;
2482 }
2483
2484 // Find the actual template from which we will instantiate.
2485 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002486 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002487 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002488 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002489
2490 // C++0x [temp.explicit]p9:
2491 // Except for inline functions, other explicit instantiation declarations
2492 // have the effect of suppressing the implicit instantiation of the entity
2493 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002494 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002495 return true;
2496
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002497 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002498}
2499
2500bool FunctionDecl::isTemplateInstantiation() const {
2501 switch (getTemplateSpecializationKind()) {
2502 case TSK_Undeclared:
2503 case TSK_ExplicitSpecialization:
2504 return false;
2505 case TSK_ImplicitInstantiation:
2506 case TSK_ExplicitInstantiationDeclaration:
2507 case TSK_ExplicitInstantiationDefinition:
2508 return true;
2509 }
2510 llvm_unreachable("All TSK values handled.");
2511}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002512
2513FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002514 // Handle class scope explicit specialization special case.
2515 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2516 return getClassScopeSpecializationPattern();
2517
Douglas Gregorafca3b42009-10-27 20:53:28 +00002518 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2519 while (Primary->getInstantiatedFromMemberTemplate()) {
2520 // If we have hit a point where the user provided a specialization of
2521 // this template, we're done looking.
2522 if (Primary->isMemberSpecialization())
2523 break;
2524
2525 Primary = Primary->getInstantiatedFromMemberTemplate();
2526 }
2527
2528 return Primary->getTemplatedDecl();
2529 }
2530
2531 return getInstantiatedFromMemberFunction();
2532}
2533
Douglas Gregor70d83e22009-06-29 17:30:29 +00002534FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002535 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002536 = TemplateOrSpecialization
2537 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002538 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002539 }
2540 return 0;
2541}
2542
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002543FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2544 return getASTContext().getClassScopeSpecializationPattern(this);
2545}
2546
Douglas Gregor70d83e22009-06-29 17:30:29 +00002547const TemplateArgumentList *
2548FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002549 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002550 = TemplateOrSpecialization
2551 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002552 return Info->TemplateArguments;
2553 }
2554 return 0;
2555}
2556
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002557const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002558FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2559 if (FunctionTemplateSpecializationInfo *Info
2560 = TemplateOrSpecialization
2561 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2562 return Info->TemplateArgumentsAsWritten;
2563 }
2564 return 0;
2565}
2566
Mike Stump11289f42009-09-09 15:08:12 +00002567void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002568FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2569 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002570 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002571 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002572 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002573 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2574 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002575 assert(TSK != TSK_Undeclared &&
2576 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002577 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002578 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002579 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002580 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2581 TemplateArgs,
2582 TemplateArgsAsWritten,
2583 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002584 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002585 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002586}
2587
John McCallb9c78482010-04-08 09:05:18 +00002588void
2589FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2590 const UnresolvedSetImpl &Templates,
2591 const TemplateArgumentListInfo &TemplateArgs) {
2592 assert(TemplateOrSpecialization.isNull());
2593 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2594 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002595 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002596 void *Buffer = Context.Allocate(Size);
2597 DependentFunctionTemplateSpecializationInfo *Info =
2598 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2599 TemplateArgs);
2600 TemplateOrSpecialization = Info;
2601}
2602
2603DependentFunctionTemplateSpecializationInfo::
2604DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2605 const TemplateArgumentListInfo &TArgs)
2606 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2607
2608 d.NumTemplates = Ts.size();
2609 d.NumArgs = TArgs.size();
2610
2611 FunctionTemplateDecl **TsArray =
2612 const_cast<FunctionTemplateDecl**>(getTemplates());
2613 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2614 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2615
2616 TemplateArgumentLoc *ArgsArray =
2617 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2618 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2619 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2620}
2621
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002622TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002623 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002624 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002625 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002626 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002627 if (FTSInfo)
2628 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002629
Douglas Gregord801b062009-10-07 23:56:10 +00002630 MemberSpecializationInfo *MSInfo
2631 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2632 if (MSInfo)
2633 return MSInfo->getTemplateSpecializationKind();
2634
2635 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002636}
2637
Mike Stump11289f42009-09-09 15:08:12 +00002638void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002639FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2640 SourceLocation PointOfInstantiation) {
2641 if (FunctionTemplateSpecializationInfo *FTSInfo
2642 = TemplateOrSpecialization.dyn_cast<
2643 FunctionTemplateSpecializationInfo*>()) {
2644 FTSInfo->setTemplateSpecializationKind(TSK);
2645 if (TSK != TSK_ExplicitSpecialization &&
2646 PointOfInstantiation.isValid() &&
2647 FTSInfo->getPointOfInstantiation().isInvalid())
2648 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2649 } else if (MemberSpecializationInfo *MSInfo
2650 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2651 MSInfo->setTemplateSpecializationKind(TSK);
2652 if (TSK != TSK_ExplicitSpecialization &&
2653 PointOfInstantiation.isValid() &&
2654 MSInfo->getPointOfInstantiation().isInvalid())
2655 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2656 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002657 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002658}
2659
2660SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002661 if (FunctionTemplateSpecializationInfo *FTSInfo
2662 = TemplateOrSpecialization.dyn_cast<
2663 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002664 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002665 else if (MemberSpecializationInfo *MSInfo
2666 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002667 return MSInfo->getPointOfInstantiation();
2668
2669 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002670}
2671
Douglas Gregor6411b922009-09-11 20:15:17 +00002672bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002673 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002674 return true;
2675
2676 // If this function was instantiated from a member function of a
2677 // class template, check whether that member function was defined out-of-line.
2678 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2679 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002680 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002681 return Definition->isOutOfLine();
2682 }
2683
2684 // If this function was instantiated from a function template,
2685 // check whether that function template was defined out-of-line.
2686 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2687 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002688 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002689 return Definition->isOutOfLine();
2690 }
2691
2692 return false;
2693}
2694
Abramo Bagnaraea947882011-03-08 16:41:52 +00002695SourceRange FunctionDecl::getSourceRange() const {
2696 return SourceRange(getOuterLocStart(), EndRangeLoc);
2697}
2698
Anna Zaks28db7ce2012-01-18 02:45:01 +00002699unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002700 IdentifierInfo *FnInfo = getIdentifier();
2701
2702 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002703 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002704
2705 // Builtin handling.
2706 switch (getBuiltinID()) {
2707 case Builtin::BI__builtin_memset:
2708 case Builtin::BI__builtin___memset_chk:
2709 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002710 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002711
2712 case Builtin::BI__builtin_memcpy:
2713 case Builtin::BI__builtin___memcpy_chk:
2714 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002715 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002716
2717 case Builtin::BI__builtin_memmove:
2718 case Builtin::BI__builtin___memmove_chk:
2719 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002720 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002721
2722 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002723 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002724 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002725 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002726
2727 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002728 case Builtin::BImemcmp:
2729 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002730
2731 case Builtin::BI__builtin_strncpy:
2732 case Builtin::BI__builtin___strncpy_chk:
2733 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002734 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002735
2736 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002737 case Builtin::BIstrncmp:
2738 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002739
2740 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002741 case Builtin::BIstrncasecmp:
2742 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002743
2744 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002745 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002746 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002747 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002748
2749 case Builtin::BI__builtin_strndup:
2750 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002751 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002752
Anna Zaks314cd092012-02-01 19:08:57 +00002753 case Builtin::BI__builtin_strlen:
2754 case Builtin::BIstrlen:
2755 return Builtin::BIstrlen;
2756
Anna Zaks201d4892012-01-13 21:52:01 +00002757 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002758 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002759 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002760 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002761 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002762 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002763 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002764 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002765 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002766 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002767 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002768 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002769 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002770 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002771 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002772 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002773 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002774 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002775 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002776 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002777 else if (FnInfo->isStr("strlen"))
2778 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002779 }
2780 break;
2781 }
Anna Zaks22122702012-01-17 00:37:07 +00002782 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002783}
2784
Chris Lattner59a25942008-03-31 00:36:02 +00002785//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002786// FieldDecl Implementation
2787//===----------------------------------------------------------------------===//
2788
Jay Foad39c79802011-01-12 09:06:06 +00002789FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002790 SourceLocation StartLoc, SourceLocation IdLoc,
2791 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002792 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002793 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002794 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002795 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002796}
2797
Douglas Gregor72172e92012-01-05 21:55:30 +00002798FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2799 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2800 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002801 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002802}
2803
Sebastian Redl833ef452010-01-26 22:01:41 +00002804bool FieldDecl::isAnonymousStructOrUnion() const {
2805 if (!isImplicit() || getDeclName())
2806 return false;
2807
2808 if (const RecordType *Record = getType()->getAs<RecordType>())
2809 return Record->getDecl()->isAnonymousStructOrUnion();
2810
2811 return false;
2812}
2813
Richard Smithcaf33902011-10-10 18:28:20 +00002814unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2815 assert(isBitField() && "not a bitfield");
2816 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2817 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2818}
2819
John McCall4e819612011-01-20 07:57:12 +00002820unsigned FieldDecl::getFieldIndex() const {
2821 if (CachedFieldIndex) return CachedFieldIndex - 1;
2822
Richard Smithd62306a2011-11-10 06:34:14 +00002823 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002824 const RecordDecl *RD = getParent();
2825 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002826 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002827
2828 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2829 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002830 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002831
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002832 if (IsMsStruct) {
2833 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002834 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002835 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002836 continue;
2837 }
David Blaikie40ed2972012-06-06 20:45:41 +00002838 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002839 }
John McCall4e819612011-01-20 07:57:12 +00002840 }
2841
Richard Smithd62306a2011-11-10 06:34:14 +00002842 assert(CachedFieldIndex && "failed to find field in parent");
2843 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002844}
2845
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002846SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002847 if (const Expr *E = InitializerOrBitWidth.getPointer())
2848 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002849 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002850}
2851
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002852void FieldDecl::setBitWidth(Expr *Width) {
2853 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2854 "bit width or initializer already set");
2855 InitializerOrBitWidth.setPointer(Width);
2856}
2857
Richard Smith938f40b2011-06-11 17:19:42 +00002858void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002859 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002860 "bit width or initializer already set");
2861 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002862}
2863
Sebastian Redl833ef452010-01-26 22:01:41 +00002864//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002865// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002866//===----------------------------------------------------------------------===//
2867
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002868SourceLocation TagDecl::getOuterLocStart() const {
2869 return getTemplateOrInnerLocStart(this);
2870}
2871
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002872SourceRange TagDecl::getSourceRange() const {
2873 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002874 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002875}
2876
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002877TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002878 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002879}
2880
Rafael Espindolabf5c33b2013-03-12 21:06:00 +00002881void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2882 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002883 if (TypeForDecl)
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002884 assert(TypeForDecl->isLinkageValid());
2885 assert(isLinkageValid());
Douglas Gregora72a4e32010-05-19 18:39:18 +00002886}
2887
Douglas Gregordee1be82009-01-17 00:42:38 +00002888void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002889 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002890
David Blaikie095deba2012-11-14 01:52:05 +00002891 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002892 struct CXXRecordDecl::DefinitionData *Data =
2893 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002894 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2895 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002896 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002897}
2898
2899void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002900 assert((!isa<CXXRecordDecl>(this) ||
2901 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2902 "definition completed but not started");
2903
John McCallf937c022011-10-07 06:10:15 +00002904 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002905 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002906
2907 if (ASTMutationListener *L = getASTMutationListener())
2908 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002909}
2910
John McCallf937c022011-10-07 06:10:15 +00002911TagDecl *TagDecl::getDefinition() const {
2912 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002913 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002914
2915 // If it's possible for us to have an out-of-date definition, check now.
2916 if (MayHaveOutOfDateDef) {
2917 if (IdentifierInfo *II = getIdentifier()) {
2918 if (II->isOutOfDate()) {
2919 updateOutOfDate(*II);
2920 }
2921 }
2922 }
2923
Andrew Trickba266ee2010-10-19 21:54:32 +00002924 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2925 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002926
2927 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002928 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002929 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002930 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002931
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002932 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002933}
2934
Douglas Gregor14454802011-02-25 02:25:35 +00002935void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2936 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002937 // Make sure the extended qualifier info is allocated.
2938 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002939 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002940 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002941 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002942 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002943 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002944 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002945 if (getExtInfo()->NumTemplParamLists == 0) {
2946 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002947 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002948 }
2949 else
2950 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002951 }
2952 }
2953}
2954
Abramo Bagnara60804e12011-03-18 15:16:37 +00002955void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2956 unsigned NumTPLists,
2957 TemplateParameterList **TPLists) {
2958 assert(NumTPLists > 0);
2959 // Make sure the extended decl info is allocated.
2960 if (!hasExtInfo())
2961 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002962 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002963 // Set the template parameter lists info.
2964 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2965}
2966
Ted Kremenek21475702008-09-05 17:16:31 +00002967//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002968// EnumDecl Implementation
2969//===----------------------------------------------------------------------===//
2970
David Blaikie68e081d2011-12-20 02:48:34 +00002971void EnumDecl::anchor() { }
2972
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002973EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2974 SourceLocation StartLoc, SourceLocation IdLoc,
2975 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002976 EnumDecl *PrevDecl, bool IsScoped,
2977 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002978 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002979 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002980 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00002981 C.getTypeDeclType(Enum, PrevDecl);
2982 return Enum;
2983}
2984
Douglas Gregor72172e92012-01-05 21:55:30 +00002985EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2986 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002987 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
2988 0, 0, false, false, false);
2989 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
2990 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002991}
2992
Douglas Gregord5058122010-02-11 01:19:42 +00002993void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002994 QualType NewPromotionType,
2995 unsigned NumPositiveBits,
2996 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002997 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002998 if (!IntegerType)
2999 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00003000 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00003001 setNumPositiveBits(NumPositiveBits);
3002 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00003003 TagDecl::completeDefinition();
3004}
3005
Richard Smith7d137e32012-03-23 03:33:32 +00003006TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3007 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3008 return MSI->getTemplateSpecializationKind();
3009
3010 return TSK_Undeclared;
3011}
3012
3013void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3014 SourceLocation PointOfInstantiation) {
3015 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3016 assert(MSI && "Not an instantiated member enumeration?");
3017 MSI->setTemplateSpecializationKind(TSK);
3018 if (TSK != TSK_ExplicitSpecialization &&
3019 PointOfInstantiation.isValid() &&
3020 MSI->getPointOfInstantiation().isInvalid())
3021 MSI->setPointOfInstantiation(PointOfInstantiation);
3022}
3023
Richard Smith4b38ded2012-03-14 23:13:10 +00003024EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3025 if (SpecializationInfo)
3026 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3027
3028 return 0;
3029}
3030
3031void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3032 TemplateSpecializationKind TSK) {
3033 assert(!SpecializationInfo && "Member enum is already a specialization");
3034 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3035}
3036
Sebastian Redl833ef452010-01-26 22:01:41 +00003037//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00003038// RecordDecl Implementation
3039//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00003040
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003041RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
3042 SourceLocation StartLoc, SourceLocation IdLoc,
3043 IdentifierInfo *Id, RecordDecl *PrevDecl)
3044 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00003045 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003046 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003047 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003048 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003049 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00003050 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00003051}
3052
Jay Foad39c79802011-01-12 09:06:06 +00003053RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003054 SourceLocation StartLoc, SourceLocation IdLoc,
3055 IdentifierInfo *Id, RecordDecl* PrevDecl) {
3056 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
3057 PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003058 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3059
Ted Kremenek21475702008-09-05 17:16:31 +00003060 C.getTypeDeclType(R, PrevDecl);
3061 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00003062}
3063
Douglas Gregor72172e92012-01-05 21:55:30 +00003064RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
3065 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003066 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
3067 SourceLocation(), 0, 0);
3068 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3069 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003070}
3071
Douglas Gregordfcad112009-03-25 15:59:44 +00003072bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00003073 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00003074 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3075}
3076
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003077RecordDecl::field_iterator RecordDecl::field_begin() const {
3078 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3079 LoadFieldsFromExternalStorage();
3080
3081 return field_iterator(decl_iterator(FirstDecl));
3082}
3083
Douglas Gregorb11aad82011-02-19 18:51:44 +00003084/// completeDefinition - Notes that the definition of this type is now
3085/// complete.
3086void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00003087 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00003088 TagDecl::completeDefinition();
3089}
3090
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003091/// isMsStruct - Get whether or not this record uses ms_struct layout.
3092/// This which can be turned on with an attribute, pragma, or the
3093/// -mms-bitfields command-line option.
3094bool RecordDecl::isMsStruct(const ASTContext &C) const {
3095 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
3096}
3097
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003098static bool isFieldOrIndirectField(Decl::Kind K) {
3099 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3100}
3101
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003102void RecordDecl::LoadFieldsFromExternalStorage() const {
3103 ExternalASTSource *Source = getASTContext().getExternalSource();
3104 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3105
3106 // Notify that we have a RecordDecl doing some initialization.
3107 ExternalASTSource::Deserializing TheFields(Source);
3108
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003109 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003110 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003111 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
3112 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003113 case ELR_Success:
3114 break;
3115
3116 case ELR_AlreadyLoaded:
3117 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003118 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003119 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003120
3121#ifndef NDEBUG
3122 // Check that all decls we got were FieldDecls.
3123 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003124 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003125#endif
3126
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003127 if (Decls.empty())
3128 return;
3129
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00003130 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
3131 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003132}
3133
Steve Naroff415d3d52008-10-08 17:01:13 +00003134//===----------------------------------------------------------------------===//
3135// BlockDecl Implementation
3136//===----------------------------------------------------------------------===//
3137
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003138void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00003139 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00003140
Steve Naroffc4b30e52009-03-13 16:56:44 +00003141 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00003142 if (!NewParamInfo.empty()) {
3143 NumParams = NewParamInfo.size();
3144 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3145 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003146 }
3147}
3148
John McCall351762c2011-02-07 10:33:21 +00003149void BlockDecl::setCaptures(ASTContext &Context,
3150 const Capture *begin,
3151 const Capture *end,
3152 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00003153 CapturesCXXThis = capturesCXXThis;
3154
3155 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00003156 NumCaptures = 0;
3157 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00003158 return;
3159 }
3160
John McCall351762c2011-02-07 10:33:21 +00003161 NumCaptures = end - begin;
3162
3163 // Avoid new Capture[] because we don't want to provide a default
3164 // constructor.
3165 size_t allocationSize = NumCaptures * sizeof(Capture);
3166 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
3167 memcpy(buffer, begin, allocationSize);
3168 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003169}
Sebastian Redl833ef452010-01-26 22:01:41 +00003170
John McCallce45f882011-06-15 22:51:16 +00003171bool BlockDecl::capturesVariable(const VarDecl *variable) const {
3172 for (capture_const_iterator
3173 i = capture_begin(), e = capture_end(); i != e; ++i)
3174 // Only auto vars can be captured, so no redeclaration worries.
3175 if (i->getVariable() == variable)
3176 return true;
3177
3178 return false;
3179}
3180
Douglas Gregor70226da2010-12-21 16:27:07 +00003181SourceRange BlockDecl::getSourceRange() const {
3182 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
3183}
Sebastian Redl833ef452010-01-26 22:01:41 +00003184
3185//===----------------------------------------------------------------------===//
3186// Other Decl Allocation/Deallocation Method Implementations
3187//===----------------------------------------------------------------------===//
3188
David Blaikie68e081d2011-12-20 02:48:34 +00003189void TranslationUnitDecl::anchor() { }
3190
Sebastian Redl833ef452010-01-26 22:01:41 +00003191TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
3192 return new (C) TranslationUnitDecl(C);
3193}
3194
David Blaikie68e081d2011-12-20 02:48:34 +00003195void LabelDecl::anchor() { }
3196
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003197LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00003198 SourceLocation IdentL, IdentifierInfo *II) {
3199 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
3200}
3201
3202LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3203 SourceLocation IdentL, IdentifierInfo *II,
3204 SourceLocation GnuLabelL) {
3205 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3206 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003207}
3208
Douglas Gregor72172e92012-01-05 21:55:30 +00003209LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3210 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3211 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00003212}
3213
David Blaikie68e081d2011-12-20 02:48:34 +00003214void ValueDecl::anchor() { }
3215
Benjamin Kramerea70eb32012-12-01 15:09:41 +00003216bool ValueDecl::isWeak() const {
3217 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3218 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3219 return true;
3220
3221 return isWeakImported();
3222}
3223
David Blaikie68e081d2011-12-20 02:48:34 +00003224void ImplicitParamDecl::anchor() { }
3225
Sebastian Redl833ef452010-01-26 22:01:41 +00003226ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003227 SourceLocation IdLoc,
3228 IdentifierInfo *Id,
3229 QualType Type) {
3230 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00003231}
3232
Douglas Gregor72172e92012-01-05 21:55:30 +00003233ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3234 unsigned ID) {
3235 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3236 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3237}
3238
Sebastian Redl833ef452010-01-26 22:01:41 +00003239FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003240 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003241 const DeclarationNameInfo &NameInfo,
3242 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003243 StorageClass SC,
Douglas Gregorff76cb92010-12-09 16:59:22 +00003244 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00003245 bool hasWrittenPrototype,
3246 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003247 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003248 T, TInfo, SC,
Richard Smitha77a0a62011-08-15 21:04:07 +00003249 isInlineSpecified,
3250 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00003251 New->HasWrittenPrototype = hasWrittenPrototype;
3252 return New;
3253}
3254
Douglas Gregor72172e92012-01-05 21:55:30 +00003255FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3256 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3257 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3258 DeclarationNameInfo(), QualType(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003259 SC_None, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00003260}
3261
Sebastian Redl833ef452010-01-26 22:01:41 +00003262BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3263 return new (C) BlockDecl(DC, L);
3264}
3265
Douglas Gregor72172e92012-01-05 21:55:30 +00003266BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3267 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3268 return new (Mem) BlockDecl(0, SourceLocation());
3269}
3270
John McCall5e77d762013-04-16 07:28:30 +00003271MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3272 unsigned ID) {
3273 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(MSPropertyDecl));
3274 return new (Mem) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
3275 QualType(), 0, SourceLocation(),
3276 0, 0);
3277}
3278
Ben Langmuir37943a72013-05-03 19:00:33 +00003279CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
3280 unsigned NumParams) {
Ben Langmuirce914fc2013-05-03 19:20:19 +00003281 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
Ben Langmuir37943a72013-05-03 19:00:33 +00003282 return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003283}
3284
Ben Langmuirce914fc2013-05-03 19:20:19 +00003285CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3286 unsigned NumParams) {
3287 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3288 void *Mem = AllocateDeserializedDecl(C, ID, Size);
3289 return new (Mem) CapturedDecl(0, NumParams);
3290}
3291
Sebastian Redl833ef452010-01-26 22:01:41 +00003292EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3293 SourceLocation L,
3294 IdentifierInfo *Id, QualType T,
3295 Expr *E, const llvm::APSInt &V) {
3296 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3297}
3298
Douglas Gregor72172e92012-01-05 21:55:30 +00003299EnumConstantDecl *
3300EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3301 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3302 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3303 llvm::APSInt());
3304}
3305
David Blaikie68e081d2011-12-20 02:48:34 +00003306void IndirectFieldDecl::anchor() { }
3307
Benjamin Kramer39593702010-11-21 14:11:41 +00003308IndirectFieldDecl *
3309IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3310 IdentifierInfo *Id, QualType T, NamedDecl **CH,
3311 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003312 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3313}
3314
Douglas Gregor72172e92012-01-05 21:55:30 +00003315IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3316 unsigned ID) {
3317 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3318 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3319 QualType(), 0, 0);
3320}
3321
Douglas Gregorbe996932010-09-01 20:41:53 +00003322SourceRange EnumConstantDecl::getSourceRange() const {
3323 SourceLocation End = getLocation();
3324 if (Init)
3325 End = Init->getLocEnd();
3326 return SourceRange(getLocation(), End);
3327}
3328
David Blaikie68e081d2011-12-20 02:48:34 +00003329void TypeDecl::anchor() { }
3330
Sebastian Redl833ef452010-01-26 22:01:41 +00003331TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003332 SourceLocation StartLoc, SourceLocation IdLoc,
3333 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3334 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003335}
3336
David Blaikie68e081d2011-12-20 02:48:34 +00003337void TypedefNameDecl::anchor() { }
3338
Douglas Gregor72172e92012-01-05 21:55:30 +00003339TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3340 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3341 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3342}
3343
Richard Smithdda56e42011-04-15 14:24:37 +00003344TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3345 SourceLocation StartLoc,
3346 SourceLocation IdLoc, IdentifierInfo *Id,
3347 TypeSourceInfo *TInfo) {
3348 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3349}
3350
Douglas Gregor72172e92012-01-05 21:55:30 +00003351TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3352 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3353 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3354}
3355
Abramo Bagnaraea947882011-03-08 16:41:52 +00003356SourceRange TypedefDecl::getSourceRange() const {
3357 SourceLocation RangeEnd = getLocation();
3358 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3359 if (typeIsPostfix(TInfo->getType()))
3360 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3361 }
3362 return SourceRange(getLocStart(), RangeEnd);
3363}
3364
Richard Smithdda56e42011-04-15 14:24:37 +00003365SourceRange TypeAliasDecl::getSourceRange() const {
3366 SourceLocation RangeEnd = getLocStart();
3367 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3368 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3369 return SourceRange(getLocStart(), RangeEnd);
3370}
3371
David Blaikie68e081d2011-12-20 02:48:34 +00003372void FileScopeAsmDecl::anchor() { }
3373
Sebastian Redl833ef452010-01-26 22:01:41 +00003374FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003375 StringLiteral *Str,
3376 SourceLocation AsmLoc,
3377 SourceLocation RParenLoc) {
3378 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003379}
Douglas Gregorba345522011-12-02 23:23:56 +00003380
Douglas Gregor72172e92012-01-05 21:55:30 +00003381FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3382 unsigned ID) {
3383 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3384 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3385}
3386
Michael Han84324352013-02-22 17:15:32 +00003387void EmptyDecl::anchor() {}
3388
3389EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3390 return new (C) EmptyDecl(DC, L);
3391}
3392
3393EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3394 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EmptyDecl));
3395 return new (Mem) EmptyDecl(0, SourceLocation());
3396}
3397
Douglas Gregorba345522011-12-02 23:23:56 +00003398//===----------------------------------------------------------------------===//
3399// ImportDecl Implementation
3400//===----------------------------------------------------------------------===//
3401
3402/// \brief Retrieve the number of module identifiers needed to name the given
3403/// module.
3404static unsigned getNumModuleIdentifiers(Module *Mod) {
3405 unsigned Result = 1;
3406 while (Mod->Parent) {
3407 Mod = Mod->Parent;
3408 ++Result;
3409 }
3410 return Result;
3411}
3412
Douglas Gregor22d09742012-01-03 18:04:46 +00003413ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003414 Module *Imported,
3415 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003416 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003417 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003418{
3419 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3420 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3421 memcpy(StoredLocs, IdentifierLocs.data(),
3422 IdentifierLocs.size() * sizeof(SourceLocation));
3423}
3424
Douglas Gregor22d09742012-01-03 18:04:46 +00003425ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003426 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003427 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003428 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003429{
3430 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3431}
3432
3433ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003434 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003435 ArrayRef<SourceLocation> IdentifierLocs) {
3436 void *Mem = C.Allocate(sizeof(ImportDecl) +
3437 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003438 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003439}
3440
3441ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003442 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003443 Module *Imported,
3444 SourceLocation EndLoc) {
3445 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003446 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003447 Import->setImplicit();
3448 return Import;
3449}
3450
Douglas Gregor72172e92012-01-05 21:55:30 +00003451ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3452 unsigned NumLocations) {
3453 void *Mem = AllocateDeserializedDecl(C, ID,
3454 (sizeof(ImportDecl) +
3455 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003456 return new (Mem) ImportDecl(EmptyShell());
3457}
3458
3459ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3460 if (!ImportedAndComplete.getInt())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00003461 return None;
Douglas Gregorba345522011-12-02 23:23:56 +00003462
3463 const SourceLocation *StoredLocs
3464 = reinterpret_cast<const SourceLocation *>(this + 1);
3465 return ArrayRef<SourceLocation>(StoredLocs,
3466 getNumModuleIdentifiers(getImportedModule()));
3467}
3468
3469SourceRange ImportDecl::getSourceRange() const {
3470 if (!ImportedAndComplete.getInt())
3471 return SourceRange(getLocation(),
3472 *reinterpret_cast<const SourceLocation *>(this + 1));
3473
3474 return SourceRange(getLocation(), getIdentifierLocs().back());
3475}