blob: 7a35c748737473aa252ffdb288dc2b200404e409 [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) {
136 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
137 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000138 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000139 continue;
140 }
141
142 if (TemplateTemplateParmDecl *TTP
143 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000144 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000145 }
146 }
147
John McCall1fb0caa2010-10-22 21:05:15 +0000148 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000149}
150
Douglas Gregor381d34e2010-12-06 18:36:25 +0000151/// getLVForDecl - Get the linkage and visibility for the given declaration.
152static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
153
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000154/// \brief Get the most restrictive linkage for the types and
155/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +0000156static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000157 unsigned NumArgs,
158 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000159 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000160
161 for (unsigned I = 0; I != NumArgs; ++I) {
162 switch (Args[I].getKind()) {
163 case TemplateArgument::Null:
164 case TemplateArgument::Integral:
165 case TemplateArgument::Expression:
166 break;
167
168 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000169 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000170 break;
171
172 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000173 // The decl can validly be null as the representation of nullptr
174 // arguments, valid only in C++0x.
175 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor89d63e52010-12-06 18:50:56 +0000176 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
177 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000178 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000179 break;
180
181 case TemplateArgument::Template:
Douglas Gregor89d63e52010-12-06 18:50:56 +0000182 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
183 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000184 break;
185
186 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000187 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000188 Args[I].pack_size(),
189 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000190 break;
191 }
192 }
193
John McCall1fb0caa2010-10-22 21:05:15 +0000194 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000195}
196
John McCallaf146032010-10-30 11:50:40 +0000197static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000198getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
199 LVFlags &F) {
200 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000201}
202
John McCall36987482010-11-02 01:45:15 +0000203static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000204 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000205 "Not a name having namespace scope");
206 ASTContext &Context = D->getASTContext();
207
208 // C++ [basic.link]p3:
209 // A name having namespace scope (3.3.6) has internal linkage if it
210 // is the name of
211 // - an object, reference, function or function template that is
212 // explicitly declared static; or,
213 // (This bullet corresponds to C99 6.2.2p3.)
214 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
215 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000216 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000217 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000218
219 // - an object or reference that is explicitly declared const
220 // and neither explicitly declared extern nor previously
221 // declared to have external linkage; or
222 // (there is no equivalent in C99)
223 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000224 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000225 Var->getStorageClass() != SC_Extern &&
226 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000227 bool FoundExtern = false;
228 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
229 PrevVar && !FoundExtern;
230 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000231 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000232 FoundExtern = true;
233
234 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000235 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000236 }
237 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000238 // C++ [temp]p4:
239 // A non-member function template can have internal linkage; any
240 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000241 const FunctionDecl *Function = 0;
242 if (const FunctionTemplateDecl *FunTmpl
243 = dyn_cast<FunctionTemplateDecl>(D))
244 Function = FunTmpl->getTemplatedDecl();
245 else
246 Function = cast<FunctionDecl>(D);
247
248 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000249 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000250 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000251 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
252 // - a data member of an anonymous union.
253 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000254 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000255 }
256
John McCall1fb0caa2010-10-22 21:05:15 +0000257 if (D->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000258 return LinkageInfo::uniqueExternal();
John McCalle7bc9722010-10-28 04:18:25 +0000259
John McCall1fb0caa2010-10-22 21:05:15 +0000260 // Set up the defaults.
261
262 // C99 6.2.2p5:
263 // If the declaration of an identifier for an object has file
264 // scope and no storage-class specifier, its linkage is
265 // external.
John McCallaf146032010-10-30 11:50:40 +0000266 LinkageInfo LV;
267
John McCall36987482010-11-02 01:45:15 +0000268 if (F.ConsiderVisibilityAttributes) {
269 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
270 LV.setVisibility(GetVisibilityFromAttr(VA), true);
271 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000272 } else {
273 // If we're declared in a namespace with a visibility attribute,
274 // use that namespace's visibility, but don't call it explicit.
275 for (const DeclContext *DC = D->getDeclContext();
276 !isa<TranslationUnitDecl>(DC);
277 DC = DC->getParent()) {
278 if (!isa<NamespaceDecl>(DC)) continue;
279 if (const VisibilityAttr *VA =
280 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
281 LV.setVisibility(GetVisibilityFromAttr(VA), false);
282 F.ConsiderGlobalVisibility = false;
283 break;
284 }
285 }
John McCall36987482010-11-02 01:45:15 +0000286 }
John McCallaf146032010-10-30 11:50:40 +0000287 }
John McCall1fb0caa2010-10-22 21:05:15 +0000288
Douglas Gregord85b5b92009-11-25 22:24:25 +0000289 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000290
Douglas Gregord85b5b92009-11-25 22:24:25 +0000291 // A name having namespace scope has external linkage if it is the
292 // name of
293 //
294 // - an object or reference, unless it has internal linkage; or
295 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000296 // GCC applies the following optimization to variables and static
297 // data members, but not to functions:
298 //
John McCall1fb0caa2010-10-22 21:05:15 +0000299 // Modify the variable's LV by the LV of its type unless this is
300 // C or extern "C". This follows from [basic.link]p9:
301 // A type without linkage shall not be used as the type of a
302 // variable or function with external linkage unless
303 // - the entity has C language linkage, or
304 // - the entity is declared within an unnamed namespace, or
305 // - the entity is not used or is defined in the same
306 // translation unit.
307 // and [basic.link]p10:
308 // ...the types specified by all declarations referring to a
309 // given variable or function shall be identical...
310 // C does not have an equivalent rule.
311 //
John McCallac65c622010-10-26 04:59:26 +0000312 // Ignore this if we've got an explicit attribute; the user
313 // probably knows what they're doing.
314 //
John McCall1fb0caa2010-10-22 21:05:15 +0000315 // Note that we don't want to make the variable non-external
316 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000317 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000318 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
319 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000320 return LinkageInfo::uniqueExternal();
321 if (!LV.visibilityExplicit())
322 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000323 }
324
John McCall35cebc32010-11-02 18:38:13 +0000325 if (Var->getStorageClass() == SC_PrivateExtern)
326 LV.setVisibility(HiddenVisibility, true);
327
Douglas Gregord85b5b92009-11-25 22:24:25 +0000328 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000329 (Var->getStorageClass() == SC_Extern ||
330 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000331
Douglas Gregord85b5b92009-11-25 22:24:25 +0000332 // C99 6.2.2p4:
333 // For an identifier declared with the storage-class specifier
334 // extern in a scope in which a prior declaration of that
335 // identifier is visible, if the prior declaration specifies
336 // internal or external linkage, the linkage of the identifier
337 // at the later declaration is the same as the linkage
338 // specified at the prior declaration. If no prior declaration
339 // is visible, or if the prior declaration specifies no
340 // linkage, then the identifier has external linkage.
341 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000342 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000343 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
344 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000345 }
346 }
347
Douglas Gregord85b5b92009-11-25 22:24:25 +0000348 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000349 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000350 // In theory, we can modify the function's LV by the LV of its
351 // type unless it has C linkage (see comment above about variables
352 // for justification). In practice, GCC doesn't do this, so it's
353 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000354
John McCall35cebc32010-11-02 18:38:13 +0000355 if (Function->getStorageClass() == SC_PrivateExtern)
356 LV.setVisibility(HiddenVisibility, true);
357
Douglas Gregord85b5b92009-11-25 22:24:25 +0000358 // C99 6.2.2p5:
359 // If the declaration of an identifier for a function has no
360 // storage-class specifier, its linkage is determined exactly
361 // as if it were declared with the storage-class specifier
362 // extern.
363 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000364 (Function->getStorageClass() == SC_Extern ||
365 Function->getStorageClass() == SC_PrivateExtern ||
366 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000367 // C99 6.2.2p4:
368 // For an identifier declared with the storage-class specifier
369 // extern in a scope in which a prior declaration of that
370 // identifier is visible, if the prior declaration specifies
371 // internal or external linkage, the linkage of the identifier
372 // at the later declaration is the same as the linkage
373 // specified at the prior declaration. If no prior declaration
374 // is visible, or if the prior declaration specifies no
375 // linkage, then the identifier has external linkage.
376 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000377 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000378 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
379 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000380 }
381 }
382
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000383 if (FunctionTemplateSpecializationInfo *SpecInfo
384 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000385 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
386 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000387 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000388 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000389 }
390
Douglas Gregord85b5b92009-11-25 22:24:25 +0000391 // - a named class (Clause 9), or an unnamed class defined in a
392 // typedef declaration in which the class has the typedef name
393 // for linkage purposes (7.1.3); or
394 // - a named enumeration (7.2), or an unnamed enumeration
395 // defined in a typedef declaration in which the enumeration
396 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000397 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
398 // Unnamed tags have no linkage.
399 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000400 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000401
John McCall1fb0caa2010-10-22 21:05:15 +0000402 // If this is a class template specialization, consider the
403 // linkage of the template and template arguments.
404 if (const ClassTemplateSpecializationDecl *Spec
405 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000406 // From the template.
407 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
408 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000409
John McCall1fb0caa2010-10-22 21:05:15 +0000410 // The arguments at which the template was instantiated.
411 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000412 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000413 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000414
John McCallac65c622010-10-26 04:59:26 +0000415 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000416 if (F.ConsiderGlobalVisibility)
417 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000418 (Context.getLangOptions().CPlusPlus &&
419 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000420
Douglas Gregord85b5b92009-11-25 22:24:25 +0000421 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000422 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000423 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000424 if (!isExternalLinkage(EnumLV.linkage()))
425 return LinkageInfo::none();
426 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000427
428 // - a template, unless it is a function template that has
429 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000430 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000431 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000432
Douglas Gregord85b5b92009-11-25 22:24:25 +0000433 // - a namespace (7.3), unless it is declared within an unnamed
434 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000435 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
436 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000437
John McCall1fb0caa2010-10-22 21:05:15 +0000438 // By extension, we assign external linkage to Objective-C
439 // interfaces.
440 } else if (isa<ObjCInterfaceDecl>(D)) {
441 // fallout
442
443 // Everything not covered here has no linkage.
444 } else {
John McCallaf146032010-10-30 11:50:40 +0000445 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000446 }
447
448 // If we ended up with non-external linkage, visibility should
449 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000450 if (LV.linkage() != ExternalLinkage)
451 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000452
453 // If we didn't end up with hidden visibility, consider attributes
454 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000455 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000456 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000457
458 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000459}
460
John McCall36987482010-11-02 01:45:15 +0000461static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000462 // Only certain class members have linkage. Note that fields don't
463 // really have linkage, but it's convenient to say they do for the
464 // purposes of calculating linkage of pointer-to-data-member
465 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000466 if (!(isa<CXXMethodDecl>(D) ||
467 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000468 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000469 (isa<TagDecl>(D) &&
470 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000471 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000472
John McCall36987482010-11-02 01:45:15 +0000473 LinkageInfo LV;
474
475 // The flags we're going to use to compute the class's visibility.
476 LVFlags ClassF = F;
477
478 // If we have an explicit visibility attribute, merge that in.
479 if (F.ConsiderVisibilityAttributes) {
480 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
481 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
482
483 // Ignore global visibility later, but not this attribute.
484 F.ConsiderGlobalVisibility = false;
485
486 // Ignore both global visibility and attributes when computing our
487 // parent's visibility.
488 ClassF = F.onlyTemplateVisibility();
489 }
490 }
John McCallaf146032010-10-30 11:50:40 +0000491
492 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000493 // linkage.
494 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
495 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000496 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000497
498 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000499 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000500 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000501
John McCall3cdfc4d2010-08-13 08:35:10 +0000502 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000503 TemplateSpecializationKind TSK = TSK_Undeclared;
504
John McCall1fb0caa2010-10-22 21:05:15 +0000505 // If this is a method template specialization, use the linkage for
506 // the template parameters and arguments.
507 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000508 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000509 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallaf146032010-10-30 11:50:40 +0000510 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000511 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000512
513 TSK = Spec->getTemplateSpecializationKind();
514 } else if (MemberSpecializationInfo *MSI =
515 MD->getMemberSpecializationInfo()) {
516 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000517 }
518
John McCall110e8e52010-10-29 22:22:43 +0000519 // If we're paying attention to global visibility, apply
520 // -finline-visibility-hidden if this is an inline method.
521 //
John McCallaf146032010-10-30 11:50:40 +0000522 // Note that ConsiderGlobalVisibility doesn't yet have information
523 // about whether containing classes have visibility attributes,
524 // and that's intentional.
525 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000526 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000527 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
528 // InlineVisibilityHidden only applies to definitions, and
529 // isInlined() only gives meaningful answers on definitions
530 // anyway.
531 const FunctionDecl *Def = 0;
532 if (MD->hasBody(Def) && Def->isInlined())
533 LV.setVisibility(HiddenVisibility);
534 }
John McCall1fb0caa2010-10-22 21:05:15 +0000535
John McCall110e8e52010-10-29 22:22:43 +0000536 // Note that in contrast to basically every other situation, we
537 // *do* apply -fvisibility to method declarations.
538
539 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000540 if (const ClassTemplateSpecializationDecl *Spec
541 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
542 // Merge template argument/parameter information for member
543 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000544 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallaf146032010-10-30 11:50:40 +0000545 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000546 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000547 }
548
John McCall110e8e52010-10-29 22:22:43 +0000549 // Static data members.
550 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000551 // Modify the variable's linkage by its type, but ignore the
552 // type's visibility unless it's a definition.
553 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
554 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000555 LV.mergeLinkage(UniqueExternalLinkage);
556 if (!LV.visibilityExplicit())
557 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000558 }
559
John McCall36987482010-11-02 01:45:15 +0000560 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000561
562 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000563 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000564 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000565 }
566
John McCall1fb0caa2010-10-22 21:05:15 +0000567 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000568}
569
Douglas Gregor381d34e2010-12-06 18:36:25 +0000570Linkage NamedDecl::getLinkage() const {
571 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000572 assert(Linkage(CachedLinkage) ==
573 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000574 return Linkage(CachedLinkage);
575 }
576
577 CachedLinkage = getLVForDecl(this,
578 LVFlags::CreateOnlyDeclLinkage()).linkage();
579 HasCachedLinkage = 1;
580 return Linkage(CachedLinkage);
581}
582
John McCallaf146032010-10-30 11:50:40 +0000583LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000584 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000585 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000586 HasCachedLinkage = 1;
587 CachedLinkage = LI.linkage();
588 return LI;
John McCall0df95872010-10-29 00:29:13 +0000589}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000590
John McCall36987482010-11-02 01:45:15 +0000591static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000592 // Objective-C: treat all Objective-C declarations as having external
593 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000594 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000595 default:
596 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000597 case Decl::TemplateTemplateParm: // count these as external
598 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000599 case Decl::ObjCAtDefsField:
600 case Decl::ObjCCategory:
601 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000602 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000603 case Decl::ObjCForwardProtocol:
604 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000605 case Decl::ObjCMethod:
606 case Decl::ObjCProperty:
607 case Decl::ObjCPropertyImpl:
608 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000609 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000610 }
611
Douglas Gregord85b5b92009-11-25 22:24:25 +0000612 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000613 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000614 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000615
616 // C++ [basic.link]p5:
617 // In addition, a member function, static data member, a named
618 // class or enumeration of class scope, or an unnamed class or
619 // enumeration defined in a class-scope typedef declaration such
620 // that the class or enumeration has the typedef name for linkage
621 // purposes (7.1.3), has external linkage if the name of the class
622 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000623 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000624 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000625
626 // C++ [basic.link]p6:
627 // The name of a function declared in block scope and the name of
628 // an object declared by a block scope extern declaration have
629 // linkage. If there is a visible declaration of an entity with
630 // linkage having the same name and type, ignoring entities
631 // declared outside the innermost enclosing namespace scope, the
632 // block scope declaration declares that same entity and receives
633 // the linkage of the previous declaration. If there is more than
634 // one such matching entity, the program is ill-formed. Otherwise,
635 // if no matching entity is found, the block scope entity receives
636 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000637 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
638 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000639 if (Function->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000640 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000641
John McCallaf146032010-10-30 11:50:40 +0000642 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000643 if (Flags.ConsiderVisibilityAttributes) {
644 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
645 LV.setVisibility(GetVisibilityFromAttr(VA));
646 }
647
John McCall1fb0caa2010-10-22 21:05:15 +0000648 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000649 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000650 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
651 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000652 }
653
654 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000655 }
656
John McCall0df95872010-10-29 00:29:13 +0000657 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000658 if (Var->getStorageClass() == SC_Extern ||
659 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000660 if (Var->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000661 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000662
John McCallaf146032010-10-30 11:50:40 +0000663 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000664 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000665 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000666 else if (Flags.ConsiderVisibilityAttributes) {
667 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
668 LV.setVisibility(GetVisibilityFromAttr(VA));
669 }
670
John McCall1fb0caa2010-10-22 21:05:15 +0000671 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000672 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000673 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
674 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000675 }
676
677 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000678 }
679 }
680
681 // C++ [basic.link]p6:
682 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000683 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000684}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000685
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000686std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000687 return getQualifiedNameAsString(getASTContext().getLangOptions());
688}
689
690std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000691 const DeclContext *Ctx = getDeclContext();
692
693 if (Ctx->isFunctionOrMethod())
694 return getNameAsString();
695
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000696 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
697 ContextsTy Contexts;
698
699 // Collect contexts.
700 while (Ctx && isa<NamedDecl>(Ctx)) {
701 Contexts.push_back(Ctx);
702 Ctx = Ctx->getParent();
703 };
704
705 std::string QualName;
706 llvm::raw_string_ostream OS(QualName);
707
708 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
709 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000710 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000711 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000712 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
713 std::string TemplateArgsStr
714 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000715 TemplateArgs.data(),
716 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000717 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000718 OS << Spec->getName() << TemplateArgsStr;
719 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000720 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000721 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000722 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000723 OS << ND;
724 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
725 if (!RD->getIdentifier())
726 OS << "<anonymous " << RD->getKindName() << '>';
727 else
728 OS << RD;
729 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000730 const FunctionProtoType *FT = 0;
731 if (FD->hasWrittenPrototype())
732 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
733
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000734 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000735 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000736 unsigned NumParams = FD->getNumParams();
737 for (unsigned i = 0; i < NumParams; ++i) {
738 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000739 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000740 std::string Param;
741 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000742 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000743 }
744
745 if (FT->isVariadic()) {
746 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000747 OS << ", ";
748 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000749 }
750 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000751 OS << ')';
752 } else {
753 OS << cast<NamedDecl>(*I);
754 }
755 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000756 }
757
John McCall8472af42010-03-16 21:48:18 +0000758 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000759 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000760 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000761 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000762
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000763 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000764}
765
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000766bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000767 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
768
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000769 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
770 // We want to keep it, unless it nominates same namespace.
771 if (getKind() == Decl::UsingDirective) {
772 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
773 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000776 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
777 // For function declarations, we keep track of redeclarations.
778 return FD->getPreviousDeclaration() == OldD;
779
Douglas Gregore53060f2009-06-25 22:08:12 +0000780 // For function templates, the underlying function declarations are linked.
781 if (const FunctionTemplateDecl *FunctionTemplate
782 = dyn_cast<FunctionTemplateDecl>(this))
783 if (const FunctionTemplateDecl *OldFunctionTemplate
784 = dyn_cast<FunctionTemplateDecl>(OldD))
785 return FunctionTemplate->getTemplatedDecl()
786 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Steve Naroff0de21fd2009-02-22 19:35:57 +0000788 // For method declarations, we keep track of redeclarations.
789 if (isa<ObjCMethodDecl>(this))
790 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000791
John McCallf36e02d2009-10-09 21:13:30 +0000792 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
793 return true;
794
John McCall9488ea12009-11-17 05:59:44 +0000795 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
796 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
797 cast<UsingShadowDecl>(OldD)->getTargetDecl();
798
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000799 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
800 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
801 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
802
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000803 // For non-function declarations, if the declarations are of the
804 // same kind then this must be a redeclaration, or semantic analysis
805 // would not have given us the new declaration.
806 return this->getKind() == OldD->getKind();
807}
808
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000809bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000810 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000811}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000812
Anders Carlssone136e0e2009-06-26 06:29:23 +0000813NamedDecl *NamedDecl::getUnderlyingDecl() {
814 NamedDecl *ND = this;
815 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000816 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000817 ND = UD->getTargetDecl();
818 else if (ObjCCompatibleAliasDecl *AD
819 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
820 return AD->getClassInterface();
821 else
822 return ND;
823 }
824}
825
John McCall161755a2010-04-06 21:38:20 +0000826bool NamedDecl::isCXXInstanceMember() const {
827 assert(isCXXClassMember() &&
828 "checking whether non-member is instance member");
829
830 const NamedDecl *D = this;
831 if (isa<UsingShadowDecl>(D))
832 D = cast<UsingShadowDecl>(D)->getTargetDecl();
833
Francois Pichet87c2e122010-11-21 06:08:52 +0000834 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000835 return true;
836 if (isa<CXXMethodDecl>(D))
837 return cast<CXXMethodDecl>(D)->isInstance();
838 if (isa<FunctionTemplateDecl>(D))
839 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
840 ->getTemplatedDecl())->isInstance();
841 return false;
842}
843
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000844//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000845// DeclaratorDecl Implementation
846//===----------------------------------------------------------------------===//
847
Douglas Gregor1693e152010-07-06 18:42:40 +0000848template <typename DeclT>
849static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
850 if (decl->getNumTemplateParameterLists() > 0)
851 return decl->getTemplateParameterList(0)->getTemplateLoc();
852 else
853 return decl->getInnerLocStart();
854}
855
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000856SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000857 TypeSourceInfo *TSI = getTypeSourceInfo();
858 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000859 return SourceLocation();
860}
861
John McCallb6217662010-03-15 10:12:16 +0000862void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
863 SourceRange QualifierRange) {
864 if (Qualifier) {
865 // Make sure the extended decl info is allocated.
866 if (!hasExtInfo()) {
867 // Save (non-extended) type source info pointer.
868 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
869 // Allocate external info struct.
870 DeclInfo = new (getASTContext()) ExtInfo;
871 // Restore savedTInfo into (extended) decl info.
872 getExtInfo()->TInfo = savedTInfo;
873 }
874 // Set qualifier info.
875 getExtInfo()->NNS = Qualifier;
876 getExtInfo()->NNSRange = QualifierRange;
877 }
878 else {
879 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
880 assert(QualifierRange.isInvalid());
881 if (hasExtInfo()) {
882 // Save type source info pointer.
883 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
884 // Deallocate the extended decl info.
885 getASTContext().Deallocate(getExtInfo());
886 // Restore savedTInfo into (non-extended) decl info.
887 DeclInfo = savedTInfo;
888 }
889 }
890}
891
Douglas Gregor1693e152010-07-06 18:42:40 +0000892SourceLocation DeclaratorDecl::getOuterLocStart() const {
893 return getTemplateOrInnerLocStart(this);
894}
895
Abramo Bagnara9b934882010-06-12 08:15:14 +0000896void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000897QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
898 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000899 TemplateParameterList **TPLists) {
900 assert((NumTPLists == 0 || TPLists != 0) &&
901 "Empty array of template parameters with positive size!");
902 assert((NumTPLists == 0 || NNS) &&
903 "Nonempty array of template parameters with no qualifier!");
904
905 // Free previous template parameters (if any).
906 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000907 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000908 TemplParamLists = 0;
909 NumTemplParamLists = 0;
910 }
911 // Set info on matched template parameter lists (if any).
912 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000913 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000914 NumTemplParamLists = NumTPLists;
915 for (unsigned i = NumTPLists; i-- > 0; )
916 TemplParamLists[i] = TPLists[i];
917 }
918}
919
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000920//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000921// VarDecl Implementation
922//===----------------------------------------------------------------------===//
923
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000924const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
925 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000926 case SC_None: break;
927 case SC_Auto: return "auto"; break;
928 case SC_Extern: return "extern"; break;
929 case SC_PrivateExtern: return "__private_extern__"; break;
930 case SC_Register: return "register"; break;
931 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000932 }
933
934 assert(0 && "Invalid storage class");
935 return 0;
936}
937
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000938VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000939 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000940 StorageClass S, StorageClass SCAsWritten) {
941 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000942}
943
Douglas Gregor381d34e2010-12-06 18:36:25 +0000944void VarDecl::setStorageClass(StorageClass SC) {
945 assert(isLegalForVariable(SC));
946 if (getStorageClass() != SC)
947 ClearLinkageCache();
948
949 SClass = SC;
950}
951
Douglas Gregor1693e152010-07-06 18:42:40 +0000952SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000953 SourceLocation Start = getTypeSpecStartLoc();
954 if (Start.isInvalid())
955 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000956 return Start;
957}
958
959SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000960 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000961 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
962 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000963}
964
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000965bool VarDecl::isExternC() const {
966 ASTContext &Context = getASTContext();
967 if (!Context.getLangOptions().CPlusPlus)
968 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000969 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000970 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
971
972 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
973 DC = DC->getParent()) {
974 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
975 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000976 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000977
978 break;
979 }
980
981 if (DC->isFunctionOrMethod())
982 return false;
983 }
984
985 return false;
986}
987
988VarDecl *VarDecl::getCanonicalDecl() {
989 return getFirstDeclaration();
990}
991
Sebastian Redle9d12b62010-01-31 22:27:38 +0000992VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
993 // C++ [basic.def]p2:
994 // A declaration is a definition unless [...] it contains the 'extern'
995 // specifier or a linkage-specification and neither an initializer [...],
996 // it declares a static data member in a class declaration [...].
997 // C++ [temp.expl.spec]p15:
998 // An explicit specialization of a static data member of a template is a
999 // definition if the declaration includes an initializer; otherwise, it is
1000 // a declaration.
1001 if (isStaticDataMember()) {
1002 if (isOutOfLine() && (hasInit() ||
1003 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1004 return Definition;
1005 else
1006 return DeclarationOnly;
1007 }
1008 // C99 6.7p5:
1009 // A definition of an identifier is a declaration for that identifier that
1010 // [...] causes storage to be reserved for that object.
1011 // Note: that applies for all non-file-scope objects.
1012 // C99 6.9.2p1:
1013 // If the declaration of an identifier for an object has file scope and an
1014 // initializer, the declaration is an external definition for the identifier
1015 if (hasInit())
1016 return Definition;
1017 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1018 if (hasExternalStorage())
1019 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001020
John McCalld931b082010-08-26 03:08:43 +00001021 if (getStorageClassAsWritten() == SC_Extern ||
1022 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001023 for (const VarDecl *PrevVar = getPreviousDeclaration();
1024 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1025 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1026 return DeclarationOnly;
1027 }
1028 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001029 // C99 6.9.2p2:
1030 // A declaration of an object that has file scope without an initializer,
1031 // and without a storage class specifier or the scs 'static', constitutes
1032 // a tentative definition.
1033 // No such thing in C++.
1034 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1035 return TentativeDefinition;
1036
1037 // What's left is (in C, block-scope) declarations without initializers or
1038 // external storage. These are definitions.
1039 return Definition;
1040}
1041
Sebastian Redle9d12b62010-01-31 22:27:38 +00001042VarDecl *VarDecl::getActingDefinition() {
1043 DefinitionKind Kind = isThisDeclarationADefinition();
1044 if (Kind != TentativeDefinition)
1045 return 0;
1046
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001047 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001048 VarDecl *First = getFirstDeclaration();
1049 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1050 I != E; ++I) {
1051 Kind = (*I)->isThisDeclarationADefinition();
1052 if (Kind == Definition)
1053 return 0;
1054 else if (Kind == TentativeDefinition)
1055 LastTentative = *I;
1056 }
1057 return LastTentative;
1058}
1059
1060bool VarDecl::isTentativeDefinitionNow() const {
1061 DefinitionKind Kind = isThisDeclarationADefinition();
1062 if (Kind != TentativeDefinition)
1063 return false;
1064
1065 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1066 if ((*I)->isThisDeclarationADefinition() == Definition)
1067 return false;
1068 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001069 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001070}
1071
Sebastian Redl31310a22010-02-01 20:16:42 +00001072VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001073 VarDecl *First = getFirstDeclaration();
1074 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1075 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001076 if ((*I)->isThisDeclarationADefinition() == Definition)
1077 return *I;
1078 }
1079 return 0;
1080}
1081
John McCall110e8e52010-10-29 22:22:43 +00001082VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1083 DefinitionKind Kind = DeclarationOnly;
1084
1085 const VarDecl *First = getFirstDeclaration();
1086 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1087 I != E; ++I)
1088 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1089
1090 return Kind;
1091}
1092
Sebastian Redl31310a22010-02-01 20:16:42 +00001093const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001094 redecl_iterator I = redecls_begin(), E = redecls_end();
1095 while (I != E && !I->getInit())
1096 ++I;
1097
1098 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001099 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001100 return I->getInit();
1101 }
1102 return 0;
1103}
1104
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001105bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001106 if (Decl::isOutOfLine())
1107 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001108
1109 if (!isStaticDataMember())
1110 return false;
1111
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001112 // If this static data member was instantiated from a static data member of
1113 // a class template, check whether that static data member was defined
1114 // out-of-line.
1115 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1116 return VD->isOutOfLine();
1117
1118 return false;
1119}
1120
Douglas Gregor0d035142009-10-27 18:42:08 +00001121VarDecl *VarDecl::getOutOfLineDefinition() {
1122 if (!isStaticDataMember())
1123 return 0;
1124
1125 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1126 RD != RDEnd; ++RD) {
1127 if (RD->getLexicalDeclContext()->isFileContext())
1128 return *RD;
1129 }
1130
1131 return 0;
1132}
1133
Douglas Gregor838db382010-02-11 01:19:42 +00001134void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001135 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1136 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001137 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001138 }
1139
1140 Init = I;
1141}
1142
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001143VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001144 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001145 return cast<VarDecl>(MSI->getInstantiatedFrom());
1146
1147 return 0;
1148}
1149
Douglas Gregor663b5a02009-10-14 20:14:33 +00001150TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001151 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001152 return MSI->getTemplateSpecializationKind();
1153
1154 return TSK_Undeclared;
1155}
1156
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001157MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001158 return getASTContext().getInstantiatedFromStaticDataMember(this);
1159}
1160
Douglas Gregor0a897e32009-10-15 17:21:20 +00001161void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1162 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001163 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001164 assert(MSI && "Not an instantiated static data member?");
1165 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001166 if (TSK != TSK_ExplicitSpecialization &&
1167 PointOfInstantiation.isValid() &&
1168 MSI->getPointOfInstantiation().isInvalid())
1169 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001170}
1171
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001172//===----------------------------------------------------------------------===//
1173// ParmVarDecl Implementation
1174//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001175
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001176ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1177 SourceLocation L, IdentifierInfo *Id,
1178 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001179 StorageClass S, StorageClass SCAsWritten,
1180 Expr *DefArg) {
1181 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1182 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001183}
1184
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001185Expr *ParmVarDecl::getDefaultArg() {
1186 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1187 assert(!hasUninstantiatedDefaultArg() &&
1188 "Default argument is not yet instantiated!");
1189
1190 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001191 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001192 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001193
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001194 return Arg;
1195}
1196
1197unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001198 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001199 return E->getNumTemporaries();
1200
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001201 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001202}
1203
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001204CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1205 assert(getNumDefaultArgTemporaries() &&
1206 "Default arguments does not have any temporaries!");
1207
John McCall4765fa02010-12-06 08:20:24 +00001208 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001209 return E->getTemporary(i);
1210}
1211
1212SourceRange ParmVarDecl::getDefaultArgRange() const {
1213 if (const Expr *E = getInit())
1214 return E->getSourceRange();
1215
1216 if (hasUninstantiatedDefaultArg())
1217 return getUninstantiatedDefaultArg()->getSourceRange();
1218
1219 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001220}
1221
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001222//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001223// FunctionDecl Implementation
1224//===----------------------------------------------------------------------===//
1225
John McCall136a6982009-09-11 06:45:03 +00001226void FunctionDecl::getNameForDiagnostic(std::string &S,
1227 const PrintingPolicy &Policy,
1228 bool Qualified) const {
1229 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1230 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1231 if (TemplateArgs)
1232 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00001233 TemplateArgs->data(),
1234 TemplateArgs->size(),
John McCall136a6982009-09-11 06:45:03 +00001235 Policy);
1236
1237}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001238
Ted Kremenek9498d382010-04-29 16:49:01 +00001239bool FunctionDecl::isVariadic() const {
1240 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1241 return FT->isVariadic();
1242 return false;
1243}
1244
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001245bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1246 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1247 if (I->Body) {
1248 Definition = *I;
1249 return true;
1250 }
1251 }
1252
1253 return false;
1254}
1255
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001256Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001257 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1258 if (I->Body) {
1259 Definition = *I;
1260 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001261 }
1262 }
1263
1264 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001265}
1266
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001267void FunctionDecl::setBody(Stmt *B) {
1268 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001269 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001270 EndRangeLoc = B->getLocEnd();
1271}
1272
Douglas Gregor21386642010-09-28 21:55:22 +00001273void FunctionDecl::setPure(bool P) {
1274 IsPure = P;
1275 if (P)
1276 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1277 Parent->markedVirtualFunctionPure();
1278}
1279
Douglas Gregor48a83b52009-09-12 00:17:51 +00001280bool FunctionDecl::isMain() const {
1281 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001282 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001283 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001284 getIdentifier() && getIdentifier()->isStr("main");
1285}
1286
Douglas Gregor48a83b52009-09-12 00:17:51 +00001287bool FunctionDecl::isExternC() const {
1288 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001289 // In C, any non-static, non-overloadable function has external
1290 // linkage.
1291 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001292 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001293
Mike Stump1eb44332009-09-09 15:08:12 +00001294 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001295 DC = DC->getParent()) {
1296 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1297 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001298 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001299 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001300
1301 break;
1302 }
Douglas Gregor45975532010-08-17 16:09:23 +00001303
1304 if (DC->isRecord())
1305 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001306 }
1307
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001308 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001309}
1310
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001311bool FunctionDecl::isGlobal() const {
1312 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1313 return Method->isStatic();
1314
John McCalld931b082010-08-26 03:08:43 +00001315 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001316 return false;
1317
Mike Stump1eb44332009-09-09 15:08:12 +00001318 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001319 DC->isNamespace();
1320 DC = DC->getParent()) {
1321 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1322 if (!Namespace->getDeclName())
1323 return false;
1324 break;
1325 }
1326 }
1327
1328 return true;
1329}
1330
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001331void
1332FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1333 redeclarable_base::setPreviousDeclaration(PrevDecl);
1334
1335 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1336 FunctionTemplateDecl *PrevFunTmpl
1337 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1338 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1339 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1340 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001341
1342 if (PrevDecl->IsInline)
1343 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001344}
1345
1346const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1347 return getFirstDeclaration();
1348}
1349
1350FunctionDecl *FunctionDecl::getCanonicalDecl() {
1351 return getFirstDeclaration();
1352}
1353
Douglas Gregor381d34e2010-12-06 18:36:25 +00001354void FunctionDecl::setStorageClass(StorageClass SC) {
1355 assert(isLegalForFunction(SC));
1356 if (getStorageClass() != SC)
1357 ClearLinkageCache();
1358
1359 SClass = SC;
1360}
1361
Douglas Gregor3e41d602009-02-13 23:20:09 +00001362/// \brief Returns a value indicating whether this function
1363/// corresponds to a builtin function.
1364///
1365/// The function corresponds to a built-in function if it is
1366/// declared at translation scope or within an extern "C" block and
1367/// its name matches with the name of a builtin. The returned value
1368/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001369/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001370/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001371unsigned FunctionDecl::getBuiltinID() const {
1372 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001373 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1374 return 0;
1375
1376 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1377 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1378 return BuiltinID;
1379
1380 // This function has the name of a known C library
1381 // function. Determine whether it actually refers to the C library
1382 // function or whether it just has the same name.
1383
Douglas Gregor9add3172009-02-17 03:23:10 +00001384 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001385 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001386 return 0;
1387
Douglas Gregor3c385e52009-02-14 18:57:46 +00001388 // If this function is at translation-unit scope and we're not in
1389 // C++, it refers to the C library function.
1390 if (!Context.getLangOptions().CPlusPlus &&
1391 getDeclContext()->isTranslationUnit())
1392 return BuiltinID;
1393
1394 // If the function is in an extern "C" linkage specification and is
1395 // not marked "overloadable", it's the real function.
1396 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001397 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001398 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001399 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001400 return BuiltinID;
1401
1402 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001403 return 0;
1404}
1405
1406
Chris Lattner1ad9b282009-04-25 06:03:53 +00001407/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001408/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001409/// after it has been created.
1410unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001411 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001412 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001413 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001414 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Reid Spencer5f016e22007-07-11 17:01:13 +00001416}
1417
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001418void FunctionDecl::setParams(ASTContext &C,
1419 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001421 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 // Zero params -> null pointer.
1424 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001425 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001426 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001428
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001429 // Update source range. The check below allows us to set EndRangeLoc before
1430 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001431 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001432 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001433 }
1434}
1435
Chris Lattner8123a952008-04-10 02:22:51 +00001436/// getMinRequiredArguments - Returns the minimum number of arguments
1437/// needed to call this function. This may be fewer than the number of
1438/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001439/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001440unsigned FunctionDecl::getMinRequiredArguments() const {
1441 unsigned NumRequiredArgs = getNumParams();
1442 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001443 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001444 --NumRequiredArgs;
1445
1446 return NumRequiredArgs;
1447}
1448
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001449bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001450 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001451 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001452
1453 if (isa<CXXMethodDecl>(this)) {
1454 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1455 return true;
1456 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001457
1458 switch (getTemplateSpecializationKind()) {
1459 case TSK_Undeclared:
1460 case TSK_ExplicitSpecialization:
1461 return false;
1462
1463 case TSK_ImplicitInstantiation:
1464 case TSK_ExplicitInstantiationDeclaration:
1465 case TSK_ExplicitInstantiationDefinition:
1466 // Handle below.
1467 break;
1468 }
1469
1470 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001471 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001472 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001473 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001474
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001475 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001476 return PatternDecl->isInlined();
1477
1478 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001479}
1480
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001481/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001482/// definition will be externally visible.
1483///
1484/// Inline function definitions are always available for inlining optimizations.
1485/// However, depending on the language dialect, declaration specifiers, and
1486/// attributes, the definition of an inline function may or may not be
1487/// "externally" visible to other translation units in the program.
1488///
1489/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001490/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001491/// inline definition becomes externally visible (C99 6.7.4p6).
1492///
1493/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1494/// definition, we use the GNU semantics for inline, which are nearly the
1495/// opposite of C99 semantics. In particular, "inline" by itself will create
1496/// an externally visible symbol, but "extern inline" will not create an
1497/// externally visible symbol.
1498bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1499 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001500 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001501 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001502
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001503 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001504 // If it's not the case that both 'inline' and 'extern' are
1505 // specified on the definition, then this inline definition is
1506 // externally visible.
1507 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1508 return true;
1509
1510 // If any declaration is 'inline' but not 'extern', then this definition
1511 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001512 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1513 Redecl != RedeclEnd;
1514 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001515 if (Redecl->isInlineSpecified() &&
1516 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001517 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001518 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001519
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001520 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001521 }
1522
1523 // C99 6.7.4p6:
1524 // [...] If all of the file scope declarations for a function in a
1525 // translation unit include the inline function specifier without extern,
1526 // then the definition in that translation unit is an inline definition.
1527 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1528 Redecl != RedeclEnd;
1529 ++Redecl) {
1530 // Only consider file-scope declarations in this test.
1531 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1532 continue;
1533
John McCalld931b082010-08-26 03:08:43 +00001534 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001535 return true; // Not an inline definition
1536 }
1537
1538 // C99 6.7.4p6:
1539 // An inline definition does not provide an external definition for the
1540 // function, and does not forbid an external definition in another
1541 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001542 return false;
1543}
1544
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001545/// getOverloadedOperator - Which C++ overloaded operator this
1546/// function represents, if any.
1547OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001548 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1549 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001550 else
1551 return OO_None;
1552}
1553
Sean Hunta6c058d2010-01-13 09:01:02 +00001554/// getLiteralIdentifier - The literal suffix identifier this function
1555/// represents, if any.
1556const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1557 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1558 return getDeclName().getCXXLiteralIdentifier();
1559 else
1560 return 0;
1561}
1562
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001563FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1564 if (TemplateOrSpecialization.isNull())
1565 return TK_NonTemplate;
1566 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1567 return TK_FunctionTemplate;
1568 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1569 return TK_MemberSpecialization;
1570 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1571 return TK_FunctionTemplateSpecialization;
1572 if (TemplateOrSpecialization.is
1573 <DependentFunctionTemplateSpecializationInfo*>())
1574 return TK_DependentFunctionTemplateSpecialization;
1575
1576 assert(false && "Did we miss a TemplateOrSpecialization type?");
1577 return TK_NonTemplate;
1578}
1579
Douglas Gregor2db32322009-10-07 23:56:10 +00001580FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001581 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001582 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1583
1584 return 0;
1585}
1586
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001587MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1588 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1589}
1590
Douglas Gregor2db32322009-10-07 23:56:10 +00001591void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001592FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1593 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001594 TemplateSpecializationKind TSK) {
1595 assert(TemplateOrSpecialization.isNull() &&
1596 "Member function is already a specialization");
1597 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001598 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001599 TemplateOrSpecialization = Info;
1600}
1601
Douglas Gregor3b846b62009-10-27 20:53:28 +00001602bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001603 // If the function is invalid, it can't be implicitly instantiated.
1604 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001605 return false;
1606
1607 switch (getTemplateSpecializationKind()) {
1608 case TSK_Undeclared:
1609 case TSK_ExplicitSpecialization:
1610 case TSK_ExplicitInstantiationDefinition:
1611 return false;
1612
1613 case TSK_ImplicitInstantiation:
1614 return true;
1615
1616 case TSK_ExplicitInstantiationDeclaration:
1617 // Handled below.
1618 break;
1619 }
1620
1621 // Find the actual template from which we will instantiate.
1622 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001623 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001624 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001625 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001626
1627 // C++0x [temp.explicit]p9:
1628 // Except for inline functions, other explicit instantiation declarations
1629 // have the effect of suppressing the implicit instantiation of the entity
1630 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001631 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001632 return true;
1633
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001634 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001635}
1636
1637FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1638 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1639 while (Primary->getInstantiatedFromMemberTemplate()) {
1640 // If we have hit a point where the user provided a specialization of
1641 // this template, we're done looking.
1642 if (Primary->isMemberSpecialization())
1643 break;
1644
1645 Primary = Primary->getInstantiatedFromMemberTemplate();
1646 }
1647
1648 return Primary->getTemplatedDecl();
1649 }
1650
1651 return getInstantiatedFromMemberFunction();
1652}
1653
Douglas Gregor16e8be22009-06-29 17:30:29 +00001654FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001655 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001656 = TemplateOrSpecialization
1657 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001658 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001659 }
1660 return 0;
1661}
1662
1663const TemplateArgumentList *
1664FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001665 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001666 = TemplateOrSpecialization
1667 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001668 return Info->TemplateArguments;
1669 }
1670 return 0;
1671}
1672
Abramo Bagnarae03db982010-05-20 15:32:11 +00001673const TemplateArgumentListInfo *
1674FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1675 if (FunctionTemplateSpecializationInfo *Info
1676 = TemplateOrSpecialization
1677 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1678 return Info->TemplateArgumentsAsWritten;
1679 }
1680 return 0;
1681}
1682
Mike Stump1eb44332009-09-09 15:08:12 +00001683void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001684FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1685 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001686 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001687 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001688 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001689 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1690 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001691 assert(TSK != TSK_Undeclared &&
1692 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001693 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001694 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001695 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001696 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1697 TemplateArgs,
1698 TemplateArgsAsWritten,
1699 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001700 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Douglas Gregor127102b2009-06-29 20:59:39 +00001702 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001703 // function template specializations.
1704 if (InsertPos)
1705 Template->getSpecializations().InsertNode(Info, InsertPos);
1706 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001707 // Try to insert the new node. If there is an existing node, leave it, the
1708 // set will contain the canonical decls while
1709 // FunctionTemplateDecl::findSpecialization will return
1710 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001711 FunctionTemplateSpecializationInfo *Existing
1712 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001713 (void)Existing;
1714 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1715 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001716 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001717}
1718
John McCallaf2094e2010-04-08 09:05:18 +00001719void
1720FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1721 const UnresolvedSetImpl &Templates,
1722 const TemplateArgumentListInfo &TemplateArgs) {
1723 assert(TemplateOrSpecialization.isNull());
1724 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1725 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001726 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001727 void *Buffer = Context.Allocate(Size);
1728 DependentFunctionTemplateSpecializationInfo *Info =
1729 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1730 TemplateArgs);
1731 TemplateOrSpecialization = Info;
1732}
1733
1734DependentFunctionTemplateSpecializationInfo::
1735DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1736 const TemplateArgumentListInfo &TArgs)
1737 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1738
1739 d.NumTemplates = Ts.size();
1740 d.NumArgs = TArgs.size();
1741
1742 FunctionTemplateDecl **TsArray =
1743 const_cast<FunctionTemplateDecl**>(getTemplates());
1744 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1745 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1746
1747 TemplateArgumentLoc *ArgsArray =
1748 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1749 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1750 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1751}
1752
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001753TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001754 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001755 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001756 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001757 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001758 if (FTSInfo)
1759 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Douglas Gregor2db32322009-10-07 23:56:10 +00001761 MemberSpecializationInfo *MSInfo
1762 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1763 if (MSInfo)
1764 return MSInfo->getTemplateSpecializationKind();
1765
1766 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001767}
1768
Mike Stump1eb44332009-09-09 15:08:12 +00001769void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001770FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1771 SourceLocation PointOfInstantiation) {
1772 if (FunctionTemplateSpecializationInfo *FTSInfo
1773 = TemplateOrSpecialization.dyn_cast<
1774 FunctionTemplateSpecializationInfo*>()) {
1775 FTSInfo->setTemplateSpecializationKind(TSK);
1776 if (TSK != TSK_ExplicitSpecialization &&
1777 PointOfInstantiation.isValid() &&
1778 FTSInfo->getPointOfInstantiation().isInvalid())
1779 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1780 } else if (MemberSpecializationInfo *MSInfo
1781 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1782 MSInfo->setTemplateSpecializationKind(TSK);
1783 if (TSK != TSK_ExplicitSpecialization &&
1784 PointOfInstantiation.isValid() &&
1785 MSInfo->getPointOfInstantiation().isInvalid())
1786 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1787 } else
1788 assert(false && "Function cannot have a template specialization kind");
1789}
1790
1791SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001792 if (FunctionTemplateSpecializationInfo *FTSInfo
1793 = TemplateOrSpecialization.dyn_cast<
1794 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001795 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001796 else if (MemberSpecializationInfo *MSInfo
1797 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001798 return MSInfo->getPointOfInstantiation();
1799
1800 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001801}
1802
Douglas Gregor9f185072009-09-11 20:15:17 +00001803bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001804 if (Decl::isOutOfLine())
1805 return true;
1806
1807 // If this function was instantiated from a member function of a
1808 // class template, check whether that member function was defined out-of-line.
1809 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1810 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001811 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001812 return Definition->isOutOfLine();
1813 }
1814
1815 // If this function was instantiated from a function template,
1816 // check whether that function template was defined out-of-line.
1817 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1818 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001819 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001820 return Definition->isOutOfLine();
1821 }
1822
1823 return false;
1824}
1825
Chris Lattner8a934232008-03-31 00:36:02 +00001826//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001827// FieldDecl Implementation
1828//===----------------------------------------------------------------------===//
1829
1830FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1831 IdentifierInfo *Id, QualType T,
1832 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1833 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1834}
1835
1836bool FieldDecl::isAnonymousStructOrUnion() const {
1837 if (!isImplicit() || getDeclName())
1838 return false;
1839
1840 if (const RecordType *Record = getType()->getAs<RecordType>())
1841 return Record->getDecl()->isAnonymousStructOrUnion();
1842
1843 return false;
1844}
1845
1846//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001847// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001848//===----------------------------------------------------------------------===//
1849
Douglas Gregor1693e152010-07-06 18:42:40 +00001850SourceLocation TagDecl::getOuterLocStart() const {
1851 return getTemplateOrInnerLocStart(this);
1852}
1853
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001854SourceRange TagDecl::getSourceRange() const {
1855 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001856 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001857}
1858
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001859TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001860 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001861}
1862
Douglas Gregor60e70642010-05-19 18:39:18 +00001863void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1864 TypedefDeclOrQualifier = TDD;
1865 if (TypeForDecl)
1866 TypeForDecl->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001867 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00001868}
1869
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001870void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001871 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001872
1873 if (isa<CXXRecordDecl>(this)) {
1874 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1875 struct CXXRecordDecl::DefinitionData *Data =
1876 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001877 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1878 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001879 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001880}
1881
1882void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001883 assert((!isa<CXXRecordDecl>(this) ||
1884 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1885 "definition completed but not started");
1886
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001887 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001888 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00001889
1890 if (ASTMutationListener *L = getASTMutationListener())
1891 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001892}
1893
Douglas Gregor952b0172010-02-11 01:04:33 +00001894TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001895 if (isDefinition())
1896 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001897 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1898 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001899
1900 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001901 R != REnd; ++R)
1902 if (R->isDefinition())
1903 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001905 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001906}
1907
John McCallb6217662010-03-15 10:12:16 +00001908void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1909 SourceRange QualifierRange) {
1910 if (Qualifier) {
1911 // Make sure the extended qualifier info is allocated.
1912 if (!hasExtInfo())
1913 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1914 // Set qualifier info.
1915 getExtInfo()->NNS = Qualifier;
1916 getExtInfo()->NNSRange = QualifierRange;
1917 }
1918 else {
1919 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1920 assert(QualifierRange.isInvalid());
1921 if (hasExtInfo()) {
1922 getASTContext().Deallocate(getExtInfo());
1923 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1924 }
1925 }
1926}
1927
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001928//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001929// EnumDecl Implementation
1930//===----------------------------------------------------------------------===//
1931
1932EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1933 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001934 EnumDecl *PrevDecl, bool IsScoped,
1935 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001936 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001937 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001938 C.getTypeDeclType(Enum, PrevDecl);
1939 return Enum;
1940}
1941
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001942EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001943 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001944 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001945}
1946
Douglas Gregor838db382010-02-11 01:19:42 +00001947void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001948 QualType NewPromotionType,
1949 unsigned NumPositiveBits,
1950 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001951 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001952 if (!IntegerType)
1953 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001954 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001955 setNumPositiveBits(NumPositiveBits);
1956 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001957 TagDecl::completeDefinition();
1958}
1959
1960//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001961// RecordDecl Implementation
1962//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001963
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001964RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001965 IdentifierInfo *Id, RecordDecl *PrevDecl,
1966 SourceLocation TKL)
1967 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001968 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001969 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001970 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001971 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001972 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001973}
1974
1975RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001976 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001977 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001979 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001980 C.getTypeDeclType(R, PrevDecl);
1981 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001982}
1983
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001984RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1985 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1986 SourceLocation());
1987}
1988
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001989bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001990 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001991 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1992}
1993
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001994RecordDecl::field_iterator RecordDecl::field_begin() const {
1995 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1996 LoadFieldsFromExternalStorage();
1997
1998 return field_iterator(decl_iterator(FirstDecl));
1999}
2000
Douglas Gregor44b43212008-12-11 16:49:14 +00002001/// completeDefinition - Notes that the definition of this type is now
2002/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00002003void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002005 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00002006}
2007
John McCallbc365c52010-05-21 01:17:40 +00002008ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
2009 // Force the decl chain to come into existence properly.
2010 if (!getNextDeclInContext()) getParent()->decls_begin();
2011
2012 assert(isAnonymousStructOrUnion());
2013 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
2014 assert(D->getType()->isRecordType());
2015 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
2016 return D;
2017}
2018
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002019void RecordDecl::LoadFieldsFromExternalStorage() const {
2020 ExternalASTSource *Source = getASTContext().getExternalSource();
2021 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2022
2023 // Notify that we have a RecordDecl doing some initialization.
2024 ExternalASTSource::Deserializing TheFields(Source);
2025
2026 llvm::SmallVector<Decl*, 64> Decls;
2027 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2028 return;
2029
2030#ifndef NDEBUG
2031 // Check that all decls we got were FieldDecls.
2032 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2033 assert(isa<FieldDecl>(Decls[i]));
2034#endif
2035
2036 LoadedFieldsFromExternalStorage = true;
2037
2038 if (Decls.empty())
2039 return;
2040
2041 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2042}
2043
Steve Naroff56ee6892008-10-08 17:01:13 +00002044//===----------------------------------------------------------------------===//
2045// BlockDecl Implementation
2046//===----------------------------------------------------------------------===//
2047
Douglas Gregor838db382010-02-11 01:19:42 +00002048void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002049 unsigned NParms) {
2050 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002051
Steve Naroffe78b8092009-03-13 16:56:44 +00002052 // Zero params -> null pointer.
2053 if (NParms) {
2054 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002055 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002056 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2057 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2058 }
2059}
2060
2061unsigned BlockDecl::getNumParams() const {
2062 return NumParams;
2063}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002064
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002065SourceRange BlockDecl::getSourceRange() const {
2066 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2067}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002068
2069//===----------------------------------------------------------------------===//
2070// Other Decl Allocation/Deallocation Method Implementations
2071//===----------------------------------------------------------------------===//
2072
2073TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2074 return new (C) TranslationUnitDecl(C);
2075}
2076
2077NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2078 SourceLocation L, IdentifierInfo *Id) {
2079 return new (C) NamespaceDecl(DC, L, Id);
2080}
2081
Douglas Gregor06c91932010-10-27 19:49:05 +00002082NamespaceDecl *NamespaceDecl::getNextNamespace() {
2083 return dyn_cast_or_null<NamespaceDecl>(
2084 NextNamespace.get(getASTContext().getExternalSource()));
2085}
2086
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002087ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2088 SourceLocation L, IdentifierInfo *Id, QualType T) {
2089 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2090}
2091
2092FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002093 const DeclarationNameInfo &NameInfo,
2094 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002095 StorageClass S, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002096 bool isInlineSpecified,
2097 bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002098 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor8f150942010-12-09 16:59:22 +00002099 S, SCAsWritten, isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002100 New->HasWrittenPrototype = hasWrittenPrototype;
2101 return New;
2102}
2103
2104BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2105 return new (C) BlockDecl(DC, L);
2106}
2107
2108EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2109 SourceLocation L,
2110 IdentifierInfo *Id, QualType T,
2111 Expr *E, const llvm::APSInt &V) {
2112 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2113}
2114
Benjamin Kramerd9811462010-11-21 14:11:41 +00002115IndirectFieldDecl *
2116IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2117 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2118 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002119 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2120}
2121
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002122SourceRange EnumConstantDecl::getSourceRange() const {
2123 SourceLocation End = getLocation();
2124 if (Init)
2125 End = Init->getLocEnd();
2126 return SourceRange(getLocation(), End);
2127}
2128
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002129TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2130 SourceLocation L, IdentifierInfo *Id,
2131 TypeSourceInfo *TInfo) {
2132 return new (C) TypedefDecl(DC, L, Id, TInfo);
2133}
2134
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002135FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2136 SourceLocation L,
2137 StringLiteral *Str) {
2138 return new (C) FileScopeAsmDecl(DC, L, Str);
2139}