blob: 0183c0b59ed9247da0e2bbcb7643d54802521329 [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"
David Blaikie9c70e042011-09-21 18:16:56 +000032#include <algorithm>
33
Chris Lattner6d9a6852006-10-25 05:11:20 +000034using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000035
Chris Lattner88f70d62008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
John McCalldf25c432013-02-16 00:17:33 +000040// Visibility rules aren't rigorously externally specified, but here
41// are the basic principles behind what we implement:
42//
43// 1. An explicit visibility attribute is generally a direct expression
44// of the user's intent and should be honored. Only the innermost
45// visibility attribute applies. If no visibility attribute applies,
46// global visibility settings are considered.
47//
48// 2. There is one caveat to the above: on or in a template pattern,
49// an explicit visibility attribute is just a default rule, and
50// visibility can be decreased by the visibility of template
51// arguments. But this, too, has an exception: an attribute on an
52// explicit specialization or instantiation causes all the visibility
53// restrictions of the template arguments to be ignored.
54//
55// 3. A variable that does not otherwise have explicit visibility can
56// be restricted by the visibility of its type.
57//
58// 4. A visibility restriction is explicit if it comes from an
59// attribute (or something like it), not a global visibility setting.
60// When emitting a reference to an external symbol, visibility
61// restrictions are ignored unless they are explicit.
62
63/// Kinds of LV computation. The linkage side of the computation is
64/// always the same, but different things can change how visibility is
65/// computed.
66enum LVComputationKind {
67 /// Do an LV computation that does everything normal for linkage but
68 /// ignores sources of visibility other than template arguments.
69 LVOnlyTemplateArguments,
70
71 /// Do a normal LV computation for, ultimately, a type.
72 LVForType,
73
74 /// Do a normal LV computation for, ultimately, a value.
75 LVForValue
76};
77
78typedef NamedDecl::LinkageInfo LinkageInfo;
79
80/// Is the given declaration a "type" or a "value" for the purposes of
81/// visibility computation?
82static bool usesTypeVisibility(const NamedDecl *D) {
83 return isa<TypeDecl>(D) || isa<ClassTemplateDecl>(D);
84}
85
86/// Return the explicit visibility of the given declaration.
Douglas Gregor1baf38f2011-03-26 12:10:19 +000087static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
88 // If this declaration has an explicit visibility attribute, use it.
89 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
90 switch (A->getVisibility()) {
91 case VisibilityAttr::Default:
92 return DefaultVisibility;
93 case VisibilityAttr::Hidden:
94 return HiddenVisibility;
95 case VisibilityAttr::Protected:
96 return ProtectedVisibility;
97 }
John McCall457a04e2010-10-22 21:05:15 +000098 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +000099
100 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
101 // implies visibility(default).
Douglas Gregore8bbc122011-09-02 00:18:52 +0000102 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000103 for (specific_attr_iterator<AvailabilityAttr>
104 A = D->specific_attr_begin<AvailabilityAttr>(),
105 AEnd = D->specific_attr_end<AvailabilityAttr>();
106 A != AEnd; ++A)
107 if ((*A)->getPlatform()->getName().equals("macosx"))
108 return DefaultVisibility;
109 }
110
111 return llvm::Optional<Visibility>();
John McCall457a04e2010-10-22 21:05:15 +0000112}
113
Rafael Espindola2f869a32012-01-14 00:30:36 +0000114static LinkageInfo getLVForType(QualType T) {
115 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
116 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
117}
118
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000119/// \brief Get the most restrictive linkage for the types in the given
John McCalldf25c432013-02-16 00:17:33 +0000120/// template parameter list. For visibility purposes, template
121/// parameters are part of the signature of a template.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000122static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000123getLVForTemplateParameterList(const TemplateParameterList *params) {
124 LinkageInfo LV;
125 for (TemplateParameterList::const_iterator P = params->begin(),
126 PEnd = params->end();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000127 P != PEnd; ++P) {
John McCalldf25c432013-02-16 00:17:33 +0000128
129 // Template type parameters are the most common and never
130 // contribute to visibility, pack or not.
131 if (isa<TemplateTypeParmDecl>(*P))
132 continue;
133
134 // Non-type template parameters can be restricted by the value type, e.g.
135 // template <enum X> class A { ... };
136 // We have to be careful here, though, because we can be dealing with
137 // dependent types.
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000138 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
John McCalldf25c432013-02-16 00:17:33 +0000139 // Handle the non-pack case first.
140 if (!NTTP->isExpandedParameterPack()) {
141 if (!NTTP->getType()->isDependentType()) {
142 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000143 }
144 continue;
145 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000146
John McCalldf25c432013-02-16 00:17:33 +0000147 // Look at all the types in an expanded pack.
148 for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) {
149 QualType type = NTTP->getExpansionType(i);
150 if (!type->isDependentType())
151 LV.merge(getLVForType(type));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000152 }
John McCalldf25c432013-02-16 00:17:33 +0000153 continue;
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000154 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000155
John McCalldf25c432013-02-16 00:17:33 +0000156 // Template template parameters can be restricted by their
157 // template parameters, recursively.
158 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
159
160 // Handle the non-pack case first.
161 if (!TTP->isExpandedParameterPack()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000162 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
John McCalldf25c432013-02-16 00:17:33 +0000163 continue;
164 }
165
166 // Look at all expansions in an expanded pack.
167 for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters();
168 i != n; ++i) {
169 LV.merge(getLVForTemplateParameterList(
170 TTP->getExpansionTemplateParameters(i)));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000171 }
172 }
173
John McCall457a04e2010-10-22 21:05:15 +0000174 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000175}
176
Rafael Espindola19de5612013-01-12 06:42:30 +0000177/// getLVForDecl - Get the linkage and visibility for the given declaration.
John McCalldf25c432013-02-16 00:17:33 +0000178static LinkageInfo getLVForDecl(const NamedDecl *D,
179 LVComputationKind computation);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000180
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000181/// \brief Get the most restrictive linkage for the types and
182/// declarations in the given template argument list.
John McCalldf25c432013-02-16 00:17:33 +0000183///
184/// Note that we don't take an LVComputationKind because we always
185/// want to honor the visibility of template arguments in the same way.
186static LinkageInfo
187getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args) {
188 LinkageInfo LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000189
John McCalldf25c432013-02-16 00:17:33 +0000190 for (unsigned i = 0, e = args.size(); i != e; ++i) {
191 const TemplateArgument &arg = args[i];
192 switch (arg.getKind()) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000193 case TemplateArgument::Null:
194 case TemplateArgument::Integral:
195 case TemplateArgument::Expression:
John McCalldf25c432013-02-16 00:17:33 +0000196 continue;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000197
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000198 case TemplateArgument::Type:
John McCalldf25c432013-02-16 00:17:33 +0000199 LV.merge(getLVForType(arg.getAsType()));
200 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000201
202 case TemplateArgument::Declaration:
John McCalldf25c432013-02-16 00:17:33 +0000203 if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
204 assert(!usesTypeVisibility(ND));
205 LV.merge(getLVForDecl(ND, LVForValue));
206 }
207 continue;
Eli Friedmanb826a002012-09-26 02:36:12 +0000208
209 case TemplateArgument::NullPtr:
John McCalldf25c432013-02-16 00:17:33 +0000210 LV.merge(getLVForType(arg.getNullPtrType()));
211 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000212
213 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000214 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000215 if (TemplateDecl *Template
John McCalldf25c432013-02-16 00:17:33 +0000216 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
217 LV.merge(getLVForDecl(Template, LVForValue));
218 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000219
220 case TemplateArgument::Pack:
John McCalldf25c432013-02-16 00:17:33 +0000221 LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray()));
222 continue;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000223 }
John McCalldf25c432013-02-16 00:17:33 +0000224 llvm_unreachable("bad template argument kind");
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000225 }
226
John McCall457a04e2010-10-22 21:05:15 +0000227 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000228}
229
Rafael Espindola2f869a32012-01-14 00:30:36 +0000230static LinkageInfo
John McCalldf25c432013-02-16 00:17:33 +0000231getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
232 return getLVForTemplateArgumentList(TArgs.asArray());
John McCall8823c652010-08-13 08:35:10 +0000233}
234
John McCalldf25c432013-02-16 00:17:33 +0000235/// Merge in template-related linkage and visibility for the given
236/// function template specialization.
237///
238/// We don't need a computation kind here because we can assume
239/// LVForValue.
240static void mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
241 const FunctionTemplateSpecializationInfo *specInfo) {
242 bool hasExplicitVisibility = fn->hasAttr<VisibilityAttr>();
243 FunctionTemplateDecl *temp = specInfo->getTemplate();
244
245 // Include visibility from the template parameters and arguments
246 // only if this is not an explicit instantiation or specialization
247 // with direct explicit visibility. (Implicit instantiations won't
248 // have a direct attribute.)
249 bool considerVisibility = !hasExplicitVisibility;
250
251 // Merge information from the template parameters.
252 LinkageInfo tempLV =
253 getLVForTemplateParameterList(temp->getTemplateParameters());
254 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
255
256 // Merge information from the template arguments.
257 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
258 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
259 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000260}
261
John McCalldf25c432013-02-16 00:17:33 +0000262/// Merge in template-related linkage and visibility for the given
263/// class template specialization.
264static void mergeTemplateLV(LinkageInfo &LV,
265 const ClassTemplateSpecializationDecl *spec,
266 LVComputationKind computation) {
267 // FIXME: type visibility
268 bool hasExplicitVisibility = spec->hasAttr<VisibilityAttr>();
269 ClassTemplateDecl *temp = spec->getSpecializedTemplate();
270
271 // Include visibility from the template parameters and arguments
272 // only if this is not an explicit instantiation or specialization
273 // with direct explicit visibility (and note that implicit
274 // instantiations won't have a direct attribute).
275 //
276 // Furthermore, we want to ignore template parameters and arguments
277 // for an explicit instantiation or specialization when computing
278 // the visibility of a member thereof with explicit visibility.
279 //
280 // This is a bit complex; let's unpack it.
281 //
282 // An explicit class specialization is an independent, top-level
283 // declaration. As such, if it or any of its members has an
284 // explicit visibility attribute, that must directly express the
285 // user's intent, and we should honor it. The same logic applies to
286 // an explicit instantiation of a member of such a thing.
287 //
288 // That we're doing this for a member with explicit visibility
289 // is encoded by the computation kind being OnlyTemplateArguments.
290 bool considerVisibility =
291 !(hasExplicitVisibility ||
292 (computation == LVOnlyTemplateArguments &&
293 spec->isExplicitInstantiationOrSpecialization()));
294
295 // Merge information from the template parameters, but ignore
296 // visibility if we're only considering template arguments.
297
298 LinkageInfo tempLV =
299 getLVForTemplateParameterList(temp->getTemplateParameters());
300 LV.mergeMaybeWithVisibility(tempLV,
301 considerVisibility && computation != LVOnlyTemplateArguments);
302
303 // Merge information from the template arguments. We ignore
304 // template-argument visibility if we've got an explicit
305 // instantiation with a visibility attribute.
306 const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
307 LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs);
308 LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
John McCallb8c604a2011-06-27 23:06:04 +0000309}
310
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000311static bool useInlineVisibilityHidden(const NamedDecl *D) {
312 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000313 const LangOptions &Opts = D->getASTContext().getLangOpts();
314 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000315 return false;
316
317 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
318 if (!FD)
319 return false;
320
321 TemplateSpecializationKind TSK = TSK_Undeclared;
322 if (FunctionTemplateSpecializationInfo *spec
323 = FD->getTemplateSpecializationInfo()) {
324 TSK = spec->getTemplateSpecializationKind();
325 } else if (MemberSpecializationInfo *MSI =
326 FD->getMemberSpecializationInfo()) {
327 TSK = MSI->getTemplateSpecializationKind();
328 }
329
330 const FunctionDecl *Def = 0;
331 // InlineVisibilityHidden only applies to definitions, and
332 // isInlined() only gives meaningful answers on definitions
333 // anyway.
334 return TSK != TSK_ExplicitInstantiationDeclaration &&
335 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000336 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000337}
338
Benjamin Kramer3e350262013-02-15 12:30:38 +0000339template <typename T> static bool isInExternCContext(T *D) {
Rafael Espindolaf4187652013-02-14 01:18:37 +0000340 const T *First = D->getFirstDeclaration();
341 return First->getDeclContext()->isExternCContext();
342}
343
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000344static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCalldf25c432013-02-16 00:17:33 +0000345 LVComputationKind computation) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000346 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000347 "Not a name having namespace scope");
348 ASTContext &Context = D->getASTContext();
349
350 // C++ [basic.link]p3:
351 // A name having namespace scope (3.3.6) has internal linkage if it
352 // is the name of
353 // - an object, reference, function or function template that is
354 // explicitly declared static; or,
355 // (This bullet corresponds to C99 6.2.2p3.)
356 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
357 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000358 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000359 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000360
Richard Smithdc0ef452012-10-19 06:37:48 +0000361 // - a non-volatile object or reference that is explicitly declared const
362 // or constexpr and neither explicitly declared extern nor previously
363 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000364 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000365 Var->getType().isConstQualified() &&
366 !Var->getType().isVolatileQualified() &&
John McCall8e7d6562010-08-26 03:08:43 +0000367 Var->getStorageClass() != SC_Extern &&
368 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000369 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000370 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000371 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000372 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000373 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000374 FoundExtern = true;
375
376 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000377 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000378 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000379 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000380 const VarDecl *PrevVar = Var->getPreviousDecl();
381 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000382 if (PrevVar->getStorageClass() == SC_PrivateExtern)
383 break;
Eli Friedmana7137bc2012-10-26 23:05:34 +0000384 if (PrevVar)
385 return PrevVar->getLinkageAndVisibility();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000386 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000387 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000388 // C++ [temp]p4:
389 // A non-member function template can have internal linkage; any
390 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000391 const FunctionDecl *Function = 0;
392 if (const FunctionTemplateDecl *FunTmpl
393 = dyn_cast<FunctionTemplateDecl>(D))
394 Function = FunTmpl->getTemplatedDecl();
395 else
396 Function = cast<FunctionDecl>(D);
397
398 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000399 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000400 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000401 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
402 // - a data member of an anonymous union.
403 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000404 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000405 }
406
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000407 if (D->isInAnonymousNamespace()) {
408 const VarDecl *Var = dyn_cast<VarDecl>(D);
409 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindolaf4187652013-02-14 01:18:37 +0000410 if ((!Var || !isInExternCContext(Var)) &&
411 (!Func || !isInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000412 return LinkageInfo::uniqueExternal();
413 }
John McCallb7139c42010-10-28 04:18:25 +0000414
John McCall457a04e2010-10-22 21:05:15 +0000415 // Set up the defaults.
416
417 // C99 6.2.2p5:
418 // If the declaration of an identifier for an object has file
419 // scope and no storage-class specifier, its linkage is
420 // external.
John McCallc273f242010-10-30 11:50:40 +0000421 LinkageInfo LV;
422
John McCalldf25c432013-02-16 00:17:33 +0000423 if (computation != LVOnlyTemplateArguments) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000424 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000425 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000426 } else {
427 // If we're declared in a namespace with a visibility attribute,
John McCalldf25c432013-02-16 00:17:33 +0000428 // use that namespace's visibility, and it still counts as explicit.
Rafael Espindola78158af2012-04-16 18:46:26 +0000429 for (const DeclContext *DC = D->getDeclContext();
430 !isa<TranslationUnitDecl>(DC);
431 DC = DC->getParent()) {
432 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
433 if (!ND) continue;
434 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000435 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000436 break;
437 }
438 }
439 }
Rafael Espindola78158af2012-04-16 18:46:26 +0000440
John McCalldf25c432013-02-16 00:17:33 +0000441 // Add in global settings if the above didn't give us direct visibility.
442 if (!LV.visibilityExplicit()) {
443 // FIXME: type visibility
444 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode(),
445 /*explicit*/ false);
446
447
448 // If we're paying attention to global visibility, apply
449 // -finline-visibility-hidden if this is an inline method.
450 if (useInlineVisibilityHidden(D))
451 LV.mergeVisibility(HiddenVisibility, true);
452 }
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000453 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000454
Douglas Gregorf73b2822009-11-25 22:24:25 +0000455 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000456
Douglas Gregorf73b2822009-11-25 22:24:25 +0000457 // A name having namespace scope has external linkage if it is the
458 // name of
459 //
460 // - an object or reference, unless it has internal linkage; or
461 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000462 // GCC applies the following optimization to variables and static
463 // data members, but not to functions:
464 //
John McCall457a04e2010-10-22 21:05:15 +0000465 // Modify the variable's LV by the LV of its type unless this is
466 // C or extern "C". This follows from [basic.link]p9:
467 // A type without linkage shall not be used as the type of a
468 // variable or function with external linkage unless
469 // - the entity has C language linkage, or
470 // - the entity is declared within an unnamed namespace, or
471 // - the entity is not used or is defined in the same
472 // translation unit.
473 // and [basic.link]p10:
474 // ...the types specified by all declarations referring to a
475 // given variable or function shall be identical...
476 // C does not have an equivalent rule.
477 //
John McCall5fe84122010-10-26 04:59:26 +0000478 // Ignore this if we've got an explicit attribute; the user
479 // probably knows what they're doing.
480 //
John McCall457a04e2010-10-22 21:05:15 +0000481 // Note that we don't want to make the variable non-external
482 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000483 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000484 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000485 LinkageInfo TypeLV = getLVForType(Var->getType());
486 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000487 return LinkageInfo::uniqueExternal();
John McCalldf25c432013-02-16 00:17:33 +0000488 if (!LV.visibilityExplicit())
489 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000490 }
491
John McCall23032652010-11-02 18:38:13 +0000492 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000493 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000494
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000495 // Note that Sema::MergeVarDecl already takes care of implementing
496 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
497 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000498
Douglas Gregorf73b2822009-11-25 22:24:25 +0000499 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000500 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000501 // In theory, we can modify the function's LV by the LV of its
502 // type unless it has C linkage (see comment above about variables
503 // for justification). In practice, GCC doesn't do this, so it's
504 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000505
John McCall23032652010-11-02 18:38:13 +0000506 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000507 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000508
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000509 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
510 // merging storage classes and visibility attributes, so we don't have to
511 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000512
John McCallf768aa72011-02-10 06:50:24 +0000513 // In C++, then if the type of the function uses a type with
514 // unique-external linkage, it's not legally usable from outside
515 // this translation unit. However, we should use the C linkage
516 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000517 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000518 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000519 Function->getType()->getLinkage() == UniqueExternalLinkage)
520 return LinkageInfo::uniqueExternal();
521
John McCallb8c604a2011-06-27 23:06:04 +0000522 // Consider LV from the template and the template arguments unless
523 // this is an explicit specialization with a visibility attribute.
524 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000525 = Function->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000526 mergeTemplateLV(LV, Function, specInfo);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000527 }
528
Douglas Gregorf73b2822009-11-25 22:24:25 +0000529 // - a named class (Clause 9), or an unnamed class defined in a
530 // typedef declaration in which the class has the typedef name
531 // for linkage purposes (7.1.3); or
532 // - a named enumeration (7.2), or an unnamed enumeration
533 // defined in a typedef declaration in which the enumeration
534 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000535 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
536 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000537 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000538 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000539
John McCall457a04e2010-10-22 21:05:15 +0000540 // If this is a class template specialization, consider the
541 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000542 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000543 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCalldf25c432013-02-16 00:17:33 +0000544 mergeTemplateLV(LV, spec, computation);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000545 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000546
547 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000548 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000549 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
John McCalldf25c432013-02-16 00:17:33 +0000550 computation);
John McCallc273f242010-10-30 11:50:40 +0000551 if (!isExternalLinkage(EnumLV.linkage()))
552 return LinkageInfo::none();
553 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000554
555 // - a template, unless it is a function template that has
556 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000557 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
John McCalldf25c432013-02-16 00:17:33 +0000558 bool considerVisibility = (computation != LVOnlyTemplateArguments);
559 LinkageInfo tempLV =
560 getLVForTemplateParameterList(temp->getTemplateParameters());
561 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
562
Douglas Gregorf73b2822009-11-25 22:24:25 +0000563 // - a namespace (7.3), unless it is declared within an unnamed
564 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000565 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
566 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000567
John McCall457a04e2010-10-22 21:05:15 +0000568 // By extension, we assign external linkage to Objective-C
569 // interfaces.
570 } else if (isa<ObjCInterfaceDecl>(D)) {
571 // fallout
572
573 // Everything not covered here has no linkage.
574 } else {
John McCallc273f242010-10-30 11:50:40 +0000575 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000576 }
577
578 // If we ended up with non-external linkage, visibility should
579 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000580 if (LV.linkage() != ExternalLinkage)
581 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000582
John McCall457a04e2010-10-22 21:05:15 +0000583 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000584}
585
John McCalldf25c432013-02-16 00:17:33 +0000586static LinkageInfo getLVForClassMember(const NamedDecl *D,
587 LVComputationKind computation) {
John McCall457a04e2010-10-22 21:05:15 +0000588 // Only certain class members have linkage. Note that fields don't
589 // really have linkage, but it's convenient to say they do for the
590 // purposes of calculating linkage of pointer-to-data-member
591 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000592 if (!(isa<CXXMethodDecl>(D) ||
593 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000594 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000595 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000596 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000597
John McCall07072662010-11-02 01:45:15 +0000598 LinkageInfo LV;
599
John McCall07072662010-11-02 01:45:15 +0000600 // If we have an explicit visibility attribute, merge that in.
John McCalldf25c432013-02-16 00:17:33 +0000601 if (computation != LVOnlyTemplateArguments) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000602 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000603 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000604 // If we're paying attention to global visibility, apply
605 // -finline-visibility-hidden if this is an inline method.
606 //
607 // Note that we do this before merging information about
608 // the class visibility.
609 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
610 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000611 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000612
613 // If this class member has an explicit visibility attribute, the only
614 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000615 // only look for them when processing the class.
John McCalldf25c432013-02-16 00:17:33 +0000616 LVComputationKind classComputation =
617 (LV.visibilityExplicit() ? LVOnlyTemplateArguments : computation);
Rafael Espindola505a7c82012-04-16 18:25:01 +0000618
Rafael Espindola53cf2192012-04-19 05:50:08 +0000619 // If this member has an visibility attribute, ClassF will exclude
620 // attributes on the class or command line options, keeping only information
621 // about the template instantiation. If the member has no visibility
622 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
623 // produces the desired result.
John McCalldf25c432013-02-16 00:17:33 +0000624 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
625 classComputation));
John McCall07072662010-11-02 01:45:15 +0000626 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000627 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000628
629 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000630 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000631 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000632
John McCall8823c652010-08-13 08:35:10 +0000633 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000634 // If the type of the function uses a type with unique-external
635 // linkage, it's not legally usable from outside this translation unit.
636 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
637 return LinkageInfo::uniqueExternal();
638
John McCall457a04e2010-10-22 21:05:15 +0000639 // If this is a method template specialization, use the linkage for
640 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000641 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000642 = MD->getTemplateSpecializationInfo()) {
John McCalldf25c432013-02-16 00:17:33 +0000643 mergeTemplateLV(LV, MD, spec);
John McCalle6e622e2010-11-01 01:29:57 +0000644 }
John McCall457a04e2010-10-22 21:05:15 +0000645
John McCall37bb6c92010-10-29 22:22:43 +0000646 // Note that in contrast to basically every other situation, we
647 // *do* apply -fvisibility to method declarations.
648
649 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000650 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000651 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
John McCalldf25c432013-02-16 00:17:33 +0000652 mergeTemplateLV(LV, spec, computation);
John McCall37bb6c92010-10-29 22:22:43 +0000653 }
654
John McCall37bb6c92010-10-29 22:22:43 +0000655 // Static data members.
656 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000657 // Modify the variable's linkage by its type, but ignore the
658 // type's visibility unless it's a definition.
John McCalldf25c432013-02-16 00:17:33 +0000659 LinkageInfo typeLV = getLVForType(VD->getType());
660 if (typeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000661 LV.mergeLinkage(UniqueExternalLinkage);
John McCalldf25c432013-02-16 00:17:33 +0000662 if (!LV.visibilityExplicit())
663 LV.mergeVisibility(typeLV);
664
665 // Template members.
666 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
667 bool considerVisibility =
668 (!LV.visibilityExplicit() && computation != LVOnlyTemplateArguments);
669 LinkageInfo tempLV =
670 getLVForTemplateParameterList(temp->getTemplateParameters());
671 LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
John McCall37bb6c92010-10-29 22:22:43 +0000672 }
673
John McCall457a04e2010-10-22 21:05:15 +0000674 return LV;
John McCall8823c652010-08-13 08:35:10 +0000675}
676
John McCalld396b972011-02-08 19:01:05 +0000677static void clearLinkageForClass(const CXXRecordDecl *record) {
678 for (CXXRecordDecl::decl_iterator
679 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
680 Decl *child = *i;
681 if (isa<NamedDecl>(child))
Rafael Espindola19de5612013-01-12 06:42:30 +0000682 cast<NamedDecl>(child)->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000683 }
684}
685
David Blaikie68e081d2011-12-20 02:48:34 +0000686void NamedDecl::anchor() { }
687
Rafael Espindola19de5612013-01-12 06:42:30 +0000688void NamedDecl::ClearLinkageCache() {
John McCalld396b972011-02-08 19:01:05 +0000689 // Note that we can't skip clearing the linkage of children just
690 // because the parent doesn't have cached linkage: we don't cache
691 // when computing linkage for parent contexts.
692
Rafael Espindola19de5612013-01-12 06:42:30 +0000693 HasCachedLinkage = 0;
John McCalld396b972011-02-08 19:01:05 +0000694
695 // If we're changing the linkage of a class, we need to reset the
696 // linkage of child declarations, too.
697 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
698 clearLinkageForClass(record);
699
Dmitri Gribenkofb04e1b2013-02-03 16:10:26 +0000700 if (ClassTemplateDecl *temp = dyn_cast<ClassTemplateDecl>(this)) {
John McCalld396b972011-02-08 19:01:05 +0000701 // Clear linkage for the template pattern.
702 CXXRecordDecl *record = temp->getTemplatedDecl();
Rafael Espindola19de5612013-01-12 06:42:30 +0000703 record->HasCachedLinkage = 0;
John McCalld396b972011-02-08 19:01:05 +0000704 clearLinkageForClass(record);
705
John McCall83779672011-02-19 02:53:41 +0000706 // We need to clear linkage for specializations, too.
707 for (ClassTemplateDecl::spec_iterator
708 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola19de5612013-01-12 06:42:30 +0000709 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000710 }
John McCall83779672011-02-19 02:53:41 +0000711
712 // Clear cached linkage for function template decls, too.
Dmitri Gribenkofb04e1b2013-02-03 16:10:26 +0000713 if (FunctionTemplateDecl *temp = dyn_cast<FunctionTemplateDecl>(this)) {
Rafael Espindola19de5612013-01-12 06:42:30 +0000714 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000715 for (FunctionTemplateDecl::spec_iterator
716 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola19de5612013-01-12 06:42:30 +0000717 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000718 }
John McCall83779672011-02-19 02:53:41 +0000719
John McCalld396b972011-02-08 19:01:05 +0000720}
721
Douglas Gregorbf62d642010-12-06 18:36:25 +0000722Linkage NamedDecl::getLinkage() const {
Richard Smith88581592013-02-12 05:48:23 +0000723 if (HasCachedLinkage)
Rafael Espindola19de5612013-01-12 06:42:30 +0000724 return Linkage(CachedLinkage);
Rafael Espindola19de5612013-01-12 06:42:30 +0000725
John McCalldf25c432013-02-16 00:17:33 +0000726 // We don't care about visibility here, so suppress all the
727 // unnecessary explicit-visibility checks by asking for a
728 // template-argument-only analysis.
729 CachedLinkage = getLVForDecl(this, LVOnlyTemplateArguments).linkage();
Rafael Espindola19de5612013-01-12 06:42:30 +0000730 HasCachedLinkage = 1;
731
732#ifndef NDEBUG
733 verifyLinkage();
734#endif
735
736 return Linkage(CachedLinkage);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000737}
738
John McCallc273f242010-10-30 11:50:40 +0000739LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCalldf25c432013-02-16 00:17:33 +0000740 LVComputationKind computation =
741 (usesTypeVisibility(this) ? LVForType : LVForValue);
742 LinkageInfo LI = getLVForDecl(this, computation);
Rafael Espindola19de5612013-01-12 06:42:30 +0000743 if (HasCachedLinkage) {
744 assert(Linkage(CachedLinkage) == LI.linkage());
745 return LI;
Rafael Espindola54606d52012-12-25 07:31:49 +0000746 }
Rafael Espindola19de5612013-01-12 06:42:30 +0000747 HasCachedLinkage = 1;
748 CachedLinkage = LI.linkage();
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000749
750#ifndef NDEBUG
Rafael Espindola19de5612013-01-12 06:42:30 +0000751 verifyLinkage();
752#endif
753
754 return LI;
755}
756
757void NamedDecl::verifyLinkage() const {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000758 // In C (because of gnu inline) and in c++ with microsoft extensions an
759 // static can follow an extern, so we can have two decls with different
760 // linkages.
761 const LangOptions &Opts = getASTContext().getLangOpts();
762 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
Rafael Espindola19de5612013-01-12 06:42:30 +0000763 return;
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000764
765 // We have just computed the linkage for this decl. By induction we know
766 // that all other computed linkages match, check that the one we just computed
767 // also does.
768 NamedDecl *D = NULL;
769 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
770 NamedDecl *T = cast<NamedDecl>(*I);
771 if (T == this)
772 continue;
Rafael Espindola19de5612013-01-12 06:42:30 +0000773 if (T->HasCachedLinkage != 0) {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000774 D = T;
775 break;
776 }
777 }
778 assert(!D || D->CachedLinkage == CachedLinkage);
John McCall033caa52010-10-29 00:29:13 +0000779}
Ted Kremenek926d8602010-04-20 23:15:35 +0000780
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000781llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
782 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000783 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000784 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola96e68242012-05-16 02:10:38 +0000785 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000786
Rafael Espindola96e68242012-05-16 02:10:38 +0000787 if (Var->isStaticDataMember()) {
788 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
789 if (InstantiatedFrom)
790 return getVisibilityOf(InstantiatedFrom);
791 }
792
793 return llvm::Optional<Visibility>();
794 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000795 // Use the most recent declaration of a function, and also handle
796 // function template specializations.
797 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000798 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000799 return V;
800
801 // If the function is a specialization of a template with an
802 // explicit visibility attribute, use that.
803 if (FunctionTemplateSpecializationInfo *templateInfo
804 = fn->getTemplateSpecializationInfo())
805 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
806
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000807 // If the function is a member of a specialization of a class template
808 // and the corresponding decl has explicit visibility, use that.
809 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
810 if (InstantiatedFrom)
811 return getVisibilityOf(InstantiatedFrom);
812
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000813 return llvm::Optional<Visibility>();
814 }
815
816 // Otherwise, just check the declaration itself first.
817 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
818 return V;
819
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000820 // The visibility of a template is stored in the templated decl.
821 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
822 return getVisibilityOf(TD->getTemplatedDecl());
823
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000824 // If there wasn't explicit visibility there, and this is a
825 // specialization of a class template, check for visibility
826 // on the pattern.
827 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolaeca5cd22012-07-13 01:19:08 +0000828 = dyn_cast<ClassTemplateSpecializationDecl>(this))
829 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000830
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000831 // If this is a member class of a specialization of a class template
832 // and the corresponding decl has explicit visibility, use that.
833 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
834 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
835 if (InstantiatedFrom)
836 return getVisibilityOf(InstantiatedFrom);
837 }
838
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000839 return llvm::Optional<Visibility>();
840}
841
John McCalldf25c432013-02-16 00:17:33 +0000842static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
843 LVComputationKind computation) {
844 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
845 if (Function->isInAnonymousNamespace() &&
846 !Function->getDeclContext()->isExternCContext())
847 return LinkageInfo::uniqueExternal();
848
849 // This is a "void f();" which got merged with a file static.
850 if (Function->getStorageClass() == SC_Static)
851 return LinkageInfo::internal();
852
853 LinkageInfo LV;
854 if (computation != LVOnlyTemplateArguments) {
855 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
856 LV.mergeVisibility(*Vis, true);
857 }
858
859 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
860 // merging storage classes and visibility attributes, so we don't have to
861 // look at previous decls in here.
862
863 return LV;
864 }
865
866 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
867 if (Var->getStorageClassAsWritten() == SC_Extern ||
868 Var->getStorageClassAsWritten() == SC_PrivateExtern) {
869 if (Var->isInAnonymousNamespace() &&
870 !Var->getDeclContext()->isExternCContext())
871 return LinkageInfo::uniqueExternal();
872
873 // This is an "extern int foo;" which got merged with a file static.
874 if (Var->getStorageClass() == SC_Static)
875 return LinkageInfo::internal();
876
877 LinkageInfo LV;
878 if (Var->getStorageClass() == SC_PrivateExtern)
879 LV.mergeVisibility(HiddenVisibility, true);
880 else if (computation != LVOnlyTemplateArguments) {
881 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
882 LV.mergeVisibility(*Vis, true);
883 }
884
885 // Note that Sema::MergeVarDecl already takes care of implementing
886 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
887 // have to do it here.
888 return LV;
889 }
890 }
891
892 return LinkageInfo::none();
893}
894
895static LinkageInfo getLVForDecl(const NamedDecl *D,
896 LVComputationKind computation) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000897 // Objective-C: treat all Objective-C declarations as having external
898 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000899 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000900 default:
901 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000902 case Decl::ParmVar:
903 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000904 case Decl::TemplateTemplateParm: // count these as external
905 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000906 case Decl::ObjCAtDefsField:
907 case Decl::ObjCCategory:
908 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000909 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000910 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000911 case Decl::ObjCMethod:
912 case Decl::ObjCProperty:
913 case Decl::ObjCPropertyImpl:
914 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000915 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000916
917 case Decl::CXXRecord: {
918 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
919 if (Record->isLambda()) {
920 if (!Record->getLambdaManglingNumber()) {
921 // This lambda has no mangling number, so it's internal.
922 return LinkageInfo::internal();
923 }
924
925 // This lambda has its linkage/visibility determined by its owner.
926 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
927 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
928 if (isa<ParmVarDecl>(ContextDecl))
929 DC = ContextDecl->getDeclContext()->getRedeclContext();
930 else
John McCalldf25c432013-02-16 00:17:33 +0000931 return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000932 }
933
934 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
John McCalldf25c432013-02-16 00:17:33 +0000935 return getLVForDecl(ND, computation);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000936
937 return LinkageInfo::external();
938 }
939
940 break;
941 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000942 }
943
Douglas Gregorf73b2822009-11-25 22:24:25 +0000944 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000945 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCalldf25c432013-02-16 00:17:33 +0000946 return getLVForNamespaceScopeDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000947
948 // C++ [basic.link]p5:
949 // In addition, a member function, static data member, a named
950 // class or enumeration of class scope, or an unnamed class or
951 // enumeration defined in a class-scope typedef declaration such
952 // that the class or enumeration has the typedef name for linkage
953 // purposes (7.1.3), has external linkage if the name of the class
954 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000955 if (D->getDeclContext()->isRecord())
John McCalldf25c432013-02-16 00:17:33 +0000956 return getLVForClassMember(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000957
958 // C++ [basic.link]p6:
959 // The name of a function declared in block scope and the name of
960 // an object declared by a block scope extern declaration have
961 // linkage. If there is a visible declaration of an entity with
962 // linkage having the same name and type, ignoring entities
963 // declared outside the innermost enclosing namespace scope, the
964 // block scope declaration declares that same entity and receives
965 // the linkage of the previous declaration. If there is more than
966 // one such matching entity, the program is ill-formed. Otherwise,
967 // if no matching entity is found, the block scope entity receives
968 // external linkage.
John McCalldf25c432013-02-16 00:17:33 +0000969 if (D->getDeclContext()->isFunctionOrMethod())
970 return getLVForLocalDecl(D, computation);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000971
972 // C++ [basic.link]p6:
973 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000974 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000975}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000976
Douglas Gregor2ada0482009-02-04 17:27:36 +0000977std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000978 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000979}
980
981std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000982 const DeclContext *Ctx = getDeclContext();
983
984 if (Ctx->isFunctionOrMethod())
985 return getNameAsString();
986
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000987 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000988 ContextsTy Contexts;
989
990 // Collect contexts.
991 while (Ctx && isa<NamedDecl>(Ctx)) {
992 Contexts.push_back(Ctx);
993 Ctx = Ctx->getParent();
994 };
995
996 std::string QualName;
997 llvm::raw_string_ostream OS(QualName);
998
999 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1000 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001001 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001002 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +00001003 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1004 std::string TemplateArgsStr
1005 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001006 TemplateArgs.data(),
1007 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +00001008 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001009 OS << Spec->getName() << TemplateArgsStr;
1010 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +00001011 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001012 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +00001013 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001014 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001015 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1016 if (!RD->getIdentifier())
1017 OS << "<anonymous " << RD->getKindName() << '>';
1018 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001019 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001020 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +00001021 const FunctionProtoType *FT = 0;
1022 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001023 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +00001024
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001025 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +00001026 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +00001027 unsigned NumParams = FD->getNumParams();
1028 for (unsigned i = 0; i < NumParams; ++i) {
1029 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001030 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +00001031 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +00001032 }
1033
1034 if (FT->isVariadic()) {
1035 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001036 OS << ", ";
1037 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +00001038 }
1039 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001040 OS << ')';
1041 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001042 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001043 }
1044 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001045 }
1046
John McCalla2a3f7d2010-03-16 21:48:18 +00001047 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001048 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +00001049 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001050 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +00001051
Benjamin Kramerd76b6982010-04-28 14:33:51 +00001052 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +00001053}
1054
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001055bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001056 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1057
Douglas Gregor889ceb72009-02-03 19:21:40 +00001058 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1059 // We want to keep it, unless it nominates same namespace.
1060 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +00001061 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1062 ->getOriginalNamespace() ==
1063 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1064 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +00001065 }
Mike Stump11289f42009-09-09 15:08:12 +00001066
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001067 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1068 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +00001069 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001070
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001071 // For function templates, the underlying function declarations are linked.
1072 if (const FunctionTemplateDecl *FunctionTemplate
1073 = dyn_cast<FunctionTemplateDecl>(this))
1074 if (const FunctionTemplateDecl *OldFunctionTemplate
1075 = dyn_cast<FunctionTemplateDecl>(OldD))
1076 return FunctionTemplate->getTemplatedDecl()
1077 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001078
Steve Naroffc4173fa2009-02-22 19:35:57 +00001079 // For method declarations, we keep track of redeclarations.
1080 if (isa<ObjCMethodDecl>(this))
1081 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001082
John McCall9f3059a2009-10-09 21:13:30 +00001083 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1084 return true;
1085
John McCall3f746822009-11-17 05:59:44 +00001086 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1087 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1088 cast<UsingShadowDecl>(OldD)->getTargetDecl();
1089
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001090 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1091 ASTContext &Context = getASTContext();
1092 return Context.getCanonicalNestedNameSpecifier(
1093 cast<UsingDecl>(this)->getQualifier()) ==
1094 Context.getCanonicalNestedNameSpecifier(
1095 cast<UsingDecl>(OldD)->getQualifier());
1096 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +00001097
Douglas Gregorb59643b2012-01-03 23:26:26 +00001098 // A typedef of an Objective-C class type can replace an Objective-C class
1099 // declaration or definition, and vice versa.
1100 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1101 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1102 return true;
1103
Douglas Gregor8b9ccca2008-12-23 21:05:05 +00001104 // For non-function declarations, if the declarations are of the
1105 // same kind then this must be a redeclaration, or semantic analysis
1106 // would not have given us the new declaration.
1107 return this->getKind() == OldD->getKind();
1108}
1109
Douglas Gregoreddf4332009-02-24 20:03:32 +00001110bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +00001111 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +00001112}
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001113
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +00001114NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +00001115 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001116 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1117 ND = UD->getTargetDecl();
1118
1119 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1120 return AD->getClassInterface();
1121
1122 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001123}
1124
John McCalla8ae2222010-04-06 21:38:20 +00001125bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001126 if (!isCXXClassMember())
1127 return false;
1128
John McCalla8ae2222010-04-06 21:38:20 +00001129 const NamedDecl *D = this;
1130 if (isa<UsingShadowDecl>(D))
1131 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1132
Francois Pichet783dd6e2010-11-21 06:08:52 +00001133 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001134 return true;
1135 if (isa<CXXMethodDecl>(D))
1136 return cast<CXXMethodDecl>(D)->isInstance();
1137 if (isa<FunctionTemplateDecl>(D))
1138 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1139 ->getTemplatedDecl())->isInstance();
1140 return false;
1141}
1142
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001143//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001144// DeclaratorDecl Implementation
1145//===----------------------------------------------------------------------===//
1146
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001147template <typename DeclT>
1148static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1149 if (decl->getNumTemplateParameterLists() > 0)
1150 return decl->getTemplateParameterList(0)->getTemplateLoc();
1151 else
1152 return decl->getInnerLocStart();
1153}
1154
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001155SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001156 TypeSourceInfo *TSI = getTypeSourceInfo();
1157 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001158 return SourceLocation();
1159}
1160
Douglas Gregor14454802011-02-25 02:25:35 +00001161void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1162 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001163 // Make sure the extended decl info is allocated.
1164 if (!hasExtInfo()) {
1165 // Save (non-extended) type source info pointer.
1166 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1167 // Allocate external info struct.
1168 DeclInfo = new (getASTContext()) ExtInfo;
1169 // Restore savedTInfo into (extended) decl info.
1170 getExtInfo()->TInfo = savedTInfo;
1171 }
1172 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001173 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001174 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001175 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001176 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001177 if (getExtInfo()->NumTemplParamLists == 0) {
1178 // Save type source info pointer.
1179 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1180 // Deallocate the extended decl info.
1181 getASTContext().Deallocate(getExtInfo());
1182 // Restore savedTInfo into (non-extended) decl info.
1183 DeclInfo = savedTInfo;
1184 }
1185 else
1186 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001187 }
1188 }
1189}
1190
Abramo Bagnara60804e12011-03-18 15:16:37 +00001191void
1192DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1193 unsigned NumTPLists,
1194 TemplateParameterList **TPLists) {
1195 assert(NumTPLists > 0);
1196 // Make sure the extended decl info is allocated.
1197 if (!hasExtInfo()) {
1198 // Save (non-extended) type source info pointer.
1199 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1200 // Allocate external info struct.
1201 DeclInfo = new (getASTContext()) ExtInfo;
1202 // Restore savedTInfo into (extended) decl info.
1203 getExtInfo()->TInfo = savedTInfo;
1204 }
1205 // Set the template parameter lists info.
1206 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1207}
1208
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001209SourceLocation DeclaratorDecl::getOuterLocStart() const {
1210 return getTemplateOrInnerLocStart(this);
1211}
1212
Abramo Bagnaraea947882011-03-08 16:41:52 +00001213namespace {
1214
1215// Helper function: returns true if QT is or contains a type
1216// having a postfix component.
1217bool typeIsPostfix(clang::QualType QT) {
1218 while (true) {
1219 const Type* T = QT.getTypePtr();
1220 switch (T->getTypeClass()) {
1221 default:
1222 return false;
1223 case Type::Pointer:
1224 QT = cast<PointerType>(T)->getPointeeType();
1225 break;
1226 case Type::BlockPointer:
1227 QT = cast<BlockPointerType>(T)->getPointeeType();
1228 break;
1229 case Type::MemberPointer:
1230 QT = cast<MemberPointerType>(T)->getPointeeType();
1231 break;
1232 case Type::LValueReference:
1233 case Type::RValueReference:
1234 QT = cast<ReferenceType>(T)->getPointeeType();
1235 break;
1236 case Type::PackExpansion:
1237 QT = cast<PackExpansionType>(T)->getPattern();
1238 break;
1239 case Type::Paren:
1240 case Type::ConstantArray:
1241 case Type::DependentSizedArray:
1242 case Type::IncompleteArray:
1243 case Type::VariableArray:
1244 case Type::FunctionProto:
1245 case Type::FunctionNoProto:
1246 return true;
1247 }
1248 }
1249}
1250
1251} // namespace
1252
1253SourceRange DeclaratorDecl::getSourceRange() const {
1254 SourceLocation RangeEnd = getLocation();
1255 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1256 if (typeIsPostfix(TInfo->getType()))
1257 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1258 }
1259 return SourceRange(getOuterLocStart(), RangeEnd);
1260}
1261
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001262void
Douglas Gregor20527e22010-06-15 17:44:38 +00001263QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1264 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001265 TemplateParameterList **TPLists) {
1266 assert((NumTPLists == 0 || TPLists != 0) &&
1267 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001268
1269 // Free previous template parameters (if any).
1270 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001271 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001272 TemplParamLists = 0;
1273 NumTemplParamLists = 0;
1274 }
1275 // Set info on matched template parameter lists (if any).
1276 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001277 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001278 NumTemplParamLists = NumTPLists;
1279 for (unsigned i = NumTPLists; i-- > 0; )
1280 TemplParamLists[i] = TPLists[i];
1281 }
1282}
1283
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001284//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001285// VarDecl Implementation
1286//===----------------------------------------------------------------------===//
1287
Sebastian Redl833ef452010-01-26 22:01:41 +00001288const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1289 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001290 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001291 case SC_Auto: return "auto";
1292 case SC_Extern: return "extern";
1293 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1294 case SC_PrivateExtern: return "__private_extern__";
1295 case SC_Register: return "register";
1296 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001297 }
1298
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001299 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001300}
1301
Abramo Bagnaradff19302011-03-08 08:55:46 +00001302VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1303 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001304 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001305 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001306 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001307}
1308
Douglas Gregor72172e92012-01-05 21:55:30 +00001309VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1310 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1311 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1312 QualType(), 0, SC_None, SC_None);
1313}
1314
Douglas Gregorbf62d642010-12-06 18:36:25 +00001315void VarDecl::setStorageClass(StorageClass SC) {
1316 assert(isLegalForVariable(SC));
1317 if (getStorageClass() != SC)
Rafael Espindola19de5612013-01-12 06:42:30 +00001318 ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001319
John McCallbeaa11c2011-05-01 02:13:58 +00001320 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001321}
1322
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001323SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001324 if (const Expr *Init = getInit()) {
1325 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001326 // If Init is implicit, ignore its source range and fallback on
1327 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1328 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001329 return SourceRange(getOuterLocStart(), InitEnd);
1330 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001331 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001332}
1333
Rafael Espindola88510672013-01-04 21:18:45 +00001334template<typename T>
Rafael Espindolaf4187652013-02-14 01:18:37 +00001335static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001336 // C++ [dcl.link]p1: All function types, function names with external linkage,
1337 // and variable names with external linkage have a language linkage.
1338 if (!isExternalLinkage(D.getLinkage()))
1339 return NoLanguageLinkage;
1340
1341 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001342 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001343 ASTContext &Context = D.getASTContext();
1344 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001345 return CLanguageLinkage;
1346
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001347 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1348 // language linkage of the names of class members and the function type of
1349 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001350 const DeclContext *DC = D.getDeclContext();
1351 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001352 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001353
1354 // If the first decl is in an extern "C" context, any other redeclaration
1355 // will have C language linkage. If the first one is not in an extern "C"
1356 // context, we would have reported an error for any other decl being in one.
Rafael Espindola88510672013-01-04 21:18:45 +00001357 const T *First = D.getFirstDeclaration();
Rafael Espindolaf4187652013-02-14 01:18:37 +00001358 if (First->getDeclContext()->isExternCContext())
1359 return CLanguageLinkage;
1360 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001361}
1362
Rafael Espindolaf4187652013-02-14 01:18:37 +00001363LanguageLinkage VarDecl::getLanguageLinkage() const {
1364 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001365}
1366
Sebastian Redl833ef452010-01-26 22:01:41 +00001367VarDecl *VarDecl::getCanonicalDecl() {
1368 return getFirstDeclaration();
1369}
1370
Daniel Dunbar9d355812012-03-09 01:51:51 +00001371VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1372 ASTContext &C) const
1373{
Sebastian Redl35351a92010-01-31 22:27:38 +00001374 // C++ [basic.def]p2:
1375 // A declaration is a definition unless [...] it contains the 'extern'
1376 // specifier or a linkage-specification and neither an initializer [...],
1377 // it declares a static data member in a class declaration [...].
1378 // C++ [temp.expl.spec]p15:
1379 // An explicit specialization of a static data member of a template is a
1380 // definition if the declaration includes an initializer; otherwise, it is
1381 // a declaration.
1382 if (isStaticDataMember()) {
1383 if (isOutOfLine() && (hasInit() ||
1384 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1385 return Definition;
1386 else
1387 return DeclarationOnly;
1388 }
1389 // C99 6.7p5:
1390 // A definition of an identifier is a declaration for that identifier that
1391 // [...] causes storage to be reserved for that object.
1392 // Note: that applies for all non-file-scope objects.
1393 // C99 6.9.2p1:
1394 // If the declaration of an identifier for an object has file scope and an
1395 // initializer, the declaration is an external definition for the identifier
1396 if (hasInit())
1397 return Definition;
1398 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1399 if (hasExternalStorage())
1400 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001401
John McCall8e7d6562010-08-26 03:08:43 +00001402 if (getStorageClassAsWritten() == SC_Extern ||
1403 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001404 for (const VarDecl *PrevVar = getPreviousDecl();
1405 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Rafael Espindola7581f322012-12-17 22:23:47 +00001406 if (PrevVar->getLinkage() == InternalLinkage)
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001407 return DeclarationOnly;
1408 }
1409 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001410 // C99 6.9.2p2:
1411 // A declaration of an object that has file scope without an initializer,
1412 // and without a storage class specifier or the scs 'static', constitutes
1413 // a tentative definition.
1414 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001415 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001416 return TentativeDefinition;
1417
1418 // What's left is (in C, block-scope) declarations without initializers or
1419 // external storage. These are definitions.
1420 return Definition;
1421}
1422
Sebastian Redl35351a92010-01-31 22:27:38 +00001423VarDecl *VarDecl::getActingDefinition() {
1424 DefinitionKind Kind = isThisDeclarationADefinition();
1425 if (Kind != TentativeDefinition)
1426 return 0;
1427
Chris Lattner48eb14d2010-06-14 18:31:46 +00001428 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001429 VarDecl *First = getFirstDeclaration();
1430 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1431 I != E; ++I) {
1432 Kind = (*I)->isThisDeclarationADefinition();
1433 if (Kind == Definition)
1434 return 0;
1435 else if (Kind == TentativeDefinition)
1436 LastTentative = *I;
1437 }
1438 return LastTentative;
1439}
1440
1441bool VarDecl::isTentativeDefinitionNow() const {
1442 DefinitionKind Kind = isThisDeclarationADefinition();
1443 if (Kind != TentativeDefinition)
1444 return false;
1445
1446 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1447 if ((*I)->isThisDeclarationADefinition() == Definition)
1448 return false;
1449 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001450 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001451}
1452
Daniel Dunbar9d355812012-03-09 01:51:51 +00001453VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001454 VarDecl *First = getFirstDeclaration();
1455 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1456 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001457 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001458 return *I;
1459 }
1460 return 0;
1461}
1462
Daniel Dunbar9d355812012-03-09 01:51:51 +00001463VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001464 DefinitionKind Kind = DeclarationOnly;
1465
1466 const VarDecl *First = getFirstDeclaration();
1467 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001468 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001469 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001470 if (Kind == Definition)
1471 break;
1472 }
John McCall37bb6c92010-10-29 22:22:43 +00001473
1474 return Kind;
1475}
1476
Sebastian Redl5ca79842010-02-01 20:16:42 +00001477const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001478 redecl_iterator I = redecls_begin(), E = redecls_end();
1479 while (I != E && !I->getInit())
1480 ++I;
1481
1482 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001483 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001484 return I->getInit();
1485 }
1486 return 0;
1487}
1488
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001489bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001490 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001491 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001492
1493 if (!isStaticDataMember())
1494 return false;
1495
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001496 // If this static data member was instantiated from a static data member of
1497 // a class template, check whether that static data member was defined
1498 // out-of-line.
1499 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1500 return VD->isOutOfLine();
1501
1502 return false;
1503}
1504
Douglas Gregor1d957a32009-10-27 18:42:08 +00001505VarDecl *VarDecl::getOutOfLineDefinition() {
1506 if (!isStaticDataMember())
1507 return 0;
1508
1509 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1510 RD != RDEnd; ++RD) {
1511 if (RD->getLexicalDeclContext()->isFileContext())
1512 return *RD;
1513 }
1514
1515 return 0;
1516}
1517
Douglas Gregord5058122010-02-11 01:19:42 +00001518void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001519 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1520 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001521 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001522 }
1523
1524 Init = I;
1525}
1526
Daniel Dunbar9d355812012-03-09 01:51:51 +00001527bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001528 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001529
Richard Smith35ecb362012-03-02 04:14:40 +00001530 if (!Lang.CPlusPlus)
1531 return false;
1532
1533 // In C++11, any variable of reference type can be used in a constant
1534 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001535 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001536 return true;
1537
1538 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001539 // not require the variable to be non-volatile, but we consider this to be a
1540 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001541 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001542 return false;
1543
1544 // In C++, const, non-volatile variables of integral or enumeration types
1545 // can be used in constant expressions.
1546 if (getType()->isIntegralOrEnumerationType())
1547 return true;
1548
Richard Smith35ecb362012-03-02 04:14:40 +00001549 // Additionally, in C++11, non-volatile constexpr variables can be used in
1550 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001551 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001552}
1553
Richard Smithd0b4dd62011-12-19 06:19:21 +00001554/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1555/// form, which contains extra information on the evaluated value of the
1556/// initializer.
1557EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1558 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1559 if (!Eval) {
1560 Stmt *S = Init.get<Stmt *>();
1561 Eval = new (getASTContext()) EvaluatedStmt;
1562 Eval->Value = S;
1563 Init = Eval;
1564 }
1565 return Eval;
1566}
1567
Richard Smithdafff942012-01-14 04:30:29 +00001568APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001569 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00001570 return evaluateValue(Notes);
1571}
1572
1573APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001574 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001575 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1576
1577 // We only produce notes indicating why an initializer is non-constant the
1578 // first time it is evaluated. FIXME: The notes won't always be emitted the
1579 // first time we try evaluation, so might not be produced at all.
1580 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001581 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001582
1583 const Expr *Init = cast<Expr>(Eval->Value);
1584 assert(!Init->isValueDependent());
1585
1586 if (Eval->IsEvaluating) {
1587 // FIXME: Produce a diagnostic for self-initialization.
1588 Eval->CheckedICE = true;
1589 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001590 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001591 }
1592
1593 Eval->IsEvaluating = true;
1594
1595 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1596 this, Notes);
1597
1598 // Ensure the result is an uninitialized APValue if evaluation fails.
1599 if (!Result)
1600 Eval->Evaluated = APValue();
1601
1602 Eval->IsEvaluating = false;
1603 Eval->WasEvaluated = true;
1604
1605 // In C++11, we have determined whether the initializer was a constant
1606 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001607 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001608 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001609 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001610 }
1611
Richard Smithdafff942012-01-14 04:30:29 +00001612 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001613}
1614
1615bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001616 // Initializers of weak variables are never ICEs.
1617 if (isWeak())
1618 return false;
1619
Richard Smithd0b4dd62011-12-19 06:19:21 +00001620 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1621 if (Eval->CheckedICE)
1622 // We have already checked whether this subexpression is an
1623 // integral constant expression.
1624 return Eval->IsICE;
1625
1626 const Expr *Init = cast<Expr>(Eval->Value);
1627 assert(!Init->isValueDependent());
1628
1629 // In C++11, evaluate the initializer to check whether it's a constant
1630 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001631 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001632 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001633 evaluateValue(Notes);
1634 return Eval->IsICE;
1635 }
1636
1637 // It's an ICE whether or not the definition we found is
1638 // out-of-line. See DR 721 and the discussion in Clang PR
1639 // 6206 for details.
1640
1641 if (Eval->CheckingICE)
1642 return false;
1643 Eval->CheckingICE = true;
1644
1645 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1646 Eval->CheckingICE = false;
1647 Eval->CheckedICE = true;
1648 return Eval->IsICE;
1649}
1650
Douglas Gregorfe314812011-06-21 17:03:29 +00001651bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001652 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001653
1654 const Expr *E = getInit();
1655 if (!E)
1656 return false;
1657
1658 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1659 E = Cleanups->getSubExpr();
1660
1661 return isa<MaterializeTemporaryExpr>(E);
1662}
1663
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001664VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001665 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001666 return cast<VarDecl>(MSI->getInstantiatedFrom());
1667
1668 return 0;
1669}
1670
Douglas Gregor3c74d412009-10-14 20:14:33 +00001671TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001672 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001673 return MSI->getTemplateSpecializationKind();
1674
1675 return TSK_Undeclared;
1676}
1677
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001678MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001679 return getASTContext().getInstantiatedFromStaticDataMember(this);
1680}
1681
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001682void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1683 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001684 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001685 assert(MSI && "Not an instantiated static data member?");
1686 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001687 if (TSK != TSK_ExplicitSpecialization &&
1688 PointOfInstantiation.isValid() &&
1689 MSI->getPointOfInstantiation().isInvalid())
1690 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001691}
1692
Sebastian Redl833ef452010-01-26 22:01:41 +00001693//===----------------------------------------------------------------------===//
1694// ParmVarDecl Implementation
1695//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001696
Sebastian Redl833ef452010-01-26 22:01:41 +00001697ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001698 SourceLocation StartLoc,
1699 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001700 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001701 StorageClass S, StorageClass SCAsWritten,
1702 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001703 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001704 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001705}
1706
Douglas Gregor72172e92012-01-05 21:55:30 +00001707ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1708 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1709 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1710 0, QualType(), 0, SC_None, SC_None, 0);
1711}
1712
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001713SourceRange ParmVarDecl::getSourceRange() const {
1714 if (!hasInheritedDefaultArg()) {
1715 SourceRange ArgRange = getDefaultArgRange();
1716 if (ArgRange.isValid())
1717 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1718 }
1719
1720 return DeclaratorDecl::getSourceRange();
1721}
1722
Sebastian Redl833ef452010-01-26 22:01:41 +00001723Expr *ParmVarDecl::getDefaultArg() {
1724 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1725 assert(!hasUninstantiatedDefaultArg() &&
1726 "Default argument is not yet instantiated!");
1727
1728 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001729 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001730 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001731
Sebastian Redl833ef452010-01-26 22:01:41 +00001732 return Arg;
1733}
1734
Sebastian Redl833ef452010-01-26 22:01:41 +00001735SourceRange ParmVarDecl::getDefaultArgRange() const {
1736 if (const Expr *E = getInit())
1737 return E->getSourceRange();
1738
1739 if (hasUninstantiatedDefaultArg())
1740 return getUninstantiatedDefaultArg()->getSourceRange();
1741
1742 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001743}
1744
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001745bool ParmVarDecl::isParameterPack() const {
1746 return isa<PackExpansionType>(getType());
1747}
1748
Ted Kremenek540017e2011-10-06 05:00:56 +00001749void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1750 getASTContext().setParameterIndex(this, parameterIndex);
1751 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1752}
1753
1754unsigned ParmVarDecl::getParameterIndexLarge() const {
1755 return getASTContext().getParameterIndex(this);
1756}
1757
Nuno Lopes394ec982008-12-17 23:39:55 +00001758//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001759// FunctionDecl Implementation
1760//===----------------------------------------------------------------------===//
1761
Douglas Gregorb11aad82011-02-19 18:51:44 +00001762void FunctionDecl::getNameForDiagnostic(std::string &S,
1763 const PrintingPolicy &Policy,
1764 bool Qualified) const {
1765 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1766 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1767 if (TemplateArgs)
1768 S += TemplateSpecializationType::PrintTemplateArgumentList(
1769 TemplateArgs->data(),
1770 TemplateArgs->size(),
1771 Policy);
1772
1773}
1774
Ted Kremenek186a0742010-04-29 16:49:01 +00001775bool FunctionDecl::isVariadic() const {
1776 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1777 return FT->isVariadic();
1778 return false;
1779}
1780
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001781bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1782 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001783 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001784 Definition = *I;
1785 return true;
1786 }
1787 }
1788
1789 return false;
1790}
1791
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001792bool FunctionDecl::hasTrivialBody() const
1793{
1794 Stmt *S = getBody();
1795 if (!S) {
1796 // Since we don't have a body for this function, we don't know if it's
1797 // trivial or not.
1798 return false;
1799 }
1800
1801 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1802 return true;
1803 return false;
1804}
1805
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001806bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1807 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001808 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001809 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1810 return true;
1811 }
1812 }
1813
1814 return false;
1815}
1816
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001817Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001818 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1819 if (I->Body) {
1820 Definition = *I;
1821 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001822 } else if (I->IsLateTemplateParsed) {
1823 Definition = *I;
1824 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001825 }
1826 }
1827
1828 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001829}
1830
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001831void FunctionDecl::setBody(Stmt *B) {
1832 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001833 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001834 EndRangeLoc = B->getLocEnd();
1835}
1836
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001837void FunctionDecl::setPure(bool P) {
1838 IsPure = P;
1839 if (P)
1840 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1841 Parent->markedVirtualFunctionPure();
1842}
1843
Douglas Gregor16618f22009-09-12 00:17:51 +00001844bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001845 const TranslationUnitDecl *tunit =
1846 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1847 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001848 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001849 getIdentifier() &&
1850 getIdentifier()->isStr("main");
1851}
1852
1853bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1854 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1855 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1856 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1857 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1858 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1859
1860 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1861 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1862
1863 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1864 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1865
1866 ASTContext &Context =
1867 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1868 ->getASTContext();
1869
1870 // The result type and first argument type are constant across all
1871 // these operators. The second argument must be exactly void*.
1872 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001873}
1874
Rafael Espindolaf4187652013-02-14 01:18:37 +00001875LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6239e052013-01-12 15:27:44 +00001876 // Users expect to be able to write
1877 // extern "C" void *__builtin_alloca (size_t);
1878 // so consider builtins as having C language linkage.
Rafael Espindolac48f7342013-01-12 15:27:43 +00001879 if (getBuiltinID())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001880 return CLanguageLinkage;
Rafael Espindolac48f7342013-01-12 15:27:43 +00001881
Rafael Espindolaf4187652013-02-14 01:18:37 +00001882 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001883}
1884
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001885bool FunctionDecl::isGlobal() const {
1886 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1887 return Method->isStatic();
1888
John McCall8e7d6562010-08-26 03:08:43 +00001889 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001890 return false;
1891
Mike Stump11289f42009-09-09 15:08:12 +00001892 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001893 DC->isNamespace();
1894 DC = DC->getParent()) {
1895 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1896 if (!Namespace->getDeclName())
1897 return false;
1898 break;
1899 }
1900 }
1901
1902 return true;
1903}
1904
Richard Smith10876ef2013-01-17 01:30:42 +00001905bool FunctionDecl::isNoReturn() const {
1906 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smithdebc59d2013-01-30 05:45:05 +00001907 hasAttr<C11NoReturnAttr>() ||
Richard Smith10876ef2013-01-17 01:30:42 +00001908 getType()->getAs<FunctionType>()->getNoReturnAttr();
1909}
1910
Sebastian Redl833ef452010-01-26 22:01:41 +00001911void
1912FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1913 redeclarable_base::setPreviousDeclaration(PrevDecl);
1914
1915 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1916 FunctionTemplateDecl *PrevFunTmpl
1917 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1918 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1919 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1920 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001921
Axel Naumannfbc7b982011-11-08 18:21:06 +00001922 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001923 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001924}
1925
1926const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1927 return getFirstDeclaration();
1928}
1929
1930FunctionDecl *FunctionDecl::getCanonicalDecl() {
1931 return getFirstDeclaration();
1932}
1933
Douglas Gregorbf62d642010-12-06 18:36:25 +00001934void FunctionDecl::setStorageClass(StorageClass SC) {
1935 assert(isLegalForFunction(SC));
1936 if (getStorageClass() != SC)
Rafael Espindola19de5612013-01-12 06:42:30 +00001937 ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001938
1939 SClass = SC;
1940}
1941
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001942/// \brief Returns a value indicating whether this function
1943/// corresponds to a builtin function.
1944///
1945/// The function corresponds to a built-in function if it is
1946/// declared at translation scope or within an extern "C" block and
1947/// its name matches with the name of a builtin. The returned value
1948/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001949/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001950/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001951unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001952 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001953 return 0;
1954
1955 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001956 if (!BuiltinID)
1957 return 0;
1958
1959 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001960 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1961 return BuiltinID;
1962
1963 // This function has the name of a known C library
1964 // function. Determine whether it actually refers to the C library
1965 // function or whether it just has the same name.
1966
Douglas Gregora908e7f2009-02-17 03:23:10 +00001967 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001968 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001969 return 0;
1970
Douglas Gregore711f702009-02-14 18:57:46 +00001971 // If this function is at translation-unit scope and we're not in
1972 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001973 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001974 getDeclContext()->isTranslationUnit())
1975 return BuiltinID;
1976
1977 // If the function is in an extern "C" linkage specification and is
1978 // not marked "overloadable", it's the real function.
1979 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001980 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001981 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001982 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001983 return BuiltinID;
1984
1985 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001986 return 0;
1987}
1988
1989
Chris Lattner47c0d002009-04-25 06:03:53 +00001990/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001991/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001992/// after it has been created.
1993unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001994 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001995 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001996 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001997 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001998
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001999}
2000
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002001void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002002 ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002003 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00002004 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00002005
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002006 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002007 if (!NewParamInfo.empty()) {
2008 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2009 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00002010 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002011}
Chris Lattner41943152007-01-25 04:52:46 +00002012
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002013void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy6f8780b2012-02-29 10:24:19 +00002014 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2015
2016 if (!NewDecls.empty()) {
2017 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2018 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002019 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy6f8780b2012-02-29 10:24:19 +00002020 }
2021}
2022
Chris Lattner58258242008-04-10 02:22:51 +00002023/// getMinRequiredArguments - Returns the minimum number of arguments
2024/// needed to call this function. This may be fewer than the number of
2025/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00002026/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00002027unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002028 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002029 return getNumParams();
2030
Douglas Gregor7825bf32011-01-06 22:09:01 +00002031 unsigned NumRequiredArgs = getNumParams();
2032
2033 // If the last parameter is a parameter pack, we don't need an argument for
2034 // it.
2035 if (NumRequiredArgs > 0 &&
2036 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2037 --NumRequiredArgs;
2038
2039 // If this parameter has a default argument, we don't need an argument for
2040 // it.
2041 while (NumRequiredArgs > 0 &&
2042 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00002043 --NumRequiredArgs;
2044
Douglas Gregor0dd423e2011-01-11 01:52:23 +00002045 // We might have parameter packs before the end. These can't be deduced,
2046 // but they can still handle multiple arguments.
2047 unsigned ArgIdx = NumRequiredArgs;
2048 while (ArgIdx > 0) {
2049 if (getParamDecl(ArgIdx - 1)->isParameterPack())
2050 NumRequiredArgs = ArgIdx;
2051
2052 --ArgIdx;
2053 }
2054
Chris Lattner58258242008-04-10 02:22:51 +00002055 return NumRequiredArgs;
2056}
2057
Eli Friedman1b125c32012-02-07 03:50:18 +00002058static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2059 // Only consider file-scope declarations in this test.
2060 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2061 return false;
2062
2063 // Only consider explicit declarations; the presence of a builtin for a
2064 // libcall shouldn't affect whether a definition is externally visible.
2065 if (Redecl->isImplicit())
2066 return false;
2067
2068 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2069 return true; // Not an inline definition
2070
2071 return false;
2072}
2073
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002074/// \brief For a function declaration in C or C++, determine whether this
2075/// declaration causes the definition to be externally visible.
2076///
Eli Friedman1b125c32012-02-07 03:50:18 +00002077/// Specifically, this determines if adding the current declaration to the set
2078/// of redeclarations of the given functions causes
2079/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002080bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2081 assert(!doesThisDeclarationHaveABody() &&
2082 "Must have a declaration without a body.");
2083
2084 ASTContext &Context = getASTContext();
2085
David Blaikiebbafb8a2012-03-11 07:00:24 +00002086 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002087 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2088 // an externally visible definition.
2089 //
2090 // FIXME: What happens if gnu_inline gets added on after the first
2091 // declaration?
2092 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
2093 return false;
2094
2095 const FunctionDecl *Prev = this;
2096 bool FoundBody = false;
2097 while ((Prev = Prev->getPreviousDecl())) {
2098 FoundBody |= Prev->Body;
2099
2100 if (Prev->Body) {
2101 // If it's not the case that both 'inline' and 'extern' are
2102 // specified on the definition, then it is always externally visible.
2103 if (!Prev->isInlineSpecified() ||
2104 Prev->getStorageClassAsWritten() != SC_Extern)
2105 return false;
2106 } else if (Prev->isInlineSpecified() &&
2107 Prev->getStorageClassAsWritten() != SC_Extern) {
2108 return false;
2109 }
2110 }
2111 return FoundBody;
2112 }
2113
David Blaikiebbafb8a2012-03-11 07:00:24 +00002114 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002115 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002116
2117 // C99 6.7.4p6:
2118 // [...] If all of the file scope declarations for a function in a
2119 // translation unit include the inline function specifier without extern,
2120 // then the definition in that translation unit is an inline definition.
2121 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002122 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002123 const FunctionDecl *Prev = this;
2124 bool FoundBody = false;
2125 while ((Prev = Prev->getPreviousDecl())) {
2126 FoundBody |= Prev->Body;
2127 if (RedeclForcesDefC99(Prev))
2128 return false;
2129 }
2130 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002131}
2132
Richard Smithf3814ad2013-01-25 00:08:28 +00002133/// \brief For an inline function definition in C, or for a gnu_inline function
2134/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002135///
2136/// Inline function definitions are always available for inlining optimizations.
2137/// However, depending on the language dialect, declaration specifiers, and
2138/// attributes, the definition of an inline function may or may not be
2139/// "externally" visible to other translation units in the program.
2140///
2141/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002142/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002143/// inline definition becomes externally visible (C99 6.7.4p6).
2144///
2145/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2146/// definition, we use the GNU semantics for inline, which are nearly the
2147/// opposite of C99 semantics. In particular, "inline" by itself will create
2148/// an externally visible symbol, but "extern inline" will not create an
2149/// externally visible symbol.
2150bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002151 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002152 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002153 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002154
David Blaikiebbafb8a2012-03-11 07:00:24 +00002155 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002156 // Note: If you change the logic here, please change
2157 // doesDeclarationForceExternallyVisibleDefinition as well.
2158 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002159 // If it's not the case that both 'inline' and 'extern' are
2160 // specified on the definition, then this inline definition is
2161 // externally visible.
2162 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2163 return true;
2164
2165 // If any declaration is 'inline' but not 'extern', then this definition
2166 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002167 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2168 Redecl != RedeclEnd;
2169 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002170 if (Redecl->isInlineSpecified() &&
2171 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002172 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002173 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002174
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002175 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002176 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002177
Richard Smithf3814ad2013-01-25 00:08:28 +00002178 // The rest of this function is C-only.
2179 assert(!Context.getLangOpts().CPlusPlus &&
2180 "should not use C inline rules in C++");
2181
Douglas Gregor299d76e2009-09-13 07:46:26 +00002182 // C99 6.7.4p6:
2183 // [...] If all of the file scope declarations for a function in a
2184 // translation unit include the inline function specifier without extern,
2185 // then the definition in that translation unit is an inline definition.
2186 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2187 Redecl != RedeclEnd;
2188 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002189 if (RedeclForcesDefC99(*Redecl))
2190 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002191 }
2192
2193 // C99 6.7.4p6:
2194 // An inline definition does not provide an external definition for the
2195 // function, and does not forbid an external definition in another
2196 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002197 return false;
2198}
2199
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002200/// getOverloadedOperator - Which C++ overloaded operator this
2201/// function represents, if any.
2202OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002203 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2204 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002205 else
2206 return OO_None;
2207}
2208
Alexis Huntc88db062010-01-13 09:01:02 +00002209/// getLiteralIdentifier - The literal suffix identifier this function
2210/// represents, if any.
2211const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2212 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2213 return getDeclName().getCXXLiteralIdentifier();
2214 else
2215 return 0;
2216}
2217
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002218FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2219 if (TemplateOrSpecialization.isNull())
2220 return TK_NonTemplate;
2221 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2222 return TK_FunctionTemplate;
2223 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2224 return TK_MemberSpecialization;
2225 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2226 return TK_FunctionTemplateSpecialization;
2227 if (TemplateOrSpecialization.is
2228 <DependentFunctionTemplateSpecializationInfo*>())
2229 return TK_DependentFunctionTemplateSpecialization;
2230
David Blaikie83d382b2011-09-23 05:06:16 +00002231 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002232}
2233
Douglas Gregord801b062009-10-07 23:56:10 +00002234FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002235 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002236 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2237
2238 return 0;
2239}
2240
Douglas Gregor06db9f52009-10-12 20:18:28 +00002241MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2242 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2243}
2244
Douglas Gregord801b062009-10-07 23:56:10 +00002245void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002246FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2247 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002248 TemplateSpecializationKind TSK) {
2249 assert(TemplateOrSpecialization.isNull() &&
2250 "Member function is already a specialization");
2251 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002252 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002253 TemplateOrSpecialization = Info;
2254}
2255
Douglas Gregorafca3b42009-10-27 20:53:28 +00002256bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002257 // If the function is invalid, it can't be implicitly instantiated.
2258 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002259 return false;
2260
2261 switch (getTemplateSpecializationKind()) {
2262 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002263 case TSK_ExplicitInstantiationDefinition:
2264 return false;
2265
2266 case TSK_ImplicitInstantiation:
2267 return true;
2268
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002269 // It is possible to instantiate TSK_ExplicitSpecialization kind
2270 // if the FunctionDecl has a class scope specialization pattern.
2271 case TSK_ExplicitSpecialization:
2272 return getClassScopeSpecializationPattern() != 0;
2273
Douglas Gregorafca3b42009-10-27 20:53:28 +00002274 case TSK_ExplicitInstantiationDeclaration:
2275 // Handled below.
2276 break;
2277 }
2278
2279 // Find the actual template from which we will instantiate.
2280 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002281 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002282 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002283 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002284
2285 // C++0x [temp.explicit]p9:
2286 // Except for inline functions, other explicit instantiation declarations
2287 // have the effect of suppressing the implicit instantiation of the entity
2288 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002289 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002290 return true;
2291
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002292 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002293}
2294
2295bool FunctionDecl::isTemplateInstantiation() const {
2296 switch (getTemplateSpecializationKind()) {
2297 case TSK_Undeclared:
2298 case TSK_ExplicitSpecialization:
2299 return false;
2300 case TSK_ImplicitInstantiation:
2301 case TSK_ExplicitInstantiationDeclaration:
2302 case TSK_ExplicitInstantiationDefinition:
2303 return true;
2304 }
2305 llvm_unreachable("All TSK values handled.");
2306}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002307
2308FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002309 // Handle class scope explicit specialization special case.
2310 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2311 return getClassScopeSpecializationPattern();
2312
Douglas Gregorafca3b42009-10-27 20:53:28 +00002313 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2314 while (Primary->getInstantiatedFromMemberTemplate()) {
2315 // If we have hit a point where the user provided a specialization of
2316 // this template, we're done looking.
2317 if (Primary->isMemberSpecialization())
2318 break;
2319
2320 Primary = Primary->getInstantiatedFromMemberTemplate();
2321 }
2322
2323 return Primary->getTemplatedDecl();
2324 }
2325
2326 return getInstantiatedFromMemberFunction();
2327}
2328
Douglas Gregor70d83e22009-06-29 17:30:29 +00002329FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002330 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002331 = TemplateOrSpecialization
2332 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002333 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002334 }
2335 return 0;
2336}
2337
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002338FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2339 return getASTContext().getClassScopeSpecializationPattern(this);
2340}
2341
Douglas Gregor70d83e22009-06-29 17:30:29 +00002342const TemplateArgumentList *
2343FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002344 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002345 = TemplateOrSpecialization
2346 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002347 return Info->TemplateArguments;
2348 }
2349 return 0;
2350}
2351
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002352const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002353FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2354 if (FunctionTemplateSpecializationInfo *Info
2355 = TemplateOrSpecialization
2356 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2357 return Info->TemplateArgumentsAsWritten;
2358 }
2359 return 0;
2360}
2361
Mike Stump11289f42009-09-09 15:08:12 +00002362void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002363FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2364 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002365 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002366 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002367 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002368 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2369 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002370 assert(TSK != TSK_Undeclared &&
2371 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002372 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002373 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002374 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002375 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2376 TemplateArgs,
2377 TemplateArgsAsWritten,
2378 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002379 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002380 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002381}
2382
John McCallb9c78482010-04-08 09:05:18 +00002383void
2384FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2385 const UnresolvedSetImpl &Templates,
2386 const TemplateArgumentListInfo &TemplateArgs) {
2387 assert(TemplateOrSpecialization.isNull());
2388 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2389 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002390 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002391 void *Buffer = Context.Allocate(Size);
2392 DependentFunctionTemplateSpecializationInfo *Info =
2393 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2394 TemplateArgs);
2395 TemplateOrSpecialization = Info;
2396}
2397
2398DependentFunctionTemplateSpecializationInfo::
2399DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2400 const TemplateArgumentListInfo &TArgs)
2401 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2402
2403 d.NumTemplates = Ts.size();
2404 d.NumArgs = TArgs.size();
2405
2406 FunctionTemplateDecl **TsArray =
2407 const_cast<FunctionTemplateDecl**>(getTemplates());
2408 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2409 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2410
2411 TemplateArgumentLoc *ArgsArray =
2412 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2413 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2414 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2415}
2416
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002417TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002418 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002419 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002420 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002421 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002422 if (FTSInfo)
2423 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002424
Douglas Gregord801b062009-10-07 23:56:10 +00002425 MemberSpecializationInfo *MSInfo
2426 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2427 if (MSInfo)
2428 return MSInfo->getTemplateSpecializationKind();
2429
2430 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002431}
2432
Mike Stump11289f42009-09-09 15:08:12 +00002433void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002434FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2435 SourceLocation PointOfInstantiation) {
2436 if (FunctionTemplateSpecializationInfo *FTSInfo
2437 = TemplateOrSpecialization.dyn_cast<
2438 FunctionTemplateSpecializationInfo*>()) {
2439 FTSInfo->setTemplateSpecializationKind(TSK);
2440 if (TSK != TSK_ExplicitSpecialization &&
2441 PointOfInstantiation.isValid() &&
2442 FTSInfo->getPointOfInstantiation().isInvalid())
2443 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2444 } else if (MemberSpecializationInfo *MSInfo
2445 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2446 MSInfo->setTemplateSpecializationKind(TSK);
2447 if (TSK != TSK_ExplicitSpecialization &&
2448 PointOfInstantiation.isValid() &&
2449 MSInfo->getPointOfInstantiation().isInvalid())
2450 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2451 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002452 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002453}
2454
2455SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002456 if (FunctionTemplateSpecializationInfo *FTSInfo
2457 = TemplateOrSpecialization.dyn_cast<
2458 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002459 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002460 else if (MemberSpecializationInfo *MSInfo
2461 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002462 return MSInfo->getPointOfInstantiation();
2463
2464 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002465}
2466
Douglas Gregor6411b922009-09-11 20:15:17 +00002467bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002468 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002469 return true;
2470
2471 // If this function was instantiated from a member function of a
2472 // class template, check whether that member function was defined out-of-line.
2473 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2474 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002475 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002476 return Definition->isOutOfLine();
2477 }
2478
2479 // If this function was instantiated from a function template,
2480 // check whether that function template was defined out-of-line.
2481 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2482 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002483 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002484 return Definition->isOutOfLine();
2485 }
2486
2487 return false;
2488}
2489
Abramo Bagnaraea947882011-03-08 16:41:52 +00002490SourceRange FunctionDecl::getSourceRange() const {
2491 return SourceRange(getOuterLocStart(), EndRangeLoc);
2492}
2493
Anna Zaks28db7ce2012-01-18 02:45:01 +00002494unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002495 IdentifierInfo *FnInfo = getIdentifier();
2496
2497 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002498 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002499
2500 // Builtin handling.
2501 switch (getBuiltinID()) {
2502 case Builtin::BI__builtin_memset:
2503 case Builtin::BI__builtin___memset_chk:
2504 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002505 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002506
2507 case Builtin::BI__builtin_memcpy:
2508 case Builtin::BI__builtin___memcpy_chk:
2509 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002510 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002511
2512 case Builtin::BI__builtin_memmove:
2513 case Builtin::BI__builtin___memmove_chk:
2514 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002515 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002516
2517 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002518 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002519 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002520 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002521
2522 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002523 case Builtin::BImemcmp:
2524 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002525
2526 case Builtin::BI__builtin_strncpy:
2527 case Builtin::BI__builtin___strncpy_chk:
2528 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002529 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002530
2531 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002532 case Builtin::BIstrncmp:
2533 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002534
2535 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002536 case Builtin::BIstrncasecmp:
2537 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002538
2539 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002540 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002541 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002542 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002543
2544 case Builtin::BI__builtin_strndup:
2545 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002546 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002547
Anna Zaks314cd092012-02-01 19:08:57 +00002548 case Builtin::BI__builtin_strlen:
2549 case Builtin::BIstrlen:
2550 return Builtin::BIstrlen;
2551
Anna Zaks201d4892012-01-13 21:52:01 +00002552 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002553 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002554 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002555 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002556 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002557 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002558 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002559 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002560 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002561 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002562 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002563 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002564 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002565 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002566 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002567 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002568 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002569 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002570 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002571 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002572 else if (FnInfo->isStr("strlen"))
2573 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002574 }
2575 break;
2576 }
Anna Zaks22122702012-01-17 00:37:07 +00002577 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002578}
2579
Chris Lattner59a25942008-03-31 00:36:02 +00002580//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002581// FieldDecl Implementation
2582//===----------------------------------------------------------------------===//
2583
Jay Foad39c79802011-01-12 09:06:06 +00002584FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002585 SourceLocation StartLoc, SourceLocation IdLoc,
2586 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002587 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002588 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002589 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002590 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002591}
2592
Douglas Gregor72172e92012-01-05 21:55:30 +00002593FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2594 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2595 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002596 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002597}
2598
Sebastian Redl833ef452010-01-26 22:01:41 +00002599bool FieldDecl::isAnonymousStructOrUnion() const {
2600 if (!isImplicit() || getDeclName())
2601 return false;
2602
2603 if (const RecordType *Record = getType()->getAs<RecordType>())
2604 return Record->getDecl()->isAnonymousStructOrUnion();
2605
2606 return false;
2607}
2608
Richard Smithcaf33902011-10-10 18:28:20 +00002609unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2610 assert(isBitField() && "not a bitfield");
2611 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2612 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2613}
2614
John McCall4e819612011-01-20 07:57:12 +00002615unsigned FieldDecl::getFieldIndex() const {
2616 if (CachedFieldIndex) return CachedFieldIndex - 1;
2617
Richard Smithd62306a2011-11-10 06:34:14 +00002618 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002619 const RecordDecl *RD = getParent();
2620 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002621 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002622
2623 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2624 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002625 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002626
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002627 if (IsMsStruct) {
2628 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002629 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002630 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002631 continue;
2632 }
David Blaikie40ed2972012-06-06 20:45:41 +00002633 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002634 }
John McCall4e819612011-01-20 07:57:12 +00002635 }
2636
Richard Smithd62306a2011-11-10 06:34:14 +00002637 assert(CachedFieldIndex && "failed to find field in parent");
2638 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002639}
2640
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002641SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002642 if (const Expr *E = InitializerOrBitWidth.getPointer())
2643 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002644 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002645}
2646
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002647void FieldDecl::setBitWidth(Expr *Width) {
2648 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2649 "bit width or initializer already set");
2650 InitializerOrBitWidth.setPointer(Width);
2651}
2652
Richard Smith938f40b2011-06-11 17:19:42 +00002653void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002654 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002655 "bit width or initializer already set");
2656 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002657}
2658
Sebastian Redl833ef452010-01-26 22:01:41 +00002659//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002660// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002661//===----------------------------------------------------------------------===//
2662
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002663SourceLocation TagDecl::getOuterLocStart() const {
2664 return getTemplateOrInnerLocStart(this);
2665}
2666
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002667SourceRange TagDecl::getSourceRange() const {
2668 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002669 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002670}
2671
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002672TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002673 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002674}
2675
Richard Smithdda56e42011-04-15 14:24:37 +00002676void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2677 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002678 if (TypeForDecl)
Rafael Espindola19de5612013-01-12 06:42:30 +00002679 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2680 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002681}
2682
Douglas Gregordee1be82009-01-17 00:42:38 +00002683void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002684 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002685
David Blaikie095deba2012-11-14 01:52:05 +00002686 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002687 struct CXXRecordDecl::DefinitionData *Data =
2688 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002689 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2690 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002691 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002692}
2693
2694void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002695 assert((!isa<CXXRecordDecl>(this) ||
2696 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2697 "definition completed but not started");
2698
John McCallf937c022011-10-07 06:10:15 +00002699 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002700 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002701
2702 if (ASTMutationListener *L = getASTMutationListener())
2703 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002704}
2705
John McCallf937c022011-10-07 06:10:15 +00002706TagDecl *TagDecl::getDefinition() const {
2707 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002708 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002709
2710 // If it's possible for us to have an out-of-date definition, check now.
2711 if (MayHaveOutOfDateDef) {
2712 if (IdentifierInfo *II = getIdentifier()) {
2713 if (II->isOutOfDate()) {
2714 updateOutOfDate(*II);
2715 }
2716 }
2717 }
2718
Andrew Trickba266ee2010-10-19 21:54:32 +00002719 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2720 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002721
2722 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002723 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002724 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002725 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002726
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002727 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002728}
2729
Douglas Gregor14454802011-02-25 02:25:35 +00002730void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2731 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002732 // Make sure the extended qualifier info is allocated.
2733 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002734 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002735 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002736 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002737 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002738 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002739 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002740 if (getExtInfo()->NumTemplParamLists == 0) {
2741 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002742 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002743 }
2744 else
2745 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002746 }
2747 }
2748}
2749
Abramo Bagnara60804e12011-03-18 15:16:37 +00002750void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2751 unsigned NumTPLists,
2752 TemplateParameterList **TPLists) {
2753 assert(NumTPLists > 0);
2754 // Make sure the extended decl info is allocated.
2755 if (!hasExtInfo())
2756 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002757 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002758 // Set the template parameter lists info.
2759 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2760}
2761
Ted Kremenek21475702008-09-05 17:16:31 +00002762//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002763// EnumDecl Implementation
2764//===----------------------------------------------------------------------===//
2765
David Blaikie68e081d2011-12-20 02:48:34 +00002766void EnumDecl::anchor() { }
2767
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002768EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2769 SourceLocation StartLoc, SourceLocation IdLoc,
2770 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002771 EnumDecl *PrevDecl, bool IsScoped,
2772 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002773 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002774 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002775 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00002776 C.getTypeDeclType(Enum, PrevDecl);
2777 return Enum;
2778}
2779
Douglas Gregor72172e92012-01-05 21:55:30 +00002780EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2781 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002782 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
2783 0, 0, false, false, false);
2784 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
2785 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002786}
2787
Douglas Gregord5058122010-02-11 01:19:42 +00002788void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002789 QualType NewPromotionType,
2790 unsigned NumPositiveBits,
2791 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002792 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002793 if (!IntegerType)
2794 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002795 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002796 setNumPositiveBits(NumPositiveBits);
2797 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002798 TagDecl::completeDefinition();
2799}
2800
Richard Smith7d137e32012-03-23 03:33:32 +00002801TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2802 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2803 return MSI->getTemplateSpecializationKind();
2804
2805 return TSK_Undeclared;
2806}
2807
2808void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2809 SourceLocation PointOfInstantiation) {
2810 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2811 assert(MSI && "Not an instantiated member enumeration?");
2812 MSI->setTemplateSpecializationKind(TSK);
2813 if (TSK != TSK_ExplicitSpecialization &&
2814 PointOfInstantiation.isValid() &&
2815 MSI->getPointOfInstantiation().isInvalid())
2816 MSI->setPointOfInstantiation(PointOfInstantiation);
2817}
2818
Richard Smith4b38ded2012-03-14 23:13:10 +00002819EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2820 if (SpecializationInfo)
2821 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2822
2823 return 0;
2824}
2825
2826void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2827 TemplateSpecializationKind TSK) {
2828 assert(!SpecializationInfo && "Member enum is already a specialization");
2829 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2830}
2831
Sebastian Redl833ef452010-01-26 22:01:41 +00002832//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002833// RecordDecl Implementation
2834//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002835
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002836RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2837 SourceLocation StartLoc, SourceLocation IdLoc,
2838 IdentifierInfo *Id, RecordDecl *PrevDecl)
2839 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002840 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002841 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002842 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00002843 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002844 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002845 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002846}
2847
Jay Foad39c79802011-01-12 09:06:06 +00002848RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002849 SourceLocation StartLoc, SourceLocation IdLoc,
2850 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2851 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2852 PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002853 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
2854
Ted Kremenek21475702008-09-05 17:16:31 +00002855 C.getTypeDeclType(R, PrevDecl);
2856 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002857}
2858
Douglas Gregor72172e92012-01-05 21:55:30 +00002859RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2860 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002861 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2862 SourceLocation(), 0, 0);
2863 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
2864 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002865}
2866
Douglas Gregordfcad112009-03-25 15:59:44 +00002867bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002868 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002869 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2870}
2871
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002872RecordDecl::field_iterator RecordDecl::field_begin() const {
2873 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2874 LoadFieldsFromExternalStorage();
2875
2876 return field_iterator(decl_iterator(FirstDecl));
2877}
2878
Douglas Gregorb11aad82011-02-19 18:51:44 +00002879/// completeDefinition - Notes that the definition of this type is now
2880/// complete.
2881void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002882 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002883 TagDecl::completeDefinition();
2884}
2885
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002886/// isMsStruct - Get whether or not this record uses ms_struct layout.
2887/// This which can be turned on with an attribute, pragma, or the
2888/// -mms-bitfields command-line option.
2889bool RecordDecl::isMsStruct(const ASTContext &C) const {
2890 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2891}
2892
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002893static bool isFieldOrIndirectField(Decl::Kind K) {
2894 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2895}
2896
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002897void RecordDecl::LoadFieldsFromExternalStorage() const {
2898 ExternalASTSource *Source = getASTContext().getExternalSource();
2899 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2900
2901 // Notify that we have a RecordDecl doing some initialization.
2902 ExternalASTSource::Deserializing TheFields(Source);
2903
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002904 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002905 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002906 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2907 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002908 case ELR_Success:
2909 break;
2910
2911 case ELR_AlreadyLoaded:
2912 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002913 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002914 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002915
2916#ifndef NDEBUG
2917 // Check that all decls we got were FieldDecls.
2918 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002919 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002920#endif
2921
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002922 if (Decls.empty())
2923 return;
2924
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002925 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2926 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002927}
2928
Steve Naroff415d3d52008-10-08 17:01:13 +00002929//===----------------------------------------------------------------------===//
2930// BlockDecl Implementation
2931//===----------------------------------------------------------------------===//
2932
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002933void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002934 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002935
Steve Naroffc4b30e52009-03-13 16:56:44 +00002936 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002937 if (!NewParamInfo.empty()) {
2938 NumParams = NewParamInfo.size();
2939 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2940 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002941 }
2942}
2943
John McCall351762c2011-02-07 10:33:21 +00002944void BlockDecl::setCaptures(ASTContext &Context,
2945 const Capture *begin,
2946 const Capture *end,
2947 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002948 CapturesCXXThis = capturesCXXThis;
2949
2950 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002951 NumCaptures = 0;
2952 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002953 return;
2954 }
2955
John McCall351762c2011-02-07 10:33:21 +00002956 NumCaptures = end - begin;
2957
2958 // Avoid new Capture[] because we don't want to provide a default
2959 // constructor.
2960 size_t allocationSize = NumCaptures * sizeof(Capture);
2961 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2962 memcpy(buffer, begin, allocationSize);
2963 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002964}
Sebastian Redl833ef452010-01-26 22:01:41 +00002965
John McCallce45f882011-06-15 22:51:16 +00002966bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2967 for (capture_const_iterator
2968 i = capture_begin(), e = capture_end(); i != e; ++i)
2969 // Only auto vars can be captured, so no redeclaration worries.
2970 if (i->getVariable() == variable)
2971 return true;
2972
2973 return false;
2974}
2975
Douglas Gregor70226da2010-12-21 16:27:07 +00002976SourceRange BlockDecl::getSourceRange() const {
2977 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2978}
Sebastian Redl833ef452010-01-26 22:01:41 +00002979
2980//===----------------------------------------------------------------------===//
2981// Other Decl Allocation/Deallocation Method Implementations
2982//===----------------------------------------------------------------------===//
2983
David Blaikie68e081d2011-12-20 02:48:34 +00002984void TranslationUnitDecl::anchor() { }
2985
Sebastian Redl833ef452010-01-26 22:01:41 +00002986TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2987 return new (C) TranslationUnitDecl(C);
2988}
2989
David Blaikie68e081d2011-12-20 02:48:34 +00002990void LabelDecl::anchor() { }
2991
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002992LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002993 SourceLocation IdentL, IdentifierInfo *II) {
2994 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2995}
2996
2997LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2998 SourceLocation IdentL, IdentifierInfo *II,
2999 SourceLocation GnuLabelL) {
3000 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3001 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003002}
3003
Douglas Gregor72172e92012-01-05 21:55:30 +00003004LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3005 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3006 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00003007}
3008
David Blaikie68e081d2011-12-20 02:48:34 +00003009void ValueDecl::anchor() { }
3010
Benjamin Kramerea70eb32012-12-01 15:09:41 +00003011bool ValueDecl::isWeak() const {
3012 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3013 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3014 return true;
3015
3016 return isWeakImported();
3017}
3018
David Blaikie68e081d2011-12-20 02:48:34 +00003019void ImplicitParamDecl::anchor() { }
3020
Sebastian Redl833ef452010-01-26 22:01:41 +00003021ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003022 SourceLocation IdLoc,
3023 IdentifierInfo *Id,
3024 QualType Type) {
3025 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00003026}
3027
Douglas Gregor72172e92012-01-05 21:55:30 +00003028ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3029 unsigned ID) {
3030 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3031 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3032}
3033
Sebastian Redl833ef452010-01-26 22:01:41 +00003034FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003035 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003036 const DeclarationNameInfo &NameInfo,
3037 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003038 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00003039 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00003040 bool hasWrittenPrototype,
3041 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003042 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
3043 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00003044 isInlineSpecified,
3045 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00003046 New->HasWrittenPrototype = hasWrittenPrototype;
3047 return New;
3048}
3049
Douglas Gregor72172e92012-01-05 21:55:30 +00003050FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3051 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3052 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3053 DeclarationNameInfo(), QualType(), 0,
3054 SC_None, SC_None, false, false);
3055}
3056
Sebastian Redl833ef452010-01-26 22:01:41 +00003057BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3058 return new (C) BlockDecl(DC, L);
3059}
3060
Douglas Gregor72172e92012-01-05 21:55:30 +00003061BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3062 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3063 return new (Mem) BlockDecl(0, SourceLocation());
3064}
3065
Sebastian Redl833ef452010-01-26 22:01:41 +00003066EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3067 SourceLocation L,
3068 IdentifierInfo *Id, QualType T,
3069 Expr *E, const llvm::APSInt &V) {
3070 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3071}
3072
Douglas Gregor72172e92012-01-05 21:55:30 +00003073EnumConstantDecl *
3074EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3075 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3076 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3077 llvm::APSInt());
3078}
3079
David Blaikie68e081d2011-12-20 02:48:34 +00003080void IndirectFieldDecl::anchor() { }
3081
Benjamin Kramer39593702010-11-21 14:11:41 +00003082IndirectFieldDecl *
3083IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3084 IdentifierInfo *Id, QualType T, NamedDecl **CH,
3085 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003086 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3087}
3088
Douglas Gregor72172e92012-01-05 21:55:30 +00003089IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3090 unsigned ID) {
3091 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3092 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3093 QualType(), 0, 0);
3094}
3095
Douglas Gregorbe996932010-09-01 20:41:53 +00003096SourceRange EnumConstantDecl::getSourceRange() const {
3097 SourceLocation End = getLocation();
3098 if (Init)
3099 End = Init->getLocEnd();
3100 return SourceRange(getLocation(), End);
3101}
3102
David Blaikie68e081d2011-12-20 02:48:34 +00003103void TypeDecl::anchor() { }
3104
Sebastian Redl833ef452010-01-26 22:01:41 +00003105TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003106 SourceLocation StartLoc, SourceLocation IdLoc,
3107 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3108 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003109}
3110
David Blaikie68e081d2011-12-20 02:48:34 +00003111void TypedefNameDecl::anchor() { }
3112
Douglas Gregor72172e92012-01-05 21:55:30 +00003113TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3114 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3115 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3116}
3117
Richard Smithdda56e42011-04-15 14:24:37 +00003118TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3119 SourceLocation StartLoc,
3120 SourceLocation IdLoc, IdentifierInfo *Id,
3121 TypeSourceInfo *TInfo) {
3122 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3123}
3124
Douglas Gregor72172e92012-01-05 21:55:30 +00003125TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3126 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3127 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3128}
3129
Abramo Bagnaraea947882011-03-08 16:41:52 +00003130SourceRange TypedefDecl::getSourceRange() const {
3131 SourceLocation RangeEnd = getLocation();
3132 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3133 if (typeIsPostfix(TInfo->getType()))
3134 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3135 }
3136 return SourceRange(getLocStart(), RangeEnd);
3137}
3138
Richard Smithdda56e42011-04-15 14:24:37 +00003139SourceRange TypeAliasDecl::getSourceRange() const {
3140 SourceLocation RangeEnd = getLocStart();
3141 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3142 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3143 return SourceRange(getLocStart(), RangeEnd);
3144}
3145
David Blaikie68e081d2011-12-20 02:48:34 +00003146void FileScopeAsmDecl::anchor() { }
3147
Sebastian Redl833ef452010-01-26 22:01:41 +00003148FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003149 StringLiteral *Str,
3150 SourceLocation AsmLoc,
3151 SourceLocation RParenLoc) {
3152 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003153}
Douglas Gregorba345522011-12-02 23:23:56 +00003154
Douglas Gregor72172e92012-01-05 21:55:30 +00003155FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3156 unsigned ID) {
3157 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3158 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3159}
3160
Douglas Gregorba345522011-12-02 23:23:56 +00003161//===----------------------------------------------------------------------===//
3162// ImportDecl Implementation
3163//===----------------------------------------------------------------------===//
3164
3165/// \brief Retrieve the number of module identifiers needed to name the given
3166/// module.
3167static unsigned getNumModuleIdentifiers(Module *Mod) {
3168 unsigned Result = 1;
3169 while (Mod->Parent) {
3170 Mod = Mod->Parent;
3171 ++Result;
3172 }
3173 return Result;
3174}
3175
Douglas Gregor22d09742012-01-03 18:04:46 +00003176ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003177 Module *Imported,
3178 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003179 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003180 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003181{
3182 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3183 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3184 memcpy(StoredLocs, IdentifierLocs.data(),
3185 IdentifierLocs.size() * sizeof(SourceLocation));
3186}
3187
Douglas Gregor22d09742012-01-03 18:04:46 +00003188ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003189 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003190 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003191 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003192{
3193 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3194}
3195
3196ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003197 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003198 ArrayRef<SourceLocation> IdentifierLocs) {
3199 void *Mem = C.Allocate(sizeof(ImportDecl) +
3200 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003201 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003202}
3203
3204ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003205 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003206 Module *Imported,
3207 SourceLocation EndLoc) {
3208 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003209 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003210 Import->setImplicit();
3211 return Import;
3212}
3213
Douglas Gregor72172e92012-01-05 21:55:30 +00003214ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3215 unsigned NumLocations) {
3216 void *Mem = AllocateDeserializedDecl(C, ID,
3217 (sizeof(ImportDecl) +
3218 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003219 return new (Mem) ImportDecl(EmptyShell());
3220}
3221
3222ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3223 if (!ImportedAndComplete.getInt())
3224 return ArrayRef<SourceLocation>();
3225
3226 const SourceLocation *StoredLocs
3227 = reinterpret_cast<const SourceLocation *>(this + 1);
3228 return ArrayRef<SourceLocation>(StoredLocs,
3229 getNumModuleIdentifiers(getImportedModule()));
3230}
3231
3232SourceRange ImportDecl::getSourceRange() const {
3233 if (!ImportedAndComplete.getInt())
3234 return SourceRange(getLocation(),
3235 *reinterpret_cast<const SourceLocation *>(this + 1));
3236
3237 return SourceRange(getLocation(), getIdentifierLocs().back());
3238}