blob: fd0f72513767088cd9c3ee7502648a701dc57758 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000024#include "clang/AST/Stmt.h"
25#include "clang/AST/TypeLoc.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000027#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000028#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000029#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000030#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000031#include "llvm/Support/ErrorHandling.h"
John McCall5f46c482013-02-21 23:42:58 +000032#include "llvm/Support/type_traits.h"
David Blaikie9c70e042011-09-21 18:16:56 +000033#include <algorithm>
34
Chris Lattner6d9a6852006-10-25 05:11:20 +000035using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000036
Chris Lattner88f70d62008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
John McCalldf25c432013-02-16 00:17:33 +000041// Visibility rules aren't rigorously externally specified, but here
42// are the basic principles behind what we implement:
43//
44// 1. An explicit visibility attribute is generally a direct expression
45// of the user's intent and should be honored. Only the innermost
46// visibility attribute applies. If no visibility attribute applies,
47// global visibility settings are considered.
48//
49// 2. There is one caveat to the above: on or in a template pattern,
50// an explicit visibility attribute is just a default rule, and
51// visibility can be decreased by the visibility of template
52// arguments. But this, too, has an exception: an attribute on an
53// explicit specialization or instantiation causes all the visibility
54// restrictions of the template arguments to be ignored.
55//
56// 3. A variable that does not otherwise have explicit visibility can
57// be restricted by the visibility of its type.
58//
59// 4. A visibility restriction is explicit if it comes from an
60// attribute (or something like it), not a global visibility setting.
61// When emitting a reference to an external symbol, visibility
62// restrictions are ignored unless they are explicit.
John McCalld041a9b2013-02-20 01:54:26 +000063//
64// 5. When computing the visibility of a non-type, including a
65// non-type member of a class, only non-type visibility restrictions
66// are considered: the 'visibility' attribute, global value-visibility
67// settings, and a few special cases like __private_extern.
68//
69// 6. When computing the visibility of a type, including a type member
70// of a class, only type visibility restrictions are considered:
71// the 'type_visibility' attribute and global type-visibility settings.
72// However, a 'visibility' attribute counts as a 'type_visibility'
73// attribute on any declaration that only has the former.
74//
75// The visibility of a "secondary" entity, like a template argument,
76// is computed using the kind of that entity, not the kind of the
77// primary entity for which we are computing visibility. For example,
78// the visibility of a specialization of either of these templates:
79// template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X);
80// template <class T, bool (&compare)(T, X)> class matcher;
81// is restricted according to the type visibility of the argument 'T',
82// the type visibility of 'bool(&)(T,X)', and the value visibility of
83// the argument function 'compare'. That 'has_match' is a value
84// and 'matcher' is a type only matters when looking for attributes
85// and settings from the immediate context.
John McCalldf25c432013-02-16 00:17:33 +000086
John McCall5f46c482013-02-21 23:42:58 +000087const unsigned IgnoreExplicitVisibilityBit = 2;
88
John McCalldf25c432013-02-16 00:17:33 +000089/// Kinds of LV computation. The linkage side of the computation is
90/// always the same, but different things can change how visibility is
91/// computed.
92enum LVComputationKind {
John McCall5f46c482013-02-21 23:42:58 +000093 /// Do an LV computation for, ultimately, a type.
94 /// Visibility may be restricted by type visibility settings and
95 /// the visibility of template arguments.
John McCalld041a9b2013-02-20 01:54:26 +000096 LVForType = NamedDecl::VisibilityForType,
John McCalldf25c432013-02-16 00:17:33 +000097
John McCall5f46c482013-02-21 23:42:58 +000098 /// Do an LV computation for, ultimately, a non-type declaration.
99 /// Visibility may be restricted by value visibility settings and
100 /// the visibility of template arguments.
John McCalld041a9b2013-02-20 01:54:26 +0000101 LVForValue = NamedDecl::VisibilityForValue,
102
John McCall5f46c482013-02-21 23:42:58 +0000103 /// Do an LV computation for, ultimately, a type that already has
104 /// some sort of explicit visibility. Visibility may only be
105 /// restricted by the visibility of template arguments.
106 LVForExplicitType = (LVForType | IgnoreExplicitVisibilityBit),
John McCalld041a9b2013-02-20 01:54:26 +0000107
John McCall5f46c482013-02-21 23:42:58 +0000108 /// Do an LV computation for, ultimately, a non-type declaration
109 /// that already has some sort of explicit visibility. Visibility
110 /// may only be restricted by the visibility of template arguments.
111 LVForExplicitValue = (LVForValue | IgnoreExplicitVisibilityBit)
John McCalldf25c432013-02-16 00:17:33 +0000112};
113
John McCalld041a9b2013-02-20 01:54:26 +0000114/// Does this computation kind permit us to consider additional
115/// visibility settings from attributes and the like?
116static bool hasExplicitVisibilityAlready(LVComputationKind computation) {
John McCall5f46c482013-02-21 23:42:58 +0000117 return ((unsigned(computation) & IgnoreExplicitVisibilityBit) != 0);
John McCalld041a9b2013-02-20 01:54:26 +0000118}
119
120/// Given an LVComputationKind, return one of the same type/value sort
121/// that records that it already has explicit visibility.
122static LVComputationKind
123withExplicitVisibilityAlready(LVComputationKind oldKind) {
124 LVComputationKind newKind =
John McCall5f46c482013-02-21 23:42:58 +0000125 static_cast<LVComputationKind>(unsigned(oldKind) |
126 IgnoreExplicitVisibilityBit);
John McCalld041a9b2013-02-20 01:54:26 +0000127 assert(oldKind != LVForType || newKind == LVForExplicitType);
128 assert(oldKind != LVForValue || newKind == LVForExplicitValue);
129 assert(oldKind != LVForExplicitType || newKind == LVForExplicitType);
130 assert(oldKind != LVForExplicitValue || newKind == LVForExplicitValue);
131 return newKind;
132}
133
David Blaikie05785d12013-02-20 22:23:23 +0000134static Optional<Visibility> getExplicitVisibility(const NamedDecl *D,
135 LVComputationKind kind) {
John McCalld041a9b2013-02-20 01:54:26 +0000136 assert(!hasExplicitVisibilityAlready(kind) &&
137 "asking for explicit visibility when we shouldn't be");
138 return D->getExplicitVisibility((NamedDecl::ExplicitVisibilityKind) kind);
139}
140
John McCalldf25c432013-02-16 00:17:33 +0000141/// Is the given declaration a "type" or a "value" for the purposes of
142/// visibility computation?
143static bool usesTypeVisibility(const NamedDecl *D) {
John McCallb4a99d32013-02-19 01:57:35 +0000144 return isa<TypeDecl>(D) ||
145 isa<ClassTemplateDecl>(D) ||
146 isa<ObjCInterfaceDecl>(D);
John McCalldf25c432013-02-16 00:17:33 +0000147}
148
John McCall5f46c482013-02-21 23:42:58 +0000149/// Does the given declaration have member specialization information,
150/// and if so, is it an explicit specialization?
151template <class T> static typename
152llvm::enable_if_c<!llvm::is_base_of<RedeclarableTemplateDecl, T>::value,
153 bool>::type
154isExplicitMemberSpecialization(const T *D) {
155 if (const MemberSpecializationInfo *member =
156 D->getMemberSpecializationInfo()) {
157 return member->isExplicitSpecialization();
158 }
159 return false;
160}
161
162/// For templates, this question is easier: a member template can't be
163/// explicitly instantiated, so there's a single bit indicating whether
164/// or not this is an explicit member specialization.
165static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) {
166 return D->isMemberSpecialization();
167}
168
John McCalld041a9b2013-02-20 01:54:26 +0000169/// Given a visibility attribute, return the explicit visibility
170/// associated with it.
171template <class T>
172static Visibility getVisibilityFromAttr(const T *attr) {
173 switch (attr->getVisibility()) {
174 case T::Default:
175 return DefaultVisibility;
176 case T::Hidden:
177 return HiddenVisibility;
178 case T::Protected:
179 return ProtectedVisibility;
180 }
181 llvm_unreachable("bad visibility kind");
182}
183
John McCalldf25c432013-02-16 00:17:33 +0000184/// Return the explicit visibility of the given declaration.
David Blaikie05785d12013-02-20 22:23:23 +0000185static Optional<Visibility> getVisibilityOf(const NamedDecl *D,
John McCalld041a9b2013-02-20 01:54:26 +0000186 NamedDecl::ExplicitVisibilityKind kind) {
187 // If we're ultimately computing the visibility of a type, look for
188 // a 'type_visibility' attribute before looking for 'visibility'.
189 if (kind == NamedDecl::VisibilityForType) {
190 if (const TypeVisibilityAttr *A = D->getAttr<TypeVisibilityAttr>()) {
191 return getVisibilityFromAttr(A);
192 }
193 }
194
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000195 // If this declaration has an explicit visibility attribute, use it.
196 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
John McCalld041a9b2013-02-20 01:54:26 +0000197 return getVisibilityFromAttr(A);
John McCall457a04e2010-10-22 21:05:15 +0000198 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000199
200 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
201 // implies visibility(default).
Douglas Gregore8bbc122011-09-02 00:18:52 +0000202 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000203 for (specific_attr_iterator<AvailabilityAttr>
204 A = D->specific_attr_begin<AvailabilityAttr>(),
205 AEnd = D->specific_attr_end<AvailabilityAttr>();
206 A != AEnd; ++A)
207 if ((*A)->getPlatform()->getName().equals("macosx"))
208 return DefaultVisibility;
209 }
210
David Blaikie7a30dc52013-02-21 01:47:18 +0000211 return None;
John McCall457a04e2010-10-22 21:05:15 +0000212}
213
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000214/// \brief Get the most restrictive linkage for the types in the given
John McCalldf25c432013-02-16 00:17:33 +0000215/// template parameter list. For visibility purposes, template
216/// parameters are part of the signature of a template.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000217static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000218getLVForTemplateParameterList(const TemplateParameterList *params) {
219 LinkageInfo LV;
220 for (TemplateParameterList::const_iterator P = params->begin(),
221 PEnd = params->end();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000222 P != PEnd; ++P) {
John McCalldf25c432013-02-16 00:17:33 +0000223
224 // Template type parameters are the most common and never
225 // contribute to visibility, pack or not.
226 if (isa<TemplateTypeParmDecl>(*P))
227 continue;
228
229 // Non-type template parameters can be restricted by the value type, e.g.
230 // template <enum X> class A { ... };
231 // We have to be careful here, though, because we can be dealing with
232 // dependent types.
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000233 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
John McCalldf25c432013-02-16 00:17:33 +0000234 // Handle the non-pack case first.
235 if (!NTTP->isExpandedParameterPack()) {
236 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000237 LV.merge(NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000238 }
239 continue;
240 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000241
John McCalldf25c432013-02-16 00:17:33 +0000242 // Look at all the types in an expanded pack.
243 for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) {
244 QualType type = NTTP->getExpansionType(i);
245 if (!type->isDependentType())
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000246 LV.merge(type->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000247 }
John McCalldf25c432013-02-16 00:17:33 +0000248 continue;
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000249 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000250
John McCalldf25c432013-02-16 00:17:33 +0000251 // Template template parameters can be restricted by their
252 // template parameters, recursively.
253 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
254
255 // Handle the non-pack case first.
256 if (!TTP->isExpandedParameterPack()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000257 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
John McCalldf25c432013-02-16 00:17:33 +0000258 continue;
259 }
260
261 // Look at all expansions in an expanded pack.
262 for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters();
263 i != n; ++i) {
264 LV.merge(getLVForTemplateParameterList(
265 TTP->getExpansionTemplateParameters(i)));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000266 }
267 }
268
John McCall457a04e2010-10-22 21:05:15 +0000269 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000270}
271
Rafael Espindola19de5612013-01-12 06:42:30 +0000272/// getLVForDecl - Get the linkage and visibility for the given declaration.
John McCalldf25c432013-02-16 00:17:33 +0000273static LinkageInfo getLVForDecl(const NamedDecl *D,
274 LVComputationKind computation);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000275
Rafael Espindolac1b38a22013-05-16 04:30:21 +0000276static const FunctionDecl *getOutermostFunctionContext(const Decl *D) {
277 const FunctionDecl *Ret = NULL;
278 const DeclContext *DC = D->getDeclContext();
279 while (DC->getDeclKind() != Decl::TranslationUnit) {
280 const FunctionDecl *F = dyn_cast<FunctionDecl>(DC);
281 if (F)
282 Ret = F;
283 DC = DC->getParent();
284 }
285 return Ret;
286}
287
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000288/// \brief Get the most restrictive linkage for the types and
289/// declarations in the given template argument list.
John McCalldf25c432013-02-16 00:17:33 +0000290///
291/// Note that we don't take an LVComputationKind because we always
292/// want to honor the visibility of template arguments in the same way.
293static LinkageInfo
294getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args) {
295 LinkageInfo LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000296
John McCalldf25c432013-02-16 00:17:33 +0000297 for (unsigned i = 0, e = args.size(); i != e; ++i) {
298 const TemplateArgument &arg = args[i];
299 switch (arg.getKind()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000300 case TemplateArgument::Null:
301 case TemplateArgument::Integral:
302 case TemplateArgument::Expression:
John McCalldf25c432013-02-16 00:17:33 +0000303 continue;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000304
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000305 case TemplateArgument::Type:
Rafael Espindola50df3a02013-05-25 17:16:20 +0000306 LV.merge(arg.getAsType()->getLinkageAndVisibility());
John McCalldf25c432013-02-16 00:17:33 +0000307 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000308
309 case TemplateArgument::Declaration:
John McCalldf25c432013-02-16 00:17:33 +0000310 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
311 assert(!usesTypeVisibility(ND));
312 LV.merge(getLVForDecl(ND, LVForValue));
313 }
314 continue;
Eli Friedmanb826a002012-09-26 02:36:12 +0000315
316 case TemplateArgument::NullPtr:
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000317 LV.merge(arg.getNullPtrType()->getLinkageAndVisibility());
John McCalldf25c432013-02-16 00:17:33 +0000318 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000319
320 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000321 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000322 if (TemplateDecl *Template
John McCalldf25c432013-02-16 00:17:33 +0000323 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
324 LV.merge(getLVForDecl(Template, LVForValue));
325 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000326
327 case TemplateArgument::Pack:
John McCalldf25c432013-02-16 00:17:33 +0000328 LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray()));
329 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000330 }
John McCalldf25c432013-02-16 00:17:33 +0000331 llvm_unreachable("bad template argument kind");
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000332 }
333
John McCall457a04e2010-10-22 21:05:15 +0000334 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000335}
336
Rafael Espindola2f869a32012-01-14 00:30:36 +0000337static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000338getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
339 return getLVForTemplateArgumentList(TArgs.asArray());
John McCall8823c652010-08-13 08:35:10 +0000340}
341
John McCall5f46c482013-02-21 23:42:58 +0000342static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
343 const FunctionTemplateSpecializationInfo *specInfo) {
344 // Include visibility from the template parameters and arguments
345 // only if this is not an explicit instantiation or specialization
346 // with direct explicit visibility. (Implicit instantiations won't
347 // have a direct attribute.)
348 if (!specInfo->isExplicitInstantiationOrSpecialization())
349 return true;
350
351 return !fn->hasAttr<VisibilityAttr>();
352}
353
John McCalldf25c432013-02-16 00:17:33 +0000354/// Merge in template-related linkage and visibility for the given
355/// function template specialization.
356///
357/// We don't need a computation kind here because we can assume
358/// LVForValue.
John McCall5f46c482013-02-21 23:42:58 +0000359///
NAKAMURA Takumi62eae082013-02-22 04:06:28 +0000360/// \param[out] LV the computation to use for the parent
John McCall5f46c482013-02-21 23:42:58 +0000361static void
362mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
363 const FunctionTemplateSpecializationInfo *specInfo) {
364 bool considerVisibility =
365 shouldConsiderTemplateVisibility(fn, specInfo);
John McCalldf25c432013-02-16 00:17:33 +0000366
367 // Merge information from the template parameters.
John McCall5f46c482013-02-21 23:42:58 +0000368 FunctionTemplateDecl *temp = specInfo->getTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000369 LinkageInfo tempLV =
370 getLVForTemplateParameterList(temp->getTemplateParameters());
371 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
372
373 // Merge information from the template arguments.
374 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
375 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
376 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000377}
378
John McCall5f46c482013-02-21 23:42:58 +0000379/// Does the given declaration have a direct visibility attribute
380/// that would match the given rules?
381static bool hasDirectVisibilityAttribute(const NamedDecl *D,
382 LVComputationKind computation) {
383 switch (computation) {
384 case LVForType:
385 case LVForExplicitType:
386 if (D->hasAttr<TypeVisibilityAttr>())
387 return true;
388 // fallthrough
389 case LVForValue:
390 case LVForExplicitValue:
391 if (D->hasAttr<VisibilityAttr>())
392 return true;
393 return false;
394 }
395 llvm_unreachable("bad visibility computation kind");
396}
397
John McCalld041a9b2013-02-20 01:54:26 +0000398/// Should we consider visibility associated with the template
399/// arguments and parameters of the given class template specialization?
400static bool shouldConsiderTemplateVisibility(
401 const ClassTemplateSpecializationDecl *spec,
402 LVComputationKind computation) {
John McCalldf25c432013-02-16 00:17:33 +0000403 // Include visibility from the template parameters and arguments
404 // only if this is not an explicit instantiation or specialization
405 // with direct explicit visibility (and note that implicit
406 // instantiations won't have a direct attribute).
407 //
408 // Furthermore, we want to ignore template parameters and arguments
John McCalld041a9b2013-02-20 01:54:26 +0000409 // for an explicit specialization when computing the visibility of a
410 // member thereof with explicit visibility.
John McCalldf25c432013-02-16 00:17:33 +0000411 //
412 // This is a bit complex; let's unpack it.
413 //
414 // An explicit class specialization is an independent, top-level
415 // declaration. As such, if it or any of its members has an
416 // explicit visibility attribute, that must directly express the
417 // user's intent, and we should honor it. The same logic applies to
418 // an explicit instantiation of a member of such a thing.
John McCalld041a9b2013-02-20 01:54:26 +0000419
420 // Fast path: if this is not an explicit instantiation or
421 // specialization, we always want to consider template-related
422 // visibility restrictions.
423 if (!spec->isExplicitInstantiationOrSpecialization())
424 return true;
425
426 // This is the 'member thereof' check.
427 if (spec->isExplicitSpecialization() &&
428 hasExplicitVisibilityAlready(computation))
429 return false;
430
John McCall5f46c482013-02-21 23:42:58 +0000431 return !hasDirectVisibilityAttribute(spec, computation);
John McCalld041a9b2013-02-20 01:54:26 +0000432}
433
434/// Merge in template-related linkage and visibility for the given
435/// class template specialization.
436static void mergeTemplateLV(LinkageInfo &LV,
437 const ClassTemplateSpecializationDecl *spec,
438 LVComputationKind computation) {
439 bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
John McCalldf25c432013-02-16 00:17:33 +0000440
441 // Merge information from the template parameters, but ignore
442 // visibility if we're only considering template arguments.
443
John McCalld041a9b2013-02-20 01:54:26 +0000444 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
John McCalldf25c432013-02-16 00:17:33 +0000445 LinkageInfo tempLV =
446 getLVForTemplateParameterList(temp->getTemplateParameters());
447 LV.mergeMaybeWithVisibility(tempLV,
John McCalld041a9b2013-02-20 01:54:26 +0000448 considerVisibility && !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000449
450 // Merge information from the template arguments. We ignore
451 // template-argument visibility if we've got an explicit
452 // instantiation with a visibility attribute.
453 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
454 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
455 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000456}
457
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000458static bool useInlineVisibilityHidden(const NamedDecl *D) {
459 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000460 const LangOptions &Opts = D->getASTContext().getLangOpts();
461 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000462 return false;
463
464 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
465 if (!FD)
466 return false;
467
468 TemplateSpecializationKind TSK = TSK_Undeclared;
469 if (FunctionTemplateSpecializationInfo *spec
470 = FD->getTemplateSpecializationInfo()) {
471 TSK = spec->getTemplateSpecializationKind();
472 } else if (MemberSpecializationInfo *MSI =
473 FD->getMemberSpecializationInfo()) {
474 TSK = MSI->getTemplateSpecializationKind();
475 }
476
477 const FunctionDecl *Def = 0;
478 // InlineVisibilityHidden only applies to definitions, and
479 // isInlined() only gives meaningful answers on definitions
480 // anyway.
481 return TSK != TSK_ExplicitInstantiationDeclaration &&
482 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000483 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000484}
485
Rafael Espindola593537a2013-05-05 20:15:21 +0000486template <typename T> static bool isFirstInExternCContext(T *D) {
Rafael Espindolaf4187652013-02-14 01:18:37 +0000487 const T *First = D->getFirstDeclaration();
Rafael Espindola593537a2013-05-05 20:15:21 +0000488 return First->isInExternCContext();
Rafael Espindolaf4187652013-02-14 01:18:37 +0000489}
490
Rafael Espindola327be3c2013-04-26 01:30:23 +0000491static bool isSingleLineExternC(const Decl &D) {
492 if (const LinkageSpecDecl *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
493 if (SD->getLanguage() == LinkageSpecDecl::lang_c && !SD->hasBraces())
494 return true;
495 return false;
496}
497
Rafael Espindola3ae00052013-05-13 00:12:11 +0000498static bool isExternalLinkage(Linkage L) {
499 return L == UniqueExternalLinkage || L == ExternalLinkage;
500}
501
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000502static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCalldf25c432013-02-16 00:17:33 +0000503 LVComputationKind computation) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000504 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000505 "Not a name having namespace scope");
506 ASTContext &Context = D->getASTContext();
507
508 // C++ [basic.link]p3:
509 // A name having namespace scope (3.3.6) has internal linkage if it
510 // is the name of
511 // - an object, reference, function or function template that is
512 // explicitly declared static; or,
513 // (This bullet corresponds to C99 6.2.2p3.)
514 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
515 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000516 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000517 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000518
Richard Smithdc0ef452012-10-19 06:37:48 +0000519 // - a non-volatile object or reference that is explicitly declared const
520 // or constexpr and neither explicitly declared extern nor previously
521 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000522 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000523 Var->getType().isConstQualified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000524 !Var->getType().isVolatileQualified()) {
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000525 const VarDecl *PrevVar = Var->getPreviousDecl();
Rafael Espindola985a3ab2013-04-03 19:22:20 +0000526 if (PrevVar)
Rafael Espindolaadea16b2013-04-03 15:50:00 +0000527 return PrevVar->getLinkageAndVisibility();
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000528
529 if (Var->getStorageClass() != SC_Extern &&
Rafael Espindola327be3c2013-04-26 01:30:23 +0000530 Var->getStorageClass() != SC_PrivateExtern &&
531 !isSingleLineExternC(*Var))
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000532 return LinkageInfo::internal();
533 }
534
535 for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar;
536 PrevVar = PrevVar->getPreviousDecl()) {
537 if (PrevVar->getStorageClass() == SC_PrivateExtern &&
538 Var->getStorageClass() == SC_None)
539 return PrevVar->getLinkageAndVisibility();
540 // Explicitly declared static.
541 if (PrevVar->getStorageClass() == SC_Static)
542 return LinkageInfo::internal();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000543 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000544 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000545 // C++ [temp]p4:
546 // A non-member function template can have internal linkage; any
547 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000548 const FunctionDecl *Function = 0;
549 if (const FunctionTemplateDecl *FunTmpl
550 = dyn_cast<FunctionTemplateDecl>(D))
551 Function = FunTmpl->getTemplatedDecl();
552 else
553 Function = cast<FunctionDecl>(D);
554
555 // Explicitly declared static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000556 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000557 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000558 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
559 // - a data member of an anonymous union.
560 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000561 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000562 }
563
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000564 if (D->isInAnonymousNamespace()) {
565 const VarDecl *Var = dyn_cast<VarDecl>(D);
566 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindola593537a2013-05-05 20:15:21 +0000567 if ((!Var || !isFirstInExternCContext(Var)) &&
568 (!Func || !isFirstInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000569 return LinkageInfo::uniqueExternal();
570 }
John McCallb7139c42010-10-28 04:18:25 +0000571
John McCall457a04e2010-10-22 21:05:15 +0000572 // Set up the defaults.
573
574 // C99 6.2.2p5:
575 // If the declaration of an identifier for an object has file
576 // scope and no storage-class specifier, its linkage is
577 // external.
John McCallc273f242010-10-30 11:50:40 +0000578 LinkageInfo LV;
579
John McCalld041a9b2013-02-20 01:54:26 +0000580 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000581 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000582 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000583 } else {
584 // If we're declared in a namespace with a visibility attribute,
John McCalldf25c432013-02-16 00:17:33 +0000585 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindola78158af2012-04-16 18:46:26 +0000586 for (const DeclContext *DC = D->getDeclContext();
587 !isa<TranslationUnitDecl>(DC);
588 DC = DC->getParent()) {
589 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
590 if (!ND) continue;
David Blaikie05785d12013-02-20 22:23:23 +0000591 if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000592 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000593 break;
594 }
595 }
596 }
Rafael Espindola78158af2012-04-16 18:46:26 +0000597
John McCalldf25c432013-02-16 00:17:33 +0000598 // Add in global settings if the above didn't give us direct visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000599 if (!LV.isVisibilityExplicit()) {
John McCallb4a99d32013-02-19 01:57:35 +0000600 // Use global type/value visibility as appropriate.
601 Visibility globalVisibility;
602 if (computation == LVForValue) {
603 globalVisibility = Context.getLangOpts().getValueVisibilityMode();
604 } else {
605 assert(computation == LVForType);
606 globalVisibility = Context.getLangOpts().getTypeVisibilityMode();
607 }
608 LV.mergeVisibility(globalVisibility, /*explicit*/ false);
John McCalldf25c432013-02-16 00:17:33 +0000609
610 // If we're paying attention to global visibility, apply
611 // -finline-visibility-hidden if this is an inline method.
612 if (useInlineVisibilityHidden(D))
613 LV.mergeVisibility(HiddenVisibility, true);
614 }
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000615 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000616
Douglas Gregorf73b2822009-11-25 22:24:25 +0000617 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000618
Douglas Gregorf73b2822009-11-25 22:24:25 +0000619 // A name having namespace scope has external linkage if it is the
620 // name of
621 //
622 // - an object or reference, unless it has internal linkage; or
623 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000624 // GCC applies the following optimization to variables and static
625 // data members, but not to functions:
626 //
John McCall457a04e2010-10-22 21:05:15 +0000627 // Modify the variable's LV by the LV of its type unless this is
628 // C or extern "C". This follows from [basic.link]p9:
629 // A type without linkage shall not be used as the type of a
630 // variable or function with external linkage unless
631 // - the entity has C language linkage, or
632 // - the entity is declared within an unnamed namespace, or
633 // - the entity is not used or is defined in the same
634 // translation unit.
635 // and [basic.link]p10:
636 // ...the types specified by all declarations referring to a
637 // given variable or function shall be identical...
638 // C does not have an equivalent rule.
639 //
John McCall5fe84122010-10-26 04:59:26 +0000640 // Ignore this if we've got an explicit attribute; the user
641 // probably knows what they're doing.
642 //
John McCall457a04e2010-10-22 21:05:15 +0000643 // Note that we don't want to make the variable non-external
644 // because of this, but unique-external linkage suits us.
Rafael Espindola593537a2013-05-05 20:15:21 +0000645 if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000646 LinkageInfo TypeLV = Var->getType()->getLinkageAndVisibility();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000647 if (TypeLV.getLinkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000648 return LinkageInfo::uniqueExternal();
Rafael Espindola4a5da442013-02-27 02:56:45 +0000649 if (!LV.isVisibilityExplicit())
John McCalldf25c432013-02-16 00:17:33 +0000650 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000651 }
652
John McCall23032652010-11-02 18:38:13 +0000653 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000654 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000655
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000656 // Note that Sema::MergeVarDecl already takes care of implementing
657 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
658 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000659
Douglas Gregorf73b2822009-11-25 22:24:25 +0000660 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000661 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000662 // In theory, we can modify the function's LV by the LV of its
663 // type unless it has C linkage (see comment above about variables
664 // for justification). In practice, GCC doesn't do this, so it's
665 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000666
John McCall23032652010-11-02 18:38:13 +0000667 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000668 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000669
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000670 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
671 // merging storage classes and visibility attributes, so we don't have to
672 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000673
John McCallf768aa72011-02-10 06:50:24 +0000674 // In C++, then if the type of the function uses a type with
675 // unique-external linkage, it's not legally usable from outside
676 // this translation unit. However, we should use the C linkage
677 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000678 if (Context.getLangOpts().CPlusPlus &&
Richard Smith50f4afc2013-05-12 23:17:59 +0000679 !Function->isInExternCContext()) {
680 // Only look at the type-as-written. If this function has an auto-deduced
681 // return type, we can't compute the linkage of that type because it could
682 // require looking at the linkage of this function, and we don't need this
683 // for correctness because the type is not part of the function's
684 // signature.
685 // FIXME: This is a hack. We should be able to solve this circularity some
686 // other way.
687 QualType TypeAsWritten = Function->getType();
688 if (TypeSourceInfo *TSI = Function->getTypeSourceInfo())
689 TypeAsWritten = TSI->getType();
690 if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
691 return LinkageInfo::uniqueExternal();
692 }
John McCallf768aa72011-02-10 06:50:24 +0000693
John McCall5f46c482013-02-21 23:42:58 +0000694 // Consider LV from the template and the template arguments.
695 // We're at file scope, so we do not need to worry about nested
696 // specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000697 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000698 = Function->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000699 mergeTemplateLV(LV, Function, specInfo);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000700 }
701
Douglas Gregorf73b2822009-11-25 22:24:25 +0000702 // - a named class (Clause 9), or an unnamed class defined in a
703 // typedef declaration in which the class has the typedef name
704 // for linkage purposes (7.1.3); or
705 // - a named enumeration (7.2), or an unnamed enumeration
706 // defined in a typedef declaration in which the enumeration
707 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000708 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
709 // Unnamed tags have no linkage.
John McCall5ea95772013-03-09 00:54:27 +0000710 if (!Tag->hasNameForLinkage())
John McCallc273f242010-10-30 11:50:40 +0000711 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000712
John McCall457a04e2010-10-22 21:05:15 +0000713 // If this is a class template specialization, consider the
John McCall5f46c482013-02-21 23:42:58 +0000714 // linkage of the template and template arguments. We're at file
715 // scope, so we do not need to worry about nested specializations.
John McCallb8c604a2011-06-27 23:06:04 +0000716 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000717 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCalldf25c432013-02-16 00:17:33 +0000718 mergeTemplateLV(LV, spec, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000719 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000720
721 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000722 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000723 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCalldf25c432013-02-16 00:17:33 +0000724 computation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000725 if (!isExternalLinkage(EnumLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000726 return LinkageInfo::none();
727 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000728
729 // - a template, unless it is a function template that has
730 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000731 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
John McCalld041a9b2013-02-20 01:54:26 +0000732 bool considerVisibility = !hasExplicitVisibilityAlready(computation);
John McCalldf25c432013-02-16 00:17:33 +0000733 LinkageInfo tempLV =
734 getLVForTemplateParameterList(temp->getTemplateParameters());
735 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
736
Douglas Gregorf73b2822009-11-25 22:24:25 +0000737 // - a namespace (7.3), unless it is declared within an unnamed
738 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000739 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
740 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000741
John McCall457a04e2010-10-22 21:05:15 +0000742 // By extension, we assign external linkage to Objective-C
743 // interfaces.
744 } else if (isa<ObjCInterfaceDecl>(D)) {
745 // fallout
746
747 // Everything not covered here has no linkage.
748 } else {
John McCallc273f242010-10-30 11:50:40 +0000749 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000750 }
751
752 // If we ended up with non-external linkage, visibility should
753 // always be default.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000754 if (LV.getLinkage() != ExternalLinkage)
755 return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000756
John McCall457a04e2010-10-22 21:05:15 +0000757 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000758}
759
John McCalldf25c432013-02-16 00:17:33 +0000760static LinkageInfo getLVForClassMember(const NamedDecl *D,
761 LVComputationKind computation) {
John McCall457a04e2010-10-22 21:05:15 +0000762 // Only certain class members have linkage. Note that fields don't
763 // really have linkage, but it's convenient to say they do for the
764 // purposes of calculating linkage of pointer-to-data-member
765 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000766 if (!(isa<CXXMethodDecl>(D) ||
767 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000768 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000769 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000770 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000771
John McCall07072662010-11-02 01:45:15 +0000772 LinkageInfo LV;
773
John McCall07072662010-11-02 01:45:15 +0000774 // If we have an explicit visibility attribute, merge that in.
John McCalld041a9b2013-02-20 01:54:26 +0000775 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +0000776 if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000777 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000778 // If we're paying attention to global visibility, apply
779 // -finline-visibility-hidden if this is an inline method.
780 //
781 // Note that we do this before merging information about
782 // the class visibility.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000783 if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000784 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000785 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000786
787 // If this class member has an explicit visibility attribute, the only
788 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000789 // only look for them when processing the class.
John McCalld041a9b2013-02-20 01:54:26 +0000790 LVComputationKind classComputation = computation;
Rafael Espindola4a5da442013-02-27 02:56:45 +0000791 if (LV.isVisibilityExplicit())
John McCalld041a9b2013-02-20 01:54:26 +0000792 classComputation = withExplicitVisibilityAlready(computation);
Rafael Espindola505a7c82012-04-16 18:25:01 +0000793
John McCall5f46c482013-02-21 23:42:58 +0000794 LinkageInfo classLV =
795 getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
Rafael Espindola4a5da442013-02-27 02:56:45 +0000796 if (!isExternalLinkage(classLV.getLinkage()))
John McCallc273f242010-10-30 11:50:40 +0000797 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000798
799 // If the class already has unique-external linkage, we can't improve.
Rafael Espindola4a5da442013-02-27 02:56:45 +0000800 if (classLV.getLinkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000801 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000802
John McCall5f46c482013-02-21 23:42:58 +0000803 // Otherwise, don't merge in classLV yet, because in certain cases
804 // we need to completely ignore the visibility from it.
805
806 // Specifically, if this decl exists and has an explicit attribute.
807 const NamedDecl *explicitSpecSuppressor = 0;
808
John McCall8823c652010-08-13 08:35:10 +0000809 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000810 // If the type of the function uses a type with unique-external
811 // linkage, it's not legally usable from outside this translation unit.
812 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
813 return LinkageInfo::uniqueExternal();
814
John McCall457a04e2010-10-22 21:05:15 +0000815 // If this is a method template specialization, use the linkage for
816 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000817 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000818 = MD->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000819 mergeTemplateLV(LV, MD, spec);
John McCall5f46c482013-02-21 23:42:58 +0000820 if (spec->isExplicitSpecialization()) {
821 explicitSpecSuppressor = MD;
822 } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
823 explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
824 }
825 } else if (isExplicitMemberSpecialization(MD)) {
826 explicitSpecSuppressor = MD;
John McCalle6e622e2010-11-01 01:29:57 +0000827 }
John McCall457a04e2010-10-22 21:05:15 +0000828
John McCall37bb6c92010-10-29 22:22:43 +0000829 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000830 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000831 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCalldf25c432013-02-16 00:17:33 +0000832 mergeTemplateLV(LV, spec, computation);
John McCall5f46c482013-02-21 23:42:58 +0000833 if (spec->isExplicitSpecialization()) {
834 explicitSpecSuppressor = spec;
835 } else {
836 const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
837 if (isExplicitMemberSpecialization(temp)) {
838 explicitSpecSuppressor = temp->getTemplatedDecl();
839 }
840 }
841 } else if (isExplicitMemberSpecialization(RD)) {
842 explicitSpecSuppressor = RD;
John McCall37bb6c92010-10-29 22:22:43 +0000843 }
844
John McCall37bb6c92010-10-29 22:22:43 +0000845 // Static data members.
846 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000847 // Modify the variable's linkage by its type, but ignore the
848 // type's visibility unless it's a definition.
Rafael Espindola6fbfafe2013-02-27 02:27:19 +0000849 LinkageInfo typeLV = VD->getType()->getLinkageAndVisibility();
John McCall5f46c482013-02-21 23:42:58 +0000850 LV.mergeMaybeWithVisibility(typeLV,
Rafael Espindola4a5da442013-02-27 02:56:45 +0000851 !LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit());
John McCall5f46c482013-02-21 23:42:58 +0000852
853 if (isExplicitMemberSpecialization(VD)) {
854 explicitSpecSuppressor = VD;
855 }
John McCalldf25c432013-02-16 00:17:33 +0000856
857 // Template members.
858 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
859 bool considerVisibility =
Rafael Espindola4a5da442013-02-27 02:56:45 +0000860 (!LV.isVisibilityExplicit() &&
861 !classLV.isVisibilityExplicit() &&
John McCalld041a9b2013-02-20 01:54:26 +0000862 !hasExplicitVisibilityAlready(computation));
John McCalldf25c432013-02-16 00:17:33 +0000863 LinkageInfo tempLV =
864 getLVForTemplateParameterList(temp->getTemplateParameters());
865 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall5f46c482013-02-21 23:42:58 +0000866
867 if (const RedeclarableTemplateDecl *redeclTemp =
868 dyn_cast<RedeclarableTemplateDecl>(temp)) {
869 if (isExplicitMemberSpecialization(redeclTemp)) {
870 explicitSpecSuppressor = temp->getTemplatedDecl();
871 }
872 }
John McCall37bb6c92010-10-29 22:22:43 +0000873 }
874
John McCall5f46c482013-02-21 23:42:58 +0000875 // We should never be looking for an attribute directly on a template.
876 assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
877
878 // If this member is an explicit member specialization, and it has
879 // an explicit attribute, ignore visibility from the parent.
880 bool considerClassVisibility = true;
881 if (explicitSpecSuppressor &&
Rafael Espindola4a5da442013-02-27 02:56:45 +0000882 // optimization: hasDVA() is true only with explicit visibility.
883 LV.isVisibilityExplicit() &&
884 classLV.getVisibility() != DefaultVisibility &&
John McCall5f46c482013-02-21 23:42:58 +0000885 hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) {
886 considerClassVisibility = false;
887 }
888
889 // Finally, merge in information from the class.
890 LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
John McCall457a04e2010-10-22 21:05:15 +0000891 return LV;
John McCall8823c652010-08-13 08:35:10 +0000892}
893
David Blaikie68e081d2011-12-20 02:48:34 +0000894void NamedDecl::anchor() { }
895
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000896bool NamedDecl::isLinkageValid() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +0000897 if (!hasCachedLinkage())
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000898 return true;
John McCalld396b972011-02-08 19:01:05 +0000899
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000900 return getLVForDecl(this, LVForExplicitValue).getLinkage() ==
Rafael Espindola50df3a02013-05-25 17:16:20 +0000901 getCachedLinkage();
John McCalld396b972011-02-08 19:01:05 +0000902}
903
Rafael Espindola3ae00052013-05-13 00:12:11 +0000904Linkage NamedDecl::getLinkageInternal() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +0000905 if (hasCachedLinkage())
906 return getCachedLinkage();
Rafael Espindola19de5612013-01-12 06:42:30 +0000907
John McCalld041a9b2013-02-20 01:54:26 +0000908 // We don't care about visibility here, so ask for the cheapest
909 // possible visibility analysis.
Rafael Espindola50df3a02013-05-25 17:16:20 +0000910 setCachedLinkage(getLVForDecl(this, LVForExplicitValue).getLinkage());
Rafael Espindola19de5612013-01-12 06:42:30 +0000911
912#ifndef NDEBUG
913 verifyLinkage();
914#endif
915
Rafael Espindola50df3a02013-05-25 17:16:20 +0000916 return getCachedLinkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000917}
918
John McCallc273f242010-10-30 11:50:40 +0000919LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCalldf25c432013-02-16 00:17:33 +0000920 LVComputationKind computation =
921 (usesTypeVisibility(this) ? LVForType : LVForValue);
922 LinkageInfo LI = getLVForDecl(this, computation);
Rafael Espindola50df3a02013-05-25 17:16:20 +0000923 if (hasCachedLinkage()) {
924 assert(getCachedLinkage() == LI.getLinkage());
Rafael Espindola19de5612013-01-12 06:42:30 +0000925 return LI;
Rafael Espindola54606d52012-12-25 07:31:49 +0000926 }
Rafael Espindola50df3a02013-05-25 17:16:20 +0000927 setCachedLinkage(LI.getLinkage());
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000928
929#ifndef NDEBUG
Rafael Espindola19de5612013-01-12 06:42:30 +0000930 verifyLinkage();
931#endif
932
933 return LI;
934}
935
936void NamedDecl::verifyLinkage() const {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000937 // In C (because of gnu inline) and in c++ with microsoft extensions an
938 // static can follow an extern, so we can have two decls with different
939 // linkages.
940 const LangOptions &Opts = getASTContext().getLangOpts();
941 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
Rafael Espindola19de5612013-01-12 06:42:30 +0000942 return;
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000943
944 // We have just computed the linkage for this decl. By induction we know
945 // that all other computed linkages match, check that the one we just computed
946 // also does.
947 NamedDecl *D = NULL;
948 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
949 NamedDecl *T = cast<NamedDecl>(*I);
950 if (T == this)
951 continue;
Rafael Espindola50df3a02013-05-25 17:16:20 +0000952 if (T->hasCachedLinkage()) {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000953 D = T;
954 break;
955 }
956 }
Rafael Espindola50df3a02013-05-25 17:16:20 +0000957 assert(!D || D->getCachedLinkage() == getCachedLinkage());
John McCall033caa52010-10-29 00:29:13 +0000958}
Ted Kremenek926d8602010-04-20 23:15:35 +0000959
David Blaikie05785d12013-02-20 22:23:23 +0000960Optional<Visibility>
John McCalld041a9b2013-02-20 01:54:26 +0000961NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
Rafael Espindola3a52c442013-02-26 19:33:14 +0000962 // Check the declaration itself first.
963 if (Optional<Visibility> V = getVisibilityOf(this, kind))
964 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000965
Rafael Espindola3a52c442013-02-26 19:33:14 +0000966 // If this is a member class of a specialization of a class template
967 // and the corresponding decl has explicit visibility, use that.
968 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
969 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
970 if (InstantiatedFrom)
971 return getVisibilityOf(InstantiatedFrom, kind);
972 }
973
974 // If there wasn't explicit visibility there, and this is a
975 // specialization of a class template, check for visibility
976 // on the pattern.
977 if (const ClassTemplateSpecializationDecl *spec
978 = dyn_cast<ClassTemplateSpecializationDecl>(this))
979 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(),
980 kind);
981
982 // Use the most recent declaration.
983 const NamedDecl *MostRecent = cast<NamedDecl>(this->getMostRecentDecl());
984 if (MostRecent != this)
985 return MostRecent->getExplicitVisibility(kind);
986
987 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola96e68242012-05-16 02:10:38 +0000988 if (Var->isStaticDataMember()) {
989 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
990 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +0000991 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola96e68242012-05-16 02:10:38 +0000992 }
993
David Blaikie7a30dc52013-02-21 01:47:18 +0000994 return None;
Rafael Espindola96e68242012-05-16 02:10:38 +0000995 }
Rafael Espindola3a52c442013-02-26 19:33:14 +0000996 // Also handle function template specializations.
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000997 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000998 // If the function is a specialization of a template with an
999 // explicit visibility attribute, use that.
1000 if (FunctionTemplateSpecializationInfo *templateInfo
1001 = fn->getTemplateSpecializationInfo())
John McCalld041a9b2013-02-20 01:54:26 +00001002 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
1003 kind);
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001004
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001005 // If the function is a member of a specialization of a class template
1006 // and the corresponding decl has explicit visibility, use that.
1007 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
1008 if (InstantiatedFrom)
John McCalld041a9b2013-02-20 01:54:26 +00001009 return getVisibilityOf(InstantiatedFrom, kind);
Rafael Espindola8093fdf2012-02-23 04:17:32 +00001010
David Blaikie7a30dc52013-02-21 01:47:18 +00001011 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001012 }
1013
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001014 // The visibility of a template is stored in the templated decl.
1015 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
John McCalld041a9b2013-02-20 01:54:26 +00001016 return getVisibilityOf(TD->getTemplatedDecl(), kind);
Rafael Espindolafb4263f2012-07-31 19:02:02 +00001017
David Blaikie7a30dc52013-02-21 01:47:18 +00001018 return None;
Douglas Gregor1baf38f2011-03-26 12:10:19 +00001019}
1020
John McCalldf25c432013-02-16 00:17:33 +00001021static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
1022 LVComputationKind computation) {
1023 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1024 if (Function->isInAnonymousNamespace() &&
Rafael Espindola593537a2013-05-05 20:15:21 +00001025 !Function->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001026 return LinkageInfo::uniqueExternal();
1027
1028 // This is a "void f();" which got merged with a file static.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001029 if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
John McCalldf25c432013-02-16 00:17:33 +00001030 return LinkageInfo::internal();
1031
1032 LinkageInfo LV;
John McCalld041a9b2013-02-20 01:54:26 +00001033 if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001034 if (Optional<Visibility> Vis =
1035 getExplicitVisibility(Function, computation))
John McCalldf25c432013-02-16 00:17:33 +00001036 LV.mergeVisibility(*Vis, true);
1037 }
1038
1039 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1040 // merging storage classes and visibility attributes, so we don't have to
1041 // look at previous decls in here.
1042
1043 return LV;
1044 }
1045
1046 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001047 if (Var->hasExternalStorage()) {
Rafael Espindola593537a2013-05-05 20:15:21 +00001048 if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
John McCalldf25c432013-02-16 00:17:33 +00001049 return LinkageInfo::uniqueExternal();
1050
John McCalldf25c432013-02-16 00:17:33 +00001051 LinkageInfo LV;
1052 if (Var->getStorageClass() == SC_PrivateExtern)
1053 LV.mergeVisibility(HiddenVisibility, true);
John McCalld041a9b2013-02-20 01:54:26 +00001054 else if (!hasExplicitVisibilityAlready(computation)) {
David Blaikie05785d12013-02-20 22:23:23 +00001055 if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
John McCalldf25c432013-02-16 00:17:33 +00001056 LV.mergeVisibility(*Vis, true);
1057 }
1058
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001059 if (const VarDecl *Prev = Var->getPreviousDecl()) {
1060 LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1061 if (PrevLV.getLinkage())
1062 LV.setLinkage(PrevLV.getLinkage());
1063 LV.mergeVisibility(PrevLV);
1064 }
1065
John McCalldf25c432013-02-16 00:17:33 +00001066 return LV;
1067 }
1068 }
1069
Rafael Espindola50df3a02013-05-25 17:16:20 +00001070 if (!isa<TagDecl>(D))
1071 return LinkageInfo::none();
1072
1073 const FunctionDecl *FD = getOutermostFunctionContext(D);
1074 if (!FD || !FD->isInlined())
1075 return LinkageInfo::none();
1076 LinkageInfo LV = FD->getLinkageAndVisibility();
1077 if (LV.getLinkage() != ExternalLinkage)
1078 return LinkageInfo::none();
1079 return LinkageInfo(VisibleNoLinkage, LV.getVisibility(),
1080 LV.isVisibilityExplicit());
John McCalldf25c432013-02-16 00:17:33 +00001081}
1082
1083static LinkageInfo getLVForDecl(const NamedDecl *D,
1084 LVComputationKind computation) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001085 // Objective-C: treat all Objective-C declarations as having external
1086 // linkage.
John McCall033caa52010-10-29 00:29:13 +00001087 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +00001088 default:
1089 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +00001090 case Decl::ParmVar:
1091 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001092 case Decl::TemplateTemplateParm: // count these as external
1093 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +00001094 case Decl::ObjCAtDefsField:
1095 case Decl::ObjCCategory:
1096 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +00001097 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +00001098 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +00001099 case Decl::ObjCMethod:
1100 case Decl::ObjCProperty:
1101 case Decl::ObjCPropertyImpl:
1102 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +00001103 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001104
1105 case Decl::CXXRecord: {
1106 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
1107 if (Record->isLambda()) {
1108 if (!Record->getLambdaManglingNumber()) {
1109 // This lambda has no mangling number, so it's internal.
1110 return LinkageInfo::internal();
1111 }
1112
1113 // This lambda has its linkage/visibility determined by its owner.
1114 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
1115 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
1116 if (isa<ParmVarDecl>(ContextDecl))
1117 DC = ContextDecl->getDeclContext()->getRedeclContext();
1118 else
John McCalldf25c432013-02-16 00:17:33 +00001119 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001120 }
1121
1122 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
John McCalldf25c432013-02-16 00:17:33 +00001123 return getLVForDecl(ND, computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +00001124
1125 return LinkageInfo::external();
1126 }
1127
1128 break;
1129 }
Ted Kremenek926d8602010-04-20 23:15:35 +00001130 }
1131
Douglas Gregorf73b2822009-11-25 22:24:25 +00001132 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +00001133 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCalldf25c432013-02-16 00:17:33 +00001134 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001135
1136 // C++ [basic.link]p5:
1137 // In addition, a member function, static data member, a named
1138 // class or enumeration of class scope, or an unnamed class or
1139 // enumeration defined in a class-scope typedef declaration such
1140 // that the class or enumeration has the typedef name for linkage
1141 // purposes (7.1.3), has external linkage if the name of the class
1142 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +00001143 if (D->getDeclContext()->isRecord())
John McCalldf25c432013-02-16 00:17:33 +00001144 return getLVForClassMember(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001145
1146 // C++ [basic.link]p6:
1147 // The name of a function declared in block scope and the name of
1148 // an object declared by a block scope extern declaration have
1149 // linkage. If there is a visible declaration of an entity with
1150 // linkage having the same name and type, ignoring entities
1151 // declared outside the innermost enclosing namespace scope, the
1152 // block scope declaration declares that same entity and receives
1153 // the linkage of the previous declaration. If there is more than
1154 // one such matching entity, the program is ill-formed. Otherwise,
1155 // if no matching entity is found, the block scope entity receives
1156 // external linkage.
John McCalldf25c432013-02-16 00:17:33 +00001157 if (D->getDeclContext()->isFunctionOrMethod())
1158 return getLVForLocalDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +00001159
1160 // C++ [basic.link]p6:
1161 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +00001162 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +00001163}
Douglas Gregorf73b2822009-11-25 22:24:25 +00001164
Douglas Gregor2ada0482009-02-04 17:27:36 +00001165std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +00001166 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +00001167}
1168
1169std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001170 std::string QualName;
1171 llvm::raw_string_ostream OS(QualName);
1172 printQualifiedName(OS, P);
1173 return OS.str();
1174}
1175
1176void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1177 printQualifiedName(OS, getASTContext().getPrintingPolicy());
1178}
1179
1180void NamedDecl::printQualifiedName(raw_ostream &OS,
1181 const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +00001182 const DeclContext *Ctx = getDeclContext();
1183
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001184 if (Ctx->isFunctionOrMethod()) {
1185 printName(OS);
1186 return;
1187 }
Douglas Gregor2ada0482009-02-04 17:27:36 +00001188
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001189 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001190 ContextsTy Contexts;
1191
1192 // Collect contexts.
1193 while (Ctx && isa<NamedDecl>(Ctx)) {
1194 Contexts.push_back(Ctx);
1195 Ctx = Ctx->getParent();
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001196 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001197
1198 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1199 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001200 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001201 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Benjamin Kramer9170e912013-02-22 15:46:01 +00001202 OS << Spec->getName();
Douglas Gregor85673582009-05-18 17:01:57 +00001203 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001204 TemplateSpecializationType::PrintTemplateArgumentList(OS,
1205 TemplateArgs.data(),
1206 TemplateArgs.size(),
1207 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001208 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +00001209 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001210 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +00001211 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001212 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001213 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1214 if (!RD->getIdentifier())
1215 OS << "<anonymous " << RD->getKindName() << '>';
1216 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001217 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001218 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +00001219 const FunctionProtoType *FT = 0;
1220 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001221 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001222
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001223 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001224 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001225 unsigned NumParams = FD->getNumParams();
1226 for (unsigned i = 0; i < NumParams; ++i) {
1227 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001228 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001229 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001230 }
1231
1232 if (FT->isVariadic()) {
1233 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001234 OS << ", ";
1235 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001236 }
1237 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001238 OS << ')';
1239 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001240 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001241 }
1242 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001243 }
1244
John McCalla2a3f7d2010-03-16 21:48:18 +00001245 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001246 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001247 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001248 OS << "<anonymous>";
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001249}
Douglas Gregor2ada0482009-02-04 17:27:36 +00001250
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +00001251void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1252 const PrintingPolicy &Policy,
1253 bool Qualified) const {
1254 if (Qualified)
1255 printQualifiedName(OS, Policy);
1256 else
1257 printName(OS);
Douglas Gregor2ada0482009-02-04 17:27:36 +00001258}
1259
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001260bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001261 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1262
Douglas Gregor889ceb72009-02-03 19:21:40 +00001263 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1264 // We want to keep it, unless it nominates same namespace.
1265 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +00001266 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1267 ->getOriginalNamespace() ==
1268 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1269 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +00001270 }
Mike Stump11289f42009-09-09 15:08:12 +00001271
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001272 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1273 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +00001274 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001275
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001276 // For function templates, the underlying function declarations are linked.
1277 if (const FunctionTemplateDecl *FunctionTemplate
1278 = dyn_cast<FunctionTemplateDecl>(this))
1279 if (const FunctionTemplateDecl *OldFunctionTemplate
1280 = dyn_cast<FunctionTemplateDecl>(OldD))
1281 return FunctionTemplate->getTemplatedDecl()
1282 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001283
Steve Naroffc4173fa2009-02-22 19:35:57 +00001284 // For method declarations, we keep track of redeclarations.
1285 if (isa<ObjCMethodDecl>(this))
1286 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001287
John McCall9f3059a2009-10-09 21:13:30 +00001288 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1289 return true;
1290
John McCall3f746822009-11-17 05:59:44 +00001291 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1292 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1293 cast<UsingShadowDecl>(OldD)->getTargetDecl();
1294
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001295 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1296 ASTContext &Context = getASTContext();
1297 return Context.getCanonicalNestedNameSpecifier(
1298 cast<UsingDecl>(this)->getQualifier()) ==
1299 Context.getCanonicalNestedNameSpecifier(
1300 cast<UsingDecl>(OldD)->getQualifier());
1301 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +00001302
Douglas Gregorb59643b2012-01-03 23:26:26 +00001303 // A typedef of an Objective-C class type can replace an Objective-C class
1304 // declaration or definition, and vice versa.
1305 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1306 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1307 return true;
1308
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001309 // For non-function declarations, if the declarations are of the
1310 // same kind then this must be a redeclaration, or semantic analysis
1311 // would not have given us the new declaration.
1312 return this->getKind() == OldD->getKind();
1313}
1314
Douglas Gregoreddf4332009-02-24 20:03:32 +00001315bool NamedDecl::hasLinkage() const {
Rafael Espindola50df3a02013-05-25 17:16:20 +00001316 return getFormalLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001317}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001318
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001319NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001320 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001321 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1322 ND = UD->getTargetDecl();
1323
1324 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1325 return AD->getClassInterface();
1326
1327 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001328}
1329
John McCalla8ae2222010-04-06 21:38:20 +00001330bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001331 if (!isCXXClassMember())
1332 return false;
1333
John McCalla8ae2222010-04-06 21:38:20 +00001334 const NamedDecl *D = this;
1335 if (isa<UsingShadowDecl>(D))
1336 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1337
John McCall5e77d762013-04-16 07:28:30 +00001338 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001339 return true;
1340 if (isa<CXXMethodDecl>(D))
1341 return cast<CXXMethodDecl>(D)->isInstance();
1342 if (isa<FunctionTemplateDecl>(D))
1343 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1344 ->getTemplatedDecl())->isInstance();
1345 return false;
1346}
1347
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001348//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001349// DeclaratorDecl Implementation
1350//===----------------------------------------------------------------------===//
1351
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001352template <typename DeclT>
1353static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1354 if (decl->getNumTemplateParameterLists() > 0)
1355 return decl->getTemplateParameterList(0)->getTemplateLoc();
1356 else
1357 return decl->getInnerLocStart();
1358}
1359
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001360SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001361 TypeSourceInfo *TSI = getTypeSourceInfo();
1362 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001363 return SourceLocation();
1364}
1365
Douglas Gregor14454802011-02-25 02:25:35 +00001366void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1367 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001368 // Make sure the extended decl info is allocated.
1369 if (!hasExtInfo()) {
1370 // Save (non-extended) type source info pointer.
1371 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1372 // Allocate external info struct.
1373 DeclInfo = new (getASTContext()) ExtInfo;
1374 // Restore savedTInfo into (extended) decl info.
1375 getExtInfo()->TInfo = savedTInfo;
1376 }
1377 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001378 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001379 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001380 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001381 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001382 if (getExtInfo()->NumTemplParamLists == 0) {
1383 // Save type source info pointer.
1384 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1385 // Deallocate the extended decl info.
1386 getASTContext().Deallocate(getExtInfo());
1387 // Restore savedTInfo into (non-extended) decl info.
1388 DeclInfo = savedTInfo;
1389 }
1390 else
1391 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001392 }
1393 }
1394}
1395
Abramo Bagnara60804e12011-03-18 15:16:37 +00001396void
1397DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1398 unsigned NumTPLists,
1399 TemplateParameterList **TPLists) {
1400 assert(NumTPLists > 0);
1401 // Make sure the extended decl info is allocated.
1402 if (!hasExtInfo()) {
1403 // Save (non-extended) type source info pointer.
1404 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1405 // Allocate external info struct.
1406 DeclInfo = new (getASTContext()) ExtInfo;
1407 // Restore savedTInfo into (extended) decl info.
1408 getExtInfo()->TInfo = savedTInfo;
1409 }
1410 // Set the template parameter lists info.
1411 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1412}
1413
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001414SourceLocation DeclaratorDecl::getOuterLocStart() const {
1415 return getTemplateOrInnerLocStart(this);
1416}
1417
Abramo Bagnaraea947882011-03-08 16:41:52 +00001418namespace {
1419
1420// Helper function: returns true if QT is or contains a type
1421// having a postfix component.
1422bool typeIsPostfix(clang::QualType QT) {
1423 while (true) {
1424 const Type* T = QT.getTypePtr();
1425 switch (T->getTypeClass()) {
1426 default:
1427 return false;
1428 case Type::Pointer:
1429 QT = cast<PointerType>(T)->getPointeeType();
1430 break;
1431 case Type::BlockPointer:
1432 QT = cast<BlockPointerType>(T)->getPointeeType();
1433 break;
1434 case Type::MemberPointer:
1435 QT = cast<MemberPointerType>(T)->getPointeeType();
1436 break;
1437 case Type::LValueReference:
1438 case Type::RValueReference:
1439 QT = cast<ReferenceType>(T)->getPointeeType();
1440 break;
1441 case Type::PackExpansion:
1442 QT = cast<PackExpansionType>(T)->getPattern();
1443 break;
1444 case Type::Paren:
1445 case Type::ConstantArray:
1446 case Type::DependentSizedArray:
1447 case Type::IncompleteArray:
1448 case Type::VariableArray:
1449 case Type::FunctionProto:
1450 case Type::FunctionNoProto:
1451 return true;
1452 }
1453 }
1454}
1455
1456} // namespace
1457
1458SourceRange DeclaratorDecl::getSourceRange() const {
1459 SourceLocation RangeEnd = getLocation();
1460 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1461 if (typeIsPostfix(TInfo->getType()))
1462 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1463 }
1464 return SourceRange(getOuterLocStart(), RangeEnd);
1465}
1466
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001467void
Douglas Gregor20527e22010-06-15 17:44:38 +00001468QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1469 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001470 TemplateParameterList **TPLists) {
1471 assert((NumTPLists == 0 || TPLists != 0) &&
1472 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001473
1474 // Free previous template parameters (if any).
1475 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001476 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001477 TemplParamLists = 0;
1478 NumTemplParamLists = 0;
1479 }
1480 // Set info on matched template parameter lists (if any).
1481 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001482 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001483 NumTemplParamLists = NumTPLists;
1484 for (unsigned i = NumTPLists; i-- > 0; )
1485 TemplParamLists[i] = TPLists[i];
1486 }
1487}
1488
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001489//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001490// VarDecl Implementation
1491//===----------------------------------------------------------------------===//
1492
Sebastian Redl833ef452010-01-26 22:01:41 +00001493const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1494 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001495 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001496 case SC_Auto: return "auto";
1497 case SC_Extern: return "extern";
1498 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1499 case SC_PrivateExtern: return "__private_extern__";
1500 case SC_Register: return "register";
1501 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001502 }
1503
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001504 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001505}
1506
Abramo Bagnaradff19302011-03-08 08:55:46 +00001507VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1508 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001509 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001510 StorageClass S) {
1511 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001512}
1513
Douglas Gregor72172e92012-01-05 21:55:30 +00001514VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1515 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1516 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001517 QualType(), 0, SC_None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001518}
1519
Douglas Gregorbf62d642010-12-06 18:36:25 +00001520void VarDecl::setStorageClass(StorageClass SC) {
1521 assert(isLegalForVariable(SC));
John McCallbeaa11c2011-05-01 02:13:58 +00001522 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001523}
1524
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001525SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001526 if (const Expr *Init = getInit()) {
1527 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001528 // If Init is implicit, ignore its source range and fallback on
1529 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1530 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001531 return SourceRange(getOuterLocStart(), InitEnd);
1532 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001533 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001534}
1535
Rafael Espindola88510672013-01-04 21:18:45 +00001536template<typename T>
Rafael Espindolaf4187652013-02-14 01:18:37 +00001537static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001538 // C++ [dcl.link]p1: All function types, function names with external linkage,
1539 // and variable names with external linkage have a language linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +00001540 if (!D.hasExternalFormalLinkage())
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001541 return NoLanguageLinkage;
1542
1543 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001544 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001545 ASTContext &Context = D.getASTContext();
1546 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001547 return CLanguageLinkage;
1548
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001549 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1550 // language linkage of the names of class members and the function type of
1551 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001552 const DeclContext *DC = D.getDeclContext();
1553 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001554 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001555
1556 // If the first decl is in an extern "C" context, any other redeclaration
1557 // will have C language linkage. If the first one is not in an extern "C"
1558 // context, we would have reported an error for any other decl being in one.
Rafael Espindola593537a2013-05-05 20:15:21 +00001559 if (isFirstInExternCContext(&D))
Rafael Espindolaf4187652013-02-14 01:18:37 +00001560 return CLanguageLinkage;
1561 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001562}
1563
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001564template<typename T>
1565static bool isExternCTemplate(const T &D) {
1566 // Since the context is ignored for class members, they can only have C++
1567 // language linkage or no language linkage.
1568 const DeclContext *DC = D.getDeclContext();
1569 if (DC->isRecord()) {
1570 assert(D.getASTContext().getLangOpts().CPlusPlus);
1571 return false;
1572 }
1573
1574 return D.getLanguageLinkage() == CLanguageLinkage;
1575}
1576
Rafael Espindolaf4187652013-02-14 01:18:37 +00001577LanguageLinkage VarDecl::getLanguageLinkage() const {
1578 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001579}
1580
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001581bool VarDecl::isExternC() const {
1582 return isExternCTemplate(*this);
1583}
1584
Rafael Espindola593537a2013-05-05 20:15:21 +00001585static bool isLinkageSpecContext(const DeclContext *DC,
1586 LinkageSpecDecl::LanguageIDs ID) {
1587 while (DC->getDeclKind() != Decl::TranslationUnit) {
1588 if (DC->getDeclKind() == Decl::LinkageSpec)
1589 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1590 DC = DC->getParent();
1591 }
1592 return false;
1593}
1594
1595template <typename T>
1596static bool isInLanguageSpecContext(T *D, LinkageSpecDecl::LanguageIDs ID) {
1597 return isLinkageSpecContext(D->getLexicalDeclContext(), ID);
1598}
1599
1600bool VarDecl::isInExternCContext() const {
1601 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
1602}
1603
1604bool VarDecl::isInExternCXXContext() const {
1605 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
1606}
1607
Sebastian Redl833ef452010-01-26 22:01:41 +00001608VarDecl *VarDecl::getCanonicalDecl() {
1609 return getFirstDeclaration();
1610}
1611
Daniel Dunbar9d355812012-03-09 01:51:51 +00001612VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1613 ASTContext &C) const
1614{
Sebastian Redl35351a92010-01-31 22:27:38 +00001615 // C++ [basic.def]p2:
1616 // A declaration is a definition unless [...] it contains the 'extern'
1617 // specifier or a linkage-specification and neither an initializer [...],
1618 // it declares a static data member in a class declaration [...].
1619 // C++ [temp.expl.spec]p15:
1620 // An explicit specialization of a static data member of a template is a
1621 // definition if the declaration includes an initializer; otherwise, it is
1622 // a declaration.
1623 if (isStaticDataMember()) {
1624 if (isOutOfLine() && (hasInit() ||
1625 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1626 return Definition;
1627 else
1628 return DeclarationOnly;
1629 }
1630 // C99 6.7p5:
1631 // A definition of an identifier is a declaration for that identifier that
1632 // [...] causes storage to be reserved for that object.
1633 // Note: that applies for all non-file-scope objects.
1634 // C99 6.9.2p1:
1635 // If the declaration of an identifier for an object has file scope and an
1636 // initializer, the declaration is an external definition for the identifier
1637 if (hasInit())
1638 return Definition;
Rafael Espindolabff59562013-04-25 12:11:36 +00001639
Sebastian Redl35351a92010-01-31 22:27:38 +00001640 if (hasExternalStorage())
1641 return DeclarationOnly;
Rafael Espindola8f326a52013-03-07 01:42:44 +00001642
Rafael Espindolabff59562013-04-25 12:11:36 +00001643 // [dcl.link] p7:
1644 // A declaration directly contained in a linkage-specification is treated
1645 // as if it contains the extern specifier for the purpose of determining
1646 // the linkage of the declared name and whether it is a definition.
Rafael Espindola327be3c2013-04-26 01:30:23 +00001647 if (isSingleLineExternC(*this))
1648 return DeclarationOnly;
Rafael Espindolabff59562013-04-25 12:11:36 +00001649
Sebastian Redl35351a92010-01-31 22:27:38 +00001650 // C99 6.9.2p2:
1651 // A declaration of an object that has file scope without an initializer,
1652 // and without a storage class specifier or the scs 'static', constitutes
1653 // a tentative definition.
1654 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001655 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001656 return TentativeDefinition;
1657
1658 // What's left is (in C, block-scope) declarations without initializers or
1659 // external storage. These are definitions.
1660 return Definition;
1661}
1662
Sebastian Redl35351a92010-01-31 22:27:38 +00001663VarDecl *VarDecl::getActingDefinition() {
1664 DefinitionKind Kind = isThisDeclarationADefinition();
1665 if (Kind != TentativeDefinition)
1666 return 0;
1667
Chris Lattner48eb14d2010-06-14 18:31:46 +00001668 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001669 VarDecl *First = getFirstDeclaration();
1670 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1671 I != E; ++I) {
1672 Kind = (*I)->isThisDeclarationADefinition();
1673 if (Kind == Definition)
1674 return 0;
1675 else if (Kind == TentativeDefinition)
1676 LastTentative = *I;
1677 }
1678 return LastTentative;
1679}
1680
1681bool VarDecl::isTentativeDefinitionNow() const {
1682 DefinitionKind Kind = isThisDeclarationADefinition();
1683 if (Kind != TentativeDefinition)
1684 return false;
1685
1686 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1687 if ((*I)->isThisDeclarationADefinition() == Definition)
1688 return false;
1689 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001690 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001691}
1692
Daniel Dunbar9d355812012-03-09 01:51:51 +00001693VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001694 VarDecl *First = getFirstDeclaration();
1695 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1696 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001697 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001698 return *I;
1699 }
1700 return 0;
1701}
1702
Daniel Dunbar9d355812012-03-09 01:51:51 +00001703VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001704 DefinitionKind Kind = DeclarationOnly;
1705
1706 const VarDecl *First = getFirstDeclaration();
1707 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001708 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001709 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001710 if (Kind == Definition)
1711 break;
1712 }
John McCall37bb6c92010-10-29 22:22:43 +00001713
1714 return Kind;
1715}
1716
Sebastian Redl5ca79842010-02-01 20:16:42 +00001717const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001718 redecl_iterator I = redecls_begin(), E = redecls_end();
1719 while (I != E && !I->getInit())
1720 ++I;
1721
1722 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001723 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001724 return I->getInit();
1725 }
1726 return 0;
1727}
1728
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001729bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001730 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001731 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001732
1733 if (!isStaticDataMember())
1734 return false;
1735
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001736 // If this static data member was instantiated from a static data member of
1737 // a class template, check whether that static data member was defined
1738 // out-of-line.
1739 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1740 return VD->isOutOfLine();
1741
1742 return false;
1743}
1744
Douglas Gregor1d957a32009-10-27 18:42:08 +00001745VarDecl *VarDecl::getOutOfLineDefinition() {
1746 if (!isStaticDataMember())
1747 return 0;
1748
1749 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1750 RD != RDEnd; ++RD) {
1751 if (RD->getLexicalDeclContext()->isFileContext())
1752 return *RD;
1753 }
1754
1755 return 0;
1756}
1757
Douglas Gregord5058122010-02-11 01:19:42 +00001758void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001759 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1760 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001761 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001762 }
1763
1764 Init = I;
1765}
1766
Daniel Dunbar9d355812012-03-09 01:51:51 +00001767bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001768 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001769
Richard Smith35ecb362012-03-02 04:14:40 +00001770 if (!Lang.CPlusPlus)
1771 return false;
1772
1773 // In C++11, any variable of reference type can be used in a constant
1774 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001775 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001776 return true;
1777
1778 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001779 // not require the variable to be non-volatile, but we consider this to be a
1780 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001781 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001782 return false;
1783
1784 // In C++, const, non-volatile variables of integral or enumeration types
1785 // can be used in constant expressions.
1786 if (getType()->isIntegralOrEnumerationType())
1787 return true;
1788
Richard Smith35ecb362012-03-02 04:14:40 +00001789 // Additionally, in C++11, non-volatile constexpr variables can be used in
1790 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001791 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001792}
1793
Richard Smithd0b4dd62011-12-19 06:19:21 +00001794/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1795/// form, which contains extra information on the evaluated value of the
1796/// initializer.
1797EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1798 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1799 if (!Eval) {
1800 Stmt *S = Init.get<Stmt *>();
1801 Eval = new (getASTContext()) EvaluatedStmt;
1802 Eval->Value = S;
1803 Init = Eval;
1804 }
1805 return Eval;
1806}
1807
Richard Smithdafff942012-01-14 04:30:29 +00001808APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001809 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00001810 return evaluateValue(Notes);
1811}
1812
1813APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001814 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001815 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1816
1817 // We only produce notes indicating why an initializer is non-constant the
1818 // first time it is evaluated. FIXME: The notes won't always be emitted the
1819 // first time we try evaluation, so might not be produced at all.
1820 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001821 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001822
1823 const Expr *Init = cast<Expr>(Eval->Value);
1824 assert(!Init->isValueDependent());
1825
1826 if (Eval->IsEvaluating) {
1827 // FIXME: Produce a diagnostic for self-initialization.
1828 Eval->CheckedICE = true;
1829 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001830 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001831 }
1832
1833 Eval->IsEvaluating = true;
1834
1835 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1836 this, Notes);
1837
1838 // Ensure the result is an uninitialized APValue if evaluation fails.
1839 if (!Result)
1840 Eval->Evaluated = APValue();
1841
1842 Eval->IsEvaluating = false;
1843 Eval->WasEvaluated = true;
1844
1845 // In C++11, we have determined whether the initializer was a constant
1846 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001847 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001848 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001849 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001850 }
1851
Richard Smithdafff942012-01-14 04:30:29 +00001852 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001853}
1854
1855bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001856 // Initializers of weak variables are never ICEs.
1857 if (isWeak())
1858 return false;
1859
Richard Smithd0b4dd62011-12-19 06:19:21 +00001860 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1861 if (Eval->CheckedICE)
1862 // We have already checked whether this subexpression is an
1863 // integral constant expression.
1864 return Eval->IsICE;
1865
1866 const Expr *Init = cast<Expr>(Eval->Value);
1867 assert(!Init->isValueDependent());
1868
1869 // In C++11, evaluate the initializer to check whether it's a constant
1870 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001871 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001872 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001873 evaluateValue(Notes);
1874 return Eval->IsICE;
1875 }
1876
1877 // It's an ICE whether or not the definition we found is
1878 // out-of-line. See DR 721 and the discussion in Clang PR
1879 // 6206 for details.
1880
1881 if (Eval->CheckingICE)
1882 return false;
1883 Eval->CheckingICE = true;
1884
1885 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1886 Eval->CheckingICE = false;
1887 Eval->CheckedICE = true;
1888 return Eval->IsICE;
1889}
1890
Douglas Gregorfe314812011-06-21 17:03:29 +00001891bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001892 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001893
1894 const Expr *E = getInit();
1895 if (!E)
1896 return false;
1897
1898 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1899 E = Cleanups->getSubExpr();
1900
1901 return isa<MaterializeTemporaryExpr>(E);
1902}
1903
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001904VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001905 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001906 return cast<VarDecl>(MSI->getInstantiatedFrom());
1907
1908 return 0;
1909}
1910
Douglas Gregor3c74d412009-10-14 20:14:33 +00001911TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001912 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001913 return MSI->getTemplateSpecializationKind();
1914
1915 return TSK_Undeclared;
1916}
1917
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001918MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001919 return getASTContext().getInstantiatedFromStaticDataMember(this);
1920}
1921
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001922void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1923 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001924 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001925 assert(MSI && "Not an instantiated static data member?");
1926 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001927 if (TSK != TSK_ExplicitSpecialization &&
1928 PointOfInstantiation.isValid() &&
1929 MSI->getPointOfInstantiation().isInvalid())
1930 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001931}
1932
Sebastian Redl833ef452010-01-26 22:01:41 +00001933//===----------------------------------------------------------------------===//
1934// ParmVarDecl Implementation
1935//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001936
Sebastian Redl833ef452010-01-26 22:01:41 +00001937ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001938 SourceLocation StartLoc,
1939 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001940 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001941 StorageClass S, Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001942 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001943 S, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001944}
1945
Douglas Gregor72172e92012-01-05 21:55:30 +00001946ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1947 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1948 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001949 0, QualType(), 0, SC_None, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001950}
1951
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001952SourceRange ParmVarDecl::getSourceRange() const {
1953 if (!hasInheritedDefaultArg()) {
1954 SourceRange ArgRange = getDefaultArgRange();
1955 if (ArgRange.isValid())
1956 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1957 }
1958
Argyrios Kyrtzidisa0772792013-04-17 01:56:48 +00001959 // DeclaratorDecl considers the range of postfix types as overlapping with the
1960 // declaration name, but this is not the case with parameters in ObjC methods.
1961 if (isa<ObjCMethodDecl>(getDeclContext()))
1962 return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
1963
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001964 return DeclaratorDecl::getSourceRange();
1965}
1966
Sebastian Redl833ef452010-01-26 22:01:41 +00001967Expr *ParmVarDecl::getDefaultArg() {
1968 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1969 assert(!hasUninstantiatedDefaultArg() &&
1970 "Default argument is not yet instantiated!");
1971
1972 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001973 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001974 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001975
Sebastian Redl833ef452010-01-26 22:01:41 +00001976 return Arg;
1977}
1978
Sebastian Redl833ef452010-01-26 22:01:41 +00001979SourceRange ParmVarDecl::getDefaultArgRange() const {
1980 if (const Expr *E = getInit())
1981 return E->getSourceRange();
1982
1983 if (hasUninstantiatedDefaultArg())
1984 return getUninstantiatedDefaultArg()->getSourceRange();
1985
1986 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001987}
1988
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001989bool ParmVarDecl::isParameterPack() const {
1990 return isa<PackExpansionType>(getType());
1991}
1992
Ted Kremenek540017e2011-10-06 05:00:56 +00001993void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1994 getASTContext().setParameterIndex(this, parameterIndex);
1995 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1996}
1997
1998unsigned ParmVarDecl::getParameterIndexLarge() const {
1999 return getASTContext().getParameterIndex(this);
2000}
2001
Nuno Lopes394ec982008-12-17 23:39:55 +00002002//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002003// FunctionDecl Implementation
2004//===----------------------------------------------------------------------===//
2005
Benjamin Kramer9170e912013-02-22 15:46:01 +00002006void FunctionDecl::getNameForDiagnostic(
2007 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2008 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002009 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2010 if (TemplateArgs)
Benjamin Kramer9170e912013-02-22 15:46:01 +00002011 TemplateSpecializationType::PrintTemplateArgumentList(
2012 OS, TemplateArgs->data(), TemplateArgs->size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +00002013}
2014
Ted Kremenek186a0742010-04-29 16:49:01 +00002015bool FunctionDecl::isVariadic() const {
2016 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
2017 return FT->isVariadic();
2018 return false;
2019}
2020
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002021bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
2022 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00002023 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002024 Definition = *I;
2025 return true;
2026 }
2027 }
2028
2029 return false;
2030}
2031
Anders Carlsson9bd7d162011-05-14 23:26:09 +00002032bool FunctionDecl::hasTrivialBody() const
2033{
2034 Stmt *S = getBody();
2035 if (!S) {
2036 // Since we don't have a body for this function, we don't know if it's
2037 // trivial or not.
2038 return false;
2039 }
2040
2041 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2042 return true;
2043 return false;
2044}
2045
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002046bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2047 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00002048 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002049 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
2050 return true;
2051 }
2052 }
2053
2054 return false;
2055}
2056
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002057Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00002058 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2059 if (I->Body) {
2060 Definition = *I;
2061 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00002062 } else if (I->IsLateTemplateParsed) {
2063 Definition = *I;
2064 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00002065 }
2066 }
2067
2068 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002069}
2070
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002071void FunctionDecl::setBody(Stmt *B) {
2072 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00002073 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002074 EndRangeLoc = B->getLocEnd();
2075}
2076
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002077void FunctionDecl::setPure(bool P) {
2078 IsPure = P;
2079 if (P)
2080 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2081 Parent->markedVirtualFunctionPure();
2082}
2083
Douglas Gregor16618f22009-09-12 00:17:51 +00002084bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00002085 const TranslationUnitDecl *tunit =
2086 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2087 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00002088 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00002089 getIdentifier() &&
2090 getIdentifier()->isStr("main");
2091}
2092
2093bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2094 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2095 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2096 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2097 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2098 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2099
2100 if (isa<CXXRecordDecl>(getDeclContext())) return false;
2101 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2102
2103 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
2104 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
2105
2106 ASTContext &Context =
2107 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2108 ->getASTContext();
2109
2110 // The result type and first argument type are constant across all
2111 // these operators. The second argument must be exactly void*.
2112 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00002113}
2114
Rafael Espindolaf4187652013-02-14 01:18:37 +00002115LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6239e052013-01-12 15:27:44 +00002116 // Users expect to be able to write
2117 // extern "C" void *__builtin_alloca (size_t);
2118 // so consider builtins as having C language linkage.
Rafael Espindolac48f7342013-01-12 15:27:43 +00002119 if (getBuiltinID())
Rafael Espindolaf4187652013-02-14 01:18:37 +00002120 return CLanguageLinkage;
Rafael Espindolac48f7342013-01-12 15:27:43 +00002121
Rafael Espindolaf4187652013-02-14 01:18:37 +00002122 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00002123}
2124
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002125bool FunctionDecl::isExternC() const {
2126 return isExternCTemplate(*this);
2127}
2128
Rafael Espindola593537a2013-05-05 20:15:21 +00002129bool FunctionDecl::isInExternCContext() const {
2130 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_c);
2131}
2132
2133bool FunctionDecl::isInExternCXXContext() const {
2134 return isInLanguageSpecContext(this, LinkageSpecDecl::lang_cxx);
2135}
2136
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002137bool FunctionDecl::isGlobal() const {
2138 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
2139 return Method->isStatic();
2140
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002141 if (getCanonicalDecl()->getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002142 return false;
2143
Mike Stump11289f42009-09-09 15:08:12 +00002144 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002145 DC->isNamespace();
2146 DC = DC->getParent()) {
2147 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
2148 if (!Namespace->getDeclName())
2149 return false;
2150 break;
2151 }
2152 }
2153
2154 return true;
2155}
2156
Richard Smith10876ef2013-01-17 01:30:42 +00002157bool FunctionDecl::isNoReturn() const {
2158 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smithdebc59d2013-01-30 05:45:05 +00002159 hasAttr<C11NoReturnAttr>() ||
Richard Smith10876ef2013-01-17 01:30:42 +00002160 getType()->getAs<FunctionType>()->getNoReturnAttr();
2161}
2162
Sebastian Redl833ef452010-01-26 22:01:41 +00002163void
2164FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
2165 redeclarable_base::setPreviousDeclaration(PrevDecl);
2166
2167 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2168 FunctionTemplateDecl *PrevFunTmpl
2169 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
2170 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
2171 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
2172 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00002173
Axel Naumannfbc7b982011-11-08 18:21:06 +00002174 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00002175 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00002176}
2177
2178const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
2179 return getFirstDeclaration();
2180}
2181
2182FunctionDecl *FunctionDecl::getCanonicalDecl() {
2183 return getFirstDeclaration();
2184}
2185
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002186/// \brief Returns a value indicating whether this function
2187/// corresponds to a builtin function.
2188///
2189/// The function corresponds to a built-in function if it is
2190/// declared at translation scope or within an extern "C" block and
2191/// its name matches with the name of a builtin. The returned value
2192/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00002193/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002194/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00002195unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00002196 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00002197 return 0;
2198
2199 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00002200 if (!BuiltinID)
2201 return 0;
2202
2203 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00002204 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2205 return BuiltinID;
2206
2207 // This function has the name of a known C library
2208 // function. Determine whether it actually refers to the C library
2209 // function or whether it just has the same name.
2210
Douglas Gregora908e7f2009-02-17 03:23:10 +00002211 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00002212 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00002213 return 0;
2214
Douglas Gregore711f702009-02-14 18:57:46 +00002215 // If this function is at translation-unit scope and we're not in
2216 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002217 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00002218 getDeclContext()->isTranslationUnit())
2219 return BuiltinID;
2220
2221 // If the function is in an extern "C" linkage specification and is
2222 // not marked "overloadable", it's the real function.
2223 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00002224 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00002225 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002226 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00002227 return BuiltinID;
2228
2229 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002230 return 0;
2231}
2232
2233
Chris Lattner47c0d002009-04-25 06:03:53 +00002234/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00002235/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00002236/// after it has been created.
2237unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00002238 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002239 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00002240 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002241 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00002242
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002243}
2244
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002245void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002246 ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002247 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002248 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002249
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002250 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002251 if (!NewParamInfo.empty()) {
2252 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2253 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002254 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002255}
Chris Lattner41943152007-01-25 04:52:46 +00002256
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002257void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy6f8780b2012-02-29 10:24:19 +00002258 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2259
2260 if (!NewDecls.empty()) {
2261 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2262 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002263 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy6f8780b2012-02-29 10:24:19 +00002264 }
2265}
2266
Chris Lattner58258242008-04-10 02:22:51 +00002267/// getMinRequiredArguments - Returns the minimum number of arguments
2268/// needed to call this function. This may be fewer than the number of
2269/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00002270/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00002271unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002272 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002273 return getNumParams();
2274
Douglas Gregor7825bf32011-01-06 22:09:01 +00002275 unsigned NumRequiredArgs = getNumParams();
2276
2277 // If the last parameter is a parameter pack, we don't need an argument for
2278 // it.
2279 if (NumRequiredArgs > 0 &&
2280 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2281 --NumRequiredArgs;
2282
2283 // If this parameter has a default argument, we don't need an argument for
2284 // it.
2285 while (NumRequiredArgs > 0 &&
2286 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00002287 --NumRequiredArgs;
2288
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002289 // We might have parameter packs before the end. These can't be deduced,
2290 // but they can still handle multiple arguments.
2291 unsigned ArgIdx = NumRequiredArgs;
2292 while (ArgIdx > 0) {
2293 if (getParamDecl(ArgIdx - 1)->isParameterPack())
2294 NumRequiredArgs = ArgIdx;
2295
2296 --ArgIdx;
2297 }
2298
Chris Lattner58258242008-04-10 02:22:51 +00002299 return NumRequiredArgs;
2300}
2301
Eli Friedman1b125c32012-02-07 03:50:18 +00002302static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2303 // Only consider file-scope declarations in this test.
2304 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2305 return false;
2306
2307 // Only consider explicit declarations; the presence of a builtin for a
2308 // libcall shouldn't affect whether a definition is externally visible.
2309 if (Redecl->isImplicit())
2310 return false;
2311
2312 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2313 return true; // Not an inline definition
2314
2315 return false;
2316}
2317
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002318/// \brief For a function declaration in C or C++, determine whether this
2319/// declaration causes the definition to be externally visible.
2320///
Eli Friedman1b125c32012-02-07 03:50:18 +00002321/// Specifically, this determines if adding the current declaration to the set
2322/// of redeclarations of the given functions causes
2323/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002324bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2325 assert(!doesThisDeclarationHaveABody() &&
2326 "Must have a declaration without a body.");
2327
2328 ASTContext &Context = getASTContext();
2329
David Blaikiebbafb8a2012-03-11 07:00:24 +00002330 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002331 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2332 // an externally visible definition.
2333 //
2334 // FIXME: What happens if gnu_inline gets added on after the first
2335 // declaration?
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002336 if (!isInlineSpecified() || getStorageClass() == SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002337 return false;
2338
2339 const FunctionDecl *Prev = this;
2340 bool FoundBody = false;
2341 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002342 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002343
2344 if (Prev->Body) {
2345 // If it's not the case that both 'inline' and 'extern' are
2346 // specified on the definition, then it is always externally visible.
2347 if (!Prev->isInlineSpecified() ||
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002348 Prev->getStorageClass() != SC_Extern)
Eli Friedman1b125c32012-02-07 03:50:18 +00002349 return false;
2350 } else if (Prev->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002351 Prev->getStorageClass() != SC_Extern) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002352 return false;
2353 }
2354 }
2355 return FoundBody;
2356 }
2357
David Blaikiebbafb8a2012-03-11 07:00:24 +00002358 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002359 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002360
2361 // C99 6.7.4p6:
2362 // [...] If all of the file scope declarations for a function in a
2363 // translation unit include the inline function specifier without extern,
2364 // then the definition in that translation unit is an inline definition.
2365 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002366 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002367 const FunctionDecl *Prev = this;
2368 bool FoundBody = false;
2369 while ((Prev = Prev->getPreviousDecl())) {
David Blaikie7d170102013-05-15 07:37:26 +00002370 FoundBody |= Prev->Body.isValid();
Eli Friedman1b125c32012-02-07 03:50:18 +00002371 if (RedeclForcesDefC99(Prev))
2372 return false;
2373 }
2374 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002375}
2376
Richard Smithf3814ad2013-01-25 00:08:28 +00002377/// \brief For an inline function definition in C, or for a gnu_inline function
2378/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002379///
2380/// Inline function definitions are always available for inlining optimizations.
2381/// However, depending on the language dialect, declaration specifiers, and
2382/// attributes, the definition of an inline function may or may not be
2383/// "externally" visible to other translation units in the program.
2384///
2385/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002386/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002387/// inline definition becomes externally visible (C99 6.7.4p6).
2388///
2389/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2390/// definition, we use the GNU semantics for inline, which are nearly the
2391/// opposite of C99 semantics. In particular, "inline" by itself will create
2392/// an externally visible symbol, but "extern inline" will not create an
2393/// externally visible symbol.
2394bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002395 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002396 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002397 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002398
David Blaikiebbafb8a2012-03-11 07:00:24 +00002399 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002400 // Note: If you change the logic here, please change
2401 // doesDeclarationForceExternallyVisibleDefinition as well.
2402 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002403 // If it's not the case that both 'inline' and 'extern' are
2404 // specified on the definition, then this inline definition is
2405 // externally visible.
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002406 if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
Douglas Gregorff76cb92010-12-09 16:59:22 +00002407 return true;
2408
2409 // If any declaration is 'inline' but not 'extern', then this definition
2410 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002411 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2412 Redecl != RedeclEnd;
2413 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002414 if (Redecl->isInlineSpecified() &&
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002415 Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002416 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002417 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002418
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002419 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002420 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002421
Richard Smithf3814ad2013-01-25 00:08:28 +00002422 // The rest of this function is C-only.
2423 assert(!Context.getLangOpts().CPlusPlus &&
2424 "should not use C inline rules in C++");
2425
Douglas Gregor299d76e2009-09-13 07:46:26 +00002426 // C99 6.7.4p6:
2427 // [...] If all of the file scope declarations for a function in a
2428 // translation unit include the inline function specifier without extern,
2429 // then the definition in that translation unit is an inline definition.
2430 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2431 Redecl != RedeclEnd;
2432 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002433 if (RedeclForcesDefC99(*Redecl))
2434 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002435 }
2436
2437 // C99 6.7.4p6:
2438 // An inline definition does not provide an external definition for the
2439 // function, and does not forbid an external definition in another
2440 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002441 return false;
2442}
2443
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002444/// getOverloadedOperator - Which C++ overloaded operator this
2445/// function represents, if any.
2446OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002447 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2448 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002449 else
2450 return OO_None;
2451}
2452
Alexis Huntc88db062010-01-13 09:01:02 +00002453/// getLiteralIdentifier - The literal suffix identifier this function
2454/// represents, if any.
2455const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2456 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2457 return getDeclName().getCXXLiteralIdentifier();
2458 else
2459 return 0;
2460}
2461
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002462FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2463 if (TemplateOrSpecialization.isNull())
2464 return TK_NonTemplate;
2465 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2466 return TK_FunctionTemplate;
2467 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2468 return TK_MemberSpecialization;
2469 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2470 return TK_FunctionTemplateSpecialization;
2471 if (TemplateOrSpecialization.is
2472 <DependentFunctionTemplateSpecializationInfo*>())
2473 return TK_DependentFunctionTemplateSpecialization;
2474
David Blaikie83d382b2011-09-23 05:06:16 +00002475 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002476}
2477
Douglas Gregord801b062009-10-07 23:56:10 +00002478FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002479 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002480 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2481
2482 return 0;
2483}
2484
2485void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002486FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2487 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002488 TemplateSpecializationKind TSK) {
2489 assert(TemplateOrSpecialization.isNull() &&
2490 "Member function is already a specialization");
2491 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002492 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002493 TemplateOrSpecialization = Info;
2494}
2495
Douglas Gregorafca3b42009-10-27 20:53:28 +00002496bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002497 // If the function is invalid, it can't be implicitly instantiated.
2498 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002499 return false;
2500
2501 switch (getTemplateSpecializationKind()) {
2502 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002503 case TSK_ExplicitInstantiationDefinition:
2504 return false;
2505
2506 case TSK_ImplicitInstantiation:
2507 return true;
2508
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002509 // It is possible to instantiate TSK_ExplicitSpecialization kind
2510 // if the FunctionDecl has a class scope specialization pattern.
2511 case TSK_ExplicitSpecialization:
2512 return getClassScopeSpecializationPattern() != 0;
2513
Douglas Gregorafca3b42009-10-27 20:53:28 +00002514 case TSK_ExplicitInstantiationDeclaration:
2515 // Handled below.
2516 break;
2517 }
2518
2519 // Find the actual template from which we will instantiate.
2520 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002521 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002522 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002523 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002524
2525 // C++0x [temp.explicit]p9:
2526 // Except for inline functions, other explicit instantiation declarations
2527 // have the effect of suppressing the implicit instantiation of the entity
2528 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002529 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002530 return true;
2531
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002532 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002533}
2534
2535bool FunctionDecl::isTemplateInstantiation() const {
2536 switch (getTemplateSpecializationKind()) {
2537 case TSK_Undeclared:
2538 case TSK_ExplicitSpecialization:
2539 return false;
2540 case TSK_ImplicitInstantiation:
2541 case TSK_ExplicitInstantiationDeclaration:
2542 case TSK_ExplicitInstantiationDefinition:
2543 return true;
2544 }
2545 llvm_unreachable("All TSK values handled.");
2546}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002547
2548FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002549 // Handle class scope explicit specialization special case.
2550 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2551 return getClassScopeSpecializationPattern();
2552
Douglas Gregorafca3b42009-10-27 20:53:28 +00002553 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2554 while (Primary->getInstantiatedFromMemberTemplate()) {
2555 // If we have hit a point where the user provided a specialization of
2556 // this template, we're done looking.
2557 if (Primary->isMemberSpecialization())
2558 break;
2559
2560 Primary = Primary->getInstantiatedFromMemberTemplate();
2561 }
2562
2563 return Primary->getTemplatedDecl();
2564 }
2565
2566 return getInstantiatedFromMemberFunction();
2567}
2568
Douglas Gregor70d83e22009-06-29 17:30:29 +00002569FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002570 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002571 = TemplateOrSpecialization
2572 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002573 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002574 }
2575 return 0;
2576}
2577
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002578FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2579 return getASTContext().getClassScopeSpecializationPattern(this);
2580}
2581
Douglas Gregor70d83e22009-06-29 17:30:29 +00002582const TemplateArgumentList *
2583FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002584 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002585 = TemplateOrSpecialization
2586 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002587 return Info->TemplateArguments;
2588 }
2589 return 0;
2590}
2591
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002592const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002593FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2594 if (FunctionTemplateSpecializationInfo *Info
2595 = TemplateOrSpecialization
2596 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2597 return Info->TemplateArgumentsAsWritten;
2598 }
2599 return 0;
2600}
2601
Mike Stump11289f42009-09-09 15:08:12 +00002602void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002603FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2604 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002605 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002606 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002607 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002608 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2609 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002610 assert(TSK != TSK_Undeclared &&
2611 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002612 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002613 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002614 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002615 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2616 TemplateArgs,
2617 TemplateArgsAsWritten,
2618 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002619 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002620 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002621}
2622
John McCallb9c78482010-04-08 09:05:18 +00002623void
2624FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2625 const UnresolvedSetImpl &Templates,
2626 const TemplateArgumentListInfo &TemplateArgs) {
2627 assert(TemplateOrSpecialization.isNull());
2628 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2629 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002630 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002631 void *Buffer = Context.Allocate(Size);
2632 DependentFunctionTemplateSpecializationInfo *Info =
2633 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2634 TemplateArgs);
2635 TemplateOrSpecialization = Info;
2636}
2637
2638DependentFunctionTemplateSpecializationInfo::
2639DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2640 const TemplateArgumentListInfo &TArgs)
2641 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2642
2643 d.NumTemplates = Ts.size();
2644 d.NumArgs = TArgs.size();
2645
2646 FunctionTemplateDecl **TsArray =
2647 const_cast<FunctionTemplateDecl**>(getTemplates());
2648 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2649 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2650
2651 TemplateArgumentLoc *ArgsArray =
2652 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2653 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2654 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2655}
2656
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002657TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002658 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002659 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002660 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002661 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002662 if (FTSInfo)
2663 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002664
Douglas Gregord801b062009-10-07 23:56:10 +00002665 MemberSpecializationInfo *MSInfo
2666 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2667 if (MSInfo)
2668 return MSInfo->getTemplateSpecializationKind();
2669
2670 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002671}
2672
Mike Stump11289f42009-09-09 15:08:12 +00002673void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002674FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2675 SourceLocation PointOfInstantiation) {
2676 if (FunctionTemplateSpecializationInfo *FTSInfo
2677 = TemplateOrSpecialization.dyn_cast<
2678 FunctionTemplateSpecializationInfo*>()) {
2679 FTSInfo->setTemplateSpecializationKind(TSK);
2680 if (TSK != TSK_ExplicitSpecialization &&
2681 PointOfInstantiation.isValid() &&
2682 FTSInfo->getPointOfInstantiation().isInvalid())
2683 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2684 } else if (MemberSpecializationInfo *MSInfo
2685 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2686 MSInfo->setTemplateSpecializationKind(TSK);
2687 if (TSK != TSK_ExplicitSpecialization &&
2688 PointOfInstantiation.isValid() &&
2689 MSInfo->getPointOfInstantiation().isInvalid())
2690 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2691 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002692 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002693}
2694
2695SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002696 if (FunctionTemplateSpecializationInfo *FTSInfo
2697 = TemplateOrSpecialization.dyn_cast<
2698 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002699 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002700 else if (MemberSpecializationInfo *MSInfo
2701 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002702 return MSInfo->getPointOfInstantiation();
2703
2704 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002705}
2706
Douglas Gregor6411b922009-09-11 20:15:17 +00002707bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002708 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002709 return true;
2710
2711 // If this function was instantiated from a member function of a
2712 // class template, check whether that member function was defined out-of-line.
2713 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2714 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002715 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002716 return Definition->isOutOfLine();
2717 }
2718
2719 // If this function was instantiated from a function template,
2720 // check whether that function template was defined out-of-line.
2721 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2722 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002723 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002724 return Definition->isOutOfLine();
2725 }
2726
2727 return false;
2728}
2729
Abramo Bagnaraea947882011-03-08 16:41:52 +00002730SourceRange FunctionDecl::getSourceRange() const {
2731 return SourceRange(getOuterLocStart(), EndRangeLoc);
2732}
2733
Anna Zaks28db7ce2012-01-18 02:45:01 +00002734unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002735 IdentifierInfo *FnInfo = getIdentifier();
2736
2737 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002738 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002739
2740 // Builtin handling.
2741 switch (getBuiltinID()) {
2742 case Builtin::BI__builtin_memset:
2743 case Builtin::BI__builtin___memset_chk:
2744 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002745 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002746
2747 case Builtin::BI__builtin_memcpy:
2748 case Builtin::BI__builtin___memcpy_chk:
2749 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002750 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002751
2752 case Builtin::BI__builtin_memmove:
2753 case Builtin::BI__builtin___memmove_chk:
2754 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002755 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002756
2757 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002758 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002759 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002760 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002761
2762 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002763 case Builtin::BImemcmp:
2764 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002765
2766 case Builtin::BI__builtin_strncpy:
2767 case Builtin::BI__builtin___strncpy_chk:
2768 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002769 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002770
2771 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002772 case Builtin::BIstrncmp:
2773 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002774
2775 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002776 case Builtin::BIstrncasecmp:
2777 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002778
2779 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002780 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002781 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002782 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002783
2784 case Builtin::BI__builtin_strndup:
2785 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002786 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002787
Anna Zaks314cd092012-02-01 19:08:57 +00002788 case Builtin::BI__builtin_strlen:
2789 case Builtin::BIstrlen:
2790 return Builtin::BIstrlen;
2791
Anna Zaks201d4892012-01-13 21:52:01 +00002792 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002793 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002794 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002795 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002796 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002797 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002798 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002799 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002800 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002801 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002802 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002803 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002804 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002805 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002806 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002807 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002808 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002809 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002810 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002811 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002812 else if (FnInfo->isStr("strlen"))
2813 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002814 }
2815 break;
2816 }
Anna Zaks22122702012-01-17 00:37:07 +00002817 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002818}
2819
Chris Lattner59a25942008-03-31 00:36:02 +00002820//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002821// FieldDecl Implementation
2822//===----------------------------------------------------------------------===//
2823
Jay Foad39c79802011-01-12 09:06:06 +00002824FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002825 SourceLocation StartLoc, SourceLocation IdLoc,
2826 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002827 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002828 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002829 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002830 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002831}
2832
Douglas Gregor72172e92012-01-05 21:55:30 +00002833FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2834 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2835 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002836 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002837}
2838
Sebastian Redl833ef452010-01-26 22:01:41 +00002839bool FieldDecl::isAnonymousStructOrUnion() const {
2840 if (!isImplicit() || getDeclName())
2841 return false;
2842
2843 if (const RecordType *Record = getType()->getAs<RecordType>())
2844 return Record->getDecl()->isAnonymousStructOrUnion();
2845
2846 return false;
2847}
2848
Richard Smithcaf33902011-10-10 18:28:20 +00002849unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2850 assert(isBitField() && "not a bitfield");
2851 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2852 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2853}
2854
John McCall4e819612011-01-20 07:57:12 +00002855unsigned FieldDecl::getFieldIndex() const {
2856 if (CachedFieldIndex) return CachedFieldIndex - 1;
2857
Richard Smithd62306a2011-11-10 06:34:14 +00002858 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002859 const RecordDecl *RD = getParent();
2860 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002861 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002862
2863 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2864 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002865 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002866
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002867 if (IsMsStruct) {
2868 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002869 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002870 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002871 continue;
2872 }
David Blaikie40ed2972012-06-06 20:45:41 +00002873 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002874 }
John McCall4e819612011-01-20 07:57:12 +00002875 }
2876
Richard Smithd62306a2011-11-10 06:34:14 +00002877 assert(CachedFieldIndex && "failed to find field in parent");
2878 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002879}
2880
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002881SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002882 if (const Expr *E = InitializerOrBitWidth.getPointer())
2883 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002884 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002885}
2886
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002887void FieldDecl::setBitWidth(Expr *Width) {
2888 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2889 "bit width or initializer already set");
2890 InitializerOrBitWidth.setPointer(Width);
2891}
2892
Richard Smith938f40b2011-06-11 17:19:42 +00002893void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002894 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002895 "bit width or initializer already set");
2896 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002897}
2898
Sebastian Redl833ef452010-01-26 22:01:41 +00002899//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002900// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002901//===----------------------------------------------------------------------===//
2902
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002903SourceLocation TagDecl::getOuterLocStart() const {
2904 return getTemplateOrInnerLocStart(this);
2905}
2906
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002907SourceRange TagDecl::getSourceRange() const {
2908 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002909 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002910}
2911
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002912TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002913 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002914}
2915
Rafael Espindolabf5c33b2013-03-12 21:06:00 +00002916void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2917 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002918 if (TypeForDecl)
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002919 assert(TypeForDecl->isLinkageValid());
2920 assert(isLinkageValid());
Douglas Gregora72a4e32010-05-19 18:39:18 +00002921}
2922
Douglas Gregordee1be82009-01-17 00:42:38 +00002923void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002924 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002925
David Blaikie095deba2012-11-14 01:52:05 +00002926 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002927 struct CXXRecordDecl::DefinitionData *Data =
2928 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002929 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2930 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002931 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002932}
2933
2934void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002935 assert((!isa<CXXRecordDecl>(this) ||
2936 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2937 "definition completed but not started");
2938
John McCallf937c022011-10-07 06:10:15 +00002939 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002940 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002941
2942 if (ASTMutationListener *L = getASTMutationListener())
2943 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002944}
2945
John McCallf937c022011-10-07 06:10:15 +00002946TagDecl *TagDecl::getDefinition() const {
2947 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002948 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002949
2950 // If it's possible for us to have an out-of-date definition, check now.
2951 if (MayHaveOutOfDateDef) {
2952 if (IdentifierInfo *II = getIdentifier()) {
2953 if (II->isOutOfDate()) {
2954 updateOutOfDate(*II);
2955 }
2956 }
2957 }
2958
Andrew Trickba266ee2010-10-19 21:54:32 +00002959 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2960 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002961
2962 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002963 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002964 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002965 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002966
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002967 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002968}
2969
Douglas Gregor14454802011-02-25 02:25:35 +00002970void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2971 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002972 // Make sure the extended qualifier info is allocated.
2973 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002974 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002975 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002976 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002977 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002978 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002979 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002980 if (getExtInfo()->NumTemplParamLists == 0) {
2981 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002982 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002983 }
2984 else
2985 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002986 }
2987 }
2988}
2989
Abramo Bagnara60804e12011-03-18 15:16:37 +00002990void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2991 unsigned NumTPLists,
2992 TemplateParameterList **TPLists) {
2993 assert(NumTPLists > 0);
2994 // Make sure the extended decl info is allocated.
2995 if (!hasExtInfo())
2996 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002997 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002998 // Set the template parameter lists info.
2999 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
3000}
3001
Ted Kremenek21475702008-09-05 17:16:31 +00003002//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00003003// EnumDecl Implementation
3004//===----------------------------------------------------------------------===//
3005
David Blaikie68e081d2011-12-20 02:48:34 +00003006void EnumDecl::anchor() { }
3007
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003008EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3009 SourceLocation StartLoc, SourceLocation IdLoc,
3010 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003011 EnumDecl *PrevDecl, bool IsScoped,
3012 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003013 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003014 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003015 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00003016 C.getTypeDeclType(Enum, PrevDecl);
3017 return Enum;
3018}
3019
Douglas Gregor72172e92012-01-05 21:55:30 +00003020EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3021 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003022 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
3023 0, 0, false, false, false);
3024 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3025 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003026}
3027
Douglas Gregord5058122010-02-11 01:19:42 +00003028void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00003029 QualType NewPromotionType,
3030 unsigned NumPositiveBits,
3031 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00003032 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00003033 if (!IntegerType)
3034 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00003035 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00003036 setNumPositiveBits(NumPositiveBits);
3037 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00003038 TagDecl::completeDefinition();
3039}
3040
Richard Smith7d137e32012-03-23 03:33:32 +00003041TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3042 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3043 return MSI->getTemplateSpecializationKind();
3044
3045 return TSK_Undeclared;
3046}
3047
3048void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3049 SourceLocation PointOfInstantiation) {
3050 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3051 assert(MSI && "Not an instantiated member enumeration?");
3052 MSI->setTemplateSpecializationKind(TSK);
3053 if (TSK != TSK_ExplicitSpecialization &&
3054 PointOfInstantiation.isValid() &&
3055 MSI->getPointOfInstantiation().isInvalid())
3056 MSI->setPointOfInstantiation(PointOfInstantiation);
3057}
3058
Richard Smith4b38ded2012-03-14 23:13:10 +00003059EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3060 if (SpecializationInfo)
3061 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3062
3063 return 0;
3064}
3065
3066void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3067 TemplateSpecializationKind TSK) {
3068 assert(!SpecializationInfo && "Member enum is already a specialization");
3069 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3070}
3071
Sebastian Redl833ef452010-01-26 22:01:41 +00003072//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00003073// RecordDecl Implementation
3074//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00003075
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003076RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
3077 SourceLocation StartLoc, SourceLocation IdLoc,
3078 IdentifierInfo *Id, RecordDecl *PrevDecl)
3079 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00003080 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003081 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003082 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003083 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003084 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00003085 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00003086}
3087
Jay Foad39c79802011-01-12 09:06:06 +00003088RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003089 SourceLocation StartLoc, SourceLocation IdLoc,
3090 IdentifierInfo *Id, RecordDecl* PrevDecl) {
3091 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
3092 PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003093 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3094
Ted Kremenek21475702008-09-05 17:16:31 +00003095 C.getTypeDeclType(R, PrevDecl);
3096 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00003097}
3098
Douglas Gregor72172e92012-01-05 21:55:30 +00003099RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
3100 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003101 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
3102 SourceLocation(), 0, 0);
3103 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3104 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00003105}
3106
Douglas Gregordfcad112009-03-25 15:59:44 +00003107bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00003108 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00003109 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3110}
3111
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003112RecordDecl::field_iterator RecordDecl::field_begin() const {
3113 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3114 LoadFieldsFromExternalStorage();
3115
3116 return field_iterator(decl_iterator(FirstDecl));
3117}
3118
Douglas Gregorb11aad82011-02-19 18:51:44 +00003119/// completeDefinition - Notes that the definition of this type is now
3120/// complete.
3121void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00003122 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00003123 TagDecl::completeDefinition();
3124}
3125
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003126/// isMsStruct - Get whether or not this record uses ms_struct layout.
3127/// This which can be turned on with an attribute, pragma, or the
3128/// -mms-bitfields command-line option.
3129bool RecordDecl::isMsStruct(const ASTContext &C) const {
3130 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
3131}
3132
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003133static bool isFieldOrIndirectField(Decl::Kind K) {
3134 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3135}
3136
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003137void RecordDecl::LoadFieldsFromExternalStorage() const {
3138 ExternalASTSource *Source = getASTContext().getExternalSource();
3139 assert(hasExternalLexicalStorage() && Source && "No external storage?");
3140
3141 // Notify that we have a RecordDecl doing some initialization.
3142 ExternalASTSource::Deserializing TheFields(Source);
3143
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003144 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003145 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003146 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
3147 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003148 case ELR_Success:
3149 break;
3150
3151 case ELR_AlreadyLoaded:
3152 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003153 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00003154 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003155
3156#ifndef NDEBUG
3157 // Check that all decls we got were FieldDecls.
3158 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00003159 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003160#endif
3161
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003162 if (Decls.empty())
3163 return;
3164
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00003165 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
3166 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003167}
3168
Steve Naroff415d3d52008-10-08 17:01:13 +00003169//===----------------------------------------------------------------------===//
3170// BlockDecl Implementation
3171//===----------------------------------------------------------------------===//
3172
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003173void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00003174 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00003175
Steve Naroffc4b30e52009-03-13 16:56:44 +00003176 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00003177 if (!NewParamInfo.empty()) {
3178 NumParams = NewParamInfo.size();
3179 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3180 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003181 }
3182}
3183
John McCall351762c2011-02-07 10:33:21 +00003184void BlockDecl::setCaptures(ASTContext &Context,
3185 const Capture *begin,
3186 const Capture *end,
3187 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00003188 CapturesCXXThis = capturesCXXThis;
3189
3190 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00003191 NumCaptures = 0;
3192 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00003193 return;
3194 }
3195
John McCall351762c2011-02-07 10:33:21 +00003196 NumCaptures = end - begin;
3197
3198 // Avoid new Capture[] because we don't want to provide a default
3199 // constructor.
3200 size_t allocationSize = NumCaptures * sizeof(Capture);
3201 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
3202 memcpy(buffer, begin, allocationSize);
3203 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00003204}
Sebastian Redl833ef452010-01-26 22:01:41 +00003205
John McCallce45f882011-06-15 22:51:16 +00003206bool BlockDecl::capturesVariable(const VarDecl *variable) const {
3207 for (capture_const_iterator
3208 i = capture_begin(), e = capture_end(); i != e; ++i)
3209 // Only auto vars can be captured, so no redeclaration worries.
3210 if (i->getVariable() == variable)
3211 return true;
3212
3213 return false;
3214}
3215
Douglas Gregor70226da2010-12-21 16:27:07 +00003216SourceRange BlockDecl::getSourceRange() const {
3217 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
3218}
Sebastian Redl833ef452010-01-26 22:01:41 +00003219
3220//===----------------------------------------------------------------------===//
3221// Other Decl Allocation/Deallocation Method Implementations
3222//===----------------------------------------------------------------------===//
3223
David Blaikie68e081d2011-12-20 02:48:34 +00003224void TranslationUnitDecl::anchor() { }
3225
Sebastian Redl833ef452010-01-26 22:01:41 +00003226TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
3227 return new (C) TranslationUnitDecl(C);
3228}
3229
David Blaikie68e081d2011-12-20 02:48:34 +00003230void LabelDecl::anchor() { }
3231
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003232LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00003233 SourceLocation IdentL, IdentifierInfo *II) {
3234 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
3235}
3236
3237LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3238 SourceLocation IdentL, IdentifierInfo *II,
3239 SourceLocation GnuLabelL) {
3240 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3241 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003242}
3243
Douglas Gregor72172e92012-01-05 21:55:30 +00003244LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3245 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3246 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00003247}
3248
David Blaikie68e081d2011-12-20 02:48:34 +00003249void ValueDecl::anchor() { }
3250
Benjamin Kramerea70eb32012-12-01 15:09:41 +00003251bool ValueDecl::isWeak() const {
3252 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3253 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3254 return true;
3255
3256 return isWeakImported();
3257}
3258
David Blaikie68e081d2011-12-20 02:48:34 +00003259void ImplicitParamDecl::anchor() { }
3260
Sebastian Redl833ef452010-01-26 22:01:41 +00003261ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003262 SourceLocation IdLoc,
3263 IdentifierInfo *Id,
3264 QualType Type) {
3265 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00003266}
3267
Douglas Gregor72172e92012-01-05 21:55:30 +00003268ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3269 unsigned ID) {
3270 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3271 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3272}
3273
Sebastian Redl833ef452010-01-26 22:01:41 +00003274FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003275 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003276 const DeclarationNameInfo &NameInfo,
3277 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003278 StorageClass SC,
Douglas Gregorff76cb92010-12-09 16:59:22 +00003279 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00003280 bool hasWrittenPrototype,
3281 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003282 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003283 T, TInfo, SC,
Richard Smitha77a0a62011-08-15 21:04:07 +00003284 isInlineSpecified,
3285 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00003286 New->HasWrittenPrototype = hasWrittenPrototype;
3287 return New;
3288}
3289
Douglas Gregor72172e92012-01-05 21:55:30 +00003290FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3291 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3292 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3293 DeclarationNameInfo(), QualType(), 0,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003294 SC_None, false, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00003295}
3296
Sebastian Redl833ef452010-01-26 22:01:41 +00003297BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3298 return new (C) BlockDecl(DC, L);
3299}
3300
Douglas Gregor72172e92012-01-05 21:55:30 +00003301BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3302 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3303 return new (Mem) BlockDecl(0, SourceLocation());
3304}
3305
John McCall5e77d762013-04-16 07:28:30 +00003306MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3307 unsigned ID) {
3308 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(MSPropertyDecl));
3309 return new (Mem) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
3310 QualType(), 0, SourceLocation(),
3311 0, 0);
3312}
3313
Ben Langmuir37943a72013-05-03 19:00:33 +00003314CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
3315 unsigned NumParams) {
Ben Langmuirce914fc2013-05-03 19:20:19 +00003316 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
Ben Langmuir37943a72013-05-03 19:00:33 +00003317 return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003318}
3319
Ben Langmuirce914fc2013-05-03 19:20:19 +00003320CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3321 unsigned NumParams) {
3322 unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3323 void *Mem = AllocateDeserializedDecl(C, ID, Size);
3324 return new (Mem) CapturedDecl(0, NumParams);
3325}
3326
Sebastian Redl833ef452010-01-26 22:01:41 +00003327EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3328 SourceLocation L,
3329 IdentifierInfo *Id, QualType T,
3330 Expr *E, const llvm::APSInt &V) {
3331 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3332}
3333
Douglas Gregor72172e92012-01-05 21:55:30 +00003334EnumConstantDecl *
3335EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3336 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3337 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3338 llvm::APSInt());
3339}
3340
David Blaikie68e081d2011-12-20 02:48:34 +00003341void IndirectFieldDecl::anchor() { }
3342
Benjamin Kramer39593702010-11-21 14:11:41 +00003343IndirectFieldDecl *
3344IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3345 IdentifierInfo *Id, QualType T, NamedDecl **CH,
3346 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003347 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3348}
3349
Douglas Gregor72172e92012-01-05 21:55:30 +00003350IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3351 unsigned ID) {
3352 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3353 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3354 QualType(), 0, 0);
3355}
3356
Douglas Gregorbe996932010-09-01 20:41:53 +00003357SourceRange EnumConstantDecl::getSourceRange() const {
3358 SourceLocation End = getLocation();
3359 if (Init)
3360 End = Init->getLocEnd();
3361 return SourceRange(getLocation(), End);
3362}
3363
David Blaikie68e081d2011-12-20 02:48:34 +00003364void TypeDecl::anchor() { }
3365
Sebastian Redl833ef452010-01-26 22:01:41 +00003366TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003367 SourceLocation StartLoc, SourceLocation IdLoc,
3368 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3369 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003370}
3371
David Blaikie68e081d2011-12-20 02:48:34 +00003372void TypedefNameDecl::anchor() { }
3373
Douglas Gregor72172e92012-01-05 21:55:30 +00003374TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3375 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3376 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3377}
3378
Richard Smithdda56e42011-04-15 14:24:37 +00003379TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3380 SourceLocation StartLoc,
3381 SourceLocation IdLoc, IdentifierInfo *Id,
3382 TypeSourceInfo *TInfo) {
3383 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3384}
3385
Douglas Gregor72172e92012-01-05 21:55:30 +00003386TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3387 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3388 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3389}
3390
Abramo Bagnaraea947882011-03-08 16:41:52 +00003391SourceRange TypedefDecl::getSourceRange() const {
3392 SourceLocation RangeEnd = getLocation();
3393 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3394 if (typeIsPostfix(TInfo->getType()))
3395 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3396 }
3397 return SourceRange(getLocStart(), RangeEnd);
3398}
3399
Richard Smithdda56e42011-04-15 14:24:37 +00003400SourceRange TypeAliasDecl::getSourceRange() const {
3401 SourceLocation RangeEnd = getLocStart();
3402 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3403 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3404 return SourceRange(getLocStart(), RangeEnd);
3405}
3406
David Blaikie68e081d2011-12-20 02:48:34 +00003407void FileScopeAsmDecl::anchor() { }
3408
Sebastian Redl833ef452010-01-26 22:01:41 +00003409FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003410 StringLiteral *Str,
3411 SourceLocation AsmLoc,
3412 SourceLocation RParenLoc) {
3413 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003414}
Douglas Gregorba345522011-12-02 23:23:56 +00003415
Douglas Gregor72172e92012-01-05 21:55:30 +00003416FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3417 unsigned ID) {
3418 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3419 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3420}
3421
Michael Han84324352013-02-22 17:15:32 +00003422void EmptyDecl::anchor() {}
3423
3424EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3425 return new (C) EmptyDecl(DC, L);
3426}
3427
3428EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3429 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EmptyDecl));
3430 return new (Mem) EmptyDecl(0, SourceLocation());
3431}
3432
Douglas Gregorba345522011-12-02 23:23:56 +00003433//===----------------------------------------------------------------------===//
3434// ImportDecl Implementation
3435//===----------------------------------------------------------------------===//
3436
3437/// \brief Retrieve the number of module identifiers needed to name the given
3438/// module.
3439static unsigned getNumModuleIdentifiers(Module *Mod) {
3440 unsigned Result = 1;
3441 while (Mod->Parent) {
3442 Mod = Mod->Parent;
3443 ++Result;
3444 }
3445 return Result;
3446}
3447
Douglas Gregor22d09742012-01-03 18:04:46 +00003448ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003449 Module *Imported,
3450 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003451 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003452 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003453{
3454 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3455 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3456 memcpy(StoredLocs, IdentifierLocs.data(),
3457 IdentifierLocs.size() * sizeof(SourceLocation));
3458}
3459
Douglas Gregor22d09742012-01-03 18:04:46 +00003460ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003461 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003462 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003463 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003464{
3465 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3466}
3467
3468ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003469 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003470 ArrayRef<SourceLocation> IdentifierLocs) {
3471 void *Mem = C.Allocate(sizeof(ImportDecl) +
3472 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003473 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003474}
3475
3476ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003477 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003478 Module *Imported,
3479 SourceLocation EndLoc) {
3480 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003481 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003482 Import->setImplicit();
3483 return Import;
3484}
3485
Douglas Gregor72172e92012-01-05 21:55:30 +00003486ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3487 unsigned NumLocations) {
3488 void *Mem = AllocateDeserializedDecl(C, ID,
3489 (sizeof(ImportDecl) +
3490 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003491 return new (Mem) ImportDecl(EmptyShell());
3492}
3493
3494ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3495 if (!ImportedAndComplete.getInt())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00003496 return None;
Douglas Gregorba345522011-12-02 23:23:56 +00003497
3498 const SourceLocation *StoredLocs
3499 = reinterpret_cast<const SourceLocation *>(this + 1);
3500 return ArrayRef<SourceLocation>(StoredLocs,
3501 getNumModuleIdentifiers(getImportedModule()));
3502}
3503
3504SourceRange ImportDecl::getSourceRange() const {
3505 if (!ImportedAndComplete.getInt())
3506 return SourceRange(getLocation(),
3507 *reinterpret_cast<const SourceLocation *>(this + 1));
3508
3509 return SourceRange(getLocation(), getIdentifierLocs().back());
3510}