blob: 0b07d13cba309509b3abefb6951368677c1f13b7 [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
John McCall1fb0caa2010-10-22 21:05:15 +0000269 if (D->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000270 return LinkageInfo::uniqueExternal();
John McCalle7bc9722010-10-28 04:18:25 +0000271
John McCall1fb0caa2010-10-22 21:05:15 +0000272 // Set up the defaults.
273
274 // C99 6.2.2p5:
275 // If the declaration of an identifier for an object has file
276 // scope and no storage-class specifier, its linkage is
277 // external.
John McCallaf146032010-10-30 11:50:40 +0000278 LinkageInfo LV;
279
John McCall36987482010-11-02 01:45:15 +0000280 if (F.ConsiderVisibilityAttributes) {
281 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
282 LV.setVisibility(GetVisibilityFromAttr(VA), true);
283 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000284 } else {
285 // If we're declared in a namespace with a visibility attribute,
286 // use that namespace's visibility, but don't call it explicit.
287 for (const DeclContext *DC = D->getDeclContext();
288 !isa<TranslationUnitDecl>(DC);
289 DC = DC->getParent()) {
290 if (!isa<NamespaceDecl>(DC)) continue;
291 if (const VisibilityAttr *VA =
292 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
293 LV.setVisibility(GetVisibilityFromAttr(VA), false);
294 F.ConsiderGlobalVisibility = false;
295 break;
296 }
297 }
John McCall36987482010-11-02 01:45:15 +0000298 }
John McCallaf146032010-10-30 11:50:40 +0000299 }
John McCall1fb0caa2010-10-22 21:05:15 +0000300
Douglas Gregord85b5b92009-11-25 22:24:25 +0000301 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000302
Douglas Gregord85b5b92009-11-25 22:24:25 +0000303 // A name having namespace scope has external linkage if it is the
304 // name of
305 //
306 // - an object or reference, unless it has internal linkage; or
307 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000308 // GCC applies the following optimization to variables and static
309 // data members, but not to functions:
310 //
John McCall1fb0caa2010-10-22 21:05:15 +0000311 // Modify the variable's LV by the LV of its type unless this is
312 // C or extern "C". This follows from [basic.link]p9:
313 // A type without linkage shall not be used as the type of a
314 // variable or function with external linkage unless
315 // - the entity has C language linkage, or
316 // - the entity is declared within an unnamed namespace, or
317 // - the entity is not used or is defined in the same
318 // translation unit.
319 // and [basic.link]p10:
320 // ...the types specified by all declarations referring to a
321 // given variable or function shall be identical...
322 // C does not have an equivalent rule.
323 //
John McCallac65c622010-10-26 04:59:26 +0000324 // Ignore this if we've got an explicit attribute; the user
325 // probably knows what they're doing.
326 //
John McCall1fb0caa2010-10-22 21:05:15 +0000327 // Note that we don't want to make the variable non-external
328 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000329 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000330 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
331 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000332 return LinkageInfo::uniqueExternal();
333 if (!LV.visibilityExplicit())
334 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000335 }
336
John McCall35cebc32010-11-02 18:38:13 +0000337 if (Var->getStorageClass() == SC_PrivateExtern)
338 LV.setVisibility(HiddenVisibility, true);
339
Douglas Gregord85b5b92009-11-25 22:24:25 +0000340 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000341 (Var->getStorageClass() == SC_Extern ||
342 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000343
Douglas Gregord85b5b92009-11-25 22:24:25 +0000344 // C99 6.2.2p4:
345 // For an identifier declared with the storage-class specifier
346 // extern in a scope in which a prior declaration of that
347 // identifier is visible, if the prior declaration specifies
348 // internal or external linkage, the linkage of the identifier
349 // at the later declaration is the same as the linkage
350 // specified at the prior declaration. If no prior declaration
351 // is visible, or if the prior declaration specifies no
352 // linkage, then the identifier has external linkage.
353 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000354 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000355 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
356 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000357 }
358 }
359
Douglas Gregord85b5b92009-11-25 22:24:25 +0000360 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000361 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000362 // In theory, we can modify the function's LV by the LV of its
363 // type unless it has C linkage (see comment above about variables
364 // for justification). In practice, GCC doesn't do this, so it's
365 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000366
John McCall35cebc32010-11-02 18:38:13 +0000367 if (Function->getStorageClass() == SC_PrivateExtern)
368 LV.setVisibility(HiddenVisibility, true);
369
Douglas Gregord85b5b92009-11-25 22:24:25 +0000370 // C99 6.2.2p5:
371 // If the declaration of an identifier for a function has no
372 // storage-class specifier, its linkage is determined exactly
373 // as if it were declared with the storage-class specifier
374 // extern.
375 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000376 (Function->getStorageClass() == SC_Extern ||
377 Function->getStorageClass() == SC_PrivateExtern ||
378 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000379 // C99 6.2.2p4:
380 // For an identifier declared with the storage-class specifier
381 // extern in a scope in which a prior declaration of that
382 // identifier is visible, if the prior declaration specifies
383 // internal or external linkage, the linkage of the identifier
384 // at the later declaration is the same as the linkage
385 // specified at the prior declaration. If no prior declaration
386 // is visible, or if the prior declaration specifies no
387 // linkage, then the identifier has external linkage.
388 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000389 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000390 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
391 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000392 }
393 }
394
John McCallaf8ca372011-02-10 06:50:24 +0000395 // In C++, then if the type of the function uses a type with
396 // unique-external linkage, it's not legally usable from outside
397 // this translation unit. However, we should use the C linkage
398 // rules instead for extern "C" declarations.
399 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
400 Function->getType()->getLinkage() == UniqueExternalLinkage)
401 return LinkageInfo::uniqueExternal();
402
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000403 if (FunctionTemplateSpecializationInfo *SpecInfo
404 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000405 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
406 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000407 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000408 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000409 }
410
Douglas Gregord85b5b92009-11-25 22:24:25 +0000411 // - a named class (Clause 9), or an unnamed class defined in a
412 // typedef declaration in which the class has the typedef name
413 // for linkage purposes (7.1.3); or
414 // - a named enumeration (7.2), or an unnamed enumeration
415 // defined in a typedef declaration in which the enumeration
416 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000417 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
418 // Unnamed tags have no linkage.
419 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000420 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000421
John McCall1fb0caa2010-10-22 21:05:15 +0000422 // If this is a class template specialization, consider the
423 // linkage of the template and template arguments.
424 if (const ClassTemplateSpecializationDecl *Spec
425 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000426 // From the template.
427 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
428 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000429
John McCall1fb0caa2010-10-22 21:05:15 +0000430 // The arguments at which the template was instantiated.
431 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000432 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000433 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000434
John McCallac65c622010-10-26 04:59:26 +0000435 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000436 if (F.ConsiderGlobalVisibility)
437 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000438 (Context.getLangOptions().CPlusPlus &&
439 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000440
Douglas Gregord85b5b92009-11-25 22:24:25 +0000441 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000442 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000443 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000444 if (!isExternalLinkage(EnumLV.linkage()))
445 return LinkageInfo::none();
446 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000447
448 // - a template, unless it is a function template that has
449 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000450 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000451 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000452
Douglas Gregord85b5b92009-11-25 22:24:25 +0000453 // - a namespace (7.3), unless it is declared within an unnamed
454 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000455 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
456 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000457
John McCall1fb0caa2010-10-22 21:05:15 +0000458 // By extension, we assign external linkage to Objective-C
459 // interfaces.
460 } else if (isa<ObjCInterfaceDecl>(D)) {
461 // fallout
462
463 // Everything not covered here has no linkage.
464 } else {
John McCallaf146032010-10-30 11:50:40 +0000465 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000466 }
467
468 // If we ended up with non-external linkage, visibility should
469 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000470 if (LV.linkage() != ExternalLinkage)
471 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000472
473 // If we didn't end up with hidden visibility, consider attributes
474 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000475 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000476 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000477
478 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000479}
480
John McCall36987482010-11-02 01:45:15 +0000481static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000482 // Only certain class members have linkage. Note that fields don't
483 // really have linkage, but it's convenient to say they do for the
484 // purposes of calculating linkage of pointer-to-data-member
485 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000486 if (!(isa<CXXMethodDecl>(D) ||
487 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000488 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000489 (isa<TagDecl>(D) &&
490 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000491 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000492
John McCall36987482010-11-02 01:45:15 +0000493 LinkageInfo LV;
494
495 // The flags we're going to use to compute the class's visibility.
496 LVFlags ClassF = F;
497
498 // If we have an explicit visibility attribute, merge that in.
499 if (F.ConsiderVisibilityAttributes) {
500 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
501 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
502
503 // Ignore global visibility later, but not this attribute.
504 F.ConsiderGlobalVisibility = false;
505
506 // Ignore both global visibility and attributes when computing our
507 // parent's visibility.
508 ClassF = F.onlyTemplateVisibility();
509 }
510 }
John McCallaf146032010-10-30 11:50:40 +0000511
512 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000513 // linkage.
514 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
515 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000516 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000517
518 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000519 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000520 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000521
John McCall3cdfc4d2010-08-13 08:35:10 +0000522 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000523 // If the type of the function uses a type with unique-external
524 // linkage, it's not legally usable from outside this translation unit.
525 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
526 return LinkageInfo::uniqueExternal();
527
John McCall110e8e52010-10-29 22:22:43 +0000528 TemplateSpecializationKind TSK = TSK_Undeclared;
529
John McCall1fb0caa2010-10-22 21:05:15 +0000530 // If this is a method template specialization, use the linkage for
531 // the template parameters and arguments.
532 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000533 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000534 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallaf146032010-10-30 11:50:40 +0000535 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000536 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000537
538 TSK = Spec->getTemplateSpecializationKind();
539 } else if (MemberSpecializationInfo *MSI =
540 MD->getMemberSpecializationInfo()) {
541 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000542 }
543
John McCall110e8e52010-10-29 22:22:43 +0000544 // If we're paying attention to global visibility, apply
545 // -finline-visibility-hidden if this is an inline method.
546 //
John McCallaf146032010-10-30 11:50:40 +0000547 // Note that ConsiderGlobalVisibility doesn't yet have information
548 // about whether containing classes have visibility attributes,
549 // and that's intentional.
550 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000551 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000552 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
553 // InlineVisibilityHidden only applies to definitions, and
554 // isInlined() only gives meaningful answers on definitions
555 // anyway.
556 const FunctionDecl *Def = 0;
557 if (MD->hasBody(Def) && Def->isInlined())
558 LV.setVisibility(HiddenVisibility);
559 }
John McCall1fb0caa2010-10-22 21:05:15 +0000560
John McCall110e8e52010-10-29 22:22:43 +0000561 // Note that in contrast to basically every other situation, we
562 // *do* apply -fvisibility to method declarations.
563
564 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000565 if (const ClassTemplateSpecializationDecl *Spec
566 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
567 // Merge template argument/parameter information for member
568 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000569 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallaf146032010-10-30 11:50:40 +0000570 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000571 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000572 }
573
John McCall110e8e52010-10-29 22:22:43 +0000574 // Static data members.
575 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000576 // Modify the variable's linkage by its type, but ignore the
577 // type's visibility unless it's a definition.
578 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
579 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000580 LV.mergeLinkage(UniqueExternalLinkage);
581 if (!LV.visibilityExplicit())
582 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000583 }
584
John McCall36987482010-11-02 01:45:15 +0000585 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000586
587 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000588 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000589 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000590 }
591
John McCall1fb0caa2010-10-22 21:05:15 +0000592 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000593}
594
John McCallf76b0922011-02-08 19:01:05 +0000595static void clearLinkageForClass(const CXXRecordDecl *record) {
596 for (CXXRecordDecl::decl_iterator
597 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
598 Decl *child = *i;
599 if (isa<NamedDecl>(child))
600 cast<NamedDecl>(child)->ClearLinkageCache();
601 }
602}
603
Douglas Gregorebe5a9b2011-02-17 17:23:19 +0000604void NamedDecl::getNameForDiagnostic(std::string &S,
605 const PrintingPolicy &Policy,
606 bool Qualified) const {
607 if (Qualified)
608 S += getQualifiedNameAsString(Policy);
609 else
610 S += getNameAsString();
611
612 const TemplateArgumentList *TemplateArgs = 0;
613
614 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
615 TemplateArgs = FD->getTemplateSpecializationArgs();
616 else if (const ClassTemplateSpecializationDecl *Spec
617 = dyn_cast<ClassTemplateSpecializationDecl>(this))
618 TemplateArgs = &Spec->getTemplateArgs();
619
620
621 if (TemplateArgs)
622 S += TemplateSpecializationType::PrintTemplateArgumentList(
623 TemplateArgs->data(),
624 TemplateArgs->size(),
625 Policy);
626}
627
John McCallf76b0922011-02-08 19:01:05 +0000628void NamedDecl::ClearLinkageCache() {
629 // Note that we can't skip clearing the linkage of children just
630 // because the parent doesn't have cached linkage: we don't cache
631 // when computing linkage for parent contexts.
632
633 HasCachedLinkage = 0;
634
635 // If we're changing the linkage of a class, we need to reset the
636 // linkage of child declarations, too.
637 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
638 clearLinkageForClass(record);
639
640 if (const ClassTemplateDecl *temp = dyn_cast<ClassTemplateDecl>(this)) {
641 // Clear linkage for the template pattern.
642 CXXRecordDecl *record = temp->getTemplatedDecl();
643 record->HasCachedLinkage = 0;
644 clearLinkageForClass(record);
645
646 // ...do we need to clear linkage for specializations, too?
647 }
648}
649
Douglas Gregor381d34e2010-12-06 18:36:25 +0000650Linkage NamedDecl::getLinkage() const {
651 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000652 assert(Linkage(CachedLinkage) ==
653 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000654 return Linkage(CachedLinkage);
655 }
656
657 CachedLinkage = getLVForDecl(this,
658 LVFlags::CreateOnlyDeclLinkage()).linkage();
659 HasCachedLinkage = 1;
660 return Linkage(CachedLinkage);
661}
662
John McCallaf146032010-10-30 11:50:40 +0000663LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000664 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000665 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000666 HasCachedLinkage = 1;
667 CachedLinkage = LI.linkage();
668 return LI;
John McCall0df95872010-10-29 00:29:13 +0000669}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000670
John McCall36987482010-11-02 01:45:15 +0000671static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000672 // Objective-C: treat all Objective-C declarations as having external
673 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000674 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000675 default:
676 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000677 case Decl::TemplateTemplateParm: // count these as external
678 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000679 case Decl::ObjCAtDefsField:
680 case Decl::ObjCCategory:
681 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000682 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000683 case Decl::ObjCForwardProtocol:
684 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000685 case Decl::ObjCMethod:
686 case Decl::ObjCProperty:
687 case Decl::ObjCPropertyImpl:
688 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000689 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000690 }
691
Douglas Gregord85b5b92009-11-25 22:24:25 +0000692 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000693 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000694 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000695
696 // C++ [basic.link]p5:
697 // In addition, a member function, static data member, a named
698 // class or enumeration of class scope, or an unnamed class or
699 // enumeration defined in a class-scope typedef declaration such
700 // that the class or enumeration has the typedef name for linkage
701 // purposes (7.1.3), has external linkage if the name of the class
702 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000703 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000704 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000705
706 // C++ [basic.link]p6:
707 // The name of a function declared in block scope and the name of
708 // an object declared by a block scope extern declaration have
709 // linkage. If there is a visible declaration of an entity with
710 // linkage having the same name and type, ignoring entities
711 // declared outside the innermost enclosing namespace scope, the
712 // block scope declaration declares that same entity and receives
713 // the linkage of the previous declaration. If there is more than
714 // one such matching entity, the program is ill-formed. Otherwise,
715 // if no matching entity is found, the block scope entity receives
716 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000717 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
718 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000719 if (Function->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000720 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000721
John McCallaf146032010-10-30 11:50:40 +0000722 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000723 if (Flags.ConsiderVisibilityAttributes) {
724 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
725 LV.setVisibility(GetVisibilityFromAttr(VA));
726 }
727
John McCall1fb0caa2010-10-22 21:05:15 +0000728 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000729 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000730 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
731 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000732 }
733
734 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000735 }
736
John McCall0df95872010-10-29 00:29:13 +0000737 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000738 if (Var->getStorageClass() == SC_Extern ||
739 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000740 if (Var->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000741 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000742
John McCallaf146032010-10-30 11:50:40 +0000743 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000744 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000745 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000746 else if (Flags.ConsiderVisibilityAttributes) {
747 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
748 LV.setVisibility(GetVisibilityFromAttr(VA));
749 }
750
John McCall1fb0caa2010-10-22 21:05:15 +0000751 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000752 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000753 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
754 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000755 }
756
757 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000758 }
759 }
760
761 // C++ [basic.link]p6:
762 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000763 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000764}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000765
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000766std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000767 return getQualifiedNameAsString(getASTContext().getLangOptions());
768}
769
770std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000771 const DeclContext *Ctx = getDeclContext();
772
773 if (Ctx->isFunctionOrMethod())
774 return getNameAsString();
775
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000776 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
777 ContextsTy Contexts;
778
779 // Collect contexts.
780 while (Ctx && isa<NamedDecl>(Ctx)) {
781 Contexts.push_back(Ctx);
782 Ctx = Ctx->getParent();
783 };
784
785 std::string QualName;
786 llvm::raw_string_ostream OS(QualName);
787
788 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
789 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000790 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000791 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000792 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
793 std::string TemplateArgsStr
794 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000795 TemplateArgs.data(),
796 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000797 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000798 OS << Spec->getName() << TemplateArgsStr;
799 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000800 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000801 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000802 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000803 OS << ND;
804 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
805 if (!RD->getIdentifier())
806 OS << "<anonymous " << RD->getKindName() << '>';
807 else
808 OS << RD;
809 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000810 const FunctionProtoType *FT = 0;
811 if (FD->hasWrittenPrototype())
812 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
813
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000814 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000815 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000816 unsigned NumParams = FD->getNumParams();
817 for (unsigned i = 0; i < NumParams; ++i) {
818 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000819 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000820 std::string Param;
821 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000822 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000823 }
824
825 if (FT->isVariadic()) {
826 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000827 OS << ", ";
828 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000829 }
830 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000831 OS << ')';
832 } else {
833 OS << cast<NamedDecl>(*I);
834 }
835 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000836 }
837
John McCall8472af42010-03-16 21:48:18 +0000838 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000839 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000840 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000841 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000842
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000843 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000844}
845
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000846bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000847 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
848
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000849 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
850 // We want to keep it, unless it nominates same namespace.
851 if (getKind() == Decl::UsingDirective) {
852 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
853 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000856 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
857 // For function declarations, we keep track of redeclarations.
858 return FD->getPreviousDeclaration() == OldD;
859
Douglas Gregore53060f2009-06-25 22:08:12 +0000860 // For function templates, the underlying function declarations are linked.
861 if (const FunctionTemplateDecl *FunctionTemplate
862 = dyn_cast<FunctionTemplateDecl>(this))
863 if (const FunctionTemplateDecl *OldFunctionTemplate
864 = dyn_cast<FunctionTemplateDecl>(OldD))
865 return FunctionTemplate->getTemplatedDecl()
866 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Steve Naroff0de21fd2009-02-22 19:35:57 +0000868 // For method declarations, we keep track of redeclarations.
869 if (isa<ObjCMethodDecl>(this))
870 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000871
John McCallf36e02d2009-10-09 21:13:30 +0000872 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
873 return true;
874
John McCall9488ea12009-11-17 05:59:44 +0000875 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
876 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
877 cast<UsingShadowDecl>(OldD)->getTargetDecl();
878
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000879 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
880 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
881 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
882
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000883 // For non-function declarations, if the declarations are of the
884 // same kind then this must be a redeclaration, or semantic analysis
885 // would not have given us the new declaration.
886 return this->getKind() == OldD->getKind();
887}
888
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000889bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000890 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000891}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000892
Anders Carlssone136e0e2009-06-26 06:29:23 +0000893NamedDecl *NamedDecl::getUnderlyingDecl() {
894 NamedDecl *ND = this;
895 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000896 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000897 ND = UD->getTargetDecl();
898 else if (ObjCCompatibleAliasDecl *AD
899 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
900 return AD->getClassInterface();
901 else
902 return ND;
903 }
904}
905
John McCall161755a2010-04-06 21:38:20 +0000906bool NamedDecl::isCXXInstanceMember() const {
907 assert(isCXXClassMember() &&
908 "checking whether non-member is instance member");
909
910 const NamedDecl *D = this;
911 if (isa<UsingShadowDecl>(D))
912 D = cast<UsingShadowDecl>(D)->getTargetDecl();
913
Francois Pichet87c2e122010-11-21 06:08:52 +0000914 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000915 return true;
916 if (isa<CXXMethodDecl>(D))
917 return cast<CXXMethodDecl>(D)->isInstance();
918 if (isa<FunctionTemplateDecl>(D))
919 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
920 ->getTemplatedDecl())->isInstance();
921 return false;
922}
923
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000924//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000925// DeclaratorDecl Implementation
926//===----------------------------------------------------------------------===//
927
Douglas Gregor1693e152010-07-06 18:42:40 +0000928template <typename DeclT>
929static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
930 if (decl->getNumTemplateParameterLists() > 0)
931 return decl->getTemplateParameterList(0)->getTemplateLoc();
932 else
933 return decl->getInnerLocStart();
934}
935
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000936SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000937 TypeSourceInfo *TSI = getTypeSourceInfo();
938 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000939 return SourceLocation();
940}
941
John McCallb6217662010-03-15 10:12:16 +0000942void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
943 SourceRange QualifierRange) {
944 if (Qualifier) {
945 // Make sure the extended decl info is allocated.
946 if (!hasExtInfo()) {
947 // Save (non-extended) type source info pointer.
948 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
949 // Allocate external info struct.
950 DeclInfo = new (getASTContext()) ExtInfo;
951 // Restore savedTInfo into (extended) decl info.
952 getExtInfo()->TInfo = savedTInfo;
953 }
954 // Set qualifier info.
955 getExtInfo()->NNS = Qualifier;
956 getExtInfo()->NNSRange = QualifierRange;
957 }
958 else {
959 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
960 assert(QualifierRange.isInvalid());
961 if (hasExtInfo()) {
962 // Save type source info pointer.
963 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
964 // Deallocate the extended decl info.
965 getASTContext().Deallocate(getExtInfo());
966 // Restore savedTInfo into (non-extended) decl info.
967 DeclInfo = savedTInfo;
968 }
969 }
970}
971
Douglas Gregor1693e152010-07-06 18:42:40 +0000972SourceLocation DeclaratorDecl::getOuterLocStart() const {
973 return getTemplateOrInnerLocStart(this);
974}
975
Abramo Bagnara9b934882010-06-12 08:15:14 +0000976void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000977QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
978 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000979 TemplateParameterList **TPLists) {
980 assert((NumTPLists == 0 || TPLists != 0) &&
981 "Empty array of template parameters with positive size!");
982 assert((NumTPLists == 0 || NNS) &&
983 "Nonempty array of template parameters with no qualifier!");
984
985 // Free previous template parameters (if any).
986 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000987 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000988 TemplParamLists = 0;
989 NumTemplParamLists = 0;
990 }
991 // Set info on matched template parameter lists (if any).
992 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000993 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000994 NumTemplParamLists = NumTPLists;
995 for (unsigned i = NumTPLists; i-- > 0; )
996 TemplParamLists[i] = TPLists[i];
997 }
998}
999
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001000//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001001// VarDecl Implementation
1002//===----------------------------------------------------------------------===//
1003
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001004const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1005 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +00001006 case SC_None: break;
1007 case SC_Auto: return "auto"; break;
1008 case SC_Extern: return "extern"; break;
1009 case SC_PrivateExtern: return "__private_extern__"; break;
1010 case SC_Register: return "register"; break;
1011 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001012 }
1013
1014 assert(0 && "Invalid storage class");
1015 return 0;
1016}
1017
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001018VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +00001019 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001020 StorageClass S, StorageClass SCAsWritten) {
1021 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001022}
1023
Douglas Gregor381d34e2010-12-06 18:36:25 +00001024void VarDecl::setStorageClass(StorageClass SC) {
1025 assert(isLegalForVariable(SC));
1026 if (getStorageClass() != SC)
1027 ClearLinkageCache();
1028
1029 SClass = SC;
1030}
1031
Douglas Gregor1693e152010-07-06 18:42:40 +00001032SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001033 SourceLocation Start = getTypeSpecStartLoc();
1034 if (Start.isInvalid())
1035 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001036 return Start;
1037}
1038
1039SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001040 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001041 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1042 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001043}
1044
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001045bool VarDecl::isExternC() const {
1046 ASTContext &Context = getASTContext();
1047 if (!Context.getLangOptions().CPlusPlus)
1048 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +00001049 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001050 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1051
1052 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
1053 DC = DC->getParent()) {
1054 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1055 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001056 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001057
1058 break;
1059 }
1060
1061 if (DC->isFunctionOrMethod())
1062 return false;
1063 }
1064
1065 return false;
1066}
1067
1068VarDecl *VarDecl::getCanonicalDecl() {
1069 return getFirstDeclaration();
1070}
1071
Sebastian Redle9d12b62010-01-31 22:27:38 +00001072VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1073 // C++ [basic.def]p2:
1074 // A declaration is a definition unless [...] it contains the 'extern'
1075 // specifier or a linkage-specification and neither an initializer [...],
1076 // it declares a static data member in a class declaration [...].
1077 // C++ [temp.expl.spec]p15:
1078 // An explicit specialization of a static data member of a template is a
1079 // definition if the declaration includes an initializer; otherwise, it is
1080 // a declaration.
1081 if (isStaticDataMember()) {
1082 if (isOutOfLine() && (hasInit() ||
1083 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1084 return Definition;
1085 else
1086 return DeclarationOnly;
1087 }
1088 // C99 6.7p5:
1089 // A definition of an identifier is a declaration for that identifier that
1090 // [...] causes storage to be reserved for that object.
1091 // Note: that applies for all non-file-scope objects.
1092 // C99 6.9.2p1:
1093 // If the declaration of an identifier for an object has file scope and an
1094 // initializer, the declaration is an external definition for the identifier
1095 if (hasInit())
1096 return Definition;
1097 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1098 if (hasExternalStorage())
1099 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001100
John McCalld931b082010-08-26 03:08:43 +00001101 if (getStorageClassAsWritten() == SC_Extern ||
1102 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001103 for (const VarDecl *PrevVar = getPreviousDeclaration();
1104 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1105 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1106 return DeclarationOnly;
1107 }
1108 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001109 // C99 6.9.2p2:
1110 // A declaration of an object that has file scope without an initializer,
1111 // and without a storage class specifier or the scs 'static', constitutes
1112 // a tentative definition.
1113 // No such thing in C++.
1114 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1115 return TentativeDefinition;
1116
1117 // What's left is (in C, block-scope) declarations without initializers or
1118 // external storage. These are definitions.
1119 return Definition;
1120}
1121
Sebastian Redle9d12b62010-01-31 22:27:38 +00001122VarDecl *VarDecl::getActingDefinition() {
1123 DefinitionKind Kind = isThisDeclarationADefinition();
1124 if (Kind != TentativeDefinition)
1125 return 0;
1126
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001127 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001128 VarDecl *First = getFirstDeclaration();
1129 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1130 I != E; ++I) {
1131 Kind = (*I)->isThisDeclarationADefinition();
1132 if (Kind == Definition)
1133 return 0;
1134 else if (Kind == TentativeDefinition)
1135 LastTentative = *I;
1136 }
1137 return LastTentative;
1138}
1139
1140bool VarDecl::isTentativeDefinitionNow() const {
1141 DefinitionKind Kind = isThisDeclarationADefinition();
1142 if (Kind != TentativeDefinition)
1143 return false;
1144
1145 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1146 if ((*I)->isThisDeclarationADefinition() == Definition)
1147 return false;
1148 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001149 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001150}
1151
Sebastian Redl31310a22010-02-01 20:16:42 +00001152VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001153 VarDecl *First = getFirstDeclaration();
1154 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1155 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001156 if ((*I)->isThisDeclarationADefinition() == Definition)
1157 return *I;
1158 }
1159 return 0;
1160}
1161
John McCall110e8e52010-10-29 22:22:43 +00001162VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1163 DefinitionKind Kind = DeclarationOnly;
1164
1165 const VarDecl *First = getFirstDeclaration();
1166 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1167 I != E; ++I)
1168 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1169
1170 return Kind;
1171}
1172
Sebastian Redl31310a22010-02-01 20:16:42 +00001173const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001174 redecl_iterator I = redecls_begin(), E = redecls_end();
1175 while (I != E && !I->getInit())
1176 ++I;
1177
1178 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001179 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001180 return I->getInit();
1181 }
1182 return 0;
1183}
1184
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001185bool VarDecl::isOutOfLine() const {
Douglas Gregorf4a03cc2011-02-17 07:02:32 +00001186 if (getLexicalDeclContext() != getDeclContext())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001187 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001188
1189 if (!isStaticDataMember())
1190 return false;
1191
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001192 // If this static data member was instantiated from a static data member of
1193 // a class template, check whether that static data member was defined
1194 // out-of-line.
1195 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1196 return VD->isOutOfLine();
1197
1198 return false;
1199}
1200
Douglas Gregor0d035142009-10-27 18:42:08 +00001201VarDecl *VarDecl::getOutOfLineDefinition() {
1202 if (!isStaticDataMember())
1203 return 0;
1204
1205 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1206 RD != RDEnd; ++RD) {
1207 if (RD->getLexicalDeclContext()->isFileContext())
1208 return *RD;
1209 }
1210
1211 return 0;
1212}
1213
Douglas Gregor838db382010-02-11 01:19:42 +00001214void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001215 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1216 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001217 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001218 }
1219
1220 Init = I;
1221}
1222
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001223VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001224 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001225 return cast<VarDecl>(MSI->getInstantiatedFrom());
1226
1227 return 0;
1228}
1229
Douglas Gregor663b5a02009-10-14 20:14:33 +00001230TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001231 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001232 return MSI->getTemplateSpecializationKind();
1233
1234 return TSK_Undeclared;
1235}
1236
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001237MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001238 return getASTContext().getInstantiatedFromStaticDataMember(this);
1239}
1240
Douglas Gregor0a897e32009-10-15 17:21:20 +00001241void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1242 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001243 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001244 assert(MSI && "Not an instantiated static data member?");
1245 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001246 if (TSK != TSK_ExplicitSpecialization &&
1247 PointOfInstantiation.isValid() &&
1248 MSI->getPointOfInstantiation().isInvalid())
1249 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001250}
1251
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001252//===----------------------------------------------------------------------===//
1253// ParmVarDecl Implementation
1254//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001255
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001256ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1257 SourceLocation L, IdentifierInfo *Id,
1258 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001259 StorageClass S, StorageClass SCAsWritten,
1260 Expr *DefArg) {
1261 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1262 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001263}
1264
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001265Expr *ParmVarDecl::getDefaultArg() {
1266 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1267 assert(!hasUninstantiatedDefaultArg() &&
1268 "Default argument is not yet instantiated!");
1269
1270 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001271 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001272 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001273
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001274 return Arg;
1275}
1276
1277unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001278 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001279 return E->getNumTemporaries();
1280
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001281 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001282}
1283
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001284CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1285 assert(getNumDefaultArgTemporaries() &&
1286 "Default arguments does not have any temporaries!");
1287
John McCall4765fa02010-12-06 08:20:24 +00001288 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001289 return E->getTemporary(i);
1290}
1291
1292SourceRange ParmVarDecl::getDefaultArgRange() const {
1293 if (const Expr *E = getInit())
1294 return E->getSourceRange();
1295
1296 if (hasUninstantiatedDefaultArg())
1297 return getUninstantiatedDefaultArg()->getSourceRange();
1298
1299 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001300}
1301
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001302bool ParmVarDecl::isParameterPack() const {
1303 return isa<PackExpansionType>(getType());
1304}
1305
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001306//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001307// FunctionDecl Implementation
1308//===----------------------------------------------------------------------===//
1309
Ted Kremenek9498d382010-04-29 16:49:01 +00001310bool FunctionDecl::isVariadic() const {
1311 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1312 return FT->isVariadic();
1313 return false;
1314}
1315
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001316bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1317 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1318 if (I->Body) {
1319 Definition = *I;
1320 return true;
1321 }
1322 }
1323
1324 return false;
1325}
1326
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001327Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001328 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1329 if (I->Body) {
1330 Definition = *I;
1331 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001332 }
1333 }
1334
1335 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001336}
1337
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001338void FunctionDecl::setBody(Stmt *B) {
1339 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001340 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001341 EndRangeLoc = B->getLocEnd();
1342}
1343
Douglas Gregor21386642010-09-28 21:55:22 +00001344void FunctionDecl::setPure(bool P) {
1345 IsPure = P;
1346 if (P)
1347 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1348 Parent->markedVirtualFunctionPure();
1349}
1350
Douglas Gregor48a83b52009-09-12 00:17:51 +00001351bool FunctionDecl::isMain() const {
1352 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001353 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001354 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001355 getIdentifier() && getIdentifier()->isStr("main");
1356}
1357
Douglas Gregor48a83b52009-09-12 00:17:51 +00001358bool FunctionDecl::isExternC() const {
1359 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001360 // In C, any non-static, non-overloadable function has external
1361 // linkage.
1362 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001363 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001364
Mike Stump1eb44332009-09-09 15:08:12 +00001365 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001366 DC = DC->getParent()) {
1367 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1368 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001369 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001370 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001371
1372 break;
1373 }
Douglas Gregor45975532010-08-17 16:09:23 +00001374
1375 if (DC->isRecord())
1376 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001377 }
1378
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001379 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001380}
1381
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001382bool FunctionDecl::isGlobal() const {
1383 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1384 return Method->isStatic();
1385
John McCalld931b082010-08-26 03:08:43 +00001386 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001387 return false;
1388
Mike Stump1eb44332009-09-09 15:08:12 +00001389 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001390 DC->isNamespace();
1391 DC = DC->getParent()) {
1392 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1393 if (!Namespace->getDeclName())
1394 return false;
1395 break;
1396 }
1397 }
1398
1399 return true;
1400}
1401
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001402void
1403FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1404 redeclarable_base::setPreviousDeclaration(PrevDecl);
1405
1406 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1407 FunctionTemplateDecl *PrevFunTmpl
1408 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1409 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1410 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1411 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001412
1413 if (PrevDecl->IsInline)
1414 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001415}
1416
1417const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1418 return getFirstDeclaration();
1419}
1420
1421FunctionDecl *FunctionDecl::getCanonicalDecl() {
1422 return getFirstDeclaration();
1423}
1424
Douglas Gregor381d34e2010-12-06 18:36:25 +00001425void FunctionDecl::setStorageClass(StorageClass SC) {
1426 assert(isLegalForFunction(SC));
1427 if (getStorageClass() != SC)
1428 ClearLinkageCache();
1429
1430 SClass = SC;
1431}
1432
Douglas Gregor3e41d602009-02-13 23:20:09 +00001433/// \brief Returns a value indicating whether this function
1434/// corresponds to a builtin function.
1435///
1436/// The function corresponds to a built-in function if it is
1437/// declared at translation scope or within an extern "C" block and
1438/// its name matches with the name of a builtin. The returned value
1439/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001440/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001441/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001442unsigned FunctionDecl::getBuiltinID() const {
1443 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001444 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1445 return 0;
1446
1447 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1448 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1449 return BuiltinID;
1450
1451 // This function has the name of a known C library
1452 // function. Determine whether it actually refers to the C library
1453 // function or whether it just has the same name.
1454
Douglas Gregor9add3172009-02-17 03:23:10 +00001455 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001456 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001457 return 0;
1458
Douglas Gregor3c385e52009-02-14 18:57:46 +00001459 // If this function is at translation-unit scope and we're not in
1460 // C++, it refers to the C library function.
1461 if (!Context.getLangOptions().CPlusPlus &&
1462 getDeclContext()->isTranslationUnit())
1463 return BuiltinID;
1464
1465 // If the function is in an extern "C" linkage specification and is
1466 // not marked "overloadable", it's the real function.
1467 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001468 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001469 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001470 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001471 return BuiltinID;
1472
1473 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001474 return 0;
1475}
1476
1477
Chris Lattner1ad9b282009-04-25 06:03:53 +00001478/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001479/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001480/// after it has been created.
1481unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001482 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001483 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001484 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001485 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Reid Spencer5f016e22007-07-11 17:01:13 +00001487}
1488
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001489void FunctionDecl::setParams(ASTContext &C,
1490 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001492 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 // Zero params -> null pointer.
1495 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001496 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001497 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001499
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001500 // Update source range. The check below allows us to set EndRangeLoc before
1501 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001502 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001503 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 }
1505}
1506
Chris Lattner8123a952008-04-10 02:22:51 +00001507/// getMinRequiredArguments - Returns the minimum number of arguments
1508/// needed to call this function. This may be fewer than the number of
1509/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001510/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001511unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001512 if (!getASTContext().getLangOptions().CPlusPlus)
1513 return getNumParams();
1514
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001515 unsigned NumRequiredArgs = getNumParams();
1516
1517 // If the last parameter is a parameter pack, we don't need an argument for
1518 // it.
1519 if (NumRequiredArgs > 0 &&
1520 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1521 --NumRequiredArgs;
1522
1523 // If this parameter has a default argument, we don't need an argument for
1524 // it.
1525 while (NumRequiredArgs > 0 &&
1526 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001527 --NumRequiredArgs;
1528
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001529 // We might have parameter packs before the end. These can't be deduced,
1530 // but they can still handle multiple arguments.
1531 unsigned ArgIdx = NumRequiredArgs;
1532 while (ArgIdx > 0) {
1533 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1534 NumRequiredArgs = ArgIdx;
1535
1536 --ArgIdx;
1537 }
1538
Chris Lattner8123a952008-04-10 02:22:51 +00001539 return NumRequiredArgs;
1540}
1541
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001542bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001543 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001544 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001545
1546 if (isa<CXXMethodDecl>(this)) {
1547 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1548 return true;
1549 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001550
1551 switch (getTemplateSpecializationKind()) {
1552 case TSK_Undeclared:
1553 case TSK_ExplicitSpecialization:
1554 return false;
1555
1556 case TSK_ImplicitInstantiation:
1557 case TSK_ExplicitInstantiationDeclaration:
1558 case TSK_ExplicitInstantiationDefinition:
1559 // Handle below.
1560 break;
1561 }
1562
1563 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001564 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001565 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001566 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001567
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001568 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001569 return PatternDecl->isInlined();
1570
1571 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001572}
1573
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001574/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001575/// definition will be externally visible.
1576///
1577/// Inline function definitions are always available for inlining optimizations.
1578/// However, depending on the language dialect, declaration specifiers, and
1579/// attributes, the definition of an inline function may or may not be
1580/// "externally" visible to other translation units in the program.
1581///
1582/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001583/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001584/// inline definition becomes externally visible (C99 6.7.4p6).
1585///
1586/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1587/// definition, we use the GNU semantics for inline, which are nearly the
1588/// opposite of C99 semantics. In particular, "inline" by itself will create
1589/// an externally visible symbol, but "extern inline" will not create an
1590/// externally visible symbol.
1591bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1592 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001593 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001594 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001595
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001596 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001597 // If it's not the case that both 'inline' and 'extern' are
1598 // specified on the definition, then this inline definition is
1599 // externally visible.
1600 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1601 return true;
1602
1603 // If any declaration is 'inline' but not 'extern', then this definition
1604 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001605 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1606 Redecl != RedeclEnd;
1607 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001608 if (Redecl->isInlineSpecified() &&
1609 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001610 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001611 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001612
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001613 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001614 }
1615
1616 // C99 6.7.4p6:
1617 // [...] If all of the file scope declarations for a function in a
1618 // translation unit include the inline function specifier without extern,
1619 // then the definition in that translation unit is an inline definition.
1620 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1621 Redecl != RedeclEnd;
1622 ++Redecl) {
1623 // Only consider file-scope declarations in this test.
1624 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1625 continue;
1626
John McCalld931b082010-08-26 03:08:43 +00001627 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001628 return true; // Not an inline definition
1629 }
1630
1631 // C99 6.7.4p6:
1632 // An inline definition does not provide an external definition for the
1633 // function, and does not forbid an external definition in another
1634 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001635 return false;
1636}
1637
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001638/// getOverloadedOperator - Which C++ overloaded operator this
1639/// function represents, if any.
1640OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001641 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1642 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001643 else
1644 return OO_None;
1645}
1646
Sean Hunta6c058d2010-01-13 09:01:02 +00001647/// getLiteralIdentifier - The literal suffix identifier this function
1648/// represents, if any.
1649const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1650 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1651 return getDeclName().getCXXLiteralIdentifier();
1652 else
1653 return 0;
1654}
1655
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001656FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1657 if (TemplateOrSpecialization.isNull())
1658 return TK_NonTemplate;
1659 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1660 return TK_FunctionTemplate;
1661 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1662 return TK_MemberSpecialization;
1663 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1664 return TK_FunctionTemplateSpecialization;
1665 if (TemplateOrSpecialization.is
1666 <DependentFunctionTemplateSpecializationInfo*>())
1667 return TK_DependentFunctionTemplateSpecialization;
1668
1669 assert(false && "Did we miss a TemplateOrSpecialization type?");
1670 return TK_NonTemplate;
1671}
1672
Douglas Gregor2db32322009-10-07 23:56:10 +00001673FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001674 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001675 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1676
1677 return 0;
1678}
1679
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001680MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1681 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1682}
1683
Douglas Gregor2db32322009-10-07 23:56:10 +00001684void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001685FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1686 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001687 TemplateSpecializationKind TSK) {
1688 assert(TemplateOrSpecialization.isNull() &&
1689 "Member function is already a specialization");
1690 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001691 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001692 TemplateOrSpecialization = Info;
1693}
1694
Douglas Gregor3b846b62009-10-27 20:53:28 +00001695bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001696 // If the function is invalid, it can't be implicitly instantiated.
1697 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001698 return false;
1699
1700 switch (getTemplateSpecializationKind()) {
1701 case TSK_Undeclared:
1702 case TSK_ExplicitSpecialization:
1703 case TSK_ExplicitInstantiationDefinition:
1704 return false;
1705
1706 case TSK_ImplicitInstantiation:
1707 return true;
1708
1709 case TSK_ExplicitInstantiationDeclaration:
1710 // Handled below.
1711 break;
1712 }
1713
1714 // Find the actual template from which we will instantiate.
1715 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001716 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001717 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001718 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001719
1720 // C++0x [temp.explicit]p9:
1721 // Except for inline functions, other explicit instantiation declarations
1722 // have the effect of suppressing the implicit instantiation of the entity
1723 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001724 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001725 return true;
1726
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001727 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001728}
1729
1730FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1731 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1732 while (Primary->getInstantiatedFromMemberTemplate()) {
1733 // If we have hit a point where the user provided a specialization of
1734 // this template, we're done looking.
1735 if (Primary->isMemberSpecialization())
1736 break;
1737
1738 Primary = Primary->getInstantiatedFromMemberTemplate();
1739 }
1740
1741 return Primary->getTemplatedDecl();
1742 }
1743
1744 return getInstantiatedFromMemberFunction();
1745}
1746
Douglas Gregor16e8be22009-06-29 17:30:29 +00001747FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001748 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001749 = TemplateOrSpecialization
1750 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001751 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001752 }
1753 return 0;
1754}
1755
1756const TemplateArgumentList *
1757FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001758 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001759 = TemplateOrSpecialization
1760 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001761 return Info->TemplateArguments;
1762 }
1763 return 0;
1764}
1765
Abramo Bagnarae03db982010-05-20 15:32:11 +00001766const TemplateArgumentListInfo *
1767FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1768 if (FunctionTemplateSpecializationInfo *Info
1769 = TemplateOrSpecialization
1770 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1771 return Info->TemplateArgumentsAsWritten;
1772 }
1773 return 0;
1774}
1775
Mike Stump1eb44332009-09-09 15:08:12 +00001776void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001777FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1778 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001779 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001780 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001781 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001782 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1783 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001784 assert(TSK != TSK_Undeclared &&
1785 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001786 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001787 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001788 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001789 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1790 TemplateArgs,
1791 TemplateArgsAsWritten,
1792 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001793 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Douglas Gregor127102b2009-06-29 20:59:39 +00001795 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001796 // function template specializations.
1797 if (InsertPos)
1798 Template->getSpecializations().InsertNode(Info, InsertPos);
1799 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001800 // Try to insert the new node. If there is an existing node, leave it, the
1801 // set will contain the canonical decls while
1802 // FunctionTemplateDecl::findSpecialization will return
1803 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001804 FunctionTemplateSpecializationInfo *Existing
1805 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001806 (void)Existing;
1807 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1808 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001809 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001810}
1811
John McCallaf2094e2010-04-08 09:05:18 +00001812void
1813FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1814 const UnresolvedSetImpl &Templates,
1815 const TemplateArgumentListInfo &TemplateArgs) {
1816 assert(TemplateOrSpecialization.isNull());
1817 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1818 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001819 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001820 void *Buffer = Context.Allocate(Size);
1821 DependentFunctionTemplateSpecializationInfo *Info =
1822 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1823 TemplateArgs);
1824 TemplateOrSpecialization = Info;
1825}
1826
1827DependentFunctionTemplateSpecializationInfo::
1828DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1829 const TemplateArgumentListInfo &TArgs)
1830 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1831
1832 d.NumTemplates = Ts.size();
1833 d.NumArgs = TArgs.size();
1834
1835 FunctionTemplateDecl **TsArray =
1836 const_cast<FunctionTemplateDecl**>(getTemplates());
1837 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1838 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1839
1840 TemplateArgumentLoc *ArgsArray =
1841 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1842 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1843 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1844}
1845
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001846TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001847 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001848 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001849 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001850 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001851 if (FTSInfo)
1852 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Douglas Gregor2db32322009-10-07 23:56:10 +00001854 MemberSpecializationInfo *MSInfo
1855 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1856 if (MSInfo)
1857 return MSInfo->getTemplateSpecializationKind();
1858
1859 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001860}
1861
Mike Stump1eb44332009-09-09 15:08:12 +00001862void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001863FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1864 SourceLocation PointOfInstantiation) {
1865 if (FunctionTemplateSpecializationInfo *FTSInfo
1866 = TemplateOrSpecialization.dyn_cast<
1867 FunctionTemplateSpecializationInfo*>()) {
1868 FTSInfo->setTemplateSpecializationKind(TSK);
1869 if (TSK != TSK_ExplicitSpecialization &&
1870 PointOfInstantiation.isValid() &&
1871 FTSInfo->getPointOfInstantiation().isInvalid())
1872 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1873 } else if (MemberSpecializationInfo *MSInfo
1874 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1875 MSInfo->setTemplateSpecializationKind(TSK);
1876 if (TSK != TSK_ExplicitSpecialization &&
1877 PointOfInstantiation.isValid() &&
1878 MSInfo->getPointOfInstantiation().isInvalid())
1879 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1880 } else
1881 assert(false && "Function cannot have a template specialization kind");
1882}
1883
1884SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001885 if (FunctionTemplateSpecializationInfo *FTSInfo
1886 = TemplateOrSpecialization.dyn_cast<
1887 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001888 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001889 else if (MemberSpecializationInfo *MSInfo
1890 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001891 return MSInfo->getPointOfInstantiation();
1892
1893 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001894}
1895
Douglas Gregor9f185072009-09-11 20:15:17 +00001896bool FunctionDecl::isOutOfLine() const {
Douglas Gregorf4a03cc2011-02-17 07:02:32 +00001897 if (getLexicalDeclContext() != getDeclContext())
Douglas Gregor9f185072009-09-11 20:15:17 +00001898 return true;
1899
1900 // If this function was instantiated from a member function of a
1901 // class template, check whether that member function was defined out-of-line.
1902 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1903 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001904 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001905 return Definition->isOutOfLine();
1906 }
1907
1908 // If this function was instantiated from a function template,
1909 // check whether that function template was defined out-of-line.
1910 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1911 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001912 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001913 return Definition->isOutOfLine();
1914 }
1915
1916 return false;
1917}
1918
Chris Lattner8a934232008-03-31 00:36:02 +00001919//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001920// FieldDecl Implementation
1921//===----------------------------------------------------------------------===//
1922
Jay Foad4ba2a172011-01-12 09:06:06 +00001923FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1924 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001925 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1926 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1927}
1928
1929bool FieldDecl::isAnonymousStructOrUnion() const {
1930 if (!isImplicit() || getDeclName())
1931 return false;
1932
1933 if (const RecordType *Record = getType()->getAs<RecordType>())
1934 return Record->getDecl()->isAnonymousStructOrUnion();
1935
1936 return false;
1937}
1938
John McCallba4f5d52011-01-20 07:57:12 +00001939unsigned FieldDecl::getFieldIndex() const {
1940 if (CachedFieldIndex) return CachedFieldIndex - 1;
1941
1942 unsigned index = 0;
1943 RecordDecl::field_iterator
1944 i = getParent()->field_begin(), e = getParent()->field_end();
1945 while (true) {
1946 assert(i != e && "failed to find field in parent!");
1947 if (*i == this)
1948 break;
1949
1950 ++i;
1951 ++index;
1952 }
1953
1954 CachedFieldIndex = index + 1;
1955 return index;
1956}
1957
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001958//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001959// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001960//===----------------------------------------------------------------------===//
1961
Douglas Gregor1693e152010-07-06 18:42:40 +00001962SourceLocation TagDecl::getOuterLocStart() const {
1963 return getTemplateOrInnerLocStart(this);
1964}
1965
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001966SourceRange TagDecl::getSourceRange() const {
1967 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001968 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001969}
1970
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001971TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001972 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001973}
1974
Douglas Gregor60e70642010-05-19 18:39:18 +00001975void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1976 TypedefDeclOrQualifier = TDD;
1977 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00001978 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001979 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00001980}
1981
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001982void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001983 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001984
1985 if (isa<CXXRecordDecl>(this)) {
1986 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1987 struct CXXRecordDecl::DefinitionData *Data =
1988 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001989 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1990 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001991 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001992}
1993
1994void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001995 assert((!isa<CXXRecordDecl>(this) ||
1996 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1997 "definition completed but not started");
1998
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001999 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002000 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002001
2002 if (ASTMutationListener *L = getASTMutationListener())
2003 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002004}
2005
Douglas Gregor952b0172010-02-11 01:04:33 +00002006TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002007 if (isDefinition())
2008 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002009 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2010 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002011
2012 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002013 R != REnd; ++R)
2014 if (R->isDefinition())
2015 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002017 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002018}
2019
John McCallb6217662010-03-15 10:12:16 +00002020void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
2021 SourceRange QualifierRange) {
2022 if (Qualifier) {
2023 // Make sure the extended qualifier info is allocated.
2024 if (!hasExtInfo())
2025 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2026 // Set qualifier info.
2027 getExtInfo()->NNS = Qualifier;
2028 getExtInfo()->NNSRange = QualifierRange;
2029 }
2030 else {
2031 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2032 assert(QualifierRange.isInvalid());
2033 if (hasExtInfo()) {
2034 getASTContext().Deallocate(getExtInfo());
2035 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2036 }
2037 }
2038}
2039
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002040//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002041// EnumDecl Implementation
2042//===----------------------------------------------------------------------===//
2043
2044EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2045 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002046 EnumDecl *PrevDecl, bool IsScoped,
2047 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002048 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002049 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002050 C.getTypeDeclType(Enum, PrevDecl);
2051 return Enum;
2052}
2053
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002054EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002055 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002056 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002057}
2058
Douglas Gregor838db382010-02-11 01:19:42 +00002059void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002060 QualType NewPromotionType,
2061 unsigned NumPositiveBits,
2062 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002063 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002064 if (!IntegerType)
2065 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002066 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002067 setNumPositiveBits(NumPositiveBits);
2068 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002069 TagDecl::completeDefinition();
2070}
2071
2072//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002073// RecordDecl Implementation
2074//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002075
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00002076RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002077 IdentifierInfo *Id, RecordDecl *PrevDecl,
2078 SourceLocation TKL)
2079 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00002080 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002081 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002082 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002083 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002084 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002085}
2086
Jay Foad4ba2a172011-01-12 09:06:06 +00002087RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002088 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00002089 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002090
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002091 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002092 C.getTypeDeclType(R, PrevDecl);
2093 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002094}
2095
Jay Foad4ba2a172011-01-12 09:06:06 +00002096RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002097 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2098 SourceLocation());
2099}
2100
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002101bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002102 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002103 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2104}
2105
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002106RecordDecl::field_iterator RecordDecl::field_begin() const {
2107 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2108 LoadFieldsFromExternalStorage();
2109
2110 return field_iterator(decl_iterator(FirstDecl));
2111}
2112
Douglas Gregor44b43212008-12-11 16:49:14 +00002113/// completeDefinition - Notes that the definition of this type is now
2114/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00002115void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002117 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00002118}
2119
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002120void RecordDecl::LoadFieldsFromExternalStorage() const {
2121 ExternalASTSource *Source = getASTContext().getExternalSource();
2122 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2123
2124 // Notify that we have a RecordDecl doing some initialization.
2125 ExternalASTSource::Deserializing TheFields(Source);
2126
2127 llvm::SmallVector<Decl*, 64> Decls;
2128 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2129 return;
2130
2131#ifndef NDEBUG
2132 // Check that all decls we got were FieldDecls.
2133 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2134 assert(isa<FieldDecl>(Decls[i]));
2135#endif
2136
2137 LoadedFieldsFromExternalStorage = true;
2138
2139 if (Decls.empty())
2140 return;
2141
2142 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2143}
2144
Steve Naroff56ee6892008-10-08 17:01:13 +00002145//===----------------------------------------------------------------------===//
2146// BlockDecl Implementation
2147//===----------------------------------------------------------------------===//
2148
Douglas Gregor838db382010-02-11 01:19:42 +00002149void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002150 unsigned NParms) {
2151 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002152
Steve Naroffe78b8092009-03-13 16:56:44 +00002153 // Zero params -> null pointer.
2154 if (NParms) {
2155 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002156 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002157 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2158 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2159 }
2160}
2161
John McCall6b5a61b2011-02-07 10:33:21 +00002162void BlockDecl::setCaptures(ASTContext &Context,
2163 const Capture *begin,
2164 const Capture *end,
2165 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002166 CapturesCXXThis = capturesCXXThis;
2167
2168 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002169 NumCaptures = 0;
2170 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002171 return;
2172 }
2173
John McCall6b5a61b2011-02-07 10:33:21 +00002174 NumCaptures = end - begin;
2175
2176 // Avoid new Capture[] because we don't want to provide a default
2177 // constructor.
2178 size_t allocationSize = NumCaptures * sizeof(Capture);
2179 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2180 memcpy(buffer, begin, allocationSize);
2181 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002182}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002183
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002184SourceRange BlockDecl::getSourceRange() const {
2185 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2186}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002187
2188//===----------------------------------------------------------------------===//
2189// Other Decl Allocation/Deallocation Method Implementations
2190//===----------------------------------------------------------------------===//
2191
2192TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2193 return new (C) TranslationUnitDecl(C);
2194}
2195
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002196LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2197 SourceLocation L, IdentifierInfo *II) {
2198 return new (C) LabelDecl(DC, L, II, 0);
2199}
2200
2201
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002202NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2203 SourceLocation L, IdentifierInfo *Id) {
2204 return new (C) NamespaceDecl(DC, L, Id);
2205}
2206
Douglas Gregor06c91932010-10-27 19:49:05 +00002207NamespaceDecl *NamespaceDecl::getNextNamespace() {
2208 return dyn_cast_or_null<NamespaceDecl>(
2209 NextNamespace.get(getASTContext().getExternalSource()));
2210}
2211
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002212ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2213 SourceLocation L, IdentifierInfo *Id, QualType T) {
2214 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2215}
2216
2217FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002218 const DeclarationNameInfo &NameInfo,
2219 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002220 StorageClass S, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002221 bool isInlineSpecified,
2222 bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002223 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor8f150942010-12-09 16:59:22 +00002224 S, SCAsWritten, isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002225 New->HasWrittenPrototype = hasWrittenPrototype;
2226 return New;
2227}
2228
2229BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2230 return new (C) BlockDecl(DC, L);
2231}
2232
2233EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2234 SourceLocation L,
2235 IdentifierInfo *Id, QualType T,
2236 Expr *E, const llvm::APSInt &V) {
2237 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2238}
2239
Benjamin Kramerd9811462010-11-21 14:11:41 +00002240IndirectFieldDecl *
2241IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2242 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2243 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002244 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2245}
2246
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002247SourceRange EnumConstantDecl::getSourceRange() const {
2248 SourceLocation End = getLocation();
2249 if (Init)
2250 End = Init->getLocEnd();
2251 return SourceRange(getLocation(), End);
2252}
2253
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002254TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2255 SourceLocation L, IdentifierInfo *Id,
2256 TypeSourceInfo *TInfo) {
2257 return new (C) TypedefDecl(DC, L, Id, TInfo);
2258}
2259
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002260FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2261 SourceLocation L,
2262 StringLiteral *Str) {
2263 return new (C) FileScopeAsmDecl(DC, L, Str);
2264}