blob: c0300c50cd9d7cd5c48888f01db3ba59a0b69ebe [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000029
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Chris Lattnerd3b90652008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCall7f1b9872010-12-18 03:30:47 +000036static const VisibilityAttr *GetExplicitVisibility(const Decl *d) {
37 // Use the most recent declaration of a variable.
38 if (const VarDecl *var = dyn_cast<VarDecl>(d))
39 return var->getMostRecentDeclaration()->getAttr<VisibilityAttr>();
40
41 // Use the most recent declaration of a function, and also handle
42 // function template specializations.
43 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(d)) {
44 if (const VisibilityAttr *attr
45 = fn->getMostRecentDeclaration()->getAttr<VisibilityAttr>())
46 return attr;
47
48 // If the function is a specialization of a template with an
49 // explicit visibility attribute, use that.
50 if (FunctionTemplateSpecializationInfo *templateInfo
51 = fn->getTemplateSpecializationInfo())
52 return templateInfo->getTemplate()->getTemplatedDecl()
53 ->getAttr<VisibilityAttr>();
54
55 return 0;
John McCalle7bc9722010-10-28 04:18:25 +000056 }
John McCall7f1b9872010-12-18 03:30:47 +000057
58 // Otherwise, just check the declaration itself first.
59 if (const VisibilityAttr *attr = d->getAttr<VisibilityAttr>())
60 return attr;
61
62 // If there wasn't explicit visibility there, and this is a
63 // specialization of a class template, check for visibility
64 // on the pattern.
65 if (const ClassTemplateSpecializationDecl *spec
66 = dyn_cast<ClassTemplateSpecializationDecl>(d))
67 return spec->getSpecializedTemplate()->getTemplatedDecl()
68 ->getAttr<VisibilityAttr>();
69
70 return 0;
John McCalle7bc9722010-10-28 04:18:25 +000071}
72
John McCall1fb0caa2010-10-22 21:05:15 +000073static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
74 switch (A->getVisibility()) {
75 case VisibilityAttr::Default:
76 return DefaultVisibility;
77 case VisibilityAttr::Hidden:
78 return HiddenVisibility;
79 case VisibilityAttr::Protected:
80 return ProtectedVisibility;
81 }
82 return DefaultVisibility;
83}
84
John McCallaf146032010-10-30 11:50:40 +000085typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall1fb0caa2010-10-22 21:05:15 +000086typedef std::pair<Linkage,Visibility> LVPair;
John McCallaf146032010-10-30 11:50:40 +000087
John McCall1fb0caa2010-10-22 21:05:15 +000088static LVPair merge(LVPair L, LVPair R) {
89 return LVPair(minLinkage(L.first, R.first),
90 minVisibility(L.second, R.second));
91}
92
John McCallaf146032010-10-30 11:50:40 +000093static LVPair merge(LVPair L, LinkageInfo R) {
94 return LVPair(minLinkage(L.first, R.linkage()),
95 minVisibility(L.second, R.visibility()));
96}
97
Benjamin Kramer752c2e92010-11-05 19:56:37 +000098namespace {
John McCall36987482010-11-02 01:45:15 +000099/// Flags controlling the computation of linkage and visibility.
100struct LVFlags {
101 bool ConsiderGlobalVisibility;
102 bool ConsiderVisibilityAttributes;
103
104 LVFlags() : ConsiderGlobalVisibility(true),
105 ConsiderVisibilityAttributes(true) {
106 }
107
Douglas Gregor381d34e2010-12-06 18:36:25 +0000108 /// \brief Returns a set of flags that is only useful for computing the
109 /// linkage, not the visibility, of a declaration.
110 static LVFlags CreateOnlyDeclLinkage() {
111 LVFlags F;
112 F.ConsiderGlobalVisibility = false;
113 F.ConsiderVisibilityAttributes = false;
114 return F;
115 }
116
John McCall36987482010-11-02 01:45:15 +0000117 /// Returns a set of flags, otherwise based on these, which ignores
118 /// off all sources of visibility except template arguments.
119 LVFlags onlyTemplateVisibility() const {
120 LVFlags F = *this;
121 F.ConsiderGlobalVisibility = false;
122 F.ConsiderVisibilityAttributes = false;
123 return F;
124 }
Douglas Gregor89d63e52010-12-06 18:50:56 +0000125};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000126} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000127
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000128/// \brief Get the most restrictive linkage for the types in the given
129/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +0000130static LVPair
131getLVForTemplateParameterList(const TemplateParameterList *Params) {
132 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000133 for (TemplateParameterList::const_iterator P = Params->begin(),
134 PEnd = Params->end();
135 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000136 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
137 if (NTTP->isExpandedParameterPack()) {
138 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
139 QualType T = NTTP->getExpansionType(I);
140 if (!T->isDependentType())
141 LV = merge(LV, T->getLinkageAndVisibility());
142 }
143 continue;
144 }
145
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000146 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000147 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000148 continue;
149 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000150 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000151
152 if (TemplateTemplateParmDecl *TTP
153 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000154 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000155 }
156 }
157
John McCall1fb0caa2010-10-22 21:05:15 +0000158 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000159}
160
Douglas Gregor381d34e2010-12-06 18:36:25 +0000161/// getLVForDecl - Get the linkage and visibility for the given declaration.
162static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
163
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000164/// \brief Get the most restrictive linkage for the types and
165/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +0000166static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000167 unsigned NumArgs,
168 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000169 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000170
171 for (unsigned I = 0; I != NumArgs; ++I) {
172 switch (Args[I].getKind()) {
173 case TemplateArgument::Null:
174 case TemplateArgument::Integral:
175 case TemplateArgument::Expression:
176 break;
177
178 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000179 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000180 break;
181
182 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000183 // The decl can validly be null as the representation of nullptr
184 // arguments, valid only in C++0x.
185 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor89d63e52010-12-06 18:50:56 +0000186 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
187 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000188 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000189 break;
190
191 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000192 case TemplateArgument::TemplateExpansion:
193 if (TemplateDecl *Template
194 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor89d63e52010-12-06 18:50:56 +0000195 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000196 break;
197
198 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000199 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000200 Args[I].pack_size(),
201 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000202 break;
203 }
204 }
205
John McCall1fb0caa2010-10-22 21:05:15 +0000206 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000207}
208
John McCallaf146032010-10-30 11:50:40 +0000209static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000210getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
211 LVFlags &F) {
212 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000213}
214
John McCall36987482010-11-02 01:45:15 +0000215static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000216 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000217 "Not a name having namespace scope");
218 ASTContext &Context = D->getASTContext();
219
220 // C++ [basic.link]p3:
221 // A name having namespace scope (3.3.6) has internal linkage if it
222 // is the name of
223 // - an object, reference, function or function template that is
224 // explicitly declared static; or,
225 // (This bullet corresponds to C99 6.2.2p3.)
226 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
227 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000228 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000229 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000230
231 // - an object or reference that is explicitly declared const
232 // and neither explicitly declared extern nor previously
233 // declared to have external linkage; or
234 // (there is no equivalent in C99)
235 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000236 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000237 Var->getStorageClass() != SC_Extern &&
238 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000239 bool FoundExtern = false;
240 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
241 PrevVar && !FoundExtern;
242 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000243 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000244 FoundExtern = true;
245
246 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000247 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000248 }
249 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000250 // C++ [temp]p4:
251 // A non-member function template can have internal linkage; any
252 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000253 const FunctionDecl *Function = 0;
254 if (const FunctionTemplateDecl *FunTmpl
255 = dyn_cast<FunctionTemplateDecl>(D))
256 Function = FunTmpl->getTemplatedDecl();
257 else
258 Function = cast<FunctionDecl>(D);
259
260 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000261 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000262 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000263 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
264 // - a data member of an anonymous union.
265 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000266 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000267 }
268
Chandler Carruth094b6432011-02-24 19:03:39 +0000269 if (D->isInAnonymousNamespace()) {
270 const VarDecl *Var = dyn_cast<VarDecl>(D);
271 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
272 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
273 return LinkageInfo::uniqueExternal();
274 }
John McCalle7bc9722010-10-28 04:18:25 +0000275
John McCall1fb0caa2010-10-22 21:05:15 +0000276 // Set up the defaults.
277
278 // C99 6.2.2p5:
279 // If the declaration of an identifier for an object has file
280 // scope and no storage-class specifier, its linkage is
281 // external.
John McCallaf146032010-10-30 11:50:40 +0000282 LinkageInfo LV;
283
John McCall36987482010-11-02 01:45:15 +0000284 if (F.ConsiderVisibilityAttributes) {
285 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
286 LV.setVisibility(GetVisibilityFromAttr(VA), true);
287 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000288 } else {
289 // If we're declared in a namespace with a visibility attribute,
290 // use that namespace's visibility, but don't call it explicit.
291 for (const DeclContext *DC = D->getDeclContext();
292 !isa<TranslationUnitDecl>(DC);
293 DC = DC->getParent()) {
294 if (!isa<NamespaceDecl>(DC)) continue;
295 if (const VisibilityAttr *VA =
296 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
297 LV.setVisibility(GetVisibilityFromAttr(VA), false);
298 F.ConsiderGlobalVisibility = false;
299 break;
300 }
301 }
John McCall36987482010-11-02 01:45:15 +0000302 }
John McCallaf146032010-10-30 11:50:40 +0000303 }
John McCall1fb0caa2010-10-22 21:05:15 +0000304
Douglas Gregord85b5b92009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000306
Douglas Gregord85b5b92009-11-25 22:24:25 +0000307 // A name having namespace scope has external linkage if it is the
308 // name of
309 //
310 // - an object or reference, unless it has internal linkage; or
311 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall1fb0caa2010-10-22 21:05:15 +0000315 // Modify the variable's LV by the LV of its type unless this is
316 // C or extern "C". This follows from [basic.link]p9:
317 // A type without linkage shall not be used as the type of a
318 // variable or function with external linkage unless
319 // - the entity has C language linkage, or
320 // - the entity is declared within an unnamed namespace, or
321 // - the entity is not used or is defined in the same
322 // translation unit.
323 // and [basic.link]p10:
324 // ...the types specified by all declarations referring to a
325 // given variable or function shall be identical...
326 // C does not have an equivalent rule.
327 //
John McCallac65c622010-10-26 04:59:26 +0000328 // Ignore this if we've got an explicit attribute; the user
329 // probably knows what they're doing.
330 //
John McCall1fb0caa2010-10-22 21:05:15 +0000331 // Note that we don't want to make the variable non-external
332 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000333 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000334 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
335 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000336 return LinkageInfo::uniqueExternal();
337 if (!LV.visibilityExplicit())
338 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000339 }
340
John McCall35cebc32010-11-02 18:38:13 +0000341 if (Var->getStorageClass() == SC_PrivateExtern)
342 LV.setVisibility(HiddenVisibility, true);
343
Douglas Gregord85b5b92009-11-25 22:24:25 +0000344 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000345 (Var->getStorageClass() == SC_Extern ||
346 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000347
Douglas Gregord85b5b92009-11-25 22:24:25 +0000348 // C99 6.2.2p4:
349 // For an identifier declared with the storage-class specifier
350 // extern in a scope in which a prior declaration of that
351 // identifier is visible, if the prior declaration specifies
352 // internal or external linkage, the linkage of the identifier
353 // at the later declaration is the same as the linkage
354 // specified at the prior declaration. If no prior declaration
355 // is visible, or if the prior declaration specifies no
356 // linkage, then the identifier has external linkage.
357 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000358 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000359 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
360 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000361 }
362 }
363
Douglas Gregord85b5b92009-11-25 22:24:25 +0000364 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000365 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000366 // In theory, we can modify the function's LV by the LV of its
367 // type unless it has C linkage (see comment above about variables
368 // for justification). In practice, GCC doesn't do this, so it's
369 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000370
John McCall35cebc32010-11-02 18:38:13 +0000371 if (Function->getStorageClass() == SC_PrivateExtern)
372 LV.setVisibility(HiddenVisibility, true);
373
Douglas Gregord85b5b92009-11-25 22:24:25 +0000374 // C99 6.2.2p5:
375 // If the declaration of an identifier for a function has no
376 // storage-class specifier, its linkage is determined exactly
377 // as if it were declared with the storage-class specifier
378 // extern.
379 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000380 (Function->getStorageClass() == SC_Extern ||
381 Function->getStorageClass() == SC_PrivateExtern ||
382 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000383 // C99 6.2.2p4:
384 // For an identifier declared with the storage-class specifier
385 // extern in a scope in which a prior declaration of that
386 // identifier is visible, if the prior declaration specifies
387 // internal or external linkage, the linkage of the identifier
388 // at the later declaration is the same as the linkage
389 // specified at the prior declaration. If no prior declaration
390 // is visible, or if the prior declaration specifies no
391 // linkage, then the identifier has external linkage.
392 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000393 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000394 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
395 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000396 }
397 }
398
John McCallaf8ca372011-02-10 06:50:24 +0000399 // In C++, then if the type of the function uses a type with
400 // unique-external linkage, it's not legally usable from outside
401 // this translation unit. However, we should use the C linkage
402 // rules instead for extern "C" declarations.
403 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
404 Function->getType()->getLinkage() == UniqueExternalLinkage)
405 return LinkageInfo::uniqueExternal();
406
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000407 if (FunctionTemplateSpecializationInfo *SpecInfo
408 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000409 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
410 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000411 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000412 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000413 }
414
Douglas Gregord85b5b92009-11-25 22:24:25 +0000415 // - a named class (Clause 9), or an unnamed class defined in a
416 // typedef declaration in which the class has the typedef name
417 // for linkage purposes (7.1.3); or
418 // - a named enumeration (7.2), or an unnamed enumeration
419 // defined in a typedef declaration in which the enumeration
420 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000421 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
422 // Unnamed tags have no linkage.
423 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000424 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000425
John McCall1fb0caa2010-10-22 21:05:15 +0000426 // If this is a class template specialization, consider the
427 // linkage of the template and template arguments.
428 if (const ClassTemplateSpecializationDecl *Spec
429 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000430 // From the template.
431 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
432 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000433
John McCall1fb0caa2010-10-22 21:05:15 +0000434 // The arguments at which the template was instantiated.
435 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000436 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000437 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000438
John McCallac65c622010-10-26 04:59:26 +0000439 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000440 if (F.ConsiderGlobalVisibility)
441 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000442 (Context.getLangOptions().CPlusPlus &&
443 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000444
Douglas Gregord85b5b92009-11-25 22:24:25 +0000445 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000446 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000447 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000448 if (!isExternalLinkage(EnumLV.linkage()))
449 return LinkageInfo::none();
450 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000451
452 // - a template, unless it is a function template that has
453 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000454 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000455 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000456
Douglas Gregord85b5b92009-11-25 22:24:25 +0000457 // - a namespace (7.3), unless it is declared within an unnamed
458 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000459 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
460 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000461
John McCall1fb0caa2010-10-22 21:05:15 +0000462 // By extension, we assign external linkage to Objective-C
463 // interfaces.
464 } else if (isa<ObjCInterfaceDecl>(D)) {
465 // fallout
466
467 // Everything not covered here has no linkage.
468 } else {
John McCallaf146032010-10-30 11:50:40 +0000469 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000470 }
471
472 // If we ended up with non-external linkage, visibility should
473 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000474 if (LV.linkage() != ExternalLinkage)
475 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000476
477 // If we didn't end up with hidden visibility, consider attributes
478 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000479 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000480 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000481
482 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000483}
484
John McCall36987482010-11-02 01:45:15 +0000485static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000486 // Only certain class members have linkage. Note that fields don't
487 // really have linkage, but it's convenient to say they do for the
488 // purposes of calculating linkage of pointer-to-data-member
489 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000490 if (!(isa<CXXMethodDecl>(D) ||
491 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000492 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000493 (isa<TagDecl>(D) &&
494 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000495 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000496
John McCall36987482010-11-02 01:45:15 +0000497 LinkageInfo LV;
498
499 // The flags we're going to use to compute the class's visibility.
500 LVFlags ClassF = F;
501
502 // If we have an explicit visibility attribute, merge that in.
503 if (F.ConsiderVisibilityAttributes) {
504 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
505 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
506
507 // Ignore global visibility later, but not this attribute.
508 F.ConsiderGlobalVisibility = false;
509
510 // Ignore both global visibility and attributes when computing our
511 // parent's visibility.
512 ClassF = F.onlyTemplateVisibility();
513 }
514 }
John McCallaf146032010-10-30 11:50:40 +0000515
516 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000517 // linkage.
518 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
519 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000520 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000521
522 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000523 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000524 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000525
John McCall3cdfc4d2010-08-13 08:35:10 +0000526 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000527 // If the type of the function uses a type with unique-external
528 // linkage, it's not legally usable from outside this translation unit.
529 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
530 return LinkageInfo::uniqueExternal();
531
John McCall110e8e52010-10-29 22:22:43 +0000532 TemplateSpecializationKind TSK = TSK_Undeclared;
533
John McCall1fb0caa2010-10-22 21:05:15 +0000534 // If this is a method template specialization, use the linkage for
535 // the template parameters and arguments.
536 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000537 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000538 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallaf146032010-10-30 11:50:40 +0000539 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000540 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000541
542 TSK = Spec->getTemplateSpecializationKind();
543 } else if (MemberSpecializationInfo *MSI =
544 MD->getMemberSpecializationInfo()) {
545 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000546 }
547
John McCall110e8e52010-10-29 22:22:43 +0000548 // If we're paying attention to global visibility, apply
549 // -finline-visibility-hidden if this is an inline method.
550 //
John McCallaf146032010-10-30 11:50:40 +0000551 // Note that ConsiderGlobalVisibility doesn't yet have information
552 // about whether containing classes have visibility attributes,
553 // and that's intentional.
554 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000555 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000556 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
557 // InlineVisibilityHidden only applies to definitions, and
558 // isInlined() only gives meaningful answers on definitions
559 // anyway.
560 const FunctionDecl *Def = 0;
561 if (MD->hasBody(Def) && Def->isInlined())
562 LV.setVisibility(HiddenVisibility);
563 }
John McCall1fb0caa2010-10-22 21:05:15 +0000564
John McCall110e8e52010-10-29 22:22:43 +0000565 // Note that in contrast to basically every other situation, we
566 // *do* apply -fvisibility to method declarations.
567
568 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000569 if (const ClassTemplateSpecializationDecl *Spec
570 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
571 // Merge template argument/parameter information for member
572 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000573 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallaf146032010-10-30 11:50:40 +0000574 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000575 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000576 }
577
John McCall110e8e52010-10-29 22:22:43 +0000578 // Static data members.
579 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000580 // Modify the variable's linkage by its type, but ignore the
581 // type's visibility unless it's a definition.
582 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
583 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000584 LV.mergeLinkage(UniqueExternalLinkage);
585 if (!LV.visibilityExplicit())
586 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000587 }
588
John McCall36987482010-11-02 01:45:15 +0000589 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000590
591 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000592 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000593 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000594 }
595
John McCall1fb0caa2010-10-22 21:05:15 +0000596 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000597}
598
John McCallf76b0922011-02-08 19:01:05 +0000599static void clearLinkageForClass(const CXXRecordDecl *record) {
600 for (CXXRecordDecl::decl_iterator
601 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
602 Decl *child = *i;
603 if (isa<NamedDecl>(child))
604 cast<NamedDecl>(child)->ClearLinkageCache();
605 }
606}
607
608void NamedDecl::ClearLinkageCache() {
609 // Note that we can't skip clearing the linkage of children just
610 // because the parent doesn't have cached linkage: we don't cache
611 // when computing linkage for parent contexts.
612
613 HasCachedLinkage = 0;
614
615 // If we're changing the linkage of a class, we need to reset the
616 // linkage of child declarations, too.
617 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
618 clearLinkageForClass(record);
619
John McCall15e310a2011-02-19 02:53:41 +0000620 if (ClassTemplateDecl *temp =
621 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000622 // Clear linkage for the template pattern.
623 CXXRecordDecl *record = temp->getTemplatedDecl();
624 record->HasCachedLinkage = 0;
625 clearLinkageForClass(record);
626
John McCall15e310a2011-02-19 02:53:41 +0000627 // We need to clear linkage for specializations, too.
628 for (ClassTemplateDecl::spec_iterator
629 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
630 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000631 }
John McCall15e310a2011-02-19 02:53:41 +0000632
633 // Clear cached linkage for function template decls, too.
634 if (FunctionTemplateDecl *temp =
635 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
636 for (FunctionTemplateDecl::spec_iterator
637 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
638 i->ClearLinkageCache();
639
John McCallf76b0922011-02-08 19:01:05 +0000640}
641
Douglas Gregor381d34e2010-12-06 18:36:25 +0000642Linkage NamedDecl::getLinkage() const {
643 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000644 assert(Linkage(CachedLinkage) ==
645 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000646 return Linkage(CachedLinkage);
647 }
648
649 CachedLinkage = getLVForDecl(this,
650 LVFlags::CreateOnlyDeclLinkage()).linkage();
651 HasCachedLinkage = 1;
652 return Linkage(CachedLinkage);
653}
654
John McCallaf146032010-10-30 11:50:40 +0000655LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000656 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000657 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000658 HasCachedLinkage = 1;
659 CachedLinkage = LI.linkage();
660 return LI;
John McCall0df95872010-10-29 00:29:13 +0000661}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000662
John McCall36987482010-11-02 01:45:15 +0000663static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000664 // Objective-C: treat all Objective-C declarations as having external
665 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000666 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000667 default:
668 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000669 case Decl::TemplateTemplateParm: // count these as external
670 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000671 case Decl::ObjCAtDefsField:
672 case Decl::ObjCCategory:
673 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000674 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000675 case Decl::ObjCForwardProtocol:
676 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000677 case Decl::ObjCMethod:
678 case Decl::ObjCProperty:
679 case Decl::ObjCPropertyImpl:
680 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000681 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000682 }
683
Douglas Gregord85b5b92009-11-25 22:24:25 +0000684 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000685 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000686 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000687
688 // C++ [basic.link]p5:
689 // In addition, a member function, static data member, a named
690 // class or enumeration of class scope, or an unnamed class or
691 // enumeration defined in a class-scope typedef declaration such
692 // that the class or enumeration has the typedef name for linkage
693 // purposes (7.1.3), has external linkage if the name of the class
694 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000695 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000696 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000697
698 // C++ [basic.link]p6:
699 // The name of a function declared in block scope and the name of
700 // an object declared by a block scope extern declaration have
701 // linkage. If there is a visible declaration of an entity with
702 // linkage having the same name and type, ignoring entities
703 // declared outside the innermost enclosing namespace scope, the
704 // block scope declaration declares that same entity and receives
705 // the linkage of the previous declaration. If there is more than
706 // one such matching entity, the program is ill-formed. Otherwise,
707 // if no matching entity is found, the block scope entity receives
708 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000709 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
710 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000711 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000712 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000713
John McCallaf146032010-10-30 11:50:40 +0000714 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000715 if (Flags.ConsiderVisibilityAttributes) {
716 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
717 LV.setVisibility(GetVisibilityFromAttr(VA));
718 }
719
John McCall1fb0caa2010-10-22 21:05:15 +0000720 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000721 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000722 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
723 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000724 }
725
726 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000727 }
728
John McCall0df95872010-10-29 00:29:13 +0000729 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000730 if (Var->getStorageClass() == SC_Extern ||
731 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000732 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000733 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000734
John McCallaf146032010-10-30 11:50:40 +0000735 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000736 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000737 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000738 else if (Flags.ConsiderVisibilityAttributes) {
739 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
740 LV.setVisibility(GetVisibilityFromAttr(VA));
741 }
742
John McCall1fb0caa2010-10-22 21:05:15 +0000743 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000744 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000745 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
746 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000747 }
748
749 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000750 }
751 }
752
753 // C++ [basic.link]p6:
754 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000755 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000756}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000757
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000758std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000759 return getQualifiedNameAsString(getASTContext().getLangOptions());
760}
761
762std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000763 const DeclContext *Ctx = getDeclContext();
764
765 if (Ctx->isFunctionOrMethod())
766 return getNameAsString();
767
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000768 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
769 ContextsTy Contexts;
770
771 // Collect contexts.
772 while (Ctx && isa<NamedDecl>(Ctx)) {
773 Contexts.push_back(Ctx);
774 Ctx = Ctx->getParent();
775 };
776
777 std::string QualName;
778 llvm::raw_string_ostream OS(QualName);
779
780 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
781 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000782 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000783 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000784 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
785 std::string TemplateArgsStr
786 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000787 TemplateArgs.data(),
788 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000789 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000790 OS << Spec->getName() << TemplateArgsStr;
791 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000792 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000793 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000794 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000795 OS << ND;
796 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
797 if (!RD->getIdentifier())
798 OS << "<anonymous " << RD->getKindName() << '>';
799 else
800 OS << RD;
801 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000802 const FunctionProtoType *FT = 0;
803 if (FD->hasWrittenPrototype())
804 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
805
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000806 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000807 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000808 unsigned NumParams = FD->getNumParams();
809 for (unsigned i = 0; i < NumParams; ++i) {
810 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000811 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000812 std::string Param;
813 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000814 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000815 }
816
817 if (FT->isVariadic()) {
818 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000819 OS << ", ";
820 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000821 }
822 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000823 OS << ')';
824 } else {
825 OS << cast<NamedDecl>(*I);
826 }
827 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000828 }
829
John McCall8472af42010-03-16 21:48:18 +0000830 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000831 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000832 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000833 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000834
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000835 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000836}
837
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000838bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000839 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
840
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000841 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
842 // We want to keep it, unless it nominates same namespace.
843 if (getKind() == Decl::UsingDirective) {
844 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
845 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000848 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
849 // For function declarations, we keep track of redeclarations.
850 return FD->getPreviousDeclaration() == OldD;
851
Douglas Gregore53060f2009-06-25 22:08:12 +0000852 // For function templates, the underlying function declarations are linked.
853 if (const FunctionTemplateDecl *FunctionTemplate
854 = dyn_cast<FunctionTemplateDecl>(this))
855 if (const FunctionTemplateDecl *OldFunctionTemplate
856 = dyn_cast<FunctionTemplateDecl>(OldD))
857 return FunctionTemplate->getTemplatedDecl()
858 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Steve Naroff0de21fd2009-02-22 19:35:57 +0000860 // For method declarations, we keep track of redeclarations.
861 if (isa<ObjCMethodDecl>(this))
862 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000863
John McCallf36e02d2009-10-09 21:13:30 +0000864 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
865 return true;
866
John McCall9488ea12009-11-17 05:59:44 +0000867 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
868 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
869 cast<UsingShadowDecl>(OldD)->getTargetDecl();
870
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000871 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
872 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
873 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
874
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000875 // For non-function declarations, if the declarations are of the
876 // same kind then this must be a redeclaration, or semantic analysis
877 // would not have given us the new declaration.
878 return this->getKind() == OldD->getKind();
879}
880
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000881bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000882 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000883}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000884
Anders Carlssone136e0e2009-06-26 06:29:23 +0000885NamedDecl *NamedDecl::getUnderlyingDecl() {
886 NamedDecl *ND = this;
887 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000888 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000889 ND = UD->getTargetDecl();
890 else if (ObjCCompatibleAliasDecl *AD
891 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
892 return AD->getClassInterface();
893 else
894 return ND;
895 }
896}
897
John McCall161755a2010-04-06 21:38:20 +0000898bool NamedDecl::isCXXInstanceMember() const {
899 assert(isCXXClassMember() &&
900 "checking whether non-member is instance member");
901
902 const NamedDecl *D = this;
903 if (isa<UsingShadowDecl>(D))
904 D = cast<UsingShadowDecl>(D)->getTargetDecl();
905
Francois Pichet87c2e122010-11-21 06:08:52 +0000906 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000907 return true;
908 if (isa<CXXMethodDecl>(D))
909 return cast<CXXMethodDecl>(D)->isInstance();
910 if (isa<FunctionTemplateDecl>(D))
911 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
912 ->getTemplatedDecl())->isInstance();
913 return false;
914}
915
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000916//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000917// DeclaratorDecl Implementation
918//===----------------------------------------------------------------------===//
919
Douglas Gregor1693e152010-07-06 18:42:40 +0000920template <typename DeclT>
921static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
922 if (decl->getNumTemplateParameterLists() > 0)
923 return decl->getTemplateParameterList(0)->getTemplateLoc();
924 else
925 return decl->getInnerLocStart();
926}
927
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000928SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000929 TypeSourceInfo *TSI = getTypeSourceInfo();
930 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000931 return SourceLocation();
932}
933
John McCallb6217662010-03-15 10:12:16 +0000934void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
935 SourceRange QualifierRange) {
936 if (Qualifier) {
937 // Make sure the extended decl info is allocated.
938 if (!hasExtInfo()) {
939 // Save (non-extended) type source info pointer.
940 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
941 // Allocate external info struct.
942 DeclInfo = new (getASTContext()) ExtInfo;
943 // Restore savedTInfo into (extended) decl info.
944 getExtInfo()->TInfo = savedTInfo;
945 }
946 // Set qualifier info.
947 getExtInfo()->NNS = Qualifier;
948 getExtInfo()->NNSRange = QualifierRange;
949 }
950 else {
951 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
952 assert(QualifierRange.isInvalid());
953 if (hasExtInfo()) {
954 // Save type source info pointer.
955 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
956 // Deallocate the extended decl info.
957 getASTContext().Deallocate(getExtInfo());
958 // Restore savedTInfo into (non-extended) decl info.
959 DeclInfo = savedTInfo;
960 }
961 }
962}
963
Douglas Gregor1693e152010-07-06 18:42:40 +0000964SourceLocation DeclaratorDecl::getOuterLocStart() const {
965 return getTemplateOrInnerLocStart(this);
966}
967
Abramo Bagnara9b934882010-06-12 08:15:14 +0000968void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000969QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
970 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000971 TemplateParameterList **TPLists) {
972 assert((NumTPLists == 0 || TPLists != 0) &&
973 "Empty array of template parameters with positive size!");
974 assert((NumTPLists == 0 || NNS) &&
975 "Nonempty array of template parameters with no qualifier!");
976
977 // Free previous template parameters (if any).
978 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000979 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000980 TemplParamLists = 0;
981 NumTemplParamLists = 0;
982 }
983 // Set info on matched template parameter lists (if any).
984 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000985 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000986 NumTemplParamLists = NumTPLists;
987 for (unsigned i = NumTPLists; i-- > 0; )
988 TemplParamLists[i] = TPLists[i];
989 }
990}
991
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000992//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000993// VarDecl Implementation
994//===----------------------------------------------------------------------===//
995
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000996const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
997 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000998 case SC_None: break;
999 case SC_Auto: return "auto"; break;
1000 case SC_Extern: return "extern"; break;
1001 case SC_PrivateExtern: return "__private_extern__"; break;
1002 case SC_Register: return "register"; break;
1003 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001004 }
1005
1006 assert(0 && "Invalid storage class");
1007 return 0;
1008}
1009
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001010VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +00001011 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001012 StorageClass S, StorageClass SCAsWritten) {
1013 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001014}
1015
Douglas Gregor381d34e2010-12-06 18:36:25 +00001016void VarDecl::setStorageClass(StorageClass SC) {
1017 assert(isLegalForVariable(SC));
1018 if (getStorageClass() != SC)
1019 ClearLinkageCache();
1020
1021 SClass = SC;
1022}
1023
Douglas Gregorda2142f2011-02-19 18:51:44 +00001024SourceLocation VarDecl::getInnerLocStart() const {
1025 SourceLocation Start = getTypeSpecStartLoc();
1026 if (Start.isInvalid())
1027 Start = getLocation();
1028 return Start;
1029}
1030
Douglas Gregor1693e152010-07-06 18:42:40 +00001031SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001032 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001033 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1034 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001035}
1036
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001037bool VarDecl::isExternC() const {
1038 ASTContext &Context = getASTContext();
1039 if (!Context.getLangOptions().CPlusPlus)
1040 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +00001041 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001042 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1043
Chandler Carruth10aad442011-02-25 00:05:02 +00001044 const DeclContext *DC = getDeclContext();
1045 if (DC->isFunctionOrMethod())
1046 return false;
1047
1048 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001049 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1050 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001051 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001052
1053 break;
1054 }
1055
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001056 }
1057
1058 return false;
1059}
1060
1061VarDecl *VarDecl::getCanonicalDecl() {
1062 return getFirstDeclaration();
1063}
1064
Sebastian Redle9d12b62010-01-31 22:27:38 +00001065VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1066 // C++ [basic.def]p2:
1067 // A declaration is a definition unless [...] it contains the 'extern'
1068 // specifier or a linkage-specification and neither an initializer [...],
1069 // it declares a static data member in a class declaration [...].
1070 // C++ [temp.expl.spec]p15:
1071 // An explicit specialization of a static data member of a template is a
1072 // definition if the declaration includes an initializer; otherwise, it is
1073 // a declaration.
1074 if (isStaticDataMember()) {
1075 if (isOutOfLine() && (hasInit() ||
1076 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1077 return Definition;
1078 else
1079 return DeclarationOnly;
1080 }
1081 // C99 6.7p5:
1082 // A definition of an identifier is a declaration for that identifier that
1083 // [...] causes storage to be reserved for that object.
1084 // Note: that applies for all non-file-scope objects.
1085 // C99 6.9.2p1:
1086 // If the declaration of an identifier for an object has file scope and an
1087 // initializer, the declaration is an external definition for the identifier
1088 if (hasInit())
1089 return Definition;
1090 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1091 if (hasExternalStorage())
1092 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001093
John McCalld931b082010-08-26 03:08:43 +00001094 if (getStorageClassAsWritten() == SC_Extern ||
1095 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001096 for (const VarDecl *PrevVar = getPreviousDeclaration();
1097 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1098 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1099 return DeclarationOnly;
1100 }
1101 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001102 // C99 6.9.2p2:
1103 // A declaration of an object that has file scope without an initializer,
1104 // and without a storage class specifier or the scs 'static', constitutes
1105 // a tentative definition.
1106 // No such thing in C++.
1107 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1108 return TentativeDefinition;
1109
1110 // What's left is (in C, block-scope) declarations without initializers or
1111 // external storage. These are definitions.
1112 return Definition;
1113}
1114
Sebastian Redle9d12b62010-01-31 22:27:38 +00001115VarDecl *VarDecl::getActingDefinition() {
1116 DefinitionKind Kind = isThisDeclarationADefinition();
1117 if (Kind != TentativeDefinition)
1118 return 0;
1119
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001120 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001121 VarDecl *First = getFirstDeclaration();
1122 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1123 I != E; ++I) {
1124 Kind = (*I)->isThisDeclarationADefinition();
1125 if (Kind == Definition)
1126 return 0;
1127 else if (Kind == TentativeDefinition)
1128 LastTentative = *I;
1129 }
1130 return LastTentative;
1131}
1132
1133bool VarDecl::isTentativeDefinitionNow() const {
1134 DefinitionKind Kind = isThisDeclarationADefinition();
1135 if (Kind != TentativeDefinition)
1136 return false;
1137
1138 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1139 if ((*I)->isThisDeclarationADefinition() == Definition)
1140 return false;
1141 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001142 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001143}
1144
Sebastian Redl31310a22010-02-01 20:16:42 +00001145VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001146 VarDecl *First = getFirstDeclaration();
1147 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1148 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001149 if ((*I)->isThisDeclarationADefinition() == Definition)
1150 return *I;
1151 }
1152 return 0;
1153}
1154
John McCall110e8e52010-10-29 22:22:43 +00001155VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1156 DefinitionKind Kind = DeclarationOnly;
1157
1158 const VarDecl *First = getFirstDeclaration();
1159 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1160 I != E; ++I)
1161 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1162
1163 return Kind;
1164}
1165
Sebastian Redl31310a22010-02-01 20:16:42 +00001166const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001167 redecl_iterator I = redecls_begin(), E = redecls_end();
1168 while (I != E && !I->getInit())
1169 ++I;
1170
1171 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001172 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001173 return I->getInit();
1174 }
1175 return 0;
1176}
1177
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001178bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001179 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001180 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001181
1182 if (!isStaticDataMember())
1183 return false;
1184
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001185 // If this static data member was instantiated from a static data member of
1186 // a class template, check whether that static data member was defined
1187 // out-of-line.
1188 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1189 return VD->isOutOfLine();
1190
1191 return false;
1192}
1193
Douglas Gregor0d035142009-10-27 18:42:08 +00001194VarDecl *VarDecl::getOutOfLineDefinition() {
1195 if (!isStaticDataMember())
1196 return 0;
1197
1198 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1199 RD != RDEnd; ++RD) {
1200 if (RD->getLexicalDeclContext()->isFileContext())
1201 return *RD;
1202 }
1203
1204 return 0;
1205}
1206
Douglas Gregor838db382010-02-11 01:19:42 +00001207void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001208 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1209 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001210 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001211 }
1212
1213 Init = I;
1214}
1215
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001216VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001217 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001218 return cast<VarDecl>(MSI->getInstantiatedFrom());
1219
1220 return 0;
1221}
1222
Douglas Gregor663b5a02009-10-14 20:14:33 +00001223TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001224 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001225 return MSI->getTemplateSpecializationKind();
1226
1227 return TSK_Undeclared;
1228}
1229
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001230MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001231 return getASTContext().getInstantiatedFromStaticDataMember(this);
1232}
1233
Douglas Gregor0a897e32009-10-15 17:21:20 +00001234void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1235 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001236 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001237 assert(MSI && "Not an instantiated static data member?");
1238 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001239 if (TSK != TSK_ExplicitSpecialization &&
1240 PointOfInstantiation.isValid() &&
1241 MSI->getPointOfInstantiation().isInvalid())
1242 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001243}
1244
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001245//===----------------------------------------------------------------------===//
1246// ParmVarDecl Implementation
1247//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001248
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001249ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1250 SourceLocation L, IdentifierInfo *Id,
1251 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001252 StorageClass S, StorageClass SCAsWritten,
1253 Expr *DefArg) {
1254 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1255 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001256}
1257
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001258Expr *ParmVarDecl::getDefaultArg() {
1259 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1260 assert(!hasUninstantiatedDefaultArg() &&
1261 "Default argument is not yet instantiated!");
1262
1263 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001264 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001265 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001266
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001267 return Arg;
1268}
1269
1270unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001271 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001272 return E->getNumTemporaries();
1273
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001274 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001275}
1276
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001277CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1278 assert(getNumDefaultArgTemporaries() &&
1279 "Default arguments does not have any temporaries!");
1280
John McCall4765fa02010-12-06 08:20:24 +00001281 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001282 return E->getTemporary(i);
1283}
1284
1285SourceRange ParmVarDecl::getDefaultArgRange() const {
1286 if (const Expr *E = getInit())
1287 return E->getSourceRange();
1288
1289 if (hasUninstantiatedDefaultArg())
1290 return getUninstantiatedDefaultArg()->getSourceRange();
1291
1292 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001293}
1294
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001295bool ParmVarDecl::isParameterPack() const {
1296 return isa<PackExpansionType>(getType());
1297}
1298
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001299//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001300// FunctionDecl Implementation
1301//===----------------------------------------------------------------------===//
1302
Douglas Gregorda2142f2011-02-19 18:51:44 +00001303void FunctionDecl::getNameForDiagnostic(std::string &S,
1304 const PrintingPolicy &Policy,
1305 bool Qualified) const {
1306 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1307 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1308 if (TemplateArgs)
1309 S += TemplateSpecializationType::PrintTemplateArgumentList(
1310 TemplateArgs->data(),
1311 TemplateArgs->size(),
1312 Policy);
1313
1314}
1315
Ted Kremenek9498d382010-04-29 16:49:01 +00001316bool FunctionDecl::isVariadic() const {
1317 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1318 return FT->isVariadic();
1319 return false;
1320}
1321
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001322bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1323 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1324 if (I->Body) {
1325 Definition = *I;
1326 return true;
1327 }
1328 }
1329
1330 return false;
1331}
1332
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001333Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001334 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1335 if (I->Body) {
1336 Definition = *I;
1337 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001338 }
1339 }
1340
1341 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001342}
1343
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001344void FunctionDecl::setBody(Stmt *B) {
1345 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001346 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001347 EndRangeLoc = B->getLocEnd();
1348}
1349
Douglas Gregor21386642010-09-28 21:55:22 +00001350void FunctionDecl::setPure(bool P) {
1351 IsPure = P;
1352 if (P)
1353 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1354 Parent->markedVirtualFunctionPure();
1355}
1356
Douglas Gregor48a83b52009-09-12 00:17:51 +00001357bool FunctionDecl::isMain() const {
1358 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001359 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001360 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001361 getIdentifier() && getIdentifier()->isStr("main");
1362}
1363
Douglas Gregor48a83b52009-09-12 00:17:51 +00001364bool FunctionDecl::isExternC() const {
1365 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001366 // In C, any non-static, non-overloadable function has external
1367 // linkage.
1368 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001369 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001370
Chandler Carruth10aad442011-02-25 00:05:02 +00001371 const DeclContext *DC = getDeclContext();
1372 if (DC->isRecord())
1373 return false;
1374
1375 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor63935192009-03-02 00:19:53 +00001376 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1377 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001378 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001379 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001380
1381 break;
1382 }
1383 }
1384
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001385 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001386}
1387
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001388bool FunctionDecl::isGlobal() const {
1389 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1390 return Method->isStatic();
1391
John McCalld931b082010-08-26 03:08:43 +00001392 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001393 return false;
1394
Mike Stump1eb44332009-09-09 15:08:12 +00001395 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001396 DC->isNamespace();
1397 DC = DC->getParent()) {
1398 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1399 if (!Namespace->getDeclName())
1400 return false;
1401 break;
1402 }
1403 }
1404
1405 return true;
1406}
1407
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001408void
1409FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1410 redeclarable_base::setPreviousDeclaration(PrevDecl);
1411
1412 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1413 FunctionTemplateDecl *PrevFunTmpl
1414 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1415 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1416 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1417 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001418
1419 if (PrevDecl->IsInline)
1420 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001421}
1422
1423const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1424 return getFirstDeclaration();
1425}
1426
1427FunctionDecl *FunctionDecl::getCanonicalDecl() {
1428 return getFirstDeclaration();
1429}
1430
Douglas Gregor381d34e2010-12-06 18:36:25 +00001431void FunctionDecl::setStorageClass(StorageClass SC) {
1432 assert(isLegalForFunction(SC));
1433 if (getStorageClass() != SC)
1434 ClearLinkageCache();
1435
1436 SClass = SC;
1437}
1438
Douglas Gregor3e41d602009-02-13 23:20:09 +00001439/// \brief Returns a value indicating whether this function
1440/// corresponds to a builtin function.
1441///
1442/// The function corresponds to a built-in function if it is
1443/// declared at translation scope or within an extern "C" block and
1444/// its name matches with the name of a builtin. The returned value
1445/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001446/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001447/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001448unsigned FunctionDecl::getBuiltinID() const {
1449 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001450 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1451 return 0;
1452
1453 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1454 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1455 return BuiltinID;
1456
1457 // This function has the name of a known C library
1458 // function. Determine whether it actually refers to the C library
1459 // function or whether it just has the same name.
1460
Douglas Gregor9add3172009-02-17 03:23:10 +00001461 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001462 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001463 return 0;
1464
Douglas Gregor3c385e52009-02-14 18:57:46 +00001465 // If this function is at translation-unit scope and we're not in
1466 // C++, it refers to the C library function.
1467 if (!Context.getLangOptions().CPlusPlus &&
1468 getDeclContext()->isTranslationUnit())
1469 return BuiltinID;
1470
1471 // If the function is in an extern "C" linkage specification and is
1472 // not marked "overloadable", it's the real function.
1473 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001474 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001475 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001476 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001477 return BuiltinID;
1478
1479 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001480 return 0;
1481}
1482
1483
Chris Lattner1ad9b282009-04-25 06:03:53 +00001484/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001485/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001486/// after it has been created.
1487unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001488 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001489 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001490 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001491 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Reid Spencer5f016e22007-07-11 17:01:13 +00001493}
1494
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001495void FunctionDecl::setParams(ASTContext &C,
1496 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001498 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 // Zero params -> null pointer.
1501 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001502 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001503 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001505
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001506 // Update source range. The check below allows us to set EndRangeLoc before
1507 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001508 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001509 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 }
1511}
1512
Chris Lattner8123a952008-04-10 02:22:51 +00001513/// getMinRequiredArguments - Returns the minimum number of arguments
1514/// needed to call this function. This may be fewer than the number of
1515/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001516/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001517unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001518 if (!getASTContext().getLangOptions().CPlusPlus)
1519 return getNumParams();
1520
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001521 unsigned NumRequiredArgs = getNumParams();
1522
1523 // If the last parameter is a parameter pack, we don't need an argument for
1524 // it.
1525 if (NumRequiredArgs > 0 &&
1526 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1527 --NumRequiredArgs;
1528
1529 // If this parameter has a default argument, we don't need an argument for
1530 // it.
1531 while (NumRequiredArgs > 0 &&
1532 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001533 --NumRequiredArgs;
1534
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001535 // We might have parameter packs before the end. These can't be deduced,
1536 // but they can still handle multiple arguments.
1537 unsigned ArgIdx = NumRequiredArgs;
1538 while (ArgIdx > 0) {
1539 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1540 NumRequiredArgs = ArgIdx;
1541
1542 --ArgIdx;
1543 }
1544
Chris Lattner8123a952008-04-10 02:22:51 +00001545 return NumRequiredArgs;
1546}
1547
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001548bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001549 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001550 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001551
1552 if (isa<CXXMethodDecl>(this)) {
1553 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1554 return true;
1555 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001556
1557 switch (getTemplateSpecializationKind()) {
1558 case TSK_Undeclared:
1559 case TSK_ExplicitSpecialization:
1560 return false;
1561
1562 case TSK_ImplicitInstantiation:
1563 case TSK_ExplicitInstantiationDeclaration:
1564 case TSK_ExplicitInstantiationDefinition:
1565 // Handle below.
1566 break;
1567 }
1568
1569 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001570 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001571 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001572 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001573
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001574 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001575 return PatternDecl->isInlined();
1576
1577 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001578}
1579
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001580/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001581/// definition will be externally visible.
1582///
1583/// Inline function definitions are always available for inlining optimizations.
1584/// However, depending on the language dialect, declaration specifiers, and
1585/// attributes, the definition of an inline function may or may not be
1586/// "externally" visible to other translation units in the program.
1587///
1588/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001589/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001590/// inline definition becomes externally visible (C99 6.7.4p6).
1591///
1592/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1593/// definition, we use the GNU semantics for inline, which are nearly the
1594/// opposite of C99 semantics. In particular, "inline" by itself will create
1595/// an externally visible symbol, but "extern inline" will not create an
1596/// externally visible symbol.
1597bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1598 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001599 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001600 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001601
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001602 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001603 // If it's not the case that both 'inline' and 'extern' are
1604 // specified on the definition, then this inline definition is
1605 // externally visible.
1606 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1607 return true;
1608
1609 // If any declaration is 'inline' but not 'extern', then this definition
1610 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001611 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1612 Redecl != RedeclEnd;
1613 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001614 if (Redecl->isInlineSpecified() &&
1615 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001616 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001617 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001618
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001619 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001620 }
1621
1622 // C99 6.7.4p6:
1623 // [...] If all of the file scope declarations for a function in a
1624 // translation unit include the inline function specifier without extern,
1625 // then the definition in that translation unit is an inline definition.
1626 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1627 Redecl != RedeclEnd;
1628 ++Redecl) {
1629 // Only consider file-scope declarations in this test.
1630 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1631 continue;
1632
John McCalld931b082010-08-26 03:08:43 +00001633 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001634 return true; // Not an inline definition
1635 }
1636
1637 // C99 6.7.4p6:
1638 // An inline definition does not provide an external definition for the
1639 // function, and does not forbid an external definition in another
1640 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001641 return false;
1642}
1643
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001644/// getOverloadedOperator - Which C++ overloaded operator this
1645/// function represents, if any.
1646OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001647 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1648 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001649 else
1650 return OO_None;
1651}
1652
Sean Hunta6c058d2010-01-13 09:01:02 +00001653/// getLiteralIdentifier - The literal suffix identifier this function
1654/// represents, if any.
1655const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1656 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1657 return getDeclName().getCXXLiteralIdentifier();
1658 else
1659 return 0;
1660}
1661
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001662FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1663 if (TemplateOrSpecialization.isNull())
1664 return TK_NonTemplate;
1665 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1666 return TK_FunctionTemplate;
1667 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1668 return TK_MemberSpecialization;
1669 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1670 return TK_FunctionTemplateSpecialization;
1671 if (TemplateOrSpecialization.is
1672 <DependentFunctionTemplateSpecializationInfo*>())
1673 return TK_DependentFunctionTemplateSpecialization;
1674
1675 assert(false && "Did we miss a TemplateOrSpecialization type?");
1676 return TK_NonTemplate;
1677}
1678
Douglas Gregor2db32322009-10-07 23:56:10 +00001679FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001680 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001681 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1682
1683 return 0;
1684}
1685
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001686MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1687 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1688}
1689
Douglas Gregor2db32322009-10-07 23:56:10 +00001690void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001691FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1692 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001693 TemplateSpecializationKind TSK) {
1694 assert(TemplateOrSpecialization.isNull() &&
1695 "Member function is already a specialization");
1696 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001697 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001698 TemplateOrSpecialization = Info;
1699}
1700
Douglas Gregor3b846b62009-10-27 20:53:28 +00001701bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001702 // If the function is invalid, it can't be implicitly instantiated.
1703 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001704 return false;
1705
1706 switch (getTemplateSpecializationKind()) {
1707 case TSK_Undeclared:
1708 case TSK_ExplicitSpecialization:
1709 case TSK_ExplicitInstantiationDefinition:
1710 return false;
1711
1712 case TSK_ImplicitInstantiation:
1713 return true;
1714
1715 case TSK_ExplicitInstantiationDeclaration:
1716 // Handled below.
1717 break;
1718 }
1719
1720 // Find the actual template from which we will instantiate.
1721 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001722 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001723 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001724 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001725
1726 // C++0x [temp.explicit]p9:
1727 // Except for inline functions, other explicit instantiation declarations
1728 // have the effect of suppressing the implicit instantiation of the entity
1729 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001730 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001731 return true;
1732
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001733 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001734}
1735
1736FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1737 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1738 while (Primary->getInstantiatedFromMemberTemplate()) {
1739 // If we have hit a point where the user provided a specialization of
1740 // this template, we're done looking.
1741 if (Primary->isMemberSpecialization())
1742 break;
1743
1744 Primary = Primary->getInstantiatedFromMemberTemplate();
1745 }
1746
1747 return Primary->getTemplatedDecl();
1748 }
1749
1750 return getInstantiatedFromMemberFunction();
1751}
1752
Douglas Gregor16e8be22009-06-29 17:30:29 +00001753FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001754 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001755 = TemplateOrSpecialization
1756 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001757 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001758 }
1759 return 0;
1760}
1761
1762const TemplateArgumentList *
1763FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001764 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001765 = TemplateOrSpecialization
1766 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001767 return Info->TemplateArguments;
1768 }
1769 return 0;
1770}
1771
Abramo Bagnarae03db982010-05-20 15:32:11 +00001772const TemplateArgumentListInfo *
1773FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1774 if (FunctionTemplateSpecializationInfo *Info
1775 = TemplateOrSpecialization
1776 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1777 return Info->TemplateArgumentsAsWritten;
1778 }
1779 return 0;
1780}
1781
Mike Stump1eb44332009-09-09 15:08:12 +00001782void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001783FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1784 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001785 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001786 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001787 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001788 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1789 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001790 assert(TSK != TSK_Undeclared &&
1791 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001792 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001793 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001794 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001795 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1796 TemplateArgs,
1797 TemplateArgsAsWritten,
1798 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001799 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Douglas Gregor127102b2009-06-29 20:59:39 +00001801 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001802 // function template specializations.
1803 if (InsertPos)
1804 Template->getSpecializations().InsertNode(Info, InsertPos);
1805 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001806 // Try to insert the new node. If there is an existing node, leave it, the
1807 // set will contain the canonical decls while
1808 // FunctionTemplateDecl::findSpecialization will return
1809 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001810 FunctionTemplateSpecializationInfo *Existing
1811 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001812 (void)Existing;
1813 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1814 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001815 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001816}
1817
John McCallaf2094e2010-04-08 09:05:18 +00001818void
1819FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1820 const UnresolvedSetImpl &Templates,
1821 const TemplateArgumentListInfo &TemplateArgs) {
1822 assert(TemplateOrSpecialization.isNull());
1823 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1824 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001825 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001826 void *Buffer = Context.Allocate(Size);
1827 DependentFunctionTemplateSpecializationInfo *Info =
1828 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1829 TemplateArgs);
1830 TemplateOrSpecialization = Info;
1831}
1832
1833DependentFunctionTemplateSpecializationInfo::
1834DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1835 const TemplateArgumentListInfo &TArgs)
1836 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1837
1838 d.NumTemplates = Ts.size();
1839 d.NumArgs = TArgs.size();
1840
1841 FunctionTemplateDecl **TsArray =
1842 const_cast<FunctionTemplateDecl**>(getTemplates());
1843 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1844 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1845
1846 TemplateArgumentLoc *ArgsArray =
1847 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1848 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1849 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1850}
1851
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001852TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001853 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001854 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001855 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001856 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001857 if (FTSInfo)
1858 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Douglas Gregor2db32322009-10-07 23:56:10 +00001860 MemberSpecializationInfo *MSInfo
1861 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1862 if (MSInfo)
1863 return MSInfo->getTemplateSpecializationKind();
1864
1865 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001866}
1867
Mike Stump1eb44332009-09-09 15:08:12 +00001868void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001869FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1870 SourceLocation PointOfInstantiation) {
1871 if (FunctionTemplateSpecializationInfo *FTSInfo
1872 = TemplateOrSpecialization.dyn_cast<
1873 FunctionTemplateSpecializationInfo*>()) {
1874 FTSInfo->setTemplateSpecializationKind(TSK);
1875 if (TSK != TSK_ExplicitSpecialization &&
1876 PointOfInstantiation.isValid() &&
1877 FTSInfo->getPointOfInstantiation().isInvalid())
1878 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1879 } else if (MemberSpecializationInfo *MSInfo
1880 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1881 MSInfo->setTemplateSpecializationKind(TSK);
1882 if (TSK != TSK_ExplicitSpecialization &&
1883 PointOfInstantiation.isValid() &&
1884 MSInfo->getPointOfInstantiation().isInvalid())
1885 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1886 } else
1887 assert(false && "Function cannot have a template specialization kind");
1888}
1889
1890SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001891 if (FunctionTemplateSpecializationInfo *FTSInfo
1892 = TemplateOrSpecialization.dyn_cast<
1893 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001894 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001895 else if (MemberSpecializationInfo *MSInfo
1896 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001897 return MSInfo->getPointOfInstantiation();
1898
1899 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001900}
1901
Douglas Gregor9f185072009-09-11 20:15:17 +00001902bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001903 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00001904 return true;
1905
1906 // If this function was instantiated from a member function of a
1907 // class template, check whether that member function was defined out-of-line.
1908 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1909 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001910 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001911 return Definition->isOutOfLine();
1912 }
1913
1914 // If this function was instantiated from a function template,
1915 // check whether that function template was defined out-of-line.
1916 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1917 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001918 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001919 return Definition->isOutOfLine();
1920 }
1921
1922 return false;
1923}
1924
Chris Lattner8a934232008-03-31 00:36:02 +00001925//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001926// FieldDecl Implementation
1927//===----------------------------------------------------------------------===//
1928
Jay Foad4ba2a172011-01-12 09:06:06 +00001929FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1930 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001931 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1932 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1933}
1934
1935bool FieldDecl::isAnonymousStructOrUnion() const {
1936 if (!isImplicit() || getDeclName())
1937 return false;
1938
1939 if (const RecordType *Record = getType()->getAs<RecordType>())
1940 return Record->getDecl()->isAnonymousStructOrUnion();
1941
1942 return false;
1943}
1944
John McCallba4f5d52011-01-20 07:57:12 +00001945unsigned FieldDecl::getFieldIndex() const {
1946 if (CachedFieldIndex) return CachedFieldIndex - 1;
1947
1948 unsigned index = 0;
1949 RecordDecl::field_iterator
1950 i = getParent()->field_begin(), e = getParent()->field_end();
1951 while (true) {
1952 assert(i != e && "failed to find field in parent!");
1953 if (*i == this)
1954 break;
1955
1956 ++i;
1957 ++index;
1958 }
1959
1960 CachedFieldIndex = index + 1;
1961 return index;
1962}
1963
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001964//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001965// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001966//===----------------------------------------------------------------------===//
1967
Douglas Gregor1693e152010-07-06 18:42:40 +00001968SourceLocation TagDecl::getOuterLocStart() const {
1969 return getTemplateOrInnerLocStart(this);
1970}
1971
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001972SourceRange TagDecl::getSourceRange() const {
1973 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001974 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001975}
1976
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001977TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001978 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001979}
1980
Douglas Gregor60e70642010-05-19 18:39:18 +00001981void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1982 TypedefDeclOrQualifier = TDD;
1983 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00001984 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001985 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00001986}
1987
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001988void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001989 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001990
1991 if (isa<CXXRecordDecl>(this)) {
1992 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1993 struct CXXRecordDecl::DefinitionData *Data =
1994 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001995 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1996 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001997 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001998}
1999
2000void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002001 assert((!isa<CXXRecordDecl>(this) ||
2002 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2003 "definition completed but not started");
2004
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002005 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002006 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002007
2008 if (ASTMutationListener *L = getASTMutationListener())
2009 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002010}
2011
Douglas Gregor952b0172010-02-11 01:04:33 +00002012TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002013 if (isDefinition())
2014 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002015 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2016 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002017
2018 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002019 R != REnd; ++R)
2020 if (R->isDefinition())
2021 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002023 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002024}
2025
John McCallb6217662010-03-15 10:12:16 +00002026void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
2027 SourceRange QualifierRange) {
2028 if (Qualifier) {
2029 // Make sure the extended qualifier info is allocated.
2030 if (!hasExtInfo())
2031 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2032 // Set qualifier info.
2033 getExtInfo()->NNS = Qualifier;
2034 getExtInfo()->NNSRange = QualifierRange;
2035 }
2036 else {
2037 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2038 assert(QualifierRange.isInvalid());
2039 if (hasExtInfo()) {
2040 getASTContext().Deallocate(getExtInfo());
2041 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2042 }
2043 }
2044}
2045
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002046//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002047// EnumDecl Implementation
2048//===----------------------------------------------------------------------===//
2049
2050EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2051 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002052 EnumDecl *PrevDecl, bool IsScoped,
2053 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002054 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002055 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002056 C.getTypeDeclType(Enum, PrevDecl);
2057 return Enum;
2058}
2059
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002060EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002061 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002062 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002063}
2064
Douglas Gregor838db382010-02-11 01:19:42 +00002065void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002066 QualType NewPromotionType,
2067 unsigned NumPositiveBits,
2068 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002069 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002070 if (!IntegerType)
2071 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002072 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002073 setNumPositiveBits(NumPositiveBits);
2074 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002075 TagDecl::completeDefinition();
2076}
2077
2078//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002079// RecordDecl Implementation
2080//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002081
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00002082RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002083 IdentifierInfo *Id, RecordDecl *PrevDecl,
2084 SourceLocation TKL)
2085 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00002086 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002087 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002088 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002089 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002090 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002091}
2092
Jay Foad4ba2a172011-01-12 09:06:06 +00002093RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002094 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00002095 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002097 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002098 C.getTypeDeclType(R, PrevDecl);
2099 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002100}
2101
Jay Foad4ba2a172011-01-12 09:06:06 +00002102RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002103 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2104 SourceLocation());
2105}
2106
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002107bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002108 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002109 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2110}
2111
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002112RecordDecl::field_iterator RecordDecl::field_begin() const {
2113 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2114 LoadFieldsFromExternalStorage();
2115
2116 return field_iterator(decl_iterator(FirstDecl));
2117}
2118
Douglas Gregorda2142f2011-02-19 18:51:44 +00002119/// completeDefinition - Notes that the definition of this type is now
2120/// complete.
2121void RecordDecl::completeDefinition() {
2122 assert(!isDefinition() && "Cannot redefine record!");
2123 TagDecl::completeDefinition();
2124}
2125
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002126void RecordDecl::LoadFieldsFromExternalStorage() const {
2127 ExternalASTSource *Source = getASTContext().getExternalSource();
2128 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2129
2130 // Notify that we have a RecordDecl doing some initialization.
2131 ExternalASTSource::Deserializing TheFields(Source);
2132
2133 llvm::SmallVector<Decl*, 64> Decls;
2134 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2135 return;
2136
2137#ifndef NDEBUG
2138 // Check that all decls we got were FieldDecls.
2139 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2140 assert(isa<FieldDecl>(Decls[i]));
2141#endif
2142
2143 LoadedFieldsFromExternalStorage = true;
2144
2145 if (Decls.empty())
2146 return;
2147
2148 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2149}
2150
Steve Naroff56ee6892008-10-08 17:01:13 +00002151//===----------------------------------------------------------------------===//
2152// BlockDecl Implementation
2153//===----------------------------------------------------------------------===//
2154
Douglas Gregor838db382010-02-11 01:19:42 +00002155void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002156 unsigned NParms) {
2157 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Steve Naroffe78b8092009-03-13 16:56:44 +00002159 // Zero params -> null pointer.
2160 if (NParms) {
2161 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002162 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002163 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2164 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2165 }
2166}
2167
John McCall6b5a61b2011-02-07 10:33:21 +00002168void BlockDecl::setCaptures(ASTContext &Context,
2169 const Capture *begin,
2170 const Capture *end,
2171 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002172 CapturesCXXThis = capturesCXXThis;
2173
2174 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002175 NumCaptures = 0;
2176 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002177 return;
2178 }
2179
John McCall6b5a61b2011-02-07 10:33:21 +00002180 NumCaptures = end - begin;
2181
2182 // Avoid new Capture[] because we don't want to provide a default
2183 // constructor.
2184 size_t allocationSize = NumCaptures * sizeof(Capture);
2185 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2186 memcpy(buffer, begin, allocationSize);
2187 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002188}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002189
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002190SourceRange BlockDecl::getSourceRange() const {
2191 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2192}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002193
2194//===----------------------------------------------------------------------===//
2195// Other Decl Allocation/Deallocation Method Implementations
2196//===----------------------------------------------------------------------===//
2197
2198TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2199 return new (C) TranslationUnitDecl(C);
2200}
2201
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002202LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2203 SourceLocation L, IdentifierInfo *II) {
2204 return new (C) LabelDecl(DC, L, II, 0);
2205}
2206
2207
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002208NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2209 SourceLocation L, IdentifierInfo *Id) {
2210 return new (C) NamespaceDecl(DC, L, Id);
2211}
2212
Douglas Gregor06c91932010-10-27 19:49:05 +00002213NamespaceDecl *NamespaceDecl::getNextNamespace() {
2214 return dyn_cast_or_null<NamespaceDecl>(
2215 NextNamespace.get(getASTContext().getExternalSource()));
2216}
2217
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002218ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
John McCall201e5192011-02-22 22:25:56 +00002219 SourceLocation loc,
2220 IdentifierInfo *name,
2221 QualType type) {
2222 return new (C) ImplicitParamDecl(DC, loc, name, type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002223}
2224
2225FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002226 const DeclarationNameInfo &NameInfo,
2227 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002228 StorageClass S, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002229 bool isInlineSpecified,
2230 bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002231 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor8f150942010-12-09 16:59:22 +00002232 S, SCAsWritten, isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002233 New->HasWrittenPrototype = hasWrittenPrototype;
2234 return New;
2235}
2236
2237BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2238 return new (C) BlockDecl(DC, L);
2239}
2240
2241EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2242 SourceLocation L,
2243 IdentifierInfo *Id, QualType T,
2244 Expr *E, const llvm::APSInt &V) {
2245 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2246}
2247
Benjamin Kramerd9811462010-11-21 14:11:41 +00002248IndirectFieldDecl *
2249IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2250 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2251 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002252 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2253}
2254
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002255SourceRange EnumConstantDecl::getSourceRange() const {
2256 SourceLocation End = getLocation();
2257 if (Init)
2258 End = Init->getLocEnd();
2259 return SourceRange(getLocation(), End);
2260}
2261
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002262TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2263 SourceLocation L, IdentifierInfo *Id,
2264 TypeSourceInfo *TInfo) {
2265 return new (C) TypedefDecl(DC, L, Id, TInfo);
2266}
2267
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002268FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2269 SourceLocation L,
2270 StringLiteral *Str) {
2271 return new (C) FileScopeAsmDecl(DC, L, Str);
2272}