blob: fded8ff38c13083aca1139a0b6f1fba47b334a47 [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
604void NamedDecl::ClearLinkageCache() {
605 // Note that we can't skip clearing the linkage of children just
606 // because the parent doesn't have cached linkage: we don't cache
607 // when computing linkage for parent contexts.
608
609 HasCachedLinkage = 0;
610
611 // If we're changing the linkage of a class, we need to reset the
612 // linkage of child declarations, too.
613 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
614 clearLinkageForClass(record);
615
616 if (const ClassTemplateDecl *temp = dyn_cast<ClassTemplateDecl>(this)) {
617 // Clear linkage for the template pattern.
618 CXXRecordDecl *record = temp->getTemplatedDecl();
619 record->HasCachedLinkage = 0;
620 clearLinkageForClass(record);
621
622 // ...do we need to clear linkage for specializations, too?
623 }
624}
625
Douglas Gregor381d34e2010-12-06 18:36:25 +0000626Linkage NamedDecl::getLinkage() const {
627 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000628 assert(Linkage(CachedLinkage) ==
629 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000630 return Linkage(CachedLinkage);
631 }
632
633 CachedLinkage = getLVForDecl(this,
634 LVFlags::CreateOnlyDeclLinkage()).linkage();
635 HasCachedLinkage = 1;
636 return Linkage(CachedLinkage);
637}
638
John McCallaf146032010-10-30 11:50:40 +0000639LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000640 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000641 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000642 HasCachedLinkage = 1;
643 CachedLinkage = LI.linkage();
644 return LI;
John McCall0df95872010-10-29 00:29:13 +0000645}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000646
John McCall36987482010-11-02 01:45:15 +0000647static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000648 // Objective-C: treat all Objective-C declarations as having external
649 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000650 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000651 default:
652 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000653 case Decl::TemplateTemplateParm: // count these as external
654 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000655 case Decl::ObjCAtDefsField:
656 case Decl::ObjCCategory:
657 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000658 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000659 case Decl::ObjCForwardProtocol:
660 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000661 case Decl::ObjCMethod:
662 case Decl::ObjCProperty:
663 case Decl::ObjCPropertyImpl:
664 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000665 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000666 }
667
Douglas Gregord85b5b92009-11-25 22:24:25 +0000668 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000669 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000670 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000671
672 // C++ [basic.link]p5:
673 // In addition, a member function, static data member, a named
674 // class or enumeration of class scope, or an unnamed class or
675 // enumeration defined in a class-scope typedef declaration such
676 // that the class or enumeration has the typedef name for linkage
677 // purposes (7.1.3), has external linkage if the name of the class
678 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000679 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000680 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000681
682 // C++ [basic.link]p6:
683 // The name of a function declared in block scope and the name of
684 // an object declared by a block scope extern declaration have
685 // linkage. If there is a visible declaration of an entity with
686 // linkage having the same name and type, ignoring entities
687 // declared outside the innermost enclosing namespace scope, the
688 // block scope declaration declares that same entity and receives
689 // the linkage of the previous declaration. If there is more than
690 // one such matching entity, the program is ill-formed. Otherwise,
691 // if no matching entity is found, the block scope entity receives
692 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000693 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
694 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000695 if (Function->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000696 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000697
John McCallaf146032010-10-30 11:50:40 +0000698 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000699 if (Flags.ConsiderVisibilityAttributes) {
700 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
701 LV.setVisibility(GetVisibilityFromAttr(VA));
702 }
703
John McCall1fb0caa2010-10-22 21:05:15 +0000704 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000705 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000706 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
707 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000708 }
709
710 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000711 }
712
John McCall0df95872010-10-29 00:29:13 +0000713 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000714 if (Var->getStorageClass() == SC_Extern ||
715 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000716 if (Var->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000717 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000718
John McCallaf146032010-10-30 11:50:40 +0000719 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000720 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000721 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000722 else if (Flags.ConsiderVisibilityAttributes) {
723 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
724 LV.setVisibility(GetVisibilityFromAttr(VA));
725 }
726
John McCall1fb0caa2010-10-22 21:05:15 +0000727 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000728 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000729 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
730 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000731 }
732
733 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000734 }
735 }
736
737 // C++ [basic.link]p6:
738 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000739 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000740}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000741
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000742std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000743 return getQualifiedNameAsString(getASTContext().getLangOptions());
744}
745
746std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000747 const DeclContext *Ctx = getDeclContext();
748
749 if (Ctx->isFunctionOrMethod())
750 return getNameAsString();
751
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000752 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
753 ContextsTy Contexts;
754
755 // Collect contexts.
756 while (Ctx && isa<NamedDecl>(Ctx)) {
757 Contexts.push_back(Ctx);
758 Ctx = Ctx->getParent();
759 };
760
761 std::string QualName;
762 llvm::raw_string_ostream OS(QualName);
763
764 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
765 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000766 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000767 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000768 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
769 std::string TemplateArgsStr
770 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000771 TemplateArgs.data(),
772 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000773 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000774 OS << Spec->getName() << TemplateArgsStr;
775 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000776 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000777 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000778 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000779 OS << ND;
780 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
781 if (!RD->getIdentifier())
782 OS << "<anonymous " << RD->getKindName() << '>';
783 else
784 OS << RD;
785 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000786 const FunctionProtoType *FT = 0;
787 if (FD->hasWrittenPrototype())
788 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
789
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000790 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000791 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000792 unsigned NumParams = FD->getNumParams();
793 for (unsigned i = 0; i < NumParams; ++i) {
794 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000795 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000796 std::string Param;
797 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000798 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000799 }
800
801 if (FT->isVariadic()) {
802 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000803 OS << ", ";
804 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000805 }
806 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000807 OS << ')';
808 } else {
809 OS << cast<NamedDecl>(*I);
810 }
811 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000812 }
813
John McCall8472af42010-03-16 21:48:18 +0000814 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000815 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000816 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000817 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000818
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000819 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000820}
821
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000822bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000823 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
824
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000825 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
826 // We want to keep it, unless it nominates same namespace.
827 if (getKind() == Decl::UsingDirective) {
828 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
829 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000832 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
833 // For function declarations, we keep track of redeclarations.
834 return FD->getPreviousDeclaration() == OldD;
835
Douglas Gregore53060f2009-06-25 22:08:12 +0000836 // For function templates, the underlying function declarations are linked.
837 if (const FunctionTemplateDecl *FunctionTemplate
838 = dyn_cast<FunctionTemplateDecl>(this))
839 if (const FunctionTemplateDecl *OldFunctionTemplate
840 = dyn_cast<FunctionTemplateDecl>(OldD))
841 return FunctionTemplate->getTemplatedDecl()
842 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Steve Naroff0de21fd2009-02-22 19:35:57 +0000844 // For method declarations, we keep track of redeclarations.
845 if (isa<ObjCMethodDecl>(this))
846 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
John McCallf36e02d2009-10-09 21:13:30 +0000848 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
849 return true;
850
John McCall9488ea12009-11-17 05:59:44 +0000851 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
852 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
853 cast<UsingShadowDecl>(OldD)->getTargetDecl();
854
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000855 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
856 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
857 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
858
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000859 // For non-function declarations, if the declarations are of the
860 // same kind then this must be a redeclaration, or semantic analysis
861 // would not have given us the new declaration.
862 return this->getKind() == OldD->getKind();
863}
864
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000865bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000866 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000867}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000868
Anders Carlssone136e0e2009-06-26 06:29:23 +0000869NamedDecl *NamedDecl::getUnderlyingDecl() {
870 NamedDecl *ND = this;
871 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000872 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000873 ND = UD->getTargetDecl();
874 else if (ObjCCompatibleAliasDecl *AD
875 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
876 return AD->getClassInterface();
877 else
878 return ND;
879 }
880}
881
John McCall161755a2010-04-06 21:38:20 +0000882bool NamedDecl::isCXXInstanceMember() const {
883 assert(isCXXClassMember() &&
884 "checking whether non-member is instance member");
885
886 const NamedDecl *D = this;
887 if (isa<UsingShadowDecl>(D))
888 D = cast<UsingShadowDecl>(D)->getTargetDecl();
889
Francois Pichet87c2e122010-11-21 06:08:52 +0000890 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000891 return true;
892 if (isa<CXXMethodDecl>(D))
893 return cast<CXXMethodDecl>(D)->isInstance();
894 if (isa<FunctionTemplateDecl>(D))
895 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
896 ->getTemplatedDecl())->isInstance();
897 return false;
898}
899
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000900//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000901// DeclaratorDecl Implementation
902//===----------------------------------------------------------------------===//
903
Douglas Gregor1693e152010-07-06 18:42:40 +0000904template <typename DeclT>
905static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
906 if (decl->getNumTemplateParameterLists() > 0)
907 return decl->getTemplateParameterList(0)->getTemplateLoc();
908 else
909 return decl->getInnerLocStart();
910}
911
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000912SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000913 TypeSourceInfo *TSI = getTypeSourceInfo();
914 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000915 return SourceLocation();
916}
917
John McCallb6217662010-03-15 10:12:16 +0000918void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
919 SourceRange QualifierRange) {
920 if (Qualifier) {
921 // Make sure the extended decl info is allocated.
922 if (!hasExtInfo()) {
923 // Save (non-extended) type source info pointer.
924 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
925 // Allocate external info struct.
926 DeclInfo = new (getASTContext()) ExtInfo;
927 // Restore savedTInfo into (extended) decl info.
928 getExtInfo()->TInfo = savedTInfo;
929 }
930 // Set qualifier info.
931 getExtInfo()->NNS = Qualifier;
932 getExtInfo()->NNSRange = QualifierRange;
933 }
934 else {
935 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
936 assert(QualifierRange.isInvalid());
937 if (hasExtInfo()) {
938 // Save type source info pointer.
939 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
940 // Deallocate the extended decl info.
941 getASTContext().Deallocate(getExtInfo());
942 // Restore savedTInfo into (non-extended) decl info.
943 DeclInfo = savedTInfo;
944 }
945 }
946}
947
Douglas Gregor1693e152010-07-06 18:42:40 +0000948SourceLocation DeclaratorDecl::getOuterLocStart() const {
949 return getTemplateOrInnerLocStart(this);
950}
951
Abramo Bagnara9b934882010-06-12 08:15:14 +0000952void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000953QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
954 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000955 TemplateParameterList **TPLists) {
956 assert((NumTPLists == 0 || TPLists != 0) &&
957 "Empty array of template parameters with positive size!");
958 assert((NumTPLists == 0 || NNS) &&
959 "Nonempty array of template parameters with no qualifier!");
960
961 // Free previous template parameters (if any).
962 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000963 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000964 TemplParamLists = 0;
965 NumTemplParamLists = 0;
966 }
967 // Set info on matched template parameter lists (if any).
968 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000969 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000970 NumTemplParamLists = NumTPLists;
971 for (unsigned i = NumTPLists; i-- > 0; )
972 TemplParamLists[i] = TPLists[i];
973 }
974}
975
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000976//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000977// VarDecl Implementation
978//===----------------------------------------------------------------------===//
979
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000980const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
981 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000982 case SC_None: break;
983 case SC_Auto: return "auto"; break;
984 case SC_Extern: return "extern"; break;
985 case SC_PrivateExtern: return "__private_extern__"; break;
986 case SC_Register: return "register"; break;
987 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000988 }
989
990 assert(0 && "Invalid storage class");
991 return 0;
992}
993
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000994VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000995 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000996 StorageClass S, StorageClass SCAsWritten) {
997 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000998}
999
Douglas Gregor381d34e2010-12-06 18:36:25 +00001000void VarDecl::setStorageClass(StorageClass SC) {
1001 assert(isLegalForVariable(SC));
1002 if (getStorageClass() != SC)
1003 ClearLinkageCache();
1004
1005 SClass = SC;
1006}
1007
Douglas Gregor1693e152010-07-06 18:42:40 +00001008SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001009 SourceLocation Start = getTypeSpecStartLoc();
1010 if (Start.isInvalid())
1011 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001012 return Start;
1013}
1014
1015SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001016 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001017 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1018 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001019}
1020
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001021bool VarDecl::isExternC() const {
1022 ASTContext &Context = getASTContext();
1023 if (!Context.getLangOptions().CPlusPlus)
1024 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +00001025 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001026 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1027
1028 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
1029 DC = DC->getParent()) {
1030 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1031 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001032 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001033
1034 break;
1035 }
1036
1037 if (DC->isFunctionOrMethod())
1038 return false;
1039 }
1040
1041 return false;
1042}
1043
1044VarDecl *VarDecl::getCanonicalDecl() {
1045 return getFirstDeclaration();
1046}
1047
Sebastian Redle9d12b62010-01-31 22:27:38 +00001048VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1049 // C++ [basic.def]p2:
1050 // A declaration is a definition unless [...] it contains the 'extern'
1051 // specifier or a linkage-specification and neither an initializer [...],
1052 // it declares a static data member in a class declaration [...].
1053 // C++ [temp.expl.spec]p15:
1054 // An explicit specialization of a static data member of a template is a
1055 // definition if the declaration includes an initializer; otherwise, it is
1056 // a declaration.
1057 if (isStaticDataMember()) {
1058 if (isOutOfLine() && (hasInit() ||
1059 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1060 return Definition;
1061 else
1062 return DeclarationOnly;
1063 }
1064 // C99 6.7p5:
1065 // A definition of an identifier is a declaration for that identifier that
1066 // [...] causes storage to be reserved for that object.
1067 // Note: that applies for all non-file-scope objects.
1068 // C99 6.9.2p1:
1069 // If the declaration of an identifier for an object has file scope and an
1070 // initializer, the declaration is an external definition for the identifier
1071 if (hasInit())
1072 return Definition;
1073 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1074 if (hasExternalStorage())
1075 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001076
John McCalld931b082010-08-26 03:08:43 +00001077 if (getStorageClassAsWritten() == SC_Extern ||
1078 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001079 for (const VarDecl *PrevVar = getPreviousDeclaration();
1080 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1081 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1082 return DeclarationOnly;
1083 }
1084 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001085 // C99 6.9.2p2:
1086 // A declaration of an object that has file scope without an initializer,
1087 // and without a storage class specifier or the scs 'static', constitutes
1088 // a tentative definition.
1089 // No such thing in C++.
1090 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1091 return TentativeDefinition;
1092
1093 // What's left is (in C, block-scope) declarations without initializers or
1094 // external storage. These are definitions.
1095 return Definition;
1096}
1097
Sebastian Redle9d12b62010-01-31 22:27:38 +00001098VarDecl *VarDecl::getActingDefinition() {
1099 DefinitionKind Kind = isThisDeclarationADefinition();
1100 if (Kind != TentativeDefinition)
1101 return 0;
1102
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001103 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001104 VarDecl *First = getFirstDeclaration();
1105 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1106 I != E; ++I) {
1107 Kind = (*I)->isThisDeclarationADefinition();
1108 if (Kind == Definition)
1109 return 0;
1110 else if (Kind == TentativeDefinition)
1111 LastTentative = *I;
1112 }
1113 return LastTentative;
1114}
1115
1116bool VarDecl::isTentativeDefinitionNow() const {
1117 DefinitionKind Kind = isThisDeclarationADefinition();
1118 if (Kind != TentativeDefinition)
1119 return false;
1120
1121 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1122 if ((*I)->isThisDeclarationADefinition() == Definition)
1123 return false;
1124 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001125 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001126}
1127
Sebastian Redl31310a22010-02-01 20:16:42 +00001128VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001129 VarDecl *First = getFirstDeclaration();
1130 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1131 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001132 if ((*I)->isThisDeclarationADefinition() == Definition)
1133 return *I;
1134 }
1135 return 0;
1136}
1137
John McCall110e8e52010-10-29 22:22:43 +00001138VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1139 DefinitionKind Kind = DeclarationOnly;
1140
1141 const VarDecl *First = getFirstDeclaration();
1142 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1143 I != E; ++I)
1144 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1145
1146 return Kind;
1147}
1148
Sebastian Redl31310a22010-02-01 20:16:42 +00001149const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001150 redecl_iterator I = redecls_begin(), E = redecls_end();
1151 while (I != E && !I->getInit())
1152 ++I;
1153
1154 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001155 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001156 return I->getInit();
1157 }
1158 return 0;
1159}
1160
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001161bool VarDecl::isOutOfLine() const {
Douglas Gregorf4a03cc2011-02-17 07:02:32 +00001162 if (getLexicalDeclContext() != getDeclContext())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001163 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001164
1165 if (!isStaticDataMember())
1166 return false;
1167
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001168 // If this static data member was instantiated from a static data member of
1169 // a class template, check whether that static data member was defined
1170 // out-of-line.
1171 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1172 return VD->isOutOfLine();
1173
1174 return false;
1175}
1176
Douglas Gregor0d035142009-10-27 18:42:08 +00001177VarDecl *VarDecl::getOutOfLineDefinition() {
1178 if (!isStaticDataMember())
1179 return 0;
1180
1181 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1182 RD != RDEnd; ++RD) {
1183 if (RD->getLexicalDeclContext()->isFileContext())
1184 return *RD;
1185 }
1186
1187 return 0;
1188}
1189
Douglas Gregor838db382010-02-11 01:19:42 +00001190void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001191 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1192 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001193 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001194 }
1195
1196 Init = I;
1197}
1198
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001199VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001200 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001201 return cast<VarDecl>(MSI->getInstantiatedFrom());
1202
1203 return 0;
1204}
1205
Douglas Gregor663b5a02009-10-14 20:14:33 +00001206TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001207 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001208 return MSI->getTemplateSpecializationKind();
1209
1210 return TSK_Undeclared;
1211}
1212
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001213MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001214 return getASTContext().getInstantiatedFromStaticDataMember(this);
1215}
1216
Douglas Gregor0a897e32009-10-15 17:21:20 +00001217void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1218 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001219 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001220 assert(MSI && "Not an instantiated static data member?");
1221 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001222 if (TSK != TSK_ExplicitSpecialization &&
1223 PointOfInstantiation.isValid() &&
1224 MSI->getPointOfInstantiation().isInvalid())
1225 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001226}
1227
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001228//===----------------------------------------------------------------------===//
1229// ParmVarDecl Implementation
1230//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001231
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001232ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1233 SourceLocation L, IdentifierInfo *Id,
1234 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001235 StorageClass S, StorageClass SCAsWritten,
1236 Expr *DefArg) {
1237 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1238 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001239}
1240
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001241Expr *ParmVarDecl::getDefaultArg() {
1242 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1243 assert(!hasUninstantiatedDefaultArg() &&
1244 "Default argument is not yet instantiated!");
1245
1246 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001247 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001248 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001249
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001250 return Arg;
1251}
1252
1253unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001254 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001255 return E->getNumTemporaries();
1256
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001257 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001258}
1259
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001260CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1261 assert(getNumDefaultArgTemporaries() &&
1262 "Default arguments does not have any temporaries!");
1263
John McCall4765fa02010-12-06 08:20:24 +00001264 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001265 return E->getTemporary(i);
1266}
1267
1268SourceRange ParmVarDecl::getDefaultArgRange() const {
1269 if (const Expr *E = getInit())
1270 return E->getSourceRange();
1271
1272 if (hasUninstantiatedDefaultArg())
1273 return getUninstantiatedDefaultArg()->getSourceRange();
1274
1275 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001276}
1277
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001278bool ParmVarDecl::isParameterPack() const {
1279 return isa<PackExpansionType>(getType());
1280}
1281
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001282//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001283// FunctionDecl Implementation
1284//===----------------------------------------------------------------------===//
1285
John McCall136a6982009-09-11 06:45:03 +00001286void FunctionDecl::getNameForDiagnostic(std::string &S,
1287 const PrintingPolicy &Policy,
1288 bool Qualified) const {
1289 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1290 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1291 if (TemplateArgs)
1292 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00001293 TemplateArgs->data(),
1294 TemplateArgs->size(),
John McCall136a6982009-09-11 06:45:03 +00001295 Policy);
1296
1297}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001298
Ted Kremenek9498d382010-04-29 16:49:01 +00001299bool FunctionDecl::isVariadic() const {
1300 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1301 return FT->isVariadic();
1302 return false;
1303}
1304
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001305bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1306 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1307 if (I->Body) {
1308 Definition = *I;
1309 return true;
1310 }
1311 }
1312
1313 return false;
1314}
1315
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001316Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001317 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1318 if (I->Body) {
1319 Definition = *I;
1320 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001321 }
1322 }
1323
1324 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001325}
1326
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001327void FunctionDecl::setBody(Stmt *B) {
1328 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001329 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001330 EndRangeLoc = B->getLocEnd();
1331}
1332
Douglas Gregor21386642010-09-28 21:55:22 +00001333void FunctionDecl::setPure(bool P) {
1334 IsPure = P;
1335 if (P)
1336 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1337 Parent->markedVirtualFunctionPure();
1338}
1339
Douglas Gregor48a83b52009-09-12 00:17:51 +00001340bool FunctionDecl::isMain() const {
1341 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001342 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001343 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001344 getIdentifier() && getIdentifier()->isStr("main");
1345}
1346
Douglas Gregor48a83b52009-09-12 00:17:51 +00001347bool FunctionDecl::isExternC() const {
1348 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001349 // In C, any non-static, non-overloadable function has external
1350 // linkage.
1351 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001352 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001353
Mike Stump1eb44332009-09-09 15:08:12 +00001354 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001355 DC = DC->getParent()) {
1356 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1357 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001358 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001359 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001360
1361 break;
1362 }
Douglas Gregor45975532010-08-17 16:09:23 +00001363
1364 if (DC->isRecord())
1365 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001366 }
1367
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001368 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001369}
1370
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001371bool FunctionDecl::isGlobal() const {
1372 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1373 return Method->isStatic();
1374
John McCalld931b082010-08-26 03:08:43 +00001375 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001376 return false;
1377
Mike Stump1eb44332009-09-09 15:08:12 +00001378 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001379 DC->isNamespace();
1380 DC = DC->getParent()) {
1381 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1382 if (!Namespace->getDeclName())
1383 return false;
1384 break;
1385 }
1386 }
1387
1388 return true;
1389}
1390
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001391void
1392FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1393 redeclarable_base::setPreviousDeclaration(PrevDecl);
1394
1395 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1396 FunctionTemplateDecl *PrevFunTmpl
1397 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1398 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1399 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1400 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001401
1402 if (PrevDecl->IsInline)
1403 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001404}
1405
1406const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1407 return getFirstDeclaration();
1408}
1409
1410FunctionDecl *FunctionDecl::getCanonicalDecl() {
1411 return getFirstDeclaration();
1412}
1413
Douglas Gregor381d34e2010-12-06 18:36:25 +00001414void FunctionDecl::setStorageClass(StorageClass SC) {
1415 assert(isLegalForFunction(SC));
1416 if (getStorageClass() != SC)
1417 ClearLinkageCache();
1418
1419 SClass = SC;
1420}
1421
Douglas Gregor3e41d602009-02-13 23:20:09 +00001422/// \brief Returns a value indicating whether this function
1423/// corresponds to a builtin function.
1424///
1425/// The function corresponds to a built-in function if it is
1426/// declared at translation scope or within an extern "C" block and
1427/// its name matches with the name of a builtin. The returned value
1428/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001429/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001430/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001431unsigned FunctionDecl::getBuiltinID() const {
1432 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001433 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1434 return 0;
1435
1436 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1437 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1438 return BuiltinID;
1439
1440 // This function has the name of a known C library
1441 // function. Determine whether it actually refers to the C library
1442 // function or whether it just has the same name.
1443
Douglas Gregor9add3172009-02-17 03:23:10 +00001444 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001445 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001446 return 0;
1447
Douglas Gregor3c385e52009-02-14 18:57:46 +00001448 // If this function is at translation-unit scope and we're not in
1449 // C++, it refers to the C library function.
1450 if (!Context.getLangOptions().CPlusPlus &&
1451 getDeclContext()->isTranslationUnit())
1452 return BuiltinID;
1453
1454 // If the function is in an extern "C" linkage specification and is
1455 // not marked "overloadable", it's the real function.
1456 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001457 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001458 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001459 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001460 return BuiltinID;
1461
1462 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001463 return 0;
1464}
1465
1466
Chris Lattner1ad9b282009-04-25 06:03:53 +00001467/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001468/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001469/// after it has been created.
1470unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001471 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001472 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001473 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001474 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Reid Spencer5f016e22007-07-11 17:01:13 +00001476}
1477
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001478void FunctionDecl::setParams(ASTContext &C,
1479 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001480 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001481 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 // Zero params -> null pointer.
1484 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001485 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001486 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001488
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001489 // Update source range. The check below allows us to set EndRangeLoc before
1490 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001491 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001492 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 }
1494}
1495
Chris Lattner8123a952008-04-10 02:22:51 +00001496/// getMinRequiredArguments - Returns the minimum number of arguments
1497/// needed to call this function. This may be fewer than the number of
1498/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001499/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001500unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001501 if (!getASTContext().getLangOptions().CPlusPlus)
1502 return getNumParams();
1503
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001504 unsigned NumRequiredArgs = getNumParams();
1505
1506 // If the last parameter is a parameter pack, we don't need an argument for
1507 // it.
1508 if (NumRequiredArgs > 0 &&
1509 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1510 --NumRequiredArgs;
1511
1512 // If this parameter has a default argument, we don't need an argument for
1513 // it.
1514 while (NumRequiredArgs > 0 &&
1515 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001516 --NumRequiredArgs;
1517
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001518 // We might have parameter packs before the end. These can't be deduced,
1519 // but they can still handle multiple arguments.
1520 unsigned ArgIdx = NumRequiredArgs;
1521 while (ArgIdx > 0) {
1522 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1523 NumRequiredArgs = ArgIdx;
1524
1525 --ArgIdx;
1526 }
1527
Chris Lattner8123a952008-04-10 02:22:51 +00001528 return NumRequiredArgs;
1529}
1530
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001531bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001532 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001533 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001534
1535 if (isa<CXXMethodDecl>(this)) {
1536 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1537 return true;
1538 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001539
1540 switch (getTemplateSpecializationKind()) {
1541 case TSK_Undeclared:
1542 case TSK_ExplicitSpecialization:
1543 return false;
1544
1545 case TSK_ImplicitInstantiation:
1546 case TSK_ExplicitInstantiationDeclaration:
1547 case TSK_ExplicitInstantiationDefinition:
1548 // Handle below.
1549 break;
1550 }
1551
1552 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001553 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001554 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001555 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001556
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001557 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001558 return PatternDecl->isInlined();
1559
1560 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001561}
1562
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001563/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001564/// definition will be externally visible.
1565///
1566/// Inline function definitions are always available for inlining optimizations.
1567/// However, depending on the language dialect, declaration specifiers, and
1568/// attributes, the definition of an inline function may or may not be
1569/// "externally" visible to other translation units in the program.
1570///
1571/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001572/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001573/// inline definition becomes externally visible (C99 6.7.4p6).
1574///
1575/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1576/// definition, we use the GNU semantics for inline, which are nearly the
1577/// opposite of C99 semantics. In particular, "inline" by itself will create
1578/// an externally visible symbol, but "extern inline" will not create an
1579/// externally visible symbol.
1580bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1581 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001582 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001583 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001584
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001585 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001586 // If it's not the case that both 'inline' and 'extern' are
1587 // specified on the definition, then this inline definition is
1588 // externally visible.
1589 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1590 return true;
1591
1592 // If any declaration is 'inline' but not 'extern', then this definition
1593 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001594 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1595 Redecl != RedeclEnd;
1596 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001597 if (Redecl->isInlineSpecified() &&
1598 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001599 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001600 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001601
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001602 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001603 }
1604
1605 // C99 6.7.4p6:
1606 // [...] If all of the file scope declarations for a function in a
1607 // translation unit include the inline function specifier without extern,
1608 // then the definition in that translation unit is an inline definition.
1609 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1610 Redecl != RedeclEnd;
1611 ++Redecl) {
1612 // Only consider file-scope declarations in this test.
1613 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1614 continue;
1615
John McCalld931b082010-08-26 03:08:43 +00001616 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001617 return true; // Not an inline definition
1618 }
1619
1620 // C99 6.7.4p6:
1621 // An inline definition does not provide an external definition for the
1622 // function, and does not forbid an external definition in another
1623 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001624 return false;
1625}
1626
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001627/// getOverloadedOperator - Which C++ overloaded operator this
1628/// function represents, if any.
1629OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001630 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1631 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001632 else
1633 return OO_None;
1634}
1635
Sean Hunta6c058d2010-01-13 09:01:02 +00001636/// getLiteralIdentifier - The literal suffix identifier this function
1637/// represents, if any.
1638const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1639 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1640 return getDeclName().getCXXLiteralIdentifier();
1641 else
1642 return 0;
1643}
1644
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001645FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1646 if (TemplateOrSpecialization.isNull())
1647 return TK_NonTemplate;
1648 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1649 return TK_FunctionTemplate;
1650 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1651 return TK_MemberSpecialization;
1652 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1653 return TK_FunctionTemplateSpecialization;
1654 if (TemplateOrSpecialization.is
1655 <DependentFunctionTemplateSpecializationInfo*>())
1656 return TK_DependentFunctionTemplateSpecialization;
1657
1658 assert(false && "Did we miss a TemplateOrSpecialization type?");
1659 return TK_NonTemplate;
1660}
1661
Douglas Gregor2db32322009-10-07 23:56:10 +00001662FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001663 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001664 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1665
1666 return 0;
1667}
1668
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001669MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1670 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1671}
1672
Douglas Gregor2db32322009-10-07 23:56:10 +00001673void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001674FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1675 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001676 TemplateSpecializationKind TSK) {
1677 assert(TemplateOrSpecialization.isNull() &&
1678 "Member function is already a specialization");
1679 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001680 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001681 TemplateOrSpecialization = Info;
1682}
1683
Douglas Gregor3b846b62009-10-27 20:53:28 +00001684bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001685 // If the function is invalid, it can't be implicitly instantiated.
1686 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001687 return false;
1688
1689 switch (getTemplateSpecializationKind()) {
1690 case TSK_Undeclared:
1691 case TSK_ExplicitSpecialization:
1692 case TSK_ExplicitInstantiationDefinition:
1693 return false;
1694
1695 case TSK_ImplicitInstantiation:
1696 return true;
1697
1698 case TSK_ExplicitInstantiationDeclaration:
1699 // Handled below.
1700 break;
1701 }
1702
1703 // Find the actual template from which we will instantiate.
1704 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001705 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001706 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001707 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001708
1709 // C++0x [temp.explicit]p9:
1710 // Except for inline functions, other explicit instantiation declarations
1711 // have the effect of suppressing the implicit instantiation of the entity
1712 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001713 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001714 return true;
1715
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001716 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001717}
1718
1719FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1720 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1721 while (Primary->getInstantiatedFromMemberTemplate()) {
1722 // If we have hit a point where the user provided a specialization of
1723 // this template, we're done looking.
1724 if (Primary->isMemberSpecialization())
1725 break;
1726
1727 Primary = Primary->getInstantiatedFromMemberTemplate();
1728 }
1729
1730 return Primary->getTemplatedDecl();
1731 }
1732
1733 return getInstantiatedFromMemberFunction();
1734}
1735
Douglas Gregor16e8be22009-06-29 17:30:29 +00001736FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001737 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001738 = TemplateOrSpecialization
1739 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001740 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001741 }
1742 return 0;
1743}
1744
1745const TemplateArgumentList *
1746FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001747 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001748 = TemplateOrSpecialization
1749 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001750 return Info->TemplateArguments;
1751 }
1752 return 0;
1753}
1754
Abramo Bagnarae03db982010-05-20 15:32:11 +00001755const TemplateArgumentListInfo *
1756FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1757 if (FunctionTemplateSpecializationInfo *Info
1758 = TemplateOrSpecialization
1759 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1760 return Info->TemplateArgumentsAsWritten;
1761 }
1762 return 0;
1763}
1764
Mike Stump1eb44332009-09-09 15:08:12 +00001765void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001766FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1767 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001768 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001769 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001770 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001771 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1772 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001773 assert(TSK != TSK_Undeclared &&
1774 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001775 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001776 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001777 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001778 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1779 TemplateArgs,
1780 TemplateArgsAsWritten,
1781 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001782 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Douglas Gregor127102b2009-06-29 20:59:39 +00001784 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001785 // function template specializations.
1786 if (InsertPos)
1787 Template->getSpecializations().InsertNode(Info, InsertPos);
1788 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001789 // Try to insert the new node. If there is an existing node, leave it, the
1790 // set will contain the canonical decls while
1791 // FunctionTemplateDecl::findSpecialization will return
1792 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001793 FunctionTemplateSpecializationInfo *Existing
1794 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001795 (void)Existing;
1796 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1797 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001798 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001799}
1800
John McCallaf2094e2010-04-08 09:05:18 +00001801void
1802FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1803 const UnresolvedSetImpl &Templates,
1804 const TemplateArgumentListInfo &TemplateArgs) {
1805 assert(TemplateOrSpecialization.isNull());
1806 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1807 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001808 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001809 void *Buffer = Context.Allocate(Size);
1810 DependentFunctionTemplateSpecializationInfo *Info =
1811 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1812 TemplateArgs);
1813 TemplateOrSpecialization = Info;
1814}
1815
1816DependentFunctionTemplateSpecializationInfo::
1817DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1818 const TemplateArgumentListInfo &TArgs)
1819 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1820
1821 d.NumTemplates = Ts.size();
1822 d.NumArgs = TArgs.size();
1823
1824 FunctionTemplateDecl **TsArray =
1825 const_cast<FunctionTemplateDecl**>(getTemplates());
1826 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1827 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1828
1829 TemplateArgumentLoc *ArgsArray =
1830 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1831 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1832 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1833}
1834
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001835TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001836 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001837 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001838 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001839 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001840 if (FTSInfo)
1841 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Douglas Gregor2db32322009-10-07 23:56:10 +00001843 MemberSpecializationInfo *MSInfo
1844 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1845 if (MSInfo)
1846 return MSInfo->getTemplateSpecializationKind();
1847
1848 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001849}
1850
Mike Stump1eb44332009-09-09 15:08:12 +00001851void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001852FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1853 SourceLocation PointOfInstantiation) {
1854 if (FunctionTemplateSpecializationInfo *FTSInfo
1855 = TemplateOrSpecialization.dyn_cast<
1856 FunctionTemplateSpecializationInfo*>()) {
1857 FTSInfo->setTemplateSpecializationKind(TSK);
1858 if (TSK != TSK_ExplicitSpecialization &&
1859 PointOfInstantiation.isValid() &&
1860 FTSInfo->getPointOfInstantiation().isInvalid())
1861 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1862 } else if (MemberSpecializationInfo *MSInfo
1863 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1864 MSInfo->setTemplateSpecializationKind(TSK);
1865 if (TSK != TSK_ExplicitSpecialization &&
1866 PointOfInstantiation.isValid() &&
1867 MSInfo->getPointOfInstantiation().isInvalid())
1868 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1869 } else
1870 assert(false && "Function cannot have a template specialization kind");
1871}
1872
1873SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001874 if (FunctionTemplateSpecializationInfo *FTSInfo
1875 = TemplateOrSpecialization.dyn_cast<
1876 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001877 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001878 else if (MemberSpecializationInfo *MSInfo
1879 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001880 return MSInfo->getPointOfInstantiation();
1881
1882 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001883}
1884
Douglas Gregor9f185072009-09-11 20:15:17 +00001885bool FunctionDecl::isOutOfLine() const {
Douglas Gregorf4a03cc2011-02-17 07:02:32 +00001886 if (getLexicalDeclContext() != getDeclContext())
Douglas Gregor9f185072009-09-11 20:15:17 +00001887 return true;
1888
1889 // If this function was instantiated from a member function of a
1890 // class template, check whether that member function was defined out-of-line.
1891 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1892 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001893 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001894 return Definition->isOutOfLine();
1895 }
1896
1897 // If this function was instantiated from a function template,
1898 // check whether that function template was defined out-of-line.
1899 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1900 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001901 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001902 return Definition->isOutOfLine();
1903 }
1904
1905 return false;
1906}
1907
Chris Lattner8a934232008-03-31 00:36:02 +00001908//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001909// FieldDecl Implementation
1910//===----------------------------------------------------------------------===//
1911
Jay Foad4ba2a172011-01-12 09:06:06 +00001912FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1913 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001914 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1915 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1916}
1917
1918bool FieldDecl::isAnonymousStructOrUnion() const {
1919 if (!isImplicit() || getDeclName())
1920 return false;
1921
1922 if (const RecordType *Record = getType()->getAs<RecordType>())
1923 return Record->getDecl()->isAnonymousStructOrUnion();
1924
1925 return false;
1926}
1927
John McCallba4f5d52011-01-20 07:57:12 +00001928unsigned FieldDecl::getFieldIndex() const {
1929 if (CachedFieldIndex) return CachedFieldIndex - 1;
1930
1931 unsigned index = 0;
1932 RecordDecl::field_iterator
1933 i = getParent()->field_begin(), e = getParent()->field_end();
1934 while (true) {
1935 assert(i != e && "failed to find field in parent!");
1936 if (*i == this)
1937 break;
1938
1939 ++i;
1940 ++index;
1941 }
1942
1943 CachedFieldIndex = index + 1;
1944 return index;
1945}
1946
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001947//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001948// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001949//===----------------------------------------------------------------------===//
1950
Douglas Gregor1693e152010-07-06 18:42:40 +00001951SourceLocation TagDecl::getOuterLocStart() const {
1952 return getTemplateOrInnerLocStart(this);
1953}
1954
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001955SourceRange TagDecl::getSourceRange() const {
1956 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001957 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001958}
1959
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001960TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001961 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001962}
1963
Douglas Gregor60e70642010-05-19 18:39:18 +00001964void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1965 TypedefDeclOrQualifier = TDD;
1966 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00001967 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001968 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00001969}
1970
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001971void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001972 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001973
1974 if (isa<CXXRecordDecl>(this)) {
1975 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1976 struct CXXRecordDecl::DefinitionData *Data =
1977 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001978 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1979 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001980 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001981}
1982
1983void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001984 assert((!isa<CXXRecordDecl>(this) ||
1985 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1986 "definition completed but not started");
1987
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001988 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001989 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00001990
1991 if (ASTMutationListener *L = getASTMutationListener())
1992 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001993}
1994
Douglas Gregor952b0172010-02-11 01:04:33 +00001995TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001996 if (isDefinition())
1997 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001998 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1999 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002000
2001 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002002 R != REnd; ++R)
2003 if (R->isDefinition())
2004 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002005
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002006 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002007}
2008
John McCallb6217662010-03-15 10:12:16 +00002009void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
2010 SourceRange QualifierRange) {
2011 if (Qualifier) {
2012 // Make sure the extended qualifier info is allocated.
2013 if (!hasExtInfo())
2014 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2015 // Set qualifier info.
2016 getExtInfo()->NNS = Qualifier;
2017 getExtInfo()->NNSRange = QualifierRange;
2018 }
2019 else {
2020 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2021 assert(QualifierRange.isInvalid());
2022 if (hasExtInfo()) {
2023 getASTContext().Deallocate(getExtInfo());
2024 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2025 }
2026 }
2027}
2028
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002029//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002030// EnumDecl Implementation
2031//===----------------------------------------------------------------------===//
2032
2033EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2034 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002035 EnumDecl *PrevDecl, bool IsScoped,
2036 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002037 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002038 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002039 C.getTypeDeclType(Enum, PrevDecl);
2040 return Enum;
2041}
2042
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002043EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002044 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002045 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002046}
2047
Douglas Gregor838db382010-02-11 01:19:42 +00002048void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002049 QualType NewPromotionType,
2050 unsigned NumPositiveBits,
2051 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002052 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002053 if (!IntegerType)
2054 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002055 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002056 setNumPositiveBits(NumPositiveBits);
2057 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002058 TagDecl::completeDefinition();
2059}
2060
2061//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002062// RecordDecl Implementation
2063//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002064
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00002065RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002066 IdentifierInfo *Id, RecordDecl *PrevDecl,
2067 SourceLocation TKL)
2068 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00002069 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002070 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002071 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002072 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002073 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002074}
2075
Jay Foad4ba2a172011-01-12 09:06:06 +00002076RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002077 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00002078 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002079
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002080 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002081 C.getTypeDeclType(R, PrevDecl);
2082 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002083}
2084
Jay Foad4ba2a172011-01-12 09:06:06 +00002085RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002086 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2087 SourceLocation());
2088}
2089
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002090bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002091 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002092 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2093}
2094
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002095RecordDecl::field_iterator RecordDecl::field_begin() const {
2096 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2097 LoadFieldsFromExternalStorage();
2098
2099 return field_iterator(decl_iterator(FirstDecl));
2100}
2101
Douglas Gregor44b43212008-12-11 16:49:14 +00002102/// completeDefinition - Notes that the definition of this type is now
2103/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00002104void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002105 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002106 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00002107}
2108
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002109void RecordDecl::LoadFieldsFromExternalStorage() const {
2110 ExternalASTSource *Source = getASTContext().getExternalSource();
2111 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2112
2113 // Notify that we have a RecordDecl doing some initialization.
2114 ExternalASTSource::Deserializing TheFields(Source);
2115
2116 llvm::SmallVector<Decl*, 64> Decls;
2117 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2118 return;
2119
2120#ifndef NDEBUG
2121 // Check that all decls we got were FieldDecls.
2122 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2123 assert(isa<FieldDecl>(Decls[i]));
2124#endif
2125
2126 LoadedFieldsFromExternalStorage = true;
2127
2128 if (Decls.empty())
2129 return;
2130
2131 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2132}
2133
Steve Naroff56ee6892008-10-08 17:01:13 +00002134//===----------------------------------------------------------------------===//
2135// BlockDecl Implementation
2136//===----------------------------------------------------------------------===//
2137
Douglas Gregor838db382010-02-11 01:19:42 +00002138void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002139 unsigned NParms) {
2140 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Steve Naroffe78b8092009-03-13 16:56:44 +00002142 // Zero params -> null pointer.
2143 if (NParms) {
2144 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002145 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002146 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2147 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2148 }
2149}
2150
John McCall6b5a61b2011-02-07 10:33:21 +00002151void BlockDecl::setCaptures(ASTContext &Context,
2152 const Capture *begin,
2153 const Capture *end,
2154 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002155 CapturesCXXThis = capturesCXXThis;
2156
2157 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002158 NumCaptures = 0;
2159 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002160 return;
2161 }
2162
John McCall6b5a61b2011-02-07 10:33:21 +00002163 NumCaptures = end - begin;
2164
2165 // Avoid new Capture[] because we don't want to provide a default
2166 // constructor.
2167 size_t allocationSize = NumCaptures * sizeof(Capture);
2168 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2169 memcpy(buffer, begin, allocationSize);
2170 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002171}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002172
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002173SourceRange BlockDecl::getSourceRange() const {
2174 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2175}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002176
2177//===----------------------------------------------------------------------===//
2178// Other Decl Allocation/Deallocation Method Implementations
2179//===----------------------------------------------------------------------===//
2180
2181TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2182 return new (C) TranslationUnitDecl(C);
2183}
2184
2185NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2186 SourceLocation L, IdentifierInfo *Id) {
2187 return new (C) NamespaceDecl(DC, L, Id);
2188}
2189
Douglas Gregor06c91932010-10-27 19:49:05 +00002190NamespaceDecl *NamespaceDecl::getNextNamespace() {
2191 return dyn_cast_or_null<NamespaceDecl>(
2192 NextNamespace.get(getASTContext().getExternalSource()));
2193}
2194
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002195ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2196 SourceLocation L, IdentifierInfo *Id, QualType T) {
2197 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2198}
2199
2200FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002201 const DeclarationNameInfo &NameInfo,
2202 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002203 StorageClass S, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002204 bool isInlineSpecified,
2205 bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002206 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor8f150942010-12-09 16:59:22 +00002207 S, SCAsWritten, isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002208 New->HasWrittenPrototype = hasWrittenPrototype;
2209 return New;
2210}
2211
2212BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2213 return new (C) BlockDecl(DC, L);
2214}
2215
2216EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2217 SourceLocation L,
2218 IdentifierInfo *Id, QualType T,
2219 Expr *E, const llvm::APSInt &V) {
2220 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2221}
2222
Benjamin Kramerd9811462010-11-21 14:11:41 +00002223IndirectFieldDecl *
2224IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2225 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2226 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002227 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2228}
2229
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002230SourceRange EnumConstantDecl::getSourceRange() const {
2231 SourceLocation End = getLocation();
2232 if (Init)
2233 End = Init->getLocEnd();
2234 return SourceRange(getLocation(), End);
2235}
2236
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002237TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2238 SourceLocation L, IdentifierInfo *Id,
2239 TypeSourceInfo *TInfo) {
2240 return new (C) TypedefDecl(DC, L, Id, TInfo);
2241}
2242
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002243FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2244 SourceLocation L,
2245 StringLiteral *Str) {
2246 return new (C) FileScopeAsmDecl(DC, L, Str);
2247}